Difference between revisions of "Hpaste.el"

From HaskellWiki
Jump to navigation Jump to search
(mentioned and linked to the makefile twb put on the talk page)
(update)
Line 1: Line 1:
 
hpaste.el is an Emacs Lisp library that integrates [http://hpaste.org hpaste] into Emacs. It provides two functions, <code>hpaste-paste-region</code> and <code>hpaste-paste-buffer</code>, which send the region or buffer to the hpaste server as required. Most things are customisable: do M-x customize and browse to the Hpaste group to see what you can change. Code is available under the GPL license. (see the [[Talk:Hpaste.el| talk page]] for a makefile allowing you to keep up-to-date on this programatically). Enjoy!
 
hpaste.el is an Emacs Lisp library that integrates [http://hpaste.org hpaste] into Emacs. It provides two functions, <code>hpaste-paste-region</code> and <code>hpaste-paste-buffer</code>, which send the region or buffer to the hpaste server as required. Most things are customisable: do M-x customize and browse to the Hpaste group to see what you can change. Code is available under the GPL license. (see the [[Talk:Hpaste.el| talk page]] for a makefile allowing you to keep up-to-date on this programatically). Enjoy!
  +
  +
: '''Update 13 Dec 07''': fixed problems with "Wrong type argument: stringp, nil" appearing after pasting.
   
 
<pre-lisp>
 
<pre-lisp>
 
;;; hpaste.el -- Integration with hpaste: http://hpaste.org.
 
;;; hpaste.el -- Integration with hpaste: http://hpaste.org.
  +
 
 
;; Author: David House <dmhouse@gmail.com>
 
;; Author: David House <dmhouse@gmail.com>
 
;; Created: 14th April 2007
 
;; Created: 14th April 2007
 
;; Version: 1.0
 
;; Version: 1.0
 
;; License: GPL
 
;; License: GPL
  +
 
 
(require 'url)
 
(require 'url)
  +
 
 
(defgroup hpaste nil "Integration with the hpaste pastebin")
 
(defgroup hpaste nil "Integration with the hpaste pastebin")
 
(defcustom hpaste-server "http://hpaste.org"
 
(defcustom hpaste-server "http://hpaste.org"
Line 32: Line 34:
 
(const :tag "Never announce" never))
 
(const :tag "Never announce" never))
 
:group 'hpaste)
 
:group 'hpaste)
  +
 
 
(defvar hpaste-last-paste-id nil
 
(defvar hpaste-last-paste-id nil
 
"Numerical ID of the last paste.")
 
"Numerical ID of the last paste.")
  +
 
(defun hpaste-after-paste (&optional redirect url)
+
(defun hpaste-after-paste (&optional redirect)
 
"Callback that runs after a paste is made. Messages the user
 
"Callback that runs after a paste is made. Messages the user
 
and tell them that everything went smoothly, and save the paste
 
and tell them that everything went smoothly, and save the paste
 
ID for use as a default ID for annotations."
 
ID for use as a default ID for annotations."
 
(message "Paste successful")
 
(message "Paste successful")
(if redirect
+
(if (eq (car redirect) ':redirect)
 
(progn
 
(progn
  +
(setq url (cadr redirect))
 
(string-match "/\\([0-9]*\\)\\(#.*\\)?$" url)
 
(string-match "/\\([0-9]*\\)\\(#.*\\)?$" url)
 
(let ((id (match-string 1 url)))
 
(let ((id (match-string 1 url)))
 
(if id (setq hpaste-last-paste-id id))))))
 
(if id (setq hpaste-last-paste-id id))))))
  +
 
 
(defun hpaste-prompt-for-annotate ()
 
(defun hpaste-prompt-for-annotate ()
 
"Ask the user whether they want to send the paste as an
 
"Ask the user whether they want to send the paste as an
Line 60: Line 63:
 
(input (read-from-minibuffer prompt)))
 
(input (read-from-minibuffer prompt)))
 
(if (> (length input) 0) input hpaste-last-paste-id))))
 
(if (> (length input) 0) input hpaste-last-paste-id))))
  +
 
 
(defun hpaste-paste-region (beg end)
 
(defun hpaste-paste-region (beg end)
 
"Send the region to the hpaste server specified in
 
"Send the region to the hpaste server specified in
Line 70: Line 73:
 
#haskell on Freenode according to `hpaste-announce', see the
 
#haskell on Freenode according to `hpaste-announce', see the
 
docstring of that variable for more information.
 
docstring of that variable for more information.
  +
 
 
For more information on hpaste, see http://hpaste.org"
 
For more information on hpaste, see http://hpaste.org"
 
(interactive "r")
 
(interactive "r")
Line 81: Line 84:
 
"&announce=true"
 
"&announce=true"
 
""))
 
""))
  +
 
 
(url (concat hpaste-server
 
(url (concat hpaste-server
 
(if annot-id (concat "/annotate/" annot-id)
 
(if annot-id (concat "/annotate/" annot-id)
Line 95: Line 98:
 
announce)))
 
announce)))
 
