Fix wording of comments in w32fns.c.
[emacs.git] / lisp / mh-e / mh-thread.el
blob48c06c3df877a9dab6cd977867df0581b49636f6
1 ;;; mh-thread.el --- MH-E threading support
3 ;; Copyright (C) 2002-2004, 2006-2012 Free Software Foundation, Inc.
5 ;; Author: Satyaki Das <satyaki@theforce.stanford.edu>
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 3 of the License, or
15 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; The threading portion of this files tries to implement the
28 ;; algorithm described at:
29 ;; http://www.jwz.org/doc/threading.html
30 ;; It also begins to implement the IMAP Threading extension RFC. The
31 ;; implementation lacks the reference and subject canonicalization of
32 ;; the RFC.
34 ;; In the presentation buffer, children messages are shown indented
35 ;; with either [ ] or < > around them. Square brackets ([ ]) denote
36 ;; that the algorithm can point out some headers which when taken
37 ;; together implies that the unindented message is an ancestor of the
38 ;; indented message. If no such proof exists then angles (< >) are
39 ;; used.
41 ;; If threading is slow on your machine, compile this file. Of all the
42 ;; files in MH-E, this one really benefits from compilation.
44 ;; Some issues and problems are as follows:
46 ;; (1) Scan truncates the fields at length 512. So longer
47 ;; references: headers get mutilated. The same kind of MH
48 ;; format string works when composing messages. Is there a way
49 ;; to avoid this? My scan command is as follows:
50 ;; scan +folder -width 10000 \
51 ;; -format "%(msg)\n%{message-id}\n%{references}\n%{subject}\n"
52 ;; I would really appreciate it if someone would help me with this.
54 ;; (2) Implement heuristics to recognize message identifiers in
55 ;; In-Reply-To: header. Right now it just assumes that the last
56 ;; text between angles (< and >) is the message identifier.
57 ;; There is the chance that this will incorrectly use an email
58 ;; address like a message identifier.
60 ;; (3) Error checking of found message identifiers should be done.
62 ;; (4) Since this breaks the assumption that message indices
63 ;; increase as one goes down the buffer, the binary search
64 ;; based mh-goto-msg doesn't work. I have a simpler replacement
65 ;; which may be less efficient.
67 ;; (5) Better canonicalizing for message identifier and subject
68 ;; strings.
70 ;;; Change Log:
72 ;;; Code:
74 (require 'mh-e)
75 (require 'mh-scan)
77 (mh-defstruct (mh-thread-message (:conc-name mh-message-)
78 (:constructor mh-thread-make-message))
79 (id nil)
80 (references ())
81 (subject "")
82 (subject-re-p nil))
84 (mh-defstruct (mh-thread-container (:conc-name mh-container-)
85 (:constructor mh-thread-make-container))
86 message parent children
87 (real-child-p t))
89 (defvar mh-thread-id-hash nil
90 "Hashtable used to canonicalize message identifiers.")
91 (make-variable-buffer-local 'mh-thread-id-hash)
93 (defvar mh-thread-subject-hash nil
94 "Hashtable used to canonicalize subject strings.")
95 (make-variable-buffer-local 'mh-thread-subject-hash)
97 (defvar mh-thread-id-table nil
98 "Thread ID table maps from message identifiers to message containers.")
99 (make-variable-buffer-local 'mh-thread-id-table)
101 (defvar mh-thread-index-id-map nil
102 "Table to look up message identifier from message index.")
103 (make-variable-buffer-local 'mh-thread-index-id-map)
105 (defvar mh-thread-id-index-map nil
106 "Table to look up message index number from message identifier.")
107 (make-variable-buffer-local 'mh-thread-id-index-map)
109 (defvar mh-thread-subject-container-hash nil
110 "Hashtable used to group messages by subject.")
111 (make-variable-buffer-local 'mh-thread-subject-container-hash)
113 (defvar mh-thread-duplicates nil
114 "Hashtable used to associate messages with the same message identifier.")
115 (make-variable-buffer-local 'mh-thread-duplicates)
117 (defvar mh-thread-history ()
118 "Variable to remember the transformations to the thread tree.
119 When new messages are added, these transformations are rewound,
120 then the links are added from the newly seen messages. Finally
121 the transformations are redone to get the new thread tree. This
122 makes incremental threading easier.")
123 (make-variable-buffer-local 'mh-thread-history)
125 (defvar mh-thread-body-width nil
126 "Width of scan substring that contains subject and body of message.")
130 ;;; MH-Folder Commands
132 ;;;###mh-autoload
133 (defun mh-thread-ancestor (&optional thread-root-flag)
134 "Display ancestor of current message.
136 If you do not care for the way a particular thread has turned,
137 you can move up the chain of messages with this command. This
138 command can also take a prefix argument THREAD-ROOT-FLAG to jump
139 to the message that started everything."
140 (interactive "P")
141 (beginning-of-line)
142 (cond ((not (memq 'unthread mh-view-ops))
143 (error "Folder isn't threaded"))
144 ((eobp)
145 (error "No message at point")))
146 (let ((current-level (mh-thread-current-indentation-level)))
147 (cond (thread-root-flag
148 (while (mh-thread-immediate-ancestor))
149 (mh-maybe-show))
150 ((equal current-level 1)
151 (message "Message has no ancestor"))
152 (t (mh-thread-immediate-ancestor)
153 (mh-maybe-show)))))
155 ;;;###mh-autoload
156 (defun mh-thread-delete ()
157 "Delete thread."
158 (interactive)
159 (cond ((not (memq 'unthread mh-view-ops))
160 (error "Folder isn't threaded"))
161 ((eobp)
162 (error "No message at point"))
163 (t (let ((region (mh-thread-find-children)))
164 (mh-iterate-on-messages-in-region () (car region) (cadr region)
165 (mh-delete-a-msg nil))
166 (mh-next-msg)))))
168 ;;;###mh-autoload
169 (defun mh-thread-next-sibling (&optional previous-flag)
170 "Display next sibling.
172 With non-nil optional argument PREVIOUS-FLAG jump to the previous
173 sibling."
174 (interactive)
175 (cond ((not (memq 'unthread mh-view-ops))
176 (error "Folder isn't threaded"))
177 ((eobp)
178 (error "No message at point")))
179 (beginning-of-line)
180 (let ((point (point))
181 (done nil)
182 (my-level (mh-thread-current-indentation-level)))
183 (while (and (not done)
184 (equal (forward-line (if previous-flag -1 1)) 0)
185 (not (eobp)))
186 (let ((level (mh-thread-current-indentation-level)))
187 (cond ((equal level my-level)
188 (setq done 'success))
189 ((< level my-level)
190 (message "No %s sibling" (if previous-flag "previous" "next"))
191 (setq done 'failure)))))
192 (cond ((eq done 'success) (mh-maybe-show))
193 ((eq done 'failure) (goto-char point))
194 (t (message "No %s sibling" (if previous-flag "previous" "next"))
195 (goto-char point)))))
197 ;;;###mh-autoload
198 (defun mh-thread-previous-sibling ()
199 "Display previous sibling."
200 (interactive)
201 (mh-thread-next-sibling t))
203 ;;;###mh-autoload
204 (defun mh-thread-refile (folder)
205 "Refile (output) thread into FOLDER."
206 (interactive (list (intern (mh-prompt-for-refile-folder))))
207 (cond ((not (memq 'unthread mh-view-ops))
208 (error "Folder isn't threaded"))
209 ((eobp)
210 (error "No message at point"))
211 (t (let ((region (mh-thread-find-children)))
212 (mh-iterate-on-messages-in-region () (car region) (cadr region)
213 (mh-refile-a-msg nil folder))
214 (mh-next-msg)))))
216 ;;;###mh-autoload
217 (defun mh-toggle-threads ()
218 "Toggle threaded view of folder."
219 (interactive)
220 (let ((msg-at-point (mh-get-msg-num nil))
221 (old-buffer-modified-flag (buffer-modified-p))
222 (buffer-read-only nil))
223 (cond ((memq 'unthread mh-view-ops)
224 (unless (mh-valid-view-change-operation-p 'unthread)
225 (error "Can't unthread folder"))
226 (let ((msg-list ()))
227 (goto-char (point-min))
228 (while (not (eobp))
229 (let ((index (mh-get-msg-num nil)))
230 (when index
231 (push index msg-list)))
232 (forward-line))
233 (mh-scan-folder mh-current-folder
234 (mapcar #'(lambda (x) (format "%s" x))
235 (mh-coalesce-msg-list msg-list))
237 (when mh-index-data
238 (mh-index-insert-folder-headers)
239 (mh-notate-cur)))
240 (t (mh-thread-folder)
241 (push 'unthread mh-view-ops)))
242 (when msg-at-point (mh-goto-msg msg-at-point t t))
243 (set-buffer-modified-p old-buffer-modified-flag)
244 (mh-recenter nil)))
248 ;;; Support Routines
250 (defun mh-thread-current-indentation-level ()
251 "Find the number of spaces by which current message is indented."
252 (save-excursion
253 (let ((address-start-offset (+ mh-cmd-note mh-scan-date-flag-width
254 mh-scan-date-width 1))
255 (level 0))
256 (beginning-of-line)
257 (forward-char address-start-offset)
258 (while (char-equal (char-after) ? )
259 (incf level)
260 (forward-char))
261 level)))
263 (defun mh-thread-immediate-ancestor ()
264 "Jump to immediate ancestor in thread tree."
265 (beginning-of-line)
266 (let ((point (point))
267 (ancestor-level (- (mh-thread-current-indentation-level) 2))
268 (done nil))
269 (if (< ancestor-level 0)
271 (while (and (not done) (equal (forward-line -1) 0))
272 (when (equal ancestor-level (mh-thread-current-indentation-level))
273 (setq done t)))
274 (unless done
275 (goto-char point))
276 done)))
278 (defun mh-thread-find-children ()
279 "Return a region containing the current message and its children.
280 The result is returned as a list of two elements. The first is
281 the point at the start of the region and the second is the point
282 at the end."
283 (beginning-of-line)
284 (if (eobp)
286 (let ((address-start-offset (+ mh-cmd-note mh-scan-date-flag-width
287 mh-scan-date-width 1))
288 (level (mh-thread-current-indentation-level))
289 spaces begin)
290 (setq begin (point))
291 (setq spaces (format (format "%%%ss" (1+ level)) ""))
292 (forward-line)
293 (block nil
294 (while (not (eobp))
295 (forward-char address-start-offset)
296 (unless (equal (string-match spaces (buffer-substring-no-properties
297 (point) (mh-line-end-position)))
299 (beginning-of-line)
300 (backward-char)
301 (return))
302 (forward-line)))
303 (list begin (point)))))
307 ;;; Thread Creation
309 (defun mh-thread-folder ()
310 "Generate thread view of folder."
311 (message "Threading %s..." (buffer-name))
312 (mh-thread-initialize)
313 (goto-char (point-min))
314 (mh-remove-all-notation)
315 (let ((msg-list ()))
316 (mh-iterate-on-range msg (cons (point-min) (point-max))
317 (setf (gethash msg mh-thread-scan-line-map) (mh-thread-parse-scan-line))
318 (push msg msg-list))
319 (let* ((range (mh-coalesce-msg-list msg-list))
320 (thread-tree (mh-thread-generate (buffer-name) range)))
321 (delete-region (point-min) (point-max))
322 (mh-thread-print-scan-lines thread-tree)
323 (mh-notate-user-sequences)
324 (mh-notate-deleted-and-refiled)
325 (mh-notate-cur)
326 (message "Threading %s...done" (buffer-name)))))
328 ;;;###mh-autoload
329 (defun mh-thread-inc (folder start-point)
330 "Update thread tree for FOLDER.
331 All messages after START-POINT are added to the thread tree."
332 (mh-thread-rewind-pruning)
333 (mh-remove-all-notation)
334 (goto-char start-point)
335 (let ((msg-list ()))
336 (while (not (eobp))
337 (let ((index (mh-get-msg-num nil)))
338 (when (numberp index)
339 (push index msg-list)
340 (setf (gethash index mh-thread-scan-line-map)
341 (mh-thread-parse-scan-line)))
342 (forward-line)))
343 (let ((thread-tree (mh-thread-generate folder msg-list))
344 (buffer-read-only nil)
345 (old-buffer-modified-flag (buffer-modified-p)))
346 (delete-region (point-min) (point-max))
347 (mh-thread-print-scan-lines thread-tree)
348 (mh-notate-user-sequences)
349 (mh-notate-deleted-and-refiled)
350 (mh-notate-cur)
351 (set-buffer-modified-p old-buffer-modified-flag))))
353 (defmacro mh-thread-initialize-hash (var test)
354 "Initialize the hash table in VAR.
355 TEST is the test to use when creating a new hash table."
356 (unless (symbolp var) (error "Expected a symbol: %s" var))
357 `(if ,var (clrhash ,var) (setq ,var (make-hash-table :test ,test))))
359 (defun mh-thread-initialize ()
360 "Make new hash tables, or clear them if already present."
361 (mh-thread-initialize-hash mh-thread-id-hash #'equal)
362 (mh-thread-initialize-hash mh-thread-subject-hash #'equal)
363 (mh-thread-initialize-hash mh-thread-id-table #'eq)
364 (mh-thread-initialize-hash mh-thread-id-index-map #'eq)
365 (mh-thread-initialize-hash mh-thread-index-id-map #'eql)
366 (mh-thread-initialize-hash mh-thread-scan-line-map #'eql)
367 (mh-thread-initialize-hash mh-thread-subject-container-hash #'eq)
368 (mh-thread-initialize-hash mh-thread-duplicates #'eq)
369 (setq mh-thread-history ()))
371 (defsubst mh-thread-id-container (id)
372 "Given ID, return the corresponding container in `mh-thread-id-table'.
373 If no container exists then a suitable container is created and
374 the id-table is updated."
375 (when (not id)
376 (error "1"))
377 (or (gethash id mh-thread-id-table)
378 (setf (gethash id mh-thread-id-table)
379 (let ((message (mh-thread-make-message :id id)))
380 (mh-thread-make-container :message message)))))
382 (defsubst mh-thread-remove-parent-link (child)
383 "Remove parent link of CHILD if it exists."
384 (let* ((child-container (if (mh-thread-container-p child)
385 child (mh-thread-id-container child)))
386 (parent-container (mh-container-parent child-container)))
387 (when parent-container
388 (setf (mh-container-children parent-container)
389 (loop for elem in (mh-container-children parent-container)
390 unless (eq child-container elem) collect elem))
391 (setf (mh-container-parent child-container) nil))))
393 (defsubst mh-thread-add-link (parent child &optional at-end-p)
394 "Add links so that PARENT becomes a parent of CHILD.
395 Doesn't make any changes if CHILD is already an ancestor of
396 PARENT. If optional argument AT-END-P is non-nil, the CHILD is
397 added to the end of the children list of PARENT."
398 (let ((parent-container (cond ((null parent) nil)
399 ((mh-thread-container-p parent) parent)
400 (t (mh-thread-id-container parent))))
401 (child-container (if (mh-thread-container-p child)
402 child (mh-thread-id-container child))))
403 (when (and parent-container
404 (not (mh-thread-ancestor-p child-container parent-container))
405 (not (mh-thread-ancestor-p parent-container child-container)))
406 (mh-thread-remove-parent-link child-container)
407 (cond ((not at-end-p)
408 (push child-container (mh-container-children parent-container)))
409 ((null (mh-container-children parent-container))
410 (push child-container (mh-container-children parent-container)))
411 (t (let ((last-child (mh-container-children parent-container)))
412 (while (cdr last-child)
413 (setq last-child (cdr last-child)))
414 (setcdr last-child (cons child-container nil)))))
415 (setf (mh-container-parent child-container) parent-container))
416 (unless parent-container
417 (mh-thread-remove-parent-link child-container))))
419 (defun mh-thread-rewind-pruning ()
420 "Restore the thread tree to its state before pruning."
421 (while mh-thread-history
422 (let ((action (pop mh-thread-history)))
423 (cond ((eq (car action) 'DROP)
424 (mh-thread-remove-parent-link (cadr action))
425 (mh-thread-add-link (caddr action) (cadr action)))
426 ((eq (car action) 'PROMOTE)
427 (let ((node (cadr action))
428 (parent (caddr action))
429 (children (cdddr action)))
430 (dolist (child children)
431 (mh-thread-remove-parent-link child)
432 (mh-thread-add-link node child))
433 (mh-thread-add-link parent node)))
434 ((eq (car action) 'SUBJECT)
435 (let ((node (cadr action)))
436 (mh-thread-remove-parent-link node)
437 (setf (mh-container-real-child-p node) t)))))))
439 (defun mh-thread-ancestor-p (ancestor successor)
440 "Return t if ANCESTOR is really an ancestor of SUCCESSOR and nil otherwise.
441 In the limit, the function returns t if ANCESTOR and SUCCESSOR
442 are the same containers."
443 (block nil
444 (while successor
445 (when (eq ancestor successor) (return t))
446 (setq successor (mh-container-parent successor)))
447 nil))
449 ;; Another and may be better approach would be to generate all the info from
450 ;; the scan which generates the threading info. For now this will have to do.
451 ;;;###mh-autoload
452 (defun mh-thread-parse-scan-line (&optional string)
453 "Parse a scan line.
454 If optional argument STRING is given then that is assumed to be
455 the scan line. Otherwise uses the line at point as the scan line
456 to parse."
457 (let* ((string (or string (buffer-substring-no-properties
458 (mh-line-beginning-position)
459 (mh-line-end-position))))
460 (address-start (+ mh-cmd-note mh-scan-field-from-start-offset))
461 (body-start (+ mh-cmd-note mh-scan-field-from-end-offset))
462 (first-string (substring string 0 address-start)))
463 (list first-string
464 (substring string address-start (- body-start 2))
465 (substring string body-start)
466 string)))
468 (defsubst mh-thread-canonicalize-id (id)
469 "Produce canonical string representation for ID.
470 This allows cheap string comparison with EQ."
471 (or (and (equal id "") (copy-sequence ""))
472 (gethash id mh-thread-id-hash)
473 (setf (gethash id mh-thread-id-hash) id)))
475 (defsubst mh-thread-prune-subject (subject)
476 "Prune leading Re:'s, Fwd:'s etc. and trailing (fwd)'s from SUBJECT.
477 If the result after pruning is not the empty string then it is
478 canonicalized so that subjects can be tested for equality with
479 eq. This is done so that all the messages without a subject are
480 not put into a single thread."
481 (let ((case-fold-search t)
482 (subject-pruned-flag nil))
483 ;; Prune subject leader
484 (while (or (string-match "^[ \t]*\\(re\\|fwd?\\)\\(\\[[0-9]*\\]\\)?:[ \t]*"
485 subject)
486 (string-match "^[ \t]*\\[[^\\]][ \t]*" subject))
487 (setq subject-pruned-flag t)
488 (setq subject (substring subject (match-end 0))))
489 ;; Prune subject trailer
490 (while (or (string-match "(fwd)$" subject)
491 (string-match "[ \t]+$" subject))
492 (setq subject-pruned-flag t)
493 (setq subject (substring subject 0 (match-beginning 0))))
494 ;; Canonicalize subject only if it is non-empty
495 (cond ((equal subject "") (list subject subject-pruned-flag))
496 (t (list
497 (or (gethash subject mh-thread-subject-hash)
498 (setf (gethash subject mh-thread-subject-hash) subject))
499 subject-pruned-flag)))))
501 (defsubst mh-thread-group-by-subject (roots)
502 "Group the set of message containers, ROOTS based on subject.
503 Bug: Check for and make sure that something without Re: is made
504 the parent in preference to something that has it."
505 (clrhash mh-thread-subject-container-hash)
506 (let ((results ()))
507 (dolist (root roots)
508 (let* ((subject (mh-thread-container-subject root))
509 (parent (gethash subject mh-thread-subject-container-hash)))
510 (cond (parent (mh-thread-remove-parent-link root)
511 (mh-thread-add-link parent root t)
512 (setf (mh-container-real-child-p root) nil)
513 (push `(SUBJECT ,root) mh-thread-history))
515 (setf (gethash subject mh-thread-subject-container-hash) root)
516 (push root results)))))
517 (nreverse results)))
519 (defun mh-thread-container-subject (container)
520 "Return the subject of CONTAINER.
521 If CONTAINER is empty return the subject info of one of its
522 children."
523 (cond ((and (mh-container-message container)
524 (mh-message-id (mh-container-message container)))
525 (mh-message-subject (mh-container-message container)))
526 (t (block nil
527 (dolist (kid (mh-container-children container))
528 (when (and (mh-container-message kid)
529 (mh-message-id (mh-container-message kid)))
530 (let ((kid-message (mh-container-message kid)))
531 (return (mh-message-subject kid-message)))))
532 (error "This can't happen")))))
534 (defsubst mh-thread-update-id-index-maps (id index)
535 "Message with id, ID is the message in INDEX.
536 The function also checks for duplicate messages (that is multiple
537 messages with the same ID). These messages are put in the
538 `mh-thread-duplicates' hash table."
539 (let ((old-index (gethash id mh-thread-id-index-map)))
540 (when old-index (push old-index (gethash id mh-thread-duplicates)))
541 (setf (gethash id mh-thread-id-index-map) index)
542 (setf (gethash index mh-thread-index-id-map) id)))
544 (defsubst mh-thread-get-message-container (message)
545 "Return container which has MESSAGE in it.
546 If there is no container present then a new container is
547 allocated."
548 (let* ((id (mh-message-id message))
549 (container (gethash id mh-thread-id-table)))
550 (cond (container (setf (mh-container-message container) message)
551 container)
552 (t (setf (gethash id mh-thread-id-table)
553 (mh-thread-make-container :message message))))))
555 (defsubst mh-thread-get-message (id subject-re-p subject refs)
556 "Return appropriate message.
557 Otherwise update message already present to have the proper ID,
558 SUBJECT-RE-P, SUBJECT and REFS fields."
559 (let* ((container (gethash id mh-thread-id-table))
560 (message (if container (mh-container-message container) nil)))
561 (cond (message
562 (setf (mh-message-subject-re-p message) subject-re-p)
563 (setf (mh-message-subject message) subject)
564 (setf (mh-message-id message) id)
565 (setf (mh-message-references message) refs)
566 message)
567 (container
568 (setf (mh-container-message container)
569 (mh-thread-make-message :id id :references refs
570 :subject subject
571 :subject-re-p subject-re-p)))
572 (t (let ((message (mh-thread-make-message :id id :references refs
573 :subject-re-p subject-re-p
574 :subject subject)))
575 (prog1 message
576 (mh-thread-get-message-container message)))))))
578 (defvar mh-message-id-regexp "^<.*@.*>$"
579 "Regexp to recognize whether a string is a message identifier.")
581 ;;;###mh-autoload
582 (defun mh-thread-generate (folder msg-list)
583 "Scan FOLDER to get info for threading.
584 Only information about messages in MSG-LIST are added to the tree."
585 (with-temp-buffer
586 (mh-thread-set-tables folder)
587 (when msg-list
588 (apply
589 #'call-process (expand-file-name mh-scan-prog mh-progs) nil '(t nil) nil
590 "-width" "10000" "-format"
591 "%(msg)\n%{message-id}\n%{references}\n%{in-reply-to}\n%{subject}\n"
592 folder (mapcar #'(lambda (x) (format "%s" x)) msg-list)))
593 (goto-char (point-min))
594 (let ((roots ())
595 (case-fold-search t))
596 (block nil
597 (while (not (eobp))
598 (block process-message
599 (let* ((index-line
600 (prog1 (buffer-substring (point) (mh-line-end-position))
601 (forward-line)))
602 (index (string-to-number index-line))
603 (id (prog1 (buffer-substring (point) (mh-line-end-position))
604 (forward-line)))
605 (refs (prog1
606 (buffer-substring (point) (mh-line-end-position))
607 (forward-line)))
608 (in-reply-to (prog1 (buffer-substring (point)
609 (mh-line-end-position))
610 (forward-line)))
611 (subject (prog1
612 (buffer-substring
613 (point) (mh-line-end-position))
614 (forward-line)))
615 (subject-re-p nil))
616 (unless (gethash index mh-thread-scan-line-map)
617 (return-from process-message))
618 (unless (integerp index) (return)) ;Error message here
619 (multiple-value-setq (subject subject-re-p)
620 (values-list (mh-thread-prune-subject subject)))
621 (setq in-reply-to (mh-thread-process-in-reply-to in-reply-to))
622 (setq refs (loop for x in (append (split-string refs) in-reply-to)
623 when (string-match mh-message-id-regexp x)
624 collect x))
625 (setq id (mh-thread-canonicalize-id id))
626 (mh-thread-update-id-index-maps id index)
627 (setq refs (mapcar #'mh-thread-canonicalize-id refs))
628 (mh-thread-get-message id subject-re-p subject refs)
629 (do ((ancestors refs (cdr ancestors)))
630 ((null (cdr ancestors))
631 (when (car ancestors)
632 (mh-thread-remove-parent-link id)
633 (mh-thread-add-link (car ancestors) id)))
634 (mh-thread-add-link (car ancestors) (cadr ancestors)))))))
635 (maphash #'(lambda (k v)
636 (declare (ignore k))
637 (when (null (mh-container-parent v))
638 (push v roots)))
639 mh-thread-id-table)
640 (setq roots (mh-thread-prune-containers roots))
641 (prog1 (setq roots (mh-thread-group-by-subject roots))
642 (let ((history mh-thread-history))
643 (set-buffer folder)
644 (setq mh-thread-history history))))))
646 (defun mh-thread-set-tables (folder)
647 "Use the tables of FOLDER in current buffer."
648 (flet ((mh-get-table (symbol)
649 (with-current-buffer folder
650 (symbol-value symbol))))
651 (setq mh-thread-id-hash (mh-get-table 'mh-thread-id-hash))
652 (setq mh-thread-subject-hash (mh-get-table 'mh-thread-subject-hash))
653 (setq mh-thread-id-table (mh-get-table 'mh-thread-id-table))
654 (setq mh-thread-id-index-map (mh-get-table 'mh-thread-id-index-map))
655 (setq mh-thread-index-id-map (mh-get-table 'mh-thread-index-id-map))
656 (setq mh-thread-scan-line-map (mh-get-table 'mh-thread-scan-line-map))
657 (setq mh-thread-subject-container-hash
658 (mh-get-table 'mh-thread-subject-container-hash))
659 (setq mh-thread-duplicates (mh-get-table 'mh-thread-duplicates))
660 (setq mh-thread-history (mh-get-table 'mh-thread-history))))
662 (defun mh-thread-process-in-reply-to (reply-to-header)
663 "Extract message id's from REPLY-TO-HEADER.
664 Ideally this should have some regexp which will try to guess if a
665 string between < and > is a message id and not an email address.
666 For now it will take the last string inside angles."
667 (let ((end (mh-search-from-end ?> reply-to-header)))
668 (when (numberp end)
669 (let ((begin (mh-search-from-end ?< (substring reply-to-header 0 end))))
670 (when (numberp begin)
671 (list (substring reply-to-header begin (1+ end))))))))
673 (defun mh-thread-prune-containers (roots)
674 "Prune empty containers in the containers ROOTS."
675 (let ((dfs-ordered-nodes ())
676 (work-list roots))
677 (while work-list
678 (let ((node (pop work-list)))
679 (dolist (child (mh-container-children node))
680 (push child work-list))
681 (push node dfs-ordered-nodes)))
682 (while dfs-ordered-nodes
683 (let ((node (pop dfs-ordered-nodes)))
684 (cond ((gethash (mh-message-id (mh-container-message node))
685 mh-thread-id-index-map)
686 ;; Keep it
687 (setf (mh-container-children node)
688 (mh-thread-sort-containers (mh-container-children node))))
689 ((and (mh-container-children node)
690 (or (null (cdr (mh-container-children node)))
691 (mh-container-parent node)))
692 ;; Promote kids
693 (let ((children ()))
694 (dolist (kid (mh-container-children node))
695 (mh-thread-remove-parent-link kid)
696 (mh-thread-add-link (mh-container-parent node) kid)
697 (push kid children))
698 (push `(PROMOTE ,node ,(mh-container-parent node) ,@children)
699 mh-thread-history)
700 (mh-thread-remove-parent-link node)))
701 ((mh-container-children node)
702 ;; Promote the first orphan to parent and add the other kids as
703 ;; his children
704 (setf (mh-container-children node)
705 (mh-thread-sort-containers (mh-container-children node)))
706 (let ((new-parent (car (mh-container-children node)))
707 (other-kids (cdr (mh-container-children node))))
708 (mh-thread-remove-parent-link new-parent)
709 (dolist (kid other-kids)
710 (mh-thread-remove-parent-link kid)
711 (setf (mh-container-real-child-p kid) nil)
712 (mh-thread-add-link new-parent kid t))
713 (push `(PROMOTE ,node ,(mh-container-parent node)
714 ,new-parent ,@other-kids)
715 mh-thread-history)
716 (mh-thread-remove-parent-link node)))
718 ;; Drop it
719 (push `(DROP ,node ,(mh-container-parent node))
720 mh-thread-history)
721 (mh-thread-remove-parent-link node)))))
722 (let ((results ()))
723 (maphash #'(lambda (k v)
724 (declare (ignore k))
725 (when (and (null (mh-container-parent v))
726 (gethash (mh-message-id (mh-container-message v))
727 mh-thread-id-index-map))
728 (push v results)))
729 mh-thread-id-table)
730 (mh-thread-sort-containers results))))
732 (defun mh-thread-sort-containers (containers)
733 "Sort a list of message CONTAINERS to be in ascending order wrt index."
734 (sort containers
735 #'(lambda (x y)
736 (when (and (mh-container-message x) (mh-container-message y))
737 (let* ((id-x (mh-message-id (mh-container-message x)))
738 (id-y (mh-message-id (mh-container-message y)))
739 (index-x (gethash id-x mh-thread-id-index-map))
740 (index-y (gethash id-y mh-thread-id-index-map)))
741 (and (integerp index-x) (integerp index-y)
742 (< index-x index-y)))))))
744 (defvar mh-thread-last-ancestor)
746 ;;;###mh-autoload
747 (defun mh-thread-print-scan-lines (thread-tree)
748 "Print scan lines in THREAD-TREE in threaded mode."
749 (let ((mh-thread-body-width (- (window-width) mh-cmd-note
750 (1- mh-scan-field-subject-start-offset)))
751 (mh-thread-last-ancestor nil))
752 (if (null mh-index-data)
753 (mh-thread-generate-scan-lines thread-tree -2)
754 (loop for x in (mh-index-group-by-folder)
755 do (let* ((old-map mh-thread-scan-line-map)
756 (mh-thread-scan-line-map (make-hash-table)))
757 (setq mh-thread-last-ancestor nil)
758 (loop for msg in (cdr x)
759 do (let ((v (gethash msg old-map)))
760 (when v
761 (setf (gethash msg mh-thread-scan-line-map) v))))
762 (when (> (hash-table-count mh-thread-scan-line-map) 0)
763 (insert (if (bobp) "" "\n") (car x) "\n")
764 (mh-thread-generate-scan-lines thread-tree -2))))
765 (mh-index-create-imenu-index))))
767 (defun mh-thread-generate-scan-lines (tree level)
768 "Generate scan lines.
769 TREE is the hierarchical tree of messages, SCAN-LINE-MAP maps
770 message indices to the corresponding scan lines and LEVEL used to
771 determine indentation of the message."
772 (cond ((null tree) nil)
773 ((mh-thread-container-p tree)
774 (let* ((message (mh-container-message tree))
775 (id (mh-message-id message))
776 (index (gethash id mh-thread-id-index-map))
777 (duplicates (gethash id mh-thread-duplicates))
778 (new-level (+ level 2))
779 (dupl-flag t)
780 (force-angle-flag nil)
781 (increment-level-flag nil))
782 (dolist (scan-line (mapcar (lambda (x)
783 (gethash x mh-thread-scan-line-map))
784 (reverse (cons index duplicates))))
785 (when scan-line
786 (when (and dupl-flag (equal level 0)
787 (mh-thread-ancestor-p mh-thread-last-ancestor tree))
788 (setq level (+ level 2)
789 new-level (+ new-level 2)
790 force-angle-flag t))
791 (when (equal level 0)
792 (setq mh-thread-last-ancestor tree)
793 (while (mh-container-parent mh-thread-last-ancestor)
794 (setq mh-thread-last-ancestor
795 (mh-container-parent mh-thread-last-ancestor))))
796 (let* ((lev (if dupl-flag level new-level))
797 (square-flag (or (and (mh-container-real-child-p tree)
798 (not force-angle-flag)
799 dupl-flag)
800 (equal lev 0))))
801 (insert (car scan-line)
802 (format (format "%%%ss" lev) "")
803 (if square-flag "[" "<")
804 (cadr scan-line)
805 (if square-flag "]" ">")
806 (truncate-string-to-width
807 (caddr scan-line) (- mh-thread-body-width lev))
808 "\n"))
809 (setq increment-level-flag t)
810 (setq dupl-flag nil)))
811 (unless increment-level-flag (setq new-level level))
812 (dolist (child (mh-container-children tree))
813 (mh-thread-generate-scan-lines child new-level))))
814 (t (let ((nlevel (+ level 2)))
815 (dolist (ch tree)
816 (mh-thread-generate-scan-lines ch nlevel))))))
820 ;;; Additional Utilities
822 ;;;###mh-autoload
823 (defun mh-thread-update-scan-line-map (msg notation offset)
824 "In threaded view update `mh-thread-scan-line-map'.
825 MSG is the message being notated with NOTATION at OFFSET."
826 (let* ((msg (or msg (mh-get-msg-num nil)))
827 (cur-scan-line (and mh-thread-scan-line-map
828 (gethash msg mh-thread-scan-line-map)))
829 (old-scan-lines (loop for map in mh-thread-scan-line-map-stack
830 collect (and map (gethash msg map)))))
831 (when cur-scan-line
832 (setf (aref (car cur-scan-line) offset) notation))
833 (dolist (line old-scan-lines)
834 (when line (setf (aref (car line) offset) notation)))))
836 ;;;###mh-autoload
837 (defun mh-thread-find-msg-subject (msg)
838 "Find canonicalized subject of MSG.
839 This function can only be used the folder is threaded."
840 (ignore-errors
841 (mh-message-subject
842 (mh-container-message (gethash (gethash msg mh-thread-index-id-map)
843 mh-thread-id-table)))))
845 ;;;###mh-autoload
846 (defun mh-thread-add-spaces (count)
847 "Add COUNT spaces to each scan line in `mh-thread-scan-line-map'."
848 (let ((spaces (format (format "%%%ss" count) "")))
849 (while (not (eobp))
850 (let* ((msg-num (mh-get-msg-num nil))
851 (old-line (nth 3 (gethash msg-num mh-thread-scan-line-map))))
852 (when (numberp msg-num)
853 (setf (gethash msg-num mh-thread-scan-line-map)
854 (mh-thread-parse-scan-line (format "%s%s" spaces old-line)))))
855 (forward-line 1))))
857 ;;;###mh-autoload
858 (defun mh-thread-forget-message (index)
859 "Forget the message INDEX from the threading tables."
860 (let* ((id (gethash index mh-thread-index-id-map))
861 (id-index (gethash id mh-thread-id-index-map))
862 (duplicates (gethash id mh-thread-duplicates)))
863 (remhash index mh-thread-index-id-map)
864 (remhash index mh-thread-scan-line-map)
865 (cond ((and (eql index id-index) (null duplicates))
866 (remhash id mh-thread-id-index-map))
867 ((eql index id-index)
868 (setf (gethash id mh-thread-id-index-map) (car duplicates))
869 (setf (gethash (car duplicates) mh-thread-index-id-map) id)
870 (setf (gethash id mh-thread-duplicates) (cdr duplicates)))
872 (setf (gethash id mh-thread-duplicates)
873 (remove index duplicates))))))
875 (provide 'mh-thread)
877 ;; Local Variables:
878 ;; indent-tabs-mode: nil
879 ;; sentence-end-double-space: nil
880 ;; End:
882 ;;; mh-thread.el ends here