Gist(gist.el / Emacs) - 在创建时设置`description`

时间:2014-04-09 18:58:45

标签: emacs elisp

gist-region的默认行为是将描述留空。要设置说明,必须切换到gist-list缓冲区,然后使用函数gist-edit-current-description设置说明

我希望能够在创建要点的同时设置描述,而无需切换到gist-list缓冲区。默认为buffer-name的迷你缓冲区提示符是处理此问题的首选方法。如何以编程方式完成?

以下是gist.el中负责上述行为的两个主要功能:

  
(defun gist-region (begin end &optional private callback)
  "Post the current region as a new paste at gist.github.com
Copies the URL into the kill ring.

With a prefix argument, makes a private paste."
  (interactive "r\nP")
  (let* ((file (or (buffer-file-name) (buffer-name)))
         (name (file-name-nondirectory file))
         (ext (or (cdr (assoc major-mode gist-supported-modes-alist))
                  (file-name-extension file)
                  "txt"))
         (fname (concat (file-name-sans-extension name) "." ext))
         (files (list
                 (gh-gist-gist-file "file"
                                    :filename fname
                                    :content (buffer-substring begin end)))))
    (gist-internal-new files private nil callback)))

(defun gist-edit-current-description ()
  (interactive)
  (let* ((id (tabulated-list-get-id))
         (gist (gist-list-db-get-gist id))
         (old-descr (oref gist :description))
         (new-descr (read-from-minibuffer "Description: " old-descr)))
    (let* ((g (clone gist
                     :files nil
                     :description new-descr))
           (api (gist-get-api t))
           (resp (gh-gist-edit api g)))
      (gh-url-add-response-callback resp
                                    (lambda (gist)
                                      (gist-list-reload))))))

1 个答案:

答案 0 :(得分:4)

查看gist-internal-new正在调用的函数gist-region

(gist-internal-new FILES &optional PRIVATE DESCRIPTION CALLBACK)

第三个参数是描述,但gist-region将其设置为零。您可以修改gist-region中的最后一个表达式,以从迷你缓冲区读取字符串以指定说明

(gist-internal-new files private (read-from-minibuffer "Gist Description: ") callback)

或者更好的是,不要修改功能,只需编写自己的功能!这样,您就不会使用其他使用该功能的软件包。

这是一个稍微修改过的版本,它添加了一个额外的参数,并使用interactive自动提示用户输入描述,同时仍然允许前缀args创建私有要点。

与我们使用`read-from-minibuffer'这个允许你在代码中使用函数并直接指定描述而不强制使用提示,除非它以交互方式调用。

;; note that we added the DESCRIPTION argument
(defun gist-region-with-description (begin end &optional description private callback)
  "Post the current region as a new paste at gist.github.com
Copies the URL into the kill ring.

With a prefix argument, makes a private paste."
  (interactive "r\nsGist Description: \nP") ;; we handle the prompt here!
  (let* ((file (or (buffer-file-name) (buffer-name)))
         (name (file-name-nondirectory file))
         (ext (or (cdr (assoc major-mode gist-supported-modes-alist))
                  (file-name-extension file)
                  "txt"))
         (fname (concat (file-name-sans-extension name) "." ext))
         (files (list
                 (gh-gist-gist-file "file"
                                    :filename fname
                                    :content (buffer-substring begin end)))))
    ;; finally we use our new arg to specify the description in the internal call
    (gist-internal-new files private description callback)))