Mercurial > emacs
annotate lisp/eshell/esh-io.el @ 108861:ad09f2bfce51
Fix config.h includes.
* src/xsettings.c:
* src/xgselect.c:
* src/nsterm.m:
* src/nsselect.m:
* src/nsimage.m:
* src/nsfont.m:
* src/nsfns.m:
* src/dbusbind.c: Use #include <config.h> instead of "config.h" as all
other files do.
author | Dan Nicolaescu <dann@ics.uci.edu> |
---|---|
date | Tue, 01 Jun 2010 21:29:53 -0700 |
parents | 1d1d5d9bd884 |
children | 672b957881a8 |
rev | line source |
---|---|
38414
67b464da13ec
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
32526
diff
changeset
|
1 ;;; esh-io.el --- I/O management |
29876 | 2 |
95619
45dbb3c749a6
Remove unnecessary eval-when-compiles and eval-and-compiles.
Glenn Morris <rgm@gnu.org>
parents:
94661
diff
changeset
|
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, |
106815 | 4 ;; 2008, 2009, 2010 Free Software Foundation, Inc. |
29876 | 5 |
32526 | 6 ;; Author: John Wiegley <johnw@gnu.org> |
7 | |
29876 | 8 ;; This file is part of GNU Emacs. |
9 | |
94661
b5b0801a7637
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
10 ;; GNU Emacs is free software: you can redistribute it and/or modify |
29876 | 11 ;; it under the terms of the GNU General Public License as published by |
94661
b5b0801a7637
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
12 ;; the Free Software Foundation, either version 3 of the License, or |
b5b0801a7637
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
13 ;; (at your option) any later version. |
29876 | 14 |
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. | |
19 | |
20 ;; You should have received a copy of the GNU General Public License | |
94661
b5b0801a7637
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
29876 | 22 |
23 ;;; Commentary: | |
24 | |
25 ;; At the moment, only output redirection is supported in Eshell. To | |
26 ;; use input redirection, the following syntax will work, assuming | |
27 ;; that the command after the pipe is always an external command: | |
28 ;; | |
29 ;; cat <file> | <command> | |
30 ;; | |
31 ;; Otherwise, output redirection and piping are provided in a manner | |
32 ;; consistent with most shells. Therefore, only unique features are | |
33 ;; mentioned here. | |
34 ;; | |
35 ;;;_* Insertion | |
36 ;; | |
37 ;; To insert at the location of point in a buffer, use '>>>': | |
38 ;; | |
39 ;; echo alpha >>> #<buffer *scratch*>; | |
40 ;; | |
41 ;;;_* Pseudo-devices | |
42 ;; | |
43 ;; A few pseudo-devices are provided, since Emacs cannot write | |
44 ;; directly to a UNIX device file: | |
45 ;; | |
46 ;; echo alpha > /dev/null ; the bit bucket | |
47 ;; echo alpha > /dev/kill ; set the kill ring | |
48 ;; echo alpha >> /dev/clip ; append to the clipboard | |
49 ;; | |
50 ;;;_* Multiple output targets | |
51 ;; | |
52 ;; Eshell can write to multiple output targets, including pipes. | |
53 ;; Example: | |
54 ;; | |
55 ;; (+ 1 2) > a > b > c ; prints number to all three files | |
56 ;; (+ 1 2) > a | wc ; prints to 'a', and pipes to 'wc' | |
57 | |
101347 | 58 ;;; Code: |
59 | |
87080
3767c8399782
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
60 (provide 'esh-io) |
3767c8399782
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
61 |
98564
f79ec7c34dc5
Sven Joachim <svenjoac at gmx.de>
Glenn Morris <rgm@gnu.org>
parents:
95619
diff
changeset
|
62 (eval-when-compile |
f79ec7c34dc5
Sven Joachim <svenjoac at gmx.de>
Glenn Morris <rgm@gnu.org>
parents:
95619
diff
changeset
|
63 (require 'cl) |
f79ec7c34dc5
Sven Joachim <svenjoac at gmx.de>
Glenn Morris <rgm@gnu.org>
parents:
95619
diff
changeset
|
64 (require 'eshell)) |
87080
3767c8399782
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
65 |
3767c8399782
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
66 (defgroup eshell-io nil |
3767c8399782
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
67 "Eshell's I/O management code provides a scheme for treating many |
3767c8399782
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
68 different kinds of objects -- symbols, files, buffers, etc. -- as |
3767c8399782
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
69 though they were files." |
3767c8399782
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
70 :tag "I/O management" |
3767c8399782
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
71 :group 'eshell) |
3767c8399782
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
72 |
29876 | 73 ;;; User Variables: |
74 | |
75 (defcustom eshell-io-load-hook '(eshell-io-initialize) | |
76 "*A hook that gets run when `eshell-io' is loaded." | |
77 :type 'hook | |
78 :group 'eshell-io) | |
79 | |
80 (defcustom eshell-number-of-handles 3 | |
81 "*The number of file handles that eshell supports. | |
82 Currently this is standard input, output and error. But even all of | |
83 these Emacs does not currently support with asynchronous processes | |
84 \(which is what eshell uses so that you can continue doing work in | |
85 other buffers) ." | |
86 :type 'integer | |
87 :group 'eshell-io) | |
88 | |
89 (defcustom eshell-output-handle 1 | |
90 "*The index of the standard output handle." | |
91 :type 'integer | |
92 :group 'eshell-io) | |
93 | |
94 (defcustom eshell-error-handle 2 | |
95 "*The index of the standard error handle." | |
96 :type 'integer | |
97 :group 'eshell-io) | |
98 | |
99 (defcustom eshell-buffer-shorthand nil | |
100 "*If non-nil, a symbol name can be used for a buffer in redirection. | |
101 If nil, redirecting to a buffer requires buffer name syntax. If this | |
102 variable is set, redirection directly to Lisp symbols will be | |
103 impossible. | |
104 | |
105 Example: | |
106 | |
107 echo hello > '*scratch* ; works if `eshell-buffer-shorthand' is t | |
108 echo hello > #<buffer *scratch*> ; always works" | |
109 :type 'boolean | |
110 :group 'eshell-io) | |
111 | |
112 (defcustom eshell-print-queue-size 5 | |
113 "*The size of the print queue, for doing buffered printing. | |
114 This is basically a speed enhancement, to avoid blocking the Lisp code | |
115 from executing while Emacs is redisplaying." | |
116 :type 'integer | |
117 :group 'eshell-io) | |
118 | |
119 (defcustom eshell-virtual-targets | |
120 '(("/dev/eshell" eshell-interactive-print nil) | |
121 ("/dev/kill" (lambda (mode) | |
122 (if (eq mode 'overwrite) | |
123 (kill-new "")) | |
124 'eshell-kill-append) t) | |
125 ("/dev/clip" (lambda (mode) | |
126 (if (eq mode 'overwrite) | |
127 (let ((x-select-enable-clipboard t)) | |
128 (kill-new ""))) | |
129 'eshell-clipboard-append) t)) | |
130 "*Map virtual devices name to Emacs Lisp functions. | |
131 If the user specifies any of the filenames above as a redirection | |
132 target, the function in the second element will be called. | |
133 | |
134 If the third element is non-nil, the redirection mode is passed as an | |
135 argument (which is the symbol `overwrite', `append' or `insert'), and | |
136 the function is expected to return another function -- which is the | |
137 output function. Otherwise, the second element itself is the output | |
138 function. | |
139 | |
31241 | 140 The output function is then called repeatedly with single strings, |
141 which represents successive pieces of the output of the command, until nil | |
29876 | 142 is passed, meaning EOF. |
143 | |
144 NOTE: /dev/null is handled specially as a virtual target, and should | |
145 not be added to this variable." | |
146 :type '(repeat | |
147 (list (string :tag "Target") | |
148 function | |
149 (choice (const :tag "Func returns output-func" t) | |
150 (const :tag "Func is output-func" nil)))) | |
151 :group 'eshell-io) | |
152 | |
153 (put 'eshell-virtual-targets 'risky-local-variable t) | |
154 | |
155 ;;; Internal Variables: | |
156 | |
157 (defvar eshell-current-handles nil) | |
158 | |
159 (defvar eshell-last-command-status 0 | |
160 "The exit code from the last command. 0 if successful.") | |
161 | |
162 (defvar eshell-last-command-result nil | |
163 "The result of the last command. Not related to success.") | |
164 | |
165 (defvar eshell-output-file-buffer nil | |
166 "If non-nil, the current buffer is a file output buffer.") | |
167 | |
168 (defvar eshell-print-count) | |
169 (defvar eshell-current-redirections) | |
170 | |
171 ;;; Functions: | |
172 | |
173 (defun eshell-io-initialize () | |
174 "Initialize the I/O subsystem code." | |
175 (add-hook 'eshell-parse-argument-hook | |
176 'eshell-parse-redirection nil t) | |
177 (make-local-variable 'eshell-current-redirections) | |
178 (add-hook 'eshell-pre-rewrite-command-hook | |
179 'eshell-strip-redirections nil t) | |
180 (add-hook 'eshell-post-rewrite-command-hook | |
181 'eshell-apply-redirections nil t)) | |
182 | |
183 (defun eshell-parse-redirection () | |
184 "Parse an output redirection, such as '2>'." | |
185 (if (and (not eshell-current-quoted) | |
186 (looking-at "\\([0-9]\\)?\\(<\\|>+\\)&?\\([0-9]\\)?\\s-*")) | |
187 (if eshell-current-argument | |
188 (eshell-finish-arg) | |
189 (let ((sh (match-string 1)) | |
190 (oper (match-string 2)) | |
191 ; (th (match-string 3)) | |
192 ) | |
193 (if (string= oper "<") | |
194 (error "Eshell does not support input redirection")) | |
195 (eshell-finish-arg | |
196 (prog1 | |
197 (list 'eshell-set-output-handle | |
62915
b89e30bcd2bb
Changed all uses of `directory-sep-char' to ?/, and all uses of
John Wiegley <johnw@newartisans.com>
parents:
62786
diff
changeset
|
198 (or (and sh (string-to-number sh)) 1) |
29876 | 199 (list 'quote |
200 (aref [overwrite append insert] | |
201 (1- (length oper))))) | |
202 (goto-char (match-end 0)))))))) | |
203 | |
204 (defun eshell-strip-redirections (terms) | |
205 "Rewrite any output redirections in TERMS." | |
206 (setq eshell-current-redirections (list t)) | |
207 (let ((tl terms) | |
208 (tt (cdr terms))) | |
209 (while tt | |
210 (if (not (and (consp (car tt)) | |
211 (eq (caar tt) 'eshell-set-output-handle))) | |
212 (setq tt (cdr tt) | |
213 tl (cdr tl)) | |
214 (unless (cdr tt) | |
215 (error "Missing redirection target")) | |
216 (nconc eshell-current-redirections | |
217 (list (list 'ignore | |
218 (append (car tt) (list (cadr tt)))))) | |
219 (setcdr tl (cddr tt)) | |
220 (setq tt (cddr tt)))) | |
221 (setq eshell-current-redirections | |
222 (cdr eshell-current-redirections)))) | |
223 | |
224 (defun eshell-apply-redirections (cmdsym) | |
225 "Apply any redirection which were specified for COMMAND." | |
226 (if eshell-current-redirections | |
227 (set cmdsym | |
228 (append (list 'progn) | |
229 eshell-current-redirections | |
230 (list (symbol-value cmdsym)))))) | |
231 | |
232 (defun eshell-create-handles | |
233 (standard-output output-mode &optional standard-error error-mode) | |
234 "Create a new set of file handles for a command. | |
235 The default location for standard output and standard error will go to | |
31241 | 236 STANDARD-OUTPUT and STANDARD-ERROR, respectively. |
237 OUTPUT-MODE and ERROR-MODE are either `overwrite', `append' or `insert'; | |
238 a nil value of mode defaults to `insert'." | |
29876 | 239 (let ((handles (make-vector eshell-number-of-handles nil)) |
240 (output-target (eshell-get-target standard-output output-mode)) | |
241 (error-target (eshell-get-target standard-error error-mode))) | |
242 (aset handles eshell-output-handle (cons output-target 1)) | |
243 (if standard-error | |
244 (aset handles eshell-error-handle (cons error-target 1)) | |
245 (aset handles eshell-error-handle (cons output-target 1))) | |
246 handles)) | |
247 | |
248 (defun eshell-protect-handles (handles) | |
249 "Protect the handles in HANDLES from a being closed." | |
250 (let ((idx 0)) | |
251 (while (< idx eshell-number-of-handles) | |
252 (if (aref handles idx) | |
253 (setcdr (aref handles idx) | |
254 (1+ (cdr (aref handles idx))))) | |
255 (setq idx (1+ idx)))) | |
256 handles) | |
257 | |
258 (defun eshell-close-target (target status) | |
259 "Close an output TARGET, passing STATUS as the result. | |
260 STATUS should be non-nil on successful termination of the output." | |
261 (cond | |
262 ((symbolp target) nil) | |
263 | |
264 ;; If we were redirecting to a file, save the file and close the | |
265 ;; buffer. | |
266 ((markerp target) | |
267 (let ((buf (marker-buffer target))) | |
268 (when buf ; somebody's already killed it! | |
269 (save-current-buffer | |
270 (set-buffer buf) | |
271 (when eshell-output-file-buffer | |
272 (save-buffer) | |
273 (when (eq eshell-output-file-buffer t) | |
274 (or status (set-buffer-modified-p nil)) | |
275 (kill-buffer buf))))))) | |
276 | |
277 ;; If we're redirecting to a process (via a pipe, or process | |
278 ;; redirection), send it EOF so that it knows we're finished. | |
31241 | 279 ((eshell-processp target) |
29876 | 280 (if (eq (process-status target) 'run) |
281 (process-send-eof target))) | |
282 | |
283 ;; A plain function redirection needs no additional arguments | |
284 ;; passed. | |
285 ((functionp target) | |
286 (funcall target status)) | |
287 | |
288 ;; But a more complicated function redirection (which can only | |
289 ;; happen with aliases at the moment) has arguments that need to be | |
290 ;; passed along with it. | |
291 ((consp target) | |
292 (apply (car target) status (cdr target))))) | |
293 | |
294 (defun eshell-close-handles (exit-code &optional result handles) | |
295 "Close all of the current handles, taking refcounts into account. | |
296 EXIT-CODE is the process exit code; mainly, it is zero, if the command | |
297 completed successfully. RESULT is the quoted value of the last | |
298 command. If nil, then the meta variables for keeping track of the | |
299 last execution result should not be changed." | |
300 (let ((idx 0)) | |
301 (assert (or (not result) (eq (car result) 'quote))) | |
302 (setq eshell-last-command-status exit-code | |
303 eshell-last-command-result (cadr result)) | |
304 (while (< idx eshell-number-of-handles) | |
305 (let ((handles (or handles eshell-current-handles))) | |
306 (when (aref handles idx) | |
307 (setcdr (aref handles idx) | |
308 (1- (cdr (aref handles idx)))) | |
309 (when (= (cdr (aref handles idx)) 0) | |
310 (let ((target (car (aref handles idx)))) | |
311 (if (not (listp target)) | |
312 (eshell-close-target target (= exit-code 0)) | |
313 (while target | |
314 (eshell-close-target (car target) (= exit-code 0)) | |
315 (setq target (cdr target))))) | |
316 (setcar (aref handles idx) nil)))) | |
317 (setq idx (1+ idx))) | |
318 nil)) | |
319 | |
320 (defun eshell-kill-append (string) | |
321 "Call `kill-append' with STRING, if it is indeed a string." | |
322 (if (stringp string) | |
323 (kill-append string nil))) | |
324 | |
325 (defun eshell-clipboard-append (string) | |
326 "Call `kill-append' with STRING, if it is indeed a string." | |
327 (if (stringp string) | |
328 (let ((x-select-enable-clipboard t)) | |
329 (kill-append string nil)))) | |
330 | |
331 (defun eshell-get-target (target &optional mode) | |
332 "Convert TARGET, which is a raw argument, into a valid output target. | |
31241 | 333 MODE is either `overwrite', `append' or `insert'; if it is omitted or nil, |
334 it defaults to `insert'." | |
29876 | 335 (setq mode (or mode 'insert)) |
336 (cond | |
337 ((stringp target) | |
338 (let ((redir (assoc target eshell-virtual-targets))) | |
55613
8e3531a00902
2004-05-15 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents:
52401
diff
changeset
|
339 (if redir |
8e3531a00902
2004-05-15 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents:
52401
diff
changeset
|
340 (if (nth 2 redir) |
8e3531a00902
2004-05-15 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents:
52401
diff
changeset
|
341 (funcall (nth 1 redir) mode) |
8e3531a00902
2004-05-15 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents:
52401
diff
changeset
|
342 (nth 1 redir)) |
8e3531a00902
2004-05-15 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents:
52401
diff
changeset
|
343 (let* ((exists (get-file-buffer target)) |
8e3531a00902
2004-05-15 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents:
52401
diff
changeset
|
344 (buf (find-file-noselect target t))) |
8e3531a00902
2004-05-15 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents:
52401
diff
changeset
|
345 (with-current-buffer buf |
8e3531a00902
2004-05-15 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents:
52401
diff
changeset
|
346 (if buffer-read-only |
8e3531a00902
2004-05-15 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents:
52401
diff
changeset
|
347 (error "Cannot write to read-only file `%s'" target)) |
8e3531a00902
2004-05-15 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents:
52401
diff
changeset
|
348 (set (make-local-variable 'eshell-output-file-buffer) |
8e3531a00902
2004-05-15 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents:
52401
diff
changeset
|
349 (if (eq exists buf) 0 t)) |
8e3531a00902
2004-05-15 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents:
52401
diff
changeset
|
350 (cond ((eq mode 'overwrite) |
8e3531a00902
2004-05-15 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents:
52401
diff
changeset
|
351 (erase-buffer)) |
8e3531a00902
2004-05-15 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents:
52401
diff
changeset
|
352 ((eq mode 'append) |
8e3531a00902
2004-05-15 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents:
52401
diff
changeset
|
353 (goto-char (point-max)))) |
8e3531a00902
2004-05-15 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents:
52401
diff
changeset
|
354 (point-marker)))))) |
8e3531a00902
2004-05-15 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents:
52401
diff
changeset
|
355 |
29876 | 356 ((or (bufferp target) |
357 (and (boundp 'eshell-buffer-shorthand) | |
358 (symbol-value 'eshell-buffer-shorthand) | |
62786
dc758ba35d6c
(eshell-get-target): If `eshell-buffer-shorthand' is in use, and the
John Wiegley <johnw@newartisans.com>
parents:
60915
diff
changeset
|
359 (symbolp target) |
dc758ba35d6c
(eshell-get-target): If `eshell-buffer-shorthand' is in use, and the
John Wiegley <johnw@newartisans.com>
parents:
60915
diff
changeset
|
360 (not (memq target '(t nil))))) |
29876 | 361 (let ((buf (if (bufferp target) |
362 target | |
363 (get-buffer-create | |
364 (symbol-name target))))) | |
365 (with-current-buffer buf | |
366 (cond ((eq mode 'overwrite) | |
367 (erase-buffer)) | |
368 ((eq mode 'append) | |
369 (goto-char (point-max)))) | |
370 (point-marker)))) | |
55613
8e3531a00902
2004-05-15 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents:
52401
diff
changeset
|
371 |
8e3531a00902
2004-05-15 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents:
52401
diff
changeset
|
372 ((functionp target) nil) |
8e3531a00902
2004-05-15 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents:
52401
diff
changeset
|
373 |
29876 | 374 ((symbolp target) |
375 (if (eq mode 'overwrite) | |
376 (set target nil)) | |
377 target) | |
55613
8e3531a00902
2004-05-15 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents:
52401
diff
changeset
|
378 |
31241 | 379 ((or (eshell-processp target) |
29876 | 380 (markerp target)) |
381 target) | |
55613
8e3531a00902
2004-05-15 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents:
52401
diff
changeset
|
382 |
29876 | 383 (t |
60915
a72129314db4
* eshell/esh-io.el, eshell/esh-var.el: Replace `illegal' with
Werner LEMBERG <wl@gnu.org>
parents:
55613
diff
changeset
|
384 (error "Invalid redirection target: %s" |
29876 | 385 (eshell-stringify target))))) |
386 | |
95619
45dbb3c749a6
Remove unnecessary eval-when-compiles and eval-and-compiles.
Glenn Morris <rgm@gnu.org>
parents:
94661
diff
changeset
|
387 (defvar grep-null-device) |
29876 | 388 |
389 (defun eshell-set-output-handle (index mode &optional target) | |
390 "Set handle INDEX, using MODE, to point to TARGET." | |
391 (when target | |
392 (if (and (stringp target) | |
393 (or (cond | |
394 ((boundp 'null-device) | |
395 (string= target null-device)) | |
396 ((boundp 'grep-null-device) | |
397 (string= target grep-null-device)) | |
398 (t nil)) | |
399 (string= target "/dev/null"))) | |
400 (aset eshell-current-handles index nil) | |
401 (let ((where (eshell-get-target target mode)) | |
402 (current (car (aref eshell-current-handles index)))) | |
403 (if (and (listp current) | |
404 (not (member where current))) | |
405 (setq current (append current (list where))) | |
47971
526a8d279adb
Bob Halley <halley@play-bow.org>: (eshell-set-output-handle): Fix so
John Wiegley <johnw@newartisans.com>
parents:
43337
diff
changeset
|
406 (setq current (list where))) |
29876 | 407 (if (not (aref eshell-current-handles index)) |
408 (aset eshell-current-handles index (cons nil 1))) | |
409 (setcar (aref eshell-current-handles index) current))))) | |
410 | |
411 (defun eshell-interactive-output-p () | |
412 "Return non-nil if current handles are bound for interactive display." | |
413 (and (eq (car (aref eshell-current-handles | |
414 eshell-output-handle)) t) | |
415 (eq (car (aref eshell-current-handles | |
416 eshell-error-handle)) t))) | |
417 | |
418 (defvar eshell-print-queue nil) | |
419 (defvar eshell-print-queue-count -1) | |
420 | |
87080
3767c8399782
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
421 (defsubst eshell-print (object) |
3767c8399782
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
422 "Output OBJECT to the standard output handle." |
3767c8399782
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
423 (eshell-output-object object eshell-output-handle)) |
3767c8399782
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
424 |
29876 | 425 (defun eshell-flush (&optional reset-p) |
426 "Flush out any lines that have been queued for printing. | |
427 Must be called before printing begins with -1 as its argument, and | |
428 after all printing is over with no argument." | |
429 (ignore | |
430 (if reset-p | |
431 (setq eshell-print-queue nil | |
432 eshell-print-queue-count reset-p) | |
433 (if eshell-print-queue | |
434 (eshell-print eshell-print-queue)) | |
435 (eshell-flush 0)))) | |
436 | |
437 (defun eshell-init-print-buffer () | |
438 "Initialize the buffered printing queue." | |
439 (eshell-flush -1)) | |
440 | |
441 (defun eshell-buffered-print (&rest strings) | |
442 "A buffered print -- *for strings only*." | |
443 (if (< eshell-print-queue-count 0) | |
444 (progn | |
445 (eshell-print (apply 'concat strings)) | |
446 (setq eshell-print-queue-count 0)) | |
447 (if (= eshell-print-queue-count eshell-print-queue-size) | |
448 (eshell-flush)) | |
449 (setq eshell-print-queue | |
450 (concat eshell-print-queue (apply 'concat strings)) | |
451 eshell-print-queue-count (1+ eshell-print-queue-count)))) | |
452 | |
453 (defsubst eshell-error (object) | |
31241 | 454 "Output OBJECT to the standard error handle." |
29876 | 455 (eshell-output-object object eshell-error-handle)) |
456 | |
457 (defsubst eshell-errorn (object) | |
31241 | 458 "Output OBJECT followed by a newline to the standard error handle." |
29876 | 459 (eshell-error object) |
460 (eshell-error "\n")) | |
461 | |
462 (defsubst eshell-printn (object) | |
31241 | 463 "Output OBJECT followed by a newline to the standard output handle." |
29876 | 464 (eshell-print object) |
465 (eshell-print "\n")) | |
466 | |
467 (defun eshell-output-object-to-target (object target) | |
468 "Insert OBJECT into TARGET. | |
469 Returns what was actually sent, or nil if nothing was sent." | |
470 (cond | |
471 ((functionp target) | |
472 (funcall target object)) | |
473 | |
474 ((symbolp target) | |
475 (if (eq target t) ; means "print to display" | |
476 (eshell-output-filter nil (eshell-stringify object)) | |
477 (if (not (symbol-value target)) | |
478 (set target object) | |
479 (setq object (eshell-stringify object)) | |
480 (if (not (stringp (symbol-value target))) | |
481 (set target (eshell-stringify | |
482 (symbol-value target)))) | |
483 (set target (concat (symbol-value target) object))))) | |
484 | |
485 ((markerp target) | |
486 (if (buffer-live-p (marker-buffer target)) | |
487 (with-current-buffer (marker-buffer target) | |
488 (let ((moving (= (point) target))) | |
489 (save-excursion | |
490 (goto-char target) | |
55613
8e3531a00902
2004-05-15 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents:
52401
diff
changeset
|
491 (unless (stringp object) |
8e3531a00902
2004-05-15 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents:
52401
diff
changeset
|
492 (setq object (eshell-stringify object))) |
29876 | 493 (insert-and-inherit object) |
494 (set-marker target (point-marker))) | |
495 (if moving | |
496 (goto-char target)))))) | |
497 | |
31241 | 498 ((eshell-processp target) |
29876 | 499 (when (eq (process-status target) 'run) |
55613
8e3531a00902
2004-05-15 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents:
52401
diff
changeset
|
500 (unless (stringp object) |
8e3531a00902
2004-05-15 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents:
52401
diff
changeset
|
501 (setq object (eshell-stringify object))) |
29876 | 502 (process-send-string target object))) |
503 | |
504 ((consp target) | |
505 (apply (car target) object (cdr target)))) | |
506 object) | |
507 | |
508 (defun eshell-output-object (object &optional handle-index handles) | |
509 "Insert OBJECT, using HANDLE-INDEX specifically)." | |
510 (let ((target (car (aref (or handles eshell-current-handles) | |
511 (or handle-index eshell-output-handle))))) | |
512 (if (and target (not (listp target))) | |
513 (eshell-output-object-to-target object target) | |
514 (while target | |
515 (eshell-output-object-to-target object (car target)) | |
516 (setq target (cdr target)))))) | |
517 | |
93975
1e3a407766b9
Fix up comment convention on the arch-tag lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
87649
diff
changeset
|
518 ;; arch-tag: 9ca2080f-d5e0-4b26-aa0b-d59194a905a2 |
29876 | 519 ;;; esh-io.el ends here |