[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: |
Tue, 28 Sep 2021 11:08:03 +0800 |
On Sat, Sep 25, 2021 at 11:03 PM Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
>
> On Sat, Sep 25, 2021 at 10:45 PM Stephen Berman <stephen.berman@gmx.net>
> wrote:
> >
> > On Sat, 25 Sep 2021 21:49:38 +0800 Hongyi Zhao <hongyi.zhao@gmail.com>
> > wrote:
> >
> > [...]
> > > 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.
> >
> > Doesn't using the built-in Emacs function `locate-dominating-file', as
> > mentioned in the stackoverflow thread, work just as well as
> > `find-file-in-heirarchy'?
>
> According to my current test, it can indeed work very well. But I
> don't know if there are edge cases which this solution doesn't treated
> correctly. The built-in Emacs function `locate-dominating-file' relies
> on several other predefined functions, and It has been tested and
> iterated a lot. So, maybe it's more robust.
Now I'm trying another test code snippet based on the idea posted here [1]:
(defun desperately-pyvenv-workon()
"Traveling up the path, find a `.python-version' and activate the
corresponding virtualenv."
(interactive)
(with-temp-buffer
(unless (equal(getenv "HOME") default-directory)
(while (not (file-exists-p ".python-version"))
(cd "..")
))
(when(file-exists-p ".python-version")
(message(expand-file-name ".python-version")))))
(desperately-pyvenv-workon)
[1] https://emacs.stackexchange.com/a/7477
But when I test the above code in scratch buffer, it seems to be stuck
in an endless loop and running there all the time. Any hints for this
problem?
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, 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, 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 <=
- 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
- Re: let*: Wrong type argument: stringp, nil, Hongyi Zhao, 2021/09/28
- Re: let*: Wrong type argument: stringp, nil, Hongyi Zhao, 2021/09/28
- Re: let*: Wrong type argument: stringp, nil, Hongyi Zhao, 2021/09/28
- Re: let*: Wrong type argument: stringp, nil, Emanuel Berg, 2021/09/28