4

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.

cms
  • 5,864
  • 2
  • 28
  • 31
Mike H-R
  • 7,726
  • 5
  • 43
  • 65

1 Answers1

3

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)"

cms
  • 5,864
  • 2
  • 28
  • 31
  • great answer, thanks. My solution was `emacsclient -e "(progn (find-file \"$FILE\") (prelude-cleanup-buffer-or-region) (save-buffer) (kill-buffer))"` in the end. Note the `(kill-buffer)` as without that my script was hanging if I tried it twice as the buffer already being open stops your command. – Mike H-R Oct 27 '14 at 13:32
  • hmm - my example was just supposed to be a hint, but find-file is fine with buffers that are already being visited. Maybe something in your other forms was causing it to expect more input on a second visit ? Anyway, the principle is sound, I'm glad you fixed it. – cms Oct 27 '14 at 14:34
  • Yes, I was confused about that myself as I thought find-file was idempotent, either way, ensuring that you `kill-buffer` after fixed the bug for me (whatever the bug might've been). Thanks again. – Mike H-R Oct 27 '14 at 14:43
  • maybe the point was left somewhere your cleanup function didn't expect it to be – cms Oct 27 '14 at 14:46