branch: elpa/annotate commit b22f594f3b813b12e442860344d2feb39d944b53 Merge: bd12129213 641daae9d0 Author: cage2 <1257703+ca...@users.noreply.github.com> Commit: GitHub <nore...@github.com>
Merge pull request #107 from cage2/prevent-saving-empty-db Prevented saving a file with an empty database; --- Changelog | 8 +++++++- NEWS.org | 12 ++++++++++++ README.org | 8 ++++++-- annotate.el | 48 ++++++++++++++++++++++++++++++++++-------------- 4 files changed, 59 insertions(+), 17 deletions(-) diff --git a/Changelog b/Changelog index e7434ad668..33999c0519 100644 --- a/Changelog +++ b/Changelog @@ -1,4 +1,10 @@ -021-04-30 cage +2021-05-07 cage + + * annotate.el: + + - prevented saving a file with an empty database; + +2021-04-30 cage * annotate.el: diff --git a/NEWS.org b/NEWS.org index eb3d0be647..3e9c0eb0dc 100644 --- a/NEWS.org +++ b/NEWS.org @@ -231,3 +231,15 @@ This version fixes a bug that prevented command like 'comment-region' to works properly when annotate-mode was active. + +- 2021-05-07 V1.3.0 cage :: + + This version added a procedure to prevent an empty database to be + saved on the user's disk. + + Moreover if an empty annotations database is going to overwrite an + existing stale database file on disk the file is deleted instead. + + Before deleting the old database file a confirmation message is + printed on the minibuffer if the custom variable + 'annotate-database-confirm-deletion' is non nil (default: t). diff --git a/README.org b/README.org index f9e8c65931..eabe5b8ec6 100644 --- a/README.org +++ b/README.org @@ -53,15 +53,19 @@ see: [[https://github.com/bastibe/annotate.el/issues/68][this thread]] and, in particular [[https://github.com/bastibe/annotate.el/issues/68#issuecomment-728218022][this message]]. +If an empty annotation database (in memory) is saved the database +file is deleted instead, if ~annotate-database-confirm-deletion~ is +non nil (the default) a confirmation action is asked to the user +before actually remove the file from the file system. + Users of [[https://github.com/emacscollective/no-littering][no-littering]] can take advantage of its packages generated files management. **** related customizable variable - ~annotate-file~ - -**** related customizable variable - ~annotate-warn-if-hash-mismatch~ + - ~annotate-database-confirm-deletion~ ** keybindings diff --git a/annotate.el b/annotate.el index 6ff3fc2ad1..2295d2b19a 100644 --- a/annotate.el +++ b/annotate.el @@ -7,7 +7,7 @@ ;; Maintainer: Bastian Bechtold ;; URL: https://github.com/bastibe/annotate.el ;; Created: 2015-06-10 -;; Version: 1.2.1 +;; Version: 1.3.0 ;; This file is NOT part of GNU Emacs. @@ -58,7 +58,7 @@ ;;;###autoload (defgroup annotate nil "Annotate files without changing them." - :version "1.2.1" + :version "1.3.0" :group 'text) ;;;###autoload @@ -155,6 +155,13 @@ database is not filtered at all." :type 'boolean :group 'annotate) +(defcustom annotate-database-confirm-deletion t + "If non nil a prompt asking confirmation before deleting a +database file that is going to be empty after saving an annotated +file will be shown" + :type 'boolean + :group 'annotate) + (defcustom annotate-annotation-max-size-not-place-new-line 15 "The maximum `string-width` allowed for an annotation to be placed on the right margin of the window instead of its own line @@ -1565,19 +1572,32 @@ annotation." (ignore-errors (%load-annotation-data)) (%load-annotation-data)))) -(defun annotate-dump-annotation-data (data) +(defun annotate-dump-annotation-data (data &optional save-empty-db) "Save `data` into annotation file." - (with-temp-file annotate-file - (let* ((print-length nil) - (%abbreviate-filename (lambda (record) - (let ((full-filename (annotate-filename-from-dump record)) - (annotations (annotate-annotations-from-dump record)) - (file-checksum (annotate-checksum-from-dump record))) - (annotate-make-record (abbreviate-file-name full-filename) - annotations - file-checksum)))) - (actual-data (mapcar %abbreviate-filename data))) - (prin1 actual-data (current-buffer))))) + (if (or save-empty-db + data) + (with-temp-file annotate-file + (let* ((print-length nil) + (%abbreviate-filename (lambda (record) + (let ((full-filename (annotate-filename-from-dump record)) + (annotations (annotate-annotations-from-dump record)) + (file-checksum (annotate-checksum-from-dump record))) + (annotate-make-record (abbreviate-file-name full-filename) + annotations + file-checksum)))) + (actual-data (mapcar %abbreviate-filename data))) + (prin1 actual-data (current-buffer)))) + (let* ((confirm-message "Delete annotations database file %S? [y/N] ") + (delete-confirmed-p (or (not annotate-database-confirm-deletion) + (string= (read-from-minibuffer (format confirm-message + annotate-file)) + "y")))) + (if delete-confirmed-p + (condition-case err + (delete-file annotate-file t) + (error (message "error removing annotation database: %S" + (error-message-string err)))) + (annotate-dump-annotation-data data t))))) (cl-defmacro with-matching-annotation-fns ((filename beginning