1 ;;; hyper-apropos.el --- Hypertext emacs lisp documentation interface.
3 ;; Copyright (C) 1997 Free Software Foundation, Inc.
4 ;; Copyright (C) 1994, 1995 Tinker Systems and INS Engineering Corp.
5 ;; Copyright (C) 1995 Sun Microsystems.
6 ;; Copyright (C) 1996 Ben Wing.
8 ;; Maintainer: Jonathan Stigelman <Stig@hackvan.com>
9 ;; Keywords: lisp, tools, help, docs, matching
11 ;; This file is part of XEmacs.
13 ;; XEmacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2 of the License, or
16 ;; (at your option) any later version.
18 ;; XEmacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with XEmacs; if not, write to the Free Software
25 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 ;;; Synched up with: Not in FSF.
31 ;; based upon emacs-apropos.el by Frank C. Guida <fcg@philabs.philips.com>
33 ;; Rather than run apropos and print all the documentation at once,
34 ;; I find it easier to view a "table of contents" first, then
35 ;; get the details for symbols as you need them.
37 ;; This version of apropos prints two lists of symbols matching the
38 ;; given regexp: functions/macros and variables/constants.
40 ;; The user can then do the following:
42 ;; - add an additional regexp to narrow the search
43 ;; - display documentation for the current symbol
44 ;; - find the tag for the current symbol
45 ;; - show any keybindings if the current symbol is a command
49 ;; An additional feature is the ability to search the current tags
50 ;; table, allowing you to interrogate functions not yet loaded (this
51 ;; isn't available with the standard package).
53 ;; Mouse bindings and menus are provided for XEmacs.
55 ;; additions by Ben Wing <ben@xemacs.org> July 1995:
56 ;; added support for function aliases, made programmer's apropos be the
57 ;; default, various other hacking.
58 ;; Massive changes by Christoph Wedler <wedler@fmi.uni-passau.de>
59 ;; Some changes for XEmacs 20.3 by hniksic
61 ;; #### The maintainer is supposed to be stig, but I haven't seen him
62 ;; around for ages. The real maintainer for the moment is Hrvoje
63 ;; Niksic <hniksic@xemacs.org>.
67 (defgroup hyper-apropos nil
68 "Hypertext emacs lisp documentation interface."
75 (defcustom hyper-apropos-show-brief-docs t
76 "*If non-nil, display some documentation in the \"*Hyper Apropos*\" buffer.
77 Setting this to nil will speed up searches."
79 :group 'hyper-apropos)
80 (define-obsolete-variable-alias
81 'hypropos-show-brief-docs 'hyper-apropos-show-brief-docs)
82 ;; I changed this to true because I think it's more useful this way. --ben
84 (defcustom hyper-apropos-programming-apropos t
85 "*If non-nil, list all the functions and variables.
86 This will cause more output to be generated, and take a longer time.
88 Otherwise, only the interactive functions and user variables will be listed."
90 :group 'hyper-apropos)
91 (define-obsolete-variable-alias
92 'hypropos-programming-apropos 'hyper-apropos-programming-apropos)
94 (defcustom hyper-apropos-shrink-window nil
95 "*If non-nil, shrink *Hyper Help* buffer if possible."
97 :group 'hyper-apropos)
98 (define-obsolete-variable-alias
99 'hypropos-shrink-window 'hyper-apropos-shrink-window)
101 (defcustom hyper-apropos-prettyprint-long-values t
102 "*If non-nil, then try to beautify the printing of very long values."
104 :group 'hyper-apropos)
105 (define-obsolete-variable-alias
106 'hypropos-prettyprint-long-values 'hyper-apropos-prettyprint-long-values)
108 (defgroup hyper-apropos-faces nil
109 "Faces defined by hyper-apropos."
110 :prefix "hyper-apropos-"
113 (defface hyper-apropos-documentation
114 '((((class color) (background light))
115 (:foreground "darkred"))
116 (((class color) (background dark))
117 (:foreground "gray90")))
118 "Hyper-apropos documentation."
119 :group 'hyper-apropos-faces)
121 (defface hyper-apropos-hyperlink
122 '((((class color) (background light))
123 (:foreground "blue4"))
124 (((class color) (background dark))
125 (:foreground "lightseagreen"))
128 "Hyper-apropos hyperlinks."
129 :group 'hyper-apropos-faces)
131 (defface hyper-apropos-major-heading '((t (:bold t)))
132 "Hyper-apropos major heading."
133 :group 'hyper-apropos-faces)
135 (defface hyper-apropos-section-heading '((t (:bold t :italic t)))
136 "Hyper-apropos section heading."
137 :group 'hyper-apropos-faces)
139 (defface hyper-apropos-heading '((t (:bold t)))
140 "Hyper-apropos heading."
141 :group 'hyper-apropos-faces)
143 (defface hyper-apropos-warning '((t (:bold t :foreground "red")))
144 "Hyper-apropos warning."
145 :group 'hyper-apropos-faces)
147 ;;; Internal variables below this point
149 (defvar hyper-apropos-ref-buffer)
150 (defvar hyper-apropos-prev-wconfig)
152 (defvar hyper-apropos-help-map
153 (let ((map (make-sparse-keymap)))
154 (suppress-keymap map)
155 (set-keymap-name map 'hyper-apropos-help-map)
157 (define-key map " " 'scroll-up)
158 (define-key map "b" 'scroll-down)
159 (define-key map [delete] 'scroll-down)
160 (define-key map [backspace] 'scroll-down)
161 (define-key map "/" 'isearch-forward)
162 (define-key map "?" 'isearch-backward)
164 (define-key map [return] 'hyper-apropos-get-doc)
165 (define-key map "s" 'hyper-apropos-set-variable)
166 (define-key map "t" 'hyper-apropos-find-tag)
167 (define-key map "l" 'hyper-apropos-last-help)
168 (define-key map "c" 'hyper-apropos-customize-variable)
169 (define-key map "f" 'hyper-apropos-find-function)
170 (define-key map [button2] 'hyper-apropos-mouse-get-doc)
171 (define-key map [button3] 'hyper-apropos-popup-menu)
172 ;; for the totally hardcore...
173 (define-key map "D" 'hyper-apropos-disassemble)
175 (define-key map "a" 'hyper-apropos)
176 (define-key map "n" 'hyper-apropos)
177 (define-key map "q" 'hyper-apropos-quit)
179 "Keybindings for the *Hyper Help* buffer and the *Hyper Apropos* buffer")
180 (define-obsolete-variable-alias
181 'hypropos-help-map 'hyper-apropos-help-map)
183 (defvar hyper-apropos-map
184 (let ((map (make-sparse-keymap)))
185 (set-keymap-name map 'hyper-apropos-map)
186 (set-keymap-parents map (list hyper-apropos-help-map))
187 ;; slightly different scrolling...
188 (define-key map " " 'hyper-apropos-scroll-up)
189 (define-key map "b" 'hyper-apropos-scroll-down)
190 (define-key map [delete] 'hyper-apropos-scroll-down)
191 (define-key map [backspace] 'hyper-apropos-scroll-down)
192 ;; act on the current line...
193 (define-key map "w" 'hyper-apropos-where-is)
194 (define-key map "i" 'hyper-apropos-invoke-fn)
195 ;; this is already defined in the parent-keymap above, isn't it?
196 ;; (define-key map "s" 'hyper-apropos-set-variable)
197 ;; more administrativa...
198 (define-key map "P" 'hyper-apropos-toggle-programming-flag)
199 (define-key map "k" 'hyper-apropos-add-keyword)
200 (define-key map "e" 'hyper-apropos-eliminate-keyword)
202 "Keybindings for the *Hyper Apropos* buffer.
203 This map inherits from `hyper-apropos-help-map.'")
204 (define-obsolete-variable-alias
205 'hypropos-map 'hyper-apropos-map)
207 ;;(defvar hyper-apropos-mousable-keymap
208 ;; (let ((map (make-sparse-keymap)))
209 ;; (define-key map [button2] 'hyper-apropos-mouse-get-doc)
212 (defvar hyper-apropos-mode-hook nil
213 "*User function run after hyper-apropos mode initialization. Usage:
214 \(add-hook 'hyper-apropos-mode-hook #'(lambda () ... your init forms ...)).")
216 ;; ---------------------------------------------------------------------- ;;
218 (defconst hyper-apropos-junk-regexp
219 "^Apropos\\|^Functions\\|^Variables\\|^$")
221 (defvar hyper-apropos-currently-showing nil) ; symbol documented in
223 (defvar hyper-apropos-help-history nil) ; chain of symbols followed as links in
225 (defvar hyper-apropos-face-history nil)
226 ;;;(defvar hyper-apropos-variable-history nil)
227 ;;;(defvar hyper-apropos-function-history nil)
228 (defvar hyper-apropos-regexp-history nil)
229 (defvar hyper-apropos-last-regexp nil) ; regex used for last apropos
230 (defconst hyper-apropos-apropos-buf "*Hyper Apropos*")
231 (defconst hyper-apropos-help-buf "*Hyper Help*")
234 (defun hyper-apropos (regexp toggle-apropos)
235 "Display lists of functions and variables matching REGEXP
236 in buffer \"*Hyper Apropos*\". If optional prefix arg is given, then the
237 value of `hyper-apropos-programming-apropos' is toggled for this search.
238 See also `hyper-apropos-mode'."
239 (interactive (list (read-from-minibuffer "List symbols matching regexp: "
240 nil nil nil 'hyper-apropos-regexp-history)
242 (or (memq major-mode '(hyper-apropos-mode hyper-apropos-help-mode))
243 (setq hyper-apropos-prev-wconfig (current-window-configuration)))
244 (if (string= "" regexp)
245 (if (get-buffer hyper-apropos-apropos-buf)
247 (setq regexp hyper-apropos-last-regexp)
249 (hyper-apropos-toggle-programming-flag)
250 (message "Using last search results")))
251 (error "Be more specific..."))
252 (set-buffer (get-buffer-create hyper-apropos-apropos-buf))
253 (setq buffer-read-only nil)
256 (if (local-variable-p 'hyper-apropos-programming-apropos
258 (setq hyper-apropos-programming-apropos
259 (not hyper-apropos-programming-apropos))
260 (set (make-local-variable 'hyper-apropos-programming-apropos)
261 (not (default-value 'hyper-apropos-programming-apropos)))))
262 (let ((flist (apropos-internal regexp
263 (if hyper-apropos-programming-apropos
266 (vlist (apropos-internal regexp
267 (if hyper-apropos-programming-apropos
269 #'user-variable-p))))
270 (insert-face (format "Apropos search for: %S\n\n" regexp)
271 'hyper-apropos-major-heading)
272 (insert-face "* = command (M-x) or user-variable.\n"
273 'hyper-apropos-documentation)
275 a = autoloaded, b = byte-compiled, i = internal, l = lambda, m = macro.\n\n"
276 'hyper-apropos-documentation)
277 (insert-face "Functions and Macros:\n\n" 'hyper-apropos-major-heading)
278 (hyper-apropos-grok-functions flist)
279 (insert-face "\n\nVariables and Constants:\n\n"
280 'hyper-apropos-major-heading)
281 (hyper-apropos-grok-variables vlist)
282 (goto-char (point-min))))
283 (switch-to-buffer hyper-apropos-apropos-buf)
284 (hyper-apropos-mode regexp))
286 (defun hyper-apropos-toggle-programming-flag ()
288 (with-current-buffer hyper-apropos-apropos-buf
289 (set (make-local-variable 'hyper-apropos-programming-apropos)
290 (not hyper-apropos-programming-apropos)))
291 (message "Re-running apropos...")
292 (hyper-apropos hyper-apropos-last-regexp nil))
294 (defun hyper-apropos-grok-functions (fns)
297 (setq bind (symbol-function fn)
298 type (cond ((subrp bind) ?i)
299 ((compiled-function-p bind) ?b)
300 ((consp bind) (or (cdr
301 (assq (car bind) '((autoload . ?a)
306 (insert type (if (commandp fn) "* " " "))
307 (let ((e (insert-face (format "%S" fn) 'hyper-apropos-hyperlink)))
308 (set-extent-property e 'mouse-face 'highlight))
309 (insert-char ?\ (let ((l (- 30 (length (format "%S" fn)))))
310 (if (natnump l) l 0)))
311 (and hyper-apropos-show-brief-docs
313 ;; A symbol's function slot can point to an unbound symbol.
314 ;; In that case, `documentation' will fail.
317 (void-function "(alias for undefined function)")
318 (error "(unexpected error from `documentation')")))
320 "^([^\n\t )]+[\t ]*\\([^\n)]+\\)?)\\(:[\t ]*\\|\n?\\'\\)"
322 (setq doc (substring doc (match-end 0) (string-match "\n" doc)))
326 (substring doc 0 (string-match "\n" doc)))
328 'hyper-apropos-documentation))
331 (defun hyper-apropos-grok-variables (vars)
334 (setq userp (user-variable-p var))
335 (insert (if userp " * " " "))
336 (let ((e (insert-face (format "%S" var) 'hyper-apropos-hyperlink)))
337 (set-extent-property e 'mouse-face 'highlight))
338 (insert-char ?\ (let ((l (- 30 (length (format "%S" var)))))
339 (if (natnump l) l 0)))
340 (and hyper-apropos-show-brief-docs
341 (setq doc (documentation-property var 'variable-documentation))
343 (concat " - " (substring doc (if userp 1 0)
344 (string-match "\n" doc)))
345 " - Not documented.")
346 'hyper-apropos-documentation))
349 ;; ---------------------------------------------------------------------- ;;
351 (defun hyper-apropos-mode (regexp)
352 "Improved apropos mode for displaying Emacs documentation. Function and
353 variable names are displayed in the buffer \"*Hyper Apropos*\".
355 Functions are preceded by a single character to indicates their types:
356 a = autoloaded, b = byte-compiled, i = internal, l = lambda, m = macro.
357 Interactive functions are also preceded by an asterisk.
358 Variables are preceded by an asterisk if they are user variables.
362 SPC - scroll documentation or apropos window forward
363 b - scroll documentation or apropos window backward
364 k - eliminate all hits that don't contain keyword
367 q - quit and restore previous window configuration
369 Operations for Symbol on Current Line:
371 RET - toggle display of symbol's documentation
372 (also on button2 in xemacs)
373 w - show the keybinding if symbol is a command
374 i - invoke function on current line
375 s - set value of variable on current line
376 t - display the C or lisp source (find-tag)"
377 (delete-other-windows)
378 (setq mode-name "Hyper-Apropos"
379 major-mode 'hyper-apropos-mode
382 hyper-apropos-last-regexp regexp
383 modeline-buffer-identification
384 (list (cons modeline-buffer-id-left-extent "Hyper Apropos: ")
385 (cons modeline-buffer-id-right-extent (concat "\"" regexp "\""))))
386 (use-local-map hyper-apropos-map)
387 (run-hooks 'hyper-apropos-mode-hook))
389 ;; ---------------------------------------------------------------------- ;;
391 ;; similar to `describe-key-briefly', copied from help.el by CW
394 (defun hyper-describe-key (key)
395 (interactive "kDescribe key: ")
396 (hyper-describe-key-briefly key t))
399 (defun hyper-describe-key-briefly (key &optional show)
400 (interactive "kDescribe key briefly: \nP")
401 (let (menup defn interm final msg)
402 (setq defn (key-or-menu-binding key 'menup))
403 (if (or (null defn) (integerp defn))
404 (or (numberp show) (message "%s is undefined" (key-description key)))
405 (cond ((stringp defn)
407 final (key-binding defn)))
409 (setq interm (append defn nil))
411 (member (key-binding (vector (car interm)))
412 '(universal-argument digit-argument)))
413 (setq interm (cdr interm)))
415 (not (setq final (key-binding (vconcat interm)))))
416 (setq interm (butlast interm)))
418 (setq interm (vconcat interm))
420 final (key-binding defn)))))
423 ;; This used to say 'This menu item' but it could also
424 ;; be a scrollbar event. We can't distinguish at the
426 (if menup "This item" (key-description key))
427 ;;(if (symbolp defn) defn (key-description defn))
428 (if (symbolp defn) defn (prin1-to-string defn))
429 (if final (concat ", " (key-description interm) " runs ") "")
431 (if (symbolp final) final (prin1-to-string final))
434 (or (not (symbolp defn))
435 (memq (symbol-function defn)
436 '(zkey-init-kbd-macro zkey-init-kbd-fn))
437 (progn (princ msg) (princ "\n")))
439 (if final (setq defn final))
440 (if (and (or (symbolp defn) (symbolp (setq defn (car-safe defn))))
443 (hyper-apropos-get-doc defn t))
444 (or (memq major-mode '(hyper-apropos-mode hyper-apropos-help-mode))
445 (setq hyper-apropos-prev-wconfig (current-window-configuration)))))))
448 (defun hyper-describe-face (symbol &optional this-ref-buffer)
450 See also `hyper-apropos' and `hyper-describe-function'."
451 ;; #### - perhaps a prefix arg should suppress the prompt...
454 (setq v (hyper-apropos-this-symbol)) ; symbol under point
456 (setq v (variable-at-point)))
457 (setq val (let ((enable-recursive-minibuffers t))
459 (concat (if (hyper-apropos-follow-ref-buffer current-prefix-arg)
463 (format " (default %s): " v)
465 (mapcar #'(lambda (x) (list (symbol-name x)))
467 nil t nil 'hyper-apropos-face-history
468 (and v (symbol-name v)))))
469 (list (intern-soft val)
470 current-prefix-arg)))
472 (message "Sorry, nothing to describe.")
473 (or (memq major-mode '(hyper-apropos-mode hyper-apropos-help-mode))
474 (setq hyper-apropos-prev-wconfig (current-window-configuration)))
475 (hyper-apropos-get-doc symbol t nil this-ref-buffer)))
478 (defun hyper-describe-variable (symbol &optional this-ref-buffer)
479 "Hypertext drop-in replacement for `describe-variable'.
480 See also `hyper-apropos' and `hyper-describe-function'."
481 ;; #### - perhaps a prefix arg should suppress the prompt...
482 (interactive (list (hyper-apropos-read-variable-symbol
483 (if (hyper-apropos-follow-ref-buffer current-prefix-arg)
485 "Describe variable"))
488 (message "Sorry, nothing to describe.")
489 (or (memq major-mode '(hyper-apropos-mode hyper-apropos-help-mode))
490 (setq hyper-apropos-prev-wconfig (current-window-configuration)))
491 (hyper-apropos-get-doc symbol t nil this-ref-buffer)))
494 (defun hyper-where-is (symbol)
495 "Print message listing key sequences that invoke specified command."
496 (interactive (list (hyper-apropos-read-function-symbol "Where is function")))
498 (message "Sorry, nothing to describe.")
502 (defun hyper-describe-function (symbol &optional this-ref-buffer)
503 "Hypertext replacement for `describe-function'. Unlike `describe-function'
504 in that the symbol under the cursor is the default if it is a function.
505 See also `hyper-apropos' and `hyper-describe-variable'."
506 ;; #### - perhaps a prefix arg should suppress the prompt...
507 (interactive (list (hyper-apropos-read-function-symbol
508 (if (hyper-apropos-follow-ref-buffer current-prefix-arg)
510 "Describe function"))
513 (message "Sorry, nothing to describe.")
514 (or (memq major-mode '(hyper-apropos-mode hyper-apropos-help-mode))
515 (setq hyper-apropos-prev-wconfig (current-window-configuration)))
516 (hyper-apropos-get-doc symbol t nil this-ref-buffer)))
519 (defun hyper-apropos-read-variable-symbol (prompt &optional predicate)
520 "Hypertext drop-in replacement for `describe-variable'.
521 See also `hyper-apropos' and `hyper-describe-function'."
522 ;; #### - perhaps a prefix arg should suppress the prompt...
523 (or predicate (setq predicate 'boundp))
525 (setq v (hyper-apropos-this-symbol)) ; symbol under point
526 (or (funcall predicate v)
527 (setq v (variable-at-point)))
528 (or (funcall predicate v)
530 (setq val (let ((enable-recursive-minibuffers t))
534 (format " (default %s): " v)
536 obarray predicate t nil 'variable-history
537 (and v (symbol-name v)))))
541 (define-obsolete-function-alias
542 'hypropos-read-variable-symbol 'hyper-apropos-read-variable-symbol)
544 (defun hyper-apropos-read-function-symbol (prompt)
545 "Read function symbol from minibuffer."
546 (let ((fn (hyper-apropos-this-symbol))
549 (setq fn (function-at-point)))
550 (setq val (let ((enable-recursive-minibuffers t))
551 (completing-read (if fn
552 (format "%s (default %s): " prompt fn)
553 (format "%s: " prompt))
554 obarray 'fboundp t nil
556 (and fn (symbol-name fn)))))
559 (defun hyper-apropos-last-help (arg)
560 "Go back to the last symbol documented in the *Hyper Help* buffer."
562 (let ((win (get-buffer-window hyper-apropos-help-buf)))
563 (or arg (setq arg (if win 1 0)))
565 ((<= (length hyper-apropos-help-history) arg)
566 ;; go back as far as we can...
567 (setcdr (nreverse hyper-apropos-help-history) nil))
569 (setq hyper-apropos-help-history
570 (nthcdr arg hyper-apropos-help-history))))
571 (if (or win (> arg 0))
572 (hyper-apropos-get-doc (car hyper-apropos-help-history) t)
573 (display-buffer hyper-apropos-help-buf))))
575 (defun hyper-apropos-insert-face (string &optional face)
576 "Insert STRING and fontify some parts with face `hyper-apropos-hyperlink'."
577 (let ((beg (point)) end)
578 (insert-face string (or face 'hyper-apropos-documentation))
581 (while (re-search-forward
582 "`\\([-a-zA-Z0-9_][-a-zA-Z0-9_][-a-zA-Z0-9_.]+\\)'"
584 (let ((e (make-extent (match-beginning 1) (match-end 1))))
585 (set-extent-face e 'hyper-apropos-hyperlink)
586 (set-extent-property e 'mouse-face 'highlight)))
588 (while (re-search-forward
589 "M-x \\([-a-zA-Z0-9_][-a-zA-Z0-9_][-a-zA-Z0-9_.]+\\)"
591 (let ((e (make-extent (match-beginning 1) (match-end 1))))
592 (set-extent-face e 'hyper-apropos-hyperlink)
593 (set-extent-property e 'mouse-face 'highlight)))))
595 (defun hyper-apropos-insert-keybinding (keys string)
597 (insert " (" string " bound to \""
598 (mapconcat 'key-description
599 (sort* keys #'< :key #'length)
603 (defun hyper-apropos-insert-section-heading (alias-desc &optional desc)
604 (or desc (setq desc alias-desc
607 (setq desc (concat alias-desc
608 (if (memq (aref desc 0)
612 (aset desc 0 (upcase (aref desc 0))) ; capitalize
613 (goto-char (point-max))
614 (newline 3) (delete-blank-lines) (newline 2)
615 (hyper-apropos-insert-face desc 'hyper-apropos-section-heading))
617 (defun hyper-apropos-insert-value (string symbol val)
618 (insert-face string 'hyper-apropos-heading)
619 (insert (if (symbol-value symbol)
620 (if (or (null val) (eq val t) (integerp val))
622 (symbol-value symbol)
627 (defun hyper-apropos-follow-ref-buffer (this-ref-buffer)
628 (and (not this-ref-buffer)
629 (eq major-mode 'hyper-apropos-help-mode)
630 hyper-apropos-ref-buffer
631 (buffer-live-p hyper-apropos-ref-buffer)))
633 (defun hyper-apropos-get-alias (symbol alias-p next-symbol &optional use)
634 "Return (TERMINAL-SYMBOL . ALIAS-DESC)."
636 (while (funcall alias-p symbol)
637 (setq aliases (cons (if use (funcall use symbol) symbol) aliases))
638 (setq symbol (funcall next-symbol symbol)))
641 (concat "an alias for `"
642 (mapconcat 'symbol-name
644 "',\nwhich is an alias for `")
647 (defun hyper-apropos-get-doc (&optional symbol force type this-ref-buffer)
648 ;; #### - update this docstring
649 "Toggle display of documentation for the symbol on the current line."
650 ;; SYMBOL is the symbol to document. FORCE, if non-nil, means to
651 ;; regenerate the documentation even if it already seems to be there. And
652 ;; TYPE, if present, forces the generation of only variable documentation
653 ;; or only function documentation. Normally, if both are present, then
654 ;; both will be generated.
656 ;; TYPES TO IMPLEMENT: obsolete face
660 (setq symbol (hyper-apropos-this-symbol)))
662 (setq type '(function variable face)))
663 (if (and (eq hyper-apropos-currently-showing symbol)
664 (get-buffer hyper-apropos-help-buf)
665 (get-buffer-window hyper-apropos-help-buf)
667 ;; we're already displaying this help, so toggle its display.
668 (delete-windows-on hyper-apropos-help-buf)
669 ;; OK, we've got to refresh and display it...
670 (or (eq symbol (car hyper-apropos-help-history))
671 (setq hyper-apropos-help-history
672 (if (eq major-mode 'hyper-apropos-help-mode)
673 ;; if we're following a link in the help buffer, then
674 ;; record that in the help history.
675 (cons symbol hyper-apropos-help-history)
676 ;; otherwise clear the history because it's a new search.
679 (if (hyper-apropos-follow-ref-buffer this-ref-buffer)
680 (set-buffer hyper-apropos-ref-buffer)
681 (setq hyper-apropos-ref-buffer (current-buffer)))
682 (let (standard-output
684 newsym symtype doc obsolete
686 global local-str global-str
688 aliases alias-desc desc)
690 (set-buffer (get-buffer-create hyper-apropos-help-buf))
691 ;;(setq standard-output (current-buffer))
692 (setq buffer-read-only nil)
694 (insert-face (format "`%s'" symbol) 'hyper-apropos-major-heading)
695 (insert (format " (buffer: %s, mode: %s)\n"
696 (buffer-name hyper-apropos-ref-buffer)
698 ;; function ----------------------------------------------------------
699 (and (memq 'function type)
703 (setq aliases (hyper-apropos-get-alias (symbol-function symbol)
707 alias-desc (cdr aliases))
708 (if (eq 'macro (car-safe newsym))
711 (setq desc "function"))
712 (setq symtype (cond ((subrp newsym) 'subr)
713 ((compiled-function-p newsym) 'bytecode)
714 ((eq (car-safe newsym) 'autoload) 'autoload)
715 ((eq (car-safe newsym) 'lambda) 'lambda))
716 desc (concat (if (commandp symbol) "interactive ")
718 '((subr . "built-in ")
719 (bytecode . "compiled Lisp ")
720 (autoload . "autoloaded Lisp ")
721 (lambda . "Lisp "))))
724 ((autoload) (format ",\n(autoloaded from \"%s\")"
726 ((bytecode) (format ",\n(loaded from \"%s\")"
727 (symbol-file symbol)))))
728 local (current-local-map)
729 global (current-global-map)
730 obsolete (get symbol 'byte-obsolete-info)
731 doc (or (condition-case nil
732 (documentation symbol)
734 "(alias for undefined function)")
735 (error "(unexpected error from `documention')"))
736 "function not documented"))
738 (set-buffer hyper-apropos-help-buf)
739 (goto-char (point-max))
740 (setq standard-output (current-buffer))
741 (hyper-apropos-insert-section-heading alias-desc desc)
744 (hyper-apropos-insert-keybinding
745 (where-is-internal symbol (list local) nil nil nil)
747 (hyper-apropos-insert-keybinding
748 (where-is-internal symbol (list global) nil nil nil)
752 (hyper-apropos-insert-face
753 (format "%s is an obsolete function; %s\n\n" symbol
754 (if (stringp (car obsolete))
756 (format "use `%s' instead." (car obsolete))))
757 'hyper-apropos-warning))
759 (insert-face "arguments: " 'hyper-apropos-heading)
760 (cond ((eq symtype 'lambda)
761 (princ (or (nth 1 newsym) "()")))
762 ((eq symtype 'bytecode)
763 (princ (or (compiled-function-arglist newsym)
765 ((and (eq symtype 'subr)
767 "[\n\t ]*\narguments: ?\\((.*)\\)\n?\\'"
769 (insert (substring doc
772 (setq doc (substring doc 0 (match-beginning 0))))
773 ((and (eq symtype 'subr)
776 \[\n\t ]*([^\n\t )]+[\t ]*\\([^\n)]+\\)?)\\(:[\t ]*\\|\n?\\'\\)"
784 (setq doc (substring doc (match-end 0))))
785 (t (princ "[not available]")))
787 (hyper-apropos-insert-face doc)
789 (indent-rigidly beg (point) 2))))
790 ;; variable ----------------------------------------------------------
791 (and (memq 'variable type)
792 (or (boundp symbol) (default-boundp symbol))
795 (setq aliases (hyper-apropos-get-alias symbol
800 alias-desc (cdr aliases))
801 (setq symtype (or (local-variable-p newsym (current-buffer))
802 (and (local-variable-p newsym
805 desc (concat (and (get newsym 'custom-type)
807 (if (user-variable-p newsym)
810 (cond ((eq symtype t) ", buffer-local")
811 ((eq symtype 'auto-local)
812 ", local when set")))
813 local (and (boundp newsym)
814 (symbol-value newsym))
815 local-str (and (boundp newsym)
816 (prin1-to-string local))
817 global (and (eq symtype t)
818 (default-boundp newsym)
819 (default-value newsym))
820 global-str (and (eq symtype t)
821 (default-boundp newsym)
822 (prin1-to-string global))
823 obsolete (get symbol 'byte-obsolete-variable)
824 doc (or (documentation-property symbol
825 'variable-documentation)
826 "variable not documented"))
828 (set-buffer hyper-apropos-help-buf)
829 (goto-char (point-max))
830 (setq standard-output (current-buffer))
831 (hyper-apropos-insert-section-heading alias-desc desc)
832 (when (and (user-variable-p newsym)
833 (get newsym 'custom-type))
834 (let ((e (make-extent (point-at-bol) (point))))
835 (set-extent-property e 'mouse-face 'highlight)
836 (set-extent-property e 'help-echo
837 (format "Customize %s" newsym))
839 e 'hyper-apropos-custom
840 `(lambda () (customize-variable (quote ,newsym))))))
844 (hyper-apropos-insert-face
845 (format "%s is an obsolete function; %s\n\n" symbol
846 (if (stringp obsolete)
848 (format "use `%s' instead." obsolete)))
849 'hyper-apropos-warning))
850 ;; generally, the value of the variable is short and the
851 ;; documentation of the variable long, so it's desirable
852 ;; to see all of the value and the start of the
853 ;; documentation. Some variables, though, have huge and
854 ;; nearly meaningless values that force you to page
855 ;; forward just to find the doc string. That is
857 (if (and (or (null local-str) (< (length local-str) 69))
858 (or (null global-str) (< (length global-str) 69)))
859 ; 80 cols. docstrings assume this.
860 (progn (insert-face "value: " 'hyper-apropos-heading)
861 (insert (or local-str "is void"))
865 (insert-face "default value: " 'hyper-apropos-heading)
866 (insert (or global-str "is void"))))
868 (hyper-apropos-insert-face doc))
869 (hyper-apropos-insert-value "value: " 'local-str local)
873 (hyper-apropos-insert-value "default-value: "
874 'global-str global)))
876 (hyper-apropos-insert-face doc)
879 (newline 3) (delete-blank-lines) (newline 1)
880 (insert-face "value: " 'hyper-apropos-heading)
881 (if hyper-apropos-prettyprint-long-values
883 (cl-prettyprint local)
884 (error (insert local-str)))
885 (insert local-str))))
888 (newline 3) (delete-blank-lines) (newline 1)
889 (insert-face "default value: " 'hyper-apropos-heading)
890 (if hyper-apropos-prettyprint-long-values
892 (cl-prettyprint global)
893 (error (insert global-str)))
894 (insert global-str)))))
895 (indent-rigidly beg (point) 2))))
896 ;; face --------------------------------------------------------------
897 (and (memq 'face type)
901 (copy-face symbol 'hyper-apropos-temp-face 'global)
902 (mapcar #'(lambda (property)
903 (setq symtype (face-property-instance symbol
906 (set-face-property 'hyper-apropos-temp-face
909 built-in-face-specifiers)
910 (setq font (cons (face-property-instance symbol 'font nil 0 t)
911 (face-property-instance symbol 'font))
912 fore (cons (face-foreground-instance symbol nil 0 t)
913 (face-foreground-instance symbol))
914 back (cons (face-background-instance symbol nil 0 t)
915 (face-background-instance symbol))
916 undl (cons (face-underline-p symbol nil 0 t)
917 (face-underline-p symbol))
918 doc (face-doc-string symbol))
919 ;; #### - add some code here
921 (set-buffer hyper-apropos-help-buf)
922 (setq standard-output (current-buffer))
923 (hyper-apropos-insert-section-heading
925 (when (get symbol 'face-defface-spec)
926 (let* ((str " (customizable)")
927 (e (make-extent 1 (length str) str)))
928 (set-extent-property e 'mouse-face 'highlight)
929 (set-extent-property e 'help-echo
930 (format "Customize %s" symbol))
931 (set-extent-property e 'unique t)
932 (set-extent-property e 'duplicable t)
934 e 'hyper-apropos-custom
935 `(lambda () (customize-face (quote ,symbol))))
939 ABCDEFHIJKLMNOPQRSTUVWXYZ abcdefhijklmnopqrstuvwxyz 0123456789"
940 'hyper-apropos-temp-face)
942 (insert-face " Font: " 'hyper-apropos-heading)
943 (insert (format (if (numberp (car font)) "(%s)\n" "%s\n")
945 (font-instance-name (cdr font)))))
946 (insert-face " Foreground: " 'hyper-apropos-heading)
947 (insert (format (if (numberp (car fore)) "(%s)\n" "%s\n")
949 (color-instance-name (cdr fore)))))
950 (insert-face " Background: " 'hyper-apropos-heading)
951 (insert (format (if (numberp (car back)) "(%s)\n" "%s\n")
953 (color-instance-name (cdr back)))))
954 (insert-face " Underline: " 'hyper-apropos-heading)
955 (insert (format (if (numberp (car undl)) "(%s)\n" "%s\n")
962 (indent-rigidly beg (point) 2))))))
963 ;; not bound & property list -----------------------------------------
966 (set-buffer hyper-apropos-help-buf)
967 (hyper-apropos-insert-section-heading
968 "symbol is not currently bound\n")))
969 (if (and (setq symtype (symbol-plist symbol))
970 (or (> (length symtype) 2)
971 (not (memq 'variable-documentation symtype))))
973 (set-buffer hyper-apropos-help-buf)
974 (goto-char (point-max))
975 (setq standard-output (current-buffer))
976 (hyper-apropos-insert-section-heading "property-list:\n\n")
978 (if (memq (car symtype)
979 '(variable-documentation byte-obsolete-info))
980 (setq symtype (cdr symtype))
981 (insert-face (concat " " (symbol-name (car symtype))
983 'hyper-apropos-heading)
984 (setq symtype (cdr symtype))
986 (insert (prin1-to-string (car symtype)) "\n"))
987 (setq symtype (cdr symtype)))))))
989 (set-buffer hyper-apropos-help-buf)
990 (goto-char (point-min))
991 ;; pop up window and shrink it if it's wasting space
992 (if hyper-apropos-shrink-window
993 (shrink-window-if-larger-than-buffer
994 (display-buffer (current-buffer)))
995 (display-buffer (current-buffer)))
996 (hyper-apropos-help-mode))
997 (setq hyper-apropos-currently-showing symbol)))
999 (define-obsolete-function-alias
1000 'hypropos-get-doc 'hyper-apropos-get-doc)
1002 ; -----------------------------------------------------------------------------
1004 (defun hyper-apropos-help-mode ()
1005 "Major mode for hypertext XEmacs help. In this mode, you can quickly
1006 follow links between back and forth between the documentation strings for
1007 different variables and functions. Common commands:
1009 \\{hyper-apropos-help-map}"
1010 (setq buffer-read-only t
1011 major-mode 'hyper-apropos-help-mode
1012 mode-name "Hyper-Help")
1013 (set-syntax-table emacs-lisp-mode-syntax-table)
1014 (use-local-map hyper-apropos-help-map))
1016 ;; ---------------------------------------------------------------------- ;;
1018 (defun hyper-apropos-scroll-up ()
1019 "Scroll up the \"*Hyper Help*\" buffer if it's visible.
1020 Otherwise, scroll the selected window up."
1022 (let ((win (get-buffer-window hyper-apropos-help-buf))
1023 (owin (selected-window)))
1029 (error (goto-char (point-max))))
1030 (select-window owin))
1033 (defun hyper-apropos-scroll-down ()
1034 "Scroll down the \"*Hyper Help*\" buffer if it's visible.
1035 Otherwise, scroll the selected window down."
1037 (let ((win (get-buffer-window hyper-apropos-help-buf))
1038 (owin (selected-window)))
1044 (error (goto-char (point-max))))
1045 (select-window owin))
1046 (scroll-down nil))))
1048 ;; ---------------------------------------------------------------------- ;;
1050 (defun hyper-apropos-mouse-get-doc (event)
1051 "Get the documentation for the symbol the mouse is on."
1053 (mouse-set-point event)
1054 (let ((e (extent-at (point) nil 'hyper-apropos-custom)))
1056 (funcall (extent-property e 'hyper-apropos-custom))
1058 (let ((symbol (hyper-apropos-this-symbol)))
1060 (hyper-apropos-get-doc symbol)
1061 (error "Click on a symbol")))))))
1063 ;; ---------------------------------------------------------------------- ;;
1065 (defun hyper-apropos-add-keyword (pattern)
1066 "Use additional keyword to narrow regexp match.
1067 Deletes lines which don't match PATTERN."
1068 (interactive "sAdditional Keyword: ")
1070 (goto-char (point-min))
1071 (let (buffer-read-only)
1072 (keep-lines (concat pattern "\\|" hyper-apropos-junk-regexp))
1075 (defun hyper-apropos-eliminate-keyword (pattern)
1076 "Use additional keyword to eliminate uninteresting matches.
1077 Deletes lines which match PATTERN."
1078 (interactive "sKeyword to eliminate: ")
1080 (goto-char (point-min))
1081 (let (buffer-read-only)
1082 (flush-lines pattern))
1085 ;; ---------------------------------------------------------------------- ;;
1087 (defun hyper-apropos-this-symbol ()
1089 (cond ((eq major-mode 'hyper-apropos-mode)
1091 (if (looking-at hyper-apropos-junk-regexp)
1094 (read (point-marker))))
1095 ;; What's this? This ends up in the same symbol already described.
1097 ;; (eq major-mode 'hyper-apropos-help-mode)
1098 ;; (> (point) (point-min)))
1100 ;; (goto-char (point-min))
1101 ;; (hyper-apropos-this-symbol)))
1104 (skip-syntax-backward "w_")
1105 ;; !@(*$^%%# stupid backquote implementation!!!
1106 (skip-chars-forward "`")
1109 (skip-syntax-forward "w_")
1110 (skip-chars-backward ".':") ; : for Local Variables
1112 (and (not (eq st en))
1113 (intern-soft (buffer-substring st en))))))))
1115 (defun hyper-apropos-where-is (symbol)
1116 "Find keybinding for symbol on current line."
1117 (interactive (list (hyper-apropos-this-symbol)))
1120 (defun hyper-apropos-invoke-fn (fn)
1121 "Interactively invoke the function on the current line."
1122 (interactive (list (hyper-apropos-this-symbol)))
1123 (cond ((not (fboundp fn))
1124 (error "%S is not a function" fn))
1125 (t (call-interactively fn))))
1128 (defun hyper-set-variable (var val &optional this-ref-buffer)
1130 (let ((var (hyper-apropos-read-variable-symbol
1131 (if (hyper-apropos-follow-ref-buffer current-prefix-arg)
1132 "In ref buffer, set user option"
1135 (list var (hyper-apropos-read-variable-value var) current-prefix-arg)))
1136 (hyper-apropos-set-variable var val this-ref-buffer))
1139 (defun hyper-apropos-set-variable (var val &optional this-ref-buffer)
1140 "Interactively set the variable on the current line."
1142 (let ((var (hyper-apropos-this-symbol)))
1143 (or (and var (boundp var))
1145 (list var (hyper-apropos-read-variable-value var))))
1149 (if (hyper-apropos-follow-ref-buffer this-ref-buffer)
1151 (set-buffer hyper-apropos-ref-buffer)
1154 (hyper-apropos-get-doc var t '(variable) this-ref-buffer))))
1156 (define-obsolete-function-alias
1157 'hypropos-set-variable 'hyper-apropos-set-variable)
1159 (defun hyper-apropos-read-variable-value (var &optional this-ref-buffer)
1162 (let ((prop (get var 'variable-interactive))
1165 (hyper-apropos-get-doc var t '(variable) current-prefix-arg)
1167 (call-interactively (list 'lambda '(arg)
1168 (list 'interactive prop)
1170 (setq val (if (hyper-apropos-follow-ref-buffer this-ref-buffer)
1172 (set-buffer hyper-apropos-ref-buffer)
1175 str (prin1-to-string val))
1177 (format "Set %s `%s' to value (evaluated): "
1178 (if (user-variable-p var) "user option" "Variable")
1183 (format (if (or (consp val)
1185 (not (memq val '(t nil)))))
1190 (defun hyper-apropos-customize-variable ()
1192 (let ((var (hyper-apropos-this-symbol)))
1194 (or (and var (boundp var))
1196 (customize-variable var))))
1198 ;; ---------------------------------------------------------------------- ;;
1200 (defun hyper-apropos-find-tag (&optional tag-name)
1201 "Find the tag for the symbol on the current line in other window. In
1202 order for this to work properly, the variable `tag-table-alist' or
1203 `tags-file-name' must be set so that a TAGS file with tags for the emacs
1204 source is found for the \"*Hyper Apropos*\" buffer."
1206 ;; there ought to be a default tags file for this...
1207 (or tag-name (setq tag-name (symbol-name (hyper-apropos-this-symbol))))
1208 (find-tag-other-window (list tag-name)))
1210 ;; ---------------------------------------------------------------------- ;;
1212 (defun hyper-apropos-find-function (fn)
1213 "Find the function for the symbol on the current line in other
1214 window. (See also `find-function'.)"
1216 (let ((fn (hyper-apropos-this-symbol)))
1221 (find-function-other-window fn)))
1223 ;; ---------------------------------------------------------------------- ;;
1225 (defun hyper-apropos-disassemble (sym)
1226 "Disassemble FUN if it is byte-coded. If it's a lambda, prettyprint it."
1227 (interactive (list (hyper-apropos-this-symbol)))
1228 (let ((fun sym) (trail nil) macrop)
1229 (while (and (symbolp fun) (not (memq fun trail)))
1230 (setq trail (cons fun trail)
1231 fun (symbol-function fun)))
1233 (error "Loop detected in function binding of `%s'" fun))
1234 (setq macrop (and (consp fun)
1235 (eq 'macro (car fun))))
1236 (cond ((compiled-function-p (if macrop (cdr fun) fun))
1238 (set-buffer "*Disassemble*")
1239 (goto-char (point-min))
1241 (insert (format " for function `%S'" sym))
1244 (with-current-buffer "*Disassemble*"
1245 (cl-prettyprint (if macrop
1246 (cons 'defmacro (cons sym (cdr (cdr fun))))
1247 (cons 'defun (cons sym (cdr fun))))))
1248 (set-buffer "*Disassemble*")
1250 ((or (vectorp fun) (stringp fun))
1251 ;; #### - do something fancy here
1252 (with-output-to-temp-buffer "*Disassemble*"
1253 (princ (format "%s is a keyboard macro:\n\n\t" sym))
1256 (error "Sorry, cannot disassemble `%s'" sym)))))
1258 ;; ---------------------------------------------------------------------- ;;
1260 (defun hyper-apropos-quit ()
1262 "Quit Hyper Apropos and restore original window config."
1263 (let ((buf (get-buffer hyper-apropos-apropos-buf)))
1264 (and buf (bury-buffer buf)))
1265 (set-window-configuration hyper-apropos-prev-wconfig))
1267 ;; ---------------------------------------------------------------------- ;;
1270 (defun hyper-apropos-popup-menu (event)
1272 (mouse-set-point event)
1273 (let* ((sym (hyper-apropos-this-symbol))
1274 (notjunk (not (null sym)))
1275 (command-p (if (commandp sym) t))
1276 (variable-p (and sym (boundp sym)))
1277 (customizable-p (and variable-p
1278 (get sym 'custom-type)
1280 (function-p (fboundp sym))
1281 (apropos-p (eq 'hyper-apropos-mode
1282 (save-excursion (set-buffer (event-buffer event))
1284 (name (if sym (symbol-name sym) ""))
1288 (list (concat "Hyper-Help: " name)
1289 (vector "Display documentation" 'hyper-apropos-get-doc notjunk)
1290 (vector "Set variable" 'hyper-apropos-set-variable variable-p)
1291 (vector "Customize variable" 'hyper-apropos-customize-variable
1293 (vector "Show keys for" 'hyper-apropos-where-is command-p)
1294 (vector "Invoke command" 'hyper-apropos-invoke-fn command-p)
1295 (vector "Find function" 'hyper-apropos-find-function function-p)
1296 (vector "Find tag" 'hyper-apropos-find-tag notjunk)
1298 ["Add keyword..." hyper-apropos-add-keyword t])
1300 ["Eliminate keyword..." hyper-apropos-eliminate-keyword t])
1302 ["Programmers' Apropos" hyper-apropos-toggle-programming-flag
1303 :style toggle :selected hyper-apropos-programming-apropos]
1304 ["Programmers' Help" hyper-apropos-toggle-programming-flag
1305 :style toggle :selected hyper-apropos-programming-apropos])
1306 (and hyper-apropos-programming-apropos
1307 (vector "Disassemble function"
1308 'hyper-apropos-disassemble
1310 ["Help" describe-mode t]
1311 ["Quit" hyper-apropos-quit t]
1313 (popup-menu hyper-apropos-menu)))
1315 (define-obsolete-function-alias
1316 'hypropos-popup-menu 'hyper-apropos-popup-menu)
1318 (provide 'hyper-apropos)
1320 ;; end of hyper-apropos.el