From: Eshel Yaron Date: Sat, 3 Feb 2024 15:06:02 +0000 (+0100) Subject: Support sorting bookmark completions by last modified X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=87c5302ad1e472dd0ad6744d6a8347586a46d690;p=emacs.git Support sorting bookmark completions by last modified * 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'. --- diff --git a/lisp/bookmark.el b/lisp/bookmark.el index e394295e8e3..b8e1fcbf70d 100644 --- a/lisp/bookmark.el +++ b/lisp/bookmark.el @@ -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."