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