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

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

[elpa] externals/compat 4710da5fa7 28/84: Add file-parent-directory from


From: ELPA Syncer
Subject: [elpa] externals/compat 4710da5fa7 28/84: Add file-parent-directory from Emacs 29
Date: Tue, 3 Jan 2023 08:57:33 -0500 (EST)

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

    Add file-parent-directory from Emacs 29
---
 compat-29.el    | 20 ++++++++++++++++++++
 compat-tests.el |  9 +++++++++
 compat.texi     | 12 ++++++++++++
 3 files changed, 41 insertions(+)

diff --git a/compat-29.el b/compat-29.el
index 8af82913ce..9538e99c72 100644
--- a/compat-29.el
+++ b/compat-29.el
@@ -361,5 +361,25 @@ be marked unmodified, effectively ignoring those changes."
                         (equal ,hash (buffer-hash)))
                (restore-buffer-modified-p nil))))))))
 
+;;;; Defined in files.el
+
+(compat-defun file-parent-directory (filename)
+  "Return the directory name of the parent directory of FILENAME.
+If FILENAME is at the root of the filesystem, return nil.
+If FILENAME is relative, it is interpreted to be relative
+to `default-directory', and the result will also be relative."
+  (let* ((expanded-filename (expand-file-name filename))
+         (parent (file-name-directory (directory-file-name 
expanded-filename))))
+    (cond
+     ;; filename is at top-level, therefore no parent
+     ((or (null parent)
+          (file-equal-p parent expanded-filename))
+      nil)
+     ;; filename is relative, return relative parent
+     ((not (file-name-absolute-p filename))
+      (file-relative-name parent))
+     (t
+      parent))))
+
 (provide 'compat-29)
 ;;; compat-29.el ends here
diff --git a/compat-tests.el b/compat-tests.el
index 567be85797..e88092312d 100644
--- a/compat-tests.el
+++ b/compat-tests.el
@@ -2016,6 +2016,15 @@ being compared against."
   (ought t "~root/")
   (ought t "~root/file"))
 
+(compat-deftests file-parent-directory        ;assuming unix
+  (ought nil "/")
+  (ought "/" "/dir")
+  (ought "./" "dir")
+  (ought "file/to/" "file/to/dir")
+  (ought "file/to/" "file/to/dir/")
+  (ought "/file/to/" "/file/to/dir")
+  (ought "/file/to/" "/file/to/dir/"))
+
 (compat-deftests string-equal-ignore-case
   (ought t "" "")
   (ought t "a" "a")
diff --git a/compat.texi b/compat.texi
index e35065883c..35a78ca7b5 100644
--- a/compat.texi
+++ b/compat.texi
@@ -2498,6 +2498,18 @@ etc. -- will not be noticed, and the buffer will still 
be marked
 unmodified, effectively ignoring those changes.
 @end defmac
 
+@c copied from lispref/files.texi
+@defun file-parent-directory filename
+This function returns the directory name of the parent directory of
+@var{filename}.  If @var{filename} is at the root directory of the
+filesystem, it returns @code{nil}.  A relative @var{filename} is
+assumed to be relative to @code{default-directory}, and the return
+value will also be relative in that case.  If the return value is
+non-@code{nil}, it ends in a slash.
+
+@xref{Directory Names,,,elisp}.
+@end defun
+
 
 
 @subsection Prefixed Definitions



reply via email to

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