Skip to content
Newer
Older
100644 216 lines (184 sloc) 8.07 KB
1876f07 @defunkt add ridiculous elisp header
authored
1 ;; Keywords: gist git github paste pastie pastebin
2 ;; Created: 21 Jul 2008
3 ;; Author: Christian Neukirchen <purl.org/net/chneukirchen>
4 ;; Maintainer: Chris Wanstrath <chris@ozmm.org>
5 ;; Contributors:
6 ;; Will Farrington <wcfarrington@gmail.com>
7 ;; Michael Ivey
8 ;; Phil Hagelberg
9
10 ;; This file is NOT part of GNU Emacs.
11
12 ;; This is free software; you can redistribute it and/or modify it under
13 ;; the terms of the GNU General Public License as published by the Free
14 ;; Software Foundation; either version 2, or (at your option) any later
15 ;; version.
16 ;;
17 ;; This is distributed in the hope that it will be useful, but WITHOUT
18 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 ;; for more details.
21 ;;
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 ;; MA 02111-1307, USA.
26
27
28 ;;; Commentary:
29
30 ;; Uses your local GitHub config if it can find it.
31 ;; See http://github.com/blog/180-local-github-config
96f8522 @defunkt Initial gist.el functionality
authored
32
33
1876f07 @defunkt add ridiculous elisp header
authored
34 ;;; Code:
35
e373824 @defunkt nex3's url.el integration
authored
36 (eval-when-compile (require 'cl))
37
e5aacb0 @wfarr Add support for posting gists associated via the new API key function…
wfarr authored
38 (defvar github-username "")
39 (defvar github-api-key "")
40
2c15870 @wfarr Use an alist for identifying the highlighting to be used online by us…
wfarr authored
41 (defvar gist-supported-modes-alist '((action-script-mode . "as")
42 (c-mode . "c")
43 (c++-mode . "cpp")
44 (common-lisp-mode . "el")
45 (css-mode . "css")
46 (diff-mode . "diff")
47 (emacs-lisp-mode . "el")
48 (erlang-mode . "erl")
49 (haskell-mode . "hs")
50 (html-mode . "html")
51 (io-mode . "io")
52 (java-mode . "java")
53 (javascript-mode . "js")
54 (jde-mode . "java")
55 (js2-mode . "js")
56 (lua-mode . "lua")
57 (ocaml-mode . "ml")
58 (objective-c-mode . "m")
59 (perl-mode "pl")
60 (php-mode . "php")
61 (python-mode . "sc")
62 (ruby-mode . "rbx")
63 (text-mode . "txt")
64 (sql-mode . "sql")
65 (scheme-mode . "scm")
66 (smalltalk-mode . "st")
67 (sh-mode . "sh")
68 (tcl-mode . "tcl")
69 (tex-mode . "tex")
70 (xml-mode . "xml")))
71
9c63d81 @nex3 Add an option to automatically open new gists in the browser.
nex3 authored
72 (defvar gist-view-gist nil
73 "If non-nil, automatically use `browse-url' to view gists after they're posted.")
74
9cdbb6f @technomancy Add autoloads.
technomancy authored
75 ;;;###autoload
602d53b @ivey Support private gists with gist-region-private and gist-buffer-private
ivey authored
76 (defun gist-region (begin end &optional private)
96f8522 @defunkt Initial gist.el functionality
authored
77 "Post the current region as a new paste at gist.github.com
f723aaa @nex3 Allow gists to be made private using prefix arguments.
nex3 authored
78 Copies the URL into the kill ring.
79
80 With a prefix argument, makes a private paste."
81 (interactive "r\nP")
e373824 @defunkt nex3's url.el integration
authored
82 (destructuring-bind (login . token) (github-auth-info)
83 (let* ((file (or (buffer-file-name) (buffer-name)))
84 (name (file-name-nondirectory file))
85 (ext (or (cdr (assoc major-mode gist-supported-modes-alist))
86 (file-name-extension file)
87 "txt"))
88 (url-max-redirections 0)
89 (url-request-method "POST")
90 (url-request-data
91 (gist-make-query-string
92 `(,@(if private '(("private" . "1")))
93 ("login" . ,login)
94 ("token" . ,token)
95 ("file_ext[gistfile1]" . ,(concat "." ext))
96 ("file_name[gistfile1]" . ,name)
97 ("file_contents[gistfile1]" . ,(buffer-substring begin end))))))
98 (with-current-buffer (url-retrieve-synchronously "http://gist.github.com/gists")
99 (re-search-backward "^Location: \\(.*\\)$")
100 (message "Paste created: %s" (match-string 1))
101 (if gist-view-gist (browse-url (match-string 1)))
102 (kill-new (match-string 1))
103 (kill-buffer (current-buffer))))))
104
105 (defun gist-make-query-string (params)
106 "Returns a query string constructed from PARAMS, which should be
107 a list with elements of the form (KEY . VALUE). KEY and VALUE
108 should both be strings."
109 (mapconcat
110 (lambda (param)
111 (concat (url-hexify-string (car param)) "="
112 (url-hexify-string (cdr param))))
113 params "&"))
e5aacb0 @wfarr Add support for posting gists associated via the new API key function…
wfarr authored
114
3705426 @ivey Merge branch 'master' of git://github.com/technomancy/gist.el
ivey authored
115 ;;;###autoload
602d53b @ivey Support private gists with gist-region-private and gist-buffer-private
ivey authored
116 (defun gist-region-private (begin end)
117 "Post the current region as a new private paste at gist.github.com
118 Copies the URL into the kill ring."
119 (interactive "r")
ecef93a @ivey Oops, thought I had saved the buffer.
ivey authored
120 (gist-region begin end t))
602d53b @ivey Support private gists with gist-region-private and gist-buffer-private
ivey authored
121
5d117aa @defunkt use global git config for username + token
authored
122 (defun github-config (key)
123 "Returns a GitHub specific value from the global Git config."
124 (let ((strip (lambda (string)
125 (if (> (length string) 0)
126 (substring string 0 (- (length string) 1))))))
127 (funcall strip (shell-command-to-string
128 (concat "git config --global github." key)))))
129
130 (defun github-set-config (key value)
131 "Sets a GitHub specific value to the global Git config."
132 (shell-command-to-string (format "git config --global github.%s %s" key value)))
133
e373824 @defunkt nex3's url.el integration
authored
134 (defun github-auth-info ()
135 "Returns the user's GitHub authorization information.
136 Searches for a GitHub username and token in the global git config,
137 and returns (USERNAME . TOKEN). If nothing is found, prompts
138 for the info then sets it to the git config."
e5aacb0 @wfarr Add support for posting gists associated via the new API key function…
wfarr authored
139 (interactive)
5d117aa @defunkt use global git config for username + token
authored
140
141 (let* ((user (github-config "user"))
e373824 @defunkt nex3's url.el integration
authored
142 (token (github-config "token")))
5d117aa @defunkt use global git config for username + token
authored
143
e373824 @defunkt nex3's url.el integration
authored
144 (when (not user)
145 (setq user (read-string "GitHub username: "))
146 (github-set-config "user" user))
5d117aa @defunkt use global git config for username + token
authored
147
e373824 @defunkt nex3's url.el integration
authored
148 (when (not token)
149 (setq token (read-string "GitHub API token: "))
150 (github-set-config "token" token))
5d117aa @defunkt use global git config for username + token
authored
151
e373824 @defunkt nex3's url.el integration
authored
152 (cons user token)))
96f8522 @defunkt Initial gist.el functionality
authored
153
9cdbb6f @technomancy Add autoloads.
technomancy authored
154 ;;;###autoload
f723aaa @nex3 Allow gists to be made private using prefix arguments.
nex3 authored
155 (defun gist-buffer (&optional private)
96f8522 @defunkt Initial gist.el functionality
authored
156 "Post the current buffer as a new paste at gist.github.com.
f723aaa @nex3 Allow gists to be made private using prefix arguments.
nex3 authored
157 Copies the URL into the kill ring.
158
159 With a prefix argument, makes a private paste."
160 (interactive "P")
161 (gist-region (point-min) (point-max) private))
96f8522 @defunkt Initial gist.el functionality
authored
162
3705426 @ivey Merge branch 'master' of git://github.com/technomancy/gist.el
ivey authored
163 ;;;###autoload
602d53b @ivey Support private gists with gist-region-private and gist-buffer-private
ivey authored
164 (defun gist-buffer-private ()
165 "Post the current buffer as a new private paste at gist.github.com.
166 Copies the URL into the kill ring."
167 (interactive)
ecef93a @ivey Oops, thought I had saved the buffer.
ivey authored
168 (gist-region-private (point-min) (point-max)))
602d53b @ivey Support private gists with gist-region-private and gist-buffer-private
ivey authored
169
3705426 @ivey Merge branch 'master' of git://github.com/technomancy/gist.el
ivey authored
170 ;;;###autoload
f723aaa @nex3 Allow gists to be made private using prefix arguments.
nex3 authored
171 (defun gist-region-or-buffer (&optional private)
5587bee @ivey Add gist-region-or-buffer and gist-region-or-buffer-private
ivey authored
172 "Post either the current region, or if mark is not set, the current buffer as a new paste at gist.github.com
f723aaa @nex3 Allow gists to be made private using prefix arguments.
nex3 authored
173 Copies the URL into the kill ring.
174
175 With a prefix argument, makes a private paste."
176 (interactive "P")
5587bee @ivey Add gist-region-or-buffer and gist-region-or-buffer-private
ivey authored
177 (condition-case nil
f723aaa @nex3 Allow gists to be made private using prefix arguments.
nex3 authored
178 (gist-region (point) (mark) private)
179 (mark-inactive (gist-buffer private))))
5587bee @ivey Add gist-region-or-buffer and gist-region-or-buffer-private
ivey authored
180
3705426 @ivey Merge branch 'master' of git://github.com/technomancy/gist.el
ivey authored
181 ;;;###autoload
5587bee @ivey Add gist-region-or-buffer and gist-region-or-buffer-private
ivey authored
182 (defun gist-region-or-buffer-private ()
183 "Post either the current region, or if mark is not set, the current buffer as a new private paste at gist.github.com
184 Copies the URL into the kill ring."
185 (interactive)
186 (condition-case nil
5d117aa @defunkt use global git config for username + token
authored
187 (gist-region-private (point) (mark))
5587bee @ivey Add gist-region-or-buffer and gist-region-or-buffer-private
ivey authored
188 (mark-inactive (gist-buffer-private))))
189
96f8522 @defunkt Initial gist.el functionality
authored
190 (defvar gist-fetch-url "http://gist.github.com/%d.txt"
191 "Raw Gist content URL format")
192
9cdbb6f @technomancy Add autoloads.
technomancy authored
193 ;;;###autoload
96f8522 @defunkt Initial gist.el functionality
authored
194 (defun gist-fetch (id)
195 "Fetches a Gist and inserts it into a new buffer
196 If the Gist already exists in a buffer, switches to it"
197 (interactive "nGist ID: ")
198
5d117aa @defunkt use global git config for username + token
authored
199 (let* ((gist-buffer-name (format "*gist %d*" id))
96f8522 @defunkt Initial gist.el functionality
authored
200 (gist-buffer (get-buffer gist-buffer-name)))
201 (if (bufferp gist-buffer)
202 (switch-to-buffer-other-window gist-buffer)
203 (progn
204 (message "Fetching Gist %d..." id)
5d117aa @defunkt use global git config for username + token
authored
205 (setq gist-buffer
96f8522 @defunkt Initial gist.el functionality
authored
206 (url-retrieve-synchronously (format gist-fetch-url id)))
5d117aa @defunkt use global git config for username + token
authored
207 (with-current-buffer gist-buffer
96f8522 @defunkt Initial gist.el functionality
authored
208 (rename-buffer gist-buffer-name t)
209 (beginning-of-buffer)
210 (search-forward-regexp "\n\n")
211 (delete-region (point-min) (point))
212 (set-buffer-modified-p nil))
213 (switch-to-buffer-other-window gist-buffer)))))
214
215 (provide 'gist)
216 ;;; gist.el ends here.
Something went wrong with that request. Please try again.