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