branch: externals/compat
commit b8594dadff23e191670412beba468c91e9308311
Author: Philip Kaludercic <phil...@posteo.net>
Commit: Philip Kaludercic <phil...@posteo.net>

    Add file-attribute-* accessors and file-attribute-collect
---
 MANUAL          |  12 +++++++
 compat-26.el    | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 compat-tests.el |  44 +++++++++++++++++++++++++
 compat.texi     |  49 ++++++++++++++++++++++++++-
 4 files changed, 204 insertions(+), 1 deletion(-)

diff --git a/MANUAL b/MANUAL
index c3224dae89..f223e3f959 100644
--- a/MANUAL
+++ b/MANUAL
@@ -253,6 +253,18 @@ provided by Compat by default:
 
   This function can also be used as a generalised variable.  To use
   this you need to explicitly require ~compat-26~.
+- Function: file-attribute-type :: See [[info:elisp#File Attributes][(elisp) 
File Attributes]].
+- Function: file-attribute-link-number :: See [[info:elisp#File 
Attributes][(elisp) File Attributes]].
+- Function: file-attribute-user-id :: See [[info:elisp#File 
Attributes][(elisp) File Attributes]].
+- Function: file-attribute-group-id :: See [[info:elisp#File 
Attributes][(elisp) File Attributes]].
+- Function: file-attribute-access-time :: See [[info:elisp#File 
Attributes][(elisp) File Attributes]].
+- Function: file-attribute-modification-time :: See [[info:elisp#File 
Attributes][(elisp) File Attributes]].
+- Function: file-attribute-status-change-time :: See [[info:elisp#File 
Attributes][(elisp) File Attributes]].
+- Function: file-attribute-size :: See [[info:elisp#File Attributes][(elisp) 
File Attributes]].
+- Function: file-attribute-modes :: See [[info:elisp#File Attributes][(elisp) 
File Attributes]].
+- Function: file-attribute-inode-number :: See [[info:elisp#File 
Attributes][(elisp) File Attributes]].
+- Function: file-attribute-device-number :: See [[info:elisp#File 
Attributes][(elisp) File Attributes]].
+- Function: file-attribute-collect :: Defined in ~files.el~.
 
 These functions are prefixed with ~compat~ prefix, and are only loaded
 when ~compat-26~ is required:
diff --git a/compat-26.el b/compat-26.el
index 3da78cffc6..07ab3a48db 100644
--- a/compat-26.el
+++ b/compat-26.el
@@ -425,6 +425,106 @@ the variable `temporary-file-directory' is returned."
           default-directory
         temporary-file-directory))))
 
+;;* UNTESTED
+(compat-defun file-attribute-type (attributes)
+  "The type field in ATTRIBUTES returned by `file-attributes'.
+The value is either t for directory, string (name linked to) for
+symbolic link, or nil."
+  (nth 0 attributes))
+
+;;* UNTESTED
+(compat-defun file-attribute-link-number (attributes)
+  "Return the number of links in ATTRIBUTES returned by `file-attributes'."
+  (nth 1 attributes))
+
+;;* UNTESTED
+(compat-defun file-attribute-user-id (attributes)
+  "The UID field in ATTRIBUTES returned by `file-attributes'.
+This is either a string or a number.  If a string value cannot be
+looked up, a numeric value, either an integer or a float, is
+returned."
+  (nth 2 attributes))
+
+;;* UNTESTED
+(compat-defun file-attribute-group-id (attributes)
+  "The GID field in ATTRIBUTES returned by `file-attributes'.
+This is either a string or a number.  If a string value cannot be
+looked up, a numeric value, either an integer or a float, is
+returned."
+  (nth 3 attributes))
+
+;;* UNTESTED
+(compat-defun file-attribute-access-time (attributes)
+  "The last access time in ATTRIBUTES returned by `file-attributes'.
+This a Lisp timestamp in the style of `current-time'."
+  (nth 4 attributes))
+
+;;* UNTESTED
+(compat-defun file-attribute-modification-time (attributes)
+  "The modification time in ATTRIBUTES returned by `file-attributes'.
+This is the time of the last change to the file's contents, and
+is a Lisp timestamp in the style of `current-time'."
+  (nth 5 attributes))
+
+;;* UNTESTED
+(compat-defun file-attribute-status-change-time (attributes)
+  "The status modification time in ATTRIBUTES returned by `file-attributes'.
+This is the time of last change to the file's attributes: owner
+and group, access mode bits, etc., and is a Lisp timestamp in the
+style of `current-time'."
+  (nth 6 attributes))
+
+;;* UNTESTED
+(compat-defun file-attribute-size (attributes)
+  "The integer size (in bytes) in ATTRIBUTES returned by `file-attributes'."
+  (nth 7 attributes))
+
+;;* UNTESTED
+(compat-defun file-attribute-modes (attributes)
+  "The file modes in ATTRIBUTES returned by `file-attributes'.
+This is a string of ten letters or dashes as in ls -l."
+  (nth 8 attributes))
+
+;;* UNTESTED
+(compat-defun file-attribute-inode-number (attributes)
+  "The inode number in ATTRIBUTES returned by `file-attributes'.
+It is a nonnegative integer."
+  (nth 10 attributes))
+
+;;* UNTESTED
+(compat-defun file-attribute-device-number (attributes)
+  "The file system device number in ATTRIBUTES returned by `file-attributes'.
+It is an integer."
+  (nth 11 attributes))
+
+(compat-defun file-attribute-collect (attributes &rest attr-names)
+  "Return a sublist of ATTRIBUTES returned by `file-attributes'.
+ATTR-NAMES are symbols with the selected attribute names.
+
+Valid attribute names are: type, link-number, user-id, group-id,
+access-time, modification-time, status-change-time, size, modes,
+inode-number and device-number."
+  (let ((idx '((type . 0)
+               (link-number . 1)
+               (user-id . 2)
+               (group-id . 3)
+               (access-time . 4)
+               (modification-time . 5)
+               (status-change-time . 6)
+               (size . 7)
+               (modes . 8)
+               (inode-number . 10)
+               (device-number . 11)))
+        result)
+    (while attr-names
+      (let ((attr (pop attr-names)))
+        (if (assq attr idx)
+            (push (nth (cdr (assq attr idx))
+                       attributes)
+                  result)
+          (error "Wrong attribute name '%S'" attr))))
+    (nreverse result)))
+
 ;;;; Defined in subr-x.el
 
 (compat-defmacro if-let* (varlist then &rest else)
diff --git a/compat-tests.el b/compat-tests.el
index 4c61e738c4..2c0e93d133 100644
--- a/compat-tests.el
+++ b/compat-tests.el
@@ -1662,5 +1662,49 @@ being compared against."
   (ought 29 2020 2)
   (ought 28 2021 2))
 
