ehelp.el 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. ;;; ehelp.el --- bindings for electric-help mode -*- lexical-binding: t -*-
  2. ;; Copyright (C) 1986, 1995, 2000-2015 Free Software Foundation, Inc.
  3. ;; Author: Richard Mlynarik
  4. ;; (according to ack.texi and authors.el)
  5. ;; Maintainer: emacs-devel@gnu.org
  6. ;; Keywords: help, extensions
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This package provides a pre-packaged `Electric Help Mode' for
  20. ;; browsing Emacs help screens. There is one entry point,
  21. ;; `with-electric-help'; all you have to give it is a no-argument
  22. ;; function that generates the actual text of the help into the current
  23. ;; buffer.
  24. ;; To make this the default, you must do
  25. ;; (require 'ehelp)
  26. ;; (define-key global-map "\C-h" 'ehelp-command)
  27. ;; (define-key global-map [help] 'ehelp-command)
  28. ;; (define-key global-map [f1] 'ehelp-command)
  29. ;;; Code:
  30. (require 'electric)
  31. (defvar electric-help-form-to-execute nil)
  32. (defgroup electric-help ()
  33. "Electric help facility."
  34. :version "21.1"
  35. :group 'help)
  36. (defcustom electric-help-shrink-window t
  37. "If set, adjust help window sizes to buffer sizes when displaying help."
  38. :type 'boolean
  39. :group 'electric-help)
  40. (defcustom electric-help-mode-hook nil
  41. "Hook run by `with-electric-help' after initializing the buffer."
  42. :type 'hook
  43. :group 'electric-help)
  44. (put 'electric-help-undefined 'suppress-keymap t)
  45. (defvar electric-help-map
  46. (let ((map (make-keymap)))
  47. ;; FIXME fragile. Should derive from help-mode-map in a smarter way.
  48. (set-keymap-parent map button-buffer-map)
  49. ;; allow all non-self-inserting keys - search, scroll, etc, but
  50. ;; let M-x and C-x exit ehelp mode and retain buffer:
  51. (suppress-keymap map)
  52. (define-key map "\C-u" 'electric-help-undefined)
  53. (define-key map [?\C-0] 'electric-help-undefined)
  54. (define-key map [?\C-1] 'electric-help-undefined)
  55. (define-key map [?\C-2] 'electric-help-undefined)
  56. (define-key map [?\C-3] 'electric-help-undefined)
  57. (define-key map [?\C-4] 'electric-help-undefined)
  58. (define-key map [?\C-5] 'electric-help-undefined)
  59. (define-key map [?\C-6] 'electric-help-undefined)
  60. (define-key map [?\C-7] 'electric-help-undefined)
  61. (define-key map [?\C-8] 'electric-help-undefined)
  62. (define-key map [?\C-9] 'electric-help-undefined)
  63. (define-key map (char-to-string help-char) 'electric-help-help)
  64. (define-key map "?" 'electric-help-help)
  65. (define-key map " " 'scroll-up)
  66. (define-key map [?\S-\ ] 'scroll-down)
  67. (define-key map "\^?" 'scroll-down)
  68. (define-key map "." 'beginning-of-buffer)
  69. (define-key map "<" 'beginning-of-buffer)
  70. (define-key map ">" 'end-of-buffer)
  71. ;(define-key map "\C-g" 'electric-help-exit)
  72. (define-key map "Q" 'electric-help-exit)
  73. (define-key map "q" 'electric-help-exit)
  74. ;;a better key than this?
  75. (define-key map "R" 'electric-help-retain)
  76. (define-key map "r" 'electric-help-retain)
  77. (define-key map "\ex" 'electric-help-execute-extended)
  78. (define-key map "\C-x" 'electric-help-ctrl-x-prefix)
  79. map)
  80. "Keymap defining commands available in `electric-help-mode'.")
  81. (defvar electric-help-orig-major-mode nil)
  82. (make-variable-buffer-local 'electric-help-orig-major-mode)
  83. (defun electric-help-mode ()
  84. "`with-electric-help' temporarily places its buffer in this mode.
  85. \(On exit from `with-electric-help', the original `major-mode' is restored.)"
  86. (setq buffer-read-only t)
  87. (setq electric-help-orig-major-mode major-mode)
  88. (setq mode-name "Help")
  89. (setq major-mode 'help-mode)
  90. (setq mode-line-buffer-identification '(" Help: %b"))
  91. (use-local-map electric-help-map)
  92. (add-hook 'mouse-leave-buffer-hook 'electric-help-retain)
  93. (view-mode -1)
  94. ;; this is done below in with-electric-help
  95. ;(run-hooks 'electric-help-mode-hook)
  96. )
  97. ;;;###autoload
  98. (defun with-electric-help (thunk &optional buffer noerase minheight)
  99. "Pop up an \"electric\" help buffer.
  100. THUNK is a function of no arguments which is called to initialize the
  101. contents of BUFFER. BUFFER defaults to `*Help*'. BUFFER will be
  102. erased before THUNK is called unless NOERASE is non-nil. THUNK will
  103. be called while BUFFER is current and with `standard-output' bound to
  104. the buffer specified by BUFFER.
  105. If THUNK returns nil, we display BUFFER starting at the top, and shrink
  106. the window to fit. If THUNK returns non-nil, we don't do those things.
  107. After THUNK has been called, this function \"electrically\" pops up a
  108. window in which BUFFER is displayed and allows the user to scroll
  109. through that buffer in `electric-help-mode'. The window's height will
  110. be at least MINHEIGHT if this value is non-nil.
  111. If THUNK returns nil, we display BUFFER starting at the top, and
  112. shrink the window to fit if `electric-help-shrink-window' is non-nil.
  113. If THUNK returns non-nil, we don't do those things.
  114. When the user exits (with `electric-help-exit', or otherwise), the help
  115. buffer's window disappears (i.e., we use `save-window-excursion'), and
  116. BUFFER is put back into its original major mode."
  117. (setq buffer (get-buffer-create (or buffer "*Help*")))
  118. (let ((one (one-window-p t))
  119. (config (current-window-configuration))
  120. (bury nil)
  121. (electric-help-form-to-execute nil))
  122. (unwind-protect
  123. (save-excursion
  124. (when one
  125. (goto-char (window-start)))
  126. (let ((pop-up-windows t))
  127. (pop-to-buffer buffer))
  128. (with-current-buffer buffer
  129. (when (and minheight (< (window-height) minheight))
  130. (enlarge-window (- minheight (window-height))))
  131. (electric-help-mode)
  132. (setq buffer-read-only nil)
  133. (unless noerase
  134. (erase-buffer)))
  135. (let ((standard-output buffer))
  136. (unless (funcall thunk)
  137. (set-buffer buffer)
  138. (set-buffer-modified-p nil)
  139. (goto-char (point-min))
  140. (when (and one electric-help-shrink-window)
  141. (shrink-window-if-larger-than-buffer))))
  142. (set-buffer buffer)
  143. (run-hooks 'electric-help-mode-hook)
  144. (setq buffer-read-only t)
  145. (if (eq (car-safe (electric-help-command-loop)) 'retain)
  146. (setq config (current-window-configuration))
  147. (setq bury t))
  148. ;; Remove the hook.
  149. (when (memq 'electric-help-retain mouse-leave-buffer-hook)
  150. (remove-hook 'mouse-leave-buffer-hook 'electric-help-retain)))
  151. (message "")
  152. (set-buffer buffer)
  153. (setq buffer-read-only nil)
  154. ;; Restore the original major mode saved by `electric-help-mode'.
  155. ;; We should really get a usable *Help* buffer when retaining
  156. ;; the electric one with `r'. The problem is that a simple
  157. ;; call to `help-mode' won't cut it; e.g. RET is bound wrong
  158. ;; afterwards (`View-scroll-line-forward' instead of `help-follow').
  159. ;; That's because Help mode should be set with `with-help-window'
  160. ;; instead of the direct call to `help-mode'. But at least
  161. ;; RET works correctly on links after using `help-mode'.
  162. ;; This is satisfactory enough.
  163. (condition-case ()
  164. (funcall (or electric-help-orig-major-mode 'fundamental-mode))
  165. (error nil))
  166. (set-window-configuration config)
  167. (when bury
  168. ;;>> Perhaps this shouldn't be done,
  169. ;; so that when we say "Press space to bury" we mean it
  170. (replace-buffer-in-windows buffer)
  171. ;; must do this outside of save-window-excursion
  172. (bury-buffer buffer))
  173. (if (functionp electric-help-form-to-execute)
  174. (funcall electric-help-form-to-execute)
  175. (eval electric-help-form-to-execute)))))
  176. (defun electric-help-command-loop ()
  177. (catch 'exit
  178. (if (pos-visible-in-window-p (point-max))
  179. (progn (message "%s" (substitute-command-keys "<<< Press Space to bury the help buffer, Press \\[electric-help-retain] to retain it >>>"))
  180. (let ((ev (read-event)))
  181. (if (equal ev ?\s)
  182. (throw 'exit t)
  183. (push ev unread-command-events)))))
  184. (let (up down both neither
  185. (standard (and (eq (key-binding " " nil t)
  186. 'scroll-up)
  187. (eq (key-binding "\^?" nil t)
  188. 'scroll-down)
  189. (eq (key-binding "q" nil t)
  190. 'electric-help-exit)
  191. (eq (key-binding "r" nil t)
  192. 'electric-help-retain))))
  193. (Electric-command-loop
  194. 'exit
  195. (function (lambda ()
  196. (sit-for 0) ;necessary if last command was end-of-buffer or
  197. ;beginning-of-buffer - otherwise pos-visible-in-window-p
  198. ;will yield a wrong result.
  199. (let ((min (pos-visible-in-window-p (point-min)))
  200. (max (pos-visible-in-window-p (1- (point-max)))))
  201. (cond (isearch-mode 'noprompt)
  202. ((and min max)
  203. (cond (standard "Press q to exit, r to retain ")
  204. (neither)
  205. (t (setq neither (substitute-command-keys "Press \\[electric-help-exit] to exit, \\[electric-help-retain] to retain ")))))
  206. (min
  207. (cond (standard "Press SPC to scroll, q to exit, r to retain ")
  208. (up)
  209. (t (setq up (substitute-command-keys "Press \\[scroll-up] to scroll, \\[electric-help-exit] to exit, \\[electric-help-retain] to retain ")))))
  210. (max
  211. (cond (standard "Press DEL to scroll back, q to exit, r to retain ")
  212. (down)
  213. (t (setq down (substitute-command-keys "Press \\[scroll-down] to scroll back, \\[electric-help-exit] to exit, \\[electric-help-retain] to retain ")))))
  214. (t
  215. (cond (standard "Press SPC to scroll, DEL to scroll back, q to exit, r to retain ")
  216. (both)
  217. (t (setq both (substitute-command-keys "Press \\[scroll-up] to scroll, \\[scroll-down] to scroll back, \\[electric-help-exit] to exit, \\[electric-help-retain] to retain ")))))))))
  218. t))))
  219. ;(defun electric-help-scroll-up (arg)
  220. ; ">>>Doc"
  221. ; (interactive "P")
  222. ; (if (and (null arg) (pos-visible-in-window-p (point-max)))
  223. ; (electric-help-exit)
  224. ; (scroll-up arg)))
  225. (defun electric-help-exit ()
  226. "Exit `with-electric-help', restoring the previous window/buffer configuration.
  227. \(The *Help* buffer will be buried.)"
  228. (interactive)
  229. ;; Make sure that we don't throw twice, even if two events cause
  230. ;; calling this function:
  231. (if (memq 'electric-help-retain mouse-leave-buffer-hook)
  232. (progn
  233. (remove-hook 'mouse-leave-buffer-hook 'electric-help-retain)
  234. (throw 'exit t))))
  235. (defun electric-help-retain ()
  236. "Exit `with-electric-help', retaining the current window/buffer configuration.
  237. \(The *Help* buffer will not be selected, but \\[switch-to-buffer-other-window] RET
  238. will select it.)"
  239. (interactive)
  240. ;; Make sure that we don't throw twice, even if two events cause
  241. ;; calling this function:
  242. (if (memq 'electric-help-retain mouse-leave-buffer-hook)
  243. (progn
  244. (remove-hook 'mouse-leave-buffer-hook 'electric-help-retain)
  245. (throw 'exit '(retain)))))
  246. (defun electric-help-undefined ()
  247. (interactive)
  248. (error "%s is undefined -- Press %s to exit"
  249. (mapconcat 'single-key-description (this-command-keys) " ")
  250. (if (eq (key-binding "q" nil t) 'electric-help-exit)
  251. "q"
  252. (substitute-command-keys "\\[electric-help-exit]"))))
  253. ;>>> this needs to be hairified (recursive help, anybody?)
  254. (defun electric-help-help ()
  255. (interactive)
  256. (if (and (eq (key-binding "q" nil t) 'electric-help-exit)
  257. (eq (key-binding " " nil t) 'scroll-up)
  258. (eq (key-binding "\^?" nil t) 'scroll-down)
  259. (eq (key-binding "r" nil t) 'electric-help-retain))
  260. (message "SPC scrolls up, DEL scrolls down, q exits burying help buffer, r exits")
  261. (message "%s" (substitute-command-keys "\\[scroll-up] scrolls up, \\[scroll-down] scrolls down, \\[electric-help-exit] exits burying help buffer, \\[electric-help-retain] exits")))
  262. (sit-for 2))
  263. ;;;###autoload
  264. (defun electric-helpify (fun &optional name)
  265. (let ((name (or name "*Help*")))
  266. (if (save-window-excursion
  267. ;; kludge-o-rama
  268. (let* ((p (symbol-function 'help-print-return-message))
  269. (b (get-buffer name))
  270. (m (buffer-modified-p b)))
  271. (and b (not (get-buffer-window b))
  272. (setq b nil))
  273. (unwind-protect
  274. (progn
  275. (message "%s..." (capitalize (symbol-name fun)))
  276. ;; with-output-to-temp-buffer marks the buffer as unmodified.
  277. ;; kludging excessively and relying on that as some sort
  278. ;; of indication leads to the following abomination...
  279. ;;>> This would be doable without such icky kludges if either
  280. ;;>> (a) there were a function to read the interactive
  281. ;;>> args for a command and return a list of those args.
  282. ;;>> (To which one would then just apply the command)
  283. ;;>> (The only problem with this is that interactive-p
  284. ;;>> would break, but that is such a misfeature in
  285. ;;>> any case that I don't care)
  286. ;;>> It is easy to do this for emacs-lisp functions;
  287. ;;>> the only problem is getting the interactive spec
  288. ;;>> for subrs
  289. ;;>> (b) there were a function which returned a
  290. ;;>> modification-tick for a buffer. One could tell
  291. ;;>> whether a buffer had changed by whether the
  292. ;;>> modification-tick were different.
  293. ;;>> (Presumably there would have to be a way to either
  294. ;;>> restore the tick to some previous value, or to
  295. ;;>> suspend updating of the tick in order to allow
  296. ;;>> things like momentary-string-display)
  297. (and b
  298. (with-current-buffer b
  299. (set-buffer-modified-p t)))
  300. (fset 'help-print-return-message 'ignore)
  301. (call-interactively fun)
  302. (and (get-buffer name)
  303. (get-buffer-window (get-buffer name))
  304. (or (not b)
  305. (not (eq b (get-buffer name)))
  306. (not (buffer-modified-p b)))))
  307. (fset 'help-print-return-message p)
  308. (and b (buffer-name b)
  309. (with-current-buffer b
  310. (set-buffer-modified-p m))))))
  311. (with-electric-help 'ignore name t))))
  312. ;; This is to be bound to M-x in ehelp mode. Retains ehelp buffer and then
  313. ;; continues with execute-extended-command.
  314. (defun electric-help-execute-extended (_prefixarg)
  315. (interactive "p")
  316. (setq electric-help-form-to-execute
  317. (lambda () (execute-extended-command nil)))
  318. (electric-help-retain))
  319. ;; This is to be buond to C-x in ehelp mode. Retains ehelp buffer and then
  320. ;; continues with ctrl-x prefix.
  321. (defun electric-help-ctrl-x-prefix (_prefixarg)
  322. (interactive "p")
  323. (setq electric-help-form-to-execute
  324. (lambda ()
  325. (message nil)
  326. (setq unread-command-events
  327. (append unread-command-events '(?\C-x)))))
  328. (electric-help-retain))
  329. (defun electric-describe-key ()
  330. (interactive)
  331. (electric-helpify 'describe-key))
  332. (defun electric-describe-mode ()
  333. (interactive)
  334. (electric-helpify 'describe-mode))
  335. (defun electric-view-lossage ()
  336. (interactive)
  337. (electric-helpify 'view-lossage))
  338. ;(defun electric-help-for-help ()
  339. ; "See help-for-help"
  340. ; (interactive)
  341. ; )
  342. (defun electric-describe-function ()
  343. (interactive)
  344. (electric-helpify 'describe-function))
  345. (defun electric-describe-variable ()
  346. (interactive)
  347. (electric-helpify 'describe-variable))
  348. (defun electric-describe-bindings ()
  349. (interactive)
  350. (electric-helpify 'describe-bindings))
  351. (defun electric-describe-syntax ()
  352. (interactive)
  353. (electric-helpify 'describe-syntax))
  354. (defun electric-command-apropos ()
  355. (interactive)
  356. (electric-helpify 'command-apropos "*Apropos*"))
  357. ;(define-key help-map "a" 'electric-command-apropos)
  358. (defun electric-apropos ()
  359. (interactive)
  360. (electric-helpify 'apropos))
  361. ;;;; ehelp-map
  362. (defvar ehelp-map
  363. (let ((map (copy-keymap help-map)))
  364. (substitute-key-definition 'apropos 'electric-apropos map)
  365. (substitute-key-definition 'command-apropos 'electric-command-apropos map)
  366. (substitute-key-definition 'describe-key 'electric-describe-key map)
  367. (substitute-key-definition 'describe-mode 'electric-describe-mode map)
  368. (substitute-key-definition 'view-lossage 'electric-view-lossage map)
  369. (substitute-key-definition 'describe-function 'electric-describe-function map)
  370. (substitute-key-definition 'describe-variable 'electric-describe-variable map)
  371. (substitute-key-definition 'describe-bindings 'electric-describe-bindings map)
  372. (substitute-key-definition 'describe-syntax 'electric-describe-syntax map)
  373. map))
  374. ;;;###(autoload 'ehelp-command "ehelp" "Prefix command for ehelp." t 'keymap)
  375. (defalias 'ehelp-command ehelp-map)
  376. (put 'ehelp-command 'documentation "Prefix command for ehelp.")
  377. (provide 'ehelp)
  378. ;;; ehelp.el ends here