I want to some Emacs lisp to manipulate same file from different Emacs processes. So I wrote the following script to check how lock-buffer
works. However, it stops when trying to lock the file by the second Emacs process (find-and-lock-file $es2 /tmp/dummy
). I need to go to another terminal and send emacsclient --socket-name server-2 --eval '(kill-emacs)'
to stop the Emacs process. Emacs prompts what to do for the file if I open an UI by emacsclient -t --socket-name server-2
, but I want to make it all done in the background and avoid using Emacs prompt to continue the process. How can I do this? Is it possible to make Emacs raise some error when it fails to lock a file?
EDIT: @event_jr proposed an answer using file-locked-p
. I think it works most of the time. However, I think other Emacs process can lock the file between the execution of file-locked-p
and lock-buffer
. So, I will keep this question open. Solved. Thanks, @event_jr!
#!/bin/bash
es1="server-1"
es2="server-2"
start-server () {
emacs -q --daemon --eval "(progn (setq server-name \"$1\") (server-start) (require 'cl))"
}
emacs-eval () {
echo "@$1 >>> $2"
emacsclient --socket-name "$1" --eval "$2"
}
kill-emacs () {
emacs-eval "$1" '(kill-emacs)'
}
find-and-lock-file () {
emacs-eval "$1" "(progn (find-file \"$2\") (set-buffer-modified-p t) (lock-buffer))"
}
start-server $es1
start-server $es2
find-and-lock-file $es1 /tmp/dummy
find-and-lock-file $es2 /tmp/dummy
kill-emacs $es1
kill-emacs $es2