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

[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 21:49:38 +0800

On Sat, Sep 25, 2021 at 7:05 PM Stephen Berman <stephen.berman@gmx.net> wrote:
>
> 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.

But the actual demand exceeds the simple processing above, I mean, the
real project maybe located under a specific folder with multiple level
subdirectories. In this case, when I open a python file, the function
should do the following: Search the current directory and its parent
directories at all levels in turn. Once the ".python-version" file is
found, activate the corresponding environment and exit the cycle;
Otherwise, continue to search upwards until the  certain top level
directory, say, $HOME. Based on the above requirements and the code
snippets given here [1-2], I finally figured out the following
solution:

-------
(defun parent-directory (dir)
  (unless (equal (getenv "HOME") dir)
    ;file-truename or directory-file-name
    (file-name-directory (directory-file-name dir))))


(defun find-file-in-heirarchy (current-dir fname)
  "Search for a file named FNAME upwards through the directory
hierarchy, starting from CURRENT-DIR"
  (let ((file (concat current-dir fname))
        (parent (parent-directory (expand-file-name current-dir))))
    (if (file-exists-p file)
        file
      (when parent
        (find-file-in-heirarchy parent fname)))))


(defun try/pyvenv-workon ()
  (when (buffer-file-name)
      (let ((file (find-file-in-heirarchy (buffer-file-name)
".python-version")))
        (when (file-exists-p file)
          (pyvenv-workon (with-temp-buffer
                           (insert-file-contents file)
                             (nth 0 (split-string
(buffer-string)))))))))

(add-hook 'python-mode-hook 'try/pyvenv-workon)
-------

It works like a charm. But any suggestions/enhancements/comments will
be greatly appreciated.

[1] https://stackoverflow.com/a/14096693  (*)
[2] http://sodaware.sdf.org/notes/emacs-lisp-find-file-upwards/

* Indicated that this is my main reference source 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.

Good advice.

> 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")))

Good advice, again.

HZ



reply via email to

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