Fungsi yang mengembalikan jalur absolut direktori induk

10
    (parent-dir "/home/sk/parent/ab.el")

memberi

    "/home/sk/parent"

kemudian,

(parent-dir "/home/sk/parent")

memberi

"/home/sk"

Adakah fungsi elisp parent-diryang memberikan basepath untuk path file & path dir?

Madhavan
sumber

Jawaban:

14

file-name-directory melakukan apa yang Anda minta.

Namun, (file-name-directory "/home/sk/parent/")(dengan trailing slash) mengembalikan direktori yang sama, bukan induknya.

Ini melakukan apa aku menebak Anda benar-benar ingin:

(file-name-directory (directory-file-name "/home/sk/parent/ab.el")) ; => "/home/sk/parent/"
(file-name-directory (directory-file-name "/home/sk/parent/")) ; => "/home/sk/"
(file-name-directory (directory-file-name "/home/sk/parent")) ; => "/home/sk/"

Dan library Dired + menggunakan fungsi ini:

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