Search code examples
emacselisp

How to edit a Gist filename using `gist.el` with Emacs


Can anyone please steer me in the right direction regarding how to edit the filename of an existing Gist using gist.el. I've tried modifying gist-edit-current-description to handle filename modification, but my attempted variations haven't worked. Here is the gist-edit-current-description function -- I assume editing the filename would be something similar to the description:

(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))))))

This may help give someone some ideas -- it is a method of setting the filename at the time of Gist creation (and it is based on a prior answer by @Jordon Biondo -- https://stackoverflow.com/a/22973794/2112489 ):

(defun gist-region-with-filename-description (begin end &optional filename 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 (if filename filename (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)))

(defun gist-buffer-with-filename-description (&optional filename description private)
  "Post the current buffer as a new paste at gist.github.com.
Copies the URL into the kill ring.
With a prefix argument, makes a private paste."
  (interactive "P")
  (let* (
      (filename (if filename filename (read-string "Filename:  " (buffer-name))))
      (description (if description description (read-string "Description:  " (buffer-name)))))
    (gist-region-with-filename-description (point-min) (point-max) filename description private nil)))

Solution

  • The developer added this feature on December 29, 2014 -- the function is gist-mode-write-file, which is used when visiting the file. Issue number 55: https://github.com/defunkt/gist.el/issues/55