ehelp.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. ;; Copyright (C) 1986 Free Software Foundation, Inc.
  2. ;; This file is part of GNU Emacs.
  3. ;; GNU Emacs is distributed in the hope that it will be useful,
  4. ;; but WITHOUT ANY WARRANTY. No author or distributor
  5. ;; accepts responsibility to anyone for the consequences of using it
  6. ;; or for whether it serves any particular purpose or works at all,
  7. ;; unless he says so in writing. Refer to the GNU Emacs General Public
  8. ;; License for full details.
  9. ;; Everyone is granted permission to copy, modify and redistribute
  10. ;; GNU Emacs, but only under the conditions described in the
  11. ;; GNU Emacs General Public License. A copy of this license is
  12. ;; supposed to have been given to you along with GNU Emacs so you
  13. ;; can know your rights and responsibilities. It should be in a
  14. ;; file named COPYING. Among other things, the copyright notice
  15. ;; and this notice must be preserved on all copies.
  16. (require 'electric)
  17. (provide 'ehelp)
  18. (defvar electric-help-map ()
  19. "Keymap defining commands available whilst scrolling
  20. through a buffer in electric-help-mode")
  21. (put 'electric-help-undefined 'suppress-keymap t)
  22. (if electric-help-map
  23. ()
  24. (let ((map (make-keymap)))
  25. (fillarray map 'electric-help-undefined)
  26. (define-key map (char-to-string meta-prefix-char) (copy-keymap map))
  27. (define-key map (char-to-string help-char) 'electric-help-help)
  28. (define-key map "?" 'electric-help-help)
  29. (define-key map " " 'scroll-up)
  30. (define-key map "\^?" 'scroll-down)
  31. (define-key map "." 'beginning-of-buffer)
  32. (define-key map "<" 'beginning-of-buffer)
  33. (define-key map ">" 'end-of-buffer)
  34. ;(define-key map "\C-g" 'electric-help-exit)
  35. (define-key map "q" 'electric-help-exit)
  36. (define-key map "Q" 'electric-help-exit)
  37. ;;a better key than this?
  38. (define-key map "r" 'electric-help-retain)
  39. (setq electric-help-map map)))
  40. (defun electric-help-mode ()
  41. "with-electric-help temporarily places its buffer in this mode
  42. \(On exit from with-electric-help, the buffer is put in default-major-mode)"
  43. (setq buffer-read-only t)
  44. (setq mode-name "Help")
  45. (setq major-mode 'help)
  46. (setq mode-line-buffer-identification '(" Help: %b"))
  47. (use-local-map electric-help-map)
  48. ;; this is done below in with-electric-help
  49. ;(run-hooks 'electric-help-mode-hook)
  50. )
  51. ;; Avoid error in Emacs versions before 18.37.
  52. (defun one-window-p (&optional nomini)
  53. (eq (selected-window)
  54. (if (and nomini (zerop (minibuffer-depth)))
  55. (next-window) (next-window (next-window)))))
  56. (defun with-electric-help (thunk &optional buffer noerase)
  57. "Arguments are THUNK &optional BUFFER NOERASE.
  58. BUFFER defaults to \"*Help*\"
  59. THUNK is a function of no arguments which is called to initialise
  60. the contents of BUFFER. BUFFER will be erased before THUNK is called unless
  61. NOERASE is non-nil. THUNK will be called with standard-output bound to
  62. the buffer specified by BUFFER
  63. After THUNK has been called, this function \"electrically\" pops up a window
  64. in which BUFFER is displayed and allows the user to scroll through that buffer
  65. in electric-help-mode.
  66. When the user exits (with electric-help-exit, or otherwise) the help
  67. buffer's window disappears (ie we use save-window-excursion)
  68. BUFFER is put into default-major-mode (or fundamental-mode) when we exit"
  69. (setq buffer (get-buffer-create (or buffer "*Help*")))
  70. (let ((one (one-window-p t))
  71. (two nil))
  72. (save-window-excursion
  73. (save-excursion
  74. (if one (goto-char (window-start (selected-window))))
  75. (let ((pop-up-windows t))
  76. (pop-to-buffer buffer))
  77. (unwind-protect
  78. (progn
  79. (save-excursion
  80. (set-buffer buffer)
  81. (electric-help-mode)
  82. (setq buffer-read-only nil)
  83. (or noerase (erase-buffer)))
  84. (let ((standard-output buffer))
  85. (if (funcall thunk)
  86. ()
  87. (set-buffer buffer)
  88. (set-buffer-modified-p nil)
  89. (goto-char (point-min))
  90. (if one (shrink-window-if-larger-than-buffer (selected-window)))))
  91. (set-buffer buffer)
  92. (run-hooks 'electric-help-mode-hook)
  93. (setq two (electric-help-command-loop))
  94. (cond ((eq (car-safe two) 'retain)
  95. (setq two (vector (window-height (selected-window))
  96. (window-start (selected-window))
  97. (window-hscroll (selected-window))
  98. (point))))
  99. (t (setq two nil))))
  100. (message "")
  101. (set-buffer buffer)
  102. (setq buffer-read-only nil)
  103. (condition-case ()
  104. (funcall (or default-major-mode 'fundamental-mode))
  105. (error nil)))))
  106. (if two
  107. (let ((pop-up-windows t)
  108. tem)
  109. (pop-to-buffer buffer)
  110. (setq tem (- (window-height (selected-window)) (elt two 0)))
  111. (if (> tem 0) (shrink-window tem))
  112. (set-window-start (selected-window) (elt two 1) t)
  113. (set-window-hscroll (selected-window) (elt two 2))
  114. (goto-char (elt two 3)))
  115. ;;>> Perhaps this shouldn't be done.
  116. ;; so that when we say "Press space to bury" we mean it
  117. (replace-buffer-in-windows buffer)
  118. ;; must do this outside of save-window-excursion
  119. (bury-buffer buffer))))
  120. (defun electric-help-command-loop ()
  121. (catch 'exit
  122. (if (pos-visible-in-window-p (point-max))
  123. (progn (message "<<< Press Space to bury the help buffer >>>")
  124. (if (= (setq unread-command-char (read-char)) ?\ )
  125. (progn (setq unread-command-char -1)
  126. (throw 'exit t)))))
  127. (let (up down both neither
  128. (standard (and (eq (key-binding " ")
  129. 'scroll-up)
  130. (eq (key-binding "\^?")
  131. 'scroll-down)
  132. (eq (key-binding "Q")
  133. 'electric-help-exit)
  134. (eq (key-binding "q")
  135. 'electric-help-exit))))
  136. (Electric-command-loop
  137. 'exit
  138. (function (lambda ()
  139. (let ((min (pos-visible-in-window-p (point-min)))
  140. (max (pos-visible-in-window-p (point-max))))
  141. (cond ((and min max)
  142. (cond (standard "Press Q to exit ")
  143. (neither)
  144. (t (setq neither (substitute-command-keys "Press \\[scroll-up] to exit ")))))
  145. (min
  146. (cond (standard "Press SPC to scroll, Q to exit ")
  147. (up)
  148. (t (setq up (substitute-command-keys "Press \\[scroll-up] to scroll; \\[electric-help-exit] to exit ")))))
  149. (max
  150. (cond (standard "Press DEL to scroll back, Q to exit ")
  151. (down)
  152. (t (setq down (substitute-command-keys "Press \\[scroll-down] to scroll back, \\[scroll-up] to exit ")))))
  153. (t
  154. (cond (standard "Press SPC to scroll, DEL to scroll back, Q to exit ")
  155. (both)
  156. (t (setq both (substitute-command-keys "Press \\[scroll-up] to scroll, \\[scroll-down] to scroll back, \\[electric-help-exit] to exit ")))))))))
  157. t))))
  158. ;(defun electric-help-scroll-up (arg)
  159. ; ">>>Doc"
  160. ; (interactive "P")
  161. ; (if (and (null arg) (pos-visible-in-window-p (point-max)))
  162. ; (electric-help-exit)
  163. ; (scroll-up arg)))
  164. (defun electric-help-exit ()
  165. ">>>Doc"
  166. (interactive)
  167. (throw 'exit t))
  168. (defun electric-help-retain ()
  169. "Exit electric-help, retaining the current window/buffer conifiguration.
  170. \(The *Help* buffer will not be selected, but \\[switch-to-buffer-other-window] RET
  171. will select it.)"
  172. (interactive)
  173. (throw 'exit '(retain)))
  174. ;(defun electric-help-undefined ()
  175. ; (interactive)
  176. ; (let* ((keys (this-command-keys))
  177. ; (n (length keys)))
  178. ; (if (or (= n 1)
  179. ; (and (= n 2)
  180. ; meta-flag
  181. ; (eq (aref keys 0) meta-prefix-char)))
  182. ; (setq unread-command-char last-input-char
  183. ; current-prefix-arg prefix-arg)
  184. ; ;;>>> I don't care.
  185. ; ;;>>> The emacs command-loop is too much pure pain to
  186. ; ;;>>> duplicate
  187. ; ))
  188. ; (throw 'exit t))
  189. (defun electric-help-undefined ()
  190. (interactive)
  191. (error "%s is undefined -- Press %s to exit"
  192. (mapconcat 'single-key-description (this-command-keys) " ")
  193. (if (eq (key-binding "Q") 'electric-help-exit)
  194. "Q"
  195. (substitute-command-keys "\\[electric-help-exit]"))))
  196. ;>>> this needs to be hairified (recursive help, anybody?)
  197. (defun electric-help-help ()
  198. (interactive)
  199. (if (and (eq (key-binding "Q") 'electric-help-exit)
  200. (eq (key-binding " ") 'scroll-up)
  201. (eq (key-binding "\^?") 'scroll-down))
  202. (message "SPC scrolls forward, DEL scrolls back, Q exits and burys help buffer")
  203. ;; to give something for user to look at while slow substitute-cmd-keys
  204. ;; grinds away
  205. (message "Help...")
  206. (message "%s" (substitute-command-keys "\\[scroll-up] scrolls forward, \\[scroll-down] scrolls back, \\[electric-help-exit] exits.")))
  207. (sit-for 2))
  208. (defun electric-helpify (fun)
  209. (let ((name "*Help*"))
  210. (if (save-window-excursion
  211. ;; kludge-o-rama
  212. (let* ((p (symbol-function 'print-help-return-message))
  213. (b (get-buffer name))
  214. (m (buffer-modified-p b)))
  215. (and b (not (get-buffer-window b))
  216. (setq b nil))
  217. (unwind-protect
  218. (progn
  219. (message "%s..." (capitalize (symbol-name fun)))
  220. ;; with-output-to-temp-buffer marks the buffer as unmodified.
  221. ;; kludging excessively and relying on that as some sort
  222. ;; of indication leads to the following abomination...
  223. ;;>> This would be doable without such icky kludges if either
  224. ;;>> (a) there were a function to read the interactive
  225. ;;>> args for a command and return a list of those args.
  226. ;;>> (To which one would then just apply the command)
  227. ;;>> (The only problem with this is that interactive-p
  228. ;;>> would break, but that is such a misfeature in
  229. ;;>> any case that I don't care)
  230. ;;>> It is easy to do this for emacs-lisp functions;
  231. ;;>> the only problem is getting the interactive spec
  232. ;;>> for subrs
  233. ;;>> (b) there were a function which returned a
  234. ;;>> modification-tick for a buffer. One could tell
  235. ;;>> whether a buffer had changed by whether the
  236. ;;>> modification-tick were different.
  237. ;;>> (Presumably there would have to be a way to either
  238. ;;>> restore the tick to some previous value, or to
  239. ;;>> suspend updating of the tick in order to allow
  240. ;;>> things like momentary-string-display)
  241. (and b
  242. (save-excursion
  243. (set-buffer b)
  244. (set-buffer-modified-p t)))
  245. (fset 'print-help-return-message 'ignore)
  246. (call-interactively fun)
  247. (and (get-buffer name)
  248. (get-buffer-window (get-buffer name))
  249. (or (not b)
  250. (not (eq b (get-buffer name)))
  251. (not (buffer-modified-p b)))))
  252. (fset 'print-help-return-message p)
  253. (and b (buffer-name b)
  254. (save-excursion
  255. (set-buffer b)
  256. (set-buffer-modified-p m))))))
  257. (with-electric-help 'ignore name t))))
  258. (defun electric-describe-key ()
  259. (interactive)
  260. (electric-helpify 'describe-key))
  261. (defun electric-describe-mode ()
  262. (interactive)
  263. (electric-helpify 'describe-mode))
  264. (defun electric-view-lossage ()
  265. (interactive)
  266. (electric-helpify 'view-lossage))
  267. ;(defun electric-help-for-help ()
  268. ; "See help-for-help"
  269. ; (interactive)
  270. ; )
  271. (defun electric-describe-function ()
  272. (interactive)
  273. (electric-helpify 'describe-function))
  274. (defun electric-describe-variable ()
  275. (interactive)
  276. (electric-helpify 'describe-variable))
  277. (defun electric-describe-bindings ()
  278. (interactive)
  279. (electric-helpify 'describe-bindings))
  280. (defun electric-describe-syntax ()
  281. (interactive)
  282. (electric-helpify 'describe-syntax))
  283. (defun electric-command-apropos ()
  284. (interactive)
  285. (electric-helpify 'command-apropos))
  286. ;(define-key help-map "a" 'electric-command-apropos)
  287. ;;;; ehelp-map
  288. (defvar ehelp-map ())
  289. (if ehelp-map
  290. nil
  291. (let ((map (copy-keymap help-map)))
  292. (substitute-key-definition 'describe-key 'electric-describe-key map)
  293. (substitute-key-definition 'describe-mode 'electric-describe-mode map)
  294. (substitute-key-definition 'view-lossage 'electric-view-lossage map)
  295. (substitute-key-definition 'describe-function 'electric-describe-function map)
  296. (substitute-key-definition 'describe-variable 'electric-describe-variable map)
  297. (substitute-key-definition 'describe-bindings 'electric-describe-bindings map)
  298. (substitute-key-definition 'describe-syntax 'electric-describe-syntax map)
  299. (setq ehelp-map map)
  300. (fset 'ehelp-command map)))
  301. ;; Do (define-key global-map "\C-h" 'ehelp-command) if you want to win