Search code examples
emacsscriptingelispemacsclient

emacs --daemon with --batch and input file


I would like to create a script that simply cleans up the whitespace and tabs on several files in a folder for me. I have created a bash file with among other things:

emacsclient -t -e '(progn (prelude-cleanup-buffer-or-region) (save-buffer-kill-terminal))' $FILE

Now this doesn't seem to work as it interprets ALL the file arguments as functions to be run (so $FILE is executed as a function). (P.S. prelude-cleanup-buffer-or-region is from here)

Now what I really want appears to be --batch described here (since I don't actually want to display anything on the screen) but this isn't one of the options of emacsclient. The reason I want to use emacsclient rather than just using emacs --batch is that I have a lot of startup files so want all of this to stay loaded otherwise my script would take too long.

Does anyone have any advice on how to go about this?

Thanks in advance.


Solution

  • emacsclient -e means evaluate lisp forms, do not edit files

    from the man page

     -e, --eval
                  do  not  visit files but instead evaluate the arguments as Emacs
                  Lisp expressions.
    

    I guess you could add a (find-file "file") to your list of forms to execute

    I just tried this snippet -

    /opt/local/bin/emacsclient  -e '(progn (find-file "./tmpfoo") 
        (end-of-buffer) (insert "ffff") (save-buffer))' 
    

    and it edits the file silently like you'd expect.

    you could use shell globbing and a script to expand an argument filename into the list of forms.

    do not run with the -t switch either, -e doesn't expect to have a persistent editor window, and you don't need the kill-terminal. The client will just run your elisp and exit.

    I think I would probably write a lisp function that took a filename argument, that I loaded into emacs at startup time, and then just call that with a filename via emacsclient,

    e.g. FILENAME="somefile"; emacsclient -e "(now-do-my-thing $FILENAME)"