Document reserved keys
[emacs.git] / lisp / help-mode.el
blob68fc319e68c1e4a28f4869094858653c434d18a9
1 ;;; help-mode.el --- `help-mode' used by *Help* buffers
3 ;; Copyright (C) 1985-1986, 1993-1994, 1998-2018 Free Software
4 ;; Foundation, Inc.
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: help, internal
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Defines `help-mode', which is the mode used by *Help* buffers, and
28 ;; associated support machinery, such as adding hyperlinks, etc.,
30 ;;; Code:
32 (require 'button)
33 (require 'cl-lib)
34 (eval-when-compile (require 'easymenu))
36 (defvar help-mode-map
37 (let ((map (make-sparse-keymap)))
38 (set-keymap-parent map (make-composed-keymap button-buffer-map
39 special-mode-map))
40 (define-key map [mouse-2] 'help-follow-mouse)
41 (define-key map "l" 'help-go-back)
42 (define-key map "r" 'help-go-forward)
43 (define-key map "\C-c\C-b" 'help-go-back)
44 (define-key map "\C-c\C-f" 'help-go-forward)
45 (define-key map [XF86Back] 'help-go-back)
46 (define-key map [XF86Forward] 'help-go-forward)
47 (define-key map "\C-c\C-c" 'help-follow-symbol)
48 (define-key map "\r" 'help-follow)
49 map)
50 "Keymap for help mode.")
52 (easy-menu-define help-mode-menu help-mode-map
53 "Menu for Help Mode."
54 '("Help-Mode"
55 ["Show Help for Symbol" help-follow-symbol
56 :help "Show the docs for the symbol at point"]
57 ["Previous Topic" help-go-back
58 :help "Go back to previous topic in this help buffer"]
59 ["Next Topic" help-go-forward
60 :help "Go back to next topic in this help buffer"]
61 ["Move to Previous Button" backward-button
62 :help "Move to the Next Button in the help buffer"]
63 ["Move to Next Button" forward-button
64 :help "Move to the Next Button in the help buffer"]))
66 (defvar help-xref-stack nil
67 "A stack of ways by which to return to help buffers after following xrefs.
68 Used by `help-follow' and `help-xref-go-back'.
69 An element looks like (POSITION FUNCTION ARGS...).
70 To use the element, do (apply FUNCTION ARGS) then goto the point.")
71 (put 'help-xref-stack 'permanent-local t)
72 (make-variable-buffer-local 'help-xref-stack)
74 (defvar help-xref-forward-stack nil
75 "A stack used to navigate help forwards after using the back button.
76 Used by `help-follow' and `help-xref-go-forward'.
77 An element looks like (POSITION FUNCTION ARGS...).
78 To use the element, do (apply FUNCTION ARGS) then goto the point.")
79 (put 'help-xref-forward-stack 'permanent-local t)
80 (make-variable-buffer-local 'help-xref-forward-stack)
82 (defvar help-xref-stack-item nil
83 "An item for `help-follow' in this buffer to push onto `help-xref-stack'.
84 The format is (FUNCTION ARGS...).")
85 (put 'help-xref-stack-item 'permanent-local t)
86 (make-variable-buffer-local 'help-xref-stack-item)
88 (defvar help-xref-stack-forward-item nil
89 "An item for `help-go-back' to push onto `help-xref-forward-stack'.
90 The format is (FUNCTION ARGS...).")
91 (put 'help-xref-stack-forward-item 'permanent-local t)
92 (make-variable-buffer-local 'help-xref-stack-forward-item)
94 (setq-default help-xref-stack nil help-xref-stack-item nil)
95 (setq-default help-xref-forward-stack nil help-xref-forward-stack-item nil)
97 (defcustom help-mode-hook nil
98 "Hook run by `help-mode'."
99 :type 'hook
100 :group 'help)
102 ;; Button types used by help
104 (define-button-type 'help-xref
105 'follow-link t
106 'action #'help-button-action)
108 (defun help-button-action (button)
109 "Call BUTTON's help function."
110 (help-do-xref nil
111 (button-get button 'help-function)
112 (button-get button 'help-args)))
114 ;; These 6 calls to define-button-type were generated in a dolist
115 ;; loop, but that is bad because it means these button types don't
116 ;; have an easily found definition.
118 (define-button-type 'help-function
119 :supertype 'help-xref
120 'help-function 'describe-function
121 'help-echo (purecopy "mouse-2, RET: describe this function"))
123 (define-button-type 'help-variable
124 :supertype 'help-xref
125 'help-function 'describe-variable
126 'help-echo (purecopy "mouse-2, RET: describe this variable"))
128 (define-button-type 'help-face
129 :supertype 'help-xref
130 'help-function 'describe-face
131 'help-echo (purecopy "mouse-2, RET: describe this face"))
133 (define-button-type 'help-coding-system
134 :supertype 'help-xref
135 'help-function 'describe-coding-system
136 'help-echo (purecopy "mouse-2, RET: describe this coding system"))
138 (define-button-type 'help-input-method
139 :supertype 'help-xref
140 'help-function 'describe-input-method
141 'help-echo (purecopy "mouse-2, RET: describe this input method"))
143 (define-button-type 'help-character-set
144 :supertype 'help-xref
145 'help-function 'describe-character-set
146 'help-echo (purecopy "mouse-2, RET: describe this character set"))
148 ;; Make some more idiosyncratic button types.
150 (define-button-type 'help-symbol
151 :supertype 'help-xref
152 'help-function #'describe-symbol
153 'help-echo (purecopy "mouse-2, RET: describe this symbol"))
155 (define-button-type 'help-back
156 :supertype 'help-xref
157 'help-function #'help-xref-go-back
158 'help-echo (purecopy "mouse-2, RET: go back to previous help buffer"))
160 (define-button-type 'help-forward
161 :supertype 'help-xref
162 'help-function #'help-xref-go-forward
163 'help-echo (purecopy "mouse-2, RET: move forward to next help buffer"))
165 (define-button-type 'help-info-variable
166 :supertype 'help-xref
167 ;; the name of the variable is put before the argument to Info
168 'help-function (lambda (_a v) (info v))
169 'help-echo (purecopy "mouse-2, RET: read this Info node"))
171 (define-button-type 'help-info
172 :supertype 'help-xref
173 'help-function #'info
174 'help-echo (purecopy "mouse-2, RET: read this Info node"))
176 (define-button-type 'help-url
177 :supertype 'help-xref
178 'help-function #'browse-url
179 'help-echo (purecopy "mouse-2, RET: view this URL in a browser"))
181 (define-button-type 'help-customize-variable
182 :supertype 'help-xref
183 'help-function (lambda (v)
184 (customize-variable v))
185 'help-echo (purecopy "mouse-2, RET: customize variable"))
187 (define-button-type 'help-customize-face
188 :supertype 'help-xref
189 'help-function (lambda (v)
190 (customize-face v))
191 'help-echo (purecopy "mouse-2, RET: customize face"))
193 (define-button-type 'help-function-def
194 :supertype 'help-xref
195 'help-function (lambda (fun &optional file type)
196 (or file
197 (setq file (find-lisp-object-file-name fun type)))
198 (if (not file)
199 (message "Unable to find defining file")
200 (require 'find-func)
201 (when (eq file 'C-source)
202 (setq file
203 (help-C-file-name (indirect-function fun) 'fun)))
204 ;; Don't use find-function-noselect because it follows
205 ;; aliases (which fails for built-in functions).
206 (let ((location
207 (find-function-search-for-symbol fun type file)))
208 (pop-to-buffer (car location))
209 (run-hooks 'find-function-after-hook)
210 (if (cdr location)
211 (goto-char (cdr location))
212 (message "Unable to find location in file")))))
213 'help-echo (purecopy "mouse-2, RET: find function's definition"))
215 (define-button-type 'help-function-cmacro ; FIXME: Obsolete since 24.4.
216 :supertype 'help-xref
217 'help-function (lambda (fun file)
218 (setq file (locate-library file t))
219 (if (and file (file-readable-p file))
220 (progn
221 (pop-to-buffer (find-file-noselect file))
222 (goto-char (point-min))
223 (if (re-search-forward
224 (format "^[ \t]*(\\(cl-\\)?define-compiler-macro[ \t]+%s"
225 (regexp-quote (symbol-name fun)))
226 nil t)
227 (forward-line 0)
228 (message "Unable to find location in file")))
229 (message "Unable to find file")))
230 'help-echo (purecopy "mouse-2, RET: find function's compiler macro"))
232 (define-button-type 'help-variable-def
233 :supertype 'help-xref
234 'help-function (lambda (var &optional file)
235 (when (eq file 'C-source)
236 (setq file (help-C-file-name var 'var)))
237 (let ((location (find-variable-noselect var file)))
238 (pop-to-buffer (car location))
239 (run-hooks 'find-function-after-hook)
240 (if (cdr location)
241 (goto-char (cdr location))
242 (message "Unable to find location in file"))))
243 'help-echo (purecopy "mouse-2, RET: find variable's definition"))
245 (define-button-type 'help-face-def
246 :supertype 'help-xref
247 'help-function (lambda (fun file)
248 (require 'find-func)
249 ;; Don't use find-function-noselect because it follows
250 ;; aliases (which fails for built-in functions).
251 (let ((location
252 (find-function-search-for-symbol fun 'defface file)))
253 (pop-to-buffer (car location))
254 (if (cdr location)
255 (goto-char (cdr location))
256 (message "Unable to find location in file"))))
257 'help-echo (purecopy "mouse-2, RET: find face's definition"))
259 (define-button-type 'help-package
260 :supertype 'help-xref
261 'help-function 'describe-package
262 'help-echo (purecopy "mouse-2, RET: Describe package"))
264 (define-button-type 'help-package-def
265 :supertype 'help-xref
266 'help-function (lambda (file) (dired file))
267 'help-echo (purecopy "mouse-2, RET: visit package directory"))
269 (define-button-type 'help-theme-def
270 :supertype 'help-xref
271 'help-function 'find-file
272 'help-echo (purecopy "mouse-2, RET: visit theme file"))
274 (define-button-type 'help-theme-edit
275 :supertype 'help-xref
276 'help-function 'customize-create-theme
277 'help-echo (purecopy "mouse-2, RET: edit this theme file"))
279 (define-button-type 'help-dir-local-var-def
280 :supertype 'help-xref
281 'help-function (lambda (_var &optional file)
282 ;; FIXME: this should go to the point where the
283 ;; local variable was defined.
284 (find-file file))
285 'help-echo (purecopy "mouse-2, RET: open directory-local variables file"))
288 (defvar bookmark-make-record-function)
290 ;;;###autoload
291 (define-derived-mode help-mode special-mode "Help"
292 "Major mode for viewing help text and navigating references in it.
293 Entry to this mode runs the normal hook `help-mode-hook'.
294 Commands:
295 \\{help-mode-map}"
296 (set (make-local-variable 'revert-buffer-function)
297 'help-mode-revert-buffer)
298 (set (make-local-variable 'bookmark-make-record-function)
299 'help-bookmark-make-record))
301 ;;;###autoload
302 (defun help-mode-setup ()
303 "Enter Help Mode in the current buffer."
304 (help-mode)
305 (setq buffer-read-only nil))
307 ;;;###autoload
308 (defun help-mode-finish ()
309 "Finalize Help Mode setup in current buffer."
310 (when (derived-mode-p 'help-mode)
311 (setq buffer-read-only t)
312 (help-make-xrefs (current-buffer))))
314 ;; Grokking cross-reference information in doc strings and
315 ;; hyperlinking it.
317 ;; This may have some scope for extension and the same or something
318 ;; similar should be done for widget doc strings, which currently use
319 ;; another mechanism.
321 (defvar help-back-label (purecopy "[back]")
322 "Label to use by `help-make-xrefs' for the go-back reference.")
324 (defvar help-forward-label (purecopy "[forward]")
325 "Label to use by `help-make-xrefs' for the go-forward reference.")
327 (defconst help-xref-symbol-regexp
328 (purecopy (concat "\\(\\<\\(\\(variable\\|option\\)\\|" ; Link to var
329 "\\(function\\|command\\|call\\)\\|" ; Link to function
330 "\\(face\\)\\|" ; Link to face
331 "\\(symbol\\|program\\|property\\)\\|" ; Don't link
332 "\\(source \\(?:code \\)?\\(?:of\\|for\\)\\)\\)"
333 "[ \t\n]+\\)?"
334 ;; Note starting with word-syntax character:
335 "['`‘]\\(\\sw\\(\\sw\\|\\s_\\)+\\|`\\)['’]"))
336 "Regexp matching doc string references to symbols.
338 The words preceding the quoted symbol can be used in doc strings to
339 distinguish references to variables, functions and symbols.")
341 (defvar help-xref-mule-regexp nil
342 "Regexp matching doc string references to MULE-related keywords.
344 It is usually nil, and is temporarily bound to an appropriate regexp
345 when help commands related to multilingual environment (e.g.,
346 `describe-coding-system') are invoked.")
349 (defconst help-xref-info-regexp
350 (purecopy
351 "\\<[Ii]nfo[ \t\n]+\\(node\\|anchor\\)[ \t\n]+['`‘]\\([^'’]+\\)['’]")
352 "Regexp matching doc string references to an Info node.")
354 (defconst help-xref-url-regexp
355 (purecopy "\\<[Uu][Rr][Ll][ \t\n]+['`‘]\\([^'’]+\\)['’]")
356 "Regexp matching doc string references to a URL.")
358 ;;;###autoload
359 (defun help-setup-xref (item interactive-p)
360 "Invoked from commands using the \"*Help*\" buffer to install some xref info.
362 ITEM is a (FUNCTION . ARGS) pair appropriate for recreating the help
363 buffer after following a reference. INTERACTIVE-P is non-nil if the
364 calling command was invoked interactively. In this case the stack of
365 items for help buffer \"back\" buttons is cleared.
367 This should be called very early, before the output buffer is cleared,
368 because we want to record the \"previous\" position of point so we can
369 restore it properly when going back."
370 (with-current-buffer (help-buffer)
371 (when help-xref-stack-item
372 (push (cons (point) help-xref-stack-item) help-xref-stack)
373 (setq help-xref-forward-stack nil))
374 (when interactive-p
375 (let ((tail (nthcdr 10 help-xref-stack)))
376 ;; Truncate the stack.
377 (if tail (setcdr tail nil))))
378 (setq help-xref-stack-item item)))
380 (defvar help-xref-following nil
381 "Non-nil when following a help cross-reference.")
383 ;;;###autoload
384 (defun help-buffer ()
385 "Return the name of a buffer for inserting help.
386 If `help-xref-following' is non-nil, this is the name of the
387 current buffer. Signal an error if this buffer is not derived
388 from `help-mode'.
389 Otherwise, return \"*Help*\", creating a buffer with that name if
390 it does not already exist."
391 (buffer-name ;for with-output-to-temp-buffer
392 (if (not help-xref-following)
393 (get-buffer-create "*Help*")
394 (unless (derived-mode-p 'help-mode)
395 (error "Current buffer is not in Help mode"))
396 (current-buffer))))
398 (defvar describe-symbol-backends
399 `((nil ,#'fboundp ,(lambda (s _b _f) (describe-function s)))
400 (nil
401 ,(lambda (symbol)
402 (or (and (boundp symbol) (not (keywordp symbol)))
403 (get symbol 'variable-documentation)))
404 ,#'describe-variable)
405 ("face" ,#'facep ,(lambda (s _b _f) (describe-face s)))))
407 ;;;###autoload
408 (defun help-make-xrefs (&optional buffer)
409 "Parse and hyperlink documentation cross-references in the given BUFFER.
411 Find cross-reference information in a buffer and activate such cross
412 references for selection with `help-follow'. Cross-references have
413 the canonical form `...' and the type of reference may be
414 disambiguated by the preceding word(s) used in
415 `help-xref-symbol-regexp'. Faces only get cross-referenced if
416 preceded or followed by the word `face'. Variables without
417 variable documentation do not get cross-referenced, unless
418 preceded by the word `variable' or `option'.
420 If the variable `help-xref-mule-regexp' is non-nil, find also
421 cross-reference information related to multilingual environment
422 \(e.g., coding-systems). This variable is also used to disambiguate
423 the type of reference as the same way as `help-xref-symbol-regexp'.
425 A special reference `back' is made to return back through a stack of
426 help buffers. Variable `help-back-label' specifies the text for
427 that."
428 (interactive "b")
429 (with-current-buffer (or buffer (current-buffer))
430 (save-excursion
431 (goto-char (point-min))
432 ;; Skip the header-type info, though it might be useful to parse
433 ;; it at some stage (e.g. "function in `library'").
434 (forward-paragraph)
435 (let ((old-modified (buffer-modified-p)))
436 (let ((stab (syntax-table))
437 (case-fold-search t)
438 (inhibit-read-only t))
439 (set-syntax-table emacs-lisp-mode-syntax-table)
440 ;; The following should probably be abstracted out.
441 (unwind-protect
442 (progn
443 ;; Info references
444 (save-excursion
445 (while (re-search-forward help-xref-info-regexp nil t)
446 (let ((data (match-string 2)))
447 (save-match-data
448 (unless (string-match "^([^)]+)" data)
449 (setq data (concat "(emacs)" data)))
450 (setq data ;; possible newlines if para filled
451 (replace-regexp-in-string "[ \t\n]+" " " data t t)))
452 (help-xref-button 2 'help-info data))))
453 ;; URLs
454 (save-excursion
455 (while (re-search-forward help-xref-url-regexp nil t)
456 (let ((data (match-string 1)))
457 (help-xref-button 1 'help-url data))))
458 ;; Mule related keywords. Do this before trying
459 ;; `help-xref-symbol-regexp' because some of Mule
460 ;; keywords have variable or function definitions.
461 (if help-xref-mule-regexp
462 (save-excursion
463 (while (re-search-forward help-xref-mule-regexp nil t)
464 (let* ((data (match-string 7))
465 (sym (intern-soft data)))
466 (cond
467 ((match-string 3) ; coding system
468 (and sym (coding-system-p sym)
469 (help-xref-button 6 'help-coding-system sym)))
470 ((match-string 4) ; input method
471 (and (assoc data input-method-alist)
472 (help-xref-button 7 'help-input-method data)))
473 ((or (match-string 5) (match-string 6)) ; charset
474 (and sym (charsetp sym)
475 (help-xref-button 7 'help-character-set sym)))
476 ((assoc data input-method-alist)
477 (help-xref-button 7 'help-character-set data))
478 ((and sym (coding-system-p sym))
479 (help-xref-button 7 'help-coding-system sym))
480 ((and sym (charsetp sym))
481 (help-xref-button 7 'help-character-set sym)))))))
482 ;; Quoted symbols
483 (save-excursion
484 (while (re-search-forward help-xref-symbol-regexp nil t)
485 (let* ((data (match-string 8))
486 (sym (intern-soft data)))
487 (if sym
488 (cond
489 ((match-string 3) ; `variable' &c
490 (and (or (boundp sym) ; `variable' doesn't ensure
491 ; it's actually bound
492 (get sym 'variable-documentation))
493 (help-xref-button 8 'help-variable sym)))
494 ((match-string 4) ; `function' &c
495 (and (fboundp sym) ; similarly
496 (help-xref-button 8 'help-function sym)))
497 ((match-string 5) ; `face'
498 (and (facep sym)
499 (help-xref-button 8 'help-face sym)))
500 ((match-string 6)) ; nothing for `symbol'
501 ((match-string 7)
502 (help-xref-button 8 'help-function-def sym))
503 ((cl-some (lambda (x) (funcall (nth 1 x) sym))
504 describe-symbol-backends)
505 (help-xref-button 8 'help-symbol sym)))))))
506 ;; An obvious case of a key substitution:
507 (save-excursion
508 (while (re-search-forward
509 ;; Assume command name is only word and symbol
510 ;; characters to get things like `use M-x foo->bar'.
511 ;; Command required to end with word constituent
512 ;; to avoid `.' at end of a sentence.
513 "\\<M-x\\s-+\\(\\sw\\(\\sw\\|\\s_\\)*\\sw\\)" nil t)
514 (let ((sym (intern-soft (match-string 1))))
515 (if (fboundp sym)
516 (help-xref-button 1 'help-function sym)))))
517 ;; Look for commands in whole keymap substitutions:
518 (save-excursion
519 ;; Make sure to find the first keymap.
520 (goto-char (point-min))
521 ;; Find a header and the column at which the command
522 ;; name will be found.
524 ;; If the keymap substitution isn't the last thing in
525 ;; the doc string, and if there is anything on the same
526 ;; line after it, this code won't recognize the end of it.
527 (while (re-search-forward "^key +binding\n\\(-+ +\\)-+\n\n"
528 nil t)
529 (let ((col (- (match-end 1) (match-beginning 1))))
530 (while
531 (and (not (eobp))
532 ;; Stop at a pair of blank lines.
533 (not (looking-at-p "\n\\s-*\n")))
534 ;; Skip a single blank line.
535 (and (eolp) (forward-line))
536 (end-of-line)
537 (skip-chars-backward "^ \t\n")
538 (if (and (>= (current-column) col)
539 (looking-at "\\(\\sw\\|\\s_\\)+$"))
540 (let ((sym (intern-soft (match-string 0))))
541 (if (fboundp sym)
542 (help-xref-button 0 'help-function sym))))
543 (forward-line))))))
544 (set-syntax-table stab))
545 ;; Delete extraneous newlines at the end of the docstring
546 (goto-char (point-max))
547 (while (and (not (bobp)) (bolp))
548 (delete-char -1))
549 (insert "\n")
550 (when (or help-xref-stack help-xref-forward-stack)
551 (insert "\n"))
552 ;; Make a back-reference in this buffer if appropriate.
553 (when help-xref-stack
554 (help-insert-xref-button help-back-label 'help-back
555 (current-buffer)))
556 ;; Make a forward-reference in this buffer if appropriate.
557 (when help-xref-forward-stack
558 (when help-xref-stack
559 (insert "\t"))
560 (help-insert-xref-button help-forward-label 'help-forward
561 (current-buffer)))
562 (when (or help-xref-stack help-xref-forward-stack)
563 (insert "\n")))
564 (set-buffer-modified-p old-modified)))))
566 ;;;###autoload
567 (defun help-xref-button (match-number type &rest args)
568 "Make a hyperlink for cross-reference text previously matched.
569 MATCH-NUMBER is the subexpression of interest in the last matched
570 regexp. TYPE is the type of button to use. Any remaining arguments are
571 passed to the button's help-function when it is invoked.
572 See `help-make-xrefs'."
573 ;; Don't mung properties we've added specially in some instances.
574 (unless (button-at (match-beginning match-number))
575 (make-text-button (match-beginning match-number)
576 (match-end match-number)
577 'type type 'help-args args)))
579 ;;;###autoload
580 (defun help-insert-xref-button (string type &rest args)
581 "Insert STRING and make a hyperlink from cross-reference text on it.
582 TYPE is the type of button to use. Any remaining arguments are passed
583 to the button's help-function when it is invoked.
584 See `help-make-xrefs'."
585 (unless (button-at (point))
586 (insert-text-button string 'type type 'help-args args)))
588 ;;;###autoload
589 (defun help-xref-on-pp (from to)
590 "Add xrefs for symbols in `pp's output between FROM and TO."
591 (if (> (- to from) 5000) nil
592 (with-syntax-table emacs-lisp-mode-syntax-table
593 (save-excursion
594 (save-restriction
595 (narrow-to-region from to)
596 (goto-char (point-min))
597 (ignore-errors
598 (while (not (eobp))
599 (cond
600 ((looking-at-p "\"") (forward-sexp 1))
601 ((looking-at-p "#<") (search-forward ">" nil 'move))
602 ((looking-at "\\(\\(\\sw\\|\\s_\\)+\\)")
603 (let* ((sym (intern-soft (match-string 1)))
604 (type (cond ((fboundp sym) 'help-function)
605 ((or (memq sym '(t nil))
606 (keywordp sym))
607 nil)
608 ((and sym
609 (or (boundp sym)
610 (get sym
611 'variable-documentation)))
612 'help-variable))))
613 (when type (help-xref-button 1 type sym)))
614 (goto-char (match-end 1)))
615 (t (forward-char 1))))))))))
618 ;; Additional functions for (re-)creating types of help buffers.
620 ;;;###autoload
621 (define-obsolete-function-alias 'help-xref-interned 'describe-symbol "25.1")
624 ;; Navigation/hyperlinking with xrefs
626 (defun help-xref-go-back (buffer)
627 "From BUFFER, go back to previous help buffer text using `help-xref-stack'."
628 (let (item position method args)
629 (with-current-buffer buffer
630 (push (cons (point) help-xref-stack-item) help-xref-forward-stack)
631 (when help-xref-stack
632 (setq item (pop help-xref-stack)
633 ;; Clear the current item so that it won't get pushed
634 ;; by the function we're about to call. TODO: We could also
635 ;; push it onto a "forward" stack and add a `forw' button.
636 help-xref-stack-item nil
637 position (car item)
638 method (cadr item)
639 args (cddr item))))
640 (apply method args)
641 (with-current-buffer buffer
642 (if (get-buffer-window buffer)
643 (set-window-point (get-buffer-window buffer) position)
644 (goto-char position)))))
646 (defun help-xref-go-forward (buffer)
647 "From BUFFER, go forward to next help buffer."
648 (let (item position method args)
649 (with-current-buffer buffer
650 (push (cons (point) help-xref-stack-item) help-xref-stack)
651 (when help-xref-forward-stack
652 (setq item (pop help-xref-forward-stack)
653 ;; Clear the current item so that it won't get pushed
654 ;; by the function we're about to call. TODO: We could also
655 ;; push it onto a "forward" stack and add a `forw' button.
656 help-xref-stack-item nil
657 position (car item)
658 method (cadr item)
659 args (cddr item))))
660 (apply method args)
661 (with-current-buffer buffer
662 (if (get-buffer-window buffer)
663 (set-window-point (get-buffer-window buffer) position)
664 (goto-char position)))))
666 (defun help-go-back ()
667 "Go back to previous topic in this help buffer."
668 (interactive)
669 (if help-xref-stack
670 (help-xref-go-back (current-buffer))
671 (user-error "No previous help buffer")))
673 (defun help-go-forward ()
674 "Go to the next topic in this help buffer."
675 (interactive)
676 (if help-xref-forward-stack
677 (help-xref-go-forward (current-buffer))
678 (user-error "No next help buffer")))
680 (defun help-do-xref (_pos function args)
681 "Call the help cross-reference function FUNCTION with args ARGS.
682 Things are set up properly so that the resulting help-buffer has
683 a proper [back] button."
684 ;; There is a reference at point. Follow it.
685 (let ((help-xref-following t))
686 (apply function (if (eq function 'info)
687 (append args (list (generate-new-buffer-name "*info*"))) args))))
689 ;; The doc string is meant to explain what buttons do.
690 (defun help-follow-mouse ()
691 "Follow the cross-reference that you click on."
692 (interactive)
693 (error "No cross-reference here"))
695 ;; The doc string is meant to explain what buttons do.
696 (defun help-follow ()
697 "Follow cross-reference at point.
699 For the cross-reference format, see `help-make-xrefs'."
700 (interactive)
701 (user-error "No cross-reference here"))
703 (defun help-follow-symbol (&optional pos)
704 "In help buffer, show docs for symbol at POS, defaulting to point.
705 Show all docs for that symbol as either a variable, function or face."
706 (interactive "d")
707 (unless pos
708 (setq pos (point)))
709 ;; check if the symbol under point is a function, variable or face
710 (let ((sym
711 (intern
712 (save-excursion
713 (goto-char pos) (skip-syntax-backward "w_")
714 (buffer-substring (point)
715 (progn (skip-syntax-forward "w_")
716 (point)))))))
717 (when (or (boundp sym)
718 (get sym 'variable-documentation)
719 (fboundp sym) (facep sym))
720 (help-do-xref pos #'describe-symbol (list sym)))))
722 (defun help-mode-revert-buffer (_ignore-auto noconfirm)
723 (when (or noconfirm (yes-or-no-p "Revert help buffer? "))
724 (let ((pos (point))
725 (item help-xref-stack-item)
726 ;; Pretend there is no current item to add to the history.
727 (help-xref-stack-item nil)
728 ;; Use the current buffer.
729 (help-xref-following t))
730 (apply (car item) (cdr item))
731 (goto-char pos))))
733 (defun help-insert-string (string)
734 "Insert STRING to the help buffer and install xref info for it.
735 This function can be used to restore the old contents of the help buffer
736 when going back to the previous topic in the xref stack. It is needed
737 in case when it is impossible to recompute the old contents of the
738 help buffer by other means."
739 (setq help-xref-stack-item (list #'help-insert-string string))
740 (with-output-to-temp-buffer (help-buffer)
741 (insert string)))
744 ;; Bookmark support
746 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
747 (declare-function bookmark-make-record-default "bookmark"
748 (&optional no-file no-context posn))
750 (defun help-bookmark-make-record ()
751 "Create and return a help-mode bookmark record.
752 Implements `bookmark-make-record-function' for help-mode buffers."
753 (unless (car help-xref-stack-item)
754 (error "Cannot create bookmark - help command not known"))
755 `(,@(bookmark-make-record-default 'NO-FILE 'NO-CONTEXT)
756 (help-fn . ,(car help-xref-stack-item))
757 (help-args . ,(cdr help-xref-stack-item))
758 (position . ,(point))
759 (handler . help-bookmark-jump)))
761 ;;;###autoload
762 (defun help-bookmark-jump (bookmark)
763 "Jump to help-mode bookmark BOOKMARK.
764 Handler function for record returned by `help-bookmark-make-record'.
765 BOOKMARK is a bookmark name or a bookmark record."
766 (let ((help-fn (bookmark-prop-get bookmark 'help-fn))
767 (help-args (bookmark-prop-get bookmark 'help-args))
768 (position (bookmark-prop-get bookmark 'position)))
769 (apply help-fn help-args)
770 (pop-to-buffer "*Help*")
771 (goto-char position)))
774 (provide 'help-mode)
776 ;;; help-mode.el ends here