all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: daanturo <daanturo@gmail.com>
To: 56355@debbugs.gnu.org
Subject: bug#56355: 29.0.50; Implement file-parent-directory
Date: Sat, 2 Jul 2022 18:06:01 +0700	[thread overview]
Message-ID: <0f199ed7-abef-d74c-b5fd-c9c81978c143@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 211 bytes --]

A way to get the parent directory of a file.

This is inspired by f.el's f-dirname, but only returns the absolute
path, also doesn't ignore `file-name-handler-alist`, doesn't strip the
final "/".

-- 
Daanturo.

[-- Attachment #2: 0001-Define-file-parent-directory.patch --]
[-- Type: text/x-patch, Size: 2969 bytes --]

From 69e7863d95fca8fb979b0182560f02b243edf172 Mon Sep 17 00:00:00 2001
From: Daanturo <daanturo@gmail.com>
Date: Sat, 2 Jul 2022 17:55:57 +0700
Subject: [PATCH] Define file-parent-directory

Get parent directory of a file.
* doc/lispref/files.texi: Document the function.
* etc/NEWS: Add its entry.
* lisp/emacs-lisp/shortdoc.el: Add it to 'file-name' group.
* lisp/files.el: implementation
---
 doc/lispref/files.texi      |  6 ++++++
 etc/NEWS                    |  3 +++
 lisp/emacs-lisp/shortdoc.el |  4 ++++
 lisp/files.el               | 10 ++++++++++
 4 files changed, 23 insertions(+)

diff --git a/doc/lispref/files.texi b/doc/lispref/files.texi
index ea8683a6d8..e1182dc191 100644
--- a/doc/lispref/files.texi
+++ b/doc/lispref/files.texi
@@ -2445,6 +2445,12 @@ Directory Names
 because it recognizes abbreviations even as part of the name.
 @end defun
 
+@defun file-parent-directory filename
+This function returns the parent directory of @var{filename}.  If
+@var{filename} is at the top-level: return nil.  @var{filename} can be
+relative to `default-directory'.
+@end defun
+
 @node File Name Expansion
 @subsection Functions that Expand Filenames
 @cindex expansion of file names
diff --git a/etc/NEWS b/etc/NEWS
index 30404cc13c..3bfebd6bb7 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -317,6 +317,9 @@ startup.  Previously, these functions ignored
 \f
 * Changes in Emacs 29.1
 
+** New function 'file-parent-directory'
+Get the parent directory of a file.
+
 ** New config variable 'syntax-wholeline-max' to reduce the cost of long lines.
 This variable is used by some operations (mostly syntax-propertization
 and font-locking) to treat lines longer than this variable as if they
diff --git a/lisp/emacs-lisp/shortdoc.el b/lisp/emacs-lisp/shortdoc.el
index c82aa3365c..68500c43d3 100644
--- a/lisp/emacs-lisp/shortdoc.el
+++ b/lisp/emacs-lisp/shortdoc.el
@@ -353,6 +353,10 @@ file-name
   (abbreviate-file-name
    :no-eval (abbreviate-file-name "/home/some-user")
    :eg-result "~some-user")
+  (file-parent-directory
+   :eval (file-parent-directory "~")
+   :eval (file-parent-directory "foo")
+   :eval (file-parent-directory "/"))
   "Quoted File Names"
   (file-name-quote
    :args (name)
diff --git a/lisp/files.el b/lisp/files.el
index 1295c24c93..289d92a9dc 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -5145,6 +5145,16 @@ file-name-split
           (setq filename nil))))
     components))
 
+(defun file-parent-directory (filename)
+  "Return the parent directory of FILENAME.
+If FILENAME is at the top-level: return nil.  FILENAME can be
+relative to `default-directory'."
+  (let* ((parent (file-name-directory
+                  (directory-file-name (expand-file-name filename)))))
+    (if (file-equal-p parent filename)
+        nil
+      parent)))
+
 (defcustom make-backup-file-name-function
   #'make-backup-file-name--default-function
   "A function that `make-backup-file-name' uses to create backup file names.
-- 
2.37.0


             reply	other threads:[~2022-07-02 11:06 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-02 11:06 daanturo [this message]
2022-07-02 11:54 ` bug#56355: 29.0.50; Implement file-parent-directory Eli Zaretskii
2022-07-02 12:02   ` daanturo
2022-07-02 12:11   ` Lars Ingebrigtsen
2022-07-02 15:29     ` Drew Adams
2022-07-02 18:24       ` daanturo
2022-07-04 11:08         ` Lars Ingebrigtsen
2022-08-31 10:26 ` daanturo
2022-09-01 10:08   ` Lars Ingebrigtsen
2022-09-01  4:11 ` daanturo
2022-09-04 14:38   ` Michael Albinus

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0f199ed7-abef-d74c-b5fd-c9c81978c143@gmail.com \
    --to=daanturo@gmail.com \
    --cc=56355@debbugs.gnu.org \
    /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 external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.