GNU bug report logs - #58364
[PATCH] Add new function 'file-name-parent-p'

Previous Next

Package: emacs;

Reported by: Philip Kaludercic <philipk <at> posteo.net>

Date: Fri, 7 Oct 2022 20:24:01 UTC

Severity: normal

Tags: patch

Done: Philip Kaludercic <philipk <at> posteo.net>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: Drew Adams <drew.adams <at> oracle.com>
To: Philip Kaludercic <philipk <at> posteo.net>, "58364 <at> debbugs.gnu.org" <58364 <at> debbugs.gnu.org>
Subject: bug#58364: [PATCH] Add new function 'file-name-parent-p'
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.)




This bug report was last modified 2 years and 223 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.