* Refer to the directory of the currently being loaded elisp script from within the Emacs lisp file itself.
@ 2021-06-10 15:02 Hongyi Zhao
2021-06-10 15:24 ` Óscar Fuentes
2021-06-10 15:31 ` Eli Zaretskii
0 siblings, 2 replies; 12+ messages in thread
From: Hongyi Zhao @ 2021-06-10 15:02 UTC (permalink / raw)
To: help-gnu-emacs
I'm writing an elisp script which will load another file in the same
directory as this elisp script itself. The currently used command is
as follows:
(load-file (expand-file-name
"~/Public/repo/github.com/DvdMgr/screen2latex.el.git/auth.el.gpg"))
I want to construct the loaded file's path with the directory of the
elisp script itself, and I tried with the following but failed to do
the trick:
(load-file (concat (file-name-directory load-file-name) "/auth.el.gpg"))
Any hints/comments/suggestions for this problem?
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
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Refer to the directory of the currently being loaded elisp script from within the Emacs lisp file itself.
2021-06-10 15:02 Refer to the directory of the currently being loaded elisp script from within the Emacs lisp file itself Hongyi Zhao
@ 2021-06-10 15:24 ` Óscar Fuentes
2021-06-10 15:31 ` Hongyi Zhao
2021-06-10 15:31 ` Eli Zaretskii
1 sibling, 1 reply; 12+ messages in thread
From: Óscar Fuentes @ 2021-06-10 15:24 UTC (permalink / raw)
To: help-gnu-emacs
Hongyi Zhao <hongyi.zhao@gmail.com> writes:
> I'm writing an elisp script which will load another file in the same
> directory as this elisp script itself. The currently used command is
> as follows:
>
> (load-file (expand-file-name
> "~/Public/repo/github.com/DvdMgr/screen2latex.el.git/auth.el.gpg"))
>
> I want to construct the loaded file's path with the directory of the
> elisp script itself, and I tried with the following but failed to do
> the trick:
>
> (load-file (concat (file-name-directory load-file-name) "/auth.el.gpg"))
>
> Any hints/comments/suggestions for this problem?
How do you know that it failed? Do you know for sure that the problem is
related to the line of code shown above? Is auth.el.gpg a valid Elisp
file?
BTW, the correct method for concatenating a directory and a file name is
(load-file
(concat
(file-name-as-directory (file-name-directory load-file-name))
"auth.el.gpg"))
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Refer to the directory of the currently being loaded elisp script from within the Emacs lisp file itself.
2021-06-10 15:24 ` Óscar Fuentes
@ 2021-06-10 15:31 ` Hongyi Zhao
2021-06-10 15:40 ` Óscar Fuentes
0 siblings, 1 reply; 12+ messages in thread
From: Hongyi Zhao @ 2021-06-10 15:31 UTC (permalink / raw)
To: Óscar Fuentes; +Cc: help-gnu-emacs
On Thu, Jun 10, 2021 at 11:24 PM Óscar Fuentes <ofv@wanadoo.es> wrote:
>
> Hongyi Zhao <hongyi.zhao@gmail.com> writes:
>
> > I'm writing an elisp script which will load another file in the same
> > directory as this elisp script itself. The currently used command is
> > as follows:
> >
> > (load-file (expand-file-name
> > "~/Public/repo/github.com/DvdMgr/screen2latex.el.git/auth.el.gpg"))
> >
> > I want to construct the loaded file's path with the directory of the
> > elisp script itself, and I tried with the following but failed to do
> > the trick:
> >
> > (load-file (concat (file-name-directory load-file-name) "/auth.el.gpg"))
> >
> > Any hints/comments/suggestions for this problem?
>
> How do you know that it failed? Do you know for sure that the problem is
> related to the line of code shown above? Is auth.el.gpg a valid Elisp
> file?
Yes.
>
> BTW, the correct method for concatenating a directory and a file name is
>
> (load-file
> (concat
> (file-name-as-directory (file-name-directory load-file-name))
> "auth.el.gpg"))
I tried you above code, it still doesn't work with the following info:
file-name-as-directory: Wrong type argument: stringp, nil [2 times]
Mark set
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
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Refer to the directory of the currently being loaded elisp script from within the Emacs lisp file itself.
2021-06-10 15:02 Refer to the directory of the currently being loaded elisp script from within the Emacs lisp file itself Hongyi Zhao
2021-06-10 15:24 ` Óscar Fuentes
@ 2021-06-10 15:31 ` Eli Zaretskii
2021-06-10 15:38 ` Hongyi Zhao
1 sibling, 1 reply; 12+ messages in thread
From: Eli Zaretskii @ 2021-06-10 15:31 UTC (permalink / raw)
To: help-gnu-emacs
> From: Hongyi Zhao <hongyi.zhao@gmail.com>
> Date: Thu, 10 Jun 2021 23:02:04 +0800
>
> (load-file (concat (file-name-directory load-file-name) "/auth.el.gpg"))
Instead of 'concat', use 'expand-file-name'.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Refer to the directory of the currently being loaded elisp script from within the Emacs lisp file itself.
2021-06-10 15:31 ` Eli Zaretskii
@ 2021-06-10 15:38 ` Hongyi Zhao
2021-06-10 15:42 ` Óscar Fuentes
0 siblings, 1 reply; 12+ messages in thread
From: Hongyi Zhao @ 2021-06-10 15:38 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: help-gnu-emacs
expand-file-name: Wrong type argument: stringp, nil
On Thu, Jun 10, 2021 at 11:34 PM Eli Zaretskii <eliz@gnu.org> wrote:
>
> > From: Hongyi Zhao <hongyi.zhao@gmail.com>
> > Date: Thu, 10 Jun 2021 23:02:04 +0800
> >
> > (load-file (concat (file-name-directory load-file-name) "/auth.el.gpg"))
>
> Instead of 'concat', use 'expand-file-name'.
I tried with the following:
(load-file (expand-file-name (file-name-directory load-file-name)
"/auth.el.gpg"))
But meet the same error as the suggested method by Óscar Fuentes:
expand-file-name: Wrong type argument: stringp, nil
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
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Refer to the directory of the currently being loaded elisp script from within the Emacs lisp file itself.
2021-06-10 15:31 ` Hongyi Zhao
@ 2021-06-10 15:40 ` Óscar Fuentes
2021-06-10 23:55 ` Hongyi Zhao
0 siblings, 1 reply; 12+ messages in thread
From: Óscar Fuentes @ 2021-06-10 15:40 UTC (permalink / raw)
To: help-gnu-emacs
Hongyi Zhao <hongyi.zhao@gmail.com> writes:
>> BTW, the correct method for concatenating a directory and a file name is
>>
>> (load-file
>> (concat
>> (file-name-as-directory (file-name-directory load-file-name))
>> "auth.el.gpg"))
>
> I tried you above code, it still doesn't work with the following info:
>
> file-name-as-directory: Wrong type argument: stringp, nil [2 times]
> Mark set
load-file-name is defined while the file is being loaded, after that it
is nil. So if you tried with `C-x C-e' or some other interactive command
it wont work.
Once the code above is in the correct file, save it, start Emacs and
report back what happens.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Refer to the directory of the currently being loaded elisp script from within the Emacs lisp file itself.
2021-06-10 15:38 ` Hongyi Zhao
@ 2021-06-10 15:42 ` Óscar Fuentes
0 siblings, 0 replies; 12+ messages in thread
From: Óscar Fuentes @ 2021-06-10 15:42 UTC (permalink / raw)
To: help-gnu-emacs
Hongyi Zhao <hongyi.zhao@gmail.com> writes:
> expand-file-name: Wrong type argument: stringp, nil
>
> On Thu, Jun 10, 2021 at 11:34 PM Eli Zaretskii <eliz@gnu.org> wrote:
>>
>> > From: Hongyi Zhao <hongyi.zhao@gmail.com>
>> > Date: Thu, 10 Jun 2021 23:02:04 +0800
>> >
>> > (load-file (concat (file-name-directory load-file-name) "/auth.el.gpg"))
>>
>> Instead of 'concat', use 'expand-file-name'.
>
> I tried with the following:
>
> (load-file (expand-file-name (file-name-directory load-file-name)
> "/auth.el.gpg"))
>
> But meet the same error as the suggested method by Óscar Fuentes:
>
> expand-file-name: Wrong type argument: stringp, nil
Eli is suggesting this:
(load-file (expand-file-name "auth.el.gpg"
(file-name-directory load-file-name)))
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Refer to the directory of the currently being loaded elisp script from within the Emacs lisp file itself.
2021-06-10 15:40 ` Óscar Fuentes
@ 2021-06-10 23:55 ` Hongyi Zhao
2021-06-11 1:14 ` Óscar Fuentes
0 siblings, 1 reply; 12+ messages in thread
From: Hongyi Zhao @ 2021-06-10 23:55 UTC (permalink / raw)
To: Óscar Fuentes; +Cc: help-gnu-emacs
On Thu, Jun 10, 2021 at 11:40 PM Óscar Fuentes <ofv@wanadoo.es> wrote:
>
> Hongyi Zhao <hongyi.zhao@gmail.com> writes:
>
> >> BTW, the correct method for concatenating a directory and a file name is
> >>
> >> (load-file
> >> (concat
> >> (file-name-as-directory (file-name-directory load-file-name))
> >> "auth.el.gpg"))
> >
> > I tried you above code, it still doesn't work with the following info:
> >
> > file-name-as-directory: Wrong type argument: stringp, nil [2 times]
> > Mark set
>
> load-file-name is defined while the file is being loaded, after that it
> is nil. So if you tried with `C-x C-e' or some other interactive command
> it wont work.
>
> Once the code above is in the correct file, save it, start Emacs and
> report back what happens.
See my current usage on
<https://github.com/hongyi-zhao/screen2latex.el/blob/bd820184a29158c14de1afaf564f9e2898dd88ac/screen2latex.el#L45>
according to the instructions here.
After I start Emacs and tried with `M-x screenlatex RET', I will see
the following info in *Messages* buffer:
expand-file-name: Wrong type argument: stringp, nil
The expected behavior is that with the above command, the encrypted
token file, i.e., "auth.el.gpg" will be loaded and the decrypted text
will be inserted in the buffer. Finally, the screen capture action
will be triggered, as happens when I use the following absolute path
shown at <https://github.com/hongyi-zhao/screen2latex.el/blob/bd820184a29158c14de1afaf564f9e2898dd88ac/screen2latex.el#L43>:
(load-file (expand-file-name
"~/Public/repo/github.com/hongyi-zhao/screen2latex.el.git/auth.el.gpg"))
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
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Refer to the directory of the currently being loaded elisp script from within the Emacs lisp file itself.
2021-06-10 23:55 ` Hongyi Zhao
@ 2021-06-11 1:14 ` Óscar Fuentes
2021-06-11 3:47 ` Hongyi Zhao
0 siblings, 1 reply; 12+ messages in thread
From: Óscar Fuentes @ 2021-06-11 1:14 UTC (permalink / raw)
To: help-gnu-emacs
Hongyi Zhao <hongyi.zhao@gmail.com> writes:
> On Thu, Jun 10, 2021 at 11:40 PM Óscar Fuentes <ofv@wanadoo.es> wrote:
>>
>> Hongyi Zhao <hongyi.zhao@gmail.com> writes:
>>
>> >> BTW, the correct method for concatenating a directory and a file name is
>> >>
>> >> (load-file
>> >> (concat
>> >> (file-name-as-directory (file-name-directory load-file-name))
>> >> "auth.el.gpg"))
>> >
>> > I tried you above code, it still doesn't work with the following info:
>> >
>> > file-name-as-directory: Wrong type argument: stringp, nil [2 times]
>> > Mark set
>>
>> load-file-name is defined while the file is being loaded, after that it
>> is nil. So if you tried with `C-x C-e' or some other interactive command
>> it wont work.
>>
>> Once the code above is in the correct file, save it, start Emacs and
>> report back what happens.
>
> See my current usage on
> <https://github.com/hongyi-zhao/screen2latex.el/blob/bd820184a29158c14de1afaf564f9e2898dd88ac/screen2latex.el#L45>
> according to the instructions here.
>
> After I start Emacs and tried with `M-x screenlatex RET', I will see
> the following info in *Messages* buffer:
>
> expand-file-name: Wrong type argument: stringp, nil
As explained previously, load-file-name is only defined when the file is
being loaded, otherwise it is nil.
You can put the directory name on a variable at load time and use that
variable on your command:
(setq my-directory (file-name-directory load-file-name)
(defun screen2latex ()
...
(load-file (expand-file-name "auth.el.gpg" my-directory))
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Refer to the directory of the currently being loaded elisp script from within the Emacs lisp file itself.
2021-06-11 1:14 ` Óscar Fuentes
@ 2021-06-11 3:47 ` Hongyi Zhao
2021-06-11 13:49 ` Óscar Fuentes
0 siblings, 1 reply; 12+ messages in thread
From: Hongyi Zhao @ 2021-06-11 3:47 UTC (permalink / raw)
To: Óscar Fuentes; +Cc: help-gnu-emacs
On Fri, Jun 11, 2021 at 9:15 AM Óscar Fuentes <ofv@wanadoo.es> wrote:
>
> Hongyi Zhao <hongyi.zhao@gmail.com> writes:
>
> > On Thu, Jun 10, 2021 at 11:40 PM Óscar Fuentes <ofv@wanadoo.es> wrote:
> >>
> >> Hongyi Zhao <hongyi.zhao@gmail.com> writes:
> >>
> >> >> BTW, the correct method for concatenating a directory and a file name is
> >> >>
> >> >> (load-file
> >> >> (concat
> >> >> (file-name-as-directory (file-name-directory load-file-name))
> >> >> "auth.el.gpg"))
> >> >
> >> > I tried you above code, it still doesn't work with the following info:
> >> >
> >> > file-name-as-directory: Wrong type argument: stringp, nil [2 times]
> >> > Mark set
> >>
> >> load-file-name is defined while the file is being loaded, after that it
> >> is nil. So if you tried with `C-x C-e' or some other interactive command
> >> it wont work.
> >>
> >> Once the code above is in the correct file, save it, start Emacs and
> >> report back what happens.
> >
> > See my current usage on
> > <https://github.com/hongyi-zhao/screen2latex.el/blob/bd820184a29158c14de1afaf564f9e2898dd88ac/screen2latex.el#L45>
> > according to the instructions here.
> >
> > After I start Emacs and tried with `M-x screenlatex RET', I will see
> > the following info in *Messages* buffer:
> >
> > expand-file-name: Wrong type argument: stringp, nil
>
> As explained previously, load-file-name is only defined when the file is
> being loaded, otherwise it is nil.
>
> You can put the directory name on a variable at load time and use that
> variable on your command:
>
> (setq my-directory (file-name-directory load-file-name)
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)))
>
> (defun screen2latex ()
> ...
> (load-file (expand-file-name "auth.el.gpg" my-directory))
>
>
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
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Refer to the directory of the currently being loaded elisp script from within the Emacs lisp file itself.
2021-06-11 3:47 ` Hongyi Zhao
@ 2021-06-11 13:49 ` Óscar Fuentes
2021-06-11 14:26 ` Hongyi Zhao
0 siblings, 1 reply; 12+ messages in thread
From: Óscar Fuentes @ 2021-06-11 13:49 UTC (permalink / raw)
To: help-gnu-emacs
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.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: Refer to the directory of the currently being loaded elisp script from within the Emacs lisp file itself.
2021-06-11 13:49 ` Óscar Fuentes
@ 2021-06-11 14:26 ` Hongyi Zhao
0 siblings, 0 replies; 12+ messages in thread
From: Hongyi Zhao @ 2021-06-11 14:26 UTC (permalink / raw)
To: Óscar Fuentes; +Cc: help-gnu-emacs
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
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2021-06-11 14:26 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-06-10 15:02 Refer to the directory of the currently being loaded elisp script from within the Emacs lisp file itself Hongyi Zhao
2021-06-10 15:24 ` Óscar Fuentes
2021-06-10 15:31 ` Hongyi Zhao
2021-06-10 15:40 ` Óscar Fuentes
2021-06-10 23:55 ` Hongyi Zhao
2021-06-11 1:14 ` Óscar Fuentes
2021-06-11 3:47 ` Hongyi Zhao
2021-06-11 13:49 ` Óscar Fuentes
2021-06-11 14:26 ` Hongyi Zhao
2021-06-10 15:31 ` Eli Zaretskii
2021-06-10 15:38 ` Hongyi Zhao
2021-06-10 15:42 ` Óscar Fuentes
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).