help-mode.el 30 KB

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