(or emacs irrelevant)

org-mode block templates in Hydra

Here's a new Hydra for you:

(defhydra hydra-org-template (:color blue :hint nil)
  "
_c_enter  _q_uote    _L_aTeX:
_l_atex   _e_xample  _i_ndex:
_a_scii   _v_erse    _I_NCLUDE:
_s_rc     ^ ^        _H_TML:
_h_tml    ^ ^        _A_SCII:
"
  ("s" (hot-expand "<s"))
  ("e" (hot-expand "<e"))
  ("q" (hot-expand "<q"))
  ("v" (hot-expand "<v"))
  ("c" (hot-expand "<c"))
  ("l" (hot-expand "<l"))
  ("h" (hot-expand "<h"))
  ("a" (hot-expand "<a"))
  ("L" (hot-expand "<L"))
  ("i" (hot-expand "<i"))
  ("I" (hot-expand "<I"))
  ("H" (hot-expand "<H"))
  ("A" (hot-expand "<A"))
  ("<" self-insert-command "ins")
  ("o" nil "quit"))

(defun hot-expand (str)
  "Expand org template."
  (insert str)
  (org-try-structure-completion))

I bind it for myself like this:

(define-key org-mode-map "<"
  (lambda () (interactive)
     (if (looking-back "^")
         (hydra-org-template/body)
       (self-insert-command 1))))

This means that when I press < from the start of the line, a Hydra will be called instead of inserting <, otherwise < will be inserted.

As the default insert method for org-mode blocks is already pretty convenient, this Hydra is more of an illustration than anything, especially of the new :hint nil feature.

Just to remind you, each head has four placeholders:

  • key binding
  • body
  • hint
  • plist

When a Hydra is active, it will show its doc in the echo area in the bottom of the frame. This doc is composed of two parts: the body doc and the heads' doc. The body doc you specify yourself, the heads' doc is built by concatenating the key binding and the hint for each head into a (single) line.

If you don't specify a hint for a head, it's assumed to be ""; this head's binding will still be in the heads' doc. If you don't want a head's binding to be in the heads' doc, set the hint to nil. This is commonly done because a head is already documented in the body doc. It can sometimes become tedious to set all the hints to nil, for instance in the Hydra above, I would need to do it 13 times. Hence the :hint nil shortcut.

Here's how it looks like:

hydra-org-template.png

I'm not a Scrabble pro: clash for word score 10 is my result, although a longer word would break the nice column layout. The first two columns contain begin/end templates, while the third one contains the one-line templates.