emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master a2be817: Extend process-lines to allow exit status handling


From: Lars Ingebrigtsen
Subject: master a2be817: Extend process-lines to allow exit status handling
Date: Sat, 19 Sep 2020 18:16:52 -0400 (EDT)

branch: master
commit a2be81780ef466f6007a8a5ec909106c5f8aebf0
Author: Peder O. Klingenberg <peder@klingenberg.no>
Commit: Lars Ingebrigtsen <larsi@gnus.org>

    Extend process-lines to allow exit status handling
    
    * subr.el (process-lines-handling-status): Extension of the old
    process-lines, with more flexible handling of the exit status.
    (process-lines): Old API implemented using the new function.
    (process-lines-ignore-status): Another use of the new function -
    return the output lines regardless of the exit status (bug#1321).
---
 doc/lispref/processes.texi |  5 +++++
 etc/NEWS                   |  6 ++++++
 lisp/subr.el               | 26 ++++++++++++++++++++++----
 3 files changed, 33 insertions(+), 4 deletions(-)

diff --git a/doc/lispref/processes.texi b/doc/lispref/processes.texi
index 4002004..e088452 100644
--- a/doc/lispref/processes.texi
+++ b/doc/lispref/processes.texi
@@ -603,6 +603,11 @@ This function works by calling @code{call-process}, so 
program output
 is decoded in the same way as for @code{call-process}.
 @end defun
 
+@defun process-lines-ignore-status program &rest args
+This function is just like @code{process-lines}, but does not signal
+an error if @var{program} exists with a non-zero exit status.
+@end defun
+
 @node Asynchronous Processes
 @section Creating an Asynchronous Process
 @cindex asynchronous subprocess
diff --git a/etc/NEWS b/etc/NEWS
index 67cfd5f..fb8f284 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1372,6 +1372,12 @@ ledit.el, lmenu.el, lucid.el and old-whitespace.el.
 * Lisp Changes in Emacs 28.1
 
 +++
+*** New function 'process-lines-ignore-status'.
+This is like 'process-lines', but does not signal an error if the
+return status is non-zero.  'process-lines-handling-status' has also
+been added, and takes a callback to handle the return status.
+
++++
 *** New function 'replace-in-string'.
 This function works along the line of 'replace-regexp-in-string', but
 matching on strings instead of regexps, and does not change the global
diff --git a/lisp/subr.el b/lisp/subr.el
index f3c1e20..f00e4ef 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -2344,13 +2344,19 @@ use `start-file-process'."
                 (if program
                     (list :command (cons program program-args))))))
 
-(defun process-lines (program &rest args)
+(defun process-lines-handling-status (program status-handler &rest args)
   "Execute PROGRAM with ARGS, returning its output as a list of lines.
-Signal an error if the program returns with a non-zero exit status."
+If STATUS-HANDLER is non-NIL, it must be a function with one
+argument, which will be called with the exit status of the
+program before the output is collected.  If STATUS-HANDLER is
+NIL, an error is signalled if the program returns with a non-zero
+exit status."
   (with-temp-buffer
     (let ((status (apply 'call-process program nil (current-buffer) nil args)))
-      (unless (eq status 0)
-       (error "%s exited with status %s" program status))
+      (if status-handler
+         (funcall status-handler status)
+       (unless (eq status 0)
+         (error "%s exited with status %s" program status)))
       (goto-char (point-min))
       (let (lines)
        (while (not (eobp))
@@ -2361,6 +2367,18 @@ Signal an error if the program returns with a non-zero 
exit status."
          (forward-line 1))
        (nreverse lines)))))
 
+(defun process-lines (program &rest args)
+  "Execute PROGRAM with ARGS, returning its output as a list of lines.
+Signal an error if the program returns with a non-zero exit status.
+Also see `process-lines-ignore-status'."
+  (apply #'process-lines-handling-status program nil args))
+
+(defun process-lines-ignore-status (program &rest args)
+  "Execute PROGRAM with ARGS, returning its output as a list of lines.
+The exit status of the program is ignored.
+Also see `process-lines'."
+  (apply #'process-lines-handling-status program #'identity args))
+
 (defun process-live-p (process)
   "Return non-nil if PROCESS is alive.
 A process is considered alive if its status is `run', `open',



reply via email to

[Prev in Thread] Current Thread [Next in Thread]