From: Drew Adams <drew.adams@oracle.com>
To: Philip Kaludercic <philipk@posteo.net>,
"58364@debbugs.gnu.org" <58364@debbugs.gnu.org>
Subject: bug#58364: [PATCH] Add new function 'file-name-parent-p'
Date: Fri, 7 Oct 2022 21:10:35 +0000 [thread overview]
Message-ID: <SJ0PR10MB54881D9B3756A5144BE90E6BF35F9@SJ0PR10MB5488.namprd10.prod.outlook.com> (raw)
In-Reply-To: <87h70f758g.fsf@posteo.net>
> 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.)
next prev parent reply other threads:[~2022-10-07 21:10 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-07 20:23 bug#58364: [PATCH] Add new function 'file-name-parent-p' Philip Kaludercic
[not found] ` <handler.58364.B.16651742163752.ack@debbugs.gnu.org>
2022-10-07 20:46 ` bug#58364: Acknowledgement ([PATCH] Add new function 'file-name-parent-p') Philip Kaludercic
2022-10-07 21:10 ` Drew Adams [this message]
2022-10-08 6:17 ` bug#58364: [PATCH] Add new function 'file-name-parent-p' Eli Zaretskii
2022-10-08 7:21 ` Philip Kaludercic
2022-10-08 8:01 ` Eli Zaretskii
2022-10-08 9:23 ` Philip Kaludercic
2022-10-08 10:10 ` Eli Zaretskii
2022-10-08 10:29 ` Philip Kaludercic
2022-10-08 11:16 ` Eli Zaretskii
2022-10-08 17:20 ` Drew Adams
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=SJ0PR10MB54881D9B3756A5144BE90E6BF35F9@SJ0PR10MB5488.namprd10.prod.outlook.com \
--to=drew.adams@oracle.com \
--cc=58364@debbugs.gnu.org \
--cc=philipk@posteo.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).