Mercurial > emacs
annotate lisp/eshell/esh-io.el @ 31881:c9d93e8e8cbd
*** empty log message ***
author | Gerd Moellmann <gerd@gnu.org> |
---|---|
date | Mon, 25 Sep 2000 15:07:45 +0000 |
parents | 3099993cba0f |
children | 8e57189d61b4 |
rev | line source |
---|---|
29876 | 1 ;;; esh-io --- I/O management |
2 | |
29934
34b1ab9d583d
Change spelling of the Free Software Foundation.
Gerd Moellmann <gerd@gnu.org>
parents:
29876
diff
changeset
|
3 ;; Copyright (C) 1999, 2000 Free Software Foundation |
29876 | 4 |
5 ;; This file is part of GNU Emacs. | |
6 | |
7 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
8 ;; it under the terms of the GNU General Public License as published by | |
9 ;; the Free Software Foundation; either version 2, or (at your option) | |
10 ;; any later version. | |
11 | |
12 ;; GNU Emacs is distributed in the hope that it will be useful, | |
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 ;; GNU General Public License for more details. | |
16 | |
17 ;; You should have received a copy of the GNU General Public License | |
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
20 ;; Boston, MA 02111-1307, USA. | |
21 | |
22 (provide 'esh-io) | |
23 | |
24 (eval-when-compile (require 'esh-maint)) | |
25 | |
26 (defgroup eshell-io nil | |
27 "Eshell's I/O management code provides a scheme for treating many | |
28 different kinds of objects -- symbols, files, buffers, etc. -- as | |
29 though they were files." | |
30 :tag "I/O management" | |
31 :group 'eshell) | |
32 | |
33 ;;; Commentary: | |
34 | |
35 ;; At the moment, only output redirection is supported in Eshell. To | |
36 ;; use input redirection, the following syntax will work, assuming | |
37 ;; that the command after the pipe is always an external command: | |
38 ;; | |
39 ;; cat <file> | <command> | |
40 ;; | |
41 ;; Otherwise, output redirection and piping are provided in a manner | |
42 ;; consistent with most shells. Therefore, only unique features are | |
43 ;; mentioned here. | |
44 ;; | |
45 ;;;_* Insertion | |
46 ;; | |
47 ;; To insert at the location of point in a buffer, use '>>>': | |
48 ;; | |
49 ;; echo alpha >>> #<buffer *scratch*>; | |
50 ;; | |
51 ;;;_* Pseudo-devices | |
52 ;; | |
53 ;; A few pseudo-devices are provided, since Emacs cannot write | |
54 ;; directly to a UNIX device file: | |
55 ;; | |
56 ;; echo alpha > /dev/null ; the bit bucket | |
57 ;; echo alpha > /dev/kill ; set the kill ring | |
58 ;; echo alpha >> /dev/clip ; append to the clipboard | |
59 ;; | |
60 ;;;_* Multiple output targets | |
61 ;; | |
62 ;; Eshell can write to multiple output targets, including pipes. | |
63 ;; Example: | |
64 ;; | |
65 ;; (+ 1 2) > a > b > c ; prints number to all three files | |
66 ;; (+ 1 2) > a | wc ; prints to 'a', and pipes to 'wc' | |
67 | |
68 ;;; User Variables: | |
69 | |
70 (defcustom eshell-io-load-hook '(eshell-io-initialize) | |
71 "*A hook that gets run when `eshell-io' is loaded." | |
72 :type 'hook | |
73 :group 'eshell-io) | |
74 | |
75 (defcustom eshell-number-of-handles 3 | |
76 "*The number of file handles that eshell supports. | |
77 Currently this is standard input, output and error. But even all of | |
78 these Emacs does not currently support with asynchronous processes | |
79 \(which is what eshell uses so that you can continue doing work in | |
80 other buffers) ." | |
81 :type 'integer | |
82 :group 'eshell-io) | |
83 | |
84 (defcustom eshell-output-handle 1 | |
85 "*The index of the standard output handle." | |
86 :type 'integer | |
87 :group 'eshell-io) | |
88 | |
89 (defcustom eshell-error-handle 2 | |
90 "*The index of the standard error handle." | |
91 :type 'integer | |
92 :group 'eshell-io) | |
93 | |
94 (defcustom eshell-buffer-shorthand nil | |
95 "*If non-nil, a symbol name can be used for a buffer in redirection. | |
96 If nil, redirecting to a buffer requires buffer name syntax. If this | |
97 variable is set, redirection directly to Lisp symbols will be | |
98 impossible. | |
99 | |
100 Example: | |
101 | |
102 echo hello > '*scratch* ; works if `eshell-buffer-shorthand' is t | |
103 echo hello > #<buffer *scratch*> ; always works" | |
104 :type 'boolean | |
105 :group 'eshell-io) | |
106 | |
107 (defcustom eshell-print-queue-size 5 | |
108 "*The size of the print queue, for doing buffered printing. | |
109 This is basically a speed enhancement, to avoid blocking the Lisp code | |
110 from executing while Emacs is redisplaying." | |
111 :type 'integer | |
112 :group 'eshell-io) | |
113 | |
114 (defcustom eshell-virtual-targets | |
115 '(("/dev/eshell" eshell-interactive-print nil) | |
116 ("/dev/kill" (lambda (mode) | |
117 (if (eq mode 'overwrite) | |
118 (kill-new "")) | |
119 'eshell-kill-append) t) | |
120 ("/dev/clip" (lambda (mode) | |
121 (if (eq mode 'overwrite) | |
122 (let ((x-select-enable-clipboard t)) | |
123 (kill-new ""))) | |
124 'eshell-clipboard-append) t)) | |
125 "*Map virtual devices name to Emacs Lisp functions. | |
126 If the user specifies any of the filenames above as a redirection | |
127 target, the function in the second element will be called. | |
128 | |
129 If the third element is non-nil, the redirection mode is passed as an | |
130 argument (which is the symbol `overwrite', `append' or `insert'), and | |
131 the function is expected to return another function -- which is the | |
132 output function. Otherwise, the second element itself is the output | |
133 function. | |
134 | |
31241 | 135 The output function is then called repeatedly with single strings, |
136 which represents successive pieces of the output of the command, until nil | |
29876 | 137 is passed, meaning EOF. |
138 | |
139 NOTE: /dev/null is handled specially as a virtual target, and should | |
140 not be added to this variable." | |
141 :type '(repeat | |
142 (list (string :tag "Target") | |
143 function | |
144 (choice (const :tag "Func returns output-func" t) | |
145 (const :tag "Func is output-func" nil)))) | |
146 :group 'eshell-io) | |
147 | |
148 (put 'eshell-virtual-targets 'risky-local-variable t) | |
149 | |
150 ;;; Internal Variables: | |
151 | |
152 (defvar eshell-current-handles nil) | |
153 | |
154 (defvar eshell-last-command-status 0 | |
155 "The exit code from the last command. 0 if successful.") | |
156 | |
157 (defvar eshell-last-command-result nil | |
158 "The result of the last command. Not related to success.") | |
159 | |
160 (defvar eshell-output-file-buffer nil | |
161 "If non-nil, the current buffer is a file output buffer.") | |
162 | |
163 (defvar eshell-print-count) | |
164 (defvar eshell-current-redirections) | |
165 | |
166 ;;; Functions: | |
167 | |
168 (defun eshell-io-initialize () | |
169 "Initialize the I/O subsystem code." | |
170 (make-local-hook 'eshell-parse-argument-hook) | |
171 (add-hook 'eshell-parse-argument-hook | |
172 'eshell-parse-redirection nil t) | |
173 (make-local-variable 'eshell-current-redirections) | |
174 (make-local-hook 'eshell-pre-rewrite-command-hook) | |
175 (add-hook 'eshell-pre-rewrite-command-hook | |
176 'eshell-strip-redirections nil t) | |
177 (make-local-hook 'eshell-post-rewrite-command-hook) | |
178 (add-hook 'eshell-post-rewrite-command-hook | |
179 'eshell-apply-redirections nil t)) | |
180 | |
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 | |
186 (eshell-finish-arg) | |
187 (let ((sh (match-string 1)) | |
188 (oper (match-string 2)) | |
189 ; (th (match-string 3)) | |
190 ) | |
191 (if (string= oper "<") | |
192 (error "Eshell does not support input redirection")) | |
193 (eshell-finish-arg | |
194 (prog1 | |
195 (list 'eshell-set-output-handle | |
196 (or (and sh (string-to-int sh)) 1) | |
197 (list 'quote | |
198 (aref [overwrite append insert] | |
199 (1- (length oper))))) | |
200 (goto-char (match-end 0)))))))) | |
201 | |
202 (defun eshell-strip-redirections (terms) | |
203 "Rewrite any output redirections in TERMS." | |
204 (setq eshell-current-redirections (list t)) | |
205 (let ((tl terms) | |
206 (tt (cdr terms))) | |
207 (while tt | |
208 (if (not (and (consp (car tt)) | |
209 (eq (caar tt) 'eshell-set-output-handle))) | |
210 (setq tt (cdr tt) | |
211 tl (cdr tl)) | |
212 (unless (cdr tt) | |
213 (error "Missing redirection target")) | |
214 (nconc eshell-current-redirections | |
215 (list (list 'ignore | |
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)))) | |
221 | |
222 (defun eshell-apply-redirections (cmdsym) | |
223 "Apply any redirection which were specified for COMMAND." | |
224 (if eshell-current-redirections | |
225 (set cmdsym | |
226 (append (list 'progn) | |
227 eshell-current-redirections | |
228 (list (symbol-value cmdsym)))))) | |
229 | |
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 | |
31241 | 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'." | |
29876 | 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)) | |
241 (if standard-error | |
242 (aset handles eshell-error-handle (cons error-target 1)) | |
243 (aset handles eshell-error-handle (cons output-target 1))) | |
244 handles)) | |
245 | |
246 (defun eshell-protect-handles (handles) | |
247 "Protect the handles in HANDLES from a being closed." | |
248 (let ((idx 0)) | |
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)))) | |
254 handles) | |
255 | |
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." | |
259 (cond | |
260 ((symbolp target) nil) | |
261 | |
262 ;; If we were redirecting to a file, save the file and close the | |
263 ;; buffer. | |
264 ((markerp target) | |
265 (let ((buf (marker-buffer target))) | |
266 (when buf ; somebody's already killed it! | |
267 (save-current-buffer | |
268 (set-buffer buf) | |
269 (when eshell-output-file-buffer | |
270 (save-buffer) | |
271 (when (eq eshell-output-file-buffer t) | |
272 (or status (set-buffer-modified-p nil)) | |
273 (kill-buffer buf))))))) | |
274 | |
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. | |
31241 | 277 ((eshell-processp target) |
29876 | 278 (if (eq (process-status target) 'run) |
279 (process-send-eof target))) | |
280 | |
281 ;; A plain function redirection needs no additional arguments | |
282 ;; passed. | |
283 ((functionp target) | |
284 (funcall target status)) | |
285 | |
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. | |
289 ((consp target) | |
290 (apply (car target) status (cdr target))))) | |
291 | |
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." | |
298 (let ((idx 0)) | |
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)) | |
311 (while target | |
312 (eshell-close-target (car target) (= exit-code 0)) | |
313 (setq target (cdr target))))) | |
314 (setcar (aref handles idx) nil)))) | |
315 (setq idx (1+ idx))) | |
316 nil)) | |
317 | |
318 (defun eshell-kill-append (string) | |
319 "Call `kill-append' with STRING, if it is indeed a string." | |
320 (if (stringp string) | |
321 (kill-append string nil))) | |
322 | |
323 (defun eshell-clipboard-append (string) | |
324 "Call `kill-append' with STRING, if it is indeed a string." | |
325 (if (stringp string) | |
326 (let ((x-select-enable-clipboard t)) | |
327 (kill-append string nil)))) | |
328 | |
329 (defun eshell-get-target (target &optional mode) | |
330 "Convert TARGET, which is a raw argument, into a valid output target. | |
31241 | 331 MODE is either `overwrite', `append' or `insert'; if it is omitted or nil, |
332 it defaults to `insert'." | |
29876 | 333 (setq mode (or mode 'insert)) |
334 (cond | |
335 ((stringp target) | |
336 (let ((redir (assoc target eshell-virtual-targets))) | |
337 (if redir | |
338 (if (nth 2 redir) | |
339 (funcall (nth 1 redir) mode) | |
340 (nth 1 redir)) | |
341 (let* ((exists (get-file-buffer target)) | |
342 (buf (find-file-noselect target t))) | |
343 (with-current-buffer buf | |
344 (if buffer-read-only | |
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) | |
349 (erase-buffer)) | |
350 ((eq mode 'append) | |
351 (goto-char (point-max)))) | |
352 (point-marker)))))) | |
353 ((or (bufferp target) | |
354 (and (boundp 'eshell-buffer-shorthand) | |
355 (symbol-value 'eshell-buffer-shorthand) | |
356 (symbolp target))) | |
357 (let ((buf (if (bufferp target) | |
358 target | |
359 (get-buffer-create | |
360 (symbol-name target))))) | |
361 (with-current-buffer buf | |
362 (cond ((eq mode 'overwrite) | |
363 (erase-buffer)) | |
364 ((eq mode 'append) | |
365 (goto-char (point-max)))) | |
366 (point-marker)))) | |
367 ((functionp target) | |
368 nil) | |
369 ((symbolp target) | |
370 (if (eq mode 'overwrite) | |
371 (set target nil)) | |
372 target) | |
31241 | 373 ((or (eshell-processp target) |
29876 | 374 (markerp target)) |
375 target) | |
376 (t | |
377 (error "Illegal redirection target: %s" | |
378 (eshell-stringify target))))) | |
379 | |
380 (eval-when-compile | |
381 (defvar grep-null-device)) | |
382 | |
383 (defun eshell-set-output-handle (index mode &optional target) | |
384 "Set handle INDEX, using MODE, to point to TARGET." | |
385 (when target | |
386 (if (and (stringp target) | |
387 (or (cond | |
388 ((boundp 'null-device) | |
389 (string= target null-device)) | |
390 ((boundp 'grep-null-device) | |
391 (string= target grep-null-device)) | |
392 (t nil)) | |
393 (string= target "/dev/null"))) | |
394 (aset eshell-current-handles index nil) | |
395 (let ((where (eshell-get-target target mode)) | |
396 (current (car (aref eshell-current-handles index)))) | |
397 (if (and (listp current) | |
398 (not (member where current))) | |
399 (setq current (append current (list where))) | |
31241 | 400 (setq current where)) |
29876 | 401 (if (not (aref eshell-current-handles index)) |
402 (aset eshell-current-handles index (cons nil 1))) | |
403 (setcar (aref eshell-current-handles index) current))))) | |
404 | |
405 (defun eshell-interactive-output-p () | |
406 "Return non-nil if current handles are bound for interactive display." | |
407 (and (eq (car (aref eshell-current-handles | |
408 eshell-output-handle)) t) | |
409 (eq (car (aref eshell-current-handles | |
410 eshell-error-handle)) t))) | |
411 | |
412 (defvar eshell-print-queue nil) | |
413 (defvar eshell-print-queue-count -1) | |
414 | |
415 (defun eshell-flush (&optional reset-p) | |
416 "Flush out any lines that have been queued for printing. | |
417 Must be called before printing begins with -1 as its argument, and | |
418 after all printing is over with no argument." | |
419 (ignore | |
420 (if reset-p | |
421 (setq eshell-print-queue nil | |
422 eshell-print-queue-count reset-p) | |
423 (if eshell-print-queue | |
424 (eshell-print eshell-print-queue)) | |
425 (eshell-flush 0)))) | |
426 | |
427 (defun eshell-init-print-buffer () | |
428 "Initialize the buffered printing queue." | |
429 (eshell-flush -1)) | |
430 | |
431 (defun eshell-buffered-print (&rest strings) | |
432 "A buffered print -- *for strings only*." | |
433 (if (< eshell-print-queue-count 0) | |
434 (progn | |
435 (eshell-print (apply 'concat strings)) | |
436 (setq eshell-print-queue-count 0)) | |
437 (if (= eshell-print-queue-count eshell-print-queue-size) | |
438 (eshell-flush)) | |
439 (setq eshell-print-queue | |
440 (concat eshell-print-queue (apply 'concat strings)) | |
441 eshell-print-queue-count (1+ eshell-print-queue-count)))) | |
442 | |
443 (defsubst eshell-print (object) | |
31241 | 444 "Output OBJECT to the standard output handle." |
29876 | 445 (eshell-output-object object eshell-output-handle)) |
446 | |
447 (defsubst eshell-error (object) | |
31241 | 448 "Output OBJECT to the standard error handle." |
29876 | 449 (eshell-output-object object eshell-error-handle)) |
450 | |
451 (defsubst eshell-errorn (object) | |
31241 | 452 "Output OBJECT followed by a newline to the standard error handle." |
29876 | 453 (eshell-error object) |
454 (eshell-error "\n")) | |
455 | |
456 (defsubst eshell-printn (object) | |
31241 | 457 "Output OBJECT followed by a newline to the standard output handle." |
29876 | 458 (eshell-print object) |
459 (eshell-print "\n")) | |
460 | |
461 (defun eshell-output-object-to-target (object target) | |
462 "Insert OBJECT into TARGET. | |
463 Returns what was actually sent, or nil if nothing was sent." | |
464 (cond | |
465 ((functionp target) | |
466 (funcall target object)) | |
467 | |
468 ((symbolp target) | |
469 (if (eq target t) ; means "print to display" | |
470 (eshell-output-filter nil (eshell-stringify object)) | |
471 (if (not (symbol-value target)) | |
472 (set target object) | |
473 (setq object (eshell-stringify object)) | |
474 (if (not (stringp (symbol-value target))) | |
475 (set target (eshell-stringify | |
476 (symbol-value target)))) | |
477 (set target (concat (symbol-value target) object))))) | |
478 | |
479 ((markerp target) | |
480 (if (buffer-live-p (marker-buffer target)) | |
481 (with-current-buffer (marker-buffer target) | |
482 (let ((moving (= (point) target))) | |
483 (save-excursion | |
484 (goto-char target) | |
485 (setq object (eshell-stringify object)) | |
486 (insert-and-inherit object) | |
487 (set-marker target (point-marker))) | |
488 (if moving | |
489 (goto-char target)))))) | |
490 | |
31241 | 491 ((eshell-processp target) |
29876 | 492 (when (eq (process-status target) 'run) |
493 (setq object (eshell-stringify object)) | |
494 (process-send-string target object))) | |
495 | |
496 ((consp target) | |
497 (apply (car target) object (cdr target)))) | |
498 object) | |
499 | |
500 (defun eshell-output-object (object &optional handle-index handles) | |
501 "Insert OBJECT, using HANDLE-INDEX specifically)." | |
502 (let ((target (car (aref (or handles eshell-current-handles) | |
503 (or handle-index eshell-output-handle))))) | |
504 (if (and target (not (listp target))) | |
505 (eshell-output-object-to-target object target) | |
506 (while target | |
507 (eshell-output-object-to-target object (car target)) | |
508 (setq target (cdr target)))))) | |
509 | |
510 ;;; Code: | |
511 | |
512 ;;; esh-io.el ends here |