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 23:03:20 +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="34082"; 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 17:44:37 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 1mU9qv-0008gs-Bz for geh-help-gnu-emacs@m.gmane-mx.org; Sat, 25 Sep 2021 17:44:37 +0200 Original-Received: from localhost ([::1]:59664 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mU9qu-0001nC-63 for geh-help-gnu-emacs@m.gmane-mx.org; Sat, 25 Sep 2021 11:44:36 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:43996) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mU9DF-0002pf-D4 for help-gnu-emacs@gnu.org; Sat, 25 Sep 2021 11:03:37 -0400 Original-Received: from mail-lf1-x12e.google.com ([2a00:1450:4864:20::12e]:44677) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mU9DC-000658-PO for help-gnu-emacs@gnu.org; Sat, 25 Sep 2021 11:03:37 -0400 Original-Received: by mail-lf1-x12e.google.com with SMTP id y26so14360403lfa.11 for ; Sat, 25 Sep 2021 08:03:33 -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=Tt7q7ClaeS6HPrk6pwTUue906faHIFEpUWuuDtOx/Wc=; b=l9HfljU5x8riNrv5nyIYTewvtYfNFgGrcOIGyiA6PNqk8uDLi8wa+yu9ZOqo+mRFCK bSyb0n6802L1rLq9YcmXaJAcJD97YSdU111We8EIxzhzVuoqT1UBhS/YHkL78oETdlqs 0+kcYfAKzE6PgO/YQyyE9JhcAn40tyf3r2aIcoYENb7nn+/baTvcjXfubEH/uyKZAbZL M0oDGePycbMOIUSw7UdmgcoZz9LxhNzrZzc0H8GaCUV2YSv2JyE+Jg0p1arBZnmL1rUi QXg+mlA1t7Zt+rWZvxGJRHBKXyPPyLfkeFMKVztFyzvdzH1K4BUdLQ3+DmfB5ojLMsmQ XB3A== 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=Tt7q7ClaeS6HPrk6pwTUue906faHIFEpUWuuDtOx/Wc=; b=LOFrPuqp1c0bBdGX1vVfCdU58s0VqQY2AUint27+X0vthQaroUfc4m+Nt85vzMywoY 8CBM6HY/6YSbFlISeD9/ROj9hvTZSgO6m2W2uRZa6fgPR7Mh/ZB3pzu2pxqfUVXu5T+V CGC7F67ngEHMxpRxKA/6QMtMOTA7d5SxYu7+E2bCxV9yQZQJ68l9CD29gP8AtyU/DLf1 adWo3+Fo0uxSDi2ttjnkkbocFBYxkRVhC8/71bUQRoHxFSu35xon7g4QRfrR+s5acUzC oS3Kb908vWHFal39mSy9ZBn2PalX5n/etK+wARF3pZsuLyWz2XjMvlLcvyNtV8SQvJQR HRug== X-Gm-Message-State: AOAM530/2sogpnazKs9XI64XEYwxYCz3CVZnz35QhPm6Hnm6LkrKR8WX rGU/tqA/oZGfWjTmsSZJQfRr4JOIfRLOfubVBYk= X-Google-Smtp-Source: ABdhPJwtD6fiDv5Zx8btosajyrbi2dLfLiSZG9F4rzq5lNz9WugPBfnV4zWIO/7S/A96xokBRBiexSEsOfzMWAn9AnY= X-Received: by 2002:a05:651c:1181:: with SMTP id w1mr11453206ljo.424.1632582211674; Sat, 25 Sep 2021 08:03:31 -0700 (PDT) In-Reply-To: <87a6k0sogz.fsf@gmx.net> Received-SPF: pass client-ip=2a00:1450:4864:20::12e; envelope-from=hongyi.zhao@gmail.com; helo=mail-lf1-x12e.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:133239 Archived-At: 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. HZ