em-smart.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. ;;; em-smart.el --- smart display of output
  2. ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
  3. ;; Author: John Wiegley <johnw@gnu.org>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; The best way to get a sense of what this code is trying to do is by
  17. ;; using it. Basically, the philosophy represents a blend between the
  18. ;; ease of use of modern day shells, and the review-before-you-proceed
  19. ;; mentality of Plan 9's 9term.
  20. ;;
  21. ;; @ When you invoke a command, it is assumed that you want to read
  22. ;; the output of that command.
  23. ;;
  24. ;; @ If the output is not what you wanted, it is assumed that you will
  25. ;; want to edit, and then resubmit a refined version of that
  26. ;; command.
  27. ;;
  28. ;; @ If the output is valid, pressing any self-inserting character key
  29. ;; will jump to end of the buffer and insert that character, in
  30. ;; order to begin entry of a new command.
  31. ;;
  32. ;; @ If you show an intention to edit the previous command -- by
  33. ;; moving around within it -- then the next self-inserting
  34. ;; characters will insert *there*, instead of at the bottom of the
  35. ;; buffer.
  36. ;;
  37. ;; @ If you show an intention to review old commands, such as M-p or
  38. ;; M-r, point will jump to the bottom of the buffer before invoking
  39. ;; that command.
  40. ;;
  41. ;; @ If none of the above has happened yet (i.e., your point is just
  42. ;; sitting on the previous command), you can use SPACE and BACKSPACE
  43. ;; (or DELETE) to page forward and backward *through the output of
  44. ;; the last command only*. It will constrain the movement of the
  45. ;; point and window so that the maximum amount of output is always
  46. ;; displayed at all times.
  47. ;;
  48. ;; @ While output is being generated from a command, the window will
  49. ;; be constantly reconfigured (until it would otherwise make no
  50. ;; difference) in order to always show you the most output from the
  51. ;; command possible. This happens if you change window sizes,
  52. ;; scroll, etc.
  53. ;;
  54. ;; @ Like I said, it's not really comprehensible until you try it! ;)
  55. ;;
  56. ;; One disadvantage of this module is that it increases Eshell's
  57. ;; memory consumption by a factor of two or more. With small commands
  58. ;; (such as pwd), where the screen is mostly full, consumption can
  59. ;; increase by orders of magnitude.
  60. ;;; Code:
  61. (eval-when-compile (require 'eshell))
  62. ;;;###autoload
  63. (eshell-defgroup eshell-smart nil
  64. "This module combines the facility of normal, modern shells with
  65. some of the edit/review concepts inherent in the design of Plan 9's
  66. 9term. See the docs for more details.
  67. Most likely you will have to turn this option on and play around with
  68. it to get a real sense of how it works."
  69. :tag "Smart display of output"
  70. ;; :link '(info-link "(eshell)Smart display of output")
  71. :group 'eshell-module)
  72. ;;; User Variables:
  73. (defcustom eshell-smart-load-hook nil
  74. "A list of functions to call when loading `eshell-smart'."
  75. :version "24.1" ; removed eshell-smart-initialize
  76. :type 'hook
  77. :group 'eshell-smart)
  78. (defcustom eshell-smart-unload-hook
  79. (list
  80. (function
  81. (lambda ()
  82. (remove-hook 'window-configuration-change-hook
  83. 'eshell-refresh-windows))))
  84. "A hook that gets run when `eshell-smart' is unloaded."
  85. :type 'hook
  86. :group 'eshell-smart)
  87. (defcustom eshell-review-quick-commands nil
  88. "If t, always review commands.
  89. Reviewing means keeping point on the text of the command that was just
  90. invoked, to allow corrections to be made easily.
  91. If set to nil, quick commands won't be reviewed. A quick command is a
  92. command that produces no output, and exits successfully.
  93. If set to `not-even-short-output', then the definition of \"quick
  94. command\" is extended to include commands that produce output, if and
  95. only if that output can be presented in its entirely in the Eshell window."
  96. :type '(choice (const :tag "No" nil)
  97. (const :tag "Yes" t)
  98. (const :tag "Not even short output"
  99. not-even-short-output))
  100. :group 'eshell-smart)
  101. (defcustom eshell-smart-display-navigate-list
  102. '(insert-parentheses
  103. mouse-yank-at-click
  104. mouse-yank-primary
  105. mouse-yank-secondary
  106. yank-pop
  107. yank-rectangle
  108. yank)
  109. "A list of commands which cause Eshell to jump to the end of buffer."
  110. :type '(repeat function)
  111. :group 'eshell-smart)
  112. (defcustom eshell-smart-space-goes-to-end t
  113. "If non-nil, space will go to end of buffer when point-max is visible.
  114. That is, if a command is running and the user presses SPACE at a time
  115. when the end of the buffer is visible, point will go to the end of the
  116. buffer and smart-display will be turned off (that is, subsequently
  117. pressing backspace will not cause the buffer to scroll down).
  118. This feature is provided to make it very easy to watch the output of a
  119. long-running command, such as make, where it's more desirable to see
  120. the output go by than to review it afterward.
  121. Setting this variable to nil means that space and backspace will
  122. always have a consistent behavior, which is to move back and forth
  123. through displayed output. But it also means that enabling output
  124. tracking requires the user to manually move point to the end of the
  125. buffer using \\[end-of-buffer]."
  126. :type 'boolean
  127. :group 'eshell-smart)
  128. (defcustom eshell-where-to-jump 'begin
  129. "This variable indicates where point should jump to after a command.
  130. The options are `begin', `after' or `end'."
  131. :type '(radio (const :tag "Beginning of command" begin)
  132. (const :tag "After command word" after)
  133. (const :tag "End of command" end))
  134. :group 'eshell-smart)
  135. ;;; Internal Variables:
  136. (defvar eshell-smart-displayed nil)
  137. (defvar eshell-smart-command-done nil)
  138. (defvar eshell-currently-handling-window nil)
  139. ;;; Functions:
  140. (defun eshell-smart-initialize ()
  141. "Setup Eshell smart display."
  142. (unless eshell-non-interactive-p
  143. ;; override a few variables, since they would interfere with the
  144. ;; smart display functionality.
  145. (set (make-local-variable 'eshell-scroll-to-bottom-on-output) nil)
  146. (set (make-local-variable 'eshell-scroll-to-bottom-on-input) nil)
  147. (set (make-local-variable 'eshell-scroll-show-maximum-output) t)
  148. (add-hook 'window-scroll-functions 'eshell-smart-scroll-window nil t)
  149. (add-hook 'window-configuration-change-hook 'eshell-refresh-windows)
  150. (add-hook 'eshell-output-filter-functions 'eshell-refresh-windows t t)
  151. (add-hook 'after-change-functions 'eshell-disable-after-change nil t)
  152. (add-hook 'eshell-input-filter-functions 'eshell-smart-display-setup nil t)
  153. (make-local-variable 'eshell-smart-command-done)
  154. (add-hook 'eshell-post-command-hook
  155. (function
  156. (lambda ()
  157. (setq eshell-smart-command-done t))) t t)
  158. (unless (eq eshell-review-quick-commands t)
  159. (add-hook 'eshell-post-command-hook
  160. 'eshell-smart-maybe-jump-to-end nil t))))
  161. (defun eshell-smart-scroll-window (wind start)
  162. "Scroll the given Eshell window accordingly."
  163. (unless eshell-currently-handling-window
  164. (let ((inhibit-point-motion-hooks t)
  165. (eshell-currently-handling-window t))
  166. (save-selected-window
  167. (select-window wind)
  168. (eshell-smart-redisplay)))))
  169. (defun eshell-refresh-windows (&optional frame)
  170. "Refresh all visible Eshell buffers."
  171. (let (affected)
  172. (walk-windows
  173. (function
  174. (lambda (wind)
  175. (with-current-buffer (window-buffer wind)
  176. (if eshell-mode
  177. (let (window-scroll-functions)
  178. (eshell-smart-scroll-window wind (window-start))
  179. (setq affected t))))))
  180. 0 frame)
  181. (if affected
  182. (let (window-scroll-functions)
  183. (eshell-redisplay)))))
  184. (defun eshell-smart-display-setup ()
  185. "Set the point to somewhere in the beginning of the last command."
  186. (cond
  187. ((eq eshell-where-to-jump 'begin)
  188. (goto-char eshell-last-input-start))
  189. ((eq eshell-where-to-jump 'after)
  190. (goto-char (next-single-property-change
  191. eshell-last-input-start 'arg-end))
  192. (if (= (point) (- eshell-last-input-end 2))
  193. (forward-char)))
  194. ((eq eshell-where-to-jump 'end)
  195. (goto-char (1- eshell-last-input-end)))
  196. (t
  197. (error "Invalid value for `eshell-where-to-jump'")))
  198. (setq eshell-smart-command-done nil)
  199. (add-hook 'pre-command-hook 'eshell-smart-display-move nil t)
  200. (eshell-refresh-windows))
  201. (defun eshell-disable-after-change (b e l)
  202. "Disable smart display mode if the buffer changes in any way."
  203. (when eshell-smart-command-done
  204. (remove-hook 'pre-command-hook 'eshell-smart-display-move t)
  205. (setq eshell-smart-command-done nil)))
  206. (defun eshell-smart-maybe-jump-to-end ()
  207. "Jump to the end of the input buffer.
  208. This is done whenever a command exits successfully and both the command
  209. and the end of the buffer are still visible."
  210. (when (and (= eshell-last-command-status 0)
  211. (if (eq eshell-review-quick-commands 'not-even-short-output)
  212. (and (pos-visible-in-window-p (point-max))
  213. (pos-visible-in-window-p eshell-last-input-start))
  214. (= (count-lines eshell-last-input-end
  215. eshell-last-output-end) 0)))
  216. (goto-char (point-max))
  217. (remove-hook 'pre-command-hook 'eshell-smart-display-move t)))
  218. (defun eshell-smart-redisplay ()
  219. "Display as much output as possible, smartly."
  220. (if (eobp)
  221. (save-excursion
  222. (recenter -1)
  223. ;; trigger the redisplay now, so that we catch any attempted
  224. ;; point motion; this is to cover for a redisplay bug
  225. (eshell-redisplay))
  226. (let ((top-point (point)))
  227. (and (memq 'eshell-smart-display-move pre-command-hook)
  228. (>= (point) eshell-last-input-start)
  229. (< (point) eshell-last-input-end)
  230. (set-window-start (selected-window)
  231. (line-beginning-position) t))
  232. (if (pos-visible-in-window-p (point-max))
  233. (save-excursion
  234. (goto-char (point-max))
  235. (recenter -1)
  236. (unless (pos-visible-in-window-p top-point)
  237. (goto-char top-point)
  238. (set-window-start (selected-window)
  239. (line-beginning-position) t)))))))
  240. (defun eshell-smart-goto-end ()
  241. "Like `end-of-buffer', but do not push a mark."
  242. (interactive)
  243. (goto-char (point-max)))
  244. (defun eshell-smart-display-move ()
  245. "Handle self-inserting or movement commands intelligently."
  246. (let (clear)
  247. (if (or current-prefix-arg
  248. (and (> (point) eshell-last-input-start)
  249. (< (point) eshell-last-input-end))
  250. (>= (point) eshell-last-output-end))
  251. (setq clear t)
  252. (cond
  253. ((eq this-command 'self-insert-command)
  254. (if (eq last-command-event ? )
  255. (if (and eshell-smart-space-goes-to-end
  256. eshell-current-command)
  257. (if (not (pos-visible-in-window-p (point-max)))
  258. (setq this-command 'scroll-up)
  259. (setq this-command 'eshell-smart-goto-end))
  260. (setq this-command 'scroll-up))
  261. (setq clear t)
  262. (goto-char (point-max))))
  263. ((eq this-command 'delete-backward-char)
  264. (setq this-command 'ignore)
  265. (if (< (point) eshell-last-input-start)
  266. (eshell-show-output)
  267. (if (pos-visible-in-window-p eshell-last-input-start)
  268. (progn
  269. (ignore-errors
  270. (scroll-down))
  271. (eshell-show-output))
  272. (scroll-down)
  273. (if (pos-visible-in-window-p eshell-last-input-end)
  274. (eshell-show-output)))))
  275. ((or (memq this-command eshell-smart-display-navigate-list)
  276. (and (eq this-command 'eshell-send-input)
  277. (not (and (>= (point) eshell-last-input-start)
  278. (< (point) eshell-last-input-end)))))
  279. (setq clear t)
  280. (goto-char (point-max)))))
  281. (if clear
  282. (remove-hook 'pre-command-hook 'eshell-smart-display-move t))))
  283. (provide 'em-smart)
  284. ;; Local Variables:
  285. ;; generated-autoload-file: "esh-groups.el"
  286. ;; End:
  287. ;;; em-smart.el ends here