* mh-acros.el:
[bpt/emacs.git] / lisp / mh-e / mh-seq.el
1 ;;; mh-seq.el --- MH-E sequences support
2
3 ;; Copyright (C) 1993, 1995,
4 ;; 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5
6 ;; Author: Bill Wohler <wohler@newt.com>
7 ;; Maintainer: Bill Wohler <wohler@newt.com>
8 ;; Keywords: mail
9 ;; See: mh-e.el
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
27
28 ;;; Commentary:
29 ;;
30 ;; This tries to implement the algorithm described at:
31 ;; http://www.jwz.org/doc/threading.html
32 ;; It is also a start to implementing the IMAP Threading extension RFC. The
33 ;; implementation lacks the reference and subject canonicalization of the
34 ;; RFC.
35 ;;
36 ;; In the presentation buffer, children messages are shown indented with
37 ;; either [ ] or < > around them. Square brackets ([ ]) denote that the
38 ;; algorithm can point out some headers which when taken together implies
39 ;; that the unindented message is an ancestor of the indented message. If
40 ;; no such proof exists then angles (< >) are used.
41 ;;
42 ;; Some issues and problems are as follows:
43 ;;
44 ;; (1) Scan truncates the fields at length 512. So longer references:
45 ;; headers get mutilated. The same kind of MH format string works when
46 ;; composing messages. Is there a way to avoid this? My scan command
47 ;; is as follows:
48 ;; scan +folder -width 10000 \
49 ;; -format "%(msg)\n%{message-id}\n%{references}\n%{subject}\n"
50 ;; I would really appreciate it if someone would help me with this.
51 ;;
52 ;; (2) Implement heuristics to recognize message identifiers in
53 ;; In-Reply-To: header. Right now it just assumes that the last text
54 ;; between angles (< and >) is the message identifier. There is the
55 ;; chance that this will incorrectly use an email address like a
56 ;; message identifier.
57 ;;
58 ;; (3) Error checking of found message identifiers should be done.
59 ;;
60 ;; (4) Since this breaks the assumption that message indices increase as
61 ;; one goes down the buffer, the binary search based mh-goto-msg
62 ;; doesn't work. I have a simpler replacement which may be less
63 ;; efficient.
64 ;;
65 ;; (5) Better canonicalizing for message identifier and subject strings.
66 ;;
67
68 ;; Internal support for MH-E package.
69
70 ;;; Change Log:
71
72 ;;; Code:
73
74 (eval-when-compile (require 'mh-acros))
75 (mh-require-cl)
76 (require 'mh-e)
77
78 ;; Shush the byte-compiler
79 (defvar tool-bar-mode)
80
81 \f
82
83 ;;; Data structures (used in message threading)...
84
85 (mh-defstruct (mh-thread-message (:conc-name mh-message-)
86 (:constructor mh-thread-make-message))
87 (id nil)
88 (references ())
89 (subject "")
90 (subject-re-p nil))
91
92 (mh-defstruct (mh-thread-container (:conc-name mh-container-)
93 (:constructor mh-thread-make-container))
94 message parent children
95 (real-child-p t))
96
97 \f
98
99 ;;; Internal variables:
100
101 (defvar mh-last-seq-used nil
102 "Name of seq to which a msg was last added.")
103
104 (defvar mh-non-seq-mode-line-annotation nil
105 "Saved value of `mh-mode-line-annotation' when narrowed to a seq.")
106
107 \f
108
109 ;;; Maps and hashes...
110
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.")
137
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)
148
149 ;;;###mh-autoload
150 (defun mh-delete-seq (sequence)
151 "Delete SEQUENCE.
152
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)
164 (when mh-index-data
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))))
170
171 ;; Avoid compiler warnings
172 (defvar view-exit-action)
173
174 ;;;###mh-autoload
175 (defun mh-list-sequences ()
176 "List all sequences in folder.
177 The list appears in a buffer named \"*MH-E Sequences*\"."
178 (interactive)
179 (let ((folder mh-current-folder)
180 (temp-buffer mh-sequences-buffer)
181 (seq-list mh-seq-list)
182 (max-len 0))
183 (with-output-to-temp-buffer temp-buffer
184 (save-excursion
185 (set-buffer temp-buffer)
186 (erase-buffer)
187 (message "Listing sequences ...")
188 (insert "Sequences in folder " folder ":\n")
189 (let ((seq-list seq-list))
190 (while seq-list
191 (setq max-len
192 (max (length (symbol-name (mh-seq-name (pop seq-list))))
193 max-len)))
194 (setq max-len (+ 2 max-len)))
195 (while seq-list
196 (let ((name (mh-seq-name (car seq-list)))
197 (sorted-seq-msgs
198 (mh-coalesce-msg-list
199 (sort (copy-sequence (mh-seq-msgs (car seq-list))) '<)))
200 name-spec)
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))
205 (window-width))
206 (insert "\n")
207 (insert (format (format "%%%ss" (length name-spec)) "")))
208 (insert next-element)))
209 (insert "\n"))
210 (setq seq-list (cdr seq-list)))
211 (goto-char (point-min))
212 (view-mode-enter)
213 (setq view-exit-action 'kill-buffer)
214 (message "Listing sequences...done")))))
215
216 ;;;###mh-autoload
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
220 appears."
221 (interactive "P")
222 (if (not 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)
226 finally return nil))
227 (deleted-flag (unless dest-folder (member message mh-delete-list))))
228 (message "Message %d%s is in sequences: %s"
229 message
230 (cond (dest-folder (format " (to be refiled to %s)" dest-folder))
231 (deleted-flag (format " (to be deleted)"))
232 (t ""))
233 (mapconcat 'concat
234 (mh-list-to-string (mh-seq-containing-msg message t))
235 " "))))
236
237 ;; Avoid compiler warning
238 (defvar tool-bar-map)
239
240 (make-variable-buffer-local 'mh-non-seq-mode-line-annotation)
241
242 ;;;###mh-autoload
243 (defun mh-narrow-to-seq (sequence)
244 "Restrict display to messages in SEQUENCE.
245
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.
249
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)
264 (mh-notate-cur)
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)
269 (mh-recenter nil)
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))
274 (save-excursion
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)))
279 (t
280 (error "No messages in sequence `%s'" (symbol-name sequence))))))
281
282 ;;;###mh-autoload
283 (defun mh-put-msg-in-seq (range sequence)
284 "Add RANGE to SEQUENCE\\<mh-folder-mode-map>.
285
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.
289
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))
308 (when mh-index-data
309 (setq folders
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))))
313
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)
318 (pop mh-view-ops))
319 (t nil)))
320
321 ;;;###mh-autoload
322 (defun mh-widen (&optional all-flag)
323 "Remove last restriction.
324 If optional prefix argument ALL-FLAG is non-nil, remove all limits."
325 (interactive "P")
326 (let ((msg (mh-get-msg-num nil)))
327 (when mh-folder-view-stack
328 (cond (all-flag
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
340 (when all-flag
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))
353 (if msg
354 (mh-goto-msg msg t t))
355 (mh-notate-deleted-and-refiled)
356 (mh-notate-user-sequences)
357 (mh-notate-cur)
358 (mh-recenter nil)))
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))
362 (save-excursion
363 (set-buffer (get-buffer mh-show-buffer))
364 (set (make-local-variable 'tool-bar-map) mh-show-tool-bar-map)))))
365
366 ;; FIXME? We may want to clear all notations and add one for current-message
367 ;; and process user sequences.
368 ;;;###mh-autoload
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))))))
385
386 \f
387
388 ;;; Commands to manipulate sequences.
389
390 ;; Sequences are stored in an alist of the form:
391 ;; ((seq-name msgs ...) (seq-name msgs ...) ...)
392
393 (defvar mh-sequence-history ())
394
395 ;;;###mh-autoload
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
401 (or mh-last-seq-used
402 (car (mh-seq-containing-msg (mh-get-msg-num nil) nil)))))
403
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:"
410 (if default
411 (format "[%s] " default)
412 ""))
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)
418 (t (intern input))))
419 (msgs (mh-seq-to-msgs seq)))
420 (if (and (null msgs) not-empty)
421 (error "No messages in sequence `%s'" seq))
422 seq))
423
424 \f
425
426 ;;; Functions to read ranges with completion...
427
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)
432
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
437 completion is over."
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)))))
445 (cond ((eq flag nil)
446 (let ((res (try-completion last-word candidates predicate)))
447 (cond ((null res) nil)
448 ((eq res t) t)
449 (t (concat prefix res)))))
450 ((eq flag t)
451 (all-completions last-word candidates predicate))
452 ((eq flag 'lambda)
453 (loop for x in candidates
454 when (equal x last-word) return t
455 finally return nil)))))
456
457 ;;;###mh-autoload
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.
461
462 If FOLDER is non-nil then a range is read from that folder, otherwise use
463 `mh-current-folder'.
464
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.
471
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.
476
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.
484
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\".
487
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)))
507 nil))
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)
513 mh-seq-list
514 (mh-read-folder-sequences folder nil)))
515 (mh-range-seq-names
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))))
523 msg-list)
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)))))
532
533 ;;;###mh-autoload
534 (defun mh-translate-range (folder expr)
535 "In FOLDER, translate the string EXPR to a list of messages numbers."
536 (save-excursion
537 (let ((strings (delete "" (split-string expr "[ \t\n]")))
538 (result ()))
539 (ignore-errors
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)))))
546
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))))
550 seq-list))
551
552 ;;;###mh-autoload
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)))
558 (or old-seq
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)))
564
565 ;;;###mh-autoload
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))
572 (beginning-of-line)
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))))
577
578 ;;;###mh-autoload
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)))
583 (if msgs
584 (apply 'mh-exec-cmd "mark" mh-current-folder "-add"
585 "-sequence" (symbol-name seq)
586 (mh-coalesce-msg-list msgs)))))
587
588 (defvar mh-thread-last-ancestor)
589
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.
596 (save-excursion
597 (let* ((msgs (mh-seq-to-msgs seq))
598 (coalesced-msgs (mh-coalesce-msg-list msgs)))
599 (goto-char (point-max))
600 (save-restriction
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))
614 (mh-index-data
615 (mh-index-insert-folder-headers)))))))
616
617 ;;;###mh-autoload
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.
622
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))
627 `(save-excursion
628 (goto-char ,begin)
629 (beginning-of-line)
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))) ())
633 ,@body))
634 (forward-line 1)))))
635
636 (put 'mh-iterate-on-messages-in-region 'lisp-indent-hook 'defun)
637
638 ;;;###mh-autoload
639 (defmacro mh-iterate-on-range (var range &rest body)
640 "Iterate an operation over a region or sequence.
641
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,
645 BODY is executed.
646
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)) ())
657 ,@body)))
658 ((and (consp ,range)
659 (numberp (car ,range)) (numberp (cdr ,range)))
660 (mh-iterate-on-messages-in-region ,var
661 (car ,range) (cdr ,range)
662 ,@body))
663 (t (let ((,msgs (cond ((and ,range (symbolp ,range))
664 (mh-seq-to-msgs ,range))
665 ((stringp ,range)
666 (mh-translate-range mh-current-folder
667 ,range))
668 (t ,range)))
669 (,seq-hash-table (make-hash-table)))
670 (dolist (msg ,msgs)
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)) ())
675 ,@body))))))))
676
677 (put 'mh-iterate-on-range 'lisp-indent-hook 'defun)
678
679 ;;;###mh-autoload
680 (defun mh-range-to-msg-list (range)
681 "Return a list of messages for RANGE.
682
683 Check the documentation of `mh-interactive-range' to see how RANGE is read in
684 interactive use."
685 (let (msg-list)
686 (mh-iterate-on-range msg range
687 (push msg msg-list))
688 (nreverse msg-list)))
689
690 ;;;###mh-autoload
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.
694
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.
697
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.
700
701 If a MH range is given, say something like last:20, then a list containing
702 the messages in that range is returned.
703
704 If DEFAULT non-nil then it is returned.
705
706 Otherwise, the message number at point is returned.
707
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))
712 (default default)
713 (t (mh-get-msg-num t))))
714
715 \f
716
717 ;;; Commands to handle new 'subject sequence ("Poor man's threading" by psg)
718
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.
725
726 Return number of messages put in the sequence:
727
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)))
734
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.
740
741 Return number of messages put in the sequence:
742
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"))
748 (save-excursion
749 (beginning-of-line)
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")
754 nil)
755 (let ((subject (match-string-no-properties 3))
756 (list))
757 (if (> (length subject) 41)
758 (setq subject (substring subject 0 41)))
759 (save-excursion
760 (if all
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))))))
768 (cond
769 (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)))
776 (while sorted-list
777 (mh-add-msgs-to-seq (car sorted-list) 'subject nil)
778 (setq sorted-list (cdr sorted-list)))
779 (safe-length list)))
780 (t
781 0))))))
782
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.
787
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
790 account."
791 (let* ((cur (mh-get-msg-num nil))
792 (subject (mh-thread-find-msg-subject cur))
793 region msgs)
794 (if (null subject)
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)
799 (push msg msgs)))
800 (setq msgs (sort msgs #'mh-lessp))
801 (if (null msgs)
802 0
803 (when (assoc 'subject mh-seq-list)
804 (mh-delete-seq 'subject))
805 (mh-add-msgs-to-seq msgs 'subject)
806 (length msgs)))))
807
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."
811 (ignore-errors
812 (mh-message-subject
813 (mh-container-message (gethash (gethash msg mh-thread-index-id-map)
814 mh-thread-id-table)))))
815
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: "
822 default-string))
823 default)))
824
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)
832 (while full-list
833 (setq current-arg (car full-list))
834 (if (null (string-match "^-" current-arg))
835 (setq collection
836 (if (null collection)
837 current-arg
838 (format "%s %s" collection current-arg)))
839 (when collection
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)))
844 (when collection
845 (setq arg-list (append arg-list (list collection))))
846 arg-list))
847
848 ;;;###mh-autoload
849 (defun mh-narrow-to-subject (&optional pick-expr)
850 "Limit to messages with same subject.
851 With a prefix argument, edit PICK-EXPR.
852
853 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
854 (interactive
855 (list (mh-edit-pick-expr (mh-current-message-header-field 'subject))))
856 (mh-narrow-to-header-field 'subject pick-expr))
857
858 ;;;###mh-autoload
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.
862
863 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
864 (interactive
865 (list (mh-edit-pick-expr (mh-current-message-header-field 'from))))
866 (mh-narrow-to-header-field 'from pick-expr))
867
868 ;;;###mh-autoload
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.
872
873 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
874 (interactive
875 (list (mh-edit-pick-expr (mh-current-message-header-field 'cc))))
876 (mh-narrow-to-header-field 'cc pick-expr))
877
878 ;;;###mh-autoload
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.
882
883 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
884 (interactive
885 (list (mh-edit-pick-expr (mh-current-message-header-field 'to))))
886 (mh-narrow-to-header-field 'to pick-expr))
887
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)))))
894 (msg-list ()))
895 (with-temp-buffer
896 (apply #'mh-exec-cmd-output "pick" nil folder
897 (append original (list "-list") pick-expr))
898 (goto-char (point-min))
899 (while (not (eobp))
900 (let ((num (read-from-string
901 (buffer-substring (point) (line-end-position)))))
902 (when (numberp (car num)) (push (car num) msg-list))
903 (forward-line))))
904 (if (null 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))))
909
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)))
913 (when num
914 (let ((folder mh-current-folder))
915 (with-temp-buffer
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))
921 ""))
922 (field-option (format "-%s" header-field))
923 (patterns (loop for x in (split-string field "[ ]*,[ ]*")
924 unless (equal x "")
925 collect (if (string-match "<\\(.*@.*\\)>" x)
926 (match-string 1 x)
927 x))))
928 (when patterns
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))))))))
933
934 ;;;###mh-autoload
935 (defun mh-narrow-to-range (range)
936 "Limit to RANGE.
937
938 Check the documentation of `mh-interactive-range' to see how RANGE is read in
939 interactive use.
940
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))
946
947
948 ;;;###mh-autoload
949 (defun mh-delete-subject ()
950 "Delete messages with same subject\\<mh-folder-mode-map>.
951
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."
956 (interactive)
957 (let ((count (mh-subject-to-sequence nil)))
958 (cond
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)))))
967
968 ;;;###mh-autoload
969 (defun mh-delete-subject-or-thread ()
970 "Delete messages with same subject or thread\\<mh-folder-mode-map>.
971
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.
976
977 However, if the buffer is displaying a threaded view of the folder then this
978 command behaves like \\[mh-thread-delete]."
979 (interactive)
980 (if (memq 'unthread mh-view-ops)
981 (mh-thread-delete)
982 (mh-delete-subject)))
983
984 \f
985
986 ;;; Message threading:
987
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))))
993
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 ()))
1005
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
1009 is updated."
1010 (when (not id)
1011 (error "1"))
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)))))
1016
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))))
1027
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))))
1053
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
1057 containers."
1058 (block nil
1059 (while successor
1060 (when (eq ancestor successor) (return t))
1061 (setq successor (mh-container-parent successor)))
1062 nil))
1063
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)
1070 container)
1071 (t (setf (gethash id mh-thread-id-table)
1072 (mh-thread-make-container :message message))))))
1073
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)))
1080 (cond (message
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)
1085 message)
1086 (container
1087 (setf (mh-container-message container)
1088 (mh-thread-make-message :id id :references refs
1089 :subject subject
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
1093 :subject subject)))
1094 (prog1 message
1095 (mh-thread-get-message-container message)))))))
1096
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)))
1103
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]*"
1113 subject)
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))
1124 (t (values
1125 (or (gethash subject mh-thread-subject-hash)
1126 (setf (gethash subject mh-thread-subject-hash) subject))
1127 subject-pruned-flag)))))
1128
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)))
1135 (t (block nil
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!")))))
1142
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)))))))
1162
1163 (defun mh-thread-prune-containers (roots)
1164 "Prune empty containers in the containers ROOTS."
1165 (let ((dfs-ordered-nodes ())
1166 (work-list roots))
1167 (while work-list
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)
1176 ;; Keep it
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)))
1182 ;; Promote kids
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)
1189 mh-thread-history)
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
1193 ;; his children
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)
1205 mh-thread-history)
1206 (mh-thread-remove-parent-link node)))
1207 (t
1208 ;; Drop it
1209 (push `(DROP ,node ,(mh-container-parent node))
1210 mh-thread-history)
1211 (mh-thread-remove-parent-link node)))))
1212 (let ((results ()))
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))
1218 (push v results)))
1219 mh-thread-id-table)
1220 (mh-thread-sort-containers results))))
1221
1222 (defun mh-thread-sort-containers (containers)
1223 "Sort a list of message CONTAINERS to be in ascending order wrt index."
1224 (sort containers
1225 #'(lambda (x y)
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)))))))
1233
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)
1239 (let ((results ()))
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))
1247 (t
1248 (setf (gethash subject mh-thread-subject-container-hash) root)
1249 (push root results)))))
1250 (nreverse results)))
1251
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)))
1258 (when (numberp end)
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))))))))
1262
1263 (defun mh-thread-set-tables (folder)
1264 "Use the tables of FOLDER in current buffer."
1265 (flet ((mh-get-table (symbol)
1266 (save-excursion
1267 (set-buffer folder)
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))))
1279
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
1284 table."
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)))
1289
1290 \f
1291
1292 ;;; Generate Threads...
1293
1294 (defvar mh-message-id-regexp "^<.*@.*>$"
1295 "Regexp to recognize whether a string is a message identifier.")
1296
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."
1300 (with-temp-buffer
1301 (mh-thread-set-tables folder)
1302 (when msg-list
1303 (apply
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))
1309 (let ((roots ())
1310 (case-fold-search t))
1311 (block nil
1312 (while (not (eobp))
1313 (block process-message
1314 (let* ((index-line
1315 (prog1 (buffer-substring (point) (line-end-position))
1316 (forward-line)))
1317 (index (car (read-from-string index-line)))
1318 (id (prog1 (buffer-substring (point) (line-end-position))
1319 (forward-line)))
1320 (refs (prog1 (buffer-substring (point) (line-end-position))
1321 (forward-line)))
1322 (in-reply-to (prog1 (buffer-substring (point)
1323 (line-end-position))
1324 (forward-line)))
1325 (subject (prog1
1326 (buffer-substring (point) (line-end-position))
1327 (forward-line)))
1328 (subject-re-p nil))
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)
1337 collect 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))
1351 (push v roots)))
1352 mh-thread-id-table)
1353 (setq roots (mh-thread-prune-containers roots))
1354 (prog1 (setq roots (mh-thread-group-by-subject roots))
1355 (let ((history mh-thread-history))
1356 (set-buffer folder)
1357 (setq mh-thread-history history))))))
1358
1359 ;;;###mh-autoload
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 ()))
1367 (while (not (eobp))
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)))
1373 (forward-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)
1381 (mh-notate-cur)
1382 (set-buffer-modified-p old-buffer-modified-flag))))
1383
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
1388 the message."
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))
1396 (dupl-flag t)
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))))
1402 (when scan-line
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)
1416 dupl-flag)
1417 (equal lev 0))))
1418 (insert (car scan-line)
1419 (format (format "%%%ss" lev) "")
1420 (if square-flag "[" "<")
1421 (cadr scan-line)
1422 (if square-flag "]" ">")
1423 (truncate-string-to-width
1424 (caddr scan-line) (- mh-thread-body-width lev))
1425 "\n"))
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)))
1432 (dolist (ch tree)
1433 (mh-thread-generate-scan-lines ch nlevel))))))
1434
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)
1438 "Parse a scan line.
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)))
1447 (list first-string
1448 (substring string address-start (- body-start 2))
1449 (substring string body-start)
1450 string)))
1451
1452 ;;;###mh-autoload
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)))))
1461 (when cur-scan-line
1462 (setf (aref (car cur-scan-line) offset) notation))
1463 (dolist (line old-scan-lines)
1464 (when line (setf (aref (car line) offset) notation)))))
1465
1466 ;;;###mh-autoload
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) "")))
1470 (while (not (eobp))
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)))))
1476 (forward-line 1))))
1477
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)))
1491 (when v
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))))
1497
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)
1514 (mh-notate-cur)
1515 (message "Threading %s...done" (buffer-name)))))
1516
1517 ;;;###mh-autoload
1518 (defun mh-toggle-threads ()
1519 "Toggle threaded view of folder."
1520 (interactive)
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))
1529 (while (not (eobp))
1530 (let ((index (mh-get-msg-num nil)))
1531 (when index
1532 (push index msg-list)))
1533 (forward-line))
1534 (mh-scan-folder mh-current-folder
1535 (mapcar #'(lambda (x) (format "%s" x))
1536 (mh-coalesce-msg-list msg-list))
1537 t))
1538 (when mh-index-data
1539 (mh-index-insert-folder-headers)
1540 (mh-notate-cur)))
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)
1545 (mh-recenter nil)))
1546
1547 ;;;###mh-autoload
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)))
1561 (t
1562 (setf (gethash id mh-thread-duplicates)
1563 (remove index duplicates))))))
1564
1565 \f
1566
1567 ;;; Operations on threads
1568
1569 (defun mh-thread-current-indentation-level ()
1570 "Find the number of spaces by which current message is indented."
1571 (save-excursion
1572 (let ((address-start-offset (+ mh-cmd-note mh-scan-date-flag-width
1573 mh-scan-date-width 1))
1574 (level 0))
1575 (beginning-of-line)
1576 (forward-char address-start-offset)
1577 (while (char-equal (char-after) ? )
1578 (incf level)
1579 (forward-char))
1580 level)))
1581
1582 ;;;###mh-autoload
1583 (defun mh-thread-next-sibling (&optional previous-flag)
1584 "Display next sibling.
1585
1586 With non-nil optional argument PREVIOUS-FLAG jump to the previous sibling."
1587 (interactive)
1588 (cond ((not (memq 'unthread mh-view-ops))
1589 (error "Folder isn't threaded"))
1590 ((eobp)
1591 (error "No message at point")))
1592 (beginning-of-line)
1593 (let ((point (point))
1594 (done nil)
1595 (my-level (mh-thread-current-indentation-level)))
1596 (while (and (not done)
1597 (equal (forward-line (if previous-flag -1 1)) 0)
1598 (not (eobp)))
1599 (let ((level (mh-thread-current-indentation-level)))
1600 (cond ((equal level my-level)
1601 (setq done 'success))
1602 ((< level my-level)
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)))))
1609
1610 ;;;###mh-autoload
1611 (defun mh-thread-previous-sibling ()
1612 "Display previous sibling."
1613 (interactive)
1614 (mh-thread-next-sibling t))
1615
1616 (defun mh-thread-immediate-ancestor ()
1617 "Jump to immediate ancestor in thread tree."
1618 (beginning-of-line)
1619 (let ((point (point))
1620 (ancestor-level (- (mh-thread-current-indentation-level) 2))
1621 (done nil))
1622 (if (< ancestor-level 0)
1623 nil
1624 (while (and (not done) (equal (forward-line -1) 0))
1625 (when (equal ancestor-level (mh-thread-current-indentation-level))
1626 (setq done t)))
1627 (unless done
1628 (goto-char point))
1629 done)))
1630
1631 ;;;###mh-autoload
1632 (defun mh-thread-ancestor (&optional thread-root-flag)
1633 "Display ancestor of current message.
1634
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."
1638 (interactive "P")
1639 (beginning-of-line)
1640 (cond ((not (memq 'unthread mh-view-ops))
1641 (error "Folder isn't threaded"))
1642 ((eobp)
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))
1647 (mh-maybe-show))
1648 ((equal current-level 1)
1649 (message "Message has no ancestor"))
1650 (t (mh-thread-immediate-ancestor)
1651 (mh-maybe-show)))))
1652
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."
1657 (beginning-of-line)
1658 (if (eobp)
1659 nil
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))
1663 spaces begin)
1664 (setq begin (point))
1665 (setq spaces (format (format "%%%ss" (1+ level)) ""))
1666 (forward-line)
1667 (block nil
1668 (while (not (eobp))
1669 (forward-char address-start-offset)
1670 (unless (equal (string-match spaces (buffer-substring-no-properties
1671 (point) (line-end-position)))
1672 0)
1673 (beginning-of-line)
1674 (backward-char)
1675 (return))
1676 (forward-line)))
1677 (list begin (point)))))
1678
1679 ;;;###mh-autoload
1680 (defun mh-thread-delete ()
1681 "Delete thread."
1682 (interactive)
1683 (cond ((not (memq 'unthread mh-view-ops))
1684 (error "Folder isn't threaded"))
1685 ((eobp)
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))
1690 (mh-next-msg)))))
1691
1692 ;;;###mh-autoload
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"))
1698 ((eobp)
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))
1703 (mh-next-msg)))))
1704
1705 \f
1706
1707 ;; Tick mark handling
1708
1709 ;;;###mh-autoload
1710 (defun mh-toggle-tick (range)
1711 "Toggle tick mark of RANGE.
1712
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.
1716
1717 Check the documentation of `mh-interactive-range' to see how RANGE is read in
1718 interactive use."
1719 (interactive (list (mh-interactive-range "Tick")))
1720 (unless mh-tick-seq
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))
1724 (ticked ())
1725 (unticked ()))
1726 (mh-iterate-on-range msg range
1727 (cond ((member msg tick-seq-msgs)
1728 (push msg unticked)
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)))
1732 (t
1733 (push msg ticked)
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)
1739 (when mh-index-data
1740 (mh-index-add-to-sequence mh-tick-seq ticked)
1741 (mh-index-delete-from-sequence mh-tick-seq unticked))))
1742
1743 ;;;###mh-autoload
1744 (defun mh-narrow-to-tick ()
1745 "Limit to ticked messages.
1746
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
1751 \\[mh-widen]."
1752 (interactive)
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))))
1758
1759 (provide 'mh-seq)
1760
1761 ;; Local Variables:
1762 ;; indent-tabs-mode: nil
1763 ;; sentence-end-double-space: nil
1764 ;; End:
1765
1766 ;; arch-tag: 8e952711-01a2-485b-bf21-c9e3ad4de942
1767 ;;; mh-seq.el ends here