unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Philipp Stephani <p.stephani2@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 25478@debbugs.gnu.org
Subject: bug#25478: 26.0.50; No hooks are called when auto-saving
Date: Sat, 22 Apr 2017 19:01:50 +0000	[thread overview]
Message-ID: <CAArVCkQ-MS-mWtf89320Th0FsuHu2VOPvt9h1Sho4j_MEo43mg@mail.gmail.com> (raw)
In-Reply-To: <CAArVCkRgPKjBstxarVKD9DYHcHTetsxeXs3c5vk4iiuBDibXgg@mail.gmail.com>


[-- Attachment #1.1: Type: text/plain, Size: 2450 bytes --]

Philipp Stephani <p.stephani2@gmail.com> schrieb am So., 26. März 2017 um
21:17 Uhr:

> Eli Zaretskii <eliz@gnu.org> schrieb am So., 26. März 2017 um 20:46 Uhr:
>
>> > From: Philipp Stephani <p.stephani2@gmail.com>
>> > Date: Sun, 26 Mar 2017 18:21:01 +0000
>> > Cc: 25478@debbugs.gnu.org
>> >
>> >  All in all, I think this option is subtly dangerous and should be
>> >  either deprecated or completely reimplemented to invoke
>> >  basic-save-buffer instead. I'm quite sure this option made sense in
>> >  the past, when complications with encoding and save-related hooks
>> >  didn't exist, but that is no longer the case in Emacs these days.
>> >  If someone thinks using this option will allow them not to bother with
>> >  saving their edits, they are in for an unpleasant surprise.
>> >
>> > That's quite unfortunate, as not having to bother with saving edits
>> manually is a very desirable feature that I
>> > think Emacs should offer out of the box.
>> >
>> > If you really want to deprecate auto-save-visited-file-name, how about
>> adding a simple global minor mode that
>> > invokes save-some-buffers from an idle timer to files.el?
>>
>> As I wrote, I'm also okay with Someone™ volunteering to reimplement
>> this option such that it invokes basic-save-buffer instead.
>>
>> A minor mode that you describe would also be fine, but then it should
>> probably disable auto-saving if auto-save-visited-file-name is set,
>> right?
>>
>
> Sounds reasonable. I'll try to come up with something by next weekend.
> Probably it will be just
>
> (defvar auto-save--timer nil)
> (defcustom auto-save-visited-interval 5
>   "seconds"
>   :group 'files
>   :type 'number
>   :set (lambda (symbol value)
>          (set-default symbol value)
>          (when auto-save--timer
>            (timer-set-idle-time auto-save--timer value :repeat))))
> (define-minor-mode auto-save-visited-mode nil
>   :group 'files
>   :global t
>   (when auto-save--timer (kill-timer auto-save--timer))
>   (setq auto-save--timer
>         (when auto-save-visited-mode
>           (run-with-idle-timer
>            auto-save-visited-interval :repeat
>            #'save-some-buffers :no-prompt
>            (lambda ()
>              (not (and buffer-auto-save-file-name
> auto-save-visited-file-name)))))))
>
> but it will also need tests and documentation, which take a bit more time.
>

Here's a patch.

[-- Attachment #1.2: Type: text/html, Size: 4548 bytes --]

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Reimplement-auto-saving-to-visited-files.txt --]
[-- Type: text/plain; charset=US-ASCII;  name="0001-Reimplement-auto-saving-to-visited-files.txt", Size: 5425 bytes --]

From 2a14636a0610d1bc6e024a56b788a575c039f784 Mon Sep 17 00:00:00 2001
From: Philipp Stephani <phst@google.com>
Date: Sat, 22 Apr 2017 20:58:38 +0200
Subject: [PATCH] Reimplement auto-saving to visited files

This reacts to confusing behavior of 'auto-save-visited-file-name',
cf. Bug#25478.

* lisp/files.el (auto-save-visited-interval): New customization option.
(auto-save-visited-mode): New global minor mode.
(auto-save-visited-file-name): Make obsolete.
(auto-save--timer): New internal helper variable.

* doc/emacs/files.texi (Auto Save Files): Document
'auto-save-visited-mode' instead of obsolete
'auto-save-visited-file-name'.
(Auto Save Control): Document customization option
'auto-save-visited-interval'.
---
 doc/emacs/files.texi | 20 +++++++++++++-------
 etc/NEWS             |  6 ++++++
 lisp/files.el        | 39 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+), 7 deletions(-)

diff --git a/doc/emacs/files.texi b/doc/emacs/files.texi
index ed17f66ca2..08c8beed96 100644
--- a/doc/emacs/files.texi
+++ b/doc/emacs/files.texi
@@ -1040,14 +1040,13 @@ Auto Save Files
 this happens, save the buffer with @kbd{C-x C-s}, or use @kbd{C-u 1 M-x
 auto-save-mode}.
 
-@vindex auto-save-visited-file-name
+@vindex auto-save-visited-mode
   If you want auto-saving to be done in the visited file rather than
