2015-10-17 xkcd-541 mode

I’m still very busy at the moment (and this will look like this for three more months – my current teaching schedule is insane), but I really do not want to lose the tempo of one blog post per week (however short). So for today I chose a lighter topic: xkcd-541-mode.

If you remember one of the classic xkcd comics (the one about smileys and closing parentheses;-)), you know the pain of writing things like the parenthesized remark in this sentence. It doesn’t hurt on my blog, since smileys are replaced by stupid little pixel-art images of me, but when editing anything with show-paren-mode on, the problem is really annoying.

So let me announce the xkcd-541-mode, which solves this problem.

The code is surprisingly short, so let me paste it here in its entirety:

(defvar smiley-regex "[:;>B][,']?-?[][()]"
  "This regex should match smileys, and only them.")

(defface smiley '((t :foreground "gray"))
  "Face for displaying smileys.")

(setq parse-sexp-lookup-properties t)
(add-to-list 'font-lock-extra-managed-props 'syntax-table)

(defconst smiley-props `(face smiley syntax-table ,(string-to-syntax ".")))

(define-minor-mode xkcd-541-mode
  "Toggle XKCD/541 mode.  In this mode, smileys (as defined by
smiley-regex) are treated as punctuation by Emacs movement
commands etc.  In particular, parens in smileys are skipped by
paren-matching commands."
  :lighter " :)"
  (if xkcd-541-mode
      (font-lock-add-keywords nil `((,smiley-regex . smiley-props)))
    (font-lock-remove-keywords nil `((,smiley-regex . smiley-props)))))

I will not dwelve into details of how this works. One of the reasons is the mentioned lack of time, though this is a lame excuse. But here’s a more interesting reason: my book will include a whole section (it is mostly written, and is currently about 2000 words) dedicated to the above code, how it evolved, why it works etc. (Yes, the work on the book has slowed down considerably, but it is still alive, and will resume when I deal with the teaching issue.)

Stay tuned for the next book update, and meanwhile use your smileys without worrying about matching parens!

CategoryEnglish, CategoryBlog, CategoryEmacs, CategoryEmacsBook