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>
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)
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.
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
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
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
64 ;; (5) Better canonicalizing for message identifier and subject strings.
67 ;; Internal support for MH-E package.
71 ;; $Id: mh-seq.el,v 1.101 2003/01/26 00:57:35 jchonig Exp $
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
))
89 (defstruct (mh-thread-container (:conc-name mh-container-
)
90 (:constructor mh-thread-make-container
))
91 message parent children
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
)
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
)
156 (defun mh-list-sequences ()
157 "List the sequences defined in the folder being visited."
159 (let ((folder mh-current-folder
)
160 (temp-buffer mh-sequences-buffer
)
161 (seq-list mh-seq-list
)
163 (with-output-to-temp-buffer temp-buffer
165 (set-buffer temp-buffer
)
167 (message "Listing sequences ...")
168 (insert "Sequences in folder " folder
":\n")
169 (let ((seq-list seq-list
))
172 (max (length (symbol-name (mh-seq-name (pop seq-list
))))
174 (setq max-len
(+ 2 max-len
)))
176 (let ((name (mh-seq-name (car seq-list
)))
178 (mh-coalesce-msg-list
179 (sort (copy-sequence (mh-seq-msgs (car seq-list
))) '<)))
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
))
187 (insert (format (format "%%%ss" (length name-spec
)) "")))
188 (insert next-element
)))
190 (setq seq-list
(cdr seq-list
)))
191 (goto-char (point-min))
193 (setq view-exit-action
'kill-buffer
)
194 (message "Listing sequences...done")))))
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"
205 (cond (dest-folder (format " (to be refiled to %s)" dest-folder
))
206 (deleted-flag (format " (to be deleted)"))
209 (mh-list-to-string (mh-seq-containing-msg message t
))
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
)
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)
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)
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
))))))
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)))
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
))
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
)
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
)
284 "Remove restrictions from current folder, thereby showing all messages."
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
)))
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))
299 (setq mh-mode-line-annotation mh-non-seq-mode-line-annotation
)
300 (mh-make-folder-mode-line))
302 (mh-goto-msg msg t t
))
303 (mh-notate-deleted-and-refiled)
304 (mh-notate-user-sequences)
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
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
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:"
352 (format "[%s] " default
)
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
)
359 (msgs (mh-seq-to-msgs seq
)))
360 (if (and (null msgs
) not-empty
)
361 (error "No messages in sequence `%s'" 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
))))
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
)))
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
)))
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."
388 (let ((msgs (if (listp seq
) seq
(mh-seq-to-msgs seq
))))
390 (if (mh-goto-msg (car msgs
) t t
)
391 (apply func
(car msgs
) args
))
392 (setq msgs
(cdr msgs
))))))
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
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
)))))
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
)
413 (setq mh-arrow-marker
(set-marker mh-arrow-marker
(point)))
414 (setq overlay-arrow-position mh-arrow-marker
))))
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
))
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.
441 (let* ((msgs (mh-seq-to-msgs seq
))
442 (coalesced-msgs (mh-coalesce-msg-list msgs
)))
443 (goto-char (point-max))
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))
451 (let ((msg (mh-get-msg-num nil
)))
453 (setf (gethash msg mh-thread-scan-line-map
)
454 (mh-thread-parse-scan-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)))
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
472 ;; Copy the current line to the LOCATION in the current buffer.
475 (let ((beginning-of-line (point))
480 (insert-buffer-substring (current-buffer) beginning-of-line end
))))
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
))
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
))) ())
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
))
506 (mh-iterate-on-messages-in-region index begin end
507 (when (numberp index
) (push index 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"))
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.")
534 (let ((subject (match-string-no-properties 3))
536 (if (> (length subject
) 41)
537 (setq subject
(substring subject
0 41)))
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
))))))
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
)))
557 (mh-add-msgs-to-seq (car sorted-list
) 'subject nil
)
558 (setq sorted-list
(cdr sorted-list
)))
564 (defun mh-narrow-to-subject ()
565 "Narrow to a sequence containing all following messages with same subject."
567 (let ((num (mh-get-msg-num nil
))
568 (count (mh-subject-to-sequence t
)))
570 ((not count
) ; No subject line, delete msg anyway
572 ((= 0 count
) ; No other msgs, delete msg anyway.
573 (message "No other messages with same Subject following this one.")
575 (t ; We have a subject sequence.
576 (message "Found %d messages for subject sequence." count
)
577 (mh-narrow-to-seq 'subject
)
579 (mh-goto-msg num t t
))))))
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
588 (let ((count (mh-subject-to-sequence nil
)))
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
)))))
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."
607 (if (memq 'unthread mh-view-ops
)
609 (mh-delete-subject)))
611 ;;; Message threading:
613 (defun mh-thread-initialize ()
614 "Make hash tables, otherwise clear them."
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
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
690 (when (eq ancestor successor
) (return t
))
691 (setq successor
(mh-container-parent successor
)))
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
)
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
)))
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
)
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
723 :subject-re-p subject-re-p
724 :id id
:references refs
)))
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]*"
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
))
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
)))
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 ())
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
)
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
)))
815 (dolist (kid (mh-container-children node
))
816 (mh-thread-remove-parent-link kid
)
817 (mh-thread-add-link (mh-container-parent node
) kid
)
819 (push `(PROMOTE ,node
,(mh-container-parent node
) ,@children
)
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
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
)
837 (mh-thread-remove-parent-link node
)))
840 (push `(DROP ,node
,(mh-container-parent node
))
842 (mh-thread-remove-parent-link node
)))))
844 (maphash #'(lambda (k v
)
846 (when (and (null (mh-container-parent v
))
847 (gethash (mh-message-id (mh-container-message v
))
848 mh-thread-id-index-map
))
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."
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
)
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
)))))
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
)))
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)
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
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."
932 (mh-thread-set-tables folder
)
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))
941 (case-fold-search t
))
944 (block process-message
946 (prog1 (buffer-substring (point) (line-end-position))
948 (index (car (read-from-string index-line
)))
949 (id (prog1 (buffer-substring (point) (line-end-position))
951 (refs (prog1 (buffer-substring (point) (line-end-position))
953 (in-reply-to (prog1 (buffer-substring (point)
957 (buffer-substring (point) (line-end-position))
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
)
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
)
981 (when (null (mh-container-parent v
))
984 (setq roots
(mh-thread-prune-containers roots
))
985 (prog1 (setq roots
(mh-thread-group-by-subject roots
))
986 (let ((history mh-thread-history
))
988 (setq mh-thread-history history
))))))
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
)
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)))
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)
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
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))
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
))))
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
)
1053 (insert (car scan-line
)
1054 (format (format "%%%ss" lev
) "")
1055 (if square-flag
"[" "<")
1057 (if square-flag
"]" ">")
1058 (truncate-string-to-width
1059 (caddr scan-line
) (- mh-thread-body-width lev
))
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)))
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
)
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
)) ?
))
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
))
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
) "")))
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
)))))
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 ()))
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))))
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)
1125 (message "Threading %s...done" (buffer-name)))))
1128 (defun mh-toggle-threads ()
1129 "Toggle threaded view of folder."
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))
1140 (let ((index (mh-get-msg-num t
)))
1142 (push index msg-list
)))
1144 (mh-scan-folder mh-current-folder
1145 (mapcar #'(lambda (x) (format "%s" x
))
1146 (mh-coalesce-msg-list msg-list
))
1149 (mh-index-insert-folder-headers)
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
)
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."
1181 (let ((address-start-offset (+ mh-cmd-note mh-scan-date-flag-width
1182 mh-scan-date-width
1))
1185 (forward-char address-start-offset
)
1186 (while (char-equal (char-after) ?
)
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."
1196 (cond ((not (memq 'unthread mh-view-ops
))
1197 (error "Folder isn't threaded"))
1199 (error "No message at point")))
1201 (let ((point (point))
1203 (my-level (mh-thread-current-indentation-level)))
1204 (while (and (not done
)
1205 (equal (forward-line (if previous-flag -
1 1)) 0)
1207 (let ((level (mh-thread-current-indentation-level)))
1208 (cond ((equal level my-level
)
1209 (setq done
'success
))
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
)))))
1219 (defun mh-thread-previous-sibling ()
1220 "Jump to previous sibling."
1222 (mh-thread-next-sibling t
))
1224 (defun mh-thread-immediate-ancestor ()
1225 "Jump to immediate ancestor in thread tree."
1227 (let ((point (point))
1228 (ancestor-level (- (mh-thread-current-indentation-level) 2))
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))
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."
1246 (cond ((not (memq 'unthread mh-view-ops
))
1247 (error "Folder isn't threaded"))
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))
1254 ((equal current-level
1)
1255 (message "Message has no ancestor"))
1256 (t (mh-thread-immediate-ancestor)
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."
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))
1270 (setq begin
(point))
1271 (setq spaces
(format (format "%%%ss" (1+ level
)) ""))
1275 (forward-char address-start-offset
)
1276 (unless (equal (string-match spaces
(buffer-substring-no-properties
1277 (point) (line-end-position)))
1283 (list begin
(point)))))
1286 (defun mh-thread-delete ()
1287 "Mark current message and all its children for subsequent deletion."
1289 (cond ((not (memq 'unthread mh-view-ops
))
1290 (error "Folder isn't threaded"))
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
))
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"))
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
))
1313 ;;; Local Variables:
1314 ;;; indent-tabs-mode: nil
1315 ;;; sentence-end-double-space: nil
1318 ;;; mh-seq.el ends here