help-fns.el 42 KB

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