bug-gnu-emacs
[Top][All Lists]
Advanced

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

bug#56355: 29.0.50; Implement file-parent-directory


From: Drew Adams
Subject: bug#56355: 29.0.50; Implement file-parent-directory
Date: Sat, 2 Jul 2022 15:29:18 +0000

> > Thanks, but it looks as a very thin wrapper around
> > file-name-directory, so I wonder whether we really 
> > need a separate function fir this.

Need?  How easy is it for a user to find and figure
out that a combination of `file-name-directory',
`directory-file-name', and `expand-file-name' can
give you the parent directory name?

Maybe your "we" is focused mainly on Emacs developers?
Such a function is helpful for users more generally,
even if not for those with greater familiarity with
the available set of functions.

And yes, how to do this has been asked multiple times
by users.  Here's one such:

https://emacs.stackexchange.com/q/9554/105

> I think it looks like a handy utility function -- we have quite a few
> instances of (file-name-directory (directory-file-name filename))
> 
> (possibly with an expand-file-name in there, too) and using
> `file-parent-directory' would both express the intention of the code
> better, and be less error prone (because many people forget the
> `directory-file-name' in the first attempt).

I proposed it long, long ago.  This version, from
Dired+, allows an optional argument to return the
relative name, i.e., just the parent component.

(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))))





reply via email to

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