This file is indexed.

/usr/share/emacs/site-lisp/mew/mew-thread.el is in mew 1:6.3-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
;;; mew-thread.el

;; Author:  Kazu Yamamoto <Kazu@Mew.org>
;; Created: Feb  1, 1999

;;; Code:

(require 'mew)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Customizable variables
;;;

(defvar mew-use-sorted-thread t)

(defcustom mew-use-complete-thread t
  "If non-nil, threads are made using two passes.

First pass -  Repeat the following procedure in numerical order:
	(1.0) Pick one message from the message list.
	(1.1) Register the current message-id: to DB.
	(1.2) Find its parent message-id: in DB.
	(1.3) If found, register the current message as a child of
	      the parent.
	(1.4) Otherwise, register the current message to the top
	      node list.

Here we have pretty good threads.  However, if the messages are not
sorted by Date:, it is possible that some top nodes can be
connected to other threads.  If 'mew-use-complete-thread' is non-nil,
the second pass is carried out.

Second pass - Repeat the following procedure for top nodes linearly:
	(2.0) Pick one message from the top node list.
	(2.1) Find its parent message-id: in DB.
	(2.2) If found, register the current message as a child of
	      the parent.
	(2.3) Otherwise, register the current message to the new top
	      node list.

If you have bogus messages and the second pass is carried out, thread
structure MAY loop. This results in an infinite loop of visualizing
threads (not making threads).

Mew does not provide any loop detection/avoidance mechanism. So, you
should understand this risk."
  :group 'mew-summary
  :type 'boolean)

(defcustom mew-thread-indent-strings [" +" " +" " |" "  "]
  "*Vector of strings to be used for indentation of thread.

This consists of four members; 1st member for prefixing to a child
message that is not the last one, 2nd member is for prefixing to the
last child, 3rd and 4th members are for prefixing to grand-child thread trees,
4th member is for the child tree of the last child message.

Example1: [\" +\" \" +\" \" |\" \"  \"] makes thread view below.

    Message 1
     +Message 2
     | +Message 3
     +Message 4
       +Message 5

Example2: [\"  \" \"  \" \"  \" \"  \"] makes thread view below.

    Message 1
      Message 2
        Message 3
      Message 4
        Message 5

All members must have the same length."
  :group 'mew-summary
  :type 'sexp)

(defcustom mew-use-thread-cursor nil
  "*If non-nil, move cursor after the indentation of thread."
  :group 'mew-summary
  :type 'boolean)

(defvar mew-use-thread-separator nil
  "*If non-nil, the specified string is inserted between threads.")
(defvar mew-thread-separator "--")

(defun mew-thread-insert-separator ()
  (if (and mew-use-thread-separator
	   (/= (save-excursion (beginning-of-line) (point)) 1))
      (insert mew-thread-separator "\n")))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Thread info macro
;;;

(defun mew-thread-make-entry ()
  (make-vector 5 nil))

(defun mew-thread-get-myid (entry)
  (aref entry 0))

(defun mew-thread-get-prntid (entry)
  (aref entry 1))

(defun mew-thread-get-child (entry)
  (aref entry 2))

(defun mew-thread-get-msg (entry)
  (aref entry 3))

(defun mew-thread-get-line (entry)
  (aref entry 4))

(defun mew-thread-set-myid (entry myid)
  (aset entry 0 myid))

(defun mew-thread-set-prntid (entry prntid)
  (aset entry 1 prntid))

(defun mew-thread-set-child (entry child)
  (aset entry 2 child))

(defun mew-thread-set-msg (entry msg)
  (aset entry 3 msg))

(defun mew-thread-set-line (entry line)
  (aset entry 4 line))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Thread setup
;;;

(defvar mew-thread-indent-length nil)
(defvar mew-thread-indent-width nil)

(defun mew-thread-setup ()
  (let ((idt1 (aref mew-thread-indent-strings 0))
	(idt2 (aref mew-thread-indent-strings 1))
	(idt3 (aref mew-thread-indent-strings 2))
	(idt4 (aref mew-thread-indent-strings 3)))
    (unless (and (= (string-width idt1) (string-width idt2))
		 (= (string-width idt2) (string-width idt3))
		 (= (string-width idt3) (string-width idt4)))
      (error
       "All members of mew-thread-indent-strings must have the same length"))
    (setq mew-thread-indent-width  (string-width idt1))
    (setq mew-thread-indent-length (length idt1))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Commands
;;;

(defun mew-summary-mark-thread ()
  "Make threads for messages marked with '*'."
  (interactive)
  (mew-summary-thread-region (point-min) (point-max) 'mark))

(defun mew-thread-cache-valid-p (vfolder)
  (let ((cfolder (mew-summary-folder-name 'ext))
	ofld)
    (save-excursion
      (when (get-buffer vfolder)
	(set-buffer vfolder)
	(setq ofld (mew-vinfo-get-original-folder))
	(and (equal ofld cfolder)
	     (get-buffer ofld)
	     (equal (mew-sinfo-get-cache-time)
		    (progn (set-buffer ofld) (mew-sinfo-get-cache-time))))))))

(defun mew-summary-make-thread (&optional arg)
  "If called in Summary mode or Selection, make threads for
all messages.

If called with '\\[universal-argument]', make threads for
messages in the region.

If called in Thread, switch back to the corresponding Summary
mode or Selection."
  (interactive "P")
  (if (mew-mark-active-p) (setq arg t))
  (if arg
      (let ((begend (mew-summary-get-region)))
	(mew-summary-thread-region (car begend) (cdr begend)))
    (mew-summary-goto-message)
    (mew-decode-syntax-delete)
    (let* ((msg (mew-summary-message-number))
	   (disp (mew-sinfo-get-disp-msg))
	   (folder (mew-summary-folder-name 'ext)) ;; xxx
	   fld vfolder)
      (cond
       ((mew-thread-p)
	(setq fld (mew-vinfo-get-original-folder))
	(if (not (and fld (get-buffer fld)))
	    (message "No original folder")
	  (mew-summary-visit-folder fld nil 'no-ls)
	  (mew-summary-toggle-disp-msg (if disp 'on 'off))
	  (if (not msg)
	      (goto-char (point-max))
	    (mew-summary-move-and-display msg))))
       ((and (setq vfolder (mew-folder-to-thread folder))
	     (mew-thread-cache-valid-p vfolder))
	(mew-summary-visit-folder vfolder)
	(mew-summary-toggle-disp-msg (if disp 'on 'off))
	(when msg
	  (mew-summary-move-and-display msg)
	  (mew-thread-move-cursor)))
       ((mew-selection-p)
	(mew-summary-thread-region (point-min) (point-max) nil msg))
       (t
	(mew-summary-thread-region (point-min) (point-max) nil msg))))))

(defun mew-summary-regexp-make-thread (&optional args)
  "Make threads for messages matched to a specified regular expression."
  (interactive "P")
  (mew-decode-syntax-delete)
  (let ((regex "") iter)
    (while (string= regex "")
      (setq regex (read-string "Regexp: ")))
    (if args
	(setq iter (lambda () (re-search-forward regex nil t)))
      (setq iter (lambda () (mew-summary-search-regexp-visible regex))))
    (mew-summary-thread-region (point-min) (point-max) nil nil iter)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Making thread
;;;

(defun mew-thread-get-iter (mark iter)
  (cond
   (iter iter)
   (mark (lambda () (re-search-forward mew-regex-msg-review nil t)))
   (t    (lambda () (not (eobp))))))

(defun mew-thread-create-db (size)
  (let ((dbsize
	 (cond
	  ((<= size 211) 211)
	  ((<= size 1511) 1511)
	  ((<= size 7211) 7211)
	  (t 18211))))
    (make-vector dbsize 0))) ;; hash

(defun mew-thread-pass-1 (db func)
  (let (start msg my-id prnt-id prnt-cld me top line prnt)
    (save-excursion
      (goto-char (point-min))
      (while (funcall func)
	(beginning-of-line)
	(setq start (point))
	(if (not (mew-sumsyn-match mew-regex-sumsyn-long))
	    (forward-line)
	  (setq msg     (mew-sumsyn-message-number))
	  (setq my-id   (mew-sumsyn-my-id))
	  (setq prnt-id (mew-sumsyn-parent-id))
	  (forward-line)
	  ;; Throw away properties here and give properties later.
	  ;; This is faster than inheriting properties.
	  (setq line (mew-buffer-substring start (point)))
	  (setq me (mew-thread-make-entry))
	  (mew-thread-set-msg me msg)
	  (mew-thread-set-line me line)
	  (if (string= my-id "")
	      (setq top (cons me top))
	    ;; some broken messages refer themselves
	    ;; don't register me here so that his parent
	    ;; will not be found.
	    (if (or (string= prnt-id "") (string= my-id prnt-id))
		(setq top (cons me top))
	      (mew-thread-set-prntid me prnt-id)
	      (setq prnt (symbol-value (intern-soft prnt-id db)))
	      (if (null prnt)
		  (setq top (cons me top))
		(setq prnt-cld (mew-thread-get-child prnt))
		(if prnt-cld
		    (nconc prnt-cld (list me))
		  (mew-thread-set-child prnt (list me)))))
	    (mew-thread-set-myid me my-id)
	    (set (intern my-id db) me)))))
    top))

(defun mew-summary-setup-vfolder (db top column)
  (let* ((ofolder (mew-summary-folder-name 'ext))
	 (vfolder (mew-folder-to-thread ofolder))
	 (pfolder (mew-summary-physical-folder))
	 (disp (mew-sinfo-get-disp-msg))
	 (ctime (mew-sinfo-get-cache-time))
	 (case (mew-sinfo-get-case)))
    (mew-summary-switch-to-folder vfolder)
    (mew-vinfo-set-mode 'thread)
    (mew-vinfo-set-physical-folder pfolder)
    (mew-vinfo-set-original-folder ofolder)
    (mew-erase-buffer)
    (mew-hscroll)
    (mew-summary-toggle-disp-msg (if disp 'on 'off))
    (mew-sinfo-set-cache-time ctime)
    (mew-sinfo-set-case case)
    (setq mew-summary-buffer-raw t)
    (mew-vinfo-set-db db)
    (mew-vinfo-set-top top)
    (mew-vinfo-set-column column)))

(defun mew-thread-pass-2 (db top)
  (if (null mew-use-complete-thread)
      (nreverse top)
    ;; This may create looped thread.
    ;; See mew-use-complete-thread for more information.
    (let (prnt prnt-id prnt-cld ret)
      (dolist (me top)
	(if (not (and (mew-thread-get-myid me)
		      (setq prnt-id (mew-thread-get-prntid me))))
	    (setq ret (cons me ret))
	  (setq prnt (symbol-value (intern-soft prnt-id db)))
	  (if (null prnt)
	      (setq ret (cons me ret))
	    (setq prnt-cld (mew-thread-get-child prnt))
	    (if prnt-cld
		(setq prnt-cld (nconc prnt-cld (list me)))
	      (mew-thread-set-child prnt (list me))))))
      ret)))

(defun mew-thread-postscript (mark disp-msg)
  (when mark (mew-mark-undo-mark mew-mark-review))
  (jit-lock-register 'mew-summary-cook-region)
  (mew-summary-set-count-line)
  (set-buffer-modified-p nil)
  (if disp-msg
      (mew-summary-move-and-display disp-msg)
    (goto-char (point-max)))
  (mew-thread-move-cursor))

(defun mew-thread-debug-info (tm1 tm2 tm3 tm4 tm5 tm6)
  (when (mew-debug 'thread)
    (let* ((t1 (mew-time-calc tm2 tm1))
	   (t2 (mew-time-calc tm4 tm3))
	   (t3 (mew-time-calc tm6 tm5)))
      (message "pass1 %f, pass2 %f, visual %f" t1 t2 t3))))

(defun mew-summary-thread-region (beg end &optional mark disp-msg iter)
  "Make threads for messages in a region.  If you want to know how
threads are created, see 'mew-use-complete-thread'."
  (interactive "r")
  (when (mew-summary-exclusive-p)
    (let* ((column (or (mew-sinfo-get-summary-column) ;; scanned
		       ;; Summary only
		       (mew-get-summary-column (mew-summary-folder-name 'ext))))
	   db top tm1 tm2 tm3 tm4 tm5 tm6)
      (save-restriction
	(narrow-to-region beg end)
	(setq db (mew-thread-create-db (count-lines beg end)))
	;;
	(message "Making thread (first pass)...")
	(setq tm1 (current-time))
	(setq top (mew-thread-pass-1 db (mew-thread-get-iter mark iter)))
	(setq tm2 (current-time)))
      ;;
      (if (null top)
	  (message "No target messages")
	(message "Making thread (second pass)...")
	(setq tm3 (current-time))
	(setq top (mew-thread-pass-2 db top))
	(setq tm4 (current-time))
	;;
	(mew-summary-setup-vfolder db top column)
	;;
	(message "Displaying thread...")
	(setq tm5 (current-time))
	(mew-summary-thread-print-top (mew-vinfo-get-top) column)
	(setq tm6 (current-time))
	;;
	(mew-thread-postscript mark disp-msg)	
	;;
	(message "Displaying thread...done")
	(run-hooks 'mew-thread-display-hook)
	(mew-thread-debug-info tm1 tm2 tm3 tm4 tm5 tm6)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Subfunctions
;;;

(defun mew-thread-put-property (beg end level)
  (put-text-property beg end 'mew-thread-indent level))

(defun mew-thread-get-property (beg)
  (get-text-property beg 'mew-thread-indent))

(defun mew-thread-previous-property (beg)
  (previous-single-property-change beg 'mew-thread-indent))

(defun mew-thread-next-property (beg)
  (next-single-property-change beg 'mew-thread-indent))

(defun mew-thread-next-property2 (beg end level)
  (text-property-any beg end 'mew-thread-indent level))

(defun mew-thread-adjust-body (level)
  (when (mew-summary-goto-body)
    (mew-elet
     (let ((end (point))
	   (width (* level mew-thread-indent-width))
	   (sum 0))
       (while (< sum width)
	 (setq sum (+ (char-width (char-before)) sum))
	 (forward-char -1))
       (delete-region (point) end)
       (when (/= sum width)
	 (insert (make-string (- sum width) mew-sp)))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Visualizing thread
;;;

(defun mew-summary-thread-print-top (top column)
  (let (cld)
    (dolist (me top)
      (setq cld (mew-thread-get-child me))
      (mew-elet
       (mew-thread-insert-separator)
       (insert (mew-thread-get-line me))
       (forward-line -1)
       (move-to-column column)
       (mew-thread-put-property (point) (1+ (point)) 0)
       (forward-line))
      (if cld (mew-summary-thread-print-tree cld column)))))

(defun mew-summary-thread-print-tree (tree column)
  (let ((tree-stack nil)
	(prefix "")
	(level 1) pos)
    (while tree
      (let* ((me (car tree))
	     (next (cdr tree))
	     (cld (mew-thread-get-child me)))
	(mew-elet
	 (insert (mew-thread-get-line me))
	 (forward-line -1)
	 (move-to-column column)
 	 (setq pos (point))
	 (if next
	     (insert prefix (aref mew-thread-indent-strings 0))
	   (insert prefix (aref mew-thread-indent-strings 1)))
	 (mew-thread-put-property pos (point) level)
	 (mew-thread-adjust-body level)
	 (forward-line))
	;;
	(setq tree next)
	(cond
	 (cld
	  (if next
	      (setq prefix
		    (concat prefix (aref mew-thread-indent-strings 2)))
	    (setq prefix
		  (concat prefix (aref mew-thread-indent-strings 3))))
	  (setq tree-stack (cons tree tree-stack))
	  (setq tree cld)
	  (setq level (1+ level)))
	 (t
	  (while (and (null tree) tree-stack)
	    (setq prefix (substring prefix 0 (- mew-thread-indent-length)))
	    (setq tree (car tree-stack))
	    (setq tree-stack (cdr tree-stack))
	    (setq level (1- level)))))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Marking thread
;;;

(defun mew-thread-mark-review ()
  "Put the '*' mark on all messages of the current sub-thread."
  (interactive)
  (mew-thread-mark mew-mark-review))

(defun mew-thread-mark-delete ()
  "Put the 'D' mark on all messages of the current sub-thread."
  (interactive)
  (mew-summary-not-in-nntp
   (mew-thread-mark mew-mark-delete 'valid-only)))

(defun mew-thread-mark-unlink ()
  "Put the 'X' mark on all messages of the current sub-thread."
  (interactive)
  (mew-thread-mark mew-mark-unlink 'valid-only))

(defun mew-thread-mark-escape ()
  "Put the '$' mark on all messages of the current sub-thread."
  (interactive)
  (mew-thread-mark mew-mark-escape))

(defun mew-thread-mark-refile ()
  "Put the 'o' mark on all messages of the current sub-thread."
  (interactive)
  (mew-thread-only
   (let* ((fld (mew-folder-basename (mew-summary-folder-name 'ext)))
	  (folders (mew-summary-refile-body nil nil nil 'no-mark))
	  (folders-str (mew-join "," folders))
	  (func (lambda ()
		  (mew-summary-refile-override-body folders-str)
		  (unless (mew-virtual-p)
		    (mew-summary-refile-log fld folders-str))))
	  alist)
     (when folders
       (setq alist (mew-thread-mark mew-mark-refile 'valid-only func))
       (mew-refile-set-from-alist alist folders)))))

(defun mew-thread-mark-copy ()
  "Put the 'o' mark on all messages of the current sub-thread
with the current folder as a candidate in addition to guessed folders."
  (interactive)
  (mew-thread-only
   (let* ((folders (mew-summary-refile-body
		    nil nil nil 'no-mark (mew-summary-folder-name)))
	  (folders-str (mew-join "," folders))
	  (func (lambda () (mew-summary-refile-override-body folders-str)))
	  alist)
     (when folders
       (setq alist (mew-thread-mark mew-mark-refile 'valid-only func))
       (mew-refile-set-from-alist alist folders)))))

(defun mew-refile-set-from-alist (alist folders)
  (let (fld)
    (dolist (ent alist)
      (setq fld (car ent))
      (dolist (msg (sort (copy-sequence (cdr ent)) '<)) ;; sort has side effect
	(setq msg (number-to-string msg))
	(when (get-buffer fld)
	  (save-excursion
	    (set-buffer fld)
	    (mew-refile-reset msg)
	    (mew-refile-set msg folders)))))))

(defun mew-thread-mark (mark &optional valid-only func)
  (mew-thread-only
   (mew-summary-msg-or-part
    (let ((regex (if valid-only mew-regex-sumsyn-valid mew-regex-sumsyn-short))
	  (column (mew-vinfo-get-column))
	  indent cindent fld msg alist bottom pruned)
      (mew-summary-goto-message)
      (mew-decode-syntax-delete)
      (save-excursion
	(beginning-of-line)
	(when (looking-at
	       (concat "^." (regexp-quote (char-to-string mew-mark-thread-root))))
	  (setq pruned (point))
	  (mew-thread-graft 'nomsg))
	(move-to-column column)
	(setq indent (mew-thread-get-property (point)))
	(when (mew-sumsyn-match regex)
	  (setq fld (mew-sumsyn-folder-name))
	  (setq msg (mew-sumsyn-message-number))
	  (if func (funcall func))
	  (mew-mark-put mark)
	  (mew-mark-alist-set alist fld msg))
	(forward-line)
	(catch 'loop
	  (while (not (eobp))
	    (move-to-column column)
	    (when (setq cindent	(mew-thread-get-property (point)))
	      (if (>= indent cindent)
		  (throw 'loop nil)
		(when (mew-sumsyn-match regex)
		  (setq fld (mew-sumsyn-folder-name))
		  (setq msg (mew-sumsyn-message-number))
		  (if func (funcall func))
		  (mew-mark-put mark)
		  (mew-mark-alist-set alist fld msg))))
	    (forward-line)))
	(beginning-of-line)
	(setq bottom (point))
	(mew-summary-mark-in-physical-alist alist mark func)
	(when pruned
	  (goto-char pruned)
	  (mew-thread-prune 'nomsg)))
      (mew-push-mark)
      (let ((mew-summary-down-function (lambda () (goto-char bottom))))
	(mew-summary-display-after mew-summary-mark-direction))
      alist))))

(defun mew-thread-undo (fld msg)
  (let* ((mark (mew-summary-get-mark))
	 (func (mew-markdb-func-undo mark)))
    (and func (fboundp func) (funcall func fld msg))))

(defun mew-thread-unmark ()
  "Unmark messages under this sub-thread."
  (interactive)
  (mew-thread-only
   (mew-summary-msg-or-part
    (let ((column (mew-vinfo-get-column))
	  fld msg alist indent cindent pruned)
      (mew-summary-goto-message)
      (mew-thread-move-cursor)
      (mew-decode-syntax-delete)
      (save-excursion
	(beginning-of-line)
	(when (looking-at
	       (concat "^." (regexp-quote (char-to-string mew-mark-thread-root))))
	  (setq pruned (point))
	  (mew-thread-graft 'nomsg))
	(move-to-column column)
 	(setq indent (mew-thread-get-property (point)))
	(setq fld (mew-summary-folder-name))
	(setq msg (mew-summary-message-number))
	(mew-mark-alist-set alist fld msg)
	(mew-thread-undo fld msg)
	(mew-mark-unmark)
	(forward-line)
	(catch 'loop
 	  (while (not (eobp))
	    (move-to-column column)
	    (when (setq cindent (mew-thread-get-property (point)))
	      (if (>= indent cindent)
		  (throw 'loop nil)
		(setq fld (mew-summary-folder-name))
		(setq msg (mew-summary-message-number))
		(mew-mark-alist-set alist fld msg)
		(mew-thread-undo fld msg)
		(mew-mark-unmark)))
	    (forward-line)))
	(when pruned
	  (goto-char pruned)
	  (mew-thread-prune 'nomsg))
	(mew-thread-unmark-physical-from-alist alist))))))

(defun mew-thread-unmark-physical-from-alist (alist)
  (let (fld msgs)
    (dolist (ent alist)
      (setq fld (car ent))
      (setq msgs (sort (copy-sequence (cdr ent)) '<)) ;; sort has side effect
      (when (get-buffer fld)
	(set-buffer fld)
	(save-excursion
	  (goto-char (point-min))
	  (dolist (msg msgs)
	    (setq msg (number-to-string msg))
	    (when (re-search-forward (mew-regex-sumsyn-msg msg) nil t)
	      (mew-thread-undo fld msg)
	      (mew-mark-unmark))))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Thread utilities
;;;

(defun mew-thread-up ()
  "Move onto the top of the current thread. If the current message is
a top node, move onto the top of the previous thread."
  (interactive)
  (mew-thread-only
   (let (here pos)
     (mew-summary-goto-message)
     (save-excursion
       (mew-decode-syntax-delete)
       (beginning-of-line)
       (setq pos (point))
       (catch 'loop
 	 (while (and (not (bobp))
		     (setq pos (mew-thread-previous-property pos)))
	   (when (and pos (eq (mew-thread-get-property pos) 0))
 	     (throw 'loop (setq here pos))))))
     (if (not here)
	 (message "No more threads")
       (goto-char here)
       (mew-thread-move-cursor)
       (mew-summary-display)))))

(defun mew-thread-down ()
  "Move onto the top of the next thread."
  (interactive)
  (mew-thread-only
   (let (here)
     (mew-summary-goto-message)
     (save-excursion
       (mew-decode-syntax-delete)
       (forward-line)
       (setq here (mew-thread-next-property2 (point) (point-max) 0)))
     (if (not here)
	 (message "No more threads")
       (goto-char here)
       (unless (mew-summary-message-number) (forward-line))
       (mew-thread-move-cursor)
       (mew-summary-display)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Diag
;;;

(defun mew-summary-parent-global (par-id)
  (mew-summary-diag-global par-id "-p" "Parent"))

(defun mew-summary-child-global (my-id)
  (mew-summary-diag-global my-id "-c" "Child"))

(defun mew-summary-diag-global (id opt who)
  (mew-msgid-check
   (let ((db (mew-expand-file "+" mew-id-db-file))
	 (regex (format "\\(.*\\)/\\([0-9]+\\)\\(%s\\)?$" (regexp-quote mew-suffix)))
	 path msg folder)
     (with-temp-buffer
       (mew-piolet mew-cs-text-for-read mew-cs-text-for-write
	 (call-process mew-prog-smew nil t nil opt id db "")
	 (goto-char (point-min))
	 (when (looking-at regex)
	   (setq path (mew-match-string 1))
	   (setq msg (mew-match-string 2)))))
     (if (not msg)
	 nil
       (setq folder (mew-folder-path-to-folder path))
       (when folder
	 (mew-summary-visit-folder folder nil 'no-ls)
	 (if (mew-summary-search-msg msg)
	     (progn
	       (mew-summary-display)
	       t)
	   (concat who " not found. Scan 'update would be necessary")))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Parent
;;;

(defun mew-summary-parent ()
  "Move onto the parent message of the current message."
  (interactive)
  (mew-summary-goto-message)
  (mew-decode-syntax-delete)
  (let ((par-id (mew-summary-parent-id)) result)
    (cond
     ((or (null par-id) (string= par-id ""))
      (message "No parent"))
     ((mew-summary-parent-local par-id)
      (message "Parent found"))
     ((and (y-or-n-p "No parent in this folder. Find in others? ")
	   (setq result (mew-summary-parent-global par-id)))
      (if (eq result t)
	  (message "Parent found")
	(message "%s" result)))
     (t
      (message "Parent not found")))))

(defun mew-summary-parent-local (par-id)
  (let ((pos (point))
	(key (mew-regex-sumsyn-my-id par-id)))
    (if (or (re-search-backward key nil t)
	    (re-search-forward  key nil t))
	(progn
	  (mew-thread-move-cursor)
	  (mew-summary-display)
	  t)
      (goto-char pos)
      nil)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Child
;;;

(defun mew-summary-child ()
  "Move onto the first child message of the current message."
  (interactive)
  (mew-summary-goto-message)
  (mew-decode-syntax-delete)
  (let ((my-id (mew-summary-my-id)) result)
    (cond
     ((or (null my-id) (string= my-id ""))
      (message "No child"))
     ((mew-summary-child-local my-id)
      (message "Child found"))
     ((and (y-or-n-p "No child in this folder. Find in others? ")
           (setq result (mew-summary-child-global my-id)))
      (if (eq result t)
	  (message "Child found")
	(message "%s" result)))
     (t
      (message "Child not found")))))

(defun mew-summary-child-local (my-id)
  (let ((pos (point))
        (key (mew-regex-sumsyn-par-id my-id)))
  (if (or (re-search-forward  key nil t)
          (re-search-backward key nil t))
      (progn
        (mew-thread-move-cursor)
        (mew-summary-display)
        t)
    (goto-char pos)
    nil)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Sibling
;;;

(defun mew-summary-thread-sibling-up ()
  "Search backward by one sibling message of the current message."
  (interactive)
  (let ((pos (point))
	(par-id (mew-summary-parent-id))
	key)
    (if (or (null par-id) (string= par-id ""))
	(message "No sibling")
      (setq key (mew-regex-sumsyn-par-id par-id))
      (if (re-search-backward key nil t)
	  (progn
	    (mew-thread-move-cursor)
	    (mew-summary-display)
	    (message "Sibling found"))
	(goto-char pos)
	(message "Sibling not found")))))

(defun mew-summary-thread-sibling-down ()
  "Search forward by one sibling message of the current message."
  (interactive)
  (let ((pos (point))
	(par-id (mew-summary-parent-id))
	key)
    (if (or (null par-id) (string= par-id ""))
	(message "No sibling")
      (setq key (mew-regex-sumsyn-par-id par-id))
      (forward-line)
      (if (re-search-forward key nil t)
	  (progn
	    (mew-thread-move-cursor)
	    (mew-summary-display)
	    (message "Sibling found"))
	(goto-char pos)
	(message "Sibling not found")))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Thread sub-functions
;;;

(defun mew-thread-move-cursor ()
  "Move cursor after indentation of thread."
  (if (and mew-use-thread-cursor
	   (mew-thread-p)
	   (mew-summary-message-number))
      (let (indent)
 	(move-to-column (mew-vinfo-get-column))
 	(if (setq indent (mew-thread-get-property (point)))
 	    (unless (= indent 0)
 	      (goto-char (mew-thread-next-property (point))))
 	  (beginning-of-line)))
    (beginning-of-line)))

(defun mew-summary-thread-get-msglst (tree &optional add-separator)
  "Get a list of message in the thread order specified by TREE."
  (let ((tree-stack nil) (level 0) msgs me cld)
    (while tree
      (setq me (car tree))
      (setq cld (mew-thread-get-child me))
      (if (and mew-use-thread-separator add-separator (= level 0))
	  (setq msgs (cons "s" msgs))) ;; "s" thread-separator line
      (setq msgs (cons (mew-thread-get-msg me) msgs))
      (setq tree (cdr tree))
      (if (null cld)
	  (while (and (null tree) tree-stack)
	    (setq tree (car tree-stack))
	    (setq tree-stack (cdr tree-stack))
	    (setq level (1- level)))
	(setq tree-stack (cons tree tree-stack))
	(setq tree cld)
	(setq level (1+ level))))
    (if (and mew-use-thread-separator add-separator)
	;; discard first "s"
	(cdr (nreverse msgs))
      (nreverse msgs))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Hide/disclose children
;;;

(defvar mew-mark-thread-root ?+)

(defun mew-thread-toggle ()
  "If children of a message are displayed, they will hide and
\"+\" is displayed on the parent.
If the children are hidden, they will appear."
  (interactive)
  (mew-thread-only
   (mew-summary-goto-message)
   (mew-decode-syntax-delete)
   (if (looking-at (concat "^." (regexp-quote (char-to-string mew-mark-thread-root))))
       (mew-thread-graft)
     (mew-thread-prune))
   (mew-thread-move-cursor)
   (set-buffer-modified-p nil)))

(defun mew-thread-toggle-all ()
  "Toggle appearance of children for all threads."
  (interactive)
  (mew-thread-only
   (let (here)
     (save-excursion
       (goto-char (point-min))
       (mew-decode-syntax-delete)
       (while (setq here (mew-thread-next-property2 (point) (point-max) 0))
	 (goto-char here)
       (beginning-of-line)
       (if (looking-at (concat "^." (regexp-quote (char-to-string mew-mark-thread-root))))
	   (mew-thread-graft)
	 (mew-thread-prune))
       (forward-line))
       (mew-thread-move-cursor)
       (set-buffer-modified-p nil)))))

(defun mew-thread-all-prune ()
  "Hide all children."
  (interactive)
  (mew-thread-only
   (mew-summary-goto-message)
   (mew-decode-syntax-delete)
   (save-excursion
     (goto-char (point-min))
     (let (pos)
       (while (setq pos (mew-thread-next-property2 (point) (point-max) 0))
	 (goto-char pos)
	 (mew-thread-prune 'nomsg)
	 (forward-line))))
   (when (eq (get-text-property (point) 'invisible) t)
     (mew-re-search-backward-visible mew-regex-msg-or-part))
   (mew-thread-move-cursor)
   (set-buffer-modified-p nil)))

(defun mew-thread-all-graft ()
  "Display all children."
  (interactive)
  (mew-thread-only
   (mew-summary-goto-message)
   (mew-decode-syntax-delete)
   (save-excursion
     (goto-char (point-min))
     (let ((regex (concat "^." (regexp-quote (char-to-string mew-mark-thread-root)))))
       (while (re-search-forward regex nil t)
	 (mew-thread-graft 'nomsg)
	 (forward-line))))
   (mew-thread-move-cursor)
   (set-buffer-modified-p nil)))

(defun mew-thread-prune (&optional nomsg)
  (beginning-of-line)
  (let ((pos (mew-thread-next-property (point))))
    (unless (and pos (eq (mew-thread-get-property pos) 0)) ;; root
      (catch 'loop
	(while (setq pos (mew-thread-previous-property pos))
	  (when (and pos (eq (mew-thread-get-property pos) 0))
	    (throw 'loop (goto-char pos)))))))
  (beginning-of-line)
  (save-excursion
    (forward-line)
    (let ((beg (point))
	  (next (mew-thread-next-property2 (point) (point-max) 0)))
      (goto-char (or next (point-max)))
      (forward-line -1)
      (if (mew-summary-message-number) (forward-line))
      (if (= beg (point))
	  (or nomsg (message "No children to be pruned"))
	(mew-elet
	 (put-text-property beg (point) 'invisible t)
	 (goto-char beg)
	 (forward-line -1)
	 (forward-char)
	 (put-text-property (point) (1+ (point)) 'invisible t)
	 (insert mew-mark-thread-root))))))

(defun mew-thread-graft (&optional nomsg)
  (save-excursion
    (forward-line)
    (let ((start (point))
	  (next (mew-thread-next-property2 (point) (point-max) 0))
	  beg end)
      (goto-char (or next (point-max)))
      (forward-line -1)
      (if (mew-summary-message-number) (forward-line))
      (setq end (point))
      (if (= start end)
	  (or nomsg (message "No children to be leaned"))
	(mew-elet
	 (goto-char start)
	 (setq beg start)
	 (while (search-forward "\r" end t)
	   (put-text-property beg (1- (point)) 'invisible nil)
	   (forward-line)
	   (put-text-property (1- (point)) (point) 'invisible nil)
	   (setq beg (point)))
	 (goto-char start)
	 (forward-line -1)
	 (forward-char)
	 (delete-char 1)
	 (put-text-property (point) (1+ (point)) 'invisible nil))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Thread editing
;;;

(defun mew-thread-glue ()
  "Glue a thread/message to the current message as its child(ren).
The thread/message is specified with the mark(\\[set-mark-command])."
  (interactive)
  (mew-summary-msg
   (let* ((zmacs-regions nil)
	  (pos (marker-position (mark-marker))))
     (cond
      ((null pos)
       (message "No marker"))
      ((mew-thread-p)
       (mew-thread-glue-it))
      (t ;; summary or virtual
       (mew-summary-glue-it))))))

(defun mew-summary-glue-it ()
  (save-excursion
    (mew-summary-goto-message)
    (beginning-of-line)
    ;; parent
    (let ((id (mew-summary-my-id))
	  fld msg)
      (goto-char (mark-marker));; user's mark
      ;; children
      (when (mew-sumsyn-match mew-regex-sumsyn-long)
	(setq fld (mew-sumsyn-folder-name))
	(setq msg (mew-sumsyn-message-number)))
      (when (and fld msg id)
	(mew-thread-change-parent-id id)
	(mew-thread-save-xref fld msg id)
	(if (mew-virtual-p)
	    (mew-summary-change-parent-id fld msg id))
	(message "Glued")))))

(defun mew-thread-glue-it ()
  (let ((column (mew-vinfo-get-column))
	(width 0) (wd 0) (adjust 0)
	(prefix "")
	fld msg id beg end tree indent idt pbeg pindent has-child m)
    (save-excursion
      ;; parent
      (mew-summary-goto-message)
      (beginning-of-line)
      (setq id (mew-summary-my-id))
      (move-to-column column)
      (setq beg (point))
      (setq pindent (mew-thread-get-property (point)))
      (goto-char (mew-thread-next-property (point)))
      (setq end (point))
      (unless (= pindent 0)
	(while (< width mew-thread-indent-width)
	  (forward-char -1)
	  (setq width (+ width (char-width (char-after)))))
	(setq prefix (mew-buffer-substring beg (point)))
	(if (string= (mew-buffer-substring (point) end)
		     (aref mew-thread-indent-strings 0))
	    (setq prefix (concat prefix (aref mew-thread-indent-strings 2)))
	  (setq prefix (concat prefix (aref mew-thread-indent-strings 3)))))
      (when (mew-summary-goto-body)
	(while (> (point) beg)
	  (setq wd (+ wd (char-width (char-before))))
	  (forward-char -1)))
      (setq wd (/ wd mew-thread-indent-width))
      (if (> pindent wd) (setq adjust (- pindent wd)))
      (setq pindent (1+ pindent))
      (forward-line)
      ;; the next line of parent
      (setq m (point-marker))
      (unless (looking-at mew-regex-thread-separator)
	(move-to-column column)
	(if (and (mew-thread-get-property (point))
		 (= (mew-thread-get-property (point)) pindent))
	    (setq has-child t)))
      (move-to-column column)
      ;; children
      (goto-char (mark-marker));; user's mark
      (when (mew-sumsyn-match mew-regex-sumsyn-long)
	(setq fld (mew-sumsyn-folder-name))
	(setq msg (mew-sumsyn-message-number)))
      (when (and fld msg id)
	(mew-elet
	 (mew-syntax-change-parent-id id)
	 (beginning-of-line)
	 (setq beg (point))
	 (move-to-column column)
	 (setq pbeg (point))
	 (setq indent (mew-thread-get-property (point)))
	 (insert prefix)
	 (if has-child
	     (insert (aref mew-thread-indent-strings 0))
	   (insert (aref mew-thread-indent-strings 1)))
	 (goto-char (mew-thread-next-property (point)))
	 (mew-thread-put-property pbeg (point) (+ indent pindent))
	 (mew-thread-adjust-body (- pindent adjust))
	 (catch 'loop
	   (while t
	     (forward-line)
	     (move-to-column column)
	     (setq pbeg (point))
	     (setq idt (mew-thread-get-property (point)))
	     (if (or (null idt) (<= idt indent))
		 (throw 'loop nil))
	     (insert prefix)
	     (if has-child
		 (insert (aref mew-thread-indent-strings 2))
	       (insert (aref mew-thread-indent-strings 3)))
	     (goto-char (mew-thread-next-property (point)))
	     (mew-thread-put-property pbeg (point) (+ idt pindent))
	     (mew-thread-adjust-body (- pindent adjust))))
	 (beginning-of-line)
	 (setq end (point))
	 (when (looking-at mew-regex-thread-separator)
	   (forward-line)
	   (delete-region end (point)))
	 ;; This must be "buffer-substring".
	 (setq tree (buffer-substring beg end))
	 (delete-region beg end)
	 ;; the next line of parent
	 (goto-char m)
	 (insert tree)
	 (set-buffer-modified-p nil))
	(mew-summary-change-parent-id fld msg id)
	(mew-thread-save-xref fld msg id)))))

(defun mew-summary-change-parent-id (fld msg id)
  (set-buffer fld)
  (save-excursion
    (when (mew-summary-search-msg msg)
      (mew-thread-change-parent-id id))))

(defun mew-thread-change-parent-id (id)
  (mew-elet
   (mew-syntax-change-parent-id id))
  (unless (mew-virtual-p)
    (mew-summary-folder-cache-save))
  (set-buffer-modified-p nil))

(defun mew-thread-save-xref (fld msg id)
  (with-temp-buffer
    (let ((file (mew-expand-msg fld msg)))
      (mew-insert-file-contents2 file)
      (mew-header-delete-lines (list mew-x-mew-ref:))
      (goto-char (point-min))
      (mew-header-insert mew-x-mew-ref: id)
      (write-region (point-min) (point-max) file nil 'no-msg))))

(provide 'mew-thread)

;;; Copyright Notice:

;; Copyright (C) 2000-2009 Mew developing team.
;; All rights reserved.

;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions
;; are met:
;;
;; 1. Redistributions of source code must retain the above copyright
;;    notice, this list of conditions and the following disclaimer.
;; 2. Redistributions in binary form must reproduce the above copyright
;;    notice, this list of conditions and the following disclaimer in the
;;    documentation and/or other materials provided with the distribution.
;; 3. Neither the name of the team nor the names of its contributors
;;    may be used to endorse or promote products derived from this software
;;    without specific prior written permission.
;;
;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
;; PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

;;; mew-thread.el ends here