Fix up comment convention on the arch-tag lines.
[bpt/emacs.git] / lisp / eshell / esh-io.el
index f59c1ef..dea723e 100644 (file)
@@ -1,12 +1,15 @@
-;;; esh-io --- I/O management
+;;; esh-io.el --- I/O management
 
-;; Copyright (C) 1999, 2000 Free Software Foundation
+;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
+;;   2005, 2006, 2007, 2008 Free Software Foundation, Inc.
+
+;; Author: John Wiegley <johnw@gnu.org>
 
 ;; This file is part of GNU Emacs.
 
 ;; GNU Emacs is free software; you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
+;; the Free Software Foundation; either version 3, or (at your option)
 ;; any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 
 ;; You should have received a copy of the GNU General Public License
 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
-
-(provide 'esh-io)
-
-(eval-when-compile (require 'esh-maint))
-
-(defgroup eshell-io nil
-  "Eshell's I/O management code provides a scheme for treating many
-different kinds of objects -- symbols, files, buffers, etc. -- as
-though they were files."
-  :tag "I/O management"
-  :group 'eshell)
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
 
 ;;; Commentary:
 
@@ -65,6 +57,17 @@ though they were files."
 ;;   (+ 1 2) > a > b > c   ; prints number to all three files
 ;;   (+ 1 2) > a | wc      ; prints to 'a', and pipes to 'wc'
 
+(provide 'esh-io)
+
+(eval-when-compile (require 'eshell))
+
+(defgroup eshell-io nil
+  "Eshell's I/O management code provides a scheme for treating many
+different kinds of objects -- symbols, files, buffers, etc. -- as
+though they were files."
+  :tag "I/O management"
+  :group 'eshell)
+
 ;;; User Variables:
 
 (defcustom eshell-io-load-hook '(eshell-io-initialize)
@@ -132,8 +135,8 @@ the function is expected to return another function -- which is the
 output function.  Otherwise, the second element itself is the output
 function.
 
-The output function is then called repeatedly with single strings,
-with represents success pieces of the output of the command, until nil
+The output function is then called repeatedly with single strings,
+which represents successive pieces of the output of the command, until nil
 is passed, meaning EOF.
 
 NOTE: /dev/null is handled specially as a virtual target, and should
@@ -167,14 +170,11 @@ not be added to this variable."
 
 (defun eshell-io-initialize ()
   "Initialize the I/O subsystem code."
-  (make-local-hook 'eshell-parse-argument-hook)
   (add-hook 'eshell-parse-argument-hook
            'eshell-parse-redirection nil t)
   (make-local-variable 'eshell-current-redirections)
-  (make-local-hook 'eshell-pre-rewrite-command-hook)
   (add-hook 'eshell-pre-rewrite-command-hook
            'eshell-strip-redirections nil t)
-  (make-local-hook 'eshell-post-rewrite-command-hook)
   (add-hook 'eshell-post-rewrite-command-hook
            'eshell-apply-redirections nil t))
 
@@ -193,7 +193,7 @@ not be added to this variable."
          (eshell-finish-arg
           (prog1
               (list 'eshell-set-output-handle
-                    (or (and sh (string-to-int sh)) 1)
+                    (or (and sh (string-to-number sh)) 1)
                     (list 'quote
                           (aref [overwrite append insert]
                                 (1- (length oper)))))
@@ -231,7 +231,9 @@ not be added to this variable."
   (standard-output output-mode &optional standard-error error-mode)
   "Create a new set of file handles for a command.
 The default location for standard output and standard error will go to
-STANDARD-OUTPUT and STANDARD-ERROR, respectively."
+STANDARD-OUTPUT and STANDARD-ERROR, respectively.
+OUTPUT-MODE and ERROR-MODE are either `overwrite', `append' or `insert';
+a nil value of mode defaults to `insert'."
   (let ((handles (make-vector eshell-number-of-handles nil))
        (output-target (eshell-get-target standard-output output-mode))
        (error-target (eshell-get-target standard-error error-mode)))
@@ -272,7 +274,7 @@ STATUS should be non-nil on successful termination of the output."
 
    ;; If we're redirecting to a process (via a pipe, or process
    ;; redirection), send it EOF so that it knows we're finished.
-   ((processp target)
+   ((eshell-processp target)
     (if (eq (process-status target) 'run)
        (process-send-eof target)))
 
@@ -326,31 +328,34 @@ last execution result should not be changed."
 
 (defun eshell-get-target (target &optional mode)
   "Convert TARGET, which is a raw argument, into a valid output target.
-MODE is either `overwrite', `append' or `insert'."
+MODE is either `overwrite', `append' or `insert'; if it is omitted or nil,
+it defaults to `insert'."
   (setq mode (or mode 'insert))
   (cond
    ((stringp target)
     (let ((redir (assoc target eshell-virtual-targets)))
-     (if redir
-        (if (nth 2 redir)
-            (funcall (nth 1 redir) mode)
-          (nth 1 redir))
-       (let* ((exists (get-file-buffer target))
-             (buf (find-file-noselect target t)))
-        (with-current-buffer buf
-          (if buffer-read-only
-              (error "Cannot write to read-only file `%s'" target))
-          (set (make-local-variable 'eshell-output-file-buffer)
-               (if (eq exists buf) 0 t))
-          (cond ((eq mode 'overwrite)
-                 (erase-buffer))
-                ((eq mode 'append)
-                 (goto-char (point-max))))
-          (point-marker))))))
+      (if redir
+         (if (nth 2 redir)
+             (funcall (nth 1 redir) mode)
+           (nth 1 redir))
+       (let* ((exists (get-file-buffer target))
+              (buf (find-file-noselect target t)))
+         (with-current-buffer buf
+           (if buffer-read-only
+               (error "Cannot write to read-only file `%s'" target))
+           (set (make-local-variable 'eshell-output-file-buffer)
+                (if (eq exists buf) 0 t))
+           (cond ((eq mode 'overwrite)
+                  (erase-buffer))
+                 ((eq mode 'append)
+                  (goto-char (point-max))))
+           (point-marker))))))
+
    ((or (bufferp target)
        (and (boundp 'eshell-buffer-shorthand)
             (symbol-value 'eshell-buffer-shorthand)
-            (symbolp target)))
+            (symbolp target)
+            (not (memq target '(t nil)))))
     (let ((buf (if (bufferp target)
                   target
                 (get-buffer-create
@@ -361,17 +366,20 @@ MODE is either `overwrite', `append' or `insert'."
              ((eq mode 'append)
               (goto-char (point-max))))
        (point-marker))))
-   ((functionp target)
-    nil)
+
+   ((functionp target) nil)
+
    ((symbolp target)
     (if (eq mode 'overwrite)
        (set target nil))
     target)
-   ((or (processp target)
+
+   ((or (eshell-processp target)
        (markerp target))
     target)
+
    (t
-    (error "Illegal redirection target: %s"
+    (error "Invalid redirection target: %s"
           (eshell-stringify target)))))
 
 (eval-when-compile
@@ -409,6 +417,10 @@ MODE is either `overwrite', `append' or `insert'."
 (defvar eshell-print-queue nil)
 (defvar eshell-print-queue-count -1)
 
+(defsubst eshell-print (object)
+  "Output OBJECT to the standard output handle."
+  (eshell-output-object object eshell-output-handle))
+
 (defun eshell-flush (&optional reset-p)
   "Flush out any lines that have been queued for printing.
 Must be called before printing begins with -1 as its argument, and
@@ -437,21 +449,17 @@ after all printing is over with no argument."
          (concat eshell-print-queue (apply 'concat strings))
          eshell-print-queue-count (1+ eshell-print-queue-count))))
 
-(defsubst eshell-print (object)
-  "Output OBJECT to the error handle."
-  (eshell-output-object object eshell-output-handle))
-
 (defsubst eshell-error (object)
-  "Output OBJECT to the error handle."
+  "Output OBJECT to the standard error handle."
   (eshell-output-object object eshell-error-handle))
 
 (defsubst eshell-errorn (object)
-  "Output OBJECT to the error handle."
+  "Output OBJECT followed by a newline to the standard error handle."
   (eshell-error object)
   (eshell-error "\n"))
 
 (defsubst eshell-printn (object)
-  "Output OBJECT to the error handle."
+  "Output OBJECT followed by a newline to the standard output handle."
   (eshell-print object)
   (eshell-print "\n"))
 
@@ -479,15 +487,17 @@ Returns what was actually sent, or nil if nothing was sent."
          (let ((moving (= (point) target)))
            (save-excursion
              (goto-char target)
-             (setq object (eshell-stringify object))
+             (unless (stringp object)
+               (setq object (eshell-stringify object)))
              (insert-and-inherit object)
              (set-marker target (point-marker)))
            (if moving
                (goto-char target))))))
 
-   ((processp target)
+   ((eshell-processp target)
     (when (eq (process-status target) 'run)
-      (setq object (eshell-stringify object))
+      (unless (stringp object)
+       (setq object (eshell-stringify object)))
       (process-send-string target object)))
 
    ((consp target)
@@ -506,4 +516,5 @@ Returns what was actually sent, or nil if nothing was sent."
 
 ;;; Code:
 
+;; arch-tag: 9ca2080f-d5e0-4b26-aa0b-d59194a905a2
 ;;; esh-io.el ends here