]> code.delx.au - gnu-emacs/blob - lisp/mh-e/mh-seq.el
Update FSF's address.
[gnu-emacs] / lisp / mh-e / mh-seq.el
1 ;;; mh-seq.el --- MH-E sequences support
2
3 ;; Copyright (C) 1993, 1995,
4 ;; 2001, 2002, 2003, 2004, 2005 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 2, or (at your option)
16 ;; 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; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
27
28 ;;; Commentary:
29 ;;
30 ;; This tries to implement the algorithm described at:
31 ;; http://www.jwz.org/doc/threading.html
32 ;; It is also a start to implementing the IMAP Threading extension RFC. The
33 ;; implementation lacks the reference and subject canonicalization of the
34 ;; RFC.
35 ;;
36 ;; In the presentation buffer, children messages are shown indented with
37 ;; either [ ] or < > around them. Square brackets ([ ]) denote that the
38 ;; algorithm can point out some headers which when taken together implies
39 ;; that the unindented message is an ancestor of the indented message. If
40 ;; no such proof exists then angles (< >) are used.
41 ;;
42 ;; Some issues and problems are as follows:
43 ;;
44 ;; (1) Scan truncates the fields at length 512. So longer references:
45 ;; headers get mutilated. The same kind of MH format string works when
46 ;; composing messages. Is there a way to avoid this? My scan command
47 ;; is as follows:
48 ;; scan +folder -width 10000 \
49 ;; -format "%(msg)\n%{message-id}\n%{references}\n%{subject}\n"
50 ;; I would really appreciate it if someone would help me with this.
51 ;;
52 ;; (2) Implement heuristics to recognize message identifiers in
53 ;; In-Reply-To: header. Right now it just assumes that the last text
54 ;; between angles (< and >) is the message identifier. There is the
55 ;; chance that this will incorrectly use an email address like a
56 ;; message identifier.
57 ;;
58 ;; (3) Error checking of found message identifiers should be done.
59 ;;
60 ;; (4) Since this breaks the assumption that message indices increase as
61 ;; one goes down the buffer, the binary search based mh-goto-msg
62 ;; doesn't work. I have a simpler replacement which may be less
63 ;; efficient.
64 ;;
65 ;; (5) Better canonicalizing for message identifier and subject strings.
66 ;;
67
68 ;; Internal support for MH-E package.
69
70 ;;; Change Log:
71
72 ;;; Code:
73
74 (eval-when-compile (require 'mh-acros))
75 (mh-require-cl)
76 (require 'mh-e)
77
78 ;; Shush the byte-compiler
79 (defvar tool-bar-mode)
80
81 ;;; Data structures (used in message threading)...
82 (mh-defstruct (mh-thread-message (:conc-name mh-message-)
83 (:constructor mh-thread-make-message))
84 (id nil)
85 (references ())
86 (subject "")
87 (subject-re-p nil))
88
89 (mh-defstruct (mh-thread-container (:conc-name mh-container-)
90 (:constructor mh-thread-make-container))
91 message parent children
92 (real-child-p t))
93
94
95 ;;; Internal variables:
96 (defvar mh-last-seq-used nil
97 "Name of seq to which a msg was last added.")
98
99 (defvar mh-non-seq-mode-line-annotation nil
100 "Saved value of `mh-mode-line-annotation' when narrowed to a seq.")
101
102 ;;; Maps and hashes...
103 (defvar mh-thread-id-hash nil
104 "Hashtable used to canonicalize message identifiers.")
105 (defvar mh-thread-subject-hash nil
106 "Hashtable used to canonicalize subject strings.")
107 (defvar mh-thread-id-table nil
108 "Thread ID table maps from message identifiers to message containers.")
109 (defvar mh-thread-id-index-map nil
110 "Table to look up message index number from message identifier.")
111 (defvar mh-thread-index-id-map nil
112 "Table to look up message identifier from message index.")
113 (defvar mh-thread-scan-line-map nil
114 "Map of message index to various parts of the scan line.")
115 (defvar mh-thread-scan-line-map-stack nil
116 "Old map of message index to various parts of the scan line.
117 This is the original map that is stored when the folder is narrowed.")
118 (defvar mh-thread-subject-container-hash nil
119 "Hashtable used to group messages by subject.")
120 (defvar mh-thread-duplicates nil
121 "Hashtable used to associate messages with the same message identifier.")
122 (defvar mh-thread-history ()
123 "Variable to remember the transformations to the thread tree.
124 When new messages are added, these transformations are rewound, then the
125 links are added from the newly seen messages. Finally the transformations are
126 redone to get the new thread tree. This makes incremental threading easier.")
127 (defvar mh-thread-body-width nil
128 "Width of scan substring that contains subject and body of message.")
129
130 (make-variable-buffer-local 'mh-thread-id-hash)
131 (make-variable-buffer-local 'mh-thread-subject-hash)
132 (make-variable-buffer-local 'mh-thread-id-table)
133 (make-variable-buffer-local 'mh-thread-id-index-map)
134 (make-variable-buffer-local 'mh-thread-index-id-map)
135 (make-variable-buffer-local 'mh-thread-scan-line-map)
136 (make-variable-buffer-local 'mh-thread-scan-line-map-stack)
137 (make-variable-buffer-local 'mh-thread-subject-container-hash)
138 (make-variable-buffer-local 'mh-thread-duplicates)
139 (make-variable-buffer-local 'mh-thread-history)
140
141 ;;;###mh-autoload
142 (defun mh-delete-seq (sequence)
143 "Delete the SEQUENCE."
144 (interactive (list (mh-read-seq-default "Delete" t)))
145 (let ((msg-list (mh-seq-to-msgs sequence))
146 (internal-flag (mh-internal-seq sequence))
147 (folders-changed (list mh-current-folder)))
148 (mh-iterate-on-range msg sequence
149 (mh-remove-sequence-notation msg internal-flag))
150 (mh-undefine-sequence sequence '("all"))
151 (mh-delete-seq-locally sequence)
152 (when mh-index-data
153 (setq folders-changed
154 (append folders-changed
155 (mh-index-delete-from-sequence sequence msg-list))))
156 (when (and (eq sequence mh-unseen-seq) (mh-speed-flists-active-p))
157 (apply #'mh-speed-flists t folders-changed))))
158
159 ;; Avoid compiler warnings
160 (defvar view-exit-action)
161
162 ;;;###mh-autoload
163 (defun mh-list-sequences ()
164 "List the sequences defined in the folder being visited."
165 (interactive)
166 (let ((folder mh-current-folder)
167 (temp-buffer mh-sequences-buffer)
168 (seq-list mh-seq-list)
169 (max-len 0))
170 (with-output-to-temp-buffer temp-buffer
171 (save-excursion
172 (set-buffer temp-buffer)
173 (erase-buffer)
174 (message "Listing sequences ...")
175 (insert "Sequences in folder " folder ":\n")
176 (let ((seq-list seq-list))
177 (while seq-list
178 (setq max-len
179 (max (length (symbol-name (mh-seq-name (pop seq-list))))
180 max-len)))
181 (setq max-len (+ 2 max-len)))
182 (while seq-list
183 (let ((name (mh-seq-name (car seq-list)))
184 (sorted-seq-msgs
185 (mh-coalesce-msg-list
186 (sort (copy-sequence (mh-seq-msgs (car seq-list))) '<)))
187 name-spec)
188 (insert (setq name-spec (format (format "%%%ss:" max-len) name)))
189 (while sorted-seq-msgs
190 (let ((next-element (format " %s" (pop sorted-seq-msgs))))
191 (when (>= (+ (current-column) (length next-element))
192 (window-width))
193 (insert "\n")
194 (insert (format (format "%%%ss" (length name-spec)) "")))
195 (insert next-element)))
196 (insert "\n"))
197 (setq seq-list (cdr seq-list)))
198 (goto-char (point-min))
199 (view-mode-enter)
200 (setq view-exit-action 'kill-buffer)
201 (message "Listing sequences...done")))))
202
203 ;;;###mh-autoload
204 (defun mh-msg-is-in-seq (message)
205 "Display the sequences in which the current message appears.
206 Use a prefix argument to display the sequences in which another MESSAGE
207 appears."
208 (interactive "P")
209 (if (not message)
210 (setq message (mh-get-msg-num t)))
211 (let* ((dest-folder (loop for seq in mh-refile-list
212 when (member message (cdr seq)) return (car seq)
213 finally return nil))
214 (deleted-flag (unless dest-folder (member message mh-delete-list))))
215 (message "Message %d%s is in sequences: %s"
216 message
217 (cond (dest-folder (format " (to be refiled to %s)" dest-folder))
218 (deleted-flag (format " (to be deleted)"))
219 (t ""))
220 (mapconcat 'concat
221 (mh-list-to-string (mh-seq-containing-msg message t))
222 " "))))
223
224 ;; Avoid compiler warning
225 (defvar tool-bar-map)
226
227 ;;;###mh-autoload
228 (defun mh-narrow-to-seq (sequence)
229 "Restrict display of this folder to just messages in SEQUENCE.
230 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
231 (interactive (list (mh-read-seq "Narrow to" t)))
232 (with-mh-folder-updating (t)
233 (cond ((mh-seq-to-msgs sequence)
234 (mh-remove-all-notation)
235 (let ((eob (point-max))
236 (msg-at-cursor (mh-get-msg-num nil)))
237 (push mh-thread-scan-line-map mh-thread-scan-line-map-stack)
238 (setq mh-thread-scan-line-map (make-hash-table :test #'eql))
239 (mh-copy-seq-to-eob sequence)
240 (push (buffer-substring-no-properties (point-min) eob)
241 mh-folder-view-stack)
242 (delete-region (point-min) eob)
243 (mh-notate-deleted-and-refiled)
244 (mh-notate-cur)
245 (when msg-at-cursor (mh-goto-msg msg-at-cursor t t))
246 (make-variable-buffer-local 'mh-non-seq-mode-line-annotation)
247 (setq mh-non-seq-mode-line-annotation mh-mode-line-annotation)
248 (setq mh-mode-line-annotation (symbol-name sequence))
249 (mh-make-folder-mode-line)
250 (mh-recenter nil)
251 (when (and (boundp 'tool-bar-mode) tool-bar-mode)
252 (set (make-local-variable 'tool-bar-map)
253 mh-folder-seq-tool-bar-map)
254 (when (buffer-live-p (get-buffer mh-show-buffer))
255 (save-excursion
256 (set-buffer (get-buffer mh-show-buffer))
257 (set (make-local-variable 'tool-bar-map)
258 mh-show-seq-tool-bar-map))))
259 (push 'widen mh-view-ops)))
260 (t
261 (error "No messages in sequence `%s'" (symbol-name sequence))))))
262
263 ;;;###mh-autoload
264 (defun mh-put-msg-in-seq (range sequence)
265 "Add RANGE to SEQUENCE.
266
267 Check the documentation of `mh-interactive-range' to see how RANGE is read in
268 interactive use."
269 (interactive (list (mh-interactive-range "Add messages from")
270 (mh-read-seq-default "Add to" nil)))
271 (unless (mh-valid-seq-p sequence)
272 (error "Can't put message in invalid sequence `%s'" sequence))
273 (let* ((internal-seq-flag (mh-internal-seq sequence))
274 (original-msgs (mh-seq-msgs (mh-find-seq sequence)))
275 (folders (list mh-current-folder))
276 (msg-list (mh-range-to-msg-list range)))
277 (mh-add-msgs-to-seq msg-list sequence nil t)
278 (mh-iterate-on-range m range
279 (unless (memq m original-msgs)
280 (mh-add-sequence-notation m internal-seq-flag)))
281 (if (not internal-seq-flag)
282 (setq mh-last-seq-used sequence))
283 (when mh-index-data
284 (setq folders
285 (append folders (mh-index-add-to-sequence sequence msg-list))))
286 (when (and (eq sequence mh-unseen-seq) (mh-speed-flists-active-p))
287 (apply #'mh-speed-flists t folders))))
288
289 (defun mh-valid-view-change-operation-p (op)
290 "Check if the view change operation can be performed.
291 OP is one of 'widen and 'unthread."
292 (cond ((eq (car mh-view-ops) op)
293 (pop mh-view-ops))
294 (t nil)))
295
296 ;;;###mh-autoload
297 (defun mh-widen (&optional all-flag)
298 "Restore the previous limit.
299 If optional prefix argument ALL-FLAG is non-nil, remove all limits."
300 (interactive "P")
301 (let ((msg (mh-get-msg-num nil)))
302 (when mh-folder-view-stack
303 (cond (all-flag
304 (while (cdr mh-view-ops)
305 (setq mh-view-ops (cdr mh-view-ops)))
306 (when (eq (car mh-view-ops) 'widen)
307 (setq mh-view-ops (cdr mh-view-ops))))
308 ((mh-valid-view-change-operation-p 'widen) nil)
309 ((memq 'widen mh-view-ops)
310 (while (not (eq (car mh-view-ops) 'widen))
311 (setq mh-view-ops (cdr mh-view-ops)))
312 (setq mh-view-ops (cdr mh-view-ops)))
313 (t (error "Widening is not applicable")))
314 ;; If ALL-FLAG is non-nil then rewind stacks
315 (when all-flag
316 (while (cdr mh-thread-scan-line-map-stack)
317 (setq mh-thread-scan-line-map-stack
318 (cdr mh-thread-scan-line-map-stack)))
319 (while (cdr mh-folder-view-stack)
320 (setq mh-folder-view-stack (cdr mh-folder-view-stack))))
321 (setq mh-thread-scan-line-map (pop mh-thread-scan-line-map-stack))
322 (with-mh-folder-updating (t)
323 (delete-region (point-min) (point-max))
324 (insert (pop mh-folder-view-stack))
325 (mh-remove-all-notation)
326 (setq mh-mode-line-annotation mh-non-seq-mode-line-annotation)
327 (mh-make-folder-mode-line))
328 (if msg
329 (mh-goto-msg msg t t))
330 (mh-notate-deleted-and-refiled)
331 (mh-notate-user-sequences)
332 (mh-notate-cur)
333 (mh-recenter nil)))
334 (when (and (null mh-folder-view-stack) (boundp 'tool-bar-mode) tool-bar-mode)
335 (set (make-local-variable 'tool-bar-map) mh-folder-tool-bar-map)
336 (when (buffer-live-p (get-buffer mh-show-buffer))
337 (save-excursion
338 (set-buffer (get-buffer mh-show-buffer))
339 (set (make-local-variable 'tool-bar-map) mh-show-tool-bar-map)))))
340
341 ;; FIXME? We may want to clear all notations and add one for current-message
342 ;; and process user sequences.
343 ;;;###mh-autoload
344 (defun mh-notate-deleted-and-refiled ()
345 "Notate messages marked for deletion or refiling.
346 Messages to be deleted are given by `mh-delete-list' while messages to be
347 refiled are present in `mh-refile-list'."
348 (let ((refiled-hash (make-hash-table))
349 (deleted-hash (make-hash-table)))
350 (dolist (msg mh-delete-list)
351 (setf (gethash msg deleted-hash) t))
352 (dolist (dest-msg-list mh-refile-list)
353 (dolist (msg (cdr dest-msg-list))
354 (setf (gethash msg refiled-hash) t)))
355 (mh-iterate-on-messages-in-region msg (point-min) (point-max)
356 (cond ((gethash msg refiled-hash)
357 (mh-notate nil mh-note-refiled mh-cmd-note))
358 ((gethash msg deleted-hash)
359 (mh-notate nil mh-note-deleted mh-cmd-note))))))
360
361 \f
362
363 ;;; Commands to manipulate sequences. Sequences are stored in an alist
364 ;;; of the form:
365 ;;; ((seq-name msgs ...) (seq-name msgs ...) ...)
366
367 (defvar mh-sequence-history ())
368
369 ;;;###mh-autoload
370 (defun mh-read-seq-default (prompt not-empty)
371 "Read and return sequence name with default narrowed or previous sequence.
372 PROMPT is the prompt to use when reading. If NOT-EMPTY is non-nil then a
373 non-empty sequence is read."
374 (mh-read-seq prompt not-empty
375 (or mh-last-seq-used
376 (car (mh-seq-containing-msg (mh-get-msg-num nil) nil)))))
377
378 (defun mh-read-seq (prompt not-empty &optional default)
379 "Read and return a sequence name.
380 Prompt with PROMPT, raise an error if the sequence is empty and the NOT-EMPTY
381 flag is non-nil, and supply an optional DEFAULT sequence. A reply of '%'
382 defaults to the first sequence containing the current message."
383 (let* ((input (completing-read (format "%s %s %s" prompt "sequence:"
384 (if default
385 (format "[%s] " default)
386 ""))
387 (mh-seq-names mh-seq-list)
388 nil nil nil 'mh-sequence-history))
389 (seq (cond ((equal input "%")
390 (car (mh-seq-containing-msg (mh-get-msg-num t) nil)))
391 ((equal input "") default)
392 (t (intern input))))
393 (msgs (mh-seq-to-msgs seq)))
394 (if (and (null msgs) not-empty)
395 (error "No messages in sequence `%s'" seq))
396 seq))
397
398 ;;; Functions to read ranges with completion...
399 (defvar mh-range-seq-names)
400 (defvar mh-range-history ())
401 (defvar mh-range-completion-map (copy-keymap minibuffer-local-completion-map))
402 (define-key mh-range-completion-map " " 'self-insert-command)
403
404 (defun mh-range-completion-function (string predicate flag)
405 "Programmable completion of message ranges.
406 STRING is the user input that is to be completed. PREDICATE if non-nil is a
407 function used to filter the possible choices and FLAG determines whether the
408 completion is over."
409 (let* ((candidates mh-range-seq-names)
410 (last-char (and (not (equal string ""))
411 (aref string (1- (length string)))))
412 (last-word (cond ((null last-char) "")
413 ((memq last-char '(? ?- ?:)) "")
414 (t (car (last (split-string string "[ -:]+"))))))
415 (prefix (substring string 0 (- (length string) (length last-word)))))
416 (cond ((eq flag nil)
417 (let ((res (try-completion last-word candidates predicate)))
418 (cond ((null res) nil)
419 ((eq res t) t)
420 (t (concat prefix res)))))
421 ((eq flag t)
422 (all-completions last-word candidates predicate))
423 ((eq flag 'lambda)
424 (loop for x in candidates
425 when (equal x last-word) return t
426 finally return nil)))))
427
428 ;;;###mh-autoload
429 (defun mh-read-range (prompt &optional folder default
430 expand-flag ask-flag number-as-range-flag)
431 "Read a message range with PROMPT.
432
433 If FOLDER is non-nil then a range is read from that folder, otherwise use
434 `mh-current-folder'.
435
436 If DEFAULT is a string then use that as default range to return. If DEFAULT is
437 nil then ask user with default answer a range based on the sequences that seem
438 relevant. Finally if DEFAULT is t, try to avoid prompting the user. Unseen
439 messages, if present, are returned. If the folder has fewer than
440 `mh-large-folder' messages then \"all\" messages are returned. Finally as a
441 last resort prompt the user.
442
443 If EXPAND-FLAG is non-nil then a list of message numbers corresponding to the
444 input is returned. If this list is empty then an error is raised. If
445 EXPAND-FLAG is nil just return the input string. In this case we don't check
446 if the range is empty.
447
448 If ASK-FLAG is non-nil, then the user is always queried for a range of
449 messages. If ASK-FLAG is nil, then the function checks if the unseen sequence
450 is non-empty. If that is the case, `mh-unseen-seq', or the list of messages in
451 it depending on the value of EXPAND, is returned. Otherwise if the folder has
452 fewer than `mh-large-folder' messages then the list of messages corresponding
453 to \"all\" is returned. If neither of the above holds then as a last resort
454 the user is queried for a range of messages.
455
456 If NUMBER-AS-RANGE-FLAG is non-nil, then if a number, N is read as input, it
457 is interpreted as the range \"last:N\".
458
459 This function replaces the existing function `mh-read-msg-range'. Calls to:
460 (mh-read-msg-range folder flag)
461 should be replaced with:
462 (mh-read-range \"Suitable prompt\" folder t nil flag
463 mh-interpret-number-as-range-flag)"
464 (setq default (or default mh-last-seq-used
465 (car (mh-seq-containing-msg (mh-get-msg-num nil) t)))
466 prompt (format "%s range" prompt))
467 (let* ((folder (or folder mh-current-folder))
468 (default (cond ((or (eq default t) (stringp default)) default)
469 ((symbolp default) (symbol-name default))))
470 (guess (eq default t))
471 (counts (and guess (mh-folder-size folder)))
472 (unseen (and counts (> (cadr counts) 0)))
473 (large (and counts mh-large-folder (> (car counts) mh-large-folder)))
474 (str (cond ((and guess large
475 (setq default (format "last:%s" mh-large-folder)
476 prompt (format "%s (folder has %s messages)"
477 prompt (car counts)))
478 nil))
479 ((and guess (not large) (setq default "all") nil))
480 ((eq default nil) "")
481 (t (format "[%s] " default))))
482 (minibuffer-local-completion-map mh-range-completion-map)
483 (seq-list (if (eq folder mh-current-folder)
484 mh-seq-list
485 (mh-read-folder-sequences folder nil)))
486 (mh-range-seq-names
487 (append '(("first") ("last") ("all") ("prev") ("next"))
488 (mh-seq-names seq-list)))
489 (input (cond ((and (not ask-flag) unseen) (symbol-name mh-unseen-seq))
490 ((and (not ask-flag) (not large)) "all")
491 (t (completing-read (format "%s: %s" prompt str)
492 'mh-range-completion-function nil nil
493 nil 'mh-range-history default))))
494 msg-list)
495 (when (and number-as-range-flag
496 (string-match "^[ \t]*\\([0-9]+\\)[ \t]*$" input))
497 (setq input (concat "last:" (match-string 1 input))))
498 (cond ((not expand-flag) input)
499 ((assoc (intern input) seq-list)
500 (cdr (assoc (intern input) seq-list)))
501 ((setq msg-list (mh-translate-range folder input)) msg-list)
502 (t (error "No messages in range `%s'" input)))))
503
504 ;;;###mh-autoload
505 (defun mh-translate-range (folder expr)
506 "In FOLDER, translate the string EXPR to a list of messages numbers."
507 (save-excursion
508 (let ((strings (delete "" (split-string expr "[ \t\n]")))
509 (result ()))
510 (ignore-errors
511 (apply #'mh-exec-cmd-quiet nil "mhpath" folder strings)
512 (set-buffer mh-temp-buffer)
513 (goto-char (point-min))
514 (while (re-search-forward "/\\([0-9]*\\)$" nil t)
515 (push (car (read-from-string (match-string 1))) result))
516 (nreverse result)))))
517
518 (defun mh-seq-names (seq-list)
519 "Return an alist containing the names of the SEQ-LIST."
520 (mapcar (lambda (entry) (list (symbol-name (mh-seq-name entry))))
521 seq-list))
522
523 ;;;###mh-autoload
524 (defun mh-rename-seq (sequence new-name)
525 "Rename SEQUENCE to have NEW-NAME."
526 (interactive (list (mh-read-seq "Old" t)
527 (intern (read-string "New sequence name: "))))
528 (let ((old-seq (mh-find-seq sequence)))
529 (or old-seq
530 (error "Sequence %s does not exist" sequence))
531 ;; create new sequence first, since it might raise an error.
532 (mh-define-sequence new-name (mh-seq-msgs old-seq))
533 (mh-undefine-sequence sequence (mh-seq-msgs old-seq))
534 (rplaca old-seq new-name)))
535
536 ;;;###mh-autoload
537 (defun mh-notate-cur ()
538 "Mark the MH sequence cur.
539 In addition to notating the current message with `mh-note-cur' the function
540 uses `overlay-arrow-position' to put a marker in the fringe."
541 (let ((cur (car (mh-seq-to-msgs 'cur))))
542 (when (and cur (mh-goto-msg cur t t))
543 (beginning-of-line)
544 (when (looking-at mh-scan-good-msg-regexp)
545 (mh-notate nil mh-note-cur mh-cmd-note))
546 (setq mh-arrow-marker (set-marker mh-arrow-marker (point)))
547 (setq overlay-arrow-position mh-arrow-marker))))
548
549 ;;;###mh-autoload
550 (defun mh-add-to-sequence (seq msgs)
551 "The sequence SEQ is augmented with the messages in MSGS."
552 ;; Add to a SEQUENCE each message the list of MSGS.
553 (if (and (mh-valid-seq-p seq) (not (mh-folder-name-p seq)))
554 (if msgs
555 (apply 'mh-exec-cmd "mark" mh-current-folder "-add"
556 "-sequence" (symbol-name seq)
557 (mh-coalesce-msg-list msgs)))))
558
559 (defvar mh-thread-last-ancestor)
560
561 (defun mh-copy-seq-to-eob (seq)
562 "Copy SEQ to the end of the buffer."
563 ;; It is quite involved to write something which will work at any place in
564 ;; the buffer, so we will write something which works only at the end of
565 ;; the buffer. If we ever need to insert sequences in the middle of the
566 ;; buffer, this will need to be fixed.
567 (save-excursion
568 (let* ((msgs (mh-seq-to-msgs seq))
569 (coalesced-msgs (mh-coalesce-msg-list msgs)))
570 (goto-char (point-max))
571 (save-restriction
572 (narrow-to-region (point) (point))
573 (mh-regenerate-headers coalesced-msgs t)
574 (cond ((memq 'unthread mh-view-ops)
575 ;; Populate restricted scan-line map
576 (mh-remove-all-notation)
577 (mh-iterate-on-range msg (cons (point-min) (point-max))
578 (setf (gethash msg mh-thread-scan-line-map)
579 (mh-thread-parse-scan-line)))
580 ;; Remove scan lines and read results from pre-computed tree
581 (delete-region (point-min) (point-max))
582 (mh-thread-print-scan-lines
583 (mh-thread-generate mh-current-folder ()))
584 (mh-notate-user-sequences))
585 (mh-index-data
586 (mh-index-insert-folder-headers)))))))
587
588 ;;;###mh-autoload
589 (defmacro mh-iterate-on-messages-in-region (var begin end &rest body)
590 "Iterate over region.
591 VAR is bound to the message on the current line as we loop starting from BEGIN
592 till END. In each step BODY is executed.
593
594 If VAR is nil then the loop is executed without any binding."
595 (unless (symbolp var)
596 (error "Can not bind the non-symbol %s" var))
597 (let ((binding-needed-flag var))
598 `(save-excursion
599 (goto-char ,begin)
600 (beginning-of-line)
601 (while (and (<= (point) ,end) (not (eobp)))
602 (when (looking-at mh-scan-valid-regexp)
603 (let ,(if binding-needed-flag `((,var (mh-get-msg-num t))) ())
604 ,@body))
605 (forward-line 1)))))
606
607 (put 'mh-iterate-on-messages-in-region 'lisp-indent-hook 'defun)
608
609 ;;;###mh-autoload
610 (defmacro mh-iterate-on-range (var range &rest body)
611 "Iterate an operation over a region or sequence.
612
613 VAR is bound to each message in turn in a loop over RANGE, which can be a
614 message number, a list of message numbers, a sequence, a region in a cons
615 cell, or a MH range (something like last:20) in a string. In each iteration,
616 BODY is executed.
617
618 The parameter RANGE is usually created with `mh-interactive-range'
619 in order to provide a uniform interface to MH-E functions."
620 (unless (symbolp var)
621 (error "Can not bind the non-symbol %s" var))
622 (let ((binding-needed-flag var)
623 (msgs (make-symbol "msgs"))
624 (seq-hash-table (make-symbol "seq-hash-table")))
625 `(cond ((numberp ,range)
626 (when (mh-goto-msg ,range t t)
627 (let ,(if binding-needed-flag `((,var ,range)) ())
628 ,@body)))
629 ((and (consp ,range)
630 (numberp (car ,range)) (numberp (cdr ,range)))
631 (mh-iterate-on-messages-in-region ,var
632 (car ,range) (cdr ,range)
633 ,@body))
634 (t (let ((,msgs (cond ((and ,range (symbolp ,range))
635 (mh-seq-to-msgs ,range))
636 ((stringp ,range)
637 (mh-translate-range mh-current-folder
638 ,range))
639 (t ,range)))
640 (,seq-hash-table (make-hash-table)))
641 (dolist (msg ,msgs)
642 (setf (gethash msg ,seq-hash-table) t))
643 (mh-iterate-on-messages-in-region v (point-min) (point-max)
644 (when (gethash v ,seq-hash-table)
645 (let ,(if binding-needed-flag `((,var v)) ())
646 ,@body))))))))
647
648 (put 'mh-iterate-on-range 'lisp-indent-hook 'defun)
649
650 ;;;###mh-autoload
651 (defun mh-range-to-msg-list (range)
652 "Return a list of messages for RANGE.
653 RANGE can be a message number, a list of message numbers, a sequence, or
654 a region in a cons cell."
655 (let (msg-list)
656 (mh-iterate-on-range msg range
657 (push msg msg-list))
658 (nreverse msg-list)))
659
660 ;;;###mh-autoload
661 (defun mh-interactive-range (range-prompt &optional default)
662 "Return interactive specification for message, sequence, range or region.
663 By convention, the name of this argument is RANGE.
664
665 If variable `transient-mark-mode' is non-nil and the mark is active, then this
666 function returns a cons-cell of the region.
667
668 If optional prefix argument is provided, then prompt for message range with
669 RANGE-PROMPT. A list of messages in that range is returned.
670
671 If a MH range is given, say something like last:20, then a list containing
672 the messages in that range is returned.
673
674 If DEFAULT non-nil then it is returned.
675
676 Otherwise, the message number at point is returned.
677
678 This function is usually used with `mh-iterate-on-range' in order to provide
679 a uniform interface to MH-E functions."
680 (cond ((mh-mark-active-p t) (cons (region-beginning) (region-end)))
681 (current-prefix-arg (mh-read-range range-prompt nil nil t t))
682 (default default)
683 (t (mh-get-msg-num t))))
684
685 \f
686
687 ;;; Commands to handle new 'subject sequence.
688 ;;; Or "Poor man's threading" by psg.
689
690 ;;; XXX: The function mh-subject-to-sequence-unthreaded uses the magic number
691 ;;; 41 for the max size of the subject part. Avoiding this would be desirable.
692 (defun mh-subject-to-sequence (all)
693 "Put all following messages with same subject in sequence 'subject.
694 If arg ALL is t, move to beginning of folder buffer to collect all messages.
695 If arg ALL is nil, collect only messages fron current one on forward.
696
697 Return number of messages put in the sequence:
698
699 nil -> there was no subject line.
700 0 -> there were no later messages with the same subject (sequence not made)
701 >1 -> the total number of messages including current one."
702 (if (memq 'unthread mh-view-ops)
703 (mh-subject-to-sequence-threaded all)
704 (mh-subject-to-sequence-unthreaded all)))
705
706 (defun mh-subject-to-sequence-unthreaded (all)
707 "Put all following messages with same subject in sequence 'subject.
708 This function only works with an unthreaded folder. If arg ALL is t, move to
709 beginning of folder buffer to collect all messages. If arg ALL is nil, collect
710 only messages fron current one on forward.
711
712 Return number of messages put in the sequence:
713
714 nil -> there was no subject line.
715 0 -> there were no later messages with the same subject (sequence not made)
716 >1 -> the total number of messages including current one."
717 (if (not (eq major-mode 'mh-folder-mode))
718 (error "Not in a folder buffer"))
719 (save-excursion
720 (beginning-of-line)
721 (if (or (not (looking-at mh-scan-subject-regexp))
722 (not (match-string 3))
723 (string-equal "" (match-string 3)))
724 (progn (message "No subject line")
725 nil)
726 (let ((subject (match-string-no-properties 3))
727 (list))
728 (if (> (length subject) 41)
729 (setq subject (substring subject 0 41)))
730 (save-excursion
731 (if all
732 (goto-char (point-min)))
733 (while (re-search-forward mh-scan-subject-regexp nil t)
734 (let ((this-subject (match-string-no-properties 3)))
735 (if (> (length this-subject) 41)
736 (setq this-subject (substring this-subject 0 41)))
737 (if (string-equal this-subject subject)
738 (setq list (cons (mh-get-msg-num t) list))))))
739 (cond
740 (list
741 ;; If we created a new sequence, add the initial message to it too.
742 (if (not (member (mh-get-msg-num t) list))
743 (setq list (cons (mh-get-msg-num t) list)))
744 (if (assoc 'subject mh-seq-list) (mh-delete-seq 'subject))
745 ;; sort the result into a sequence
746 (let ((sorted-list (sort (copy-sequence list) 'mh-lessp)))
747 (while sorted-list
748 (mh-add-msgs-to-seq (car sorted-list) 'subject nil)
749 (setq sorted-list (cdr sorted-list)))
750 (safe-length list)))
751 (t
752 0))))))
753
754 (defun mh-subject-to-sequence-threaded (all)
755 "Put all messages with the same subject in the 'subject sequence.
756 This function works when the folder is threaded. In this situation the subject
757 could get truncated and so the normal matching doesn't work.
758
759 The parameter ALL is non-nil then all the messages in the buffer are
760 considered, otherwise only the messages after the current one are taken into
761 account."
762 (let* ((cur (mh-get-msg-num nil))
763 (subject (mh-thread-find-msg-subject cur))
764 region msgs)
765 (if (null subject)
766 (and (message "No subject line") nil)
767 (setq region (cons (if all (point-min) (point)) (point-max)))
768 (mh-iterate-on-range msg region
769 (when (eq (mh-thread-find-msg-subject msg) subject)
770 (push msg msgs)))
771 (setq msgs (sort msgs #'mh-lessp))
772 (if (null msgs)
773 0
774 (when (assoc 'subject mh-seq-list)
775 (mh-delete-seq 'subject))
776 (mh-add-msgs-to-seq msgs 'subject)
777 (length msgs)))))
778
779 (defun mh-thread-find-msg-subject (msg)
780 "Find canonicalized subject of MSG.
781 This function can only be used the folder is threaded."
782 (ignore-errors
783 (mh-message-subject
784 (mh-container-message (gethash (gethash msg mh-thread-index-id-map)
785 mh-thread-id-table)))))
786
787 (defun mh-edit-pick-expr (default)
788 "With prefix arg edit a pick expression.
789 If no prefix arg is given, then return DEFAULT."
790 (let ((default-string (loop for x in default concat (format " %s" x))))
791 (if (or current-prefix-arg (equal default-string ""))
792 (mh-pick-args-list (read-string "Pick expression: "
793 default-string))
794 default)))
795
796 (defun mh-pick-args-list (s)
797 "Form list by grouping elements in string S suitable for pick arguments.
798 For example, the string \"-subject a b c -from Joe User <user@domain.com>\"
799 is converted to (\"-subject\" \"a b c\" \"-from\"
800 \"Joe User <user@domain.com>\""
801 (let ((full-list (split-string s))
802 current-arg collection arg-list)
803 (while full-list
804 (setq current-arg (car full-list))
805 (if (null (string-match "^-" current-arg))
806 (setq collection
807 (if (null collection)
808 current-arg
809 (format "%s %s" collection current-arg)))
810 (when collection
811 (setq arg-list (append arg-list (list collection)))
812 (setq collection nil))
813 (setq arg-list (append arg-list (list current-arg))))
814 (setq full-list (cdr full-list)))
815 (when collection
816 (setq arg-list (append arg-list (list collection))))
817 arg-list))
818
819 ;;;###mh-autoload
820 (defun mh-narrow-to-subject (&optional pick-expr)
821 "Limit to messages with same subject.
822 With a prefix argument, edit PICK-EXPR.
823
824 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
825 (interactive
826 (list (mh-edit-pick-expr (mh-current-message-header-field 'subject))))
827 (mh-narrow-to-header-field 'subject pick-expr))
828
829 ;;;###mh-autoload
830 (defun mh-narrow-to-from (&optional pick-expr)
831 "Limit to messages with the same `From:' field.
832 With a prefix argument, edit PICK-EXPR.
833
834 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
835 (interactive
836 (list (mh-edit-pick-expr (mh-current-message-header-field 'from))))
837 (mh-narrow-to-header-field 'from pick-expr))
838
839 ;;;###mh-autoload
840 (defun mh-narrow-to-cc (&optional pick-expr)
841 "Limit to messages with the same `Cc:' field.
842 With a prefix argument, edit PICK-EXPR.
843
844 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
845 (interactive
846 (list (mh-edit-pick-expr (mh-current-message-header-field 'cc))))
847 (mh-narrow-to-header-field 'cc pick-expr))
848
849 ;;;###mh-autoload
850 (defun mh-narrow-to-to (&optional pick-expr)
851 "Limit to messages with the same `To:' field.
852 With a prefix argument, edit PICK-EXPR.
853
854 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
855 (interactive
856 (list (mh-edit-pick-expr (mh-current-message-header-field 'to))))
857 (mh-narrow-to-header-field 'to pick-expr))
858
859 (defun mh-narrow-to-header-field (header-field pick-expr)
860 "Limit to messages whose HEADER-FIELD match PICK-EXPR.
861 The MH command pick is used to do the match."
862 (let ((folder mh-current-folder)
863 (original (mh-coalesce-msg-list
864 (mh-range-to-msg-list (cons (point-min) (point-max)))))
865 (msg-list ()))
866 (with-temp-buffer
867 (apply #'mh-exec-cmd-output "pick" nil folder
868 (append original (list "-list") pick-expr))
869 (goto-char (point-min))
870 (while (not (eobp))
871 (let ((num (read-from-string
872 (buffer-substring (point) (line-end-position)))))
873 (when (numberp (car num)) (push (car num) msg-list))
874 (forward-line))))
875 (if (null msg-list)
876 (message "No matches")
877 (when (assoc 'header mh-seq-list) (mh-delete-seq 'header))
878 (mh-add-msgs-to-seq msg-list 'header)
879 (mh-narrow-to-seq 'header))))
880
881 (defun mh-current-message-header-field (header-field)
882 "Return a pick regexp to match HEADER-FIELD of the message at point."
883 (let ((num (mh-get-msg-num nil)))
884 (when num
885 (let ((folder mh-current-folder))
886 (with-temp-buffer
887 (insert-file-contents-literally (mh-msg-filename num folder))
888 (goto-char (point-min))
889 (when (search-forward "\n\n" nil t)
890 (narrow-to-region (point-min) (point)))
891 (let* ((field (or (message-fetch-field (format "%s" header-field))
892 ""))
893 (field-option (format "-%s" header-field))
894 (patterns (loop for x in (split-string field "[ ]*,[ ]*")
895 unless (equal x "")
896 collect (if (string-match "<\\(.*@.*\\)>" x)
897 (match-string 1 x)
898 x))))
899 (when patterns
900 (loop with accum = `(,field-option ,(car patterns))
901 for e in (cdr patterns)
902 do (setq accum `(,field-option ,e "-or" ,@accum))
903 finally return accum))))))))
904
905 ;;;###mh-autoload
906 (defun mh-narrow-to-range (range)
907 "Limit to messages in RANGE.
908
909 Check the documentation of `mh-interactive-range' to see how RANGE is read in
910 interactive use.
911
912 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
913 (interactive (list (mh-interactive-range "Narrow to")))
914 (when (assoc 'range mh-seq-list) (mh-delete-seq 'range))
915 (mh-add-msgs-to-seq (mh-range-to-msg-list range) 'range)
916 (mh-narrow-to-seq 'range))
917
918
919 ;;;###mh-autoload
920 (defun mh-delete-subject ()
921 "Mark all following messages with same subject to be deleted.
922 This puts the messages in a sequence named subject. You can undo the last
923 deletion marks using `mh-undo' with a prefix argument and then specifying the
924 subject sequence."
925 (interactive)
926 (let ((count (mh-subject-to-sequence nil)))
927 (cond
928 ((not count) ; No subject line, delete msg anyway
929 (mh-delete-msg (mh-get-msg-num t)))
930 ((= 0 count) ; No other msgs, delete msg anyway.
931 (message "No other messages with same Subject following this one")
932 (mh-delete-msg (mh-get-msg-num t)))
933 (t ; We have a subject sequence.
934 (message "Marked %d messages for deletion" count)
935 (mh-delete-msg 'subject)))))
936
937 ;;;###mh-autoload
938 (defun mh-delete-subject-or-thread ()
939 "Mark messages for deletion intelligently.
940 If the folder is threaded then `mh-thread-delete' is used to mark the current
941 message and all its descendants for deletion. Otherwise `mh-delete-subject' is
942 used to mark the current message and all messages following it with the same
943 subject for deletion."
944 (interactive)
945 (if (memq 'unthread mh-view-ops)
946 (mh-thread-delete)
947 (mh-delete-subject)))
948
949 ;;; Message threading:
950
951 (defmacro mh-thread-initialize-hash (var test)
952 "Initialize the hash table in VAR.
953 TEST is the test to use when creating a new hash table."
954 (unless (symbolp var) (error "Expected a symbol: %s" var))
955 `(if ,var (clrhash ,var) (setq ,var (make-hash-table :test ,test))))
956
957 (defun mh-thread-initialize ()
958 "Make new hash tables, or clear them if already present."
959 (mh-thread-initialize-hash mh-thread-id-hash #'equal)
960 (mh-thread-initialize-hash mh-thread-subject-hash #'equal)
961 (mh-thread-initialize-hash mh-thread-id-table #'eq)
962 (mh-thread-initialize-hash mh-thread-id-index-map #'eq)
963 (mh-thread-initialize-hash mh-thread-index-id-map #'eql)
964 (mh-thread-initialize-hash mh-thread-scan-line-map #'eql)
965 (mh-thread-initialize-hash mh-thread-subject-container-hash #'eq)
966 (mh-thread-initialize-hash mh-thread-duplicates #'eq)
967 (setq mh-thread-history ()))
968
969 (defsubst mh-thread-id-container (id)
970 "Given ID, return the corresponding container in `mh-thread-id-table'.
971 If no container exists then a suitable container is created and the id-table
972 is updated."
973 (when (not id)
974 (error "1"))
975 (or (gethash id mh-thread-id-table)
976 (setf (gethash id mh-thread-id-table)
977 (let ((message (mh-thread-make-message :id id)))
978 (mh-thread-make-container :message message)))))
979
980 (defsubst mh-thread-remove-parent-link (child)
981 "Remove parent link of CHILD if it exists."
982 (let* ((child-container (if (mh-thread-container-p child)
983 child (mh-thread-id-container child)))
984 (parent-container (mh-container-parent child-container)))
985 (when parent-container
986 (setf (mh-container-children parent-container)
987 (loop for elem in (mh-container-children parent-container)
988 unless (eq child-container elem) collect elem))
989 (setf (mh-container-parent child-container) nil))))
990
991 (defsubst mh-thread-add-link (parent child &optional at-end-p)
992 "Add links so that PARENT becomes a parent of CHILD.
993 Doesn't make any changes if CHILD is already an ancestor of PARENT. If
994 optional argument AT-END-P is non-nil, the CHILD is added to the end of the
995 children list of PARENT."
996 (let ((parent-container (cond ((null parent) nil)
997 ((mh-thread-container-p parent) parent)
998 (t (mh-thread-id-container parent))))
999 (child-container (if (mh-thread-container-p child)
1000 child (mh-thread-id-container child))))
1001 (when (and parent-container
1002 (not (mh-thread-ancestor-p child-container parent-container))
1003 (not (mh-thread-ancestor-p parent-container child-container)))
1004 (mh-thread-remove-parent-link child-container)
1005 (cond ((not at-end-p)
1006 (push child-container (mh-container-children parent-container)))
1007 ((null (mh-container-children parent-container))
1008 (push child-container (mh-container-children parent-container)))
1009 (t (let ((last-child (mh-container-children parent-container)))
1010 (while (cdr last-child)
1011 (setq last-child (cdr last-child)))
1012 (setcdr last-child (cons child-container nil)))))
1013 (setf (mh-container-parent child-container) parent-container))
1014 (unless parent-container
1015 (mh-thread-remove-parent-link child-container))))
1016
1017 (defun mh-thread-ancestor-p (ancestor successor)
1018 "Return t if ANCESTOR is really an ancestor of SUCCESSOR and nil otherwise.
1019 In the limit, the function returns t if ANCESTOR and SUCCESSOR are the same
1020 containers."
1021 (block nil
1022 (while successor
1023 (when (eq ancestor successor) (return t))
1024 (setq successor (mh-container-parent successor)))
1025 nil))
1026
1027 (defsubst mh-thread-get-message-container (message)
1028 "Return container which has MESSAGE in it.
1029 If there is no container present then a new container is allocated."
1030 (let* ((id (mh-message-id message))
1031 (container (gethash id mh-thread-id-table)))
1032 (cond (container (setf (mh-container-message container) message)
1033 container)
1034 (t (setf (gethash id mh-thread-id-table)
1035 (mh-thread-make-container :message message))))))
1036
1037 (defsubst mh-thread-get-message (id subject-re-p subject refs)
1038 "Return appropriate message.
1039 Otherwise update message already present to have the proper ID, SUBJECT-RE-P,
1040 SUBJECT and REFS fields."
1041 (let* ((container (gethash id mh-thread-id-table))
1042 (message (if container (mh-container-message container) nil)))
1043 (cond (message
1044 (setf (mh-message-subject-re-p message) subject-re-p)
1045 (setf (mh-message-subject message) subject)
1046 (setf (mh-message-id message) id)
1047 (setf (mh-message-references message) refs)
1048 message)
1049 (container
1050 (setf (mh-container-message container)
1051 (mh-thread-make-message :id id :references refs
1052 :subject subject
1053 :subject-re-p subject-re-p)))
1054 (t (let ((message (mh-thread-make-message :id id :references refs
1055 :subject-re-p subject-re-p
1056 :subject subject)))
1057 (prog1 message
1058 (mh-thread-get-message-container message)))))))
1059
1060 (defsubst mh-thread-canonicalize-id (id)
1061 "Produce canonical string representation for ID.
1062 This allows cheap string comparison with EQ."
1063 (or (and (equal id "") (copy-sequence ""))
1064 (gethash id mh-thread-id-hash)
1065 (setf (gethash id mh-thread-id-hash) id)))
1066
1067 (defsubst mh-thread-prune-subject (subject)
1068 "Prune leading Re:'s, Fwd:'s etc. and trailing (fwd)'s from SUBJECT.
1069 If the result after pruning is not the empty string then it is canonicalized
1070 so that subjects can be tested for equality with eq. This is done so that all
1071 the messages without a subject are not put into a single thread."
1072 (let ((case-fold-search t)
1073 (subject-pruned-flag nil))
1074 ;; Prune subject leader
1075 (while (or (string-match "^[ \t]*\\(re\\|fwd?\\)\\(\\[[0-9]*\\]\\)?:[ \t]*"
1076 subject)
1077 (string-match "^[ \t]*\\[[^\\]][ \t]*" subject))
1078 (setq subject-pruned-flag t)
1079 (setq subject (substring subject (match-end 0))))
1080 ;; Prune subject trailer
1081 (while (or (string-match "(fwd)$" subject)
1082 (string-match "[ \t]+$" subject))
1083 (setq subject-pruned-flag t)
1084 (setq subject (substring subject 0 (match-beginning 0))))
1085 ;; Canonicalize subject only if it is non-empty
1086 (cond ((equal subject "") (values subject subject-pruned-flag))
1087 (t (values
1088 (or (gethash subject mh-thread-subject-hash)
1089 (setf (gethash subject mh-thread-subject-hash) subject))
1090 subject-pruned-flag)))))
1091
1092 (defun mh-thread-container-subject (container)
1093 "Return the subject of CONTAINER.
1094 If CONTAINER is empty return the subject info of one of its children."
1095 (cond ((and (mh-container-message container)
1096 (mh-message-id (mh-container-message container)))
1097 (mh-message-subject (mh-container-message container)))
1098 (t (block nil
1099 (dolist (kid (mh-container-children container))
1100 (when (and (mh-container-message kid)
1101 (mh-message-id (mh-container-message kid)))
1102 (let ((kid-message (mh-container-message kid)))
1103 (return (mh-message-subject kid-message)))))
1104 (error "This can't happen!")))))
1105
1106 (defun mh-thread-rewind-pruning ()
1107 "Restore the thread tree to its state before pruning."
1108 (while mh-thread-history
1109 (let ((action (pop mh-thread-history)))
1110 (cond ((eq (car action) 'DROP)
1111 (mh-thread-remove-parent-link (cadr action))
1112 (mh-thread-add-link (caddr action) (cadr action)))
1113 ((eq (car action) 'PROMOTE)
1114 (let ((node (cadr action))
1115 (parent (caddr action))
1116 (children (cdddr action)))
1117 (dolist (child children)
1118 (mh-thread-remove-parent-link child)
1119 (mh-thread-add-link node child))
1120 (mh-thread-add-link parent node)))
1121 ((eq (car action) 'SUBJECT)
1122 (let ((node (cadr action)))
1123 (mh-thread-remove-parent-link node)
1124 (setf (mh-container-real-child-p node) t)))))))
1125
1126 (defun mh-thread-prune-containers (roots)
1127 "Prune empty containers in the containers ROOTS."
1128 (let ((dfs-ordered-nodes ())
1129 (work-list roots))
1130 (while work-list
1131 (let ((node (pop work-list)))
1132 (dolist (child (mh-container-children node))
1133 (push child work-list))
1134 (push node dfs-ordered-nodes)))
1135 (while dfs-ordered-nodes
1136 (let ((node (pop dfs-ordered-nodes)))
1137 (cond ((gethash (mh-message-id (mh-container-message node))
1138 mh-thread-id-index-map)
1139 ;; Keep it
1140 (setf (mh-container-children node)
1141 (mh-thread-sort-containers (mh-container-children node))))
1142 ((and (mh-container-children node)
1143 (or (null (cdr (mh-container-children node)))
1144 (mh-container-parent node)))
1145 ;; Promote kids
1146 (let ((children ()))
1147 (dolist (kid (mh-container-children node))
1148 (mh-thread-remove-parent-link kid)
1149 (mh-thread-add-link (mh-container-parent node) kid)
1150 (push kid children))
1151 (push `(PROMOTE ,node ,(mh-container-parent node) ,@children)
1152 mh-thread-history)
1153 (mh-thread-remove-parent-link node)))
1154 ((mh-container-children node)
1155 ;; Promote the first orphan to parent and add the other kids as
1156 ;; his children
1157 (setf (mh-container-children node)
1158 (mh-thread-sort-containers (mh-container-children node)))
1159 (let ((new-parent (car (mh-container-children node)))
1160 (other-kids (cdr (mh-container-children node))))
1161 (mh-thread-remove-parent-link new-parent)
1162 (dolist (kid other-kids)
1163 (mh-thread-remove-parent-link kid)
1164 (setf (mh-container-real-child-p kid) nil)
1165 (mh-thread-add-link new-parent kid t))
1166 (push `(PROMOTE ,node ,(mh-container-parent node)
1167 ,new-parent ,@other-kids)
1168 mh-thread-history)
1169 (mh-thread-remove-parent-link node)))
1170 (t
1171 ;; Drop it
1172 (push `(DROP ,node ,(mh-container-parent node))
1173 mh-thread-history)
1174 (mh-thread-remove-parent-link node)))))
1175 (let ((results ()))
1176 (maphash #'(lambda (k v)
1177 (declare (ignore k))
1178 (when (and (null (mh-container-parent v))
1179 (gethash (mh-message-id (mh-container-message v))
1180 mh-thread-id-index-map))
1181 (push v results)))
1182 mh-thread-id-table)
1183 (mh-thread-sort-containers results))))
1184
1185 (defun mh-thread-sort-containers (containers)
1186 "Sort a list of message CONTAINERS to be in ascending order wrt index."
1187 (sort containers
1188 #'(lambda (x y)
1189 (when (and (mh-container-message x) (mh-container-message y))
1190 (let* ((id-x (mh-message-id (mh-container-message x)))
1191 (id-y (mh-message-id (mh-container-message y)))
1192 (index-x (gethash id-x mh-thread-id-index-map))
1193 (index-y (gethash id-y mh-thread-id-index-map)))
1194 (and (integerp index-x) (integerp index-y)
1195 (< index-x index-y)))))))
1196
1197 (defsubst mh-thread-group-by-subject (roots)
1198 "Group the set of message containers, ROOTS based on subject.
1199 Bug: Check for and make sure that something without Re: is made the parent in
1200 preference to something that has it."
1201 (clrhash mh-thread-subject-container-hash)
1202 (let ((results ()))
1203 (dolist (root roots)
1204 (let* ((subject (mh-thread-container-subject root))
1205 (parent (gethash subject mh-thread-subject-container-hash)))
1206 (cond (parent (mh-thread-remove-parent-link root)
1207 (mh-thread-add-link parent root t)
1208 (setf (mh-container-real-child-p root) nil)
1209 (push `(SUBJECT ,root) mh-thread-history))
1210 (t
1211 (setf (gethash subject mh-thread-subject-container-hash) root)
1212 (push root results)))))
1213 (nreverse results)))
1214
1215 (defun mh-thread-process-in-reply-to (reply-to-header)
1216 "Extract message id's from REPLY-TO-HEADER.
1217 Ideally this should have some regexp which will try to guess if a string
1218 between < and > is a message id and not an email address. For now it will
1219 take the last string inside angles."
1220 (let ((end (mh-search-from-end ?> reply-to-header)))
1221 (when (numberp end)
1222 (let ((begin (mh-search-from-end ?< (substring reply-to-header 0 end))))
1223 (when (numberp begin)
1224 (list (substring reply-to-header begin (1+ end))))))))
1225
1226 (defun mh-thread-set-tables (folder)
1227 "Use the tables of FOLDER in current buffer."
1228 (flet ((mh-get-table (symbol)
1229 (save-excursion
1230 (set-buffer folder)
1231 (symbol-value symbol))))
1232 (setq mh-thread-id-hash (mh-get-table 'mh-thread-id-hash))
1233 (setq mh-thread-subject-hash (mh-get-table 'mh-thread-subject-hash))
1234 (setq mh-thread-id-table (mh-get-table 'mh-thread-id-table))
1235 (setq mh-thread-id-index-map (mh-get-table 'mh-thread-id-index-map))
1236 (setq mh-thread-index-id-map (mh-get-table 'mh-thread-index-id-map))
1237 (setq mh-thread-scan-line-map (mh-get-table 'mh-thread-scan-line-map))
1238 (setq mh-thread-subject-container-hash
1239 (mh-get-table 'mh-thread-subject-container-hash))
1240 (setq mh-thread-duplicates (mh-get-table 'mh-thread-duplicates))
1241 (setq mh-thread-history (mh-get-table 'mh-thread-history))))
1242
1243 (defsubst mh-thread-update-id-index-maps (id index)
1244 "Message with id, ID is the message in INDEX.
1245 The function also checks for duplicate messages (that is multiple messages
1246 with the same ID). These messages are put in the `mh-thread-duplicates' hash
1247 table."
1248 (let ((old-index (gethash id mh-thread-id-index-map)))
1249 (when old-index (push old-index (gethash id mh-thread-duplicates)))
1250 (setf (gethash id mh-thread-id-index-map) index)
1251 (setf (gethash index mh-thread-index-id-map) id)))
1252
1253 \f
1254
1255 ;;; Generate Threads...
1256
1257 (defvar mh-message-id-regexp "^<.*@.*>$"
1258 "Regexp to recognize whether a string is a message identifier.")
1259
1260 (defun mh-thread-generate (folder msg-list)
1261 "Scan FOLDER to get info for threading.
1262 Only information about messages in MSG-LIST are added to the tree."
1263 (with-temp-buffer
1264 (mh-thread-set-tables folder)
1265 (when msg-list
1266 (apply
1267 #'call-process (expand-file-name mh-scan-prog mh-progs) nil '(t nil) nil
1268 "-width" "10000" "-format"
1269 "%(msg)\n%{message-id}\n%{references}\n%{in-reply-to}\n%{subject}\n"
1270 folder (mapcar #'(lambda (x) (format "%s" x)) msg-list)))
1271 (goto-char (point-min))
1272 (let ((roots ())
1273 (case-fold-search t))
1274 (block nil
1275 (while (not (eobp))
1276 (block process-message
1277 (let* ((index-line
1278 (prog1 (buffer-substring (point) (line-end-position))
1279 (forward-line)))
1280 (index (car (read-from-string index-line)))
1281 (id (prog1 (buffer-substring (point) (line-end-position))
1282 (forward-line)))
1283 (refs (prog1 (buffer-substring (point) (line-end-position))
1284 (forward-line)))
1285 (in-reply-to (prog1 (buffer-substring (point)
1286 (line-end-position))
1287 (forward-line)))
1288 (subject (prog1
1289 (buffer-substring (point) (line-end-position))
1290 (forward-line)))
1291 (subject-re-p nil))
1292 (unless (gethash index mh-thread-scan-line-map)
1293 (return-from process-message))
1294 (unless (integerp index) (return)) ;Error message here
1295 (multiple-value-setq (subject subject-re-p)
1296 (mh-thread-prune-subject subject))
1297 (setq in-reply-to (mh-thread-process-in-reply-to in-reply-to))
1298 (setq refs (loop for x in (append (split-string refs) in-reply-to)
1299 when (string-match mh-message-id-regexp x)
1300 collect x))
1301 (setq id (mh-thread-canonicalize-id id))
1302 (mh-thread-update-id-index-maps id index)
1303 (setq refs (mapcar #'mh-thread-canonicalize-id refs))
1304 (mh-thread-get-message id subject-re-p subject refs)
1305 (do ((ancestors refs (cdr ancestors)))
1306 ((null (cdr ancestors))
1307 (when (car ancestors)
1308 (mh-thread-remove-parent-link id)
1309 (mh-thread-add-link (car ancestors) id)))
1310 (mh-thread-add-link (car ancestors) (cadr ancestors)))))))
1311 (maphash #'(lambda (k v)
1312 (declare (ignore k))
1313 (when (null (mh-container-parent v))
1314 (push v roots)))
1315 mh-thread-id-table)
1316 (setq roots (mh-thread-prune-containers roots))
1317 (prog1 (setq roots (mh-thread-group-by-subject roots))
1318 (let ((history mh-thread-history))
1319 (set-buffer folder)
1320 (setq mh-thread-history history))))))
1321
1322 ;;;###mh-autoload
1323 (defun mh-thread-inc (folder start-point)
1324 "Update thread tree for FOLDER.
1325 All messages after START-POINT are added to the thread tree."
1326 (mh-thread-rewind-pruning)
1327 (mh-remove-all-notation)
1328 (goto-char start-point)
1329 (let ((msg-list ()))
1330 (while (not (eobp))
1331 (let ((index (mh-get-msg-num nil)))
1332 (when (numberp index)
1333 (push index msg-list)
1334 (setf (gethash index mh-thread-scan-line-map)
1335 (mh-thread-parse-scan-line)))
1336 (forward-line)))
1337 (let ((thread-tree (mh-thread-generate folder msg-list))
1338 (buffer-read-only nil)
1339 (old-buffer-modified-flag (buffer-modified-p)))
1340 (delete-region (point-min) (point-max))
1341 (mh-thread-print-scan-lines thread-tree)
1342 (mh-notate-user-sequences)
1343 (mh-notate-deleted-and-refiled)
1344 (mh-notate-cur)
1345 (set-buffer-modified-p old-buffer-modified-flag))))
1346
1347 (defun mh-thread-generate-scan-lines (tree level)
1348 "Generate scan lines.
1349 TREE is the hierarchical tree of messages, SCAN-LINE-MAP maps message indices
1350 to the corresponding scan lines and LEVEL used to determine indentation of
1351 the message."
1352 (cond ((null tree) nil)
1353 ((mh-thread-container-p tree)
1354 (let* ((message (mh-container-message tree))
1355 (id (mh-message-id message))
1356 (index (gethash id mh-thread-id-index-map))
1357 (duplicates (gethash id mh-thread-duplicates))
1358 (new-level (+ level 2))
1359 (dupl-flag t)
1360 (force-angle-flag nil)
1361 (increment-level-flag nil))
1362 (dolist (scan-line (mapcar (lambda (x)
1363 (gethash x mh-thread-scan-line-map))
1364 (reverse (cons index duplicates))))
1365 (when scan-line
1366 (when (and dupl-flag (equal level 0)
1367 (mh-thread-ancestor-p mh-thread-last-ancestor tree))
1368 (setq level (+ level 2)
1369 new-level (+ new-level 2)
1370 force-angle-flag t))
1371 (when (equal level 0)
1372 (setq mh-thread-last-ancestor tree)
1373 (while (mh-container-parent mh-thread-last-ancestor)
1374 (setq mh-thread-last-ancestor
1375 (mh-container-parent mh-thread-last-ancestor))))
1376 (let* ((lev (if dupl-flag level new-level))
1377 (square-flag (or (and (mh-container-real-child-p tree)
1378 (not force-angle-flag)
1379 dupl-flag)
1380 (equal lev 0))))
1381 (insert (car scan-line)
1382 (format (format "%%%ss" lev) "")
1383 (if square-flag "[" "<")
1384 (cadr scan-line)
1385 (if square-flag "]" ">")
1386 (truncate-string-to-width
1387 (caddr scan-line) (- mh-thread-body-width lev))
1388 "\n"))
1389 (setq increment-level-flag t)
1390 (setq dupl-flag nil)))
1391 (unless increment-level-flag (setq new-level level))
1392 (dolist (child (mh-container-children tree))
1393 (mh-thread-generate-scan-lines child new-level))))
1394 (t (let ((nlevel (+ level 2)))
1395 (dolist (ch tree)
1396 (mh-thread-generate-scan-lines ch nlevel))))))
1397
1398 ;; Another and may be better approach would be to generate all the info from
1399 ;; the scan which generates the threading info. For now this will have to do.
1400 (defun mh-thread-parse-scan-line (&optional string)
1401 "Parse a scan line.
1402 If optional argument STRING is given then that is assumed to be the scan line.
1403 Otherwise uses the line at point as the scan line to parse."
1404 (let* ((string (or string
1405 (buffer-substring-no-properties (line-beginning-position)
1406 (line-end-position))))
1407 (address-start (+ mh-cmd-note mh-scan-field-from-start-offset))
1408 (body-start (+ mh-cmd-note mh-scan-field-from-end-offset))
1409 (first-string (substring string 0 address-start)))
1410 (list first-string
1411 (substring string address-start (- body-start 2))
1412 (substring string body-start)
1413 string)))
1414
1415 ;;;###mh-autoload
1416 (defun mh-thread-update-scan-line-map (msg notation offset)
1417 "In threaded view update `mh-thread-scan-line-map'.
1418 MSG is the message being notated with NOTATION at OFFSET."
1419 (let* ((msg (or msg (mh-get-msg-num nil)))
1420 (cur-scan-line (and mh-thread-scan-line-map
1421 (gethash msg mh-thread-scan-line-map)))
1422 (old-scan-lines (loop for map in mh-thread-scan-line-map-stack
1423 collect (and map (gethash msg map)))))
1424 (when cur-scan-line
1425 (setf (aref (car cur-scan-line) offset) notation))
1426 (dolist (line old-scan-lines)
1427 (when line (setf (aref (car line) offset) notation)))))
1428
1429 ;;;###mh-autoload
1430 (defun mh-thread-add-spaces (count)
1431 "Add COUNT spaces to each scan line in `mh-thread-scan-line-map'."
1432 (let ((spaces (format (format "%%%ss" count) "")))
1433 (while (not (eobp))
1434 (let* ((msg-num (mh-get-msg-num nil))
1435 (old-line (nth 3 (gethash msg-num mh-thread-scan-line-map))))
1436 (when (numberp msg-num)
1437 (setf (gethash msg-num mh-thread-scan-line-map)
1438 (mh-thread-parse-scan-line (format "%s%s" spaces old-line)))))
1439 (forward-line 1))))
1440
1441 (defun mh-thread-print-scan-lines (thread-tree)
1442 "Print scan lines in THREAD-TREE in threaded mode."
1443 (let ((mh-thread-body-width (- (window-width) mh-cmd-note
1444 (1- mh-scan-field-subject-start-offset)))
1445 (mh-thread-last-ancestor nil))
1446 (if (null mh-index-data)
1447 (mh-thread-generate-scan-lines thread-tree -2)
1448 (loop for x in (mh-index-group-by-folder)
1449 do (let* ((old-map mh-thread-scan-line-map)
1450 (mh-thread-scan-line-map (make-hash-table)))
1451 (setq mh-thread-last-ancestor nil)
1452 (loop for msg in (cdr x)
1453 do (let ((v (gethash msg old-map)))
1454 (when v
1455 (setf (gethash msg mh-thread-scan-line-map) v))))
1456 (when (> (hash-table-count mh-thread-scan-line-map) 0)
1457 (insert (if (bobp) "" "\n") (car x) "\n")
1458 (mh-thread-generate-scan-lines thread-tree -2))))
1459 (mh-index-create-imenu-index))))
1460
1461 (defun mh-thread-folder ()
1462 "Generate thread view of folder."
1463 (message "Threading %s..." (buffer-name))
1464 (mh-thread-initialize)
1465 (goto-char (point-min))
1466 (mh-remove-all-notation)
1467 (let ((msg-list ()))
1468 (mh-iterate-on-range msg (cons (point-min) (point-max))
1469 (setf (gethash msg mh-thread-scan-line-map) (mh-thread-parse-scan-line))
1470 (push msg msg-list))
1471 (let* ((range (mh-coalesce-msg-list msg-list))
1472 (thread-tree (mh-thread-generate (buffer-name) range)))
1473 (delete-region (point-min) (point-max))
1474 (mh-thread-print-scan-lines thread-tree)
1475 (mh-notate-user-sequences)
1476 (mh-notate-deleted-and-refiled)
1477 (mh-notate-cur)
1478 (message "Threading %s...done" (buffer-name)))))
1479
1480 ;;;###mh-autoload
1481 (defun mh-toggle-threads ()
1482 "Toggle threaded view of folder."
1483 (interactive)
1484 (let ((msg-at-point (mh-get-msg-num nil))
1485 (old-buffer-modified-flag (buffer-modified-p))
1486 (buffer-read-only nil))
1487 (cond ((memq 'unthread mh-view-ops)
1488 (unless (mh-valid-view-change-operation-p 'unthread)
1489 (error "Can't unthread folder"))
1490 (let ((msg-list ()))
1491 (goto-char (point-min))
1492 (while (not (eobp))
1493 (let ((index (mh-get-msg-num nil)))
1494 (when index
1495 (push index msg-list)))
1496 (forward-line))
1497 (mh-scan-folder mh-current-folder
1498 (mapcar #'(lambda (x) (format "%s" x))
1499 (mh-coalesce-msg-list msg-list))
1500 t))
1501 (when mh-index-data
1502 (mh-index-insert-folder-headers)
1503 (mh-notate-cur)))
1504 (t (mh-thread-folder)
1505 (push 'unthread mh-view-ops)))
1506 (when msg-at-point (mh-goto-msg msg-at-point t t))
1507 (set-buffer-modified-p old-buffer-modified-flag)
1508 (mh-recenter nil)))
1509
1510 ;;;###mh-autoload
1511 (defun mh-thread-forget-message (index)
1512 "Forget the message INDEX from the threading tables."
1513 (let* ((id (gethash index mh-thread-index-id-map))
1514 (id-index (gethash id mh-thread-id-index-map))
1515 (duplicates (gethash id mh-thread-duplicates)))
1516 (remhash index mh-thread-index-id-map)
1517 (remhash index mh-thread-scan-line-map)
1518 (cond ((and (eql index id-index) (null duplicates))
1519 (remhash id mh-thread-id-index-map))
1520 ((eql index id-index)
1521 (setf (gethash id mh-thread-id-index-map) (car duplicates))
1522 (setf (gethash (car duplicates) mh-thread-index-id-map) id)
1523 (setf (gethash id mh-thread-duplicates) (cdr duplicates)))
1524 (t
1525 (setf (gethash id mh-thread-duplicates)
1526 (remove index duplicates))))))
1527
1528 \f
1529
1530 ;;; Operations on threads
1531
1532 (defun mh-thread-current-indentation-level ()
1533 "Find the number of spaces by which current message is indented."
1534 (save-excursion
1535 (let ((address-start-offset (+ mh-cmd-note mh-scan-date-flag-width
1536 mh-scan-date-width 1))
1537 (level 0))
1538 (beginning-of-line)
1539 (forward-char address-start-offset)
1540 (while (char-equal (char-after) ? )
1541 (incf level)
1542 (forward-char))
1543 level)))
1544
1545 ;;;###mh-autoload
1546 (defun mh-thread-next-sibling (&optional previous-flag)
1547 "Jump to next sibling.
1548 With non-nil optional argument PREVIOUS-FLAG jump to the previous sibling."
1549 (interactive)
1550 (cond ((not (memq 'unthread mh-view-ops))
1551 (error "Folder isn't threaded"))
1552 ((eobp)
1553 (error "No message at point")))
1554 (beginning-of-line)
1555 (let ((point (point))
1556 (done nil)
1557 (my-level (mh-thread-current-indentation-level)))
1558 (while (and (not done)
1559 (equal (forward-line (if previous-flag -1 1)) 0)
1560 (not (eobp)))
1561 (let ((level (mh-thread-current-indentation-level)))
1562 (cond ((equal level my-level)
1563 (setq done 'success))
1564 ((< level my-level)
1565 (message "No %s sibling" (if previous-flag "previous" "next"))
1566 (setq done 'failure)))))
1567 (cond ((eq done 'success) (mh-maybe-show))
1568 ((eq done 'failure) (goto-char point))
1569 (t (message "No %s sibling" (if previous-flag "previous" "next"))
1570 (goto-char point)))))
1571
1572 ;;;###mh-autoload
1573 (defun mh-thread-previous-sibling ()
1574 "Jump to previous sibling."
1575 (interactive)
1576 (mh-thread-next-sibling t))
1577
1578 (defun mh-thread-immediate-ancestor ()
1579 "Jump to immediate ancestor in thread tree."
1580 (beginning-of-line)
1581 (let ((point (point))
1582 (ancestor-level (- (mh-thread-current-indentation-level) 2))
1583 (done nil))
1584 (if (< ancestor-level 0)
1585 nil
1586 (while (and (not done) (equal (forward-line -1) 0))
1587 (when (equal ancestor-level (mh-thread-current-indentation-level))
1588 (setq done t)))
1589 (unless done
1590 (goto-char point))
1591 done)))
1592
1593 ;;;###mh-autoload
1594 (defun mh-thread-ancestor (&optional thread-root-flag)
1595 "Jump to the ancestor of current message.
1596 If optional argument THREAD-ROOT-FLAG is non-nil then jump to the root of the
1597 thread tree the message belongs to."
1598 (interactive "P")
1599 (beginning-of-line)
1600 (cond ((not (memq 'unthread mh-view-ops))
1601 (error "Folder isn't threaded"))
1602 ((eobp)
1603 (error "No message at point")))
1604 (let ((current-level (mh-thread-current-indentation-level)))
1605 (cond (thread-root-flag
1606 (while (mh-thread-immediate-ancestor))
1607 (mh-maybe-show))
1608 ((equal current-level 1)
1609 (message "Message has no ancestor"))
1610 (t (mh-thread-immediate-ancestor)
1611 (mh-maybe-show)))))
1612
1613 (defun mh-thread-find-children ()
1614 "Return a region containing the current message and its children.
1615 The result is returned as a list of two elements. The first is the point at the
1616 start of the region and the second is the point at the end."
1617 (beginning-of-line)
1618 (if (eobp)
1619 nil
1620 (let ((address-start-offset (+ mh-cmd-note mh-scan-date-flag-width
1621 mh-scan-date-width 1))
1622 (level (mh-thread-current-indentation-level))
1623 spaces begin)
1624 (setq begin (point))
1625 (setq spaces (format (format "%%%ss" (1+ level)) ""))
1626 (forward-line)
1627 (block nil
1628 (while (not (eobp))
1629 (forward-char address-start-offset)
1630 (unless (equal (string-match spaces (buffer-substring-no-properties
1631 (point) (line-end-position)))
1632 0)
1633 (beginning-of-line)
1634 (backward-char)
1635 (return))
1636 (forward-line)))
1637 (list begin (point)))))
1638
1639 ;;;###mh-autoload
1640 (defun mh-thread-delete ()
1641 "Mark current message and all its children for subsequent deletion."
1642 (interactive)
1643 (cond ((not (memq 'unthread mh-view-ops))
1644 (error "Folder isn't threaded"))
1645 ((eobp)
1646 (error "No message at point"))
1647 (t (let ((region (mh-thread-find-children)))
1648 (mh-iterate-on-messages-in-region () (car region) (cadr region)
1649 (mh-delete-a-msg nil))
1650 (mh-next-msg)))))
1651
1652 ;;;###mh-autoload
1653 (defun mh-thread-refile (folder)
1654 "Mark current message and all its children for refiling to FOLDER."
1655 (interactive (list (intern (mh-prompt-for-refile-folder))))
1656 (cond ((not (memq 'unthread mh-view-ops))
1657 (error "Folder isn't threaded"))
1658 ((eobp)
1659 (error "No message at point"))
1660 (t (let ((region (mh-thread-find-children)))
1661 (mh-iterate-on-messages-in-region () (car region) (cadr region)
1662 (mh-refile-a-msg nil folder))
1663 (mh-next-msg)))))
1664
1665 \f
1666
1667 ;; Tick mark handling
1668
1669 ;;;###mh-autoload
1670 (defun mh-toggle-tick (range)
1671 "Toggle tick mark of all messages in RANGE."
1672 (interactive (list (mh-interactive-range "Tick")))
1673 (unless mh-tick-seq
1674 (error "Enable ticking by customizing `mh-tick-seq'"))
1675 (let* ((tick-seq (mh-find-seq mh-tick-seq))
1676 (tick-seq-msgs (mh-seq-msgs tick-seq))
1677 (ticked ())
1678 (unticked ()))
1679 (mh-iterate-on-range msg range
1680 (cond ((member msg tick-seq-msgs)
1681 (push msg unticked)
1682 (setcdr tick-seq (delq msg (cdr tick-seq)))
1683 (when (null (cdr tick-seq)) (setq mh-last-seq-used nil))
1684 (mh-remove-sequence-notation msg (mh-colors-in-use-p)))
1685 (t
1686 (push msg ticked)
1687 (setq mh-last-seq-used mh-tick-seq)
1688 (let ((mh-seq-list (cons `(,mh-tick-seq ,msg) mh-seq-list)))
1689 (mh-add-sequence-notation msg (mh-colors-in-use-p))))))
1690 (mh-add-msgs-to-seq ticked mh-tick-seq nil t)
1691 (mh-undefine-sequence mh-tick-seq unticked)
1692 (when mh-index-data
1693 (mh-index-add-to-sequence mh-tick-seq ticked)
1694 (mh-index-delete-from-sequence mh-tick-seq unticked))))
1695
1696 ;;;###mh-autoload
1697 (defun mh-narrow-to-tick ()
1698 "Limit to messages in `mh-tick-seq'.
1699
1700 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
1701 (interactive)
1702 (cond ((not mh-tick-seq)
1703 (error "Enable ticking by customizing `mh-tick-seq'"))
1704 ((null (mh-seq-msgs (mh-find-seq mh-tick-seq)))
1705 (message "No messages in %s sequence" mh-tick-seq))
1706 (t (mh-narrow-to-seq mh-tick-seq))))
1707
1708 (provide 'mh-seq)
1709
1710 ;;; Local Variables:
1711 ;;; indent-tabs-mode: nil
1712 ;;; sentence-end-double-space: nil
1713 ;;; End:
1714
1715 ;;; arch-tag: 8e952711-01a2-485b-bf21-c9e3ad4de942
1716 ;;; mh-seq.el ends here