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