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