(url-retrieve url 'hpaste-after-paste)))
 
(url-retrieve url 'hpaste-after-paste)))
  +
 
 
(defun hpaste-paste-buffer ()
 
(defun hpaste-paste-buffer ()
 
"Like `hpaste-paste-region', but paste the entire buffer instead."
 
"Like `hpaste-paste-region', but paste the entire buffer instead."
 
(interactive)
 
(interactive)
 
(hpaste-paste-region (point-min) (point-max)))
 
(hpaste-paste-region (point-min) (point-max)))
  +
 
 
(provide 'hpaste)
 
(provide 'hpaste)
 
</pre-lisp>
 
</pre-lisp>

Revision as of 15:39, 13 December 2007

hpaste.el is an Emacs Lisp library that integrates hpaste into Emacs. It provides two functions, hpaste-paste-region and hpaste-paste-buffer, which send the region or buffer to the hpaste server as required. Most things are customisable: do M-x customize and browse to the Hpaste group to see what you can change. Code is available under the GPL license. (see the talk page for a makefile allowing you to keep up-to-date on this programatically). Enjoy!

Update 13 Dec 07: fixed problems with "Wrong type argument: stringp, nil" appearing after pasting.

<pre-lisp>

hpaste.el -- Integration with hpaste
http://hpaste.org.
Author
David House <dmhouse@gmail.com>
Created
14th April 2007
Version
1.0
License
GPL

(require 'url)

(defgroup hpaste nil "Integration with the hpaste pastebin") (defcustom hpaste-server "http://hpaste.org"

 "Base URL for the hpaste server."
 :type '(string)
 :group 'hpaste)

(defcustom hpaste-default-nick nil

 "What to tell the server your nick is. If NIL, then prompt every time."
 :type '(choice (string) (const :tag "Ask every time" nil))
 :group 'hpaste)

(defcustom hpaste-blank-title nil

 "If non-NIL, don't send a title to the server."
 :type '(boolean)
 :group 'hpaste)

(defcustom hpaste-announce 'ask

 "Whether to announce the paste in the #haskell channel on

Freenode. If ALWAYS, then announce every time. If ASK, then prompt every time. If NEVER, then never announce."

 :type '(choice (const :tag "Always announce" always)
                (const :tag "Ask each time" ask) 
                (const :tag "Never announce" never))
 :group 'hpaste)

(defvar hpaste-last-paste-id nil

 "Numerical ID of the last paste.")

(defun hpaste-after-paste (&optional redirect)

 "Callback that runs after a paste is made. Messages the user

and tell them that everything went smoothly, and save the paste ID for use as a default ID for annotations."

 (message "Paste successful")
 (if (eq (car redirect) ':redirect)
     (progn 
       (setq url (cadr redirect))
       (string-match "/\\([0-9]*\\)\\(#.*\\)?$" url)
       (let ((id (match-string 1 url)))
         (if id (setq hpaste-last-paste-id id))))))

(defun hpaste-prompt-for-annotate ()

 "Ask the user whether they want to send the paste as an

annotation, and if so, the ID of the paste to annotate (defaulting to the last paste made through this interface)."

 (if (y-or-n-p "Send as annotation? ")
     (let* ((prompt
             (if hpaste-last-paste-id
                 (format "Paste to annotate (default %s): "
                         hpaste-last-paste-id)
               "Paste to annotate: "))
            (input (read-from-minibuffer prompt)))
       (if (> (length input) 0) input hpaste-last-paste-id))))

(defun hpaste-paste-region (beg end)

 "Send the region to the hpaste server specified in

`hpaste-server'. Use the nick in `hpaste-default-nick', or prompt for one if that is NIL. You can still appear as (anonymous) by just not filling out a nick when prompted (just hit RET). Prompt for a title, unless `hpaste-blank-title' is non-NIL, in which case just send a blank title. Pastes will be announced in

  1. haskell on Freenode according to `hpaste-announce', see the

docstring of that variable for more information.

For more information on hpaste, see http://hpaste.org"

 (interactive "r")
 (let* ((nick (or hpaste-default-nick (read-from-minibuffer "Nick: ")))
        (title (if hpaste-blank-title "" (read-from-minibuffer "Title: ")))
        (annot-id (hpaste-prompt-for-annotate))
        (announce (if (or (eq hpaste-announce 'always)
                          (and (eq hpaste-announce 'ask)
                               (y-or-n-p "Announce paste? ")))
                      "&announce=true"
                    ""))

        (url (concat hpaste-server
                     (if annot-id (concat "/annotate/" annot-id)
                       "/new")))
        (url-request-method "POST")
        (url-request-extra-headers
         '(("Content-Type" . "application/x-www-form-urlencoded")))
        (url-request-data
         (format "content=%s&nick=%s&title=%s%s&x=0&y=0\r\n"
                 (url-hexify-string (buffer-substring-no-properties beg end))
                 (url-hexify-string nick)
                 (url-hexify-string title)
                 announce)))
   (url-retrieve url 'hpaste-after-paste)))

(defun hpaste-paste-buffer ()

 "Like `hpaste-paste-region', but paste the entire buffer instead."
 (interactive)
 (hpaste-paste-region (point-min) (point-max)))

(provide 'hpaste) </pre-lisp>