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

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

zealsave: Zealous (auto) save (minor) mode 0.2


From: Ivan Shmakov
Subject: zealsave: Zealous (auto) save (minor) mode 0.2
Date: Tue, 25 Feb 2014 19:28:37 +0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Archive-name: zealsave.el-2014-is
Submitted-by: address@hidden
Last-modified: 2014-01-04 10:51:18 +00:00
Copyright-Notice: Both the README and the code are under CC0 PD dedication.

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

        The 0.2 release fixes a silly bug caused by an incorrect
        assumption that auto-save-hook would be called in the buffer to
        be saved, which made it (quite) a possibility that some
        unrelated file will be moved to its numeric backup whenever an
        auto-save is performed.

        Caution: with this mode turned on, the number of backups may
        easily reach hundreds in a couple of hacking sessions.  Please
        therefore consider setting both kept-old-versions and
        kept-new-versions to a sufficiently large 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
;; Version: 0.2
;; 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:
(defgroup zealous-save nil
  "Zealous (auto) save mode."
  :group 'auto-save)

(defcustom zealous-save-mode-hook nil
  "Functions to run when zealous (auto) save mode is activated."
  :group 'zealous-save
  :type 'hook)

(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)))
    ;; NB: the file may not (yet) exist
    (condition-case err
        ;; .
        (rename-file filename
                     (let ((version-control t))
                       (car (find-backup-file-name filename))))
      (file-error
       (unless (equal (nth 2 err) "no such file or directory")
         (signal (car err) (cdr err)))))))

(defun zealous-save-backup-all (&optional predicate)
  "Call zealous-save-backup for all the Zealous Save mode buffers.

This function calls `zealous-save-backup' for all the modified buffers
for which both Auto Save and Zealous Save minor mode are enabled and
PREDICATE (if given) returns true when called from within such a
buffer."
  ;; FIXME: use the ‘proper’ single-quotes in the docstring above
  (dolist (buf (buffer-list))
    (with-current-buffer buf
      (and zealous-save-mode
           (stringp buffer-auto-save-file-name)
           (buffer-modified-p)
           (or (not predicate)
               (funcall predicate))
           (zealous-save-backup)))))

(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)
         ;; NB: adding globally upon first use; not to be removed
         (add-hook 'auto-save-hook 'zealous-save-backup-all 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)))))
  ;; 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]