Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

nex3's url.el integration

  • Loading branch information...
commit e373824c9c8ecf41fbf0af3fe8848dd1135eae02 1 parent f723aaa
@defunkt defunkt authored
Showing with 47 additions and 52 deletions.
  1. +47 −52 gist.el
View
99 gist.el
@@ -1,8 +1,3 @@
-;;; gist.el --- Emacs integration for gist.github.com
-
-;; Copyright (C) 2008 Christian Neukirchen <purl.org/net/chneukirchen>
-
-;; Version: 0.3.1
;; Keywords: gist git github paste pastie pastebin
;; Created: 21 Jul 2008
;; Author: Christian Neukirchen <purl.org/net/chneukirchen>
@@ -38,6 +33,8 @@
;;; Code:
+(eval-when-compile (require 'cl))
+
(defvar github-username "")
(defvar github-api-key "")
@@ -82,30 +79,38 @@ 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"))
- (output (generate-new-buffer " *gist*"))
- (login (github-auth-string))
- (do-private (if private "-F private=1" "")))
- (shell-command-on-region
- begin end
- (format (concat "curl -sS "
- "%s "
- "-F 'file_ext[gistfile1]=.%s' "
- "-F 'file_name[gistfile1]=%s' "
- "-F 'file_contents[gistfile1]=<-' "
- "%s "
- "http://gist.github.com/gists") login ext name do-private)
- output)
- (with-current-buffer output
- (re-search-backward "href=\"\\(.*\\)\"")
- (message "Paste created: %s" (match-string 1))
- (if gist-view-gist (browse-url (match-string 1)))
- (kill-new (match-string 1)))
- (kill-buffer output)))
+ (destructuring-bind (login . token) (github-auth-info)
+ (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"))
+ (url-max-redirections 0)
+ (url-request-method "POST")
+ (url-request-data
+ (gist-make-query-string
+ `(,@(if private '(("private" . "1")))
+ ("login" . ,login)
+ ("token" . ,token)
+ ("file_ext[gistfile1]" . ,(concat "." ext))
+ ("file_name[gistfile1]" . ,name)
+ ("file_contents[gistfile1]" . ,(buffer-substring begin end))))))
+ (with-current-buffer (url-retrieve-synchronously "http://gist.github.com/gists")
+ (re-search-backward "^Location: \\(.*\\)$")
+ (message "Paste created: %s" (match-string 1))
+ (if gist-view-gist (browse-url (match-string 1)))
+ (kill-new (match-string 1))
+ (kill-buffer (current-buffer))))))
+
+(defun gist-make-query-string (params)
+ "Returns a query string constructed from PARAMS, which should be
+a list with elements of the form (KEY . VALUE). KEY and VALUE
+should both be strings."
+ (mapconcat
+ (lambda (param)
+ (concat (url-hexify-string (car param)) "="
+ (url-hexify-string (cdr param))))
+ params "&"))
;;;###autoload
(defun gist-region-private (begin end)
@@ -114,11 +119,6 @@ Copies the URL into the kill ring."
(interactive "r")
(gist-region begin end t))
-(defun github-raw-auth-string (user token)
- "Given a username and API token, returns a curl-friendly string."
- (if (and (> (length user) 0) (> (length token) 0))
- (format "-F 'login=%s' -F 'token=%s'" user token)))
-
(defun github-config (key)
"Returns a GitHub specific value from the global Git config."
(let ((strip (lambda (string)
@@ -131,30 +131,25 @@ Copies the URL into the kill ring."
"Sets a GitHub specific value to the global Git config."
(shell-command-to-string (format "git config --global github.%s %s" key value)))
-(defun github-auth-string ()
- "Returns a curl-friendly GitHub auth string.
-Searches for a GitHub username and token in the global git config.
-If nothing is found, prompts for the info then sets it to the git config."
+(defun github-auth-info ()
+ "Returns the user's GitHub authorization information.
+Searches for a GitHub username and token in the global git config,
+and returns (USERNAME . TOKEN). If nothing is found, prompts
+for the info then sets it to the git config."
(interactive)
(let* ((user (github-config "user"))
- (token (github-config "token"))
- (auth-string (github-raw-auth-string user token)))
-
- (if (> (length auth-string) 0)
- auth-string
+ (token (github-config "token")))
- (cond
- ((not user)
- (setq user (read-string "GitHub username: "))
- (github-set-config "user" user)))
+ (when (not user)
+ (setq user (read-string "GitHub username: "))
+ (github-set-config "user" user))
- (cond
- ((not token)
- (setq token (read-string "GitHub API token: "))
- (github-set-config "token" token)))
+ (when (not token)
+ (setq token (read-string "GitHub API token: "))
+ (github-set-config "token" token))
- (github-raw-auth-string user token))))
+ (cons user token)))
;;;###autoload
(defun gist-buffer (&optional private)
Please sign in to comment.
Something went wrong with that request. Please try again.