]> git.eshelyaron.com Git - emacs.git/commitdiff
Support sorting bookmark completions by last modified
authorEshel Yaron <me@eshelyaron.com>
Sat, 3 Feb 2024 15:06:02 +0000 (16:06 +0100)
committerEshel Yaron <me@eshelyaron.com>
Sat, 3 Feb 2024 15:15:24 +0000 (16:15 +0100)
* lisp/bookmark.el (bookmark-sort-by-last-modified-time): New fun.
(bookmark-completing-read): Provide it as a sorting option via
'minibuffer-completions-sort-orders'.

lisp/bookmark.el

index e394295e8e30f915fcc841a6842e1da4ebdc1758..b8e1fcbf70d3ff182516f292e2de529da78f2d80 100644 (file)
@@ -527,6 +527,19 @@ See user option `bookmark-fringe-mark'."
               (when (eq 'bookmark (overlay-get temp 'category))
                 (delete-overlay (setq found temp))))))))))
 
+(defun bookmark-sort-by-last-modified-time (names)
+  "Sort bookmark NAMES by bookmark last modified time, then alphabetically."
+  (sort names (lambda (x y)
+                (let ((tx (bookmark-get-last-modified x))
+                      (ty (bookmark-get-last-modified y)))
+                  (if tx
+                      (if ty
+                          (or (time-less-p ty tx)
+                              (and (time-equal-p ty tx)
+                                   (string-lessp x y)))
+                        t)
+                    (unless ty (string-lessp x y)))))))
+
 (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.
@@ -556,15 +569,22 @@ If DEFAULT is nil then return empty string for empty input."
       (bookmark-menu-popup-paned-menu t prompt
                                       (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)
-                       (completion-table-with-metadata
-                        bookmark-alist
-                        '((category . bookmark)
-                          (narrow-completions-function
-                           . bookmark-narrow-completions-by-type)))
-                       nil 0 nil 'bookmark-history default))))
+    (let ((default (unless (equal "" default) default)))
+      (minibuffer-with-setup-hook
+          (lambda ()
+            (setq-local completion-ignore-case bookmark-completion-ignore-case)
+            (setq-local minibuffer-completions-sort-orders
+                        (cons '(?m "modified" "Sort by last modified time"
+                                   bookmark--sort-by-last-modified-time
+                                   "latest modified first")
+                              minibuffer-completions-sort-orders)))
+        (completing-read (format-prompt prompt default)
+                         (completion-table-with-metadata
+                          bookmark-alist
+                          `((category . bookmark)
+                            (narrow-completions-function
+                             . bookmark-narrow-completions-by-type)))
+                         nil 0 nil 'bookmark-history default)))))
 
 (defun bookmark-narrow-completions-by-type ()
   "Restrict bookmark completions list to bookmarks of a given type."