Subject: Re: bug#77797: 31.0.50; Visit an empty file on creating it

From: Eshel Yaron <[email-masked]>
Date: Mon, 14 Apr 2025 18:59:11 +0200

Hi,

Eli Zaretskii <[email-masked]> writes:

>> From: Stephen Berman <[email-masked]>
>> Cc: [email-masked]
>> Date: Mon, 14 Apr 2025 17:14:03 +0200
>> 
>> Since you don't necessarily object, what's the next step?
>
> I'd like to hear more opinions about this, since I think it takes that
> command in directions we didn't intend it to go.

FWIW, I use C-x C-f to create new files all the time, also in
non-existing directories.  Then I save the buffer as usual, and that
prompts me to create the directory if needed, which I find to be fine.
So I probably wouldn't find much use for this addition.  I wouldn't mind
if it was added nonetheless, of course.

BTW if you want to suppress the prompt for creating a new directory and
always just create it when needed, you can (ab)use write-file-functions
for that:

--8<---------------cut here---------------start------------->8---
(defun my-ensure-dir ()
  (let ((dir (file-name-directory (buffer-file-name))))
    (unless (file-directory-p dir)
      (make-directory dir t)))
  nil)

(add-hook 'write-file-functions #'my-ensure-dir)
--8<---------------cut here---------------end--------------->8---

Best regards,

Eshel