I want Emacs lisp to work with the 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 with 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 asks what to do for the file if I open the user interface emacsclient -t --socket-name server-2 , but I want to do all this in the background and not use the Emacs prompt to continue the process. How can i do this? Is it possible to make Emacs some kind of error if the file cannot be locked?
EDIT : @event_jr suggested an answer using file-locked-p . I think this works most of the time. However, I think another Emacs process may lock the file between executing file-locked-p and lock-buffer . So, I will leave this question open. It is resolved. Thank @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
source share