From 409afb58f365d609c80c598a5de687213c027065 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Sun, 18 Jun 2023 17:06:03 +0800 Subject: desktop and bookmark: proper save mechanism * bookmark-conf.el (durand-saving-bookmarks): This variable indicates whether or not we are saving bookmarks already. (#'bookmark-write-file): Just a formating change. (durand-save-bookmark-or-desktop): Save bookmarks and/or the desktop file, depending on its argument. (ctl-x-r-map): Bind 'C-x r s' to `durand-save-bookmark-or-desktop`. * desktop-conf.el (durand-saving-bookmarks): In case this variable is not defined, silence the errors. (desktop-also-save-bookmarks): If we are not already saving bookmarks, save the bookmarks as well. (desktop-save-hook): Add `desktop-also-save-bookmarks` to this hook, so that when saving desktop and not already saving bookmarks, Emacs can automatically save bookmarks as well. --- bookmark-conf.el | 30 +++++++++++++++++++++++++++++- desktop-conf.el | 12 ++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/bookmark-conf.el b/bookmark-conf.el index 074628f..eddb3d5 100644 --- a/bookmark-conf.el +++ b/bookmark-conf.el @@ -442,8 +442,36 @@ Modified by Durand to use `print' instead of `pp'. (kill-buffer (current-buffer)) (progress-reporter-done reporter)))))) -(advice-add #'bookmark-write-file :override #'durand-bookmark-write-file) +(advice-add #'bookmark-write-file :override + #'durand-bookmark-write-file) +;;; Intelligent saving of bookmarks and desktop files + +(load-config "desktop-conf.el") + +(defvar durand-saving-bookmarks nil + "Non-nil if we are saving bookmarks already.") + +(defun durand-save-bookmark-or-desktop (&optional arg) + "Save bookmarks and/or desktop depending on ARG. + +If and only if ARG is non-nil, save the desktop file. + +If and only if ARG is an integer, do not save bookmarks. + +Below are listed three possible scenarios: + +1. ARG = nil: save bookmarks only. +2. ARG = '(4): save both bookmarks and desktop. +3. ARG = integer: save desktop only." + (interactive "P") + (cond (arg + (let ((durand-saving-bookmarks t)) + (desktop-save (car desktop-path)) + (message "desktop saved")))) + (cond ((not (integerp arg)) (bookmark-save)))) + +(define-key ctl-x-r-map (vector ?s) #'durand-save-bookmark-or-desktop) (provide 'bookmark-conf) ;;; bookmark-conf.el ends here diff --git a/desktop-conf.el b/desktop-conf.el index 462c549..074aad0 100644 --- a/desktop-conf.el +++ b/desktop-conf.el @@ -198,5 +198,17 @@ no questions asked." (desktop-save-mode 1) +;;; Add a hook to save bookmarks if we are not already saving them. + +(defvar durand-saving-bookmarks) + +(defun desktop-also-save-bookmarks () + "Save bookmarks if we are not already saving bookmarks." + (cond + ((and (functionp #'bookmark-save) (not durand-saving-bookmarks)) + (bookmark-save)))) + +(add-hook 'desktop-save-hook #'desktop-also-save-bookmarks) + (provide 'desktop-conf) ;;; desktop-conf.el ends here -- cgit v1.2.3-18-g5258