Skip to content

Verbosity‐Long‐Short

tbanel edited this page Nov 24, 2024 · 1 revision

Large + one-liner hints

Large hints over several lines are great for casual usage. Sometimes, thought, experienced users may find them annoying.

Hints can be turned off with the :verbosity property.

What about something in the middle? What about switching on the fly to a one-liner hint, displayed in the echo area? The layout of windows would not be disturbed.

How to do that? With defhydra, only one hint can be specified.

A hack

;;╭──────────────────────╮
;;│ 1. define some Hydra │
;;╰──────────────────────╯
(defhydra hydra-hintable (:exit nil :hint nil)
  "
_a_ action a
_b_ action b
_c_ action c
_t_ toggle hints"
("a" (action 'a))
("b" (action 'b))
("c" (action 'c))
("t" toggle-hint-size))

(defun action (something)
  (insert (format "(%s)" something)))

;;╭─────────────────────╮
;;│ 2. boolean variable │
;;╰─────────────────────╯
(defvar full-hint-p t)

;;╭──────────────────────────╮
;;│ 3. pack 2 hints into one │
;;╰──────────────────────────╯
(setq hydra-hintable/hint
      `(if full-hint-p
           ,hydra-hintable/hint
         "actions: abc, toggle hint: t"))

;;╭────────────────────╮
;;│ 4. toggle function │
;;╰────────────────────╯
(defun toggle-hint-size ()
    (interactive)
    (setq full-hint-p (not full-hint-p))
    (hydra-set-property
    'hydra-hintable
    :verbosity
    (if full-hint-p t 1)))

How does it work?

  1. First, we have a standard Hydra, with a long hint. The "t" head toggles hints.

  2. We need a state-variable telling which is the current hint size:

    • t: full fledged standard hint
    • nil: one-liner hint in the echo-area
  3. Now we tweak the standard hint so that it depends on this variable. The idea is to pack the long and the short hints into the standard hint variable. We do that just once.

    This works because Hydra evaluates the hint each time it wants to display it ("dynamic hint").

  4. Now we implement the function to toggle hints. It toggles the state-variable, and changes the Hydra verbosity accordingly. The :verbosity property can be:

    • t: standard display usually through the lv library
    • 1: display in the echo area
    • 0: no display

    We make use of the first two settings.

That's all (folks).

Built-in?

Of course, it would be nice to have this feature built-in.

It could be done by extending the possible values for the :verbosity property (currently t, 1, 0).

A :verbosity of maybe :short, for instance, would display the short version.

Now, where can we store the alternate one-liner hint?

  • Maybe in the :verbosity property itself?
  • In a new parameter to the defhydra macro? Without breaking compatibility.
Clone this wiki locally