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: Stephen Berman
Subject: Re: let*: Wrong type argument: stringp, nil
Date: Sat, 25 Sep 2021 16:45:48 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

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'?

Steve Berman



reply via email to

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