I am trying to execute an external program on a string. This code works, but it saves the string to a temporary file in order to run
call-process. This seems quite roundabout. I'd rather not use a shell command either.
Is there a way to call an external program on a string without writing to a temporary file?
(defun my/execute-program-on-string (program string &optional args) "Call PROGRAM with ARGS on STRING." (let (input-file input-buffer output-buffer exit-code result) (unwind-protect (setq input-file (make-temp-file "/tmp/emacs-execute-")) (write-region string nil input-file nil) (setq input-buffer (create-file-buffer input-file) output-buffer (generate-new-buffer (format "*%s*" program)) exit-code (call-process program input-file output-buffer nil args)) (if (zerop exit-code) (setq result (with-current-buffer output-buffer (buffer-string))) (throw 'external-program-error exit-code)) (progn (kill-buffer input-buffer) (kill-buffer output-buffer))) result)) (my/execute-program-on-string "cat" "Hello, World!" "-n")