From 51326170de92b88e14b8d8f0ed30f33e3eeb4c48 Mon Sep 17 00:00:00 2001 From: Daniel Mendler Date: Fri, 10 Feb 2023 00:22:43 +0100 Subject: compat-28: Add process-lines-ignore-status and process-lines-handling-status --- NEWS.org | 1 + compat-28.el | 29 +++++++++++++++++++++++++++++ compat-tests.el | 14 ++++++++++++++ compat.texi | 19 ++++++++++++++++--- 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/NEWS.org b/NEWS.org index 4876ac6..d0b1296 100644 --- a/NEWS.org +++ b/NEWS.org @@ -3,6 +3,7 @@ * Development - compat-27: Drop obsolete ~compat-call dired-get-marked-files~. +- compat-28: Add ~process-lines-handling-status~ and ~process-lines-ignore-status~. * Release of "Compat" Version 29.1.3.3 diff --git a/compat-28.el b/compat-28.el index 645f5cb..e3bde86 100644 --- a/compat-28.el +++ b/compat-28.el @@ -225,6 +225,35 @@ If LENGTH is nil, use the window width." ;;;; Defined in subr.el +(compat-defun process-lines-handling-status (program status-handler &rest args) ;; + "Execute PROGRAM with ARGS, returning its output as a list of lines. +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 signaled if the program returns with a non-zero +exit status." + (with-temp-buffer + (let ((status (apply #'call-process program nil (current-buffer) nil args))) + (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)) + (setq lines (cons (buffer-substring-no-properties + (line-beginning-position) + (line-end-position)) + lines)) + (forward-line 1)) + (nreverse lines))))) + +(compat-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 #'ignore args)) + ;; FIXME Should handle multibyte regular expressions (compat-defun string-replace (fromstring tostring instring) ;; "Replace FROMSTRING with TOSTRING in INSTRING each time it occurs." diff --git a/compat-tests.el b/compat-tests.el index ce818c1..8ea2ce8 100644 --- a/compat-tests.el +++ b/compat-tests.el @@ -2923,6 +2923,20 @@ (should-equal (cons 1 1) (compat-tests--once-only (cl-incf x))) (should-equal 1 x))) +(ert-deftest process-lines-ignore-status () + (should-equal '("line1" "line2" "") + (process-lines-ignore-status "echo" "line1\nline2\n"))) + +(ert-deftest process-lines-handling-status () + (let (status) + (should-equal '("line1" "line2") + (process-lines-handling-status + "echo" (lambda (s) (setq status s)) "line1\nline2")) + (should-equal status 0) + (should-not (process-lines-handling-status "false" (lambda (s) (setq status s)))) + (should-equal status 1) + (should-error (process-lines-handling-status "false" nil)))) + (ert-deftest seq () (should-equal 3 (seq-length '(a b c))) (should-equal 3 (seq-length [a b c]))) diff --git a/compat.texi b/compat.texi index 4a84f29..922dda1 100644 --- a/compat.texi +++ b/compat.texi @@ -1676,6 +1676,22 @@ The following functions and macros are implemented in Emacs 28.1. These functions are made available by Compat on Emacs versions older than 28.1. +@c copied from lispref/processes.texi +@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} exits with a non-zero exit status. +@end defun + +@c based on lisp/subr.el +@defun process-lines-handling-status program status-handler &rest args +Execute @var{program} with @var{args}, returning its output as a list +of lines. If @var{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 @var{status-handler} is +nil, an error is signaled if the program returns with a non-zero exit +status. +@end defun + @c copied from lispref/help.texi @defun text-quoting-style You should not read the value of the variable @@ -2207,9 +2223,6 @@ The function @code{num-processors}. @item The function @code{object-intervals}. @item -The functions @code{process-lines-handling-status} and -@code{process-lines-ignore-status}. -@item The function @code{require-theme}. @item The function @code{syntax-class-to-char}. -- cgit v1.0