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

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

Re: backup abbrev_defs


From: Michael Heerdegen
Subject: Re: backup abbrev_defs
Date: Sun, 15 Dec 2019 18:01:50 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

Sharon Kimble <boudiccas@skimble.plus.com> writes:

> How can I backup my abbrev_defs file every time it is changed, just
> like any other file in emacs, please? And, logically, to the same
> place, my '~/.emacs.d/backups' directory?

Recently I had a similar requirement for the Gnus Registry save file.
Like `write-abbrev-file' saving is done there with a `write-region' call
from within a temporary helper buffer so no backups are created
automatically.

My solution (canonical I think) was to register a file name handler for
that save file and the `write-region` operation.  For information about
`file-name-handler-alist' see

 (info "(elisp) Magic File Names")

Code:

#+begin_src emacs-lisp
(setf (alist-get (regexp-quote ".gnus.registry.eieio")
                 file-name-handler-alist nil nil #'equal)
      (defun my-gnus-registry-save-file-handler (operation &rest args)
        (pcase-let ((`(,_start ,_end ,filename . ,_rest) args))
          (when (eq operation 'write-region)
            (let ((buffer-backed-up nil)
                  (buffer-file-name filename)
                  (file-precious-flag t)
                  (kept-new-versions 300))
              (backup-buffer))))
        ;; Invoke original handler
        (let ((inhibit-file-name-handlers
               (cons 'my-gnus-registry-save-file-handler
                     inhibit-file-name-handlers))
              (inhibit-file-name-operation operation))
          (apply operation args))))
#+end_src

It should be obvious how to translate it for your file.

The backup is created with `backup-buffer' which doesn't refer to the
temp buffer but to the file named by `buffer-file-name' (thus the
let-binding).

Since `backup-buffer' is a high-level function, all your settings
concerning backup creation are respected.  To control where the backup
is created `backup-directory-alist' is consulted - you can also add a
binding in the above defun of course.  The `kept-new-versions' binding
to 300 I use above is only because Gnus registry saving is currently
still a bit buggy and I want to force the creation of so many backup
files that I can be sure I don't lose anything even if I notice problems
after a longer time has passed.

I don't know if the above solution is the simplest one btw, it's just
the one that came to my mind at first.

Michael.



reply via email to

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