unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Tassilo Horn <tsdh@gnu.org>
To: Hongyi Zhao <hongyi.zhao@gmail.com>
Cc: help-gnu-emacs@gnu.org
Subject: Re: Set the default-directory dynamically according to the buffer-file-name.
Date: Wed, 29 Sep 2021 17:31:26 +0200	[thread overview]
Message-ID: <87bl4b2y70.fsf@gnu.org> (raw)
In-Reply-To: <CAGP6POLJ6qFegRByXLYAD78_aZA+Zg=FkT0OeFFoPBMP1c-jrw@mail.gmail.com>

Hongyi Zhao <hongyi.zhao@gmail.com> 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



  parent reply	other threads:[~2021-09-29 15:31 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-29 14:31 Set the default-directory dynamically according to the buffer-file-name Hongyi Zhao
2021-09-29 14:50 ` Hongyi Zhao
2021-09-29 15:31 ` Tassilo Horn [this message]
2021-09-30  1:08   ` Hongyi Zhao
2021-09-30  3:37     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-30  7:00       ` Hongyi Zhao
2021-09-30  7:39         ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-30  8:54           ` Hongyi Zhao
2021-09-30  9:58             ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-29 15:47 ` [External] : " Drew Adams
2021-10-01  4:41   ` Hongyi Zhao
2021-10-01  7:09     ` Emanuel Berg via Users list for the GNU Emacs text editor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87bl4b2y70.fsf@gnu.org \
    --to=tsdh@gnu.org \
    --cc=help-gnu-emacs@gnu.org \
    --cc=hongyi.zhao@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).