src.nth.io/

summaryrefslogtreecommitdiff
path: root/hoersten-pastebin-region.el
blob: b03a042607a4b5cf246598da0fcb4e159a1b1bd7 (plain)
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
;; ~/.emacs.d/hoersten-pastebin-region.el - Luke Hoersten - v0.6

;; custom keys
(global-set-key (kbd "C-c w") 'pastebin-region)

;; Based on http://www.emacswiki.org/cgi-bin/wiki/download/pastebin.el
(defvar pastebin-type-assoc
  '((emacs-lisp-mode . "common-lisp")
	 (c-mode          . "c")
	 (python-mode     . "python")
	 (nxml-mode       . "xml")
	 (c++-mode        . "cpp")))

(defun pastebin-region (start end)
  "Send selected text to dpaste pastebin."
  (interactive "r")
  (let* ((pastebin-url "http://build-ch-03/paste/")
			(url-request-method "POST")
			(url-request-extra-headers '(("Content-Type" . "application/x-www-form-urlencoded")))
			(url-request-data
			 (format "title=%s&content=%s&lexer=%s&author=%s"
						(url-hexify-string (buffer-file-name)) ;; title
						(url-hexify-string (buffer-substring-no-properties start end)) ;; content
						(url-hexify-string (or (assoc-default major-mode pastebin-type-assoc) "text")) ;; lexer
						(url-hexify-string (user-full-name))))) ;; author
	 (url-retrieve pastebin-url (lambda (arg)
											(cond
											 ((equal :error (car arg))
											  (signal (cdr arg)))
											 ((equal :redirect (car arg))
											  (let ((redirected (cadr arg)))
												 (message redirected)
												 (with-temp-buffer
													(insert redirected)
													(clipboard-kill-ring-save (point-min) (point-max))))))))))