Skip to content

Instantly share code, notes, and snippets.

@Gavinok
Created March 8, 2024 20:32
Show Gist options
  • Save Gavinok/a2c8d3d2c2b6ebed4d3040168e26b343 to your computer and use it in GitHub Desktop.
Save Gavinok/a2c8d3d2c2b6ebed4d3040168e26b343 to your computer and use it in GitHub Desktop.
Elisp code for quickly creating a github gist from a given region
(defun gist-from-region (BEG END fname desc &optional private)
"Collect the current region creating a github gist with the
filename FNAME and description DESC.
If the optional argument PRIVATE is non-nil then the gist will be
made private. Otherwise the gist will be default to public.
Depends on the `gh' commandline tool"
(interactive (list (mark) (point)
(read-string "File Name: ")
(read-string "Description: ")
current-prefix-arg))
(let* ((extra-args (unless private '("--public")))
(command `("gh" "gist" "create"
"--filename" ,fname
"--desc" ,desc
,@extra-args
"-"))
(proc (make-process :name "Gist Creation"
:buffer "*Gist URL*"
:command command
:sentinel (lambda (process event)
"Listens for process finish and prints the gist's URL"
(unless (process-live-p process )
(if (string-match "finis.*" event)
(let ((url (with-current-buffer (process-buffer process)
(goto-char (point-max))
(thing-at-point 'line))))
(message "Gist for file %s created at %s (copied to clipboard)"
fname
(with-current-buffer (process-buffer process)
(goto-char (point-max))
(thing-at-point 'line)))
(kill-new url))
(switch-to-buffer "*Gist URL*")))))))
(message "Creating Gist")
(process-send-region proc BEG END)
(process-send-eof proc)
(process-send-eof proc)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment