Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 290 lines (257 sloc) 10.956 kb
1b6dc56c »
2009-11-30 initial release
1 ;; codepad.el --- Emacs integration for codepad.org
2 ;;
3 ;; Author: Rüdiger Sonderfeld <ruediger@c-plusplus.de>
afeb25ca »
2009-11-30 added Thomas to the Contributor list
4 ;; Contributors: Thomas Weidner <thomas001le@gmail.com>
bfe6a10a »
2009-11-30 added url to github
5 ;; Website: http://github.com/ruediger/emacs-codepad
78c64c24 »
2009-11-30 comply with Convention for Library Headers (see (elisp) Library Headers)
6 ;; Created: <2009-11-29>
1b6dc56c »
2009-11-30 initial release
7 ;; Keywords: codepad paste pastie pastebin
8 ;;
9 ;; This code is inspired by gist.el (written by Christian Neukirchen et.al.)
10 ;; see http://github.com/defunkt/gist.el/blob/master/gist.el
11 ;;
12 ;; This file is NOT part of GNU Emacs.
13 ;;
14 ;; This is free software; you can redistribute it and/or modify it under
15 ;; the terms of the GNU General Public License as published by the Free
16 ;; Software Foundation; either version 2, or (at your option) any later
17 ;; version.
18 ;;
19 ;; This is distributed in the hope that it will be useful, but WITHOUT
20 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 ;; for more details.
23 ;;
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 ;; MA 02111-1307, USA.
28
29 ;;; Commentary:
30
31 ;; This code can be used to paste code to codepad.org.
32
3ddcaacf »
2009-11-30 fixed checkdoc errors.
33 ;; codepad-paste-region pastes a region to codepad.org. The URL is printed
0ed925e5 »
2009-11-30 paste async
34 ;; and if codepad-view is T opened in the browser.
1b6dc56c »
2009-11-30 initial release
35 ;;
36 ;; codepad-paste-buffer pastes the whole buffer.
37
78c64c24 »
2009-11-30 comply with Convention for Library Headers (see (elisp) Library Headers)
38 ;; TODO:
1b6dc56c »
2009-11-30 initial release
39
97191054 »
2009-11-30 added README
40 ;; * fetch Output from codepad.org (if run is True)
1b6dc56c »
2009-11-30 initial release
41 ;; * support projects (http://project.codepad.org)
42 ;; * support user accounts
43
44 ;;; Code:
45
822877ca »
2010-01-12 added missing (require 'cl)
46 (require 'cl)
0e5bb339 » Lorenzo Villani
2010-06-05 fix "error in process filter: Symbol's value as variable is void: url…
47 (require 'url-http)
822877ca »
2010-01-12 added missing (require 'cl)
48
ce40faa4 »
2009-11-30 added docstrings
49 (defconst +codepad-url+ "http://codepad.org"
3ddcaacf »
2009-11-30 fixed checkdoc errors.
50 "Url to codepad.org.")
1b6dc56c »
2009-11-30 initial release
51
52 (defconst +codepad-lang+ '((c-mode . "C")
53 (c++-mode . "C++")
54 (d-mode . "D")
55 (haskell-mode . "Haskell")
56 (lua-mode . "Lua")
57 (ocaml-mode . "OCaml")
58 (php-mode . "PHP")
59 (perl-mode . "Perl")
60 (python-mode . "Python")
61 (ruby-mode . "Ruby")
62 (scheme-mode . "Scheme")
ce40faa4 »
2009-11-30 added docstrings
63 (tcl-mode . "Tcl"))
3ddcaacf »
2009-11-30 fixed checkdoc errors.
64 "Association of major-modes to language names used by codepad.org.")
1b6dc56c »
2009-11-30 initial release
65
ce40faa4 »
2009-11-30 added docstrings
66 (defconst +codepad-default-lang+ "Plain Text"
9351c111 »
2009-11-30 fixed typo
67 "Language if `major-mode' is not supported by codepad.org.")
1b6dc56c »
2009-11-30 initial release
68
69 (defgroup codepad nil
70 "Codepad paste support"
71 :prefix "codepad-"
72 :tag "Codepad"
73 :group 'external
bfe6a10a »
2009-11-30 added url to github
74 :link '(url-link "http://github.com/ruediger/emacs-codepad"))
1b6dc56c »
2009-11-30 initial release
75
76 (defcustom codepad-private 'ask
77 "Private pastes?"
78 :group 'codepad
45b2321c »
2009-11-30 improved custom menu
79 :type '(radio
80 (const :tag "Always ask" :value ask)
81 (const :tag "Check prefix" :value prefix)
82 (const :tag "No" :value no)
83 (const :tag "Yes" :value yes)))
1b6dc56c »
2009-11-30 initial release
84
85 (defcustom codepad-run 'yes
86 "Run pastes?"
87 :group 'codepad
45b2321c »
2009-11-30 improved custom menu
88 :type '(radio
89 (const :tag "Always ask" :value ask)
90 (const :tag "Check prefix" :value prefix)
91 (const :tag "No" :value no)
92 (const :tag "Yes" :value yes)))
1b6dc56c »
2009-11-30 initial release
93
94 (defcustom codepad-view t
95 "View paste in browser?"
96 :group 'codepad
97 :type 'boolean)
98
a051b66e »
2009-11-30 cleaned up fetch-code (don't parse headers with regex use local vars)
99 (defcustom codepad-autoset-mode t
100 "Try to determine and set mode for fetched code?"
101 :group 'codepad
102 :type 'boolean)
103
d7d6c2f4 »
2009-11-30 Can fork existing pastes now
104 (defcustom codepad-autofork t
105 "Create new pastes as a fork of `codepad-id'?"
106 :group 'codepad
107 :type 'boolean)
108
166614ff »
2009-11-30 added more fixes from thomas001.
109 (defcustom codepad-use-x-clipboard t
110 "Copy URL also to the X clipboard?"
111 :group 'codepad
112 :type 'boolean)
113
822877ca »
2010-01-12 added missing (require 'cl)
114 (defvar codepad-id nil "ID on Codepad or nil. Buffer local.")
115
1b6dc56c »
2009-11-30 initial release
116 (defun codepad-read-p (prompt &optional default)
3ddcaacf »
2009-11-30 fixed checkdoc errors.
117 "Read true (t,y,true,yes) or false (nil,false,no) from the minibuffer.
118 Uses PROMPT as prompt and DEFAULT is the default value."
c0ae7f49 »
2009-11-30 changed prompt to be consistent with the rest of emacs
119 (let ((val (downcase (read-string (concat prompt " (default "
120 (if default "Yes" "No") "): ")))))
1b6dc56c »
2009-11-30 initial release
121 (cond
122 ((string= val "") default)
123 ((member val '("t" "y" "true" "yes")) t)
124 ((member val '("nil" "f" "n" "false" "no")) nil)
d5d1c7c2 »
2009-11-30 beauty change
125 (t (message "Wrong input '%s'! Please enter either Yes or No" val)
1b6dc56c »
2009-11-30 initial release
126 (codepad-read-p prompt default)))))
127
128 (defun codepad-interactive-option (var prompt)
3ddcaacf »
2009-11-30 fixed checkdoc errors.
129 "Handle interactive option for VAR. Use PROMPT if user is asked."
1b6dc56c »
2009-11-30 initial release
130 (case var
131 ((ask) (codepad-read-p prompt))
132 ((no) nil)
133 ((yes) t)
134 ((prefix) current-prefix-arg)
135 (t var)))
136
137 (defun codepad-true-or-false (val)
3ddcaacf »
2009-11-30 fixed checkdoc errors.
138 "Convert VAL into a string True or False."
1b6dc56c »
2009-11-30 initial release
139 (if val
140 "True"
141 "False"))
142
3ddcaacf »
2009-11-30 fixed checkdoc errors.
143 (defun codepad-url-encode (string)
144 "Encode STRING. Like `url-hexify-string' but space is turned into +."
1b6dc56c »
2009-11-30 initial release
145 (replace-regexp-in-string "%20" "+" (url-hexify-string string)))
146
147 ;; copied from gist.el
148 (defun codepad-make-query-string (params)
3ddcaacf »
2009-11-30 fixed checkdoc errors.
149 "Return a query string constructed from PARAMS.
150 PARAMS should be a list with elements of the form (KEY . VALUE). KEY and VALUE
1b6dc56c »
2009-11-30 initial release
151 should both be strings."
152 (mapconcat
153 (lambda (param)
4ee62f9b »
2009-11-30 fixed: rename codepad-hexify-string to codepad-url-encode
154 (concat (codepad-url-encode (car param)) "="
155 (codepad-url-encode (cdr param))))
1b6dc56c »
2009-11-30 initial release
156 params "&"))
157
158 ;;;###autoload
3ddcaacf »
2009-11-30 fixed checkdoc errors.
159 (defun* codepad-paste-region (begin end
166614ff »
2009-11-30 added more fixes from thomas001.
160 &optional (private 'check-custom)
d7d6c2f4 »
2009-11-30 Can fork existing pastes now
161 (fork 'check-custom)
166614ff »
2009-11-30 added more fixes from thomas001.
162 callback cbargs)
163 "Paste region to codepad.org.
d7d6c2f4 »
2009-11-30 Can fork existing pastes now
164 If PRIVATE is set the pase will be private.
165 If FORK is set to an id the paste will be created as a fork of this paste.
166 If FORK is set to 'auto (or to check-custom and codepad-autofork is t) it
167 will fork the paste in `codepad-id'.
168 Call CALLBACK as (apply CALLBACK URL ERR-P CBARGS) where ERR-P is nil and
169 URL is the resulted url in the case of success or ERR is an error descriptor."
1b6dc56c »
2009-11-30 initial release
170 (interactive "r")
d7d6c2f4 »
2009-11-30 Can fork existing pastes now
171 (let* ((codepad-url (cond
172 ((and
173 (or (eql fork 'auto)
174 (and (eql fork 'check-custom)
175 codepad-autofork))
176 (stringp codepad-id))
177 (format "%s/%s/fork" +codepad-url+ codepad-id))
178 ((stringp fork)
179 (format "%s/%s/fork" +codepad-url+ fork))
180 (t +codepad-url+)))
181 (private (codepad-interactive-option (if (eql private 'check-custom)
99636ce9 »
2009-11-30 private flag now behaves similar to sync flag
182 codepad-private
183 private)
184 "Private Paste?"))
1b6dc56c »
2009-11-30 initial release
185 (lang (or (cdr (assoc major-mode +codepad-lang+))
186 +codepad-default-lang+))
187 (run (codepad-interactive-option codepad-run "Run Paste?"))
188 (url-max-redirections 0)
189 (url-request-method "POST")
99636ce9 »
2009-11-30 private flag now behaves similar to sync flag
190 (url-request-extra-headers
191 '(("Content-type" . "application/x-www-form-urlencoded")))
1b6dc56c »
2009-11-30 initial release
192 (url-request-data
3ddcaacf »
2009-11-30 fixed checkdoc errors.
193 (codepad-make-query-string
1b6dc56c »
2009-11-30 initial release
194 `(("submit" . "Submit")
195 ("private" . ,(codepad-true-or-false private))
196 ("run" . ,(codepad-true-or-false run))
197 ("lang" . ,lang)
0ed925e5 »
2009-11-30 paste async
198 ("code" . ,(buffer-substring begin end))))))
d7d6c2f4 »
2009-11-30 Can fork existing pastes now
199 (url-retrieve codepad-url
166614ff »
2009-11-30 added more fixes from thomas001.
200 (lambda (status callback cbargs)
a9c03fb6 »
2009-11-30 removed sync pasting and cleaned up hack (no re-search-forward!)
201 (let ((url (plist-get status :redirect))
202 (err (plist-get status :error)))
166614ff »
2009-11-30 added more fixes from thomas001.
203 (when callback
204 (apply callback url err cbargs))
a9c03fb6 »
2009-11-30 removed sync pasting and cleaned up hack (no re-search-forward!)
205 (when err
206 (signal (car err) (cdr err)))
207 (message "Paste created: %s" url)
208 (when codepad-view (browse-url url))
166614ff »
2009-11-30 added more fixes from thomas001.
209 (let ((x-select-enable-clipboard
210 (or codepad-use-x-clipboard
211 x-select-enable-clipboard)))
212 (kill-new url))
a9c03fb6 »
2009-11-30 removed sync pasting and cleaned up hack (no re-search-forward!)
213 (kill-buffer (current-buffer))
166614ff »
2009-11-30 added more fixes from thomas001.
214 url))
215 (list callback cbargs))))
1b6dc56c »
2009-11-30 initial release
216
217 ;;;###autoload
99636ce9 »
2009-11-30 private flag now behaves similar to sync flag
218 (defun* codepad-paste-buffer (&optional
166614ff »
2009-11-30 added more fixes from thomas001.
219 (private 'check-custom)
d7d6c2f4 »
2009-11-30 Can fork existing pastes now
220 (fork 'check-custom)
166614ff »
2009-11-30 added more fixes from thomas001.
221 callback cbargs)
222 "Paste buffer to codepad.org. See `codepad-paste-region'."
1b6dc56c »
2009-11-30 initial release
223 (interactive)
166614ff »
2009-11-30 added more fixes from thomas001.
224 (codepad-paste-region (point-min) (point-max) private callback cbargs))
1b6dc56c »
2009-11-30 initial release
225
dff74d74 »
2009-11-30 implemented codepad-fetch-code
226 (defconst +codepad-mime-to-mode+ '(("c++src" . c++-mode)
227 ("csrc" . c-mode)
2864fb8d »
2009-11-30 added the rest of the mode detection
228 ("dsrc" . d-mode)
229 ("haskell" . haskell-mode)
230 ("lua" . lua-mode)
231 ("ocaml" . ocaml-mode)
232 ("php" . php-mode)
0e5bb339 » Lorenzo Villani
2010-06-05 fix "error in process filter: Symbol's value as variable is void: url…
233 ("perl" . perl-mode)
2864fb8d »
2009-11-30 added the rest of the mode detection
234 ("python" . python-mode)
235 ("ruby" . ruby-mode)
236 ("scheme" . scheme-mode)
237 ("tcl" . tcl-mode))
dff74d74 »
2009-11-30 implemented codepad-fetch-code
238 "MIME text/x-... to emacs mode.")
239
faa38639 »
2009-11-30 cleanup: use url-http-end-of-headers insted of searching for it
240 ;; stuff from url-http.el
a051b66e »
2009-11-30 cleaned up fetch-code (don't parse headers with regex use local vars)
241 (defvar url-http-content-type)
faa38639 »
2009-11-30 cleanup: use url-http-end-of-headers insted of searching for it
242 (defvar url-http-end-of-headers)
a051b66e »
2009-11-30 cleaned up fetch-code (don't parse headers with regex use local vars)
243
1b6dc56c »
2009-11-30 initial release
244 ;;;###autoload
dff74d74 »
2009-11-30 implemented codepad-fetch-code
245 (defun codepad-fetch-code (id &optional buffer-name)
246 "Fetch code from codepad.org.
247 Argument ID is the codepad id and
248 optional argument is the BUFFER-NAME where to write."
249 (interactive "sCodepad ID: ")
250 (let* ((just-id (replace-regexp-in-string "^.*/" "" id)) ; strip http://...
251 (buffer-name (or buffer-name (format "*codepad %s*" just-id)))
252 (url (concat +codepad-url+ "/" just-id "/raw"))
253 (buffer (get-buffer buffer-name)))
05f4800c »
2009-11-30 fetch async
254 (if (bufferp buffer)
255 (pop-to-buffer buffer)
321f7db3 »
2009-11-30 set codepad-id var if fetched
256
05f4800c »
2009-11-30 fetch async
257 (message "Fetching %s from Codepad" just-id)
258 (url-retrieve url
259 (lambda (status buffer-name just-id)
260 (let ((err (plist-get status :error)))
261 (when err
262 (signal (car err) (cdr err))))
263 (rename-buffer buffer-name t)
0e5bb339 » Lorenzo Villani
2010-06-05 fix "error in process filter: Symbol's value as variable is void: url…
264
05f4800c »
2009-11-30 fetch async
265 ;; set codepad-id to the id
266 (make-local-variable 'codepad-id)
267 (setq codepad-id just-id)
321f7db3 »
2009-11-30 set codepad-id var if fetched
268
5bfeea39 »
2009-11-30 code cleanup
269 ;; Delete Headers
faa38639 »
2009-11-30 cleanup: use url-http-end-of-headers insted of searching for it
270 (delete-region (point-min) url-http-end-of-headers)
5bfeea39 »
2009-11-30 code cleanup
271
272 ;; Determine and set mode
273 (if (and codepad-autoset-mode
274 url-http-content-type
275 (string-match "text/x-\\([^;[:space:]]*\\)"
276 url-http-content-type))
05f4800c »
2009-11-30 fetch async
277 (let ((mode
278 (cdr (assoc
279 (match-string 1 url-http-content-type)
280 +codepad-mime-to-mode+))))
fd7754a0 »
2009-11-30 set fundamental-mode if no major-mode
281 (if mode
5bfeea39 »
2009-11-30 code cleanup
282 (funcall mode)
283 (fundamental-mode)))
fd7754a0 »
2009-11-30 set fundamental-mode if no major-mode
284 (fundamental-mode))
5bfeea39 »
2009-11-30 code cleanup
285 (set-buffer-modified-p nil)
286 (pop-to-buffer (current-buffer)))
05f4800c »
2009-11-30 fetch async
287 (list buffer-name just-id)))))
189d4a27 »
2009-11-30 added missing provide
288
289 (provide 'codepad)
290 ;;; codepad.el ends here
Something went wrong with that request. Please try again.