Skip to content

Commit

Permalink
Add option to specify :hint in body
Browse files Browse the repository at this point in the history
* hydra.el (hydra-plist-get-default): New defun, extended `plist-get'.
(hydra--head-property): Use `hydra-plist-get-default'.
(defhydra): The heads will inherit their hint from body :hint parameter.
They can override it, of course.

The most use you can get out of this parameter is to specify :hint nil
for hydras with a format-style docstring.

Example:

(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"))

Some setup:

(defun hot-expand (str)
  "Expand org template."
  (insert str)
  (org-try-structure-completion))
(define-key org-mode-map "<"
  (lambda () (interactive)
     (if (looking-back "^")
         (hydra-org-template/body)
       (self-insert-command 1))))
  • Loading branch information
abo-abo committed Mar 6, 2015
1 parent e342c33 commit 77c8e40
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions hydra.el
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,21 @@ should be a single statement. Wrap it in an interactive lambda."
(interactive)
,x)))

(defun hydra-plist-get-default (plist prop default)
"Extract a value from a property list.
PLIST is a property list, which is a list of the form
\(PROP1 VALUE1 PROP2 VALUE2...).
Return the value corresponding to PROP, or DEFAULT if PROP is not
one of the properties on the list."
(if (memq prop plist)
(plist-get plist prop)
default))

(defun hydra--head-property (h prop &optional default)
"Return for Hydra head H the value of property PROP.
Return DEFAULT if PROP is not in H."
(let ((plist (cl-cdddr h)))
(if (memq prop h)
(plist-get plist prop)
default)))
(hydra-plist-get-default (cl-cdddr h) prop default))

(defun hydra--aggregate-color (head-color body-color)
"Return the resulting head color for HEAD-COLOR and BODY-COLOR."
Expand Down Expand Up @@ -839,12 +847,17 @@ result of `defhydra'."
(cond ((< len 2)
(error "Each head should have at least two items: %S" h))
((= len 2)
(setcdr (cdr h) `("" :cmd-name ,cmd-name)))
(setcdr (cdr h)
(list
(hydra-plist-get-default (cddr body) :hint "")
:cmd-name cmd-name)))
(t
(let ((hint (cl-caddr h)))
(unless (or (null hint)
(stringp hint))
(setcdr (cdr h) (cons "" (cddr h))))
(setcdr (cdr h) (cons
(hydra-plist-get-default (cddr body) :hint "")
(cddr h))))
(setcdr (cddr h) `(:cmd-name ,cmd-name ,@(cl-cdddr h))))))))
(let* ((keymap (copy-keymap hydra-base-map))
(body-name (intern (format "%S/body" name)))
Expand Down

0 comments on commit 77c8e40

Please sign in to comment.