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

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

zealsave: Zealous (auto) save mode


From: Ivan Shmakov
Subject: zealsave: Zealous (auto) save mode
Date: Wed, 11 Dec 2013 16:12:30 +0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

        The essence of the zealous auto-save minor mode
        (M-x zealous-save-mode) is that every time Emacs auto-saves the
        buffer, the result is saved into the file visited, while the
        previous version thereof is retained within a numeric backup
        (like: example.el.~42~.)

        With this mode, I no longer have to care about saving the code
        (or anything else, for that matter) I’m working on – or on
        saving the pieces I’m about to delete right now, but which I may
        happen to need later.

        Caution: with this mode turned on, the number of backups may
        easily reach a few hundreds in a couple of hacking sessions.
        Please therefore consider setting both kept-old-versions and
        kept-new-versions to a sufficiently high value in order to avoid
        potential removal of the backups thus created, like:

(setq kept-old-versions 999
      kept-new-versions 999)

        See also the documentation for the auto-save-visited-file-name
        and version-control Emacs variables.

-- 
FSF associate member #7257
;;; zealsave.el – Zealous (auto) save mode  -*- Emacs-Lisp -*-

;; Ivan Shmakov, 2013

;; To the extent possible under law, the author(s) have dedicated all
;; copyright and related and neighboring rights to this software to the
;; public domain worldwide.  This software is distributed without any
;; warranty.

;; You should have received a copy of the CC0 Public Domain Dedication
;; along with this software.  If not, see
;; <http://creativecommons.org/publicdomain/zero/1.0/>.

;; Author: Ivan Shmakov
;; Created: 2013-12-11
;; Keywords: convenience, minor-mode

;;; Commentary:

;; Provides zealous-save-mode minor mode.  When enabled, it:

;;   – directs auto-save in the file visited (instead of a separate
;;     file; see the auto-save-visited-file-name variable);

;;   – forces numeric backups while auto-saving (see version-control.)

;;; Code:

(defun zealous-save-backup (&optional filename)
  "Rename FILENAME to a fresh numeric backup.

Uses `buffer-auto-save-file-name' if FILENAME is not given or nil."
  ;; FIXME: use the ‘proper’ single-quotes in the docstring above
  (let ((filename (or filename buffer-auto-save-file-name)))
    ;; .
    (rename-file filename
                 (let ((version-control t))
                   (car (find-backup-file-name filename))))))

(define-minor-mode zealous-save-mode
  "Toggle zealous auto-saving in the current buffer (Zealous Save mode).
With a prefix argument ARG, enable Auto Save mode if ARG is
positive, and disable it otherwise.

If called from Lisp, enable the mode if ARG is omitted or nil."
  :lighter " ZS"
  (cond (zealous-save-mode
         (set (make-local-variable 'auto-save-visited-file-name) t)
         (add-hook 'auto-save-hook 'zealous-save-backup t t))
        (t
         (when (local-variable-p 'auto-save-visited-file-name)
           ;; FIXME: should probably restore, not reset
           (setq auto-save-visited-file-name
                 (default-value 'auto-save-visited-file-name)))
         (remove-hook 'auto-save-hook 'zealous-save-backup t)))
  ;; implies auto-save-mode; force buffer-auto-save-file-name reset
  (auto-save-mode (if (or zealous-save-mode auto-save-default)
                      +1
                    -1)))

(provide 'zealsave)

;;; Emacs trailer
;; Local variables:
;; coding: utf-8
;; fill-column: 72
;; indent-tabs-mode: nil
;; End:
;;; zealsave.el ends here

reply via email to

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