Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have Emacs save file at multiple locations?

Tags:

emacs

Is there an easy way to have emacs save current buffer in two locations? I could in the 'after-save-hook' programmatically copy the current file to a second location, but writing lisp code for that might take some time.

For those that are curious why I want this: I want the changes I make to my JSP immediately be deployed in tomcat's webapps/myapp directory.

So everytime I save a JSP file I want it saved in both my current version controlled source location as well as in the directory where my Tomcat application is deployed.

I can't use symlinks because I use a windows machine and the destination location is a directory in Linux box that is exported through Samba.

like image 960
Murali Avatar asked Dec 21 '09 23:12

Murali


2 Answers

Something like this should work:

(add-hook 'local-write-file-hooks 'my-save-hook)
(defun my-save-hook ()
  "write the file in two places"
  (let ((orig (buffer-file-name)))
    (write-file (concat "/some/other/path" (file-name-nondirectory orig)) nil)
    (write-file orig nil)))

For more on local-write-file-hooks see this answer.

Obviously customize the file name created in the first call to 'write-file.

like image 113
Trey Jackson Avatar answered Sep 30 '22 19:09

Trey Jackson


Given the problem you are trying to solve is to deploy changes immediately, I would suggest writing a script (in your case a batch file) that invokes rsync with the appropriate options. You could either run this in the after-save-hook (which is probably overkill) or assign a hotkey to run it for you when you have made a set of changes that you want to test. Something like:

(global-set-key 'f11 (shell-command "c:/dev/deploy_to_test.bat"))

where the script would look like this:

rsync -avz --del c:/dev/mywebapp z:/srv/tomcat/mywebapp

This is probably better than saving the same file in multiple places, as it ensures the deployment directory always matches what you have in your source repository.

like image 37
gavinb Avatar answered Sep 30 '22 20:09

gavinb