How do I read the entire contents of stdin into an Emacs buffer in
#! /usr/bin/env emacs --script mode? Stdin may not be a regular file.
After a bit of research, apparently this is the best we can do right now:
#! /usr/bin/env emacs --script (defun slurp-stdin () (condition-case nil (while t (insert (read-from-minibuffer "") "\n")) (error))) (with-temp-buffer (slurp-stdin) (princ (buffer-string)))
- It abuses a gimmick of
read-from-minibufferthat causes that function to read a line from stdin when Emacs is in batch mode.
- There's no fundamental reason to read a line at a time instead of chunk-at-a-time, but I can't find a chunk-at-a-time read function in Emacs.
- It cannot distinguish between different kinds of newlines (CR or CR/LF) and cannot differentiate between the presence or lack of a final newline at the end of stdin. I'm not sure what kind of newline conversion (if any) is done.
- It cannot distinguish between end-of-file and other errors. Emacs has an
end-of-filecondition type that is used for that purpose by
read-from-minibufferdoes not use it.
- On the bright side, it works fine with pipes and sockets.