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