Operating on numbers with emacs hydra

Operating on numbers in place in any emacs buffer is one of the cool features that no one thinks they need, but is surprisingly handy, especially for converting bit-masks between decimal and hexadecimal during programming. Akinori Musha's operate-on-number package makes it very easy to set it up with a single keybinding. The only minor issue is that the instructions for setting it up refer to smartrep while my preferred package for setting up such keybindings is hydra. Assuming a recent enough version of hydra (where an escaping bug has been fixed), here is the elisp code needed to set up operate-on-number using hydra:

(defhydra hydra-operate-on-number ()
  "
Arithmetic operations: + - * /
Remainder: \\
Exponent: \\^
Arithmetic shift: < >
Base conversion: b o x X #
Format: %%%%
"
  ("+" apply-operation-to-number-at-point)
  ("-" apply-operation-to-number-at-point)
  ("*" apply-operation-to-number-at-point)
  ("/" apply-operation-to-number-at-point)
  ("\\" apply-operation-to-number-at-point)
  ("^" apply-operation-to-number-at-point)
  ("<" apply-operation-to-number-at-point)
  (">" apply-operation-to-number-at-point)
  ("b" apply-operation-to-number-at-point :exit t)
  ("o" apply-operation-to-number-at-point :exit t)
  ("x" apply-operation-to-number-at-point :exit t)
  ("X" apply-operation-to-number-at-point :exit t)
  ("#" apply-operation-to-number-at-point :exit t)
  ("%" apply-operation-to-number-at-point :exit t))

The only trick is to format the help string correctly with all the escape sequences required by hydra. I bind the generated function hydra-operate-on-number/body to M-g M-d.

Comments

Comments powered by Disqus