help-macro.el 8.1 KB

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