This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-07-18
Channels
- # announcements (8)
- # asami (10)
- # babashka (58)
- # beginners (20)
- # calva (4)
- # clerk (9)
- # clj-on-windows (5)
- # cljsrn (1)
- # clojure (12)
- # clojure-australia (2)
- # clojure-europe (24)
- # clojure-gamedev (1)
- # clojure-norway (14)
- # clojure-romania (1)
- # clojurescript (28)
- # conjure (2)
- # cursive (5)
- # dev-tooling (38)
- # emacs (15)
- # events (1)
- # hoplon (12)
- # hyperfiddle (41)
- # introduce-yourself (4)
- # jobs (2)
- # jobs-discuss (17)
- # malli (23)
- # matrix (5)
- # nextjournal (11)
- # pathom (19)
- # pedestal (1)
- # polylith (3)
- # reitit (3)
- # sci (7)
- # xtdb (7)
Hi! I want a function to get the name of the current folder. If I’m editing /home/teodorlu/myproject/myfile.clj
, I want "myproject"
.
I wrote a function which seems to work (below), but I suspect that there’s something built in that does this already. Any pointers?
(defun directory-containing-current-file ()
(car (last (s-split "/" (replace-regexp-in-string "/$" "" default-directory)))))
(thread-last
"/home/teodorlu/myproject/myfile.clj"
f-dirname
f-split
cadddr)
Depends on f
but pretty much every package includes it so you should have itIf I want this to work for arbitrary deep paths, should I replace cadddr
with last car
?
(thread-last
"/home/teodorlu/a/b/c/myproject/myfile.clj"
f-dirname
f-split
last
car)
Great — thanks! Didn’t know about f.el
or thread-last
, those are going to be useful 🙌
The latter is built-in since 28.1 in subr-x
which is full of other useful macros and the former is depended on by so many packages it's not unreasonable to expect it to exist
hey this exists 😛
(file-name-parent-directory FILENAME)
Return the directory name of the parent directory of FILENAME.
If FILENAME is at the root of the filesystem, return nil.
If FILENAME is relative, it is interpreted to be relative
to 'default-directory', and the result will also be relative.
(file-name-parent-directory "/home/teodorlu/myproject/myfile.clj")
"/home/teodorlu/myproject/"(file-name-base (directory-file-name (file-name-parent-directory "/home/teodorlu/myproject/myfile.clj"))) "myproject"
Only adding this (from my config) because it surprised me that there was even another way of doing the same thing:
(f-base (file-name-directory buffer-file-name))
This is how I have defined my dotemacs' root dir.
(defvar dotemacs-dir
(file-name-directory (or load-file-name (buffer-file-name)))
"The dotemacs' root. Normally it should be ~/.emacs.d.")
One thing that tripped me up is that directory names must be terminated correctly (forward slash on linux). To do this I have been using file-name-as-directory
:
(defvar dotemacs-savefile-dir
(file-name-as-directory (expand-file-name "savefile" dotemacs-dir))
"This folder stores all the automatically generated save/history-files.")