help-fns.el 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  1. ;;; help-fns.el --- Complex help functions -*- lexical-binding: t -*-
  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. ;; This file contains those help commands which are complicated, and
  20. ;; which may not be used in every session. For example
  21. ;; `describe-function' will probably be heavily used when doing elisp
  22. ;; programming, but not if just editing C files. Simpler help commands
  23. ;; are in help.el
  24. ;;; Code:
  25. (require 'cl-lib)
  26. (require 'help-mode)
  27. (require 'radix-tree)
  28. (defvar help-fns-describe-function-functions nil
  29. "List of functions to run in help buffer in `describe-function'.
  30. Those functions will be run after the header line and argument
  31. list was inserted, and before the documentation will be inserted.
  32. The functions will receive the function name as argument.")
  33. ;; Functions
  34. (defvar help-definition-prefixes nil
  35. ;; FIXME: We keep `definition-prefixes' as a hash-table so as to
  36. ;; avoid pre-loading radix-tree and because it takes slightly less
  37. ;; memory. But when we use this table it's more efficient to
  38. ;; represent it as a radix tree, since the main operation is to do
  39. ;; `radix-tree-prefixes'. Maybe we should just bite the bullet and
  40. ;; use a radix tree for `definition-prefixes' (it's not *that*
  41. ;; costly, really).
  42. "Radix-tree representation replacing `definition-prefixes'.")
  43. (defun help-definition-prefixes ()
  44. "Return the up-to-date radix-tree form of `definition-prefixes'."
  45. (when (> (hash-table-count definition-prefixes) 0)
  46. (maphash (lambda (prefix files)
  47. (let ((old (radix-tree-lookup help-definition-prefixes prefix)))
  48. (setq help-definition-prefixes
  49. (radix-tree-insert help-definition-prefixes
  50. prefix (append old files)))))
  51. definition-prefixes)
  52. (clrhash definition-prefixes))
  53. help-definition-prefixes)
  54. (defun help--loaded-p (file)
  55. "Try and figure out if FILE has already been loaded."
  56. (or (let ((feature (intern-soft file)))
  57. (and feature (featurep feature)))
  58. (let* ((re (load-history-regexp file))
  59. (done nil))
  60. (dolist (x load-history)
  61. (and (car x) (string-match-p re (car x)) (setq done t)))
  62. done)))
  63. (defun help--load-prefixes (prefixes)
  64. (pcase-dolist (`(,prefix . ,files) prefixes)
  65. (setq help-definition-prefixes
  66. (radix-tree-insert help-definition-prefixes prefix nil))
  67. (dolist (file files)
  68. ;; FIXME: Should we scan help-definition-prefixes to remove
  69. ;; other prefixes of the same file?
  70. ;; FIXME: this regexp business is not good enough: for file
  71. ;; `toto', it will say `toto' is loaded when in reality it was
  72. ;; just cedet/semantic/toto that has been loaded.
  73. (unless (help--loaded-p file)
  74. (load file 'noerror 'nomessage)))))
  75. (defun help--symbol-completion-table (string pred action)
  76. (let ((prefixes (radix-tree-prefixes (help-definition-prefixes) string)))
  77. (help--load-prefixes prefixes))
  78. (let ((prefix-completions
  79. (mapcar #'intern (all-completions string definition-prefixes))))
  80. (complete-with-action action obarray string
  81. (if pred (lambda (sym)
  82. (or (funcall pred sym)
  83. (memq sym prefix-completions)))))))
  84. (defvar describe-function-orig-buffer nil
  85. "Buffer that was current when `describe-function' was invoked.
  86. Functions on `help-fns-describe-function-functions' can use this
  87. to get buffer-local values.")
  88. ;;;###autoload
  89. (defun describe-function (function)
  90. "Display the full documentation of FUNCTION (a symbol).
  91. When called from lisp, FUNCTION may also be a function object."
  92. (interactive
  93. (let* ((fn (function-called-at-point))
  94. (enable-recursive-minibuffers t)
  95. (val (completing-read
  96. (if fn
  97. (format "Describe function (default %s): " fn)
  98. "Describe function: ")
  99. #'help--symbol-completion-table
  100. (lambda (f) (or (fboundp f) (get f 'function-documentation)))
  101. t nil nil
  102. (and fn (symbol-name fn)))))
  103. (unless (equal val "")
  104. (setq fn (intern val)))
  105. (unless (and fn (symbolp fn))
  106. (user-error "You didn't specify a function symbol"))
  107. (unless (or (fboundp fn) (get fn 'function-documentation))
  108. (user-error "Symbol's function definition is void: %s" fn))
  109. (list fn)))
  110. ;; We save describe-function-orig-buffer on the help xref stack, so
  111. ;; it is restored by the back/forward buttons. 'help-buffer'
  112. ;; expects (current-buffer) to be a help buffer when processing
  113. ;; those buttons, so we can't change the current buffer before
  114. ;; calling that.
  115. (let ((describe-function-orig-buffer
  116. (or describe-function-orig-buffer
  117. (current-buffer))))
  118. (help-setup-xref
  119. (list (lambda (function buffer)
  120. (let ((describe-function-orig-buffer
  121. (if (buffer-live-p buffer) buffer)))
  122. (describe-function function)))
  123. function describe-function-orig-buffer)
  124. (called-interactively-p 'interactive))
  125. (save-excursion
  126. (with-help-window (help-buffer)
  127. (if (get function 'reader-construct)
  128. (princ function)
  129. (prin1 function))
  130. ;; Use " is " instead of a colon so that
  131. ;; it is easier to get out the function name using forward-sexp.
  132. (princ " is ")
  133. (describe-function-1 function)
  134. (with-current-buffer standard-output
  135. ;; Return the text we displayed.
  136. (buffer-string))))
  137. ))
  138. ;; Could be this, if we make symbol-file do the work below.
  139. ;; (defun help-C-file-name (subr-or-var kind)
  140. ;; "Return the name of the C file where SUBR-OR-VAR is defined.
  141. ;; KIND should be `var' for a variable or `subr' for a subroutine."
  142. ;; (symbol-file (if (symbolp subr-or-var) subr-or-var
  143. ;; (subr-name subr-or-var))
  144. ;; (if (eq kind 'var) 'defvar 'defun)))
  145. ;;;###autoload
  146. (defun help-C-file-name (subr-or-var kind)
  147. "Return the name of the C file where SUBR-OR-VAR is defined.
  148. KIND should be `var' for a variable or `subr' for a subroutine."
  149. (let ((docbuf (get-buffer-create " *DOC*"))
  150. (name (if (eq 'var kind)
  151. (concat "V" (symbol-name subr-or-var))
  152. (concat "F" (subr-name (advice--cd*r subr-or-var))))))
  153. (with-current-buffer docbuf
  154. (goto-char (point-min))
  155. (if (eobp)
  156. (insert-file-contents-literally
  157. (expand-file-name internal-doc-file-name doc-directory)))
  158. (let ((file (catch 'loop
  159. (while t
  160. (let ((pnt (search-forward (concat "" name "\n"))))
  161. (re-search-backward "S\\(.*\\)")
  162. (let ((file (match-string 1)))
  163. (if (member file build-files)
  164. (throw 'loop file)
  165. (goto-char pnt))))))))
  166. (if (string-match "^ns.*\\(\\.o\\|obj\\)\\'" file)
  167. (setq file (replace-match ".m" t t file 1))
  168. (if (string-match "\\.\\(o\\|obj\\)\\'" file)
  169. (setq file (replace-match ".c" t t file))))
  170. (if (string-match "\\.\\(c\\|m\\)\\'" file)
  171. (concat "src/" file)
  172. file)))))
  173. (defcustom help-downcase-arguments nil
  174. "If non-nil, argument names in *Help* buffers are downcased."
  175. :type 'boolean
  176. :group 'help
  177. :version "23.2")
  178. (defun help-highlight-arg (arg)
  179. "Highlight ARG as an argument name for a *Help* buffer.
  180. Return ARG in face `help-argument-name'; ARG is also downcased
  181. if the variable `help-downcase-arguments' is non-nil."
  182. (propertize (if help-downcase-arguments (downcase arg) arg)
  183. 'face 'help-argument-name))
  184. (defun help-do-arg-highlight (doc args)
  185. (with-syntax-table (make-syntax-table emacs-lisp-mode-syntax-table)
  186. (modify-syntax-entry ?\- "w")
  187. (dolist (arg args)
  188. (setq doc (replace-regexp-in-string
  189. ;; This is heuristic, but covers all common cases
  190. ;; except ARG1-ARG2
  191. (concat "\\<" ; beginning of word
  192. "\\(?:[a-z-]*-\\)?" ; for xxx-ARG
  193. "\\("
  194. (regexp-quote arg)
  195. "\\)"
  196. "\\(?:es\\|s\\|th\\)?" ; for ARGth, ARGs
  197. "\\(?:-[a-z0-9-]+\\)?" ; for ARG-xxx, ARG-n
  198. "\\(?:-[{([<`\"‘].*?\\)?"; for ARG-{x}, (x), <x>, [x], `x', ‘x’
  199. "\\>") ; end of word
  200. (help-highlight-arg arg)
  201. doc t t 1)))
  202. doc))
  203. (defun help-highlight-arguments (usage doc &rest args)
  204. (when (and usage (string-match "^(" usage))
  205. (with-temp-buffer
  206. (insert usage)
  207. (goto-char (point-min))
  208. (let ((case-fold-search nil)
  209. (next (not (or args (looking-at "\\["))))
  210. (opt nil))
  211. ;; Make a list of all arguments
  212. (skip-chars-forward "^ ")
  213. (while next
  214. (or opt (not (looking-at " &")) (setq opt t))
  215. (if (not (re-search-forward " \\([\\[(]*\\)\\([^] &).]+\\)" nil t))
  216. (setq next nil)
  217. (setq args (cons (match-string 2) args))
  218. (when (and opt (string= (match-string 1) "("))
  219. ;; A pesky CL-style optional argument with default value,
  220. ;; so let's skip over it
  221. (search-backward "(")
  222. (goto-char (scan-sexps (point) 1)))))
  223. ;; Highlight arguments in the USAGE string
  224. (setq usage (help-do-arg-highlight (buffer-string) args))
  225. ;; Highlight arguments in the DOC string
  226. (setq doc (and doc (help-do-arg-highlight doc args))))))
  227. ;; Return value is like the one from help-split-fundoc, but highlighted
  228. (cons usage doc))
  229. ;; The following function was compiled from the former functions
  230. ;; `describe-simplify-lib-file-name' and `find-source-lisp-file' with
  231. ;; some excerpts from `describe-function-1' and `describe-variable'.
  232. ;; The only additional twists provided are (1) locate the defining file
  233. ;; for autoloaded functions, and (2) give preference to files in the
  234. ;; "install directory" (directories found via `load-path') rather than
  235. ;; to files in the "compile directory" (directories found by searching
  236. ;; the loaddefs.el file). We autoload it because it's also used by
  237. ;; `describe-face' (instead of `describe-simplify-lib-file-name').
  238. ;;;###autoload
  239. (defun find-lisp-object-file-name (object type)
  240. "Guess the file that defined the Lisp object OBJECT, of type TYPE.
  241. OBJECT should be a symbol associated with a function, variable, or face;
  242. alternatively, it can be a function definition.
  243. If TYPE is `defvar', search for a variable definition.
  244. If TYPE is `defface', search for a face definition.
  245. If TYPE is not a symbol, search for a function definition.
  246. The return value is the absolute name of a readable file where OBJECT is
  247. defined. If several such files exist, preference is given to a file
  248. found via `load-path'. The return value can also be `C-source', which
  249. means that OBJECT is a function or variable defined in C. If no
  250. suitable file is found, return nil."
  251. (let* ((autoloaded (autoloadp type))
  252. (file-name (or (and autoloaded (nth 1 type))
  253. (symbol-file
  254. ;; FIXME: Why do we have this weird "If TYPE is the
  255. ;; value returned by `symbol-function' for a function
  256. ;; symbol" exception?
  257. object (or (if (symbolp type) type) 'defun)))))
  258. (cond
  259. (autoloaded
  260. ;; An autoloaded function: Locate the file since `symbol-function'
  261. ;; has only returned a bare string here.
  262. (setq file-name
  263. (locate-file file-name load-path '(".el" ".elc") 'readable)))
  264. ((and (stringp file-name)
  265. (string-match "[.]*loaddefs.el\\'" file-name))
  266. ;; An autoloaded variable or face. Visit loaddefs.el in a buffer
  267. ;; and try to extract the defining file. The following form is
  268. ;; from `describe-function-1' and `describe-variable'.
  269. (let ((location
  270. (condition-case nil
  271. (find-function-search-for-symbol object nil file-name)
  272. (error nil))))
  273. (when (cdr location)
  274. (with-current-buffer (car location)
  275. (goto-char (cdr location))
  276. (when (re-search-backward
  277. "^;;; Generated autoloads from \\(.*\\)" nil t)
  278. (setq file-name
  279. (locate-file
  280. (file-name-sans-extension
  281. (match-string-no-properties 1))
  282. load-path '(".el" ".elc") 'readable))))))))
  283. (cond
  284. ((and (not file-name) (subrp type))
  285. ;; A built-in function. The form is from `describe-function-1'.
  286. (if (get-buffer " *DOC*")
  287. (help-C-file-name type 'subr)
  288. 'C-source))
  289. ((and (not file-name) (symbolp object)
  290. (integerp (get object 'variable-documentation)))
  291. ;; A variable defined in C. The form is from `describe-variable'.
  292. (if (get-buffer " *DOC*")
  293. (help-C-file-name object 'var)
  294. 'C-source))
  295. ((not (stringp file-name))
  296. ;; If we don't have a file-name string by now, we lost.
  297. nil)
  298. ;; Now, `file-name' should have become an absolute file name.
  299. ;; For files loaded from ~/.foo.elc, try ~/.foo.
  300. ;; This applies to config files like ~/.emacs,
  301. ;; which people sometimes compile.
  302. ((let (fn)
  303. (and (string-match "\\`\\..*\\.elc\\'"
  304. (file-name-nondirectory file-name))
  305. (string-equal (file-name-directory file-name)
  306. (file-name-as-directory (expand-file-name "~")))
  307. (file-readable-p (setq fn (file-name-sans-extension file-name)))
  308. fn)))
  309. ;; When the Elisp source file can be found in the install
  310. ;; directory, return the name of that file.
  311. ((let ((lib-name
  312. (if (string-match "[.]elc\\'" file-name)
  313. (substring-no-properties file-name 0 -1)
  314. file-name)))
  315. (or (and (file-readable-p lib-name) lib-name)
  316. ;; The library might be compressed.
  317. (and (file-readable-p (concat lib-name ".gz")) lib-name))))
  318. ((let* ((lib-name (file-name-nondirectory file-name))
  319. ;; The next form is from `describe-simplify-lib-file-name'.
  320. (file-name
  321. ;; Try converting the absolute file name to a library
  322. ;; name, convert that back to a file name and see if we
  323. ;; get the original one. If so, they are equivalent.
  324. (if (equal file-name (locate-file lib-name load-path '("")))
  325. (if (string-match "[.]elc\\'" lib-name)
  326. (substring-no-properties lib-name 0 -1)
  327. lib-name)
  328. file-name))
  329. (src-file (locate-library file-name t nil 'readable)))
  330. (and src-file (file-readable-p src-file) src-file))))))
  331. (defun help-fns--key-bindings (function)
  332. (when (commandp function)
  333. (let ((pt2 (with-current-buffer standard-output (point)))
  334. (remapped (command-remapping function)))
  335. (unless (memq remapped '(ignore undefined))
  336. (let ((keys (where-is-internal
  337. (or remapped function) overriding-local-map nil nil))
  338. non-modified-keys)
  339. (if (and (eq function 'self-insert-command)
  340. (vectorp (car-safe keys))
  341. (consp (aref (car keys) 0)))
  342. (princ "It is bound to many ordinary text characters.\n")
  343. ;; Which non-control non-meta keys run this command?
  344. (dolist (key keys)
  345. (if (member (event-modifiers (aref key 0)) '(nil (shift)))
  346. (push key non-modified-keys)))
  347. (when remapped
  348. (princ "Its keys are remapped to ")
  349. (princ (if (symbolp remapped)
  350. (format-message "`%s'" remapped)
  351. "an anonymous command"))
  352. (princ ".\n"))
  353. (when keys
  354. (princ (if remapped
  355. "Without this remapping, it would be bound to "
  356. "It is bound to "))
  357. ;; If lots of ordinary text characters run this command,
  358. ;; don't mention them one by one.
  359. (if (< (length non-modified-keys) 10)
  360. (princ (mapconcat #'key-description keys ", "))
  361. (dolist (key non-modified-keys)
  362. (setq keys (delq key keys)))
  363. (if keys
  364. (progn
  365. (princ (mapconcat #'key-description keys ", "))
  366. (princ ", and many ordinary text characters"))
  367. (princ "many ordinary text characters"))))
  368. (when (or remapped keys non-modified-keys)
  369. (princ ".")
  370. (terpri)))))
  371. (with-current-buffer standard-output
  372. (fill-region-as-paragraph pt2 (point))
  373. (unless (looking-back "\n\n" (- (point) 2))
  374. (terpri))))))
  375. (defun help-fns--compiler-macro (function)
  376. (let ((handler (function-get function 'compiler-macro)))
  377. (when handler
  378. (insert "\nThis function has a compiler macro")
  379. (if (symbolp handler)
  380. (progn
  381. (insert (format-message " `%s'" handler))
  382. (save-excursion
  383. (re-search-backward (substitute-command-keys "`\\([^`']+\\)'")
  384. nil t)
  385. (help-xref-button 1 'help-function handler)))
  386. ;; FIXME: Obsolete since 24.4.
  387. (let ((lib (get function 'compiler-macro-file)))
  388. (when (stringp lib)
  389. (insert (format-message " in `%s'" lib))
  390. (save-excursion
  391. (re-search-backward (substitute-command-keys "`\\([^`']+\\)'")
  392. nil t)
  393. (help-xref-button 1 'help-function-cmacro function lib)))))
  394. (insert ".\n"))))
  395. (defun help-fns--signature (function doc real-def real-function buffer)
  396. "Insert usage at point and return docstring. With highlighting."
  397. (if (keymapp function)
  398. doc ; If definition is a keymap, skip arglist note.
  399. (let* ((advertised (gethash real-def advertised-signature-table t))
  400. (arglist (if (listp advertised)
  401. advertised (help-function-arglist real-def)))
  402. (usage (help-split-fundoc doc function)))
  403. (if usage (setq doc (cdr usage)))
  404. (let* ((use (cond
  405. ((and usage (not (listp advertised))) (car usage))
  406. ((listp arglist)
  407. (help--make-usage-docstring function arglist))
  408. ((stringp arglist) arglist)
  409. ;; Maybe the arglist is in the docstring of a symbol
  410. ;; this one is aliased to.
  411. ((let ((fun real-function))
  412. (while (and (symbolp fun)
  413. (setq fun (symbol-function fun))
  414. (not (setq usage (help-split-fundoc
  415. (documentation fun)
  416. function)))))
  417. usage)
  418. (car usage))
  419. ((or (stringp real-def)
  420. (vectorp real-def))
  421. (format "\nMacro: %s"
  422. (help--docstring-quote
  423. (format-kbd-macro real-def))))
  424. (t "[Missing arglist. Please make a bug report.]")))
  425. ;; Insert "`X", not "(\` X)", when documenting `X.
  426. (use1 (replace-regexp-in-string
  427. "\\`(\\\\=\\\\\\\\=` \\([^\n ]*\\))\\'"
  428. "\\\\=`\\1" use t))
  429. (high (if buffer
  430. (let (subst-use1 subst-doc)
  431. (with-current-buffer buffer
  432. (setq subst-use1 (substitute-command-keys use1))
  433. (setq subst-doc (substitute-command-keys doc)))
  434. (help-highlight-arguments subst-use1 subst-doc))
  435. (cons use1 doc))))
  436. (let ((fill-begin (point))
  437. (high-usage (car high))
  438. (high-doc (cdr high)))
  439. (unless (and (symbolp function)
  440. (get function 'reader-construct))
  441. (insert high-usage "\n"))
  442. (fill-region fill-begin (point))
  443. high-doc)))))
  444. (defun help-fns--parent-mode (function)
  445. ;; If this is a derived mode, link to the parent.
  446. (let ((parent-mode (and (symbolp function)
  447. (get function
  448. 'derived-mode-parent))))
  449. (when parent-mode
  450. (insert (substitute-command-keys "\nParent mode: `"))
  451. (let ((beg (point)))
  452. (insert (format "%s" parent-mode))
  453. (make-text-button beg (point)
  454. 'type 'help-function
  455. 'help-args (list parent-mode)))
  456. (insert (substitute-command-keys "'.\n")))))
  457. (defun help-fns--obsolete (function)
  458. ;; Ignore lambda constructs, keyboard macros, etc.
  459. (let* ((obsolete (and (symbolp function)
  460. (get function 'byte-obsolete-info)))
  461. (use (car obsolete)))
  462. (when obsolete
  463. (insert "\nThis "
  464. (if (eq (car-safe (symbol-function function)) 'macro)
  465. "macro"
  466. "function")
  467. " is obsolete")
  468. (when (nth 2 obsolete)
  469. (insert (format " since %s" (nth 2 obsolete))))
  470. (insert (cond ((stringp use) (concat ";\n" use))
  471. (use (format-message ";\nuse `%s' instead." use))
  472. (t "."))
  473. "\n"))))
  474. ;; We could use `symbol-file' but this is a wee bit more efficient.
  475. (defun help-fns--autoloaded-p (function file)
  476. "Return non-nil if FUNCTION has previously been autoloaded.
  477. FILE is the file where FUNCTION was probably defined."
  478. (let* ((file (file-name-sans-extension (file-truename file)))
  479. (load-hist load-history)
  480. (target (cons t function))
  481. found)
  482. (while (and load-hist (not found))
  483. (and (caar load-hist)
  484. (equal (file-name-sans-extension (caar load-hist)) file)
  485. (setq found (member target (cdar load-hist))))
  486. (setq load-hist (cdr load-hist)))
  487. found))
  488. (defun help-fns--interactive-only (function)
  489. "Insert some help blurb if FUNCTION should only be used interactively."
  490. ;; Ignore lambda constructs, keyboard macros, etc.
  491. (and (symbolp function)
  492. (not (eq (car-safe (symbol-function function)) 'macro))
  493. (let* ((interactive-only
  494. (or (get function 'interactive-only)
  495. (if (boundp 'byte-compile-interactive-only-functions)
  496. (memq function
  497. byte-compile-interactive-only-functions)))))
  498. (when interactive-only
  499. (insert "\nThis function is for interactive use only"
  500. ;; Cf byte-compile-form.
  501. (cond ((stringp interactive-only)
  502. (format ";\nin Lisp code %s" interactive-only))
  503. ((and (symbolp 'interactive-only)
  504. (not (eq interactive-only t)))
  505. (format-message ";\nin Lisp code use `%s' instead."
  506. interactive-only))
  507. (t "."))
  508. "\n")))))
  509. (defun help-fns-short-filename (filename)
  510. (let* ((abbrev (abbreviate-file-name filename))
  511. (short abbrev))
  512. (dolist (dir load-path)
  513. (let ((rel (file-relative-name filename dir)))
  514. (if (< (length rel) (length short))
  515. (setq short rel)))
  516. (let ((rel (file-relative-name abbrev dir)))
  517. (if (< (length rel) (length short))
  518. (setq short rel))))
  519. short))
  520. (defun help-fns--analyse-function (function)
  521. "Return information about FUNCTION.
  522. Returns a list of the form (REAL-FUNCTION DEF ALIASED REAL-DEF)."
  523. (let* ((advised (and (symbolp function)
  524. (advice--p (advice--symbol-function function))))
  525. ;; If the function is advised, use the symbol that has the
  526. ;; real definition, if that symbol is already set up.
  527. (real-function
  528. (or (and advised
  529. (advice--cd*r (advice--symbol-function function)))
  530. function))
  531. ;; Get the real definition, if any.
  532. (def (if (symbolp real-function)
  533. (cond ((symbol-function real-function))
  534. ((get real-function 'function-documentation)
  535. nil)
  536. (t (signal 'void-function (list real-function))))
  537. real-function))
  538. (aliased (and def
  539. (or (symbolp def)
  540. ;; Advised & aliased function.
  541. (and advised (symbolp real-function)
  542. (not (eq 'autoload (car-safe def))))
  543. (and (subrp def)
  544. (not (string= (subr-name def)
  545. (symbol-name function)))))))
  546. (real-def (cond
  547. ((and aliased (not (subrp def)))
  548. (let ((f real-function))
  549. (while (and (fboundp f)
  550. (symbolp (symbol-function f)))
  551. (setq f (symbol-function f)))
  552. f))
  553. ((subrp def) (intern (subr-name def)))
  554. (t def))))
  555. (list real-function def aliased real-def)))
  556. (defun help-fns-function-description-header (function)
  557. "Print a line describing FUNCTION to `standard-output'."
  558. (pcase-let* ((`(,_real-function ,def ,aliased ,real-def)
  559. (help-fns--analyse-function function))
  560. (file-name (find-lisp-object-file-name function (if aliased 'defun
  561. def)))
  562. (beg (if (and (or (byte-code-function-p def)
  563. (keymapp def)
  564. (memq (car-safe def) '(macro lambda closure)))
  565. (stringp file-name)
  566. (help-fns--autoloaded-p function file-name))
  567. (if (commandp def)
  568. "an interactive autoloaded "
  569. "an autoloaded ")
  570. (if (commandp def) "an interactive " "a "))))
  571. ;; Print what kind of function-like object FUNCTION is.
  572. (princ (cond ((or (stringp def) (vectorp def))
  573. "a keyboard macro")
  574. ((and (symbolp function)
  575. (get function 'reader-construct))
  576. "a reader construct")
  577. ;; Aliases are Lisp functions, so we need to check
  578. ;; aliases before functions.
  579. (aliased
  580. (format-message "an alias for `%s'" real-def))
  581. ((subrp def)
  582. (if (eq 'unevalled (cdr (subr-arity def)))
  583. (concat beg "special form")
  584. (concat beg "built-in function")))
  585. ((autoloadp def)
  586. (format "%s autoloaded %s"
  587. (if (commandp def) "an interactive" "an")
  588. (if (eq (nth 4 def) 'keymap) "keymap"
  589. (if (nth 4 def) "Lisp macro" "Lisp function"))))
  590. ((or (eq (car-safe def) 'macro)
  591. ;; For advised macros, def is a lambda
  592. ;; expression or a byte-code-function-p, so we
  593. ;; need to check macros before functions.
  594. (macrop function))
  595. (concat beg "Lisp macro"))
  596. ((byte-code-function-p def)
  597. (concat beg "compiled Lisp function"))
  598. ((eq (car-safe def) 'lambda)
  599. (concat beg "Lisp function"))
  600. ((eq (car-safe def) 'closure)
  601. (concat beg "Lisp closure"))
  602. ((keymapp def)
  603. (let ((is-full nil)
  604. (elts (cdr-safe def)))
  605. (while elts
  606. (if (char-table-p (car-safe elts))
  607. (setq is-full t
  608. elts nil))
  609. (setq elts (cdr-safe elts)))
  610. (concat beg (if is-full "keymap" "sparse keymap"))))
  611. (t "")))
  612. (if (and aliased (not (fboundp real-def)))
  613. (princ ",\nwhich is not defined. Please make a bug report.")
  614. (with-current-buffer standard-output
  615. (save-excursion
  616. (save-match-data
  617. (when (re-search-backward (substitute-command-keys
  618. "alias for `\\([^`']+\\)'")
  619. nil t)
  620. (help-xref-button 1 'help-function real-def)))))
  621. (when file-name
  622. ;; We used to add .el to the file name,
  623. ;; but that's completely wrong when the user used load-file.
  624. (princ (format-message " in `%s'"
  625. (if (eq file-name 'C-source)
  626. "C source code"
  627. (help-fns-short-filename file-name))))
  628. ;; Make a hyperlink to the library.
  629. (with-current-buffer standard-output
  630. (save-excursion
  631. (re-search-backward (substitute-command-keys "`\\([^`']+\\)'")
  632. nil t)
  633. (help-xref-button 1 'help-function-def function file-name))))
  634. (princ "."))))
  635. ;;;###autoload
  636. (defun describe-function-1 (function)
  637. (let ((pt1 (with-current-buffer (help-buffer) (point))))
  638. (help-fns-function-description-header function)
  639. (with-current-buffer (help-buffer)
  640. (fill-region-as-paragraph (save-excursion (goto-char pt1) (forward-line 0) (point))
  641. (point))))
  642. (terpri)(terpri)
  643. (pcase-let ((`(,real-function ,def ,_aliased ,real-def)
  644. (help-fns--analyse-function function))
  645. (doc-raw (documentation function t))
  646. (key-bindings-buffer (current-buffer)))
  647. ;; If the function is autoloaded, and its docstring has
  648. ;; key substitution constructs, load the library.
  649. (and (autoloadp real-def) doc-raw
  650. help-enable-auto-load
  651. (string-match "\\([^\\]=\\|[^=]\\|\\`\\)\\\\[[{<]" doc-raw)
  652. (autoload-do-load real-def))
  653. (help-fns--key-bindings function)
  654. (with-current-buffer standard-output
  655. (let ((doc (help-fns--signature
  656. function doc-raw
  657. (if (subrp def) (indirect-function real-def) real-def)
  658. real-function key-bindings-buffer)))
  659. (run-hook-with-args 'help-fns-describe-function-functions function)
  660. (insert "\n" (or doc "Not documented.")))
  661. ;; Avoid asking the user annoying questions if she decides
  662. ;; to save the help buffer, when her locale's codeset
  663. ;; isn't UTF-8.
  664. (unless (memq text-quoting-style '(straight grave))
  665. (set-buffer-file-coding-system 'utf-8)))))
  666. ;; Add defaults to `help-fns-describe-function-functions'.
  667. (add-hook 'help-fns-describe-function-functions #'help-fns--obsolete)
  668. (add-hook 'help-fns-describe-function-functions #'help-fns--interactive-only)
  669. (add-hook 'help-fns-describe-function-functions #'help-fns--parent-mode)
  670. (add-hook 'help-fns-describe-function-functions #'help-fns--compiler-macro)
  671. ;; Variables
  672. ;;;###autoload
  673. (defun variable-at-point (&optional any-symbol)
  674. "Return the bound variable symbol found at or before point.
  675. Return 0 if there is no such symbol.
  676. If ANY-SYMBOL is non-nil, don't insist the symbol be bound."
  677. (with-syntax-table emacs-lisp-mode-syntax-table
  678. (or (condition-case ()
  679. (save-excursion
  680. (skip-chars-forward "'")
  681. (or (not (zerop (skip-syntax-backward "_w")))
  682. (eq (char-syntax (following-char)) ?w)
  683. (eq (char-syntax (following-char)) ?_)
  684. (forward-sexp -1))
  685. (skip-chars-forward "'")
  686. (let ((obj (read (current-buffer))))
  687. (and (symbolp obj) (boundp obj) obj)))
  688. (error nil))
  689. (let* ((str (find-tag-default))
  690. (sym (if str (intern-soft str))))
  691. (if (and sym (or any-symbol (boundp sym)))
  692. sym
  693. (save-match-data
  694. (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str))
  695. (setq sym (intern-soft (match-string 1 str)))
  696. (and (or any-symbol (boundp sym)) sym)))))
  697. 0)))
  698. (defun describe-variable-custom-version-info (variable)
  699. (let ((custom-version (get variable 'custom-version))
  700. (cpv (get variable 'custom-package-version))
  701. (output nil))
  702. (if custom-version
  703. (setq output
  704. (format "This variable was introduced, or its default value was changed, in\nversion %s of Emacs.\n"
  705. custom-version))
  706. (when cpv
  707. (let* ((package (car-safe cpv))
  708. (version (if (listp (cdr-safe cpv))
  709. (car (cdr-safe cpv))
  710. (cdr-safe cpv)))
  711. (pkg-versions (assq package customize-package-emacs-version-alist))
  712. (emacsv (cdr (assoc version pkg-versions))))
  713. (if (and package version)
  714. (setq output
  715. (format (concat "This variable was introduced, or its default value was changed, in\nversion %s of the %s package"
  716. (if emacsv
  717. (format " that is part of Emacs %s" emacsv))
  718. ".\n")
  719. version package))))))
  720. output))
  721. ;;;###autoload
  722. (defun describe-variable (variable &optional buffer frame)
  723. "Display the full documentation of VARIABLE (a symbol).
  724. Returns the documentation as a string, also.
  725. If VARIABLE has a buffer-local value in BUFFER or FRAME
  726. \(default to the current buffer and current frame),
  727. it is displayed along with the global value."
  728. (interactive
  729. (let ((v (variable-at-point))
  730. (enable-recursive-minibuffers t)
  731. (orig-buffer (current-buffer))
  732. val)
  733. (setq val (completing-read
  734. (if (symbolp v)
  735. (format
  736. "Describe variable (default %s): " v)
  737. "Describe variable: ")
  738. #'help--symbol-completion-table
  739. (lambda (vv)
  740. ;; In case the variable only exists in the buffer
  741. ;; the command we switch back to that buffer before
  742. ;; we examine the variable.
  743. (with-current-buffer orig-buffer
  744. (or (get vv 'variable-documentation)
  745. (and (boundp vv) (not (keywordp vv))))))
  746. t nil nil
  747. (if (symbolp v) (symbol-name v))))
  748. (list (if (equal val "")
  749. v (intern val)))))
  750. (let (file-name)
  751. (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
  752. (unless (frame-live-p frame) (setq frame (selected-frame)))
  753. (if (not (symbolp variable))
  754. (message "You did not specify a variable")
  755. (save-excursion
  756. (let ((valvoid (not (with-current-buffer buffer (boundp variable))))
  757. (permanent-local (get variable 'permanent-local))
  758. val val-start-pos locus)
  759. ;; Extract the value before setting up the output buffer,
  760. ;; in case `buffer' *is* the output buffer.
  761. (unless valvoid
  762. (with-selected-frame frame
  763. (with-current-buffer buffer
  764. (setq val (symbol-value variable)
  765. locus (variable-binding-locus variable)))))
  766. (help-setup-xref (list #'describe-variable variable buffer)
  767. (called-interactively-p 'interactive))
  768. (with-help-window (help-buffer)
  769. (with-current-buffer buffer
  770. (prin1 variable)
  771. (setq file-name (find-lisp-object-file-name variable 'defvar))
  772. (if file-name
  773. (progn
  774. (princ (format-message
  775. " is a variable defined in `%s'.\n"
  776. (if (eq file-name 'C-source)
  777. "C source code"
  778. (file-name-nondirectory file-name))))
  779. (with-current-buffer standard-output
  780. (save-excursion
  781. (re-search-backward (substitute-command-keys
  782. "`\\([^`']+\\)'")
  783. nil t)
  784. (help-xref-button 1 'help-variable-def
  785. variable file-name)))
  786. (if valvoid
  787. (princ "It is void as a variable.")
  788. (princ "Its ")))
  789. (if valvoid
  790. (princ " is void as a variable.")
  791. (princ (substitute-command-keys "'s ")))))
  792. (unless valvoid
  793. (with-current-buffer standard-output
  794. (setq val-start-pos (point))
  795. (princ "value is")
  796. (let ((line-beg (line-beginning-position))
  797. (print-rep
  798. (let ((rep
  799. (let ((print-quoted t)
  800. (print-circle t))
  801. (cl-prin1-to-string val))))
  802. (if (and (symbolp val) (not (booleanp val)))
  803. (format-message "`%s'" rep)
  804. rep))))
  805. (if (< (+ (length print-rep) (point) (- line-beg)) 68)
  806. (insert " " print-rep)
  807. (terpri)
  808. (let ((buf (current-buffer)))
  809. (with-temp-buffer
  810. (insert print-rep)
  811. (pp-buffer)
  812. (let ((pp-buffer (current-buffer)))
  813. (with-current-buffer buf
  814. (insert-buffer-substring pp-buffer)))))
  815. ;; Remove trailing newline.
  816. (and (= (char-before) ?\n) (delete-char -1)))
  817. (let* ((sv (get variable 'standard-value))
  818. (origval (and (consp sv)
  819. (condition-case nil
  820. (eval (car sv))
  821. (error :help-eval-error))))
  822. from)
  823. (when (and (consp sv)
  824. (not (equal origval val))
  825. (not (equal origval :help-eval-error)))
  826. (princ "\nOriginal value was \n")
  827. (setq from (point))
  828. (cl-prin1 origval)
  829. (save-restriction
  830. (narrow-to-region from (point))
  831. (save-excursion (pp-buffer)))
  832. (if (< (point) (+ from 20))
  833. (delete-region (1- from) from)))))))
  834. (terpri)
  835. (when locus
  836. (cond
  837. ((bufferp locus)
  838. (princ (format "Local in buffer %s; "
  839. (buffer-name buffer))))
  840. ((terminal-live-p locus)
  841. (princ (format "It is a terminal-local variable; ")))
  842. (t
  843. (princ (format "It is local to %S" locus))))
  844. (if (not (default-boundp variable))
  845. (princ "globally void")
  846. (let ((global-val (default-value variable)))
  847. (with-current-buffer standard-output
  848. (princ "global value is ")
  849. (if (eq val global-val)
  850. (princ "the same.")
  851. (terpri)
  852. ;; Fixme: pp can take an age if you happen to
  853. ;; ask for a very large expression. We should
  854. ;; probably print it raw once and check it's a
  855. ;; sensible size before prettyprinting. -- fx
  856. (let ((from (point)))
  857. (cl-prin1 global-val)
  858. (save-restriction
  859. (narrow-to-region from (point))
  860. (save-excursion (pp-buffer)))
  861. ;; See previous comment for this function.
  862. ;; (help-xref-on-pp from (point))
  863. (if (< (point) (+ from 20))
  864. (delete-region (1- from) from)))))))
  865. (terpri))
  866. ;; If the value is large, move it to the end.
  867. (with-current-buffer standard-output
  868. (when (> (count-lines (point-min) (point-max)) 10)
  869. ;; Note that setting the syntax table like below
  870. ;; makes forward-sexp move over a `'s' at the end
  871. ;; of a symbol.
  872. (set-syntax-table emacs-lisp-mode-syntax-table)
  873. (goto-char val-start-pos)
  874. ;; The line below previously read as
  875. ;; (delete-region (point) (progn (end-of-line) (point)))
  876. ;; which suppressed display of the buffer local value for
  877. ;; large values.
  878. (when (looking-at "value is") (replace-match ""))
  879. (save-excursion
  880. (insert "\n\nValue:")
  881. (set (make-local-variable 'help-button-cache)
  882. (point-marker)))
  883. (insert "value is shown ")
  884. (insert-button "below"
  885. 'action help-button-cache
  886. 'follow-link t
  887. 'help-echo "mouse-2, RET: show value")
  888. (insert ".\n")))
  889. (terpri)
  890. (let* ((alias (condition-case nil
  891. (indirect-variable variable)
  892. (error variable)))
  893. (obsolete (get variable 'byte-obsolete-variable))
  894. (watchpoints (get-variable-watchers variable))
  895. (use (car obsolete))
  896. (safe-var (get variable 'safe-local-variable))
  897. (doc (or (documentation-property
  898. variable 'variable-documentation)
  899. (documentation-property
  900. alias 'variable-documentation)))
  901. (extra-line nil))
  902. ;; Mention if it's a local variable.
  903. (cond
  904. ((and (local-variable-if-set-p variable)
  905. (or (not (local-variable-p variable))
  906. (with-temp-buffer
  907. (local-variable-if-set-p variable))))
  908. (setq extra-line t)
  909. (princ " Automatically becomes ")
  910. (if permanent-local
  911. (princ "permanently "))
  912. (princ "buffer-local when set.\n"))
  913. ((not permanent-local))
  914. ((bufferp locus)
  915. (setq extra-line t)
  916. (princ
  917. (substitute-command-keys
  918. " This variable's buffer-local value is permanent.\n")))
  919. (t
  920. (setq extra-line t)
  921. (princ (substitute-command-keys
  922. " This variable's value is permanent \
  923. if it is given a local binding.\n"))))
  924. ;; Mention if it's an alias.
  925. (unless (eq alias variable)
  926. (setq extra-line t)
  927. (princ (format-message
  928. " This variable is an alias for `%s'.\n"
  929. alias)))
  930. (when obsolete
  931. (setq extra-line t)
  932. (princ " This variable is obsolete")
  933. (if (nth 2 obsolete)
  934. (princ (format " since %s" (nth 2 obsolete))))
  935. (princ (cond ((stringp use) (concat ";\n " use))
  936. (use (format-message ";\n use `%s' instead."
  937. (car obsolete)))
  938. (t ".")))
  939. (terpri))
  940. (when watchpoints
  941. (setq extra-line t)
  942. (princ " Calls these functions when changed: ")
  943. (princ watchpoints)
  944. (terpri))
  945. (when (member (cons variable val)
  946. (with-current-buffer buffer
  947. file-local-variables-alist))
  948. (setq extra-line t)
  949. (if (member (cons variable val)
  950. (with-current-buffer buffer
  951. dir-local-variables-alist))
  952. (let ((file (and (buffer-file-name buffer)
  953. (not (file-remote-p
  954. (buffer-file-name buffer)))
  955. (dir-locals-find-file
  956. (buffer-file-name buffer))))
  957. (is-directory nil))
  958. (princ (substitute-command-keys
  959. " This variable's value is directory-local"))
  960. (when (consp file) ; result from cache
  961. ;; If the cache element has an mtime, we
  962. ;; assume it came from a file.
  963. (if (nth 2 file)
  964. ;; (car file) is a directory.
  965. (setq file (dir-locals--all-files (car file)))
  966. ;; Otherwise, assume it was set directly.
  967. (setq file (car file)
  968. is-directory t)))
  969. (if (null file)
  970. (princ ".\n")
  971. (princ ", set ")
  972. (princ (substitute-command-keys
  973. (cond
  974. (is-directory "for the directory\n `")
  975. ;; Many files matched.
  976. ((and (consp file) (cdr file))
  977. (setq file (file-name-directory (car file)))
  978. (format "by one of the\n %s files in the directory\n `"
  979. dir-locals-file))
  980. (t (setq file (car file))
  981. "by the file\n `"))))
  982. (with-current-buffer standard-output
  983. (insert-text-button
  984. file 'type 'help-dir-local-var-def
  985. 'help-args (list variable file)))
  986. (princ (substitute-command-keys "'.\n"))))
  987. (princ (substitute-command-keys
  988. " This variable's value is file-local.\n"))))
  989. (when (memq variable ignored-local-variables)
  990. (setq extra-line t)
  991. (princ " This variable is ignored as a file-local \
  992. variable.\n"))
  993. ;; Can be both risky and safe, eg auto-fill-function.
  994. (when (risky-local-variable-p variable)
  995. (setq extra-line t)
  996. (princ " This variable may be risky if used as a \
  997. file-local variable.\n")
  998. (when (assq variable safe-local-variable-values)
  999. (princ (substitute-command-keys
  1000. " However, you have added it to \
  1001. `safe-local-variable-values'.\n"))))
  1002. (when safe-var
  1003. (setq extra-line t)
  1004. (princ " This variable is safe as a file local variable ")
  1005. (princ "if its value\n satisfies the predicate ")
  1006. (princ (if (byte-code-function-p safe-var)
  1007. "which is a byte-compiled expression.\n"
  1008. (format-message "`%s'.\n" safe-var))))
  1009. (if extra-line (terpri))
  1010. (princ "Documentation:\n")
  1011. (with-current-buffer standard-output
  1012. (insert (or doc "Not documented as a variable."))))
  1013. ;; Make a link to customize if this variable can be customized.
  1014. (when (custom-variable-p variable)
  1015. (let ((customize-label "customize"))
  1016. (terpri)
  1017. (terpri)
  1018. (princ (concat "You can " customize-label " this variable."))
  1019. (with-current-buffer standard-output
  1020. (save-excursion
  1021. (re-search-backward
  1022. (concat "\\(" customize-label "\\)") nil t)
  1023. (help-xref-button 1 'help-customize-variable variable))))
  1024. ;; Note variable's version or package version.
  1025. (let ((output (describe-variable-custom-version-info variable)))
  1026. (when output
  1027. (terpri)
  1028. (terpri)
  1029. (princ output))))
  1030. (with-current-buffer standard-output
  1031. ;; Return the text we displayed.
  1032. (buffer-string))))))))
  1033. (defvar help-xref-stack-item)
  1034. ;;;###autoload
  1035. (defun describe-symbol (symbol &optional buffer frame)
  1036. "Display the full documentation of SYMBOL.
  1037. Will show the info of SYMBOL as a function, variable, and/or face.
  1038. Optional arguments BUFFER and FRAME specify for which buffer and
  1039. frame to show the information about SYMBOL; they default to the
  1040. current buffer and the selected frame, respectively."
  1041. (interactive
  1042. (let* ((v-or-f (symbol-at-point))
  1043. (found (if v-or-f (cl-some (lambda (x) (funcall (nth 1 x) v-or-f))
  1044. describe-symbol-backends)))
  1045. (v-or-f (if found v-or-f (function-called-at-point)))
  1046. (found (or found v-or-f))
  1047. (enable-recursive-minibuffers t)
  1048. (val (completing-read (if found
  1049. (format
  1050. "Describe symbol (default %s): " v-or-f)
  1051. "Describe symbol: ")
  1052. obarray
  1053. (lambda (vv)
  1054. (cl-some (lambda (x) (funcall (nth 1 x) vv))
  1055. describe-symbol-backends))
  1056. t nil nil
  1057. (if found (symbol-name v-or-f)))))
  1058. (list (if (equal val "")
  1059. v-or-f (intern val)))))
  1060. (if (not (symbolp symbol))
  1061. (user-error "You didn't specify a function or variable"))
  1062. (unless (buffer-live-p buffer) (setq buffer (current-buffer)))
  1063. (unless (frame-live-p frame) (setq frame (selected-frame)))
  1064. (with-current-buffer (help-buffer)
  1065. ;; Push the previous item on the stack before clobbering the output buffer.
  1066. (help-setup-xref nil nil)
  1067. (let* ((docs
  1068. (nreverse
  1069. (delq nil
  1070. (mapcar (pcase-lambda (`(,name ,testfn ,descfn))
  1071. (when (funcall testfn symbol)
  1072. ;; Don't record the current entry in the stack.
  1073. (setq help-xref-stack-item nil)
  1074. (cons name
  1075. (funcall descfn symbol buffer frame))))
  1076. describe-symbol-backends))))
  1077. (single (null (cdr docs))))
  1078. (while (cdr docs)
  1079. (goto-char (point-min))
  1080. (let ((inhibit-read-only t)
  1081. (name (caar docs)) ;Name of doc currently at BOB.
  1082. (doc (cdr (cadr docs)))) ;Doc to add at BOB.
  1083. (when doc
  1084. (insert doc)
  1085. (delete-region (point)
  1086. (progn (skip-chars-backward " \t\n") (point)))
  1087. (insert "\n\n"
  1088. (eval-when-compile
  1089. (propertize "\n" 'face '(:height 0.1 :inverse-video t)))
  1090. "\n")
  1091. (when name
  1092. (insert (symbol-name symbol)
  1093. " is also a " name "." "\n\n"))))
  1094. (setq docs (cdr docs)))
  1095. (unless single
  1096. ;; Don't record the `describe-variable' item in the stack.
  1097. (setq help-xref-stack-item nil)
  1098. (help-setup-xref (list #'describe-symbol symbol) nil))
  1099. (goto-char (point-min)))))
  1100. ;;;###autoload
  1101. (defun describe-syntax (&optional buffer)
  1102. "Describe the syntax specifications in the syntax table of BUFFER.
  1103. The descriptions are inserted in a help buffer, which is then displayed.
  1104. BUFFER defaults to the current buffer."
  1105. (interactive)
  1106. (setq buffer (or buffer (current-buffer)))
  1107. (help-setup-xref (list #'describe-syntax buffer)
  1108. (called-interactively-p 'interactive))
  1109. (with-help-window (help-buffer)
  1110. (let ((table (with-current-buffer buffer (syntax-table))))
  1111. (with-current-buffer standard-output
  1112. (describe-vector table 'internal-describe-syntax-value)
  1113. (while (setq table (char-table-parent table))
  1114. (insert "\nThe parent syntax table is:")
  1115. (describe-vector table 'internal-describe-syntax-value))))))
  1116. (defun help-describe-category-set (value)
  1117. (insert (cond
  1118. ((null value) "default")
  1119. ((char-table-p value) "deeper char-table ...")
  1120. (t (condition-case nil
  1121. (category-set-mnemonics value)
  1122. (error "invalid"))))))
  1123. ;;;###autoload
  1124. (defun describe-categories (&optional buffer)
  1125. "Describe the category specifications in the current category table.
  1126. The descriptions are inserted in a buffer, which is then displayed.
  1127. If BUFFER is non-nil, then describe BUFFER's category table instead.
  1128. BUFFER should be a buffer or a buffer name."
  1129. (interactive)
  1130. (setq buffer (or buffer (current-buffer)))
  1131. (help-setup-xref (list #'describe-categories buffer)
  1132. (called-interactively-p 'interactive))
  1133. (with-help-window (help-buffer)
  1134. (let* ((table (with-current-buffer buffer (category-table)))
  1135. (docs (char-table-extra-slot table 0)))
  1136. (if (or (not (vectorp docs)) (/= (length docs) 95))
  1137. (error "Invalid first extra slot in this category table\n"))
  1138. (with-current-buffer standard-output
  1139. (setq-default help-button-cache (make-marker))
  1140. (insert "Legend of category mnemonics ")
  1141. (insert-button "(longer descriptions at the bottom)"
  1142. 'action help-button-cache
  1143. 'follow-link t
  1144. 'help-echo "mouse-2, RET: show full legend")
  1145. (insert "\n")
  1146. (let ((pos (point)) (items 0) lines n)
  1147. (dotimes (i 95)
  1148. (if (aref docs i) (setq items (1+ items))))
  1149. (setq lines (1+ (/ (1- items) 4)))
  1150. (setq n 0)
  1151. (dotimes (i 95)
  1152. (let ((elt (aref docs i)))
  1153. (when elt
  1154. (string-match ".*" elt)
  1155. (setq elt (match-string 0 elt))
  1156. (if (>= (length elt) 17)
  1157. (setq elt (concat (substring elt 0 14) "...")))
  1158. (if (< (point) (point-max))
  1159. (move-to-column (* 20 (/ n lines)) t))
  1160. (insert (+ i ?\s) ?: elt)
  1161. (if (< (point) (point-max))
  1162. (forward-line 1)
  1163. (insert "\n"))
  1164. (setq n (1+ n))
  1165. (if (= (% n lines) 0)
  1166. (goto-char pos))))))
  1167. (goto-char (point-max))
  1168. (insert "\n"
  1169. "character(s)\tcategory mnemonics\n"
  1170. "------------\t------------------")
  1171. (describe-vector table 'help-describe-category-set)
  1172. (set-marker help-button-cache (point))
  1173. (insert "Legend of category mnemonics:\n")
  1174. (dotimes (i 95)
  1175. (let ((elt (aref docs i)))
  1176. (when elt
  1177. (if (string-match "\n" elt)
  1178. (setq elt (substring elt (match-end 0))))
  1179. (insert (+ i ?\s) ": " elt "\n"))))
  1180. (while (setq table (char-table-parent table))
  1181. (insert "\nThe parent category table is:")
  1182. (describe-vector table 'help-describe-category-set))))))
  1183. ;;; Replacements for old lib-src/ programs. Don't seem especially useful.
  1184. ;; Replaces lib-src/digest-doc.c.
  1185. ;;;###autoload
  1186. (defun doc-file-to-man (file)
  1187. "Produce an nroff buffer containing the doc-strings from the DOC file."
  1188. (interactive (list (read-file-name "Name of DOC file: " doc-directory
  1189. internal-doc-file-name t)))
  1190. (or (file-readable-p file)
  1191. (error "Cannot read file `%s'" file))
  1192. (pop-to-buffer (generate-new-buffer "*man-doc*"))
  1193. (setq buffer-undo-list t)
  1194. (insert ".TH \"Command Summary for GNU Emacs\"\n"
  1195. ".AU Richard M. Stallman\n")
  1196. (insert-file-contents file)
  1197. (let (notfirst)
  1198. (while (search-forward "" nil 'move)
  1199. (if (= (following-char) ?S)
  1200. (delete-region (1- (point)) (line-end-position))
  1201. (delete-char -1)
  1202. (if notfirst
  1203. (insert "\n.DE\n")
  1204. (setq notfirst t))
  1205. (insert "\n.SH ")
  1206. (insert (if (= (following-char) ?F) "Function " "Variable "))
  1207. (delete-char 1)
  1208. (forward-line 1)
  1209. (insert ".DS L\n"))))
  1210. (insert "\n.DE\n")
  1211. (setq buffer-undo-list nil)
  1212. (nroff-mode))
  1213. ;; Replaces lib-src/sorted-doc.c.
  1214. ;;;###autoload
  1215. (defun doc-file-to-info (file)
  1216. "Produce a texinfo buffer with sorted doc-strings from the DOC file."
  1217. (interactive (list (read-file-name "Name of DOC file: " doc-directory
  1218. internal-doc-file-name t)))
  1219. (or (file-readable-p file)
  1220. (error "Cannot read file `%s'" file))
  1221. (let ((i 0) type name doc alist)
  1222. (with-temp-buffer
  1223. (insert-file-contents file)
  1224. ;; The characters "@{}" need special treatment.
  1225. (while (re-search-forward "[@{}]" nil t)
  1226. (backward-char)
  1227. (insert "@")
  1228. (forward-char 1))
  1229. (goto-char (point-min))
  1230. (while (search-forward "" nil t)
  1231. (when (/= (following-char) ?S)
  1232. (setq type (char-after)
  1233. name (buffer-substring (1+ (point)) (line-end-position))
  1234. doc (buffer-substring (line-beginning-position 2)
  1235. (if (search-forward "" nil 'move)
  1236. (1- (point))
  1237. (point)))
  1238. alist (cons (list name type doc) alist))
  1239. (backward-char 1))))
  1240. (pop-to-buffer (generate-new-buffer "*info-doc*"))
  1241. (setq buffer-undo-list t)
  1242. ;; Write the output header.
  1243. (insert "\\input texinfo @c -*-texinfo-*-\n"
  1244. "@setfilename emacsdoc.info\n"
  1245. "@settitle Command Summary for GNU Emacs\n"
  1246. "@finalout\n"
  1247. "\n@node Top\n"
  1248. "@unnumbered Command Summary for GNU Emacs\n\n"
  1249. "@table @asis\n\n"
  1250. "@iftex\n"
  1251. "@global@let@ITEM@item\n"
  1252. "@def@item{@filbreak@vskip5pt@ITEM}\n"
  1253. "@font@tensy cmsy10 scaled @magstephalf\n"
  1254. "@font@teni cmmi10 scaled @magstephalf\n"
  1255. "@def\\{{@tensy@char110}}\n" ; this backslash goes with cmr10
  1256. "@def|{{@tensy@char106}}\n"
  1257. "@def@{{{@tensy@char102}}\n"
  1258. "@def@}{{@tensy@char103}}\n"
  1259. "@def<{{@teni@char62}}\n"
  1260. "@def>{{@teni@char60}}\n"
  1261. "@chardef@@64\n"
  1262. "@catcode43=12\n"
  1263. "@tableindent-0.2in\n"
  1264. "@end iftex\n")
  1265. ;; Sort the array by name; within each name, by type (functions first).
  1266. (setq alist (sort alist (lambda (e1 e2)
  1267. (if (string-equal (car e1) (car e2))
  1268. (<= (cadr e1) (cadr e2))
  1269. (string-lessp (car e1) (car e2))))))
  1270. ;; Print each function.
  1271. (dolist (e alist)
  1272. (insert "\n@item "
  1273. (if (char-equal (cadr e) ?\F) "Function" "Variable")
  1274. " @code{" (car e) "}\n@display\n"
  1275. (nth 2 e)
  1276. "\n@end display\n")
  1277. ;; Try to avoid a save size overflow in the TeX output routine.
  1278. (if (zerop (setq i (% (1+ i) 100)))
  1279. (insert "\n@end table\n@table @asis\n")))
  1280. (insert "@end table\n"
  1281. "@bye\n")
  1282. (setq buffer-undo-list nil)
  1283. (texinfo-mode)))
  1284. (provide 'help-fns)
  1285. ;;; help-fns.el ends here