*** empty log message ***
[emacs.git] / lisp / emacs-lisp / helper.el
blobe3565856f56085041c67907e3a026bab5553ffdf
1 ;;; helper.el --- utility help package supporting help in electric modes
3 ;; Copyright (C) 1985 Free Software Foundation, Inc.
4 ;; Principal author K. Shane Hartman
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 1, or (at your option)
11 ;; any later version.
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to
20 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 ; hey, here's a helping hand.
25 ;; Bind this to a string for <blank> in "... Other keys <blank>".
26 ;; Helper-help uses this to construct help string when scrolling.
27 ;; Defaults to "return"
28 (defvar Helper-return-blurb nil)
30 ;; Keymap implementation doesn't work too well for non-standard loops.
31 ;; But define it anyway for those who can use it. Non-standard loops
32 ;; will probably have to use Helper-help. You can't autoload the
33 ;; keymap either.
36 (defvar Helper-help-map nil)
37 (if Helper-help-map
38 nil
39 (setq Helper-help-map (make-keymap))
40 ;(fillarray Helper-help-map 'undefined)
41 (define-key Helper-help-map "m" 'Helper-describe-mode)
42 (define-key Helper-help-map "b" 'Helper-describe-bindings)
43 (define-key Helper-help-map "c" 'Helper-describe-key-briefly)
44 (define-key Helper-help-map "k" 'Helper-describe-key)
45 ;(define-key Helper-help-map "f" 'Helper-describe-function)
46 ;(define-key Helper-help-map "v" 'Helper-describe-variable)
47 (define-key Helper-help-map "?" 'Helper-help-options)
48 (define-key Helper-help-map (char-to-string help-char) 'Helper-help-options)
49 (fset 'Helper-help-map Helper-help-map))
51 (defun Helper-help-scroller ()
52 (let ((blurb (or (and (boundp 'Helper-return-blurb)
53 Helper-return-blurb)
54 "return")))
55 (save-window-excursion
56 (goto-char (window-start (selected-window)))
57 (if (get-buffer-window "*Help*")
58 (pop-to-buffer "*Help*")
59 (switch-to-buffer "*Help*"))
60 (goto-char (point-min))
61 (let ((continue t) state)
62 (while continue
63 (setq state (+ (* 2 (if (pos-visible-in-window-p (point-max)) 1 0))
64 (if (pos-visible-in-window-p (point-min)) 1 0)))
65 (message
66 (nth state
67 '("Space forward, Delete back. Other keys %s"
68 "Space scrolls forward. Other keys %s"
69 "Delete scrolls back. Other keys %s"
70 "Type anything to %s"))
71 blurb)
72 (setq continue (read-char))
73 (cond ((and (memq continue '(?\ ?\C-v)) (< state 2))
74 (scroll-up))
75 ((= continue ?\C-l)
76 (recenter))
77 ((and (= continue ?\177) (zerop (% state 2)))
78 (scroll-down))
79 (t (setq continue nil))))))))
81 (defun Helper-help-options ()
82 "Describe help options."
83 (interactive)
84 (message "c (key briefly), m (mode), k (key), b (bindings)")
85 ;(message "c (key briefly), m (mode), k (key), v (variable), f (function)")
86 (sit-for 4))
88 (defun Helper-describe-key-briefly (key)
89 "Briefly describe binding of KEY."
90 (interactive "kDescribe key briefly: ")
91 (describe-key-briefly key)
92 (sit-for 4))
94 (defun Helper-describe-key (key)
95 "Describe binding of KEY."
96 (interactive "kDescribe key: ")
97 (save-window-excursion (describe-key key))
98 (Helper-help-scroller))
100 (defun Helper-describe-function ()
101 "Describe a function. Name read interactively."
102 (interactive)
103 (save-window-excursion (call-interactively 'describe-function))
104 (Helper-help-scroller))
106 (defun Helper-describe-variable ()
107 "Describe a variable. Name read interactively."
108 (interactive)
109 (save-window-excursion (call-interactively 'describe-variable))
110 (Helper-help-scroller))
112 (defun Helper-describe-mode ()
113 "Describe the current mode."
114 (interactive)
115 (let ((name mode-name)
116 (documentation (documentation major-mode)))
117 (save-excursion
118 (set-buffer (get-buffer-create "*Help*"))
119 (erase-buffer)
120 (insert name " Mode\n" documentation)))
121 (Helper-help-scroller))
123 ;;;###autoload
124 (defun Helper-describe-bindings ()
125 "Describe local key bindings of current mode."
126 (interactive)
127 (message "Making binding list...")
128 (save-window-excursion (describe-bindings))
129 (Helper-help-scroller))
131 ;;;###autoload
132 (defun Helper-help ()
133 "Provide help for current mode."
134 (interactive)
135 (let ((continue t) c)
136 (while continue
137 (message "Help (Type ? for further options)")
138 (setq c (char-to-string (downcase (read-char))))
139 (setq c (lookup-key Helper-help-map c))
140 (cond ((eq c 'Helper-help-options)
141 (Helper-help-options))
142 ((commandp c)
143 (call-interactively c)
144 (setq continue nil))
146 (ding)
147 (setq continue nil))))))
149 (provide 'helper)
151 ;;; helper.el ends here