help-mode.el 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. ;;; help-mode.el --- `help-mode' used by *Help* buffers
  2. ;; Copyright (C) 1985-1986, 1993-1994, 1998-2017 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 nil
  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. (run-hooks 'find-function-after-hook)
  168. (if (cdr location)
  169. (goto-char (cdr location))
  170. (message "Unable to find location in file"))))
  171. 'help-echo (purecopy "mouse-2, RET: find function's definition"))
  172. (define-button-type 'help-function-cmacro ; FIXME: Obsolete since 24.4.
  173. :supertype 'help-xref
  174. 'help-function (lambda (fun file)
  175. (setq file (locate-library file t))
  176. (if (and file (file-readable-p file))
  177. (progn
  178. (pop-to-buffer (find-file-noselect file))
  179. (goto-char (point-min))
  180. (if (re-search-forward
  181. (format "^[ \t]*(\\(cl-\\)?define-compiler-macro[ \t]+%s"
  182. (regexp-quote (symbol-name fun)))
  183. nil t)
  184. (forward-line 0)
  185. (message "Unable to find location in file")))
  186. (message "Unable to find file")))
  187. 'help-echo (purecopy "mouse-2, RET: find function's compiler macro"))
  188. (define-button-type 'help-variable-def
  189. :supertype 'help-xref
  190. 'help-function (lambda (var &optional file)
  191. (when (eq file 'C-source)
  192. (setq file (help-C-file-name var 'var)))
  193. (let ((location (find-variable-noselect var file)))
  194. (pop-to-buffer (car location))
  195. (run-hooks 'find-function-after-hook)
  196. (if (cdr location)
  197. (goto-char (cdr location))
  198. (message "Unable to find location in file"))))
  199. 'help-echo (purecopy "mouse-2, RET: find variable's definition"))
  200. (define-button-type 'help-face-def
  201. :supertype 'help-xref
  202. 'help-function (lambda (fun file)
  203. (require 'find-func)
  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 'defface file)))
  208. (pop-to-buffer (car location))
  209. (if (cdr location)
  210. (goto-char (cdr location))
  211. (message "Unable to find location in file"))))
  212. 'help-echo (purecopy "mouse-2, RET: find face's definition"))
  213. (define-button-type 'help-package
  214. :supertype 'help-xref
  215. 'help-function 'describe-package
  216. 'help-echo (purecopy "mouse-2, RET: Describe package"))
  217. (define-button-type 'help-package-def
  218. :supertype 'help-xref
  219. 'help-function (lambda (file) (dired file))
  220. 'help-echo (purecopy "mouse-2, RET: visit package directory"))
  221. (define-button-type 'help-theme-def
  222. :supertype 'help-xref
  223. 'help-function 'find-file
  224. 'help-echo (purecopy "mouse-2, RET: visit theme file"))
  225. (define-button-type 'help-theme-edit
  226. :supertype 'help-xref
  227. 'help-function 'customize-create-theme
  228. 'help-echo (purecopy "mouse-2, RET: edit this theme file"))
  229. (define-button-type 'help-dir-local-var-def
  230. :supertype 'help-xref
  231. 'help-function (lambda (_var &optional file)
  232. ;; FIXME: this should go to the point where the
  233. ;; local variable was defined.
  234. (find-file file))
  235. 'help-echo (purecopy "mouse-2, RET: open directory-local variables file"))
  236. (defvar bookmark-make-record-function)
  237. ;;;###autoload
  238. (define-derived-mode help-mode special-mode "Help"
  239. "Major mode for viewing help text and navigating references in it.
  240. Entry to this mode runs the normal hook `help-mode-hook'.
  241. Commands:
  242. \\{help-mode-map}"
  243. (set (make-local-variable 'revert-buffer-function)
  244. 'help-mode-revert-buffer)
  245. (set (make-local-variable 'bookmark-make-record-function)
  246. 'help-bookmark-make-record))
  247. ;;;###autoload
  248. (defun help-mode-setup ()
  249. "Enter Help Mode in the current buffer."
  250. (help-mode)
  251. (setq buffer-read-only nil))
  252. ;;;###autoload
  253. (defun help-mode-finish ()
  254. "Finalize Help Mode setup in current buffer."
  255. (when (derived-mode-p 'help-mode)
  256. (setq buffer-read-only t)
  257. (help-make-xrefs (current-buffer))))
  258. ;; Grokking cross-reference information in doc strings and
  259. ;; hyperlinking it.
  260. ;; This may have some scope for extension and the same or something
  261. ;; similar should be done for widget doc strings, which currently use
  262. ;; another mechanism.
  263. (defvar help-back-label (purecopy "[back]")
  264. "Label to use by `help-make-xrefs' for the go-back reference.")
  265. (defvar help-forward-label (purecopy "[forward]")
  266. "Label to use by `help-make-xrefs' for the go-forward reference.")
  267. (defconst help-xref-symbol-regexp
  268. (purecopy (concat "\\(\\<\\(\\(variable\\|option\\)\\|" ; Link to var
  269. "\\(function\\|command\\|call\\)\\|" ; Link to function
  270. "\\(face\\)\\|" ; Link to face
  271. "\\(symbol\\|program\\|property\\)\\|" ; Don't link
  272. "\\(source \\(?:code \\)?\\(?:of\\|for\\)\\)\\)"
  273. "[ \t\n]+\\)?"
  274. ;; Note starting with word-syntax character:
  275. "['`‘]\\(\\sw\\(\\sw\\|\\s_\\)+\\|`\\)['’]"))
  276. "Regexp matching doc string references to symbols.
  277. The words preceding the quoted symbol can be used in doc strings to
  278. distinguish references to variables, functions and symbols.")
  279. (defvar help-xref-mule-regexp nil
  280. "Regexp matching doc string references to MULE-related keywords.
  281. It is usually nil, and is temporarily bound to an appropriate regexp
  282. when help commands related to multilingual environment (e.g.,
  283. `describe-coding-system') are invoked.")
  284. (defconst help-xref-info-regexp
  285. (purecopy
  286. "\\<[Ii]nfo[ \t\n]+\\(node\\|anchor\\)[ \t\n]+['`‘]\\([^'’]+\\)['’]")
  287. "Regexp matching doc string references to an Info node.")
  288. (defconst help-xref-url-regexp
  289. (purecopy "\\<[Uu][Rr][Ll][ \t\n]+['`‘]\\([^'’]+\\)['’]")
  290. "Regexp matching doc string references to a URL.")
  291. ;;;###autoload
  292. (defun help-setup-xref (item interactive-p)
  293. "Invoked from commands using the \"*Help*\" buffer to install some xref info.
  294. ITEM is a (FUNCTION . ARGS) pair appropriate for recreating the help
  295. buffer after following a reference. INTERACTIVE-P is non-nil if the
  296. calling command was invoked interactively. In this case the stack of
  297. items for help buffer \"back\" buttons is cleared.
  298. This should be called very early, before the output buffer is cleared,
  299. because we want to record the \"previous\" position of point so we can
  300. restore it properly when going back."
  301. (with-current-buffer (help-buffer)
  302. (when help-xref-stack-item
  303. (push (cons (point) help-xref-stack-item) help-xref-stack)
  304. (setq help-xref-forward-stack nil))
  305. (when interactive-p
  306. (let ((tail (nthcdr 10 help-xref-stack)))
  307. ;; Truncate the stack.
  308. (if tail (setcdr tail nil))))
  309. (setq help-xref-stack-item item)))
  310. (defvar help-xref-following nil
  311. "Non-nil when following a help cross-reference.")
  312. ;;;###autoload
  313. (defun help-buffer ()
  314. "Return the name of a buffer for inserting help.
  315. If `help-xref-following' is non-nil, this is the name of the
  316. current buffer. Signal an error if this buffer is not derived
  317. from `help-mode'.
  318. Otherwise, return \"*Help*\", creating a buffer with that name if
  319. it does not already exist."
  320. (buffer-name ;for with-output-to-temp-buffer
  321. (if (not help-xref-following)
  322. (get-buffer-create "*Help*")
  323. (unless (derived-mode-p 'help-mode)
  324. (error "Current buffer is not in Help mode"))
  325. (current-buffer))))
  326. (defvar describe-symbol-backends
  327. `((nil ,#'fboundp ,(lambda (s _b _f) (describe-function s)))
  328. ("face" ,#'facep ,(lambda (s _b _f) (describe-face s)))
  329. (nil
  330. ,(lambda (symbol)
  331. (or (and (boundp symbol) (not (keywordp symbol)))
  332. (get symbol 'variable-documentation)))
  333. ,#'describe-variable)))
  334. ;;;###autoload
  335. (defun help-make-xrefs (&optional buffer)
  336. "Parse and hyperlink documentation cross-references in the given BUFFER.
  337. Find cross-reference information in a buffer and activate such cross
  338. references for selection with `help-follow'. Cross-references have
  339. the canonical form `...' and the type of reference may be
  340. disambiguated by the preceding word(s) used in
  341. `help-xref-symbol-regexp'. Faces only get cross-referenced if
  342. preceded or followed by the word `face'. Variables without
  343. variable documentation do not get cross-referenced, unless
  344. preceded by the word `variable' or `option'.
  345. If the variable `help-xref-mule-regexp' is non-nil, find also
  346. cross-reference information related to multilingual environment
  347. \(e.g., coding-systems). This variable is also used to disambiguate
  348. the type of reference as the same way as `help-xref-symbol-regexp'.
  349. A special reference `back' is made to return back through a stack of
  350. help buffers. Variable `help-back-label' specifies the text for
  351. that."
  352. (interactive "b")
  353. (with-current-buffer (or buffer (current-buffer))
  354. (save-excursion
  355. (goto-char (point-min))
  356. ;; Skip the header-type info, though it might be useful to parse
  357. ;; it at some stage (e.g. "function in `library'").
  358. (forward-paragraph)
  359. (let ((old-modified (buffer-modified-p)))
  360. (let ((stab (syntax-table))
  361. (case-fold-search t)
  362. (inhibit-read-only t))
  363. (set-syntax-table emacs-lisp-mode-syntax-table)
  364. ;; The following should probably be abstracted out.
  365. (unwind-protect
  366. (progn
  367. ;; Info references
  368. (save-excursion
  369. (while (re-search-forward help-xref-info-regexp nil t)
  370. (let ((data (match-string 2)))
  371. (save-match-data
  372. (unless (string-match "^([^)]+)" data)
  373. (setq data (concat "(emacs)" data)))
  374. (setq data ;; possible newlines if para filled
  375. (replace-regexp-in-string "[ \t\n]+" " " data t t)))
  376. (help-xref-button 2 'help-info data))))
  377. ;; URLs
  378. (save-excursion
  379. (while (re-search-forward help-xref-url-regexp nil t)
  380. (let ((data (match-string 1)))
  381. (help-xref-button 1 'help-url data))))
  382. ;; Mule related keywords. Do this before trying
  383. ;; `help-xref-symbol-regexp' because some of Mule
  384. ;; keywords have variable or function definitions.
  385. (if help-xref-mule-regexp
  386. (save-excursion
  387. (while (re-search-forward help-xref-mule-regexp nil t)
  388. (let* ((data (match-string 7))
  389. (sym (intern-soft data)))
  390. (cond
  391. ((match-string 3) ; coding system
  392. (and sym (coding-system-p sym)
  393. (help-xref-button 6 'help-coding-system sym)))
  394. ((match-string 4) ; input method
  395. (and (assoc data input-method-alist)
  396. (help-xref-button 7 'help-input-method data)))
  397. ((or (match-string 5) (match-string 6)) ; charset
  398. (and sym (charsetp sym)
  399. (help-xref-button 7 'help-character-set sym)))
  400. ((assoc data input-method-alist)
  401. (help-xref-button 7 'help-character-set data))
  402. ((and sym (coding-system-p sym))
  403. (help-xref-button 7 'help-coding-system sym))
  404. ((and sym (charsetp sym))
  405. (help-xref-button 7 'help-character-set sym)))))))
  406. ;; Quoted symbols
  407. (save-excursion
  408. (while (re-search-forward help-xref-symbol-regexp nil t)
  409. (let* ((data (match-string 8))
  410. (sym (intern-soft data)))
  411. (if sym
  412. (cond
  413. ((match-string 3) ; `variable' &c
  414. (and (or (boundp sym) ; `variable' doesn't ensure
  415. ; it's actually bound
  416. (get sym 'variable-documentation))
  417. (help-xref-button 8 'help-variable sym)))
  418. ((match-string 4) ; `function' &c
  419. (and (fboundp sym) ; similarly
  420. (help-xref-button 8 'help-function sym)))
  421. ((match-string 5) ; `face'
  422. (and (facep sym)
  423. (help-xref-button 8 'help-face sym)))
  424. ((match-string 6)) ; nothing for `symbol'
  425. ((match-string 7)
  426. ;; this used:
  427. ;; #'(lambda (arg)
  428. ;; (let ((location
  429. ;; (find-function-noselect arg)))
  430. ;; (pop-to-buffer (car location))
  431. ;; (goto-char (cdr location))))
  432. (help-xref-button 8 'help-function-def sym))
  433. ((cl-some (lambda (x) (funcall (nth 1 x) sym))
  434. describe-symbol-backends)
  435. (help-xref-button 8 'help-symbol sym)))))))
  436. ;; An obvious case of a key substitution:
  437. (save-excursion
  438. (while (re-search-forward
  439. ;; Assume command name is only word and symbol
  440. ;; characters to get things like `use M-x foo->bar'.
  441. ;; Command required to end with word constituent
  442. ;; to avoid `.' at end of a sentence.
  443. "\\<M-x\\s-+\\(\\sw\\(\\sw\\|\\s_\\)*\\sw\\)" nil t)
  444. (let ((sym (intern-soft (match-string 1))))
  445. (if (fboundp sym)
  446. (help-xref-button 1 'help-function sym)))))
  447. ;; Look for commands in whole keymap substitutions:
  448. (save-excursion
  449. ;; Make sure to find the first keymap.
  450. (goto-char (point-min))
  451. ;; Find a header and the column at which the command
  452. ;; name will be found.
  453. ;; If the keymap substitution isn't the last thing in
  454. ;; the doc string, and if there is anything on the same
  455. ;; line after it, this code won't recognize the end of it.
  456. (while (re-search-forward "^key +binding\n\\(-+ +\\)-+\n\n"
  457. nil t)
  458. (let ((col (- (match-end 1) (match-beginning 1))))
  459. (while
  460. (and (not (eobp))
  461. ;; Stop at a pair of blank lines.
  462. (not (looking-at-p "\n\\s-*\n")))
  463. ;; Skip a single blank line.
  464. (and (eolp) (forward-line))
  465. (end-of-line)
  466. (skip-chars-backward "^ \t\n")
  467. (if (and (>= (current-column) col)
  468. (looking-at "\\(\\sw\\|\\s_\\)+$"))
  469. (let ((sym (intern-soft (match-string 0))))
  470. (if (fboundp sym)
  471. (help-xref-button 0 'help-function sym))))
  472. (forward-line))))))
  473. (set-syntax-table stab))
  474. ;; Delete extraneous newlines at the end of the docstring
  475. (goto-char (point-max))
  476. (while (and (not (bobp)) (bolp))
  477. (delete-char -1))
  478. (insert "\n")
  479. (when (or help-xref-stack help-xref-forward-stack)
  480. (insert "\n"))
  481. ;; Make a back-reference in this buffer if appropriate.
  482. (when help-xref-stack
  483. (help-insert-xref-button help-back-label 'help-back
  484. (current-buffer)))
  485. ;; Make a forward-reference in this buffer if appropriate.
  486. (when help-xref-forward-stack
  487. (when help-xref-stack
  488. (insert "\t"))
  489. (help-insert-xref-button help-forward-label 'help-forward
  490. (current-buffer)))
  491. (when (or help-xref-stack help-xref-forward-stack)
  492. (insert "\n")))
  493. (set-buffer-modified-p old-modified)))))
  494. ;;;###autoload
  495. (defun help-xref-button (match-number type &rest args)
  496. "Make a hyperlink for cross-reference text previously matched.
  497. MATCH-NUMBER is the subexpression of interest in the last matched
  498. regexp. TYPE is the type of button to use. Any remaining arguments are
  499. passed to the button's help-function when it is invoked.
  500. See `help-make-xrefs'."
  501. ;; Don't mung properties we've added specially in some instances.
  502. (unless (button-at (match-beginning match-number))
  503. (make-text-button (match-beginning match-number)
  504. (match-end match-number)
  505. 'type type 'help-args args)))
  506. ;;;###autoload
  507. (defun help-insert-xref-button (string type &rest args)
  508. "Insert STRING and make a hyperlink from cross-reference text on it.
  509. TYPE is the type of button to use. Any remaining arguments are passed
  510. to the button's help-function when it is invoked.
  511. See `help-make-xrefs'."
  512. (unless (button-at (point))
  513. (insert-text-button string 'type type 'help-args args)))
  514. ;;;###autoload
  515. (defun help-xref-on-pp (from to)
  516. "Add xrefs for symbols in `pp's output between FROM and TO."
  517. (if (> (- to from) 5000) nil
  518. (with-syntax-table emacs-lisp-mode-syntax-table
  519. (save-excursion
  520. (save-restriction
  521. (narrow-to-region from to)
  522. (goto-char (point-min))
  523. (ignore-errors
  524. (while (not (eobp))
  525. (cond
  526. ((looking-at-p "\"") (forward-sexp 1))
  527. ((looking-at-p "#<") (search-forward ">" nil 'move))
  528. ((looking-at "\\(\\(\\sw\\|\\s_\\)+\\)")
  529. (let* ((sym (intern-soft (match-string 1)))
  530. (type (cond ((fboundp sym) 'help-function)
  531. ((or (memq sym '(t nil))
  532. (keywordp sym))
  533. nil)
  534. ((and sym
  535. (or (boundp sym)
  536. (get sym
  537. 'variable-documentation)))
  538. 'help-variable))))
  539. (when type (help-xref-button 1 type sym)))
  540. (goto-char (match-end 1)))
  541. (t (forward-char 1))))))))))
  542. ;; Additional functions for (re-)creating types of help buffers.
  543. ;;;###autoload
  544. (define-obsolete-function-alias 'help-xref-interned 'describe-symbol "25.1")
  545. ;; Navigation/hyperlinking with xrefs
  546. (defun help-xref-go-back (buffer)
  547. "From BUFFER, go back to previous help buffer text using `help-xref-stack'."
  548. (let (item position method args)
  549. (with-current-buffer buffer
  550. (push (cons (point) help-xref-stack-item) help-xref-forward-stack)
  551. (when help-xref-stack
  552. (setq item (pop help-xref-stack)
  553. ;; Clear the current item so that it won't get pushed
  554. ;; by the function we're about to call. TODO: We could also
  555. ;; push it onto a "forward" stack and add a `forw' button.
  556. help-xref-stack-item nil
  557. position (car item)
  558. method (cadr item)
  559. args (cddr item))))
  560. (apply method args)
  561. (with-current-buffer buffer
  562. (if (get-buffer-window buffer)
  563. (set-window-point (get-buffer-window buffer) position)
  564. (goto-char position)))))
  565. (defun help-xref-go-forward (buffer)
  566. "From BUFFER, go forward to next help buffer."
  567. (let (item position method args)
  568. (with-current-buffer buffer
  569. (push (cons (point) help-xref-stack-item) help-xref-stack)
  570. (when help-xref-forward-stack
  571. (setq item (pop help-xref-forward-stack)
  572. ;; Clear the current item so that it won't get pushed
  573. ;; by the function we're about to call. TODO: We could also
  574. ;; push it onto a "forward" stack and add a `forw' button.
  575. help-xref-stack-item nil
  576. position (car item)
  577. method (cadr item)
  578. args (cddr item))))
  579. (apply method args)
  580. (with-current-buffer buffer
  581. (if (get-buffer-window buffer)
  582. (set-window-point (get-buffer-window buffer) position)
  583. (goto-char position)))))
  584. (defun help-go-back ()
  585. "Go back to previous topic in this help buffer."
  586. (interactive)
  587. (if help-xref-stack
  588. (help-xref-go-back (current-buffer))
  589. (user-error "No previous help buffer")))
  590. (defun help-go-forward ()
  591. "Go to the next topic in this help buffer."
  592. (interactive)
  593. (if help-xref-forward-stack
  594. (help-xref-go-forward (current-buffer))
  595. (user-error "No next help buffer")))
  596. (defun help-do-xref (_pos function args)
  597. "Call the help cross-reference function FUNCTION with args ARGS.
  598. Things are set up properly so that the resulting help-buffer has
  599. a proper [back] button."
  600. ;; There is a reference at point. Follow it.
  601. (let ((help-xref-following t))
  602. (apply function (if (eq function 'info)
  603. (append args (list (generate-new-buffer-name "*info*"))) args))))
  604. ;; The doc string is meant to explain what buttons do.
  605. (defun help-follow-mouse ()
  606. "Follow the cross-reference that you click on."
  607. (interactive)
  608. (error "No cross-reference here"))
  609. ;; The doc string is meant to explain what buttons do.
  610. (defun help-follow ()
  611. "Follow cross-reference at point.
  612. For the cross-reference format, see `help-make-xrefs'."
  613. (interactive)
  614. (user-error "No cross-reference here"))
  615. (defun help-follow-symbol (&optional pos)
  616. "In help buffer, show docs for symbol at POS, defaulting to point.
  617. Show all docs for that symbol as either a variable, function or face."
  618. (interactive "d")
  619. (unless pos
  620. (setq pos (point)))
  621. ;; check if the symbol under point is a function, variable or face
  622. (let ((sym
  623. (intern
  624. (save-excursion
  625. (goto-char pos) (skip-syntax-backward "w_")
  626. (buffer-substring (point)
  627. (progn (skip-syntax-forward "w_")
  628. (point)))))))
  629. (when (or (boundp sym)
  630. (get sym 'variable-documentation)
  631. (fboundp sym) (facep sym))
  632. (help-do-xref pos #'describe-symbol (list sym)))))
  633. (defun help-mode-revert-buffer (_ignore-auto noconfirm)
  634. (when (or noconfirm (yes-or-no-p "Revert help buffer? "))
  635. (let ((pos (point))
  636. (item help-xref-stack-item)
  637. ;; Pretend there is no current item to add to the history.
  638. (help-xref-stack-item nil)
  639. ;; Use the current buffer.
  640. (help-xref-following t))
  641. (apply (car item) (cdr item))
  642. (goto-char pos))))
  643. (defun help-insert-string (string)
  644. "Insert STRING to the help buffer and install xref info for it.
  645. This function can be used to restore the old contents of the help buffer
  646. when going back to the previous topic in the xref stack. It is needed
  647. in case when it is impossible to recompute the old contents of the
  648. help buffer by other means."
  649. (setq help-xref-stack-item (list #'help-insert-string string))
  650. (with-output-to-temp-buffer (help-buffer)
  651. (insert string)))
  652. ;; Bookmark support
  653. (declare-function bookmark-prop-get "bookmark" (bookmark prop))
  654. (declare-function bookmark-make-record-default "bookmark"
  655. (&optional no-file no-context posn))
  656. (defun help-bookmark-make-record ()
  657. "Create and return a help-mode bookmark record.
  658. Implements `bookmark-make-record-function' for help-mode buffers."
  659. (unless (car help-xref-stack-item)
  660. (error "Cannot create bookmark - help command not known"))
  661. `(,@(bookmark-make-record-default 'NO-FILE 'NO-CONTEXT)
  662. (help-fn . ,(car help-xref-stack-item))
  663. (help-args . ,(cdr help-xref-stack-item))
  664. (position . ,(point))
  665. (handler . help-bookmark-jump)))
  666. ;;;###autoload
  667. (defun help-bookmark-jump (bookmark)
  668. "Jump to help-mode bookmark BOOKMARK.
  669. Handler function for record returned by `help-bookmark-make-record'.
  670. BOOKMARK is a bookmark name or a bookmark record."
  671. (let ((help-fn (bookmark-prop-get bookmark 'help-fn))
  672. (help-args (bookmark-prop-get bookmark 'help-args))
  673. (position (bookmark-prop-get bookmark 'position)))
  674. (apply help-fn help-args)
  675. (pop-to-buffer "*Help*")
  676. (goto-char position)))
  677. (provide 'help-mode)
  678. ;;; help-mode.el ends here