0

I'm writing a package with several different files, in which the placement of the root directory is important. Since I don't know where other users will put the package, I thought I'd be clever and reference the location of the root directory using a custom whose default is the parent directory of (the parent directory of the parent directory of) the file in which the file housing the customs is located which would be found out as the file corresponding to the buffer reading the file, so

(defcustom daselt-directory (file-name-parent-directory (file-name-parent-directory (file-name-directory (buffer-file-name)))) "This should be the directory in which Daselt is housed. Please include the trailing backslash." :group 'Daselt :type 'string) 

And when I'm reading in the file using require while Emacs is already running, this seems to work alright, but when I'm putting the same require defun into my init, I get

Debugger entered--Lisp error: (wrong-type-argument stringp nil) file-name-directory(nil) (file-name-parent-directory (file-name-directory (buffer-file-name))) 

So apparently, during startup buffer-file-name doesn't return anything, I guess because the file isn't really read in using a buffer. But isn't there a way I can find out the path of the file require is reading while it's reading it during startup?

3
  • This question is likely a duplicate - please search and delete, if so. You can't count on which buffer might be current at a particular point during startup. That's your problem here. Commented Oct 21, 2024 at 18:31
  • @Drew I've looked at all questions tagged "start-up" and either "buffers", "files" or "filenames" but didn't find any similar question. Is there also no way to find out where require is reading its code from? Commented Oct 21, 2024 at 19:23
  • 1
    The question in your comment, about finding out what file actually is loaded, when (and by implication from where), is a different question. The answer to that (one answer, at least), is to scan the value of variable load-history. Commented Oct 21, 2024 at 20:56

1 Answer 1

0

Thanks to Drew's mention of load-history, I found a way that seems to work: searching through the cars of that variable and taking a matching string. More precisely, I did this:

(defcustom daselt-directory (condition-case nil (file-name-parent-directory (file-name-parent-directory (file-name-directory (buffer-file-name)))) (error (cl-loop for loaded in load-history for loadpath = (car loaded) if (string-match "/Daselt/" loadpath) do (cl-return (substring loadpath 0 (match-end 0)))))) "This should be the directory in which Daselt is housed. Please include the trailing backslash. Per default, this directory is set to the parent directory of the parent directory of the parent directory of the parent directory of the buffer corresponding to the file containing the custom, but during startup, this returns nil, so it is instead looked up by going through LOAD-HISTORY and looking for matches for \"Daselt\", so normally it should be set right automatically." :group 'Daselt :type 'string) 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.