help-fns.el 49 KB

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