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: Set the default-directory dynamically according to the buffer-file-name. Date: Wed, 29 Sep 2021 22:31:20 +0800 Message-ID: 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="21545"; mail-complaints-to="usenet@ciao.gmane.io" To: help-gnu-emacs Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Wed Sep 29 16:33:01 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 1mVadp-0005T7-4l for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 29 Sep 2021 16:33:01 +0200 Original-Received: from localhost ([::1]:56570 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mVadn-00055u-V8 for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 29 Sep 2021 10:32:59 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:33446) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mVacV-00055B-RJ for help-gnu-emacs@gnu.org; Wed, 29 Sep 2021 10:31:39 -0400 Original-Received: from mail-ua1-x936.google.com ([2607:f8b0:4864:20::936]:38502) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mVacP-0005o5-0U for help-gnu-emacs@gnu.org; Wed, 29 Sep 2021 10:31:39 -0400 Original-Received: by mail-ua1-x936.google.com with SMTP id b15so1710176uaj.5 for ; Wed, 29 Sep 2021 07:31:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:from:date:message-id:subject:to; bh=NwCb9LqWJUihxx9qv4faCPSSgJdd/dyGBS61na7r5BA=; b=JNHpkm7UrY2t0+CAy20TyMJabwQ8aSJq5wVrB+QtYBwjXSXZyOc3biGx/ouYwvuxWz qfV1Qf95m+W+O1Rp1PLeOgmi+hUH4jD9+QEZKV7QkJ746n0K7Z4slK5YPmGKqV7z0+f9 Hx/RFlxwlQyvpfEb4DDOWK1EGc6UQjI2e6kF4IQSGgYsDyOdEu4rayaTsVsPn/4yXsze TlYCgrWlaTAYxeOp1nK9JnOlGzcm+srnPQ2zLOFiaEQQzEcVW5SEbHB3ngebaSIazliX CxFe8tzai2KgDZl6GhVU0MeRl1Lqg8SnmkzWTE7DptWdJwNLdYLt+Be681rNwJtXbz/s 7yMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=NwCb9LqWJUihxx9qv4faCPSSgJdd/dyGBS61na7r5BA=; b=mtMqjWLvf8yTS3hqgC20r1ebvA7HdyfiUKj2ydXwx+KSuN33PRKA5Y8crnKGqSu1Rw 7uD0qJavP1Iwrp18EwgYTJuqYVam2o66eDuEIwTQlo1QlNYvtRlioVJyUZeiO4LBN2e5 9uoAjoSnFtXjm78HwNfM1JK21Rmj09p3e2jIf3WvJrLIsT8TEQNS7pUp0vTlkIhwWpi1 eDHq2rtySWvJu0yCnw4DAx5fq/TsISn6zMDefUjJcS+e3vTiEVBNIbyw+Qxfxe9xF0Lx 5qEREsKpAIFyUZwhAwZqiPDkVKpQbKhNV+fRPtoiS7v6vYS1zbdMIYrfPN71WSCdIs1p fq2Q== X-Gm-Message-State: AOAM533WUivPy2x9YqIhJEmBLalA/mXStdtzhw3efvMBNwU7YV1GmrJo pCfCQIbEfFFu5Jgl8G9oJzEfP5BBMnR/g+QI+Cj/HfVVSTerSIrH X-Google-Smtp-Source: ABdhPJyyyvDcXdtAiUFVQcrgM2bTfR/A25o7wVd13IWBV69VKVuH5P2fDkzBQwma/EhmnoO7ffcrmhIV20xsHv03QrE= X-Received: by 2002:ab0:53c8:: with SMTP id l8mr421058uaa.26.1632925891409; Wed, 29 Sep 2021 07:31:31 -0700 (PDT) Received-SPF: pass client-ip=2607:f8b0:4864:20::936; envelope-from=hongyi.zhao@gmail.com; helo=mail-ua1-x936.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, T_SPF_TEMPERROR=0.01 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:133392 Archived-At: 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. Regards -- Assoc. Prof. Hongyi Zhao Theory and Simulation of Materials Hebei Vocational University of Technology and Engineering No. 473, Quannan West Street, Xindu District, Xingtai, Hebei province