aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStéphane Marks2025-12-15 16:07:14 -0500
committerSean Whitton2025-12-17 11:57:26 +0000
commit2b1440946e20bcf910f03cd665f68a268c687192 (patch)
treea7d46768565e05eb447ac3f9c5614ff467c717e6
parent14f16535ed219f601a423f9a7e0d2adac30fa897 (diff)
downloademacs-2b1440946e20bcf910f03cd665f68a268c687192.tar.gz
emacs-2b1440946e20bcf910f03cd665f68a268c687192.zip
New optional recentf autosave timer (bug#80002)
* etc/NEWS: Announce the new user option. * lisp/recentf.el (recentf-auto-cleanup): When canceling the auto-cleanup timer, set recentf-auto-cleanup-timer to nil to avoid false positive timerp. (recentf-autosave-interval): New defcustom. (recentf--autosave-timer, recentf--cancel-autosave-timer) (recentf--manage-autosave-timer): New defun. (recentf-mode): Call recentf--manage-autosave-timer.
-rw-r--r--etc/NEWS13
-rw-r--r--lisp/recentf.el39
2 files changed, 51 insertions, 1 deletions
diff --git a/etc/NEWS b/etc/NEWS
index 1c53e0789a6..311bf044476 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1536,6 +1536,19 @@ compatible.
1536** Recentf 1536** Recentf
1537 1537
1538--- 1538---
1539*** You can now regularly auto-save recently opened files.
1540Customize user option 'recentf-autosave-interval' to the number of
1541seconds between auto saving recently opened files. For example,
1542customize this variable to 300 to save recently opened files every 5
1543minutes. In Elisp, use 'setopt' not 'setq'.
1544
1545If 'recentf-autosave-interval' is nil, auto saving is disabled; this is
1546the default.
1547
1548This user option is in sympathy with save-place, and savehist autosave
1549timers.
1550
1551---
1539*** New user option 'recentf-show-messages'. 1552*** New user option 'recentf-show-messages'.
1540'recentf-save-list' can print a message when saving the recentf list. 1553'recentf-save-list' can print a message when saving the recentf list.
1541The new option, if set to nil, suppresses this message. 1554The new option, if set to nil, suppresses this message.
diff --git a/lisp/recentf.el b/lisp/recentf.el
index 519a6e1dfec..4b9c1d3d270 100644
--- a/lisp/recentf.el
+++ b/lisp/recentf.el
@@ -393,7 +393,8 @@ See also the option `recentf-auto-cleanup'.")
393(defun recentf-auto-cleanup () 393(defun recentf-auto-cleanup ()
394 "Automatic cleanup of the recent list." 394 "Automatic cleanup of the recent list."
395 (when (timerp recentf-auto-cleanup-timer) 395 (when (timerp recentf-auto-cleanup-timer)
396 (cancel-timer recentf-auto-cleanup-timer)) 396 (cancel-timer recentf-auto-cleanup-timer)
397 (setq recentf-auto-cleanup-timer nil))
397 (when recentf-mode 398 (when recentf-mode
398 (setq recentf-auto-cleanup-timer 399 (setq recentf-auto-cleanup-timer
399 (cond 400 (cond
@@ -1353,6 +1354,41 @@ Optional argument N must be a valid digit number. It defaults to 1.
1353 1354
1354;;; Save/load/cleanup the recent list 1355;;; Save/load/cleanup the recent list
1355;; 1356;;
1357(defvar recentf--autosave-timer nil)
1358
1359(defun recentf--cancel-autosave-timer ()
1360 "Cancel `recentf--autosave-timer', if set."
1361 (when (timerp recentf--autosave-timer)
1362 (cancel-timer recentf--autosave-timer))
1363 (setq recentf--autosave-timer nil))
1364
1365(defvar recentf-autosave-interval)
1366
1367(defun recentf--manage-autosave-timer ()
1368 "Set or cancel an invocation of `recentf-save-list' on a timer.
1369If `recentf-mode' is enabled, set the timer, otherwise cancel the timer."
1370 (if (and recentf-mode
1371 recentf-autosave-interval
1372 (null recentf--autosave-timer))
1373 (setq recentf--autosave-timer
1374 (run-with-timer
1375 recentf-autosave-interval
1376 recentf-autosave-interval #'recentf-save-list))
1377 (recentf--cancel-autosave-timer)))
1378
1379(defcustom recentf-autosave-interval nil
1380 "The interval between auto saves of recently opened files.
1381If set to nil, disables timer-based auto saving.
1382Do not set this variable via `setq', use either `setopt' or
1383`customize-option' instead."
1384 :type '(choice (const :tag "Disabled" nil)
1385 (integer :tag "Auto-save interval in seconds"))
1386 :version "31.1"
1387 :set (lambda (sym val)
1388 (set-default sym val)
1389 (recentf--cancel-autosave-timer)
1390 (recentf--manage-autosave-timer)))
1391
1356(defconst recentf-save-file-header 1392(defconst recentf-save-file-header
1357 ;; FIXME: This should arguably be a `lisp-data' file, but currently 1393 ;; FIXME: This should arguably be a `lisp-data' file, but currently
1358 ;; it contains and is used as an executable Elisp code. 1394 ;; it contains and is used as an executable Elisp code.
@@ -1469,6 +1505,7 @@ buffers you switch to a lot, you can say something like the following:
1469 (recentf-hide-menu) 1505 (recentf-hide-menu)
1470 (recentf-save-list)) 1506 (recentf-save-list))
1471 (recentf-auto-cleanup) 1507 (recentf-auto-cleanup)
1508 (recentf--manage-autosave-timer)
1472 (let ((hook-setup (if recentf-mode 'add-hook 'remove-hook))) 1509 (let ((hook-setup (if recentf-mode 'add-hook 'remove-hook)))
1473 (dolist (hook recentf-used-hooks) 1510 (dolist (hook recentf-used-hooks)
1474 (apply hook-setup hook))))) 1511 (apply hook-setup hook)))))