Skip to content
Newer
Older
100644 440 lines (382 sloc) 15.7 KB
bc0df7a @sigma make dependency to tabulated-list explicit (hence works only with rec…
sigma authored
1 ;;; gist.el --- Emacs integration for gist.github.com
5787e29 @chrislo Tiny changes to make compatible with ELPA
chrislo authored
2
1876f07 @defunkt add ridiculous elisp header
authored
3 ;; Author: Christian Neukirchen <purl.org/net/chneukirchen>
4 ;; Maintainer: Chris Wanstrath <chris@ozmm.org>
40b73e6 @drewr M-x delete-trailing-whitespace.
drewr authored
5 ;; Contributors:
1876f07 @defunkt add ridiculous elisp header
authored
6 ;; Will Farrington <wcfarrington@gmail.com>
7 ;; Michael Ivey
8 ;; Phil Hagelberg
58a7ce1 @defunkt whitespace, version bump
authored
9 ;; Dan McKinley
a41fc0e @sigma rebase gist.el on top of gh-gist.el
sigma authored
10 ;; Yann Hodique <yann.hodique@gmail.com>
bc0df7a @sigma make dependency to tabulated-list explicit (hence works only with rec…
sigma authored
11 ;; Version: 0.5a
5787e29 @chrislo Tiny changes to make compatible with ELPA
chrislo authored
12 ;; Created: 21 Jul 2008
13 ;; Keywords: gist git github paste pastie pastebin
e6a97d2 @sigma add ability to edit descriptions from gist-list buffer
sigma authored
14 ;; Package-Requires: ((gh "0.3.1") (tabulated-list "0"))
1876f07 @defunkt add ridiculous elisp header
authored
15
16 ;; This file is NOT part of GNU Emacs.
17
18 ;; This is free software; you can redistribute it and/or modify it under
19 ;; the terms of the GNU General Public License as published by the Free
20 ;; Software Foundation; either version 2, or (at your option) any later
21 ;; version.
22 ;;
23 ;; This is distributed in the hope that it will be useful, but WITHOUT
24 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
25 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 ;; for more details.
27 ;;
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with GNU Emacs; see the file COPYING. If not, write to the
30 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
31 ;; MA 02111-1307, USA.
32
33 ;;; Commentary:
40b73e6 @drewr M-x delete-trailing-whitespace.
drewr authored
34
1876f07 @defunkt add ridiculous elisp header
authored
35 ;; Uses your local GitHub config if it can find it.
36 ;; See http://github.com/blog/180-local-github-config
96f8522 @defunkt Initial gist.el functionality
authored
37
a41fc0e @sigma rebase gist.el on top of gh-gist.el
sigma authored
38 ;; THIS IS AN EXPERIMENTAL VERSION BASED ON gh.el LIBRARY
39 ;; see https://github.com/sigma/gh.el
40b73e6 @drewr M-x delete-trailing-whitespace.
drewr authored
40
a41fc0e @sigma rebase gist.el on top of gh-gist.el
sigma authored
41 ;;; Code:
e373824 @defunkt nex3's url.el integration
authored
42
f65ca6f @sigma fix compilation
sigma authored
43 (eval-when-compile
44 (require 'cl))
45
b171e7b @sigma use persistent cache
sigma authored
46 (require 'eieio)
47 (require 'eieio-base)
a41fc0e @sigma rebase gist.el on top of gh-gist.el
sigma authored
48 (require 'gh-gist)
bc0df7a @sigma make dependency to tabulated-list explicit (hence works only with rec…
sigma authored
49 (require 'tabulated-list)
87df390 @defunkt Remove `github-username` and `github-api-key`, add `github-user` and …
authored
50
51 (defvar gist-view-gist nil
a41fc0e @sigma rebase gist.el on top of gh-gist.el
sigma authored
52 "If non-nil, automatically use `browse-url' to view gists after
53 they're posted.")
e5aacb0 @wfarr Add support for posting gists associated via the new API key function…
wfarr authored
54
2c15870 @wfarr Use an alist for identifying the highlighting to be used online by us…
wfarr authored
55 (defvar gist-supported-modes-alist '((action-script-mode . "as")
56 (c-mode . "c")
57 (c++-mode . "cpp")
154d958 @drewr Add clojure-mode.
drewr authored
58 (clojure-mode . "clj")
88de328 @defunkt fix extensions that don't exactly match common usage
authored
59 (common-lisp-mode . "lisp")
2c15870 @wfarr Use an alist for identifying the highlighting to be used online by us…
wfarr authored
60 (css-mode . "css")
61 (diff-mode . "diff")
62 (emacs-lisp-mode . "el")
63 (erlang-mode . "erl")
64 (haskell-mode . "hs")
65 (html-mode . "html")
66 (io-mode . "io")
67 (java-mode . "java")
68 (javascript-mode . "js")
69 (jde-mode . "java")
70 (js2-mode . "js")
71 (lua-mode . "lua")
72 (ocaml-mode . "ml")
73 (objective-c-mode . "m")
f8f96a6 @toshiharu add .
toshiharu authored
74 (perl-mode . "pl")
2c15870 @wfarr Use an alist for identifying the highlighting to be used online by us…
wfarr authored
75 (php-mode . "php")
88de328 @defunkt fix extensions that don't exactly match common usage
authored
76 (python-mode . "py")
77 (ruby-mode . "rb")
2c15870 @wfarr Use an alist for identifying the highlighting to be used online by us…
wfarr authored
78 (text-mode . "txt")
817a13c @rubbish Added scala-mode to list of recognized modes
rubbish authored
79 (scala-mode . "scala")
2c15870 @wfarr Use an alist for identifying the highlighting to be used online by us…
wfarr authored
80 (sql-mode . "sql")
81 (scheme-mode . "scm")
82 (smalltalk-mode . "st")
83 (sh-mode . "sh")
84 (tcl-mode . "tcl")
85 (tex-mode . "tex")
86 (xml-mode . "xml")))
87
e6a97d2 @sigma add ability to edit descriptions from gist-list buffer
sigma authored
88 (defun gist-internal-new (files &optional private description callback)
aa4b20d @sigma multi-files gist
sigma authored
89 (let* ((api (gh-gist-api "api" :sync nil))
459534d @sigma use gh.el v0.3 stub
sigma authored
90 (gist (gh-gist-gist-stub "gist"
91 :public (not private)
92 :description (or description "")
93 :files files))
aa4b20d @sigma multi-files gist
sigma authored
94 (resp (gh-gist-new api gist)))
95 (gh-api-add-response-callback resp (or callback 'gist-created-callback))))
96
9cdbb6f @technomancy Add autoloads.
technomancy authored
97 ;;;###autoload
a41fc0e @sigma rebase gist.el on top of gh-gist.el
sigma authored
98 (defun gist-region (begin end &optional private callback)
96f8522 @defunkt Initial gist.el functionality
authored
99 "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
100 Copies the URL into the kill ring.
101
102 With a prefix argument, makes a private paste."
103 (interactive "r\nP")
660423d @mcfunley Adds a basic gist-list command that fetches the list of the user's gi…
mcfunley authored
104 (let* ((file (or (buffer-file-name) (buffer-name)))
105 (name (file-name-nondirectory file))
106 (ext (or (cdr (assoc major-mode gist-supported-modes-alist))
107 (file-name-extension file)
a41fc0e @sigma rebase gist.el on top of gh-gist.el
sigma authored
108 "txt"))
459534d @sigma use gh.el v0.3 stub
sigma authored
109 (filename (concat (file-name-sans-extension name) "." ext))
110 (files (list (gh-gist-gist-file "file"
a41fc0e @sigma rebase gist.el on top of gh-gist.el
sigma authored
111 :filename filename
aa4b20d @sigma multi-files gist
sigma authored
112 :content (buffer-substring begin end)))))
e6a97d2 @sigma add ability to edit descriptions from gist-list buffer
sigma authored
113 (gist-internal-new files private nil callback)))
aa4b20d @sigma multi-files gist
sigma authored
114
115 (defun gist-files (filenames &optional private callback)
116 (let ((files nil))
117 (dolist (f filenames)
459534d @sigma use gh.el v0.3 stub
sigma authored
118 (with-temp-buffer
aa4b20d @sigma multi-files gist
sigma authored
119 (insert-file-contents f)
120 (let ((name (file-name-nondirectory f)))
121 (push (gh-gist-gist-file name :filename name :content (buffer-string))
122 files))))
e6a97d2 @sigma add ability to edit descriptions from gist-list buffer
sigma authored
123 (gist-internal-new files private nil callback)))
a41fc0e @sigma rebase gist.el on top of gh-gist.el
sigma authored
124
125 (defun gist-created-callback (gist)
126 (let ((location (oref gist :url)))
1a01ae5 @mcfunley Replaces beginning-of-buffer call with (goto-char (point-min)), which…
mcfunley authored
127 (message "Paste created: %s" location)
87df390 @defunkt Remove `github-username` and `github-api-key`, add `github-user` and …
authored
128 (when gist-view-gist
057d016 @mcfunley fixes broken autobrowse
mcfunley authored
129 (browse-url location))
a41fc0e @sigma rebase gist.el on top of gh-gist.el
sigma authored
130 (kill-new location)))
e5aacb0 @wfarr Add support for posting gists associated via the new API key function…
wfarr authored
131
3705426 @ivey Merge branch 'master' of git://github.com/technomancy/gist.el
ivey authored
132 ;;;###autoload
602d53b @ivey Support private gists with gist-region-private and gist-buffer-private
ivey authored
133 (defun gist-region-private (begin end)
134 "Post the current region as a new private paste at gist.github.com
135 Copies the URL into the kill ring."
136 (interactive "r")
ecef93a @ivey Oops, thought I had saved the buffer.
ivey authored
137 (gist-region begin end t))
602d53b @ivey Support private gists with gist-region-private and gist-buffer-private
ivey authored
138
9cdbb6f @technomancy Add autoloads.
technomancy authored
139 ;;;###autoload
f723aaa @nex3 Allow gists to be made private using prefix arguments.
nex3 authored
140 (defun gist-buffer (&optional private)
96f8522 @defunkt Initial gist.el functionality
authored
141 "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
142 Copies the URL into the kill ring.
143
144 With a prefix argument, makes a private paste."
145 (interactive "P")
146 (gist-region (point-min) (point-max) private))
96f8522 @defunkt Initial gist.el functionality
authored
147
3705426 @ivey Merge branch 'master' of git://github.com/technomancy/gist.el
ivey authored
148 ;;;###autoload
602d53b @ivey Support private gists with gist-region-private and gist-buffer-private
ivey authored
149 (defun gist-buffer-private ()
150 "Post the current buffer as a new private paste at gist.github.com.
151 Copies the URL into the kill ring."
152 (interactive)
ecef93a @ivey Oops, thought I had saved the buffer.
ivey authored
153 (gist-region-private (point-min) (point-max)))
602d53b @ivey Support private gists with gist-region-private and gist-buffer-private
ivey authored
154
3705426 @ivey Merge branch 'master' of git://github.com/technomancy/gist.el
ivey authored
155 ;;;###autoload
f723aaa @nex3 Allow gists to be made private using prefix arguments.
nex3 authored
156 (defun gist-region-or-buffer (&optional private)
5587bee @ivey Add gist-region-or-buffer and gist-region-or-buffer-private
ivey authored
157 "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
158 Copies the URL into the kill ring.
159
160 With a prefix argument, makes a private paste."
161 (interactive "P")
5587bee @ivey Add gist-region-or-buffer and gist-region-or-buffer-private
ivey authored
162 (condition-case nil
f723aaa @nex3 Allow gists to be made private using prefix arguments.
nex3 authored
163 (gist-region (point) (mark) private)
164 (mark-inactive (gist-buffer private))))
5587bee @ivey Add gist-region-or-buffer and gist-region-or-buffer-private
ivey authored
165
3705426 @ivey Merge branch 'master' of git://github.com/technomancy/gist.el
ivey authored
166 ;;;###autoload
5587bee @ivey Add gist-region-or-buffer and gist-region-or-buffer-private
ivey authored
167 (defun gist-region-or-buffer-private ()
168 "Post either the current region, or if mark is not set, the current buffer as a new private paste at gist.github.com
169 Copies the URL into the kill ring."
170 (interactive)
171 (condition-case nil
5d117aa @defunkt use global git config for username + token
authored
172 (gist-region-private (point) (mark))
5587bee @ivey Add gist-region-or-buffer and gist-region-or-buffer-private
ivey authored
173 (mark-inactive (gist-buffer-private))))
174
9cdbb6f @technomancy Add autoloads.
technomancy authored
175 ;;;###autoload
b171e7b @sigma use persistent cache
sigma authored
176 (defun gist-list (&optional force-reload)
660423d @mcfunley Adds a basic gist-list command that fetches the list of the user's gi…
mcfunley authored
177 "Displays a list of all of the current user's gists in a new buffer."
b171e7b @sigma use persistent cache
sigma authored
178 (interactive "P")
179 (when (or force-reload
180 (> (float-time (current-time))
181 (+ (oref gist-list-cache-db :timestamp) gist-list-cache-timeout)))
182 (gist-list-cache-invalidate gist-list-cache-db))
183 (if (not (oref gist-list-cache-db :gists))
184 (progn
185 (message "Retrieving list of your gists...")
186 (let ((api (gh-gist-api "api" :sync nil)))
187 (let ((resp (gh-gist-list api)))
459534d @sigma use gh.el v0.3 stub
sigma authored
188 (gh-api-add-response-callback
b171e7b @sigma use persistent cache
sigma authored
189 resp 'gist-lists-retrieved-callback))))
190 (gist-list-cache-render gist-list-cache-db)))
a41fc0e @sigma rebase gist.el on top of gh-gist.el
sigma authored
191
8a12613 @sigma add reload key
sigma authored
192 (defun gist-list-reload ()
193 (interactive)
194 (gist-list t))
195
57ea42b @sigma use a tabulated-list-mode for rendering gists list
sigma authored
196 (defun gist-tabulated-entry (gist)
197 (let* ((data (gist-parse-gist gist))
198 (repo (car data)))
199 (list repo (apply 'vector data))))
200
a41fc0e @sigma rebase gist.el on top of gh-gist.el
sigma authored
201 (defun gist-lists-retrieved-callback (gists)
202 "Called when the list of gists has been retrieved. Displays
203 the list."
b171e7b @sigma use persistent cache
sigma authored
204 (gist-list-cache-update gist-list-cache-db gists)
205 (gist-list-cache-render gist-list-cache-db))
58a7ce1 @defunkt whitespace, version bump
authored
206
660423d @mcfunley Adds a basic gist-list command that fetches the list of the user's gi…
mcfunley authored
207 (defun gist-parse-gist (gist)
58a7ce1 @defunkt whitespace, version bump
authored
208 "Returns a list of the gist's attributes for display, given the xml list
660423d @mcfunley Adds a basic gist-list command that fetches the list of the user's gi…
mcfunley authored
209 for the gist."
a41fc0e @sigma rebase gist.el on top of gh-gist.el
sigma authored
210 (let ((repo (oref gist :id))
211 (created-at (let ((vec (timezone-parse-date (oref gist :date))))
459534d @sigma use gh.el v0.3 stub
sigma authored
212 (format "%s-%s-%s %s"
a41fc0e @sigma rebase gist.el on top of gh-gist.el
sigma authored
213 (aref vec 0) (aref vec 1) (aref vec 2) (aref vec 3))))
214 (description (or (oref gist :description) ""))
215 (public (if (eq t (oref gist :public)) "public" "private")))
660423d @mcfunley Adds a basic gist-list command that fetches the list of the user's gi…
mcfunley authored
216 (list repo created-at public description)))
217
218 ;;;###autoload
96f8522 @defunkt Initial gist.el functionality
authored
219 (defun gist-fetch (id)
bfb3bb0 @sigma code cleanup
sigma authored
220 (interactive "sGist ID: ")
ed28d0b @sigma handle multi-files gists
sigma authored
221 (let ((gist nil)
222 (multi nil)
223 (prefix (format "*gist %s*" id))
224 (result nil))
225 (dolist (g (oref gist-list-cache-db :gists))
226 (when (string= (oref g :id) id)
227 (setq gist g)))
228 (let ((api (gh-gist-api "api" :sync t)))
229 (cond ((null gist)
230 ;; fetch it
231 (setq gist (oref (gh-gist-get api id) :data))
459534d @sigma use gh.el v0.3 stub
sigma authored
232 (object-add-to-list gist-list-cache-db :gists gist))
ed28d0b @sigma handle multi-files gists
sigma authored
233 ((not (gh-gist-gist-has-files gist))
234 (gh-gist-get api gist))))
235 (let ((files (oref gist :files)))
236 (setq multi (< 1 (length files)))
237 (dolist (f files)
238 (let ((buffer (get-buffer-create (format "%s/%s" prefix
239 (oref f :filename))))
240 (mode (car (rassoc (file-name-extension (oref f :filename))
241 gist-supported-modes-alist))))
242 (with-current-buffer buffer
243 (delete-region (point-min) (point-max))
244 (insert (oref f :content))
245 (when (fboundp mode)
246 (funcall mode))
9dcd4f2 @sigma introduce basic gist minor mode
sigma authored
247 ;; set minor mode
248 (gist-mode 1)
249 (setq gist-id id
250 gist-filename (oref f :filename))
ed28d0b @sigma handle multi-files gists
sigma authored
251 (set-buffer-modified-p nil))
252 (setq result buffer))))
253 (if multi
254 (let ((ibuffer-mode-hook nil)
255 (ibuffer-use-header-line nil)
459534d @sigma use gh.el v0.3 stub
sigma authored
256 (ibuffer-show-empty-filter-groups nil))
ed28d0b @sigma handle multi-files gists
sigma authored
257 (ibuffer t prefix
258 `((name . ,(regexp-quote (concat prefix "/"))))
259 nil nil
459534d @sigma use gh.el v0.3 stub
sigma authored
260 nil
ed28d0b @sigma handle multi-files gists
sigma authored
261 '((name))))
262 (switch-to-buffer-other-window result))))
96f8522 @defunkt Initial gist.el functionality
authored
263
57ea42b @sigma use a tabulated-list-mode for rendering gists list
sigma authored
264 (defun gist-fetch-current ()
265 (interactive)
266 (gist-fetch (tabulated-list-get-id)))
267
e6a97d2 @sigma add ability to edit descriptions from gist-list buffer
sigma authored
268 (defun gist-edit-current-description ()
269 (interactive)
270 (let* ((id (tabulated-list-get-id))
271 (gist (gist-list-cache-get-gist gist-list-cache-db id))
272 (old-descr (oref gist :description))
273 (new-descr (read-from-minibuffer "Description: " old-descr)))
ec09893 @sigma allow direct edition of gist files
sigma authored
274 (let* ((g (clone gist
275 :files nil
276 :description new-descr))
e6a97d2 @sigma add ability to edit descriptions from gist-list buffer
sigma authored
277 (api (gh-gist-api "api" :sync t))
278 (resp (gh-gist-edit api g)))
279 (gh-api-add-response-callback resp
280 (lambda (gist)
281 (gist-list-reload))))))
282
57ea42b @sigma use a tabulated-list-mode for rendering gists list
sigma authored
283 (defvar gist-list-menu-mode-map
284 (let ((map (make-sparse-keymap)))
285 (set-keymap-parent map tabulated-list-mode-map)
286 (define-key map "\C-m" 'gist-fetch-current)
8a12613 @sigma add reload key
sigma authored
287 (define-key map "g" 'gist-list-reload)
e6a97d2 @sigma add ability to edit descriptions from gist-list buffer
sigma authored
288 (define-key map "e" 'gist-edit-current-description)
57ea42b @sigma use a tabulated-list-mode for rendering gists list
sigma authored
289 map))
290
291 (define-derived-mode gist-list-mode tabulated-list-mode "Gist Menu"
292 "Major mode for browsing gists.
293 \\<gist-list-menu-mode-map>
294 \\{gist-list-menu-mode-map}"
eeb0314 @sigma add a '+' marker for multi-files gists
sigma authored
295 (setq tabulated-list-format [("Id" 10 nil)
57ea42b @sigma use a tabulated-list-mode for rendering gists list
sigma authored
296 ("Created" 20 nil)
297 ("Visibility" 10 nil)
298 ("Description" 0 nil)]
299 tabulated-list-padding 2
300 tabulated-list-sort-key nil)
301 (tabulated-list-init-header)
302 (use-local-map gist-list-menu-mode-map))
303
459534d @sigma use gh.el v0.3 stub
sigma authored
304 ;;; Gist list persistent cache
305
b171e7b @sigma use persistent cache
sigma authored
306 (defclass gist-list-cache (eieio-persistent)
459534d @sigma use gh.el v0.3 stub
sigma authored
307 ((version :initarg :version
b171e7b @sigma use persistent cache
sigma authored
308 :initform 0.1
309 :type float)
310 (timestamp :initarg :timestamp
311 :initform (float-time (current-time))
312 :type float)
313 (gists :initarg :gists
314 :initform nil
315 :type list)))
316
317 (defvar gist-list-cache-file
318 (concat user-emacs-directory "var/gists.eieio"))
319
320 (defun gist-list-cache-load ()
321 (condition-case nil
322 (eieio-persistent-read gist-list-cache-file)
323 (error (gist-list-cache "Gists" :file gist-list-cache-file))))
324
325 (defun gist-list-cache-save ()
326 (eieio-persistent-save gist-list-cache-db))
327
328 (defvar gist-list-cache-db (gist-list-cache-load))
329
330 (defvar gist-list-cache-timeout 600)
331
332 (defmethod gist-list-cache-render ((cache gist-list-cache))
333 (let ((gists (oref cache :gists)))
334 (with-current-buffer (get-buffer-create "*gists*")
335 (gist-list-mode)
336 (setq tabulated-list-entries
337 (mapcar 'gist-tabulated-entry gists))
338 (tabulated-list-print)
eeb0314 @sigma add a '+' marker for multi-files gists
sigma authored
339 (gist-list-cache-tag-multi-files cache)
b171e7b @sigma use persistent cache
sigma authored
340 (set-window-buffer nil (current-buffer)))))
341
eeb0314 @sigma add a '+' marker for multi-files gists
sigma authored
342 (defmethod gist-list-cache-tag-multi-files ((cache gist-list-cache))
343 (let ((ids nil))
344 (let ((gists (oref cache :gists)))
345 (dolist (gist gists)
346 (when (< 1 (length (oref gist :files)))
347 (push (oref gist :id) ids))))
348 (save-excursion
349 (goto-char (point-min))
350 (forward-line 2)
351 (while (not (eobp))
459534d @sigma use gh.el v0.3 stub
sigma authored
352 (if (member (tabulated-list-get-id) ids)
eeb0314 @sigma add a '+' marker for multi-files gists
sigma authored
353 (tabulated-list-put-tag "+" t)
354 (forward-line 1))))))
459534d @sigma use gh.el v0.3 stub
sigma authored
355
e6a97d2 @sigma add ability to edit descriptions from gist-list buffer
sigma authored
356 (defmethod gist-list-cache-get-gist ((cache gist-list-cache) id)
357 (let ((gists (oref cache :gists)))
358 (loop for gist in gists if (string= (oref gist :id) id)
359 return gist)))
360
b171e7b @sigma use persistent cache
sigma authored
361 (defmethod gist-list-cache-invalidate ((cache gist-list-cache))
362 (oset cache :gists nil))
363
364 (defmethod gist-list-cache-update ((cache gist-list-cache) gists)
365 (oset cache :gists gists)
366 (oset cache :timestamp (float-time (current-time)))
367 (gist-list-cache-save))
368
459534d @sigma use gh.el v0.3 stub
sigma authored
369 ;;; Gist minor mode
370
9dcd4f2 @sigma introduce basic gist minor mode
sigma authored
371 (defvar gist-id nil)
372 (defvar gist-filename nil)
373
ec09893 @sigma allow direct edition of gist files
sigma authored
374 (defun gist-mode-edit-buffer (&optional new-name)
375 (when (or (buffer-modified-p) new-name)
376 (let* ((id gist-id)
377 (gist (gist-list-cache-get-gist gist-list-cache-db id))
378 (files (list
379 (make-instance 'gh-gist-gist-file
380 :filename (or new-name gist-filename)
381 :content (buffer-string)))))
382 (when new-name
383 ;; remove old file as well
384 (add-to-list 'files
385 (make-instance 'gh-gist-gist-file
386 :filename gist-filename
387 :content nil)))
388 (let* ((g (clone gist
389 :files files))
390 (api (gh-gist-api "api" :sync t))
391 (resp (gh-gist-edit api g)))
392 (gh-api-add-response-callback
393 resp
394 (lambda (gist)
395 (set-buffer-modified-p nil)
396 (when new-name
397 (rename-buffer (replace-regexp-in-string "/.*$"
398 (concat "/" new-name)
399 (buffer-name)))
400 (setq gist-filename new-name))
401 (let ((g (gist-list-cache-get-gist
402 gist-list-cache-db
403 (oref gist :id))))
404 (oset g :files (oref gist :files)))))))))
405
406 (defun gist-mode-save-buffer ()
407 (interactive)
408 (gist-mode-edit-buffer))
409
410 (defun gist-mode-write-file ()
411 (interactive)
412 (let ((new-name (read-from-minibuffer "File name: " gist-filename)))
413 (gist-mode-edit-buffer new-name)))
414
415 (defvar gist-mode-map
416 (let ((map (make-sparse-keymap)))
417 (define-key map [remap save-buffer] 'gist-mode-save-buffer)
418 (define-key map [remap write-file] 'gist-mode-write-file)
419 map))
420
9dcd4f2 @sigma introduce basic gist minor mode
sigma authored
421 (define-minor-mode gist-mode
422 "Minor mode for buffers containing gists files"
423 :lighter " gist"
ec09893 @sigma allow direct edition of gist files
sigma authored
424 :map 'gist-mode-map
9dcd4f2 @sigma introduce basic gist minor mode
sigma authored
425 (make-local-variable 'gist-id)
426 (make-local-variable 'gist-filename))
427
5978566 @sigma fix dired integration
sigma authored
428 ;;; Dired integration
429
430 (require 'dired)
aa4b20d @sigma multi-files gist
sigma authored
431
432 (defun dired-do-gist (&optional private)
433 (interactive "P")
434 (gist-files (dired-get-marked-files) private))
435
5978566 @sigma fix dired integration
sigma authored
436 (define-key dired-mode-map "@" 'dired-do-gist)
437
96f8522 @defunkt Initial gist.el functionality
authored
438 (provide 'gist)
6aa8b00 @oyvindio Define github-with-auth-info before it is used.
oyvindio authored
439 ;;; gist.el ends here.
Something went wrong with that request. Please try again.