help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to force auto-save of buffers not visiting files, right now?


From: Jean Louis
Subject: Re: how to force auto-save of buffers not visiting files, right now?
Date: Tue, 15 Mar 2022 09:47:37 +0300
User-agent: Mutt/2.2.0 (2022-02-12)

* hw <hw@adminart.net> [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.

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]