Advertisement
asvil

Untitled

Nov 13th, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 8.39 KB | None | 0 0
  1. ;;; pastebin.el --- A simple interface to the www.pastebin.com webservice
  2.  
  3. ;;; Copyright (C) 2008 by Nic Ferrier <nic@ferrier.me.uk>
  4. ;;; Copyright (C) 2010 by Ivan Korotkov <twee@tweedle-dee.org>
  5. ;;; Copyright (C) 2012 by Filonenko Michael <filonenko.mikhail@gmail.com>
  6.  
  7. ;;; This program is free software; you can redistribute it and/or modify
  8. ;;; it under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 2, or (at your option)
  10. ;;; any later version.
  11.  
  12. ;;; This program is distributed in the hope that it will be useful,
  13. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ;;; GNU General Public License for more details.
  16.  
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with this program; see the file COPYING.  If not, write to the
  19. ;;; Free Software Foundation, Inc.,   51 Franklin Street, Fifth Floor,
  20. ;;; Boston, MA  02110-1301  USA
  21.  
  22. ;;; Commentary:
  23. ;;;
  24. ;;; Make pastebin account:
  25. ;;;
  26. ;;;   http://pastebin.com/signup
  27. ;;;
  28. ;;; Get Unique developer key:
  29. ;;;
  30. ;;;   http://pastebin.com/api  
  31. ;;;
  32. ;;; Setup emacs:
  33. ;;;
  34. ;;;   (require 'pastebin)
  35. ;;;   (setq pastebin-unique-developer-api-key "HEX")
  36. ;;;   (setq pastebin-user-name "username")
  37. ;;;   (setq pastebin-password "password")
  38. ;;;
  39. ;;;   (pastebin-login)
  40. ;;; To send the whole buffer or select a region and run
  41. ;;;
  42. ;;;  M-x pastebin
  43. ;;;
  44. ;;; In either case the url that pastebin generates is left on the kill
  45. ;;; ring and the paste buffer.
  46.  
  47.  
  48. ;;; Code:
  49.  
  50. (defgroup pastebin nil
  51.   "Pastebin -- pastebin.com client"
  52.   :tag "Pastebin"
  53.   :group 'tools)
  54.  
  55. (defcustom pastebin-unique-developer-api-key "INSERT_YOURS"
  56.   "Everybody using our API is required to use a valid Developer
  57. API Key. You automaticly get a key when you become a member of
  58. Pastebin. http://pastebin.com/api"
  59.   :type 'string
  60.   :group 'pastebin)
  61.  
  62. (defcustom pastebin-user-name "guest"
  63.   "Username of the user you want to login."
  64.   :type 'string
  65.   :group 'pastebin)
  66.  
  67. (defcustom pastebin-password "guest"
  68.   "Password of the user you want to login."
  69.   :type 'string
  70.   :group 'pastebin)
  71.  
  72. (defcustom pastebin-post-request-login-url "http://pastebin.com/api/api_login.php"
  73.   "Login url"
  74.   :type 'string
  75.   :group 'pastebin)
  76.  
  77. (defcustom pastebin-post-request-paste-url "http://pastebin.com/api/api_post.php"
  78.   "Paste url"
  79.   :type 'string
  80.   :group 'pastebin)
  81.  
  82. (defcustom pastebin-user-api-key "INSERT_YOURS"
  83.   "Key for pastebin members"
  84.   :type 'string
  85.   :group 'pastebin)
  86.  
  87. (defun pastebin-login ()
  88.   (let* ((params (concat "api_dev_key=" pastebin-unique-developer-api-key
  89.                          "&api_user_name=" (url-hexify-string pastebin-user-name)
  90.                          "&api_user_password=" (url-hexify-string pastebin-password)))
  91.          (url-request-method "POST")
  92.          (url-request-extra-headers
  93.           '(("Content-Type" . "application/x-www-form-urlencoded")))
  94.          (url-request-data params)
  95.          (content-buf (url-retrieve
  96.                        pastebin-post-request-login-url
  97.                        (lambda (arg)
  98.                          (cond
  99.                           ((equal :error (car arg))
  100.                            (signal 'pastebin-error (cdr arg)))
  101.                           (t
  102.                            (re-search-forward "\n\n")
  103.                            (clipboard-kill-ring-save (point) (point-max))
  104.                            (setq pastebin-user-api-key (buffer-substring (point) (point-max)))))))))))
  105.  
  106.  
  107. (defcustom pastebin-type-assoc
  108.   '((actionscript-mode . " actionscript")
  109.     (ada-mode . "ada")
  110.     (asm-mode . "asm")
  111.     (autoconf-mode . "bash")
  112.     (bibtex-mode . "bibtex")
  113.     (cmake-mode . "cmake")
  114.     (c-mode . "c")
  115.     (c++-mode . "cpp")
  116.     (cobol-mode . "cobol")
  117.     (conf-colon-mode . "properties")
  118.     (conf-javaprop-mode . "properties")
  119.     (conf-mode . "ini")
  120.     (conf-space-mode . "properties")
  121.     (conf-unix-mode . "ini")
  122.     (conf-windows-mode . "ini")
  123.     (cperl-mode . "perl")
  124.     (csharp-mode . "csharp")
  125.     (css-mode . "css")
  126.     (delphi-mode . "delphi")
  127.     (diff-mode . "dff")
  128.     (ebuild-mode . "bash")
  129.     (eiffel-mode . "eiffel")
  130.     (emacs-lisp-mode . "lisp")
  131.     (erlang-mode . "erlang")
  132.     (erlang-shell-mode . "erlang")
  133.     (espresso-mode . "javascript")
  134.     (fortran-mode . "fortran")
  135.     (glsl-mode . "glsl")
  136.     (gnuplot-mode . "gnuplot")
  137.     (graphviz-dot-mode . "dot")
  138.     (haskell-mode . "haskell")
  139.     (html-mode . "html4strict")
  140.     (idl-mode . "idl")
  141.     (inferior-haskell-mode . "haskell")
  142.     (inferior-octave-mode . "octave")
  143.     (inferior-python-mode . "python")
  144.     (inferior-ruby-mode . "ruby")
  145.     (java-mode . "java")
  146.     (js2-mode . "javascript")
  147.     (jython-mode . "python")
  148.     (latex-mode . "latex")
  149.     (lisp-mode . "lisp")
  150.     (lua-mode . "lua")
  151.     (makefile-mode . "make")
  152.     (makefile-automake-mode . "make")
  153.     (makefile-gmake-mode . "make")
  154.     (makefile-makepp-mode . "make")
  155.     (makefile-bsdmake-mode . "make")
  156.     (makefile-imake-mode . "make")
  157.     (matlab-mode . "matlab")
  158.     (nxml-mode . "xml")
  159.     (oberon-mode . "oberon2")
  160.     (objc-mode . "objc")
  161.     (ocaml-mode . "ocaml")
  162.     (octave-mode . "matlab")
  163.     (pascal-mode . "pascal")
  164.     (perl-mode . "perl")
  165.     (php-mode . "php")
  166.     (plsql-mode . "plsql")
  167.     (po-mode . "gettext")
  168.     (prolog-mode . "prolog")
  169.     (python-2-mode . "python")
  170.     (python-3-mode . "python")
  171.     (python-basic-mode . "python")
  172.     (python-mode . "python")
  173.     (ruby-mode . "ruby")
  174.     (scheme-mode . "lisp")
  175.     (shell-mode . "bash")
  176.     (sh-mode . "bash")
  177.     (smalltalk-mode . "smalltalk")
  178.     (sql-mode . "sql")
  179.     (tcl-mode . "tcl")
  180.     (visual-basic-mode . "vb")
  181.     (xml-mode . "xml")
  182.     (yaml-mode . "properties")
  183.     (text-mode . "text"))
  184.   "Alist composed of major-mode names and corresponding pastebin highlight formats."
  185.   :type '(alist :key-type symbol :value-tupe string)
  186.   :group 'pastebin)
  187.  
  188. (defcustom pastebin-type-assoc
  189.   '((never . "N")) ;;    N = Never
  190.     (ten-minutes . "10M")             ;; 10M = 10 Minutes
  191.     (one-hour . "1H")  ;; 1H = 1 Hour
  192.     (one-day . "1D")     ;; 1D = 1 Day
  193.     (one-month . "1M")     ;; 1M = 1 Month
  194.     "We have 5 valid values available which you can use with the 'api_paste_expire_date' parameter."
  195.     :type '(alist :key-type symbol :value-tupe string)
  196.     :group 'pastebin)
  197.  
  198. (defvar pastebin-domain-history '())
  199.  
  200. (defun pastebin (start end &optional name)
  201.   "Send the region to the pastebin service specified.
  202.  
  203. See pastebin.com for more information about pastebin.
  204.  
  205. Called interactively pastebin uses the current region for
  206. preference for sending... if the mark is NOT set then the entire
  207. buffer is sent.
  208.  
  209. Argument START is the start of region.
  210. Argument END is the end of region.
  211.  
  212. If domain is used pastebin prompts for a domain defaulting to
  213. 'pastebin-default-domain' so you can send requests or use a
  214. different domain.
  215. "
  216.   (interactive
  217.    (if (mark)
  218.        (list (region-beginning) (region-end) nil)
  219.      (list (point-min) (point-max) nil)))
  220.   ;; Main function
  221.   (print (buffer-substring-no-properties start end))
  222.   (let* ((data (buffer-substring-no-properties start end))
  223.          (params (concat "api_dev_key=" pastebin-unique-developer-api-key
  224.                          "&api_user_key=" pastebin-user-api-key
  225.                          "&api_option=" "paste"
  226.                          "&api_paste_code=" (url-hexify-string data)
  227.                          "&api_paste_format=" (or
  228.                                                (assoc-default major-mode pastebin-type-assoc nil "text")
  229.                                                "text")
  230.                          (if name
  231.                              (concat "&api_paste_name=" (url-hexify-string "pastebin.el"))
  232.                            "")
  233.                          ))
  234.          (url-request-method "POST")
  235.          (url-request-extra-headers
  236.           '(("Content-Type" . "application/x-www-form-urlencoded")))
  237.          (url-request-data
  238.           params)
  239.          (content-buf (url-retrieve
  240.                        pastebin-post-request-paste-url
  241.                        (lambda (arg)
  242.                          (cond
  243.                           ((equal :error (car arg))
  244.                            (signal 'pastebin-error (cdr arg)))
  245.                           (t
  246.                            (re-search-forward "\n\n")
  247.                            (clipboard-kill-ring-save (point) (point-max))
  248.                            (message "Pastebin URL: %s" (buffer-substring (point) (point-max)))))))))))
  249.  
  250. (provide 'pastebin)
  251. ;;; pastebin.el ends here
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement