While there are packages that format on save, I'd like to be able to run a custom command that auto-formats a file on save.
Upon saving it runs an external process that:
- Takes the buffer as
stdin
. - Outputs to a temporary file.
After that:
The output is used to replace the current buffer which is then saved.
Any errors from the
stderr
are reported as errors.- Any output from the
stdout
is printed as messages.
For the purpose of testing, this could be the auto-formatting command:
It converts the text to title-caps.
python -c "with open(__import__('sys').argv[-1], 'w') as fh: fh.write(__import__('sys').stdin.read().title())" -- /tmp/TEMP_FILE_FROM_EMACS.txt
... where /tmp/TEMP_FILE_FROM_EMACS.txt
is a generated temp file name.
I realize this may be an involved answer. If this seems like too much hassle to answer, I'll investigate and post an answer myself, since I think it's useful to have a general function to handle this.