Skip to content

Instantly share code, notes, and snippets.

@kayomarz
Created September 12, 2023 06:23
Show Gist options
  • Save kayomarz/90070ec67eebe07ef58831df90c44811 to your computer and use it in GitHub Desktop.
Save kayomarz/90070ec67eebe07ef58831df90c44811 to your computer and use it in GitHub Desktop.
pathname-as-dir - return a `pathname` directory
;; `(pathname "/foo/bar")` uses the trailing directory separator to decide if a
;; path refers to a file or directory
;; (pathname "/home/foo.bar" ) => #P"/home/foo.bar"
;; (pathname "/home/foo.bar/") => #P"/home/foo.bar/"
;; Instead, this function definition `pathname-as-dir` results in a pathname
;; which is a always a directory irrespective of the trailing slash contained in
;; the string argument.
;; (pathname-as-dir "/foo/foo.bar") => #P"/foo/foo.bar/"
;; (pathname-as-dir "/foo/foo.bar/") => #P"/foo/foo.bar/"
;; Instead of using this snippet, you should use `cl-fad:pathname-as-directory`
;; which is part of the robust `cl-fad` library.
;; This below snippet was useful where `cl-fad` could not be loaded in an early
;; setup stage (although there might be a way to do it)
(defun pathname-as-dir (path)
(let* ((p (pathname path))
(dir (or (pathname-directory p) (list :relative)))
(type (pathname-type p))
(suffix (when type (concatenate 'string "." type)))
(name (concatenate 'string (pathname-name p) suffix)))
(make-pathname :directory
(if name (append dir (list name)) dir)
:name nil
:type nil
:defaults p)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment