*** empty log message ***
[emacs.git] / lisp / mh-e / mh-seq.el
blobf00afa84f86cde0a1c14699bafa717607ae3660f
1 ;;; mh-seq.el --- MH-E sequences support
3 ;; Copyright (C) 1993, 1995, 2001, 2002 Free Software Foundation, Inc.
5 ;; Author: Bill Wohler <wohler@newt.com>
6 ;; Maintainer: Bill Wohler <wohler@newt.com>
7 ;; Keywords: mail
8 ;; See: mh-e.el
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; This tries to implement the algorithm described at:
30 ;; http://www.jwz.org/doc/threading.html
31 ;; It is also a start to implementing the IMAP Threading extension RFC. The
32 ;; implementation lacks the reference and subject canonicalization of the
33 ;; RFC.
35 ;; In the presentation buffer, children messages are shown indented with
36 ;; either [ ] or < > around them. Square brackets ([ ]) denote that the
37 ;; algorithm can point out some headers which when taken together implies
38 ;; that the unindented message is an ancestor of the indented message. If
39 ;; no such proof exists then angles (< >) are used.
41 ;; Some issues and problems are as follows:
43 ;; (1) Scan truncates the fields at length 512. So longer references:
44 ;; headers get mutilated. The same kind of MH format string works when
45 ;; composing messages. Is there a way to avoid this? My scan command
46 ;; is as follows:
47 ;; scan +folder -width 10000 \
48 ;; -format "%(msg)\n%{message-id}\n%{references}\n%{subject}\n"
49 ;; I would really appreciate it if someone would help me with this.
51 ;; (2) Implement heuristics to recognize message identifiers in
52 ;; In-Reply-To: header. Right now it just assumes that the last text
53 ;; between angles (< and >) is the message identifier. There is the
54 ;; chance that this will incorrectly use an email address like a
55 ;; message identifier.
57 ;; (3) Error checking of found message identifiers should be done.
59 ;; (4) Since this breaks the assumption that message indices increase as
60 ;; one goes down the buffer, the binary search based mh-goto-msg
61 ;; doesn't work. I have a simpler replacement which may be less
62 ;; efficient.
64 ;; (5) Better canonicalizing for message identifier and subject strings.
67 ;; Internal support for MH-E package.
69 ;;; Change Log:
71 ;; $Id: mh-seq.el,v 1.101 2003/01/26 00:57:35 jchonig Exp $
73 ;;; Code:
75 (require 'cl)
76 (require 'mh-e)
78 ;; Shush the byte-compiler
79 (defvar tool-bar-mode)
81 ;;; Data structures (used in message threading)...
82 (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))
89 (defstruct (mh-thread-container (:conc-name mh-container-)
90 (:constructor mh-thread-make-container))
91 message parent children
92 (real-child-p t))
95 ;;; Internal variables:
96 (defvar mh-last-seq-used nil
97 "Name of seq to which a msg was last added.")
99 (defvar mh-non-seq-mode-line-annotation nil
100 "Saved value of `mh-mode-line-annotation' when narrowed to a seq.")
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-old-scan-line-map 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.")
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-old-scan-line-map)
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)
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 (mh-undefine-sequence sequence '("all"))
147 (mh-delete-seq-locally sequence)
148 (mh-iterate-on-messages-in-region msg (point-min) (point-max)
149 (when (and (member msg msg-list) (not (mh-seq-containing-msg msg nil)))
150 (mh-notate nil ? (1+ mh-cmd-note))))))
152 ;; Avoid compiler warnings
153 (defvar view-exit-action)
155 ;;;###mh-autoload
156 (defun mh-list-sequences ()
157 "List the sequences defined in the folder being visited."
158 (interactive)
159 (let ((folder mh-current-folder)
160 (temp-buffer mh-sequences-buffer)
161 (seq-list mh-seq-list)
162 (max-len 0))
163 (with-output-to-temp-buffer temp-buffer
164 (save-excursion
165 (set-buffer temp-buffer)
166 (erase-buffer)
167 (message "Listing sequences ...")
168 (insert "Sequences in folder " folder ":\n")
169 (let ((seq-list seq-list))
170 (while seq-list
171 (setq max-len
172 (max (length (symbol-name (mh-seq-name (pop seq-list))))
173 max-len)))
174 (setq max-len (+ 2 max-len)))
175 (while seq-list
176 (let ((name (mh-seq-name (car seq-list)))
177 (sorted-seq-msgs
178 (mh-coalesce-msg-list
179 (sort (copy-sequence (mh-seq-msgs (car seq-list))) '<)))
180 name-spec)
181 (insert (setq name-spec (format (format "%%%ss:" max-len) name)))
182 (while sorted-seq-msgs
183 (let ((next-element (format " %s" (pop sorted-seq-msgs))))
184 (when (>= (+ (current-column) (length next-element))
185 (window-width))
186 (insert "\n")
187 (insert (format (format "%%%ss" (length name-spec)) "")))
188 (insert next-element)))
189 (insert "\n"))
190 (setq seq-list (cdr seq-list)))
191 (goto-char (point-min))
192 (view-mode 1)
193 (setq view-exit-action 'kill-buffer)
194 (message "Listing sequences...done")))))
196 ;;;###mh-autoload
197 (defun mh-msg-is-in-seq (message)
198 "Display the sequences that contain MESSAGE (default: current message)."
199 (interactive (list (mh-get-msg-num t)))
200 (let* ((dest-folder (loop for seq in mh-refile-list
201 when (member message (cdr seq)) return (car seq)))
202 (deleted-flag (unless dest-folder (member message mh-delete-list))))
203 (message "Message %d%s is in sequences: %s"
204 message
205 (cond (dest-folder (format " (to be refiled to %s)" dest-folder))
206 (deleted-flag (format " (to be deleted)"))
207 (t ""))
208 (mapconcat 'concat
209 (mh-list-to-string (mh-seq-containing-msg message t))
210 " "))))
212 ;;;###mh-autoload
213 (defun mh-narrow-to-seq (sequence)
214 "Restrict display of this folder to just messages in SEQUENCE.
215 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
216 (interactive (list (mh-read-seq "Narrow to" t)))
217 (with-mh-folder-updating (t)
218 (cond ((mh-seq-to-msgs sequence)
219 (mh-widen)
220 (mh-remove-all-notation)
221 (let ((eob (point-max))
222 (msg-at-cursor (mh-get-msg-num nil)))
223 (setq mh-thread-old-scan-line-map mh-thread-scan-line-map)
224 (setq mh-thread-scan-line-map (make-hash-table :test #'eql))
225 (mh-copy-seq-to-eob sequence)
226 (narrow-to-region eob (point-max))
227 (mh-notate-user-sequences)
228 (mh-notate-deleted-and-refiled)
229 (mh-notate-cur)
230 (when msg-at-cursor (mh-goto-msg msg-at-cursor t t))
231 (make-variable-buffer-local 'mh-non-seq-mode-line-annotation)
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 (if (and (boundp 'tool-bar-mode) tool-bar-mode)
237 (set (make-local-variable 'tool-bar-map)
238 mh-folder-seq-tool-bar-map))
239 (setq mh-narrowed-to-seq sequence)
240 (push 'widen mh-view-ops)))
242 (error "No messages in sequence `%s'" (symbol-name sequence))))))
244 ;;;###mh-autoload
245 (defun mh-put-msg-in-seq (msg-or-seq sequence)
246 "Add MSG-OR-SEQ (default: displayed message) to SEQUENCE.
247 If optional prefix argument provided, then prompt for the message sequence.
248 If variable `transient-mark-mode' is non-nil and the mark is active, then
249 the selected region is added to the sequence."
250 (interactive (list (cond
251 ((mh-mark-active-p t)
252 (cons (region-beginning) (region-end)))
253 (current-prefix-arg
254 (mh-read-seq-default "Add messages from" t))
256 (cons (line-beginning-position) (line-end-position))))
257 (mh-read-seq-default "Add to" nil)))
258 (let ((internal-seq-flag (mh-internal-seq sequence))
259 msg-list)
260 (cond ((and (consp msg-or-seq)
261 (numberp (car msg-or-seq)) (numberp (cdr msg-or-seq)))
262 (mh-iterate-on-messages-in-region m (car msg-or-seq) (cdr msg-or-seq)
263 (push m msg-list)
264 (unless internal-seq-flag
265 (mh-notate nil mh-note-seq (1+ mh-cmd-note))))
266 (mh-add-msgs-to-seq msg-list sequence internal-seq-flag t))
267 ((or (numberp msg-or-seq) (listp msg-or-seq))
268 (when (numberp msg-or-seq)
269 (setq msg-or-seq (list msg-or-seq)))
270 (mh-add-msgs-to-seq msg-or-seq sequence internal-seq-flag))
271 (t (mh-add-msgs-to-seq (mh-seq-to-msgs msg-or-seq) sequence)))
272 (if (not internal-seq-flag)
273 (setq mh-last-seq-used sequence))))
275 (defun mh-valid-view-change-operation-p (op)
276 "Check if the view change operation can be performed.
277 OP is one of 'widen and 'unthread."
278 (cond ((eq (car mh-view-ops) op)
279 (pop mh-view-ops))
280 (t nil)))
282 ;;;###mh-autoload
283 (defun mh-widen ()
284 "Remove restrictions from current folder, thereby showing all messages."
285 (interactive)
286 (let ((msg (mh-get-msg-num nil)))
287 (when mh-narrowed-to-seq
288 (cond ((mh-valid-view-change-operation-p 'widen) nil)
289 ((memq 'widen mh-view-ops)
290 (while (not (eq (car mh-view-ops) 'widen))
291 (setq mh-view-ops (cdr mh-view-ops)))
292 (pop mh-view-ops))
293 (t (error "Widening is not applicable")))
294 (when (memq 'unthread mh-view-ops)
295 (setq mh-thread-scan-line-map mh-thread-old-scan-line-map))
296 (with-mh-folder-updating (t)
297 (delete-region (point-min) (point-max))
298 (widen)
299 (setq mh-mode-line-annotation mh-non-seq-mode-line-annotation)
300 (mh-make-folder-mode-line))
301 (if msg
302 (mh-goto-msg msg t t))
303 (mh-notate-deleted-and-refiled)
304 (mh-notate-user-sequences)
305 (mh-notate-cur)
306 (mh-recenter nil)))
307 (if (and (boundp 'tool-bar-mode) tool-bar-mode)
308 (set (make-local-variable 'tool-bar-map) mh-folder-tool-bar-map))
309 (setq mh-narrowed-to-seq nil))
311 ;; FIXME? We may want to clear all notations and add one for current-message
312 ;; and process user sequences.
313 (defun mh-notate-deleted-and-refiled ()
314 "Notate messages marked for deletion or refiling.
315 Messages to be deleted are given by `mh-delete-list' while messages to be
316 refiled are present in `mh-refile-list'."
317 (let ((refiled-hash (make-hash-table))
318 (deleted-hash (make-hash-table)))
319 (dolist (msg mh-delete-list)
320 (setf (gethash msg deleted-hash) t))
321 (dolist (dest-msg-list mh-refile-list)
322 (dolist (msg (cdr dest-msg-list))
323 (setf (gethash msg refiled-hash) t)))
324 (mh-iterate-on-messages-in-region msg (point-min) (point-max)
325 (cond ((gethash msg refiled-hash)
326 (mh-notate nil mh-note-refiled mh-cmd-note))
327 ((gethash msg deleted-hash)
328 (mh-notate nil mh-note-deleted mh-cmd-note))))))
332 ;;; Commands to manipulate sequences. Sequences are stored in an alist
333 ;;; of the form:
334 ;;; ((seq-name msgs ...) (seq-name msgs ...) ...)
336 (defun mh-read-seq-default (prompt not-empty)
337 "Read and return sequence name with default narrowed or previous sequence.
338 PROMPT is the prompt to use when reading. If NOT-EMPTY is non-nil then a
339 non-empty sequence is read."
340 (mh-read-seq prompt not-empty
341 (or mh-narrowed-to-seq
342 mh-last-seq-used
343 (car (mh-seq-containing-msg (mh-get-msg-num nil) nil)))))
345 (defun mh-read-seq (prompt not-empty &optional default)
346 "Read and return a sequence name.
347 Prompt with PROMPT, raise an error if the sequence is empty and the NOT-EMPTY
348 flag is non-nil, and supply an optional DEFAULT sequence. A reply of '%'
349 defaults to the first sequence containing the current message."
350 (let* ((input (completing-read (format "%s %s %s" prompt "sequence:"
351 (if default
352 (format "[%s] " default)
353 ""))
354 (mh-seq-names mh-seq-list)))
355 (seq (cond ((equal input "%")
356 (car (mh-seq-containing-msg (mh-get-msg-num t) nil)))
357 ((equal input "") default)
358 (t (intern input))))
359 (msgs (mh-seq-to-msgs seq)))
360 (if (and (null msgs) not-empty)
361 (error "No messages in sequence `%s'" seq))
362 seq))
364 (defun mh-seq-names (seq-list)
365 "Return an alist containing the names of the SEQ-LIST."
366 (mapcar (lambda (entry) (list (symbol-name (mh-seq-name entry))))
367 seq-list))
369 ;;;###mh-autoload
370 (defun mh-rename-seq (sequence new-name)
371 "Rename SEQUENCE to have NEW-NAME."
372 (interactive (list (mh-read-seq "Old" t)
373 (intern (read-string "New sequence name: "))))
374 (let ((old-seq (mh-find-seq sequence)))
375 (or old-seq
376 (error "Sequence %s does not exist" sequence))
377 ;; create new sequence first, since it might raise an error.
378 (mh-define-sequence new-name (mh-seq-msgs old-seq))
379 (mh-undefine-sequence sequence (mh-seq-msgs old-seq))
380 (rplaca old-seq new-name)))
382 ;;;###mh-autoload
383 (defun mh-map-to-seq-msgs (func seq &rest args)
384 "Invoke the FUNC at each message in the SEQ.
385 SEQ can either be a list of messages or a MH sequence. The remaining ARGS are
386 passed as arguments to FUNC."
387 (save-excursion
388 (let ((msgs (if (listp seq) seq (mh-seq-to-msgs seq))))
389 (while msgs
390 (if (mh-goto-msg (car msgs) t t)
391 (apply func (car msgs) args))
392 (setq msgs (cdr msgs))))))
394 ;;;###mh-autoload
395 (defun mh-notate-seq (seq notation offset)
396 "Mark the scan listing.
397 All messages in SEQ are marked with NOTATION at OFFSET from the beginning of
398 the line."
399 (let ((msg-list (mh-seq-to-msgs seq)))
400 (mh-iterate-on-messages-in-region msg (point-min) (point-max)
401 (when (member msg msg-list)
402 (mh-notate nil notation offset)))))
404 ;;;###mh-autoload
405 (defun mh-notate-cur ()
406 "Mark the MH sequence cur.
407 In addition to notating the current message with `mh-note-cur' the function
408 uses `overlay-arrow-position' to put a marker in the fringe."
409 (let ((cur (car (mh-seq-to-msgs 'cur))))
410 (when (and cur (mh-goto-msg cur t t))
411 (mh-notate nil mh-note-cur mh-cmd-note)
412 (beginning-of-line)
413 (setq mh-arrow-marker (set-marker mh-arrow-marker (point)))
414 (setq overlay-arrow-position mh-arrow-marker))))
416 ;;;###mh-autoload
417 (defun mh-add-to-sequence (seq msgs)
418 "The sequence SEQ is augmented with the messages in MSGS."
419 ;; Add to a SEQUENCE each message the list of MSGS.
420 (if (not (mh-folder-name-p seq))
421 (if msgs
422 (apply 'mh-exec-cmd "mark" mh-current-folder "-add"
423 "-sequence" (symbol-name seq)
424 (mh-coalesce-msg-list msgs)))))
426 ;; This has a tricky bug. mh-map-to-seq-msgs uses mh-goto-msg, which assumes
427 ;; that the folder buffer is sorted. However in this case that assumption
428 ;; doesn't hold. So we will do this the dumb way.
429 ;(defun mh-copy-seq-to-point (seq location)
430 ; ;; Copy the scan listing of the messages in SEQUENCE to after the point
431 ; ;; LOCATION in the current buffer.
432 ; (mh-map-to-seq-msgs 'mh-copy-line-to-point seq location))
434 (defun mh-copy-seq-to-eob (seq)
435 "Copy SEQ to the end of the buffer."
436 ;; It is quite involved to write something which will work at any place in
437 ;; the buffer, so we will write something which works only at the end of
438 ;; the buffer. If we ever need to insert sequences in the middle of the
439 ;; buffer, this will need to be fixed.
440 (save-excursion
441 (let* ((msgs (mh-seq-to-msgs seq))
442 (coalesced-msgs (mh-coalesce-msg-list msgs)))
443 (goto-char (point-max))
444 (save-restriction
445 (narrow-to-region (point) (point))
446 (mh-regenerate-headers coalesced-msgs t)
447 (cond ((memq 'unthread mh-view-ops)
448 ;; Populate restricted scan-line map
449 (goto-char (point-min))
450 (while (not (eobp))
451 (let ((msg (mh-get-msg-num nil)))
452 (when (numberp msg)
453 (setf (gethash msg mh-thread-scan-line-map)
454 (mh-thread-parse-scan-line))))
455 (forward-line))
456 ;; Remove scan lines and read results from pre-computed tree
457 (delete-region (point-min) (point-max))
458 (let ((thread-tree (mh-thread-generate mh-current-folder ()))
459 (mh-thread-body-width
460 (- (window-width) mh-cmd-note
461 (1- mh-scan-field-subject-start-offset)))
462 (mh-thread-last-ancestor nil))
463 (mh-thread-generate-scan-lines thread-tree -2)))
464 (mh-index-data
465 (mh-index-insert-folder-headers)))))))
467 (defun mh-copy-line-to-point (msg location)
468 "Copy current message line to a specific location.
469 The argument MSG is not used. The message in the current line is copied to
470 LOCATION."
471 ;; msg is not used?
472 ;; Copy the current line to the LOCATION in the current buffer.
473 (beginning-of-line)
474 (save-excursion
475 (let ((beginning-of-line (point))
476 end)
477 (forward-line 1)
478 (setq end (point))
479 (goto-char location)
480 (insert-buffer-substring (current-buffer) beginning-of-line end))))
482 ;;;###mh-autoload
483 (defmacro mh-iterate-on-messages-in-region (var begin end &rest body)
484 "Iterate over region.
485 VAR is bound to the message on the current line as we loop starting from BEGIN
486 till END. In each step BODY is executed.
488 If VAR is nil then the loop is executed without any binding."
489 (unless (symbolp var)
490 (error "Can not bind the non-symbol %s" var))
491 (let ((binding-needed-flag var))
492 `(save-excursion
493 (goto-char ,begin)
494 (while (and (<= (point) ,end) (not (eobp)))
495 (when (looking-at mh-scan-valid-regexp)
496 (let ,(if binding-needed-flag `((,var (mh-get-msg-num t))) ())
497 ,@body))
498 (forward-line 1)))))
500 ;;;###mh-autoload
501 (defun mh-region-to-msg-list (begin end)
502 "Return a list of messages within the region between BEGIN and END."
503 ;; If end is end of buffer back up one position
504 (setq end (if (equal end (point-max)) (1- end) end))
505 (let ((result))
506 (mh-iterate-on-messages-in-region index begin end
507 (when (numberp index) (push index result)))
508 result))
512 ;;; Commands to handle new 'subject sequence.
513 ;;; Or "Poor man's threading" by psg.
515 (defun mh-subject-to-sequence (all)
516 "Put all following messages with same subject in sequence 'subject.
517 If arg ALL is t, move to beginning of folder buffer to collect all messages.
518 If arg ALL is nil, collect only messages fron current one on forward.
520 Return number of messages put in the sequence:
522 nil -> there was no subject line.
523 0 -> there were no later messages with the same subject (sequence not made)
524 >1 -> the total number of messages including current one."
525 (if (not (eq major-mode 'mh-folder-mode))
526 (error "Not in a folder buffer"))
527 (save-excursion
528 (beginning-of-line)
529 (if (or (not (looking-at mh-scan-subject-regexp))
530 (not (match-string 3))
531 (string-equal "" (match-string 3)))
532 (progn (message "No subject line.")
533 nil)
534 (let ((subject (match-string-no-properties 3))
535 (list))
536 (if (> (length subject) 41)
537 (setq subject (substring subject 0 41)))
538 (save-excursion
539 (if all
540 (goto-char (point-min)))
541 (while (re-search-forward mh-scan-subject-regexp nil t)
542 (let ((this-subject (match-string-no-properties 3)))
543 (if (> (length this-subject) 41)
544 (setq this-subject (substring this-subject 0 41)))
545 (if (string-equal this-subject subject)
546 (setq list (cons (mh-get-msg-num t) list))))))
547 (cond
548 (list
549 ;; If we created a new sequence, add the initial message to it too.
550 (if (not (member (mh-get-msg-num t) list))
551 (setq list (cons (mh-get-msg-num t) list)))
552 (if (member '("subject") (mh-seq-names mh-seq-list))
553 (mh-delete-seq 'subject))
554 ;; sort the result into a sequence
555 (let ((sorted-list (sort (copy-sequence list) 'mh-lessp)))
556 (while sorted-list
557 (mh-add-msgs-to-seq (car sorted-list) 'subject nil)
558 (setq sorted-list (cdr sorted-list)))
559 (safe-length list)))
561 0))))))
563 ;;;###mh-autoload
564 (defun mh-narrow-to-subject ()
565 "Narrow to a sequence containing all following messages with same subject."
566 (interactive)
567 (let ((num (mh-get-msg-num nil))
568 (count (mh-subject-to-sequence t)))
569 (cond
570 ((not count) ; No subject line, delete msg anyway
571 nil)
572 ((= 0 count) ; No other msgs, delete msg anyway.
573 (message "No other messages with same Subject following this one.")
574 nil)
575 (t ; We have a subject sequence.
576 (message "Found %d messages for subject sequence." count)
577 (mh-narrow-to-seq 'subject)
578 (if (numberp num)
579 (mh-goto-msg num t t))))))
581 ;;;###mh-autoload
582 (defun mh-delete-subject ()
583 "Mark all following messages with same subject to be deleted.
584 This puts the messages in a sequence named subject. You can undo the last
585 deletion marks using `mh-undo' with a prefix argument and then specifying the
586 subject sequence."
587 (interactive)
588 (let ((count (mh-subject-to-sequence nil)))
589 (cond
590 ((not count) ; No subject line, delete msg anyway
591 (mh-delete-msg (mh-get-msg-num t)))
592 ((= 0 count) ; No other msgs, delete msg anyway.
593 (message "No other messages with same Subject following this one.")
594 (mh-delete-msg (mh-get-msg-num t)))
595 (t ; We have a subject sequence.
596 (message "Marked %d messages for deletion" count)
597 (mh-delete-msg 'subject)))))
599 ;;;###mh-autoload
600 (defun mh-delete-subject-or-thread ()
601 "Mark messages for deletion intelligently.
602 If the folder is threaded then `mh-thread-delete' is used to mark the current
603 message and all its descendants for deletion. Otherwise `mh-delete-subject' is
604 used to mark the current message and all messages following it with the same
605 subject for deletion."
606 (interactive)
607 (if (memq 'unthread mh-view-ops)
608 (mh-thread-delete)
609 (mh-delete-subject)))
611 ;;; Message threading:
613 (defun mh-thread-initialize ()
614 "Make hash tables, otherwise clear them."
615 (cond
616 (mh-thread-id-hash
617 (clrhash mh-thread-id-hash)
618 (clrhash mh-thread-subject-hash)
619 (clrhash mh-thread-id-table)
620 (clrhash mh-thread-id-index-map)
621 (clrhash mh-thread-index-id-map)
622 (clrhash mh-thread-scan-line-map)
623 (clrhash mh-thread-subject-container-hash)
624 (clrhash mh-thread-duplicates)
625 (setq mh-thread-history ()))
626 (t (setq mh-thread-id-hash (make-hash-table :test #'equal))
627 (setq mh-thread-subject-hash (make-hash-table :test #'equal))
628 (setq mh-thread-id-table (make-hash-table :test #'eq))
629 (setq mh-thread-id-index-map (make-hash-table :test #'eq))
630 (setq mh-thread-index-id-map (make-hash-table :test #'eql))
631 (setq mh-thread-scan-line-map (make-hash-table :test #'eql))
632 (setq mh-thread-subject-container-hash (make-hash-table :test #'eq))
633 (setq mh-thread-duplicates (make-hash-table :test #'eq))
634 (setq mh-thread-history ()))))
636 (defsubst mh-thread-id-container (id)
637 "Given ID, return the corresponding container in `mh-thread-id-table'.
638 If no container exists then a suitable container is created and the id-table
639 is updated."
640 (when (not id)
641 (error "1"))
642 (or (gethash id mh-thread-id-table)
643 (setf (gethash id mh-thread-id-table)
644 (let ((message (mh-thread-make-message :id id)))
645 (mh-thread-make-container :message message)))))
647 (defsubst mh-thread-remove-parent-link (child)
648 "Remove parent link of CHILD if it exists."
649 (let* ((child-container (if (mh-thread-container-p child)
650 child (mh-thread-id-container child)))
651 (parent-container (mh-container-parent child-container)))
652 (when parent-container
653 (setf (mh-container-children parent-container)
654 (loop for elem in (mh-container-children parent-container)
655 unless (eq child-container elem) collect elem))
656 (setf (mh-container-parent child-container) nil))))
658 (defsubst mh-thread-add-link (parent child &optional at-end-p)
659 "Add links so that PARENT becomes a parent of CHILD.
660 Doesn't make any changes if CHILD is already an ancestor of PARENT. If
661 optional argument AT-END-P is non-nil, the CHILD is added to the end of the
662 children list of PARENT."
663 (let ((parent-container (cond ((null parent) nil)
664 ((mh-thread-container-p parent) parent)
665 (t (mh-thread-id-container parent))))
666 (child-container (if (mh-thread-container-p child)
667 child (mh-thread-id-container child))))
668 (when (and parent-container
669 (not (mh-thread-ancestor-p child-container parent-container))
670 (not (mh-thread-ancestor-p parent-container child-container)))
671 (mh-thread-remove-parent-link child-container)
672 (cond ((not at-end-p)
673 (push child-container (mh-container-children parent-container)))
674 ((null (mh-container-children parent-container))
675 (push child-container (mh-container-children parent-container)))
676 (t (let ((last-child (mh-container-children parent-container)))
677 (while (cdr last-child)
678 (setq last-child (cdr last-child)))
679 (setcdr last-child (cons child-container nil)))))
680 (setf (mh-container-parent child-container) parent-container))
681 (unless parent-container
682 (mh-thread-remove-parent-link child-container))))
684 (defun mh-thread-ancestor-p (ancestor successor)
685 "Return t if ANCESTOR is really an ancestor of SUCCESSOR and nil otherwise.
686 In the limit, the function returns t if ANCESTOR and SUCCESSOR are the same
687 containers."
688 (block nil
689 (while successor
690 (when (eq ancestor successor) (return t))
691 (setq successor (mh-container-parent successor)))
692 nil))
694 (defsubst mh-thread-get-message-container (message)
695 "Return container which has MESSAGE in it.
696 If there is no container present then a new container is allocated."
697 (let* ((id (mh-message-id message))
698 (container (gethash id mh-thread-id-table)))
699 (cond (container (setf (mh-container-message container) message)
700 container)
701 (t (setf (gethash id mh-thread-id-table)
702 (mh-thread-make-container :message message))))))
704 (defsubst mh-thread-get-message (id subject-re-p subject refs)
705 "Return appropriate message.
706 Otherwise update message already present to have the proper ID, SUBJECT-RE-P,
707 SUBJECT and REFS fields."
708 (let* ((container (gethash id mh-thread-id-table))
709 (message (if container (mh-container-message container) nil)))
710 (cond (message
711 (setf (mh-message-subject-re-p message) subject-re-p)
712 (setf (mh-message-subject message) subject)
713 (setf (mh-message-id message) id)
714 (setf (mh-message-references message) refs)
715 message)
716 (container
717 (setf (mh-container-message container)
718 (mh-thread-make-message :subject subject
719 :subject-re-p subject-re-p
720 :id id :references refs)))
721 (t (let ((message (mh-thread-make-message
722 :subject subject
723 :subject-re-p subject-re-p
724 :id id :references refs)))
725 (prog1 message
726 (mh-thread-get-message-container message)))))))
728 (defsubst mh-thread-canonicalize-id (id)
729 "Produce canonical string representation for ID.
730 This allows cheap string comparison with EQ."
731 (or (and (equal id "") (copy-sequence ""))
732 (gethash id mh-thread-id-hash)
733 (setf (gethash id mh-thread-id-hash) id)))
735 (defsubst mh-thread-prune-subject (subject)
736 "Prune leading Re:'s, Fwd:'s etc. and trailing (fwd)'s from SUBJECT.
737 If the result after pruning is not the empty string then it is canonicalized
738 so that subjects can be tested for equality with eq. This is done so that all
739 the messages without a subject are not put into a single thread."
740 (let ((case-fold-search t)
741 (subject-pruned-flag nil))
742 ;; Prune subject leader
743 (while (or (string-match "^[ \t]*\\(re\\|fwd?\\)\\(\\[[0-9]*\\]\\)?:[ \t]*"
744 subject)
745 (string-match "^[ \t]*\\[[^\\]][ \t]*" subject))
746 (setq subject-pruned-flag t)
747 (setq subject (substring subject (match-end 0))))
748 ;; Prune subject trailer
749 (while (or (string-match "(fwd)$" subject)
750 (string-match "[ \t]+$" subject))
751 (setq subject-pruned-flag t)
752 (setq subject (substring subject 0 (match-beginning 0))))
753 ;; Canonicalize subject only if it is non-empty
754 (cond ((equal subject "") (values subject subject-pruned-flag))
755 (t (values
756 (or (gethash subject mh-thread-subject-hash)
757 (setf (gethash subject mh-thread-subject-hash) subject))
758 subject-pruned-flag)))))
760 (defun mh-thread-container-subject (container)
761 "Return the subject of CONTAINER.
762 If CONTAINER is empty return the subject info of one of its children."
763 (cond ((and (mh-container-message container)
764 (mh-message-id (mh-container-message container)))
765 (mh-message-subject (mh-container-message container)))
766 (t (block nil
767 (dolist (kid (mh-container-children container))
768 (when (and (mh-container-message kid)
769 (mh-message-id (mh-container-message kid)))
770 (let ((kid-message (mh-container-message kid)))
771 (return (mh-message-subject kid-message)))))
772 (error "This can't happen!")))))
774 (defun mh-thread-rewind-pruning ()
775 "Restore the thread tree to its state before pruning."
776 (while mh-thread-history
777 (let ((action (pop mh-thread-history)))
778 (cond ((eq (car action) 'DROP)
779 (mh-thread-remove-parent-link (cadr action))
780 (mh-thread-add-link (caddr action) (cadr action)))
781 ((eq (car action) 'PROMOTE)
782 (let ((node (cadr action))
783 (parent (caddr action))
784 (children (cdddr action)))
785 (dolist (child children)
786 (mh-thread-remove-parent-link child)
787 (mh-thread-add-link node child))
788 (mh-thread-add-link parent node)))
789 ((eq (car action) 'SUBJECT)
790 (let ((node (cadr action)))
791 (mh-thread-remove-parent-link node)
792 (setf (mh-container-real-child-p node) t)))))))
794 (defun mh-thread-prune-containers (roots)
795 "Prune empty containers in the containers ROOTS."
796 (let ((dfs-ordered-nodes ())
797 (work-list roots))
798 (while work-list
799 (let ((node (pop work-list)))
800 (dolist (child (mh-container-children node))
801 (push child work-list))
802 (push node dfs-ordered-nodes)))
803 (while dfs-ordered-nodes
804 (let ((node (pop dfs-ordered-nodes)))
805 (cond ((gethash (mh-message-id (mh-container-message node))
806 mh-thread-id-index-map)
807 ;; Keep it
808 (setf (mh-container-children node)
809 (mh-thread-sort-containers (mh-container-children node))))
810 ((and (mh-container-children node)
811 (or (null (cdr (mh-container-children node)))
812 (mh-container-parent node)))
813 ;; Promote kids
814 (let ((children ()))
815 (dolist (kid (mh-container-children node))
816 (mh-thread-remove-parent-link kid)
817 (mh-thread-add-link (mh-container-parent node) kid)
818 (push kid children))
819 (push `(PROMOTE ,node ,(mh-container-parent node) ,@children)
820 mh-thread-history)
821 (mh-thread-remove-parent-link node)))
822 ((mh-container-children node)
823 ;; Promote the first orphan to parent and add the other kids as
824 ;; his children
825 (setf (mh-container-children node)
826 (mh-thread-sort-containers (mh-container-children node)))
827 (let ((new-parent (car (mh-container-children node)))
828 (other-kids (cdr (mh-container-children node))))
829 (mh-thread-remove-parent-link new-parent)
830 (dolist (kid other-kids)
831 (mh-thread-remove-parent-link kid)
832 (setf (mh-container-real-child-p kid) nil)
833 (mh-thread-add-link new-parent kid t))
834 (push `(PROMOTE ,node ,(mh-container-parent node)
835 ,new-parent ,@other-kids)
836 mh-thread-history)
837 (mh-thread-remove-parent-link node)))
839 ;; Drop it
840 (push `(DROP ,node ,(mh-container-parent node))
841 mh-thread-history)
842 (mh-thread-remove-parent-link node)))))
843 (let ((results ()))
844 (maphash #'(lambda (k v)
845 (declare (ignore k))
846 (when (and (null (mh-container-parent v))
847 (gethash (mh-message-id (mh-container-message v))
848 mh-thread-id-index-map))
849 (push v results)))
850 mh-thread-id-table)
851 (mh-thread-sort-containers results))))
853 (defun mh-thread-sort-containers (containers)
854 "Sort a list of message CONTAINERS to be in ascending order wrt index."
855 (sort containers
856 #'(lambda (x y)
857 (when (and (mh-container-message x) (mh-container-message y))
858 (let* ((id-x (mh-message-id (mh-container-message x)))
859 (id-y (mh-message-id (mh-container-message y)))
860 (index-x (gethash id-x mh-thread-id-index-map))
861 (index-y (gethash id-y mh-thread-id-index-map)))
862 (and (integerp index-x) (integerp index-y)
863 (< index-x index-y)))))))
865 (defsubst mh-thread-group-by-subject (roots)
866 "Group the set of message containers, ROOTS based on subject.
867 Bug: Check for and make sure that something without Re: is made the parent in
868 preference to something that has it."
869 (clrhash mh-thread-subject-container-hash)
870 (let ((results ()))
871 (dolist (root roots)
872 (let* ((subject (mh-thread-container-subject root))
873 (parent (gethash subject mh-thread-subject-container-hash)))
874 (cond (parent (mh-thread-remove-parent-link root)
875 (mh-thread-add-link parent root t)
876 (setf (mh-container-real-child-p root) nil)
877 (push `(SUBJECT ,root) mh-thread-history))
879 (setf (gethash subject mh-thread-subject-container-hash) root)
880 (push root results)))))
881 (nreverse results)))
883 (defsubst mh-thread-process-in-reply-to (reply-to-header)
884 "Extract message id's from REPLY-TO-HEADER.
885 Ideally this should have some regexp which will try to guess if a string
886 between < and > is a message id and not an email address. For now it will
887 take the last string inside angles."
888 (let ((end (mh-search-from-end ?> reply-to-header)))
889 (when (numberp end)
890 (let ((begin (mh-search-from-end ?< (substring reply-to-header 0 end))))
891 (when (numberp begin)
892 (list (substring reply-to-header begin (1+ end))))))))
894 (defun mh-thread-set-tables (folder)
895 "Use the tables of FOLDER in current buffer."
896 (flet ((mh-get-table (symbol)
897 (save-excursion
898 (set-buffer folder)
899 (symbol-value symbol))))
900 (setq mh-thread-id-hash (mh-get-table 'mh-thread-id-hash))
901 (setq mh-thread-subject-hash (mh-get-table 'mh-thread-subject-hash))
902 (setq mh-thread-id-table (mh-get-table 'mh-thread-id-table))
903 (setq mh-thread-id-index-map (mh-get-table 'mh-thread-id-index-map))
904 (setq mh-thread-index-id-map (mh-get-table 'mh-thread-index-id-map))
905 (setq mh-thread-scan-line-map (mh-get-table 'mh-thread-scan-line-map))
906 (setq mh-thread-subject-container-hash
907 (mh-get-table 'mh-thread-subject-container-hash))
908 (setq mh-thread-duplicates (mh-get-table 'mh-thread-duplicates))
909 (setq mh-thread-history (mh-get-table 'mh-thread-history))))
911 (defsubst mh-thread-update-id-index-maps (id index)
912 "Message with id, ID is the message in INDEX.
913 The function also checks for duplicate messages (that is multiple messages
914 with the same ID). These messages are put in the `mh-thread-duplicates' hash
915 table."
916 (let ((old-index (gethash id mh-thread-id-index-map)))
917 (when old-index (push old-index (gethash id mh-thread-duplicates)))
918 (setf (gethash id mh-thread-id-index-map) index)
919 (setf (gethash index mh-thread-index-id-map) id)))
923 ;;; Generate Threads...
925 (defvar mh-message-id-regexp "^<.*@.*>$"
926 "Regexp to recognize whether a string is a message identifier.")
928 (defun mh-thread-generate (folder msg-list)
929 "Scan FOLDER to get info for threading.
930 Only information about messages in MSG-LIST are added to the tree."
931 (with-temp-buffer
932 (mh-thread-set-tables folder)
933 (when msg-list
934 (apply
935 #'call-process (expand-file-name mh-scan-prog mh-progs) nil '(t nil) nil
936 "-width" "10000" "-format"
937 "%(msg)\n%{message-id}\n%{references}\n%{in-reply-to}\n%{subject}\n"
938 folder (mapcar #'(lambda (x) (format "%s" x)) msg-list)))
939 (goto-char (point-min))
940 (let ((roots ())
941 (case-fold-search t))
942 (block nil
943 (while (not (eobp))
944 (block process-message
945 (let* ((index-line
946 (prog1 (buffer-substring (point) (line-end-position))
947 (forward-line)))
948 (index (car (read-from-string index-line)))
949 (id (prog1 (buffer-substring (point) (line-end-position))
950 (forward-line)))
951 (refs (prog1 (buffer-substring (point) (line-end-position))
952 (forward-line)))
953 (in-reply-to (prog1 (buffer-substring (point)
954 (line-end-position))
955 (forward-line)))
956 (subject (prog1
957 (buffer-substring (point) (line-end-position))
958 (forward-line)))
959 (subject-re-p nil))
960 (unless (gethash index mh-thread-scan-line-map)
961 (return-from process-message))
962 (unless (integerp index) (return)) ;Error message here
963 (multiple-value-setq (subject subject-re-p)
964 (mh-thread-prune-subject subject))
965 (setq in-reply-to (mh-thread-process-in-reply-to in-reply-to))
966 (setq refs (loop for x in (append (split-string refs) in-reply-to)
967 when (string-match mh-message-id-regexp x)
968 collect x))
969 (setq id (mh-thread-canonicalize-id id))
970 (mh-thread-update-id-index-maps id index)
971 (setq refs (mapcar #'mh-thread-canonicalize-id refs))
972 (mh-thread-get-message id subject-re-p subject refs)
973 (do ((ancestors refs (cdr ancestors)))
974 ((null (cdr ancestors))
975 (when (car ancestors)
976 (mh-thread-remove-parent-link id)
977 (mh-thread-add-link (car ancestors) id)))
978 (mh-thread-add-link (car ancestors) (cadr ancestors)))))))
979 (maphash #'(lambda (k v)
980 (declare (ignore k))
981 (when (null (mh-container-parent v))
982 (push v roots)))
983 mh-thread-id-table)
984 (setq roots (mh-thread-prune-containers roots))
985 (prog1 (setq roots (mh-thread-group-by-subject roots))
986 (let ((history mh-thread-history))
987 (set-buffer folder)
988 (setq mh-thread-history history))))))
990 ;;;###mh-autoload
991 (defun mh-thread-inc (folder start-point)
992 "Update thread tree for FOLDER.
993 All messages after START-POINT are added to the thread tree."
994 (mh-thread-rewind-pruning)
995 (goto-char start-point)
996 (let ((msg-list ()))
997 (while (not (eobp))
998 (let ((index (mh-get-msg-num nil)))
999 (when (numberp index)
1000 (push index msg-list)
1001 (setf (gethash index mh-thread-scan-line-map)
1002 (mh-thread-parse-scan-line)))
1003 (forward-line)))
1004 (let ((thread-tree (mh-thread-generate folder msg-list))
1005 (buffer-read-only nil)
1006 (old-buffer-modified-flag (buffer-modified-p)))
1007 (delete-region (point-min) (point-max))
1008 (let ((mh-thread-body-width (- (window-width) mh-cmd-note
1009 (1- mh-scan-field-subject-start-offset)))
1010 (mh-thread-last-ancestor nil))
1011 (mh-thread-generate-scan-lines thread-tree -2))
1012 (mh-notate-user-sequences)
1013 (mh-notate-deleted-and-refiled)
1014 (mh-notate-cur)
1015 (set-buffer-modified-p old-buffer-modified-flag))))
1017 (defvar mh-thread-last-ancestor)
1019 (defun mh-thread-generate-scan-lines (tree level)
1020 "Generate scan lines.
1021 TREE is the hierarchical tree of messages, SCAN-LINE-MAP maps message indices
1022 to the corresponding scan lines and LEVEL used to determine indentation of
1023 the message."
1024 (cond ((null tree) nil)
1025 ((mh-thread-container-p tree)
1026 (let* ((message (mh-container-message tree))
1027 (id (mh-message-id message))
1028 (index (gethash id mh-thread-id-index-map))
1029 (duplicates (gethash id mh-thread-duplicates))
1030 (new-level (+ level 2))
1031 (dupl-flag t)
1032 (force-angle-flag nil)
1033 (increment-level-flag nil))
1034 (dolist (scan-line (mapcar (lambda (x)
1035 (gethash x mh-thread-scan-line-map))
1036 (reverse (cons index duplicates))))
1037 (when scan-line
1038 (when (and dupl-flag (equal level 0)
1039 (mh-thread-ancestor-p mh-thread-last-ancestor tree))
1040 (setq level (+ level 2)
1041 new-level (+ new-level 2)
1042 force-angle-flag t))
1043 (when (equal level 0)
1044 (setq mh-thread-last-ancestor tree)
1045 (while (mh-container-parent mh-thread-last-ancestor)
1046 (setq mh-thread-last-ancestor
1047 (mh-container-parent mh-thread-last-ancestor))))
1048 (let* ((lev (if dupl-flag level new-level))
1049 (square-flag (or (and (mh-container-real-child-p tree)
1050 (not force-angle-flag)
1051 dupl-flag)
1052 (equal lev 0))))
1053 (insert (car scan-line)
1054 (format (format "%%%ss" lev) "")
1055 (if square-flag "[" "<")
1056 (cadr scan-line)
1057 (if square-flag "]" ">")
1058 (truncate-string-to-width
1059 (caddr scan-line) (- mh-thread-body-width lev))
1060 "\n"))
1061 (setq increment-level-flag t)
1062 (setq dupl-flag nil)))
1063 (unless increment-level-flag (setq new-level level))
1064 (dolist (child (mh-container-children tree))
1065 (mh-thread-generate-scan-lines child new-level))))
1066 (t (let ((nlevel (+ level 2)))
1067 (dolist (ch tree)
1068 (mh-thread-generate-scan-lines ch nlevel))))))
1070 ;; Another and may be better approach would be to generate all the info from
1071 ;; the scan which generates the threading info. For now this will have to do.
1072 (defun mh-thread-parse-scan-line (&optional string)
1073 "Parse a scan line.
1074 If optional argument STRING is given then that is assumed to be the scan line.
1075 Otherwise uses the line at point as the scan line to parse."
1076 (let* ((string (or string
1077 (buffer-substring-no-properties (line-beginning-position)
1078 (line-end-position))))
1079 (first-string (substring string 0 (+ mh-cmd-note 8))))
1080 (setf (elt first-string mh-cmd-note) ? )
1081 (when (equal (elt first-string (1+ mh-cmd-note)) (elt mh-note-seq 0))
1082 (setf (elt first-string (1+ mh-cmd-note)) ? ))
1083 (list first-string
1084 (substring string
1085 (+ mh-cmd-note mh-scan-field-from-start-offset)
1086 (+ mh-cmd-note mh-scan-field-from-end-offset -2))
1087 (substring string (+ mh-cmd-note mh-scan-field-from-end-offset))
1088 string)))
1090 ;;;###mh-autoload
1091 (defun mh-thread-add-spaces (count)
1092 "Add COUNT spaces to each scan line in `mh-thread-scan-line-map'."
1093 (let ((spaces (format (format "%%%ss" count) "")))
1094 (while (not (eobp))
1095 (let* ((msg-num (mh-get-msg-num nil))
1096 (old-line (nth 3 (gethash msg-num mh-thread-scan-line-map))))
1097 (when (numberp msg-num)
1098 (setf (gethash msg-num mh-thread-scan-line-map)
1099 (mh-thread-parse-scan-line (format "%s%s" spaces old-line)))))
1100 (forward-line 1))))
1102 (defun mh-thread-folder ()
1103 "Generate thread view of folder."
1104 (message "Threading %s..." (buffer-name))
1105 (mh-thread-initialize)
1106 (goto-char (point-min))
1107 (let ((msg-list ()))
1108 (while (not (eobp))
1109 (let ((index (mh-get-msg-num nil)))
1110 (when (numberp index)
1111 (push index msg-list)
1112 (setf (gethash index mh-thread-scan-line-map)
1113 (mh-thread-parse-scan-line))))
1114 (forward-line))
1115 (let* ((range (mh-coalesce-msg-list msg-list))
1116 (thread-tree (mh-thread-generate (buffer-name) range)))
1117 (delete-region (point-min) (point-max))
1118 (let ((mh-thread-body-width (- (window-width) mh-cmd-note
1119 (1- mh-scan-field-subject-start-offset)))
1120 (mh-thread-last-ancestor nil))
1121 (mh-thread-generate-scan-lines thread-tree -2))
1122 (mh-notate-user-sequences)
1123 (mh-notate-deleted-and-refiled)
1124 (mh-notate-cur)
1125 (message "Threading %s...done" (buffer-name)))))
1127 ;;;###mh-autoload
1128 (defun mh-toggle-threads ()
1129 "Toggle threaded view of folder."
1130 (interactive)
1131 (let ((msg-at-point (mh-get-msg-num nil))
1132 (old-buffer-modified-flag (buffer-modified-p))
1133 (buffer-read-only nil))
1134 (cond ((memq 'unthread mh-view-ops)
1135 (unless (mh-valid-view-change-operation-p 'unthread)
1136 (error "Can't unthread folder"))
1137 (let ((msg-list ()))
1138 (goto-char (point-min))
1139 (while (not (eobp))
1140 (let ((index (mh-get-msg-num t)))
1141 (when index
1142 (push index msg-list)))
1143 (forward-line))
1144 (mh-scan-folder mh-current-folder
1145 (mapcar #'(lambda (x) (format "%s" x))
1146 (mh-coalesce-msg-list msg-list))
1148 (when mh-index-data
1149 (mh-index-insert-folder-headers)
1150 (mh-notate-cur)))
1151 (t (mh-thread-folder)
1152 (push 'unthread mh-view-ops)))
1153 (when msg-at-point (mh-goto-msg msg-at-point t t))
1154 (set-buffer-modified-p old-buffer-modified-flag)
1155 (mh-recenter nil)))
1157 ;;;###mh-autoload
1158 (defun mh-thread-forget-message (index)
1159 "Forget the message INDEX from the threading tables."
1160 (let* ((id (gethash index mh-thread-index-id-map))
1161 (id-index (gethash id mh-thread-id-index-map))
1162 (duplicates (gethash id mh-thread-duplicates)))
1163 (remhash index mh-thread-index-id-map)
1164 (cond ((and (eql index id-index) (null duplicates))
1165 (remhash id mh-thread-id-index-map))
1166 ((eql index id-index)
1167 (setf (gethash id mh-thread-id-index-map) (car duplicates))
1168 (setf (gethash (car duplicates) mh-thread-index-id-map) id)
1169 (setf (gethash id mh-thread-duplicates) (cdr duplicates)))
1171 (setf (gethash id mh-thread-duplicates)
1172 (remove index duplicates))))))
1176 ;;; Operations on threads
1178 (defun mh-thread-current-indentation-level ()
1179 "Find the number of spaces by which current message is indented."
1180 (save-excursion
1181 (let ((address-start-offset (+ mh-cmd-note mh-scan-date-flag-width
1182 mh-scan-date-width 1))
1183 (level 0))
1184 (beginning-of-line)
1185 (forward-char address-start-offset)
1186 (while (char-equal (char-after) ? )
1187 (incf level)
1188 (forward-char))
1189 level)))
1191 ;;;###mh-autoload
1192 (defun mh-thread-next-sibling (&optional previous-flag)
1193 "Jump to next sibling.
1194 With non-nil optional argument PREVIOUS-FLAG jump to the previous sibling."
1195 (interactive)
1196 (cond ((not (memq 'unthread mh-view-ops))
1197 (error "Folder isn't threaded"))
1198 ((eobp)
1199 (error "No message at point")))
1200 (beginning-of-line)
1201 (let ((point (point))
1202 (done nil)
1203 (my-level (mh-thread-current-indentation-level)))
1204 (while (and (not done)
1205 (equal (forward-line (if previous-flag -1 1)) 0)
1206 (not (eobp)))
1207 (let ((level (mh-thread-current-indentation-level)))
1208 (cond ((equal level my-level)
1209 (setq done 'success))
1210 ((< level my-level)
1211 (message "No %s sibling" (if previous-flag "previous" "next"))
1212 (setq done 'failure)))))
1213 (cond ((eq done 'success) (mh-maybe-show))
1214 ((eq done 'failure) (goto-char point))
1215 (t (message "No %s sibling" (if previous-flag "previous" "next"))
1216 (goto-char point)))))
1218 ;;;###mh-autoload
1219 (defun mh-thread-previous-sibling ()
1220 "Jump to previous sibling."
1221 (interactive)
1222 (mh-thread-next-sibling t))
1224 (defun mh-thread-immediate-ancestor ()
1225 "Jump to immediate ancestor in thread tree."
1226 (beginning-of-line)
1227 (let ((point (point))
1228 (ancestor-level (- (mh-thread-current-indentation-level) 2))
1229 (done nil))
1230 (if (< ancestor-level 0)
1232 (while (and (not done) (equal (forward-line -1) 0))
1233 (when (equal ancestor-level (mh-thread-current-indentation-level))
1234 (setq done t)))
1235 (unless done
1236 (goto-char point))
1237 done)))
1239 ;;;###mh-autoload
1240 (defun mh-thread-ancestor (&optional thread-root-flag)
1241 "Jump to the ancestor of current message.
1242 If optional argument THREAD-ROOT-FLAG is non-nil then jump to the root of the
1243 thread tree the message belongs to."
1244 (interactive "P")
1245 (beginning-of-line)
1246 (cond ((not (memq 'unthread mh-view-ops))
1247 (error "Folder isn't threaded"))
1248 ((eobp)
1249 (error "No message at point")))
1250 (let ((current-level (mh-thread-current-indentation-level)))
1251 (cond (thread-root-flag
1252 (while (mh-thread-immediate-ancestor))
1253 (mh-maybe-show))
1254 ((equal current-level 1)
1255 (message "Message has no ancestor"))
1256 (t (mh-thread-immediate-ancestor)
1257 (mh-maybe-show)))))
1259 (defun mh-thread-find-children ()
1260 "Return a region containing the current message and its children.
1261 The result is returned as a list of two elements. The first is the point at the
1262 start of the region and the second is the point at the end."
1263 (beginning-of-line)
1264 (if (eobp)
1266 (let ((address-start-offset (+ mh-cmd-note mh-scan-date-flag-width
1267 mh-scan-date-width 1))
1268 (level (mh-thread-current-indentation-level))
1269 spaces begin)
1270 (setq begin (point))
1271 (setq spaces (format (format "%%%ss" (1+ level)) ""))
1272 (forward-line)
1273 (block nil
1274 (while (not (eobp))
1275 (forward-char address-start-offset)
1276 (unless (equal (string-match spaces (buffer-substring-no-properties
1277 (point) (line-end-position)))
1279 (beginning-of-line)
1280 (backward-char)
1281 (return))
1282 (forward-line)))
1283 (list begin (point)))))
1285 ;;;###mh-autoload
1286 (defun mh-thread-delete ()
1287 "Mark current message and all its children for subsequent deletion."
1288 (interactive)
1289 (cond ((not (memq 'unthread mh-view-ops))
1290 (error "Folder isn't threaded"))
1291 ((eobp)
1292 (error "No message at point"))
1293 (t (let ((region (mh-thread-find-children)))
1294 (mh-iterate-on-messages-in-region () (car region) (cadr region)
1295 (mh-delete-a-msg nil))
1296 (mh-next-msg)))))
1298 ;;;###mh-autoload
1299 (defun mh-thread-refile (folder)
1300 "Mark current message and all its children for refiling to FOLDER."
1301 (interactive (list (intern (mh-prompt-for-refile-folder))))
1302 (cond ((not (memq 'unthread mh-view-ops))
1303 (error "Folder isn't threaded"))
1304 ((eobp)
1305 (error "No message at point"))
1306 (t (let ((region (mh-thread-find-children)))
1307 (mh-iterate-on-messages-in-region () (car region) (cadr region)
1308 (mh-refile-a-msg nil folder))
1309 (mh-next-msg)))))
1311 (provide 'mh-seq)
1313 ;;; Local Variables:
1314 ;;; indent-tabs-mode: nil
1315 ;;; sentence-end-double-space: nil
1316 ;;; End:
1318 ;;; mh-seq.el ends here