* help.texi (Misc Help): Mention `describe-prefix-bindings' explicitly.
[emacs.git] / lisp / help-macro.el
blob8efb99d42d8d1c579e2f3c1b375ff84cf745bdd7
1 ;;; help-macro.el --- makes command line help such as help-for-help
3 ;; Copyright (C) 1993-1994, 2001-2011 Free Software Foundation, Inc.
5 ;; Author: Lynn Slater <lrs@indetech.com>
6 ;; Maintainer: FSF
7 ;; Created: Mon Oct 1 11:42:39 1990
8 ;; Adapted-By: ESR
9 ;; Package: emacs
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs 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 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; This file supplies the macro make-help-screen which constructs
29 ;; single character dispatching with browsable help such as that provided
30 ;; by help-for-help. This can be used to make many modes easier to use; for
31 ;; example, the GNU Emacs Empire Tool uses this for every "nested" mode map
32 ;; called from the main mode map.
34 ;; The name of this package was changed from help-screen.el to
35 ;; help-macro.el in order to fit in a 14-character limit.
37 ;;-> *********************** Example of use *********************************
39 ;;->(make-help-screen help-for-empire-redistribute-map
40 ;;-> "c:civ m:mil p:population f:food ?"
41 ;;-> "You have discovered the GEET redistribution commands
42 ;;-> From here, you can use the following options:
43 ;;->
44 ;;->c Redistribute civs from overfull sectors into connected underfull ones
45 ;;-> The functions typically named by empire-ideal-civ-fcn control
46 ;;-> based in part on empire-sector-civ-threshold
47 ;;->m Redistribute military using levels given by empire-ideal-mil-fcn
48 ;;->p Redistribute excess population to highways for max pop growth
49 ;;-> Excess is any sector so full babies will not be born.
50 ;;->f Even out food on highways to highway min and leave levels
51 ;;-> This is good to pump max food to all warehouses/dist pts
52 ;;->
53 ;;->
54 ;;->Use \\[help-for-empire-redistribute-map] for help on redistribution.
55 ;;->Use \\[help-for-empire-extract-map] for help on data extraction.
56 ;;->Please use \\[describe-key] to find out more about any of the other keys."
57 ;;-> empire-shell-redistribute-map)
59 ;;-> (define-key c-mp "\C-h" 'help-for-empire-redistribute-map)
60 ;;-> (define-key c-mp help-character 'help-for-empire-redistribute-map)
62 ;;; Change Log:
64 ;; 22-Jan-1991 Lynn Slater x2048
65 ;; Last Modified: Mon Oct 1 11:43:52 1990 #3 (Lynn Slater)
66 ;; documented better
68 ;;; Code:
70 (require 'backquote)
72 ;;;###autoload
73 (defcustom three-step-help nil
74 "Non-nil means give more info about Help command in three steps.
75 The three steps are simple prompt, prompt with all options, and
76 window listing and describing the options.
77 A value of nil means skip the middle step, so that \\[help-command] \\[help-command]
78 gives the window that lists the options."
79 :type 'boolean
80 :group 'help)
82 (defmacro make-help-screen (fname help-line help-text helped-map)
83 "Construct help-menu function name FNAME.
84 When invoked, FNAME shows HELP-LINE and reads a command using HELPED-MAP.
85 If the command is the help character, FNAME displays HELP-TEXT
86 and continues trying to read a command using HELPED-MAP.
87 If HELP-TEXT contains the sequence `%THIS-KEY%', that is replaced
88 with the key sequence that invoked FNAME.
89 When FNAME finally does get a command, it executes that command
90 and then returns."
91 (let ((doc-fn (intern (concat (symbol-name fname) "-doc"))))
92 `(progn
93 (defun ,doc-fn () ,help-text nil)
94 (defun ,fname ()
95 "Help command."
96 (interactive)
97 (let ((line-prompt
98 (substitute-command-keys ,help-line)))
99 (when three-step-help
100 (message "%s" line-prompt))
101 (let* ((help-screen (documentation (quote ,doc-fn)))
102 ;; We bind overriding-local-map for very small
103 ;; sections, *excluding* where we switch buffers
104 ;; and where we execute the chosen help command.
105 (local-map (make-sparse-keymap))
106 (new-minor-mode-map-alist minor-mode-map-alist)
107 (prev-frame (selected-frame))
108 config new-frame key char)
109 (when (string-match "%THIS-KEY%" help-screen)
110 (setq help-screen
111 (replace-match (key-description
112 (substring (this-command-keys) 0 -1))
113 t t help-screen)))
114 (unwind-protect
115 (let ((minor-mode-map-alist nil))
116 (setcdr local-map ,helped-map)
117 (define-key local-map [t] 'undefined)
118 ;; Make the scroll bar keep working normally.
119 (define-key local-map [vertical-scroll-bar]
120 (lookup-key global-map [vertical-scroll-bar]))
121 (if three-step-help
122 (progn
123 (setq key (let ((overriding-local-map local-map))
124 (read-key-sequence nil)))
125 ;; Make the HELP key translate to C-h.
126 (if (lookup-key function-key-map key)
127 (setq key (lookup-key function-key-map key)))
128 (setq char (aref key 0)))
129 (setq char ??))
130 (when (or (eq char ??) (eq char help-char)
131 (memq char help-event-list))
132 (setq config (current-window-configuration))
133 (switch-to-buffer-other-window "*Help*")
134 (and (fboundp 'make-frame)
135 (not (eq (window-frame (selected-window))
136 prev-frame))
137 (setq new-frame (window-frame (selected-window))
138 config nil))
139 (setq buffer-read-only nil)
140 (let ((inhibit-read-only t))
141 (erase-buffer)
142 (insert help-screen))
143 (let ((minor-mode-map-alist new-minor-mode-map-alist))
144 (help-mode)
145 (setq new-minor-mode-map-alist minor-mode-map-alist))
146 (goto-char (point-min))
147 (while (or (memq char (append help-event-list
148 (cons help-char '(?? ?\C-v ?\s ?\177 delete backspace vertical-scroll-bar ?\M-v))))
149 (eq (car-safe char) 'switch-frame)
150 (equal key "\M-v"))
151 (condition-case nil
152 (cond
153 ((eq (car-safe char) 'switch-frame)
154 (handle-switch-frame char))
155 ((memq char '(?\C-v ?\s))
156 (scroll-up))
157 ((or (memq char '(?\177 ?\M-v delete backspace))
158 (equal key "\M-v"))
159 (scroll-down)))
160 (error nil))
161 (let ((cursor-in-echo-area t)
162 (overriding-local-map local-map))
163 (setq key (read-key-sequence
164 (format "Type one of the options listed%s: "
165 (if (pos-visible-in-window-p
166 (point-max))
167 "" ", or SPACE or DEL to scroll")))
168 char (aref key 0)))
170 ;; If this is a scroll bar command, just run it.
171 (when (eq char 'vertical-scroll-bar)
172 (command-execute (lookup-key local-map key) nil key))))
173 ;; We don't need the prompt any more.
174 (message "")
175 ;; Mouse clicks are not part of the help feature,
176 ;; so reexecute them in the standard environment.
177 (if (listp char)
178 (setq unread-command-events
179 (cons char unread-command-events)
180 config nil)
181 (let ((defn (lookup-key local-map key)))
182 (if defn
183 (progn
184 (when config
185 (set-window-configuration config)
186 (setq config nil))
187 ;; `defn' must make sure that its frame is
188 ;; selected, so we won't iconify it below.
189 (call-interactively defn)
190 (when new-frame
191 ;; Do not iconify the selected frame.
192 (unless (eq new-frame (selected-frame))
193 (iconify-frame new-frame))
194 (setq new-frame nil)))
195 (ding)))))
196 (when config
197 (set-window-configuration config))
198 (when new-frame
199 (iconify-frame new-frame))
200 (setq minor-mode-map-alist new-minor-mode-map-alist))))))))
202 (provide 'help-macro)
204 ;;; help-macro.el ends here