From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Tassilo Horn Newsgroups: gmane.emacs.help Subject: Re: Set the default-directory dynamically according to the buffer-file-name. Date: Wed, 29 Sep 2021 17:31:26 +0200 Message-ID: <87bl4b2y70.fsf@gnu.org> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="34870"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.7.0; emacs 28.0.50 Cc: help-gnu-emacs@gnu.org To: Hongyi Zhao Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Wed Sep 29 17:35:53 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 1mVbcf-0008yN-Rh for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 29 Sep 2021 17:35:53 +0200 Original-Received: from localhost ([::1]:41660 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mVbce-0006di-Nk for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 29 Sep 2021 11:35:52 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:50424) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mVbax-0005bn-8C for help-gnu-emacs@gnu.org; Wed, 29 Sep 2021 11:34:07 -0400 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]:33200) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mVbav-0007iR-Lw; Wed, 29 Sep 2021 11:34:06 -0400 Original-Received: from auth2-smtp.messagingengine.com ([66.111.4.228]:53859) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mVbap-0000B6-36; Wed, 29 Sep 2021 11:34:01 -0400 Original-Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailauth.nyi.internal (Postfix) with ESMTP id 430C627C0054; Wed, 29 Sep 2021 11:33:57 -0400 (EDT) Original-Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Wed, 29 Sep 2021 11:33:57 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvtddrudekvddgkeelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfhgfhffvufffjgfkgggtsehttdertddtredtnecuhfhrohhmpefvrghsshhi lhhoucfjohhrnhcuoehtshguhhesghhnuhdrohhrgheqnecuggftrfgrthhtvghrnhepve evieekteekveeigfefffeivdetgeduvdffueeuudevgedttdehvdfhueevfffhnecuvehl uhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepthhhohhrnhdomh gvshhmthhprghuthhhphgvrhhsohhnrghlihhthidqkeeijeefkeejkeegqdeifeehvdel kedqthhsughhpeepghhnuhdrohhrghesfhgrshhtmhgrihhlrdhfmh X-ME-Proxy: Original-Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 29 Sep 2021 11:33:56 -0400 (EDT) In-reply-to: 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:133395 Archived-At: Hongyi Zhao writes: > I wrote the following function to set the default-directory > dynamically according to the buffer-file-name: > > (defun my/default-directory () > (let ((file (buffer-file-name))) > (if (not file) > (message "no file name here.") > (setq default-directory (file-name-directory (buffer-file-name)))))) > > Then I apply this function to specific hooks, say, python-mode-hook. > This will make it easier for me to invoke files using the relative > path of each project. I want to know if this is a good practice. Isn't that already the case that file-visiting buffers have (file-name-directory (buffer-file-name)) as `default-directory'? Bye, Tassilo