.. title: Operating on numbers with emacs hydra
.. slug: operating-on-numbers-with-emacs-hydra
.. date: 2015-07-05 20:23:07 UTC-07:00
.. tags: emacs, hydra, elisp
.. category:
.. link:
.. description:
.. type: text
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``:
.. code:: elisp
(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``.