aboutsummaryrefslogtreecommitdiffstats
path: root/lisp
diff options
context:
space:
mode:
authorStefan Kangas2022-03-24 09:23:31 +0100
committerLars Ingebrigtsen2022-03-24 09:23:31 +0100
commitb4f504a0ea90eb7fed2f3c0291d0eab43ef483d6 (patch)
treeb19636c62595bfb5542f232209160d9d2d6526b1 /lisp
parent72ab6c4f141ce249c34933d1ebc91e2491e34b3a (diff)
downloademacs-b4f504a0ea90eb7fed2f3c0291d0eab43ef483d6.tar.gz
emacs-b4f504a0ea90eb7fed2f3c0291d0eab43ef483d6.zip
Load desktop without prompting if process is dead
* doc/emacs/misc.texi (Saving Emacs Sessions): Document the new 'check' value. * etc/NEWS: Announce the change (bug#1474). * lisp/desktop.el (desktop-load-locked-desktop): Add new value 'check' to load desktop file without prompting if locking Emacs process does not exist on the local machine. (Bug#1474) (desktop-read): Extract function from here... (desktop--load-locked-desktop-p): ...to here. New function handles the semantics of 'desktop-load-locked-desktop', including above new value 'check'. (desktop--emacs-pid-running-p): New function. * test/lisp/desktop-tests.el: New file with tests for the above.
Diffstat (limited to 'lisp')
-rw-r--r--lisp/desktop.el50
1 files changed, 39 insertions, 11 deletions
diff --git a/lisp/desktop.el b/lisp/desktop.el
index e7a368e21f5..773f0f050f9 100644
--- a/lisp/desktop.el
+++ b/lisp/desktop.el
@@ -230,16 +230,26 @@ Zero or nil means disable auto-saving due to idleness."
230(defcustom desktop-load-locked-desktop 'ask 230(defcustom desktop-load-locked-desktop 'ask
231 "Specifies whether the desktop should be loaded if locked. 231 "Specifies whether the desktop should be loaded if locked.
232Possible values are: 232Possible values are:
233 t -- load anyway. 233 t -- load anyway.
234 nil -- don't load. 234 nil -- don't load.
235 ask -- ask the user. 235 ask -- ask the user.
236If the value is nil, or `ask' and the user chooses not to load the desktop, 236 check-pid -- load if locking Emacs process is missing locally.
237the normal hook `desktop-not-loaded-hook' is run." 237
238If the value is nil, or `ask' and the user chooses not to load
239the desktop, the normal hook `desktop-not-loaded-hook' is run.
240
241If the value is `check-pid', load the desktop if the Emacs
242process that has locked it is not running on the local machine.
243This should not be used in circumstances where the locking Emacs
244might still be running on another machine. That could be the
245case if you have remotely mounted (NFS) paths in
246`desktop-dirname'."
238 :type 247 :type
239 '(choice 248 '(choice
240 (const :tag "Load anyway" t) 249 (const :tag "Load anyway" t)
241 (const :tag "Don't load" nil) 250 (const :tag "Don't load" nil)
242 (const :tag "Ask the user" ask)) 251 (const :tag "Ask the user" ask)
252 (const :tag "Load if no local process" check-pid))
243 :group 'desktop 253 :group 'desktop
244 :version "22.2") 254 :version "22.2")
245 255
@@ -662,6 +672,28 @@ DIRNAME omitted or nil means use `desktop-dirname'."
662 (integerp owner))) 672 (integerp owner)))
663 owner))) 673 owner)))
664 674
675(defun desktop--emacs-pid-running-p (pid)
676 "Return t if an Emacs process with PID exists."
677 (when-let ((attr (process-attributes pid)))
678 (equal (alist-get 'comm attr)
679 (file-name-nondirectory (car command-line-args)))))
680
681(defun desktop--load-locked-desktop-p (owner)
682 "Return t if a locked desktop should be loaded.
683OWNER is the pid in the lock file.
684The return value of this function depends on the value of
685`desktop-load-locked-desktop'."
686 (pcase desktop-load-locked-desktop
687 ('ask
688 (unless (daemonp)
689 (y-or-n-p (format "Warning: desktop file appears to be in use by PID %s.\n\
690Using it may cause conflicts. Use it anyway? " owner))))
691 ('check-pid
692 (or (eq (emacs-pid) owner)
693 (not (desktop--emacs-pid-running-p owner))))
694 ('nil nil)
695 (_ t)))
696
665(defun desktop-claim-lock (&optional dirname) 697(defun desktop-claim-lock (&optional dirname)
666 "Record this Emacs process as the owner of the desktop file in DIRNAME. 698 "Record this Emacs process as the owner of the desktop file in DIRNAME.
667DIRNAME omitted or nil means use `desktop-dirname'." 699DIRNAME omitted or nil means use `desktop-dirname'."
@@ -1263,11 +1295,7 @@ It returns t if a desktop file was loaded, nil otherwise.
1263 (desktop-save nil) 1295 (desktop-save nil)
1264 (desktop-autosave-was-enabled)) 1296 (desktop-autosave-was-enabled))
1265 (if (and owner 1297 (if (and owner
1266 (memq desktop-load-locked-desktop '(nil ask)) 1298 (not (desktop--load-locked-desktop-p owner)))
1267 (or (null desktop-load-locked-desktop)
1268 (daemonp)
1269 (not (y-or-n-p (format "Warning: desktop file appears to be in use by PID %s.\n\
1270Using it may cause conflicts. Use it anyway? " owner)))))
1271 (let ((default-directory desktop-dirname)) 1299 (let ((default-directory desktop-dirname))
1272 (setq desktop-dirname nil) 1300 (setq desktop-dirname nil)
1273 (run-hooks 'desktop-not-loaded-hook) 1301 (run-hooks 'desktop-not-loaded-hook)