On 3/15/22 07:47, Jean Louis wrote: > * hw [2022-03-15 08:22]: >> Yes, and how do I make it so that a buffer not visiting a file is >> being auto-saved without delay once the auto-save-mode has been >> enabled? Or can I make it auto-save once before enabling auto-save >> mode? > > Buffer not visiting file does not know where it should be saved. I am > using and doing something similar like you, I use buffers without file > names as additional scratch buffers. If Emacs session is over, those > temporary buffers are over. And I like sometimes to invoke them in > different mode, such as `sql-mode' or `emacs-lisp-mode'. Because they > are not connected to files, they can't be saved so easily. > > (defvar rcd-temp-buffer-mode-history nil) > (defvar rcd-temp-file-directory "~/tmp/") > (defvar rcd-temp-buffer-name "RCD TEMPORARY BUFFER") > (defvar rcd-temp-buffer-modes '(("adoc-mode" . "adoc") > ("emacs-lisp-mode" . "el") > ("lisp-mode" . ".lisp") > ("markdown-mode" . ".md") > ("org-mode" . "org") > ("sql-mode" . "sql") > ("fundamental-mode" . "txt") > ("html-mode" . "html"))) > > > (defun rcd-temp-buffer (&optional name mode) > "Generate new temporary buffer." > (interactive "p") > (let* ((format (concat "*" rcd-temp-buffer-name "%s%s*")) > (buffer (if name (format format ": " name) (format format "" "")))) > (switch-to-buffer (generate-new-buffer buffer)) > (if current-prefix-arg > (let* ((mode (completing-read > "Mode: " > (map-keys rcd-temp-buffer-modes) nil t nil > 'rcd-temp-buffer-mode-history))) > (funcall (intern mode))) > (funcall (intern (or mode "fundamental-mode")))))) > > and that function could be adopted to provide file names for buffers: > > (defun rcd-temp-buffer (&optional name mode) > "Generate new temporary buffer." > (interactive "p") > (let* ((format (concat "*" rcd-temp-buffer-name "%s%s*")) > (buffer (if name (format format ": " name) (format format "" ""))) > (file-name (concat rcd-temp-file-directory (format-time-string "%Y-%m-%d-%H:%M:%S.txt")))) > (switch-to-buffer (generate-new-buffer buffer)) > (setq buffer-file-name file-name) > (if current-prefix-arg > (let* ((mode (completing-read > "Mode: " > (map-keys rcd-temp-buffer-modes) nil t nil > 'rcd-temp-buffer-mode-history))) > (funcall (intern mode))) > (funcall (intern (or mode "fundamental-mode")))))) > > Now if I have `do-auto-save' those temporary buffers would be saved I guess. Thank you very much! With this, it's not at all necessary to tie the buffers to files. The do-auto-save function saves "all buffers that need it", and does it "right now", without delay when called. That's perfect for what I'm doing.