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