[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: let*: Wrong type argument: stringp, nil
From: |
Stephen Berman |
Subject: |
Re: let*: Wrong type argument: stringp, nil |
Date: |
Sat, 25 Sep 2021 13:04:59 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) |
On Sat, 25 Sep 2021 18:45:28 +0800 Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
> On Sat, Sep 25, 2021 at 6:09 PM Stephen Berman <stephen.berman@gmx.net> wrote:
>>
>> On Fri, 24 Sep 2021 22:52:45 +0800 Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
>>
>> > According to the idea represented here [1], I wrote the following
>> > function to activate the python virtual environment automatically
>> > using the content of the ".python-version" file located in the project
>> > directory of the current opened python file:
>> >
>> > (defun pyvenv-autoload ()
>> > (require 'projectile)
>> > (let* ((pdir (file-name-directory buffer-file-name)) (pfile
>> > (concat pdir ".python-version")))
>> > (if (file-exists-p pfile)
>> > (pyvenv-workon (with-temp-buffer
>> > (insert-file-contents pfile)
>> > (nth 0 (split-string (buffer-string))))))))
>> > (add-hook 'python-mode-hook 'pyvenv-autoload)
>> >
>> > It seems the corresponding python virtual environment can be activated
>> > automatically when I open the python file in Emacs. But when I try to
>> > evaluate the file, the following message will be triggered, and defeat
>> > the evaluate operation:
>> >
>> > let*: Wrong type argument: stringp, nil
>> >
>> > [1]
>> > https://github.com/jorgenschaefer/pyvenv/issues/51#issuecomment-253902450
>> >
>> > Any hints for fixing this problem?
>>
>> Evidently buffer-file-name is nil when the sexp `(file-name-directory
>> buffer-file-name)' is evaluated, which indicates the current buffer when
>> that function is called is not visiting a file (e.g., you get the same
>> error when you evaluate `(file-name-directory buffer-file-name)' in
>> *scratch*).
>
> Thank you for pointing this out. Based on your above comment, I fixed
> the problem with the code below, and it does the trick:
>
> (defun pyvenv-autoload ()
> (require 'projectile)
> (if (buffer-file-name)
> (let* ((pdir (file-name-directory buffer-file-name)) (pfile
> (concat pdir ".python-version")))
> (if (file-exists-p pfile)
> (pyvenv-workon (with-temp-buffer
> (insert-file-contents pfile)
> (nth 0 (split-string (buffer-string)))))))))
> (add-hook 'python-mode-hook 'pyvenv-autoload)
>
> BTW, can I use `when' to write the above code?
Certainly, indeed `when' is typically favored over `if' when there's no
'else' branch, as in the above code. On the other hand, since the point
of the function is activate a virtual environment on visiting a Python
file, you might want to add an 'else' branch that returns a suitable
message when the current buffer is not visiting a file. Without that,
in such a case the function just returns nil, so there's no indication
the activation failed.
BTW, your code would be easier to read if you began the second clause of
the let* on a new line:
(let* ((pdir (file-name-directory buffer-file-name))
(pfile (concat pdir ".python-version")))
Steve Berman
- let*: Wrong type argument: stringp, nil, Hongyi Zhao, 2021/09/24
- Re: let*: Wrong type argument: stringp, nil, Emanuel Berg, 2021/09/24
- Re: let*: Wrong type argument: stringp, nil, Stephen Berman, 2021/09/25
- Re: let*: Wrong type argument: stringp, nil, Hongyi Zhao, 2021/09/25
- Re: let*: Wrong type argument: stringp, nil,
Stephen Berman <=
- Re: let*: Wrong type argument: stringp, nil, Hongyi Zhao, 2021/09/25
- Re: let*: Wrong type argument: stringp, nil, Stephen Berman, 2021/09/25
- Re: let*: Wrong type argument: stringp, nil, Hongyi Zhao, 2021/09/25
- Re: let*: Wrong type argument: stringp, nil, Hongyi Zhao, 2021/09/27
- Re: let*: Wrong type argument: stringp, nil, Emanuel Berg, 2021/09/27
- Re: let*: Wrong type argument: stringp, nil, Emanuel Berg, 2021/09/27
- Re: let*: Wrong type argument: stringp, nil, Hongyi Zhao, 2021/09/28
- Re: let*: Wrong type argument: stringp, nil, Emanuel Berg, 2021/09/28
- Re: let*: Wrong type argument: stringp, nil, Yuri Khan, 2021/09/28
- Re: let*: Wrong type argument: stringp, nil, Hongyi Zhao, 2021/09/28