-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpaw-search.el
441 lines (399 loc) · 20 KB
/
paw-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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
;;; paw-search.el -*- lexical-binding: t; -*-
(defvar paw-live-filteringp nil)
(defvar paw-group-filteringp nil)
(defvar paw-search-filter-active nil
"When non-nil, paw is currently reading a filter from the minibuffer.
When live editing the filter, it is bound to :live.")
(defcustom paw-trailing-width 30
"Space reserved for displaying the feed and tag information."
:group 'paw
:type 'integer)
(defvar paw-print-entry-function #'paw-print-entry--default
"Function to print entries into the *paw-search* buffer.")
(defun paw-print-entry--default (entry num)
"Print ENTRY to the buffer."
(unless (equal entry "")
(let (beg end)
(setq beg (point))
(insert (paw-parse-entry-as-string entry))
(setq end (point))
(put-text-property beg end 'paw-entry entry)
(put-text-property beg end 'paw-id num)
(insert "\n"))))
(defun paw-parse-entry-as-string (entry)
"Parse the paw ENTRY and return as string."
(let* ((word (or (alist-get 'word entry) "NO TITLE"))
(exp (alist-get 'exp entry))
(content (alist-get 'content entry))
(content-json (condition-case nil
(let ((output (json-read-from-string content)))
(if (and (not (eq output nil))
(not (arrayp output))
(not (numberp output)))
output
nil))
(error nil)))
(content-filename (or (alist-get 'filename content-json) ""))
(content-path (or (alist-get 'path content-json) ""))
(anki-note-id (alist-get 'anki-note-id content-json))
(serverp (alist-get 'serverp entry))
(note (alist-get 'note entry))
(note-type (alist-get 'note_type entry))
(origin-type (alist-get 'origin_type entry))
(origin-path (alist-get 'origin_path entry))
(origin-point (alist-get 'origin_point entry))
(created-at (alist-get 'created_at entry))
(word-width (- (window-width (get-buffer-window (paw-buffer))) 10 paw-trailing-width)))
(format "%s %s %s %s %s %s %s"
(propertize (paw-format-icon note-type content serverp origin-path) 'paw-entry entry)
(pcase serverp
(2 ;; local annotations
(paw-format-content note-type word content content-path content-filename))
(_
(s-pad-right 10 " " (propertize (s-truncate 10 word) 'face 'default) )))
(s-pad-right 12 " " (propertize (s-truncate 10 created-at "") 'face 'paw-date-face ))
(s-collapse-whitespace (propertize (if (stringp origin-point)
origin-point
"" ) 'face 'paw-origin-point-face ))
(s-collapse-whitespace (if origin-path
(pcase origin-type
('wallabag-entry-mode
(propertize origin-path 'face 'paw-wallabag-face))
('nov-mode
(propertize (file-name-nondirectory origin-path) 'face 'paw-nov-face))
((or 'pdf-view-mode 'nov-mode "pdf-viewer")
(propertize (file-name-nondirectory origin-path) 'face 'paw-pdf-face))
((or 'eaf-mode "browser" 'eww-mode)
(propertize origin-path 'face 'paw-link-face))
(_ (propertize (file-name-nondirectory origin-path ) 'face 'paw-file-face)))
""))
(s-collapse-whitespace (propertize (if (s-blank-str? exp) "" (format "| %s |" exp)) 'face 'paw-exp-face ))
(s-collapse-whitespace (or (replace-regexp-in-string word (propertize word 'face '(bold underline)) note) "")))))
(defun paw-parse-entry-as-string-2 (entry)
(concat
(propertize (or (alist-get 'created_at entry) "") 'paw-entry entry)
" "
(let* ((word (alist-get 'word entry))
(content (alist-get 'content entry))
(content-json (condition-case nil
(let ((output (json-read-from-string content)))
(if (and (not (eq output nil))
(not (arrayp output))
(not (numberp output)))
output
nil))
(error nil)))
(content-filename (or (alist-get 'filename content-json) ""))
(content-path (or (alist-get 'path content-json) ""))
(serverp (or (alist-get 'serverp content-json) 2))
(origin-path (alist-get 'origin_path entry))
(origin-point (alist-get 'origin_point entry))
(origin-type (alist-get 'origin_type entry))
(note-type (alist-get 'note_type entry)))
(concat
(paw-format-column
(paw-format-icon note-type content serverp origin-path)
2 :left)
" "
(propertize (s-truncate 55 (paw-get-real-word word) ) 'face 'paw-offline-face)
" "
(if (stringp origin-point)
origin-point
(if origin-path
(pcase origin-type
('wallabag-entry-mode
(propertize origin-path 'face 'paw-wallabag-face))
('nov-mode
(propertize (file-name-nondirectory origin-path) 'face 'paw-nov-face))
((or 'pdf-view-mode 'nov-mode "pdf-viewer")
(propertize (file-name-nondirectory origin-path) 'face 'paw-pdf-face))
((or 'eaf-mode "browser" 'eww-mode)
(propertize origin-path 'face 'paw-link-face))
(_ (propertize (file-name-nondirectory origin-path ) 'face 'paw-file-face)))
""))
))
" "
(s-collapse-whitespace (or (s-truncate 120 (alist-get 'note entry)) ""))))
(defun paw-format-content (note-type word content content-path content-filename)
(pcase (car note-type)
('attachment
(s-pad-right 10 " "
(let* ((ext (downcase (file-name-extension content-path)))
(ext (if (string= ext "jpg") "jpeg" ext)))
(pcase ext
((or "pbm" "xbm" "xpm" "gif" "jpeg" "tiff" "png" "svg" "jpg")
(propertize "IMAGS"
'face 'paw-offline-face
'display (create-image (expand-file-name content-path paw-note-dir) nil nil :width (if (eq system-type 'gnu/linux) 200 100) :height nil :margin '(0 . 1))))
(_ (propertize (format "%s %s" (paw-attach-icon-for (expand-file-name content-filename)) (paw-format-column content-filename 40 :left) )
'face 'paw-offline-face))) ) ))
('image
(s-pad-right 10 " "
(propertize "IMAGS"
'face 'paw-offline-face
'display (create-image (expand-file-name content-path paw-note-dir) nil nil :width (if (eq system-type 'gnu/linux) 200 100) :height nil :margin '(0 . 1))) ))
(_ (s-pad-right 10 " "
(propertize (s-truncate 10 (s-collapse-whitespace (or (if (equal content 0) word (if content content "")) (paw-get-real-word word))))
'face 'paw-offline-face)))))
;;; format
(defun paw-format-icon (note-type content serverp origin-path)
"Return the icon based on NOTE-TYPE and CONTENT.
CONTENT is useful for sub types, for example, link."
(pcase (car note-type)
('word
(propertize (cdr note-type) 'display (let* ((content-json (condition-case nil
(let ((output (json-read-from-string content)))
(if (and (not (eq output nil))
(not (arrayp output))
(not (numberp output)))
output
nil))
(error nil)))
(anki-note-id (alist-get 'anki-note-id content-json)))
(if anki-note-id
paw-word-icon
paw-star-face-icon)
;; (pcase serverp
;; (1 )
;; paw-word-icon)
)))
('image
(propertize (cdr note-type) 'display paw-image-icon))
('bookmark
(propertize (cdr note-type) 'display (let* ((origin-path (let* ((url (url-generic-parse-url origin-path))
(https (url-type url))
(host (url-host url)))
(concat https "://" host)))
(ico-file-hash (md5 origin-path))
(icon-file-path (concat (expand-file-name ico-file-hash paw-cache-dir) ".png")))
(make-directory paw-cache-dir t)
(if (file-exists-p icon-file-path)
(create-image icon-file-path nil nil :ascent 'center)
paw-bookmark-icon))))
('attachment
(propertize (cdr note-type) 'display paw-attachment-icon))
('question
(propertize (cdr note-type) 'display paw-question-icon))
('link
(let* ((json (condition-case nil
(let ((output (json-read-from-string content)))
(if (and (not (eq output nil))
(not (arrayp output))
(not (numberp output)))
output
nil))
(error nil)))
(type (or (alist-get 'type json) ""))
(link (or (alist-get 'link json) "")))
(pcase type
("file"
(propertize (cdr note-type) 'display paw-file-link-icon))
("url"
(propertize (cdr note-type) 'display paw-url-link-icon))
("annotation"
(propertize (cdr note-type) 'display paw-annotation-link-icon))
("org"
(propertize (cdr note-type) 'display (create-image paw-org-link-file nil nil :width nil :height nil :ascent 'center))))))
('todo
(propertize (cdr note-type) 'display paw-todo-icon))
('done
(propertize (cdr note-type) 'display paw-done-icon))
('cancel
(propertize (cdr note-type) 'display paw-cancel-icon))
('highlight-1
(propertize " " 'face (cdr note-type)))
('highlight-2
(propertize " " 'face (cdr note-type)))
('highlight-3
(propertize " " 'face (cdr note-type)))
('highlight-4
(propertize " " 'face (cdr note-type)))
('highlight
(propertize "HL" 'face (cdr note-type)))
('stamp
(propertize (cdr note-type) 'display (cdr note-type)))
(_ " ")))
(defcustom paw-search-filter ""
"Query string filtering shown entries."
:group 'paw
:type 'string)
(defun paw-search-buffer ()
"Create buffer *paw*."
(get-buffer-create "*paw*"))
(defun paw-search-live-filter ()
(interactive)
(setq paw-live-filteringp t)
(setq paw-group-filteringp nil)
(setq paw-search-current-page (if (= paw-search-pages 0) 0 1))
(unwind-protect
(let ((paw-search-filter-active :live))
(setq paw-search-filter
(read-from-minibuffer "Filter: " paw-search-filter))
(message paw-search-filter))
(progn (paw-search-update-buffer)
(setq paw-live-filteringp nil))))
(defun paw-search-minibuffer-setup ()
"Set up the minibuffer for live filtering."
(when paw-search-filter-active
(when (eq :live paw-search-filter-active)
(add-hook 'post-command-hook 'paw-search-update-buffer-with-minibuffer-contents nil :local))))
(defun paw-search-update-buffer-with-minibuffer-contents ()
"Update the *paw-search* buffer based on the contents of the minibuffer."
(when (eq :live paw-search-filter-active)
;; (message "HELLO")
(let ((buffer (paw-search-buffer))
(current-filter (minibuffer-contents-no-properties)))
(when buffer
(with-current-buffer buffer
(let ((paw-search-filter current-filter))
(paw-search-update-buffer)))))))
(defvar paw-group-filteringp nil)
(defun paw-search-update-buffer (&optional page)
"Update the *paw-search* buffer listing to match the database.
When FORCE is non-nil, redraw even when the database hasn't changed."
(interactive)
(with-current-buffer (paw-search-buffer)
(let* ((inhibit-read-only t)
(standard-output (current-buffer))
(id 0)
(group-p paw-group-filteringp)
(entries (if group-p
(paw-search-get-grouped-entries page)
(paw-search-get-filtered-entries page)))
(len (length entries)))
(setq paw-search-entries-length (or (cdaar (paw-db-select
`[:select (funcall count word)
:from
,(paw-search-parse-filter paw-search-filter :group-p group-p)])) 0 ))
(setq paw-search-pages (ceiling paw-search-entries-length paw-search-page-max-rows))
(erase-buffer)
(dolist (entry entries)
(setq id (1+ id))
(if (<= id paw-search-page-max-rows)
(funcall paw-print-entry-function entry id)))
(if (< len paw-search-entries-length)
(dotimes (i paw-search-pages)
(let ((button-string (format "%d" (1+ i))))
(if (equal (string-to-number button-string) paw-search-current-page)
(add-face-text-property 0 (length button-string) 'paw-current-page-button-face t button-string))
(insert " " (buttonize button-string #'paw-search-more-data (1+ i)) " ") ))
(insert "End of entries.\n"))
(goto-char (point-min)))))
(defun paw-search-update-buffer-and-resume (&optional page)
(interactive)
(let (beg pos)
(setq beg (point))
(setq pos (window-start))
(paw-search-update-buffer paw-search-current-page)
(set-window-start (selected-window) pos)
(goto-char beg)
;; (hl-line-mode 1)
)
)
(defun paw-search-more-data (page)
(let ((inhibit-read-only t))
(setq paw-search-current-page page)
(beginning-of-line)
(delete-region (point) (progn (forward-line 1) (point)))
(paw-search-update-buffer page)))
(defun paw-search-next-page ()
(interactive)
(if (< paw-search-current-page paw-search-pages)
(progn
(setq paw-search-current-page (1+ paw-search-current-page))
(paw-search-update-buffer paw-search-current-page) )
(message "Last page.")))
(defun paw-search-previous-page ()
(interactive)
(if (> paw-search-current-page 1)
(progn
(setq paw-search-current-page (1- paw-search-current-page))
(paw-search-update-buffer paw-search-current-page) )
(message "First page.")))
(defun paw-search-get-filtered-entries (&optional page)
(let* ((sql (paw-search-parse-filter paw-search-filter :limit paw-search-page-max-rows :page page))
(entries (paw-db-select sql)))
entries))
(defun paw-search-get-grouped-entries (&optional page)
(let* ((sql (paw-search-parse-filter paw-search-filter :limit paw-search-page-max-rows :page page :group-p t))
(entries (paw-db-select sql)))
entries))
(defcustom paw-search-page-max-rows 41
"The maximum number of entries to display in a single page."
:group 'paw
:type 'integer)
(defvar paw-search-current-page 1
"The number of current page in the current search result.")
(defvar paw-search-pages 0
"The number of pages in the current search result.")
(defun paw-search-parse-filter (filter &rest properties)
"Parse the elements of a search FILTER into an emacsql."
(let* ((limit (plist-get properties :limit))
(page (plist-get properties :page))
(group-p (plist-get properties :group-p))
(words))
(if group-p
(setq words filter)
(setq words (split-string filter " ")))
(apply #'vector
(append '(:select [items:word items:exp status:content status:serverp status:note status:note_type status:origin_type status:origin_path status:origin_id status:origin_point status:created_at] :from items
:inner :join status
:on (= items:word status:word))
`(,@(when words
(if group-p
;; only search group
(list :where
`(or
,@(when words
`((= status:origin_path ,words)))
,@(when words
`((in origin_path ,(vconcat (-map (lambda (dir)
(concat dir (file-name-nondirectory words))) ;; no need to expand, otherwise, the string will be different and can not match in sql
paw-annotation-search-paths)))))
,@(when words
`((= status:origin_point ,words)))))
;; vague search
(list :where
`(or
,@(when words
(cl-loop for word in words
collect `(like items:word ,(concat "%" word "%"))))
,@(when words
(cl-loop for word in words
collect `(like status:content ,(concat "%" word "%"))))
,@(when words
(cl-loop for word in words
collect `(like status:created_at ,(concat "%" word "%"))))
,@(when words
(cl-loop for word in words
collect `(like status:origin_path ,(concat "%" word "%"))))
,@(when words
(cl-loop for word in words
collect `(like status:origin_point ,(concat "%" word "%"))))
))))
:order-by (desc status:created_at)
,@(when limit
(list :limit limit) )
,@(when page
(list :offset (* (1- page) paw-search-page-max-rows)))
)
))))
(defun paw-valid-regexp-p (regexp)
"Return t if REGEXP is a valid REGEXP."
(ignore-errors
(prog1 t
(string-match-p regexp ""))))
(defun paw-search-clear-filter ()
"Clear the fitler keyword."
(interactive)
(setq paw-group-filteringp nil)
(setq paw-search-current-page (if (= paw-search-pages 0) 0 1))
(paw-search-update-buffer-with-keyword ""))
(defun paw-search-update-buffer-with-keyword (keyword)
"Filter the *paw-search* buffer with KEYWORD."
(setq paw-search-filter keyword)
(paw-search-update-buffer))
(defun paw-search-refresh ()
(interactive)
(paw-search-update-buffer-with-keyword paw-search-filter))
(provide 'paw-search)