2

I remember reading about this function (it's part of Emacs) somewhere, but I can't for the life of me remember what it's called.

It takes a single argument arg (not sure if there's any more optional args)

  • By default it looks for (expand-file-name arg "~/") and returns the path if it exists.
  • If not, it looks for (expand-file-name arg user-emacs-directory) and returns the path if it exists.
  • Otherwise, it creates the subdirectory in user-emacs-directory and returns the path.

1 Answer 1

3

You are looking for locate-user-emacs-file.


Check out the below example code that,

  • Creates abc/ sub-directory under user-emacs-directory if ~/abc/ doesn't already exist.
  • Always returns the full directory path, whether it got created or not.
  • Displays a message in the event the directory did not get created for some reason.
(let ((dir (locate-user-emacs-file "abc/")))
  (make-directory dir :parents)
  (when (not (file-directory-p dir))
    (message "%s directory was not created." dir))
  dir) 
7
  • I can expand more on this answer when I get to a computer tomorrow. Oct 15, 2015 at 3:12
  • Indeed I am. Looking forward to your elaboration.
    – PythonNut
    Oct 15, 2015 at 3:13
  • btw this function will not create any sub-directories. The only directory it will create is user-emacs-directory if the OLD-NAME file does not exist. Oct 15, 2015 at 3:18
  • Oh, that's a shame. That's the reason I was looking for it in the first place.
    – PythonNut
    Oct 15, 2015 at 3:20
  • I couldn't try it now but it might be as simple as wrapping this function with make-directory with the second argument PARENTS set to t. Oct 15, 2015 at 3:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.