]> git.eshelyaron.com Git - emacs.git/commitdiff
Add completion annotations for file name completion
authorEshel Yaron <me@eshelyaron.com>
Fri, 5 Jan 2024 18:03:13 +0000 (19:03 +0100)
committerEshel Yaron <me@eshelyaron.com>
Fri, 5 Jan 2024 21:31:31 +0000 (22:31 +0100)
* lisp/minibuffer.el (completion-file-name-affixation): New function.
(completion-file-name-table): Use it as 'affixation-function'.
* doc/emacs/help.texi (Name Help): Move doc of 'completions-detailed'
from here to...
* doc/emacs/mini.texi (Completion Options): ...here.  Improve wording.
* etc/NEWS: Announce.

doc/emacs/help.texi
doc/emacs/mini.texi
etc/NEWS
lisp/minibuffer.el

index 959c7dae375896a0ea839f6ab27041d7ae79e92b..c9fa4cbb077f124ee7e5f9af3f0333f5a0f2ee5a 100644 (file)
@@ -360,14 +360,6 @@ completions to only include symbols with a given property by typing
 @xref{Narrow Completions}, and @ref{Symbol Properties,,,elisp,The
 Emacs Lisp Reference Manual}.
 
-@vindex completions-detailed
-  If the @code{completions-detailed} user option is non-@code{nil},
-some commands provide details about the possible values when
-displaying completions.  For instance, @kbd{C-h o TAB} will then
-include the first line of the doc string, and will also say whether
-each symbol is a function or a variable (and so on).  Which details
-are included varies depending on the command used.
-
 @node Apropos
 @section Apropos
 @cindex apropos
index a423c84a31fbdfc84d67438caf804cd1c96f4013..1d2dfa8fc4f966b1bf34ab42839265b374cf5659 100644 (file)
@@ -887,6 +887,17 @@ face.  The default value of this variable is
 highlighting.  This feature uses the special text property
 @code{cursor-face}.
 
+@vindex completions-detailed
+If the @code{completions-detailed} user option is non-@code{nil}, some
+commands that provide completion display extra details as annotations
+next to completion candidates in the @file{*Completions*} buffer.
+Which details are included varies between different commands.  For
+instance, the completions list for @kbd{C-h o} (@pxref{Name Help})
+includes the first line of the doc string of each symbol, and says
+whether each symbol is a function or a variable (and so on).  For file
+name completion, the extra details annotations include file modes,
+sizes, modification times and ownership information.
+
 @node Minibuffer History
 @section Minibuffer History
 @cindex minibuffer history
index 613b976efc714c41544e86961ac3900e4d8e7831..7694f8b1acce04ad190bb6ff891dcc894e4f6fa7 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -756,6 +756,12 @@ You can now customize the sorting order for any category in
 'completion-category-overrides' that will override the sorting order
 defined in the metadata or in 'completions-sort'.
 
++++
+*** File name completions can now provide detailed candidate annotations.
+With non-nil user option 'completions-detailed', Emacs now displays
+extra details about file name completion candidates in the
+"*Completions*" buffer as completion annotations.
+
 ** Pcomplete
 
 ---
index 8ae9ec295f3da2968918fb16c02f78c91cd09aad..5f517b3f54ba8ac4652bc3057643214722a940bd 100644 (file)
@@ -3358,11 +3358,47 @@ same as `substitute-in-file-name'."
           (completion-table-with-context
            prefix table (substring string beg) nil action)))))))
 
+(defun completion-file-name-affixation (files)
+  "Return completion affixations for file list FILES."
+  (let ((max-file (seq-max (mapcar #'string-width files))))
+    (mapcar
+     (lambda (file)
+       (list
+        file
+        ""                              ; empty prefix
+        (if-let ((attrs
+                  (ignore-errors
+                    (file-attributes
+                     (substitute-in-file-name
+                      (concat minibuffer-completion-base file))
+                     'string))))
+            (propertize
+             (concat (propertize " " 'display
+                                 `(space :align-to ,(+ max-file 2)))
+                     (file-attribute-modes attrs)
+                     " "
+                     (format "%8s" (file-size-human-readable
+                                    (file-attribute-size attrs)))
+                     "   "
+                     (format-time-string
+                      "%Y-%m-%d %T" (file-attribute-modification-time attrs))
+                     "   "
+                     (file-attribute-user-id attrs)
+                     ":"
+                     (file-attribute-group-id attrs))
+             'face 'completions-annotations)
+          "")))
+     files)))
+
 (defun completion-file-name-table (string pred action)
   "Completion table for file names."
   (condition-case nil
       (cond
-       ((eq action 'metadata) '(metadata (category . file)))
+       ((eq action 'metadata)
+        `(metadata
+          (category . file)
+          ,@(when completions-detailed
+              '((affixation-function . completion-file-name-affixation)))))
        ((string-match-p "\\`~[^/\\]*\\'" string)
         (completion-table-with-context "~"
                                        (mapcar (lambda (u) (concat u "/"))