问题
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
回答1:
There doesn't seem to be a way to make emacsclient --eval
return error code. But You can make it print what you need to know:
#!/usr/bin/env bash
es1="server-1"
es2="server-2"
emacs=/Applications/Emacs.app/Contents/MacOS/Emacs
[ -e $emacs ] || emacs=emacs
start-server () {
read -r -d '' script <<EOF
(progn
(setq server-name "$1")
(server-start)
(require 'cl)
(defun my-set-buffer-modified-p (flag)
(flet ((ask-user-about-lock
(&rest args)
;; (signal 'file-locked args)
(apply 'error "%s was locked by %s" args)))
(set-buffer-modified-p flag))))
EOF
$emacs -q --daemon --eval "$script"
}
emacs-eval () {
echo "@$1 >>> $2"
emacsclient --socket-name "$1" --eval "$2"
}
kill-emacs () {
emacs-eval "$1" '(kill-emacs)'
}
find-and-lock-file () {
read -r -d '' script <<EOF
(with-current-buffer (find-file-noselect "$2")
(my-set-buffer-modified-p t))
EOF
emacs-eval "$1" "$script"
}
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
EDIT: I've dug around the source a little bit and found a reference to
ask-user-about-lock
, which solves it nicely.
回答2:
I found another answer using run-with-timer
, to bypass the emacsclient --eval bug, so that I can check that (signal 'file-locked ...)
works in "normal" situation.
#!/usr/bin/env bash
es1="server-1"
es2="server-2"
emacs=/Applications/Emacs.app/Contents/MacOS/Emacs
[ -e $emacs ] || emacs=emacs
start-server () {
read -r -d '' script <<EOF
(progn
(setq server-name "$1")
(server-start)
(require 'cl)
(defvar my-file-is-locked "undefined")
(defun my-set-buffer-modified-p (flag)
(flet ((ask-user-about-lock
(&rest args)
(setq my-file-is-locked "no")
(signal 'file-locked args)))
(set-buffer-modified-p flag)
(setq my-file-is-locked "yes"))))
EOF
$emacs -q --daemon --eval "$script"
}
emacs-eval () {
echo "@$1 >>> $2"
emacsclient --socket-name "$1" --eval "$2"
}
kill-emacs () {
emacs-eval "$1" '(kill-emacs)'
}
find-and-lock-file () {
read -r -d '' script <<EOF
(run-with-timer 0 nil (lambda ()
(with-current-buffer (find-file-noselect "$2")
(my-set-buffer-modified-p t))))
EOF
emacs-eval "$1" "$script"
}
file-locked-p () {
emacs-eval "$1" "(message \"my-file-is-locked = %s\" my-file-is-locked)"
}
start-server $es1
start-server $es2
find-and-lock-file $es1 /tmp/dummy
find-and-lock-file $es2 /tmp/dummy
file-locked-p $es1
file-locked-p $es2
kill-emacs $es1
kill-emacs $es2
来源:https://stackoverflow.com/questions/9608715/how-to-make-emacs-lock-buffer-fail-when-it-cannot-lock-a-file