File: wl-fldmgr.el

package info (click to toggle)
wl-beta 2.15.9+0.20190205-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 6,260 kB
  • sloc: lisp: 51,446; sh: 191; makefile: 141
file content (1395 lines) | stat: -rw-r--r-- 44,066 bytes parent folder | download
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
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
;;; wl-fldmgr.el --- Folder manager for Wanderlust.

;; Copyright 1998,1999,2000 Masahiro MURATA <muse@ba2.so-net.ne.jp>
;;                          Yuuichi Teranishi <teranisi@gohome.org>

;; Author: Masahiro MURATA <muse@ba2.so-net.ne.jp>
;; Keywords: mail, net news

;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;

;;; Commentary:
;;

;;; Code:
;;

(require 'wl-folder)
(require 'wl-summary)
(require 'wl-highlight)
(require 'wl-version)
(eval-when-compile
  (require 'wl-util))

;;; Global Variable

(defvar wl-fldmgr-modified nil)
(defvar wl-fldmgr-modified-access-list nil)
(defvar wl-fldmgr-cut-entity-list nil)
(defvar wl-fldmgr-entity-list nil)
(defvar wl-fldmgr-group-insert-opened nil)

(defconst wl-fldmgr-folders-header
  (format
   "#
# Folder definition file
# This file is generated automatically by %s.
#
# If you edit this file by hand, be sure that comment lines
# will be washed out by wl-fldmgr.
#

" (product-string-1 'wl-version t)))

;;; Initial setup

(defvar wl-fldmgr-mode-map nil)
(if wl-fldmgr-mode-map
    nil
  (define-prefix-command 'wl-fldmgr-mode-map)
  (define-key wl-fldmgr-mode-map "\C-s"   'wl-fldmgr-save-folders)
  (define-key wl-fldmgr-mode-map "m"      'wl-fldmgr-make-multi)
  (define-key wl-fldmgr-mode-map "g"      'wl-fldmgr-make-group)
  (define-key wl-fldmgr-mode-map "A"      'wl-fldmgr-make-access-group)
  (define-key wl-fldmgr-mode-map "f"      'wl-fldmgr-make-filter)
  (define-key wl-fldmgr-mode-map "p"      'wl-fldmgr-set-petname)
  (define-key wl-fldmgr-mode-map "a"      'wl-fldmgr-add)
  (define-key wl-fldmgr-mode-map "d"      'wl-fldmgr-delete)
  (define-key wl-fldmgr-mode-map "R"      'wl-fldmgr-rename)
  (define-key wl-fldmgr-mode-map "c"      'wl-fldmgr-copy)
  (define-key wl-fldmgr-mode-map "k"      'wl-fldmgr-cut)
  (define-key wl-fldmgr-mode-map "W"      'wl-fldmgr-copy-region)
  (define-key wl-fldmgr-mode-map "\C-w"   'wl-fldmgr-cut-region)
  (define-key wl-fldmgr-mode-map "y"      'wl-fldmgr-yank)
  (define-key wl-fldmgr-mode-map "s"      'wl-fldmgr-sort)
  (define-key wl-fldmgr-mode-map "l"      'wl-fldmgr-access-display-normal)
  (define-key wl-fldmgr-mode-map "L"      'wl-fldmgr-access-display-all)
  (define-key wl-fldmgr-mode-map "q"      'wl-fldmgr-clear-cut-entity-list)
  (define-key wl-fldmgr-mode-map "r"      'wl-fldmgr-reconst-entity-hashtb)
  (define-key wl-fldmgr-mode-map "u"      'wl-fldmgr-unsubscribe)
  (define-key wl-fldmgr-mode-map "U"      'wl-fldmgr-unsubscribe-region))

(add-hook 'wl-folder-mode-hook 'wl-fldmgr-init)

(defun wl-fldmgr-init ()
  (setq wl-fldmgr-cut-entity-list nil)
  (setq wl-fldmgr-modified nil)
  (setq wl-fldmgr-modified-access-list nil))

(defun wl-fldmgr-exit ()
  (when (and wl-fldmgr-modified
	     (or (not wl-interactive-save-folders)
		 (y-or-n-p
		  (concat "Folder view was modified"
			  (and wl-fldmgr-cut-entity-list
			       (format " (%s in cut stack)"
				       (length wl-fldmgr-cut-entity-list)))
			  ".  Save current folders? "))))
    (wl-fldmgr-save-folders)))

;;; Macro and misc Function
;;

(defun wl-fldmgr-delete-line ()
  (delete-region (point-at-bol) (1+ (point-at-eol))))

(defun wl-fldmgr-make-indent (level)
  (concat " " (make-string (* 2 level) (string-to-char " "))))

(defmacro wl-fldmgr-get-entity-id (&optional entity)
  `(get-text-property (if ,entity
			  0
			(point))
		      'wl-folder-entity-id
		      ,entity))

(defmacro wl-fldmgr-assign-id (entity &optional id)
  `(let ((entity-id (or ,id wl-folder-entity-id)))
     (put-text-property 0 (length ,entity)
			'wl-folder-entity-id
			entity-id
			,entity)))

(defsubst wl-fldmgr-read-string (str)
  (if (string-match "\n" str)
      (error "Not supported name: %s" str)
    (elmo-string str)))

(defsubst wl-fldmgr-add-modified-access-list (group)
  (if (not (member group wl-fldmgr-modified-access-list))
      (wl-append wl-fldmgr-modified-access-list (list group))))

(defsubst wl-fldmgr-delete-modified-access-list (group)
  (if (member group wl-fldmgr-modified-access-list)
      (setq wl-fldmgr-modified-access-list
	    (delete group wl-fldmgr-modified-access-list))))

(defsubst wl-fldmgr-add-group (group)
  (or (assoc group wl-folder-group-alist)
      (wl-append wl-folder-group-alist
		 (list (cons group
			     wl-fldmgr-group-insert-opened)))))

(defsubst wl-fldmgr-delete-group (group)
  (wl-fldmgr-delete-modified-access-list group)
  (setq wl-folder-group-alist
	(delete (assoc group wl-folder-group-alist)
		wl-folder-group-alist)))

(defun wl-fldmgr-add-entity-hashtb (entities)
  "Update `wl-folder-entity-hashtb', `wl-folder-newsgroups-hashtb'.
Return value is diffs '(new unread all)."
  (let* ((new-diff 0)
	 (unread-diff 0)
	 (all-diff 0)
	 val entity entity-stack)
    (setq wl-folder-newsgroups-hashtb
	  (or (wl-folder-create-newsgroups-hashtb entities t)
	      wl-folder-newsgroups-hashtb))
    (while entities
      (setq entity (wl-pop entities))
      (cond
       ((consp entity)
	(wl-fldmgr-add-group (car entity))
	(and entities
	     (wl-push entities entity-stack))
	(setq entities (nth 2 entity)))
       ((stringp entity)
	(if (not (setq val (wl-folder-get-entity-info entity)))
	    (wl-folder-set-entity-info entity nil)
	  (setq new-diff    (+ new-diff    (or (nth 0 val) 0)))
	  (setq unread-diff (+ unread-diff (or (nth 1 val) 0)))
	  (setq all-diff    (+ all-diff    (or (nth 2 val) 0))))))
      (unless entities
	(setq entities (wl-pop entity-stack))))
    (setq unread-diff (+ unread-diff new-diff))
    (list new-diff unread-diff all-diff)))

(defun wl-fldmgr-delete-entity-hashtb (entities &optional clear)
  "Update `wl-folder-entity-hashtb'.
return value is diffs '(-new -unread -all)."
  (let* ((new-diff 0)
	 (unread-diff 0)
	 (all-diff 0)
	 entity val
	 entity-stack)
    (while entities
      (setq entity (wl-pop entities))
      (cond
       ((consp entity)
	(wl-fldmgr-delete-group (car entity))
	(and entities
	     (wl-push entities entity-stack))
	(setq entities (nth 2 entity)))
       ((stringp entity)
	(when (setq val (wl-folder-get-entity-info entity))
	  (setq new-diff    (+ new-diff    (or (nth 0 val) 0)))
	  (setq unread-diff (+ unread-diff (or (nth 1 val) 0)))
	  (setq all-diff    (+ all-diff    (or (nth 2 val) 0)))
	  (and clear (wl-folder-clear-entity-info entity)))))
      (unless entities
	(setq entities (wl-pop entity-stack))))
    (setq unread-diff (+ unread-diff new-diff))
    (list (- new-diff) (- unread-diff) (- all-diff))))

;; return value
;; example: '(("Desktop" group) ("+ml" access) "+ml/wl")

(defun wl-fldmgr-get-path (entity target-entity &optional group-target)
  (let* ((target-id (wl-fldmgr-get-entity-id target-entity))
	 (entities (list entity))
	 entity-stack result-path)
    (reverse
     (catch 'done
       (while entities
	 (setq entity (wl-pop entities))
	 (cond
	  ((consp entity)
	   (if (and (string= target-entity (car entity))
		    (eq target-id (wl-fldmgr-get-entity-id (car entity))))
	       (throw 'done
		      (wl-push (if group-target
				   (car entity)
				 (list (car entity) (nth 1 entity)))
			       result-path))
	     (wl-push (list (car entity) (nth 1 entity))
		      result-path))
	   (wl-push entities entity-stack)
	   (setq entities (nth 2 entity)))
	  ((stringp entity)
	   (if (and (string= target-entity entity)
		    (eq target-id (wl-fldmgr-get-entity-id entity)))
	       (throw 'done
		      (wl-push entity result-path)))))
	 (unless entities
	   (while (and entity-stack
		       (not entities))
	     (setq result-path (cdr result-path))
	     (setq entities (wl-pop entity-stack)))))))))

;;;(defun wl-fldmgr-get-previous-entity (entity key-id)
;;;  (cdr (wl-fldmgr-get-previous-entity-internal '(nil . nil) entity key-id)))
;;;
;;;(defun wl-fldmgr-get-previous-entity-internal (result entity key-id)
;;;  (cond
;;;   ((stringp entity)
;;;    (if (eq key-id (wl-fldmgr-get-entity-id entity))
;;;	(cons t result)
;;;      (cons nil (cons entity entity))))
;;;   ((consp entity)
;;;    (if (eq key-id (wl-fldmgr-get-entity-id (car entity)))
;;;	(cons t result)
;;;      (setcar result (car entity))
;;;      (let ((flist (nth 2 entity))
;;;	    return found)
;;;	(while (and flist (not found))
;;;	  (if (car (setq return
;;;			 (wl-fldmgr-get-previous-entity-internal
;;;			  result (car flist) key-id)))
;;;	      (setq found t))
;;;	  (setq result (cdr return))
;;;	  (setq flist (cdr flist)))
;;;	(cons found result))))))

;; path is get `wl-fldmgr-get-path-from-buffer'.
(defun wl-fldmgr-update-group (path diffs)
  (save-excursion
    (while (and path (consp (car path)))
      (if (string= (caar path) wl-folder-desktop-name) ; update desktop
	  (progn
	    (goto-char (point-min))
	    (wl-folder-update-diff-line diffs))
	;; goto the path line.
	(goto-char (point-min))
	(if (wl-folder-buffer-search-group
	     (wl-folder-get-petname (caar path)))
	    (wl-folder-update-diff-line diffs)))
      (setq path (cdr path)))))

;;; Function for wl-folder-entity
;;

;; usage:
;; (wl-delete-entity '(("Desktop") ("ML") "+ml/wl") '("+ml/wl") wl-folder-entity)
;; (wl-delete-entity '(("Desktop") "ML") '("+inbox" "ML") wl-folder-entity)
;; (wl-delete-entity '(("Desktop") "ML") nil wl-folder-entity)

(defun wl-delete-entity (key-path delete-list entity &optional clear)
  (let (wl-fldmgr-entity-list)
    (when (and (string= (caar key-path) (car entity))
	       (wl-delete-entity-sub (cdr key-path) delete-list entity clear))
      ;; return value is non-nil (diffs)
      (wl-fldmgr-delete-entity-hashtb wl-fldmgr-entity-list clear))))

(defun wl-delete-entity-sub (key-path delete-list entity clear)
  (let ((flist (nth 2 entity))
	(key (car key-path))
	next)
    (cond
     ((consp key);; into group
      (if (setq next (assoc (car key) flist))
	  (wl-delete-entity-sub (cdr key-path)
				delete-list
				next
				clear)
	;; not found
	nil))
     ((stringp key) ;; delete entities
      (if (not delete-list)
	  (setq delete-list (list key)))
      (let* ((group (car entity))
	     (access (eq (nth 1 entity) 'access))
	     (unsubscribes (and access (nth 3 entity)))
	     (update t)
	     cut-entity is-group)
	(catch 'done
	  (while delete-list
	    (setq key (car delete-list))
	    (cond ((member key flist);; entity
		   (setq flist (delete key flist))
		   (unless clear
		     (wl-push key wl-fldmgr-cut-entity-list))
		   (wl-append wl-fldmgr-entity-list (list key))
		   (setq is-group nil))
		  ((setq cut-entity (assoc key flist));; group
		   (setq flist (delete cut-entity flist))
		   (unless clear
		     (wl-push cut-entity wl-fldmgr-cut-entity-list))
		   (wl-append wl-fldmgr-entity-list (list cut-entity))
		   (setq is-group t))
		  (t
		   ;; not found
		   (message "%s not found" key)
		   (setq update nil)
		   (throw 'done t)))
	    (when (and access (not clear))
	      (if is-group
		  (wl-append unsubscribes
			     (list (list (elmo-string key) 'access nil)))
		(wl-append unsubscribes (list (elmo-string key)))))
	    (setq delete-list (cdr delete-list))))
	(when update
	  (setcdr (cdr entity) (list flist unsubscribes))
	  (when access
	    (wl-fldmgr-add-modified-access-list group))
	  t
	  ))))))

;; usage:
;; (wl-add-entity '(("Desktop") ("ML") "ml/wl") '("+ml/new") wl-folder-entity 12)
;; (wl-add-entity '(("Desktop") "ML") '("+ml/new")  wl-folder-entity 10)

(defun wl-add-entity (key-path new entity prev-entity-id &optional errmes)
  (when (string= (caar key-path) (car entity))
    (let ((entities new))
      (while entities
	(wl-folder-entity-assign-id
	 (pop entities) wl-folder-entity-id-name-hashtb t)))
    (when (wl-add-entity-sub (cdr key-path) new entity errmes)
      ;; return value is non-nil (diffs)
      (wl-fldmgr-add-entity-hashtb new))))

(defun wl-add-entity-sub (key-path new entity &optional errmes)
  (let ((flist (nth 2 entity))
	entry)
    (catch 'success
      (cond
       ((consp (car key-path));; into group
	(if (setq entry (assoc (caar key-path) flist))
	    (if (not (wl-add-entity-sub (cdr key-path)
					new
					entry
					errmes))
		(throw 'success nil))
	  (and errmes (message "%s not found" (caar key-path)))
	  (throw 'success nil)))
       (t;; insert entities
	(let* ((new2 new)
	       (group (car entity))
	       (access (eq (nth 1 entity) 'access))
	       (unsubscribes (and access (nth 3 entity))))
	  ;; check
	  (while new2
	    (cond
	     ((stringp (car new2)) ;; folder
	      (cond
	       ((elmo-string-member (car new2) flist)
		(and errmes (message "%s: already exists" (car new2)))
		(throw 'success nil))
	       ((and access
		     (not (elmo-string-member (car new2) unsubscribes)))
		(and errmes (message "%s: not access group folder" (car new2)))
		(throw 'success nil))))
	     (t			   ;; group
	      (when (and access
			 (not (wl-string-assoc (caar new2) unsubscribes)))
		(and errmes (message "%s: can't insert access group"
				     (caar new2)))
		(throw 'success nil))))
	    (setq new2 (cdr new2)))
	  ;; do it
	  (when access
	    ;; remove from unsubscribe
	    (setq new2 new)
	    (while new2
	      (if (consp (car new2))
		  (setq unsubscribes
			(delq (wl-string-assoc (car (car new2)) unsubscribes)
			      unsubscribes))
		(setq unsubscribes (delete (elmo-string (car new2))
					   unsubscribes)))
	      (setq new2 (cdr new2)))
	    (setcdr (cddr entity) (list unsubscribes))
	    (wl-fldmgr-add-modified-access-list group))
	  (if (not key-path);; insert group top
	      (if (cddr entity)
		  (setcar (cddr entity) (append new flist))
		(setcdr (cdr entity) (list new)))
	    (let (akey)
	      (if (catch 'done
		    (while flist
		      (setq akey (car flist))
		      (cond ((consp akey);; group
			     (if (equal (car key-path) (car akey))
				 (throw 'done t)))
			    (t
			     (if (equal (car key-path) akey)
				 (throw 'done t))))
		      (setq flist (cdr flist))))
		  (setcdr flist (append new (cdr flist)))
		(and errmes (message "%s not found" (car key-path)))
		(throw 'success nil)))))))
      (throw 'success t))))

;; return value is
;; (path indent-level (group . type) previous-entity-id target-entity)
;; previous-entity-id is (id-name-alist-prev-id . entity-alist-prev-id)
;; example:
;; '((("Desktop" group) ("ML" group) "+ml/wl") '(3 2) ("ML" . group) nil "+ml/wl")

(defun wl-fldmgr-get-path-from-buffer (&optional prev)
  (let ((indent-level 0)
	(group-target t)
	folder-path group-type previous-entity entity)
    (save-excursion
      (beginning-of-line)
      (when prev
;;;	(wl-folder-next-entity-skip-invalid t)
;;;	(and (setq previous-entity
;;;		   (wl-fldmgr-get-previous-entity wl-folder-entity
;;;						  (wl-fldmgr-get-entity-id)))
;;;	     ;; change entity to id
;;;	     (setq previous-entity
;;;		   (cons
;;;		    (and (car previous-entity)
;;;			 (wl-fldmgr-get-entity-id (car previous-entity)))
;;;		    (and (cdr previous-entity)
;;;			 (wl-fldmgr-get-entity-id (cdr previous-entity))))))
	(wl-folder-prev-entity-skip-invalid))
      (if (and prev
	       (wl-folder-buffer-group-p)
	       (looking-at wl-folder-group-regexp)
	       (string= (elmo-match-buffer 2) "-"))
	  (setq group-target nil)
	(if (and prev (bobp))
	    (error "Out of desktop group")))
      (setq folder-path (wl-fldmgr-get-path wl-folder-entity
					    (wl-folder-get-entity-from-buffer)
;;;					    (wl-fldmgr-get-entity-id)
					    group-target))
      (let ((fp folder-path))
	(while fp
	  (if (consp (car fp))
	      (progn
		(setq indent-level (1+ indent-level))
		(setq group-type (cons (caar fp) (nth 1 (car fp)))))
	    (setq entity (car fp)))
	  (setq fp (cdr fp))))
      (list folder-path indent-level group-type previous-entity entity))))

;;; Command
;;

(defun wl-fldmgr-clear-cut-entity-list ()
  (interactive)
  (setq wl-fldmgr-cut-entity-list nil)
  (message "Cleared cut entity list"))

(defun wl-fldmgr-reconst-entity-hashtb (&optional arg nomes)
  (interactive "P")
  (or nomes (message "Reconstructing entity alist..."))
  (when (not arg)
    (setq wl-folder-entity-id 0)
    (wl-folder-entity-assign-id wl-folder-entity))
  (setq wl-folder-entity-hashtb
	(wl-folder-create-entity-hashtb
	 wl-folder-entity
	 wl-folder-entity-hashtb
	 t))
  ;; reset property on buffer
  (when (not arg)
    (let ((inhibit-read-only t)
	  (cur-point (point)))
      (erase-buffer)
      (wl-folder-insert-entity " " wl-folder-entity)
      (goto-char cur-point)
      (set-buffer-modified-p nil)))
  (or nomes (message "Reconstructing entity alist...done")))


(defun wl-fldmgr-cut-region ()
  (interactive)
  (let* ((p1 (region-beginning))
	 (p2 (region-end))
	 (r1 (progn
	       (goto-char p1)
	       (beginning-of-line)
	       (point)))
	 (r2 (progn
	       (goto-char p2)
	       (beginning-of-line)
	       (point)))
	 (from (min r1 r2))
	 (to (max r1 r2))
	 (count 0)
	 (errmes nil)
	 (cut-list nil)
	 name pre-indent indent)
    (catch 'err
      (save-excursion
	(goto-char from)
	(and (looking-at "^\\([ ]*\\)")
	     (setq pre-indent (elmo-match-buffer 1)))
	(while (< (point) to)
	  (and (looking-at "^\\([ ]*\\)")
	       (setq indent (elmo-match-buffer 1)))
	  (cond ((= (length pre-indent) (length indent))
		 (setq pre-indent indent)
		 (setq count (1+ count))
		 (and (setq name (wl-folder-get-entity-from-buffer))
		      (wl-append cut-list (list name)))
		 (forward-line))
		((< (length pre-indent) (length indent))
		 (wl-folder-goto-bottom-of-current-folder pre-indent)
		 (beginning-of-line))
		(t
		 (setq errmes "bad region")
		 (throw 'err t))))
	(unless (eq (point) to)
	  (setq errmes "bad region")
	  (throw 'err t)))
      (save-excursion
	(let ((count2 (length cut-list))
	      tmp path ent diffs)
	  (goto-char from)
	  (save-excursion
	    (wl-folder-next-entity-skip-invalid t)
	    (setq tmp (wl-fldmgr-get-path-from-buffer)))
	  (setq path (car tmp))
	  (setq diffs
		(wl-delete-entity path cut-list wl-folder-entity))
	  (catch 'done
	    (while (> count 0)
	      (setq ent (looking-at wl-folder-entity-regexp))
	      (if (not (wl-fldmgr-cut (and ent tmp)
				      (and ent (pop cut-list))))
		  (throw 'done nil))
	      (setq count (1- count))))
	  (if (> count2 0)
	      (wl-push count2 wl-fldmgr-cut-entity-list))
	  (if diffs
	      (wl-fldmgr-update-group path diffs))
	  t))
      (throw 'err nil))
    (if errmes
	(message "%s" errmes))))

(defun wl-fldmgr-cut (&optional tmp entity clear)
  (interactive)
  (save-excursion
    (beginning-of-line)
    (let ((ret-val nil)
	  (inhibit-read-only t)
	  path diffs)
      (if (bobp)
	  (message "Can't remove desktop group")
	(or tmp (setq tmp (wl-fldmgr-get-path-from-buffer)))
	(setq path (car tmp))
	(if (not path)
	    (if (not (eobp))
		(wl-fldmgr-delete-line))   ;; unsubscribe or removed folder
	  (if (not entity)
	      (setq diffs
		    (wl-delete-entity path nil wl-folder-entity clear)))
	  (setq wl-fldmgr-modified t)
	  ;;
	  (if (and (wl-folder-buffer-group-p)
		   (looking-at wl-folder-group-regexp))
	      ;; group
	      (let (beg end indent opened)
		(setq indent (elmo-match-buffer 1))
		(setq opened (elmo-match-buffer 2))
		(if (string= opened "+")
		    (wl-fldmgr-delete-line)
		  (setq beg (point))
		  (end-of-line)
		  (save-match-data
		    (setq end
			  (progn
			    (wl-folder-goto-bottom-of-current-folder indent)
			    (beginning-of-line)
			    (point))))
		  (delete-region beg end)))
	    ;; entity
	    (wl-fldmgr-delete-line))
	  (if diffs
	      (wl-fldmgr-update-group path diffs))
	  (set-buffer-modified-p nil))
	(setq ret-val t))
      ret-val)))

(defun wl-fldmgr-copy-region ()
  (interactive)
  (let* ((p1 (region-beginning))
	 (p2 (region-end))
	 (r1 (progn
	       (goto-char p1)
	       (beginning-of-line)
	       (point)))
	 (r2 (progn
	       (goto-char p2)
	       (beginning-of-line)
	       (point)))
	 (from (min r1 r2))
	 (to (max r1 r2))
	 (errmes nil)
	 (cut-list nil)
	 (count 0)
	 name
	 pre-indent indent)
    (catch 'err
      (save-excursion
	(goto-char from)
	(when (bobp)
	  (setq errmes "can't copy desktop group")
	  (throw 'err t))
	(and (looking-at "^\\([ ]*\\)")
	     (setq pre-indent (elmo-match-buffer 1)))
	(while (< (point) to)
	  (and (looking-at "^\\([ ]*\\)")
	       (setq indent (elmo-match-buffer 1)))
	  (if (wl-folder-buffer-group-p)
	      (progn
		(setq errmes "can't copy group folder")
		(throw 'err t)))
	  (cond ((= (length pre-indent) (length indent))
		 (if (setq name (wl-folder-get-entity-from-buffer))
		     (progn
		       (setq pre-indent indent)
		       (wl-push name cut-list)))
		 (forward-line))
		((< (length pre-indent) (length indent))
		 (wl-folder-goto-bottom-of-current-folder pre-indent)
		 (beginning-of-line))
		(t
		 (setq errmes "bad region")
		 (throw 'err t))))
	(unless (eq (point) to)
	  (setq errmes "bad region")
	  (throw 'err t)))
      (catch 'done
	(setq cut-list (reverse cut-list))
	(while cut-list
	  (setq name (pop cut-list))
	  (unless (wl-fldmgr-copy name)
	    (throw 'done nil))
	  (setq count (1+ count)))
	(wl-push count wl-fldmgr-cut-entity-list)
	(message "Copy %s folders" count)
	(throw 'err nil)))
    (if errmes
	(message "%s" errmes))))

(defun wl-fldmgr-copy (&optional ename)
  (interactive "P")
  (save-excursion
    (beginning-of-line)
    (let ((ret-val nil))
      (if (and (not ename)
	       (wl-folder-buffer-group-p))
	  (message "Can't copy group folder")
	(let* ((name (or ename (wl-folder-get-entity-from-buffer)))
	       (entity (elmo-string name)))
	  (when name
	    (if (member entity wl-fldmgr-cut-entity-list)
		(setq wl-fldmgr-cut-entity-list
		      (delete entity wl-fldmgr-cut-entity-list)))
	    (wl-push entity wl-fldmgr-cut-entity-list)
	    (or ename
		(message "Copy: %s" name))
	    (setq ret-val t))))
      ret-val)))

(defun wl-fldmgr-yank ()
  (interactive)
  (save-excursion
    (beginning-of-line)
    (if (bobp)
	(message "Can't insert in the out of desktop group")
      (let ((inhibit-read-only t)
	    (top (car wl-fldmgr-cut-entity-list))
	    tmp indent path count new
	    access new-list diffs)
	(if (not top)
	    (message "No cut buffer")
	  (setq tmp (wl-fldmgr-get-path-from-buffer t))
	  (setq path (car tmp))
	  (setq indent (wl-fldmgr-make-indent (nth 1 tmp)))
	  (if (numberp top)
	      (setq count (pop wl-fldmgr-cut-entity-list))
	    (setq count 1))
	  (if (catch 'err
		(let ((count count)
		      (cut-list wl-fldmgr-cut-entity-list))
		  ;; check insert entity
		  (while (> count 0)
		    (setq new (car cut-list))
		    (wl-push new new-list)
		    (when (consp new);; group
		      (cond
		       (access
			(message "Can't insert group in access")
			(throw 'err t))
		       ((wl-string-assoc (car new) wl-folder-group-alist)
			(message "%s: group already exists" (car new))
			(throw 'err t))))
		    (setq cut-list (cdr cut-list))
		    (setq count (1- count))))
		(if (not (setq diffs
			       (wl-add-entity
				path new-list wl-folder-entity (nth 3 tmp) t)))
		    (throw 'err t))
		(while (> count 0)
		  (setq new (pop wl-fldmgr-cut-entity-list))
		  (save-excursion
		    (wl-folder-insert-entity indent new)
		    (setq wl-fldmgr-modified t))
		  (setq count (1- count)))
		(wl-fldmgr-update-group path diffs)
		(set-buffer-modified-p nil))
	      ;; error
	      (wl-push count wl-fldmgr-cut-entity-list)))))))

(defvar wl-fldmgr-add-completion-hashtb (make-vector 7 0))

(defun wl-fldmgr-add-completion-all-completions (string)
  (let ((table
	 (catch 'found
	   (mapatoms
	    (lambda (atom)
	      (if (string-match (symbol-name atom) string)
		  (throw 'found (symbol-value atom))))
	    wl-fldmgr-add-completion-hashtb)))
	(pattern
	 (if (string-match "\\.$"
			   (elmo-folder-prefix-internal
			    (wl-folder-get-elmo-folder string)))
	     (substring string 0 (match-beginning 0))
	   (concat string nil))))
    (or table
	(setq table (elmo-folder-list-subfolders
		     (wl-folder-get-elmo-folder pattern)))
	(and table
	     (or (/= (length table) 1)
		 (elmo-folder-exists-p (wl-folder-get-elmo-folder
					(car table)))))
	(setq pattern
	      (if (string-match "\\.[^\\.]+$" string)
		  (substring string 0 (match-beginning 0))
		(char-to-string (aref string 0)))
	      table (elmo-folder-list-subfolders
		     (wl-folder-get-elmo-folder pattern))))
    (setq pattern (concat "^" (regexp-quote pattern)))
    (unless (intern-soft pattern wl-fldmgr-add-completion-hashtb)
      (set (intern pattern wl-fldmgr-add-completion-hashtb) table))
    table))

(defun wl-fldmgr-add-completion-subr (string predicate flag)
  (let ((table
	 (if (string= string "")
	     (mapcar (lambda (spec)
		       (list (char-to-string (car spec))))
		     elmo-folder-type-alist)
	   (when (assq (aref string 0) elmo-folder-type-alist)
	     (delq nil (mapcar
			(function list)
			(condition-case nil
			    (wl-fldmgr-add-completion-all-completions string)
			  (error nil))))))))
    (cond
     ((null flag)
      (try-completion string table predicate))
     ((eq flag 'lambda)
      (eq t (try-completion string table predicate)))
     (t
      (all-completions string table predicate)))))

(defun wl-fldmgr-add (&optional name)
  (interactive)
  (save-excursion
    (beginning-of-line)
    (let ((ret-val nil)
	  (inhibit-read-only t)
	  (wl-folder-complete-folder-candidate
	   (if wl-fldmgr-add-complete-with-current-folder-list
	       (function wl-fldmgr-add-completion-subr)))
	  tmp indent path diffs)
      (if (bobp)
	  (message "Can't insert in the out of desktop group")
	(setq tmp (wl-fldmgr-get-path-from-buffer t))
	(setq path (car tmp))
	(setq indent (wl-fldmgr-make-indent (nth 1 tmp)))
	(or name
	    (setq name (wl-fldmgr-read-string
			(wl-summary-read-folder wl-default-folder "to add"))))
	;; maybe add elmo-plugged-alist.
	(elmo-folder-set-plugged (wl-folder-get-elmo-folder
				  (if (listp name) (car name) name))
				 wl-plugged t)
	(when (setq diffs
		    (wl-add-entity
		     path (list name) wl-folder-entity (nth 3 tmp) t))
	  (wl-folder-insert-entity indent name)
	  (wl-fldmgr-update-group path diffs)
	  (setq wl-fldmgr-modified t)
	  (set-buffer-modified-p nil)
	  (setq ret-val t)))
      ret-val)))

(defun wl-fldmgr-delete ()
  (interactive)
  (save-excursion
    (beginning-of-line)
    (if (wl-folder-buffer-group-p)
	(error "Can't delete group folder"))
    (let* ((inhibit-read-only t)
	   (tmp (wl-fldmgr-get-path-from-buffer))
	   (entity (elmo-string (nth 4 tmp)))
	   (folder (wl-folder-get-elmo-folder entity)))
      (when (elmo-folder-delete folder)
	(wl-folder-clear-entity-info entity)
	(wl-fldmgr-cut tmp nil t)
	(wl-fldmgr-save-access-list)))))

(defun wl-fldmgr-rename ()
  (interactive)
  (save-excursion
    (beginning-of-line)
    (if (bobp)
	(message "Can't rename desktop group")
      (cond
       ((and (wl-folder-buffer-group-p)
	     (looking-at wl-folder-group-regexp)) ;; group
	(let* ((indent (elmo-match-buffer 1))
	       (old-group (wl-folder-get-entity-from-buffer))
	       (group-entity (wl-folder-search-group-entity-by-name
			      old-group wl-folder-entity))
	       group)
	  (if (eq (nth 1 group-entity) 'access)
	      (message "%s: can't rename access group folder" old-group)
	    (setq group (wl-fldmgr-read-string
			 (read-from-minibuffer "Rename: " old-group)))
	    (if (string-match "/$" group)
		(message "Remove tail slash.")
	      (cond
	       ((or (string= group "")
		    (string= old-group group))
		nil)
	       (t
		(if (wl-string-assoc group wl-folder-group-alist)
		    (message "%s: group already exists" group)
		  (let ((inhibit-read-only t)
			(id (wl-fldmgr-get-entity-id
			     (car group-entity))))
		    (wl-fldmgr-assign-id group id)
		    (setcar group-entity group)
		    (setcar (wl-string-assoc old-group wl-folder-group-alist)
			    group)
;;;		    (setcdr (assq id wl-folder-entity-id-name-alist) group)
		    (wl-folder-set-id-name id group)
		    (wl-fldmgr-delete-line)
		    (wl-folder-insert-entity
		     indent
		     group-entity t)
		    (setq wl-fldmgr-modified t)
		    (set-buffer-modified-p nil)))))))))
       (t ;; folder
	(let* ((tmp (wl-fldmgr-get-path-from-buffer))
	       (old-folder (nth 4 tmp))
	       new-folder)
	  (unless old-folder (error "No folder"))
	  (setq new-folder
		(wl-fldmgr-read-string
		 (wl-summary-read-folder old-folder "to rename" t t old-folder)))
	  (if (or (wl-folder-entity-exists-p new-folder)
		  (file-exists-p (elmo-folder-msgdb-path
				  (wl-folder-get-elmo-folder new-folder))))
	      (error "Already exists folder: %s" new-folder))
	  (if (and (eq (cdr (nth 2 tmp)) 'access)
		   (null wl-fldmgr-allow-rename-access-group)
		   (null (string-match
			  (format "^%s" (regexp-quote (car (nth 2 tmp))))
			  new-folder)))
	      (error "Can't rename access folder"))
	  (elmo-folder-rename (wl-folder-get-elmo-folder old-folder)
			      new-folder)
	  (wl-folder-set-entity-info
	   new-folder
	   (wl-folder-get-entity-info old-folder))
	  (wl-folder-clear-entity-info old-folder)
	  (setq wl-folder-info-alist-modified t)
	  (if (eq (cdr (nth 2 tmp)) 'access)

	      ;; force update access group
	      (progn
		(wl-folder-open-close)
		(wl-folder-jump-to-current-entity t)
		(message "%s is renamed to %s" old-folder new-folder)
		(sit-for 1))
	    ;; update folder list
	    (when (wl-fldmgr-cut tmp nil t)
	      (wl-fldmgr-add new-folder)))))))))

(defun wl-fldmgr-make-access-group ()
  (interactive)
  (wl-fldmgr-make-group nil t))

(defun wl-fldmgr-make-group (&optional group-name access)
  (interactive)
  (save-excursion
    (beginning-of-line)
    (if (bobp)
	(message "Can't insert in the out of desktop group")
      (let ((inhibit-read-only t)
	    (type 'group)
	    group tmp indent path new prev-id flist diffs)
	(setq tmp (wl-fldmgr-get-path-from-buffer t))
	(setq path (car tmp))
	(setq indent (wl-fldmgr-make-indent (nth 1 tmp)))
	(setq prev-id (nth 3 tmp))
	(if (eq (cdr (nth 2 tmp)) 'access)
	    (message "Can't insert access group")
	  (setq group (or group-name
			  (wl-fldmgr-read-string
			   (read-from-minibuffer
			    (if access "Access Type Group: " "Group: ")))))
	  ;; To check the folder name is correct.
	  (if access (elmo-make-folder group))
	  (when (or access (string-match "[\t ]*/$" group))
	    (setq group (if access group
			  (substring group 0 (match-beginning 0))))
	    (setq type 'access)
	    (setq flist (wl-create-access-folder-entity group)))
	  (if (string= group "")
	      nil
	    (if (wl-string-assoc group wl-folder-group-alist)
		(message "%s: group already exists" group)
	      (setq new (append (list group type) flist))
	      (when (setq diffs (wl-add-entity path
					       (list new)
					       wl-folder-entity
					       prev-id))
		(wl-folder-insert-entity indent new)
		(wl-fldmgr-update-group path diffs)
		(setq wl-fldmgr-modified t)
		(set-buffer-modified-p nil)))))))))

(defun wl-fldmgr-make-multi ()
  (interactive)
  (if (not wl-fldmgr-cut-entity-list)
      (message "No cut buffer")
    (let ((cut-entity wl-fldmgr-cut-entity-list)
	  (new-entity "")
	  (first t)
	  status)
      (setq status
	    (catch 'done
	      (while cut-entity
		(cond
		 ((numberp (car cut-entity))
		  nil)
		 ((consp (car cut-entity))
		  (message "Can't make multi included group folder")
		  (throw 'done nil))
		 (t
		  (let ((folder (wl-folder-get-elmo-folder
				 (car cut-entity)))
			multi-fld)
		    (if (eq (elmo-folder-type-internal folder) 'multi)
			(setq multi-fld
			      (substring (car cut-entity) 1)))
		    (setq new-entity
			  (format "%s%s%s"
				  (or multi-fld (car cut-entity))
				  (if first "" ",")
				  new-entity))
		    (setq first nil))))
		(setq cut-entity (cdr cut-entity)))
	      (throw 'done t)))
      (when status
	(setq new-entity (concat "*" new-entity))
	(wl-fldmgr-add new-entity)))))

(defun wl-fldmgr-make-filter ()
  (interactive)
  (save-excursion
    (beginning-of-line)
    (let ((tmp (wl-fldmgr-get-path-from-buffer))
	  entity)
      (if (eq (cdr (nth 2 tmp)) 'access)
	  (message "Can't change access group")
	(if (wl-folder-buffer-group-p)
	    (setq entity
		  (concat
		   "*"
		   (mapconcat 'identity
			      (wl-folder-get-entity-list
			       (wl-folder-search-group-entity-by-name
				(nth 4 tmp)
				wl-folder-entity)) ",")))
	  (setq entity (nth 4 tmp)))
	(unless entity (error "No folder"))
	(wl-fldmgr-add (concat "/"
			       (wl-read-search-condition
				wl-fldmgr-make-filter-default)
			       "/" entity))))))

(defun wl-fldmgr-sort (&optional arg)
  (interactive "P")
  (save-excursion
    (beginning-of-line)
    (let ((inhibit-read-only t)
	  entity flist indent opened)
      (when (and (wl-folder-buffer-group-p)
		 (looking-at wl-folder-group-regexp)
		 (prog1
		     (y-or-n-p (format "Sort subfolders of %s? "
				       (wl-folder-get-entity-from-buffer)))
		   (message nil)))
	(setq indent (elmo-match-buffer 1))
	(setq opened (elmo-match-buffer 2))
	(setq entity (wl-folder-search-group-entity-by-name
		      (wl-folder-get-entity-from-buffer)
		      wl-folder-entity))
	(message "Sorting...")
	(setq flist (sort (nth 2 entity) wl-fldmgr-sort-function))
	(when arg (setq flist (nreverse flist)))
	(setcar (cddr entity) flist)
	(wl-fldmgr-add-modified-access-list (car entity))
	(setq wl-fldmgr-modified t)
	(when (string= opened "-")
	  (let (beg end)
	    (setq beg (point))
	    (end-of-line)
	    (save-match-data
	      (setq end
		    (progn
		      (wl-folder-goto-bottom-of-current-folder indent)
		      (beginning-of-line)
		      (point))))
	    (delete-region beg end)
	    (wl-folder-insert-entity indent entity)))
	(message "Sorting...done")
	(set-buffer-modified-p nil)))))

(defun wl-fldmgr-sort-standard (x y)
  (cond ((and (consp x) (not (consp y)))
	 wl-fldmgr-sort-group-first)
	((and (not (consp x)) (consp y))
	 (not wl-fldmgr-sort-group-first))
	((and (consp x) (consp y))
	 (string-lessp (car x) (car y)))
	(t
	 (string-lessp x y))))

(defun wl-fldmgr-subscribe-region ()
  (interactive)
  (wl-fldmgr-unsubscribe-region -1))

(defun wl-fldmgr-unsubscribe-region (&optional arg)
  (interactive "P")
  (let* ((p1 (region-beginning))
	 (p2 (region-end))
	 (r1 (progn
	       (goto-char p1)
	       (beginning-of-line)
	       (point)))
	 (r2 (progn
	       (goto-char p2)
	       (beginning-of-line)
	       (point)))
	 (from (min r1 r2))
	 (to (max r1 r2))
	 (count 0))
    (goto-char from)
    (while (< (point) to)
      (setq count (1+ count))
      (forward-line))
    (goto-char from)
    (message "Unsubscribe region...")
    (while (and (> count 0)
		(wl-fldmgr-unsubscribe (or arg 1) t))
      (setq count (1- count)))
    (message "Unsubscribe region...done")))

(defun wl-fldmgr-subscribe ()
  (interactive)
  (wl-fldmgr-unsubscribe -1))

(defun wl-fldmgr-unsubscribe (&optional arg force)
  (interactive "P")
  (let ((type (and arg (prefix-numeric-value arg)))
	execed is-group)
    (save-excursion
      (beginning-of-line)
      (let ((inhibit-read-only t)
	    folder
	    tmp indent beg)
	(cond
	 ((looking-at (format "^[ ]*%s\\[[+-]\\]\\(.*\\)" wl-folder-unsubscribe-mark))
	  (if (and type (> type 0))
	      nil
	    (setq folder (list (elmo-match-buffer 1) 'access nil))
	    (if (wl-string-assoc (car folder) wl-folder-group-alist)
		(message "%s: group already exists" (car folder))
	      (wl-fldmgr-delete-line)
	      (when (wl-fldmgr-add folder)
		(wl-folder-maybe-load-folder-list folder)
;;;		(wl-folder-search-group-entity-by-name (car folder)
;;;						       wl-folder-entity)
		(setq execed t)))))
	 ((looking-at (format "^[ ]*%s\\(.*\\)" wl-folder-unsubscribe-mark))
	  (if (and type (> type 0))
	      nil
	    (setq folder (elmo-match-buffer 1))
	    (wl-fldmgr-delete-line)
	    (when (wl-fldmgr-add folder)
	      (setq execed t))))
	 (t
	  (if (and type (< type 0))
	      nil
	    (setq is-group (wl-folder-buffer-group-p))
	    (setq tmp (wl-fldmgr-get-path-from-buffer))
	    (setq indent (wl-fldmgr-make-indent (nth 1 tmp)))
	    (if (eq (cdr (nth 2 tmp)) 'access)
		(when (wl-fldmgr-cut tmp)
		  ;; don't leave cut-list
		  (setq wl-fldmgr-cut-entity-list (cdr wl-fldmgr-cut-entity-list))
		  (setq beg (point))
		  (insert indent wl-folder-unsubscribe-mark
			  (if is-group
			      (concat "[+]" (nth 4 tmp))
			    (nth 4 tmp))
			  "\n")
		  (save-excursion (forward-line -1)
				  (wl-highlight-folder-current-line))
		  (remove-text-properties beg (point) '(wl-folder-entity-id))
		  (setq execed t))
	      (message "not an access group folder")))))
	(set-buffer-modified-p nil)))
    (if (or force execed)
	(progn
	  (forward-line)
	  t))))

(defun wl-fldmgr-access-display-normal (&optional arg)
  (interactive "P")
  (wl-fldmgr-access-display-all (not arg)))

(defun wl-fldmgr-access-display-all (&optional arg)
  (interactive "P")
  (let ((id (save-excursion
	      (wl-folder-prev-entity-skip-invalid t)
	      (wl-fldmgr-get-entity-id))))
    (save-excursion
    (beginning-of-line)
    (let ((inhibit-read-only t)
	  entity indent opened
	  unsubscribes beg)
      (when (not
	     (and (wl-folder-buffer-group-p)
		  (looking-at wl-folder-group-regexp)))
	(wl-folder-goto-top-of-current-folder)
	(looking-at wl-folder-group-regexp))
      (setq indent (elmo-match-buffer 1))
      (setq opened (elmo-match-buffer 2))
      (setq entity (wl-folder-search-group-entity-by-name
		    (wl-folder-get-entity-from-buffer)
		    wl-folder-entity))
      (when (eq (nth 1 entity) 'access)
	(save-excursion
	  (if (string= opened "-")
	      (let (beg end)
		(setq beg (point))
		(end-of-line)
		(save-match-data
		  (setq end
			(progn
			  (wl-folder-goto-bottom-of-current-folder indent)
			  (beginning-of-line)
			  (point))))
		(delete-region beg end))
	    (wl-fldmgr-delete-line)
	    (setcdr (assoc (car entity) wl-folder-group-alist) t));; set open
	  (wl-folder-insert-entity indent entity))
	(when (not arg)
	  (setq unsubscribes (nth 3 entity))
	  (forward-line)
	  (while unsubscribes
	    (setq beg (point))
	    (insert indent "  " wl-folder-unsubscribe-mark
		    (if (consp (car unsubscribes))
			(concat "[+]" (caar unsubscribes))
		      (car unsubscribes))
		    "\n")
	    (remove-text-properties beg (point) '(wl-folder-entity-id))
	    (save-excursion (forward-line -1)
			    (wl-highlight-folder-current-line))
	    (setq unsubscribes (cdr unsubscribes))))
	(set-buffer-modified-p nil))))
    (wl-folder-move-path id)))

(defun wl-fldmgr-set-petname ()
  (interactive)
  (save-excursion
    (beginning-of-line)
      (let* ((is-group (wl-folder-buffer-group-p))
	     (name (wl-folder-get-entity-from-buffer))
	     (searchname (wl-folder-get-petname name))
	     (pentry (wl-string-assoc name wl-folder-petname-alist))
	     (old-petname (or (cdr pentry) ""))
	     (change)
	     petname)
	(unless name (error "No folder"))
	(if (and is-group
		 (not (eq (nth 1 (wl-folder-search-group-entity-by-name
				  name wl-folder-entity))
			  'access)))
	    (message "Can't set petname. please rename.")
	(setq petname (wl-fldmgr-read-string
		       (read-from-minibuffer "Petname: " old-petname)))
	(cond
	 ((string= petname "")
	  (when pentry
	    (setq wl-folder-petname-alist
		  (delete pentry wl-folder-petname-alist))
	    (setq change t)))
	 (t
	  (if (string= petname old-petname)
	      nil
	    (if (or (rassoc petname wl-folder-petname-alist)
		    (and is-group
			 (wl-string-assoc petname wl-folder-group-alist)))
		(message "%s: already exists" petname)
	      (wl-folder-append-petname name petname)
	      (setq change t)))))
	(when change
	  (let ((inhibit-read-only t)
		indent)
	    (goto-char (point-min))
	    (if is-group
		(progn
		  (if (string= old-petname "")
		      (setq old-petname name))
		  (while (wl-folder-buffer-search-group old-petname)
		    (beginning-of-line)
		    (and (looking-at "^\\([ ]*\\)")
			 (setq indent (elmo-match-buffer 1)))
		    (wl-fldmgr-delete-line)
		    (wl-folder-insert-entity
		     indent
		     (wl-folder-search-group-entity-by-name
		      name wl-folder-entity)
		     t)))
	      (while (wl-folder-buffer-search-entity name searchname)
		(save-excursion
		  (beginning-of-line)
		  (and (looking-at "^\\([ ]*\\)")
		       (setq indent (elmo-match-buffer 1)))
		  (wl-fldmgr-delete-line))
		(wl-folder-insert-entity indent name)))
	    (setq wl-fldmgr-modified t)
	    (set-buffer-modified-p nil)))))))

;;; Function for save folders
;;

(defun wl-fldmgr-insert-folders-buffer (indent entities &optional pet-entities)
  (let ((flist entities)
	name petname)
    (while flist
      (setq name (car flist))
      (cond ((stringp name)
	     (if (setq petname (cdr (wl-string-assoc name wl-folder-petname-alist)))
		 (wl-append pet-entities (list name)))
	     (insert indent name
		     (if petname
			 (concat "\t\"" petname "\"")
		       "")
		     "\n"))
	    ((consp name)
	     (let ((group (car name))
		   (type (nth 1 name)))
	       (cond ((eq type 'group)
		      (insert indent group "{\n")
		      (setq pet-entities
			    (wl-fldmgr-insert-folders-buffer
			     (concat indent wl-fldmgr-folders-indent)
			     (nth 2 name) pet-entities))
		      (insert indent "}\n"))
		     ((eq type 'access)
		      (insert indent group "/\n"))))))
      (setq flist (cdr flist))))
  pet-entities)

(defun wl-fldmgr-insert-petname-buffer (pet-entities)
  (let ((alist wl-folder-petname-alist))
    (while alist
      (if (wl-string-member (caar alist) pet-entities)
	  nil
	(insert "=\t" (caar alist) "\t\"" (cdar alist) "\"\n"))
      (setq alist (cdr alist)))))

(defun wl-fldmgr-delete-disused-petname ()
  (let ((alist wl-folder-petname-alist))
    (while alist
      (unless (wl-folder-search-entity-by-name (caar alist) wl-folder-entity)
	(setq wl-folder-petname-alist
	      (delete (car alist) wl-folder-petname-alist)))
      (setq alist (cdr alist)))))

(defun wl-fldmgr-save-folders ()
  (interactive)
  (let ((tmp-buf (get-buffer-create " *wl-fldmgr-tmp*"))
	save-petname-entities)
    (message "Saving folders...")
    (set-buffer tmp-buf)
    (erase-buffer)
    (insert wl-fldmgr-folders-header)
    (wl-fldmgr-delete-disused-petname)
    (setq save-petname-entities
	  (wl-fldmgr-insert-folders-buffer "" (nth 2 wl-folder-entity)))
    (insert "\n# petname definition (access group, folder in access group)\n")
    (wl-fldmgr-insert-petname-buffer save-petname-entities)
    (insert "\n# end of file.\n")
    (if (and wl-fldmgr-make-backup
	     (file-exists-p wl-folders-file))
	(rename-file wl-folders-file (concat wl-folders-file ".bak") t))
    (let ((output-coding-system (mime-charset-to-coding-system
				 wl-mime-charset)))
      (write-region
       (point-min)
       (point-max)
       wl-folders-file
       nil
       'no-msg)
      (set-file-modes wl-folders-file (+ (* 64 6) (* 8 0) 0))) ; chmod 0600
    (kill-buffer tmp-buf)
    (wl-fldmgr-save-access-list)
    (setq wl-fldmgr-modified nil)
    (message "Saving folders...done")))

(defun wl-fldmgr-save-access-list ()
  (let ((access-list wl-fldmgr-modified-access-list)
	entity)
    (while access-list
      (setq entity (wl-folder-search-group-entity-by-name
		    (car access-list) wl-folder-entity))
      (elmo-msgdb-flist-save
       (car access-list)
       (list
	(wl-folder-make-save-access-list (nth 2 entity))
	(wl-folder-make-save-access-list (nth 3 entity))))
      (setq access-list (cdr access-list)))
    (setq wl-fldmgr-modified-access-list nil)))

(require 'product)
(product-provide (provide 'wl-fldmgr) (require 'wl-version))

;;; wl-fldmgr.el ends here