help-mode.el 31 KB

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