unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* backup abbrev_defs
@ 2019-12-14 13:33 Sharon Kimble
  2019-12-15 17:01 ` Michael Heerdegen
  0 siblings, 1 reply; 4+ messages in thread
From: Sharon Kimble @ 2019-12-14 13:33 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 342 bytes --]


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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: backup abbrev_defs
  2019-12-14 13:33 backup abbrev_defs Sharon Kimble
@ 2019-12-15 17:01 ` Michael Heerdegen
  2019-12-19 18:18   ` Sharon Kimble
  0 siblings, 1 reply; 4+ messages in thread
From: Michael Heerdegen @ 2019-12-15 17:01 UTC (permalink / raw)
  To: Sharon Kimble; +Cc: help-gnu-emacs

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.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: backup abbrev_defs
  2019-12-15 17:01 ` Michael Heerdegen
@ 2019-12-19 18:18   ` Sharon Kimble
  2019-12-19 21:49     ` Michael Heerdegen
  0 siblings, 1 reply; 4+ messages in thread
From: Sharon Kimble @ 2019-12-19 18:18 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 3313 bytes --]

Michael Heerdegen <michael_heerdegen@web.de> writes:

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

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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: backup abbrev_defs
  2019-12-19 18:18   ` Sharon Kimble
@ 2019-12-19 21:49     ` Michael Heerdegen
  0 siblings, 0 replies; 4+ messages in thread
From: Michael Heerdegen @ 2019-12-19 21:49 UTC (permalink / raw)
  To: Sharon Kimble; +Cc: help-gnu-emacs

Sharon Kimble <boudiccas@skimble.plus.com> 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.



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-12-19 21:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-12-14 13:33 backup abbrev_defs Sharon Kimble
2019-12-15 17:01 ` Michael Heerdegen
2019-12-19 18:18   ` Sharon Kimble
2019-12-19 21:49     ` Michael Heerdegen

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