backup abbrev_defs

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

backup abbrev_defs

Sharon Kimble-2

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?

Thanks
Sharon.
--
A taste of linux = http://www.sharons.org.uk
DrugFacts = https://www.drugfacts.org.uk
Debian 10.1, fluxbox 1.3.7, emacs 26.3, org 9.3

signature.asc (847 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: backup abbrev_defs

Michael Heerdegen
Sharon Kimble <[hidden email]> 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 | Threaded
Open this post in threaded view
|

Re: backup abbrev_defs

Sharon Kimble-2
Michael Heerdegen <[hidden email]> writes:

> Sharon Kimble <[hidden email]> 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.
>
Thanks for replying Michael.

I've been trying out various things, and I've finally settled on
rewriting my 'abbrev_defs' code, and also backing up my emacs to my
server hourly, as well as with rsnapshot every 3 hours.

My code is now -

#+BEGIN_SRC emacs-lisp
;; turn on abbrev mode globally
(setq-default abbrev-mode t)
(setq save-abbrevs 'silently)
(setq abbrev-file-name "~/.emacs.d/abbrev_defs")
(read-abbrev-file "~/.emacs.d/abbrev_defs")
(abbrev-table-put global-abbrev-table :case-fixed t)
#+END_SRC

I did try adapting your code above but wasn't able to get it to work
successfully, sorry!

Thanks
Sharon.
--
A taste of linux = http://www.sharons.org.uk
DrugFacts = https://www.drugfacts.org.uk
Debian 10.1, fluxbox 1.3.7, emacs 26.3, org 9.3

signature.asc (847 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: backup abbrev_defs

Michael Heerdegen
Sharon Kimble <[hidden email]> writes:

> I did try adapting your code above but wasn't able to get it to work
> successfully, sorry!

I think it should work for your use case.  If you want, you can show
what you tried and didn't work, but don't feel obliged.

Michael.