1 ;;; wl-fldmgr.el -- Folder manager for Wanderlust.
3 ;; Copyright 1998,1999,2000 Masahiro MURATA <muse@ba2.so-net.ne.jp>
4 ;; Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Author: Masahiro MURATA <muse@ba2.so-net.ne.jp>
7 ;; Keywords: mail, net news
9 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
35 (require 'wl-highlight)
42 (defvar wl-fldmgr-modified nil)
43 (defvar wl-fldmgr-modified-access-list nil)
44 (defvar wl-fldmgr-cut-entity-list nil)
45 (defvar wl-fldmgr-entity-list nil)
46 (defvar wl-fldmgr-group-insert-opened nil)
48 (defconst wl-fldmgr-folders-header
51 # Folder definition file
52 # This file is generated automatically by %s.
55 " (product-string-1 'wl-version t)))
59 (defvar wl-fldmgr-mode-map nil)
60 (if wl-fldmgr-mode-map
62 (define-prefix-command 'wl-fldmgr-mode-map)
63 (define-key wl-fldmgr-mode-map "\C-s" 'wl-fldmgr-save-folders)
64 (define-key wl-fldmgr-mode-map "m" 'wl-fldmgr-make-multi)
65 (define-key wl-fldmgr-mode-map "g" 'wl-fldmgr-make-group)
66 (define-key wl-fldmgr-mode-map "A" 'wl-fldmgr-make-access-group)
67 (define-key wl-fldmgr-mode-map "f" 'wl-fldmgr-make-filter)
68 (define-key wl-fldmgr-mode-map "p" 'wl-fldmgr-set-petname)
69 (define-key wl-fldmgr-mode-map "a" 'wl-fldmgr-add)
70 (define-key wl-fldmgr-mode-map "d" 'wl-fldmgr-delete)
71 (define-key wl-fldmgr-mode-map "R" 'wl-fldmgr-rename)
72 (define-key wl-fldmgr-mode-map "c" 'wl-fldmgr-copy)
73 (define-key wl-fldmgr-mode-map "k" 'wl-fldmgr-cut)
74 (define-key wl-fldmgr-mode-map "W" 'wl-fldmgr-copy-region)
75 (define-key wl-fldmgr-mode-map "\C-w" 'wl-fldmgr-cut-region)
76 (define-key wl-fldmgr-mode-map "y" 'wl-fldmgr-yank)
77 (define-key wl-fldmgr-mode-map "s" 'wl-fldmgr-sort)
78 (define-key wl-fldmgr-mode-map "l" 'wl-fldmgr-access-display-normal)
79 (define-key wl-fldmgr-mode-map "L" 'wl-fldmgr-access-display-all)
80 (define-key wl-fldmgr-mode-map "q" 'wl-fldmgr-clear-cut-entity-list)
81 (define-key wl-fldmgr-mode-map "r" 'wl-fldmgr-reconst-entity-hashtb)
82 (define-key wl-fldmgr-mode-map "u" 'wl-fldmgr-unsubscribe)
83 (define-key wl-fldmgr-mode-map "U" 'wl-fldmgr-unsubscribe-region))
85 (add-hook 'wl-folder-mode-hook 'wl-fldmgr-init)
87 (defun wl-fldmgr-init ()
88 (setq wl-fldmgr-cut-entity-list nil)
89 (setq wl-fldmgr-modified nil)
90 (setq wl-fldmgr-modified-access-list nil))
92 (defun wl-fldmgr-exit ()
93 (when (and wl-fldmgr-modified
94 (or (not wl-interactive-save-folders)
95 (y-or-n-p "Folder view was modified. Save current folders? ")))
96 (wl-fldmgr-save-folders)))
98 ;;; Macro and misc Function
101 (defmacro wl-fldmgr-delete-line ()
102 (` (delete-region (save-excursion (beginning-of-line)
104 (save-excursion (end-of-line)
107 (defmacro wl-fldmgr-make-indent (level)
108 (` (concat " " (make-string (* 2 (, level)) ? ))))
110 (defmacro wl-fldmgr-get-entity-id (&optional entity)
111 (` (get-text-property (if (, entity)
117 (defmacro wl-fldmgr-assign-id (entity &optional id)
118 (` (let ((entity-id (or (, id) wl-folder-entity-id)))
119 (put-text-property 0 (length (, entity))
124 (defsubst wl-fldmgr-read-string (str)
125 (if (string-match "\n" str)
126 (error "Not supported name: %s" str)
129 (defsubst wl-fldmgr-add-modified-access-list (group)
130 (if (not (member group wl-fldmgr-modified-access-list))
131 (wl-append wl-fldmgr-modified-access-list (list group))))
133 (defsubst wl-fldmgr-delete-modified-access-list (group)
134 (if (member group wl-fldmgr-modified-access-list)
135 (setq wl-fldmgr-modified-access-list
136 (delete group wl-fldmgr-modified-access-list))))
138 (defsubst wl-fldmgr-add-group (group)
139 (or (assoc group wl-folder-group-alist)
140 (wl-append wl-folder-group-alist
142 wl-fldmgr-group-insert-opened)))))
144 (defsubst wl-fldmgr-delete-group (group)
145 (wl-fldmgr-delete-modified-access-list group)
146 (setq wl-folder-group-alist
147 (delete (assoc group wl-folder-group-alist)
148 wl-folder-group-alist)))
150 (defun wl-fldmgr-add-entity-hashtb (entities)
151 "Update `wl-folder-entity-hashtb', `wl-folder-newsgroups-hashtb'.
152 Return value is diffs '(new unread all)."
156 val entity entity-stack)
157 (setq wl-folder-newsgroups-hashtb
158 (or (wl-folder-create-newsgroups-hashtb entities t)
159 wl-folder-newsgroups-hashtb))
161 (setq entity (wl-pop entities))
164 (wl-fldmgr-add-group (car entity))
166 (wl-push entities entity-stack))
167 (setq entities (nth 2 entity)))
169 (if (not (setq val (wl-folder-get-entity-info entity)))
170 (wl-folder-set-entity-info entity nil)
171 (setq new-diff (+ new-diff (or (nth 0 val) 0)))
172 (setq unread-diff (+ unread-diff (or (nth 1 val) 0)))
173 (setq all-diff (+ all-diff (or (nth 2 val) 0))))))
175 (setq entities (wl-pop entity-stack))))
176 (setq unread-diff (+ unread-diff new-diff))
177 (list new-diff unread-diff all-diff)))
179 (defun wl-fldmgr-delete-entity-hashtb (entities &optional clear)
180 "Update `wl-folder-entity-hashtb'.
181 return value is diffs '(-new -unread -all)."
188 (setq entity (wl-pop entities))
191 (wl-fldmgr-delete-group (car entity))
193 (wl-push entities entity-stack))
194 (setq entities (nth 2 entity)))
196 (when (setq val (wl-folder-get-entity-info entity))
197 (setq new-diff (+ new-diff (or (nth 0 val) 0)))
198 (setq unread-diff (+ unread-diff (or (nth 1 val) 0)))
199 (setq all-diff (+ all-diff (or (nth 2 val) 0)))
200 (and clear (wl-folder-clear-entity-info entity)))))
202 (setq entities (wl-pop entity-stack))))
203 (setq unread-diff (+ unread-diff new-diff))
204 (list (- 0 new-diff) (- 0 unread-diff) (- 0 all-diff))))
207 ;; example: '(("Desktop" group) ("+ml" access) "+ml/wl")
209 (defun wl-fldmgr-get-path (entity target-entity &optional group-target)
210 (let* ((target-id (wl-fldmgr-get-entity-id target-entity))
211 (entities (list entity))
212 entity-stack result-path)
216 (setq entity (wl-pop entities))
219 (if (and (string= target-entity (car entity))
220 (eq target-id (wl-fldmgr-get-entity-id (car entity))))
222 (wl-push (if group-target
224 (list (car entity) (nth 1 entity)))
226 (wl-push (list (car entity) (nth 1 entity))
228 (wl-push entities entity-stack)
229 (setq entities (nth 2 entity)))
231 (if (and (string= target-entity entity)
232 (eq target-id (wl-fldmgr-get-entity-id entity)))
234 (wl-push entity result-path)))))
236 (while (and entity-stack
238 (setq result-path (cdr result-path))
239 (setq entities (wl-pop entity-stack)))))))))
241 ;; (defun wl-fldmgr-get-previous-entity (entity key-id)
242 ;; (cdr (wl-fldmgr-get-previous-entity-internal '(nil . nil) entity key-id)))
244 ;; (defun wl-fldmgr-get-previous-entity-internal (result entity key-id)
247 ;; (if (eq key-id (wl-fldmgr-get-entity-id entity))
249 ;; (cons nil (cons entity entity))))
251 ;; (if (eq key-id (wl-fldmgr-get-entity-id (car entity)))
253 ;; (setcar result (car entity))
254 ;; (let ((flist (nth 2 entity))
256 ;; (while (and flist (not found))
257 ;; (if (car (setq return
258 ;; (wl-fldmgr-get-previous-entity-internal
259 ;; result (car flist) key-id)))
261 ;; (setq result (cdr return))
262 ;; (setq flist (cdr flist)))
263 ;; (cons found result))))))
265 ;; path is get `wl-fldmgr-get-path-from-buffer'.
266 (defun wl-fldmgr-update-group (path diffs)
268 (while (and path (consp (car path)))
269 (if (string= (caar path) wl-folder-desktop-name) ; update desktop
271 (goto-char (point-min))
272 (wl-folder-update-diff-line diffs))
273 ;; goto the path line.
274 (goto-char (point-min))
275 (if (wl-folder-buffer-search-group
276 (wl-folder-get-petname (caar path)))
277 (wl-folder-update-diff-line diffs)))
278 (setq path (cdr path)))))
280 ;;; Function for wl-folder-entity
284 ;; (wl-delete-entity '(("Desktop") ("ML") "+ml/wl") '("+ml/wl") wl-folder-entity)
285 ;; (wl-delete-entity '(("Desktop") "ML") '("+inbox" "ML") wl-folder-entity)
286 ;; (wl-delete-entity '(("Desktop") "ML") nil wl-folder-entity)
288 (defun wl-delete-entity (key-path delete-list entity &optional clear)
289 (let (wl-fldmgr-entity-list)
290 (when (and (string= (caar key-path) (car entity))
291 (wl-delete-entity-sub (cdr key-path) delete-list entity clear))
292 ;; return value is non-nil (diffs)
293 (wl-fldmgr-delete-entity-hashtb wl-fldmgr-entity-list clear))))
295 (defun wl-delete-entity-sub (key-path delete-list entity clear)
296 (let ((flist (nth 2 entity))
300 ((consp key);; into group
301 (if (setq next (assoc (car key) flist))
302 (wl-delete-entity-sub (cdr key-path)
308 ((stringp key) ;; delete entities
309 (if (not delete-list)
310 (setq delete-list (list key)))
311 (let* ((group (car entity))
312 (access (eq (nth 1 entity) 'access))
313 (unsubscribes (and access (nth 3 entity)))
318 (setq key (car delete-list))
319 (cond ((member key flist);; entity
320 (setq flist (delete key flist))
322 (wl-push key wl-fldmgr-cut-entity-list))
323 (wl-append wl-fldmgr-entity-list (list key))
325 ((setq cut-entity (assoc key flist));; group
326 (setq flist (delete cut-entity flist))
328 (wl-push cut-entity wl-fldmgr-cut-entity-list))
329 (wl-append wl-fldmgr-entity-list (list cut-entity))
333 (message "%s not found" key)
338 (wl-append unsubscribes
339 (list (list (elmo-string key) 'access nil)))
340 (wl-append unsubscribes (list (elmo-string key)))))
341 (setq delete-list (cdr delete-list))))
343 (setcdr (cdr entity) (list flist unsubscribes))
345 (wl-fldmgr-add-modified-access-list group))
350 ;; (wl-add-entity '(("Desktop") ("ML") "ml/wl") '("+ml/new") wl-folder-entity 12)
351 ;; (wl-add-entity '(("Desktop") "ML") '("+ml/new") wl-folder-entity 10)
353 (defun wl-add-entity (key-path new entity prev-entity-id &optional errmes)
354 (when (string= (caar key-path) (car entity))
355 (let ((entities new))
357 (wl-folder-entity-assign-id
358 (pop entities) wl-folder-entity-id-name-hashtb t)))
359 (when (wl-add-entity-sub (cdr key-path) new entity errmes)
360 ;; return value is non-nil (diffs)
361 (wl-fldmgr-add-entity-hashtb new))))
363 (defun wl-add-entity-sub (key-path new entity &optional errmes)
364 (let ((flist (nth 2 entity))
368 ((consp (car key-path));; into group
369 (if (setq entry (assoc (caar key-path) flist))
370 (if (not (wl-add-entity-sub (cdr key-path)
374 (throw 'success nil))
375 (and errmes (message "%s not found" (caar key-path)))
376 (throw 'success nil)))
380 (access (eq (nth 1 entity) 'access))
381 (unsubscribes (and access (nth 3 entity))))
385 ((stringp (car new2)) ;; folder
387 ((wl-string-member (car new2) flist)
388 (and errmes (message "%s: already exists" (car new2)))
389 (throw 'success nil))
391 (not (wl-string-member (car new2) unsubscribes)))
392 (and errmes (message "%s: not access group folder" (car new2)))
393 (throw 'success nil))))
396 (not (wl-string-assoc (caar new2) unsubscribes)))
397 (and errmes (message "%s: can't insert access group"
399 (throw 'success nil))))
400 (setq new2 (cdr new2)))
403 ;; remove from unsubscribe
406 (if (consp (car new2))
408 (delq (wl-string-assoc (car (car new2)) unsubscribes)
410 (setq unsubscribes (delete (elmo-string (car new2))
412 (setq new2 (cdr new2)))
413 (setcdr (cddr entity) (list unsubscribes))
414 (wl-fldmgr-add-modified-access-list group))
415 (if (not key-path);; insert group top
417 (setcar (cddr entity) (append new flist))
418 (setcdr (cdr entity) (list new)))
422 (setq akey (car flist))
423 (cond ((consp akey);; group
424 (if (equal (car key-path) (car akey))
427 (if (equal (car key-path) akey)
429 (setq flist (cdr flist))))
430 (setcdr flist (append new (cdr flist)))
431 (and errmes (message "%s not found" (car key-path)))
432 (throw 'success nil)))))))
433 (throw 'success t))))
436 ;; (path indent-level (group . type) previous-entity-id target-entity)
437 ;; previous-entity-id is (id-name-alist-prev-id . entity-alist-prev-id)
439 ;; '((("Desktop" group) ("ML" group) "+ml/wl") '(3 2) ("ML" . group) nil "+ml/wl")
441 (defun wl-fldmgr-get-path-from-buffer (&optional prev)
442 (let ((indent-level 0)
444 folder-path group-type previous-entity entity)
448 ;;; (wl-folder-next-entity-skip-invalid t)
449 ;;; (and (setq previous-entity
450 ;;; (wl-fldmgr-get-previous-entity wl-folder-entity
451 ;;; (wl-fldmgr-get-entity-id)))
452 ;;; ;; change entity to id
453 ;;; (setq previous-entity
455 ;;; (and (car previous-entity)
456 ;;; (wl-fldmgr-get-entity-id (car previous-entity)))
457 ;;; (and (cdr previous-entity)
458 ;;; (wl-fldmgr-get-entity-id (cdr previous-entity))))))
459 (wl-folder-prev-entity-skip-invalid))
461 (looking-at wl-folder-group-regexp)
462 (string= (wl-match-buffer 2) "-"))
463 (setq group-target nil)
464 (if (and prev (bobp))
465 (error "Out of desktop group")))
466 (setq folder-path (wl-fldmgr-get-path wl-folder-entity
467 (wl-folder-get-entity-from-buffer)
468 ;;; (wl-fldmgr-get-entity-id)
470 (let ((fp folder-path))
474 (setq indent-level (1+ indent-level))
475 (setq group-type (cons (caar fp) (nth 1 (car fp)))))
476 (setq entity (car fp)))
478 (list folder-path indent-level group-type previous-entity entity))))
483 (defun wl-fldmgr-clear-cut-entity-list ()
485 (setq wl-fldmgr-cut-entity-list nil)
486 (message "Cleared cut entity list"))
488 (defun wl-fldmgr-reconst-entity-hashtb (&optional arg nomes)
490 (or nomes (message "Reconstructing entity alist..."))
492 (setq wl-folder-entity-id 0)
493 (wl-folder-entity-assign-id wl-folder-entity))
494 (setq wl-folder-entity-hashtb
495 (wl-folder-create-entity-hashtb
497 wl-folder-entity-hashtb
499 ;; reset property on buffer
501 (let ((inhibit-read-only t)
504 (wl-folder-insert-entity " " wl-folder-entity)
505 (goto-char cur-point)
506 (set-buffer-modified-p nil)))
507 (or nomes (message "Reconstructing entity alist...done")))
510 (defun wl-fldmgr-cut-region ()
512 (let* ((p1 (region-beginning))
527 name pre-indent indent)
531 (and (looking-at "^\\([ ]*\\)")
532 (setq pre-indent (wl-match-buffer 1)))
533 (while (< (point) to)
534 (and (looking-at "^\\([ ]*\\)")
535 (setq indent (wl-match-buffer 1)))
536 (cond ((= (length pre-indent) (length indent))
537 (setq pre-indent indent)
538 (setq count (1+ count))
539 (and (setq name (wl-folder-get-entity-from-buffer))
540 (wl-append cut-list (list name)))
542 ((< (length pre-indent) (length indent))
543 (wl-folder-goto-bottom-of-current-folder pre-indent)
546 (setq errmes "bad region")
548 (unless (eq (point) to)
549 (setq errmes "bad region")
552 (let ((count2 (length cut-list))
556 (wl-folder-next-entity-skip-invalid t)
557 (setq tmp (wl-fldmgr-get-path-from-buffer)))
558 (setq path (car tmp))
560 (wl-delete-entity path cut-list wl-folder-entity))
563 (setq ent (looking-at wl-folder-entity-regexp))
564 (if (not (wl-fldmgr-cut (and ent tmp)
565 (and ent (pop cut-list))))
567 (setq count (1- count))))
569 (wl-push count2 wl-fldmgr-cut-entity-list))
571 (wl-fldmgr-update-group path diffs))
575 (message "%s" errmes))))
577 (defun wl-fldmgr-cut (&optional tmp entity clear)
582 (inhibit-read-only t)
585 (message "Can't remove desktop group")
586 (or tmp (setq tmp (wl-fldmgr-get-path-from-buffer)))
587 (setq path (car tmp))
590 (wl-fldmgr-delete-line)) ;; unsubscribe or removed folder
593 (wl-delete-entity path nil wl-folder-entity clear)))
594 (setq wl-fldmgr-modified t)
596 (if (looking-at wl-folder-group-regexp)
598 (let (beg end indent opened)
599 (setq indent (wl-match-buffer 1))
600 (setq opened (wl-match-buffer 2))
601 (if (string= opened "+")
602 (wl-fldmgr-delete-line)
608 (wl-folder-goto-bottom-of-current-folder indent)
611 (delete-region beg end)))
613 (wl-fldmgr-delete-line))
615 (wl-fldmgr-update-group path diffs))
616 (set-buffer-modified-p nil))
620 (defun wl-fldmgr-copy-region ()
622 (let* ((p1 (region-beginning))
643 (setq errmes "can't copy desktop group")
645 (and (looking-at "^\\([ ]*\\)")
646 (setq pre-indent (wl-match-buffer 1)))
647 (while (< (point) to)
648 (and (looking-at "^\\([ ]*\\)")
649 (setq indent (wl-match-buffer 1)))
650 (if (looking-at wl-folder-group-regexp)
652 (setq errmes "can't copy group folder")
654 (cond ((= (length pre-indent) (length indent))
655 (if (setq name (wl-folder-get-entity-from-buffer))
657 (setq pre-indent indent)
658 (wl-push name cut-list)))
660 ((< (length pre-indent) (length indent))
661 (wl-folder-goto-bottom-of-current-folder pre-indent)
664 (setq errmes "bad region")
666 (unless (eq (point) to)
667 (setq errmes "bad region")
670 (setq cut-list (reverse cut-list))
672 (setq name (pop cut-list))
673 (unless (wl-fldmgr-copy name)
675 (setq count (1+ count)))
676 (wl-push count wl-fldmgr-cut-entity-list)
677 (message "Copy %s folders" count)
680 (message "%s" errmes))))
682 (defun wl-fldmgr-copy (&optional ename)
688 (looking-at wl-folder-group-regexp))
689 (message "Can't copy group folder")
690 (let* ((name (or ename (wl-folder-get-entity-from-buffer)))
691 (entity (elmo-string name)))
693 (if (member entity wl-fldmgr-cut-entity-list)
694 (setq wl-fldmgr-cut-entity-list
695 (delete entity wl-fldmgr-cut-entity-list)))
696 (wl-push entity wl-fldmgr-cut-entity-list)
698 (message "Copy: %s" name))
702 (defun wl-fldmgr-yank ()
707 (message "Can't insert in the out of desktop group")
708 (let ((inhibit-read-only t)
709 (top (car wl-fldmgr-cut-entity-list))
710 tmp indent path count new
711 access new-list diffs)
713 (message "No cut buffer")
714 (setq tmp (wl-fldmgr-get-path-from-buffer t))
715 (setq path (car tmp))
716 (setq indent (wl-fldmgr-make-indent (nth 1 tmp)))
718 (setq count (pop wl-fldmgr-cut-entity-list))
722 (cut-list wl-fldmgr-cut-entity-list))
723 ;; check insert entity
725 (setq new (car cut-list))
726 (wl-push new new-list)
727 (when (consp new);; group
730 (message "Can't insert group in access")
732 ((wl-string-assoc (car new) wl-folder-group-alist)
733 (message "%s: group already exists" (car new))
735 (setq cut-list (cdr cut-list))
736 (setq count (1- count))))
739 path new-list wl-folder-entity (nth 3 tmp) t)))
742 (setq new (pop wl-fldmgr-cut-entity-list))
744 (wl-folder-insert-entity indent new)
745 (setq wl-fldmgr-modified t))
746 (setq count (1- count)))
747 (wl-fldmgr-update-group path diffs)
748 (set-buffer-modified-p nil))
750 (wl-push count wl-fldmgr-cut-entity-list)))))))
752 (defvar wl-fldmgr-add-completion-hashtb (make-vector 7 0))
754 ;(defun wl-fldmgr-add-completion-all-completions (string)
760 ; (if (string-match (symbol-name atom) string)
761 ; (throw 'found (symbol-value atom)))))
762 ; wl-fldmgr-add-completion-hashtb)))
764 ; (if (string-match "\\.$"
765 ; (car (elmo-network-get-spec
766 ; string nil nil nil nil)))
767 ; (substring string 0 (match-beginning 0))
768 ; (concat string nil))))
770 ; (setq table (elmo-folder-list-subfolders (wl-folder-get-elmo-folder
773 ; (or (/= (length table) 1)
774 ; (elmo-folder-exists-p (wl-folder-get-elmo-folder
777 ; (if (string-match "\\.[^\\.]+$" string)
778 ; (substring string 0 (match-beginning 0))
779 ; (char-to-string (aref string 0)))
780 ; table (elmo-folder-list-subfolders
781 ; (wl-folder-get-elmo-folder pattern))))
782 ; (setq pattern (concat "^" (regexp-quote pattern)))
783 ; (unless (intern-soft pattern wl-fldmgr-add-completion-hashtb)
784 ; (set (intern pattern wl-fldmgr-add-completion-hashtb) table))
787 ;(defun wl-fldmgr-add-completion-subr (string predicate flag)
789 ; (if (string= string "")
790 ; (mapcar (function (lambda (spec)
791 ; (list (char-to-string (car spec)))))
793 ; (when (assq (aref string 0) elmo-spec-alist)
796 ; (condition-case nil
797 ; (wl-fldmgr-add-completion-all-completions string)
800 ; (try-completion string table predicate)
801 ; (if (eq flag 'lambda)
802 ; (eq t (try-completion string table predicate))
804 ; (all-completions string table predicate))))))
806 (defun wl-fldmgr-add (&optional name)
811 (inhibit-read-only t)
812 (wl-folder-completion-function
813 (if wl-fldmgr-add-complete-with-current-folder-list
814 (function wl-fldmgr-add-completion-subr)))
815 tmp indent path diffs)
817 (message "Can't insert in the out of desktop group")
818 (setq tmp (wl-fldmgr-get-path-from-buffer t))
819 (setq path (car tmp))
820 (setq indent (wl-fldmgr-make-indent (nth 1 tmp)))
822 (setq name (wl-fldmgr-read-string
823 (wl-summary-read-folder wl-default-folder "to add"))))
824 ;; maybe add elmo-plugged-alist.
825 (elmo-folder-set-plugged (wl-folder-get-elmo-folder name) wl-plugged t)
828 path (list name) wl-folder-entity (nth 3 tmp) t))
829 (wl-folder-insert-entity indent name)
830 (wl-fldmgr-update-group path diffs)
831 (setq wl-fldmgr-modified t)
832 (set-buffer-modified-p nil)
836 (defun wl-fldmgr-delete ()
840 (if (looking-at wl-folder-group-regexp)
841 (error "Can't delete group folder"))
842 (let* ((inhibit-read-only t)
843 (tmp (wl-fldmgr-get-path-from-buffer))
844 (entity (elmo-string (nth 4 tmp)))
845 (folder (wl-folder-get-elmo-folder entity))
846 (msgs (and (elmo-folder-exists-p folder)
847 (elmo-folder-list-messages folder))))
848 (when (yes-or-no-p (format "%sDo you really want to delete \"%s\"? "
849 (if (> (length msgs) 0)
850 (format "%d msg(s) exists. " (length msgs))
853 (elmo-folder-delete folder)
854 (wl-fldmgr-cut tmp nil t)))))
856 (defun wl-fldmgr-rename ()
861 (message "Can't rename desktop group")
863 ((looking-at wl-folder-group-regexp) ;; group
864 (let* ((indent (wl-match-buffer 1))
865 (old-group (wl-folder-get-realname (wl-match-buffer 3)))
866 (group-entity (wl-folder-search-group-entity-by-name
867 old-group wl-folder-entity))
869 (if (eq (nth 1 group-entity) 'access)
870 (message "%s: can't rename access group folder" old-group)
871 (setq group (wl-fldmgr-read-string
872 (read-from-minibuffer "Rename: " old-group)))
873 (if (string-match "/$" group)
874 (message "Remove tail slash.")
876 ((or (string= group "")
877 (string= old-group group))
880 (if (wl-string-assoc group wl-folder-group-alist)
881 (message "%s: group already exists" group)
882 (let ((inhibit-read-only t)
883 (id (wl-fldmgr-get-entity-id
884 (car group-entity))))
885 (wl-fldmgr-assign-id group id)
886 (setcar group-entity group)
887 (setcar (wl-string-assoc old-group wl-folder-group-alist)
889 ;;; (setcdr (assq id wl-folder-entity-id-name-alist) group)
890 (wl-folder-set-id-name id group)
891 (wl-fldmgr-delete-line)
892 (wl-folder-insert-entity
895 (setq wl-fldmgr-modified t)
896 (set-buffer-modified-p nil)))))))))
898 (let* ((tmp (wl-fldmgr-get-path-from-buffer))
899 (old-folder (nth 4 tmp))
901 (if (eq (cdr (nth 2 tmp)) 'access)
902 (error "Can't rename access folder"))
904 (wl-fldmgr-read-string
905 (wl-summary-read-folder old-folder "to rename" t t old-folder)))
906 (if (or (wl-folder-entity-exists-p new-folder)
907 (file-exists-p (elmo-folder-msgdb-path
908 (wl-folder-get-elmo-folder new-folder))))
909 (error "Already exists folder: %s" new-folder))
910 (elmo-folder-rename (wl-folder-get-elmo-folder old-folder)
912 (wl-folder-set-entity-info
914 (wl-folder-get-entity-info old-folder))
915 (when (wl-fldmgr-cut tmp nil t)
916 (wl-fldmgr-add new-folder))))))))
918 (defun wl-fldmgr-make-access-group ()
920 (wl-fldmgr-make-group nil t))
922 (defun wl-fldmgr-make-group (&optional group-name access)
927 (message "Can't insert in the out of desktop group")
928 (let ((inhibit-read-only t)
930 group tmp indent path new prev-id flist diffs)
931 (setq tmp (wl-fldmgr-get-path-from-buffer t))
932 (setq path (car tmp))
933 (setq indent (wl-fldmgr-make-indent (nth 1 tmp)))
934 (setq prev-id (nth 3 tmp))
935 (if (eq (cdr (nth 2 tmp)) 'access)
936 (message "Can't insert access group")
937 (setq group (or group-name
938 (wl-fldmgr-read-string
939 (read-from-minibuffer
940 (if access "Access Type Group: " "Group: ")))))
941 (when (or access (string-match "[\t ]*/$" group))
942 (setq group (if access group
943 (substring group 0 (match-beginning 0))))
945 (setq flist (wl-create-access-folder-entity group)))
946 (if (string= group "")
948 (if (wl-string-assoc group wl-folder-group-alist)
949 (message "%s: group already exists" group)
950 (setq new (append (list group type) flist))
951 (when (setq diffs (wl-add-entity path
955 (wl-folder-insert-entity indent new)
956 (wl-fldmgr-update-group path diffs)
957 (setq wl-fldmgr-modified t)
958 (set-buffer-modified-p nil)))))))))
960 (defun wl-fldmgr-make-multi ()
962 (if (not wl-fldmgr-cut-entity-list)
963 (message "No cut buffer")
964 (let ((cut-entity wl-fldmgr-cut-entity-list)
972 ((numberp (car cut-entity))
974 ((consp (car cut-entity))
975 (message "Can't make multi included group folder")
978 (let ((folder (wl-folder-get-elmo-folder
981 (if (eq (elmo-folder-type-internal folder) 'multi)
983 (substring (car cut-entity) 1)))
986 (or multi-fld (car cut-entity))
990 (setq cut-entity (cdr cut-entity)))
993 (setq new-entity (concat "*" new-entity))
994 (wl-fldmgr-add new-entity)))))
996 (defun wl-fldmgr-make-filter ()
1000 (if (looking-at wl-folder-group-regexp)
1001 (message "This folder is group")
1002 (let ((tmp (wl-fldmgr-get-path-from-buffer))
1004 (if (eq (cdr (nth 2 tmp)) 'access)
1005 (message "Can't change access group")
1006 (setq entity (nth 4 tmp))
1007 (unless entity (error "No folder"))
1008 (wl-fldmgr-add (concat "/"
1009 (elmo-read-search-condition
1010 wl-fldmgr-make-filter-default)
1013 (defun wl-fldmgr-sort ()
1017 (let ((inhibit-read-only t)
1018 entity flist indent opened)
1019 (when (looking-at wl-folder-group-regexp)
1020 (setq indent (wl-match-buffer 1))
1021 (setq opened (wl-match-buffer 2))
1022 (setq entity (wl-folder-search-group-entity-by-name
1023 (wl-folder-get-realname (wl-match-buffer 3))
1025 (message "Sorting...")
1026 (setq flist (sort (nth 2 entity) wl-fldmgr-sort-function))
1027 (setcar (cddr entity) flist)
1028 (wl-fldmgr-add-modified-access-list (car entity))
1029 (setq wl-fldmgr-modified t)
1031 (when (string= opened "-")
1038 (wl-folder-goto-bottom-of-current-folder indent)
1041 (delete-region beg end)
1042 (wl-folder-insert-entity indent entity)))
1043 ;;; (wl-fldmgr-reconst-entity-hashtb t t)
1044 (message "Sorting...done")
1045 (set-buffer-modified-p nil)))))
1047 (defun wl-fldmgr-sort-standard (x y)
1048 (cond ((and (consp x) (not (consp y)))
1049 wl-fldmgr-sort-group-first)
1050 ((and (not (consp x)) (consp y))
1051 (not wl-fldmgr-sort-group-first))
1052 ((and (consp x) (consp y))
1053 (string-lessp (car x) (car y)))
1055 (string-lessp x y))))
1057 (defun wl-fldmgr-subscribe-region ()
1059 (wl-fldmgr-unsubscribe-region -1))
1061 (defun wl-fldmgr-unsubscribe-region (&optional arg)
1063 (let* ((p1 (region-beginning))
1077 (while (< (point) to)
1078 (setq count (1+ count))
1081 (message "Unsubscribe region...")
1082 (while (and (> count 0)
1083 (wl-fldmgr-unsubscribe (or arg 1) t))
1084 (setq count (1- count)))
1085 (message "Unsubscribe region...done")))
1087 (defun wl-fldmgr-subscribe ()
1089 (wl-fldmgr-unsubscribe -1))
1091 (defun wl-fldmgr-unsubscribe (&optional arg force)
1093 (let ((type (and arg (prefix-numeric-value arg)))
1097 (let ((inhibit-read-only t)
1101 ((looking-at (format "^[ ]*%s\\[[+-]\\]\\(.*\\)" wl-folder-unsubscribe-mark))
1102 (if (and type (> type 0))
1104 (setq folder (list (wl-match-buffer 1) 'access nil))
1105 (if (wl-string-assoc (car folder) wl-folder-group-alist)
1106 (message "%s: group already exists" (car folder))
1107 (wl-fldmgr-delete-line)
1108 (when (wl-fldmgr-add folder)
1109 (wl-folder-maybe-load-folder-list folder)
1110 ;;; (wl-folder-search-group-entity-by-name (car folder)
1111 ;;; wl-folder-entity)
1113 ((looking-at (format "^[ ]*%s\\(.*\\)" wl-folder-unsubscribe-mark))
1114 (if (and type (> type 0))
1116 (setq folder (wl-match-buffer 1))
1117 (wl-fldmgr-delete-line)
1118 (when (wl-fldmgr-add folder)
1121 (if (and type (< type 0))
1123 (setq is-group (looking-at wl-folder-group-regexp))
1124 (setq tmp (wl-fldmgr-get-path-from-buffer))
1125 (setq indent (wl-fldmgr-make-indent (nth 1 tmp)))
1126 (if (eq (cdr (nth 2 tmp)) 'access)
1127 (when (wl-fldmgr-cut tmp)
1128 (pop wl-fldmgr-cut-entity-list) ;; don't leave cut-list
1130 (insert indent wl-folder-unsubscribe-mark
1132 (concat "[+]" (nth 4 tmp))
1135 (save-excursion (forward-line -1)
1136 (wl-highlight-folder-current-line))
1137 (remove-text-properties beg (point) '(wl-folder-entity-id))
1138 (setq execed t))))))
1139 (set-buffer-modified-p nil)))
1140 (if (or force execed)
1145 (defun wl-fldmgr-access-display-normal (&optional arg)
1147 (wl-fldmgr-access-display-all (not arg)))
1149 (defun wl-fldmgr-access-display-all (&optional arg)
1151 (let ((id (save-excursion
1152 (wl-folder-prev-entity-skip-invalid t)
1153 (wl-fldmgr-get-entity-id))))
1156 (let ((inhibit-read-only t)
1157 entity indent opened
1159 (when (not (looking-at wl-folder-group-regexp))
1160 (wl-folder-goto-top-of-current-folder)
1161 (looking-at wl-folder-group-regexp))
1162 (setq indent (wl-match-buffer 1))
1163 (setq opened (wl-match-buffer 2))
1164 (setq entity (wl-folder-search-group-entity-by-name
1165 (wl-folder-get-realname (wl-match-buffer 3))
1167 (when (eq (nth 1 entity) 'access)
1169 (if (string= opened "-")
1176 (wl-folder-goto-bottom-of-current-folder indent)
1179 (delete-region beg end))
1180 (wl-fldmgr-delete-line)
1181 (setcdr (assoc (car entity) wl-folder-group-alist) t));; set open
1182 (wl-folder-insert-entity indent entity))
1184 (setq unsubscribes (nth 3 entity))
1188 (insert indent " " wl-folder-unsubscribe-mark
1189 (if (consp (car unsubscribes))
1190 (concat "[+]" (caar unsubscribes))
1193 (remove-text-properties beg (point) '(wl-folder-entity-id))
1194 (save-excursion (forward-line -1)
1195 (wl-highlight-folder-current-line))
1196 (setq unsubscribes (cdr unsubscribes))))
1197 (set-buffer-modified-p nil))))
1198 (wl-folder-move-path id)))
1200 (defun wl-fldmgr-set-petname ()
1204 (let* ((is-group (looking-at wl-folder-group-regexp))
1205 (name (wl-folder-get-entity-from-buffer))
1206 (searchname (wl-folder-get-petname name))
1207 (pentry (wl-string-assoc name wl-folder-petname-alist))
1208 (old-petname (or (cdr pentry) ""))
1211 (unless name (error "No folder"))
1213 (not (eq (nth 1 (wl-folder-search-group-entity-by-name
1214 name wl-folder-entity))
1216 (message "Can't set petname. please rename.")
1217 (setq petname (wl-fldmgr-read-string
1218 (read-from-minibuffer "Petname: " old-petname)))
1220 ((string= petname "")
1222 (setq wl-folder-petname-alist
1223 (delete pentry wl-folder-petname-alist))
1226 (if (string= petname old-petname)
1228 (if (or (rassoc petname wl-folder-petname-alist)
1229 (wl-string-assoc petname wl-folder-group-alist))
1230 (message "%s: already exists" petname)
1231 (wl-folder-append-petname name petname)
1234 (let ((inhibit-read-only t)
1236 (goto-char (point-min))
1239 (if (string= old-petname "")
1240 (setq old-petname name))
1241 (while (wl-folder-buffer-search-group old-petname)
1243 (and (looking-at "^\\([ ]*\\)")
1244 (setq indent (wl-match-buffer 1)))
1245 (wl-fldmgr-delete-line)
1246 (wl-folder-insert-entity
1248 (wl-folder-search-group-entity-by-name
1249 name wl-folder-entity)
1251 (while (wl-folder-buffer-search-entity name searchname)
1254 (and (looking-at "^\\([ ]*\\)")
1255 (setq indent (wl-match-buffer 1)))
1256 (wl-fldmgr-delete-line))
1257 (wl-folder-insert-entity indent name)))
1258 (setq wl-fldmgr-modified t)
1259 (set-buffer-modified-p nil)))))))
1261 ;;; Function for save folders
1264 (defun wl-fldmgr-insert-folders-buffer (indent entities &optional pet-entities)
1265 (let ((flist entities)
1268 (setq name (car flist))
1269 (cond ((stringp name)
1270 (if (setq petname (cdr (wl-string-assoc name wl-folder-petname-alist)))
1271 (wl-append pet-entities (list name)))
1274 (concat "\t\"" petname "\"")
1278 (let ((group (wl-folder-get-realname (car name)))
1279 (type (nth 1 name)))
1280 (if (not (string= group (car name))) ; petname.
1281 (wl-append pet-entities (list (car name))))
1282 (cond ((eq type 'group)
1283 (insert indent group "{\n")
1285 (wl-fldmgr-insert-folders-buffer
1286 (concat indent wl-fldmgr-folders-indent)
1287 (nth 2 name) pet-entities))
1288 (insert indent "}\n"))
1290 (insert indent group "/\n"))))))
1291 (setq flist (cdr flist))))
1294 (defun wl-fldmgr-insert-petname-buffer (pet-entities)
1295 (let ((alist wl-folder-petname-alist))
1297 (if (wl-string-member (caar alist) pet-entities)
1299 (insert "=\t" (caar alist) "\t\"" (cdar alist) "\"\n"))
1300 (setq alist (cdr alist)))))
1302 (defun wl-fldmgr-delete-disused-petname ()
1303 (let ((alist wl-folder-petname-alist))
1305 (unless (wl-folder-search-entity-by-name (caar alist) wl-folder-entity)
1306 (setq wl-folder-petname-alist
1307 (delete (car alist) wl-folder-petname-alist)))
1308 (setq alist (cdr alist)))))
1310 (defun wl-fldmgr-save-folders ()
1312 (let ((tmp-buf (get-buffer-create " *wl-fldmgr-tmp*"))
1313 (access-list wl-fldmgr-modified-access-list)
1315 save-petname-entities)
1316 (message "Saving folders...")
1317 (set-buffer tmp-buf)
1319 (insert wl-fldmgr-folders-header)
1320 (wl-fldmgr-delete-disused-petname)
1321 (setq save-petname-entities
1322 (wl-fldmgr-insert-folders-buffer "" (nth 2 wl-folder-entity)))
1323 (insert "\n# petname definition (group, folder in access group)\n")
1324 (wl-fldmgr-insert-petname-buffer save-petname-entities)
1325 (insert "\n# end of file.\n")
1326 (if (and wl-fldmgr-make-backup
1327 (file-exists-p wl-folders-file))
1328 (rename-file wl-folders-file (concat wl-folders-file ".bak") t))
1329 (let ((output-coding-system (mime-charset-to-coding-system
1337 (set-file-modes wl-folders-file (+ (* 64 6) (* 8 0) 0))) ; chmod 0600
1338 (kill-buffer tmp-buf)
1340 (setq entity (wl-folder-search-group-entity-by-name
1341 (car access-list) wl-folder-entity))
1342 (elmo-msgdb-flist-save
1345 (wl-folder-make-save-access-list (nth 2 entity))
1346 (wl-folder-make-save-access-list (nth 3 entity))))
1347 (setq access-list (cdr access-list)))
1348 (setq wl-fldmgr-modified nil)
1349 (setq wl-fldmgr-modified-access-list nil)
1350 (message "Saving folders...done")))
1353 (product-provide (provide 'wl-fldmgr) (require 'wl-version))
1355 ;;; wl-fldmgr.el ends here