[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/calibre 569c096746 45/76: Use macros to avoid repeating
From: |
ELPA Syncer |
Subject: |
[elpa] externals/calibre 569c096746 45/76: Use macros to avoid repeating similar function definitions |
Date: |
Thu, 18 May 2023 09:57:44 -0400 (EDT) |
branch: externals/calibre
commit 569c0967465acd4e29bde20068367d5d7ea556a7
Author: Kjartan Óli Ágústsson <kjartanoli@disroot.org>
Commit: Kjartan Óli Ágústsson <kjartanoli@disroot.org>
Use macros to avoid repeating similar function definitions
* calibre-search.el (calibre-chose-author, calibre-chose-publisher,
calibre-chose-tag): Rename, and define via calibre-search--choice-function.
(calibre-search--choice-function): Create
(calibre-library-search-author, calibre-library-search-publisher,
calibre-library-search-tag): Define via calibre-library--search-function.
(calibre-library--search-function): Create
---
calibre-search.el | 79 +++++++++++++++++++++++++++----------------------------
1 file changed, 39 insertions(+), 40 deletions(-)
diff --git a/calibre-search.el b/calibre-search.el
index 4c30203106..fad8824fa3 100644
--- a/calibre-search.el
+++ b/calibre-search.el
@@ -24,52 +24,51 @@
(require 'transient)
(require 'calibre-db)
-(defun calibre-chose-author ()
- "Prompt the user to select an author."
- (interactive)
- (completing-read "Author: " (calibre-db--get-authors)))
-
-(defun calibre-chose-publisher ()
- "Prompt the user to select a publisher."
- (interactive)
- (completing-read "Author: " (calibre-db--get-publishers)))
-
-(defun calibre-chose-tag ()
- "Prompt the user to select a tag."
- (interactive)
- (completing-read "Tag: " (calibre-db--get-tags)))
+(defmacro calibre-search--choice-function (field &optional an plural)
+ "Create a function to prompt the user to select a value for FIELD.
+FIELD should be the name of a metadata field associated with
+books. A corresponding calibre-db--get-FIELDs function must
+exist, i.e. if FIELD is author the function
+calibre-db--get-authors must exist.
+
+If AN is non-nil the new function's docstring will read: Prompt
+the user to select an FIELD, otherwise it will read: Prompt the
+user to select a FIELD.
+
+If PLURAL is non-nil FIELD is assumed to be a plural word, and an s
+will not be appended to the calibre-db--get-FIELD function's name."
+ `(defun ,(intern (format "calibre-search-chose-%s" field)) ()
+ ,(format "Prompt the user to select %s %s" (if an "an" "a") field)
+ (interactive)
+ (completing-read ,(format "%s:" (capitalize field))
+ (,(intern (format "calibre-db--get-%s%s"
+ field
+ (if plural "" "s")))))))
+
+(calibre-search--choice-function "author" t)
+(calibre-search--choice-function "publisher")
+(calibre-search--choice-function "tag")
(defun calibre-search--operation (args)
"Return the appropriate symbol for a filter operation.
ARGS is the argument list of a transient command."
(if (cl-find "--exclude" args :test #'string=) '- '+))
-(defun calibre-library-search-author (author &optional args)
- "Add a filter for AUTHOR.
-ARGS determines whether the created filter is inclusive or exclusive."
- (interactive (list (calibre-chose-author)
- (transient-args 'calibre-search)))
- (setf calibre-library--filters (cons `[,(calibre-search--operation args)
author ,author]
- calibre-library--filters))
- (calibre-library--refresh))
-
-(defun calibre-library-search-publisher (publisher &optional args)
- "Add a filter for PUBLISHER.
-ARGS determines whether the created filter is inclusive or exclusive."
- (interactive (list (calibre-chose-publisher)
- (transient-args 'calibre-search)))
- (setf calibre-library--filters (cons `[,(calibre-search--operation args)
publisher ,publisher]
- calibre-library--filters))
- (calibre-library--refresh))
-
-(defun calibre-library-search-tag (tag &optional args)
- "Add a filter for TAG.
-ARGS determines whether the created filter is inclusive or exclusive."
- (interactive (list (calibre-chose-tag)
- (transient-args 'calibre-search)))
- (setf calibre-library--filters (cons `[,(calibre-search--operation args) tag
,tag]
- calibre-library--filters))
- (calibre-library--refresh))
+(defmacro calibre-library--search-function (field)
+ "Create a function adding a filter for FIELD."
+ `(defun ,(intern (format "calibre-library-search-%s" field)) (val &optional
args)
+ (interactive (list (,(intern (format "calibre-search-chose-%s" field)))
+ (transient-args 'calibre-search)))
+ (setf calibre-library--filters (cons
+ (vector (calibre-search--operation args)
+ (quote ,(intern field))
+ val)
+ calibre-library--filters))
+ (calibre-library--refresh)))
+
+(calibre-library--search-function "author")
+(calibre-library--search-function "publisher")
+(calibre-library--search-function "tag")
(defun calibre-library-clear-last-search ()
"Clear the last applied search filter."
- [elpa] externals/calibre 89e52d61c0 50/76: Only allow single publisher per book, (continued)
- [elpa] externals/calibre 89e52d61c0 50/76: Only allow single publisher per book, ELPA Syncer, 2023/05/18
- [elpa] externals/calibre c6a5772da0 29/76: Fix docstring, ELPA Syncer, 2023/05/18
- [elpa] externals/calibre 7396636341 30/76: Only refresh Library buffer if it exists, ELPA Syncer, 2023/05/18
- [elpa] externals/calibre 6d7ed97ab6 31/76: Use the value of calibre-calibredb-executable, ELPA Syncer, 2023/05/18
- [elpa] externals/calibre 4a0d5b9606 32/76: Factor the setting of the Library header into a function, ELPA Syncer, 2023/05/18
- [elpa] externals/calibre 85e3ac6337 48/76: Add missing space in completing-read prompt, ELPA Syncer, 2023/05/18
- [elpa] externals/calibre 6366966c9b 60/76: Add abstraction layer on top of calibre-db, ELPA Syncer, 2023/05/18
- [elpa] externals/calibre 9a54c7a6b4 09/76: Add function to add books from dired, ELPA Syncer, 2023/05/18
- [elpa] externals/calibre 533c68f55f 27/76: Do right alignment correctly, ELPA Syncer, 2023/05/18
- [elpa] externals/calibre 3e6ce3abc2 39/76: Bump package version, ELPA Syncer, 2023/05/18
- [elpa] externals/calibre 569c096746 45/76: Use macros to avoid repeating similar function definitions,
ELPA Syncer <=
- [elpa] externals/calibre 04c484a2ef 55/76: Preserve marks when refreshing library, ELPA Syncer, 2023/05/18
- [elpa] externals/calibre d51ac1d3e6 54/76: Don't use message to format string, ELPA Syncer, 2023/05/18
- [elpa] externals/calibre 30ec9ca2da 56/76: Use queuing to avoid multiple concurrent calls to calibredb, ELPA Syncer, 2023/05/18
- [elpa] externals/calibre 48fc9eb57b 61/76: Don't create buffer for processes, ELPA Syncer, 2023/05/18
- [elpa] externals/calibre 4069bbfd10 71/76: Bump package version, ELPA Syncer, 2023/05/18
- [elpa] externals/calibre 3e9b74bfd4 65/76: Use constants instead of magic literals for marks, ELPA Syncer, 2023/05/18
- [elpa] externals/calibre e283a2c928 76/76: Use correct separator when setting multiple authors, ELPA Syncer, 2023/05/18