-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpastie.el
105 lines (93 loc) · 4.24 KB
/
pastie.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
;; pastie.el -- Emacs integration for pastie.caboo.se.
;; Copyright (C) 2006 Christian Neukirchen <purl.org/net/chneukirchen>
;; Licensed under the GPL.
;; 2007-12-27 Updated to work with more recent changes to the pastie API. (Rob Christie)
;; 2007-12-30 Added more major mode sniffs (Ryan McGeary)
;; 2007-12-31 Added some minor mode sniffs that are Rails specific. (Rob Christie)
(defun pastie-language ()
"Sniffs for the language of the region that is being pasted"
(or (when (boundp 'rails-view-minor-mode) (if rails-view-minor-mode "html_rails"))
(when (boundp 'rails-minor-mode) (if rails-minor-mode "ruby_on_rails"))
(cdr (assoc major-mode '((c-mode . "c++")
(c++-mode . "c++")
(css-mode . "css")
(diff-mode . "diff")
(html-mode . "html")
(java-mode . "java")
(javascript-mode . "javascript")
(jde-mode . "java")
(php-mode . "php")
(ruby-mode . "ruby")
(text-mode . "plain_text")
(sql-mode . "sql")
(sh-mode . "shell-unix-generic"))))
"plain_text"))
(defun pastie-region (begin end)
"Post the current region as a new paste at pastie.caboo.se.
Copies the URL into the kill ring."
(interactive "r")
(let* ((body-raw (buffer-substring begin end))
(body (replace-regexp-in-string
"[<>&]"
(lambda (match)
(case (string-to-char match)
(?< "<")
(?> ">")
(?& "&")))
body-raw))
;; Adjust as needed.
(mode (pastie-language))
(url-request-method "POST")
(url-mime-accept-string "application/xml")
(url-request-extra-headers '(("Content-Type" . "application/xml")))
(url (url-generic-parse-url "http://pastie.caboo.se/pastes"))
(url-request-data
(concat "<paste>"
"<parser>" mode "</parser>"
"<authorization>burger</authorization>"
"<body>" body "</body>"
"</paste>")))
(let ((pastie-buffer (url-retrieve-synchronously url)))
(with-current-buffer pastie-buffer
(goto-char (point-min))
(search-forward-regexp "^Status: \\([0-9]+.*\\)")
(let ((status (match-string 1)))
(if (string-match "^20[01]" status)
(progn
(goto-char (point-max))
(beginning-of-line)
(let ((id (buffer-substring (point) (point-max))))
(message "Paste created: http://pastie.caboo.se/paste/%s" id)
(kill-new (format "http://pastie.caboo.se/paste/%s" id))))
(message "Error occured: %s" status))))
(kill-buffer pastie-buffer))))
(defun pastie-buffer ()
"Post the current buffer as a new paste at pastie.caboo.se.
Copies the URL into the kill ring."
(interactive)
(pastie-region (point-min) (point-max)))
(defun pastie-get (id)
"Fetch the contents of the paste from pastie.caboo.se into a new buffer."
(interactive "nPastie #: ")
(let ((url-request-method "GET")
(url-request-extra-headers nil)
(url-mime-accept-string "*/*")
(url (url-generic-parse-url
(format "http://pastie.caboo.se/pastes/%s/download" id))))
(setq pastie-buffer (url-retrieve-synchronously url))
(with-current-buffer pastie-buffer
(goto-char (point-min))
(search-forward-regexp "^Status: \\([0-9]+.*\\)")
(let ((status (match-string 1)))
(if (string-match "^200" status)
(progn
(search-forward-regexp
"^Content-Disposition: attachment; filename=\"\\(.*\\)\"")
(set-visited-file-name (match-string 1))
(search-forward-regexp "\n\n")
(delete-region (point-min) (point))
(normal-mode)
(set-buffer-modified-p nil)
(switch-to-buffer pastie-buffer))
(message "Error occured: %s" status)
(kill-buffer pastie-buffer))))))