January 31, 2007

Emacs Markdown Functions

Typo supports Markdown, which is super sweet. I far prefer it to Textile. The only problem with Typo is that I have yet to find a good Emacs XML-RPC mode/library that work with Typo, so I’ve decided to compose my blog entries in Emacs as Markdown and then add them to the blog when I’m done. This may have the added effect of making me think more about what I’m writing.

I didn’t find a good Markdown tool that works with Emacs, and all I really care about is being able to preview the HTML that it will output. The following will do just that:

(defun markdown-preview-buffer ()
  "Preview the current buffer as it will look when run through
markdown."
  (interactive)
  (let ((buf (get-buffer-create "<em>markdown-preview</em>")))
    (clear-buffer buf)
    (call-process-region (point-min) (point-max) "~/bin/markdown" nil buf nil)
    (browse-url-of-buffer buf)))</p>

<p>(defun clear-buffer (buf)
  "Clear a buffer"
  (save-excursion
    (set-buffer buf)
    (kill-region (point-min) (point-max))))

M-x markdown-preview-buffer will run the current buffer through markdown (assuming it’s installed in ~/bin/markdown and is executable) and open the resulting HTML in your default browser.

Edit: That’s much nicer with save-excursion and bare references to point-min and point-max.