首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gist (gist.el / Emacs) -在创建时设置“描述”

Gist (gist.el / Emacs) -在创建时设置“描述”
EN

Stack Overflow用户
提问于 2014-04-09 18:58:45
回答 1查看 133关注 0票数 4

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

我希望能够在创建gist的同时设置description,而无需切换到gist-list缓冲区。默认为buffer-name的小型缓冲区提示将是处理此问题的首选方法。如何以编程方式完成这一任务?

下面是gist.el中负责上述行为的两个主要函数:

代码语言:javascript
复制
(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))))))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-09 20:58:34

看一看gist-region正在调用的函数gist-region

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

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

代码语言:javascript
复制
(gist-internal-new files private (read-from-minibuffer "Gist Description: ") callback)

或者更好的是,不要修改函数,只编写自己的函数!这样,您就不会与使用该函数的其他包混为一谈。

这里只是一个稍微修改过的版本,它添加了一个额外的参数,并使用interactive自动提示用户提供一个描述,同时仍然允许前缀args生成私有文档。

与我们使用“从小型缓冲区读取”的第一个示例不同,这个示例允许您在代码中使用函数并直接指定描述,而不强制使用提示符,除非它是交互调用的。

代码语言:javascript
复制
;; 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)))
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22971635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档