[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: let*: Wrong type argument: stringp, nil
From: |
Hongyi Zhao |
Subject: |
Re: let*: Wrong type argument: stringp, nil |
Date: |
Sat, 25 Sep 2021 18:45:28 +0800 |
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?
HZ
- 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 <=
- 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, 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