From: Hongyi Zhao <hongyi.zhao@gmail.com>
To: Stephen Berman <stephen.berman@gmx.net>
Cc: help-gnu-emacs <help-gnu-emacs@gnu.org>
Subject: Re: let*: Wrong type argument: stringp, nil
Date: Tue, 28 Sep 2021 11:08:03 +0800 [thread overview]
Message-ID: <CAGP6POLQB1q-++zGwDB7g8vYgwWMdFmKcHWbLE_9HTX7Ao=aKw@mail.gmail.com> (raw)
In-Reply-To: <CAGP6POJRpXgcqzdPuCjYWz6ZR_wwrhNA-SS+FkWxaY0MZiQBUA@mail.gmail.com>
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
next prev parent reply other threads:[~2021-09-28 3:08 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-24 14:52 let*: Wrong type argument: stringp, nil Hongyi Zhao
2021-09-24 14:57 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-25 1:29 ` Hongyi Zhao
2021-09-25 23:42 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-25 10:09 ` Stephen Berman
2021-09-25 10:45 ` Hongyi Zhao
2021-09-25 11:04 ` Stephen Berman
2021-09-25 13:49 ` Hongyi Zhao
2021-09-25 14:45 ` Stephen Berman
2021-09-25 15:03 ` Hongyi Zhao
2021-09-28 3:08 ` Hongyi Zhao [this message]
2021-09-28 3:21 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-28 3:24 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-28 5:25 ` Hongyi Zhao
2021-09-28 7:20 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-28 8:02 ` Yuri Khan
2021-09-28 8:24 ` Hongyi Zhao
2021-09-29 1:43 ` Hongyi Zhao
2021-09-29 2:03 ` Hongyi Zhao
2021-09-29 3:02 ` Hongyi Zhao
2021-09-29 3:57 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-29 6:15 ` Hongyi Zhao
2021-09-29 6:29 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-29 7:37 ` Hongyi Zhao
2021-09-29 8:21 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-29 9:02 ` Hongyi Zhao
2021-09-29 9:14 ` Yuri Khan
2021-09-29 10:29 ` Hongyi Zhao
2021-09-29 12:29 ` Yuri Khan
2021-09-29 12:49 ` Hongyi Zhao
2021-10-08 3:36 ` Hongyi Zhao
2021-10-10 14:05 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-30 3:38 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-29 4:35 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-29 6:17 ` Hongyi Zhao
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAGP6POLQB1q-++zGwDB7g8vYgwWMdFmKcHWbLE_9HTX7Ao=aKw@mail.gmail.com' \
--to=hongyi.zhao@gmail.com \
--cc=help-gnu-emacs@gnu.org \
--cc=stephen.berman@gmx.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.