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