(frame-parameter) <defsetf>: Make it return the assigned value.
[emacs.git] / lisp / help-mode.el
blob841ad8e251280c3b5ac8452672a7e1be70d969d6
1 ;;; help-mode.el --- `help-mode' used by *Help* buffers
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: help, internal
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; Defines `help-mode', which is the mode used by *Help* buffers, and
29 ;; associated support machinery, such as adding hyperlinks, etc.,
31 ;;; Code:
33 (require 'button)
34 (require 'view)
35 (eval-when-compile (require 'easymenu))
37 (defvar help-mode-map (make-sparse-keymap)
38 "Keymap for help mode.")
40 (set-keymap-parent help-mode-map button-buffer-map)
42 (define-key help-mode-map [mouse-2] 'help-follow-mouse)
43 (define-key help-mode-map "\C-c\C-b" 'help-go-back)
44 (define-key help-mode-map "\C-c\C-f" 'help-go-forward)
45 (define-key help-mode-map "\C-c\C-c" 'help-follow-symbol)
46 ;; Documentation only, since we use minor-mode-overriding-map-alist.
47 (define-key help-mode-map "\r" 'help-follow)
49 (easy-menu-define help-mode-menu help-mode-map
50 "Menu for Help Mode."
51 '("Help-Mode"
52 ["Show Help for Symbol" help-follow-symbol
53 :help "Show the docs for the symbol at point"]
54 ["Previous Topic" help-go-back
55 :help "Go back to previous topic in this help buffer"]
56 ["Next Topic" help-go-forward
57 :help "Go back to next topic in this help buffer"]
58 ["Move to Previous Button" backward-button
59 :help "Move to the Next Button in the help buffer"]
60 ["Move to Next Button" forward-button
61 :help "Move to the Next Button in the help buffer"]))
63 (defvar help-xref-stack nil
64 "A stack of ways by which to return to help buffers after following xrefs.
65 Used by `help-follow' and `help-xref-go-back'.
66 An element looks like (POSITION FUNCTION ARGS...).
67 To use the element, do (apply FUNCTION ARGS) then goto the point.")
68 (put 'help-xref-stack 'permanent-local t)
69 (make-variable-buffer-local 'help-xref-stack)
71 (defvar help-xref-forward-stack nil
72 "The stack of used to navigate help forwards after using the back button.
73 Used by `help-follow' and `help-xref-go-forward'.
74 An element looks like (POSITION FUNCTION ARGS...).
75 To use the element, do (apply FUNCTION ARGS) then goto the point.")
76 (put 'help-xref-forward-stack 'permanent-local t)
77 (make-variable-buffer-local 'help-xref-forward-stack)
79 (defvar help-xref-stack-item nil
80 "An item for `help-follow' in this buffer to push onto `help-xref-stack'.
81 The format is (FUNCTION ARGS...).")
82 (put 'help-xref-stack-item 'permanent-local t)
83 (make-variable-buffer-local 'help-xref-stack-item)
85 (defvar help-xref-stack-forward-item nil
86 "An item for `help-go-back' to push onto `help-xref-forward-stack'.
87 The format is (FUNCTION ARGS...).")
88 (put 'help-xref-stack-forward-item 'permanent-local t)
89 (make-variable-buffer-local 'help-xref-stack-forward-item)
91 (setq-default help-xref-stack nil help-xref-stack-item nil)
92 (setq-default help-xref-forward-stack nil help-xref-forward-stack-item nil)
94 (defcustom help-mode-hook nil
95 "Hook run by `help-mode'."
96 :type 'hook
97 :group 'help)
99 ;; Button types used by help
101 (define-button-type 'help-xref
102 'follow-link t
103 'action #'help-button-action)
105 (defun help-button-action (button)
106 "Call BUTTON's help function."
107 (help-do-xref (button-start button)
108 (button-get button 'help-function)
109 (button-get button 'help-args)))
111 ;; These 6 calls to define-button-type were generated in a dolist
112 ;; loop, but that is bad because it means these button types don't
113 ;; have an easily found definition.
115 (define-button-type 'help-function
116 :supertype 'help-xref
117 'help-function 'describe-function
118 'help-echo (purecopy "mouse-2, RET: describe this function"))
120 (define-button-type 'help-variable
121 :supertype 'help-xref
122 'help-function 'describe-variable
123 'help-echo (purecopy "mouse-2, RET: describe this variable"))
125 (define-button-type 'help-face
126 :supertype 'help-xref
127 'help-function 'describe-face
128 'help-echo (purecopy "mouse-2, RET: describe this face"))
130 (define-button-type 'help-coding-system
131 :supertype 'help-xref
132 'help-function 'describe-coding-system
133 'help-echo (purecopy "mouse-2, RET: describe this coding system"))
135 (define-button-type 'help-input-method
136 :supertype 'help-xref
137 'help-function 'describe-input-method
138 'help-echo (purecopy "mouse-2, RET: describe this input method"))
140 (define-button-type 'help-character-set
141 :supertype 'help-xref
142 'help-function 'describe-character-set
143 'help-echo (purecopy "mouse-2, RET: describe this character set"))
145 ;; make some more ideosyncratic button types
147 (define-button-type 'help-symbol
148 :supertype 'help-xref
149 'help-function #'help-xref-interned
150 'help-echo (purecopy "mouse-2, RET: describe this symbol"))
152 (define-button-type 'help-back
153 :supertype 'help-xref
154 'help-function #'help-xref-go-back
155 'help-echo (purecopy "mouse-2, RET: go back to previous help buffer"))
157 (define-button-type 'help-forward
158 :supertype 'help-xref
159 'help-function #'help-xref-go-forward
160 'help-echo (purecopy "mouse-2, RET: move forward to next help buffer"))
162 (define-button-type 'help-info-variable
163 :supertype 'help-xref
164 ;; the name of the variable is put before the argument to Info
165 'help-function (lambda (a v) (info v))
166 'help-echo (purecopy "mouse-2, RET: read this Info node"))
168 (define-button-type 'help-info
169 :supertype 'help-xref
170 'help-function #'info
171 'help-echo (purecopy "mouse-2, RET: read this Info node"))
173 (define-button-type 'help-url
174 :supertype 'help-xref
175 'help-function #'browse-url
176 'help-echo (purecopy "mouse-2, RET: view this URL in a browser"))
178 (define-button-type 'help-customize-variable
179 :supertype 'help-xref
180 'help-function (lambda (v)
181 (customize-variable v))
182 'help-echo (purecopy "mouse-2, RET: customize variable"))
184 (define-button-type 'help-customize-face
185 :supertype 'help-xref
186 'help-function (lambda (v)
187 (customize-face v))
188 'help-echo (purecopy "mouse-2, RET: customize face"))
190 (define-button-type 'help-function-def
191 :supertype 'help-xref
192 'help-function (lambda (fun file)
193 (require 'find-func)
194 (when (eq file 'C-source)
195 (setq file
196 (help-C-file-name (indirect-function fun) 'fun)))
197 ;; Don't use find-function-noselect because it follows
198 ;; aliases (which fails for built-in functions).
199 (let ((location
200 (find-function-search-for-symbol fun nil file)))
201 (pop-to-buffer (car location))
202 (if (cdr location)
203 (goto-char (cdr location))
204 (message "Unable to find location in file"))))
205 'help-echo (purecopy "mouse-2, RET: find function's definition"))
207 (define-button-type 'help-variable-def
208 :supertype 'help-xref
209 'help-function (lambda (var &optional file)
210 (when (eq file 'C-source)
211 (setq file (help-C-file-name var 'var)))
212 (let ((location (find-variable-noselect var file)))
213 (pop-to-buffer (car location))
214 (if (cdr location)
215 (goto-char (cdr location))
216 (message "Unable to find location in file"))))
217 'help-echo (purecopy "mouse-2, RET: find variable's definition"))
219 (define-button-type 'help-face-def
220 :supertype 'help-xref
221 'help-function (lambda (fun file)
222 (require 'find-func)
223 ;; Don't use find-function-noselect because it follows
224 ;; aliases (which fails for built-in functions).
225 (let ((location
226 (find-function-search-for-symbol fun 'defface file)))
227 (pop-to-buffer (car location))
228 (if (cdr location)
229 (goto-char (cdr location))
230 (message "Unable to find location in file"))))
231 'help-echo (purecopy "mouse-2, RET: find face's definition"))
234 ;;;###autoload
235 (defun help-mode ()
236 "Major mode for viewing help text and navigating references in it.
237 Entry to this mode runs the normal hook `help-mode-hook'.
238 Commands:
239 \\{help-mode-map}"
240 (interactive)
241 (kill-all-local-variables)
242 (use-local-map help-mode-map)
243 (setq mode-name "Help")
244 (setq major-mode 'help-mode)
246 (view-mode)
247 (set (make-local-variable 'view-no-disable-on-exit) t)
248 ;; With Emacs 22 `view-exit-action' could delete the selected window
249 ;; disregarding whether the help buffer was shown in that window at
250 ;; all. Since `view-exit-action' is called with the help buffer as
251 ;; argument it seems more appropriate to have it work on the buffer
252 ;; only and leave it to `view-mode-exit' to delete any associated
253 ;; window(s).
254 (setq view-exit-action
255 (lambda (buffer)
256 ;; Use `with-current-buffer' to make sure that `bury-buffer'
257 ;; also removes BUFFER from the selected window.
258 (with-current-buffer buffer
259 (bury-buffer))))
261 (run-mode-hooks 'help-mode-hook))
263 ;;;###autoload
264 (defun help-mode-setup ()
265 (help-mode)
266 (setq buffer-read-only nil))
268 ;;;###autoload
269 (defun help-mode-finish ()
270 (if (eq help-window t)
271 ;; If `help-window' is t, `view-return-to-alist' is handled by
272 ;; `with-help-window'. In this case set `help-window' to the
273 ;; selected window since now is the only moment where we can
274 ;; unambiguously identify it.
275 (setq help-window (selected-window))
276 (let ((entry (assq (selected-window) view-return-to-alist)))
277 (if entry
278 ;; When entering Help mode from the Help window,
279 ;; such as by following a link, preserve the same
280 ;; meaning for the q command.
281 ;; (setcdr entry (cons (selected-window) help-return-method))
283 (setq view-return-to-alist
284 (cons (cons (selected-window) help-return-method)
285 view-return-to-alist)))))
287 (when (eq major-mode 'help-mode)
288 ;; View mode's read-only status of existing *Help* buffer is lost
289 ;; by with-output-to-temp-buffer.
290 (toggle-read-only 1)
291 (help-make-xrefs (current-buffer))))
293 ;; Grokking cross-reference information in doc strings and
294 ;; hyperlinking it.
296 ;; This may have some scope for extension and the same or something
297 ;; similar should be done for widget doc strings, which currently use
298 ;; another mechanism.
300 (defvar help-back-label (purecopy "[back]")
301 "Label to use by `help-make-xrefs' for the go-back reference.")
303 (defvar help-forward-label (purecopy "[forward]")
304 "Label to use by `help-make-xrefs' for the go-forward reference.")
306 (defconst help-xref-symbol-regexp
307 (purecopy (concat "\\(\\<\\(\\(variable\\|option\\)\\|" ; Link to var
308 "\\(function\\|command\\)\\|" ; Link to function
309 "\\(face\\)\\|" ; Link to face
310 "\\(symbol\\|program\\|property\\)\\|" ; Don't link
311 "\\(source \\(?:code \\)?\\(?:of\\|for\\)\\)\\)"
312 "[ \t\n]+\\)?"
313 ;; Note starting with word-syntax character:
314 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'"))
315 "Regexp matching doc string references to symbols.
317 The words preceding the quoted symbol can be used in doc strings to
318 distinguish references to variables, functions and symbols.")
320 (defvar help-xref-mule-regexp nil
321 "Regexp matching doc string references to MULE-related keywords.
323 It is usually nil, and is temporarily bound to an appropriate regexp
324 when help commands related to multilingual environment (e.g.,
325 `describe-coding-system') are invoked.")
328 (defconst help-xref-info-regexp
329 (purecopy "\\<[Ii]nfo[ \t\n]+\\(node\\|anchor\\)[ \t\n]+`\\([^']+\\)'")
330 "Regexp matching doc string references to an Info node.")
332 (defconst help-xref-url-regexp
333 (purecopy "\\<[Uu][Rr][Ll][ \t\n]+`\\([^']+\\)'")
334 "Regexp matching doc string references to a URL.")
336 ;;;###autoload
337 (defun help-setup-xref (item interactive-p)
338 "Invoked from commands using the \"*Help*\" buffer to install some xref info.
340 ITEM is a (FUNCTION . ARGS) pair appropriate for recreating the help
341 buffer after following a reference. INTERACTIVE-P is non-nil if the
342 calling command was invoked interactively. In this case the stack of
343 items for help buffer \"back\" buttons is cleared.
345 This should be called very early, before the output buffer is cleared,
346 because we want to record the \"previous\" position of point so we can
347 restore it properly when going back."
348 (with-current-buffer (help-buffer)
349 (when help-xref-stack-item
350 (push (cons (point) help-xref-stack-item) help-xref-stack)
351 (setq help-xref-forward-stack nil))
352 (when interactive-p
353 (let ((tail (nthcdr 10 help-xref-stack)))
354 ;; Truncate the stack.
355 (if tail (setcdr tail nil))))
356 (setq help-xref-stack-item item)))
358 (defvar help-xref-following nil
359 "Non-nil when following a help cross-reference.")
361 ;;;###autoload
362 (defun help-buffer ()
363 (buffer-name ;for with-output-to-temp-buffer
364 (if help-xref-following
365 (current-buffer)
366 (get-buffer-create "*Help*"))))
368 (defvar help-xref-override-view-map
369 (let ((map (make-sparse-keymap)))
370 (set-keymap-parent map view-mode-map)
371 (define-key map "\r" nil)
372 map)
373 "Replacement keymap for `view-mode' in help buffers.")
375 ;;;###autoload
376 (defun help-make-xrefs (&optional buffer)
377 "Parse and hyperlink documentation cross-references in the given BUFFER.
379 Find cross-reference information in a buffer and activate such cross
380 references for selection with `help-follow'. Cross-references have
381 the canonical form `...' and the type of reference may be
382 disambiguated by the preceding word(s) used in
383 `help-xref-symbol-regexp'. Faces only get cross-referenced if
384 preceded or followed by the word `face'. Variables without
385 variable documentation do not get cross-referenced, unless
386 preceded by the word `variable' or `option'.
388 If the variable `help-xref-mule-regexp' is non-nil, find also
389 cross-reference information related to multilingual environment
390 \(e.g., coding-systems). This variable is also used to disambiguate
391 the type of reference as the same way as `help-xref-symbol-regexp'.
393 A special reference `back' is made to return back through a stack of
394 help buffers. Variable `help-back-label' specifies the text for
395 that."
396 (interactive "b")
397 (save-excursion
398 (set-buffer (or buffer (current-buffer)))
399 (goto-char (point-min))
400 ;; Skip the header-type info, though it might be useful to parse
401 ;; it at some stage (e.g. "function in `library'").
402 (forward-paragraph)
403 (let ((old-modified (buffer-modified-p)))
404 (let ((stab (syntax-table))
405 (case-fold-search t)
406 (inhibit-read-only t))
407 (set-syntax-table emacs-lisp-mode-syntax-table)
408 ;; The following should probably be abstracted out.
409 (unwind-protect
410 (progn
411 ;; Info references
412 (save-excursion
413 (while (re-search-forward help-xref-info-regexp nil t)
414 (let ((data (match-string 2)))
415 (save-match-data
416 (unless (string-match "^([^)]+)" data)
417 (setq data (concat "(emacs)" data))))
418 (help-xref-button 2 'help-info data))))
419 ;; URLs
420 (save-excursion
421 (while (re-search-forward help-xref-url-regexp nil t)
422 (let ((data (match-string 1)))
423 (help-xref-button 1 'help-url data))))
424 ;; Mule related keywords. Do this before trying
425 ;; `help-xref-symbol-regexp' because some of Mule
426 ;; keywords have variable or function definitions.
427 (if help-xref-mule-regexp
428 (save-excursion
429 (while (re-search-forward help-xref-mule-regexp nil t)
430 (let* ((data (match-string 7))
431 (sym (intern-soft data)))
432 (cond
433 ((match-string 3) ; coding system
434 (and sym (coding-system-p sym)
435 (help-xref-button 6 'help-coding-system sym)))
436 ((match-string 4) ; input method
437 (and (assoc data input-method-alist)
438 (help-xref-button 7 'help-input-method data)))
439 ((or (match-string 5) (match-string 6)) ; charset
440 (and sym (charsetp sym)
441 (help-xref-button 7 'help-character-set sym)))
442 ((assoc data input-method-alist)
443 (help-xref-button 7 'help-character-set data))
444 ((and sym (coding-system-p sym))
445 (help-xref-button 7 'help-coding-system sym))
446 ((and sym (charsetp sym))
447 (help-xref-button 7 'help-character-set sym)))))))
448 ;; Quoted symbols
449 (save-excursion
450 (while (re-search-forward help-xref-symbol-regexp nil t)
451 (let* ((data (match-string 8))
452 (sym (intern-soft data)))
453 (if sym
454 (cond
455 ((match-string 3) ; `variable' &c
456 (and (or (boundp sym) ; `variable' doesn't ensure
457 ; it's actually bound
458 (get sym 'variable-documentation))
459 (help-xref-button 8 'help-variable sym)))
460 ((match-string 4) ; `function' &c
461 (and (fboundp sym) ; similarly
462 (help-xref-button 8 'help-function sym)))
463 ((match-string 5) ; `face'
464 (and (facep sym)
465 (help-xref-button 8 'help-face sym)))
466 ((match-string 6)) ; nothing for `symbol'
467 ((match-string 7)
468 ;;; this used:
469 ;;; #'(lambda (arg)
470 ;;; (let ((location
471 ;;; (find-function-noselect arg)))
472 ;;; (pop-to-buffer (car location))
473 ;;; (goto-char (cdr location))))
474 (help-xref-button 8 'help-function-def sym))
475 ((and
476 (facep sym)
477 (save-match-data (looking-at "[ \t\n]+face\\W")))
478 (help-xref-button 8 'help-face sym))
479 ((and (or (boundp sym)
480 (get sym 'variable-documentation))
481 (fboundp sym))
482 ;; We can't intuit whether to use the
483 ;; variable or function doc -- supply both.
484 (help-xref-button 8 'help-symbol sym))
485 ((and
486 (or (boundp sym)
487 (get sym 'variable-documentation))
489 (documentation-property
490 sym 'variable-documentation)
491 (condition-case nil
492 (documentation-property
493 (indirect-variable sym)
494 'variable-documentation)
495 (cyclic-variable-indirection nil))))
496 (help-xref-button 8 'help-variable sym))
497 ((fboundp sym)
498 (help-xref-button 8 'help-function sym)))))))
499 ;; An obvious case of a key substitution:
500 (save-excursion
501 (while (re-search-forward
502 ;; Assume command name is only word and symbol
503 ;; characters to get things like `use M-x foo->bar'.
504 ;; Command required to end with word constituent
505 ;; to avoid `.' at end of a sentence.
506 "\\<M-x\\s-+\\(\\sw\\(\\sw\\|\\s_\\)*\\sw\\)" nil t)
507 (let ((sym (intern-soft (match-string 1))))
508 (if (fboundp sym)
509 (help-xref-button 1 'help-function sym)))))
510 ;; Look for commands in whole keymap substitutions:
511 (save-excursion
512 ;; Make sure to find the first keymap.
513 (goto-char (point-min))
514 ;; Find a header and the column at which the command
515 ;; name will be found.
517 ;; If the keymap substitution isn't the last thing in
518 ;; the doc string, and if there is anything on the
519 ;; same line after it, this code won't recognize the end of it.
520 (while (re-search-forward "^key +binding\n\\(-+ +\\)-+\n\n"
521 nil t)
522 (let ((col (- (match-end 1) (match-beginning 1))))
523 (while
524 (and (not (eobp))
525 ;; Stop at a pair of blank lines.
526 (not (looking-at "\n\\s-*\n")))
527 ;; Skip a single blank line.
528 (and (eolp) (forward-line))
529 (end-of-line)
530 (skip-chars-backward "^ \t\n")
531 (if (and (>= (current-column) col)
532 (looking-at "\\(\\sw\\|\\s_\\)+$"))
533 (let ((sym (intern-soft (match-string 0))))
534 (if (fboundp sym)
535 (help-xref-button 0 'help-function sym))))
536 (forward-line))))))
537 (set-syntax-table stab))
538 ;; Delete extraneous newlines at the end of the docstring
539 (goto-char (point-max))
540 (while (and (not (bobp)) (bolp))
541 (delete-char -1))
542 (insert "\n")
543 (when (or help-xref-stack help-xref-forward-stack)
544 (insert "\n"))
545 ;; Make a back-reference in this buffer if appropriate.
546 (when help-xref-stack
547 (help-insert-xref-button help-back-label 'help-back
548 (current-buffer)))
549 ;; Make a forward-reference in this buffer if appropriate.
550 (when help-xref-forward-stack
551 (when help-xref-stack
552 (insert "\t"))
553 (help-insert-xref-button help-forward-label 'help-forward
554 (current-buffer)))
555 (when (or help-xref-stack help-xref-forward-stack)
556 (insert "\n")))
557 ;; View mode steals RET from us.
558 (set (make-local-variable 'minor-mode-overriding-map-alist)
559 (list (cons 'view-mode help-xref-override-view-map)))
560 (set-buffer-modified-p old-modified))))
562 ;;;###autoload
563 (defun help-xref-button (match-number type &rest args)
564 "Make a hyperlink for cross-reference text previously matched.
565 MATCH-NUMBER is the subexpression of interest in the last matched
566 regexp. TYPE is the type of button to use. Any remaining arguments are
567 passed to the button's help-function when it is invoked.
568 See `help-make-xrefs'."
569 ;; Don't mung properties we've added specially in some instances.
570 (unless (button-at (match-beginning match-number))
571 (make-text-button (match-beginning match-number)
572 (match-end match-number)
573 'type type 'help-args args)))
575 ;;;###autoload
576 (defun help-insert-xref-button (string type &rest args)
577 "Insert STRING and make a hyperlink from cross-reference text on it.
578 TYPE is the type of button to use. Any remaining arguments are passed
579 to the button's help-function when it is invoked.
580 See `help-make-xrefs'."
581 (unless (button-at (point))
582 (insert-text-button string 'type type 'help-args args)))
584 ;;;###autoload
585 (defun help-xref-on-pp (from to)
586 "Add xrefs for symbols in `pp's output between FROM and TO."
587 (if (> (- to from) 5000) nil
588 (with-syntax-table emacs-lisp-mode-syntax-table
589 (save-excursion
590 (save-restriction
591 (narrow-to-region from to)
592 (goto-char (point-min))
593 (condition-case nil
594 (while (not (eobp))
595 (cond
596 ((looking-at "\"") (forward-sexp 1))
597 ((looking-at "#<") (search-forward ">" nil 'move))
598 ((looking-at "\\(\\(\\sw\\|\\s_\\)+\\)")
599 (let* ((sym (intern-soft (match-string 1)))
600 (type (cond ((fboundp sym) 'help-function)
601 ((or (memq sym '(t nil))
602 (keywordp sym))
603 nil)
604 ((and sym
605 (or (boundp sym)
606 (get sym
607 'variable-documentation)))
608 'help-variable))))
609 (when type (help-xref-button 1 type sym)))
610 (goto-char (match-end 1)))
611 (t (forward-char 1))))
612 (error nil)))))))
615 ;; Additional functions for (re-)creating types of help buffers.
616 (defun help-xref-interned (symbol)
617 "Follow a hyperlink which appeared to be an arbitrary interned SYMBOL.
618 Both variable, function and face documentation are extracted into a single
619 help buffer."
620 (with-current-buffer (help-buffer)
621 ;; Push the previous item on the stack before clobbering the output buffer.
622 (help-setup-xref nil nil)
623 (let ((facedoc (when (facep symbol)
624 ;; Don't record the current entry in the stack.
625 (setq help-xref-stack-item nil)
626 (describe-face symbol)))
627 (fdoc (when (fboundp symbol)
628 ;; Don't record the current entry in the stack.
629 (setq help-xref-stack-item nil)
630 (describe-function symbol)))
631 (sdoc (when (or (boundp symbol)
632 (get symbol 'variable-documentation))
633 ;; Don't record the current entry in the stack.
634 (setq help-xref-stack-item nil)
635 (describe-variable symbol))))
636 (cond
637 (sdoc
638 ;; We now have a help buffer on the variable.
639 ;; Insert the function and face text before it.
640 (when (or fdoc facedoc)
641 (goto-char (point-min))
642 (let ((inhibit-read-only t))
643 (when fdoc
644 (insert fdoc "\n\n")
645 (when facedoc
646 (insert (make-string 30 ?-) "\n\n" (symbol-name symbol)
647 " is also a " "face." "\n\n")))
648 (when facedoc
649 (insert facedoc "\n\n"))
650 (insert (make-string 30 ?-) "\n\n" (symbol-name symbol)
651 " is also a " "variable." "\n\n"))
652 ;; Don't record the `describe-variable' item in the stack.
653 (setq help-xref-stack-item nil)
654 (help-setup-xref (list #'help-xref-interned symbol) nil)))
655 (fdoc
656 ;; We now have a help buffer on the function.
657 ;; Insert face text before it.
658 (when facedoc
659 (goto-char (point-max))
660 (let ((inhibit-read-only t))
661 (insert "\n\n" (make-string 30 ?-) "\n\n" (symbol-name symbol)
662 " is also a " "face." "\n\n" facedoc))
663 ;; Don't record the `describe-function' item in the stack.
664 (setq help-xref-stack-item nil)
665 (help-setup-xref (list #'help-xref-interned symbol) nil)))))))
668 ;; Navigation/hyperlinking with xrefs
670 (defun help-xref-go-back (buffer)
671 "From BUFFER, go back to previous help buffer text using `help-xref-stack'."
672 (let (item position method args)
673 (with-current-buffer buffer
674 (push (cons (point) help-xref-stack-item) help-xref-forward-stack)
675 (when help-xref-stack
676 (setq item (pop help-xref-stack)
677 ;; Clear the current item so that it won't get pushed
678 ;; by the function we're about to call. TODO: We could also
679 ;; push it onto a "forward" stack and add a `forw' button.
680 help-xref-stack-item nil
681 position (car item)
682 method (cadr item)
683 args (cddr item))))
684 (apply method args)
685 (with-current-buffer buffer
686 (if (get-buffer-window buffer)
687 (set-window-point (get-buffer-window buffer) position)
688 (goto-char position)))))
690 (defun help-xref-go-forward (buffer)
691 "From BUFFER, go forward to next help buffer."
692 (let (item position method args)
693 (with-current-buffer buffer
694 (push (cons (point) help-xref-stack-item) help-xref-stack)
695 (when help-xref-forward-stack
696 (setq item (pop help-xref-forward-stack)
697 ;; Clear the current item so that it won't get pushed
698 ;; by the function we're about to call. TODO: We could also
699 ;; push it onto a "forward" stack and add a `forw' button.
700 help-xref-stack-item nil
701 position (car item)
702 method (cadr item)
703 args (cddr item))))
704 (apply method args)
705 (with-current-buffer buffer
706 (if (get-buffer-window buffer)
707 (set-window-point (get-buffer-window buffer) position)
708 (goto-char position)))))
710 (defun help-go-back ()
711 "Go back to previous topic in this help buffer."
712 (interactive)
713 (if help-xref-stack
714 (help-xref-go-back (current-buffer))
715 (error "No previous help buffer")))
717 (defun help-go-forward ()
718 "Go back to next topic in this help buffer."
719 (interactive)
720 (if help-xref-forward-stack
721 (help-xref-go-forward (current-buffer))
722 (error "No next help buffer")))
724 (defun help-do-xref (pos function args)
725 "Call the help cross-reference function FUNCTION with args ARGS.
726 Things are set up properly so that the resulting help-buffer has
727 a proper [back] button."
728 ;; There is a reference at point. Follow it.
729 (let ((help-xref-following t))
730 (apply function args)))
732 ;; The doc string is meant to explain what buttons do.
733 (defun help-follow-mouse ()
734 "Follow the cross-reference that you click on."
735 (interactive)
736 (error "No cross-reference here"))
738 ;; The doc string is meant to explain what buttons do.
739 (defun help-follow ()
740 "Follow cross-reference at point.
742 For the cross-reference format, see `help-make-xrefs'."
743 (interactive)
744 (error "No cross-reference here"))
746 (defun help-follow-symbol (&optional pos)
747 "In help buffer, show docs for symbol at POS, defaulting to point.
748 Show all docs for that symbol as either a variable, function or face."
749 (interactive "d")
750 (unless pos
751 (setq pos (point)))
752 ;; check if the symbol under point is a function, variable or face
753 (let ((sym
754 (intern
755 (save-excursion
756 (goto-char pos) (skip-syntax-backward "w_")
757 (buffer-substring (point)
758 (progn (skip-syntax-forward "w_")
759 (point)))))))
760 (when (or (boundp sym)
761 (get sym 'variable-documentation)
762 (fboundp sym) (facep sym))
763 (help-do-xref pos #'help-xref-interned (list sym)))))
765 (defun help-insert-string (string)
766 "Insert STRING to the help buffer and install xref info for it.
767 This function can be used to restore the old contents of the help buffer
768 when going back to the previous topic in the xref stack. It is needed
769 in case when it is impossible to recompute the old contents of the
770 help buffer by other means."
771 (setq help-xref-stack-item (list #'help-insert-string string))
772 (with-output-to-temp-buffer (help-buffer)
773 (insert string)))
775 (provide 'help-mode)
777 ;; arch-tag: 850954ae-3725-4cb4-8e91-0bf6d52d6b0b
778 ;;; help-mode.el ends here