Skip to content
Newer
Older
100644 240 lines (217 sloc) 7.94 KB
24b5d51 init pastebin elisp
nferrier authored
1 ;;; pastebin.el --- A simple interface to the www.pastebin.com webservice
2
3 ;;; Copyright (C) 2008 by Nic Ferrier <nic@ferrier.me.uk>
4 ;;; Copyright (C) 2010 by Ivan Korotkov <twee@tweedle-dee.org>
5
6 ;;; This program is free software; you can redistribute it and/or modify
7 ;;; it under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 2, or (at your option)
9 ;;; any later version.
10
11 ;;; This program is distributed in the hope that it will be useful,
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with this program; see the file COPYING. If not, write to the
18 ;;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 ;;; Boston, MA 02110-1301 USA
20
21 ;;; Commentary:
22 ;;;
23 ;;; Load this file and run:
24 ;;;
25 ;;; M-x pastebin-buffer
26 ;;;
27 ;;; to send the whole buffer or select a region and run
28 ;;;
29 ;;; M-x pastebin
30 ;;;
31 ;;; to send just the region.
32 ;;;
33 ;;; In either case the url that pastebin generates is left on the kill
34 ;;; ring and the paste buffer.
35
36
37 ;;; Code:
38
39 ;;;###autoload
40 (defgroup pastebin nil
41 "Pastebin -- pastebin.com client"
42 :tag "Pastebin"
43 :group 'tools)
44
45 (defcustom pastebin-default-domain "pastebin.com"
46 "Pastebin domain to use by default"
47 :type 'string
48 :group 'pastebin
49 )
50
51 (defcustom pastebin-domain-versions '(("pastebin.com" "/api_public.php")
2aba765 replace example v1 domain
nferrier authored
52 ("pastebin.example.com" "/pastebin.php"))
24b5d51 init pastebin elisp
nferrier authored
53 "The version of pastebin that is supported by domains that you use.
54
55 As Pastebin changes versions they sometimes change the path used.
56 Valid paths are:
57
58 /pastebin.php - early version
59 /api_public.php - current version
60
61 The pastebin code adapts to the version depending on this.
62 "
63 :group 'pastebin
64 )
65
66 (defcustom pastebin-type-assoc
67 '((actionscript-mode . " actionscript")
68 (ada-mode . "ada")
69 (asm-mode . "asm")
70 (autoconf-mode . "bash")
71 (bibtex-mode . "bibtex")
72 (cmake-mode . "cmake")
73 (c-mode . "c")
74 (c++-mode . "cpp")
75 (cobol-mode . "cobol")
76 (conf-colon-mode . "properties")
77 (conf-javaprop-mode . "properties")
78 (conf-mode . "ini")
79 (conf-space-mode . "properties")
80 (conf-unix-mode . "ini")
81 (conf-windows-mode . "ini")
82 (cperl-mode . "perl")
83 (csharp-mode . "csharp")
84 (css-mode . "css")
85 (delphi-mode . "delphi")
86 (diff-mode . "dff")
87 (ebuild-mode . "bash")
88 (eiffel-mode . "eiffel")
89 (emacs-lisp-mode . "lisp")
90 (erlang-mode . "erlang")
91 (erlang-shell-mode . "erlang")
92 (espresso-mode . "javascript")
93 (fortran-mode . "fortran")
94 (glsl-mode . "glsl")
95 (gnuplot-mode . "gnuplot")
96 (graphviz-dot-mode . "dot")
97 (haskell-mode . "haskell")
98 (html-mode . "html4strict")
99 (idl-mode . "idl")
100 (inferior-haskell-mode . "haskell")
101 (inferior-octave-mode . "octave")
102 (inferior-python-mode . "python")
103 (inferior-ruby-mode . "ruby")
104 (java-mode . "java")
105 (js2-mode . "javascript")
106 (jython-mode . "python")
107 (latex-mode . "latex")
108 (lisp-mode . "lisp")
109 (lua-mode . "lua")
110 (makefile-mode . "make")
111 (makefile-automake-mode . "make")
112 (makefile-gmake-mode . "make")
113 (makefile-makepp-mode . "make")
114 (makefile-bsdmake-mode . "make")
115 (makefile-imake-mode . "make")
116 (matlab-mode . "matlab")
117 (nxml-mode . "xml")
118 (oberon-mode . "oberon2")
119 (objc-mode . "objc")
120 (ocaml-mode . "ocaml")
121 (octave-mode . "matlab")
122 (pascal-mode . "pascal")
123 (perl-mode . "perl")
124 (php-mode . "php")
125 (plsql-mode . "plsql")
126 (po-mode . "gettext")
127 (prolog-mode . "prolog")
128 (python-2-mode . "python")
129 (python-3-mode . "python")
130 (python-basic-mode . "python")
131 (python-mode . "python")
132 (ruby-mode . "ruby")
133 (scheme-mode . "lisp")
134 (shell-mode . "bash")
135 (sh-mode . "bash")
136 (smalltalk-mode . "smalltalk")
137 (sql-mode . "sql")
138 (tcl-mode . "tcl")
139 (visual-basic-mode . "vb")
140 (xml-mode . "xml")
141 (yaml-mode . "properties"))
142 "Alist composed of major-mode names and corresponding pastebin highlight formats."
143 :type '(alist :key-type symbol :value-tupe string)
144 :group 'pastebin)
145
146 (defvar pastebin-domain-history '())
147
148 ;;;###autoload
149 (defun pastebin-buffer (&optional domain)
150 "Send the whole buffer to pastebin.com.
151 Optional argument domain will request the virtual host to use,
152 eg:'emacs.pastebin.com' or 'mylocalpastebin.com'."
153 (interactive
154 (let ((pastebin-domain
155 (if current-prefix-arg
156 (read-string "pastebin domain:"
157 pastebin-default-domain
158 'pastebin-domain-history) pastebin-default-domain)))
159 (list pastebin-domain)))
160 (pastebin (point-min) (point-max) domain))
161
162 ;;;###autoload
163 (defun pastebin (start end &optional domain)
164 "Send the region to the pastebin service specified by domain.
165
166 See pastebin.com for more information about pastebin.
167
168 Called interactively pastebin uses the current region for
169 preference for sending... if the mark is NOT set then the entire
170 buffer is sent.
171
172 Argument START is the start of region.
173 Argument END is the end of region.
174
175 If domain is used pastebin prompts for a domain defaulting to
176 'pastebin-default-domain' so you can send requests or use a
177 different domain.
178 "
179 (interactive
180 (let ((pastebin-domain
181 (if current-prefix-arg
182 (read-string "pastebin domain:"
183 pastebin-default-domain
184 'pastebin-domain-history) pastebin-default-domain)))
185 (if (mark)
186 (list (region-beginning) (region-end) pastebin-domain)
187 (list (point-min) (point-max) pastebin-domain))))
188 ;; Main function
189 (if (not domain)
190 (setq domain pastebin-default-domain))
191 (let* ((path (cadr (assoc domain pastebin-domain-versions)))
192 (params (cond
193 ((equal path "/api_public.php")
194 (concat "submit=submit"
195 "&paste_private=0"
196 "&paste_expire_date=N"
197 (if (not (equal domain "pastebin.com"))
198 "&paste_subdomain=%s"
199 "paste_placeholder=%s")
200 "&paste_format=%s"
201 "&paste_name=%s"
202 "&paste_code=%s"))
203 ((equal path "/pastebin.php")
204 (concat "paste=Send"
205 "&private=0"
206 "&expiry=N"
207 "&subdomain=%s"
208 "&format=%s"
209 "&poster=%s"
210 "&code2=%s"))
211 ('t
212 (signal
213 'pastebin-version-error
214 "pastebin.el doesn't support that version of pastebin"))))
215 (data (buffer-substring-no-properties start end))
8e9a829 fix path reference
nferrier authored
216 (pastebin-url (format "http://%s%s" domain path))
24b5d51 init pastebin elisp
nferrier authored
217 (url-request-method "POST")
218 (url-request-extra-headers
219 '(("Content-Type" . "application/x-www-form-urlencoded")))
220 (url-request-data
221 (concat (format
222 params
223 domain
224 (or (assoc-default major-mode pastebin-type-assoc) "text")
225 (url-hexify-string (user-full-name))
226 (url-hexify-string data))))
227 (content-buf (url-retrieve
228 pastebin-url
229 (lambda (arg)
230 (cond
231 ((equal :error (car arg))
232 (signal 'pastebin-error (cdr arg)))
233 (t
234 (re-search-forward "\n\n")
235 (clipboard-kill-ring-save (point) (point-max))
236 (message "Pastebin URL: %s" (buffer-substring (point) (point-max)))))))))))
237
238 (provide 'pastebin)
239 ;;; pastebin.el ends here
Something went wrong with that request. Please try again.