]> code.delx.au - gnu-emacs/blob - lisp/mh-e/mh-seq.el
Add 2009 to copyright years.
[gnu-emacs] / lisp / mh-e / mh-seq.el
1 ;;; mh-seq.el --- MH-E sequences support
2
3 ;; Copyright (C) 1993, 1995, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009 Free Software Foundation, Inc.
5
6 ;; Author: Bill Wohler <wohler@newt.com>
7 ;; Maintainer: Bill Wohler <wohler@newt.com>
8 ;; Keywords: mail
9 ;; See: mh-e.el
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; Sequences are stored in the alist `mh-seq-list' in the form:
29 ;; ((seq-name msgs ...) (seq-name msgs ...) ...)
30
31 ;;; Change Log:
32
33 ;;; Code:
34
35 (require 'mh-e)
36 (mh-require-cl)
37 (require 'mh-scan)
38
39 (require 'font-lock)
40
41 ;;; Variables
42
43 (defvar mh-last-seq-used nil
44 "Name of seq to which a msg was last added.")
45
46 (defvar mh-non-seq-mode-line-annotation nil
47 "Saved value of `mh-mode-line-annotation' when narrowed to a seq.")
48 (make-variable-buffer-local 'mh-non-seq-mode-line-annotation)
49
50 (defvar mh-internal-seqs '(answered cur deleted forwarded printed))
51
52 ;;; Macros
53
54 (defsubst mh-make-seq (name msgs)
55 "Create sequence NAME with the given MSGS."
56 (cons name msgs))
57
58 (defsubst mh-seq-name (sequence)
59 "Extract sequence name from the given SEQUENCE."
60 (car sequence))
61
62 \f
63
64 ;;; MH-Folder Commands
65
66 ;; Alphabetical.
67
68 ;;;###mh-autoload
69 (defun mh-catchup (range)
70 "Delete RANGE from the \"unseen\" sequence.
71
72 Check the documentation of `mh-interactive-range' to see how
73 RANGE is read in interactive use."
74 (interactive (list (mh-interactive-range "Catchup"
75 (cons (point-min) (point-max)))))
76 (mh-delete-msg-from-seq range mh-unseen-seq))
77
78 ;;;###mh-autoload
79 (defun mh-delete-msg-from-seq (range sequence &optional internal-flag)
80 "Delete RANGE from SEQUENCE.
81
82 Check the documentation of `mh-interactive-range' to see how
83 RANGE is read in interactive use.
84
85 In a program, non-nil INTERNAL-FLAG means do not inform MH of the
86 change."
87 (interactive (list (mh-interactive-range "Delete")
88 (mh-read-seq-default "Delete from" t)
89 nil))
90 (let ((entry (mh-find-seq sequence))
91 (user-sequence-flag (not (mh-internal-seq sequence)))
92 (folders-changed (list mh-current-folder))
93 (msg-list ()))
94 (when entry
95 (mh-iterate-on-range msg range
96 (push msg msg-list)
97 ;; Calling "mark" repeatedly takes too long. So we will pretend here
98 ;; that we are just modifying an internal sequence...
99 (when (memq msg (cdr entry))
100 (mh-remove-sequence-notation msg (not user-sequence-flag)))
101 (mh-delete-a-msg-from-seq msg sequence t))
102 ;; ... and here we will "mark" all the messages at one go.
103 (unless internal-flag (mh-undefine-sequence sequence msg-list))
104 (when (and mh-index-data (not internal-flag))
105 (setq folders-changed
106 (append folders-changed
107 (mh-index-delete-from-sequence sequence msg-list))))
108 (when (and (eq sequence mh-unseen-seq) (mh-speed-flists-active-p))
109 (apply #'mh-speed-flists t folders-changed)))))
110
111 ;;;###mh-autoload
112 (defun mh-delete-seq (sequence)
113 "Delete SEQUENCE.
114
115 You are prompted for the sequence to delete. Note that this
116 deletes only the sequence, not the messages in the sequence. If
117 you want to delete the messages, use \"\\[universal-argument]
118 \\[mh-delete-msg]\"."
119 (interactive (list (mh-read-seq-default "Delete" t)))
120 (let ((msg-list (mh-seq-to-msgs sequence))
121 (internal-flag (mh-internal-seq sequence))
122 (folders-changed (list mh-current-folder)))
123 (mh-iterate-on-range msg sequence
124 (mh-remove-sequence-notation msg internal-flag))
125 (mh-undefine-sequence sequence '("all"))
126 (mh-delete-seq-locally sequence)
127 (when mh-index-data
128 (setq folders-changed
129 (append folders-changed
130 (mh-index-delete-from-sequence sequence msg-list))))
131 (when (and (eq sequence mh-unseen-seq) (mh-speed-flists-active-p))
132 (apply #'mh-speed-flists t folders-changed))))
133
134 ;; Shush compiler.
135 (defvar view-exit-action)
136
137 ;;;###mh-autoload
138 (defun mh-list-sequences ()
139 "List all sequences in folder.
140
141 The list appears in a buffer named \"*MH-E Sequences*\"."
142 (interactive)
143 (let ((folder mh-current-folder)
144 (temp-buffer mh-sequences-buffer)
145 (seq-list mh-seq-list)
146 (max-len 0))
147 (with-output-to-temp-buffer temp-buffer
148 (save-excursion
149 (set-buffer temp-buffer)
150 (erase-buffer)
151 (message "Listing sequences ...")
152 (insert "Sequences in folder " folder ":\n")
153 (let ((seq-list seq-list))
154 (while seq-list
155 (setq max-len
156 (max (length (symbol-name (mh-seq-name (pop seq-list))))
157 max-len)))
158 (setq max-len (+ 2 max-len)))
159 (while seq-list
160 (let ((name (mh-seq-name (car seq-list)))
161 (sorted-seq-msgs
162 (mh-coalesce-msg-list
163 (sort (copy-sequence (mh-seq-msgs (car seq-list))) '<)))
164 name-spec)
165 (insert (setq name-spec (format (format "%%%ss:" max-len) name)))
166 (while sorted-seq-msgs
167 (let ((next-element (format " %s" (pop sorted-seq-msgs))))
168 (when (>= (+ (current-column) (length next-element))
169 (window-width))
170 (insert "\n")
171 (insert (format (format "%%%ss" (length name-spec)) "")))
172 (insert next-element)))
173 (insert "\n"))
174 (setq seq-list (cdr seq-list)))
175 (goto-char (point-min))
176 (mh-view-mode-enter)
177 (setq view-exit-action 'kill-buffer)
178 (message "Listing sequences...done")))))
179
180 ;;;###mh-autoload
181 (defun mh-msg-is-in-seq (message)
182 "Display the sequences in which the current message appears.
183
184 Use a prefix argument to display the sequences in which another
185 MESSAGE appears."
186 (interactive "P")
187 (if (not message)
188 (setq message (mh-get-msg-num t)))
189 (let* ((dest-folder (loop for seq in mh-refile-list
190 when (member message (cdr seq)) return (car seq)
191 finally return nil))
192 (deleted-flag (unless dest-folder (member message mh-delete-list))))
193 (message "Message %d%s is in sequences: %s"
194 message
195 (cond (dest-folder (format " (to be refiled to %s)" dest-folder))
196 (deleted-flag (format " (to be deleted)"))
197 (t ""))
198 (mapconcat 'concat
199 (mh-list-to-string (mh-seq-containing-msg message t))
200 " "))))
201
202 ;; Shush compiler.
203 (defvar tool-bar-mode) ; XEmacs
204 (defvar tool-bar-map)
205
206 ;;;###mh-autoload
207 (defun mh-narrow-to-seq (sequence)
208 "Restrict display to messages in SEQUENCE.
209
210 You are prompted for the name of the sequence. What this command
211 does is show only those messages that are in the selected
212 sequence in the MH-Folder buffer. In addition, it limits further
213 MH-E searches to just those messages.
214
215 When you want to widen the view to all your messages again, use
216 \\[mh-widen]."
217 (interactive (list (mh-read-seq "Narrow to" t)))
218 (with-mh-folder-updating (t)
219 (cond ((mh-seq-to-msgs sequence)
220 (mh-remove-all-notation)
221 (let ((eob (point-max))
222 (msg-at-cursor (mh-get-msg-num nil)))
223 (push mh-thread-scan-line-map mh-thread-scan-line-map-stack)
224 (setq mh-thread-scan-line-map (make-hash-table :test #'eql))
225 (mh-copy-seq-to-eob sequence)
226 (push (buffer-substring-no-properties (point-min) eob)
227 mh-folder-view-stack)
228 (delete-region (point-min) eob)
229 (mh-notate-deleted-and-refiled)
230 (mh-notate-cur)
231 (when msg-at-cursor (mh-goto-msg msg-at-cursor t t))
232 (setq mh-non-seq-mode-line-annotation mh-mode-line-annotation)
233 (setq mh-mode-line-annotation (symbol-name sequence))
234 (mh-make-folder-mode-line)
235 (mh-recenter nil)
236 (when (and (boundp 'tool-bar-mode) tool-bar-mode)
237 (set (make-local-variable 'tool-bar-map)
238 mh-folder-seq-tool-bar-map)
239 (when (buffer-live-p (get-buffer mh-show-buffer))
240 (with-current-buffer mh-show-buffer
241 (set (make-local-variable 'tool-bar-map)
242 mh-show-seq-tool-bar-map))))
243 (push 'widen mh-view-ops)))
244 (t
245 (error "No messages in sequence %s" (symbol-name sequence))))))
246
247 ;;;###mh-autoload
248 (defun mh-narrow-to-tick ()
249 "Limit to ticked messages.
250
251 What this command does is show only those messages that are in
252 the \"tick\" sequence (which you can customize via the
253 `mh-tick-seq' option) in the MH-Folder buffer. In addition, it
254 limits further MH-E searches to just those messages. When you
255 want to widen the view to all your messages again, use
256 \\[mh-widen]."
257 (interactive)
258 (cond ((not mh-tick-seq)
259 (error "Enable ticking by customizing `mh-tick-seq'"))
260 ((null (mh-seq-msgs (mh-find-seq mh-tick-seq)))
261 (message "No messages in %s sequence" mh-tick-seq))
262 (t (mh-narrow-to-seq mh-tick-seq))))
263
264 ;;;###mh-autoload
265 (defun mh-put-msg-in-seq (range sequence)
266 "Add RANGE to SEQUENCE\\<mh-folder-mode-map>.
267
268 Give this command a RANGE and you can add all the messages in a
269 sequence to another sequence (for example,
270 \"\\[universal-argument] \\[mh-put-msg-in-seq] SourceSequence RET
271 DestSequence RET\"). Check the documentation of
272 `mh-interactive-range' to see how RANGE is read in interactive
273 use."
274 (interactive (list (mh-interactive-range "Add messages from")
275 (mh-read-seq-default "Add to" nil)))
276 (unless (mh-valid-seq-p sequence)
277 (error "Can't put message in invalid sequence %s" sequence))
278 (let* ((internal-seq-flag (mh-internal-seq sequence))
279 (original-msgs (mh-seq-msgs (mh-find-seq sequence)))
280 (folders (list mh-current-folder))
281 (msg-list (mh-range-to-msg-list range)))
282 (mh-add-msgs-to-seq msg-list sequence nil t)
283 (mh-iterate-on-range m range
284 (unless (memq m original-msgs)
285 (mh-add-sequence-notation m internal-seq-flag)))
286 (if (not internal-seq-flag)
287 (setq mh-last-seq-used sequence))
288 (when mh-index-data
289 (setq folders
290 (append folders (mh-index-add-to-sequence sequence msg-list))))
291 (when (and (eq sequence mh-unseen-seq) (mh-speed-flists-active-p))
292 (apply #'mh-speed-flists t folders))))
293
294 ;;;###mh-autoload
295 (defun mh-toggle-tick (range)
296 "Toggle tick mark of RANGE.
297
298 This command adds messages to the \"tick\" sequence (which you can customize
299 via the option `mh-tick-seq'). This sequence can be viewed later with the
300 \\[mh-index-ticked-messages] command.
301
302 Check the documentation of `mh-interactive-range' to see how RANGE is read in
303 interactive use."
304 (interactive (list (mh-interactive-range "Tick")))
305 (unless mh-tick-seq
306 (error "Enable ticking by customizing `mh-tick-seq'"))
307 (let* ((tick-seq (mh-find-seq mh-tick-seq))
308 (tick-seq-msgs (mh-seq-msgs tick-seq))
309 (ticked ())
310 (unticked ()))
311 (mh-iterate-on-range msg range
312 (cond ((member msg tick-seq-msgs)
313 (push msg unticked)
314 (setcdr tick-seq (delq msg (cdr tick-seq)))
315 (when (null (cdr tick-seq)) (setq mh-last-seq-used nil))
316 (mh-remove-sequence-notation msg (mh-colors-in-use-p)))
317 (t
318 (push msg ticked)
319 (setq mh-last-seq-used mh-tick-seq)
320 (let ((mh-seq-list (cons `(,mh-tick-seq ,msg) mh-seq-list)))
321 (mh-add-sequence-notation msg (mh-colors-in-use-p))))))
322 (mh-add-msgs-to-seq ticked mh-tick-seq nil t)
323 (mh-undefine-sequence mh-tick-seq unticked)
324 (when mh-index-data
325 (mh-index-add-to-sequence mh-tick-seq ticked)
326 (mh-index-delete-from-sequence mh-tick-seq unticked))))
327
328 ;;;###mh-autoload
329 (defun mh-widen (&optional all-flag)
330 "Remove last restriction.
331
332 Each limit or sequence restriction can be undone in turn with
333 this command. Give this command a prefix argument ALL-FLAG to
334 remove all limits and sequence restrictions."
335 (interactive "P")
336 (let ((msg (mh-get-msg-num nil)))
337 (when mh-folder-view-stack
338 (cond (all-flag
339 (while (cdr mh-view-ops)
340 (setq mh-view-ops (cdr mh-view-ops)))
341 (when (eq (car mh-view-ops) 'widen)
342 (setq mh-view-ops (cdr mh-view-ops))))
343 ((mh-valid-view-change-operation-p 'widen) nil)
344 ((memq 'widen mh-view-ops)
345 (while (not (eq (car mh-view-ops) 'widen))
346 (setq mh-view-ops (cdr mh-view-ops)))
347 (setq mh-view-ops (cdr mh-view-ops)))
348 (t (error "Widening is not applicable")))
349 ;; If ALL-FLAG is non-nil then rewind stacks
350 (when all-flag
351 (while (cdr mh-thread-scan-line-map-stack)
352 (setq mh-thread-scan-line-map-stack
353 (cdr mh-thread-scan-line-map-stack)))
354 (while (cdr mh-folder-view-stack)
355 (setq mh-folder-view-stack (cdr mh-folder-view-stack))))
356 (setq mh-thread-scan-line-map (pop mh-thread-scan-line-map-stack))
357 (with-mh-folder-updating (t)
358 (delete-region (point-min) (point-max))
359 (insert (pop mh-folder-view-stack))
360 (mh-remove-all-notation)
361 (setq mh-mode-line-annotation mh-non-seq-mode-line-annotation)
362 (mh-make-folder-mode-line))
363 (if msg
364 (mh-goto-msg msg t t))
365 (mh-notate-deleted-and-refiled)
366 (mh-notate-user-sequences)
367 (mh-notate-cur)
368 (mh-recenter nil)))
369 (when (and (null mh-folder-view-stack) (boundp 'tool-bar-mode) tool-bar-mode)
370 (set (make-local-variable 'tool-bar-map) mh-folder-tool-bar-map)
371 (when (buffer-live-p (get-buffer mh-show-buffer))
372 (with-current-buffer mh-show-buffer
373 (set (make-local-variable 'tool-bar-map) mh-show-tool-bar-map)))))
374
375 \f
376
377 ;;; Support Routines
378
379 (defvar mh-sequence-history ())
380
381 ;;;###mh-autoload
382 (defun mh-read-seq-default (prompt not-empty)
383 "Read and return sequence name with default narrowed or previous sequence.
384 PROMPT is the prompt to use when reading. If NOT-EMPTY is non-nil
385 then a non-empty sequence is read."
386 (mh-read-seq prompt not-empty
387 (or mh-last-seq-used
388 (car (mh-seq-containing-msg (mh-get-msg-num nil) nil)))))
389
390 (defun mh-read-seq (prompt not-empty &optional default)
391 "Read and return a sequence name.
392 Prompt with PROMPT, raise an error if the sequence is empty and
393 the NOT-EMPTY flag is non-nil, and supply an optional DEFAULT
394 sequence. A reply of '%' defaults to the first sequence
395 containing the current message."
396 (let* ((input (completing-read (format "%s sequence%s: " prompt
397 (if default
398 (format " (default %s)" default)
399 ""))
400 (mh-seq-names mh-seq-list)
401 nil nil nil 'mh-sequence-history))
402 (seq (cond ((equal input "%")
403 (car (mh-seq-containing-msg (mh-get-msg-num t) nil)))
404 ((equal input "") default)
405 (t (intern input))))
406 (msgs (mh-seq-to-msgs seq)))
407 (if (and (null msgs) not-empty)
408 (error "No messages in sequence %s" seq))
409 seq))
410
411 (defun mh-internal-seq (name)
412 "Return non-nil if NAME is the name of an internal MH-E sequence."
413 (or (memq name mh-internal-seqs)
414 (eq name mh-unseen-seq)
415 (and (mh-colors-in-use-p) mh-tick-seq (eq name mh-tick-seq))
416 (eq name mh-previous-seq)
417 (mh-folder-name-p name)))
418
419 ;;;###mh-autoload
420 (defun mh-valid-seq-p (name)
421 "Return non-nil if NAME is a valid MH sequence name."
422 (and (symbolp name)
423 (string-match "^[a-zA-Z][a-zA-Z0-9]*$" (symbol-name name))))
424
425 ;;;###mh-autoload
426 (defun mh-find-seq (name)
427 "Return sequence NAME."
428 (assoc name mh-seq-list))
429
430 ;;;###mh-autoload
431 (defun mh-seq-to-msgs (seq)
432 "Return a list of the messages in SEQ."
433 (mh-seq-msgs (mh-find-seq seq)))
434
435 (defun mh-seq-containing-msg (msg &optional include-internal-flag)
436 "Return a list of the sequences containing MSG.
437 If INCLUDE-INTERNAL-FLAG non-nil, include MH-E internal sequences
438 in list."
439 (let ((l mh-seq-list)
440 (seqs ()))
441 (while l
442 (and (memq msg (mh-seq-msgs (car l)))
443 (or include-internal-flag
444 (not (mh-internal-seq (mh-seq-name (car l)))))
445 (setq seqs (cons (mh-seq-name (car l)) seqs)))
446 (setq l (cdr l)))
447 seqs))
448
449 ;;;###mh-autoload
450 (defun mh-define-sequence (seq msgs)
451 "Define the SEQ to contain the list of MSGS.
452 Do not mark pseudo-sequences or empty sequences.
453 Signals an error if SEQ is an invalid name."
454 (if (and msgs
455 (mh-valid-seq-p seq)
456 (not (mh-folder-name-p seq)))
457 (save-excursion
458 (mh-exec-cmd-error nil "mark" mh-current-folder "-add" "-zero"
459 "-sequence" (symbol-name seq)
460 (mh-coalesce-msg-list msgs)))))
461
462 ;;;###mh-autoload
463 (defun mh-undefine-sequence (seq msgs)
464 "Remove from the SEQ the list of MSGS."
465 (when (and (mh-valid-seq-p seq) msgs)
466 (apply #'mh-exec-cmd "mark" mh-current-folder "-delete"
467 "-sequence" (symbol-name seq) (mh-coalesce-msg-list msgs))))
468
469 ;;;###mh-autoload
470 (defun mh-add-msgs-to-seq (msgs seq &optional internal-flag dont-annotate-flag)
471 "Add MSGS to SEQ.
472
473 Remove duplicates and keep sequence sorted. If optional
474 INTERNAL-FLAG is non-nil, do not mark the message in the scan
475 listing or inform MH of the addition.
476
477 If DONT-ANNOTATE-FLAG is non-nil then the annotations in the
478 folder buffer are not updated."
479 (let ((entry (mh-find-seq seq))
480 (internal-seq-flag (mh-internal-seq seq)))
481 (if (and msgs (atom msgs)) (setq msgs (list msgs)))
482 (if (null entry)
483 (setq mh-seq-list
484 (cons (mh-make-seq seq (mh-canonicalize-sequence msgs))
485 mh-seq-list))
486 (if msgs (setcdr entry (mh-canonicalize-sequence
487 (append msgs (mh-seq-msgs entry))))))
488 (unless internal-flag
489 (mh-add-to-sequence seq msgs)
490 (when (not dont-annotate-flag)
491 (mh-iterate-on-range msg msgs
492 (unless (memq msg (cdr entry))
493 (mh-add-sequence-notation msg internal-seq-flag)))))))
494
495 (defun mh-add-to-sequence (seq msgs)
496 "The sequence SEQ is augmented with the messages in MSGS."
497 ;; Add to a SEQUENCE each message the list of MSGS.
498 (if (and (mh-valid-seq-p seq) (not (mh-folder-name-p seq)))
499 (if msgs
500 (apply 'mh-exec-cmd "mark" mh-current-folder "-add"
501 "-sequence" (symbol-name seq)
502 (mh-coalesce-msg-list msgs)))))
503
504 (defun mh-canonicalize-sequence (msgs)
505 "Sort MSGS in decreasing order and remove duplicates."
506 (let* ((sorted-msgs (sort (copy-sequence msgs) '>))
507 (head sorted-msgs))
508 (while (cdr head)
509 (if (= (car head) (cadr head))
510 (setcdr head (cddr head))
511 (setq head (cdr head))))
512 sorted-msgs))
513
514 (defun mh-delete-a-msg-from-seq (msg sequence internal-flag)
515 "Delete MSG from SEQUENCE.
516 If INTERNAL-FLAG is non-nil, then do not inform MH of the
517 change."
518 (let ((entry (mh-find-seq sequence)))
519 (when (and entry (memq msg (mh-seq-msgs entry)))
520 (if (not internal-flag)
521 (mh-undefine-sequence sequence (list msg)))
522 (setcdr entry (delq msg (mh-seq-msgs entry))))))
523
524 (defun mh-delete-seq-locally (seq)
525 "Remove MH-E's record of SEQ."
526 (let ((entry (mh-find-seq seq)))
527 (setq mh-seq-list (delq entry mh-seq-list))))
528
529 (defun mh-copy-seq-to-eob (seq)
530 "Copy SEQ to the end of the buffer."
531 ;; It is quite involved to write something which will work at any place in
532 ;; the buffer, so we will write something which works only at the end of
533 ;; the buffer. If we ever need to insert sequences in the middle of the
534 ;; buffer, this will need to be fixed.
535 (save-excursion
536 (let* ((msgs (mh-seq-to-msgs seq))
537 (coalesced-msgs (mh-coalesce-msg-list msgs)))
538 (goto-char (point-max))
539 (save-restriction
540 (narrow-to-region (point) (point))
541 (mh-regenerate-headers coalesced-msgs t)
542 (cond ((memq 'unthread mh-view-ops)
543 ;; Populate restricted scan-line map
544 (mh-remove-all-notation)
545 (mh-iterate-on-range msg (cons (point-min) (point-max))
546 (setf (gethash msg mh-thread-scan-line-map)
547 (mh-thread-parse-scan-line)))
548 ;; Remove scan lines and read results from pre-computed tree
549 (delete-region (point-min) (point-max))
550 (mh-thread-print-scan-lines
551 (mh-thread-generate mh-current-folder ()))
552 (mh-notate-user-sequences))
553 (mh-index-data
554 (mh-index-insert-folder-headers)))))))
555
556 ;;;###mh-autoload
557 (defun mh-valid-view-change-operation-p (op)
558 "Check if the view change operation can be performed.
559 OP is one of 'widen and 'unthread."
560 (cond ((eq (car mh-view-ops) op)
561 (pop mh-view-ops))
562 (t nil)))
563
564 \f
565
566 ;;; Ranges
567
568 (defvar mh-range-seq-names)
569 (defvar mh-range-history ())
570 (defvar mh-range-completion-map (copy-keymap minibuffer-local-completion-map))
571 (define-key mh-range-completion-map " " 'self-insert-command)
572
573 ;;;###mh-autoload
574 (defun mh-interactive-range (range-prompt &optional default)
575 "Return interactive specification for message, sequence, range or region.
576 By convention, the name of this argument is RANGE.
577
578 If variable `transient-mark-mode' is non-nil and the mark is active,
579 then this function returns a cons-cell of the region.
580
581 If optional prefix argument is provided, then prompt for message range
582 with RANGE-PROMPT. A list of messages in that range is returned.
583
584 If a MH range is given, say something like last:20, then a list
585 containing the messages in that range is returned.
586
587 If DEFAULT non-nil then it is returned.
588
589 Otherwise, the message number at point is returned.
590
591 This function is usually used with `mh-iterate-on-range' in order to
592 provide a uniform interface to MH-E functions."
593 (cond ((mh-mark-active-p t) (cons (region-beginning) (region-end)))
594 (current-prefix-arg (mh-read-range range-prompt nil nil t t))
595 (default default)
596 (t (mh-get-msg-num t))))
597
598 ;;;###mh-autoload
599 (defun mh-read-range (prompt &optional folder default
600 expand-flag ask-flag number-as-range-flag)
601 "Read a message range with PROMPT.
602
603 If FOLDER is non-nil then a range is read from that folder, otherwise
604 use `mh-current-folder'.
605
606 If DEFAULT is a string then use that as default range to return. If
607 DEFAULT is nil then ask user with default answer a range based on the
608 sequences that seem relevant. Finally if DEFAULT is t, try to avoid
609 prompting the user. Unseen messages, if present, are returned. If the
610 folder has fewer than `mh-large-folder' messages then \"all\" messages
611 are returned. Finally as a last resort prompt the user.
612
613 If EXPAND-FLAG is non-nil then a list of message numbers corresponding
614 to the input is returned. If this list is empty then an error is
615 raised. If EXPAND-FLAG is nil just return the input string. In this
616 case we don't check if the range is empty.
617
618 If ASK-FLAG is non-nil, then the user is always queried for a range of
619 messages. If ASK-FLAG is nil, then the function checks if the unseen
620 sequence is non-empty. If that is the case, `mh-unseen-seq', or the
621 list of messages in it depending on the value of EXPAND, is returned.
622 Otherwise if the folder has fewer than `mh-large-folder' messages then
623 the list of messages corresponding to \"all\" is returned. If neither
624 of the above holds then as a last resort the user is queried for a
625 range of messages.
626
627 If NUMBER-AS-RANGE-FLAG is non-nil, then if a number, N is read as
628 input, it is interpreted as the range \"last:N\".
629
630 This function replaces the existing function `mh-read-msg-range'.
631 Calls to:
632
633 (mh-read-msg-range folder flag)
634
635 should be replaced with:
636
637 (mh-read-range \"Suitable prompt\" folder t nil flag
638 mh-interpret-number-as-range-flag)"
639 (setq default (or default mh-last-seq-used
640 (car (mh-seq-containing-msg (mh-get-msg-num nil) t)))
641 prompt (format "%s range" prompt))
642 (let* ((folder (or folder mh-current-folder))
643 (guess (eq default t))
644 (counts (and guess (mh-folder-size folder)))
645 (unseen (and counts (> (cadr counts) 0)))
646 (large (and counts mh-large-folder (> (car counts) mh-large-folder)))
647 (default (cond ((and guess large) (format "last:%s" mh-large-folder))
648 ((and guess (not large)) "all")
649 ((stringp default) default)
650 ((symbolp default) (symbol-name default))))
651 (prompt (cond ((and guess large default)
652 (format "%s (folder has %s messages, default %s)"
653 prompt (car counts) default))
654 ((and guess large)
655 (format "%s (folder has %s messages)"
656 prompt (car counts)))
657 (default
658 (format "%s (default %s)" prompt default))))
659 (minibuffer-local-completion-map mh-range-completion-map)
660 (seq-list (if (eq folder mh-current-folder)
661 mh-seq-list
662 (mh-read-folder-sequences folder nil)))
663 (mh-range-seq-names
664 (append '(("first") ("last") ("all") ("prev") ("next"))
665 (mh-seq-names seq-list)))
666 (input (cond ((and (not ask-flag) unseen) (symbol-name mh-unseen-seq))
667 ((and (not ask-flag) (not large)) "all")
668 (t (completing-read (format "%s: " prompt)
669 'mh-range-completion-function nil nil
670 nil 'mh-range-history default))))
671 msg-list)
672 (when (and number-as-range-flag
673 (string-match "^[ \t]*\\([0-9]+\\)[ \t]*$" input))
674 (setq input (concat "last:" (match-string 1 input))))
675 (cond ((not expand-flag) input)
676 ((assoc (intern input) seq-list)
677 (cdr (assoc (intern input) seq-list)))
678 ((setq msg-list (mh-translate-range folder input)) msg-list)
679 (t (error "No messages in range %s" input)))))
680
681 ;;;###mh-autoload
682 (defun mh-range-to-msg-list (range)
683 "Return a list of messages for RANGE.
684
685 Check the documentation of `mh-interactive-range' to see how
686 RANGE is read in interactive use."
687 (let (msg-list)
688 (mh-iterate-on-range msg range
689 (push msg msg-list))
690 (nreverse msg-list)))
691
692 ;;;###mh-autoload
693 (defun mh-translate-range (folder expr)
694 "In FOLDER, translate the string EXPR to a list of messages numbers."
695 (save-excursion
696 (let ((strings (delete "" (split-string expr "[ \t\n]")))
697 (result ()))
698 (ignore-errors
699 (apply #'mh-exec-cmd-quiet nil "mhpath" folder strings)
700 (set-buffer mh-temp-buffer)
701 (goto-char (point-min))
702 (while (re-search-forward "/\\([0-9]*\\)$" nil t)
703 (push (string-to-number (match-string 1)) result))
704 (nreverse result)))))
705
706 (defun mh-range-completion-function (string predicate flag)
707 "Programmable completion of message ranges.
708 STRING is the user input that is to be completed. PREDICATE if non-nil is a
709 function used to filter the possible choices and FLAG determines whether the
710 completion is over."
711 (let* ((candidates mh-range-seq-names)
712 (last-char (and (not (equal string ""))
713 (aref string (1- (length string)))))
714 (last-word (cond ((null last-char) "")
715 ((memq last-char '(? ?- ?:)) "")
716 (t (car (last (split-string string "[ -:]+"))))))
717 (prefix (substring string 0 (- (length string) (length last-word)))))
718 (cond ((eq flag nil)
719 (let ((res (try-completion last-word candidates predicate)))
720 (cond ((null res) nil)
721 ((eq res t) t)
722 (t (concat prefix res)))))
723 ((eq flag t)
724 (all-completions last-word candidates predicate))
725 ((eq flag 'lambda)
726 (loop for x in candidates
727 when (equal x last-word) return t
728 finally return nil)))))
729
730 (defun mh-seq-names (seq-list)
731 "Return an alist containing the names of the SEQ-LIST."
732 (mapcar (lambda (entry) (list (symbol-name (mh-seq-name entry))))
733 seq-list))
734
735 (defun mh-folder-size (folder)
736 "Find size of FOLDER."
737 (if mh-flists-present-flag
738 (mh-folder-size-flist folder)
739 (mh-folder-size-folder folder)))
740
741 (defun mh-folder-size-flist (folder)
742 "Find size of FOLDER using \"flist\"."
743 (with-temp-buffer
744 (call-process (expand-file-name "flist" mh-progs) nil t nil "-showzero"
745 "-norecurse" folder "-sequence" (symbol-name mh-unseen-seq))
746 (goto-char (point-min))
747 (multiple-value-bind (folder unseen total)
748 (mh-parse-flist-output-line
749 (buffer-substring (point) (mh-line-end-position)))
750 (values total unseen folder))))
751
752 (defun mh-folder-size-folder (folder)
753 "Find size of FOLDER using \"folder\"."
754 (with-temp-buffer
755 (let ((u (length (cdr (assoc mh-unseen-seq
756 (mh-read-folder-sequences folder nil))))))
757 (call-process (expand-file-name "folder" mh-progs) nil t nil
758 "-norecurse" folder)
759 (goto-char (point-min))
760 (if (re-search-forward " has \\([0-9]+\\) " nil t)
761 (values (string-to-number (match-string 1)) u folder)
762 (values 0 u folder)))))
763
764 ;;;###mh-autoload
765 (defun mh-parse-flist-output-line (line &optional current-folder)
766 "Parse LINE to generate folder name, unseen messages and total messages.
767 If CURRENT-FOLDER is non-nil then it contains the current folder
768 name and it is used to avoid problems in corner cases involving
769 folders whose names end with a '+' character."
770 (with-temp-buffer
771 (insert line)
772 (goto-char (point-max))
773 (let (folder unseen total p)
774 (when (search-backward " out of " (point-min) t)
775 (setq total (string-to-number
776 (buffer-substring-no-properties
777 (match-end 0) (mh-line-end-position))))
778 (when (search-backward " in sequence " (point-min) t)
779 (setq p (point))
780 (when (search-backward " has " (point-min) t)
781 (setq unseen (string-to-number (buffer-substring-no-properties
782 (match-end 0) p)))
783 (while (eq (char-after) ? )
784 (backward-char))
785 (setq folder (buffer-substring-no-properties
786 (point-min) (1+ (point))))
787 (when (and (equal (aref folder (1- (length folder))) ?+)
788 (equal current-folder folder))
789 (setq folder (substring folder 0 (1- (length folder)))))
790 (values (format "+%s" folder) unseen total)))))))
791
792 ;;;###mh-autoload
793 (defun mh-read-folder-sequences (folder save-refiles)
794 "Read and return the predefined sequences for a FOLDER.
795 If SAVE-REFILES is non-nil, then keep the sequences
796 that note messages to be refiled."
797 (let ((seqs ()))
798 (cond (save-refiles
799 (mh-mapc (function (lambda (seq) ; Save the refiling sequences
800 (if (mh-folder-name-p (mh-seq-name seq))
801 (setq seqs (cons seq seqs)))))
802 mh-seq-list)))
803 (save-excursion
804 (if (eq 0 (mh-exec-cmd-quiet nil "mark" folder "-list"))
805 (progn
806 ;; look for name in line of form "cur: 4" or "myseq (private): 23"
807 (while (re-search-forward "^[^: ]+" nil t)
808 (setq seqs (cons (mh-make-seq (intern (buffer-substring
809 (match-beginning 0)
810 (match-end 0)))
811 (mh-read-msg-list))
812 seqs)))
813 (delete-region (point-min) (point))))) ; avoid race with
814 ; mh-process-daemon
815 seqs))
816
817 (defun mh-read-msg-list ()
818 "Return a list of message numbers from point to the end of the line.
819 Expands ranges into set of individual numbers."
820 (let ((msgs ())
821 (end-of-line (save-excursion (end-of-line) (point)))
822 num)
823 (while (re-search-forward "[0-9]+" end-of-line t)
824 (setq num (string-to-number (buffer-substring (match-beginning 0)
825 (match-end 0))))
826 (cond ((looking-at "-") ; Message range
827 (forward-char 1)
828 (re-search-forward "[0-9]+" end-of-line t)
829 (let ((num2 (string-to-number
830 (buffer-substring (match-beginning 0)
831 (match-end 0)))))
832 (if (< num2 num)
833 (error "Bad message range: %d-%d" num num2))
834 (while (<= num num2)
835 (setq msgs (cons num msgs))
836 (setq num (1+ num)))))
837 ((not (zerop num)) ;"pick" outputs "0" to mean no match
838 (setq msgs (cons num msgs)))))
839 msgs))
840
841 \f
842
843 ;;; Notation
844
845 ;;;###mh-autoload
846 (defun mh-notate (msg notation offset)
847 "Mark MSG with the character NOTATION at position OFFSET.
848 Null MSG means the message at cursor.
849 If NOTATION is nil then no change in the buffer occurs."
850 (save-excursion
851 (if (or (null msg)
852 (mh-goto-msg msg t t))
853 (with-mh-folder-updating (t)
854 (beginning-of-line)
855 (forward-char offset)
856 (let* ((change-stack-flag
857 (and (equal offset
858 (+ mh-cmd-note mh-scan-field-destination-offset))
859 (not (eq notation mh-note-seq))))
860 (msg (and change-stack-flag (or msg (mh-get-msg-num nil))))
861 (stack (and msg (gethash msg mh-sequence-notation-history)))
862 (notation (or notation (char-after))))
863 (if stack
864 ;; The presence of the stack tells us that we don't need to
865 ;; notate the message, since the notation would be replaced
866 ;; by a sequence notation. So we will just put the notation
867 ;; at the bottom of the stack. If the sequence is deleted,
868 ;; the correct notation will be shown.
869 (setf (gethash msg mh-sequence-notation-history)
870 (reverse (cons notation (cdr (reverse stack)))))
871 ;; Since we don't have any sequence notations in the way, just
872 ;; notate the scan line.
873 (delete-char 1)
874 (insert notation))
875 (when change-stack-flag
876 (mh-thread-update-scan-line-map msg notation offset)))))))
877
878 ;;;###mh-autoload
879 (defun mh-notate-cur ()
880 "Mark the MH sequence cur.
881 In addition to notating the current message with `mh-note-cur'
882 the function uses `overlay-arrow-position' to put a marker in the
883 fringe."
884 (let ((cur (car (mh-seq-to-msgs 'cur))))
885 (when (and cur (mh-goto-msg cur t t))
886 (beginning-of-line)
887 (when (looking-at mh-scan-good-msg-regexp)
888 (mh-notate nil mh-note-cur mh-cmd-note))
889 (setq mh-arrow-marker (set-marker mh-arrow-marker (point)))
890 (setq overlay-arrow-position mh-arrow-marker))))
891
892 ;;;###mh-autoload
893 (defun mh-remove-cur-notation ()
894 "Remove old cur notation."
895 (let ((cur-msg (car (mh-seq-to-msgs 'cur))))
896 (save-excursion
897 (when (and cur-msg
898 (mh-goto-msg cur-msg t t)
899 (looking-at mh-scan-cur-msg-number-regexp))
900 (mh-notate nil ? mh-cmd-note)
901 (setq overlay-arrow-position nil)))))
902
903 ;; FIXME? We may want to clear all notations and add one for current-message
904 ;; and process user sequences.
905 ;;;###mh-autoload
906 (defun mh-notate-deleted-and-refiled ()
907 "Notate messages marked for deletion or refiling.
908 Messages to be deleted are given by `mh-delete-list' while
909 messages to be refiled are present in `mh-refile-list'."
910 (let ((refiled-hash (make-hash-table))
911 (deleted-hash (make-hash-table)))
912 (dolist (msg mh-delete-list)
913 (setf (gethash msg deleted-hash) t))
914 (dolist (dest-msg-list mh-refile-list)
915 (dolist (msg (cdr dest-msg-list))
916 (setf (gethash msg refiled-hash) t)))
917 (mh-iterate-on-messages-in-region msg (point-min) (point-max)
918 (cond ((gethash msg refiled-hash)
919 (mh-notate nil mh-note-refiled mh-cmd-note))
920 ((gethash msg deleted-hash)
921 (mh-notate nil mh-note-deleted mh-cmd-note))))))
922
923 ;;;###mh-autoload
924 (defun mh-notate-user-sequences (&optional range)
925 "Mark user-defined sequences in RANGE.
926
927 Check the documentation of `mh-interactive-range' to see how
928 RANGE is read in interactive use; if nil all messages are
929 notated."
930 (unless range
931 (setq range (cons (point-min) (point-max))))
932 (let ((seqs mh-seq-list)
933 (msg-hash (make-hash-table)))
934 (dolist (seq seqs)
935 (dolist (msg (mh-seq-msgs seq))
936 (push (car seq) (gethash msg msg-hash))))
937 (mh-iterate-on-range msg range
938 (loop for seq in (gethash msg msg-hash)
939 do (mh-add-sequence-notation msg (mh-internal-seq seq))))))
940
941 (defun mh-add-sequence-notation (msg internal-seq-flag)
942 "Add sequence notation to the MSG on the current line.
943 If INTERNAL-SEQ-FLAG is non-nil, then refontify the scan line if
944 font-lock is turned on."
945 (with-mh-folder-updating (t)
946 (save-excursion
947 (beginning-of-line)
948 (if internal-seq-flag
949 (progn
950 ;; Change the buffer so that if transient-mark-mode is active
951 ;; and there is an active region it will get deactivated as in
952 ;; the case of user sequences.
953 (mh-notate nil nil mh-cmd-note)
954 (when font-lock-mode
955 (font-lock-fontify-region (point) (mh-line-end-position))))
956 (forward-char (+ mh-cmd-note mh-scan-field-destination-offset))
957 (let ((stack (gethash msg mh-sequence-notation-history)))
958 (setf (gethash msg mh-sequence-notation-history)
959 (cons (char-after) stack)))
960 (mh-notate nil mh-note-seq
961 (+ mh-cmd-note mh-scan-field-destination-offset))))))
962
963 (defun mh-remove-sequence-notation (msg internal-seq-flag &optional all)
964 "Remove sequence notation from the MSG on the current line.
965 If INTERNAL-SEQ-FLAG is non-nil, then `font-lock' was used to
966 highlight the sequence. In that case, no notation needs to be removed.
967 Otherwise the effect of inserting `mh-note-seq' needs to be reversed.
968 If ALL is non-nil, then all sequence marks on the scan line are
969 removed."
970 (with-mh-folder-updating (t)
971 ;; This takes care of internal sequences...
972 (mh-notate nil nil mh-cmd-note)
973 (unless internal-seq-flag
974 ;; ... and this takes care of user sequences.
975 (let ((stack (gethash msg mh-sequence-notation-history)))
976 (while (and all (cdr stack))
977 (setq stack (cdr stack)))
978 (when stack
979 (save-excursion
980 (beginning-of-line)
981 (forward-char (+ mh-cmd-note mh-scan-field-destination-offset))
982 (delete-char 1)
983 (insert (car stack))))
984 (setf (gethash msg mh-sequence-notation-history) (cdr stack))))))
985
986 ;;;###mh-autoload
987 (defun mh-remove-all-notation ()
988 "Remove all notations on all scan lines that MH-E introduces."
989 (save-excursion
990 (setq overlay-arrow-position nil)
991 (goto-char (point-min))
992 (mh-iterate-on-range msg (cons (point-min) (point-max))
993 (mh-notate nil ? mh-cmd-note)
994 (mh-remove-sequence-notation msg nil t))
995 (clrhash mh-sequence-notation-history)))
996
997 \f
998
999 ;; XXX Unused, delete, or create bind key?
1000 (defun mh-rename-seq (sequence new-name)
1001 "Rename SEQUENCE to have NEW-NAME."
1002 (interactive (list (mh-read-seq "Old" t)
1003 (intern (read-string "New sequence name: "))))
1004 (let ((old-seq (mh-find-seq sequence)))
1005 (or old-seq
1006 (error "Sequence %s does not exist" sequence))
1007 ;; Create new sequence first, since it might raise an error.
1008 (mh-define-sequence new-name (mh-seq-msgs old-seq))
1009 (mh-undefine-sequence sequence (mh-seq-msgs old-seq))
1010 (rplaca old-seq new-name)))
1011
1012 (provide 'mh-seq)
1013
1014 ;; Local Variables:
1015 ;; indent-tabs-mode: nil
1016 ;; sentence-end-double-space: nil
1017 ;; End:
1018
1019 ;; arch-tag: 8e952711-01a2-485b-bf21-c9e3ad4de942
1020 ;;; mh-seq.el ends here