esh-mode.el 36 KB

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