]> git.siccegge.de Git - dotfiles/emacs.git/blob - lisp/dpaste.el
Clean up config
[dotfiles/emacs.git] / lisp / dpaste.el
1 ;;; pastebin.el --- A simple interface to the www.pastebin.com webservice
2
3 ;;; Copyright (C) 2008 by Tapsell-Ferrier Limited
4
5 ;;; This program is free software; you can redistribute it and/or modify
6 ;;; it under the terms of the GNU General Public License as published by
7 ;;; the Free Software Foundation; either version 2, or (at your option)
8 ;;; any later version.
9
10 ;;; This program is distributed in the hope that it will be useful,
11 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;;; GNU General Public License for more details.
14
15 ;;; You should have received a copy of the GNU General Public License
16 ;;; along with this program; see the file COPYING. If not, write to the
17 ;;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 ;;; Boston, MA 02110-1301 USA
19
20 ;;; Commentary:
21 ;;;
22 ;;; Load this file and run:
23 ;;;
24 ;;; M-x pastebin-buffer
25 ;;;
26 ;;; to send the whole buffer or select a region and run
27 ;;;
28 ;;; M-x pastebin
29 ;;;
30 ;;; to send just the region.
31 ;;;
32 ;;; In either case the url that pastebin generates is left on the kill
33 ;;; ring and the paste buffer.
34
35
36 ;;; Code:
37
38 (defvar dpastebin-type-assoc
39 '((emacs-lisp-mode . "887")
40 (scheme-mode . "887")
41 (python-mode . "795")
42 (nxml-mode . "840")))
43
44 (defvar dpastebin-retrieved nil)
45
46 (defvar dpastebin-expire-history '())
47
48 (defun dpastebin-buffer (&optional expire)
49 "Send the whole buffer to paste.q0a.de."
50 (interactive (if current-prefix-arg
51 (list (read-string "pastebin expire:" nil 'dpastebin-expire-history))))
52 (dpastebin (point-min) (point-max) expire))
53
54 (defun dpastebin (start end &optional expire)
55 "An interface to the pastebin code snippet www service.
56
57 See pastebin.com for more information about pastebin.
58
59 Called interactively pastebin uses the current region for
60 preference for sending... if the mark is NOT set then the entire
61 buffer is sent.
62
63 Argument START is the start of region.
64 Argument END is the end of region.
65
66 If PREFIX is used pastebin prompts for a prefix to be used as the
67 virtual host to use. For example use 'emacs' for 'emacs.pastebin.com'."
68 (interactive
69 (let ((pastebin-expire
70 (if current-prefix-arg
71 (read-string "pastebin expire:" nil 'dpastebin-expire-history))))
72 (if (mark)
73 (list (region-beginning) (region-end) pastebin-expire)
74 (list (point-min) (point-max) pastebin-expire))))
75 ;; Main function
76 (let* ((data (buffer-substring-no-properties start end))
77 (pastebin-url (format "http://paste.debian.net"))
78 (url-request-method "POST")
79 (url-request-extra-headers
80 '(("Content-Type" . "multipart/form-data")))
81 (url-request-data
82 (concat (format "paste=Send&lang=%s&expiry=%s&poster=%s&code=%s"
83 (assoc-default major-mode dpastebin-type-assoc nil "878")
84 (or expire "d")
85 (user-full-name)
86 (url-hexify-string data))))
87 (content-buf (url-retrieve pastebin-url
88 (lambda (arg)
89 (cond
90 ((equal :error (car arg))
91 (message (cdr arg)))
92 ((equal :redirect (car arg))
93 (setq pastebin-retrieved (cadr arg))
94 (with-temp-buffer
95 (insert pastebin-retrieved)
96 (clipboard-kill-ring-save (point-min) (point-max)))))))))
97 (car kill-ring)))
98
99 (provide 'debian-pastebin)
100 ;;; pastebin.el ends here