esh-mode.el 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. ;;; esh-mode.el --- user interface
  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. ;; Basically, Eshell is used just like shell mode (<M-x shell>). The
  17. ;; keystrokes for navigating the buffer, and accessing the command
  18. ;; history, are identical. Unlike shell mode, however, Eshell mode's
  19. ;; governing process is Emacs itself. With shell mode, an inferior
  20. ;; shell process is executed that communicates with Emacs via comint
  21. ;; -- a mode for handling sub-process interaction. Eshell mode, on
  22. ;; the other hand, is a truly native Emacs shell. No subprocess are
  23. ;; invoked except the ones requested by the user at the prompt.
  24. ;;
  25. ;; After entering a command, use <RET> to invoke it ([Command
  26. ;; invocation]) . If there is a command on disk, it will be executed
  27. ;; as in a normal shell. If there is no command by that name on disk,
  28. ;; but a Lisp function with that name is defined, the Lisp function
  29. ;; will be called, using the arguments passed on the command line.
  30. ;;
  31. ;; Some of the other features of the command interaction mode are:
  32. ;;
  33. ;; @ <M-RET> can be used to accumulate further commands while a
  34. ;; command is currently running. Since all input is passed to the
  35. ;; subprocess being executed, there is no automatic input queueing
  36. ;; as there is with other shells.
  37. ;;
  38. ;; @ <C-c C-t> can be used to truncate the buffer if it grows too
  39. ;; large.
  40. ;;
  41. ;; @ <C-c C-r> will move point to the beginning of the output of the
  42. ;; last command. With a prefix argument, it will narrow to view
  43. ;; only that output.
  44. ;;
  45. ;; @ <C-c C-o> will delete the output from the last command.
  46. ;;
  47. ;; @ <C-c C-f> will move forward a complete shell argument.
  48. ;;
  49. ;; @ <C-c C-b> will move backward a complete shell argument.
  50. ;;; Code:
  51. (provide 'esh-mode)
  52. (eval-when-compile (require 'esh-util))
  53. (require 'esh-module)
  54. (require 'esh-cmd)
  55. (require 'esh-io)
  56. (require 'esh-var)
  57. (defgroup eshell-mode nil
  58. "This module contains code for handling input from the user."
  59. :tag "User interface"
  60. :group 'eshell)
  61. ;;; User Variables:
  62. (defcustom eshell-mode-unload-hook nil
  63. "A hook that gets run when `eshell-mode' is unloaded."
  64. :type 'hook
  65. :group 'eshell-mode)
  66. (defcustom eshell-mode-hook nil
  67. "A hook that gets run when `eshell-mode' is entered."
  68. :type 'hook
  69. :group 'eshell-mode)
  70. (defcustom eshell-first-time-mode-hook nil
  71. "A hook that gets run the first time `eshell-mode' is entered.
  72. That is to say, the first time during an Emacs session."
  73. :type 'hook
  74. :group 'eshell-mode)
  75. (defcustom eshell-exit-hook nil
  76. "A hook that is run whenever `eshell' is exited.
  77. This hook is only run if exiting actually kills the buffer."
  78. :version "24.1" ; removed eshell-query-kill-processes
  79. :type 'hook
  80. :group 'eshell-mode)
  81. (defcustom eshell-kill-on-exit t
  82. "If non-nil, kill the Eshell buffer on the `exit' command.
  83. Otherwise, the buffer will simply be buried."
  84. :type 'boolean
  85. :group 'eshell-mode)
  86. (defcustom eshell-input-filter-functions nil
  87. "Functions to call before input is processed.
  88. The input is contained in the region from `eshell-last-input-start' to
  89. `eshell-last-input-end'."
  90. :type 'hook
  91. :group 'eshell-mode)
  92. (defcustom eshell-send-direct-to-subprocesses nil
  93. "If t, send any input immediately to a subprocess."
  94. :type 'boolean
  95. :group 'eshell-mode)
  96. (defcustom eshell-expand-input-functions nil
  97. "Functions to call before input is parsed.
  98. Each function is passed two arguments, which bounds the region of the
  99. current input text."
  100. :type 'hook
  101. :group 'eshell-mode)
  102. (defcustom eshell-scroll-to-bottom-on-input nil
  103. "Controls whether input to interpreter causes window to scroll.
  104. If nil, then do not scroll. If t or `all', scroll all windows showing
  105. buffer. If `this', scroll only the selected window.
  106. See `eshell-preinput-scroll-to-bottom'."
  107. :type '(radio (const :tag "Do not scroll Eshell windows" nil)
  108. (const :tag "Scroll all windows showing the buffer" all)
  109. (const :tag "Scroll only the selected window" this))
  110. :group 'eshell-mode)
  111. (defcustom eshell-scroll-to-bottom-on-output nil
  112. "Controls whether interpreter output causes window to scroll.
  113. If nil, then do not scroll. If t or `all', scroll all windows showing
  114. buffer. If `this', scroll only the selected window. If `others',
  115. scroll only those that are not the selected window.
  116. See variable `eshell-scroll-show-maximum-output' and function
  117. `eshell-postoutput-scroll-to-bottom'."
  118. :type '(radio (const :tag "Do not scroll Eshell windows" nil)
  119. (const :tag "Scroll all windows showing the buffer" all)
  120. (const :tag "Scroll only the selected window" this)
  121. (const :tag "Scroll all windows other than selected" this))
  122. :group 'eshell-mode)
  123. (defcustom eshell-scroll-show-maximum-output t
  124. "Controls how interpreter output causes window to scroll.
  125. If non-nil, then show the maximum output when the window is scrolled.
  126. See variable `eshell-scroll-to-bottom-on-output' and function
  127. `eshell-postoutput-scroll-to-bottom'."
  128. :type 'boolean
  129. :group 'eshell-mode)
  130. (defcustom eshell-buffer-maximum-lines 1024
  131. "The maximum size in lines for eshell buffers.
  132. Eshell buffers are truncated from the top to be no greater than this
  133. number, if the function `eshell-truncate-buffer' is on
  134. `eshell-output-filter-functions'."
  135. :type 'integer
  136. :group 'eshell-mode)
  137. (defcustom eshell-output-filter-functions
  138. '(eshell-postoutput-scroll-to-bottom
  139. eshell-handle-control-codes
  140. eshell-handle-ansi-color
  141. eshell-watch-for-password-prompt)
  142. "Functions to call before output is displayed.
  143. These functions are only called for output that is displayed
  144. interactively, and not for output which is redirected."
  145. :type 'hook
  146. :group 'eshell-mode)
  147. (defcustom eshell-preoutput-filter-functions nil
  148. "Functions to call before output is inserted into the buffer.
  149. These functions get one argument, a string containing the text to be
  150. inserted. They return the string as it should be inserted."
  151. :type 'hook
  152. :group 'eshell-mode)
  153. (defcustom eshell-password-prompt-regexp
  154. "[Pp]ass\\(word\\|phrase\\).*:\\s *\\'"
  155. "Regexp matching prompts for passwords in the inferior process.
  156. This is used by `eshell-watch-for-password-prompt'."
  157. :type 'regexp
  158. :group 'eshell-mode)
  159. (defcustom eshell-skip-prompt-function nil
  160. "A function called from beginning of line to skip the prompt."
  161. :type '(choice (const nil) function)
  162. :group 'eshell-mode)
  163. (defcustom eshell-status-in-modeline t
  164. "If non-nil, let the user know a command is running in the modeline."
  165. :type 'boolean
  166. :group 'eshell-mode)
  167. (defvar eshell-first-time-p t
  168. "A variable which is non-nil the first time Eshell is loaded.")
  169. ;; Internal Variables:
  170. ;; these are only set to `nil' initially for the sake of the
  171. ;; byte-compiler, when compiling other files which `require' this one
  172. (defvar eshell-mode nil)
  173. (defvar eshell-mode-map nil)
  174. (defvar eshell-command-running-string "--")
  175. (defvar eshell-command-map nil)
  176. (defvar eshell-command-prefix nil)
  177. (defvar eshell-last-input-start nil)
  178. (defvar eshell-last-input-end nil)
  179. (defvar eshell-last-output-start nil)
  180. (defvar eshell-last-output-block-begin nil)
  181. (defvar eshell-last-output-end nil)
  182. (defvar eshell-currently-handling-window nil)
  183. (defvar eshell-mode-syntax-table nil)
  184. (defvar eshell-mode-abbrev-table nil)
  185. (define-abbrev-table 'eshell-mode-abbrev-table ())
  186. (if (not eshell-mode-syntax-table)
  187. (let ((i 0))
  188. (setq eshell-mode-syntax-table (make-syntax-table))
  189. (while (< i ?0)
  190. (modify-syntax-entry i "_ " eshell-mode-syntax-table)
  191. (setq i (1+ i)))
  192. (setq i (1+ ?9))
  193. (while (< i ?A)
  194. (modify-syntax-entry i "_ " eshell-mode-syntax-table)
  195. (setq i (1+ i)))
  196. (setq i (1+ ?Z))
  197. (while (< i ?a)
  198. (modify-syntax-entry i "_ " eshell-mode-syntax-table)
  199. (setq i (1+ i)))
  200. (setq i (1+ ?z))
  201. (while (< i 128)
  202. (modify-syntax-entry i "_ " eshell-mode-syntax-table)
  203. (setq i (1+ i)))
  204. (modify-syntax-entry ? " " eshell-mode-syntax-table)
  205. (modify-syntax-entry ?\t " " eshell-mode-syntax-table)
  206. (modify-syntax-entry ?\f " " eshell-mode-syntax-table)
  207. (modify-syntax-entry ?\n "> " eshell-mode-syntax-table)
  208. ;; Give CR the same syntax as newline, for selective-display.
  209. (modify-syntax-entry ?\^m "> " eshell-mode-syntax-table)
  210. ;;; (modify-syntax-entry ?\; "< " eshell-mode-syntax-table)
  211. (modify-syntax-entry ?` "' " eshell-mode-syntax-table)
  212. (modify-syntax-entry ?' "' " eshell-mode-syntax-table)
  213. (modify-syntax-entry ?, "' " eshell-mode-syntax-table)
  214. ;; Used to be singlequote; changed for flonums.
  215. (modify-syntax-entry ?. "_ " eshell-mode-syntax-table)
  216. (modify-syntax-entry ?- "_ " eshell-mode-syntax-table)
  217. (modify-syntax-entry ?| ". " eshell-mode-syntax-table)
  218. (modify-syntax-entry ?# "' " eshell-mode-syntax-table)
  219. (modify-syntax-entry ?\" "\" " eshell-mode-syntax-table)
  220. (modify-syntax-entry ?\\ "/ " eshell-mode-syntax-table)
  221. (modify-syntax-entry ?\( "() " eshell-mode-syntax-table)
  222. (modify-syntax-entry ?\) ")( " eshell-mode-syntax-table)
  223. (modify-syntax-entry ?\{ "(} " eshell-mode-syntax-table)
  224. (modify-syntax-entry ?\} "){ " eshell-mode-syntax-table)
  225. (modify-syntax-entry ?\[ "(] " eshell-mode-syntax-table)
  226. (modify-syntax-entry ?\] ")[ " eshell-mode-syntax-table)
  227. ;; All non-word multibyte characters should be `symbol'.
  228. (if (featurep 'xemacs)
  229. (map-char-table
  230. (function
  231. (lambda (key val)
  232. (and (characterp key)
  233. (>= (char-int key) 256)
  234. (/= (char-syntax key) ?w)
  235. (modify-syntax-entry key "_ "
  236. eshell-mode-syntax-table))))
  237. (standard-syntax-table))
  238. (map-char-table
  239. (function
  240. (lambda (key val)
  241. (and (if (consp key)
  242. (and (>= (car key) 128)
  243. (/= (char-syntax (car key)) ?w))
  244. (and (>= key 256)
  245. (/= (char-syntax key) ?w)))
  246. (modify-syntax-entry key "_ "
  247. eshell-mode-syntax-table))))
  248. (standard-syntax-table)))))
  249. ;;; User Functions:
  250. (defun eshell-kill-buffer-function ()
  251. "Function added to `kill-buffer-hook' in Eshell buffers.
  252. This runs the function `eshell-kill-processes-on-exit',
  253. and the hook `eshell-exit-hook'."
  254. ;; It's fine to run this unconditionally since it can be customized
  255. ;; via the `eshell-kill-processes-on-exit' variable.
  256. (and (fboundp 'eshell-query-kill-processes)
  257. (not (memq 'eshell-query-kill-processes eshell-exit-hook))
  258. (eshell-query-kill-processes))
  259. (run-hooks 'eshell-exit-hook))
  260. ;;;###autoload
  261. (defun eshell-mode ()
  262. "Emacs shell interactive mode.
  263. \\{eshell-mode-map}"
  264. (kill-all-local-variables)
  265. (setq major-mode 'eshell-mode)
  266. (setq mode-name "EShell")
  267. (set (make-local-variable 'eshell-mode) t)
  268. (make-local-variable 'eshell-mode-map)
  269. (setq eshell-mode-map (make-sparse-keymap))
  270. (use-local-map eshell-mode-map)
  271. (when eshell-status-in-modeline
  272. (make-local-variable 'eshell-command-running-string)
  273. (let ((fmt (copy-sequence mode-line-format)))
  274. (make-local-variable 'mode-line-format)
  275. (setq mode-line-format fmt))
  276. (let ((modeline (memq 'mode-line-modified mode-line-format)))
  277. (if modeline
  278. (setcar modeline 'eshell-command-running-string))))
  279. (define-key eshell-mode-map [return] 'eshell-send-input)
  280. (define-key eshell-mode-map [(control ?m)] 'eshell-send-input)
  281. (define-key eshell-mode-map [(control ?j)] 'eshell-send-input)
  282. (define-key eshell-mode-map [(meta return)] 'eshell-queue-input)
  283. (define-key eshell-mode-map [(meta control ?m)] 'eshell-queue-input)
  284. (define-key eshell-mode-map [(meta control ?l)] 'eshell-show-output)
  285. (define-key eshell-mode-map [(control ?a)] 'eshell-bol)
  286. (set (make-local-variable 'eshell-command-prefix)
  287. (make-symbol "eshell-command-prefix"))
  288. (fset eshell-command-prefix (make-sparse-keymap))
  289. (set (make-local-variable 'eshell-command-map)
  290. (symbol-function eshell-command-prefix))
  291. (define-key eshell-mode-map [(control ?c)] eshell-command-prefix)
  292. ;; without this, find-tag complains about read-only text being
  293. ;; modified
  294. (if (eq (key-binding [(meta ?.)]) 'find-tag)
  295. (define-key eshell-mode-map [(meta ?.)] 'eshell-find-tag))
  296. (define-key eshell-command-map [(meta ?o)] 'eshell-mark-output)
  297. (define-key eshell-command-map [(meta ?d)] 'eshell-toggle-direct-send)
  298. (define-key eshell-command-map [(control ?a)] 'eshell-bol)
  299. (define-key eshell-command-map [(control ?b)] 'eshell-backward-argument)
  300. (define-key eshell-command-map [(control ?e)] 'eshell-show-maximum-output)
  301. (define-key eshell-command-map [(control ?f)] 'eshell-forward-argument)
  302. (define-key eshell-command-map [return] 'eshell-copy-old-input)
  303. (define-key eshell-command-map [(control ?m)] 'eshell-copy-old-input)
  304. (define-key eshell-command-map [(control ?o)] 'eshell-kill-output)
  305. (define-key eshell-command-map [(control ?r)] 'eshell-show-output)
  306. (define-key eshell-command-map [(control ?t)] 'eshell-truncate-buffer)
  307. (define-key eshell-command-map [(control ?u)] 'eshell-kill-input)
  308. (define-key eshell-command-map [(control ?w)] 'backward-kill-word)
  309. (define-key eshell-command-map [(control ?y)] 'eshell-repeat-argument)
  310. (setq local-abbrev-table eshell-mode-abbrev-table)
  311. (set-syntax-table eshell-mode-syntax-table)
  312. (set (make-local-variable 'dired-directory) default-directory)
  313. (set (make-local-variable 'list-buffers-directory)
  314. (expand-file-name default-directory))
  315. ;; always set the tab width to 8 in Eshell buffers, since external
  316. ;; commands which do their own formatting almost always expect this
  317. (set (make-local-variable 'tab-width) 8)
  318. ;; don't ever use auto-fill in Eshell buffers
  319. (setq auto-fill-function nil)
  320. ;; always display everything from a return value
  321. (if (boundp 'print-length)
  322. (set (make-local-variable 'print-length) nil))
  323. (if (boundp 'print-level)
  324. (set (make-local-variable 'print-level) nil))
  325. ;; set require-final-newline to nil; otherwise, all redirected
  326. ;; output will end with a newline, whether or not the source
  327. ;; indicated it!
  328. (set (make-local-variable 'require-final-newline) nil)
  329. (set (make-local-variable 'max-lisp-eval-depth)
  330. (max 3000 max-lisp-eval-depth))
  331. (set (make-local-variable 'max-specpdl-size)
  332. (max 6000 max-lisp-eval-depth))
  333. (set (make-local-variable 'eshell-last-input-start) (point-marker))
  334. (set (make-local-variable 'eshell-last-input-end) (point-marker))
  335. (set (make-local-variable 'eshell-last-output-start) (point-marker))
  336. (set (make-local-variable 'eshell-last-output-end) (point-marker))
  337. (set (make-local-variable 'eshell-last-output-block-begin) (point))
  338. (let ((modules-list (copy-sequence eshell-modules-list)))
  339. (make-local-variable 'eshell-modules-list)
  340. (setq eshell-modules-list modules-list))
  341. ;; load extension modules into memory. This will cause any global
  342. ;; variables they define to be visible, since some of the core
  343. ;; modules sometimes take advantage of their functionality if used.
  344. (dolist (module eshell-modules-list)
  345. (let ((module-fullname (symbol-name module))
  346. module-shortname)
  347. (if (string-match "^eshell-\\(.*\\)" module-fullname)
  348. (setq module-shortname
  349. (concat "em-" (match-string 1 module-fullname))))
  350. (unless module-shortname
  351. (error "Invalid Eshell module name: %s" module-fullname))
  352. (unless (featurep (intern module-shortname))
  353. (load module-shortname))))
  354. (unless (file-exists-p eshell-directory-name)
  355. (eshell-make-private-directory eshell-directory-name t))
  356. ;; Load core Eshell modules, then extension modules, for this session.
  357. (dolist (module (append (eshell-subgroups 'eshell) eshell-modules-list))
  358. (let ((load-hook (intern-soft (format "%s-load-hook" module)))
  359. (initfunc (intern-soft (format "%s-initialize" module))))
  360. (when (and load-hook (boundp load-hook))
  361. (if (memq initfunc (symbol-value load-hook)) (setq initfunc nil))
  362. (run-hooks load-hook))
  363. ;; So we don't need the -initialize functions on the hooks (b#5375).
  364. (and initfunc (fboundp initfunc) (funcall initfunc))))
  365. (if eshell-send-direct-to-subprocesses
  366. (add-hook 'pre-command-hook 'eshell-intercept-commands t t))
  367. (if eshell-scroll-to-bottom-on-input
  368. (add-hook 'pre-command-hook 'eshell-preinput-scroll-to-bottom t t))
  369. (when eshell-scroll-show-maximum-output
  370. (set (make-local-variable 'scroll-conservatively) 1000))
  371. (when eshell-status-in-modeline
  372. (add-hook 'eshell-pre-command-hook 'eshell-command-started nil t)
  373. (add-hook 'eshell-post-command-hook 'eshell-command-finished nil t))
  374. (add-hook 'kill-buffer-hook 'eshell-kill-buffer-function t t)
  375. (if eshell-first-time-p
  376. (run-hooks 'eshell-first-time-mode-hook))
  377. (run-mode-hooks 'eshell-mode-hook)
  378. (run-hooks 'eshell-post-command-hook))
  379. (put 'eshell-mode 'mode-class 'special)
  380. (defun eshell-command-started ()
  381. "Indicate in the modeline that a command has started."
  382. (setq eshell-command-running-string "**")
  383. (force-mode-line-update))
  384. (defun eshell-command-finished ()
  385. "Indicate in the modeline that a command has finished."
  386. (setq eshell-command-running-string "--")
  387. (force-mode-line-update))
  388. ;;; Internal Functions:
  389. (defun eshell-toggle-direct-send ()
  390. (interactive)
  391. (if eshell-send-direct-to-subprocesses
  392. (progn
  393. (setq eshell-send-direct-to-subprocesses nil)
  394. (remove-hook 'pre-command-hook 'eshell-intercept-commands t)
  395. (message "Sending subprocess input on RET"))
  396. (setq eshell-send-direct-to-subprocesses t)
  397. (add-hook 'pre-command-hook 'eshell-intercept-commands t t)
  398. (message "Sending subprocess input directly")))
  399. (defun eshell-self-insert-command (N)
  400. (interactive "i")
  401. (process-send-string
  402. (eshell-interactive-process)
  403. (char-to-string (if (symbolp last-command-event)
  404. (get last-command-event 'ascii-character)
  405. last-command-event))))
  406. (defun eshell-intercept-commands ()
  407. (when (and (eshell-interactive-process)
  408. (not (and (integerp last-input-event)
  409. (memq last-input-event '(?\C-x ?\C-c)))))
  410. (let ((possible-events (where-is-internal this-command))
  411. (name (symbol-name this-command))
  412. (intercept t))
  413. ;; Assume that any multikey combination which does NOT target an
  414. ;; Eshell command, is a combo the user wants invoked rather than
  415. ;; sent to the underlying subprocess.
  416. (unless (and (> (length name) 7)
  417. (equal (substring name 0 7) "eshell-"))
  418. (while possible-events
  419. (if (> (length (car possible-events)) 1)
  420. (setq intercept nil possible-events nil)
  421. (setq possible-events (cdr possible-events)))))
  422. (if intercept
  423. (setq this-command 'eshell-self-insert-command)))))
  424. (declare-function find-tag-interactive "etags" (prompt &optional no-default))
  425. (defun eshell-find-tag (&optional tagname next-p regexp-p)
  426. "A special version of `find-tag' that ignores read-onlyness."
  427. (interactive)
  428. (require 'etags)
  429. (let ((inhibit-read-only t)
  430. (no-default (eobp))
  431. (find-tag-default-function 'ignore))
  432. (setq tagname (car (find-tag-interactive "Find tag: " no-default)))
  433. (find-tag tagname next-p regexp-p)))
  434. (defun eshell-move-argument (limit func property arg)
  435. "Move forward ARG arguments."
  436. (catch 'eshell-incomplete
  437. (eshell-parse-arguments (save-excursion (eshell-bol) (point))
  438. (line-end-position)))
  439. (let ((pos (save-excursion
  440. (funcall func 1)
  441. (while (and (> arg 0) (/= (point) limit))
  442. (if (get-text-property (point) property)
  443. (setq arg (1- arg)))
  444. (if (> arg 0)
  445. (funcall func 1)))
  446. (point))))
  447. (goto-char pos)
  448. (if (and (eq func 'forward-char)
  449. (= (1+ pos) limit))
  450. (forward-char 1))))
  451. (defun eshell-forward-argument (&optional arg)
  452. "Move forward ARG arguments."
  453. (interactive "p")
  454. (eshell-move-argument (point-max) 'forward-char 'arg-end arg))
  455. (defun eshell-backward-argument (&optional arg)
  456. "Move backward ARG arguments."
  457. (interactive "p")
  458. (eshell-move-argument (point-min) 'backward-char 'arg-begin arg))
  459. (defun eshell-repeat-argument (&optional arg)
  460. (interactive "p")
  461. (let ((begin (save-excursion
  462. (eshell-backward-argument arg)
  463. (point))))
  464. (kill-ring-save begin (point))
  465. (yank)))
  466. (defun eshell-bol ()
  467. "Goes to the beginning of line, then skips past the prompt, if any."
  468. (interactive)
  469. (beginning-of-line)
  470. (and eshell-skip-prompt-function
  471. (funcall eshell-skip-prompt-function)))
  472. (defsubst eshell-push-command-mark ()
  473. "Push a mark at the end of the last input text."
  474. (push-mark (1- eshell-last-input-end) t))
  475. (custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark)
  476. (defsubst eshell-goto-input-start ()
  477. "Goto the start of the last command input.
  478. Putting this function on `eshell-pre-command-hook' will mimic Plan 9's
  479. 9term behavior."
  480. (goto-char eshell-last-input-start))
  481. (custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark)
  482. (defsubst eshell-interactive-print (string)
  483. "Print STRING to the eshell display buffer."
  484. (eshell-output-filter nil string))
  485. (defsubst eshell-begin-on-new-line ()
  486. "This function outputs a newline if not at beginning of line."
  487. (save-excursion
  488. (goto-char eshell-last-output-end)
  489. (or (bolp)
  490. (eshell-interactive-print "\n"))))
  491. (defsubst eshell-reset (&optional no-hooks)
  492. "Output a prompt on a new line, aborting any current input.
  493. If NO-HOOKS is non-nil, then `eshell-post-command-hook' won't be run."
  494. (goto-char (point-max))
  495. (setq eshell-last-input-start (point-marker)
  496. eshell-last-input-end (point-marker)
  497. eshell-last-output-start (point-marker)
  498. eshell-last-output-block-begin (point)
  499. eshell-last-output-end (point-marker))
  500. (eshell-begin-on-new-line)
  501. (unless no-hooks
  502. (run-hooks 'eshell-post-command-hook)
  503. (goto-char (point-max))))
  504. (defun eshell-parse-command-input (beg end &optional args)
  505. "Parse the command input from BEG to END.
  506. The difference is that `eshell-parse-command' expects a complete
  507. command string (and will error if it doesn't get one), whereas this
  508. function will inform the caller whether more input is required.
  509. If nil is returned, more input is necessary (probably because a
  510. multi-line input string wasn't terminated properly). Otherwise, it
  511. will return the parsed command."
  512. (let (delim command)
  513. (if (setq delim
  514. (catch 'eshell-incomplete
  515. (ignore
  516. (setq command (eshell-parse-command (cons beg end)
  517. args t)))))
  518. (ignore
  519. (message "Expecting completion of delimiter %c ..."
  520. (if (listp delim)
  521. (car delim)
  522. delim)))
  523. command)))
  524. (defun eshell-update-markers (pmark)
  525. "Update the input and output markers relative to point and PMARK."
  526. (set-marker eshell-last-input-start pmark)
  527. (set-marker eshell-last-input-end (point))
  528. (set-marker eshell-last-output-end (point)))
  529. (defun eshell-queue-input (&optional use-region)
  530. "Queue the current input text for execution by Eshell.
  531. Particularly, don't send the text to the current process, even if it's
  532. waiting for input."
  533. (interactive "P")
  534. (eshell-send-input use-region t))
  535. (defun eshell-send-input (&optional use-region queue-p no-newline)
  536. "Send the input received to Eshell for parsing and processing.
  537. After `eshell-last-output-end', sends all text from that marker to
  538. point as input. Before that marker, calls `eshell-get-old-input' to
  539. retrieve old input, copies it to the end of the buffer, and sends it.
  540. If USE-REGION is non-nil, the current region (between point and mark)
  541. will be used as input.
  542. If QUEUE-P is non-nil, input will be queued until the next prompt,
  543. rather than sent to the currently active process. If no process, the
  544. input is processed immediately.
  545. If NO-NEWLINE is non-nil, the input is sent without an implied final
  546. newline."
  547. (interactive "P")
  548. ;; Note that the input string does not include its terminal newline.
  549. (let ((proc-running-p (and (eshell-interactive-process)
  550. (not queue-p)))
  551. (inhibit-point-motion-hooks t)
  552. after-change-functions)
  553. (unless (and proc-running-p
  554. (not (eq (process-status
  555. (eshell-interactive-process)) 'run)))
  556. (if (or proc-running-p
  557. (>= (point) eshell-last-output-end))
  558. (goto-char (point-max))
  559. (let ((copy (eshell-get-old-input use-region)))
  560. (goto-char eshell-last-output-end)
  561. (insert-and-inherit copy)))
  562. (unless (or no-newline
  563. (and eshell-send-direct-to-subprocesses
  564. proc-running-p))
  565. (insert-before-markers-and-inherit ?\n))
  566. (if proc-running-p
  567. (progn
  568. (eshell-update-markers eshell-last-output-end)
  569. (if (or eshell-send-direct-to-subprocesses
  570. (= eshell-last-input-start eshell-last-input-end))
  571. (unless no-newline
  572. (process-send-string (eshell-interactive-process) "\n"))
  573. (process-send-region (eshell-interactive-process)
  574. eshell-last-input-start
  575. eshell-last-input-end)))
  576. (if (= eshell-last-output-end (point))
  577. (run-hooks 'eshell-post-command-hook)
  578. (let (input)
  579. (eshell-condition-case err
  580. (progn
  581. (setq input (buffer-substring-no-properties
  582. eshell-last-output-end (1- (point))))
  583. (run-hook-with-args 'eshell-expand-input-functions
  584. eshell-last-output-end (1- (point)))
  585. (let ((cmd (eshell-parse-command-input
  586. eshell-last-output-end (1- (point)))))
  587. (when cmd
  588. (eshell-update-markers eshell-last-output-end)
  589. (setq input (buffer-substring-no-properties
  590. eshell-last-input-start
  591. (1- eshell-last-input-end)))
  592. (run-hooks 'eshell-input-filter-functions)
  593. (and (catch 'eshell-terminal
  594. (ignore
  595. (if (eshell-invoke-directly cmd input)
  596. (eval cmd)
  597. (eshell-eval-command cmd input))))
  598. (eshell-life-is-too-much)))))
  599. (quit
  600. (eshell-reset t)
  601. (run-hooks 'eshell-post-command-hook)
  602. (signal 'quit nil))
  603. (error
  604. (eshell-reset t)
  605. (eshell-interactive-print
  606. (concat (error-message-string err) "\n"))
  607. (run-hooks 'eshell-post-command-hook)
  608. (insert-and-inherit input)))))))))
  609. (defsubst eshell-kill-new ()
  610. "Add the last input text to the kill ring."
  611. (kill-ring-save eshell-last-input-start eshell-last-input-end))
  612. (custom-add-option 'eshell-input-filter-functions 'eshell-kill-new)
  613. (defun eshell-output-filter (process string)
  614. "Send the output from PROCESS (STRING) to the interactive display.
  615. This is done after all necessary filtering has been done."
  616. (let ((oprocbuf (if process (process-buffer process)
  617. (current-buffer)))
  618. (inhibit-point-motion-hooks t)
  619. after-change-functions)
  620. (let ((functions eshell-preoutput-filter-functions))
  621. (while (and functions string)
  622. (setq string (funcall (car functions) string))
  623. (setq functions (cdr functions))))
  624. (if (and string oprocbuf (buffer-name oprocbuf))
  625. (let (opoint obeg oend)
  626. (with-current-buffer oprocbuf
  627. (setq opoint (point))
  628. (setq obeg (point-min))
  629. (setq oend (point-max))
  630. (let ((buffer-read-only nil)
  631. (nchars (length string))
  632. (ostart nil))
  633. (widen)
  634. (goto-char eshell-last-output-end)
  635. (setq ostart (point))
  636. (if (<= (point) opoint)
  637. (setq opoint (+ opoint nchars)))
  638. (if (< (point) obeg)
  639. (setq obeg (+ obeg nchars)))
  640. (if (<= (point) oend)
  641. (setq oend (+ oend nchars)))
  642. (insert-before-markers string)
  643. (if (= (window-start (selected-window)) (point))
  644. (set-window-start (selected-window)
  645. (- (point) nchars)))
  646. (if (= (point) eshell-last-input-end)
  647. (set-marker eshell-last-input-end
  648. (- eshell-last-input-end nchars)))
  649. (set-marker eshell-last-output-start ostart)
  650. (set-marker eshell-last-output-end (point))
  651. (force-mode-line-update))
  652. (narrow-to-region obeg oend)
  653. (goto-char opoint)
  654. (eshell-run-output-filters))))))
  655. (defun eshell-run-output-filters ()
  656. "Run the `eshell-output-filter-functions' on the current output."
  657. (save-current-buffer
  658. (run-hooks 'eshell-output-filter-functions))
  659. (setq eshell-last-output-block-begin
  660. (marker-position eshell-last-output-end)))
  661. ;;; jww (1999-10-23): this needs testing
  662. (defun eshell-preinput-scroll-to-bottom ()
  663. "Go to the end of buffer in all windows showing it.
  664. Movement occurs if point in the selected window is not after the
  665. process mark, and `this-command' is an insertion command. Insertion
  666. commands recognized are `self-insert-command', `yank', and
  667. `hilit-yank'. Depends on the value of
  668. `eshell-scroll-to-bottom-on-input'.
  669. This function should be a pre-command hook."
  670. (if (memq this-command '(self-insert-command yank hilit-yank))
  671. (let* ((selected (selected-window))
  672. (current (current-buffer))
  673. (scroll eshell-scroll-to-bottom-on-input))
  674. (if (< (point) eshell-last-output-end)
  675. (if (eq scroll 'this)
  676. (goto-char (point-max))
  677. (walk-windows
  678. (function
  679. (lambda (window)
  680. (when (and (eq (window-buffer window) current)
  681. (or (eq scroll t) (eq scroll 'all)))
  682. (select-window window)
  683. (goto-char (point-max))
  684. (select-window selected))))
  685. nil t))))))
  686. ;;; jww (1999-10-23): this needs testing
  687. (defun eshell-postoutput-scroll-to-bottom ()
  688. "Go to the end of buffer in all windows showing it.
  689. Does not scroll if the current line is the last line in the buffer.
  690. Depends on the value of `eshell-scroll-to-bottom-on-output' and
  691. `eshell-scroll-show-maximum-output'.
  692. This function should be in the list `eshell-output-filter-functions'."
  693. (let* ((selected (selected-window))
  694. (current (current-buffer))
  695. (scroll eshell-scroll-to-bottom-on-output))
  696. (unwind-protect
  697. (walk-windows
  698. (function
  699. (lambda (window)
  700. (if (eq (window-buffer window) current)
  701. (progn
  702. (select-window window)
  703. (if (and (< (point) eshell-last-output-end)
  704. (or (eq scroll t) (eq scroll 'all)
  705. ;; Maybe user wants point to jump to end.
  706. (and (eq scroll 'this)
  707. (eq selected window))
  708. (and (eq scroll 'others)
  709. (not (eq selected window)))
  710. ;; If point was at the end, keep it at end.
  711. (>= (point) eshell-last-output-start)))
  712. (goto-char eshell-last-output-end))
  713. ;; Optionally scroll so that the text
  714. ;; ends at the bottom of the window.
  715. (if (and eshell-scroll-show-maximum-output
  716. (>= (point) eshell-last-output-end))
  717. (save-excursion
  718. (goto-char (point-max))
  719. (recenter -1)))
  720. (select-window selected)))))
  721. nil t)
  722. (set-buffer current))))
  723. (defun eshell-beginning-of-input ()
  724. "Return the location of the start of the previous input."
  725. eshell-last-input-start)
  726. (defun eshell-beginning-of-output ()
  727. "Return the location of the end of the previous output block."
  728. eshell-last-input-end)
  729. (defun eshell-end-of-output ()
  730. "Return the location of the end of the previous output block."
  731. (if (eshell-using-module 'eshell-prompt)
  732. eshell-last-output-start
  733. eshell-last-output-end))
  734. (defun eshell-kill-output ()
  735. "Kill all output from interpreter since last input.
  736. Does not delete the prompt."
  737. (interactive)
  738. (save-excursion
  739. (goto-char (eshell-beginning-of-output))
  740. (insert "*** output flushed ***\n")
  741. (delete-region (point) (eshell-end-of-output))))
  742. (defun eshell-show-output (&optional arg)
  743. "Display start of this batch of interpreter output at top of window.
  744. Sets mark to the value of point when this command is run.
  745. With a prefix argument, narrows region to last command output."
  746. (interactive "P")
  747. (goto-char (eshell-beginning-of-output))
  748. (set-window-start (selected-window)
  749. (save-excursion
  750. (goto-char (eshell-beginning-of-input))
  751. (line-beginning-position)))
  752. (if arg
  753. (narrow-to-region (eshell-beginning-of-output)
  754. (eshell-end-of-output)))
  755. (eshell-end-of-output))
  756. (defun eshell-mark-output (&optional arg)
  757. "Display start of this batch of interpreter output at top of window.
  758. Sets mark to the value of point when this command is run.
  759. With a prefix argument, narrows region to last command output."
  760. (interactive "P")
  761. (push-mark (eshell-show-output arg)))
  762. (defun eshell-kill-input ()
  763. "Kill all text from last stuff output by interpreter to point."
  764. (interactive)
  765. (if (> (point) eshell-last-output-end)
  766. (kill-region eshell-last-output-end (point))
  767. (let ((here (point)))
  768. (eshell-bol)
  769. (kill-region (point) here))))
  770. (defun eshell-show-maximum-output (&optional interactive)
  771. "Put the end of the buffer at the bottom of the window.
  772. When run interactively, widen the buffer first."
  773. (interactive "p")
  774. (if interactive
  775. (widen))
  776. (goto-char (point-max))
  777. (recenter -1))
  778. (defun eshell-get-old-input (&optional use-current-region)
  779. "Return the command input on the current line."
  780. (if use-current-region
  781. (buffer-substring (min (point) (mark))
  782. (max (point) (mark)))
  783. (save-excursion
  784. (beginning-of-line)
  785. (and eshell-skip-prompt-function
  786. (funcall eshell-skip-prompt-function))
  787. (let ((beg (point)))
  788. (end-of-line)
  789. (buffer-substring beg (point))))))
  790. (defun eshell-copy-old-input ()
  791. "Insert after prompt old input at point as new input to be edited."
  792. (interactive)
  793. (let ((input (eshell-get-old-input)))
  794. (goto-char eshell-last-output-end)
  795. (insert-and-inherit input)))
  796. (defun eshell/exit ()
  797. "Leave or kill the Eshell buffer, depending on `eshell-kill-on-exit'."
  798. (throw 'eshell-terminal t))
  799. (defun eshell-life-is-too-much ()
  800. "Kill the current buffer (or bury it). Good-bye Eshell."
  801. (interactive)
  802. (if (not eshell-kill-on-exit)
  803. (bury-buffer)
  804. (kill-buffer (current-buffer))))
  805. (defun eshell-truncate-buffer ()
  806. "Truncate the buffer to `eshell-buffer-maximum-lines'.
  807. This function could be on `eshell-output-filter-functions' or bound to
  808. a key."
  809. (interactive)
  810. (save-excursion
  811. (goto-char eshell-last-output-end)
  812. (let ((lines (count-lines 1 (point)))
  813. (inhibit-read-only t))
  814. (forward-line (- eshell-buffer-maximum-lines))
  815. (beginning-of-line)
  816. (let ((pos (point)))
  817. (if (bobp)
  818. (if (called-interactively-p 'interactive)
  819. (message "Buffer too short to truncate"))
  820. (delete-region (point-min) (point))
  821. (if (called-interactively-p 'interactive)
  822. (message "Truncated buffer from %d to %d lines (%.1fk freed)"
  823. lines eshell-buffer-maximum-lines
  824. (/ pos 1024.0))))))))
  825. (custom-add-option 'eshell-output-filter-functions
  826. 'eshell-truncate-buffer)
  827. (defun eshell-send-invisible (str)
  828. "Read a string without echoing.
  829. Then send it to the process running in the current buffer."
  830. (interactive "P") ; Defeat snooping via C-x ESC ESC
  831. (let ((str (read-passwd
  832. (format "%s Password: "
  833. (process-name (eshell-interactive-process))))))
  834. (if (stringp str)
  835. (process-send-string (eshell-interactive-process)
  836. (concat str "\n"))
  837. (message "Warning: text will be echoed"))))
  838. (defun eshell-watch-for-password-prompt ()
  839. "Prompt in the minibuffer for password and send without echoing.
  840. This function uses `eshell-send-invisible' to read and send a password to the
  841. buffer's process if STRING contains a password prompt defined by
  842. `eshell-password-prompt-regexp'.
  843. This function could be in the list `eshell-output-filter-functions'."
  844. (when (eshell-interactive-process)
  845. (save-excursion
  846. (goto-char eshell-last-output-block-begin)
  847. (beginning-of-line)
  848. (if (re-search-forward eshell-password-prompt-regexp
  849. eshell-last-output-end t)
  850. (eshell-send-invisible nil)))))
  851. (custom-add-option 'eshell-output-filter-functions
  852. 'eshell-watch-for-password-prompt)
  853. (defun eshell-handle-control-codes ()
  854. "Act properly when certain control codes are seen."
  855. (save-excursion
  856. (let ((orig (point)))
  857. (goto-char eshell-last-output-block-begin)
  858. (unless (eolp)
  859. (beginning-of-line))
  860. (while (< (point) eshell-last-output-end)
  861. (let ((char (char-after)))
  862. (cond
  863. ((eq char ?\r)
  864. (if (< (1+ (point)) eshell-last-output-end)
  865. (if (memq (char-after (1+ (point)))
  866. '(?\n ?\r))
  867. (delete-char 1)
  868. (let ((end (1+ (point))))
  869. (beginning-of-line)
  870. (delete-region (point) end)))
  871. (add-text-properties (point) (1+ (point))
  872. '(invisible t))
  873. (forward-char)))
  874. ((eq char ?\a)
  875. (delete-char 1)
  876. (beep))
  877. ((eq char ?\C-h)
  878. (delete-backward-char 1)
  879. (delete-char 1))
  880. (t
  881. (forward-char))))))))
  882. (custom-add-option 'eshell-output-filter-functions
  883. 'eshell-handle-control-codes)
  884. (autoload 'ansi-color-apply-on-region "ansi-color")
  885. (defun eshell-handle-ansi-color ()
  886. "Handle ANSI color codes."
  887. (ansi-color-apply-on-region eshell-last-output-start
  888. eshell-last-output-end))
  889. (custom-add-option 'eshell-output-filter-functions
  890. 'eshell-handle-ansi-color)
  891. ;;; esh-mode.el ends here