+(compat-deftest file-attribute-collect
+  (ought '(0) '(0 1 2 3 4 5 6 7 8 9 10 11)
+         'type)
+  (ought '(8) '(0 1 2 3 4 5 6 7 8 9 10 11)
+         'modes)
+  (ought '(10) '(0 1 2 3 4 5 6 7 8 9 10 11)
+         'inode-number)
+  (expect error '(0 1 2 3 4 5 6 7 8 9 10 11)
+          'something-else-that-shouldnt-exist)
+  (ought '(0 8) '(0 1 2 3 4 5 6 7 8 9 10 11)
+         'type 'modes)
+  (ought '(8 0) '(0 1 2 3 4 5 6 7 8 9 10 11)
+         'modes 'type)
+  (ought '(0 8 8) '(0 1 2 3 4 5 6 7 8 9 10 11)
+         'type 'modes 'modes)
+  (ought '(8 0 8) '(0 1 2 3 4 5 6 7 8 9 10 11)
+         'modes 'type 'modes)
+  (ought '(0 1 2 3 4 5 6 7 8 10 11)
+         '(0 1 2 3 4 5 6 7 8 9 10 11)
+         'type
+         'link-number
+         'user-id
+         'group-id
+         'access-time
+         'modification-time
+         'status-change-time
+         'size
+         'modes
+         'inode-number
+         'device-number)
+  (ought '(0 1 2 3 4 5 6 7 9 10 11)
+         '(1 0 3 2 5 4 7 6 9 8 11 10)
+         'link-number
+         'type
+         'group-id
+         'user-id
+         'modification-time
+         'access-time
+         'size
+         'status-change-time
+         'modes
+         'device-number
+         'inode-number))
+
 (provide 'compat-tests)
 ;;; compat-tests.el ends here
diff --git a/compat.texi b/compat.texi
index 4fbe7ce372..3fc6e653b9 100644
--- a/compat.texi
+++ b/compat.texi
@@ -534,7 +534,54 @@ This function can also be used as a generalised variable.  
To use
 this you need to explicitly require @code{compat-26}.
 @end defun
 
-These functions are prefixed with @code{compat} prefix, and are only loaded
+@defun file-attribute-type
+See @ref{File Attributes,(elisp) File Attributes,,elisp,}.
+@end defun
+
+@defun file-attribute-link-number
+See @ref{File Attributes,(elisp) File Attributes,,elisp,}.
+@end defun
+
+@defun file-attribute-user-id
+See @ref{File Attributes,(elisp) File Attributes,,elisp,}.
+@end defun
+
+@defun file-attribute-group-id
+See @ref{File Attributes,(elisp) File Attributes,,elisp,}.
+@end defun
+
+@defun file-attribute-access-time
+See @ref{File Attributes,(elisp) File Attributes,,elisp,}.
+@end defun
+
+@defun file-attribute-modification-time
+See @ref{File Attributes,(elisp) File Attributes,,elisp,}.
+@end defun
+
+@defun file-attribute-status-change-time
+See @ref{File Attributes,(elisp) File Attributes,,elisp,}.
+@end defun
+
+@defun file-attribute-size
+See @ref{File Attributes,(elisp) File Attributes,,elisp,}.
+@end defun
+
+@defun file-attribute-modes
+See @ref{File Attributes,(elisp) File Attributes,,elisp,}.
+@end defun
+
+@defun file-attribute-inode-number
+See @ref{File Attributes,(elisp) File Attributes,,elisp,}.
+@end defun
+
+@defun file-attribute-device-number
+See @ref{File Attributes,(elisp) File Attributes,,elisp,}.
+@end defun
+
+@defun file-attribute-collect
+Defined in @code{files.el}.
+@end defun
+
 when @code{compat-26} is required:
 
 @defun compat-assoc

Reply via email to