Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; help-macro.el --- makes command line help such as help-for-help |
465fceed | 2 | |
ab422c4d | 3 | ;; Copyright (C) 1993-1994, 2001-2013 Free Software Foundation, Inc. |
465fceed ER |
4 | |
5 | ;; Author: Lynn Slater <lrs@indetech.com> | |
4228277d | 6 | ;; Maintainer: FSF |
55535639 | 7 | ;; Created: Mon Oct 1 11:42:39 1990 |
465fceed | 8 | ;; Adapted-By: ESR |
bd78fa1d | 9 | ;; Package: emacs |
465fceed ER |
10 | |
11 | ;; This file is part of GNU Emacs. | |
12 | ||
eb3fa2cf | 13 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
465fceed | 14 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
15 | ;; the Free Software Foundation, either version 3 of the License, or |
16 | ;; (at your option) any later version. | |
465fceed ER |
17 | |
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. | |
22 | ||
23 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 24 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
465fceed ER |
25 | |
26 | ;;; Commentary: | |
b578f267 EN |
27 | |
28 | ;; This file supplies the macro make-help-screen which constructs | |
465fceed ER |
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 | |
55535639 | 31 | ;; example, the GNU Emacs Empire Tool uses this for every "nested" mode map |
465fceed ER |
32 | ;; called from the main mode map. |
33 | ||
3aee11cd ER |
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. | |
36 | ||
465fceed ER |
37 | ;;-> *********************** Example of use ********************************* |
38 | ||
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 | |
2ebf3469 | 46 | ;;-> based in part on empire-sector-civ-threshold |
465fceed ER |
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) | |
58 | ||
59 | ;;-> (define-key c-mp "\C-h" 'help-for-empire-redistribute-map) | |
60 | ;;-> (define-key c-mp help-character 'help-for-empire-redistribute-map) | |
61 | ||
62 | ;;; Change Log: | |
63 | ;; | |
2ebf3469 | 64 | ;; 22-Jan-1991 Lynn Slater x2048 |
465fceed ER |
65 | ;; Last Modified: Mon Oct 1 11:43:52 1990 #3 (Lynn Slater) |
66 | ;; documented better | |
67 | ||
68 | ;;; Code: | |
69 | ||
465fceed ER |
70 | (require 'backquote) |
71 | ||
05e153a6 GM |
72 | ;; This needs to be autoloaded because it is used in the |
73 | ;; make-help-screen macro. Using (bound-and-true-p three-step-help) | |
74 | ;; is not an acceptable alternative, because nothing loads help-macro | |
75 | ;; in a normal session, so any user customization would never be applied. | |
76 | ;;;###autoload | |
2ebf3469 | 77 | (defcustom three-step-help nil |
6c73d986 MR |
78 | "Non-nil means give more info about Help command in three steps. |
79 | The three steps are simple prompt, prompt with all options, and | |
80 | window listing and describing the options. | |
81 | A value of nil means skip the middle step, so that \\[help-command] \\[help-command] | |
82 | gives the window that lists the options." | |
2ebf3469 DL |
83 | :type 'boolean |
84 | :group 'help) | |
d189e346 | 85 | |
465fceed | 86 | (defmacro make-help-screen (fname help-line help-text helped-map) |
657a7909 RS |
87 | "Construct help-menu function name FNAME. |
88 | When invoked, FNAME shows HELP-LINE and reads a command using HELPED-MAP. | |
e31e7d37 | 89 | If the command is the help character, FNAME displays HELP-TEXT |
657a7909 | 90 | and continues trying to read a command using HELPED-MAP. |
527a0902 RS |
91 | If HELP-TEXT contains the sequence `%THIS-KEY%', that is replaced |
92 | with the key sequence that invoked FNAME. | |
657a7909 RS |
93 | When FNAME finally does get a command, it executes that command |
94 | and then returns." | |
6c73d986 MR |
95 | (let ((doc-fn (intern (concat (symbol-name fname) "-doc")))) |
96 | `(progn | |
ce9a0ccb | 97 | (defun ,doc-fn () ,help-text nil) |
6c73d986 MR |
98 | (defun ,fname () |
99 | "Help command." | |
100 | (interactive) | |
101 | (let ((line-prompt | |
102 | (substitute-command-keys ,help-line))) | |
103 | (when three-step-help | |
104 | (message "%s" line-prompt)) | |
105 | (let* ((help-screen (documentation (quote ,doc-fn))) | |
106 | ;; We bind overriding-local-map for very small | |
107 | ;; sections, *excluding* where we switch buffers | |
108 | ;; and where we execute the chosen help command. | |
109 | (local-map (make-sparse-keymap)) | |
110 | (new-minor-mode-map-alist minor-mode-map-alist) | |
111 | (prev-frame (selected-frame)) | |
112 | config new-frame key char) | |
113 | (when (string-match "%THIS-KEY%" help-screen) | |
114 | (setq help-screen | |
115 | (replace-match (key-description | |
116 | (substring (this-command-keys) 0 -1)) | |
117 | t t help-screen))) | |
118 | (unwind-protect | |
119 | (let ((minor-mode-map-alist nil)) | |
120 | (setcdr local-map ,helped-map) | |
121 | (define-key local-map [t] 'undefined) | |
122 | ;; Make the scroll bar keep working normally. | |
123 | (define-key local-map [vertical-scroll-bar] | |
124 | (lookup-key global-map [vertical-scroll-bar])) | |
125 | (if three-step-help | |
126 | (progn | |
127 | (setq key (let ((overriding-local-map local-map)) | |
128 | (read-key-sequence nil))) | |
129 | ;; Make the HELP key translate to C-h. | |
130 | (if (lookup-key function-key-map key) | |
131 | (setq key (lookup-key function-key-map key))) | |
132 | (setq char (aref key 0))) | |
133 | (setq char ??)) | |
134 | (when (or (eq char ??) (eq char help-char) | |
873dd80b | 135 | (memq char help-event-list)) |
6c73d986 | 136 | (setq config (current-window-configuration)) |
6ae57a67 | 137 | (pop-to-buffer " *Metahelp*" nil t) |
6c73d986 | 138 | (and (fboundp 'make-frame) |
290d5b58 | 139 | (not (eq (window-frame) |
6c73d986 | 140 | prev-frame)) |
290d5b58 | 141 | (setq new-frame (window-frame) |
6c73d986 MR |
142 | config nil)) |
143 | (setq buffer-read-only nil) | |
144 | (let ((inhibit-read-only t)) | |
145 | (erase-buffer) | |
146 | (insert help-screen)) | |
147 | (let ((minor-mode-map-alist new-minor-mode-map-alist)) | |
148 | (help-mode) | |
149 | (setq new-minor-mode-map-alist minor-mode-map-alist)) | |
150 | (goto-char (point-min)) | |
151 | (while (or (memq char (append help-event-list | |
152 | (cons help-char '(?? ?\C-v ?\s ?\177 delete backspace vertical-scroll-bar ?\M-v)))) | |
153 | (eq (car-safe char) 'switch-frame) | |
154 | (equal key "\M-v")) | |
155 | (condition-case nil | |
156 | (cond | |
157 | ((eq (car-safe char) 'switch-frame) | |
158 | (handle-switch-frame char)) | |
159 | ((memq char '(?\C-v ?\s)) | |
160 | (scroll-up)) | |
161 | ((or (memq char '(?\177 ?\M-v delete backspace)) | |
162 | (equal key "\M-v")) | |
163 | (scroll-down))) | |
164 | (error nil)) | |
165 | (let ((cursor-in-echo-area t) | |
166 | (overriding-local-map local-map)) | |
167 | (setq key (read-key-sequence | |
168 | (format "Type one of the options listed%s: " | |
169 | (if (pos-visible-in-window-p | |
170 | (point-max)) | |
171 | "" ", or SPACE or DEL to scroll"))) | |
172 | char (aref key 0))) | |
173 | ||
174 | ;; If this is a scroll bar command, just run it. | |
175 | (when (eq char 'vertical-scroll-bar) | |
176 | (command-execute (lookup-key local-map key) nil key)))) | |
177 | ;; We don't need the prompt any more. | |
178 | (message "") | |
179 | ;; Mouse clicks are not part of the help feature, | |
180 | ;; so reexecute them in the standard environment. | |
181 | (if (listp char) | |
182 | (setq unread-command-events | |
183 | (cons char unread-command-events) | |
184 | config nil) | |
185 | (let ((defn (lookup-key local-map key))) | |
186 | (if defn | |
187 | (progn | |
188 | (when config | |
189 | (set-window-configuration config) | |
190 | (setq config nil)) | |
dd6e3cdd MR |
191 | ;; Temporarily rebind `minor-mode-map-alist' |
192 | ;; to `new-minor-mode-map-alist' (Bug#10454). | |
193 | (let ((minor-mode-map-alist new-minor-mode-map-alist)) | |
194 | ;; `defn' must make sure that its frame is | |
195 | ;; selected, so we won't iconify it below. | |
196 | (call-interactively defn)) | |
6c73d986 MR |
197 | (when new-frame |
198 | ;; Do not iconify the selected frame. | |
199 | (unless (eq new-frame (selected-frame)) | |
200 | (iconify-frame new-frame)) | |
201 | (setq new-frame nil))) | |
202 | (ding))))) | |
203 | (when config | |
204 | (set-window-configuration config)) | |
205 | (when new-frame | |
206 | (iconify-frame new-frame)) | |
207 | (setq minor-mode-map-alist new-minor-mode-map-alist)))))))) | |
465fceed | 208 | |
2ebf3469 | 209 | (provide 'help-macro) |
465fceed | 210 | |
2ebf3469 | 211 | ;;; help-macro.el ends here |