help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Refer to the directory of the currently being loaded elisp script fr


From: Hongyi Zhao
Subject: Re: Refer to the directory of the currently being loaded elisp script from within the Emacs lisp file itself.
Date: Fri, 11 Jun 2021 22:26:22 +0800

On Fri, Jun 11, 2021 at 9:50 PM Óscar Fuentes <ofv@wanadoo.es> wrote:
>
> Hongyi Zhao <hongyi.zhao@gmail.com> writes:
>
> > It really does the trick. Thank you for pointing this out. BTW, It should 
> > be:
> >
> >  (setq my-directory (file-name-directory load-file-name))
> >
> > What about the following according to the tutorial at
> > <http://ergoemacs.org/emacs/elisp_relative_path.html>?:
> >
> > (setq my-directory (file-name-directory (or load-file-name 
> > buffer-file-name)))
>
> In general, that's incorrect. See the docsstring of buffer-file-name,
> where it is explained that the value of that variable depends on the
> current buffer, it is not related at all to the file that contains the
> Lisp code being executed.
>

Considering that the current execution method is loading the lisp code
for the script file, so it's not applicable to this scenario. But the
original explanation given by By Xah Lee [1] still seems reasonable:

To know the current file's full path, emacs has 2 ways:
`load-file-name' and `buffer-file-name'. If the file is loaded by
`load', then `load-file-name' works but `buffer-file-name' doesn't. If
the file is called by `eval-buffer', then `load-file-name' is nil. You
want to be able to get the current file's full path regardless the
file is run by `load' or interactively by `eval-buffer'."

[1] Refer <http://ergoemacs.org/emacs/elisp_relative_path.html> for
more detailed info.

Regards
-- 
Assoc. Prof. Hongyi Zhao <hongyi.zhao@gmail.com>
Theory and Simulation of Materials
Hebei Vocational University of Technology and Engineering
NO. 552 North Gangtie Road, Xingtai, China



reply via email to

[Prev in Thread] Current Thread [Next in Thread]