comparison lisp/help-macro.el @ 211:78478c60bfcd r20-4b4

Import from CVS: tag r20-4b4
author cvs
date Mon, 13 Aug 2007 10:05:51 +0200
parents
children 262b8bb4a523
comparison
equal deleted inserted replaced
210:49f55ca3ba57 211:78478c60bfcd
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 (defun help-read-key (prompt)
80 (let (events)
81 (while (not (key-press-event-p
82 (aref (setq events (read-key-sequence prompt)) 0)))
83 ;; Mouse clicks are not part of the help feature, so reexecute
84 ;; them in the standard environment.
85 (mapc 'dispatch-event events))
86 (let ((key (nconc (event-modifiers (aref events 0))
87 (list (event-key (aref events 0))))))
88 ;; Make the HELP key translate to C-h.
89 (when (lookup-key function-key-map key)
90 (setq key (lookup-key function-key-map key)))
91 (if (eq (length key) 1)
92 (car key)
93 key))))
94
95 (defmacro make-help-screen (fname help-line help-text helped-map)
96 "Construct help-menu function name FNAME.
97 When invoked, FNAME shows HELP-LINE and reads a command using HELPED-MAP.
98 If the command is the help character, FNAME displays HELP-TEXT
99 and continues trying to read a command using HELPED-MAP.
100 When FNAME finally does get a command, it executes that command
101 and then returns."
102 `(defun ,fname ()
103 ,help-text
104 (interactive)
105 (let ((line-prompt
106 (substitute-command-keys ,help-line)))
107 (when three-step-help
108 (message "%s" line-prompt))
109 (let* ((help-screen (documentation (quote ,fname)))
110 ;; We bind overriding-local-map for very small
111 ;; sections, *excluding* where we switch buffers
112 ;; and where we execute the chosen help command.
113 (local-map (make-sparse-keymap))
114 (minor-mode-map-alist nil)
115 (prev-frame (selected-frame))
116 config new-frame key)
117 (unwind-protect
118 (progn
119 (set-keymap-parents local-map (list ,helped-map))
120 (cond (three-step-help
121 (let* ((overriding-local-map local-map))
122 (setq key (help-read-key nil))))
123 (t
124 (setq key ??)))
125 (when (or (equal key ??)
126 (equal key (list help-char)))
127 (setq config (current-window-configuration))
128 (switch-to-buffer-other-window "*Help*")
129 (and (not (eq (window-frame (selected-window))
130 prev-frame))
131 (setq new-frame (window-frame (selected-window))
132 config nil))
133 (setq buffer-read-only nil)
134 (erase-buffer)
135 (insert help-screen)
136 (help-mode)
137 (goto-char (point-min))
138 (while (member key `((,help-char) ?? (control v) space ?\177
139 delete backspace (meta v)))
140 (ignore-errors
141 (cond ((member key '((control v) space))
142 (scroll-up))
143 ((member key '(?\177 delete (meta v) backspace))
144 (scroll-down))))
145 (let ((cursor-in-echo-area t)
146 (overriding-local-map local-map))
147 (setq key (help-read-key
148 (format "Type one of the options listed%s: "
149 (if (pos-visible-in-window-p
150 (point-max))
151 "" " or Space to scroll")))))))
152 ;; We don't need the prompt any more.
153 (message nil)
154 (let ((defn (lookup-key local-map key)))
155 (cond (defn
156 (when config
157 (set-window-configuration config)
158 (setq config nil))
159 (when new-frame
160 (iconify-frame new-frame)
161 (setq new-frame nil))
162 (call-interactively defn))
163 (t
164 (ding)))))
165 (and (get-buffer "*Help*")
166 (bury-buffer "*Help*"))
167 (and new-frame (iconify-frame new-frame))
168 (and config
169 (set-window-configuration config)))))))
170
171 ;;; help-macro.el
172