debpaste.el 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. ;;; debpaste.el --- Interface for getting/posting/deleting pastes from paste.debian.net
  2. ;; Copyright (C) 2013-2014 Alex Kost
  3. ;; Author: Alex Kost <alezost@gmail.com>
  4. ;; Created: 3 Dec 2013
  5. ;; Version: 0.1.5
  6. ;; Package-Requires: ((xml-rpc "1.6.7"))
  7. ;; URL: http://github.com/alezost/debpaste.el
  8. ;; Keywords: paste
  9. ;; This program is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; This package provides an interface for receiving, posting and
  21. ;; deleting pastes from <http://paste.debian.net/>.
  22. ;; You can install the package from MELPA. If you prefer the manual
  23. ;; installation, the easiest way is to put these lines into your
  24. ;; init-file:
  25. ;; (add-to-list 'load-path "/path/to/debpaste-dir")
  26. ;; (require 'debpaste)
  27. ;; Basic interactive commands:
  28. ;; - `debpaste-display-paste',
  29. ;; - `debpaste-paste-region',
  30. ;; - `debpaste-delete-paste'.
  31. ;; The package provides a keymap, that can be bound like this:
  32. ;; (global-set-key (kbd "M-D") 'debpaste-command-map)
  33. ;; You will probably want to modify a default poster name:
  34. ;; (setq debpaste-user-name user-login-name)
  35. ;; For full description, see <http://github.com/alezost/debpaste.el>.
  36. ;; For information about features provided by debian paste server,
  37. ;; read <http://paste.debian.net/rpc-interface.html>.
  38. ;;; Code:
  39. (require 'cl-macs)
  40. (require 'xml-rpc)
  41. (require 'url-expand)
  42. (defgroup debpaste nil
  43. "Emacs interface for debian paste server."
  44. :group 'xml-rpc
  45. :group 'url)
  46. ;;; URLs
  47. (defcustom debpaste-base-url "http://paste.debian.net/"
  48. "Root URL of the paste server."
  49. :type 'string
  50. :group 'debpaste)
  51. (defcustom debpaste-server-url
  52. (url-expand-file-name "server.pl" debpaste-base-url)
  53. "URL of the XML-RPC paste server."
  54. :type 'string
  55. :group 'debpaste)
  56. (defcustom debpaste-paste-url-regexp
  57. (concat "^" (regexp-quote debpaste-base-url)
  58. "/?" ; `debpaste-base-url' may end with "/" or not
  59. "\\([0-9]+\\)")
  60. "Regexp matching URL of a non-hidden paste.
  61. The first parenthesized expression should match ID of the paste."
  62. :type 'string
  63. :group 'debpaste)
  64. (defcustom debpaste-hidden-paste-url-regexp
  65. (concat "^" (regexp-quote debpaste-base-url)
  66. "/?hidden/\\([0-9a-f]+\\)")
  67. "Regexp matching URL of a hidden paste.
  68. The first parenthesized expression should match ID of the paste."
  69. :type 'string
  70. :group 'debpaste)
  71. (defun debpaste-get-id-by-url (url)
  72. "Return ID from a paste URL.
  73. Return nil if URL doesn't match `debpaste-paste-url-regexp' or
  74. `debpaste-hidden-paste-url-regexp'."
  75. (and (or (string-match debpaste-paste-url-regexp url)
  76. (string-match debpaste-hidden-paste-url-regexp url))
  77. (match-string 1 url)))
  78. ;;; Interaction with the paste server
  79. (defun debpaste-send-command (cmd &rest opts)
  80. "Send command CMD with options OPTS to the paste server.
  81. CMD is a symbol from `debpaste-commands'."
  82. (apply #'xml-rpc-method-call
  83. debpaste-server-url
  84. (debpaste-get-command-name cmd)
  85. opts))
  86. (cl-defun debpaste-action (&key cmd opts filters)
  87. "Send command to the server and make some actions with output.
  88. After sending command (method) CMD with option list OPTS, the
  89. paste server returns some information which is transformed into
  90. alist of parameters and values by `xml-rpc-method-call'. This
  91. alist (it is called \"info\" in this package) is passed as an
  92. argument to the first function from the list FILTERS, the
  93. returned result is passed to the second function from that list
  94. and so on.
  95. Each filter function should accept a single argument - info alist
  96. and should return info alist. Functions may add associations to
  97. the alist. In this case you might want to add descriptions of
  98. the added symbols into `debpaste-param-description-alist'.
  99. `debpaste-filter-intern' should be the first in the FILTERS list
  100. as other functions from this package use symbols for working with
  101. info parameters (see `debpaste-param-alist')."
  102. (let ((info (apply 'debpaste-send-command cmd opts)))
  103. (mapc (lambda (fun)
  104. (setq info (funcall fun info)))
  105. filters)))
  106. ;;; Server commands and returned parameters
  107. (defvar debpaste-command-alist
  108. '((get-paste . "paste.getPaste")
  109. (add-paste . "paste.addPaste")
  110. (del-paste . "paste.deletePaste")
  111. (get-langs . "paste.getLanguages")
  112. (add-short-url . "paste.addShortURL")
  113. (resolve-short-url . "paste.resolveShortURL")
  114. (short-url-clicks . "paste.ShortURLClicks"))
  115. "Association list of symbols and names of server commands (methods).
  116. Car of each assoc is a symbol used in code of this package;
  117. cdr - is a command name (string) sent to the paste server.")
  118. (defvar debpaste-param-alist
  119. '((ret . "rc")
  120. (status . "statusmessage")
  121. (text . "code")
  122. (lang . "lang")
  123. (submitter . "submitter")
  124. (id . "id")
  125. (base-url . "base_url")
  126. (view-url . "view_url")
  127. (download-url . "download_url")
  128. (delete-url . "delete_url")
  129. (digest . "digest")
  130. (hidden . "hidden")
  131. (submit-date . "submitdate")
  132. (expire-sec . "expiredate"))
  133. "Association list of symbols and names of info parameters.
  134. Car of each assoc is a symbol used in code of this package;
  135. cdr - is a parameter name (string) returned by paste server.")
  136. (defcustom debpaste-param-description-alist
  137. '((id . "ID")
  138. (ret . "Return code")
  139. (status . "Server message")
  140. (lang . "Language")
  141. (base-url . "Base URL")
  142. (view-url . "View URL")
  143. (download-url . "Download URL")
  144. (delete-url . "Delete URL")
  145. (digest . "Digest (SHA1 value for deleting a paste)")
  146. (hidden . "Hidden")
  147. (submitter . "Submitter")
  148. (submit-date . "Submitting date")
  149. (expire-date . "Expiration date")
  150. (expire-sec . "Expiration time (seconds)"))
  151. "Association list of symbols and descriptions of parameters.
  152. Descriptions are used for displaying paste information.
  153. Symbols are either from `debpaste-param-alist' or are added
  154. by filter functions. See `debpaste-action' for details."
  155. :type '(alist :key-type symbol :value-type string)
  156. :group 'debpaste)
  157. (defun debpaste-get-command-name (cmd)
  158. "Return a name (string) of a command CMD."
  159. (cdr (assoc cmd debpaste-command-alist)))
  160. (defun debpaste-get-param-name (param-symbol)
  161. "Return a name of a parameter PARAM-SYMBOL."
  162. (cdr (assoc param-symbol debpaste-param-alist)))
  163. (defun debpaste-get-param-symbol (param-name)
  164. "Return a symbol of a parameter PARAM-NAME."
  165. (car (rassoc param-name debpaste-param-alist)))
  166. (defun debpaste-get-param-description (param-symbol)
  167. "Return a description of a parameter PARAM-SYMBOL."
  168. (cdr (assoc param-symbol debpaste-param-description-alist)))
  169. (defun debpaste-get-param-val (param info)
  170. "Return a value of a parameter PARAM from paste INFO."
  171. (cdr (assoc param info)))
  172. ;;; General filters for processing info alist
  173. (defcustom debpaste-date-format
  174. "%Y-%m-%d %T"
  175. "Time format used to represent submit and expire dates of a paste.
  176. For information about time formats, see `format-time-string'."
  177. :type 'string
  178. :group 'debpaste)
  179. (defun debpaste-filter-intern (info)
  180. "Return INFO with names of parameters replaced with symbols.
  181. For names and symbols of parameters, see `debpaste-param-alist'."
  182. (delq nil
  183. (mapcar (lambda (param)
  184. (let* ((param-name (car param))
  185. (param-symbol (debpaste-get-param-symbol param-name))
  186. (param-val (cdr param)))
  187. (if param-symbol
  188. (cons param-symbol param-val)
  189. (message "Warning: paste server has returned unknown parameter %s. It will be omitted."
  190. param-name)
  191. nil)))
  192. info)))
  193. (defun debpaste-filter-error-check (info)
  194. "Check INFO for server errors.
  195. Return INFO."
  196. (if (= 0 (debpaste-get-param-val 'ret info))
  197. info
  198. (error "Server error: %s" (debpaste-get-param-val 'status info))))
  199. (defun debpaste-filter-hidden (info)
  200. "Substitute 0/1 with no/yes value for hidden parameter of INFO.
  201. Return INFO."
  202. (let ((val (debpaste-get-param-val 'hidden info)))
  203. (when val
  204. (cond
  205. ((equal 0 val)
  206. (setcdr (assoc 'hidden info) "no"))
  207. ((equal 1 val)
  208. (setcdr (assoc 'hidden info) "yes")))))
  209. info)
  210. (defun debpaste-filter-url (info)
  211. "Add \"http:\" string to url parameters from paste INFO.
  212. Return modified info."
  213. (mapc (lambda (param)
  214. (let ((param-name (symbol-name (car param)))
  215. (param-val (cdr param)))
  216. (and (string-match "url$" param-name)
  217. (setcdr param (concat "http:" param-val)))))
  218. info)
  219. info)
  220. (defun debpaste-filter-date (info)
  221. "Format dates of paste INFO with `debpaste-date-format'.
  222. Add `expire-date' symbol to the info alist.
  223. Return modified info."
  224. (let ((submit-time (date-to-time
  225. (debpaste-get-param-val 'submit-date info)))
  226. (expire-time (debpaste-get-param-val 'expire-sec info)))
  227. (setq expire-time
  228. (if (equal expire-time -1)
  229. "never"
  230. (format-time-string debpaste-date-format
  231. (time-add submit-time
  232. (seconds-to-time expire-time)))))
  233. (setcdr (assoc 'submit-date info)
  234. (format-time-string debpaste-date-format submit-time))
  235. (add-to-list 'info (cons 'expire-date expire-time))))
  236. ;;; Languages and major-modes
  237. ;; you can help with this, if you know what server language can be
  238. ;; used for highlighting a particular major-mode or what major-mode
  239. ;; suits a language better
  240. (defcustom debpaste-language-alist
  241. '(("Cucumber")
  242. ("abap")
  243. ("ada" . ada-mode)
  244. ("ahk")
  245. ("antlr")
  246. ("antlr-as")
  247. ("antlr-cpp" . c++-mode)
  248. ("antlr-csharp" . csharp-mode)
  249. ("antlr-java" . java-mode)
  250. ("antlr-objc" . objc-mode)
  251. ("antlr-perl" . perl-mode)
  252. ("antlr-python" . python-mode)
  253. ("antlr-ruby" . ruby-mode)
  254. ("apacheconf")
  255. ("applescript")
  256. ("as")
  257. ("as3")
  258. ("aspx-cs")
  259. ("aspx-vb")
  260. ("asy")
  261. ("awk" . awk-mode)
  262. ("basemake")
  263. ("bash" . sh-mode)
  264. ("bash" . shell-mode)
  265. ("bat")
  266. ("bbcode")
  267. ("befunge")
  268. ("blitzmax")
  269. ("boo")
  270. ("brainfuck")
  271. ("bro")
  272. ("c" . c-mode)
  273. ("c-objdump")
  274. ("cfengine3")
  275. ("cfm")
  276. ("cfs")
  277. ("cheetah")
  278. ("clojure" . clojure-mode)
  279. ("cmake" . cmake-mode)
  280. ("coffee-script")
  281. ("common-lisp" . lisp-mode)
  282. ("console")
  283. ("control")
  284. ("coq")
  285. ("cpp" . c++-mode)
  286. ("cpp-objdump" . c++-mode)
  287. ("csharp" . csharp-mode)
  288. ("css" . css-mode)
  289. ("css+django" . css-mode)
  290. ("css+erb" . css-mode)
  291. ("css+genshitext" . css-mode)
  292. ("css+mako" . css-mode)
  293. ("css+myghty" . css-mode)
  294. ("css+php" . css-mode)
  295. ("css+smarty" . css-mode)
  296. ("cython" . python-mode)
  297. ("d")
  298. ("d-objdump")
  299. ("dart")
  300. ("delphi" . delphi-mode)
  301. ("diff" . diff-mode)
  302. ("django" . python-mode)
  303. ("dpatch")
  304. ("dtd")
  305. ("duel")
  306. ("dylan")
  307. ("ec")
  308. ("ecl")
  309. ("elixir")
  310. ("erb")
  311. ("erl")
  312. ("erlang" . erlang-mode)
  313. ("evoque")
  314. ("factor")
  315. ("fan")
  316. ("fancy")
  317. ("felix")
  318. ("fortran" . fortran-mode)
  319. ("fsharp")
  320. ("gas")
  321. ("genshi")
  322. ("genshitext")
  323. ("glsl" . glsl-mode)
  324. ("gnuplot" . gnuplot-mode)
  325. ("go")
  326. ("gooddata-cl")
  327. ("gosu")
  328. ("groff" . nroff-mode)
  329. ("groovy")
  330. ("gst")
  331. ("haml")
  332. ("haskell" . haskell-mode)
  333. ("html" . html-mode)
  334. ("html+cheetah" . html-mode)
  335. ("html+django" . html-mode)
  336. ("html+evoque" . html-mode)
  337. ("html+genshi" . html-mode)
  338. ("html+mako" . html-mode)
  339. ("html+myghty" . html-mode)
  340. ("html+php" . html-mode)
  341. ("html+smarty" . html-mode)
  342. ("html+velocity" . html-mode)
  343. ("http")
  344. ("hx")
  345. ("hybris")
  346. ("iex")
  347. ("ini" . conf-mode)
  348. ("ini" . conf-colon-mode)
  349. ("ini" . conf-space-mode)
  350. ("ini" . conf-unix-mode)
  351. ("ini" . conf-windows-mode)
  352. ("ini" . conf-xdefaults-mode)
  353. ("io")
  354. ("ioke")
  355. ("irc")
  356. ("jade")
  357. ("java" . java-mode)
  358. ("js" . js-mode)
  359. ("js+cheetah" . js-mode)
  360. ("js+django" . js-mode)
  361. ("js+erb" . js-mode)
  362. ("js+genshitext" . js-mode)
  363. ("js+mako" . js-mode)
  364. ("js+myghty" . js-mode)
  365. ("js+php" . js-mode)
  366. ("js+smarty" . js-mode)
  367. ("json")
  368. ("jsp")
  369. ("kotlin")
  370. ("lhs")
  371. ("lighty")
  372. ("llvm")
  373. ("logtalk")
  374. ("lua" . lua-mode)
  375. ("make" . makefile-mode)
  376. ("make" . makefile-automake-mode)
  377. ("make" . makefile-bsdmake-mode)
  378. ("make" . makefile-gmake-mode)
  379. ("make" . makefile-imake-mode)
  380. ("make" . makefile-makepp-mode)
  381. ("mako")
  382. ("maql")
  383. ("mason")
  384. ("matlab" . matlab-mode)
  385. ("matlabsession")
  386. ("minid")
  387. ("modelica")
  388. ("modula2")
  389. ("moocode")
  390. ("moon")
  391. ("mupad")
  392. ("mxml" . nxml-mode)
  393. ("myghty")
  394. ("mysql" . sql-mode)
  395. ("nasm" . asm-mode)
  396. ("nemerle")
  397. ;; Debian paste service uses Pygments <http://pygments.org/> to
  398. ;; highlight languages for web-interface. As Pygments doesn't
  399. ;; support elisp yet, i decided to pick some rarely used language
  400. ;; (newlisp) for associating it with emacs-lisp. So when you post
  401. ;; a paste from emacs-lisp-mode, it is sent with "newlisp" as a
  402. ;; language parameter, and when this paste is received (with
  403. ;; `debpaste-display-paste') emacs-lisp-mode is enabled for it.
  404. ("newlisp" . emacs-lisp-mode)
  405. ("newlisp" . lisp-interaction-mode)
  406. ("newspeak")
  407. ("nginx")
  408. ("nimrod")
  409. ("numpy")
  410. ("objdump")
  411. ("objective-c" . objc-mode)
  412. ("objective-j")
  413. ("ocaml" . ocaml-mode)
  414. ("ocaml" . tuareg-mode)
  415. ("octave" . octave-mode)
  416. ("ooc")
  417. ("opa")
  418. ("openedge")
  419. ("perl" . perl-mode)
  420. ("php" . php-mode)
  421. ("plpgsql" . sql-mode)
  422. ("postgresql" . sql-mode)
  423. ("postscript")
  424. ("pot")
  425. ("pov")
  426. ("powershell")
  427. ("prolog" . prolog-mode)
  428. ("properties")
  429. ("protobuf")
  430. ("psql" . sql-mode)
  431. ("py3tb")
  432. ("pycon")
  433. ("pypylog")
  434. ("pytb")
  435. ("python" . inferior-python-mode)
  436. ("python" . jython-mode)
  437. ("python" . python-mode)
  438. ("python3" . python-mode)
  439. ("ragel")
  440. ("ragel-c")
  441. ("ragel-cpp" . c++-mode)
  442. ("ragel-d")
  443. ("ragel-em")
  444. ("ragel-java" . java-mode)
  445. ("ragel-objc" . objc-mode)
  446. ("ragel-ruby" . ruby-mode)
  447. ("raw")
  448. ("rb")
  449. ("rbcon")
  450. ("rconsole")
  451. ("rebol")
  452. ("redcode")
  453. ("rhtml" . html-mode)
  454. ("rst")
  455. ("sass")
  456. ("scala")
  457. ("scaml")
  458. ("scheme" . scheme-mode)
  459. ("scilab")
  460. ("scss")
  461. ("smalltalk")
  462. ("smarty")
  463. ("sml")
  464. ("snobol")
  465. ("sourceslist")
  466. ("splus")
  467. ("sql" . sql-mode)
  468. ("sqlite3" . sql-mode)
  469. ("squidconf")
  470. ("ssp")
  471. ("sv")
  472. ("tcl" . tcl-mode)
  473. ("tcsh")
  474. ("tea")
  475. ("tex" . tex-mode)
  476. ("text" . text-mode)
  477. ("trac-wiki")
  478. ("urbiscript")
  479. ("v")
  480. ("vala")
  481. ("vb.net")
  482. ("velocity")
  483. ("vhdl")
  484. ("vim")
  485. ("xml" . nxml-mode)
  486. ("xml+cheetah" . nxml-mode)
  487. ("xml+django" . nxml-mode)
  488. ("xml+erb" . nxml-mode)
  489. ("xml+evoque" . nxml-mode)
  490. ("xml+mako" . nxml-mode)
  491. ("xml+myghty" . nxml-mode)
  492. ("xml+php" . nxml-mode)
  493. ("xml+smarty" . nxml-mode)
  494. ("xml+velocity" . nxml-mode)
  495. ("xquery")
  496. ("xslt")
  497. ("yaml" . yaml-mode))
  498. "Association list of server languages and major modes."
  499. :type '(alist :key-type string :value-type symbol)
  500. :group 'debpaste)
  501. (defcustom debpaste-paste-language "text"
  502. "Default language used for a posting paste.
  503. It is used if there is no association for current major mode in
  504. `debpaste-language-alist'."
  505. :type 'string
  506. :group 'debpaste)
  507. (defun debpaste-get-lang-name (mode)
  508. "Return a name of the language for a major mode MODE.
  509. If there is no association for the MODE in
  510. `debpaste-language-alist', return `debpaste-paste-language'."
  511. (or (car (rassoc mode debpaste-language-alist))
  512. debpaste-paste-language))
  513. (defun debpaste-get-lang-mode (name)
  514. "Return a major mode of the paste language NAME.
  515. If there is no association for the NAME in
  516. `debpaste-language-alist', return `fundamental-mode'"
  517. (or (cdr (assoc name debpaste-language-alist))
  518. 'fundamental-mode))
  519. ;;; Debpaste buffers
  520. (defcustom debpaste-buffer-name-regexp
  521. "^\\*debpaste .*\\*$"
  522. "Regexp matching debpaste buffers.
  523. Used for killing and quitting debpaste buffers."
  524. :type 'string
  525. :group 'debpaste)
  526. (defcustom debpaste-received-paste-buffer-name-function
  527. 'debpaste-received-paste-buffer-name-default
  528. "Function returning the name of a buffer with a received paste.
  529. This function should accept one argument (info alist)."
  530. :type 'function
  531. :group 'debpaste)
  532. (defcustom debpaste-received-info-buffer-name-function
  533. 'debpaste-received-info-buffer-name-default
  534. "Function returning the name of a buffer with a received info.
  535. This function should accept one argument (info alist)."
  536. :type 'function
  537. :group 'debpaste)
  538. (defcustom debpaste-posted-info-buffer-name-function
  539. 'debpaste-posted-info-buffer-name-default
  540. "Function returning the name of a buffer with a posted info.
  541. This function should accept one argument (info alist)."
  542. :type 'function
  543. :group 'debpaste)
  544. (defun debpaste-received-paste-buffer-name-default (info)
  545. "Return the default name of a buffer for displaying received paste.
  546. INFO is alist of paste parameters."
  547. (format "*debpaste %s*"
  548. (debpaste-get-param-val 'id info)))
  549. (defun debpaste-received-info-buffer-name-default (info)
  550. "Return the default name of a buffer for displaying received info.
  551. INFO is alist of paste parameters."
  552. (format "*debpaste %s (received info)*"
  553. (debpaste-get-param-val 'id info)))
  554. (defun debpaste-posted-info-buffer-name-default (info)
  555. "Return the default name of a buffer for displaying posted info.
  556. INFO is alist of paste parameters."
  557. (format "*debpaste %s (posted info)*"
  558. (debpaste-get-param-val 'id info)))
  559. ;; wild name, isn't it?
  560. (defun debpaste-debpaste-bufferp (buffer-or-name)
  561. "Return non-nil if BUFFER-OR-NAME is a debpaste buffer.
  562. BUFFER-OR-NAME must be either a string (buffer name) or a buffer."
  563. (let ((name (if (bufferp buffer-or-name)
  564. (buffer-name buffer-or-name)
  565. buffer-or-name)))
  566. (string-match debpaste-buffer-name-regexp name)))
  567. (defun debpaste-kill-all-buffers ()
  568. "Kill all debpaste buffers.
  569. Buffers are defined by `debpaste-buffer-name-regexp'."
  570. (interactive)
  571. (mapc (lambda (buf)
  572. (and (debpaste-debpaste-bufferp buf)
  573. (kill-buffer buf)))
  574. (buffer-list))
  575. (message "All debpaste buffers were killed."))
  576. (defun debpaste-quit-buffers ()
  577. "Bury debpaste buffers.
  578. Buffers are defined by `debpaste-buffer-name-regexp'."
  579. (interactive)
  580. (mapc (lambda (win)
  581. ;; `select-window' is essential, otherwise (bury-buffer buf)
  582. ;; will not bury buffer
  583. (select-window win)
  584. (let ((buf (window-buffer win)))
  585. (and (debpaste-debpaste-bufferp buf)
  586. (bury-buffer))))
  587. (window-list)))
  588. ;;; Displaying info
  589. (defcustom debpaste-info-buffer-format "%s: %s\n"
  590. "String used to format each parameter for info displayed in buffer.
  591. It should contain 2 '%s'-sequences for a description and a value."
  592. :type 'string
  593. :group 'debpaste)
  594. (defcustom debpaste-info-minibuffer-format "%s: %s\n"
  595. "String used to format each parameter for info displayed in minibuffer.
  596. It should contain 2 '%s'-sequences for a description and a value."
  597. :type 'string
  598. :group 'debpaste)
  599. (defcustom debpaste-ignore-empty-params t
  600. "If non-nil, do not display empty parameters of a paste info."
  601. :type 'boolean
  602. :group 'debpaste)
  603. (defun debpaste-get-info-string (info fmt &optional params)
  604. "Return a string containing INFO.
  605. FMT is a string to format descriptions and values of parameters.
  606. PARAMS is a list of parameters included in a returned string. If
  607. it is not specified, show all info parameters (respecting
  608. `debpaste-ignore-empty-params').
  609. Parameters and their descriptions got from
  610. `debpaste-param-description-alist'."
  611. (unless params
  612. (setq params (mapcar #'car info)))
  613. (mapconcat (lambda (param)
  614. (let ((desc (debpaste-get-param-description param))
  615. (val (debpaste-get-param-val param info)))
  616. (if (and debpaste-ignore-empty-params
  617. (null val))
  618. "" ; delete empty params
  619. (format fmt desc (or val "")))))
  620. params
  621. ""))
  622. (defun debpaste-display-info-in-buffer (buffer-or-name info &optional params)
  623. "Display INFO in a separate buffer BUFFER-OR-NAME.
  624. Return INFO.
  625. BUFFER-OR-NAME may be a buffer or a string (a buffer name).
  626. Use `debpaste-info-buffer-format' to format displayed info.
  627. See `debpaste-get-info-string' for description of FMT and PARAMS."
  628. (let ((info-str (debpaste-get-info-string
  629. info debpaste-info-buffer-format params)))
  630. (unless (get-buffer buffer-or-name)
  631. (with-current-buffer (get-buffer-create buffer-or-name)
  632. (insert info-str)
  633. (text-mode) ;; i don't like this
  634. (view-mode) ;; and this (any suggestions?)
  635. (goto-char (point-min))))
  636. (let ((win (get-buffer-window buffer-or-name)))
  637. (if win
  638. (select-window win)
  639. (display-buffer buffer-or-name '((display-buffer-same-window))))))
  640. info)
  641. (defun debpaste-display-info-in-minibuffer (info &optional params)
  642. "Display INFO in the minibuffer.
  643. Return INFO.
  644. Use `debpaste-info-minibuffer-format' to format displayed info.
  645. See `debpaste-get-info-string' for description of FMT and PARAMS."
  646. (message (debpaste-get-info-string
  647. info debpaste-info-minibuffer-format params))
  648. info)
  649. ;;; Getting (receiving) a paste
  650. (defcustom debpaste-received-filter-functions
  651. '(debpaste-filter-intern debpaste-filter-error-check
  652. debpaste-add-id-to-info debpaste-filter-url debpaste-filter-date
  653. debpaste-save-last-received-info debpaste-display-received-paste
  654. debpaste-display-received-info-in-minibuffer)
  655. "List of functions for filtering info returned after receiving a paste.
  656. See `debpaste-action' for details."
  657. :type '(repeat function)
  658. :group 'debpaste)
  659. (defcustom debpaste-received-info-buffer-params
  660. '(id status lang submitter submit-date expire-date)
  661. "List of info parameters of a received paste displayed in buffer.
  662. If nil, display all parameters.
  663. Parameters are symbols from `debpaste-param-description-alist'."
  664. :type '(repeat symbol)
  665. :group 'debpaste)
  666. (defcustom debpaste-received-info-minibuffer-params
  667. '(submitter submit-date expire-date)
  668. "List of info parameters of a received paste displayed in minibuffer.
  669. If nil, display all parameters.
  670. Parameters are symbols from `debpaste-param-description-alist'."
  671. :type '(repeat symbol)
  672. :group 'debpaste)
  673. (defcustom debpaste-confirm-id-at-point nil
  674. "If non-nil, prompt for ID even if there is a paste URL at point.
  675. See `debpaste-display-paste'."
  676. :type 'boolean
  677. :group 'debpaste)
  678. (defvar-local debpaste-info nil
  679. "Alist with additional info of the current paste.
  680. Car of each assoc is a symbol from `debpaste-param-description-alist';
  681. cdr - is a value of that parameter.")
  682. (put 'debpaste-info 'permanent-local t) ; (info "(elisp) Creating Buffer-Local")
  683. (defvar debpaste-paste-id nil
  684. "Temporary value of a paste ID.")
  685. (defun debpaste-add-id-to-info (info)
  686. "Add id parameter to the INFO.
  687. Return modified info."
  688. ;; id parameter is vital for info; as the server doesn't return it,
  689. ;; we add it here using `debpaste-paste-id'
  690. ;; (`debpaste-display-paste' should bother about this variable)
  691. (or (debpaste-get-param-val 'id info)
  692. (add-to-list 'info (cons 'id debpaste-paste-id)))
  693. info)
  694. (defvar debpaste-last-received-info nil
  695. "Alist with info of the last received paste.")
  696. (defun debpaste-save-last-received-info (info)
  697. "Set `debpaste-last-received-info' to INFO value.
  698. Return INFO."
  699. (setq debpaste-last-received-info info))
  700. (defun debpaste-get-received-info ()
  701. "Return info of a received paste.
  702. If current buffer contains a received paste, return info of this
  703. paste; otherwise return info of the last received paste."
  704. (or (and (local-variable-p 'debpaste-info)
  705. debpaste-info)
  706. debpaste-last-received-info
  707. (error "You have not received pastes in this session")))
  708. (defun debpaste-display-received-info-in-buffer (info)
  709. "Display INFO of a received paste in a separate buffer.
  710. Return INFO.
  711. See `debpaste-get-received-info' for details.
  712. Display parameters from `debpaste-received-info-buffer-params'
  713. using `debpaste-info-buffer-format' to format info text.
  714. Use `debpaste-received-info-buffer-name-function' for buffer name."
  715. (interactive (list (debpaste-get-received-info)))
  716. (debpaste-display-info-in-buffer
  717. (funcall debpaste-received-info-buffer-name-function
  718. info)
  719. info debpaste-received-info-buffer-params))
  720. (defun debpaste-display-received-info-in-minibuffer (info)
  721. "Display INFO of a received paste in the minibuffer.
  722. Return INFO.
  723. See `debpaste-get-received-info' for details.
  724. Display parameters from `debpaste-received-info-minibuffer-params'
  725. using `debpaste-info-minibuffer-format' to format info text."
  726. (interactive (list (debpaste-get-received-info)))
  727. (debpaste-display-info-in-minibuffer
  728. info debpaste-received-info-minibuffer-params))
  729. (defun debpaste-display-received-paste (info)
  730. "Display text parameter from INFO in a debpaste buffer.
  731. Return INFO.
  732. Store additional info (without paste text) in a buffer-local
  733. `debpaste-info' variable."
  734. (let ((buf (get-buffer-create
  735. (funcall debpaste-received-paste-buffer-name-function
  736. info)))
  737. (paste-text (debpaste-get-param-val 'text info))
  738. (paste-info (cl-delete-if (lambda (param) (equal (car param) 'text))
  739. info))
  740. (mode (debpaste-get-lang-mode
  741. (debpaste-get-param-val 'lang info))))
  742. (with-current-buffer buf
  743. (erase-buffer)
  744. (insert paste-text)
  745. (and (fboundp mode) (funcall mode))
  746. (goto-char (point-min))
  747. (setq debpaste-info paste-info))
  748. (let ((win (get-buffer-window buf)))
  749. (if win
  750. (select-window win)
  751. (display-buffer buf '((display-buffer-same-window))))))
  752. info)
  753. ;;;###autoload
  754. (defun debpaste-display-paste (id)
  755. "Receive and display a paste with numeric or string ID.
  756. Interactively, prompt for ID unless there is a paste URL at point
  757. and `debpaste-confirm-id-at-point' is nil."
  758. (interactive
  759. (list (let* ((url (thing-at-point 'url))
  760. (id (and url (debpaste-get-id-by-url url))))
  761. (if (or (null id) debpaste-confirm-id-at-point)
  762. (read-string "Paste ID: " id)
  763. id))))
  764. (when (numberp id)
  765. (setq id (number-to-string id)))
  766. (let ((debpaste-paste-id id))
  767. (debpaste-action :cmd 'get-paste
  768. :opts (list id)
  769. :filters debpaste-received-filter-functions)))
  770. ;;; Adding (posting) a paste
  771. (defcustom debpaste-posted-filter-functions
  772. '(debpaste-filter-intern debpaste-filter-error-check debpaste-filter-hidden
  773. debpaste-filter-url debpaste-save-last-posted-info
  774. debpaste-posted-kill-url-display-summary)
  775. "List of functions for filtering info returned after posting a paste.
  776. See `debpaste-action' for details."
  777. :type '(repeat function)
  778. :group 'debpaste)
  779. (defcustom debpaste-posted-info-buffer-params
  780. '(id digest hidden view-url download-url delete-url)
  781. "List of info parameters of a posted paste displayed in buffer.
  782. If nil, display all parameters.
  783. Parameters are symbols from `debpaste-param-description-alist'."
  784. :type '(repeat symbol)
  785. :group 'debpaste)
  786. (defcustom debpaste-posted-info-minibuffer-params
  787. '(download-url delete-url)
  788. "List of info parameters of a posted paste displayed in minibuffer.
  789. If nil, display all parameters.
  790. Parameters are symbols from `debpaste-param-description-alist'."
  791. :type '(repeat symbol)
  792. :group 'debpaste)
  793. (defcustom debpaste-completing-read-function
  794. 'ido-completing-read
  795. "Function for reading a string in the minibuffer.
  796. It is used to prompt for paste options.
  797. Usual values are: `completing-read' or `ido-completing-read'."
  798. :type 'function
  799. :group 'debpaste)
  800. (defcustom debpaste-user-name "anonymous"
  801. "Default user name used for a posting paste."
  802. :type 'string
  803. :group 'debpaste)
  804. (defcustom debpaste-expire-time (* 24 3600)
  805. "Default expiration time (in seconds) for a posting paste."
  806. :type 'integer
  807. :group 'debpaste)
  808. (defcustom debpaste-expire-time-alist
  809. `((3600 . "1 hour")
  810. (,(* 12 3600) . "12 hours")
  811. (,(* 24 3600) . "1 day")
  812. (,(* 3 24 3600) . "3 days")
  813. (,(* 7 24 3600) . "7 days")
  814. (-1 . "never"))
  815. "Association list of time values (in seconds) and strings for prompting."
  816. :type '(alist :key-type integer :value-type string)
  817. :group 'debpaste)
  818. (defcustom debpaste-paste-is-hidden nil
  819. "If non-nil, post hidden pastes (not shown on a frontpage)."
  820. :type 'boolean
  821. :group 'debpaste)
  822. (defcustom debpaste-prompted-paste-options nil
  823. "List of prompted options for a posting paste.
  824. If list, prompt for values of options specified in this list and
  825. use default values for other options. Each option from the list
  826. is one of these symbols: `user', `expire-sec', `lang', `hidden'.
  827. If nil, use default values for all paste options without
  828. prompting."
  829. :type '(repeat symbol)
  830. :group 'debpaste)
  831. (defun debpaste-prompt-for-user-name ()
  832. "Prompt for and return user name for a posting paste."
  833. (funcall debpaste-completing-read-function
  834. "User name: " nil nil nil debpaste-user-name))
  835. (defun debpaste-prompt-for-expire-time ()
  836. "Prompt for and return expiration time for a posting paste."
  837. (let ((time (funcall debpaste-completing-read-function
  838. "Expiration time (seconds or completion value): "
  839. (mapcar #'cdr debpaste-expire-time-alist))))
  840. (or (car (rassoc time debpaste-expire-time-alist))
  841. (string-to-number time))))
  842. (defun debpaste-prompt-for-lang ()
  843. "Prompt for and return language for a posting paste."
  844. (funcall debpaste-completing-read-function
  845. "Language: "
  846. (mapcar #'car debpaste-language-alist)
  847. nil nil nil nil (debpaste-get-lang-name major-mode)))
  848. (defun debpaste-prompt-for-hidden ()
  849. "Prompt for and return hidden option (t or nil) for a posting paste."
  850. (y-or-n-p "Hidden paste?"))
  851. (defun debpaste-url-to-kill-ring (info)
  852. "Add view-url parameter from INFO to the `kill-ring'.
  853. Return INFO."
  854. (kill-new (debpaste-get-param-val 'view-url info))
  855. info)
  856. ;;;###autoload
  857. (defun debpaste-paste-region (start end)
  858. "Send a text between START and END to the paste server.
  859. Interactively use current region.
  860. Prompt for additional options specified in
  861. `debpaste-prompted-paste-options'.
  862. With \\[universal-argument] prompt for all paste options."
  863. (interactive "r")
  864. (let ((opt-list (if (equal current-prefix-arg '(4))
  865. '(user expire-sec lang hidden)
  866. debpaste-prompted-paste-options)))
  867. (cl-flet ((get-opt-val (opt default prompt-fun &rest args)
  868. (if (member opt opt-list)
  869. (apply prompt-fun args)
  870. default)))
  871. (let ((text (buffer-substring-no-properties start end))
  872. (user (get-opt-val 'user debpaste-user-name
  873. 'debpaste-prompt-for-user-name))
  874. (expire (get-opt-val 'expire-sec debpaste-expire-time
  875. 'debpaste-prompt-for-expire-time))
  876. (lang (get-opt-val 'lang (debpaste-get-lang-name major-mode)
  877. 'debpaste-prompt-for-lang))
  878. (hidden (if (get-opt-val 'hidden debpaste-paste-is-hidden
  879. 'debpaste-prompt-for-hidden)
  880. 1
  881. 0)))
  882. (debpaste-action :cmd 'add-paste
  883. :opts (list text user expire lang hidden)
  884. :filters debpaste-posted-filter-functions)))))
  885. ;;;###autoload
  886. (defun debpaste-paste-buffer (buffer-or-name)
  887. "Send a buffer BUFFER-OR-NAME to the paste server.
  888. Interactively use current buffer."
  889. (interactive (list (current-buffer)))
  890. (with-current-buffer buffer-or-name
  891. (debpaste-paste-region (point-min) (point-max))))
  892. (defvar debpaste-last-posted-info nil
  893. "Alist with info about last posted paste.")
  894. (defun debpaste-save-last-posted-info (info)
  895. "Set `debpaste-last-posted-info' to INFO value.
  896. Return INFO."
  897. (setq debpaste-last-posted-info info))
  898. (defun debpaste-get-posted-info ()
  899. "Return info of a last posted paste."
  900. (or debpaste-last-posted-info
  901. (error "You have not posted pastes in this session")))
  902. (defun debpaste-display-posted-info-in-buffer (info)
  903. "Display INFO of a posted paste in a separate buffer.
  904. Return INFO.
  905. Interactively, display info of the last posted paste.
  906. Display parameters from `debpaste-posted-info-buffer-params'
  907. using `debpaste-info-buffer-format' to format info text.
  908. Use `debpaste-posted-info-buffer-name-function' for buffer name."
  909. (interactive (list (debpaste-get-posted-info)))
  910. (debpaste-display-info-in-buffer
  911. (funcall debpaste-posted-info-buffer-name-function
  912. info)
  913. info debpaste-posted-info-buffer-params))
  914. (defun debpaste-display-posted-info-in-minibuffer (info)
  915. "Display INFO of a posted paste in the minibuffer.
  916. Return INFO.
  917. Interactively, display info of the last posted paste.
  918. Display parameters from `debpaste-posted-info-minibuffer-params'
  919. using `debpaste-info-minibuffer-format' to format info text."
  920. (interactive (list (debpaste-get-posted-info)))
  921. (message "Your paste has been posted successfully.\n%s"
  922. (debpaste-get-info-string
  923. info debpaste-info-minibuffer-format
  924. debpaste-posted-info-minibuffer-params))
  925. info)
  926. (defun debpaste-posted-kill-url-display-summary (info)
  927. "Add paste URL to the `kill-ring' and display paste summary.
  928. Return INFO.
  929. Interactively, use info of the last posted paste.
  930. See `debpaste-url-to-kill-ring'."
  931. (interactive (list (debpaste-get-posted-info)))
  932. (debpaste-url-to-kill-ring info)
  933. (message "Your paste has been posted successfully.
  934. Paste URL has been added to the kill ring.\n%s"
  935. (debpaste-get-info-string
  936. info debpaste-info-minibuffer-format
  937. '(digest)))
  938. info)
  939. ;;; Deleting a paste
  940. (defcustom debpaste-deleted-filter-functions
  941. '(debpaste-filter-intern debpaste-filter-error-check
  942. debpaste-display-deleted-info-in-minibuffer)
  943. "List of functions for filtering info returned after deleting a paste.
  944. See `debpaste-action' for details."
  945. :type '(repeat function)
  946. :group 'debpaste)
  947. (defun debpaste-display-deleted-info-in-minibuffer (info)
  948. "Display info about deleted paste.
  949. Return INFO."
  950. (debpaste-display-info-in-minibuffer
  951. info '(status)))
  952. ;;;###autoload
  953. (defun debpaste-delete-paste (digest)
  954. "Delete a paste with specified DIGEST from the paste server.
  955. Interactively, prompt for DIGEST. If there is SHA1 digest at
  956. point, it will be used as initial input.
  957. You receive DIGEST after posting a paste."
  958. (interactive
  959. (list (read-string "Paste digest: "
  960. (debpaste-sha1-at-point))))
  961. (debpaste-action :cmd 'del-paste
  962. :opts (list digest)
  963. :filters debpaste-deleted-filter-functions))
  964. (defun debpaste-sha1-at-point ()
  965. "Return the SHA1 digest at point, or nil if none is found.
  966. SHA1 (Secure Hash Algorithm) digest is a word of 40 hexadecimal symbols."
  967. (let ((word (thing-at-point 'word)))
  968. (and word
  969. (string-match "^[0-9a-f]\\{40\\}$" word)
  970. word)))
  971. ;;; Keymap with commands
  972. ;;;###autoload
  973. (defvar debpaste-command-map
  974. (let ((map (make-sparse-keymap)))
  975. (define-key map "r" 'debpaste-display-paste) ; receive
  976. (define-key map "g" 'debpaste-display-paste) ; get
  977. (define-key map "p" 'debpaste-paste-region) ; post
  978. (define-key map "a" 'debpaste-paste-region) ; add
  979. (define-key map "P" 'debpaste-paste-buffer)
  980. (define-key map "A" 'debpaste-paste-buffer)
  981. (define-key map "d" 'debpaste-delete-paste)
  982. (define-key map "ir" 'debpaste-display-received-info-in-buffer)
  983. (define-key map "ig" 'debpaste-display-received-info-in-buffer)
  984. (define-key map "ip" 'debpaste-display-posted-info-in-buffer)
  985. (define-key map "ia" 'debpaste-display-posted-info-in-buffer)
  986. (define-key map "k" 'debpaste-kill-all-buffers)
  987. (define-key map "q" 'debpaste-quit-buffers)
  988. map)
  989. "Keymap for debpaste commands.")
  990. ;;;###autoload
  991. (fset 'debpaste-command-map debpaste-command-map)
  992. (provide 'debpaste)
  993. ;;; debpaste.el ends here