]> git.eshelyaron.com Git - emacs.git/commitdiff
; Provide metadata for 'recentf' completions
authorEshel Yaron <me@eshelyaron.com>
Tue, 16 Jan 2024 12:39:39 +0000 (13:39 +0100)
committerEshel Yaron <me@eshelyaron.com>
Tue, 16 Jan 2024 12:51:52 +0000 (13:51 +0100)
* lisp/recentf.el (recentf-completion-table): New function.
(recentf-open): Use it.  Allow specifying file names that are not in
'recentf-list', after confirmation.

lisp/recentf.el

index dfb783205df3646e64714e57b5de402ce44856f0..4c8b852197a405833ad0aa1f58a657842e64f321 100644 (file)
@@ -484,18 +484,35 @@ Return non-nil if F1 is less than F2."
 ;;; Open files
 ;;
 
+(defun recentf-completion-table (string pred action)
+  "Completion table for recent file names.
+
+See Info node `(elisp)Programmed Completion' for the meaning of
+STRING, PRED and ACTION."
+  (if (eq action 'metadata)
+      `(metadata
+        ;; Report `recent-file' rather than `file' as the category, so
+        ;; users can configure the two separately.
+        (category . recent-file)
+        ;; Sort candidates by their position in `recentf-list'.
+        (cycle-sort-function   . identity)
+        (display-sort-function . identity)
+        ,@(when completions-detailed
+            '((affixation-function . completion-file-name-affixation))))
+    (complete-with-action action recentf-list string pred)))
+
 ;;;###autoload
 (defun recentf-open (file)
-  "Prompt for FILE in `recentf-list' and visit it.
-Enable `recentf-mode' if it isn't already."
+  "Open recently visited FILE.
+
+Interactively, prompt for FILE, and enable `recentf-mode' if it
+is not already on."
   (interactive
-   (let ((completions-sort nil))
-     (list
-      (progn (unless recentf-mode (recentf-mode 1))
-             (completing-read (format-prompt "Open recent file" nil)
-                              recentf-list nil t)))))
-  (when file
-    (funcall recentf-menu-action file)))
+   (progn
+     (unless recentf-mode (recentf-mode 1))
+     (list (completing-read "Open recent file: "
+                            #'recentf-completion-table nil 'confirm))))
+  (funcall recentf-menu-action file))
 
 ;;;###autoload
 (defalias 'recentf 'recentf-open)