gregnewman / dpaste.el

Emacs mode to post to dpaste.com

dpaste.el / dpaste.el
100644 131 lines (103 sloc) 4.883 kb
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
;;; dpaste.el --- Emacs integration for dpaste.com
 
;; Copyright (C) 2008, 2009 Greg Newman <20seven.org>
 
;; Version: 0.2
;; Keywords: paste pastie pastebin dpaste python
;; Created: 01 Dec 2008
;; Author: Greg Newman <grep@20seven.org>
;; Guilherme Gondim <semente@taurinus.org>
;; Maintainer: Greg Newman <greg@20seven.org>
 
;; This file is NOT part of GNU Emacs.
 
;; This 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 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
;; MA 02111-1307, USA.
 
;;; Commentary:
 
;; dpaste.el provides functions to post a region or buffer to
;; <http://dpaste.com> and put the paste URL into the kill-ring.
 
;; Inspired by gist.el
 
;; Current dpaste.com API usage example:
 
;; curl -si -F 'content=<-' http://dpaste.com/api/v1/ | \
;; grep ^Location: | colrm 1 10
 
;; Thanks to Paul Bissex (http://news.e-scribe.com) for a great paste
;; service.
 
;; Installation and setup:
 
;; Put this file in a directory where Emacs can find it. On GNU/Linux
;; it's usually /usr/local/share/emacs/site-lisp/ and on Windows it's
;; something like "C:\Program Files\Emacs<version>\site-lisp". Then
;; add the follow instructions in your .emacs.el:
 
;; (require 'dpaste nil)
;; (global-set-key (kbd "C-c p") 'dpaste-region-or-buffer)
;; (setq dpaste-poster "Guido van Rossum")
 
;; Then with C-c p you can run `dpaste-region-or-buffer'. With a prefix
;; argument (C-u C-c p), your paste will use the hold option.
 
;; Todo:
 
;; - Use emacs lisp code to post paste instead curl (version 0.3)
 
;;; Code:
(defvar dpaste-poster "dpaste.el"
  "Paste author name or e-mail. Don't put more than 30 characters here.")
 
(defvar dpaste-supported-modes-alist '((css-mode . "Css")
                                       (diff-mode . "Diff")
                                       (haskell-mode . "Haskell")
                                       (html-mode . "DjangoTemplate")
                                       (javascript-mode . "JScript")
                                       (js2-mode . "JScript")
                                       (python-mode . "Python")
                                       (inferior-python-mode . "PythonConsole")
                                       (ruby-mode . "Ruby")
                                       (sql-mode . "Sql")
                                       (sh-mode . "Bash")
                                       (xml-mode . "Xml")))
 
 
;;;###autoload
(defun dpaste-region (begin end title &optional arg)
  "Post the current region or buffer to dpaste.com and yank the
url to the kill-ring.
 
With a prefix argument, use hold option."
  (interactive "r\nsPaste title: \nP")
  (let* ((file (or (buffer-file-name) (buffer-name)))
         (name (file-name-nondirectory file))
         (lang (or (cdr (assoc major-mode dpaste-supported-modes-alist))
                  ""))
         (hold (if arg "on" "off"))
         (output (generate-new-buffer "*dpaste*")))
    (shell-command-on-region begin end
(concat "curl -si"
                                     " -F 'content=<-'"
                                     " -F 'language=" lang "'"
                                     " -F 'title=" title "'"
                                     " -F 'poster=" dpaste-poster "'"
                                     " -F 'hold=" hold "'"
                                     " http://dpaste.com/api/v1/")
output)
    (with-current-buffer output
      (search-forward-regexp "^Location: \\(http://dpaste\\.com/\\(hold/\\)?[0-9]+/\\)")
      (message "Paste created: %s (yanked)" (match-string 1))
      (kill-new (match-string 1)))
    (kill-buffer output)))
 
;;;###autoload
(defun dpaste-buffer (title &optional arg)
  "Post the current buffer to dpaste.com and yank the url to the
kill-ring.
 
With a prefix argument, use hold option."
  (interactive "sPaste title: \nP")
  (dpaste-region (point-min) (point-max) title arg))
 
;;;###autoload
(defun dpaste-region-or-buffer (title &optional arg)
  "Post the current region or buffer to dpaste.com and yank the
url to the kill-ring.
 
With a prefix argument, use hold option."
  (interactive "sPaste title: \nP")
  (condition-case nil
      (dpaste-region (point) (mark) title arg)
    (mark-inactive (dpaste-buffer title arg))))
 
 
(provide 'dpaste)
;;; dpaste.el ends here.