Subject: RE: [External] : bug#58364: [PATCH] Add new function 'file-name-parent-p'

From: Drew Adams <[email-masked]>
Date: Fri, 7 Oct 2022 21:10:35 +0000

> Unless I am mistaken, there is no direct function
> to quickly test if a directory is a super-directory
> of file.  As I have encountered this issue more
> than once the last few days of hacking, I would
> like to propose the below function.

Parent is not the same as super-directory
(or "eventual parent", as your doc says).

A parent is an immediate direct ancestor.
It's not just _any_ ancestor.

"Ancestor" includes parent, grandparent,...
-- any level.  I guess it's what you
meant by "parent" and "super-directory".
___

FWIW:

I use this, for a _parent_ directory.
(I've likely proposed this to Emacs before.)

The name is because it's defined in dired+.el,
but the function isn't specific to Dired.

(defun diredp-parent-dir (file &optional relativep)
  "Return the parent directory of FILE, or nil if none.
Optional arg RELATIVEP non-nil means return a relative name, that is,
just the parent component."
  (let ((parent  (file-name-directory
                   (directory-file-name
                     (expand-file-name file))))
        relparent)
    (when relativep
      (setq relparent  (file-name-nondirectory
                         (directory-file-name parent))))
    (and (not (equal parent file))
         (or relparent  parent))))

IOW, in a nutshell, this is a parent dir of FILE:

(file-name-directory
  (directory-file-name (expand-file-name FILE)))

FWIW2:

I use this to get a list of all ancestors of DIR.

(defun diredp-ancestor-dirs (dir)
  "Return a list of the ancestor directories of directory DIR."
  (mapcar #'file-name-as-directory
          (diredp-maplist
             (lambda (dd)
               (mapconcat #'identity (reverse dd) "/"))
             (cdr (nreverse (split-string dir "/" t))))))

(You can use `cl-maplist'.  I use `diredp-maplist'
because dired+.el needs compatibility with old Emacs
versions.)