1;;; mew-thread.el 2 3;; Author: Kazu Yamamoto <Kazu@Mew.org> 4;; Created: Feb 1, 1999 5 6;;; Code: 7 8(require 'mew) 9 10;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 11;;; 12;;; Customizable variables 13;;; 14 15(defvar mew-use-sorted-thread t) 16 17(defcustom mew-use-complete-thread t 18 "If non-nil, threads are made using two passes. 19 20First pass - Repeat the following procedure in numerical order: 21 (1.0) Pick one message from the message list. 22 (1.1) Register the current message-id: to DB. 23 (1.2) Find its parent message-id: in DB. 24 (1.3) If found, register the current message as a child of 25 the parent. 26 (1.4) Otherwise, register the current message to the top 27 node list. 28 29Here we have pretty good threads. However, if the messages are not 30sorted by Date:, it is possible that some top nodes can be 31connected to other threads. If 'mew-use-complete-thread' is non-nil, 32the second pass is carried out. 33 34Second pass - Repeat the following procedure for top nodes linearly: 35 (2.0) Pick one message from the top node list. 36 (2.1) Find its parent message-id: in DB. 37 (2.2) If found, register the current message as a child of 38 the parent. 39 (2.3) Otherwise, register the current message to the new top 40 node list. 41 42If you have bogus messages and the second pass is carried out, thread 43structure MAY loop. This results in an infinite loop of visualizing 44threads (not making threads). 45 46Mew does not provide any loop detection/avoidance mechanism. So, you 47should understand this risk." 48 :group 'mew-summary 49 :type 'boolean) 50 51(defcustom mew-thread-indent-strings [" +" " +" " |" " "] 52 "*Vector of strings to be used for indentation of thread. 53 54This consists of four members; 1st member for prefixing to a child 55message that is not the last one, 2nd member is for prefixing to the 56last child, 3rd and 4th members are for prefixing to grand-child thread trees, 574th member is for the child tree of the last child message. 58 59Example1: [\" +\" \" +\" \" |\" \" \"] makes thread view below. 60 61 Message 1 62 +Message 2 63 | +Message 3 64 +Message 4 65 +Message 5 66 67Example2: [\" \" \" \" \" \" \" \"] makes thread view below. 68 69 Message 1 70 Message 2 71 Message 3 72 Message 4 73 Message 5 74 75All members must have the same length." 76 :group 'mew-summary 77 :type 'sexp) 78 79(defcustom mew-use-thread-cursor nil 80 "*If non-nil, move cursor after the indentation of thread." 81 :group 'mew-summary 82 :type 'boolean) 83 84(defvar mew-use-thread-separator nil 85 "*If non-nil, the specified string is inserted between threads.") 86(defvar mew-thread-separator "--") 87 88(defun mew-thread-insert-separator () 89 (if (and mew-use-thread-separator 90 (/= (save-excursion (beginning-of-line) (point)) 1)) 91 (insert mew-thread-separator "\n"))) 92 93;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 94;;; 95;;; Thread info macro 96;;; 97 98(defun mew-thread-make-entry () 99 (make-vector 5 nil)) 100 101(defun mew-thread-get-myid (entry) 102 (aref entry 0)) 103 104(defun mew-thread-get-prntid (entry) 105 (aref entry 1)) 106 107(defun mew-thread-get-child (entry) 108 (aref entry 2)) 109 110(defun mew-thread-get-msg (entry) 111 (aref entry 3)) 112 113(defun mew-thread-get-line (entry) 114 (aref entry 4)) 115 116(defun mew-thread-set-myid (entry myid) 117 (aset entry 0 myid)) 118 119(defun mew-thread-set-prntid (entry prntid) 120 (aset entry 1 prntid)) 121 122(defun mew-thread-set-child (entry child) 123 (aset entry 2 child)) 124 125(defun mew-thread-set-msg (entry msg) 126 (aset entry 3 msg)) 127 128(defun mew-thread-set-line (entry line) 129 (aset entry 4 line)) 130 131;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 132;;; 133;;; Thread setup 134;;; 135 136(defvar mew-thread-indent-length nil) 137(defvar mew-thread-indent-width nil) 138 139(defun mew-thread-setup () 140 (let ((idt1 (aref mew-thread-indent-strings 0)) 141 (idt2 (aref mew-thread-indent-strings 1)) 142 (idt3 (aref mew-thread-indent-strings 2)) 143 (idt4 (aref mew-thread-indent-strings 3))) 144 (unless (and (= (string-width idt1) (string-width idt2)) 145 (= (string-width idt2) (string-width idt3)) 146 (= (string-width idt3) (string-width idt4))) 147 (error 148 "All members of mew-thread-indent-strings must have the same length")) 149 (setq mew-thread-indent-width (string-width idt1)) 150 (setq mew-thread-indent-length (length idt1)))) 151 152;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 153;;; 154;;; Commands 155;;; 156 157(defun mew-summary-mark-thread () 158 "Make threads for messages marked with '*'." 159 (interactive) 160 (mew-summary-thread-region (point-min) (point-max) 'mark)) 161 162(defun mew-thread-cache-valid-p (vfolder) 163 (let ((cfolder (mew-summary-folder-name 'ext)) 164 ofld) 165 (when (get-buffer vfolder) 166 (with-current-buffer vfolder 167 (setq ofld (mew-vinfo-get-original-folder)) 168 (and (equal ofld cfolder) 169 (get-buffer ofld) 170 (equal (mew-sinfo-get-cache-time) 171 (progn (set-buffer ofld) (mew-sinfo-get-cache-time)))))))) 172 173(defun mew-summary-make-thread (&optional arg) 174 "If called in Summary mode or Selection, make threads for 175all messages. 176 177If called with '\\[universal-argument]', make threads for 178messages in the region. 179 180If called in Thread, switch back to the corresponding Summary 181mode or Selection." 182 (interactive "P") 183 (if (mew-mark-active-p) (setq arg t)) 184 (if arg 185 (let ((begend (mew-summary-get-region))) 186 (mew-summary-thread-region (car begend) (cdr begend))) 187 (mew-summary-goto-message) 188 (mew-decode-syntax-delete) 189 (let* ((msg (mew-summary-message-number)) 190 (disp (mew-sinfo-get-disp-msg)) 191 (folder (mew-summary-folder-name 'ext)) ;; xxx 192 fld vfolder) 193 (cond 194 ((mew-thread-p) 195 (setq fld (mew-vinfo-get-original-folder)) 196 (if (not (and fld (get-buffer fld))) 197 (message "No original folder") 198 (mew-summary-visit-folder fld nil 'no-ls) 199 (mew-summary-toggle-disp-msg (if disp 'on 'off)) 200 (if (not msg) 201 (goto-char (point-max)) 202 (mew-summary-move-and-display msg)))) 203 ((and (setq vfolder (mew-folder-to-thread folder)) 204 (mew-thread-cache-valid-p vfolder)) 205 (mew-summary-visit-folder vfolder) 206 (mew-summary-toggle-disp-msg (if disp 'on 'off)) 207 (when msg 208 (mew-summary-move-and-display msg) 209 (mew-thread-move-cursor))) 210 ((mew-selection-p) 211 (mew-summary-thread-region (point-min) (point-max) nil msg)) 212 (t 213 (mew-summary-thread-region (point-min) (point-max) nil msg)))))) 214 215(defun mew-summary-regexp-make-thread (&optional args) 216 "Make threads for messages matched to a specified regular expression." 217 (interactive "P") 218 (mew-decode-syntax-delete) 219 (let ((regex "") iter) 220 (while (string= regex "") 221 (setq regex (read-string "Regexp: "))) 222 (if args 223 (setq iter (lambda () (re-search-forward regex nil t))) 224 (setq iter (lambda () (mew-summary-search-regexp-visible regex)))) 225 (mew-summary-thread-region (point-min) (point-max) nil nil iter))) 226 227;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 228;;; 229;;; Making thread 230;;; 231 232(defun mew-thread-get-iter (mark iter) 233 (cond 234 (iter iter) 235 (mark (lambda () (re-search-forward mew-regex-msg-review nil t))) 236 (t (lambda () (not (eobp)))))) 237 238(defun mew-thread-create-db (size) 239 (let ((dbsize 240 (cond 241 ((<= size 211) 211) 242 ((<= size 1511) 1511) 243 ((<= size 7211) 7211) 244 (t 18211)))) 245 (make-vector dbsize 0))) ;; hash 246 247(defun mew-thread-pass-1 (db func) 248 (let (start msg my-id prnt-id prnt-cld me top line prnt) 249 (save-excursion 250 (goto-char (point-min)) 251 (while (funcall func) 252 (beginning-of-line) 253 (setq start (point)) 254 (if (not (mew-sumsyn-match mew-regex-sumsyn-long)) 255 (forward-line) 256 (setq msg (mew-sumsyn-message-number)) 257 (setq my-id (mew-sumsyn-my-id)) 258 (setq prnt-id (mew-sumsyn-parent-id)) 259 (forward-line) 260 ;; Throw away properties here and give properties later. 261 ;; This is faster than inheriting properties. 262 (setq line (mew-buffer-substring start (point))) 263 (setq me (mew-thread-make-entry)) 264 (mew-thread-set-msg me msg) 265 (mew-thread-set-line me line) 266 (if (string= my-id "") 267 (setq top (cons me top)) 268 ;; some broken messages refer themselves 269 ;; don't register me here so that his parent 270 ;; will not be found. 271 (if (or (string= prnt-id "") (string= my-id prnt-id)) 272 (setq top (cons me top)) 273 (mew-thread-set-prntid me prnt-id) 274 (setq prnt (symbol-value (intern-soft prnt-id db))) 275 (if (null prnt) 276 (setq top (cons me top)) 277 (setq prnt-cld (mew-thread-get-child prnt)) 278 (if prnt-cld 279 (nconc prnt-cld (list me)) 280 (mew-thread-set-child prnt (list me))))) 281 (mew-thread-set-myid me my-id) 282 (set (intern my-id db) me))))) 283 top)) 284 285(defun mew-summary-setup-vfolder (db top column) 286 (let* ((ofolder (mew-summary-folder-name 'ext)) 287 (vfolder (mew-folder-to-thread ofolder)) 288 (pfolder (mew-summary-physical-folder)) 289 (disp (mew-sinfo-get-disp-msg)) 290 (ctime (mew-sinfo-get-cache-time)) 291 (case (mew-sinfo-get-case))) 292 (mew-summary-switch-to-folder vfolder) 293 (mew-vinfo-set-mode 'thread) 294 (mew-vinfo-set-physical-folder pfolder) 295 (mew-vinfo-set-original-folder ofolder) 296 (mew-erase-buffer) 297 (mew-hscroll) 298 (mew-summary-toggle-disp-msg (if disp 'on 'off)) 299 (mew-sinfo-set-cache-time ctime) 300 (mew-sinfo-set-case case) 301 (setq mew-summary-buffer-raw t) 302 (mew-vinfo-set-db db) 303 (mew-vinfo-set-top top) 304 (mew-vinfo-set-column column))) 305 306(defun mew-thread-pass-2 (db top) 307 (if (null mew-use-complete-thread) 308 (nreverse top) 309 ;; This may create looped thread. 310 ;; See mew-use-complete-thread for more information. 311 (let (prnt prnt-id prnt-cld ret) 312 (dolist (me top) 313 (if (not (and (mew-thread-get-myid me) 314 (setq prnt-id (mew-thread-get-prntid me)))) 315 (setq ret (cons me ret)) 316 (setq prnt (symbol-value (intern-soft prnt-id db))) 317 (if (null prnt) 318 (setq ret (cons me ret)) 319 (setq prnt-cld (mew-thread-get-child prnt)) 320 (if prnt-cld 321 (setq prnt-cld (nconc prnt-cld (list me))) 322 (mew-thread-set-child prnt (list me)))))) 323 ret))) 324 325(defun mew-thread-postscript (mark disp-msg) 326 (when mark (mew-mark-undo-mark mew-mark-review)) 327 (jit-lock-register 'mew-summary-cook-region) 328 (mew-summary-set-count-line) 329 (set-buffer-modified-p nil) 330 (if disp-msg 331 (mew-summary-move-and-display disp-msg) 332 (goto-char (point-max))) 333 (mew-thread-move-cursor)) 334 335(defun mew-thread-debug-info (tm1 tm2 tm3 tm4 tm5 tm6) 336 (when (mew-debug 'thread) 337 (let* ((t1 (mew-time-calc tm2 tm1)) 338 (t2 (mew-time-calc tm4 tm3)) 339 (t3 (mew-time-calc tm6 tm5))) 340 (message "pass1 %f, pass2 %f, visual %f" t1 t2 t3)))) 341 342(defun mew-summary-thread-region (beg end &optional mark disp-msg iter) 343 "Make threads for messages in a region. If you want to know how 344threads are created, see 'mew-use-complete-thread'." 345 (interactive "r") 346 (when (mew-summary-exclusive-p) 347 (let* ((column (or (mew-sinfo-get-summary-column) ;; scanned 348 ;; Summary only 349 (mew-get-summary-column (mew-summary-folder-name 'ext)))) 350 db top tm1 tm2 tm3 tm4 tm5 tm6) 351 (save-restriction 352 (narrow-to-region beg end) 353 (setq db (mew-thread-create-db (count-lines beg end))) 354 ;; 355 (message "Making thread (first pass)...") 356 (setq tm1 (current-time)) 357 (setq top (mew-thread-pass-1 db (mew-thread-get-iter mark iter))) 358 (setq tm2 (current-time))) 359 ;; 360 (if (null top) 361 (message "No target messages") 362 (message "Making thread (second pass)...") 363 (setq tm3 (current-time)) 364 (setq top (mew-thread-pass-2 db top)) 365 (setq tm4 (current-time)) 366 ;; 367 (mew-summary-setup-vfolder db top column) 368 ;; 369 (message "Displaying thread...") 370 (setq tm5 (current-time)) 371 (mew-summary-thread-print-top (mew-vinfo-get-top) column) 372 (setq tm6 (current-time)) 373 ;; 374 (mew-thread-postscript mark disp-msg) 375 ;; 376 (message "Displaying thread...done") 377 (run-hooks 'mew-thread-display-hook) 378 (mew-thread-debug-info tm1 tm2 tm3 tm4 tm5 tm6))))) 379 380;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 381;;; 382;;; Subfunctions 383;;; 384 385(defun mew-thread-put-property (beg end level) 386 (put-text-property beg end 'mew-thread-indent level)) 387 388(defun mew-thread-get-property (beg) 389 (get-text-property beg 'mew-thread-indent)) 390 391(defun mew-thread-previous-property (beg) 392 (previous-single-property-change beg 'mew-thread-indent)) 393 394(defun mew-thread-next-property (beg) 395 (next-single-property-change beg 'mew-thread-indent)) 396 397(defun mew-thread-next-property2 (beg end level) 398 (text-property-any beg end 'mew-thread-indent level)) 399 400(defun mew-thread-adjust-body (level) 401 (when (mew-summary-goto-body) 402 (mew-elet 403 (let ((end (point)) 404 (width (* level mew-thread-indent-width)) 405 (sum 0)) 406 (while (< sum width) 407 (setq sum (+ (char-width (char-before)) sum)) 408 (forward-char -1)) 409 (delete-region (point) end) 410 (when (/= sum width) 411 (insert (make-string (- sum width) mew-sp))))))) 412 413;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 414;;; 415;;; Visualizing thread 416;;; 417 418(defun mew-summary-thread-print-top (top column) 419 (let (cld) 420 (dolist (me top) 421 (setq cld (mew-thread-get-child me)) 422 (mew-elet 423 (mew-thread-insert-separator) 424 (insert (mew-thread-get-line me)) 425 (forward-line -1) 426 (move-to-column column) 427 (mew-thread-put-property (point) (1+ (point)) 0) 428 (forward-line)) 429 (if cld (mew-summary-thread-print-tree cld column))))) 430 431(defun mew-summary-thread-print-tree (tree column) 432 (let ((tree-stack nil) 433 (prefix "") 434 (level 1) pos) 435 (while tree 436 (let* ((me (car tree)) 437 (next (cdr tree)) 438 (cld (mew-thread-get-child me))) 439 (mew-elet 440 (insert (mew-thread-get-line me)) 441 (forward-line -1) 442 (move-to-column column) 443 (setq pos (point)) 444 (if next 445 (insert prefix (aref mew-thread-indent-strings 0)) 446 (insert prefix (aref mew-thread-indent-strings 1))) 447 (mew-thread-put-property pos (point) level) 448 (mew-thread-adjust-body level) 449 (forward-line)) 450 ;; 451 (setq tree next) 452 (cond 453 (cld 454 (if next 455 (setq prefix 456 (concat prefix (aref mew-thread-indent-strings 2))) 457 (setq prefix 458 (concat prefix (aref mew-thread-indent-strings 3)))) 459 (setq tree-stack (cons tree tree-stack)) 460 (setq tree cld) 461 (setq level (1+ level))) 462 (t 463 (while (and (null tree) tree-stack) 464 (setq prefix (substring prefix 0 (- mew-thread-indent-length))) 465 (setq tree (car tree-stack)) 466 (setq tree-stack (cdr tree-stack)) 467 (setq level (1- level))))))))) 468 469;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 470;;; 471;;; Marking thread 472;;; 473 474(defun mew-thread-mark-review () 475 "Put the '*' mark on all messages of the current sub-thread." 476 (interactive) 477 (mew-thread-mark mew-mark-review)) 478 479(defun mew-thread-mark-delete () 480 "Put the 'D' mark on all messages of the current sub-thread." 481 (interactive) 482 (mew-summary-not-in-nntp 483 (mew-thread-mark mew-mark-delete 'valid-only))) 484 485(defun mew-thread-mark-unlink () 486 "Put the 'X' mark on all messages of the current sub-thread." 487 (interactive) 488 (mew-thread-mark mew-mark-unlink 'valid-only)) 489 490(defun mew-thread-mark-escape () 491 "Put the '$' mark on all messages of the current sub-thread." 492 (interactive) 493 (mew-thread-mark mew-mark-escape)) 494 495(defun mew-thread-mark-refile () 496 "Put the 'o' mark on all messages of the current sub-thread." 497 (interactive) 498 (mew-thread-only 499 (let* ((fld (mew-folder-basename (mew-summary-folder-name 'ext))) 500 (folders (mew-summary-refile-body nil nil nil 'no-mark)) 501 (folders-str (mew-join "," folders)) 502 (func (lambda () 503 (mew-summary-refile-override-body folders-str) 504 (unless (mew-virtual-p) 505 (mew-summary-refile-log fld folders-str)))) 506 alist) 507 (when folders 508 (setq alist (mew-thread-mark mew-mark-refile 'valid-only func)) 509 (mew-refile-set-from-alist alist folders))))) 510 511(defun mew-thread-mark-copy () 512 "Put the 'o' mark on all messages of the current sub-thread 513with the current folder as a candidate in addition to guessed folders." 514 (interactive) 515 (mew-thread-only 516 (let* ((folders (mew-summary-refile-body 517 nil nil nil 'no-mark (mew-summary-folder-name))) 518 (folders-str (mew-join "," folders)) 519 (func (lambda () (mew-summary-refile-override-body folders-str))) 520 alist) 521 (when folders 522 (setq alist (mew-thread-mark mew-mark-refile 'valid-only func)) 523 (mew-refile-set-from-alist alist folders))))) 524 525(defun mew-refile-set-from-alist (alist folders) 526 (let (fld) 527 (dolist (ent alist) 528 (setq fld (car ent)) 529 (dolist (msg (sort (copy-sequence (cdr ent)) '<)) ;; sort has side effect 530 (setq msg (number-to-string msg)) 531 (when (get-buffer fld) 532 (with-current-buffer fld 533 (mew-refile-reset msg) 534 (mew-refile-set msg folders))))))) 535 536(defun mew-thread-mark (mark &optional valid-only func) 537 (mew-thread-only 538 (mew-summary-msg-or-part 539 (let ((regex (if valid-only mew-regex-sumsyn-valid mew-regex-sumsyn-short)) 540 (column (mew-vinfo-get-column)) 541 indent cindent fld msg alist bottom pruned) 542 (mew-summary-goto-message) 543 (mew-decode-syntax-delete) 544 (save-excursion 545 (beginning-of-line) 546 (when (looking-at 547 (concat "^." (regexp-quote (char-to-string mew-mark-thread-root)))) 548 (setq pruned (point)) 549 (mew-thread-graft 'nomsg)) 550 (move-to-column column) 551 (setq indent (mew-thread-get-property (point))) 552 (when (mew-sumsyn-match regex) 553 (setq fld (mew-sumsyn-folder-name)) 554 (setq msg (mew-sumsyn-message-number)) 555 (if func (funcall func)) 556 (mew-mark-put mark) 557 (mew-mark-alist-set alist fld msg)) 558 (forward-line) 559 (catch 'loop 560 (while (not (eobp)) 561 (move-to-column column) 562 (when (setq cindent (mew-thread-get-property (point))) 563 (if (>= indent cindent) 564 (throw 'loop nil) 565 (when (mew-sumsyn-match regex) 566 (setq fld (mew-sumsyn-folder-name)) 567 (setq msg (mew-sumsyn-message-number)) 568 (if func (funcall func)) 569 (mew-mark-put mark) 570 (mew-mark-alist-set alist fld msg)))) 571 (forward-line))) 572 (beginning-of-line) 573 (setq bottom (point)) 574 (mew-summary-mark-in-physical-alist alist mark func) 575 (when pruned 576 (goto-char pruned) 577 (mew-thread-prune 'nomsg))) 578 (mew-push-mark) 579 (let ((mew-summary-down-function (lambda () (goto-char bottom)))) 580 (mew-summary-display-after mew-summary-mark-direction)) 581 alist)))) 582 583(defun mew-thread-undo (fld msg) 584 (let* ((mark (mew-summary-get-mark)) 585 (func (mew-markdb-func-undo mark))) 586 (and func (fboundp func) (funcall func fld msg)))) 587 588(defun mew-thread-unmark () 589 "Unmark messages under this sub-thread." 590 (interactive) 591 (mew-thread-only 592 (mew-summary-msg-or-part 593 (let ((column (mew-vinfo-get-column)) 594 fld msg alist indent cindent pruned) 595 (mew-summary-goto-message) 596 (mew-thread-move-cursor) 597 (mew-decode-syntax-delete) 598 (save-excursion 599 (beginning-of-line) 600 (when (looking-at 601 (concat "^." (regexp-quote (char-to-string mew-mark-thread-root)))) 602 (setq pruned (point)) 603 (mew-thread-graft 'nomsg)) 604 (move-to-column column) 605 (setq indent (mew-thread-get-property (point))) 606 (setq fld (mew-summary-folder-name)) 607 (setq msg (mew-summary-message-number)) 608 (mew-mark-alist-set alist fld msg) 609 (mew-thread-undo fld msg) 610 (mew-mark-unmark) 611 (forward-line) 612 (catch 'loop 613 (while (not (eobp)) 614 (move-to-column column) 615 (when (setq cindent (mew-thread-get-property (point))) 616 (if (>= indent cindent) 617 (throw 'loop nil) 618 (setq fld (mew-summary-folder-name)) 619 (setq msg (mew-summary-message-number)) 620 (mew-mark-alist-set alist fld msg) 621 (mew-thread-undo fld msg) 622 (mew-mark-unmark))) 623 (forward-line))) 624 (when pruned 625 (goto-char pruned) 626 (mew-thread-prune 'nomsg)) 627 (mew-thread-unmark-physical-from-alist alist)))))) 628 629(defun mew-thread-unmark-physical-from-alist (alist) 630 (let (fld msgs) 631 (dolist (ent alist) 632 (setq fld (car ent)) 633 (setq msgs (sort (copy-sequence (cdr ent)) '<)) ;; sort has side effect 634 (when (get-buffer fld) 635 (set-buffer fld) 636 (save-excursion 637 (goto-char (point-min)) 638 (dolist (msg msgs) 639 (setq msg (number-to-string msg)) 640 (when (re-search-forward (mew-regex-sumsyn-msg msg) nil t) 641 (mew-thread-undo fld msg) 642 (mew-mark-unmark)))))))) 643 644;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 645;;; 646;;; Thread utilities 647;;; 648 649(defun mew-thread-up () 650 "Move onto the top of the current thread. If the current message is 651a top node, move onto the top of the previous thread." 652 (interactive) 653 (mew-thread-only 654 (let (here pos) 655 (mew-summary-goto-message) 656 (save-excursion 657 (mew-decode-syntax-delete) 658 (beginning-of-line) 659 (setq pos (point)) 660 (catch 'loop 661 (while (and (not (bobp)) 662 (setq pos (mew-thread-previous-property pos))) 663 (when (and pos (eq (mew-thread-get-property pos) 0)) 664 (throw 'loop (setq here pos)))))) 665 (if (not here) 666 (message "No more threads") 667 (goto-char here) 668 (mew-thread-move-cursor) 669 (mew-summary-display))))) 670 671(defun mew-thread-down () 672 "Move onto the top of the next thread." 673 (interactive) 674 (mew-thread-only 675 (let (here) 676 (mew-summary-goto-message) 677 (save-excursion 678 (mew-decode-syntax-delete) 679 (forward-line) 680 (setq here (mew-thread-next-property2 (point) (point-max) 0))) 681 (if (not here) 682 (message "No more threads") 683 (goto-char here) 684 (unless (mew-summary-message-number) (forward-line)) 685 (mew-thread-move-cursor) 686 (mew-summary-display))))) 687 688;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 689;;; 690;;; Diag 691;;; 692 693(defun mew-summary-parent-global (par-id) 694 (mew-summary-diag-global par-id "-p" "Parent")) 695 696(defun mew-summary-child-global (my-id) 697 (mew-summary-diag-global my-id "-c" "Child")) 698 699(defun mew-summary-diag-global (id opt who) 700 (mew-msgid-check 701 (let ((db (mew-expand-file "+" mew-id-db-file)) 702 (regex (format "\\(.*\\)/\\([0-9]+\\)\\(%s\\)?$" (regexp-quote mew-suffix))) 703 path msg folder) 704 (with-temp-buffer 705 (mew-piolet mew-cs-text-for-read mew-cs-text-for-write 706 (call-process mew-prog-smew nil t nil opt id db "") 707 (goto-char (point-min)) 708 (when (looking-at regex) 709 (setq path (mew-match-string 1)) 710 (setq msg (mew-match-string 2))))) 711 (if (not msg) 712 nil 713 (setq folder (mew-folder-path-to-folder path)) 714 (when folder 715 (mew-summary-visit-folder folder nil 'no-ls) 716 (if (mew-summary-search-msg msg) 717 (progn 718 (mew-summary-display) 719 t) 720 (concat who " not found. Scan 'update would be necessary"))))))) 721 722;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 723;;; 724;;; Parent 725;;; 726 727(defun mew-summary-parent () 728 "Move onto the parent message of the current message." 729 (interactive) 730 (mew-summary-goto-message) 731 (mew-decode-syntax-delete) 732 (let ((par-id (mew-summary-parent-id)) result) 733 (cond 734 ((or (null par-id) (string= par-id "")) 735 (message "No parent")) 736 ((mew-summary-parent-local par-id) 737 (message "Parent found")) 738 ((and (y-or-n-p "No parent in this folder. Find in others? ") 739 (setq result (mew-summary-parent-global par-id))) 740 (if (eq result t) 741 (message "Parent found") 742 (message "%s" result))) 743 (t 744 (message "Parent not found"))))) 745 746(defun mew-summary-parent-local (par-id) 747 (let ((pos (point)) 748 (key (mew-regex-sumsyn-my-id par-id))) 749 (if (or (re-search-backward key nil t) 750 (re-search-forward key nil t)) 751 (progn 752 (mew-thread-move-cursor) 753 (mew-summary-display) 754 t) 755 (goto-char pos) 756 nil))) 757 758;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 759;;; 760;;; Child 761;;; 762 763(defun mew-summary-child () 764 "Move onto the first child message of the current message." 765 (interactive) 766 (mew-summary-goto-message) 767 (mew-decode-syntax-delete) 768 (let ((my-id (mew-summary-my-id)) result) 769 (cond 770 ((or (null my-id) (string= my-id "")) 771 (message "No child")) 772 ((mew-summary-child-local my-id) 773 (message "Child found")) 774 ((and (y-or-n-p "No child in this folder. Find in others? ") 775 (setq result (mew-summary-child-global my-id))) 776 (if (eq result t) 777 (message "Child found") 778 (message "%s" result))) 779 (t 780 (message "Child not found"))))) 781 782(defun mew-summary-child-local (my-id) 783 (let ((pos (point)) 784 (key (mew-regex-sumsyn-par-id my-id))) 785 (if (or (re-search-forward key nil t) 786 (re-search-backward key nil t)) 787 (progn 788 (mew-thread-move-cursor) 789 (mew-summary-display) 790 t) 791 (goto-char pos) 792 nil))) 793 794;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 795;;; 796;;; Sibling 797;;; 798 799(defun mew-summary-thread-sibling-up () 800 "Search backward by one sibling message of the current message." 801 (interactive) 802 (let ((pos (point)) 803 (par-id (mew-summary-parent-id)) 804 key) 805 (if (or (null par-id) (string= par-id "")) 806 (message "No sibling") 807 (setq key (mew-regex-sumsyn-par-id par-id)) 808 (if (re-search-backward key nil t) 809 (progn 810 (mew-thread-move-cursor) 811 (mew-summary-display) 812 (message "Sibling found")) 813 (goto-char pos) 814 (message "Sibling not found"))))) 815 816(defun mew-summary-thread-sibling-down () 817 "Search forward by one sibling message of the current message." 818 (interactive) 819 (let ((pos (point)) 820 (par-id (mew-summary-parent-id)) 821 key) 822 (if (or (null par-id) (string= par-id "")) 823 (message "No sibling") 824 (setq key (mew-regex-sumsyn-par-id par-id)) 825 (forward-line) 826 (if (re-search-forward key nil t) 827 (progn 828 (mew-thread-move-cursor) 829 (mew-summary-display) 830 (message "Sibling found")) 831 (goto-char pos) 832 (message "Sibling not found"))))) 833 834;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 835;;; 836;;; Thread sub-functions 837;;; 838 839(defun mew-thread-move-cursor () 840 "Move cursor after indentation of thread." 841 (if (and mew-use-thread-cursor 842 (mew-thread-p) 843 (mew-summary-message-number)) 844 (let (indent) 845 (move-to-column (mew-vinfo-get-column)) 846 (if (setq indent (mew-thread-get-property (point))) 847 (unless (= indent 0) 848 (goto-char (mew-thread-next-property (point)))) 849 (beginning-of-line))) 850 (beginning-of-line))) 851 852(defun mew-summary-thread-get-msglst (tree &optional add-separator) 853 "Get a list of message in the thread order specified by TREE." 854 (let ((tree-stack nil) (level 0) msgs me cld) 855 (while tree 856 (setq me (car tree)) 857 (setq cld (mew-thread-get-child me)) 858 (if (and mew-use-thread-separator add-separator (= level 0)) 859 (setq msgs (cons "s" msgs))) ;; "s" thread-separator line 860 (setq msgs (cons (mew-thread-get-msg me) msgs)) 861 (setq tree (cdr tree)) 862 (if (null cld) 863 (while (and (null tree) tree-stack) 864 (setq tree (car tree-stack)) 865 (setq tree-stack (cdr tree-stack)) 866 (setq level (1- level))) 867 (setq tree-stack (cons tree tree-stack)) 868 (setq tree cld) 869 (setq level (1+ level)))) 870 (if (and mew-use-thread-separator add-separator) 871 ;; discard first "s" 872 (cdr (nreverse msgs)) 873 (nreverse msgs)))) 874 875;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 876;;; 877;;; Hide/disclose children 878;;; 879 880(defvar mew-mark-thread-root ?+) 881 882(defun mew-thread-toggle () 883 "If children of a message are displayed, they will hide and 884\"+\" is displayed on the parent. 885If the children are hidden, they will appear." 886 (interactive) 887 (mew-thread-only 888 (mew-summary-goto-message) 889 (mew-decode-syntax-delete) 890 (if (looking-at (concat "^." (regexp-quote (char-to-string mew-mark-thread-root)))) 891 (mew-thread-graft) 892 (mew-thread-prune)) 893 (mew-thread-move-cursor) 894 (set-buffer-modified-p nil))) 895 896(defun mew-thread-toggle-all () 897 "Toggle appearance of children for all threads." 898 (interactive) 899 (mew-thread-only 900 (let (here) 901 (save-excursion 902 (goto-char (point-min)) 903 (mew-decode-syntax-delete) 904 (while (setq here (mew-thread-next-property2 (point) (point-max) 0)) 905 (goto-char here) 906 (beginning-of-line) 907 (if (looking-at (concat "^." (regexp-quote (char-to-string mew-mark-thread-root)))) 908 (mew-thread-graft) 909 (mew-thread-prune)) 910 (forward-line)) 911 (mew-thread-move-cursor) 912 (set-buffer-modified-p nil))))) 913 914(defun mew-thread-all-prune () 915 "Hide all children." 916 (interactive) 917 (mew-thread-only 918 (mew-summary-goto-message) 919 (mew-decode-syntax-delete) 920 (save-excursion 921 (goto-char (point-min)) 922 (let (pos) 923 (while (setq pos (mew-thread-next-property2 (point) (point-max) 0)) 924 (goto-char pos) 925 (mew-thread-prune 'nomsg) 926 (forward-line)))) 927 (when (eq (get-text-property (point) 'invisible) t) 928 (mew-re-search-backward-visible mew-regex-msg-or-part)) 929 (mew-thread-move-cursor) 930 (set-buffer-modified-p nil))) 931 932(defun mew-thread-all-graft () 933 "Display all children." 934 (interactive) 935 (mew-thread-only 936 (mew-summary-goto-message) 937 (mew-decode-syntax-delete) 938 (save-excursion 939 (goto-char (point-min)) 940 (let ((regex (concat "^." (regexp-quote (char-to-string mew-mark-thread-root))))) 941 (while (re-search-forward regex nil t) 942 (mew-thread-graft 'nomsg) 943 (forward-line)))) 944 (mew-thread-move-cursor) 945 (set-buffer-modified-p nil))) 946 947(defun mew-thread-prune (&optional nomsg) 948 (beginning-of-line) 949 (let ((pos (mew-thread-next-property (point)))) 950 (unless (and pos (eq (mew-thread-get-property pos) 0)) ;; root 951 (catch 'loop 952 (while (setq pos (mew-thread-previous-property pos)) 953 (when (and pos (eq (mew-thread-get-property pos) 0)) 954 (throw 'loop (goto-char pos))))))) 955 (beginning-of-line) 956 (save-excursion 957 (forward-line) 958 (let ((beg (point)) 959 (next (mew-thread-next-property2 (point) (point-max) 0))) 960 (goto-char (or next (point-max))) 961 (forward-line -1) 962 (if (mew-summary-message-number) (forward-line)) 963 (if (= beg (point)) 964 (or nomsg (message "No children to be pruned")) 965 (mew-elet 966 (put-text-property beg (point) 'invisible t) 967 (goto-char beg) 968 (forward-line -1) 969 (forward-char) 970 (put-text-property (point) (1+ (point)) 'invisible t) 971 (insert mew-mark-thread-root)))))) 972 973(defun mew-thread-graft (&optional nomsg) 974 (save-excursion 975 (forward-line) 976 (let ((start (point)) 977 (next (mew-thread-next-property2 (point) (point-max) 0)) 978 beg end) 979 (goto-char (or next (point-max))) 980 (forward-line -1) 981 (if (mew-summary-message-number) (forward-line)) 982 (setq end (point)) 983 (if (= start end) 984 (or nomsg (message "No children to be leaned")) 985 (mew-elet 986 (goto-char start) 987 (setq beg start) 988 (while (search-forward "\r" end t) 989 (put-text-property beg (1- (point)) 'invisible nil) 990 (forward-line) 991 (put-text-property (1- (point)) (point) 'invisible nil) 992 (setq beg (point))) 993 (goto-char start) 994 (forward-line -1) 995 (forward-char) 996 (delete-char 1) 997 (put-text-property (point) (1+ (point)) 'invisible nil)))))) 998 999;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 1000;;; 1001;;; Thread editing 1002;;; 1003 1004(defun mew-thread-glue () 1005 "Glue a thread/message to the current message as its child(ren). 1006The thread/message is specified with the mark(\\[set-mark-command])." 1007 (interactive) 1008 (mew-summary-msg 1009 (let* ((pos (marker-position (mark-marker)))) 1010 (cond 1011 ((null pos) 1012 (message "No marker")) 1013 ((mew-thread-p) 1014 (mew-thread-glue-it)) 1015 (t ;; summary or virtual 1016 (mew-summary-glue-it)))))) 1017 1018(defun mew-summary-glue-it () 1019 (save-excursion 1020 (mew-summary-goto-message) 1021 (beginning-of-line) 1022 ;; parent 1023 (let ((id (mew-summary-my-id)) 1024 fld msg) 1025 (goto-char (mark-marker));; user's mark 1026 ;; children 1027 (when (mew-sumsyn-match mew-regex-sumsyn-long) 1028 (setq fld (mew-sumsyn-folder-name)) 1029 (setq msg (mew-sumsyn-message-number))) 1030 (when (and fld msg id) 1031 (mew-thread-change-parent-id id) 1032 (mew-thread-save-xref fld msg id) 1033 (if (mew-virtual-p) 1034 (mew-summary-change-parent-id fld msg id)) 1035 (message "Glued"))))) 1036 1037(defun mew-thread-glue-it () 1038 (let ((column (mew-vinfo-get-column)) 1039 (width 0) (wd 0) (adjust 0) 1040 (prefix "") 1041 fld msg id beg end tree indent idt pbeg pindent has-child m) 1042 (save-excursion 1043 ;; parent 1044 (mew-summary-goto-message) 1045 (beginning-of-line) 1046 (setq id (mew-summary-my-id)) 1047 (move-to-column column) 1048 (setq beg (point)) 1049 (setq pindent (mew-thread-get-property (point))) 1050 (goto-char (mew-thread-next-property (point))) 1051 (setq end (point)) 1052 (unless (= pindent 0) 1053 (while (< width mew-thread-indent-width) 1054 (forward-char -1) 1055 (setq width (+ width (char-width (char-after))))) 1056 (setq prefix (mew-buffer-substring beg (point))) 1057 (if (string= (mew-buffer-substring (point) end) 1058 (aref mew-thread-indent-strings 0)) 1059 (setq prefix (concat prefix (aref mew-thread-indent-strings 2))) 1060 (setq prefix (concat prefix (aref mew-thread-indent-strings 3))))) 1061 (when (mew-summary-goto-body) 1062 (while (> (point) beg) 1063 (setq wd (+ wd (char-width (char-before)))) 1064 (forward-char -1))) 1065 (setq wd (/ wd mew-thread-indent-width)) 1066 (if (> pindent wd) (setq adjust (- pindent wd))) 1067 (setq pindent (1+ pindent)) 1068 (forward-line) 1069 ;; the next line of parent 1070 (setq m (point-marker)) 1071 (unless (looking-at mew-regex-thread-separator) 1072 (move-to-column column) 1073 (if (and (mew-thread-get-property (point)) 1074 (= (mew-thread-get-property (point)) pindent)) 1075 (setq has-child t))) 1076 (move-to-column column) 1077 ;; children 1078 (goto-char (mark-marker));; user's mark 1079 (when (mew-sumsyn-match mew-regex-sumsyn-long) 1080 (setq fld (mew-sumsyn-folder-name)) 1081 (setq msg (mew-sumsyn-message-number))) 1082 (when (and fld msg id) 1083 (mew-elet 1084 (mew-syntax-change-parent-id id) 1085 (beginning-of-line) 1086 (setq beg (point)) 1087 (move-to-column column) 1088 (setq pbeg (point)) 1089 (setq indent (mew-thread-get-property (point))) 1090 (insert prefix) 1091 (if has-child 1092 (insert (aref mew-thread-indent-strings 0)) 1093 (insert (aref mew-thread-indent-strings 1))) 1094 (goto-char (mew-thread-next-property (point))) 1095 (mew-thread-put-property pbeg (point) (+ indent pindent)) 1096 (mew-thread-adjust-body (- pindent adjust)) 1097 (catch 'loop 1098 (while t 1099 (forward-line) 1100 (move-to-column column) 1101 (setq pbeg (point)) 1102 (setq idt (mew-thread-get-property (point))) 1103 (if (or (null idt) (<= idt indent)) 1104 (throw 'loop nil)) 1105 (insert prefix) 1106 (if has-child 1107 (insert (aref mew-thread-indent-strings 2)) 1108 (insert (aref mew-thread-indent-strings 3))) 1109 (goto-char (mew-thread-next-property (point))) 1110 (mew-thread-put-property pbeg (point) (+ idt pindent)) 1111 (mew-thread-adjust-body (- pindent adjust)))) 1112 (beginning-of-line) 1113 (setq end (point)) 1114 (when (looking-at mew-regex-thread-separator) 1115 (forward-line) 1116 (delete-region end (point))) 1117 ;; This must be "buffer-substring". 1118 (setq tree (buffer-substring beg end)) 1119 (delete-region beg end) 1120 ;; the next line of parent 1121 (goto-char m) 1122 (insert tree) 1123 (set-buffer-modified-p nil)) 1124 (mew-summary-change-parent-id fld msg id) 1125 (mew-thread-save-xref fld msg id))))) 1126 1127(defun mew-summary-change-parent-id (fld msg id) 1128 (set-buffer fld) 1129 (save-excursion 1130 (when (mew-summary-search-msg msg) 1131 (mew-thread-change-parent-id id)))) 1132 1133(defun mew-thread-change-parent-id (id) 1134 (mew-elet 1135 (mew-syntax-change-parent-id id)) 1136 (unless (mew-virtual-p) 1137 (mew-summary-folder-cache-save)) 1138 (set-buffer-modified-p nil)) 1139 1140(defun mew-thread-save-xref (fld msg id) 1141 (with-temp-buffer 1142 (let ((file (mew-expand-msg fld msg))) 1143 (mew-plet 1144 (mew-insert-file-contents2 file) 1145 (mew-header-delete-lines (list mew-x-mew-ref:)) 1146 (goto-char (point-min)) 1147 (mew-header-insert mew-x-mew-ref: id) 1148 (write-region (point-min) (point-max) file nil 'no-msg))))) 1149 1150(provide 'mew-thread) 1151 1152;;; Copyright Notice: 1153 1154;; Copyright (C) 2000-2015 Mew developing team. 1155;; All rights reserved. 1156 1157;; Redistribution and use in source and binary forms, with or without 1158;; modification, are permitted provided that the following conditions 1159;; are met: 1160;; 1161;; 1. Redistributions of source code must retain the above copyright 1162;; notice, this list of conditions and the following disclaimer. 1163;; 2. Redistributions in binary form must reproduce the above copyright 1164;; notice, this list of conditions and the following disclaimer in the 1165;; documentation and/or other materials provided with the distribution. 1166;; 3. Neither the name of the team nor the names of its contributors 1167;; may be used to endorse or promote products derived from this software 1168;; without specific prior written permission. 1169;; 1170;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND 1171;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1172;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 1173;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE 1174;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1175;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1176;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 1177;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 1178;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 1179;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 1180;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1181 1182;;; mew-thread.el ends here 1183 1184;; Local Variables: 1185;; no-native-compile: t 1186;; End: 1187