1 ;;; mh-seq.el --- MH-E sequences support
3 ;; Copyright (C) 1993, 1995,
4 ;; 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
6 ;; Author: Bill Wohler <wohler@newt.com>
7 ;; Maintainer: Bill Wohler <wohler@newt.com>
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
30 ;; This tries to implement the algorithm described at:
31 ;; http://www.jwz.org/doc/threading.html
32 ;; It is also a start to implementing the IMAP Threading extension RFC. The
33 ;; implementation lacks the reference and subject canonicalization of the
36 ;; In the presentation buffer, children messages are shown indented with
37 ;; either [ ] or < > around them. Square brackets ([ ]) denote that the
38 ;; algorithm can point out some headers which when taken together implies
39 ;; that the unindented message is an ancestor of the indented message. If
40 ;; no such proof exists then angles (< >) are used.
42 ;; Some issues and problems are as follows:
44 ;; (1) Scan truncates the fields at length 512. So longer references:
45 ;; headers get mutilated. The same kind of MH format string works when
46 ;; composing messages. Is there a way to avoid this? My scan command
48 ;; scan +folder -width 10000 \
49 ;; -format "%(msg)\n%{message-id}\n%{references}\n%{subject}\n"
50 ;; I would really appreciate it if someone would help me with this.
52 ;; (2) Implement heuristics to recognize message identifiers in
53 ;; In-Reply-To: header. Right now it just assumes that the last text
54 ;; between angles (< and >) is the message identifier. There is the
55 ;; chance that this will incorrectly use an email address like a
56 ;; message identifier.
58 ;; (3) Error checking of found message identifiers should be done.
60 ;; (4) Since this breaks the assumption that message indices increase as
61 ;; one goes down the buffer, the binary search based mh-goto-msg
62 ;; doesn't work. I have a simpler replacement which may be less
65 ;; (5) Better canonicalizing for message identifier and subject strings.
68 ;; Internal support for MH-E package.
74 (eval-when-compile (require 'mh-acros
))
78 ;; Shush the byte-compiler
79 (defvar tool-bar-mode
)
83 ;;; Data structures (used in message threading)...
85 (mh-defstruct (mh-thread-message (:conc-name mh-message-
)
86 (:constructor mh-thread-make-message
))
92 (mh-defstruct (mh-thread-container (:conc-name mh-container-
)
93 (:constructor mh-thread-make-container
))
94 message parent children
99 ;;; Internal variables:
101 (defvar mh-last-seq-used nil
102 "Name of seq to which a msg was last added.")
104 (defvar mh-non-seq-mode-line-annotation nil
105 "Saved value of `mh-mode-line-annotation' when narrowed to a seq.")
109 ;;; Maps and hashes...
111 (defvar mh-thread-id-hash nil
112 "Hashtable used to canonicalize message identifiers.")
113 (defvar mh-thread-subject-hash nil
114 "Hashtable used to canonicalize subject strings.")
115 (defvar mh-thread-id-table nil
116 "Thread ID table maps from message identifiers to message containers.")
117 (defvar mh-thread-id-index-map nil
118 "Table to look up message index number from message identifier.")
119 (defvar mh-thread-index-id-map nil
120 "Table to look up message identifier from message index.")
121 (defvar mh-thread-scan-line-map nil
122 "Map of message index to various parts of the scan line.")
123 (defvar mh-thread-scan-line-map-stack nil
124 "Old map of message index to various parts of the scan line.
125 This is the original map that is stored when the folder is narrowed.")
126 (defvar mh-thread-subject-container-hash nil
127 "Hashtable used to group messages by subject.")
128 (defvar mh-thread-duplicates nil
129 "Hashtable used to associate messages with the same message identifier.")
130 (defvar mh-thread-history
()
131 "Variable to remember the transformations to the thread tree.
132 When new messages are added, these transformations are rewound, then the
133 links are added from the newly seen messages. Finally the transformations are
134 redone to get the new thread tree. This makes incremental threading easier.")
135 (defvar mh-thread-body-width nil
136 "Width of scan substring that contains subject and body of message.")
138 (make-variable-buffer-local 'mh-thread-id-hash
)
139 (make-variable-buffer-local 'mh-thread-subject-hash
)
140 (make-variable-buffer-local 'mh-thread-id-table
)
141 (make-variable-buffer-local 'mh-thread-id-index-map
)
142 (make-variable-buffer-local 'mh-thread-index-id-map
)
143 (make-variable-buffer-local 'mh-thread-scan-line-map
)
144 (make-variable-buffer-local 'mh-thread-scan-line-map-stack
)
145 (make-variable-buffer-local 'mh-thread-subject-container-hash
)
146 (make-variable-buffer-local 'mh-thread-duplicates
)
147 (make-variable-buffer-local 'mh-thread-history
)
150 (defun mh-delete-seq (sequence)
153 You are prompted for the sequence to delete. Note that this deletes only the
154 sequence, not the messages in the sequence. If you want to delete the
155 messages, use \"\\[universal-argument] \\[mh-delete-msg]\"."
156 (interactive (list (mh-read-seq-default "Delete" t
)))
157 (let ((msg-list (mh-seq-to-msgs sequence
))
158 (internal-flag (mh-internal-seq sequence
))
159 (folders-changed (list mh-current-folder
)))
160 (mh-iterate-on-range msg sequence
161 (mh-remove-sequence-notation msg internal-flag
))
162 (mh-undefine-sequence sequence
'("all"))
163 (mh-delete-seq-locally sequence
)
165 (setq folders-changed
166 (append folders-changed
167 (mh-index-delete-from-sequence sequence msg-list
))))
168 (when (and (eq sequence mh-unseen-seq
) (mh-speed-flists-active-p))
169 (apply #'mh-speed-flists t folders-changed
))))
171 ;; Avoid compiler warnings
172 (defvar view-exit-action
)
175 (defun mh-list-sequences ()
176 "List all sequences in folder.
177 The list appears in a buffer named \"*MH-E Sequences*\"."
179 (let ((folder mh-current-folder
)
180 (temp-buffer mh-sequences-buffer
)
181 (seq-list mh-seq-list
)
183 (with-output-to-temp-buffer temp-buffer
185 (set-buffer temp-buffer
)
187 (message "Listing sequences ...")
188 (insert "Sequences in folder " folder
":\n")
189 (let ((seq-list seq-list
))
192 (max (length (symbol-name (mh-seq-name (pop seq-list
))))
194 (setq max-len
(+ 2 max-len
)))
196 (let ((name (mh-seq-name (car seq-list
)))
198 (mh-coalesce-msg-list
199 (sort (copy-sequence (mh-seq-msgs (car seq-list
))) '<)))
201 (insert (setq name-spec
(format (format "%%%ss:" max-len
) name
)))
202 (while sorted-seq-msgs
203 (let ((next-element (format " %s" (pop sorted-seq-msgs
))))
204 (when (>= (+ (current-column) (length next-element
))
207 (insert (format (format "%%%ss" (length name-spec
)) "")))
208 (insert next-element
)))
210 (setq seq-list
(cdr seq-list
)))
211 (goto-char (point-min))
213 (setq view-exit-action
'kill-buffer
)
214 (message "Listing sequences...done")))))
217 (defun mh-msg-is-in-seq (message)
218 "Display the sequences in which the current message appears.
219 Use a prefix argument to display the sequences in which another MESSAGE
223 (setq message
(mh-get-msg-num t
)))
224 (let* ((dest-folder (loop for seq in mh-refile-list
225 when
(member message
(cdr seq
)) return
(car seq
)
227 (deleted-flag (unless dest-folder
(member message mh-delete-list
))))
228 (message "Message %d%s is in sequences: %s"
230 (cond (dest-folder (format " (to be refiled to %s)" dest-folder
))
231 (deleted-flag (format " (to be deleted)"))
234 (mh-list-to-string (mh-seq-containing-msg message t
))
237 ;; Avoid compiler warning
238 (defvar tool-bar-map
)
240 (make-variable-buffer-local 'mh-non-seq-mode-line-annotation
)
243 (defun mh-narrow-to-seq (sequence)
244 "Restrict display to messages in SEQUENCE.
246 You are prompted for the name of the sequence. What this command does is show
247 only those messages that are in the selected sequence in the MH-Folder buffer.
248 In addition, it limits further MH-E searches to just those messages.
250 When you want to widen the view to all your messages again, use \\[mh-widen]."
251 (interactive (list (mh-read-seq "Narrow to" t
)))
252 (with-mh-folder-updating (t)
253 (cond ((mh-seq-to-msgs sequence
)
254 (mh-remove-all-notation)
255 (let ((eob (point-max))
256 (msg-at-cursor (mh-get-msg-num nil
)))
257 (push mh-thread-scan-line-map mh-thread-scan-line-map-stack
)
258 (setq mh-thread-scan-line-map
(make-hash-table :test
#'eql
))
259 (mh-copy-seq-to-eob sequence
)
260 (push (buffer-substring-no-properties (point-min) eob
)
261 mh-folder-view-stack
)
262 (delete-region (point-min) eob
)
263 (mh-notate-deleted-and-refiled)
265 (when msg-at-cursor
(mh-goto-msg msg-at-cursor t t
))
266 (setq mh-non-seq-mode-line-annotation mh-mode-line-annotation
)
267 (setq mh-mode-line-annotation
(symbol-name sequence
))
268 (mh-make-folder-mode-line)
270 (when (and (boundp 'tool-bar-mode
) tool-bar-mode
)
271 (set (make-local-variable 'tool-bar-map
)
272 mh-folder-seq-tool-bar-map
)
273 (when (buffer-live-p (get-buffer mh-show-buffer
))
275 (set-buffer (get-buffer mh-show-buffer
))
276 (set (make-local-variable 'tool-bar-map
)
277 mh-show-seq-tool-bar-map
))))
278 (push 'widen mh-view-ops
)))
280 (error "No messages in sequence `%s'" (symbol-name sequence
))))))
283 (defun mh-put-msg-in-seq (range sequence
)
284 "Add RANGE to SEQUENCE\\<mh-folder-mode-map>.
286 To place a message in a sequence, use this command to do it manually, or use
287 the MH command \"pick\" or the MH-E version of \"pick\", \\[mh-search-folder],
288 which create a sequence automatically.
290 Give this command a RANGE and you can add all the messages in a sequence to
291 another sequence (for example, \"\\[universal-argument] \\[mh-put-msg-in-seq]
292 SourceSequence RET DestSequence RET\"). Check the documentation of
293 `mh-interactive-range' to see how RANGE is read in interactive use."
294 (interactive (list (mh-interactive-range "Add messages from")
295 (mh-read-seq-default "Add to" nil
)))
296 (unless (mh-valid-seq-p sequence
)
297 (error "Can't put message in invalid sequence `%s'" sequence
))
298 (let* ((internal-seq-flag (mh-internal-seq sequence
))
299 (original-msgs (mh-seq-msgs (mh-find-seq sequence
)))
300 (folders (list mh-current-folder
))
301 (msg-list (mh-range-to-msg-list range
)))
302 (mh-add-msgs-to-seq msg-list sequence nil t
)
303 (mh-iterate-on-range m range
304 (unless (memq m original-msgs
)
305 (mh-add-sequence-notation m internal-seq-flag
)))
306 (if (not internal-seq-flag
)
307 (setq mh-last-seq-used sequence
))
310 (append folders
(mh-index-add-to-sequence sequence msg-list
))))
311 (when (and (eq sequence mh-unseen-seq
) (mh-speed-flists-active-p))
312 (apply #'mh-speed-flists t folders
))))
314 (defun mh-valid-view-change-operation-p (op)
315 "Check if the view change operation can be performed.
316 OP is one of 'widen and 'unthread."
317 (cond ((eq (car mh-view-ops
) op
)
322 (defun mh-widen (&optional all-flag
)
323 "Remove last restriction.
324 If optional prefix argument ALL-FLAG is non-nil, remove all limits."
326 (let ((msg (mh-get-msg-num nil
)))
327 (when mh-folder-view-stack
329 (while (cdr mh-view-ops
)
330 (setq mh-view-ops
(cdr mh-view-ops
)))
331 (when (eq (car mh-view-ops
) 'widen
)
332 (setq mh-view-ops
(cdr mh-view-ops
))))
333 ((mh-valid-view-change-operation-p 'widen
) nil
)
334 ((memq 'widen mh-view-ops
)
335 (while (not (eq (car mh-view-ops
) 'widen
))
336 (setq mh-view-ops
(cdr mh-view-ops
)))
337 (setq mh-view-ops
(cdr mh-view-ops
)))
338 (t (error "Widening is not applicable")))
339 ;; If ALL-FLAG is non-nil then rewind stacks
341 (while (cdr mh-thread-scan-line-map-stack
)
342 (setq mh-thread-scan-line-map-stack
343 (cdr mh-thread-scan-line-map-stack
)))
344 (while (cdr mh-folder-view-stack
)
345 (setq mh-folder-view-stack
(cdr mh-folder-view-stack
))))
346 (setq mh-thread-scan-line-map
(pop mh-thread-scan-line-map-stack
))
347 (with-mh-folder-updating (t)
348 (delete-region (point-min) (point-max))
349 (insert (pop mh-folder-view-stack
))
350 (mh-remove-all-notation)
351 (setq mh-mode-line-annotation mh-non-seq-mode-line-annotation
)
352 (mh-make-folder-mode-line))
354 (mh-goto-msg msg t t
))
355 (mh-notate-deleted-and-refiled)
356 (mh-notate-user-sequences)
359 (when (and (null mh-folder-view-stack
) (boundp 'tool-bar-mode
) tool-bar-mode
)
360 (set (make-local-variable 'tool-bar-map
) mh-folder-tool-bar-map
)
361 (when (buffer-live-p (get-buffer mh-show-buffer
))
363 (set-buffer (get-buffer mh-show-buffer
))
364 (set (make-local-variable 'tool-bar-map
) mh-show-tool-bar-map
)))))
366 ;; FIXME? We may want to clear all notations and add one for current-message
367 ;; and process user sequences.
369 (defun mh-notate-deleted-and-refiled ()
370 "Notate messages marked for deletion or refiling.
371 Messages to be deleted are given by `mh-delete-list' while messages to be
372 refiled are present in `mh-refile-list'."
373 (let ((refiled-hash (make-hash-table))
374 (deleted-hash (make-hash-table)))
375 (dolist (msg mh-delete-list
)
376 (setf (gethash msg deleted-hash
) t
))
377 (dolist (dest-msg-list mh-refile-list
)
378 (dolist (msg (cdr dest-msg-list
))
379 (setf (gethash msg refiled-hash
) t
)))
380 (mh-iterate-on-messages-in-region msg
(point-min) (point-max)
381 (cond ((gethash msg refiled-hash
)
382 (mh-notate nil mh-note-refiled mh-cmd-note
))
383 ((gethash msg deleted-hash
)
384 (mh-notate nil mh-note-deleted mh-cmd-note
))))))
388 ;;; Commands to manipulate sequences.
390 ;; Sequences are stored in an alist of the form:
391 ;; ((seq-name msgs ...) (seq-name msgs ...) ...)
393 (defvar mh-sequence-history
())
396 (defun mh-read-seq-default (prompt not-empty
)
397 "Read and return sequence name with default narrowed or previous sequence.
398 PROMPT is the prompt to use when reading. If NOT-EMPTY is non-nil then a
399 non-empty sequence is read."
400 (mh-read-seq prompt not-empty
402 (car (mh-seq-containing-msg (mh-get-msg-num nil
) nil
)))))
404 (defun mh-read-seq (prompt not-empty
&optional default
)
405 "Read and return a sequence name.
406 Prompt with PROMPT, raise an error if the sequence is empty and the NOT-EMPTY
407 flag is non-nil, and supply an optional DEFAULT sequence. A reply of '%'
408 defaults to the first sequence containing the current message."
409 (let* ((input (completing-read (format "%s %s %s" prompt
"sequence:"
411 (format "[%s] " default
)
413 (mh-seq-names mh-seq-list
)
414 nil nil nil
'mh-sequence-history
))
415 (seq (cond ((equal input
"%")
416 (car (mh-seq-containing-msg (mh-get-msg-num t
) nil
)))
417 ((equal input
"") default
)
419 (msgs (mh-seq-to-msgs seq
)))
420 (if (and (null msgs
) not-empty
)
421 (error "No messages in sequence `%s'" seq
))
426 ;;; Functions to read ranges with completion...
428 (defvar mh-range-seq-names
)
429 (defvar mh-range-history
())
430 (defvar mh-range-completion-map
(copy-keymap minibuffer-local-completion-map
))
431 (define-key mh-range-completion-map
" " 'self-insert-command
)
433 (defun mh-range-completion-function (string predicate flag
)
434 "Programmable completion of message ranges.
435 STRING is the user input that is to be completed. PREDICATE if non-nil is a
436 function used to filter the possible choices and FLAG determines whether the
438 (let* ((candidates mh-range-seq-names
)
439 (last-char (and (not (equal string
""))
440 (aref string
(1- (length string
)))))
441 (last-word (cond ((null last-char
) "")
442 ((memq last-char
'(? ?- ?
:)) "")
443 (t (car (last (split-string string
"[ -:]+"))))))
444 (prefix (substring string
0 (- (length string
) (length last-word
)))))
446 (let ((res (try-completion last-word candidates predicate
)))
447 (cond ((null res
) nil
)
449 (t (concat prefix res
)))))
451 (all-completions last-word candidates predicate
))
453 (loop for x in candidates
454 when
(equal x last-word
) return t
455 finally return nil
)))))
458 (defun mh-read-range (prompt &optional folder default
459 expand-flag ask-flag number-as-range-flag
)
460 "Read a message range with PROMPT.
462 If FOLDER is non-nil then a range is read from that folder, otherwise use
465 If DEFAULT is a string then use that as default range to return. If DEFAULT is
466 nil then ask user with default answer a range based on the sequences that seem
467 relevant. Finally if DEFAULT is t, try to avoid prompting the user. Unseen
468 messages, if present, are returned. If the folder has fewer than
469 `mh-large-folder' messages then \"all\" messages are returned. Finally as a
470 last resort prompt the user.
472 If EXPAND-FLAG is non-nil then a list of message numbers corresponding to the
473 input is returned. If this list is empty then an error is raised. If
474 EXPAND-FLAG is nil just return the input string. In this case we don't check
475 if the range is empty.
477 If ASK-FLAG is non-nil, then the user is always queried for a range of
478 messages. If ASK-FLAG is nil, then the function checks if the unseen sequence
479 is non-empty. If that is the case, `mh-unseen-seq', or the list of messages in
480 it depending on the value of EXPAND, is returned. Otherwise if the folder has
481 fewer than `mh-large-folder' messages then the list of messages corresponding
482 to \"all\" is returned. If neither of the above holds then as a last resort
483 the user is queried for a range of messages.
485 If NUMBER-AS-RANGE-FLAG is non-nil, then if a number, N is read as input, it
486 is interpreted as the range \"last:N\".
488 This function replaces the existing function `mh-read-msg-range'. Calls to:
489 (mh-read-msg-range folder flag)
490 should be replaced with:
491 (mh-read-range \"Suitable prompt\" folder t nil flag
492 mh-interpret-number-as-range-flag)"
493 (setq default
(or default mh-last-seq-used
494 (car (mh-seq-containing-msg (mh-get-msg-num nil
) t
)))
495 prompt
(format "%s range" prompt
))
496 (let* ((folder (or folder mh-current-folder
))
497 (default (cond ((or (eq default t
) (stringp default
)) default
)
498 ((symbolp default
) (symbol-name default
))))
499 (guess (eq default t
))
500 (counts (and guess
(mh-folder-size folder
)))
501 (unseen (and counts
(> (cadr counts
) 0)))
502 (large (and counts mh-large-folder
(> (car counts
) mh-large-folder
)))
503 (str (cond ((and guess large
504 (setq default
(format "last:%s" mh-large-folder
)
505 prompt
(format "%s (folder has %s messages)"
506 prompt
(car counts
)))
508 ((and guess
(not large
) (setq default
"all") nil
))
509 ((eq default nil
) "")
510 (t (format "[%s] " default
))))
511 (minibuffer-local-completion-map mh-range-completion-map
)
512 (seq-list (if (eq folder mh-current-folder
)
514 (mh-read-folder-sequences folder nil
)))
516 (append '(("first") ("last") ("all") ("prev") ("next"))
517 (mh-seq-names seq-list
)))
518 (input (cond ((and (not ask-flag
) unseen
) (symbol-name mh-unseen-seq
))
519 ((and (not ask-flag
) (not large
)) "all")
520 (t (completing-read (format "%s: %s" prompt str
)
521 'mh-range-completion-function nil nil
522 nil
'mh-range-history default
))))
524 (when (and number-as-range-flag
525 (string-match "^[ \t]*\\([0-9]+\\)[ \t]*$" input
))
526 (setq input
(concat "last:" (match-string 1 input
))))
527 (cond ((not expand-flag
) input
)
528 ((assoc (intern input
) seq-list
)
529 (cdr (assoc (intern input
) seq-list
)))
530 ((setq msg-list
(mh-translate-range folder input
)) msg-list
)
531 (t (error "No messages in range `%s'" input
)))))
534 (defun mh-translate-range (folder expr
)
535 "In FOLDER, translate the string EXPR to a list of messages numbers."
537 (let ((strings (delete "" (split-string expr
"[ \t\n]")))
540 (apply #'mh-exec-cmd-quiet nil
"mhpath" folder strings
)
541 (set-buffer mh-temp-buffer
)
542 (goto-char (point-min))
543 (while (re-search-forward "/\\([0-9]*\\)$" nil t
)
544 (push (car (read-from-string (match-string 1))) result
))
545 (nreverse result
)))))
547 (defun mh-seq-names (seq-list)
548 "Return an alist containing the names of the SEQ-LIST."
549 (mapcar (lambda (entry) (list (symbol-name (mh-seq-name entry
))))
553 (defun mh-rename-seq (sequence new-name
)
554 "Rename SEQUENCE to have NEW-NAME."
555 (interactive (list (mh-read-seq "Old" t
)
556 (intern (read-string "New sequence name: "))))
557 (let ((old-seq (mh-find-seq sequence
)))
559 (error "Sequence %s does not exist" sequence
))
560 ;; create new sequence first, since it might raise an error.
561 (mh-define-sequence new-name
(mh-seq-msgs old-seq
))
562 (mh-undefine-sequence sequence
(mh-seq-msgs old-seq
))
563 (rplaca old-seq new-name
)))
566 (defun mh-notate-cur ()
567 "Mark the MH sequence cur.
568 In addition to notating the current message with `mh-note-cur' the function
569 uses `overlay-arrow-position' to put a marker in the fringe."
570 (let ((cur (car (mh-seq-to-msgs 'cur
))))
571 (when (and cur
(mh-goto-msg cur t t
))
573 (when (looking-at mh-scan-good-msg-regexp
)
574 (mh-notate nil mh-note-cur mh-cmd-note
))
575 (setq mh-arrow-marker
(set-marker mh-arrow-marker
(point)))
576 (setq overlay-arrow-position mh-arrow-marker
))))
579 (defun mh-add-to-sequence (seq msgs
)
580 "The sequence SEQ is augmented with the messages in MSGS."
581 ;; Add to a SEQUENCE each message the list of MSGS.
582 (if (and (mh-valid-seq-p seq
) (not (mh-folder-name-p seq
)))
584 (apply 'mh-exec-cmd
"mark" mh-current-folder
"-add"
585 "-sequence" (symbol-name seq
)
586 (mh-coalesce-msg-list msgs
)))))
588 (defvar mh-thread-last-ancestor
)
590 (defun mh-copy-seq-to-eob (seq)
591 "Copy SEQ to the end of the buffer."
592 ;; It is quite involved to write something which will work at any place in
593 ;; the buffer, so we will write something which works only at the end of
594 ;; the buffer. If we ever need to insert sequences in the middle of the
595 ;; buffer, this will need to be fixed.
597 (let* ((msgs (mh-seq-to-msgs seq
))
598 (coalesced-msgs (mh-coalesce-msg-list msgs
)))
599 (goto-char (point-max))
601 (narrow-to-region (point) (point))
602 (mh-regenerate-headers coalesced-msgs t
)
603 (cond ((memq 'unthread mh-view-ops
)
604 ;; Populate restricted scan-line map
605 (mh-remove-all-notation)
606 (mh-iterate-on-range msg
(cons (point-min) (point-max))
607 (setf (gethash msg mh-thread-scan-line-map
)
608 (mh-thread-parse-scan-line)))
609 ;; Remove scan lines and read results from pre-computed tree
610 (delete-region (point-min) (point-max))
611 (mh-thread-print-scan-lines
612 (mh-thread-generate mh-current-folder
()))
613 (mh-notate-user-sequences))
615 (mh-index-insert-folder-headers)))))))
618 (defmacro mh-iterate-on-messages-in-region
(var begin end
&rest body
)
619 "Iterate over region.
620 VAR is bound to the message on the current line as we loop starting from BEGIN
621 till END. In each step BODY is executed.
623 If VAR is nil then the loop is executed without any binding."
624 (unless (symbolp var
)
625 (error "Can not bind the non-symbol %s" var
))
626 (let ((binding-needed-flag var
))
630 (while (and (<= (point) ,end
) (not (eobp)))
631 (when (looking-at mh-scan-valid-regexp
)
632 (let ,(if binding-needed-flag
`((,var
(mh-get-msg-num t
))) ())
636 (put 'mh-iterate-on-messages-in-region
'lisp-indent-hook
'defun
)
639 (defmacro mh-iterate-on-range
(var range
&rest body
)
640 "Iterate an operation over a region or sequence.
642 VAR is bound to each message in turn in a loop over RANGE, which can be a
643 message number, a list of message numbers, a sequence, a region in a cons
644 cell, or a MH range (something like last:20) in a string. In each iteration,
647 The parameter RANGE is usually created with `mh-interactive-range'
648 in order to provide a uniform interface to MH-E functions."
649 (unless (symbolp var
)
650 (error "Can not bind the non-symbol %s" var
))
651 (let ((binding-needed-flag var
)
652 (msgs (make-symbol "msgs"))
653 (seq-hash-table (make-symbol "seq-hash-table")))
654 `(cond ((numberp ,range
)
655 (when (mh-goto-msg ,range t t
)
656 (let ,(if binding-needed-flag
`((,var
,range
)) ())
659 (numberp (car ,range
)) (numberp (cdr ,range
)))
660 (mh-iterate-on-messages-in-region ,var
661 (car ,range
) (cdr ,range
)
663 (t (let ((,msgs
(cond ((and ,range
(symbolp ,range
))
664 (mh-seq-to-msgs ,range
))
666 (mh-translate-range mh-current-folder
669 (,seq-hash-table
(make-hash-table)))
671 (setf (gethash msg
,seq-hash-table
) t
))
672 (mh-iterate-on-messages-in-region v
(point-min) (point-max)
673 (when (gethash v
,seq-hash-table
)
674 (let ,(if binding-needed-flag
`((,var v
)) ())
677 (put 'mh-iterate-on-range
'lisp-indent-hook
'defun
)
680 (defun mh-range-to-msg-list (range)
681 "Return a list of messages for RANGE.
683 Check the documentation of `mh-interactive-range' to see how RANGE is read in
686 (mh-iterate-on-range msg range
688 (nreverse msg-list
)))
691 (defun mh-interactive-range (range-prompt &optional default
)
692 "Return interactive specification for message, sequence, range or region.
693 By convention, the name of this argument is RANGE.
695 If variable `transient-mark-mode' is non-nil and the mark is active, then this
696 function returns a cons-cell of the region.
698 If optional prefix argument is provided, then prompt for message range with
699 RANGE-PROMPT. A list of messages in that range is returned.
701 If a MH range is given, say something like last:20, then a list containing
702 the messages in that range is returned.
704 If DEFAULT non-nil then it is returned.
706 Otherwise, the message number at point is returned.
708 This function is usually used with `mh-iterate-on-range' in order to provide
709 a uniform interface to MH-E functions."
710 (cond ((mh-mark-active-p t
) (cons (region-beginning) (region-end)))
711 (current-prefix-arg (mh-read-range range-prompt nil nil t t
))
713 (t (mh-get-msg-num t
))))
717 ;;; Commands to handle new 'subject sequence ("Poor man's threading" by psg)
719 ;; XXX: The function mh-subject-to-sequence-unthreaded uses the magic number
720 ;; 41 for the max size of the subject part. Avoiding this would be desirable.
721 (defun mh-subject-to-sequence (all)
722 "Put all following messages with same subject in sequence 'subject.
723 If arg ALL is t, move to beginning of folder buffer to collect all messages.
724 If arg ALL is nil, collect only messages fron current one on forward.
726 Return number of messages put in the sequence:
728 nil -> there was no subject line.
729 0 -> there were no later messages with the same subject (sequence not made)
730 >1 -> the total number of messages including current one."
731 (if (memq 'unthread mh-view-ops
)
732 (mh-subject-to-sequence-threaded all
)
733 (mh-subject-to-sequence-unthreaded all
)))
735 (defun mh-subject-to-sequence-unthreaded (all)
736 "Put all following messages with same subject in sequence 'subject.
737 This function only works with an unthreaded folder. If arg ALL is t, move to
738 beginning of folder buffer to collect all messages. If arg ALL is nil, collect
739 only messages fron current one on forward.
741 Return number of messages put in the sequence:
743 nil -> there was no subject line.
744 0 -> there were no later messages with the same subject (sequence not made)
745 >1 -> the total number of messages including current one."
746 (if (not (eq major-mode
'mh-folder-mode
))
747 (error "Not in a folder buffer"))
750 (if (or (not (looking-at mh-scan-subject-regexp
))
751 (not (match-string 3))
752 (string-equal "" (match-string 3)))
753 (progn (message "No subject line")
755 (let ((subject (match-string-no-properties 3))
757 (if (> (length subject
) 41)
758 (setq subject
(substring subject
0 41)))
761 (goto-char (point-min)))
762 (while (re-search-forward mh-scan-subject-regexp nil t
)
763 (let ((this-subject (match-string-no-properties 3)))
764 (if (> (length this-subject
) 41)
765 (setq this-subject
(substring this-subject
0 41)))
766 (if (string-equal this-subject subject
)
767 (setq list
(cons (mh-get-msg-num t
) list
))))))
770 ;; If we created a new sequence, add the initial message to it too.
771 (if (not (member (mh-get-msg-num t
) list
))
772 (setq list
(cons (mh-get-msg-num t
) list
)))
773 (if (assoc 'subject mh-seq-list
) (mh-delete-seq 'subject
))
774 ;; sort the result into a sequence
775 (let ((sorted-list (sort (copy-sequence list
) 'mh-lessp
)))
777 (mh-add-msgs-to-seq (car sorted-list
) 'subject nil
)
778 (setq sorted-list
(cdr sorted-list
)))
783 (defun mh-subject-to-sequence-threaded (all)
784 "Put all messages with the same subject in the 'subject sequence.
785 This function works when the folder is threaded. In this situation the subject
786 could get truncated and so the normal matching doesn't work.
788 The parameter ALL is non-nil then all the messages in the buffer are
789 considered, otherwise only the messages after the current one are taken into
791 (let* ((cur (mh-get-msg-num nil
))
792 (subject (mh-thread-find-msg-subject cur
))
795 (and (message "No subject line") nil
)
796 (setq region
(cons (if all
(point-min) (point)) (point-max)))
797 (mh-iterate-on-range msg region
798 (when (eq (mh-thread-find-msg-subject msg
) subject
)
800 (setq msgs
(sort msgs
#'mh-lessp
))
803 (when (assoc 'subject mh-seq-list
)
804 (mh-delete-seq 'subject
))
805 (mh-add-msgs-to-seq msgs
'subject
)
808 (defun mh-thread-find-msg-subject (msg)
809 "Find canonicalized subject of MSG.
810 This function can only be used the folder is threaded."
813 (mh-container-message (gethash (gethash msg mh-thread-index-id-map
)
814 mh-thread-id-table
)))))
816 (defun mh-edit-pick-expr (default)
817 "With prefix arg edit a pick expression.
818 If no prefix arg is given, then return DEFAULT."
819 (let ((default-string (loop for x in default concat
(format " %s" x
))))
820 (if (or current-prefix-arg
(equal default-string
""))
821 (mh-pick-args-list (read-string "Pick expression: "
825 (defun mh-pick-args-list (s)
826 "Form list by grouping elements in string S suitable for pick arguments.
827 For example, the string \"-subject a b c -from Joe User <user@domain.com>\"
828 is converted to (\"-subject\" \"a b c\" \"-from\"
829 \"Joe User <user@domain.com>\""
830 (let ((full-list (split-string s
))
831 current-arg collection arg-list
)
833 (setq current-arg
(car full-list
))
834 (if (null (string-match "^-" current-arg
))
836 (if (null collection
)
838 (format "%s %s" collection current-arg
)))
840 (setq arg-list
(append arg-list
(list collection
)))
841 (setq collection nil
))
842 (setq arg-list
(append arg-list
(list current-arg
))))
843 (setq full-list
(cdr full-list
)))
845 (setq arg-list
(append arg-list
(list collection
))))
849 (defun mh-narrow-to-subject (&optional pick-expr
)
850 "Limit to messages with same subject.
851 With a prefix argument, edit PICK-EXPR.
853 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
855 (list (mh-edit-pick-expr (mh-current-message-header-field 'subject
))))
856 (mh-narrow-to-header-field 'subject pick-expr
))
859 (defun mh-narrow-to-from (&optional pick-expr
)
860 "Limit to messages with the same `From:' field.
861 With a prefix argument, edit PICK-EXPR.
863 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
865 (list (mh-edit-pick-expr (mh-current-message-header-field 'from
))))
866 (mh-narrow-to-header-field 'from pick-expr
))
869 (defun mh-narrow-to-cc (&optional pick-expr
)
870 "Limit to messages with the same `Cc:' field.
871 With a prefix argument, edit PICK-EXPR.
873 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
875 (list (mh-edit-pick-expr (mh-current-message-header-field 'cc
))))
876 (mh-narrow-to-header-field 'cc pick-expr
))
879 (defun mh-narrow-to-to (&optional pick-expr
)
880 "Limit to messages with the same `To:' field.
881 With a prefix argument, edit PICK-EXPR.
883 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
885 (list (mh-edit-pick-expr (mh-current-message-header-field 'to
))))
886 (mh-narrow-to-header-field 'to pick-expr
))
888 (defun mh-narrow-to-header-field (header-field pick-expr
)
889 "Limit to messages whose HEADER-FIELD match PICK-EXPR.
890 The MH command pick is used to do the match."
891 (let ((folder mh-current-folder
)
892 (original (mh-coalesce-msg-list
893 (mh-range-to-msg-list (cons (point-min) (point-max)))))
896 (apply #'mh-exec-cmd-output
"pick" nil folder
897 (append original
(list "-list") pick-expr
))
898 (goto-char (point-min))
900 (let ((num (read-from-string
901 (buffer-substring (point) (line-end-position)))))
902 (when (numberp (car num
)) (push (car num
) msg-list
))
905 (message "No matches")
906 (when (assoc 'header mh-seq-list
) (mh-delete-seq 'header
))
907 (mh-add-msgs-to-seq msg-list
'header
)
908 (mh-narrow-to-seq 'header
))))
910 (defun mh-current-message-header-field (header-field)
911 "Return a pick regexp to match HEADER-FIELD of the message at point."
912 (let ((num (mh-get-msg-num nil
)))
914 (let ((folder mh-current-folder
))
916 (insert-file-contents-literally (mh-msg-filename num folder
))
917 (goto-char (point-min))
918 (when (search-forward "\n\n" nil t
)
919 (narrow-to-region (point-min) (point)))
920 (let* ((field (or (message-fetch-field (format "%s" header-field
))
922 (field-option (format "-%s" header-field
))
923 (patterns (loop for x in
(split-string field
"[ ]*,[ ]*")
925 collect
(if (string-match "<\\(.*@.*\\)>" x
)
929 (loop with accum
= `(,field-option
,(car patterns
))
930 for e in
(cdr patterns
)
931 do
(setq accum
`(,field-option
,e
"-or" ,@accum
))
932 finally return accum
))))))))
935 (defun mh-narrow-to-range (range)
938 Check the documentation of `mh-interactive-range' to see how RANGE is read in
941 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
942 (interactive (list (mh-interactive-range "Narrow to")))
943 (when (assoc 'range mh-seq-list
) (mh-delete-seq 'range
))
944 (mh-add-msgs-to-seq (mh-range-to-msg-list range
) 'range
)
945 (mh-narrow-to-seq 'range
))
949 (defun mh-delete-subject ()
950 "Delete messages with same subject\\<mh-folder-mode-map>.
952 To delete messages faster, you can use this command to delete all the messages
953 with the same subject as the current message. This command puts these messages
954 in a sequence named \"subject\". You can undo this action by using \\[mh-undo]
955 with a prefix argument and then specifying the \"subject\" sequence."
957 (let ((count (mh-subject-to-sequence nil
)))
959 ((not count
) ; No subject line, delete msg anyway
960 (mh-delete-msg (mh-get-msg-num t
)))
961 ((= 0 count
) ; No other msgs, delete msg anyway.
962 (message "No other messages with same Subject following this one")
963 (mh-delete-msg (mh-get-msg-num t
)))
964 (t ; We have a subject sequence.
965 (message "Marked %d messages for deletion" count
)
966 (mh-delete-msg 'subject
)))))
969 (defun mh-delete-subject-or-thread ()
970 "Delete messages with same subject or thread\\<mh-folder-mode-map>.
972 To delete messages faster, you can use this command to delete all the messages
973 with the same subject as the current message. This command puts these messages
974 in a sequence named \"subject\". You can undo this action by using \\[mh-undo]
975 with a prefix argument and then specifying the \"subject\" sequence.
977 However, if the buffer is displaying a threaded view of the folder then this
978 command behaves like \\[mh-thread-delete]."
980 (if (memq 'unthread mh-view-ops
)
982 (mh-delete-subject)))
986 ;;; Message threading:
988 (defmacro mh-thread-initialize-hash
(var test
)
989 "Initialize the hash table in VAR.
990 TEST is the test to use when creating a new hash table."
991 (unless (symbolp var
) (error "Expected a symbol: %s" var
))
992 `(if ,var
(clrhash ,var
) (setq ,var
(make-hash-table :test
,test
))))
994 (defun mh-thread-initialize ()
995 "Make new hash tables, or clear them if already present."
996 (mh-thread-initialize-hash mh-thread-id-hash
#'equal
)
997 (mh-thread-initialize-hash mh-thread-subject-hash
#'equal
)
998 (mh-thread-initialize-hash mh-thread-id-table
#'eq
)
999 (mh-thread-initialize-hash mh-thread-id-index-map
#'eq
)
1000 (mh-thread-initialize-hash mh-thread-index-id-map
#'eql
)
1001 (mh-thread-initialize-hash mh-thread-scan-line-map
#'eql
)
1002 (mh-thread-initialize-hash mh-thread-subject-container-hash
#'eq
)
1003 (mh-thread-initialize-hash mh-thread-duplicates
#'eq
)
1004 (setq mh-thread-history
()))
1006 (defsubst mh-thread-id-container
(id)
1007 "Given ID, return the corresponding container in `mh-thread-id-table'.
1008 If no container exists then a suitable container is created and the id-table
1012 (or (gethash id mh-thread-id-table
)
1013 (setf (gethash id mh-thread-id-table
)
1014 (let ((message (mh-thread-make-message :id id
)))
1015 (mh-thread-make-container :message message
)))))
1017 (defsubst mh-thread-remove-parent-link
(child)
1018 "Remove parent link of CHILD if it exists."
1019 (let* ((child-container (if (mh-thread-container-p child
)
1020 child
(mh-thread-id-container child
)))
1021 (parent-container (mh-container-parent child-container
)))
1022 (when parent-container
1023 (setf (mh-container-children parent-container
)
1024 (loop for elem in
(mh-container-children parent-container
)
1025 unless
(eq child-container elem
) collect elem
))
1026 (setf (mh-container-parent child-container
) nil
))))
1028 (defsubst mh-thread-add-link
(parent child
&optional at-end-p
)
1029 "Add links so that PARENT becomes a parent of CHILD.
1030 Doesn't make any changes if CHILD is already an ancestor of PARENT. If
1031 optional argument AT-END-P is non-nil, the CHILD is added to the end of the
1032 children list of PARENT."
1033 (let ((parent-container (cond ((null parent
) nil
)
1034 ((mh-thread-container-p parent
) parent
)
1035 (t (mh-thread-id-container parent
))))
1036 (child-container (if (mh-thread-container-p child
)
1037 child
(mh-thread-id-container child
))))
1038 (when (and parent-container
1039 (not (mh-thread-ancestor-p child-container parent-container
))
1040 (not (mh-thread-ancestor-p parent-container child-container
)))
1041 (mh-thread-remove-parent-link child-container
)
1042 (cond ((not at-end-p
)
1043 (push child-container
(mh-container-children parent-container
)))
1044 ((null (mh-container-children parent-container
))
1045 (push child-container
(mh-container-children parent-container
)))
1046 (t (let ((last-child (mh-container-children parent-container
)))
1047 (while (cdr last-child
)
1048 (setq last-child
(cdr last-child
)))
1049 (setcdr last-child
(cons child-container nil
)))))
1050 (setf (mh-container-parent child-container
) parent-container
))
1051 (unless parent-container
1052 (mh-thread-remove-parent-link child-container
))))
1054 (defun mh-thread-ancestor-p (ancestor successor
)
1055 "Return t if ANCESTOR is really an ancestor of SUCCESSOR and nil otherwise.
1056 In the limit, the function returns t if ANCESTOR and SUCCESSOR are the same
1060 (when (eq ancestor successor
) (return t
))
1061 (setq successor
(mh-container-parent successor
)))
1064 (defsubst mh-thread-get-message-container
(message)
1065 "Return container which has MESSAGE in it.
1066 If there is no container present then a new container is allocated."
1067 (let* ((id (mh-message-id message
))
1068 (container (gethash id mh-thread-id-table
)))
1069 (cond (container (setf (mh-container-message container
) message
)
1071 (t (setf (gethash id mh-thread-id-table
)
1072 (mh-thread-make-container :message message
))))))
1074 (defsubst mh-thread-get-message
(id subject-re-p subject refs
)
1075 "Return appropriate message.
1076 Otherwise update message already present to have the proper ID, SUBJECT-RE-P,
1077 SUBJECT and REFS fields."
1078 (let* ((container (gethash id mh-thread-id-table
))
1079 (message (if container
(mh-container-message container
) nil
)))
1081 (setf (mh-message-subject-re-p message
) subject-re-p
)
1082 (setf (mh-message-subject message
) subject
)
1083 (setf (mh-message-id message
) id
)
1084 (setf (mh-message-references message
) refs
)
1087 (setf (mh-container-message container
)
1088 (mh-thread-make-message :id id
:references refs
1090 :subject-re-p subject-re-p
)))
1091 (t (let ((message (mh-thread-make-message :id id
:references refs
1092 :subject-re-p subject-re-p
1095 (mh-thread-get-message-container message
)))))))
1097 (defsubst mh-thread-canonicalize-id
(id)
1098 "Produce canonical string representation for ID.
1099 This allows cheap string comparison with EQ."
1100 (or (and (equal id
"") (copy-sequence ""))
1101 (gethash id mh-thread-id-hash
)
1102 (setf (gethash id mh-thread-id-hash
) id
)))
1104 (defsubst mh-thread-prune-subject
(subject)
1105 "Prune leading Re:'s, Fwd:'s etc. and trailing (fwd)'s from SUBJECT.
1106 If the result after pruning is not the empty string then it is canonicalized
1107 so that subjects can be tested for equality with eq. This is done so that all
1108 the messages without a subject are not put into a single thread."
1109 (let ((case-fold-search t
)
1110 (subject-pruned-flag nil
))
1111 ;; Prune subject leader
1112 (while (or (string-match "^[ \t]*\\(re\\|fwd?\\)\\(\\[[0-9]*\\]\\)?:[ \t]*"
1114 (string-match "^[ \t]*\\[[^\\]][ \t]*" subject
))
1115 (setq subject-pruned-flag t
)
1116 (setq subject
(substring subject
(match-end 0))))
1117 ;; Prune subject trailer
1118 (while (or (string-match "(fwd)$" subject
)
1119 (string-match "[ \t]+$" subject
))
1120 (setq subject-pruned-flag t
)
1121 (setq subject
(substring subject
0 (match-beginning 0))))
1122 ;; Canonicalize subject only if it is non-empty
1123 (cond ((equal subject
"") (values subject subject-pruned-flag
))
1125 (or (gethash subject mh-thread-subject-hash
)
1126 (setf (gethash subject mh-thread-subject-hash
) subject
))
1127 subject-pruned-flag
)))))
1129 (defun mh-thread-container-subject (container)
1130 "Return the subject of CONTAINER.
1131 If CONTAINER is empty return the subject info of one of its children."
1132 (cond ((and (mh-container-message container
)
1133 (mh-message-id (mh-container-message container
)))
1134 (mh-message-subject (mh-container-message container
)))
1136 (dolist (kid (mh-container-children container
))
1137 (when (and (mh-container-message kid
)
1138 (mh-message-id (mh-container-message kid
)))
1139 (let ((kid-message (mh-container-message kid
)))
1140 (return (mh-message-subject kid-message
)))))
1141 (error "This can't happen!")))))
1143 (defun mh-thread-rewind-pruning ()
1144 "Restore the thread tree to its state before pruning."
1145 (while mh-thread-history
1146 (let ((action (pop mh-thread-history
)))
1147 (cond ((eq (car action
) 'DROP
)
1148 (mh-thread-remove-parent-link (cadr action
))
1149 (mh-thread-add-link (caddr action
) (cadr action
)))
1150 ((eq (car action
) 'PROMOTE
)
1151 (let ((node (cadr action
))
1152 (parent (caddr action
))
1153 (children (cdddr action
)))
1154 (dolist (child children
)
1155 (mh-thread-remove-parent-link child
)
1156 (mh-thread-add-link node child
))
1157 (mh-thread-add-link parent node
)))
1158 ((eq (car action
) 'SUBJECT
)
1159 (let ((node (cadr action
)))
1160 (mh-thread-remove-parent-link node
)
1161 (setf (mh-container-real-child-p node
) t
)))))))
1163 (defun mh-thread-prune-containers (roots)
1164 "Prune empty containers in the containers ROOTS."
1165 (let ((dfs-ordered-nodes ())
1168 (let ((node (pop work-list
)))
1169 (dolist (child (mh-container-children node
))
1170 (push child work-list
))
1171 (push node dfs-ordered-nodes
)))
1172 (while dfs-ordered-nodes
1173 (let ((node (pop dfs-ordered-nodes
)))
1174 (cond ((gethash (mh-message-id (mh-container-message node
))
1175 mh-thread-id-index-map
)
1177 (setf (mh-container-children node
)
1178 (mh-thread-sort-containers (mh-container-children node
))))
1179 ((and (mh-container-children node
)
1180 (or (null (cdr (mh-container-children node
)))
1181 (mh-container-parent node
)))
1183 (let ((children ()))
1184 (dolist (kid (mh-container-children node
))
1185 (mh-thread-remove-parent-link kid
)
1186 (mh-thread-add-link (mh-container-parent node
) kid
)
1187 (push kid children
))
1188 (push `(PROMOTE ,node
,(mh-container-parent node
) ,@children
)
1190 (mh-thread-remove-parent-link node
)))
1191 ((mh-container-children node
)
1192 ;; Promote the first orphan to parent and add the other kids as
1194 (setf (mh-container-children node
)
1195 (mh-thread-sort-containers (mh-container-children node
)))
1196 (let ((new-parent (car (mh-container-children node
)))
1197 (other-kids (cdr (mh-container-children node
))))
1198 (mh-thread-remove-parent-link new-parent
)
1199 (dolist (kid other-kids
)
1200 (mh-thread-remove-parent-link kid
)
1201 (setf (mh-container-real-child-p kid
) nil
)
1202 (mh-thread-add-link new-parent kid t
))
1203 (push `(PROMOTE ,node
,(mh-container-parent node
)
1204 ,new-parent
,@other-kids
)
1206 (mh-thread-remove-parent-link node
)))
1209 (push `(DROP ,node
,(mh-container-parent node
))
1211 (mh-thread-remove-parent-link node
)))))
1213 (maphash #'(lambda (k v
)
1214 (declare (ignore k
))
1215 (when (and (null (mh-container-parent v
))
1216 (gethash (mh-message-id (mh-container-message v
))
1217 mh-thread-id-index-map
))
1220 (mh-thread-sort-containers results
))))
1222 (defun mh-thread-sort-containers (containers)
1223 "Sort a list of message CONTAINERS to be in ascending order wrt index."
1226 (when (and (mh-container-message x
) (mh-container-message y
))
1227 (let* ((id-x (mh-message-id (mh-container-message x
)))
1228 (id-y (mh-message-id (mh-container-message y
)))
1229 (index-x (gethash id-x mh-thread-id-index-map
))
1230 (index-y (gethash id-y mh-thread-id-index-map
)))
1231 (and (integerp index-x
) (integerp index-y
)
1232 (< index-x index-y
)))))))
1234 (defsubst mh-thread-group-by-subject
(roots)
1235 "Group the set of message containers, ROOTS based on subject.
1236 Bug: Check for and make sure that something without Re: is made the parent in
1237 preference to something that has it."
1238 (clrhash mh-thread-subject-container-hash
)
1240 (dolist (root roots
)
1241 (let* ((subject (mh-thread-container-subject root
))
1242 (parent (gethash subject mh-thread-subject-container-hash
)))
1243 (cond (parent (mh-thread-remove-parent-link root
)
1244 (mh-thread-add-link parent root t
)
1245 (setf (mh-container-real-child-p root
) nil
)
1246 (push `(SUBJECT ,root
) mh-thread-history
))
1248 (setf (gethash subject mh-thread-subject-container-hash
) root
)
1249 (push root results
)))))
1250 (nreverse results
)))
1252 (defun mh-thread-process-in-reply-to (reply-to-header)
1253 "Extract message id's from REPLY-TO-HEADER.
1254 Ideally this should have some regexp which will try to guess if a string
1255 between < and > is a message id and not an email address. For now it will
1256 take the last string inside angles."
1257 (let ((end (mh-search-from-end ?
> reply-to-header
)))
1259 (let ((begin (mh-search-from-end ?
< (substring reply-to-header
0 end
))))
1260 (when (numberp begin
)
1261 (list (substring reply-to-header begin
(1+ end
))))))))
1263 (defun mh-thread-set-tables (folder)
1264 "Use the tables of FOLDER in current buffer."
1265 (flet ((mh-get-table (symbol)
1268 (symbol-value symbol
))))
1269 (setq mh-thread-id-hash
(mh-get-table 'mh-thread-id-hash
))
1270 (setq mh-thread-subject-hash
(mh-get-table 'mh-thread-subject-hash
))
1271 (setq mh-thread-id-table
(mh-get-table 'mh-thread-id-table
))
1272 (setq mh-thread-id-index-map
(mh-get-table 'mh-thread-id-index-map
))
1273 (setq mh-thread-index-id-map
(mh-get-table 'mh-thread-index-id-map
))
1274 (setq mh-thread-scan-line-map
(mh-get-table 'mh-thread-scan-line-map
))
1275 (setq mh-thread-subject-container-hash
1276 (mh-get-table 'mh-thread-subject-container-hash
))
1277 (setq mh-thread-duplicates
(mh-get-table 'mh-thread-duplicates
))
1278 (setq mh-thread-history
(mh-get-table 'mh-thread-history
))))
1280 (defsubst mh-thread-update-id-index-maps
(id index
)
1281 "Message with id, ID is the message in INDEX.
1282 The function also checks for duplicate messages (that is multiple messages
1283 with the same ID). These messages are put in the `mh-thread-duplicates' hash
1285 (let ((old-index (gethash id mh-thread-id-index-map
)))
1286 (when old-index
(push old-index
(gethash id mh-thread-duplicates
)))
1287 (setf (gethash id mh-thread-id-index-map
) index
)
1288 (setf (gethash index mh-thread-index-id-map
) id
)))
1292 ;;; Generate Threads...
1294 (defvar mh-message-id-regexp
"^<.*@.*>$"
1295 "Regexp to recognize whether a string is a message identifier.")
1297 (defun mh-thread-generate (folder msg-list
)
1298 "Scan FOLDER to get info for threading.
1299 Only information about messages in MSG-LIST are added to the tree."
1301 (mh-thread-set-tables folder
)
1304 #'call-process
(expand-file-name mh-scan-prog mh-progs
) nil
'(t nil
) nil
1305 "-width" "10000" "-format"
1306 "%(msg)\n%{message-id}\n%{references}\n%{in-reply-to}\n%{subject}\n"
1307 folder
(mapcar #'(lambda (x) (format "%s" x
)) msg-list
)))
1308 (goto-char (point-min))
1310 (case-fold-search t
))
1313 (block process-message
1315 (prog1 (buffer-substring (point) (line-end-position))
1317 (index (car (read-from-string index-line
)))
1318 (id (prog1 (buffer-substring (point) (line-end-position))
1320 (refs (prog1 (buffer-substring (point) (line-end-position))
1322 (in-reply-to (prog1 (buffer-substring (point)
1323 (line-end-position))
1326 (buffer-substring (point) (line-end-position))
1329 (unless (gethash index mh-thread-scan-line-map
)
1330 (return-from process-message
))
1331 (unless (integerp index
) (return)) ;Error message here
1332 (multiple-value-setq (subject subject-re-p
)
1333 (mh-thread-prune-subject subject
))
1334 (setq in-reply-to
(mh-thread-process-in-reply-to in-reply-to
))
1335 (setq refs
(loop for x in
(append (split-string refs
) in-reply-to
)
1336 when
(string-match mh-message-id-regexp x
)
1338 (setq id
(mh-thread-canonicalize-id id
))
1339 (mh-thread-update-id-index-maps id index
)
1340 (setq refs
(mapcar #'mh-thread-canonicalize-id refs
))
1341 (mh-thread-get-message id subject-re-p subject refs
)
1342 (do ((ancestors refs
(cdr ancestors
)))
1343 ((null (cdr ancestors
))
1344 (when (car ancestors
)
1345 (mh-thread-remove-parent-link id
)
1346 (mh-thread-add-link (car ancestors
) id
)))
1347 (mh-thread-add-link (car ancestors
) (cadr ancestors
)))))))
1348 (maphash #'(lambda (k v
)
1349 (declare (ignore k
))
1350 (when (null (mh-container-parent v
))
1353 (setq roots
(mh-thread-prune-containers roots
))
1354 (prog1 (setq roots
(mh-thread-group-by-subject roots
))
1355 (let ((history mh-thread-history
))
1357 (setq mh-thread-history history
))))))
1360 (defun mh-thread-inc (folder start-point
)
1361 "Update thread tree for FOLDER.
1362 All messages after START-POINT are added to the thread tree."
1363 (mh-thread-rewind-pruning)
1364 (mh-remove-all-notation)
1365 (goto-char start-point
)
1366 (let ((msg-list ()))
1368 (let ((index (mh-get-msg-num nil
)))
1369 (when (numberp index
)
1370 (push index msg-list
)
1371 (setf (gethash index mh-thread-scan-line-map
)
1372 (mh-thread-parse-scan-line)))
1374 (let ((thread-tree (mh-thread-generate folder msg-list
))
1375 (buffer-read-only nil
)
1376 (old-buffer-modified-flag (buffer-modified-p)))
1377 (delete-region (point-min) (point-max))
1378 (mh-thread-print-scan-lines thread-tree
)
1379 (mh-notate-user-sequences)
1380 (mh-notate-deleted-and-refiled)
1382 (set-buffer-modified-p old-buffer-modified-flag
))))
1384 (defun mh-thread-generate-scan-lines (tree level
)
1385 "Generate scan lines.
1386 TREE is the hierarchical tree of messages, SCAN-LINE-MAP maps message indices
1387 to the corresponding scan lines and LEVEL used to determine indentation of
1389 (cond ((null tree
) nil
)
1390 ((mh-thread-container-p tree
)
1391 (let* ((message (mh-container-message tree
))
1392 (id (mh-message-id message
))
1393 (index (gethash id mh-thread-id-index-map
))
1394 (duplicates (gethash id mh-thread-duplicates
))
1395 (new-level (+ level
2))
1397 (force-angle-flag nil
)
1398 (increment-level-flag nil
))
1399 (dolist (scan-line (mapcar (lambda (x)
1400 (gethash x mh-thread-scan-line-map
))
1401 (reverse (cons index duplicates
))))
1403 (when (and dupl-flag
(equal level
0)
1404 (mh-thread-ancestor-p mh-thread-last-ancestor tree
))
1405 (setq level
(+ level
2)
1406 new-level
(+ new-level
2)
1407 force-angle-flag t
))
1408 (when (equal level
0)
1409 (setq mh-thread-last-ancestor tree
)
1410 (while (mh-container-parent mh-thread-last-ancestor
)
1411 (setq mh-thread-last-ancestor
1412 (mh-container-parent mh-thread-last-ancestor
))))
1413 (let* ((lev (if dupl-flag level new-level
))
1414 (square-flag (or (and (mh-container-real-child-p tree
)
1415 (not force-angle-flag
)
1418 (insert (car scan-line
)
1419 (format (format "%%%ss" lev
) "")
1420 (if square-flag
"[" "<")
1422 (if square-flag
"]" ">")
1423 (truncate-string-to-width
1424 (caddr scan-line
) (- mh-thread-body-width lev
))
1426 (setq increment-level-flag t
)
1427 (setq dupl-flag nil
)))
1428 (unless increment-level-flag
(setq new-level level
))
1429 (dolist (child (mh-container-children tree
))
1430 (mh-thread-generate-scan-lines child new-level
))))
1431 (t (let ((nlevel (+ level
2)))
1433 (mh-thread-generate-scan-lines ch nlevel
))))))
1435 ;; Another and may be better approach would be to generate all the info from
1436 ;; the scan which generates the threading info. For now this will have to do.
1437 (defun mh-thread-parse-scan-line (&optional string
)
1439 If optional argument STRING is given then that is assumed to be the scan line.
1440 Otherwise uses the line at point as the scan line to parse."
1441 (let* ((string (or string
1442 (buffer-substring-no-properties (line-beginning-position)
1443 (line-end-position))))
1444 (address-start (+ mh-cmd-note mh-scan-field-from-start-offset
))
1445 (body-start (+ mh-cmd-note mh-scan-field-from-end-offset
))
1446 (first-string (substring string
0 address-start
)))
1448 (substring string address-start
(- body-start
2))
1449 (substring string body-start
)
1453 (defun mh-thread-update-scan-line-map (msg notation offset
)
1454 "In threaded view update `mh-thread-scan-line-map'.
1455 MSG is the message being notated with NOTATION at OFFSET."
1456 (let* ((msg (or msg
(mh-get-msg-num nil
)))
1457 (cur-scan-line (and mh-thread-scan-line-map
1458 (gethash msg mh-thread-scan-line-map
)))
1459 (old-scan-lines (loop for map in mh-thread-scan-line-map-stack
1460 collect
(and map
(gethash msg map
)))))
1462 (setf (aref (car cur-scan-line
) offset
) notation
))
1463 (dolist (line old-scan-lines
)
1464 (when line
(setf (aref (car line
) offset
) notation
)))))
1467 (defun mh-thread-add-spaces (count)
1468 "Add COUNT spaces to each scan line in `mh-thread-scan-line-map'."
1469 (let ((spaces (format (format "%%%ss" count
) "")))
1471 (let* ((msg-num (mh-get-msg-num nil
))
1472 (old-line (nth 3 (gethash msg-num mh-thread-scan-line-map
))))
1473 (when (numberp msg-num
)
1474 (setf (gethash msg-num mh-thread-scan-line-map
)
1475 (mh-thread-parse-scan-line (format "%s%s" spaces old-line
)))))
1478 (defun mh-thread-print-scan-lines (thread-tree)
1479 "Print scan lines in THREAD-TREE in threaded mode."
1480 (let ((mh-thread-body-width (- (window-width) mh-cmd-note
1481 (1- mh-scan-field-subject-start-offset
)))
1482 (mh-thread-last-ancestor nil
))
1483 (if (null mh-index-data
)
1484 (mh-thread-generate-scan-lines thread-tree -
2)
1485 (loop for x in
(mh-index-group-by-folder)
1486 do
(let* ((old-map mh-thread-scan-line-map
)
1487 (mh-thread-scan-line-map (make-hash-table)))
1488 (setq mh-thread-last-ancestor nil
)
1489 (loop for msg in
(cdr x
)
1490 do
(let ((v (gethash msg old-map
)))
1492 (setf (gethash msg mh-thread-scan-line-map
) v
))))
1493 (when (> (hash-table-count mh-thread-scan-line-map
) 0)
1494 (insert (if (bobp) "" "\n") (car x
) "\n")
1495 (mh-thread-generate-scan-lines thread-tree -
2))))
1496 (mh-index-create-imenu-index))))
1498 (defun mh-thread-folder ()
1499 "Generate thread view of folder."
1500 (message "Threading %s..." (buffer-name))
1501 (mh-thread-initialize)
1502 (goto-char (point-min))
1503 (mh-remove-all-notation)
1504 (let ((msg-list ()))
1505 (mh-iterate-on-range msg
(cons (point-min) (point-max))
1506 (setf (gethash msg mh-thread-scan-line-map
) (mh-thread-parse-scan-line))
1507 (push msg msg-list
))
1508 (let* ((range (mh-coalesce-msg-list msg-list
))
1509 (thread-tree (mh-thread-generate (buffer-name) range
)))
1510 (delete-region (point-min) (point-max))
1511 (mh-thread-print-scan-lines thread-tree
)
1512 (mh-notate-user-sequences)
1513 (mh-notate-deleted-and-refiled)
1515 (message "Threading %s...done" (buffer-name)))))
1518 (defun mh-toggle-threads ()
1519 "Toggle threaded view of folder."
1521 (let ((msg-at-point (mh-get-msg-num nil
))
1522 (old-buffer-modified-flag (buffer-modified-p))
1523 (buffer-read-only nil
))
1524 (cond ((memq 'unthread mh-view-ops
)
1525 (unless (mh-valid-view-change-operation-p 'unthread
)
1526 (error "Can't unthread folder"))
1527 (let ((msg-list ()))
1528 (goto-char (point-min))
1530 (let ((index (mh-get-msg-num nil
)))
1532 (push index msg-list
)))
1534 (mh-scan-folder mh-current-folder
1535 (mapcar #'(lambda (x) (format "%s" x
))
1536 (mh-coalesce-msg-list msg-list
))
1539 (mh-index-insert-folder-headers)
1541 (t (mh-thread-folder)
1542 (push 'unthread mh-view-ops
)))
1543 (when msg-at-point
(mh-goto-msg msg-at-point t t
))
1544 (set-buffer-modified-p old-buffer-modified-flag
)
1548 (defun mh-thread-forget-message (index)
1549 "Forget the message INDEX from the threading tables."
1550 (let* ((id (gethash index mh-thread-index-id-map
))
1551 (id-index (gethash id mh-thread-id-index-map
))
1552 (duplicates (gethash id mh-thread-duplicates
)))
1553 (remhash index mh-thread-index-id-map
)
1554 (remhash index mh-thread-scan-line-map
)
1555 (cond ((and (eql index id-index
) (null duplicates
))
1556 (remhash id mh-thread-id-index-map
))
1557 ((eql index id-index
)
1558 (setf (gethash id mh-thread-id-index-map
) (car duplicates
))
1559 (setf (gethash (car duplicates
) mh-thread-index-id-map
) id
)
1560 (setf (gethash id mh-thread-duplicates
) (cdr duplicates
)))
1562 (setf (gethash id mh-thread-duplicates
)
1563 (remove index duplicates
))))))
1567 ;;; Operations on threads
1569 (defun mh-thread-current-indentation-level ()
1570 "Find the number of spaces by which current message is indented."
1572 (let ((address-start-offset (+ mh-cmd-note mh-scan-date-flag-width
1573 mh-scan-date-width
1))
1576 (forward-char address-start-offset
)
1577 (while (char-equal (char-after) ?
)
1583 (defun mh-thread-next-sibling (&optional previous-flag
)
1584 "Display next sibling.
1586 With non-nil optional argument PREVIOUS-FLAG jump to the previous sibling."
1588 (cond ((not (memq 'unthread mh-view-ops
))
1589 (error "Folder isn't threaded"))
1591 (error "No message at point")))
1593 (let ((point (point))
1595 (my-level (mh-thread-current-indentation-level)))
1596 (while (and (not done
)
1597 (equal (forward-line (if previous-flag -
1 1)) 0)
1599 (let ((level (mh-thread-current-indentation-level)))
1600 (cond ((equal level my-level
)
1601 (setq done
'success
))
1603 (message "No %s sibling" (if previous-flag
"previous" "next"))
1604 (setq done
'failure
)))))
1605 (cond ((eq done
'success
) (mh-maybe-show))
1606 ((eq done
'failure
) (goto-char point
))
1607 (t (message "No %s sibling" (if previous-flag
"previous" "next"))
1608 (goto-char point
)))))
1611 (defun mh-thread-previous-sibling ()
1612 "Display previous sibling."
1614 (mh-thread-next-sibling t
))
1616 (defun mh-thread-immediate-ancestor ()
1617 "Jump to immediate ancestor in thread tree."
1619 (let ((point (point))
1620 (ancestor-level (- (mh-thread-current-indentation-level) 2))
1622 (if (< ancestor-level
0)
1624 (while (and (not done
) (equal (forward-line -
1) 0))
1625 (when (equal ancestor-level
(mh-thread-current-indentation-level))
1632 (defun mh-thread-ancestor (&optional thread-root-flag
)
1633 "Display ancestor of current message.
1635 If you do not care for the way a particular thread has turned, you can move up
1636 the chain of messages with this command. This command can also take a prefix
1637 argument THREAD-ROOT-FLAG to jump to the message that started everything."
1640 (cond ((not (memq 'unthread mh-view-ops
))
1641 (error "Folder isn't threaded"))
1643 (error "No message at point")))
1644 (let ((current-level (mh-thread-current-indentation-level)))
1645 (cond (thread-root-flag
1646 (while (mh-thread-immediate-ancestor))
1648 ((equal current-level
1)
1649 (message "Message has no ancestor"))
1650 (t (mh-thread-immediate-ancestor)
1653 (defun mh-thread-find-children ()
1654 "Return a region containing the current message and its children.
1655 The result is returned as a list of two elements. The first is the point at the
1656 start of the region and the second is the point at the end."
1660 (let ((address-start-offset (+ mh-cmd-note mh-scan-date-flag-width
1661 mh-scan-date-width
1))
1662 (level (mh-thread-current-indentation-level))
1664 (setq begin
(point))
1665 (setq spaces
(format (format "%%%ss" (1+ level
)) ""))
1669 (forward-char address-start-offset
)
1670 (unless (equal (string-match spaces
(buffer-substring-no-properties
1671 (point) (line-end-position)))
1677 (list begin
(point)))))
1680 (defun mh-thread-delete ()
1683 (cond ((not (memq 'unthread mh-view-ops
))
1684 (error "Folder isn't threaded"))
1686 (error "No message at point"))
1687 (t (let ((region (mh-thread-find-children)))
1688 (mh-iterate-on-messages-in-region () (car region
) (cadr region
)
1689 (mh-delete-a-msg nil
))
1693 (defun mh-thread-refile (folder)
1694 "Refile (output) thread into FOLDER."
1695 (interactive (list (intern (mh-prompt-for-refile-folder))))
1696 (cond ((not (memq 'unthread mh-view-ops
))
1697 (error "Folder isn't threaded"))
1699 (error "No message at point"))
1700 (t (let ((region (mh-thread-find-children)))
1701 (mh-iterate-on-messages-in-region () (car region
) (cadr region
)
1702 (mh-refile-a-msg nil folder
))
1707 ;; Tick mark handling
1710 (defun mh-toggle-tick (range)
1711 "Toggle tick mark of RANGE.
1713 This command adds messages to the \"tick\" sequence (which you can customize
1714 via the option `mh-tick-seq'). This sequence can be viewed later with the
1715 \\[mh-index-ticked-messages] command.
1717 Check the documentation of `mh-interactive-range' to see how RANGE is read in
1719 (interactive (list (mh-interactive-range "Tick")))
1721 (error "Enable ticking by customizing `mh-tick-seq'"))
1722 (let* ((tick-seq (mh-find-seq mh-tick-seq
))
1723 (tick-seq-msgs (mh-seq-msgs tick-seq
))
1726 (mh-iterate-on-range msg range
1727 (cond ((member msg tick-seq-msgs
)
1729 (setcdr tick-seq
(delq msg
(cdr tick-seq
)))
1730 (when (null (cdr tick-seq
)) (setq mh-last-seq-used nil
))
1731 (mh-remove-sequence-notation msg
(mh-colors-in-use-p)))
1734 (setq mh-last-seq-used mh-tick-seq
)
1735 (let ((mh-seq-list (cons `(,mh-tick-seq
,msg
) mh-seq-list
)))
1736 (mh-add-sequence-notation msg
(mh-colors-in-use-p))))))
1737 (mh-add-msgs-to-seq ticked mh-tick-seq nil t
)
1738 (mh-undefine-sequence mh-tick-seq unticked
)
1740 (mh-index-add-to-sequence mh-tick-seq ticked
)
1741 (mh-index-delete-from-sequence mh-tick-seq unticked
))))
1744 (defun mh-narrow-to-tick ()
1745 "Limit to ticked messages.
1747 What this command does is show only those messages that are in the \"tick\"
1748 sequence (which you can customize via the `mh-tick-seq' option) in the
1749 MH-Folder buffer. In addition, it limits further MH-E searches to just those
1750 messages. When you want to widen the view to all your messages again, use
1753 (cond ((not mh-tick-seq
)
1754 (error "Enable ticking by customizing `mh-tick-seq'"))
1755 ((null (mh-seq-msgs (mh-find-seq mh-tick-seq
)))
1756 (message "No messages in %s sequence" mh-tick-seq
))
1757 (t (mh-narrow-to-seq mh-tick-seq
))))
1762 ;; indent-tabs-mode: nil
1763 ;; sentence-end-double-space: nil
1766 ;; arch-tag: 8e952711-01a2-485b-bf21-c9e3ad4de942
1767 ;;; mh-seq.el ends here