Debian Codegraph @

mew-pick.el

raw
1 ;;; mew-pick.el --- Picking up messages for Mew
2
3 ;; Author: Kazu Yamamoto <[email protected]>
4 ;; Created: Oct 2, 1996
5
6 ;;; Code:
7
8 (require 'mew)
9
10 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11 ;;;
12 ;;; Pick commands
13 ;;;
14
15 (defun mew-summary-pick-msgs (folder regionp)
16 (let* ((dir (mew-expand-folder folder))
17 (default-directory dir) ;; buffer local
18 beg end region msg msgs)
19 (if (mew-mark-active-p) (setq regionp t))
20 (cond
21 (regionp
22 (setq region (mew-summary-get-region))
23 (setq beg (car region))
24 (setq end (cdr region))
25 (save-excursion
26 (goto-char beg)
27 (while (re-search-forward mew-regex-msg-or-part end t) ;; for thread
28 (setq msg (mew-summary-message-number))
29 (setq msg (mew-msg-get-filename msg))
30 (setq msgs (cons msg msgs))
31 (forward-line)))
32 (setq msgs (delq nil msgs))
33 (setq msgs (nreverse msgs)))
34 (t
35 (setq msgs (mew-dir-messages ".")) ;; all messages
36 (setq msgs (delq nil msgs))))))
37
38 (defun mew-summary-pick (&optional regionp)
39 "Pick messages according to a specified pick pattern.
40 Then put the '*' mark onto them. 'mewl' or 'grep' is called as a
41 picking command. If called with '\\[universal-argument]', the
42 target is the region."
43 (interactive "P")
44 (mew-pickable
45 (mew-summary-with-mewl
46 (let* ((folder (mew-summary-physical-folder))
47 (msgs (mew-summary-pick-msgs folder regionp))
48 (prompt (format "%s/%s pick" mew-prog-mewl mew-prog-grep))
49 (prog mew-prog-grep)
50 (opts mew-prog-grep-opts)
51 mew-inherit-pick-mewlp
52 grepp pattern prog-opts-pat)
53 (if (not msgs)
54 (message "No message")
55 (setq pattern (mew-input-pick-pattern prompt))
56 (cond
57 ((string= pattern "")
58 (setq prog-opts-pat (mew-input-pick-command prog opts))
59 (mew-set '(prog opts pattern) prog-opts-pat)
60 (setq grepp t))
61 (t
62 (setq pattern (mew-pick-canonicalize-pattern pattern))
63 (unless mew-inherit-pick-mewlp (setq grepp t))))
64 (if (and grepp (not (mew-which-exec prog)))
65 (message "'%s' not found" prog)
66 (cond
67 (grepp
68 (mew-sinfo-set-find-key pattern)
69 (message "Picking messages in %s..." folder)
70 (setq msgs (mew-summary-pick-with-grep prog opts pattern folder msgs))
71 (message "Picking messages in %s...done" folder))
72 (t
73 (mew-sinfo-set-find-key nil) ;; force to ask a user
74 (message "Picking messages in %s..." folder)
75 (setq msgs (mew-summary-pick-with-mewl pattern folder msgs))
76 (message "Picking messages in %s...done" folder)))
77 (mew-summary-pick-ls folder msgs)))))))
78
79 (defmacro mew-dolist-eob (spec &rest body)
80 ;; (declare (indent 1))
81 `(let ((--mew-temp-- ,(nth 1 spec)) ,(nth 0 spec))
82 (while (and --mew-temp-- (not (eobp)))
83 (setq ,(nth 0 spec) (car --mew-temp--))
84 (setq --mew-temp-- (cdr --mew-temp--))
85 ,@body)))
86 (put 'mew-dolist-eob 'lisp-indent-function 1)
87
88 (defun mew-summary-pick-ls-thread (msgs)
89 (let* ((preline 0)
90 (threadmsgs (mew-summary-thread-get-msglst
91 (mew-vinfo-get-top) 'separator))
92 (total (length threadmsgs))
93 (msgs2 msgs)
94 linenum)
95 (goto-char (point-min))
96 (mew-dolist-eob (msg msgs)
97 (setq linenum (member msg threadmsgs))
98 (if (null linenum)
99 (setq msgs2 (delete (car msgs) msgs2))
100 (setq linenum (- total (length linenum)))
101 (forward-line (- linenum preline))
102 (setq preline linenum)
103 (mew-summary-mark-as mew-mark-review)))
104 (set-buffer-modified-p nil)
105 msgs2))
106
107 (defun mew-summary-pick-ls-selection (msgs)
108 ;; messages are not displayed in order
109 (goto-char (point-min))
110 (let (msgs2)
111 (mew-dolist-eob (msg msgs)
112 (when (mew-summary-search-msg msg)
113 (setq msgs2 (cons (car msgs) msgs2))
114 (mew-summary-mark-as mew-mark-review))
115 (forward-line))
116 (set-buffer-modified-p nil)
117 (nreverse msgs2)))
118
119 (defun mew-summary-pick-ls-summary (folder msgs)
120 (when (get-buffer folder)
121 (set-buffer folder)
122 (save-excursion
123 (goto-char (point-min))
124 (mew-dolist-eob (msg msgs)
125 (when (re-search-forward (mew-regex-sumsyn-msg msg) nil t)
126 (mew-summary-mark-as mew-mark-review)
127 (forward-line))))
128 (set-buffer-modified-p nil)))
129
130 (defun mew-summary-pick-ls (folder msgs)
131 (if (null msgs)
132 (message "No message to be marked")
133 (let ((n (length msgs)) msgs2)
134 (if (= n 1)
135 (message "Marking 1 message...")
136 (message "Marking %d messages..." n))
137 (save-excursion
138 (cond
139 ((mew-thread-p)
140 (setq msgs2 (mew-summary-pick-ls-thread msgs)))
141 ((mew-selection-p)
142 (setq msgs2 (mew-summary-pick-ls-selection msgs)))
143 (t ;; Summary mode
144 (setq msgs2 msgs)))
145 (mew-summary-pick-ls-summary folder msgs2))
146 (if (= n 1)
147 (message "Marking 1 message...done")
148 (message "Marking %d messages...done" n)))))
149
150 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
151 ;;;
152 ;;; Pick internal
153 ;;;
154
155 (defun mew-summary-pick-range (src-msgs)
156 (when src-msgs
157 (let* ((min (string-to-number (car src-msgs)))
158 (max min)
159 i)
160 (setq src-msgs (cdr src-msgs))
161 (dolist (src-msg src-msgs)
162 (setq i (string-to-number src-msg))
163 (if (< i min) (setq min i))
164 (if (> i max) (setq max i)))
165 (format "%d-%d" min max))))
166
167 (defun mew-summary-pick-with-mewl (pattern folder src-msgs)
168 "A function to pick messages matching PATTERN with 'mewl'"
169 (let ((pfolder (mew-scan-mewl-folder (mew-expand-folder2 folder)))
170 (range (mew-summary-pick-range src-msgs))
171 (opts (list "-b" mew-mail-path
172 "-l" (number-to-string mew-scan-max-field-length)
173 "-x" mew-suffix
174 "-p" pattern))
175 msgs)
176 (if range
177 (setq opts (nconc opts (list pfolder range)))
178 (setq opts (nconc opts (list pfolder))))
179 (with-temp-buffer
180 (mew-set-buffer-multibyte t)
181 (mew-piolet mew-cs-text-for-read mew-cs-text-for-write
182 (apply 'call-process mew-prog-mewl nil t nil opts))
183 (goto-char (point-min))
184 (while (re-search-forward mew-regex-message-files2 nil t)
185 (setq msgs (cons (mew-match-string 1) msgs))
186 (forward-line)))
187 (nreverse msgs)))
188
189 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
190 ;;;
191 ;;; Grep
192 ;;;
193
194 (defun mew-summary-pick-with-grep (prog opts pattern folder src-msgs)
195 "A function to pick messages matching PATTERN."
196 (let* ((dir (mew-expand-folder folder))
197 (default-directory dir) ;; buffer local
198 msgs nxt)
199 ;; no sort here
200 (if (= (length src-msgs) 1) (setq src-msgs (cons null-device src-msgs)))
201 (if pattern (setq pattern (mew-cs-encode-arg pattern)))
202 (with-temp-buffer
203 (mew-set-buffer-multibyte t)
204 (cd dir)
205 (mew-piolet mew-cs-text-for-read mew-cs-text-for-write
206 (while src-msgs
207 (goto-char (point-max))
208 (setq nxt (nthcdr mew-prog-grep-max-msgs src-msgs))
209 (if nxt (mew-ntake mew-prog-grep-max-msgs src-msgs))
210 (apply 'call-process prog nil t nil
211 (append opts (and pattern (list pattern)) src-msgs))
212 (setq src-msgs nxt)))
213 (goto-char (point-min))
214 (while (re-search-forward mew-regex-message-files2 nil t)
215 (setq msgs (cons (mew-match-string 1) msgs))
216 (forward-line)))
217 (setq msgs (mew-uniq-list msgs))
218 (setq msgs (mapcar 'string-to-number msgs))
219 (setq msgs (sort msgs '<))
220 (mapcar 'number-to-string msgs)))
221
222 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
223 ;;;
224 ;;; Displaying keyword
225 ;;;
226
227 (defun mew-summary-find-keyword-down (&optional arg)
228 "Display a message in the forward direction, find a keyword
229 and highlight it.
230 In Summary mode, the target is messages marked with '*'.
231 In Virtual mode, the target is all messages in a Virtual folder.
232 The keyword is stored in a buffer local variable in Summary mode.
233 If no keyword is set to the variable, this command first asks you
234 for a keyword.
235 If you want to change the stored keyword, execute this command with '\\[universal-argument]'."
236 (interactive "P")
237 (let* ((fld (mew-summary-folder-name))
238 (msg (mew-summary-message-number))
239 (fid (mew-frame-id))
240 (ofld (mew-current-get-fld fid))
241 (omsg (mew-current-get-msg fid))
242 (cwin (get-buffer-window (current-buffer)))
243 (mbuf (mew-buffer-message))
244 (mwin (get-buffer-window mbuf))
245 (key (mew-sinfo-get-find-key))
246 (mark (mew-summary-get-mark))
247 (virtualp (mew-virtual-p))
248 (search t)
249 end top)
250 (when (or arg (not (stringp key)))
251 (setq key (read-string
252 "Keyword: " (or (car mew-input-pick-pattern-hist) key)))
253 (mew-sinfo-set-find-key key))
254 (cond
255 ((and (or virtualp (equal mew-mark-review mark)) ;; MUST be equal
256 (or (not (string= fld ofld)) (not (string= msg omsg))))
257 (mew-summary-display-asis)
258 (setq top t))
259 ((or (null mwin)
260 (not (or virtualp (equal mew-mark-review mark))) ;; MUST be equal
261 (or (not (string= fld ofld)) (not (string= msg omsg)))
262 (with-current-buffer mbuf (eobp)))
263 (cond
264 (virtualp
265 (forward-line)
266 (mew-summary-display-asis)
267 (setq top t))
268 ((mew-summary-down-mark mew-mark-review)
269 (mew-summary-display-asis)
270 (setq top t))
271 (t
272 (setq search nil)))))
273 (setq mwin (get-buffer-window mbuf))
274 (if (not search)
275 (message "No more marked messages")
276 (select-window mwin)
277 (unwind-protect
278 (progn
279 (if top (goto-char (point-min)))
280 (if (setq end (re-search-forward key nil t))
281 (progn
282 (isearch-highlight (- end (length key)) end)
283 (recenter (/ (window-height) 2)))
284 (goto-char (point-max))
285 (message "Keyword '%s' is not found" key)
286 (recenter -1)))
287 (select-window cwin)))))
288
289 (defun mew-summary-find-keyword-up (&optional arg)
290 "Display a message in the backward direction, find a keyword
291 and highlight it.
292 In Summary mode, the target is messages marked with '*'.
293 In Virtual mode, the target is all messages in a Virtual folder.
294 The keyword is stored in a buffer local variable in Summary mode.
295 If no keyword is set to the variable, this command first asks you
296 for a keyword.
297 If you want to change the stored keyword, execute this command with '\\[universal-argument]'."
298 (interactive "P")
299 (let* ((fld (mew-summary-folder-name))
300 (msg (mew-summary-message-number))
301 (fid (mew-frame-id))
302 (ofld (mew-current-get-fld fid))
303 (omsg (mew-current-get-msg fid))
304 (cwin (get-buffer-window (current-buffer)))
305 (mbuf (mew-buffer-message))
306 (mwin (get-buffer-window mbuf))
307 (key (mew-sinfo-get-find-key))
308 (mark (mew-summary-get-mark))
309 (virtualp (mew-virtual-p))
310 (search t)
311 beg bottom)
312 (when (or arg (not (stringp key)))
313 (setq key (read-string
314 "Keyword: " (or (car mew-input-pick-pattern-hist) key)))
315 (mew-sinfo-set-find-key key))
316 (cond
317 ((and (or virtualp (equal mew-mark-review mark)) ;; MUST be equal
318 (or (not (string= fld ofld)) (not (string= msg omsg))))
319 (mew-summary-display-asis)
320 (setq bottom t))
321 ((or (null mwin)
322 (not (or virtualp (equal mew-mark-review mark))) ;; MUST be equal
323 (or (not (string= fld ofld)) (not (string= msg omsg)))
324 (with-current-buffer mbuf (bobp)))
325 (cond
326 (virtualp
327 (forward-line -1)
328 (mew-summary-display-asis)
329 (setq bottom t))
330 ((mew-summary-up-mark mew-mark-review)
331 (mew-summary-display-asis)
332 (setq bottom t))
333 (t
334 (setq search nil)))))
335 (setq mwin (get-buffer-window mbuf))
336 (if (not search)
337 (message "No more marked messages")
338 (select-window mwin)
339 (unwind-protect
340 (progn
341 (if bottom (goto-char (point-max)))
342 (if (setq beg (re-search-backward key nil t))
343 (progn
344 (isearch-highlight beg (+ beg (length key)))
345 (recenter (/ (window-height) 2)))
346 (goto-char (point-min))
347 (message "Keyword '%s' is not found" key)
348 (recenter 0)))
349 (select-window cwin)))))
350
351 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
352 ;;;
353 ;;; Pick macro
354 ;;;
355
356 (defvar mew-pick-macro-alist nil)
357
358 (defun mew-pick-define-macro (str1 str2)
359 "Define pick macro."
360 (interactive (list (read-string "Pick pattern: ")
361 (read-string "Macro body: ")))
362 ;; macro-pattern is a string including no #, or
363 ;; a string in a form FIELD=#1 #2 #3...#n.
364 ;; #1 can be replaced by #.
365 (let (args body asc value)
366 (while (string-match "\\(#[0-9]*\\)[, ]*" str1)
367 (setq args (cons (intern (match-string 1 str1)) args))
368 (setq str1 (replace-match "" nil t str1)))
369 (while (string-match "#[0-9]*" str2)
370 (setq body (cons (substring str2 0 (match-beginning 0)) body))
371 (setq body (cons (intern (match-string 0 str2)) body))
372 (setq str2 (substring str2 (match-end 0))))
373 (setq body (cons str2 body))
374 (setq asc (assoc str1 mew-pick-macro-alist))
375 (setq value (cons (nreverse args) (nreverse body)))
376 (if asc
377 (setcdr asc value)
378 (setq mew-pick-macro-alist
379 (cons (cons str1 value) mew-pick-macro-alist)))))
380
381 (defun mew-pick-macro-expand (name args)
382 (let ((asc (assoc name mew-pick-macro-alist))
383 alist args2 body body-copy assq)
384 (if (not asc)
385 name
386 (setq args2 (nth 1 asc))
387 (setq body (nthcdr 2 asc))
388 (while (and args args2)
389 (setq alist (cons (cons (car args2) (car args)) alist))
390 (setq args (cdr args))
391 (setq args2 (cdr args2)))
392 (dolist (bd body)
393 (if (stringp bd)
394 (setq body-copy (cons bd body-copy))
395 (setq assq (assq bd alist))
396 (if assq (setq body-copy (cons (cdr assq) body-copy)))))
397 (concat "("
398 (mew-pick-macro-expand-string
399 (apply 'concat (nreverse body-copy)))
400 ")"))))
401
402 (defun mew-pick-macro-expand-string (str)
403 (if (string= str "")
404 ""
405 (let ((first (string-to-char str))
406 asc key rest eq-flag val args)
407 (if (memq first (list ?\( ?\! ?\& ?\| ?= mew-sp ?\)))
408 (concat (char-to-string first)
409 (mew-pick-macro-expand-string (substring str 1)))
410 (if (string-match "=\\| \\|)\\|&\\||" str)
411 (if (string= (match-string 0 str) "=")
412 (progn
413 (setq eq-flag t)
414 (setq key (substring str 0 (match-end 0)))
415 (setq rest (substring str (match-end 0))))
416 (setq key (substring str 0 (match-beginning 0)))
417 (setq rest (substring str (match-beginning 0))))
418 (setq key str)
419 (setq rest ""))
420 (setq asc (assoc key mew-pick-macro-alist))
421 (cond
422 (asc
423 (setq args (nth 1 asc))
424 (while args
425 (if (string-match ",\\| \\|)\\|&\\||" rest)
426 (progn
427 (setq val (cons (substring rest 0 (match-beginning 0)) val))
428 (setq rest (substring rest (match-beginning 0))))
429 (setq val (cons rest val))
430 (setq rest ""))
431 (setq args (cdr args)))
432 (concat
433 (mew-pick-macro-expand key (nreverse val))
434 (mew-pick-macro-expand-string rest)))
435 (eq-flag
436 (if (string-match " \\|)\\|&\\||" rest)
437 (progn
438 (setq val (substring rest 0 (match-beginning 0)))
439 (setq rest (substring rest (match-beginning 0))))
440 (setq val rest)
441 (setq rest ""))
442 (concat key val (mew-pick-macro-expand-string rest)))
443 (t
444 (concat key (mew-pick-macro-expand-string rest))))))))
445
446 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
447 ;;;
448 ;;; Lex and Parser
449 ;;;
450
451 (defmacro mew-pick-lex-cut ()
452 `(when (and start (< start i))
453 (let ((word (substring pattern start i)) key op val)
454 ;; Allowing "to,cc"
455 (if (string-match "^\\([-a-z0-9,]+\\)\\(!?==?\\)\\(.+\\)$" word)
456 (progn
457 (setq key (mew-match-string 1 word))
458 (setq op (mew-match-string 2 word))
459 (setq val (mew-match-string 3 word))
460 (setq val (mew-remove-single-quote val))
461 (setq ret (cons (list op key val) ret)))
462 (setq word (mew-remove-single-quote word))
463 (setq ret (cons word ret)))
464 (setq start nil))))
465
466 (defun mew-pick-lex (pattern)
467 (let ((len (length pattern))
468 (i 0) ret start c dq)
469 (while (< i len) ;; cannot use dotimes
470 (setq c (aref pattern i))
471 (cond
472 (dq
473 (if (or (char-equal c ?\") (char-equal c ?')) (setq dq nil)))
474 ((or (char-equal c ?\") (char-equal c ?'))
475 (unless start (setq start i))
476 (setq dq t))
477 ((char-equal c ?\()
478 (mew-pick-lex-cut)
479 (setq ret (cons 'open ret)))
480 ((char-equal c ?\))
481 (mew-pick-lex-cut)
482 (setq ret (cons 'close ret)))
483 ((char-equal c ?&)
484 (mew-pick-lex-cut)
485 (setq ret (cons 'and ret)))
486 ((char-equal c ?|)
487 (mew-pick-lex-cut)
488 (setq ret (cons 'or ret)))
489 ((and (char-equal c ?!)
490 (not (char-equal (aref pattern (1+ i)) ?=)))
491 (mew-pick-lex-cut)
492 (setq ret (cons 'not ret)))
493 ((char-equal c mew-sp)
494 (mew-pick-lex-cut))
495 (t
496 (unless start (setq start i))))
497 (setq i (1+ i)))
498 (mew-pick-lex-cut)
499 (nreverse ret)))
500
501 (defun mew-pick-parse (mew-inherit-pick-tokens)
502 (let (mew-inherit-pick-ret)
503 (mew-pick-parse1)
504 (nreverse mew-inherit-pick-ret)))
505
506 (defun mew-pick-parse1 ()
507 (when mew-inherit-pick-tokens
508 (mew-pick-parse-elements)
509 (while mew-inherit-pick-tokens
510 (mew-pick-parse-and/or))))
511
512 (defun mew-pick-parse-elements ()
513 (let ((cur (car mew-inherit-pick-tokens)))
514 (setq mew-inherit-pick-tokens (cdr mew-inherit-pick-tokens))
515 (setq mew-inherit-pick-mewlp t)
516 (cond
517 ((stringp cur)
518 (setq mew-inherit-pick-mewlp nil)
519 (setq mew-inherit-pick-ret (cons cur mew-inherit-pick-ret)))
520 ((listp cur)
521 (setq mew-inherit-pick-ret (cons cur mew-inherit-pick-ret)))
522 ((eq cur 'open)
523 (mew-pick-parse-open/close))
524 ((eq cur 'close)
525 (error "')' alone"))
526 ((eq cur 'not)
527 (mew-pick-parse-not))
528 ((eq cur 'and)
529 (error "'&' alone"))
530 ((eq cur 'or)
531 (error "'|' alone")))))
532
533 (defun mew-pick-parse-and/or ()
534 (setq mew-inherit-pick-mewlp t)
535 (let ((cur (car mew-inherit-pick-tokens)))
536 (cond
537 ((or (stringp cur) (listp cur))
538 (unless mew-inherit-pick-omit-and
539 (setq mew-inherit-pick-ret (cons 'and mew-inherit-pick-ret)))
540 (mew-pick-parse-elements))
541 ((eq cur 'open)
542 (unless mew-inherit-pick-omit-and
543 (setq mew-inherit-pick-ret (cons 'and mew-inherit-pick-ret)))
544 (setq mew-inherit-pick-tokens (cdr mew-inherit-pick-tokens))
545 (mew-pick-parse-open/close))
546 ((eq cur 'close)
547 (error "')' alone"))
548 ((eq cur 'not)
549 (unless (or mew-inherit-pick-omit-and2 mew-inherit-pick-omit-and)
550 (setq mew-inherit-pick-ret (cons 'and mew-inherit-pick-ret)))
551 (setq mew-inherit-pick-tokens (cdr mew-inherit-pick-tokens))
552 (mew-pick-parse-not))
553 ((eq cur 'and)
554 (unless mew-inherit-pick-omit-and
555 (setq mew-inherit-pick-ret (cons 'and mew-inherit-pick-ret)))
556
557 (setq mew-inherit-pick-tokens (cdr mew-inherit-pick-tokens))
558 (mew-pick-parse-elements))
559 ((eq cur 'or)
560 (setq mew-inherit-pick-ret (cons 'or mew-inherit-pick-ret))
561 (setq mew-inherit-pick-tokens (cdr mew-inherit-pick-tokens))
562 (mew-pick-parse-elements)))))
563
564 (defun mew-pick-parse-not ()
565 (if (eq (car mew-inherit-pick-ret) 'not)
566 (setq mew-inherit-pick-ret (cdr mew-inherit-pick-ret))
567 (setq mew-inherit-pick-ret (cons 'not mew-inherit-pick-ret)))
568 (mew-pick-parse-elements))
569
570 (defun mew-pick-parse-open/close ()
571 (let ((sub mew-inherit-pick-tokens)
572 (level 0)
573 cur prv pprv found)
574 (catch 'loop
575 (while mew-inherit-pick-tokens
576 (setq pprv prv)
577 (setq prv mew-inherit-pick-tokens)
578 (setq cur (car mew-inherit-pick-tokens))
579 (setq mew-inherit-pick-tokens (cdr mew-inherit-pick-tokens))
580 (cond
581 ((eq cur 'open)
582 (setq level (1+ level)))
583 ((eq cur 'close)
584 (if (/= level 0)
585 (setq level (1- level))
586 (if (not pprv) (error "'( )' is empty"))
587 (setcdr pprv nil)
588 (let ((mew-inherit-pick-tokens sub))
589 (setq mew-inherit-pick-ret (cons 'open mew-inherit-pick-ret))
590 (mew-pick-parse1)
591 (setq mew-inherit-pick-ret (cons 'close mew-inherit-pick-ret)))
592 (throw 'loop (setq found t)))))))
593 (unless found (error "')' alone"))))
594
595 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
596 ;;;
597 ;;; Canonicalizer
598 ;;;
599
600 (defun mew-pick-native-text (prefix token)
601 (let (suffix func)
602 (cond
603 ((eq token 'and)
604 (setq suffix "and"))
605 ((eq token 'or)
606 (setq suffix "or"))
607 ((eq token 'open)
608 (setq suffix "open"))
609 ((eq token 'close)
610 (setq suffix "close"))
611 ((eq token 'not)
612 (setq suffix "not"))
613 ((stringp token)
614 (setq suffix "key"))
615 ((listp token)
616 (setq suffix "kyvl")))
617 (when suffix
618 (setq func (intern (concat prefix suffix)))
619 (if (fboundp func) (funcall func token)))))
620
621 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
622 ;;;
623 ;;; mewl/grep
624 ;;;
625
626 (defun mew-pick-canonicalize-pattern (pattern)
627 (mapconcat
628 'mew-pick-native-text-mewl
629 (mew-pick-parse (mew-pick-lex pattern))
630 " "))
631
632 (defun mew-pick-native-text-mewl (token)
633 (mew-pick-native-text "mew-pick-pattern-mewl-" token))
634
635 (defun mew-pick-pattern-mewl-and (_sym) "&")
636 (defun mew-pick-pattern-mewl-or (_sym) "|")
637 (defun mew-pick-pattern-mewl-open (_sym) "(")
638 (defun mew-pick-pattern-mewl-close (_sym) ")")
639 (defun mew-pick-pattern-mewl-not (_sym) "!")
640 (defun mew-pick-pattern-mewl-key (key) key)
641 (defun mew-pick-pattern-mewl-kyvl (kyvl)
642 (format "%s%s%s" (nth 1 kyvl) (nth 0 kyvl) (nth 2 kyvl)))
643
644 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
645 ;;;
646 ;;; Hyper Estraier
647 ;;;
648
649 (defun mew-pick-canonicalize-pattern-est (pattern)
650 (let ((mew-inherit-pick-omit-and2 t))
651 (mapconcat
652 'mew-pick-native-text-est
653 (mew-pick-parse (mew-pick-lex pattern))
654 " ")))
655
656 (defun mew-pick-native-text-est (token)
657 (mew-pick-native-text "mew-pick-pattern-est-" token))
658
659 (defun mew-pick-pattern-est-and (_sym) "AND")
660 (defun mew-pick-pattern-est-or (_sym) "OR")
661 (defun mew-pick-pattern-est-open (_sym) (error "'(' is not supported"))
662 (defun mew-pick-pattern-est-close (_sym) (error "')' is not supported"))
663 (defun mew-pick-pattern-est-not (_sym) "ANDNOT")
664 (defun mew-pick-pattern-est-key (key) key)
665 (defun mew-pick-pattern-est-kyvl (kyvl)
666 (error "'%s' is not supported" kyvl))
667
668 (defvar mew-pick-filter-est-head-fields "date,subject,from,to,cc,resent-from,resent-to,resent-cc,reply-to,mail-followup-to,x-mail-count,x-ml-count,x-ml-name,x-seqno,x-sequence,mailinglist-id,message-id,in-reply-to,references,newsgroups,followup-to")
669
670 (defun mew-pick-filter-est-kyvl (kyvl)
671 (unless (and (listp kyvl) (= (length kyvl) 3))
672 (error "Filter %s is invalid" kyvl))
673 (let ((op (nth 0 kyvl))
674 (ky (nth 1 kyvl))
675 (vl (nth 2 kyvl)))
676 (if (string= ky "head")
677 (setq ky mew-pick-filter-est-head-fields))
678 (cond
679 ((string= op "=")
680 (setq op "ISTRINC"))
681 ((string= op "==")
682 (setq op "STRINC"))
683 ((string= op "!=")
684 (setq op "!ISTRINC"))
685 ((string= op "!==")
686 (setq op "!STRINC"))
687 (t
688 (error "'%s' is not supported" op)))
689 (format "%s %s %s" ky op vl)))
690
691 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
692 ;;;
693 ;;; Google
694 ;;;
695
696 (defun mew-pick-canonicalize-pattern-google (pattern)
697 (let ((mew-inherit-pick-omit-and t)
698 (tokens (mew-pick-parse (mew-pick-lex pattern)))
699 ret)
700 (dolist (token tokens)
701 (if (eq token 'not)
702 (if (stringp (car tokens))
703 (progn
704 (setq ret (cons (concat "-" (car tokens)) ret))
705 (setq tokens (cdr tokens)))
706 nil) ;; xxx
707 (setq ret (cons token ret))))
708 (mapconcat
709 'mew-pick-native-text-google
710 (nreverse ret)
711 " ")))
712
713 (defun mew-pick-native-text-google (token)
714 (mew-pick-native-text "mew-pick-pattern-google-" token))
715
716 (defun mew-pick-pattern-google-and (_sym) "and")
717 (defun mew-pick-pattern-google-or (_sym) "or")
718 (defun mew-pick-pattern-google-open (_sym) (error "'(' is not supported"))
719 (defun mew-pick-pattern-google-close (_sym) (error "')' is not supported"))
720 (defun mew-pick-pattern-google-not (_sym) "-")
721 (defun mew-pick-pattern-google-key (key) key)
722 (defun mew-pick-pattern-google-kyvl (kyvl)
723 (let ((op (nth 0 kyvl)))
724 (error "'%s' is not supported" op)))
725
726 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
727 ;;;
728 ;;; WDS
729 ;;;
730
731 (defun mew-pick-canonicalize-pattern-wds (pattern)
732 (let ((mew-inherit-pick-omit-and t)
733 (tokens (mew-pick-parse (mew-pick-lex pattern)))
734 ret)
735 (dolist (token tokens)
736 (if (eq token 'not)
737 (if (stringp (car tokens))
738 (progn
739 (setq ret (cons (concat "-" (car tokens)) ret))
740 (setq tokens (cdr tokens)))
741 nil) ;; xxx
742 (setq ret (cons token ret))))
743 (mapconcat
744 'mew-pick-native-text-wds
745 (nreverse ret)
746 " ")))
747
748 (defun mew-pick-native-text-wds (token)
749 (mew-pick-native-text "mew-pick-pattern-wds-" token))
750
751 (defun mew-pick-pattern-wds-and (_sym) "")
752 (defun mew-pick-pattern-wds-or (_sym) "OR")
753 (defun mew-pick-pattern-wds-open (_sym) "(")
754 (defun mew-pick-pattern-wds-close (_sym) ")")
755 (defun mew-pick-pattern-wds-not (_sym) "-")
756 (defun mew-pick-pattern-wds-key (key) key)
757 (defun mew-pick-pattern-wds-kyvl (kyvl)
758 (let ((op (nth 0 kyvl)))
759 (error "'%s' is not supported" op)))
760
761 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
762 ;;;
763 ;;; Spotlight
764 ;;;
765
766 (defun mew-pick-canonicalize-pattern-spotlight (pattern)
767 (let* ((tokens (mew-pick-parse (mew-pick-lex pattern)))
768 (str (mapconcat
769 'mew-pick-native-text-spotlight
770 tokens
771 " ")))
772 (concat "kMDItemContentType == \"mew\"w && " str)))
773
774 (defun mew-pick-native-text-spotlight (token)
775 (mew-pick-native-text "mew-pick-pattern-spotlight-" token))
776
777 (defun mew-pick-pattern-spotlight-and (_sym) "&&")
778 (defun mew-pick-pattern-spotlight-or (_sym) "||")
779 (defun mew-pick-pattern-spotlight-open (_sym) "(")
780 (defun mew-pick-pattern-spotlight-close (_sym) ")")
781 (defun mew-pick-pattern-spotlight-not (_sym) (error "'!' is not supported"))
782 (defun mew-pick-pattern-spotlight-key (key)
783 (format "kMDItemTextContent == \"%s\"wc" key))
784 (defun mew-pick-pattern-spotlight-kyvl (kyvl)
785 (let ((op (nth 0 kyvl))
786 (ky (nth 1 kyvl))
787 (vl (nth 2 kyvl))
788 kmd)
789 (cond
790 ((string= ky "subject")
791 (setq kmd "kMDItemTitle"))
792 ((string= ky "from")
793 (setq kmd "kMDItemAuthors"))
794 ((string= ky "body")
795 (setq kmd "kMDItemTextContent"))
796 (t
797 (error "'%s' is not supported" ky)))
798 (cond
799 ((string= op "=")
800 (format "%s == \"%s\"wc" kmd vl))
801 ((string= op "==")
802 (format "%s == \"%s\"w" kmd vl))
803 ;; ((string= op "!=")
804 ;; (format "%s != \"%s\"wc" kmd vl))
805 ;; ((string= op "!==")
806 ;; (format "%s != \"%s\"c" kmd vl))
807 (t
808 (error "'%s' is not supported" op)))))
809
810 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
811 ;;;
812 ;;; Old
813 ;;;
814
815 (defun mew-summary-grep-old ()
816 "Obsoleted command."
817 (interactive)
818 (mew-message-for-summary "This command was obsoleted. Use '\\[mew-summary-pick]'"))
819
820
821 (provide 'mew-pick)
822
823 ;;; Copyright Notice:
824
825 ;; Copyright (C) 1996-2015 Mew developing team.
826 ;; All rights reserved.
827
828 ;; Redistribution and use in source and binary forms, with or without
829 ;; modification, are permitted provided that the following conditions
830 ;; are met:
831 ;;
832 ;; 1. Redistributions of source code must retain the above copyright
833 ;; notice, this list of conditions and the following disclaimer.
834 ;; 2. Redistributions in binary form must reproduce the above copyright
835 ;; notice, this list of conditions and the following disclaimer in the
836 ;; documentation and/or other materials provided with the distribution.
837 ;; 3. Neither the name of the team nor the names of its contributors
838 ;; may be used to endorse or promote products derived from this software
839 ;; without specific prior written permission.
840 ;;
841 ;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
842 ;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
843 ;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
844 ;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
845 ;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
846 ;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
847 ;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
848 ;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
849 ;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
850 ;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
851 ;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
852
853 ;;; mew-pick.el ends here