Commit | Line | Data |
---|---|---|
125eb411 | 1 | ;;; help-fns.el --- Complex help functions |
44e75f50 | 2 | |
0d30b337 | 3 | ;; Copyright (C) 1985, 1986, 1993, 1994, 1998, 1999, 2000, 2001, |
409cc4a3 | 4 | ;; 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
44e75f50 MB |
5 | |
6 | ;; Maintainer: FSF | |
7 | ;; Keywords: help, internal | |
8 | ||
9 | ;; This file is part of GNU Emacs. | |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
b4aa6026 | 13 | ;; the Free Software Foundation; either version 3, or (at your option) |
44e75f50 MB |
14 | ;; any later version. |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
086add15 LK |
23 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 | ;; Boston, MA 02110-1301, USA. | |
44e75f50 MB |
25 | |
26 | ;;; Commentary: | |
27 | ||
28 | ;; This file contains those help commands which are complicated, and | |
29 | ;; which may not be used in every session. For example | |
30 | ;; `describe-function' will probably be heavily used when doing elisp | |
31 | ;; programming, but not if just editing C files. Simpler help commands | |
32 | ;; are in help.el | |
33 | ||
34 | ;;; Code: | |
35 | ||
8422a3b8 SM |
36 | (require 'help-mode) |
37 | ||
44e75f50 MB |
38 | ;; Functions |
39 | ||
40 | ;;;###autoload | |
41 | (defun describe-function (function) | |
42 | "Display the full documentation of FUNCTION (a symbol)." | |
43 | (interactive | |
44 | (let ((fn (function-called-at-point)) | |
45 | (enable-recursive-minibuffers t) | |
46 | val) | |
47 | (setq val (completing-read (if fn | |
48 | (format "Describe function (default %s): " fn) | |
49 | "Describe function: ") | |
5effb4d8 RS |
50 | obarray 'fboundp t nil nil |
51 | (and fn (symbol-name fn)))) | |
44e75f50 MB |
52 | (list (if (equal val "") |
53 | fn (intern val))))) | |
54 | (if (null function) | |
55 | (message "You didn't specify a function") | |
8422a3b8 | 56 | (help-setup-xref (list #'describe-function function) (interactive-p)) |
5a6a8d3b | 57 | (save-excursion |
aa13a094 | 58 | (with-help-window (help-buffer) |
5a6a8d3b RS |
59 | (prin1 function) |
60 | ;; Use " is " instead of a colon so that | |
61 | ;; it is easier to get out the function name using forward-sexp. | |
62 | (princ " is ") | |
63 | (describe-function-1 function) | |
5a6a8d3b RS |
64 | (with-current-buffer standard-output |
65 | ;; Return the text we displayed. | |
66 | (buffer-string)))))) | |
44e75f50 | 67 | |
00ed4f90 JB |
68 | (defun help-split-fundoc (docstring def) |
69 | "Split a function DOCSTRING into the actual doc and the usage info. | |
f63f0981 | 70 | Return (USAGE . DOC) or nil if there's no usage info. |
00ed4f90 | 71 | DEF is the function whose usage we're looking for in DOCSTRING." |
f63f0981 | 72 | ;; Functions can get the calling sequence at the end of the doc string. |
2dbbed9e | 73 | ;; In cases where `function' has been fset to a subr we can't search for |
f63f0981 SM |
74 | ;; function's name in the doc string so we use `fn' as the anonymous |
75 | ;; function name instead. | |
00ed4f90 | 76 | (when (and docstring (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring)) |
bbb7041a SM |
77 | (cons (format "(%s%s" |
78 | ;; Replace `fn' with the actual function name. | |
79 | (if (consp def) "anonymous" def) | |
00ed4f90 JB |
80 | (match-string 1 docstring)) |
81 | (substring docstring 0 (match-beginning 0))))) | |
bbb7041a | 82 | |
00ed4f90 JB |
83 | (defun help-add-fundoc-usage (docstring arglist) |
84 | "Add the usage info to DOCSTRING. | |
85 | If DOCSTRING already has a usage info, then just return it unchanged. | |
86 | The usage info is built from ARGLIST. DOCSTRING can be nil. | |
87 | ARGLIST can also be t or a string of the form \"(FUN ARG1 ARG2 ...)\"." | |
88 | (unless (stringp docstring) (setq docstring "Not documented")) | |
89 | (if (or (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring) (eq arglist t)) | |
90 | docstring | |
7d57db29 | 91 | (concat docstring |
00ed4f90 | 92 | (if (string-match "\n?\n\\'" docstring) |
88ff724b | 93 | (if (< (- (match-end 0) (match-beginning 0)) 2) "\n" "") |
bbb7041a | 94 | "\n\n") |
95734598 SM |
95 | (if (and (stringp arglist) |
96 | (string-match "\\`([^ ]+\\(.*\\))\\'" arglist)) | |
97 | (concat "(fn" (match-string 1 arglist) ")") | |
7d57db29 | 98 | (format "%S" (help-make-usage 'fn arglist)))))) |
2dbbed9e SM |
99 | |
100 | (defun help-function-arglist (def) | |
26f19477 SM |
101 | ;; Handle symbols aliased to other symbols. |
102 | (if (and (symbolp def) (fboundp def)) (setq def (indirect-function def))) | |
103 | ;; If definition is a macro, find the function inside it. | |
104 | (if (eq (car-safe def) 'macro) (setq def (cdr def))) | |
2dbbed9e SM |
105 | (cond |
106 | ((byte-code-function-p def) (aref def 0)) | |
107 | ((eq (car-safe def) 'lambda) (nth 1 def)) | |
108 | ((and (eq (car-safe def) 'autoload) (not (eq (nth 4 def) 'keymap))) | |
109 | "[Arg list not available until function definition is loaded.]") | |
110 | (t t))) | |
111 | ||
112 | (defun help-make-usage (function arglist) | |
113 | (cons (if (symbolp function) function 'anonymous) | |
114 | (mapcar (lambda (arg) | |
f63f0981 SM |
115 | (if (not (symbolp arg)) |
116 | (if (and (consp arg) (symbolp (car arg))) | |
117 | ;; CL style default values for optional args. | |
118 | (cons (intern (upcase (symbol-name (car arg)))) | |
119 | (cdr arg)) | |
120 | arg) | |
2dbbed9e SM |
121 | (let ((name (symbol-name arg))) |
122 | (if (string-match "\\`&" name) arg | |
123 | (intern (upcase name)))))) | |
124 | arglist))) | |
125 | ||
d4b4dfc6 SM |
126 | ;; Could be this, if we make symbol-file do the work below. |
127 | ;; (defun help-C-file-name (subr-or-var kind) | |
128 | ;; "Return the name of the C file where SUBR-OR-VAR is defined. | |
129 | ;; KIND should be `var' for a variable or `subr' for a subroutine." | |
130 | ;; (symbol-file (if (symbolp subr-or-var) subr-or-var | |
131 | ;; (subr-name subr-or-var)) | |
132 | ;; (if (eq kind 'var) 'defvar 'defun))) | |
6dfa731f | 133 | ;;;###autoload |
154ee9b7 SM |
134 | (defun help-C-file-name (subr-or-var kind) |
135 | "Return the name of the C file where SUBR-OR-VAR is defined. | |
136 | KIND should be `var' for a variable or `subr' for a subroutine." | |
137 | (let ((docbuf (get-buffer-create " *DOC*")) | |
138 | (name (if (eq 'var kind) | |
139 | (concat "V" (symbol-name subr-or-var)) | |
3ff0a7e9 | 140 | (concat "F" (subr-name subr-or-var))))) |
154ee9b7 SM |
141 | (with-current-buffer docbuf |
142 | (goto-char (point-min)) | |
143 | (if (eobp) | |
144 | (insert-file-contents-literally | |
145 | (expand-file-name internal-doc-file-name doc-directory))) | |
84c2fd9f JD |
146 | (let ((file (catch 'loop |
147 | (while t | |
148 | (let ((pnt (search-forward (concat "\1f" name "\n")))) | |
1bc9c7ed RS |
149 | (re-search-backward "\1fS\\(.*\\)") |
150 | (let ((file (match-string 1))) | |
84c2fd9f JD |
151 | (if (member file build-files) |
152 | (throw 'loop file) | |
153 | (goto-char pnt)))))))) | |
154ee9b7 | 154 | (if (string-match "\\.\\(o\\|obj\\)\\'" file) |
3ff0a7e9 SM |
155 | (setq file (replace-match ".c" t t file))) |
156 | (if (string-match "\\.c\\'" file) | |
157 | (concat "src/" file) | |
154ee9b7 SM |
158 | file))))) |
159 | ||
8c1138be | 160 | (defface help-argument-name '((((supports :slant italic)) :inherit italic)) |
575f6118 JL |
161 | "Face to highlight argument names in *Help* buffers." |
162 | :group 'help) | |
8be2a2dd | 163 | |
aae424b9 JB |
164 | (defun help-default-arg-highlight (arg) |
165 | "Default function to highlight arguments in *Help* buffers. | |
8be2a2dd JB |
166 | It returns ARG in face `help-argument-name'; ARG is also |
167 | downcased if it displays differently than the default | |
168 | face (according to `face-differs-from-default-p')." | |
169 | (propertize (if (face-differs-from-default-p 'help-argument-name) | |
170 | (downcase arg) | |
171 | arg) | |
172 | 'face 'help-argument-name)) | |
c57ada27 JB |
173 | |
174 | (defun help-do-arg-highlight (doc args) | |
6aab5f13 JB |
175 | (with-syntax-table (make-syntax-table emacs-lisp-mode-syntax-table) |
176 | (modify-syntax-entry ?\- "w") | |
7a650da0 JB |
177 | (dolist (arg args doc) |
178 | (setq doc (replace-regexp-in-string | |
179 | ;; This is heuristic, but covers all common cases | |
180 | ;; except ARG1-ARG2 | |
181 | (concat "\\<" ; beginning of word | |
182 | "\\(?:[a-z-]*-\\)?" ; for xxx-ARG | |
183 | "\\(" | |
184 | (regexp-quote arg) | |
185 | "\\)" | |
186 | "\\(?:es\\|s\\|th\\)?" ; for ARGth, ARGs | |
187 | "\\(?:-[a-z0-9-]+\\)?" ; for ARG-xxx, ARG-n | |
dee503e5 | 188 | "\\(?:-[{([<`\"].*?\\)?"; for ARG-{x}, (x), <x>, [x], `x' |
7a650da0 JB |
189 | "\\>") ; end of word |
190 | (help-default-arg-highlight arg) | |
191 | doc t t 1))))) | |
c57ada27 JB |
192 | |
193 | (defun help-highlight-arguments (usage doc &rest args) | |
194 | (when usage | |
fef3a3ab JB |
195 | (with-temp-buffer |
196 | (insert usage) | |
197 | (goto-char (point-min)) | |
198 | (let ((case-fold-search nil) | |
199 | (next (not (or args (looking-at "\\[")))) | |
200 | (opt nil)) | |
c57ada27 | 201 | ;; Make a list of all arguments |
7d57db29 | 202 | (skip-chars-forward "^ ") |
c57ada27 | 203 | (while next |
6aab5f13 | 204 | (or opt (not (looking-at " &")) (setq opt t)) |
fef3a3ab | 205 | (if (not (re-search-forward " \\([\\[(]*\\)\\([^] &)\.]+\\)" nil t)) |
c57ada27 JB |
206 | (setq next nil) |
207 | (setq args (cons (match-string 2) args)) | |
6aab5f13 | 208 | (when (and opt (string= (match-string 1) "(")) |
c57ada27 JB |
209 | ;; A pesky CL-style optional argument with default value, |
210 | ;; so let's skip over it | |
211 | (search-backward "(") | |
212 | (goto-char (scan-sexps (point) 1))))) | |
213 | ;; Highlight aguments in the USAGE string | |
60c24955 JB |
214 | (setq usage (help-do-arg-highlight (buffer-string) args)) |
215 | ;; Highlight arguments in the DOC string | |
55fdba31 JB |
216 | (setq doc (and doc (help-do-arg-highlight doc args)))))) |
217 | ;; Return value is like the one from help-split-fundoc, but highlighted | |
218 | (cons usage doc)) | |
c57ada27 | 219 | |
ac8c0cce | 220 | ;;;###autoload |
9888177f RS |
221 | (defun describe-simplify-lib-file-name (file) |
222 | "Simplify a library name FILE to a relative name, and make it a source file." | |
223 | (if file | |
224 | ;; Try converting the absolute file name to a library name. | |
225 | (let ((libname (file-name-nondirectory file))) | |
226 | ;; Now convert that back to a file name and see if we get | |
227 | ;; the original one. If so, they are equivalent. | |
228 | (if (equal file (locate-file libname load-path '(""))) | |
1cf586a4 | 229 | (if (string-match "[.]elc\\'" libname) |
9888177f RS |
230 | (substring libname 0 -1) |
231 | libname) | |
232 | file)))) | |
233 | ||
2f041d62 JD |
234 | (defun find-source-lisp-file (file-name) |
235 | (let* ((elc-file (locate-file (concat file-name | |
236 | (if (string-match "\\.el" file-name) | |
237 | "c" | |
238 | ".elc")) | |
239 | load-path)) | |
240 | (str (if (and elc-file (file-readable-p elc-file)) | |
20beebfe | 241 | (with-temp-buffer |
2f041d62 JD |
242 | (insert-file-contents-literally elc-file nil 0 256) |
243 | (buffer-string)))) | |
244 | (src-file (and str | |
245 | (string-match ";;; from file \\(.*\\.el\\)" str) | |
246 | (match-string 1 str)))) | |
247 | (if (and src-file (file-readable-p src-file)) | |
248 | src-file | |
249 | file-name))) | |
250 | ||
2c52d7a3 | 251 | (declare-function ad-get-advice-info "advice" (function)) |
e8ffb999 | 252 | |
8422a3b8 SM |
253 | ;;;###autoload |
254 | (defun describe-function-1 (function) | |
b2f2cd56 RS |
255 | (let* ((advised (and (symbolp function) (featurep 'advice) |
256 | (ad-get-advice-info function))) | |
dd3f89d7 RS |
257 | ;; If the function is advised, use the symbol that has the |
258 | ;; real definition, if that symbol is already set up. | |
72a20032 | 259 | (real-function |
dd3f89d7 RS |
260 | (or (and advised |
261 | (cdr (assq 'origname advised)) | |
262 | (fboundp (cdr (assq 'origname advised))) | |
263 | (cdr (assq 'origname advised))) | |
07894d42 | 264 | function)) |
72a20032 RS |
265 | ;; Get the real definition. |
266 | (def (if (symbolp real-function) | |
267 | (symbol-function real-function) | |
44e75f50 | 268 | function)) |
8422a3b8 | 269 | file-name string |
44e75f50 MB |
270 | (beg (if (commandp def) "an interactive " "a "))) |
271 | (setq string | |
272 | (cond ((or (stringp def) | |
273 | (vectorp def)) | |
274 | "a keyboard macro") | |
275 | ((subrp def) | |
276 | (if (eq 'unevalled (cdr (subr-arity def))) | |
277 | (concat beg "special form") | |
278 | (concat beg "built-in function"))) | |
279 | ((byte-code-function-p def) | |
280 | (concat beg "compiled Lisp function")) | |
281 | ((symbolp def) | |
282 | (while (symbolp (symbol-function def)) | |
283 | (setq def (symbol-function def))) | |
284 | (format "an alias for `%s'" def)) | |
285 | ((eq (car-safe def) 'lambda) | |
286 | (concat beg "Lisp function")) | |
287 | ((eq (car-safe def) 'macro) | |
288 | "a Lisp macro") | |
44e75f50 MB |
289 | ((eq (car-safe def) 'autoload) |
290 | (setq file-name (nth 1 def)) | |
291 | (format "%s autoloaded %s" | |
292 | (if (commandp def) "an interactive" "an") | |
293 | (if (eq (nth 4 def) 'keymap) "keymap" | |
294 | (if (nth 4 def) "Lisp macro" "Lisp function")) | |
295 | )) | |
26f19477 | 296 | ((keymapp def) |
44e75f50 MB |
297 | (let ((is-full nil) |
298 | (elts (cdr-safe def))) | |
299 | (while elts | |
300 | (if (char-table-p (car-safe elts)) | |
301 | (setq is-full t | |
302 | elts nil)) | |
303 | (setq elts (cdr-safe elts))) | |
304 | (if is-full | |
305 | "a full keymap" | |
306 | "a sparse keymap"))) | |
307 | (t ""))) | |
44e75f50 | 308 | (princ string) |
8422a3b8 | 309 | (with-current-buffer standard-output |
44e75f50 MB |
310 | (save-excursion |
311 | (save-match-data | |
312 | (if (re-search-backward "alias for `\\([^`']+\\)'" nil t) | |
313 | (help-xref-button 1 'help-function def))))) | |
314 | (or file-name | |
38fb0354 | 315 | (setq file-name (symbol-file function 'defun))) |
9888177f | 316 | (setq file-name (describe-simplify-lib-file-name file-name)) |
35679c3f MR |
317 | (when (equal file-name "loaddefs.el") |
318 | ;; Find the real def site of the preloaded function. | |
319 | ;; This is necessary only for defaliases. | |
320 | (let ((location | |
321 | (condition-case nil | |
d24c52bb | 322 | (find-function-search-for-symbol function nil "loaddefs.el") |
35679c3f MR |
323 | (error nil)))) |
324 | (when location | |
325 | (with-current-buffer (car location) | |
326 | (goto-char (cdr location)) | |
327 | (when (re-search-backward | |
328 | "^;;; Generated autoloads from \\(.*\\)" nil t) | |
329 | (setq file-name (match-string 1))))))) | |
3ff0a7e9 | 330 | (when (and (null file-name) (subrp def)) |
154ee9b7 | 331 | ;; Find the C source file name. |
3ff0a7e9 SM |
332 | (setq file-name (if (get-buffer " *DOC*") |
333 | (help-C-file-name def 'subr) | |
334 | 'C-source))) | |
154ee9b7 | 335 | (when file-name |
5a6a8d3b RS |
336 | (princ " in `") |
337 | ;; We used to add .el to the file name, | |
338 | ;; but that's completely wrong when the user used load-file. | |
3ff0a7e9 | 339 | (princ (if (eq file-name 'C-source) "C source code" file-name)) |
5a6a8d3b | 340 | (princ "'") |
2f041d62 JD |
341 | ;; See if lisp files are present where they where installed from. |
342 | (if (not (eq file-name 'C-source)) | |
343 | (setq file-name (find-source-lisp-file file-name))) | |
344 | ||
5a6a8d3b | 345 | ;; Make a hyperlink to the library. |
8422a3b8 | 346 | (with-current-buffer standard-output |
154ee9b7 | 347 | (save-excursion |
5a6a8d3b | 348 | (re-search-backward "`\\([^`']+\\)'" nil t) |
72a20032 | 349 | (help-xref-button 1 'help-function-def real-function file-name)))) |
44e75f50 MB |
350 | (princ ".") |
351 | (terpri) | |
352 | (when (commandp function) | |
ddbb1956 RS |
353 | (if (and (eq function 'self-insert-command) |
354 | (eq (key-binding "a") 'self-insert-command) | |
355 | (eq (key-binding "b") 'self-insert-command) | |
356 | (eq (key-binding "c") 'self-insert-command)) | |
357 | (princ "It is bound to many ordinary text characters.\n") | |
358 | (let* ((remapped (command-remapping function)) | |
359 | (keys (where-is-internal | |
360 | (or remapped function) overriding-local-map nil nil)) | |
361 | non-modified-keys) | |
362 | ;; Which non-control non-meta keys run this command? | |
363 | (dolist (key keys) | |
364 | (if (member (event-modifiers (aref key 0)) '(nil (shift))) | |
365 | (push key non-modified-keys))) | |
366 | (when remapped | |
367 | (princ "It is remapped to `") | |
368 | (princ (symbol-name remapped)) | |
369 | (princ "'")) | |
19a151e5 | 370 | |
ddbb1956 RS |
371 | (when keys |
372 | (princ (if remapped " which is bound to " "It is bound to ")) | |
373 | ;; If lots of ordinary text characters run this command, | |
374 | ;; don't mention them one by one. | |
375 | (if (< (length non-modified-keys) 10) | |
376 | (princ (mapconcat 'key-description keys ", ")) | |
377 | (dolist (key non-modified-keys) | |
378 | (setq keys (delq key keys))) | |
379 | (if keys | |
380 | (progn | |
381 | (princ (mapconcat 'key-description keys ", ")) | |
382 | (princ ", and many ordinary text characters")) | |
383 | (princ "many ordinary text characters")))) | |
384 | (when (or remapped keys non-modified-keys) | |
385 | (princ ".") | |
386 | (terpri))))) | |
2dbbed9e SM |
387 | (let* ((arglist (help-function-arglist def)) |
388 | (doc (documentation function)) | |
f63f0981 | 389 | (usage (help-split-fundoc doc function))) |
c57ada27 JB |
390 | (with-current-buffer standard-output |
391 | ;; If definition is a keymap, skip arglist note. | |
392 | (unless (keymapp def) | |
393 | (let* ((use (cond | |
394 | (usage (setq doc (cdr usage)) (car usage)) | |
395 | ((listp arglist) | |
396 | (format "%S" (help-make-usage function arglist))) | |
397 | ((stringp arglist) arglist) | |
72a20032 RS |
398 | ;; Maybe the arglist is in the docstring of a symbol |
399 | ;; this one is aliased to. | |
400 | ((let ((fun real-function)) | |
c57ada27 JB |
401 | (while (and (symbolp fun) |
402 | (setq fun (symbol-function fun)) | |
403 | (not (setq usage (help-split-fundoc | |
404 | (documentation fun) | |
405 | function))))) | |
406 | usage) | |
407 | (car usage)) | |
408 | ((or (stringp def) | |
409 | (vectorp def)) | |
410 | (format "\nMacro: %s" (format-kbd-macro def))) | |
411 | (t "[Missing arglist. Please make a bug report.]"))) | |
412 | (high (help-highlight-arguments use doc))) | |
24374f5a JPW |
413 | (let ((fill-begin (point))) |
414 | (insert (car high) "\n") | |
415 | (fill-region fill-begin (point))) | |
c57ada27 JB |
416 | (setq doc (cdr high)))) |
417 | (let ((obsolete (and | |
418 | ;; function might be a lambda construct. | |
419 | (symbolp function) | |
420 | (get function 'byte-obsolete-info)))) | |
421 | (when obsolete | |
422 | (princ "\nThis function is obsolete") | |
423 | (when (nth 2 obsolete) | |
424 | (insert (format " since %s" (nth 2 obsolete)))) | |
425 | (insert ";\n" | |
426 | (if (stringp (car obsolete)) (car obsolete) | |
427 | (format "use `%s' instead." (car obsolete))) | |
428 | "\n")) | |
429 | (insert "\n" | |
430 | (or doc "Not documented."))))))) | |
44e75f50 MB |
431 | |
432 | \f | |
433 | ;; Variables | |
434 | ||
435 | ;;;###autoload | |
20a514ce | 436 | (defun variable-at-point (&optional any-symbol) |
64a8fd66 | 437 | "Return the bound variable symbol found at or before point. |
20a514ce RS |
438 | Return 0 if there is no such symbol. |
439 | If ANY-SYMBOL is non-nil, don't insist the symbol be bound." | |
cc3064a5 JL |
440 | (or (condition-case () |
441 | (with-syntax-table emacs-lisp-mode-syntax-table | |
442 | (save-excursion | |
443 | (or (not (zerop (skip-syntax-backward "_w"))) | |
444 | (eq (char-syntax (following-char)) ?w) | |
445 | (eq (char-syntax (following-char)) ?_) | |
446 | (forward-sexp -1)) | |
447 | (skip-chars-forward "'") | |
448 | (let ((obj (read (current-buffer)))) | |
449 | (and (symbolp obj) (boundp obj) obj)))) | |
450 | (error nil)) | |
451 | (let* ((str (find-tag-default)) | |
f507d6a1 | 452 | (sym (if str (intern-soft str)))) |
20a514ce | 453 | (if (and sym (or any-symbol (boundp sym))) |
f507d6a1 JL |
454 | sym |
455 | (save-match-data | |
456 | (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str)) | |
457 | (setq sym (intern-soft (match-string 1 str))) | |
20a514ce | 458 | (and (or any-symbol (boundp sym)) sym))))) |
cc3064a5 | 459 | 0)) |
44e75f50 | 460 | |
05849e2a JPW |
461 | (defun describe-variable-custom-version-info (variable) |
462 | (let ((custom-version (get variable 'custom-version)) | |
463 | (cpv (get variable 'custom-package-version)) | |
464 | (output nil)) | |
465 | (if custom-version | |
466 | (setq output | |
467 | (format "This variable was introduced, or its default value was changed, in\nversion %s of Emacs.\n" | |
468 | custom-version)) | |
469 | (when cpv | |
470 | (let* ((package (car-safe cpv)) | |
471 | (version (car (cdr-safe cpv))) | |
472 | (pkg-versions (assq package customize-package-emacs-version-alist)) | |
473 | (emacsv (cdr (assoc version pkg-versions)))) | |
474 | (if (and package version) | |
475 | (setq output | |
476 | (format (concat "This variable was introduced, or its default value was changed, in\nversion %s of the %s package" | |
477 | (if emacsv | |
478 | (format " that is part of Emacs %s" emacsv)) | |
479 | ".\n") | |
480 | version package)))))) | |
481 | output)) | |
482 | ||
44e75f50 | 483 | ;;;###autoload |
88a76a1f | 484 | (defun describe-variable (variable &optional buffer frame) |
44e75f50 MB |
485 | "Display the full documentation of VARIABLE (a symbol). |
486 | Returns the documentation as a string, also. | |
88a76a1f KL |
487 | If VARIABLE has a buffer-local value in BUFFER or FRAME |
488 | \(default to the current buffer and current frame), | |
44e75f50 MB |
489 | it is displayed along with the global value." |
490 | (interactive | |
491 | (let ((v (variable-at-point)) | |
492 | (enable-recursive-minibuffers t) | |
493 | val) | |
494 | (setq val (completing-read (if (symbolp v) | |
495 | (format | |
496 | "Describe variable (default %s): " v) | |
497 | "Describe variable: ") | |
f192689e AM |
498 | obarray |
499 | '(lambda (vv) | |
500 | (or (boundp vv) | |
501 | (get vv 'variable-documentation))) | |
502 | t nil nil | |
44e75f50 MB |
503 | (if (symbolp v) (symbol-name v)))) |
504 | (list (if (equal val "") | |
505 | v (intern val))))) | |
d24c52bb | 506 | (unless (buffer-live-p buffer) (setq buffer (current-buffer))) |
88a76a1f | 507 | (unless (frame-live-p frame) (setq frame (selected-frame))) |
44e75f50 MB |
508 | (if (not (symbolp variable)) |
509 | (message "You did not specify a variable") | |
5a6a8d3b | 510 | (save-excursion |
88a76a1f | 511 | (let ((valvoid (not (with-current-buffer buffer (boundp variable)))) |
567c8878 | 512 | val val-start-pos locus) |
88a76a1f KL |
513 | ;; Extract the value before setting up the output buffer, |
514 | ;; in case `buffer' *is* the output buffer. | |
515 | (unless valvoid | |
516 | (with-selected-frame frame | |
517 | (with-current-buffer buffer | |
518 | (setq val (symbol-value variable) | |
519 | locus (variable-binding-locus variable))))) | |
5a6a8d3b RS |
520 | (help-setup-xref (list #'describe-variable variable buffer) |
521 | (interactive-p)) | |
aa13a094 | 522 | (with-help-window (help-buffer) |
5a6a8d3b RS |
523 | (with-current-buffer buffer |
524 | (prin1 variable) | |
136b8cbe RS |
525 | ;; Make a hyperlink to the library if appropriate. (Don't |
526 | ;; change the format of the buffer's initial line in case | |
527 | ;; anything expects the current format.) | |
528 | (let ((file-name (symbol-file variable 'defvar))) | |
9888177f | 529 | (setq file-name (describe-simplify-lib-file-name file-name)) |
136b8cbe RS |
530 | (when (equal file-name "loaddefs.el") |
531 | ;; Find the real def site of the preloaded variable. | |
532 | (let ((location | |
533 | (condition-case nil | |
534 | (find-variable-noselect variable file-name) | |
535 | (error nil)))) | |
536 | (when location | |
537 | (with-current-buffer (car location) | |
38586111 RS |
538 | (when (cdr location) |
539 | (goto-char (cdr location))) | |
136b8cbe RS |
540 | (when (re-search-backward |
541 | "^;;; Generated autoloads from \\(.*\\)" nil t) | |
542 | (setq file-name (match-string 1))))))) | |
543 | (when (and (null file-name) | |
544 | (integerp (get variable 'variable-documentation))) | |
545 | ;; It's a variable not defined in Elisp but in C. | |
546 | (setq file-name | |
547 | (if (get-buffer " *DOC*") | |
548 | (help-C-file-name variable 'var) | |
549 | 'C-source))) | |
550 | (if file-name | |
551 | (progn | |
552 | (princ " is a variable defined in `") | |
553 | (princ (if (eq file-name 'C-source) "C source code" file-name)) | |
554 | (princ "'.\n") | |
555 | (with-current-buffer standard-output | |
556 | (save-excursion | |
557 | (re-search-backward "`\\([^`']+\\)'" nil t) | |
558 | (help-xref-button 1 'help-variable-def | |
559 | variable file-name))) | |
560 | (if valvoid | |
e75b11f8 | 561 | (princ "It is void as a variable.") |
136b8cbe RS |
562 | (princ "Its "))) |
563 | (if valvoid | |
e75b11f8 | 564 | (princ " is void as a variable.") |
136b8cbe | 565 | (princ "'s ")))) |
26f19477 | 566 | (if valvoid |
136b8cbe | 567 | nil |
26f19477 | 568 | (with-current-buffer standard-output |
136b8cbe RS |
569 | (setq val-start-pos (point)) |
570 | (princ "value is ") | |
26f19477 SM |
571 | (terpri) |
572 | (let ((from (point))) | |
573 | (pp val) | |
1bd53977 NR |
574 | ;; Hyperlinks in variable's value are quite frequently |
575 | ;; inappropriate e.g C-h v <RET> features <RET> | |
576 | ;; (help-xref-on-pp from (point)) | |
26f19477 | 577 | (if (< (point) (+ from 20)) |
f63f0981 | 578 | (delete-region (1- from) from))))) |
5a6a8d3b | 579 | (terpri) |
136b8cbe | 580 | |
88a76a1f KL |
581 | (when locus |
582 | (if (bufferp locus) | |
583 | (princ (format "%socal in buffer %s; " | |
584 | (if (get variable 'permanent-local) | |
585 | "Permanently l" "L") | |
586 | (buffer-name))) | |
587 | (princ (format "It is a frame-local variable; "))) | |
5a6a8d3b RS |
588 | (if (not (default-boundp variable)) |
589 | (princ "globally void") | |
590 | (let ((val (default-value variable))) | |
591 | (with-current-buffer standard-output | |
592 | (princ "global value is ") | |
593 | (terpri) | |
594 | ;; Fixme: pp can take an age if you happen to | |
595 | ;; ask for a very large expression. We should | |
596 | ;; probably print it raw once and check it's a | |
597 | ;; sensible size before prettyprinting. -- fx | |
598 | (let ((from (point))) | |
599 | (pp val) | |
1bd53977 NR |
600 | ;; See previous comment for this function. |
601 | ;; (help-xref-on-pp from (point)) | |
5a6a8d3b | 602 | (if (< (point) (+ from 20)) |
a888f521 SM |
603 | (delete-region (1- from) from)))))) |
604 | (terpri)) | |
136b8cbe RS |
605 | |
606 | ;; If the value is large, move it to the end. | |
5a6a8d3b RS |
607 | (with-current-buffer standard-output |
608 | (when (> (count-lines (point-min) (point-max)) 10) | |
609 | ;; Note that setting the syntax table like below | |
610 | ;; makes forward-sexp move over a `'s' at the end | |
611 | ;; of a symbol. | |
612 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
136b8cbe | 613 | (goto-char val-start-pos) |
7fd09fb5 MR |
614 | ;; The line below previously read as |
615 | ;; (delete-region (point) (progn (end-of-line) (point))) | |
616 | ;; which suppressed display of the buffer local value for | |
617 | ;; large values. | |
618 | (when (looking-at "value is") (replace-match "")) | |
44e75f50 | 619 | (save-excursion |
136b8cbe RS |
620 | (insert "\n\nValue:") |
621 | (set (make-local-variable 'help-button-cache) | |
622 | (point-marker))) | |
623 | (insert "value is shown ") | |
624 | (insert-button "below" | |
625 | 'action help-button-cache | |
626 | 'follow-link t | |
627 | 'help-echo "mouse-2, RET: show value") | |
7fd09fb5 | 628 | (insert ".\n"))) |
ed2a19a1 | 629 | (terpri) |
136b8cbe | 630 | |
50a2c5f9 | 631 | (let* ((alias (condition-case nil |
d00a3408 | 632 | (indirect-variable variable) |
50a2c5f9 JB |
633 | (error variable))) |
634 | (obsolete (get variable 'byte-obsolete-variable)) | |
084a6638 | 635 | (safe-var (get variable 'safe-local-variable)) |
50a2c5f9 | 636 | (doc (or (documentation-property variable 'variable-documentation) |
ed2a19a1 SM |
637 | (documentation-property alias 'variable-documentation))) |
638 | (extra-line nil)) | |
639 | ;; Add a note for variables that have been make-var-buffer-local. | |
640 | (when (and (local-variable-if-set-p variable) | |
641 | (or (not (local-variable-p variable)) | |
642 | (with-temp-buffer | |
643 | (local-variable-if-set-p variable)))) | |
644 | (setq extra-line t) | |
645 | (princ " Automatically becomes buffer-local when set in any fashion.\n")) | |
646 | ||
647 | ;; Mention if it's an alias | |
d00a3408 | 648 | (unless (eq alias variable) |
ed2a19a1 SM |
649 | (setq extra-line t) |
650 | (princ (format " This variable is an alias for `%s'.\n" alias))) | |
cc86b31b | 651 | |
50a2c5f9 | 652 | (when obsolete |
ed2a19a1 SM |
653 | (setq extra-line t) |
654 | (princ " This variable is obsolete") | |
50a2c5f9 | 655 | (if (cdr obsolete) (princ (format " since %s" (cdr obsolete)))) |
a888f521 | 656 | (princ ";\n ") |
50a2c5f9 JB |
657 | (princ (if (stringp (car obsolete)) (car obsolete) |
658 | (format "use `%s' instead." (car obsolete)))) | |
50a2c5f9 | 659 | (terpri)) |
6fe7b8a4 | 660 | (when safe-var |
ed2a19a1 SM |
661 | (setq extra-line t) |
662 | (princ " This variable is safe as a file local variable ") | |
663 | (princ "if its value\n satisfies the predicate ") | |
6fe7b8a4 RS |
664 | (princ (if (byte-code-function-p safe-var) |
665 | "which is byte-compiled expression.\n" | |
cc86b31b | 666 | (format "`%s'.\n" safe-var)))) |
ed2a19a1 SM |
667 | |
668 | (if extra-line (terpri)) | |
669 | (princ "Documentation:\n") | |
fccd9537 GM |
670 | (with-current-buffer standard-output |
671 | (insert (or doc "Not documented as a variable.")))) | |
5a6a8d3b | 672 | ;; Make a link to customize if this variable can be customized. |
05849e2a JPW |
673 | (when (custom-variable-p variable) |
674 | (let ((customize-label "customize")) | |
675 | (terpri) | |
676 | (terpri) | |
677 | (princ (concat "You can " customize-label " this variable.")) | |
678 | (with-current-buffer standard-output | |
679 | (save-excursion | |
680 | (re-search-backward | |
681 | (concat "\\(" customize-label "\\)") nil t) | |
682 | (help-xref-button 1 'help-customize-variable variable)))) | |
683 | ;; Note variable's version or package version | |
684 | (let ((output (describe-variable-custom-version-info variable))) | |
685 | (when output | |
5a6a8d3b RS |
686 | (terpri) |
687 | (terpri) | |
05849e2a JPW |
688 | (princ output)))) |
689 | ||
5a6a8d3b RS |
690 | (save-excursion |
691 | (set-buffer standard-output) | |
692 | ;; Return the text we displayed. | |
693 | (buffer-string)))))))) | |
44e75f50 | 694 | |
44e75f50 | 695 | |
c477f688 SM |
696 | ;;;###autoload |
697 | (defun describe-syntax (&optional buffer) | |
c477f688 SM |
698 | "Describe the syntax specifications in the syntax table of BUFFER. |
699 | The descriptions are inserted in a help buffer, which is then displayed. | |
700 | BUFFER defaults to the current buffer." | |
240cbfca | 701 | (interactive) |
c477f688 SM |
702 | (setq buffer (or buffer (current-buffer))) |
703 | (help-setup-xref (list #'describe-syntax buffer) (interactive-p)) | |
aa13a094 | 704 | (with-help-window (help-buffer) |
c477f688 SM |
705 | (let ((table (with-current-buffer buffer (syntax-table)))) |
706 | (with-current-buffer standard-output | |
707 | (describe-vector table 'internal-describe-syntax-value) | |
708 | (while (setq table (char-table-parent table)) | |
709 | (insert "\nThe parent syntax table is:") | |
710 | (describe-vector table 'internal-describe-syntax-value)))))) | |
711 | ||
861d9ef6 SM |
712 | (defun help-describe-category-set (value) |
713 | (insert (cond | |
714 | ((null value) "default") | |
715 | ((char-table-p value) "deeper char-table ...") | |
716 | (t (condition-case err | |
717 | (category-set-mnemonics value) | |
718 | (error "invalid")))))) | |
719 | ||
720 | ;;;###autoload | |
721 | (defun describe-categories (&optional buffer) | |
722 | "Describe the category specifications in the current category table. | |
47f8b8f1 LT |
723 | The descriptions are inserted in a buffer, which is then displayed. |
724 | If BUFFER is non-nil, then describe BUFFER's category table instead. | |
725 | BUFFER should be a buffer or a buffer name." | |
861d9ef6 SM |
726 | (interactive) |
727 | (setq buffer (or buffer (current-buffer))) | |
728 | (help-setup-xref (list #'describe-categories buffer) (interactive-p)) | |
aa13a094 | 729 | (with-help-window (help-buffer) |
861d9ef6 SM |
730 | (let ((table (with-current-buffer buffer (category-table)))) |
731 | (with-current-buffer standard-output | |
732 | (describe-vector table 'help-describe-category-set) | |
733 | (let ((docs (char-table-extra-slot table 0))) | |
734 | (if (or (not (vectorp docs)) (/= (length docs) 95)) | |
735 | (insert "Invalid first extra slot in this char table\n") | |
736 | (insert "Meanings of mnemonic characters are:\n") | |
737 | (dotimes (i 95) | |
738 | (let ((elt (aref docs i))) | |
739 | (when elt | |
7a650da0 | 740 | (insert (+ i ?\s) ": " elt "\n")))) |
861d9ef6 SM |
741 | (while (setq table (char-table-parent table)) |
742 | (insert "\nThe parent category table is:") | |
743 | (describe-vector table 'help-describe-category-set)))))))) | |
744 | ||
125eb411 | 745 | (provide 'help-fns) |
44e75f50 | 746 | |
d4b4dfc6 | 747 | ;; arch-tag: 9e10331c-ae81-4d13-965d-c4819aaab0b3 |
125eb411 | 748 | ;;; help-fns.el ends here |