branch: externals/calibre
commit e809272c46f6020b9c87554cd0326244d40a4cb1
Author: Kjartan Oli Agustsson <[email protected]>
Commit: Kjartan Oli Agustsson <[email protected]>

    Change tag editing internals
    
    * calibre-edit.el (calibre-edit-add-tag): Simply call
    calibre-edit-add-tags with a singleton list.
    (calibre-edit-add-tags): New function.
    (calibre-edit-remove-tags): New function.
    (calibre-edit-remove-tag): Simply call calibre-edit-remove-tag with a
    singleton list.
    * calibre-library.el (calibre-library-add-tags): Call
    calibre-edit-add-tags instead of looping through the tags calling
    calibre-edit-add-tag.
    (calibre-library-mark-remove): Call calibre-edit-remove-tags instead
    of looping through the tags calling calibre-edit-remove-tag.
    
    The nested dolists in calibre-library-add-tags and
    calibre-library-remove-tags were really ugly and guaranteed O(n + m)
    performance.  This is at least significantly less ugly and might be
    more performant depending on the efficiency of the various seq-
    functions.
---
 calibre-edit.el    | 26 +++++++++++++++++---------
 calibre-library.el | 10 ++++------
 2 files changed, 21 insertions(+), 15 deletions(-)

diff --git a/calibre-edit.el b/calibre-edit.el
index 462924f2a1..d41f63c1f7 100644
--- a/calibre-edit.el
+++ b/calibre-edit.el
@@ -122,20 +122,28 @@ function does nothing."
   (unless (calibre-util-find-book book calibre-edit--edited-books)
     (push (copy-calibre-book book) calibre-edit--edited-books)))
 
-(defun calibre-edit-add-tag (tag book)
-  "Add TAG to BOOK."
+(defun calibre-edit-add-tags (tags book)
+  "Add TAGS to BOOK."
   (calibre-edit--preserve-original book)
-  (unless (member tag (calibre-book-tags book))
-    (push tag (calibre-book-tags book))
+  (when (seq-difference tags (calibre-book-tags book))
+    (setf (calibre-book-tags book) (seq-union tags (calibre-book-tags book)))
     (calibre-edit--mark-modified book)))
 
+(defun calibre-edit-remove-tags (tags book)
+  "Remove TAGS from BOOK."
+  (calibre-edit--preserve-original book)
+  (let ((difference (seq-difference (calibre-book-tags book) tags)))
+    (unless (seq-set-equal-p (calibre-book-tags book) difference)
+      (setf (calibre-book-tags book) difference)
+      (calibre-edit--mark-modified book))))
+
+(defun calibre-edit-add-tag (tag book)
+  "Add TAG to BOOK."
+  (calibre-edit-add-tags (list tag) book))
+
 (defun calibre-edit-remove-tag (tag book)
   "Remove TAG from BOOK."
-  (calibre-edit--preserve-original book)
-  (when (member tag (calibre-book-tags book))
-    (setf (calibre-book-tags book)
-          (seq-remove (apply-partially #'string= tag) (calibre-book-tags 
book)))
-    (calibre-edit--mark-modified book)))
+  (calibre-edit-remove-tags (list tag) book))
 
 (defun calibre-edit-book (book)
   "Edit the metadata of BOOK."
diff --git a/calibre-library.el b/calibre-library.el
index 66db3151a4..47826a59e6 100644
--- a/calibre-library.el
+++ b/calibre-library.el
@@ -83,9 +83,8 @@ TAGS should be a list of strings to add to FILE."
   (interactive (list (calibre--read-tags)
                      (or (calibre-library-get-marked) (list 
(tabulated-list-get-id))))
                calibre-library-mode)
-  (dolist (tag tags)
-    (dolist (book books)
-      (calibre-edit-add-tag tag book)))
+  (dolist (book books)
+      (calibre-edit-add-tags tags book))
   (calibre-library--refresh))
 
 (defun calibre-library-remove-tags (tags books)
@@ -93,9 +92,8 @@ TAGS should be a list of strings to add to FILE."
   (interactive (list (calibre--read-tags)
                      (or (calibre-library-get-marked) (list 
(tabulated-list-get-id))))
                calibre-library-mode)
-  (dolist (tag tags)
-    (dolist (book books)
-      (calibre-edit-remove-tag tag book)))
+  (dolist (book books)
+    (calibre-edit-remove-tags tags book))
   (calibre-library--refresh))
 
 (defun calibre-library-remove-books (books)

Reply via email to