From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stephen Berman Newsgroups: gmane.emacs.help Subject: Re: let*: Wrong type argument: stringp, nil Date: Sat, 25 Sep 2021 13:04:59 +0200 Message-ID: <87ee9csyp0.fsf@gmx.net> References: <87lf3lrmos.fsf@gmx.net> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="4481"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) Cc: help-gnu-emacs To: Hongyi Zhao Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Sat Sep 25 13:06:44 2021 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mU5Vz-0000x5-Vn for geh-help-gnu-emacs@m.gmane-mx.org; Sat, 25 Sep 2021 13:06:44 +0200 Original-Received: from localhost ([::1]:41516 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mU5Vy-0007qq-R0 for geh-help-gnu-emacs@m.gmane-mx.org; Sat, 25 Sep 2021 07:06:42 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:38718) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mU5UO-0007oc-Jd for help-gnu-emacs@gnu.org; Sat, 25 Sep 2021 07:05:04 -0400 Original-Received: from mout.gmx.net ([212.227.15.18]:53189) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mU5UM-000395-E9 for help-gnu-emacs@gnu.org; Sat, 25 Sep 2021 07:05:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.net; s=badeba3b8450; t=1632567900; bh=+BGkFidenaG+Srv2QD9snmNH868jZXjJgxgW+ts2cRE=; h=X-UI-Sender-Class:From:To:Cc:Subject:References:Date:In-Reply-To; b=jaYi7oQqsFUyrqkuCdCsCc+0QNijFd2vv9iGa+dALvegxOi59RD6w6AizzSIGiJan AAHSUH+nMo5TXLntn1J6rQofjnYbAuWQC+qbmzitgd52nrwK+P/Td1wqvSTHRJHa9O pxPV9roHyX2WcBQRdCVa9qXfG2wzmMAF99GsNDfY= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Original-Received: from strobelfs ([188.109.144.155]) by mail.gmx.net (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MsHns-1mj9KY0zBi-00tmOj; Sat, 25 Sep 2021 13:05:00 +0200 In-Reply-To: (Hongyi Zhao's message of "Sat, 25 Sep 2021 18:45:28 +0800") X-Provags-ID: V03:K1:h1lTCP83fRyE38bQ/wOEOrZDriemIHK97FUVGlbCgmTHNBOX6WC cBTpOjjcB5Bi8PI4wwWLvZk/Vk0zSssLFCGp73z6Jp2nX8mu0dK0c+ys9FT+ZXg5cGfrxf/ N4ejIji6To9th489ZadRRUgX0nT5FBps3YPnDzqgX8HTvmq1MrTTpjXpE8O+O8C+4NdeONN 0z50aRZty7Nb+07YTIuQw== X-UI-Out-Filterresults: notjunk:1;V03:K0:DvKXp497U/A=:8RXalQ9WnWshOKw8BJT4lp pw+CRZwPkGYEW/lzhqRswHaUYJUV6WCxvtUNshFdGFexatQO0VJ3T9rvcOALEYEJp6GxY47W9 sjT+LHtKitC11AQkjcJPF/Q7iDufrHk953CWnRPx/1z2N1JeKJjQYt6N9LJ1PJcwEgn2BX6sv 4oXFPzPmwazb7ehqi5jH0o+4qFn880u6S5u4sX5QGzvPmG0qK/daWRtMSVkm1mt+A4XO3RUe7 I5W8iMlWLS7RT3q7Yq1Yj7lqNQ6+RwIN4YK6SJ4ER4VS/gKu8ypyZVKVxeetMMnPsRcXitW73 mbQSg+RBgM8ZN2axmX/LFXGQOiF8BQu4uL84mPPq4EoaNkFr/gOl2dk/eRet2ITLcgTtVGXFw o6AGbvQMefMhA1Isq1muLEtIf0BztvIzFydKO0GIeOBvSiUP+eh8yjezHT93pxejKL9X7Z51O pqHMG7+wtnNvwXUuj5N7DyoJuH0FZCmxm4znSpEqvv9xmc5X0v4yrojPVG6BW+40HN9QFYYd+ +TRSGFns7U8+CHvc5PbJdJViYkmKA36QbVp2Y0kIBapbE9DHXkYHVUufzAu6ajbSUYDd1bYCl bqChkjE42dNGRcvxlS4nV1dDAZB6u/fJeZKCwl/yYn6pHnrxPoZ6m3WG2bu0ci0C7X3IZVlg9 w1EMbSbfEXeZ5aptf0o5NWoRzKaJ1eDsRRPogQwx3O4/0NXqwDIWcPB/EimI1u9gTHzj8mBru +J2lHvAtckgx1JxcraYxMzwIcwmd18cNu8LoBndQDVGi2Rn/E7bkgIaLD49F0Cg30Oa2LGA2 Received-SPF: pass client-ip=212.227.15.18; envelope-from=stephen.berman@gmx.net; helo=mout.gmx.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:133235 Archived-At: On Sat, 25 Sep 2021 18:45:28 +0800 Hongyi Zhao wrote: > On Sat, Sep 25, 2021 at 6:09 PM Stephen Berman wrote: >> >> On Fri, 24 Sep 2021 22:52:45 +0800 Hongyi Zhao 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. 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. 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"))) Steve Berman