Last active
March 8, 2024 20:34
-
-
Save Gavinok/6974ea0b0c162c2202ed6332e3e74880 to your computer and use it in GitHub Desktop.
Elisp code for quickly creating a github gist from a given region
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 ((select-enable-clipboard t) | |
(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