Merge remote-tracking branch 'origin/master' into for-steve
[sxemacs] / lisp / help-macro.el
1 ;;; help-macro.el --- Makes command line help such as help-for-help
2
3 ;; Copyright (C) 1993, 1994, 1997 Free Software Foundation, Inc.
4
5 ;; Author: Lynn Slater <lrs@indetech.com>
6 ;; Maintainer: FSF
7 ;; Created: : Mon Oct  1 11:42:39 1990
8 ;; Adapted-By: ESR
9
10 ;; This file is part of SXEmacs.
11
12 ;; SXEmacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; SXEmacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;;; Synched up with: FSF 20.2.
28
29 ;; This file supplies the macro make-help-screen which constructs
30 ;; single character dispatching with browsable help such as that provided
31 ;; by help-for-help. This can be used to make many modes easier to use; for
32 ;; example, the Gnu Emacs Empire Tool uses this for every "nested" mode map
33 ;; called from the main mode map.
34
35 ;;       The name of this package was changed from help-screen.el to
36 ;; help-macro.el in order to fit in a 14-character limit.
37
38 ;;-> ***********************  Example of use *********************************
39
40 ;;->(make-help-screen help-for-empire-redistribute-map
41 ;;->              "c:civ m:mil p:population f:food ?"
42 ;;->              "You have discovered the GEET redistribution commands
43 ;;->   From here, you can use the following options:
44 ;;->
45 ;;->c   Redistribute civs from overfull sectors into connected underfull ones
46 ;;->      The functions typically named by empire-ideal-civ-fcn control
47 ;;->          based in part on empire-sector-civ-threshold
48 ;;->m   Redistribute military using levels given by empire-ideal-mil-fcn
49 ;;->p   Redistribute excess population to highways for max pop growth
50 ;;->      Excess is any sector so full babies will not be born.
51 ;;->f   Even out food on highways to highway min and leave levels
52 ;;->      This is good to pump max food to all warehouses/dist pts
53 ;;->
54 ;;->
55 ;;->Use \\[help-for-empire-redistribute-map] for help on redistribution.
56 ;;->Use \\[help-for-empire-extract-map] for help on data extraction.
57 ;;->Please use \\[describe-key] to find out more about any of the other keys."
58 ;;->              empire-shell-redistribute-map)
59
60 ;;->  (define-key c-mp "\C-h" 'help-for-empire-redistribute-map)
61 ;;->  (define-key c-mp help-character 'help-for-empire-redistribute-map)
62
63 ;;; Code:
64
65 (provide 'help-macro)
66
67 ;;;###autoload
68 (defcustom three-step-help t
69   "*Non-nil means give more info about Help command in three steps.
70 The three steps are simple prompt, prompt with all options,
71 and window listing and describing the options.
72 A value of nil means skip the middle step, so that
73 \\[help-command] \\[help-command] gives the window that lists the options."
74   :type 'boolean
75   :group 'help-appearance)
76
77 (defmacro make-help-screen (fname help-line help-text helped-map)
78   "Construct help-menu function name FNAME.
79 When invoked, FNAME shows HELP-LINE and reads a command using HELPED-MAP.
80 If the command is the help character, FNAME displays HELP-TEXT
81 and continues trying to read a command using HELPED-MAP.
82 When FNAME finally does get a command, it executes that command
83 and then returns."
84   `(defun ,fname ()
85      ,help-text
86      (interactive)
87      (flet ((help-read-key (prompt)
88               ;; This is in `flet' to avoid problems with autoloading.
89               ;; #### The function is ill-conceived -- there should be
90               ;; a way to do it without all the hassle!
91               (let (events)
92                 (while (not (key-press-event-p
93                              (aref (setq events (read-key-sequence prompt)) 0)))
94                   ;; Mouse clicks are not part of the help feature, so
95                   ;; reexecute them in the standard environment.
96                   (mapc 'dispatch-event events))
97                 (let ((key (nconc (event-modifiers (aref events 0))
98                                   (list (event-key (aref events 0))))))
99                   ;; Make the HELP key translate to C-h.
100                   (when (lookup-key function-key-map key)
101                     (setq key (lookup-key function-key-map key)))
102                   (if (eq (length key) 1)
103                       (car key)
104                     key)))))
105        (let ((line-prompt
106               (substitute-command-keys ,help-line)))
107          (when three-step-help
108            (message "%s" line-prompt))
109          (let* ((help-screen
110                  (condition-case nil
111                      (documentation (quote ,fname))
112                    (void-function "(alias for undefined function)")
113                    (error "(unexpected error from `documention')")))
114                 ;; We bind overriding-local-map for very small
115                 ;; sections, *excluding* where we switch buffers and
116                 ;; where we execute the chosen help command.
117                 (local-map (make-sparse-keymap))
118                 (minor-mode-map-alist nil)
119                 (prev-frame (selected-frame))
120                 config new-frame key)
121            (unwind-protect
122                (progn
123                  (set-keymap-parents local-map (list ,helped-map))
124                  (cond (three-step-help
125                         (let* ((overriding-local-map local-map))
126                           (setq key (help-read-key nil))))
127                        (t
128                         (setq key ??)))
129                  (when (or (equal key ??)
130                            (equal key (list help-char)))
131                    (setq config (current-window-configuration))
132                    (switch-to-buffer-other-window "*Help*")
133                    (and (not (eq (window-frame (selected-window))
134                                  prev-frame))
135                         (setq new-frame (window-frame (selected-window))
136                               config nil))
137                    (setq buffer-read-only nil)
138                    (erase-buffer)
139                    (insert help-screen)
140                    (help-mode)
141                    (goto-char (point-min))
142                    (while (member key `((,help-char) ?? (control v) space ?\177
143                                         delete backspace (meta v)))
144                      (ignore-errors
145                        (cond ((member key '((control v) space))
146                               (scroll-up))
147                              ((member key '(?\177 delete (meta v) backspace))
148                               (scroll-down))))
149                      (let ((cursor-in-echo-area t)
150                            (overriding-local-map local-map))
151                        (setq key (help-read-key
152                                   (format "Type one of the options listed%s: "
153                                           (if (pos-visible-in-window-p
154                                                (point-max))
155                                               "" " or Space to scroll")))))))
156                  ;; We don't need the prompt any more.
157                  (message nil)
158                  (let ((defn (lookup-key local-map key)))
159                    (cond (defn
160                            (when config
161                              (set-window-configuration config)
162                              (setq config nil))
163                            (when new-frame
164                              (iconify-frame new-frame)
165                              (setq new-frame nil))
166                            (call-interactively defn))
167                          (t
168                           (ding)))))
169              (and (get-buffer "*Help*")
170                   (bury-buffer "*Help*"))
171              (and new-frame (iconify-frame new-frame))
172              (and config
173                   (set-window-configuration config))))))))
174
175 ;;; help-macro.el