]> git.eshelyaron.com Git - emacs.git/commitdiff
Add annotations for bookmark completion candidates
authorEshel Yaron <me@eshelyaron.com>
Sat, 3 Feb 2024 15:09:15 +0000 (16:09 +0100)
committerEshel Yaron <me@eshelyaron.com>
Sat, 3 Feb 2024 15:15:26 +0000 (16:15 +0100)
* lisp/bookmark.el (bookmark--affixation): New function.
(bookmark-completing-read): Use it as 'affixation-function'.

lisp/bookmark.el

index b8e1fcbf70d3ff182516f292e2de529da78f2d80..976f1bc1886bb7b69d8d163cf8e31dba35020971 100644 (file)
@@ -558,6 +558,33 @@ is ordered from most recently created to least recently created bookmark."
                                 (t (time-less-p ty tx)))))))
           (t copy))))
 
+(defun bookmark--affixation (names)
+  "Return completion affixations for bookmark name list NAMES."
+  (let ((max-name 0)
+        (max-type 0)
+        (all nil)
+        (res nil))
+    (dolist (name names)
+      (setq max-name (max max-name (string-width name)))
+      (let* ((bmrk (assoc name bookmark-alist))
+             (type (bookmark-type-from-full-record bmrk))
+             (type (if (and type (not (string-empty-p type))) type "File")))
+        (setq max-type (max max-type (string-width type)))
+        (push (list name type (bookmark-get-last-modified bmrk)) all)))
+    (pcase-dolist (`(,name ,type ,lmod) all)
+      (push (list name ""
+                  (propertize
+                   (concat (make-string (- (+ max-name 2) (string-width name)) ?\s)
+                           type
+                           (when lmod
+                             (concat (make-string (- (+ max-type 2)
+                                                     (string-width type))
+                                                  ?\s)
+                                     (format-time-string "%Y-%m-%d %T" lmod))))
+                   'face 'completions-annotations))
+            res))
+    res))
+
 (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
@@ -583,7 +610,9 @@ If DEFAULT is nil then return empty string for empty input."
                           bookmark-alist
                           `((category . bookmark)
                             (narrow-completions-function
-                             . bookmark-narrow-completions-by-type)))
+                             . bookmark-narrow-completions-by-type)
+                            ,@(when completions-detailed
+                                '((affixation-function . bookmark--affixation)))))
                          nil 0 nil 'bookmark-history default)))))
 
 (defun bookmark-narrow-completions-by-type ()
@@ -591,7 +620,7 @@ If DEFAULT is nil then return empty string for empty input."
   (let* ((get-type
           (lambda (bmk-record)
             (let ((res (bookmark-type-from-full-record bmk-record)))
-              (if (and res (not (string-empty-p res))) res "Regular"))))
+              (if (and res (not (string-empty-p res))) res "File"))))
          (types (delete-dups (mapcar get-type bookmark-alist)))
          (type (completing-read "Keep bookmark completions of type: "
                                 types nil t)))