Improve the make-process-fn function; add make-shell-fn

This commit is contained in:
Jeremy Dormitzer 2019-04-24 11:38:17 -04:00
parent b5bec979c2
commit 77bf041606

View File

@ -284,15 +284,29 @@ A function to call a process passing some string as stdin and returning the proc
"Returns a function that, when called, call `program` with arguments `args`,
passing the function argument as stdin"
(lambda (&optional input)
(let ((infile
(when input
(let ((tmp-file (make-temp-file "make-process-input")))
(with-temp-file tmp-file
(insert input)
tmp-file)))))
(with-temp-buffer
(if input
(progn
(insert input)
(apply #'call-process-region (point-min) (point-max) program t t nil args))
(apply #'call-process program nil t nil args))
(buffer-substring-no-properties (point-min) (point-max)))))
#+END_SRC
The same function but for commands that need to run in a shell:
#+BEGIN_SRC emacs-lisp
(defun make-shell-fn (program &rest args)
"Returns a function that, when called, calls `program` in a shell
with arguments `args`, passing the function argument as stdin"
(lambda (&optional input)
(let ((cmd (combine-and-quote-strings `(,program ,@args))))
(with-temp-buffer
(apply #'call-process program infile t nil args)
(buffer-substring-no-properties (point-min) (point-max))))))
(if input
(progn
(insert input)
(call-shell-region (point-min) (point-max) cmd t t))
(call-process-shell-command cmd nil t))
(buffer-substring-no-properties (point-min) (point-max))))))
#+END_SRC
* Customization File