From: Michael Heerdegen <michael_heerdegen@web.de>
To: Sharon Kimble <boudiccas@skimble.plus.com>
Cc: help-gnu-emacs@gnu.org
Subject: Re: backup abbrev_defs
Date: Sun, 15 Dec 2019 18:01:50 +0100 [thread overview]
Message-ID: <875zihy135.fsf@web.de> (raw)
In-Reply-To: <87sglnt4jx.fsf@skimble.plus.com> (Sharon Kimble's message of "Sat, 14 Dec 2019 13:33:38 +0000")
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.
next prev parent reply other threads:[~2019-12-15 17:01 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-12-14 13:33 backup abbrev_defs Sharon Kimble
2019-12-15 17:01 ` Michael Heerdegen [this message]
2019-12-19 18:18 ` Sharon Kimble
2019-12-19 21:49 ` Michael Heerdegen
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=875zihy135.fsf@web.de \
--to=michael_heerdegen@web.de \
--cc=boudiccas@skimble.plus.com \
--cc=help-gnu-emacs@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 external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.