-
Notifications
You must be signed in to change notification settings - Fork 3
/
anki-search.el
253 lines (224 loc) · 9.42 KB
/
anki-search.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
;;; anki-search.el -*- lexical-binding: t; -*-
;; Author: Damon Chan <elecming@gmail.com>
;; 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 'anki-core)
(defface anki-search-header-highlight-face
'((t :inherit region :weight bold :underline t))
"Face for the header at point."
:group 'anki-faces)
(defcustom anki-search-unique-buffers nil
"TODO: When non-nil, every entry buffer gets a unique name.
This allows for displaying multiple serch buffers at the same
time."
:group 'anki
:type 'boolean)
(defcustom anki-search-filter ""
"Query string filtering shown entries."
:group 'anki
:type 'string)
(defvar anki-search-header-function #'anki-search-header
"Function that returns the string to be used for the Calibredb search header.")
(defvar anki-search-mode-map
(let ((map (make-sparse-keymap)))
(define-key map [mouse-3] #'anki-search-mouse)
(define-key map (kbd "<RET>") #'anki-preview-card)
(define-key map "f" #'anki-preview-front)
(define-key map "b" #'anki-preview-back)
(define-key map "F" #'anki-preview-front-on-browser)
(define-key map "B" #'anki-preview-back-on-browser)
(define-key map "j" #'anki-search-next-card)
(define-key map "k" #'anki-search-previous-card)
(define-key map "l" #'anki-list-decks)
(define-key map "r" #'anki-replay-audio)
(define-key map "u" #'anki-search-update)
(define-key map "q" #'anki-search-quit)
(define-key map "j" #'anki-search-next-card)
(define-key map "k" #'anki-search-previous-card)
(define-key map "\M-n" #'anki-search-preview-next-card)
(define-key map "\M-p" #'anki-search-preview-previous-card)
map)
"Keymap for `anki-search-mode'.")
(defvar anki-search-filter-active nil
"When non-nil, anki is currently reading a filter from the minibuffer.
When live editing the filter, it is bound to :live.")
(define-derived-mode anki-search-mode fundamental-mode "anki-search"
"Major mode for listing calibre entries.
\\{anki-search-mode-map}"
(setq truncate-lines t
buffer-read-only t
header-line-format '(:eval (funcall anki-search-header-function)))
(buffer-disable-undo)
(set (make-local-variable 'hl-line-face) 'anki-search-header-highlight-face)
(hl-line-mode)
(add-hook 'minibuffer-setup-hook 'anki-search--minibuffer-setup))
(defun anki-search-header ()
"TODO: Return the string to be used as the Calibredb header.
Indicating the library you use."
(format "%s: %s %s"
(propertize "Anki Browser" 'face font-lock-warning-face)
anki-core-current-deck
(concat
(propertize (format "Total: %s"
(if (equal anki-search-entries '(""))
"0 "
(concat (number-to-string (length anki-search-entries)) " "))) 'face font-lock-warning-face)
(propertize (format "%s" (if (equal anki-search-filter "")
""
(concat anki-search-filter " "))) 'face font-lock-keyword-face)
;; (propertize (let ((len (length (anki-find-marked-candidates))))
;; (if (> len 0)
;; (concat "Marked: " (number-to-string len)) "")) 'face font-lock-negation-char-face)
))
)
(defun anki-search--minibuffer-setup ()
"Set up the minibuffer for live filtering."
(when anki-search-filter-active
(when (eq :live anki-search-filter-active)
(add-hook 'post-command-hook 'anki-search--live-update nil :local))))
(defun anki-search--live-update ()
"Update the anki-search buffer based on the contents of the minibuffer."
(when (eq :live anki-search-filter-active)
;; (message "HELLO")
(let ((buffer (anki-search-buffer))
(current-filter (minibuffer-contents-no-properties)))
(when buffer
(with-current-buffer buffer
(let ((anki-search-filter current-filter))
(anki-search-update :force)))))))
(defun anki-search-buffer ()
"Create buffer anki-search."
(get-buffer-create "*anki-search*"))
(defun anki-search--buffer-name ()
"Return the appropriate buffer name for ENTRY.
The result depends on the value of `anki-search-unique-buffers'."
(if anki-search-unique-buffers
(format "*anki-search-<%s>*" anki-collection-dir)
"*anki-search*"))
(defun anki-search-update (&optional force)
"Update the anki-search buffer listing to match the database.
When FORCE is non-nil, redraw even when the database hasn't changed."
(interactive)
(with-current-buffer (anki-search-buffer)
(when force
(let ((inhibit-read-only t)
(standard-output (current-buffer)))
(erase-buffer)
;; reset anki-virtual-library-name
;; (unless (-contains? (mapcar 'cdr anki-virtual-library-alist) anki-search-filter)
;; (setq anki-virtual-library-name anki-virtual-library-default-name))
;; (anki-search--update-list)
;; (setq anki-search-entries (anki-candidates))
(dolist (entry anki-search-entries)
(funcall anki-search-print-entry-function entry)
(insert "\n"))
;; (insert "End of entries.\n")
(goto-char (point-min)) ; back to point-min after filtering
(setf anki-search-last-update (float-time))))))
;;;###autoload
(defun anki-browser ()
"Enter calibre Search Buffer."
(interactive)
(anki-core-db)
(let ((cand
(if anki-search-entries
anki-search-entries
(progn
(setq anki-search-entries (anki-core-cards-list))
(setq anki-full-entries anki-search-entries)))
;; (progn
;; (setq anki-search-entries (anki-core-cards-list))
;; (setq anki-full-entries anki-search-entries))
))
(cond ((not cand)
(message "INVALID ANKI"))
(t
(when (get-buffer (anki-search-buffer))
(kill-buffer (anki-search-buffer)))
;; Set virtual library name when the first time to launch anki
;; (if (equal anki-search-filter "")
;; (setq anki-virtual-library-name anki-virtual-library-default-name))
(switch-to-buffer (anki-search-buffer))
(goto-char (point-min))
(unless (equal cand '("")) ; not empty library
(dolist (item cand)
(if (hash-table-p item)
(let (beg end)
(setq beg (point))
(insert
(concat
(if (stringp (gethash 'card-format item))
(gethash 'card-format item) "")))
;; (anki-detail-view-insert-image item)
(setq end (point))
(put-text-property beg end 'anki-entry item)
;; (require 'shr)
;; (if (fboundp 'shr-render-region)
;; (shr-render-region beg end))
(insert "\n"))))
(goto-char (point-min)))
(unless (eq major-mode 'anki-search-mode)
(anki-search-mode))))))
(defun anki-search-quit ()
"Quit *anki-card* or *anki-search*."
(interactive)
(when (eq major-mode 'anki-search-mode)
(cond ((get-buffer "*anki-card*")
(pop-to-buffer "*anki-card*")
(if (< (length (window-prev-buffers)) 2)
(kill-buffer-and-window)
(kill-buffer)))
((get-buffer "*anki-search*")
(kill-buffer "*anki-search*")))
(let ((process (get-process "anki-audio-player")))
(if (process-live-p process)
(delete-process process)))))
(defun anki-search-update ()
"Refresh anki."
(interactive)
(setq anki-search-entries (anki-core-cards-list))
(setq anki-full-entries anki-search-entries)
(anki-browser))
(defun anki-search-next-card ()
"Move to next card."
(interactive)
(forward-line 1)
;; (let ((ori "") (new ""))
;; (while (and (equal new ori) new ori)
;; (setq ori (gethash 'id (anki-core-find-card-at-point)))
;; (forward-line 1)
;; (setq new (gethash 'id (anki-core-find-card-at-point)))))
)
(defun anki-search-previous-card ()
(interactive)
(forward-line -1)
;; (let ((ori "") (new ""))
;; (while (and (equal new ori) new ori (> (line-number-at-pos) 1))
;; (forward-line -1)
;; (save-excursion
;; (setq ori (gethash 'id (anki-core-find-card-at-point)))
;; (forward-line -1)
;; (setq new (gethash 'id (anki-core-find-card-at-point))))))
)
(defun anki-search-preview-next-card ()
"Preview next card."
(interactive)
(anki-search-next-card)
(anki-show-card (anki-core-find-card-at-point) :switch))
(defun anki-search-preview-previous-card ()
"Preview previous card."
(interactive)
(anki-search-previous-card)
(anki-show-card (anki-core-find-card-at-point) :switch))
(provide 'anki-search)