From 2124f0e77207ad7281ff19fa5533ceeb0719cfea Mon Sep 17 00:00:00 2001 From: Manuel Giraud Date: Thu, 26 May 2022 18:25:52 +0200 Subject: [PATCH] add a timestamp when a bookmark is set. add the 'timestamp option to `bookmark-sort-flag' to display bookmark list sorted by those timestamp. --- etc/NEWS | 5 +++ lisp/bookmark.el | 92 ++++++++++++++++++++++++++++-------------------- 2 files changed, 58 insertions(+), 39 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 4ebaf6e07a..a93d917df0 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -1673,6 +1673,11 @@ manual for more details. Types are registered via a 'bookmark-handler-type' symbol property on the jumping function. ++++ +*** 'bookmark-sort-flag' can now be set to 'timestamp. +This will display bookmark list from most recently set to least +recently set. + --- *** New minor mode 'elide-head-mode'. Enabling this minor mode turns on hiding header material, like diff --git a/lisp/bookmark.el b/lisp/bookmark.el index c604395dd7..ce925df8fc 100644 --- a/lisp/bookmark.el +++ b/lisp/bookmark.el @@ -115,10 +115,14 @@ bookmark-completion-ignore-case (defcustom bookmark-sort-flag t - "Non-nil means that bookmarks will be displayed sorted by bookmark name. + "T means that bookmarks will be displayed sorted by bookmark name. +TIMESTAMP means that bookmarks will be displayed sorted from most +recently set to last recently set. Otherwise they will be displayed in LIFO order (that is, most -recently set ones come first, oldest ones come last)." - :type 'boolean) +recently created ones come first, oldest ones come last)." + :type '(choice (const :tag "By name" t) + (const :tag "By modified time" timestamp) + (const :tag "By creation time" nil))) (defcustom bookmark-menu-confirm-deletion nil @@ -460,6 +464,10 @@ bookmark-get-handler "Return the handler function for BOOKMARK-NAME-OR-RECORD, or nil if none." (bookmark-prop-get bookmark-name-or-record 'handler)) +(defun bookmark-get-timestamp (bookmark-name-or-record) + "Return the timestamp for BOOKMARK-NAME-OR-RECORD, or nil if none." + (bookmark-prop-get bookmark-name-or-record 'timestamp)) + (defvar bookmark-history nil "The history list for bookmark functions.") @@ -497,6 +505,21 @@ bookmark--remove-fringe-mark (when (eq 'bookmark (overlay-get temp 'category)) (delete-overlay (setq found temp)))))))))) +(defun bookmark-maybe-sort-alist () + "Return `bookmark-alist' for display. +If `bookmark-sort-flag' is T, then return a sorted by name copy of the alist. +If `bookmark-sort-flag' is TIMESTAMP, then return a sorted by last modified +copy of the alist. Otherwise, just return `bookmark-alist', which by default +is ordered from most recently created to least recently created bookmark." + (let ((copy (copy-alist bookmark-alist))) + (cond ((eq bookmark-sort-flag t) + (sort copy (lambda (x y) (string-lessp (car x) (car y))))) + ((eq bookmark-sort-flag 'timestamp) + (sort copy (lambda (x y) + (time-less-p (bookmark-get-timestamp y) + (bookmark-get-timestamp x))))) + (t copy)))) + (defun bookmark-completing-read (prompt &optional default) "Prompting with PROMPT, read a bookmark name in completion. PROMPT will get a \": \" stuck on the end no matter what, so you @@ -506,10 +529,8 @@ bookmark-completing-read (bookmark-maybe-load-default-file) ; paranoia (if (listp last-nonmenu-event) (bookmark-menu-popup-paned-menu t prompt - (if bookmark-sort-flag - (sort (bookmark-all-names) - 'string-lessp) - (bookmark-all-names))) + (mapcar 'bookmark-name-from-full-record + (bookmark-maybe-sort-alist))) (let* ((completion-ignore-case bookmark-completion-ignore-case) (default (unless (equal "" default) default))) (completing-read (format-prompt prompt default) @@ -630,7 +651,8 @@ bookmark-make-record-default (point) (- (point) bookmark-search-size)) nil)))) - (position . ,(or posn (point))))) + (position . ,(or posn (point))) + (timestamp . ,(current-time)))) ;;; File format stuff @@ -1140,15 +1162,6 @@ bookmark-maybe-load-default-file (car bookmark-bookmarks-timestamp))))))) (bookmark-load (car bookmark-bookmarks-timestamp) t t)))) -(defun bookmark-maybe-sort-alist () - "Return `bookmark-alist' for display. -If `bookmark-sort-flag' is non-nil, then return a sorted copy of the alist. -Otherwise, just return `bookmark-alist', which by default is ordered -from most recently created to least recently created bookmark." - (if bookmark-sort-flag - (sort (copy-alist bookmark-alist) - (lambda (x y) (string-lessp (car x) (car y)))) - bookmark-alist)) (defvar bookmark-after-jump-hook nil @@ -1825,27 +1838,28 @@ bookmark-bmenu--revert entries))) ;; The value of `bookmark-sort-flag' might have changed since the ;; last time the buffer contents were generated, so re-check it. - (if bookmark-sort-flag - (progn - (setq tabulated-list-sort-key '("Bookmark Name" . nil)) - (setq tabulated-list-entries entries)) - (setq tabulated-list-sort-key nil) - ;; And since we're not sorting by bookmark name, show bookmarks - ;; according to order of creation, with the most recently - ;; created bookmarks at the top and the least recently created - ;; at the bottom. - ;; - ;; Note that clicking the column sort toggle for the bookmark - ;; name column will invoke the `tabulated-list-mode' sort, which - ;; uses `bookmark-bmenu--name-predicate' to sort lexically by - ;; bookmark name instead of by (reverse) creation order. - ;; Clicking the toggle again will reverse the lexical sort, but - ;; the sort will still be lexical not creation-order. However, - ;; if the user reverts the buffer, then the above check of - ;; `bookmark-sort-flag' will happen again and the buffer will - ;; go back to a creation-order sort. This is all expected - ;; behavior, as documented in `bookmark-bmenu-mode'. - (setq tabulated-list-entries (reverse entries))) + (cond ((eq bookmark-sort-flag t) + (setq tabulated-list-sort-key '("Bookmark Name" . nil) + tabulated-list-entries entries)) + ((or (null bookmark-sort-flag) + (eq bookmark-sort-flag 'timestamp)) + (setq tabulated-list-sort-key nil) + ;; And since we're not sorting by bookmark name, show bookmarks + ;; according to order of creation, with the most recently + ;; created bookmarks at the top and the least recently created + ;; at the bottom. + ;; + ;; Note that clicking the column sort toggle for the bookmark + ;; name column will invoke the `tabulated-list-mode' sort, which + ;; uses `bookmark-bmenu--name-predicate' to sort lexically by + ;; bookmark name instead of by (reverse) creation order. + ;; Clicking the toggle again will reverse the lexical sort, but + ;; the sort will still be lexical not creation-order. However, + ;; if the user reverts the buffer, then the above check of + ;; `bookmark-sort-flag' will happen again and the buffer will + ;; go back to a creation-order sort. This is all expected + ;; behavior, as documented in `bookmark-bmenu-mode'. + (setq tabulated-list-entries (reverse entries)))) ;; Generate the header only after `tabulated-list-sort-key' is ;; settled, because if that's non-nil then the sort-direction ;; indicator will be shown in the named column, but if it's @@ -1953,7 +1967,7 @@ bookmark-bmenu-mode ,@(if bookmark-bmenu-toggle-filenames '(("File" 0 bookmark-bmenu--file-predicate)))]) (setq tabulated-list-padding bookmark-bmenu-marks-width) - (when bookmark-sort-flag + (when (eq bookmark-sort-flag t) (setq tabulated-list-sort-key '("Bookmark Name" . nil))) (add-hook 'tabulated-list-revert-hook #'bookmark-bmenu--revert nil t)' (setq revert-buffer-function 'bookmark-bmenu--revert) -- 2.36.0