Two Wrongs

Emacs Magic: Simple Pastebin

Emacs Magic: Simple Pastebin

A while ago I learned about the Emacs htmlize function, which takes whatever is currently visible in your Emacs window and renders it out to html, with the current syntax highlighting and all.11 It’s essentially a screenshot of your Emacs window, except in text format so without all the accessibility issues of an image depicting text. I’ve wanted to make something cool with it since, but just now got around to it.

I already have a web server hosting this website, so I started to think, what if I could just get Emacs to automatically upload the htmlize output to the server? Then I’d have my own personal pastebin without all the shit that comes with the existing services. So I did22 The remote file management is performed by tramp, which apparently comes with Emacs by default. I just have to provide the remote host in the correct format as part of the file path, and then tramp will take care of the rest. tramp even makes sure to use ssh-agent and ssh multiplexing for a snappy, low-overhead connection. It actually feels like working locally. It’s insane..

(defvar htmlize-paste-it-target-directory
(defvar htmlize-paste-it-base-url

(defun htmlize-paste-it ()
  "Htmlize region-or-buffer and copy to directory."
  (let* ((start (if (region-active-p)
                    (region-beginning) (point-min)))
         (end (if (region-active-p)
                  (region-end) (point-max)))

         ;; We use a basename-hash.ext.html format
         (basename (file-name-base (buffer-name)))
         (extension (file-name-extension (buffer-name)))
         (hash (sha1 (current-buffer) start end))
         (file-name (concat basename
                            "-" (substring hash 0 6)
                            "." extension

         (new-file (concat

         (access-url (concat
    ;; Region messes with clipboard, so deactivate it
    (with-current-buffer (htmlize-region start end)
      ;; Copy htmlized contents to target
      (write-file new-file)
      ;; Ensure target can be accessed by web server
      (chmod new-file #o755))
    ;; Put URL into clipboard
    (kill-new access-url)))

This will let me select any part of the text/code I work with, run the htmlize-paste-it command, and once that has finished running (takes 1–2 seconds in the worst case) my clipboard has been populated with a link to a nicely formatted html file containing code/text I had just selected33 If no text is selected, the paste will simply be created from the entire file I have open.. As you can see in this example, the link also contains part of a sha1 hash of the content being pasted, in case I want to create different pastes from different sections of the same file.

This is immensely cool.

People ask me why I use Emacs.

This is ultimately why.

I don’t feel like I have configured anything. I just did what I do best: programming. I wrote a function and now I have this new amazing functionality. I didn’t even have to restart Emacs to apply any changes.

Give me an editor that is this hackable, and I will love it. I don’t see why anyone would not want that.