From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Hongyi Zhao Newsgroups: gmane.emacs.help Subject: Re: let*: Wrong type argument: stringp, nil Date: Sat, 25 Sep 2021 21:49:38 +0800 Message-ID: References: <87lf3lrmos.fsf@gmx.net> <87ee9csyp0.fsf@gmx.net> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="29276"; mail-complaints-to="usenet@ciao.gmane.io" Cc: help-gnu-emacs To: Stephen Berman Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Sat Sep 25 15:50:49 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 1mU84m-0007SV-Hc for geh-help-gnu-emacs@m.gmane-mx.org; Sat, 25 Sep 2021 15:50:48 +0200 Original-Received: from localhost ([::1]:46364 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mU84l-0005us-54 for geh-help-gnu-emacs@m.gmane-mx.org; Sat, 25 Sep 2021 09:50:47 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:34660) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mU83w-0005uF-Am for help-gnu-emacs@gnu.org; Sat, 25 Sep 2021 09:49:56 -0400 Original-Received: from mail-lf1-x129.google.com ([2a00:1450:4864:20::129]:35466) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mU83t-0004df-0q for help-gnu-emacs@gnu.org; Sat, 25 Sep 2021 09:49:55 -0400 Original-Received: by mail-lf1-x129.google.com with SMTP id m3so53428202lfu.2 for ; Sat, 25 Sep 2021 06:49:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=0KgggYaLm0bkNBFL4H8QzFD1BUjZYaPuH07eSM3Xgno=; b=Iv9E5Aq4vqjOpwiHpb+4Jj5/EAb9g+FSXBOcI7fseRn5d3FnIQaFU3iux1LhcASwkh opFcM3dQcp9sIi2eV+JWuyHBH68WqCodOPkhzdNhPCrUXP2xvgxosUEcqGLp6/LTfXEa umSMNulfaTIiI+M+pGmjGiyIRzvBmhqtrGtd1DTcKS+7leZREHfm8x4iflNjUzxeGLuG nGNhwRwCQ/Z30w45zeGH3A1yu1pUwqUePXzwPkELikikvBKK6NgXVEOwiUa756WOlx0Y oYvnqYkPQiglHepNL/ZPO634ApKV0rl/wvN+/5jM5v4W4VeVxOKdxO46l1GxcR/IV/fC 2Hvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=0KgggYaLm0bkNBFL4H8QzFD1BUjZYaPuH07eSM3Xgno=; b=pTm/gWkKcAivwk6gCJ2fUeYrqobN8P9Qxf3LWuQciNzcBV2jG1p9fgJ8Tc3DD5dgeV HllN/vC1TElNti5ZHo3dsTrOARy4QJmgLbw66NP/ANVgZ2FpQCvpVNBy8dWiQcKEmm2H EpCxgHoIKNp+JWTaq6xfY91l+k6PHiMpLmk5f78nVhIDtbHl1g+jY4LBwALoB7O9hrjT AFRo43nTH2TMa6DftlZ1OJxj9SJrtYdlX0679ewecv05x1m1yY4X2UFVMfALp3yz4xlI H6GWLAo6vQaZ9kaD4RhAd5VYAXb8HU+7SNvLThMZZZ3H0nvdzn2pbZ3Y5PlJWFTAGzcj PNag== X-Gm-Message-State: AOAM530WN/dkAISoXK0q5dP7R+Obcgn8sQ82/M0p2nGMCzK1wWVQorvf gVExg07QjrBaL5+whnJcCbGHPob2dIHOt510TIw= X-Google-Smtp-Source: ABdhPJzAQSFMNRWTAnl5KjvfLODqk0xIg8m3iVpUYEcwq9o4hBWDdezYf+nDZt17JsgOTon8woHMtWR4UG625yPirCA= X-Received: by 2002:ac2:59c6:: with SMTP id x6mr15028406lfn.298.1632577790057; Sat, 25 Sep 2021 06:49:50 -0700 (PDT) In-Reply-To: <87ee9csyp0.fsf@gmx.net> Received-SPF: pass client-ip=2a00:1450:4864:20::129; envelope-from=hongyi.zhao@gmail.com; helo=mail-lf1-x129.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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_NONE=-0.0001, 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:133237 Archived-At: On Sat, Sep 25, 2021 at 7:05 PM Stephen Berman wrote: > > 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. 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