1

I am trying to figure out how how to replace structural elements in org-mode with glyphs/abbreviations to make them more readable. Unfortunately, I'm getting stuck on replacing a simple checkbox [X] with (glyph).

 (font-lock-add-keywords
     'org-mode `(             
                  ("^\s*\\**\\(-\\|\\+\|[0-9]+\\|\\)\s\\(\\[X\\]\\)"
                      (3 (progn (compose-region (match-beginning 1) (match-end 1) 
                                 "")
                            nil)))
                 )
     )

My goal is to replace the checkbox capturing group with the glyph, but no matter which group index I pick, I either get a pre-pended glyph or no glyph at all. More disturbingly, this messes with the rest of Org-mode's fontification when you toggle to fundamental-mode and then back to org-mode. Is this is breaking the syntax table or something? How do you avoid this happening?

Update

I think the problem is with the complex regex capture groups. I you just code this for the - [X] case (no OR expressions needed, one capture group), everything works and the other font-locks do not get messed up.

  • Have you located the portion of code in the org-mode source that deals with these specific checkboxes; and, if so, can you let us know where it is and what version of org-mode your are using? Some people use the built-in version and others like to pull the latest stable release, and some people are using older versions of Emacs with older built-in org-mode versions. You will almost certainly need to modify the location where this checkbox stuff normally happens, as there is just too much happening in org-mode to modify font-lock stuff after the fact (in my opinion, that is). – lawlist Jun 8 '17 at 1:51
  • I use spacemacs, so the version is from the Org ELPA repo: org-plus-contrib-20170210. The file org-element.el seems to be responsible for defining checkboxes. I wish there was some way to use the built-in faces to do text replacement fontification. Unfortunately, all faces do is colors, fonts etc, but you cannot replace text. – user27636 Jun 8 '17 at 12:05
2

There's no need to use font-lock for this. Emacs comes with prettify-symbols-mode which replaces fixed strings with single character glyphs. You can add additional symbols to prettify by adding them to prettify-symbols-alist a list (STRING . GLYPH) pairs

(add-hook 'org-mode-hook
          (lambda ()
            (push '("[ ]" .  "🞎") prettify-symbols-alist)
            (push '("[X]" . "🗷" ) prettify-symbols-alist)
            (push '("[-]" . "◫" ) prettify-symbols-alist)
            (prettify-symbols-mode)
            ))

Of course you can change the glyphs to any that you like. If you want them to also export prettily to HTML use

(setq org-html-checkbox-type 'unicode)
(setq org-html-checkbox-types
 '((unicode (on . "<span class=\"task-done\">&#x2611;</span>")
            (off . "<span class=\"task-todo\">&#x2610;</span>")
            (trans . "<span class=\"task-in-progress\">&#x25eb;</span>"))))

There doesn't seem to be a corresponding setting for other export backends.

  • Is it possible to specify context with this technique? (a checkbox mid- sentence is not a checkbox) – user27636 Jun 9 '17 at 11:20
  • prettify-symbol-mode only replaces strings; it doesn't understand regexs or anything like that. I, at least, rarely type "[ ]" except to make checkboxes. It could happen but maybe not often to enough to offset the awesome looking checkboxes. – erikstokes Jun 9 '17 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.