08 October 2009

Emacs -> Sprunge

Woo code! I got bored and wrote a function for Emacs that posts the current buffer to sprunge. Since I'm usually pasting IRC snippets, I also threw together a major mode with 4 regexps to syntax highlight IRC. Behold:

Here's the elisp source. The hash table maps major mode names to the corresponding pygments lexer name, which sprunge uses for syntax highlighting; if the mode of the buffer you post is in the map, the sprunge link will include the correct argument to syntax highlight it the same way

(setq sprunge-suffixes (make-hash-table :test 'equal))
(puthash "Python" "py" sprunge-suffixes)
(puthash "Shell-script" "sh" sprunge-suffixes)
(puthash "IRC" "irc" sprunge-suffixes)

(defun sprunge ()
"Posts the current buffer to sprunge, and shows the resulting URL in a new buffer"
(if (buffer-file-name) (save-buffer) (write-file "/tmp/sprunge-post"))
(let ((sprunge-buffer (get-buffer-create "*sprunge*"))
(sprunge-window (split-window-vertically (- (window-height) 5)))
(filename buffer-file-name)
(suffix (if (gethash mode-name sprunge-suffixes) (concat "?" (gethash mode-name sprunge-suffixes)) "")))
(select-window sprunge-window)
(set-window-buffer sprunge-window sprunge-buffer)
(insert (shell-command-to-string (concat "curl -F 'sprunge=<" filename "' http://sprunge.us")))
(delete-char -1) ; Newline after URL
(insert suffix "\n")))

(define-derived-mode irc-mode
text-mode "IRC" "Major mode for IRC logs"
(setq font-lock-defaults
'((("\\[[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\\]" . font-lock-constant-face)
("<.*>" . font-lock-keyword-face)
("[a-zA-Z0-9`^_-]+:" . font-lock-type-face)
(">>> .*" . font-lock-builtin-face)

No comments: