-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcalibredb-helm.el
125 lines (105 loc) · 5.2 KB
/
calibredb-helm.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
;;; calibredb-show.el --- Helm for calibredb -*- lexical-binding: t; -*-
;; Copyright (C) 2020 Damon Chan
;; Author: Damon Chan <[email protected]>
;; URL: https://github.com/chenyanming/calibredb.el
;; Keywords: tools
;; Version: 2.13.0
;; This file is NOT part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
(require 'calibredb-core)
(declare-function calibredb-set-metadata--tags "calibredb-utils.el")
(declare-function calibredb-set-metadata--comments "calibredb-utils.el")
(declare-function calibredb-open-file-with-default-tool "calibredb-utils.el")
(declare-function calibredb-show-entry "calibredb-search.el")
(declare-function calibredb-get-file-path "calibredb-utils.el")
(defvar calibredb-helm-map
(if (boundp 'helm-map)
(let ((map (make-sparse-keymap)))
(set-keymap-parent map helm-map)
(define-key map "\M-t" #'calibredb-set-metadata--tags-1)
(define-key map "\M-c" #'calibredb-set-metadata--comments-1)
map))
"Keymap for `calibredb-find-helm'.")
(defcustom calibredb-helm-actions
(if (fboundp 'helm-make-actions)
(helm-make-actions
"Open file" 'calibredb-find-file
"View details" 'calibredb-show-entry
"Open file other frame" 'calibredb-find-file-other-frame
"Open file with default tool" (lambda (candidate)
(calibredb-open-file-with-default-tool nil candidate))
"Open Cover Page" 'calibredb-find-cover
"Set tags" 'calibredb-set-metadata--tags
"Set comments" 'calibredb-set-metadata--comments
"List fileds" 'calibredb-set-metadata--list-fields
"Show metadata" 'calibredb-show-metadata
"Export" 'calibredb-export
"Remove" 'calibredb-remove
"Insert an org link" (lambda (candidate)
(unless (featurep 'org)
(require 'org))
(if (fboundp 'org-insert-link)
(org-insert-link nil (calibredb-get-file-path candidate t) (calibredb-getattr candidate :book-title))))
"Mail Add attachment" (lambda (candidate)
(mail-add-attachment (calibredb-get-file-path candidate t)))))
"Default actions for calibredb helm."
:group 'calibredb
:type '(alist :key-type string :value-type function))
(defun calibredb-helm-read ()
"Helm read for calibredb."
(when (fboundp 'helm)
(when (get-buffer "*helm action*")
(kill-buffer "*helm action*"))
(unwind-protect
(helm :sources (if (fboundp 'helm-build-sync-source)
(helm-build-sync-source "calibredb"
:header-name (lambda (name)
(concat name " in [" calibredb-root-dir "]"))
:candidates (lambda ()
(calibredb-candidates))
;; :filtered-candidate-transformer 'helm-findutils-transformer
;; :action-transformer 'helm-transform-file-load-el
:persistent-action 'calibredb-view--helm
:action 'calibredb-helm-actions
;; :help-message 'helm-generic-file-help-message
:keymap calibredb-helm-map
:candidate-number-limit 9999
;; :requires-pattern 3
))
:buffer "*helm calibredb*") )))
(defun calibredb-find-helm ()
"Use helm to list all ebooks details."
(interactive)
(calibredb-helm-read))
(defun calibredb-set-metadata--tags-1 ()
"Set metadata tag function used in helm action."
(interactive)
(if (fboundp 'with-helm-alive-p)
(with-helm-alive-p
(if (fboundp 'helm-exit-and-execute-action)
(helm-exit-and-execute-action #'calibredb-set-metadata--tags)))))
(defun calibredb-set-metadata--comments-1 ()
"Set metadata comments function used in helm actions."
(interactive)
(if (fboundp 'with-helm-alive-p)
(with-helm-alive-p
(if (fboundp 'helm-exit-and-execute-action)
(helm-exit-and-execute-action #'calibredb-set-metadata--comments)))))
(defun calibredb-view--helm (candidate)
"Visit the calibredb-entry with helm.
Argument CANDIDATE is the selected candidate."
(interactive)
(calibredb-show-entry candidate))
(provide 'calibredb-helm)
;;; calibredb-helm.el ends here