I have the function now working to my satisfaction, as below, but am open to suggestions if I'm not doing things optimally. In the solution below, I use function read-file-name
to prompt the user, and use condition-case
to trap a user response of C-g
, the quit
signal, so that the default buffer file name is set for the buffer.
(defun my-write-test ()
(interactive)
(let*
((name "foo.txt")
(dir "/home/me/")
(buf (create-file-buffer name)))
(pop-to-buffer buf)
(insert "random text\n")
(condition-case nil
(progn ()
(set-visited-file-name
(expand-file-name
(read-file-name "Save to: " dir nil nil name)))
(save-buffer))
(quit (set-visited-file-name name))))))