Emacs tweaks: Export Org checkboxes using UTF-8 symbols

UPDATE 2014-03-28: Newer versions of org have the org-html-checkbox-type variable, which you can set to unicode. Use M-x customize-variable org-html-checkbox-type to see if you have it.

This snippet turns - [X] into ☑ and - [ ] into ☐.

(defun sacha/org-html-checkbox (checkbox)
  "Format CHECKBOX into HTML."
  (case checkbox (on "<span class=\"check\">&#x2611;</span>") ; checkbox (checked)
        (off "<span class=\"checkbox\">&#x2610;</span>")
        (trans "<code>[-]</code>")
        (t "")))
(defadvice org-html-checkbox (around sacha activate)
  (setq ad-return-value (sacha/org-html-checkbox (ad-get-arg 0))))

To find this code, I searched ox-html.el for [. Eventually I found org-html-checkbox, which is directly called by org-html-format-list-item instead of being a function variable that you can change. So that meant I needed to override the behaviour of org-html-checkbox through defadvice. You can see above how I wrap advice around org-html-checkbox and replace the return value with my own function. For more about advice, read the Emacs Lisp Intro manual.

To find the hex codes for the UTF-8 characters, I searched Google for UTF-8 checkbox and found BALLOT BOX WITH CHECK. I used the hex code so that I didn’t have to worry about encoding issues. I tested it by updating one of my weekly reviews. Tada!

Inspired by Grant from Wisdom and Wonder.

  • Grant Rettke

    That works perfectly. Thank you for that!

  • Leu

    Hi, thank you for your post,it is really attractive, but why my org-mode does not possess that variable? Of course, the function does not work, too.

    • If you’re using the Org built into Emacs 24, that’s still version 7, which could explain why that doesn’t work. You can install Org from MELPA in order to upgrade to a newer version. You will need to start with emacs -q, then follow the instructions from MELPA’s getting started guide (http://melpa.milkbox.net/#/getting-started) to load package and add MELPA to your sources list. Then you can use M-x package-install to install org. Hope that helps!

      • Leu

        Many thanks. I will try as you said, and I think it will work absolutely!

  • robertsms

    I use the latest version of Org 8.2.6, but still cannot find org-html-checkbox-type variable. Could you give me some advise?

    • It’s defined in ox-html.el, which might not be loaded until you export something in HTML form or add (require 'ox-html) to your config. Note that if you’ve got Org 8.2.6 loaded from packages (since it’s not built into Emacs yet), you’ll need to add (require 'package) (package-initialize) beforehand or use something like (eval-after-load ...), since packages aren’t usually loaded until after ~/.emacs.d/init.el has been processed.

  • I wanted to use ✔ in place of those X in emacs itself. Could not find a way so far…