1 ;;; esh-io.el --- I/O management
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: John Wiegley <johnw@gnu.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 (eval-when-compile (require 'esh-maint
))
29 (defgroup eshell-io nil
30 "Eshell's I/O management code provides a scheme for treating many
31 different kinds of objects -- symbols, files, buffers, etc. -- as
32 though they were files."
38 ;; At the moment, only output redirection is supported in Eshell. To
39 ;; use input redirection, the following syntax will work, assuming
40 ;; that the command after the pipe is always an external command:
42 ;; cat <file> | <command>
44 ;; Otherwise, output redirection and piping are provided in a manner
45 ;; consistent with most shells. Therefore, only unique features are
50 ;; To insert at the location of point in a buffer, use '>>>':
52 ;; echo alpha >>> #<buffer *scratch*>;
56 ;; A few pseudo-devices are provided, since Emacs cannot write
57 ;; directly to a UNIX device file:
59 ;; echo alpha > /dev/null ; the bit bucket
60 ;; echo alpha > /dev/kill ; set the kill ring
61 ;; echo alpha >> /dev/clip ; append to the clipboard
63 ;;;_* Multiple output targets
65 ;; Eshell can write to multiple output targets, including pipes.
68 ;; (+ 1 2) > a > b > c ; prints number to all three files
69 ;; (+ 1 2) > a | wc ; prints to 'a', and pipes to 'wc'
73 (defcustom eshell-io-load-hook
'(eshell-io-initialize)
74 "*A hook that gets run when `eshell-io' is loaded."
78 (defcustom eshell-number-of-handles
3
79 "*The number of file handles that eshell supports.
80 Currently this is standard input, output and error. But even all of
81 these Emacs does not currently support with asynchronous processes
82 \(which is what eshell uses so that you can continue doing work in
87 (defcustom eshell-output-handle
1
88 "*The index of the standard output handle."
92 (defcustom eshell-error-handle
2
93 "*The index of the standard error handle."
97 (defcustom eshell-buffer-shorthand nil
98 "*If non-nil, a symbol name can be used for a buffer in redirection.
99 If nil, redirecting to a buffer requires buffer name syntax. If this
100 variable is set, redirection directly to Lisp symbols will be
105 echo hello > '*scratch* ; works if `eshell-buffer-shorthand' is t
106 echo hello > #<buffer *scratch*> ; always works"
110 (defcustom eshell-print-queue-size
5
111 "*The size of the print queue, for doing buffered printing.
112 This is basically a speed enhancement, to avoid blocking the Lisp code
113 from executing while Emacs is redisplaying."
117 (defcustom eshell-virtual-targets
118 '(("/dev/eshell" eshell-interactive-print nil
)
119 ("/dev/kill" (lambda (mode)
120 (if (eq mode
'overwrite
)
122 'eshell-kill-append
) t
)
123 ("/dev/clip" (lambda (mode)
124 (if (eq mode
'overwrite
)
125 (let ((x-select-enable-clipboard t
))
127 'eshell-clipboard-append
) t
))
128 "*Map virtual devices name to Emacs Lisp functions.
129 If the user specifies any of the filenames above as a redirection
130 target, the function in the second element will be called.
132 If the third element is non-nil, the redirection mode is passed as an
133 argument (which is the symbol `overwrite', `append' or `insert'), and
134 the function is expected to return another function -- which is the
135 output function. Otherwise, the second element itself is the output
138 The output function is then called repeatedly with single strings,
139 which represents successive pieces of the output of the command, until nil
140 is passed, meaning EOF.
142 NOTE: /dev/null is handled specially as a virtual target, and should
143 not be added to this variable."
145 (list (string :tag
"Target")
147 (choice (const :tag
"Func returns output-func" t
)
148 (const :tag
"Func is output-func" nil
))))
151 (put 'eshell-virtual-targets
'risky-local-variable t
)
153 ;;; Internal Variables:
155 (defvar eshell-current-handles nil
)
157 (defvar eshell-last-command-status
0
158 "The exit code from the last command. 0 if successful.")
160 (defvar eshell-last-command-result nil
161 "The result of the last command. Not related to success.")
163 (defvar eshell-output-file-buffer nil
164 "If non-nil, the current buffer is a file output buffer.")
166 (defvar eshell-print-count
)
167 (defvar eshell-current-redirections
)
171 (defun eshell-io-initialize ()
172 "Initialize the I/O subsystem code."
173 (add-hook 'eshell-parse-argument-hook
174 'eshell-parse-redirection nil t
)
175 (make-local-variable 'eshell-current-redirections
)
176 (add-hook 'eshell-pre-rewrite-command-hook
177 'eshell-strip-redirections nil t
)
178 (add-hook 'eshell-post-rewrite-command-hook
179 'eshell-apply-redirections nil t
))
181 (defun eshell-parse-redirection ()
182 "Parse an output redirection, such as '2>'."
183 (if (and (not eshell-current-quoted
)
184 (looking-at "\\([0-9]\\)?\\(<\\|>+\\)&?\\([0-9]\\)?\\s-*"))
185 (if eshell-current-argument
187 (let ((sh (match-string 1))
188 (oper (match-string 2))
189 ; (th (match-string 3))
191 (if (string= oper
"<")
192 (error "Eshell does not support input redirection"))
195 (list 'eshell-set-output-handle
196 (or (and sh
(string-to-number sh
)) 1)
198 (aref [overwrite append insert
]
199 (1- (length oper
)))))
200 (goto-char (match-end 0))))))))
202 (defun eshell-strip-redirections (terms)
203 "Rewrite any output redirections in TERMS."
204 (setq eshell-current-redirections
(list t
))
208 (if (not (and (consp (car tt
))
209 (eq (caar tt
) 'eshell-set-output-handle
)))
213 (error "Missing redirection target"))
214 (nconc eshell-current-redirections
216 (append (car tt
) (list (cadr tt
))))))
217 (setcdr tl
(cddr tt
))
218 (setq tt
(cddr tt
))))
219 (setq eshell-current-redirections
220 (cdr eshell-current-redirections
))))
222 (defun eshell-apply-redirections (cmdsym)
223 "Apply any redirection which were specified for COMMAND."
224 (if eshell-current-redirections
226 (append (list 'progn
)
227 eshell-current-redirections
228 (list (symbol-value cmdsym
))))))
230 (defun eshell-create-handles
231 (standard-output output-mode
&optional standard-error error-mode
)
232 "Create a new set of file handles for a command.
233 The default location for standard output and standard error will go to
234 STANDARD-OUTPUT and STANDARD-ERROR, respectively.
235 OUTPUT-MODE and ERROR-MODE are either `overwrite', `append' or `insert';
236 a nil value of mode defaults to `insert'."
237 (let ((handles (make-vector eshell-number-of-handles nil
))
238 (output-target (eshell-get-target standard-output output-mode
))
239 (error-target (eshell-get-target standard-error error-mode
)))
240 (aset handles eshell-output-handle
(cons output-target
1))
242 (aset handles eshell-error-handle
(cons error-target
1))
243 (aset handles eshell-error-handle
(cons output-target
1)))
246 (defun eshell-protect-handles (handles)
247 "Protect the handles in HANDLES from a being closed."
249 (while (< idx eshell-number-of-handles
)
250 (if (aref handles idx
)
251 (setcdr (aref handles idx
)
252 (1+ (cdr (aref handles idx
)))))
253 (setq idx
(1+ idx
))))
256 (defun eshell-close-target (target status
)
257 "Close an output TARGET, passing STATUS as the result.
258 STATUS should be non-nil on successful termination of the output."
260 ((symbolp target
) nil
)
262 ;; If we were redirecting to a file, save the file and close the
265 (let ((buf (marker-buffer target
)))
266 (when buf
; somebody's already killed it!
269 (when eshell-output-file-buffer
271 (when (eq eshell-output-file-buffer t
)
272 (or status
(set-buffer-modified-p nil
))
273 (kill-buffer buf
)))))))
275 ;; If we're redirecting to a process (via a pipe, or process
276 ;; redirection), send it EOF so that it knows we're finished.
277 ((eshell-processp target
)
278 (if (eq (process-status target
) 'run
)
279 (process-send-eof target
)))
281 ;; A plain function redirection needs no additional arguments
284 (funcall target status
))
286 ;; But a more complicated function redirection (which can only
287 ;; happen with aliases at the moment) has arguments that need to be
288 ;; passed along with it.
290 (apply (car target
) status
(cdr target
)))))
292 (defun eshell-close-handles (exit-code &optional result handles
)
293 "Close all of the current handles, taking refcounts into account.
294 EXIT-CODE is the process exit code; mainly, it is zero, if the command
295 completed successfully. RESULT is the quoted value of the last
296 command. If nil, then the meta variables for keeping track of the
297 last execution result should not be changed."
299 (assert (or (not result
) (eq (car result
) 'quote
)))
300 (setq eshell-last-command-status exit-code
301 eshell-last-command-result
(cadr result
))
302 (while (< idx eshell-number-of-handles
)
303 (let ((handles (or handles eshell-current-handles
)))
304 (when (aref handles idx
)
305 (setcdr (aref handles idx
)
306 (1- (cdr (aref handles idx
))))
307 (when (= (cdr (aref handles idx
)) 0)
308 (let ((target (car (aref handles idx
))))
309 (if (not (listp target
))
310 (eshell-close-target target
(= exit-code
0))
312 (eshell-close-target (car target
) (= exit-code
0))
313 (setq target
(cdr target
)))))
314 (setcar (aref handles idx
) nil
))))
318 (defun eshell-kill-append (string)
319 "Call `kill-append' with STRING, if it is indeed a string."
321 (kill-append string nil
)))
323 (defun eshell-clipboard-append (string)
324 "Call `kill-append' with STRING, if it is indeed a string."
326 (let ((x-select-enable-clipboard t
))
327 (kill-append string nil
))))
329 (defun eshell-get-target (target &optional mode
)
330 "Convert TARGET, which is a raw argument, into a valid output target.
331 MODE is either `overwrite', `append' or `insert'; if it is omitted or nil,
332 it defaults to `insert'."
333 (setq mode
(or mode
'insert
))
336 (let ((redir (assoc target eshell-virtual-targets
)))
339 (funcall (nth 1 redir
) mode
)
341 (let* ((exists (get-file-buffer target
))
342 (buf (find-file-noselect target t
)))
343 (with-current-buffer buf
345 (error "Cannot write to read-only file `%s'" target
))
346 (set (make-local-variable 'eshell-output-file-buffer
)
347 (if (eq exists buf
) 0 t
))
348 (cond ((eq mode
'overwrite
)
351 (goto-char (point-max))))
354 ((or (bufferp target
)
355 (and (boundp 'eshell-buffer-shorthand
)
356 (symbol-value 'eshell-buffer-shorthand
)
358 (not (memq target
'(t nil
)))))
359 (let ((buf (if (bufferp target
)
362 (symbol-name target
)))))
363 (with-current-buffer buf
364 (cond ((eq mode
'overwrite
)
367 (goto-char (point-max))))
370 ((functionp target
) nil
)
373 (if (eq mode
'overwrite
)
377 ((or (eshell-processp target
)
382 (error "Invalid redirection target: %s"
383 (eshell-stringify target
)))))
386 (defvar grep-null-device
))
388 (defun eshell-set-output-handle (index mode
&optional target
)
389 "Set handle INDEX, using MODE, to point to TARGET."
391 (if (and (stringp target
)
393 ((boundp 'null-device
)
394 (string= target null-device
))
395 ((boundp 'grep-null-device
)
396 (string= target grep-null-device
))
398 (string= target
"/dev/null")))
399 (aset eshell-current-handles index nil
)
400 (let ((where (eshell-get-target target mode
))
401 (current (car (aref eshell-current-handles index
))))
402 (if (and (listp current
)
403 (not (member where current
)))
404 (setq current
(append current
(list where
)))
405 (setq current
(list where
)))
406 (if (not (aref eshell-current-handles index
))
407 (aset eshell-current-handles index
(cons nil
1)))
408 (setcar (aref eshell-current-handles index
) current
)))))
410 (defun eshell-interactive-output-p ()
411 "Return non-nil if current handles are bound for interactive display."
412 (and (eq (car (aref eshell-current-handles
413 eshell-output-handle
)) t
)
414 (eq (car (aref eshell-current-handles
415 eshell-error-handle
)) t
)))
417 (defvar eshell-print-queue nil
)
418 (defvar eshell-print-queue-count -
1)
420 (defun eshell-flush (&optional reset-p
)
421 "Flush out any lines that have been queued for printing.
422 Must be called before printing begins with -1 as its argument, and
423 after all printing is over with no argument."
426 (setq eshell-print-queue nil
427 eshell-print-queue-count reset-p
)
428 (if eshell-print-queue
429 (eshell-print eshell-print-queue
))
432 (defun eshell-init-print-buffer ()
433 "Initialize the buffered printing queue."
436 (defun eshell-buffered-print (&rest strings
)
437 "A buffered print -- *for strings only*."
438 (if (< eshell-print-queue-count
0)
440 (eshell-print (apply 'concat strings
))
441 (setq eshell-print-queue-count
0))
442 (if (= eshell-print-queue-count eshell-print-queue-size
)
444 (setq eshell-print-queue
445 (concat eshell-print-queue
(apply 'concat strings
))
446 eshell-print-queue-count
(1+ eshell-print-queue-count
))))
448 (defsubst eshell-print
(object)
449 "Output OBJECT to the standard output handle."
450 (eshell-output-object object eshell-output-handle
))
452 (defsubst eshell-error
(object)
453 "Output OBJECT to the standard error handle."
454 (eshell-output-object object eshell-error-handle
))
456 (defsubst eshell-errorn
(object)
457 "Output OBJECT followed by a newline to the standard error handle."
458 (eshell-error object
)
461 (defsubst eshell-printn
(object)
462 "Output OBJECT followed by a newline to the standard output handle."
463 (eshell-print object
)
466 (defun eshell-output-object-to-target (object target
)
467 "Insert OBJECT into TARGET.
468 Returns what was actually sent, or nil if nothing was sent."
471 (funcall target object
))
474 (if (eq target t
) ; means "print to display"
475 (eshell-output-filter nil
(eshell-stringify object
))
476 (if (not (symbol-value target
))
478 (setq object
(eshell-stringify object
))
479 (if (not (stringp (symbol-value target
)))
480 (set target
(eshell-stringify
481 (symbol-value target
))))
482 (set target
(concat (symbol-value target
) object
)))))
485 (if (buffer-live-p (marker-buffer target
))
486 (with-current-buffer (marker-buffer target
)
487 (let ((moving (= (point) target
)))
490 (unless (stringp object
)
491 (setq object
(eshell-stringify object
)))
492 (insert-and-inherit object
)
493 (set-marker target
(point-marker)))
495 (goto-char target
))))))
497 ((eshell-processp target
)
498 (when (eq (process-status target
) 'run
)
499 (unless (stringp object
)
500 (setq object
(eshell-stringify object
)))
501 (process-send-string target object
)))
504 (apply (car target
) object
(cdr target
))))
507 (defun eshell-output-object (object &optional handle-index handles
)
508 "Insert OBJECT, using HANDLE-INDEX specifically)."
509 (let ((target (car (aref (or handles eshell-current-handles
)
510 (or handle-index eshell-output-handle
)))))
511 (if (and target
(not (listp target
)))
512 (eshell-output-object-to-target object target
)
514 (eshell-output-object-to-target object
(car target
))
515 (setq target
(cdr target
))))))
519 ;;; arch-tag: 9ca2080f-d5e0-4b26-aa0b-d59194a905a2
520 ;;; esh-io.el ends here