emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/compat 8d17ffdd88 29/84: Add file-has-changed-p from Em


From: ELPA Syncer
Subject: [elpa] externals/compat 8d17ffdd88 29/84: Add file-has-changed-p from Emacs 29
Date: Tue, 3 Jan 2023 08:57:33 -0500 (EST)

branch: externals/compat
commit 8d17ffdd88d913e70cd61ea8a85407034a6260a2
Author: Philip Kaludercic <philipk@posteo.net>
Commit: Philip Kaludercic <philipk@posteo.net>

    Add file-has-changed-p from Emacs 29
---
 compat-29.el | 24 ++++++++++++++++++++++++
 compat.texi  | 17 +++++++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/compat-29.el b/compat-29.el
index 9538e99c72..540350d6c0 100644
--- a/compat-29.el
+++ b/compat-29.el
@@ -381,5 +381,29 @@ to `default-directory', and the result will also be 
relative."
      (t
       parent))))
 
+(defvar compat--file-has-changed-p--hash-table (make-hash-table :test #'equal)
+  "Internal variable used by `file-has-changed-p'.")
+
+;;* UNTESTED
+(compat-defun file-has-changed-p (file &optional tag)
+  "Return non-nil if FILE has changed.
+The size and modification time of FILE are compared to the size
+and modification time of the same FILE during a previous
+invocation of `file-has-changed-p'.  Thus, the first invocation
+of `file-has-changed-p' always returns non-nil when FILE exists.
+The optional argument TAG, which must be a symbol, can be used to
+limit the comparison to invocations with identical tags; it can be
+the symbol of the calling function, for example."
+  (let* ((file (directory-file-name (expand-file-name file)))
+         (remote-file-name-inhibit-cache t)
+         (fileattr (file-attributes file 'integer))
+        (attr (and fileattr
+                    (cons (file-attribute-size fileattr)
+                         (file-attribute-modification-time fileattr))))
+        (sym (concat (symbol-name tag) "@" file))
+        (cachedattr (gethash sym compat--file-has-changed-p--hash-table)))
+     (when (not (equal attr cachedattr))
+       (puthash sym attr compat--file-has-changed-p--hash-table))))
+
 (provide 'compat-29)
 ;;; compat-29.el ends here
diff --git a/compat.texi b/compat.texi
index 35a78ca7b5..064533a3f2 100644
--- a/compat.texi
+++ b/compat.texi
@@ -2510,6 +2510,23 @@ non-@code{nil}, it ends in a slash.
 @xref{Directory Names,,,elisp}.
 @end defun
 
+@c copied from lispref/files.texi
+@defun file-has-changed-p file &optional tag
+This function returns non-@code{nil} if the time stamp of
+@var{filename} has changed since the last call.  When called for the
+first time for some @var{filename}, it records the last modification
+time and size of the file, and returns non-@code{nil} when
+@var{filename} exists.  Thereafter, when called for the same
+@var{filename}, it compares the current time stamp and size with the
+recorded ones, and returns non-@code{nil} only if either the time
+stamp or the size (or both) are different.  This is useful when a Lisp
+program wants to re-read a file whenever it changes.  With an optional
+argument @var{tag}, which must be a symbol, the size and modification
+time comparisons are limited to calls with the same tag.
+
+@xref{File Attributes,,,elisp}.
+@end defun
+
 
 
 @subsection Prefixed Definitions



reply via email to

[Prev in Thread] Current Thread [Next in Thread]