-in a separate auto-save file, set the variable
-@code{auto-save-visited-file-name} to a non-@code{nil} value.  In this
-mode, auto-saving is very similar to explicit saving.  However,
-differences still exist, in particular for modes which modify the
-buffer-saving process in non-trivial ways via various hooks
-(@pxref{Saving Buffers,,, elisp, The Emacs Lisp Reference Manual}).
+in a separate auto-save file, enable the global minor mode
+@code{auto-save-visited-mode}.  In this mode, auto-saving is very
+similar to explicit saving.  Note that this mode is orthogonal to the
+@code{auto-save} mode described above; you can enable both at the same
+time.
 
 @vindex delete-auto-save-files
   A buffer's auto-save file is deleted when you save the buffer in its
@@ -1091,6 +1090,13 @@ Auto Save Control
 a while; second, it may avoid some auto-saving while you are actually
 typing.
 
+@vindex auto-save-visited-interval
+  When @code{auto-save-visited-mode} is enabled, Emacs will auto-save
+file-visiting buffers after five seconds of idle time.  You can
+customize the variable @code{auto-save-visited-interval} to change the
+idle time interval.  @code{auto-save-interval} and
+@code{auto-save-timeout} have no effect on this mode.
+
   Emacs also does auto-saving whenever it gets a fatal error.  This
 includes killing the Emacs job with a shell command such as @samp{kill
 %emacs}, or disconnecting a phone line or network connection.
diff --git a/etc/NEWS b/etc/NEWS
index 7281827878..2f10276d12 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -383,6 +383,12 @@ Strings such as ΌΣΟΣ are now correctly converted to Όσος when
 capitalized instead of incorrect Όσοσ (compare lowercase sigma at the
 end of the word).
 
+** Emacs can now auto-save buffers to visited files in a more robust
+manner via the new mode 'auto-save-visited-mode'.  Unlike
+'auto-save-visited-file-name', this mode uses the normal saving
+procedure and therefore obeys saving hooks.
+'auto-save-visited-file-name' is now obsolete.
+
 \f
 * Changes in Specialized Modes and Packages in Emacs 26.1
 
diff --git a/lisp/files.el b/lisp/files.el
index 6848818cad..cf9037ec68 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -393,6 +393,45 @@ auto-save-file-name-transforms
   :initialize 'custom-initialize-delay
   :version "21.1")
 
+(defvar auto-save--timer nil "Timer for `auto-save-visited-mode'.")
+
+(defcustom auto-save-visited-interval 5
+  "Interval in seconds for `auto-save-visited-mode'.
+If `auto-save-visited-mode' is enabled, Emacs will save all
+buffers visiting a file to the visited file after it has been
+idle for `auto-save-visited-interval' seconds."
+  :group 'auto-save
+  :type 'number
+  :set (lambda (symbol value)
+         (set-default symbol value)
+         (when auto-save--timer
+           (timer-set-idle-time auto-save--timer value :repeat))))
+
+(define-minor-mode auto-save-visited-mode
+  "Toggle automatic saving to file-visiting buffers on or off.
+With a prefix argument ARG, enable regular saving of all buffers
+vising a file if ARG is positive, and disable it otherwise.
+Unlike `auto-save-mode', this mode will auto-save buffer contents
+to the visited files directly and will also run all save-related
+hooks.  See Info node `Saving' for details of the save process.
+
+If called from Lisp, enable the mode if ARG is omitted or nil,
+and toggle it if ARG is `toggle'."
+  :group 'auto-save
+  :global t
+  (when auto-save--timer (kill-timer auto-save--timer))
+  (setq auto-save--timer
+        (when auto-save-visited-mode
+          (run-with-idle-timer
+           auto-save-visited-interval :repeat
+           #'save-some-buffers :no-prompt
+           (lambda ()
+             (not (and buffer-auto-save-file-name
+                       auto-save-visited-file-name)))))))
+
+(make-obsolete-variable 'auto-save-visited-file-name 'auto-save-visited-mode
+                        "Emacs 26.1")
+
 (defcustom save-abbrevs t
   "Non-nil means save word abbrevs too when files are saved.
 If `silently', don't ask the user before saving."
-- 
2.12.2


  reply	other threads:[~2017-04-22 19:01 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-18 21:39 bug#25478: 26.0.50; No hooks are called when auto-saving Philipp Stephani
2017-01-19 20:29 ` Glenn Morris
2017-01-20  8:45   ` Eli Zaretskii
2017-01-20 17:42     ` Glenn Morris
2017-03-26 18:21     ` Philipp Stephani
2017-03-26 18:45       ` Eli Zaretskii
2017-03-26 19:17         ` Philipp Stephani
2017-04-22 19:01           ` Philipp Stephani [this message]
2017-04-28  9:27             ` Eli Zaretskii
2017-04-29 18:47               ` Philipp Stephani
2017-04-29 18:49                 ` bug#25478: [PATCH] Reimplement auto-saving to visited files Philipp Stephani
     [not found]                 ` <20170429184909.40204-1-phst@google.com>
2017-04-29 19:20                   ` Eli Zaretskii
2017-04-29 19:31                     ` Philipp Stephani
     [not found]                     ` <CAArVCkR-BeJDgC5tu0PfN-63PRB5UN9Yid-UbuVdT-CpafVeAg@mail.gmail.com>
2020-08-11  8:08                       ` Stefan Kangas
2020-10-01 12:11                         ` Stefan Kangas

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=CAArVCkQ-MS-mWtf89320Th0FsuHu2VOPvt9h1Sho4j_MEo43mg@mail.gmail.com \
    --to=p.stephani2@gmail.com \
    --cc=25478@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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).