Skip to content

Instantly share code, notes, and snippets.

@nex3
Created October 27, 2008 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nex3/20047 to your computer and use it in GitHub Desktop.
Save nex3/20047 to your computer and use it in GitHub Desktop.
;;;###autoload
(defun gist-region (begin end &optional private)
"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"))
(login (github-auth-string))
(do-private (if private "-F private=1" ""))
(url-request-method "POST")
(url-request-data
(gist-make-query-string
`(("file_ext[gistfile1]" . ,(concat "." ext))
("file_name[gistfile1]" . ,name)
("file_contents[gistfile1]" . ,(buffer-substring begin end)))))
(output (url-retrieve-synchronously "http://gist.github.com/gists")))
(with-current-buffer output
(re-search-forward "^Location: \(.*\)$")
(message "Paste created: %s" (match-string 1))
(if gist-view-gist (browse-url (match-string 1)))
(kill-new (match-string 1)))
(kill-buffer output)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment