]> code.delx.au - gnu-emacs/blob - lisp/mh-e/mh-seq.el
* mh-comp.el (mh-forward): Went over all uses of the word "RANGE" in
[gnu-emacs] / 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 ;;; Data structures (used in message threading)...
82 (mh-defstruct (mh-thread-message (:conc-name mh-message-)
83 (:constructor mh-thread-make-message))
84 (id nil)
85 (references ())
86 (subject "")
87 (subject-re-p nil))
88
89 (mh-defstruct (mh-thread-container (:conc-name mh-container-)
90 (:constructor mh-thread-make-container))
91 message parent children
92 (real-child-p t))
93
94
95 ;;; Internal variables:
96 (defvar mh-last-seq-used nil
97 "Name of seq to which a msg was last added.")
98
99 (defvar mh-non-seq-mode-line-annotation nil
100 "Saved value of `mh-mode-line-annotation' when narrowed to a seq.")
101
102 ;;; Maps and hashes...
103 (defvar mh-thread-id-hash nil
104 "Hashtable used to canonicalize message identifiers.")
105 (defvar mh-thread-subject-hash nil
106 "Hashtable used to canonicalize subject strings.")
107 (defvar mh-thread-id-table nil
108 "Thread ID table maps from message identifiers to message containers.")
109 (defvar mh-thread-id-index-map nil
110 "Table to look up message index number from message identifier.")
111 (defvar mh-thread-index-id-map nil
112 "Table to look up message identifier from message index.")
113 (defvar mh-thread-scan-line-map nil
114 "Map of message index to various parts of the scan line.")
115 (defvar mh-thread-scan-line-map-stack nil
116 "Old map of message index to various parts of the scan line.
117 This is the original map that is stored when the folder is narrowed.")
118 (defvar mh-thread-subject-container-hash nil
119 "Hashtable used to group messages by subject.")
120 (defvar mh-thread-duplicates nil
121 "Hashtable used to associate messages with the same message identifier.")
122 (defvar mh-thread-history ()
123 "Variable to remember the transformations to the thread tree.
124 When new messages are added, these transformations are rewound, then the
125 links are added from the newly seen messages. Finally the transformations are
126 redone to get the new thread tree. This makes incremental threading easier.")
127 (defvar mh-thread-body-width nil
128 "Width of scan substring that contains subject and body of message.")
129
130 (make-variable-buffer-local 'mh-thread-id-hash)
131 (make-variable-buffer-local 'mh-thread-subject-hash)
132 (make-variable-buffer-local 'mh-thread-id-table)
133 (make-variable-buffer-local 'mh-thread-id-index-map)
134 (make-variable-buffer-local 'mh-thread-index-id-map)
135 (make-variable-buffer-local 'mh-thread-scan-line-map)
136 (make-variable-buffer-local 'mh-thread-scan-line-map-stack)
137 (make-variable-buffer-local 'mh-thread-subject-container-hash)
138 (make-variable-buffer-local 'mh-thread-duplicates)
139 (make-variable-buffer-local 'mh-thread-history)
140
141 ;;;###mh-autoload
142 (defun mh-delete-seq (sequence)
143 "Delete SEQUENCE.
144
145 You are prompted for the sequence to delete. Note that this deletes only the
146 sequence, not the messages in the sequence. If you want to delete the
147 messages, use \"\\[universal-argument] \\[mh-delete-msg]\"."
148 (interactive (list (mh-read-seq-default "Delete" t)))
149 (let ((msg-list (mh-seq-to-msgs sequence))
150 (internal-flag (mh-internal-seq sequence))
151 (folders-changed (list mh-current-folder)))
152 (mh-iterate-on-range msg sequence
153 (mh-remove-sequence-notation msg internal-flag))
154 (mh-undefine-sequence sequence '("all"))
155 (mh-delete-seq-locally sequence)
156 (when mh-index-data
157 (setq folders-changed
158 (append folders-changed
159 (mh-index-delete-from-sequence sequence msg-list))))
160 (when (and (eq sequence mh-unseen-seq) (mh-speed-flists-active-p))
161 (apply #'mh-speed-flists t folders-changed))))
162
163 ;; Avoid compiler warnings
164 (defvar view-exit-action)
165
166 ;;;###mh-autoload
167 (defun mh-list-sequences ()
168 "List all sequences in folder.
169 The list appears in a buffer named \"*MH-E Sequences*\"."
170 (interactive)
171 (let ((folder mh-current-folder)
172 (temp-buffer mh-sequences-buffer)
173 (seq-list mh-seq-list)
174 (max-len 0))
175 (with-output-to-temp-buffer temp-buffer
176 (save-excursion
177 (set-buffer temp-buffer)
178 (erase-buffer)
179 (message "Listing sequences ...")
180 (insert "Sequences in folder " folder ":\n")
181 (let ((seq-list seq-list))
182 (while seq-list
183 (setq max-len
184 (max (length (symbol-name (mh-seq-name (pop seq-list))))
185 max-len)))
186 (setq max-len (+ 2 max-len)))
187 (while seq-list
188 (let ((name (mh-seq-name (car seq-list)))
189 (sorted-seq-msgs
190 (mh-coalesce-msg-list
191 (sort (copy-sequence (mh-seq-msgs (car seq-list))) '<)))
192 name-spec)
193 (insert (setq name-spec (format (format "%%%ss:" max-len) name)))
194 (while sorted-seq-msgs
195 (let ((next-element (format " %s" (pop sorted-seq-msgs))))
196 (when (>= (+ (current-column) (length next-element))
197 (window-width))
198 (insert "\n")
199 (insert (format (format "%%%ss" (length name-spec)) "")))
200 (insert next-element)))
201 (insert "\n"))
202 (setq seq-list (cdr seq-list)))
203 (goto-char (point-min))
204 (view-mode-enter)
205 (setq view-exit-action 'kill-buffer)
206 (message "Listing sequences...done")))))
207
208 ;;;###mh-autoload
209 (defun mh-msg-is-in-seq (message)
210 "Display the sequences in which the current message appears.
211 Use a prefix argument to display the sequences in which another MESSAGE
212 appears."
213 (interactive "P")
214 (if (not message)
215 (setq message (mh-get-msg-num t)))
216 (let* ((dest-folder (loop for seq in mh-refile-list
217 when (member message (cdr seq)) return (car seq)
218 finally return nil))
219 (deleted-flag (unless dest-folder (member message mh-delete-list))))
220 (message "Message %d%s is in sequences: %s"
221 message
222 (cond (dest-folder (format " (to be refiled to %s)" dest-folder))
223 (deleted-flag (format " (to be deleted)"))
224 (t ""))
225 (mapconcat 'concat
226 (mh-list-to-string (mh-seq-containing-msg message t))
227 " "))))
228
229 ;; Avoid compiler warning
230 (defvar tool-bar-map)
231
232 (make-variable-buffer-local 'mh-non-seq-mode-line-annotation)
233
234 ;;;###mh-autoload
235 (defun mh-narrow-to-seq (sequence)
236 "Restrict display to messages in SEQUENCE.
237
238 You are prompted for the name of the sequence. What this command does is show
239 only those messages that are in the selected sequence in the MH-Folder buffer.
240 In addition, it limits further MH-E searches to just those messages.
241
242 When you want to widen the view to all your messages again, use \\[mh-widen]."
243 (interactive (list (mh-read-seq "Narrow to" t)))
244 (with-mh-folder-updating (t)
245 (cond ((mh-seq-to-msgs sequence)
246 (mh-remove-all-notation)
247 (let ((eob (point-max))
248 (msg-at-cursor (mh-get-msg-num nil)))
249 (push mh-thread-scan-line-map mh-thread-scan-line-map-stack)
250 (setq mh-thread-scan-line-map (make-hash-table :test #'eql))
251 (mh-copy-seq-to-eob sequence)
252 (push (buffer-substring-no-properties (point-min) eob)
253 mh-folder-view-stack)
254 (delete-region (point-min) eob)
255 (mh-notate-deleted-and-refiled)
256 (mh-notate-cur)
257 (when msg-at-cursor (mh-goto-msg msg-at-cursor t t))
258 (setq mh-non-seq-mode-line-annotation mh-mode-line-annotation)
259 (setq mh-mode-line-annotation (symbol-name sequence))
260 (mh-make-folder-mode-line)
261 (mh-recenter nil)
262 (when (and (boundp 'tool-bar-mode) tool-bar-mode)
263 (set (make-local-variable 'tool-bar-map)
264 mh-folder-seq-tool-bar-map)
265 (when (buffer-live-p (get-buffer mh-show-buffer))
266 (save-excursion
267 (set-buffer (get-buffer mh-show-buffer))
268 (set (make-local-variable 'tool-bar-map)
269 mh-show-seq-tool-bar-map))))
270 (push 'widen mh-view-ops)))
271 (t
272 (error "No messages in sequence `%s'" (symbol-name sequence))))))
273
274 ;;;###mh-autoload
275 (defun mh-put-msg-in-seq (range sequence)
276 "Add RANGE to SEQUENCE\\<mh-folder-mode-map>.
277
278 To place a message in a sequence, use this command to do it manually, or use
279 the MH command \"pick\" or the MH-E version of \"pick\", \\[mh-search-folder],
280 which create a sequence automatically.
281
282 Give this command a RANGE and you can add all the messages in a sequence to
283 another sequence (for example, \"\\[universal-argument] \\[mh-put-msg-in-seq]
284 SourceSequence RET DestSequence RET\"). Check the documentation of
285 `mh-interactive-range' to see how RANGE is read in interactive use."
286 (interactive (list (mh-interactive-range "Add messages from")
287 (mh-read-seq-default "Add to" nil)))
288 (unless (mh-valid-seq-p sequence)
289 (error "Can't put message in invalid sequence `%s'" sequence))
290 (let* ((internal-seq-flag (mh-internal-seq sequence))
291 (original-msgs (mh-seq-msgs (mh-find-seq sequence)))
292 (folders (list mh-current-folder))
293 (msg-list (mh-range-to-msg-list range)))
294 (mh-add-msgs-to-seq msg-list sequence nil t)
295 (mh-iterate-on-range m range
296 (unless (memq m original-msgs)
297 (mh-add-sequence-notation m internal-seq-flag)))
298 (if (not internal-seq-flag)
299 (setq mh-last-seq-used sequence))
300 (when mh-index-data
301 (setq folders
302 (append folders (mh-index-add-to-sequence sequence msg-list))))
303 (when (and (eq sequence mh-unseen-seq) (mh-speed-flists-active-p))
304 (apply #'mh-speed-flists t folders))))
305
306 (defun mh-valid-view-change-operation-p (op)
307 "Check if the view change operation can be performed.
308 OP is one of 'widen and 'unthread."
309 (cond ((eq (car mh-view-ops) op)
310 (pop mh-view-ops))
311 (t nil)))
312
313 ;;;###mh-autoload
314 (defun mh-widen (&optional all-flag)
315 "Remove last restriction.
316 If optional prefix argument ALL-FLAG is non-nil, remove all limits."
317 (interactive "P")
318 (let ((msg (mh-get-msg-num nil)))
319 (when mh-folder-view-stack
320 (cond (all-flag
321 (while (cdr mh-view-ops)
322 (setq mh-view-ops (cdr mh-view-ops)))
323 (when (eq (car mh-view-ops) 'widen)
324 (setq mh-view-ops (cdr mh-view-ops))))
325 ((mh-valid-view-change-operation-p 'widen) nil)
326 ((memq 'widen mh-view-ops)
327 (while (not (eq (car mh-view-ops) 'widen))
328 (setq mh-view-ops (cdr mh-view-ops)))
329 (setq mh-view-ops (cdr mh-view-ops)))
330 (t (error "Widening is not applicable")))
331 ;; If ALL-FLAG is non-nil then rewind stacks
332 (when all-flag
333 (while (cdr mh-thread-scan-line-map-stack)
334 (setq mh-thread-scan-line-map-stack
335 (cdr mh-thread-scan-line-map-stack)))
336 (while (cdr mh-folder-view-stack)
337 (setq mh-folder-view-stack (cdr mh-folder-view-stack))))
338 (setq mh-thread-scan-line-map (pop mh-thread-scan-line-map-stack))
339 (with-mh-folder-updating (t)
340 (delete-region (point-min) (point-max))
341 (insert (pop mh-folder-view-stack))
342 (mh-remove-all-notation)
343 (setq mh-mode-line-annotation mh-non-seq-mode-line-annotation)
344 (mh-make-folder-mode-line))
345 (if msg
346 (mh-goto-msg msg t t))
347 (mh-notate-deleted-and-refiled)
348 (mh-notate-user-sequences)
349 (mh-notate-cur)
350 (mh-recenter nil)))
351 (when (and (null mh-folder-view-stack) (boundp 'tool-bar-mode) tool-bar-mode)
352 (set (make-local-variable 'tool-bar-map) mh-folder-tool-bar-map)
353 (when (buffer-live-p (get-buffer mh-show-buffer))
354 (save-excursion
355 (set-buffer (get-buffer mh-show-buffer))
356 (set (make-local-variable 'tool-bar-map) mh-show-tool-bar-map)))))
357
358 ;; FIXME? We may want to clear all notations and add one for current-message
359 ;; and process user sequences.
360 ;;;###mh-autoload
361 (defun mh-notate-deleted-and-refiled ()
362 "Notate messages marked for deletion or refiling.
363 Messages to be deleted are given by `mh-delete-list' while messages to be
364 refiled are present in `mh-refile-list'."
365 (let ((refiled-hash (make-hash-table))
366 (deleted-hash (make-hash-table)))
367 (dolist (msg mh-delete-list)
368 (setf (gethash msg deleted-hash) t))
369 (dolist (dest-msg-list mh-refile-list)
370 (dolist (msg (cdr dest-msg-list))
371 (setf (gethash msg refiled-hash) t)))
372 (mh-iterate-on-messages-in-region msg (point-min) (point-max)
373 (cond ((gethash msg refiled-hash)
374 (mh-notate nil mh-note-refiled mh-cmd-note))
375 ((gethash msg deleted-hash)
376 (mh-notate nil mh-note-deleted mh-cmd-note))))))
377
378 \f
379
380 ;;; Commands to manipulate sequences. Sequences are stored in an alist
381 ;;; of the form:
382 ;;; ((seq-name msgs ...) (seq-name msgs ...) ...)
383
384 (defvar mh-sequence-history ())
385
386 ;;;###mh-autoload
387 (defun mh-read-seq-default (prompt not-empty)
388 "Read and return sequence name with default narrowed or previous sequence.
389 PROMPT is the prompt to use when reading. If NOT-EMPTY is non-nil then a
390 non-empty sequence is read."
391 (mh-read-seq prompt not-empty
392 (or mh-last-seq-used
393 (car (mh-seq-containing-msg (mh-get-msg-num nil) nil)))))
394
395 (defun mh-read-seq (prompt not-empty &optional default)
396 "Read and return a sequence name.
397 Prompt with PROMPT, raise an error if the sequence is empty and the NOT-EMPTY
398 flag is non-nil, and supply an optional DEFAULT sequence. A reply of '%'
399 defaults to the first sequence containing the current message."
400 (let* ((input (completing-read (format "%s %s %s" prompt "sequence:"
401 (if default
402 (format "[%s] " default)
403 ""))
404 (mh-seq-names mh-seq-list)
405 nil nil nil 'mh-sequence-history))
406 (seq (cond ((equal input "%")
407 (car (mh-seq-containing-msg (mh-get-msg-num t) nil)))
408 ((equal input "") default)
409 (t (intern input))))
410 (msgs (mh-seq-to-msgs seq)))
411 (if (and (null msgs) not-empty)
412 (error "No messages in sequence `%s'" seq))
413 seq))
414
415 ;;; Functions to read ranges with completion...
416 (defvar mh-range-seq-names)
417 (defvar mh-range-history ())
418 (defvar mh-range-completion-map (copy-keymap minibuffer-local-completion-map))
419 (define-key mh-range-completion-map " " 'self-insert-command)
420
421 (defun mh-range-completion-function (string predicate flag)
422 "Programmable completion of message ranges.
423 STRING is the user input that is to be completed. PREDICATE if non-nil is a
424 function used to filter the possible choices and FLAG determines whether the
425 completion is over."
426 (let* ((candidates mh-range-seq-names)
427 (last-char (and (not (equal string ""))
428 (aref string (1- (length string)))))
429 (last-word (cond ((null last-char) "")
430 ((memq last-char '(? ?- ?:)) "")
431 (t (car (last (split-string string "[ -:]+"))))))
432 (prefix (substring string 0 (- (length string) (length last-word)))))
433 (cond ((eq flag nil)
434 (let ((res (try-completion last-word candidates predicate)))
435 (cond ((null res) nil)
436 ((eq res t) t)
437 (t (concat prefix res)))))
438 ((eq flag t)
439 (all-completions last-word candidates predicate))
440 ((eq flag 'lambda)
441 (loop for x in candidates
442 when (equal x last-word) return t
443 finally return nil)))))
444
445 ;;;###mh-autoload
446 (defun mh-read-range (prompt &optional folder default
447 expand-flag ask-flag number-as-range-flag)
448 "Read a message range with PROMPT.
449
450 If FOLDER is non-nil then a range is read from that folder, otherwise use
451 `mh-current-folder'.
452
453 If DEFAULT is a string then use that as default range to return. If DEFAULT is
454 nil then ask user with default answer a range based on the sequences that seem
455 relevant. Finally if DEFAULT is t, try to avoid prompting the user. Unseen
456 messages, if present, are returned. If the folder has fewer than
457 `mh-large-folder' messages then \"all\" messages are returned. Finally as a
458 last resort prompt the user.
459
460 If EXPAND-FLAG is non-nil then a list of message numbers corresponding to the
461 input is returned. If this list is empty then an error is raised. If
462 EXPAND-FLAG is nil just return the input string. In this case we don't check
463 if the range is empty.
464
465 If ASK-FLAG is non-nil, then the user is always queried for a range of
466 messages. If ASK-FLAG is nil, then the function checks if the unseen sequence
467 is non-empty. If that is the case, `mh-unseen-seq', or the list of messages in
468 it depending on the value of EXPAND, is returned. Otherwise if the folder has
469 fewer than `mh-large-folder' messages then the list of messages corresponding
470 to \"all\" is returned. If neither of the above holds then as a last resort
471 the user is queried for a range of messages.
472
473 If NUMBER-AS-RANGE-FLAG is non-nil, then if a number, N is read as input, it
474 is interpreted as the range \"last:N\".
475
476 This function replaces the existing function `mh-read-msg-range'. Calls to:
477 (mh-read-msg-range folder flag)
478 should be replaced with:
479 (mh-read-range \"Suitable prompt\" folder t nil flag
480 mh-interpret-number-as-range-flag)"
481 (setq default (or default mh-last-seq-used
482 (car (mh-seq-containing-msg (mh-get-msg-num nil) t)))
483 prompt (format "%s range" prompt))
484 (let* ((folder (or folder mh-current-folder))
485 (default (cond ((or (eq default t) (stringp default)) default)
486 ((symbolp default) (symbol-name default))))
487 (guess (eq default t))
488 (counts (and guess (mh-folder-size folder)))
489 (unseen (and counts (> (cadr counts) 0)))
490 (large (and counts mh-large-folder (> (car counts) mh-large-folder)))
491 (str (cond ((and guess large
492 (setq default (format "last:%s" mh-large-folder)
493 prompt (format "%s (folder has %s messages)"
494 prompt (car counts)))
495 nil))
496 ((and guess (not large) (setq default "all") nil))
497 ((eq default nil) "")
498 (t (format "[%s] " default))))
499 (minibuffer-local-completion-map mh-range-completion-map)
500 (seq-list (if (eq folder mh-current-folder)
501 mh-seq-list
502 (mh-read-folder-sequences folder nil)))
503 (mh-range-seq-names
504 (append '(("first") ("last") ("all") ("prev") ("next"))
505 (mh-seq-names seq-list)))
506 (input (cond ((and (not ask-flag) unseen) (symbol-name mh-unseen-seq))
507 ((and (not ask-flag) (not large)) "all")
508 (t (completing-read (format "%s: %s" prompt str)
509 'mh-range-completion-function nil nil
510 nil 'mh-range-history default))))
511 msg-list)
512 (when (and number-as-range-flag
513 (string-match "^[ \t]*\\([0-9]+\\)[ \t]*$" input))
514 (setq input (concat "last:" (match-string 1 input))))
515 (cond ((not expand-flag) input)
516 ((assoc (intern input) seq-list)
517 (cdr (assoc (intern input) seq-list)))
518 ((setq msg-list (mh-translate-range folder input)) msg-list)
519 (t (error "No messages in range `%s'" input)))))
520
521 ;;;###mh-autoload
522 (defun mh-translate-range (folder expr)
523 "In FOLDER, translate the string EXPR to a list of messages numbers."
524 (save-excursion
525 (let ((strings (delete "" (split-string expr "[ \t\n]")))
526 (result ()))
527 (ignore-errors
528 (apply #'mh-exec-cmd-quiet nil "mhpath" folder strings)
529 (set-buffer mh-temp-buffer)
530 (goto-char (point-min))
531 (while (re-search-forward "/\\([0-9]*\\)$" nil t)
532 (push (car (read-from-string (match-string 1))) result))
533 (nreverse result)))))
534
535 (defun mh-seq-names (seq-list)
536 "Return an alist containing the names of the SEQ-LIST."
537 (mapcar (lambda (entry) (list (symbol-name (mh-seq-name entry))))
538 seq-list))
539
540 ;;;###mh-autoload
541 (defun mh-rename-seq (sequence new-name)
542 "Rename SEQUENCE to have NEW-NAME."
543 (interactive (list (mh-read-seq "Old" t)
544 (intern (read-string "New sequence name: "))))
545 (let ((old-seq (mh-find-seq sequence)))
546 (or old-seq
547 (error "Sequence %s does not exist" sequence))
548 ;; create new sequence first, since it might raise an error.
549 (mh-define-sequence new-name (mh-seq-msgs old-seq))
550 (mh-undefine-sequence sequence (mh-seq-msgs old-seq))
551 (rplaca old-seq new-name)))
552
553 ;;;###mh-autoload
554 (defun mh-notate-cur ()
555 "Mark the MH sequence cur.
556 In addition to notating the current message with `mh-note-cur' the function
557 uses `overlay-arrow-position' to put a marker in the fringe."
558 (let ((cur (car (mh-seq-to-msgs 'cur))))
559 (when (and cur (mh-goto-msg cur t t))
560 (beginning-of-line)
561 (when (looking-at mh-scan-good-msg-regexp)
562 (mh-notate nil mh-note-cur mh-cmd-note))
563 (setq mh-arrow-marker (set-marker mh-arrow-marker (point)))
564 (setq overlay-arrow-position mh-arrow-marker))))
565
566 ;;;###mh-autoload
567 (defun mh-add-to-sequence (seq msgs)
568 "The sequence SEQ is augmented with the messages in MSGS."
569 ;; Add to a SEQUENCE each message the list of MSGS.
570 (if (and (mh-valid-seq-p seq) (not (mh-folder-name-p seq)))
571 (if msgs
572 (apply 'mh-exec-cmd "mark" mh-current-folder "-add"
573 "-sequence" (symbol-name seq)
574 (mh-coalesce-msg-list msgs)))))
575
576 (defvar mh-thread-last-ancestor)
577
578 (defun mh-copy-seq-to-eob (seq)
579 "Copy SEQ to the end of the buffer."
580 ;; It is quite involved to write something which will work at any place in
581 ;; the buffer, so we will write something which works only at the end of
582 ;; the buffer. If we ever need to insert sequences in the middle of the
583 ;; buffer, this will need to be fixed.
584 (save-excursion
585 (let* ((msgs (mh-seq-to-msgs seq))
586 (coalesced-msgs (mh-coalesce-msg-list msgs)))
587 (goto-char (point-max))
588 (save-restriction
589 (narrow-to-region (point) (point))
590 (mh-regenerate-headers coalesced-msgs t)
591 (cond ((memq 'unthread mh-view-ops)
592 ;; Populate restricted scan-line map
593 (mh-remove-all-notation)
594 (mh-iterate-on-range msg (cons (point-min) (point-max))
595 (setf (gethash msg mh-thread-scan-line-map)
596 (mh-thread-parse-scan-line)))
597 ;; Remove scan lines and read results from pre-computed tree
598 (delete-region (point-min) (point-max))
599 (mh-thread-print-scan-lines
600 (mh-thread-generate mh-current-folder ()))
601 (mh-notate-user-sequences))
602 (mh-index-data
603 (mh-index-insert-folder-headers)))))))
604
605 ;;;###mh-autoload
606 (defmacro mh-iterate-on-messages-in-region (var begin end &rest body)
607 "Iterate over region.
608 VAR is bound to the message on the current line as we loop starting from BEGIN
609 till END. In each step BODY is executed.
610
611 If VAR is nil then the loop is executed without any binding."
612 (unless (symbolp var)
613 (error "Can not bind the non-symbol %s" var))
614 (let ((binding-needed-flag var))
615 `(save-excursion
616 (goto-char ,begin)
617 (beginning-of-line)
618 (while (and (<= (point) ,end) (not (eobp)))
619 (when (looking-at mh-scan-valid-regexp)
620 (let ,(if binding-needed-flag `((,var (mh-get-msg-num t))) ())
621 ,@body))
622 (forward-line 1)))))
623
624 (put 'mh-iterate-on-messages-in-region 'lisp-indent-hook 'defun)
625
626 ;;;###mh-autoload
627 (defmacro mh-iterate-on-range (var range &rest body)
628 "Iterate an operation over a region or sequence.
629
630 VAR is bound to each message in turn in a loop over RANGE, which can be a
631 message number, a list of message numbers, a sequence, a region in a cons
632 cell, or a MH range (something like last:20) in a string. In each iteration,
633 BODY is executed.
634
635 The parameter RANGE is usually created with `mh-interactive-range'
636 in order to provide a uniform interface to MH-E functions."
637 (unless (symbolp var)
638 (error "Can not bind the non-symbol %s" var))
639 (let ((binding-needed-flag var)
640 (msgs (make-symbol "msgs"))
641 (seq-hash-table (make-symbol "seq-hash-table")))
642 `(cond ((numberp ,range)
643 (when (mh-goto-msg ,range t t)
644 (let ,(if binding-needed-flag `((,var ,range)) ())
645 ,@body)))
646 ((and (consp ,range)
647 (numberp (car ,range)) (numberp (cdr ,range)))
648 (mh-iterate-on-messages-in-region ,var
649 (car ,range) (cdr ,range)
650 ,@body))
651 (t (let ((,msgs (cond ((and ,range (symbolp ,range))
652 (mh-seq-to-msgs ,range))
653 ((stringp ,range)
654 (mh-translate-range mh-current-folder
655 ,range))
656 (t ,range)))
657 (,seq-hash-table (make-hash-table)))
658 (dolist (msg ,msgs)
659 (setf (gethash msg ,seq-hash-table) t))
660 (mh-iterate-on-messages-in-region v (point-min) (point-max)
661 (when (gethash v ,seq-hash-table)
662 (let ,(if binding-needed-flag `((,var v)) ())
663 ,@body))))))))
664
665 (put 'mh-iterate-on-range 'lisp-indent-hook 'defun)
666
667 ;;;###mh-autoload
668 (defun mh-range-to-msg-list (range)
669 "Return a list of messages for RANGE.
670
671 Check the documentation of `mh-interactive-range' to see how RANGE is read in
672 interactive use."
673 (let (msg-list)
674 (mh-iterate-on-range msg range
675 (push msg msg-list))
676 (nreverse msg-list)))
677
678 ;;;###mh-autoload
679 (defun mh-interactive-range (range-prompt &optional default)
680 "Return interactive specification for message, sequence, range or region.
681 By convention, the name of this argument is RANGE.
682
683 If variable `transient-mark-mode' is non-nil and the mark is active, then this
684 function returns a cons-cell of the region.
685
686 If optional prefix argument is provided, then prompt for message range with
687 RANGE-PROMPT. A list of messages in that range is returned.
688
689 If a MH range is given, say something like last:20, then a list containing
690 the messages in that range is returned.
691
692 If DEFAULT non-nil then it is returned.
693
694 Otherwise, the message number at point is returned.
695
696 This function is usually used with `mh-iterate-on-range' in order to provide
697 a uniform interface to MH-E functions."
698 (cond ((mh-mark-active-p t) (cons (region-beginning) (region-end)))
699 (current-prefix-arg (mh-read-range range-prompt nil nil t t))
700 (default default)
701 (t (mh-get-msg-num t))))
702
703 \f
704
705 ;;; Commands to handle new 'subject sequence.
706 ;;; Or "Poor man's threading" by psg.
707
708 ;;; XXX: The function mh-subject-to-sequence-unthreaded uses the magic number
709 ;;; 41 for the max size of the subject part. Avoiding this would be desirable.
710 (defun mh-subject-to-sequence (all)
711 "Put all following messages with same subject in sequence 'subject.
712 If arg ALL is t, move to beginning of folder buffer to collect all messages.
713 If arg ALL is nil, collect only messages fron current one on forward.
714
715 Return number of messages put in the sequence:
716
717 nil -> there was no subject line.
718 0 -> there were no later messages with the same subject (sequence not made)
719 >1 -> the total number of messages including current one."
720 (if (memq 'unthread mh-view-ops)
721 (mh-subject-to-sequence-threaded all)
722 (mh-subject-to-sequence-unthreaded all)))
723
724 (defun mh-subject-to-sequence-unthreaded (all)
725 "Put all following messages with same subject in sequence 'subject.
726 This function only works with an unthreaded folder. If arg ALL is t, move to
727 beginning of folder buffer to collect all messages. If arg ALL is nil, collect
728 only messages fron current one on forward.
729
730 Return number of messages put in the sequence:
731
732 nil -> there was no subject line.
733 0 -> there were no later messages with the same subject (sequence not made)
734 >1 -> the total number of messages including current one."
735 (if (not (eq major-mode 'mh-folder-mode))
736 (error "Not in a folder buffer"))
737 (save-excursion
738 (beginning-of-line)
739 (if (or (not (looking-at mh-scan-subject-regexp))
740 (not (match-string 3))
741 (string-equal "" (match-string 3)))
742 (progn (message "No subject line")
743 nil)
744 (let ((subject (match-string-no-properties 3))
745 (list))
746 (if (> (length subject) 41)
747 (setq subject (substring subject 0 41)))
748 (save-excursion
749 (if all
750 (goto-char (point-min)))
751 (while (re-search-forward mh-scan-subject-regexp nil t)
752 (let ((this-subject (match-string-no-properties 3)))
753 (if (> (length this-subject) 41)
754 (setq this-subject (substring this-subject 0 41)))
755 (if (string-equal this-subject subject)
756 (setq list (cons (mh-get-msg-num t) list))))))
757 (cond
758 (list
759 ;; If we created a new sequence, add the initial message to it too.
760 (if (not (member (mh-get-msg-num t) list))
761 (setq list (cons (mh-get-msg-num t) list)))
762 (if (assoc 'subject mh-seq-list) (mh-delete-seq 'subject))
763 ;; sort the result into a sequence
764 (let ((sorted-list (sort (copy-sequence list) 'mh-lessp)))
765 (while sorted-list
766 (mh-add-msgs-to-seq (car sorted-list) 'subject nil)
767 (setq sorted-list (cdr sorted-list)))
768 (safe-length list)))
769 (t
770 0))))))
771
772 (defun mh-subject-to-sequence-threaded (all)
773 "Put all messages with the same subject in the 'subject sequence.
774 This function works when the folder is threaded. In this situation the subject
775 could get truncated and so the normal matching doesn't work.
776
777 The parameter ALL is non-nil then all the messages in the buffer are
778 considered, otherwise only the messages after the current one are taken into
779 account."
780 (let* ((cur (mh-get-msg-num nil))
781 (subject (mh-thread-find-msg-subject cur))
782 region msgs)
783 (if (null subject)
784 (and (message "No subject line") nil)
785 (setq region (cons (if all (point-min) (point)) (point-max)))
786 (mh-iterate-on-range msg region
787 (when (eq (mh-thread-find-msg-subject msg) subject)
788 (push msg msgs)))
789 (setq msgs (sort msgs #'mh-lessp))
790 (if (null msgs)
791 0
792 (when (assoc 'subject mh-seq-list)
793 (mh-delete-seq 'subject))
794 (mh-add-msgs-to-seq msgs 'subject)
795 (length msgs)))))
796
797 (defun mh-thread-find-msg-subject (msg)
798 "Find canonicalized subject of MSG.
799 This function can only be used the folder is threaded."
800 (ignore-errors
801 (mh-message-subject
802 (mh-container-message (gethash (gethash msg mh-thread-index-id-map)
803 mh-thread-id-table)))))
804
805 (defun mh-edit-pick-expr (default)
806 "With prefix arg edit a pick expression.
807 If no prefix arg is given, then return DEFAULT."
808 (let ((default-string (loop for x in default concat (format " %s" x))))
809 (if (or current-prefix-arg (equal default-string ""))
810 (mh-pick-args-list (read-string "Pick expression: "
811 default-string))
812 default)))
813
814 (defun mh-pick-args-list (s)
815 "Form list by grouping elements in string S suitable for pick arguments.
816 For example, the string \"-subject a b c -from Joe User <user@domain.com>\"
817 is converted to (\"-subject\" \"a b c\" \"-from\"
818 \"Joe User <user@domain.com>\""
819 (let ((full-list (split-string s))
820 current-arg collection arg-list)
821 (while full-list
822 (setq current-arg (car full-list))
823 (if (null (string-match "^-" current-arg))
824 (setq collection
825 (if (null collection)
826 current-arg
827 (format "%s %s" collection current-arg)))
828 (when collection
829 (setq arg-list (append arg-list (list collection)))
830 (setq collection nil))
831 (setq arg-list (append arg-list (list current-arg))))
832 (setq full-list (cdr full-list)))
833 (when collection
834 (setq arg-list (append arg-list (list collection))))
835 arg-list))
836
837 ;;;###mh-autoload
838 (defun mh-narrow-to-subject (&optional pick-expr)
839 "Limit to messages with same subject.
840 With a prefix argument, edit PICK-EXPR.
841
842 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
843 (interactive
844 (list (mh-edit-pick-expr (mh-current-message-header-field 'subject))))
845 (mh-narrow-to-header-field 'subject pick-expr))
846
847 ;;;###mh-autoload
848 (defun mh-narrow-to-from (&optional pick-expr)
849 "Limit to messages with the same `From:' field.
850 With a prefix argument, edit PICK-EXPR.
851
852 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
853 (interactive
854 (list (mh-edit-pick-expr (mh-current-message-header-field 'from))))
855 (mh-narrow-to-header-field 'from pick-expr))
856
857 ;;;###mh-autoload
858 (defun mh-narrow-to-cc (&optional pick-expr)
859 "Limit to messages with the same `Cc:' field.
860 With a prefix argument, edit PICK-EXPR.
861
862 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
863 (interactive
864 (list (mh-edit-pick-expr (mh-current-message-header-field 'cc))))
865 (mh-narrow-to-header-field 'cc pick-expr))
866
867 ;;;###mh-autoload
868 (defun mh-narrow-to-to (&optional pick-expr)
869 "Limit to messages with the same `To:' field.
870 With a prefix argument, edit PICK-EXPR.
871
872 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
873 (interactive
874 (list (mh-edit-pick-expr (mh-current-message-header-field 'to))))
875 (mh-narrow-to-header-field 'to pick-expr))
876
877 (defun mh-narrow-to-header-field (header-field pick-expr)
878 "Limit to messages whose HEADER-FIELD match PICK-EXPR.
879 The MH command pick is used to do the match."
880 (let ((folder mh-current-folder)
881 (original (mh-coalesce-msg-list
882 (mh-range-to-msg-list (cons (point-min) (point-max)))))
883 (msg-list ()))
884 (with-temp-buffer
885 (apply #'mh-exec-cmd-output "pick" nil folder
886 (append original (list "-list") pick-expr))
887 (goto-char (point-min))
888 (while (not (eobp))
889 (let ((num (read-from-string
890 (buffer-substring (point) (line-end-position)))))
891 (when (numberp (car num)) (push (car num) msg-list))
892 (forward-line))))
893 (if (null msg-list)
894 (message "No matches")
895 (when (assoc 'header mh-seq-list) (mh-delete-seq 'header))
896 (mh-add-msgs-to-seq msg-list 'header)
897 (mh-narrow-to-seq 'header))))
898
899 (defun mh-current-message-header-field (header-field)
900 "Return a pick regexp to match HEADER-FIELD of the message at point."
901 (let ((num (mh-get-msg-num nil)))
902 (when num
903 (let ((folder mh-current-folder))
904 (with-temp-buffer
905 (insert-file-contents-literally (mh-msg-filename num folder))
906 (goto-char (point-min))
907 (when (search-forward "\n\n" nil t)
908 (narrow-to-region (point-min) (point)))
909 (let* ((field (or (message-fetch-field (format "%s" header-field))
910 ""))
911 (field-option (format "-%s" header-field))
912 (patterns (loop for x in (split-string field "[ ]*,[ ]*")
913 unless (equal x "")
914 collect (if (string-match "<\\(.*@.*\\)>" x)
915 (match-string 1 x)
916 x))))
917 (when patterns
918 (loop with accum = `(,field-option ,(car patterns))
919 for e in (cdr patterns)
920 do (setq accum `(,field-option ,e "-or" ,@accum))
921 finally return accum))))))))
922
923 ;;;###mh-autoload
924 (defun mh-narrow-to-range (range)
925 "Limit to RANGE.
926
927 Check the documentation of `mh-interactive-range' to see how RANGE is read in
928 interactive use.
929
930 Use \\<mh-folder-mode-map>\\[mh-widen] to undo this command."
931 (interactive (list (mh-interactive-range "Narrow to")))
932 (when (assoc 'range mh-seq-list) (mh-delete-seq 'range))
933 (mh-add-msgs-to-seq (mh-range-to-msg-list range) 'range)
934 (mh-narrow-to-seq 'range))
935
936
937 ;;;###mh-autoload
938 (defun mh-delete-subject ()
939 "Mark all following messages with same subject to be deleted.
940 This puts the messages in a sequence named subject. You can undo the last
941 deletion marks using `mh-undo' with a prefix argument and then specifying the
942 subject sequence."
943 (interactive)
944 (let ((count (mh-subject-to-sequence nil)))
945 (cond
946 ((not count) ; No subject line, delete msg anyway
947 (mh-delete-msg (mh-get-msg-num t)))
948 ((= 0 count) ; No other msgs, delete msg anyway.
949 (message "No other messages with same Subject following this one")
950 (mh-delete-msg (mh-get-msg-num t)))
951 (t ; We have a subject sequence.
952 (message "Marked %d messages for deletion" count)
953 (mh-delete-msg 'subject)))))
954
955 ;;;###mh-autoload
956 (defun mh-delete-subject-or-thread ()
957 "Mark messages for deletion intelligently.
958 If the folder is threaded then `mh-thread-delete' is used to mark the current
959 message and all its descendants for deletion. Otherwise `mh-delete-subject' is
960 used to mark the current message and all messages following it with the same
961 subject for deletion."
962 (interactive)
963 (if (memq 'unthread mh-view-ops)
964 (mh-thread-delete)
965 (mh-delete-subject)))
966
967 ;;; Message threading:
968
969 (defmacro mh-thread-initialize-hash (var test)
970 "Initialize the hash table in VAR.
971 TEST is the test to use when creating a new hash table."
972 (unless (symbolp var) (error "Expected a symbol: %s" var))
973 `(if ,var (clrhash ,var) (setq ,var (make-hash-table :test ,test))))
974
975 (defun mh-thread-initialize ()
976 "Make new hash tables, or clear them if already present."
977 (mh-thread-initialize-hash mh-thread-id-hash #'equal)
978 (mh-thread-initialize-hash mh-thread-subject-hash #'equal)
979 (mh-thread-initialize-hash mh-thread-id-table #'eq)
980 (mh-thread-initialize-hash mh-thread-id-index-map #'eq)
981 (mh-thread-initialize-hash mh-thread-index-id-map #'eql)
982 (mh-thread-initialize-hash mh-thread-scan-line-map #'eql)
983 (mh-thread-initialize-hash mh-thread-subject-container-hash #'eq)
984 (mh-thread-initialize-hash mh-thread-duplicates #'eq)
985 (setq mh-thread-history ()))
986
987 (defsubst mh-thread-id-container (id)
988 "Given ID, return the corresponding container in `mh-thread-id-table'.
989 If no container exists then a suitable container is created and the id-table
990 is updated."
991 (when (not id)
992 (error "1"))
993 (or (gethash id mh-thread-id-table)
994 (setf (gethash id mh-thread-id-table)
995 (let ((message (mh-thread-make-message :id id)))
996 (mh-thread-make-container :message message)))))
997
998 (defsubst mh-thread-remove-parent-link (child)
999 "Remove parent link of CHILD if it exists."
1000 (let* ((child-container (if (mh-thread-container-p child)
1001 child (mh-thread-id-container child)))
1002 (parent-container (mh-container-parent child-container)))
1003 (when parent-container
1004 (setf (mh-container-children parent-container)
1005 (loop for elem in (mh-container-children parent-container)
1006 unless (eq child-container elem) collect elem))
1007 (setf (mh-container-parent child-container) nil))))
1008
1009 (defsubst mh-thread-add-link (parent child &optional at-end-p)
1010 "Add links so that PARENT becomes a parent of CHILD.
1011 Doesn't make any changes if CHILD is already an ancestor of PARENT. If
1012 optional argument AT-END-P is non-nil, the CHILD is added to the end of the
1013 children list of PARENT."
1014 (let ((parent-container (cond ((null parent) nil)
1015 ((mh-thread-container-p parent) parent)
1016 (t (mh-thread-id-container parent))))
1017 (child-container (if (mh-thread-container-p child)
1018 child (mh-thread-id-container child))))
1019 (when (and parent-container
1020 (not (mh-thread-ancestor-p child-container parent-container))
1021 (not (mh-thread-ancestor-p parent-container child-container)))
1022 (mh-thread-remove-parent-link child-container)
1023 (cond ((not at-end-p)
1024 (push child-container (mh-container-children parent-container)))
1025 ((null (mh-container-children parent-container))
1026 (push child-container (mh-container-children parent-container)))
1027 (t (let ((last-child (mh-container-children parent-container)))
1028 (while (cdr last-child)
1029 (setq last-child (cdr last-child)))
1030 (setcdr last-child (cons child-container nil)))))
1031 (setf (mh-container-parent child-container) parent-container))
1032 (unless parent-container
1033 (mh-thread-remove-parent-link child-container))))
1034
1035 (defun mh-thread-ancestor-p (ancestor successor)
1036 "Return t if ANCESTOR is really an ancestor of SUCCESSOR and nil otherwise.
1037 In the limit, the function returns t if ANCESTOR and SUCCESSOR are the same
1038 containers."
1039 (block nil
1040 (while successor
1041 (when (eq ancestor successor) (return t))
1042 (setq successor (mh-container-parent successor)))
1043 nil))
1044
1045 (defsubst mh-thread-get-message-container (message)
1046 "Return container which has MESSAGE in it.
1047 If there is no container present then a new container is allocated."
1048 (let* ((id (mh-message-id message))
1049 (container (gethash id mh-thread-id-table)))
1050 (cond (container (setf (mh-container-message container) message)
1051 container)
1052 (t (setf (gethash id mh-thread-id-table)
1053 (mh-thread-make-container :message message))))))
1054
1055 (defsubst mh-thread-get-message (id subject-re-p subject refs)
1056 "Return appropriate message.
1057 Otherwise update message already present to have the proper ID, SUBJECT-RE-P,
1058 SUBJECT and REFS fields."
1059 (let* ((container (gethash id mh-thread-id-table))
1060 (message (if container (mh-container-message container) nil)))
1061 (cond (message
1062 (setf (mh-message-subject-re-p message) subject-re-p)
1063 (setf (mh-message-subject message) subject)
1064 (setf (mh-message-id message) id)
1065 (setf (mh-message-references message) refs)
1066 message)
1067 (container
1068 (setf (mh-container-message container)
1069 (mh-thread-make-message :id id :references refs
1070 :subject subject
1071 :subject-re-p subject-re-p)))
1072 (t (let ((message (mh-thread-make-message :id id :references refs
1073 :subject-re-p subject-re-p
1074 :subject subject)))
1075 (prog1 message
1076 (mh-thread-get-message-container message)))))))
1077
1078 (defsubst mh-thread-canonicalize-id (id)
1079 "Produce canonical string representation for ID.
1080 This allows cheap string comparison with EQ."
1081 (or (and (equal id "") (copy-sequence ""))
1082 (gethash id mh-thread-id-hash)
1083 (setf (gethash id mh-thread-id-hash) id)))
1084
1085 (defsubst mh-thread-prune-subject (subject)
1086 "Prune leading Re:'s, Fwd:'s etc. and trailing (fwd)'s from SUBJECT.
1087 If the result after pruning is not the empty string then it is canonicalized
1088 so that subjects can be tested for equality with eq. This is done so that all
1089 the messages without a subject are not put into a single thread."
1090 (let ((case-fold-search t)
1091 (subject-pruned-flag nil))
1092 ;; Prune subject leader
1093 (while (or (string-match "^[ \t]*\\(re\\|fwd?\\)\\(\\[[0-9]*\\]\\)?:[ \t]*"
1094 subject)
1095 (string-match "^[ \t]*\\[[^\\]][ \t]*" subject))
1096 (setq subject-pruned-flag t)
1097 (setq subject (substring subject (match-end 0))))
1098 ;; Prune subject trailer
1099 (while (or (string-match "(fwd)$" subject)
1100 (string-match "[ \t]+$" subject))
1101 (setq subject-pruned-flag t)
1102 (setq subject (substring subject 0 (match-beginning 0))))
1103 ;; Canonicalize subject only if it is non-empty
1104 (cond ((equal subject "") (values subject subject-pruned-flag))
1105 (t (values
1106 (or (gethash subject mh-thread-subject-hash)
1107 (setf (gethash subject mh-thread-subject-hash) subject))
1108 subject-pruned-flag)))))
1109
1110 (defun mh-thread-container-subject (container)
1111 "Return the subject of CONTAINER.
1112 If CONTAINER is empty return the subject info of one of its children."
1113 (cond ((and (mh-container-message container)
1114 (mh-message-id (mh-container-message container)))
1115 (mh-message-subject (mh-container-message container)))
1116 (t (block nil
1117 (dolist (kid (mh-container-children container))
1118 (when (and (mh-container-message kid)
1119 (mh-message-id (mh-container-message kid)))
1120 (let ((kid-message (mh-container-message kid)))
1121 (return (mh-message-subject kid-message)))))
1122 (error "This can't happen!")))))
1123
1124 (defun mh-thread-rewind-pruning ()
1125 "Restore the thread tree to its state before pruning."
1126 (while mh-thread-history
1127 (let ((action (pop mh-thread-history)))
1128 (cond ((eq (car action) 'DROP)
1129 (mh-thread-remove-parent-link (cadr action))
1130 (mh-thread-add-link (caddr action) (cadr action)))
1131 ((eq (car action) 'PROMOTE)
1132 (let ((node (cadr action))
1133 (parent (caddr action))
1134 (children (cdddr action)))
1135 (dolist (child children)
1136 (mh-thread-remove-parent-link child)
1137 (mh-thread-add-link node child))
1138 (mh-thread-add-link parent node)))
1139 ((eq (car action) 'SUBJECT)
1140 (let ((node (cadr action)))
1141 (mh-thread-remove-parent-link node)
1142 (setf (mh-container-real-child-p node) t)))))))
1143
1144 (defun mh-thread-prune-containers (roots)
1145 "Prune empty containers in the containers ROOTS."
1146 (let ((dfs-ordered-nodes ())
1147 (work-list roots))
1148 (while work-list
1149 (let ((node (pop work-list)))
1150 (dolist (child (mh-container-children node))
1151 (push child work-list))
1152 (push node dfs-ordered-nodes)))
1153 (while dfs-ordered-nodes
1154 (let ((node (pop dfs-ordered-nodes)))
1155 (cond ((gethash (mh-message-id (mh-container-message node))
1156 mh-thread-id-index-map)
1157 ;; Keep it
1158 (setf (mh-container-children node)
1159 (mh-thread-sort-containers (mh-container-children node))))
1160 ((and (mh-container-children node)
1161 (or (null (cdr (mh-container-children node)))
1162 (mh-container-parent node)))
1163 ;; Promote kids
1164 (let ((children ()))
1165 (dolist (kid (mh-container-children node))
1166 (mh-thread-remove-parent-link kid)
1167 (mh-thread-add-link (mh-container-parent node) kid)
1168 (push kid children))
1169 (push `(PROMOTE ,node ,(mh-container-parent node) ,@children)
1170 mh-thread-history)
1171 (mh-thread-remove-parent-link node)))
1172 ((mh-container-children node)
1173 ;; Promote the first orphan to parent and add the other kids as
1174 ;; his children
1175 (setf (mh-container-children node)
1176 (mh-thread-sort-containers (mh-container-children node)))
1177 (let ((new-parent (car (mh-container-children node)))
1178 (other-kids (cdr (mh-container-children node))))
1179 (mh-thread-remove-parent-link new-parent)
1180 (dolist (kid other-kids)
1181 (mh-thread-remove-parent-link kid)
1182 (setf (mh-container-real-child-p kid) nil)
1183 (mh-thread-add-link new-parent kid t))
1184 (push `(PROMOTE ,node ,(mh-container-parent node)
1185 ,new-parent ,@other-kids)
1186 mh-thread-history)
1187 (mh-thread-remove-parent-link node)))
1188 (t
1189 ;; Drop it
1190 (push `(DROP ,node ,(mh-container-parent node))
1191 mh-thread-history)
1192 (mh-thread-remove-parent-link node)))))
1193 (let ((results ()))
1194 (maphash #'(lambda (k v)
1195 (declare (ignore k))
1196 (when (and (null (mh-container-parent v))
1197 (gethash (mh-message-id (mh-container-message v))
1198 mh-thread-id-index-map))
1199 (push v results)))
1200 mh-thread-id-table)
1201 (mh-thread-sort-containers results))))
1202
1203 (defun mh-thread-sort-containers (containers)
1204 "Sort a list of message CONTAINERS to be in ascending order wrt index."
1205 (sort containers
1206 #'(lambda (x y)
1207 (when (and (mh-container-message x) (mh-container-message y))
1208 (let* ((id-x (mh-message-id (mh-container-message x)))
1209 (id-y (mh-message-id (mh-container-message y)))
1210 (index-x (gethash id-x mh-thread-id-index-map))
1211 (index-y (gethash id-y mh-thread-id-index-map)))
1212 (and (integerp index-x) (integerp index-y)
1213 (< index-x index-y)))))))
1214
1215 (defsubst mh-thread-group-by-subject (roots)
1216 "Group the set of message containers, ROOTS based on subject.
1217 Bug: Check for and make sure that something without Re: is made the parent in
1218 preference to something that has it."
1219 (clrhash mh-thread-subject-container-hash)
1220 (let ((results ()))
1221 (dolist (root roots)
1222 (let* ((subject (mh-thread-container-subject root))
1223 (parent (gethash subject mh-thread-subject-container-hash)))
1224 (cond (parent (mh-thread-remove-parent-link root)
1225 (mh-thread-add-link parent root t)
1226 (setf (mh-container-real-child-p root) nil)
1227 (push `(SUBJECT ,root) mh-thread-history))
1228 (t
1229 (setf (gethash subject mh-thread-subject-container-hash) root)
1230 (push root results)))))
1231 (nreverse results)))
1232
1233 (defun mh-thread-process-in-reply-to (reply-to-header)
1234 "Extract message id's from REPLY-TO-HEADER.
1235 Ideally this should have some regexp which will try to guess if a string
1236 between < and > is a message id and not an email address. For now it will
1237 take the last string inside angles."
1238 (let ((end (mh-search-from-end ?> reply-to-header)))
1239 (when (numberp end)
1240 (let ((begin (mh-search-from-end ?< (substring reply-to-header 0 end))))
1241 (when (numberp begin)
1242 (list (substring reply-to-header begin (1+ end))))))))
1243
1244 (defun mh-thread-set-tables (folder)
1245 "Use the tables of FOLDER in current buffer."
1246 (flet ((mh-get-table (symbol)
1247 (save-excursion
1248 (set-buffer folder)
1249 (symbol-value symbol))))
1250 (setq mh-thread-id-hash (mh-get-table 'mh-thread-id-hash))
1251 (setq mh-thread-subject-hash (mh-get-table 'mh-thread-subject-hash))
1252 (setq mh-thread-id-table (mh-get-table 'mh-thread-id-table))
1253 (setq mh-thread-id-index-map (mh-get-table 'mh-thread-id-index-map))
1254 (setq mh-thread-index-id-map (mh-get-table 'mh-thread-index-id-map))
1255 (setq mh-thread-scan-line-map (mh-get-table 'mh-thread-scan-line-map))
1256 (setq mh-thread-subject-container-hash
1257 (mh-get-table 'mh-thread-subject-container-hash))
1258 (setq mh-thread-duplicates (mh-get-table 'mh-thread-duplicates))
1259 (setq mh-thread-history (mh-get-table 'mh-thread-history))))
1260
1261 (defsubst mh-thread-update-id-index-maps (id index)
1262 "Message with id, ID is the message in INDEX.
1263 The function also checks for duplicate messages (that is multiple messages
1264 with the same ID). These messages are put in the `mh-thread-duplicates' hash
1265 table."
1266 (let ((old-index (gethash id mh-thread-id-index-map)))
1267 (when old-index (push old-index (gethash id mh-thread-duplicates)))
1268 (setf (gethash id mh-thread-id-index-map) index)
1269 (setf (gethash index mh-thread-index-id-map) id)))
1270
1271 \f
1272
1273 ;;; Generate Threads...
1274
1275 (defvar mh-message-id-regexp "^<.*@.*>$"
1276 "Regexp to recognize whether a string is a message identifier.")
1277
1278 (defun mh-thread-generate (folder msg-list)
1279 "Scan FOLDER to get info for threading.
1280 Only information about messages in MSG-LIST are added to the tree."
1281 (with-temp-buffer
1282 (mh-thread-set-tables folder)
1283 (when msg-list
1284 (apply
1285 #'call-process (expand-file-name mh-scan-prog mh-progs) nil '(t nil) nil
1286 "-width" "10000" "-format"
1287 "%(msg)\n%{message-id}\n%{references}\n%{in-reply-to}\n%{subject}\n"
1288 folder (mapcar #'(lambda (x) (format "%s" x)) msg-list)))
1289 (goto-char (point-min))
1290 (let ((roots ())
1291 (case-fold-search t))
1292 (block nil
1293 (while (not (eobp))
1294 (block process-message
1295 (let* ((index-line
1296 (prog1 (buffer-substring (point) (line-end-position))
1297 (forward-line)))
1298 (index (car (read-from-string index-line)))
1299 (id (prog1 (buffer-substring (point) (line-end-position))
1300 (forward-line)))
1301 (refs (prog1 (buffer-substring (point) (line-end-position))
1302 (forward-line)))
1303 (in-reply-to (prog1 (buffer-substring (point)
1304 (line-end-position))
1305 (forward-line)))
1306 (subject (prog1
1307 (buffer-substring (point) (line-end-position))
1308 (forward-line)))
1309 (subject-re-p nil))
1310 (unless (gethash index mh-thread-scan-line-map)
1311 (return-from process-message))
1312 (unless (integerp index) (return)) ;Error message here
1313 (multiple-value-setq (subject subject-re-p)
1314 (mh-thread-prune-subject subject))
1315 (setq in-reply-to (mh-thread-process-in-reply-to in-reply-to))
1316 (setq refs (loop for x in (append (split-string refs) in-reply-to)
1317 when (string-match mh-message-id-regexp x)
1318 collect x))
1319 (setq id (mh-thread-canonicalize-id id))
1320 (mh-thread-update-id-index-maps id index)
1321 (setq refs (mapcar #'mh-thread-canonicalize-id refs))
1322 (mh-thread-get-message id subject-re-p subject refs)
1323 (do ((ancestors refs (cdr ancestors)))
1324 ((null (cdr ancestors))
1325 (when (car ancestors)
1326 (mh-thread-remove-parent-link id)
1327 (mh-thread-add-link (car ancestors) id)))
1328 (mh-thread-add-link (car ancestors) (cadr ancestors)))))))
1329 (maphash #'(lambda (k v)
1330 (declare (ignore k))
1331 (when (null (mh-container-parent v))
1332 (push v roots)))
1333 mh-thread-id-table)
1334 (setq roots (mh-thread-prune-containers roots))
1335 (prog1 (setq roots (mh-thread-group-by-subject roots))
1336 (let ((history mh-thread-history))
1337 (set-buffer folder)
1338 (setq mh-thread-history history))))))
1339
1340 ;;;###mh-autoload
1341 (defun mh-thread-inc (folder start-point)
1342 "Update thread tree for FOLDER.
1343 All messages after START-POINT are added to the thread tree."
1344 (mh-thread-rewind-pruning)
1345 (mh-remove-all-notation)
1346 (goto-char start-point)
1347 (let ((msg-list ()))
1348 (while (not (eobp))
1349 (let ((index (mh-get-msg-num nil)))
1350 (when (numberp index)
1351 (push index msg-list)
1352 (setf (gethash index mh-thread-scan-line-map)
1353 (mh-thread-parse-scan-line)))
1354 (forward-line)))
1355 (let ((thread-tree (mh-thread-generate folder msg-list))
1356 (buffer-read-only nil)
1357 (old-buffer-modified-flag (buffer-modified-p)))
1358 (delete-region (point-min) (point-max))
1359 (mh-thread-print-scan-lines thread-tree)
1360 (mh-notate-user-sequences)
1361 (mh-notate-deleted-and-refiled)
1362 (mh-notate-cur)
1363 (set-buffer-modified-p old-buffer-modified-flag))))
1364
1365 (defun mh-thread-generate-scan-lines (tree level)
1366 "Generate scan lines.
1367 TREE is the hierarchical tree of messages, SCAN-LINE-MAP maps message indices
1368 to the corresponding scan lines and LEVEL used to determine indentation of
1369 the message."
1370 (cond ((null tree) nil)
1371 ((mh-thread-container-p tree)
1372 (let* ((message (mh-container-message tree))
1373 (id (mh-message-id message))
1374 (index (gethash id mh-thread-id-index-map))
1375 (duplicates (gethash id mh-thread-duplicates))
1376 (new-level (+ level 2))
1377 (dupl-flag t)
1378 (force-angle-flag nil)
1379 (increment-level-flag nil))
1380 (dolist (scan-line (mapcar (lambda (x)
1381 (gethash x mh-thread-scan-line-map))
1382 (reverse (cons index duplicates))))
1383 (when scan-line
1384 (when (and dupl-flag (equal level 0)
1385 (mh-thread-ancestor-p mh-thread-last-ancestor tree))
1386 (setq level (+ level 2)
1387 new-level (+ new-level 2)
1388 force-angle-flag t))
1389 (when (equal level 0)
1390 (setq mh-thread-last-ancestor tree)
1391 (while (mh-container-parent mh-thread-last-ancestor)
1392 (setq mh-thread-last-ancestor
1393 (mh-container-parent mh-thread-last-ancestor))))
1394 (let* ((lev (if dupl-flag level new-level))
1395 (square-flag (or (and (mh-container-real-child-p tree)
1396 (not force-angle-flag)
1397 dupl-flag)
1398 (equal lev 0))))
1399 (insert (car scan-line)
1400 (format (format "%%%ss" lev) "")
1401 (if square-flag "[" "<")
1402 (cadr scan-line)
1403 (if square-flag "]" ">")
1404 (truncate-string-to-width
1405 (caddr scan-line) (- mh-thread-body-width lev))
1406 "\n"))
1407 (setq increment-level-flag t)
1408 (setq dupl-flag nil)))
1409 (unless increment-level-flag (setq new-level level))
1410 (dolist (child (mh-container-children tree))
1411 (mh-thread-generate-scan-lines child new-level))))
1412 (t (let ((nlevel (+ level 2)))
1413 (dolist (ch tree)
1414 (mh-thread-generate-scan-lines ch nlevel))))))
1415
1416 ;; Another and may be better approach would be to generate all the info from
1417 ;; the scan which generates the threading info. For now this will have to do.
1418 (defun mh-thread-parse-scan-line (&optional string)
1419 "Parse a scan line.
1420 If optional argument STRING is given then that is assumed to be the scan line.
1421 Otherwise uses the line at point as the scan line to parse."
1422 (let* ((string (or string
1423 (buffer-substring-no-properties (line-beginning-position)
1424 (line-end-position))))
1425 (address-start (+ mh-cmd-note mh-scan-field-from-start-offset))
1426 (body-start (+ mh-cmd-note mh-scan-field-from-end-offset))
1427 (first-string (substring string 0 address-start)))
1428 (list first-string
1429 (substring string address-start (- body-start 2))
1430 (substring string body-start)
1431 string)))
1432
1433 ;;;###mh-autoload
1434 (defun mh-thread-update-scan-line-map (msg notation offset)
1435 "In threaded view update `mh-thread-scan-line-map'.
1436 MSG is the message being notated with NOTATION at OFFSET."
1437 (let* ((msg (or msg (mh-get-msg-num nil)))
1438 (cur-scan-line (and mh-thread-scan-line-map
1439 (gethash msg mh-thread-scan-line-map)))
1440 (old-scan-lines (loop for map in mh-thread-scan-line-map-stack
1441 collect (and map (gethash msg map)))))
1442 (when cur-scan-line
1443 (setf (aref (car cur-scan-line) offset) notation))
1444 (dolist (line old-scan-lines)
1445 (when line (setf (aref (car line) offset) notation)))))
1446
1447 ;;;###mh-autoload
1448 (defun mh-thread-add-spaces (count)
1449 "Add COUNT spaces to each scan line in `mh-thread-scan-line-map'."
1450 (let ((spaces (format (format "%%%ss" count) "")))
1451 (while (not (eobp))
1452 (let* ((msg-num (mh-get-msg-num nil))
1453 (old-line (nth 3 (gethash msg-num mh-thread-scan-line-map))))
1454 (when (numberp msg-num)
1455 (setf (gethash msg-num mh-thread-scan-line-map)
1456 (mh-thread-parse-scan-line (format "%s%s" spaces old-line)))))
1457 (forward-line 1))))
1458
1459 (defun mh-thread-print-scan-lines (thread-tree)
1460 "Print scan lines in THREAD-TREE in threaded mode."
1461 (let ((mh-thread-body-width (- (window-width) mh-cmd-note
1462 (1- mh-scan-field-subject-start-offset)))
1463 (mh-thread-last-ancestor nil))
1464 (if (null mh-index-data)
1465 (mh-thread-generate-scan-lines thread-tree -2)
1466 (loop for x in (mh-index-group-by-folder)
1467 do (let* ((old-map mh-thread-scan-line-map)
1468 (mh-thread-scan-line-map (make-hash-table)))
1469 (setq mh-thread-last-ancestor nil)
1470 (loop for msg in (cdr x)
1471 do (let ((v (gethash msg old-map)))
1472 (when v
1473 (setf (gethash msg mh-thread-scan-line-map) v))))
1474 (when (> (hash-table-count mh-thread-scan-line-map) 0)
1475 (insert (if (bobp) "" "\n") (car x) "\n")
1476 (mh-thread-generate-scan-lines thread-tree -2))))
1477 (mh-index-create-imenu-index))))
1478
1479 (defun mh-thread-folder ()
1480 "Generate thread view of folder."
1481 (message "Threading %s..." (buffer-name))
1482 (mh-thread-initialize)
1483 (goto-char (point-min))
1484 (mh-remove-all-notation)
1485 (let ((msg-list ()))
1486 (mh-iterate-on-range msg (cons (point-min) (point-max))
1487 (setf (gethash msg mh-thread-scan-line-map) (mh-thread-parse-scan-line))
1488 (push msg msg-list))
1489 (let* ((range (mh-coalesce-msg-list msg-list))
1490 (thread-tree (mh-thread-generate (buffer-name) range)))
1491 (delete-region (point-min) (point-max))
1492 (mh-thread-print-scan-lines thread-tree)
1493 (mh-notate-user-sequences)
1494 (mh-notate-deleted-and-refiled)
1495 (mh-notate-cur)
1496 (message "Threading %s...done" (buffer-name)))))
1497
1498 ;;;###mh-autoload
1499 (defun mh-toggle-threads ()
1500 "Toggle threaded view of folder."
1501 (interactive)
1502 (let ((msg-at-point (mh-get-msg-num nil))
1503 (old-buffer-modified-flag (buffer-modified-p))
1504 (buffer-read-only nil))
1505 (cond ((memq 'unthread mh-view-ops)
1506 (unless (mh-valid-view-change-operation-p 'unthread)
1507 (error "Can't unthread folder"))
1508 (let ((msg-list ()))
1509 (goto-char (point-min))
1510 (while (not (eobp))
1511 (let ((index (mh-get-msg-num nil)))
1512 (when index
1513 (push index msg-list)))
1514 (forward-line))
1515 (mh-scan-folder mh-current-folder
1516 (mapcar #'(lambda (x) (format "%s" x))
1517 (mh-coalesce-msg-list msg-list))
1518 t))
1519 (when mh-index-data
1520 (mh-index-insert-folder-headers)
1521 (mh-notate-cur)))
1522 (t (mh-thread-folder)
1523 (push 'unthread mh-view-ops)))
1524 (when msg-at-point (mh-goto-msg msg-at-point t t))
1525 (set-buffer-modified-p old-buffer-modified-flag)
1526 (mh-recenter nil)))
1527
1528 ;;;###mh-autoload
1529 (defun mh-thread-forget-message (index)
1530 "Forget the message INDEX from the threading tables."
1531 (let* ((id (gethash index mh-thread-index-id-map))
1532 (id-index (gethash id mh-thread-id-index-map))
1533 (duplicates (gethash id mh-thread-duplicates)))
1534 (remhash index mh-thread-index-id-map)
1535 (remhash index mh-thread-scan-line-map)
1536 (cond ((and (eql index id-index) (null duplicates))
1537 (remhash id mh-thread-id-index-map))
1538 ((eql index id-index)
1539 (setf (gethash id mh-thread-id-index-map) (car duplicates))
1540 (setf (gethash (car duplicates) mh-thread-index-id-map) id)
1541 (setf (gethash id mh-thread-duplicates) (cdr duplicates)))
1542 (t
1543 (setf (gethash id mh-thread-duplicates)
1544 (remove index duplicates))))))
1545
1546 \f
1547
1548 ;;; Operations on threads
1549
1550 (defun mh-thread-current-indentation-level ()
1551 "Find the number of spaces by which current message is indented."
1552 (save-excursion
1553 (let ((address-start-offset (+ mh-cmd-note mh-scan-date-flag-width
1554 mh-scan-date-width 1))
1555 (level 0))
1556 (beginning-of-line)
1557 (forward-char address-start-offset)
1558 (while (char-equal (char-after) ? )
1559 (incf level)
1560 (forward-char))
1561 level)))
1562
1563 ;;;###mh-autoload
1564 (defun mh-thread-next-sibling (&optional previous-flag)
1565 "Jump to next sibling.
1566 With non-nil optional argument PREVIOUS-FLAG jump to the previous sibling."
1567 (interactive)
1568 (cond ((not (memq 'unthread mh-view-ops))
1569 (error "Folder isn't threaded"))
1570 ((eobp)
1571 (error "No message at point")))
1572 (beginning-of-line)
1573 (let ((point (point))
1574 (done nil)
1575 (my-level (mh-thread-current-indentation-level)))
1576 (while (and (not done)
1577 (equal (forward-line (if previous-flag -1 1)) 0)
1578 (not (eobp)))
1579 (let ((level (mh-thread-current-indentation-level)))
1580 (cond ((equal level my-level)
1581 (setq done 'success))
1582 ((< level my-level)
1583 (message "No %s sibling" (if previous-flag "previous" "next"))
1584 (setq done 'failure)))))
1585 (cond ((eq done 'success) (mh-maybe-show))
1586 ((eq done 'failure) (goto-char point))
1587 (t (message "No %s sibling" (if previous-flag "previous" "next"))
1588 (goto-char point)))))
1589
1590 ;;;###mh-autoload
1591 (defun mh-thread-previous-sibling ()
1592 "Jump to previous sibling."
1593 (interactive)
1594 (mh-thread-next-sibling t))
1595
1596 (defun mh-thread-immediate-ancestor ()
1597 "Jump to immediate ancestor in thread tree."
1598 (beginning-of-line)
1599 (let ((point (point))
1600 (ancestor-level (- (mh-thread-current-indentation-level) 2))
1601 (done nil))
1602 (if (< ancestor-level 0)
1603 nil
1604 (while (and (not done) (equal (forward-line -1) 0))
1605 (when (equal ancestor-level (mh-thread-current-indentation-level))
1606 (setq done t)))
1607 (unless done
1608 (goto-char point))
1609 done)))
1610
1611 ;;;###mh-autoload
1612 (defun mh-thread-ancestor (&optional thread-root-flag)
1613 "Jump to the ancestor of current message.
1614 If optional argument THREAD-ROOT-FLAG is non-nil then jump to the root of the
1615 thread tree the message belongs to."
1616 (interactive "P")
1617 (beginning-of-line)
1618 (cond ((not (memq 'unthread mh-view-ops))
1619 (error "Folder isn't threaded"))
1620 ((eobp)
1621 (error "No message at point")))
1622 (let ((current-level (mh-thread-current-indentation-level)))
1623 (cond (thread-root-flag
1624 (while (mh-thread-immediate-ancestor))
1625 (mh-maybe-show))
1626 ((equal current-level 1)
1627 (message "Message has no ancestor"))
1628 (t (mh-thread-immediate-ancestor)
1629 (mh-maybe-show)))))
1630
1631 (defun mh-thread-find-children ()
1632 "Return a region containing the current message and its children.
1633 The result is returned as a list of two elements. The first is the point at the
1634 start of the region and the second is the point at the end."
1635 (beginning-of-line)
1636 (if (eobp)
1637 nil
1638 (let ((address-start-offset (+ mh-cmd-note mh-scan-date-flag-width
1639 mh-scan-date-width 1))
1640 (level (mh-thread-current-indentation-level))
1641 spaces begin)
1642 (setq begin (point))
1643 (setq spaces (format (format "%%%ss" (1+ level)) ""))
1644 (forward-line)
1645 (block nil
1646 (while (not (eobp))
1647 (forward-char address-start-offset)
1648 (unless (equal (string-match spaces (buffer-substring-no-properties
1649 (point) (line-end-position)))
1650 0)
1651 (beginning-of-line)
1652 (backward-char)
1653 (return))
1654 (forward-line)))
1655 (list begin (point)))))
1656
1657 ;;;###mh-autoload
1658 (defun mh-thread-delete ()
1659 "Mark current message and all its children for subsequent deletion."
1660 (interactive)
1661 (cond ((not (memq 'unthread mh-view-ops))
1662 (error "Folder isn't threaded"))
1663 ((eobp)
1664 (error "No message at point"))
1665 (t (let ((region (mh-thread-find-children)))
1666 (mh-iterate-on-messages-in-region () (car region) (cadr region)
1667 (mh-delete-a-msg nil))
1668 (mh-next-msg)))))
1669
1670 ;;;###mh-autoload
1671 (defun mh-thread-refile (folder)
1672 "Mark current message and all its children for refiling to FOLDER."
1673 (interactive (list (intern (mh-prompt-for-refile-folder))))
1674 (cond ((not (memq 'unthread mh-view-ops))
1675 (error "Folder isn't threaded"))
1676 ((eobp)
1677 (error "No message at point"))
1678 (t (let ((region (mh-thread-find-children)))
1679 (mh-iterate-on-messages-in-region () (car region) (cadr region)
1680 (mh-refile-a-msg nil folder))
1681 (mh-next-msg)))))
1682
1683 \f
1684
1685 ;; Tick mark handling
1686
1687 ;;;###mh-autoload
1688 (defun mh-toggle-tick (range)
1689 "Toggle tick mark of RANGE.
1690
1691 This command adds messages to the \"tick\" sequence (which you can customize
1692 via the option `mh-tick-seq'). This sequence can be viewed later with the
1693 \\[mh-index-ticked-messages] command.
1694
1695 Check the documentation of `mh-interactive-range' to see how RANGE is read in
1696 interactive use."
1697 (interactive (list (mh-interactive-range "Tick")))
1698 (unless mh-tick-seq
1699 (error "Enable ticking by customizing `mh-tick-seq'"))
1700 (let* ((tick-seq (mh-find-seq mh-tick-seq))
1701 (tick-seq-msgs (mh-seq-msgs tick-seq))
1702 (ticked ())
1703 (unticked ()))
1704 (mh-iterate-on-range msg range
1705 (cond ((member msg tick-seq-msgs)
1706 (push msg unticked)
1707 (setcdr tick-seq (delq msg (cdr tick-seq)))
1708 (when (null (cdr tick-seq)) (setq mh-last-seq-used nil))
1709 (mh-remove-sequence-notation msg (mh-colors-in-use-p)))
1710 (t
1711 (push msg ticked)
1712 (setq mh-last-seq-used mh-tick-seq)
1713 (let ((mh-seq-list (cons `(,mh-tick-seq ,msg) mh-seq-list)))
1714 (mh-add-sequence-notation msg (mh-colors-in-use-p))))))
1715 (mh-add-msgs-to-seq ticked mh-tick-seq nil t)
1716 (mh-undefine-sequence mh-tick-seq unticked)
1717 (when mh-index-data
1718 (mh-index-add-to-sequence mh-tick-seq ticked)
1719 (mh-index-delete-from-sequence mh-tick-seq unticked))))
1720
1721 ;;;###mh-autoload
1722 (defun mh-narrow-to-tick ()
1723 "Limit to ticked messages.
1724
1725 What this command does is show only those messages that are in the \"tick\"
1726 sequence (which you can customize via the `mh-tick-seq' option) in the
1727 MH-Folder buffer. In addition, it limits further MH-E searches to just those
1728 messages. When you want to widen the view to all your messages again, use
1729 \\[mh-widen]."
1730 (interactive)
1731 (cond ((not mh-tick-seq)
1732 (error "Enable ticking by customizing `mh-tick-seq'"))
1733 ((null (mh-seq-msgs (mh-find-seq mh-tick-seq)))
1734 (message "No messages in %s sequence" mh-tick-seq))
1735 (t (mh-narrow-to-seq mh-tick-seq))))
1736
1737 (provide 'mh-seq)
1738
1739 ;;; Local Variables:
1740 ;;; indent-tabs-mode: nil
1741 ;;; sentence-end-double-space: nil
1742 ;;; End:
1743
1744 ;;; arch-tag: 8e952711-01a2-485b-bf21-c9e3ad4de942
1745 ;;; mh-seq.el ends here