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: Tue, 28 Sep 2021 11:08:03 +0800 Message-ID: References: <87lf3lrmos.fsf@gmx.net> <87ee9csyp0.fsf@gmx.net> <87a6k0sogz.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="33943"; 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 Tue Sep 28 05:09:19 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 1mV3Ud-0008fV-2z for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 28 Sep 2021 05:09:19 +0200 Original-Received: from localhost ([::1]:33934 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mV3Ub-0006wT-4B for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 27 Sep 2021 23:09:17 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:38498) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mV3Tf-0006wL-Us for help-gnu-emacs@gnu.org; Mon, 27 Sep 2021 23:08:19 -0400 Original-Received: from mail-vs1-xe32.google.com ([2607:f8b0:4864:20::e32]:45019) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mV3Tb-0003ll-UP for help-gnu-emacs@gnu.org; Mon, 27 Sep 2021 23:08:19 -0400 Original-Received: by mail-vs1-xe32.google.com with SMTP id 66so16503468vsd.11 for ; Mon, 27 Sep 2021 20:08:15 -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=L8b7kpX7Nq+As3v9nOM/pNU3EcrS8nZl3KEUCTr5+CI=; b=koZxu+i3Q0eIaic7eZ6wC6h8dyHnnRUo7b7Ww3UYLQEnZoLCBJmDzJ3HP3C0YHnfZe atgUyOyEETkLk4pZZhd84xtv7JZyqSGO+gYgW0qwx1/RLTTW35K+TA/EjuBJwWJxOBm9 KkHjiYjF13n/GICLo9p6SLGjFf6uL+at7QERZnOf/pTi58LWJ3WXYTOtdFC7G0Bzriq4 SBLBEju7hqguph2p6cng406TfvVZ0/7yZfleGOcWfJS0qkIt/24rpqcFbErZjiiHPGI6 Y/k6HRLYbRAwDeqJxXFHJdPJIV8Itjf6ju8IyCmxVrLSDxW+mtvM+GlqZpcDu10bC+Jx xARA== 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=L8b7kpX7Nq+As3v9nOM/pNU3EcrS8nZl3KEUCTr5+CI=; b=4pd0PoYScSRmY0W4cQ9GF/unP72XtzyWyZngalPJxT2CfXmK9K+HAvMe8Ef+8COOos ZzAe5qZ94qvBnjSrs7qWpaUPm5UAM4XdkY4zbJXtHEaj5ifpR7Z4OUtEB4ZKupLqcJAs msmBll3fLg6/ZoKbVOxgaaXRH20P8415jV/wJYXrG/8BxPs/WZ4wBPKbVScjdRRrkOkB Im6Y9aW/zeEWwH0qxTQl8mgAF6tu1lEtlch7vhWx+EgERNJGhB5rvHwDrEmMeGwjra8P CKFtU3q1UHUVra3dLqVFrJVeIpBAEkO/i3OxHp0sx5h4LTxujoOaRIzeZf7WN1mfPCqb RIeQ== X-Gm-Message-State: AOAM530AFO1ThqVmBWp55eDAc0pZK4xaKhZVQ/JvxRpPFwOaQ3FxKKdi 83R4Ae9Ixp75Mw1w0l46bKtJOr2131IgC1YQRPtc+54FQYfQ1jPn X-Google-Smtp-Source: ABdhPJzdA2arUpAQHahQ5ZPIgCPTSlJawpG93D9worR5nOfl7+SEUDHwTgJ3IetidXRZZEquooK62uctPFaTkPSTHp8= X-Received: by 2002:a05:6102:5f4:: with SMTP id w20mr3466640vsf.55.1632798494660; Mon, 27 Sep 2021 20:08:14 -0700 (PDT) In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::e32; envelope-from=hongyi.zhao@gmail.com; helo=mail-vs1-xe32.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:133317 Archived-At: On Sat, Sep 25, 2021 at 11:03 PM Hongyi Zhao wrote: > > On Sat, Sep 25, 2021 at 10:45 PM Stephen Berman wrote: > > > > On Sat, 25 Sep 2021 21:49:38 +0800 Hongyi Zhao 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