help-macro.el 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. ;;; help-macro.el --- makes command line help such as help-for-help
  2. ;; Copyright (C) 1993-1994, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Lynn Slater <lrs@indetech.com>
  4. ;; Maintainer: FSF
  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. ;;;###autoload
  57. (defcustom three-step-help nil
  58. "Non-nil means give more info about Help command in three steps.
  59. The three steps are simple prompt, prompt with all options, and
  60. window listing and describing the options.
  61. A value of nil means skip the middle step, so that \\[help-command] \\[help-command]
  62. gives the window that lists the options."
  63. :type 'boolean
  64. :group 'help)
  65. (defmacro make-help-screen (fname help-line help-text helped-map)
  66. "Construct help-menu function name FNAME.
  67. When invoked, FNAME shows HELP-LINE and reads a command using HELPED-MAP.
  68. If the command is the help character, FNAME displays HELP-TEXT
  69. and continues trying to read a command using HELPED-MAP.
  70. If HELP-TEXT contains the sequence `%THIS-KEY%', that is replaced
  71. with the key sequence that invoked FNAME.
  72. When FNAME finally does get a command, it executes that command
  73. and then returns."
  74. (let ((doc-fn (intern (concat (symbol-name fname) "-doc"))))
  75. `(progn
  76. (defun ,doc-fn () ,help-text nil)
  77. (defun ,fname ()
  78. "Help command."
  79. (interactive)
  80. (let ((line-prompt
  81. (substitute-command-keys ,help-line)))
  82. (when three-step-help
  83. (message "%s" line-prompt))
  84. (let* ((help-screen (documentation (quote ,doc-fn)))
  85. ;; We bind overriding-local-map for very small
  86. ;; sections, *excluding* where we switch buffers
  87. ;; and where we execute the chosen help command.
  88. (local-map (make-sparse-keymap))
  89. (new-minor-mode-map-alist minor-mode-map-alist)
  90. (prev-frame (selected-frame))
  91. config new-frame key char)
  92. (when (string-match "%THIS-KEY%" help-screen)
  93. (setq help-screen
  94. (replace-match (key-description
  95. (substring (this-command-keys) 0 -1))
  96. t t help-screen)))
  97. (unwind-protect
  98. (let ((minor-mode-map-alist nil))
  99. (setcdr local-map ,helped-map)
  100. (define-key local-map [t] 'undefined)
  101. ;; Make the scroll bar keep working normally.
  102. (define-key local-map [vertical-scroll-bar]
  103. (lookup-key global-map [vertical-scroll-bar]))
  104. (if three-step-help
  105. (progn
  106. (setq key (let ((overriding-local-map local-map))
  107. (read-key-sequence nil)))
  108. ;; Make the HELP key translate to C-h.
  109. (if (lookup-key function-key-map key)
  110. (setq key (lookup-key function-key-map key)))
  111. (setq char (aref key 0)))
  112. (setq char ??))
  113. (when (or (eq char ??) (eq char help-char)
  114. (memq char help-event-list))
  115. (setq config (current-window-configuration))
  116. (switch-to-buffer-other-window "*Help*")
  117. (and (fboundp 'make-frame)
  118. (not (eq (window-frame (selected-window))
  119. prev-frame))
  120. (setq new-frame (window-frame (selected-window))
  121. config nil))
  122. (setq buffer-read-only nil)
  123. (let ((inhibit-read-only t))
  124. (erase-buffer)
  125. (insert help-screen))
  126. (let ((minor-mode-map-alist new-minor-mode-map-alist))
  127. (help-mode)
  128. (setq new-minor-mode-map-alist minor-mode-map-alist))
  129. (goto-char (point-min))
  130. (while (or (memq char (append help-event-list
  131. (cons help-char '(?? ?\C-v ?\s ?\177 delete backspace vertical-scroll-bar ?\M-v))))
  132. (eq (car-safe char) 'switch-frame)
  133. (equal key "\M-v"))
  134. (condition-case nil
  135. (cond
  136. ((eq (car-safe char) 'switch-frame)
  137. (handle-switch-frame char))
  138. ((memq char '(?\C-v ?\s))
  139. (scroll-up))
  140. ((or (memq char '(?\177 ?\M-v delete backspace))
  141. (equal key "\M-v"))
  142. (scroll-down)))
  143. (error nil))
  144. (let ((cursor-in-echo-area t)
  145. (overriding-local-map local-map))
  146. (setq key (read-key-sequence
  147. (format "Type one of the options listed%s: "
  148. (if (pos-visible-in-window-p
  149. (point-max))
  150. "" ", or SPACE or DEL to scroll")))
  151. char (aref key 0)))
  152. ;; If this is a scroll bar command, just run it.
  153. (when (eq char 'vertical-scroll-bar)
  154. (command-execute (lookup-key local-map key) nil key))))
  155. ;; We don't need the prompt any more.
  156. (message "")
  157. ;; Mouse clicks are not part of the help feature,
  158. ;; so reexecute them in the standard environment.
  159. (if (listp char)
  160. (setq unread-command-events
  161. (cons char unread-command-events)
  162. config nil)
  163. (let ((defn (lookup-key local-map key)))
  164. (if defn
  165. (progn
  166. (when config
  167. (set-window-configuration config)
  168. (setq config nil))
  169. ;; Temporarily rebind `minor-mode-map-alist'
  170. ;; to `new-minor-mode-map-alist' (Bug#10454).
  171. (let ((minor-mode-map-alist new-minor-mode-map-alist))
  172. ;; `defn' must make sure that its frame is
  173. ;; selected, so we won't iconify it below.
  174. (call-interactively defn))
  175. (when new-frame
  176. ;; Do not iconify the selected frame.
  177. (unless (eq new-frame (selected-frame))
  178. (iconify-frame new-frame))
  179. (setq new-frame nil)))
  180. (ding)))))
  181. (when config
  182. (set-window-configuration config))
  183. (when new-frame
  184. (iconify-frame new-frame))
  185. (setq minor-mode-map-alist new-minor-mode-map-alist))))))))
  186. (provide 'help-macro)
  187. ;;; help-macro.el ends here