]> git.rkrishnan.org Git - .emacs.d.git/blob - emacs/pastebin.el
remove toolbar and menubar
[.emacs.d.git] / emacs / pastebin.el
1 ;;; pastebin.el --- A simple interface to the www.pastebin.com webservice
2
3 ;;; Copyright (C) 2008 by Tapsell-Ferrier Limited
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-subdomain ""
46   "Pastebin subdomain to use by default"
47   :type 'string
48   :group 'pastebin)
49
50 (defcustom pastebin-type-assoc
51   '((actionscript-mode . " actionscript")
52     (ada-mode . "ada")
53     (asm-mode . "asm")
54     (autoconf-mode . "bash")
55     (bibtex-mode . "bibtex")
56     (cmake-mode . "cmake")
57     (c-mode . "c")
58     (c++-mode . "cpp")
59     (cobol-mode . "cobol")
60     (conf-colon-mode . "properties")
61     (conf-javaprop-mode . "properties")
62     (conf-mode . "ini")
63     (conf-space-mode . "properties")
64     (conf-unix-mode . "ini")
65     (conf-windows-mode . "ini")
66     (cperl-mode . "perl")
67     (csharp-mode . "csharp")
68     (css-mode . "css")
69     (delphi-mode . "delphi")
70     (diff-mode . "diff")
71     (ebuild-mode . "bash")
72     (eiffel-mode . "eiffel")
73     (emacs-lisp-mode . "lisp")
74     (erlang-mode . "erlang")
75     (erlang-shell-mode . "erlang")
76     (espresso-mode . "javascript")
77     (fortran-mode . "fortran")
78     (glsl-mode . "glsl")
79     (gnuplot-mode . "gnuplot")
80     (graphviz-dot-mode . "dot")
81     (haskell-mode . "haskell")
82     (html-mode . "html4strict")
83     (idl-mode . "idl")
84     (inferior-haskell-mode . "haskell")
85     (inferior-octave-mode . "octave")
86     (inferior-python-mode . "python")
87     (inferior-ruby-mode . "ruby")
88     (java-mode . "java")
89     (js2-mode . "javascript")
90     (jython-mode . "python")
91     (latex-mode . "latex")
92     (lisp-mode . "lisp")
93     (lua-mode . "lua")
94     (makefile-mode . "make")
95     (makefile-automake-mode . "make")
96     (makefile-gmake-mode . "make")
97     (makefile-makepp-mode . "make")
98     (makefile-bsdmake-mode . "make")
99     (makefile-imake-mode . "make")
100     (matlab-mode . "matlab")
101     (nxml-mode . "xml")
102     (oberon-mode . "oberon2")
103     (objc-mode . "objc")
104     (ocaml-mode . "ocaml")
105     (octave-mode . "matlab")
106     (pascal-mode . "pascal")
107     (perl-mode . "perl")
108     (php-mode . "php")
109     (plsql-mode . "plsql")
110     (po-mode . "gettext")
111     (prolog-mode . "prolog")
112     (python-2-mode . "python")
113     (python-3-mode . "python")
114     (python-basic-mode . "python")
115     (python-mode . "python")
116     (ruby-mode . "ruby")
117     (scheme-mode . "lisp")
118     (shell-mode . "bash")
119     (sh-mode . "bash")
120     (smalltalk-mode . "smalltalk")
121     (sql-mode . "sql")
122     (tcl-mode . "tcl")
123     (visual-basic-mode . "vb")
124     (xml-mode . "xml")
125     (yaml-mode . "properties"))
126   "Alist composed of major-mode names and corresponding pastebin highlight formats."
127   :type '(alist :key-type symbol :value-tupe string)
128   :group 'pastebin)
129
130 (defvar pastebin-subdomain-history '())
131
132 ;;;###autoload
133 (defun pastebin-buffer (&optional subdomain)
134   "Send the whole buffer to pastebin.com.
135 Optional argument subdomain will request the virtual host to use,
136  eg:'emacs' for 'emacs.pastebin.com'."
137   (interactive
138    (let ((pastebin-subdomain
139       (if current-prefix-arg
140           (read-string "pastebin subdomain:" nil 'pastebin-subdomain-history) pastebin-default-subdomain)))
141      (list pastebin-subdomain)))
142   (pastebin (point-min) (point-max) subdomain))
143
144 ;;;###autoload
145 (defun pastebin (start end &optional subdomain)
146   "An interface to the pastebin code snippet www service.
147
148 See pastebin.com for more information about pastebin.
149
150 Called interactively pastebin uses the current region for
151 preference for sending... if the mark is NOT set then the entire
152 buffer is sent.
153
154 Argument START is the start of region.
155 Argument END is the end of region.
156
157 If subdomain is used pastebin prompts for a subdomain to be used as the
158 virtual host to use.  For example use 'emacs' for 'emacs.pastebin.com'."
159   (interactive
160    (let ((pastebin-subdomain
161           (if current-prefix-arg
162               (read-string "pastebin subdomain:" nil 'pastebin-subdomain-history) pastebin-default-subdomain)))
163      (if (mark)
164          (list (region-beginning) (region-end) pastebin-subdomain)
165        (list (point-min) (point-max) pastebin-subdomain))))
166   ;; Main function
167   (let* ((data (buffer-substring-no-properties start end))
168          (pastebin-url "http://pastebin.com/api_public.php")
169          (url-request-method "POST")
170          (url-request-extra-headers
171           '(("Content-Type" . "application/x-www-form-urlencoded")))
172          (url-request-data
173
174           (concat (format "submit=submit&paste_private=0&paste_expire_date=N&paste_subdomain=%s&paste_format=%s&paste_name=%s&paste_code=%s"
175               subdomain
176                           (or (assoc-default major-mode pastebin-type-assoc) "text")
177                           (url-hexify-string (user-full-name))
178                           (url-hexify-string data))))
179          (content-buf (url-retrieve pastebin-url
180                                     (lambda (arg)
181                                       (cond
182                                        ((equal :error (car arg))
183                                         (signal 'pastebin-error (cdr arg)))
184                                        (t
185                     (re-search-forward "\n\n")
186                     (clipboard-kill-ring-save (point) (point-max))
187                         (message "Pastebin URL: %s" (buffer-substring (point) (point-max)))))))))))
188
189 (provide 'pastebin)
190 ;;; pastebin.el ends here