Skip to content

Commit e68cb09

Browse files
author
Musa Al-hassy
committed
Fix my helpful “C-h o” method 😁
1 parent b1026b8 commit e68cb09

File tree

2 files changed

+76
-64
lines changed

2 files changed

+76
-64
lines changed

init.el

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,40 +1122,46 @@ associated major mode; that's what we aim to do here."
11221122
;; ⌘-r, ⌘-i, ⌘-o: Sleek Semantic Selection:1 ends here
11231123

11241124
;; [[file:init.org::#Editor-Documentation-with-Contextual-Information][Editor Documentation with Contextual Information:1]]
1125-
(use-package helpful)
1125+
(use-package helpful
1126+
:commands (helpful-callable helpful-symbol)
1127+
:bind (("C-h k" . #'helpful-key)
1128+
("C-h o" . #'my/describe-symbol)))
11261129

11271130
(defun my/describe-symbol (symbol)
1128-
"A “C-h o” replacement using “helpful”:
1129-
If there's a thing at point, offer that as default search item.
1130-
1131-
If a prefix is provided, i.e., “C-u C-h o” then the built-in
1132-
“describe-symbol” command is used.
1133-
1134-
⇨ Pretty docstrings, with links and highlighting.
1135-
⇨ Source code of symbol.
1136-
⇨ Callers of function symbol.
1137-
⇨ Key bindings for function symbol.
1138-
⇨ Aliases.
1139-
⇨ Options to enable tracing, dissable, and forget/unbind the symbol!
1140-
"
1141-
(interactive "p")
1142-
(let* ((thing (symbol-at-point))
1143-
(val (completing-read
1144-
(format "Describe symbol (default %s): " thing)
1145-
(vconcat (list thing) obarray)
1146-
(lambda (vv)
1147-
(cl-some (lambda (x) (funcall (nth 1 x) vv))
1148-
describe-symbol-backends))
1149-
t nil nil))
1150-
(it (intern val)))
1151-
(cond
1152-
(current-prefix-arg (funcall #'describe-symbol it))
1153-
((or (functionp it) (macrop it) (commandp it)) (helpful-callable it))
1154-
(t (helpful-symbol it)))))
1131+
"A “C-h o” replacement using “helpful”.
1132+
1133+
If there's a thing at point, offer that as default search item.
11551134
1156-
;; Keybindings.
1157-
(global-set-key (kbd "C-h o") #'my/describe-symbol)
1158-
(global-set-key (kbd "C-h k") #'helpful-key)
1135+
If a prefix is provided, i.e., “C-u C-h o” then the built-in
1136+
“describe-symbol” command is used.
1137+
1138+
⇨ Pretty docstrings, with links and highlighting.
1139+
⇨ Source code of symbol.
1140+
⇨ Callers of function symbol.
1141+
⇨ Key bindings for function symbol.
1142+
⇨ Aliases.
1143+
⇨ Options to enable tracing, dissable, and forget/unbind the symbol!"
1144+
(interactive "P")
1145+
(let* ((sym-at-pt (symbol-at-point))
1146+
(default (and (symbolp sym-at-pt) (symbol-name sym-at-pt)))
1147+
(prompt (if default
1148+
(format "Describe symbol (default %s): " default)
1149+
"Describe symbol: "))
1150+
(pred (lambda (sym) ; SYM is a symbol when COLLECTION is an obarray
1151+
(cl-some (lambda (x) (funcall (cadr x) sym))
1152+
describe-symbol-backends)))
1153+
(name (completing-read prompt
1154+
obarray ; ← use obarray directly
1155+
pred
1156+
t ; require-match
1157+
nil nil
1158+
default)) ; ← default goes here
1159+
(sym (intern name)))
1160+
(if current-prefix-arg
1161+
(describe-symbol sym) ; C-u C-h o → built-in
1162+
(if (or (functionp sym) (macrop sym) (commandp sym))
1163+
(helpful-callable sym)
1164+
(helpful-symbol sym)))))
11591165
;; Editor Documentation with Contextual Information:1 ends here
11601166

11611167
;; [[file:init.org::#Let's-make-working-with-Emacs-Lisp-even-better][[[https://github.com/xuchunyang/elisp-demos][Append existing ELisp docstrings with example use and actual output.]]:1]]

init.org

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,8 +1286,8 @@ that defines it./
12861286

12871287
As always, inspect the generated code (via ~macrostep-expand~) to see that it does what you expect:
12881288
#+begin_src emacs-lisp :tangle no
1289-
(use-package annotation :commands annotation-annotate)
1290-
≈ (autoload (function annotation-annotate) "annotation" nil t)
1289+
(use-package helpful :commands helpful-callable)
1290+
≈ (autoload (function helpful-callable) "helpful" nil t)
12911291
#+end_src
12921292

12931293
** Emacs Package Manager
@@ -5726,40 +5726,46 @@ Let's use a helpful Emacs /documentation/ system that cleanly shows a lot of
57265726
contextual information ---then let's /extend/ that to work as we want it to:
57275727
~C-h o~ to describe the symbol at point.
57285728
#+begin_src emacs-lisp
5729-
(use-package helpful)
5729+
(use-package helpful
5730+
:commands (helpful-callable helpful-symbol)
5731+
:bind (("C-h k" . #'helpful-key)
5732+
("C-h o" . #'my/describe-symbol)))
57305733

57315734
(defun my/describe-symbol (symbol)
5732-
"A “C-h o” replacement using “helpful”:
5733-
If there's a thing at point, offer that as default search item.
5734-
5735-
If a prefix is provided, i.e., “C-u C-h o” then the built-in
5736-
“describe-symbol” command is used.
5737-
5738-
⇨ Pretty docstrings, with links and highlighting.
5739-
⇨ Source code of symbol.
5740-
⇨ Callers of function symbol.
5741-
⇨ Key bindings for function symbol.
5742-
⇨ Aliases.
5743-
⇨ Options to enable tracing, dissable, and forget/unbind the symbol!
5744-
"
5745-
(interactive "p")
5746-
(let* ((thing (symbol-at-point))
5747-
(val (completing-read
5748-
(format "Describe symbol (default %s): " thing)
5749-
(vconcat (list thing) obarray)
5750-
(lambda (vv)
5751-
(cl-some (lambda (x) (funcall (nth 1 x) vv))
5752-
describe-symbol-backends))
5753-
t nil nil))
5754-
(it (intern val)))
5755-
(cond
5756-
(current-prefix-arg (funcall #'describe-symbol it))
5757-
((or (functionp it) (macrop it) (commandp it)) (helpful-callable it))
5758-
(t (helpful-symbol it)))))
5735+
"A “C-h o” replacement using “helpful”.
5736+
5737+
If there's a thing at point, offer that as default search item.
57595738

5760-
;; Keybindings.
5761-
(global-set-key (kbd "C-h o") #'my/describe-symbol)
5762-
(global-set-key (kbd "C-h k") #'helpful-key)
5739+
If a prefix is provided, i.e., “C-u C-h o” then the built-in
5740+
“describe-symbol” command is used.
5741+
5742+
⇨ Pretty docstrings, with links and highlighting.
5743+
⇨ Source code of symbol.
5744+
⇨ Callers of function symbol.
5745+
⇨ Key bindings for function symbol.
5746+
⇨ Aliases.
5747+
⇨ Options to enable tracing, dissable, and forget/unbind the symbol!"
5748+
(interactive "P")
5749+
(let* ((sym-at-pt (symbol-at-point))
5750+
(default (and (symbolp sym-at-pt) (symbol-name sym-at-pt)))
5751+
(prompt (if default
5752+
(format "Describe symbol (default %s): " default)
5753+
"Describe symbol: "))
5754+
(pred (lambda (sym) ; SYM is a symbol when COLLECTION is an obarray
5755+
(cl-some (lambda (x) (funcall (cadr x) sym))
5756+
describe-symbol-backends)))
5757+
(name (completing-read prompt
5758+
obarray ; ← use obarray directly
5759+
pred
5760+
t ; require-match
5761+
nil nil
5762+
default)) ; ← default goes here
5763+
(sym (intern name)))
5764+
(if current-prefix-arg
5765+
(describe-symbol sym) ; C-u C-h o → built-in
5766+
(if (or (functionp sym) (macrop sym) (commandp sym))
5767+
(helpful-callable sym)
5768+
(helpful-symbol sym)))))
57635769
#+END_SRC
57645770

57655771
I like [[https://github.com/Wilfred/helpful][helpful]] and wanted it to have the same behaviour as ~C-h o~, which

0 commit comments

Comments
 (0)