1 ;;; wl-addrmgr.el --- Address manager for Wanderlust.
3 ;; Copyright (C) 2001 Kitamoto Tsuyoshi <tsuyoshi.kitamoto@city.sapporo.jp>
4 ;; Copyright (C) 2001 Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Author: Kitamoto Tsuyoshi <tsuyoshi.kitamoto@city.sapporo.jp>
7 ;; Yuuichi Teranishi <teranisi@gohome.org>
8 ;; Keywords: mail, net news
10 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; Edit To:, Cc:, Bcc: fields interactively from E-Mail address list
30 ;; on ~/.address file.
37 (eval-when-compile (require 'cl
))
40 (defgroup wl-addrmgr nil
41 "Wanderlust Address manager."
45 (defcustom wl-addrmgr-buffer-lines
10
46 "*Buffer lines for ADDRMGR buffer for draft."
50 (defcustom wl-addrmgr-default-sort-key
'realname
51 "Default element for sort."
52 :type
'(choice '(address realname petname none
))
55 (defcustom wl-addrmgr-default-sort-order
'ascending
56 "Default element for sort."
57 :type
'(choice '(ascending descending
))
60 (defcustom wl-addrmgr-realname-width
17
65 (defcustom wl-addrmgr-petname-width
10
70 (defcustom wl-addrmgr-line-width
78
71 "Width for each line."
75 (defcustom wl-addrmgr-realname-face
'wl-highlight-summary-normal-face
80 (defcustom wl-addrmgr-petname-face
'wl-highlight-summary-unread-face
85 (defcustom wl-addrmgr-address-face
'wl-highlight-summary-new-face
90 (defcustom wl-addrmgr-default-method
'local
91 "Default access method for address entries."
95 (defvar wl-addrmgr-buffer-name
"Address")
96 (defvar wl-addrmgr-mode-map nil
)
97 (defvar wl-addrmgr-method-list
'(local))
99 ;; buffer local variable.
100 (defvar wl-addrmgr-draft-buffer nil
)
101 (defvar wl-addrmgr-unknown-list nil
)
102 (defvar wl-addrmgr-sort-key nil
)
103 (defvar wl-addrmgr-sort-order nil
)
104 (defvar wl-addrmgr-method nil
)
105 (defvar wl-addrmgr-list nil
)
106 (defvar wl-addrmgr-method-name nil
)
108 (make-variable-buffer-local 'wl-addrmgr-draft-buffer
)
109 (make-variable-buffer-local 'wl-addrmgr-unknown-list
)
110 (make-variable-buffer-local 'wl-addrmgr-sort-key
)
111 (make-variable-buffer-local 'wl-addrmgr-sort-order
)
112 (make-variable-buffer-local 'wl-addrmgr-method
)
113 (make-variable-buffer-local 'wl-addrmgr-list
)
114 (make-variable-buffer-local 'wl-addrmgr-method-name
)
118 (if wl-addrmgr-mode-map
120 (setq wl-addrmgr-mode-map
(make-sparse-keymap))
121 (define-key wl-addrmgr-mode-map
"<" 'wl-addrmgr-goto-top
)
122 (define-key wl-addrmgr-mode-map
">" 'wl-addrmgr-goto-bottom
)
123 (define-key wl-addrmgr-mode-map
"t" 'wl-addrmgr-mark-set-to
)
124 (define-key wl-addrmgr-mode-map
"b" 'wl-addrmgr-mark-set-bcc
)
125 (define-key wl-addrmgr-mode-map
"c" 'wl-addrmgr-mark-set-cc
)
126 (define-key wl-addrmgr-mode-map
"u" 'wl-addrmgr-unmark
)
127 (define-key wl-addrmgr-mode-map
"x" 'wl-addrmgr-apply
)
129 (define-key wl-addrmgr-mode-map
"\C-c\C-c" 'wl-addrmgr-apply
)
131 (define-key wl-addrmgr-mode-map
"n" 'wl-addrmgr-next
)
132 (define-key wl-addrmgr-mode-map
"j" 'wl-addrmgr-next
)
133 (define-key wl-addrmgr-mode-map
"k" 'wl-addrmgr-prev
)
134 (define-key wl-addrmgr-mode-map
"p" 'wl-addrmgr-prev
)
135 (define-key wl-addrmgr-mode-map
[down] 'wl-addrmgr-next)
136 (define-key wl-addrmgr-mode-map [up] 'wl-addrmgr-prev)
138 (define-key wl-addrmgr-mode-map "s" 'wl-addrmgr-sort)
140 (define-key wl-addrmgr-mode-map "a" 'wl-addrmgr-add)
141 (define-key wl-addrmgr-mode-map "d" 'wl-addrmgr-delete)
142 (define-key wl-addrmgr-mode-map "e" 'wl-addrmgr-edit)
143 (define-key wl-addrmgr-mode-map "\n" 'wl-addrmgr-edit)
144 (define-key wl-addrmgr-mode-map "\r" 'wl-addrmgr-edit)
146 (define-key wl-addrmgr-mode-map "q" 'wl-addrmgr-quit)
147 (define-key wl-addrmgr-mode-map "\C-c\C-k" 'wl-addrmgr-quit)
149 (define-key wl-addrmgr-mode-map "C" 'wl-addrmgr-change-method)
151 (define-key wl-addrmgr-mode-map "Z" 'wl-addrmgr-reload)
152 (define-key wl-addrmgr-mode-map "\C-c\C-l" 'wl-addrmgr-redraw))
154 (defun wl-addrmgr-mode ()
155 "Major mode for Wanderlust address management.
156 See info under Wanderlust for full documentation.
158 \\{wl-addrmgr-mode-map}"
159 (kill-all-local-variables)
160 (setq mode-name "Address"
161 major-mode 'wl-addrmgr-mode)
162 (wl-mode-line-buffer-identification
163 '("Wanderlust: Address (" wl-addrmgr-method-name ")"))
164 (use-local-map wl-addrmgr-mode-map)
165 (setq buffer-read-only t))
167 (defun wl-addrmgr-address-entry-list (field)
168 "Return address list."
171 (nth 1 (std11-extract-address-components addr)))
175 (elmo-multiple-fields-body-list (list field) mail-header-separator)
178 (defun wl-addrmgr-pickup-entry-list (buffer)
179 "Return a list of address entiry from BUFFER."
181 (with-current-buffer buffer
184 (let ((structure (std11-extract-address-components addr)))
185 (list (cadr structure)
186 (or (car structure) "")
187 (or (car structure) ""))))
191 (elmo-multiple-fields-body-list '("to" "cc" "bcc")
192 mail-header-separator)
195 (defun wl-addrmgr-merge-entries (base-list append-list)
196 "Return a merged list of address entiry."
197 (dolist (entry append-list)
198 (unless (assoc (car entry) base-list)
199 (setq base-list (nconc base-list (list entry)))))
204 "Start an Address manager."
206 (let ((buffer (if (eq major-mode 'wl-draft-mode) (current-buffer)))
207 (already-list (list (cons 'to (wl-addrmgr-address-entry-list "to"))
208 (cons 'cc (wl-addrmgr-address-entry-list "cc"))
209 (cons 'bcc (wl-addrmgr-address-entry-list "bcc")))))
210 (if (eq major-mode 'wl-draft-mode)
211 (if (get-buffer-window wl-addrmgr-buffer-name)
213 (split-window (selected-window)
214 (- (window-height (selected-window))
215 wl-addrmgr-buffer-lines))
216 (select-window (next-window))
217 ;; Non-nil means display-buffer should make new windows.
218 (let ((pop-up-windows nil))
220 (get-buffer-create wl-addrmgr-buffer-name))))
221 (switch-to-buffer (get-buffer-create wl-addrmgr-buffer-name)))
222 (set-buffer wl-addrmgr-buffer-name)
224 (unless wl-addrmgr-method
225 (setq wl-addrmgr-method wl-addrmgr-default-method
226 wl-addrmgr-method-name (symbol-name wl-addrmgr-default-method)))
227 (unless wl-addrmgr-sort-key
228 (setq wl-addrmgr-sort-key wl-addrmgr-default-sort-key))
229 (unless wl-addrmgr-sort-order
230 (setq wl-addrmgr-sort-order wl-addrmgr-default-sort-order))
231 (setq wl-addrmgr-draft-buffer buffer)
232 (setq wl-addrmgr-list
233 (wl-addrmgr-merge-entries (wl-addrmgr-list)
234 (wl-addrmgr-pickup-entry-list buffer)))
235 (wl-addrmgr-draw already-list)
236 (setq wl-addrmgr-unknown-list already-list)
237 (wl-addrmgr-goto-top)))
239 (defun wl-addrmgr-goto-top ()
241 (goto-char (point-min))
247 (defun wl-addrmgr-goto-bottom ()
249 (goto-char (point-max))
253 (defun wl-addrmgr-reload ()
254 "Reload addresses entries."
256 (setq wl-addrmgr-list (wl-addrmgr-list 'reload))
259 (defun wl-addrmgr-redraw ()
260 "Redraw addresses entries."
262 (let ((rcpt (wl-addrmgr-mark-check)))
263 (wl-addrmgr-draw (list (cons 'to (nth 0 rcpt))
264 (cons 'cc (nth 1 rcpt))
265 (cons 'bcc (nth 2 rcpt)))))
266 (wl-addrmgr-goto-top))
268 (defun wl-addrmgr-sort-list (key list order)
276 (setq sorted (sort list `(lambda (a b) (string< (nth ,pos a)
278 (if (eq order 'descending)
283 (defun wl-addrmgr-insert-line (entry)
284 (let ((real (nth 2 entry))
290 (setq real (wl-set-string-width wl-addrmgr-realname-width real))
291 (put-text-property 0 (length real) 'face
292 wl-addrmgr-realname-face
294 (setq pet (wl-set-string-width wl-addrmgr-petname-width pet))
295 (put-text-property 0 (length pet) 'face
296 wl-addrmgr-petname-face
298 (setq addr (copy-sequence addr))
299 (put-text-property 0 (length addr) 'face
300 wl-addrmgr-address-face
304 (- wl-addrmgr-line-width 4)
305 (concat real " " pet " " addr)))
306 (put-text-property beg (point) 'wl-addrmgr-entry entry)))
308 (defun wl-addrmgr-search-forward-address (address)
309 "Search forward from point for ADDRESS.
310 Return nil if no ADDRESS exists."
314 (if (string= address (car (wl-addrmgr-address-entry)))
321 (defun wl-addrmgr-draw (already-list)
322 "Show recipients mail addresses."
324 (let ((buffer-read-only nil)
325 list field addrs beg real pet addr)
327 (goto-char (point-min))
330 (wl-set-string-width wl-addrmgr-realname-width
333 (wl-set-string-width wl-addrmgr-petname-width
337 (make-string wl-addrmgr-realname-width ?-)
339 (make-string wl-addrmgr-petname-width ?-)
341 (unless wl-addrmgr-list (insert "\n"))
342 (dolist (entry (wl-addrmgr-sort-list wl-addrmgr-sort-key
343 (copy-sequence wl-addrmgr-list)
344 wl-addrmgr-sort-order))
346 (wl-addrmgr-insert-line entry))
347 (set-buffer-modified-p nil)
349 (setq list (car already-list)
353 (goto-char (point-min))
354 (when (wl-addrmgr-search-forward-address (car addrs))
355 (wl-addrmgr-mark-write field)
356 (setcdr list (delq (car addrs) (cdr list))))
357 (setq addrs (cdr addrs)))
358 (setq already-list (cdr already-list))))))
360 (defun wl-addrmgr-next ()
361 "Move cursor next line."
364 (let ((current (count-lines (point-min) (point)))
368 (when (setq first (next-single-property-change (point) 'wl-addrmgr-entry
378 (defun wl-addrmgr-prev ()
379 "Move cursor prev line."
381 (let ((current (count-lines (point-min) (point))))
387 (goto-char (point-min))
394 (defun wl-addrmgr-quit-yes ()
395 (let ((draft-buffer wl-addrmgr-draft-buffer))
396 (if (and draft-buffer
397 (buffer-live-p draft-buffer)
398 (null (get-buffer-window draft-buffer 'visible)))
399 (switch-to-buffer draft-buffer)
400 (unless (one-window-p)
402 (kill-buffer wl-addrmgr-buffer-name)
403 (if (and draft-buffer (not (one-window-p)))
404 (switch-to-buffer-other-window draft-buffer))))
406 (defun wl-addrmgr-quit ()
407 "Exit from electric reference mode without inserting reference."
409 (let ((rcpt (wl-addrmgr-mark-check)))
413 (when (y-or-n-p "There is marked address. Quit wl-addrmgr really? ")
414 (wl-addrmgr-quit-yes))
415 (wl-addrmgr-quit-yes)))
418 (defun wl-addrmgr-mark-set-to ()
421 (wl-addrmgr-mark-write 'to)
424 (defun wl-addrmgr-mark-set-cc ()
427 (wl-addrmgr-mark-write 'cc)
430 (defun wl-addrmgr-mark-set-bcc ()
433 (wl-addrmgr-mark-write 'bcc)
436 (defun wl-addrmgr-unmark ()
439 (let ((entry (wl-addrmgr-address-entry))
443 (delete-region (point) (progn (end-of-line)(point)))
444 (wl-addrmgr-insert-line entry))
445 (set-buffer-modified-p nil)
448 (defun wl-addrmgr-sort ()
449 "Sort address entry."
451 (setq wl-addrmgr-sort-key (intern
453 (format "Sort By (%s): "
454 (symbol-name wl-addrmgr-sort-key))
455 '(("address")("realname")("petname")("none"))
457 (symbol-name wl-addrmgr-sort-key))))
458 (if (eq wl-addrmgr-sort-key 'none)
460 (setq wl-addrmgr-sort-order (intern
462 (format "Sort Order (%s): "
463 (symbol-name wl-addrmgr-sort-order))
464 '(("ascending") ("descending"))
466 (symbol-name wl-addrmgr-sort-order))))
467 (wl-addrmgr-redraw)))
470 (defun wl-addrmgr-method-call (method &rest args)
471 (apply (intern (concat "wl-addrmgr-"
472 (symbol-name wl-addrmgr-method)
473 "-" (symbol-name method)))
476 (defun wl-addrmgr-change-method ()
478 (setq wl-addrmgr-method (intern
479 (setq wl-addrmgr-method-name
481 (format "Method (%s): "
482 (symbol-name wl-addrmgr-method))
483 (mapcar (lambda (method)
484 (list (symbol-name method)))
485 wl-addrmgr-method-list)
487 (symbol-name wl-addrmgr-method)))))
490 (defun wl-addrmgr-list (&optional reload)
491 "List address entries."
492 (wl-addrmgr-method-call 'list reload))
494 (defun wl-addrmgr-add ()
497 (let ((entry (wl-addrmgr-method-call 'add)))
498 (if (eq wl-addrmgr-sort-key 'none)
500 (setq wl-addrmgr-list (cons entry wl-addrmgr-list))
502 (message "Added `%s'." (wl-string (car entry)))))
504 (defun wl-addrmgr-delete ()
505 "Delete address entry."
507 (let ((addr (wl-string (car (wl-addrmgr-address-entry))))
510 (y-or-n-p (format "Delete '%s'? " addr)))
511 (setq lines (count-lines (point-min) (point)))
512 (wl-addrmgr-method-call 'delete addr)
513 (setq wl-addrmgr-list (delq (assoc addr wl-addrmgr-list)
516 (forward-line (- lines 2))
517 (message "Deleted `%s'." addr))))
519 (defun wl-addrmgr-edit ()
520 "Edit address entry."
522 (let ((orig (wl-addrmgr-address-entry))
524 (setq entry (wl-addrmgr-method-call 'edit (wl-string (car orig))))
525 (setq lines (count-lines (point-min) (point)))
526 (if (eq wl-addrmgr-sort-key 'none)
528 (setq wl-addrmgr-list (delq (assoc (car orig) wl-addrmgr-list)
530 wl-addrmgr-list (cons entry wl-addrmgr-list))
532 (forward-line (- lines 1))
533 (message "Modified `%s'." (wl-string (car entry)))))
535 ;;; local address book implementation.
536 (defun wl-addrmgr-local-list (reload)
537 (if (or (null wl-address-list) reload)
539 (copy-sequence wl-address-list))
541 (defun wl-addrmgr-local-add ()
542 (wl-address-add-or-change nil nil 'addr-too))
544 (defun wl-addrmgr-local-edit (address)
545 (wl-address-add-or-change address nil 'addr-too))
547 (defun wl-addrmgr-local-delete (address)
548 (wl-address-delete address))
550 ;;; LDAP implementation (Implement Me)
554 (defun wl-addrmgr-address-entry ()
557 (get-text-property (previous-single-property-change
558 (point) 'wl-addrmgr-entry nil
564 (defun wl-addrmgr-mark-write (&optional mark)
565 "Set MARK to the current address entry."
568 (unless (< (count-lines (point-min) (point)) 3)
569 (let ((buffer-read-only nil) beg end)
577 (insert (make-string (- 4 (current-column)) ? ))
580 (setq end (progn (end-of-line)
582 (put-text-property beg end 'face nil)
583 (wl-highlight-message beg end nil))
584 (set-buffer-modified-p nil)
588 (defun wl-addrmgr-apply ()
590 (let ((rcpt (wl-addrmgr-mark-check 'full)))
591 (when (or (or (nth 0 rcpt)
594 (or (cdr (assq 'to wl-addrmgr-unknown-list))
595 (cdr (assq 'cc wl-addrmgr-unknown-list))
596 (cdr (assq 'bcc wl-addrmgr-unknown-list))))
597 (wl-addrmgr-apply-exec (wl-addrmgr-mark-check 'full)))
598 (wl-addrmgr-quit-yes)))
600 (defun wl-addrmgr-mark-check (&optional full)
601 "Return list of recipients (TO CC BCC)."
602 (save-excursion ; save cursor POINT
603 (goto-char (point-min))
605 (let (to-list cc-list bcc-list mark addr realname)
606 (while (and (not (eobp))
607 (re-search-forward "^\\([^ ]+:\\) " nil t))
608 (setq mark (match-string 1))
609 (setq addr (car (wl-addrmgr-address-entry)))
610 (setq realname (nth 2 (wl-addrmgr-address-entry)))
612 ((string= mark "To:")
615 (not (or (string= realname "")
616 (string-match ".*:.*;$" addr))))
618 (wl-address-quote-specials realname)
622 ((string= mark "Cc:")
625 (not (or (string= realname "")
626 (string-match ".*:.*;$" addr))))
628 (wl-address-quote-specials realname)
632 ((string= mark "Bcc:")
635 (not (or (string= realname "")
636 (string-match ".*:.*;$" addr))))
638 (wl-address-quote-specials realname)
642 (list to-list cc-list bcc-list))))
644 (defun wl-addrmgr-apply-exec (rcpt)
645 (let ((to (nconc (nth 0 rcpt) (cdr (assq 'to wl-addrmgr-unknown-list))))
646 (cc (nconc (nth 1 rcpt) (cdr (assq 'cc wl-addrmgr-unknown-list))))
647 (bcc (nconc (nth 2 rcpt) (cdr (assq 'bcc wl-addrmgr-unknown-list))))
649 (setq clist (list (cons "Bcc" (if bcc (mapconcat 'identity bcc ",\n\t")))
650 (cons "Cc" (if cc (mapconcat 'identity cc ",\n\t")))
651 (cons "To" (if to (mapconcat 'identity to ",\n\t")))))
652 (when (or (null wl-addrmgr-draft-buffer)
653 (not (buffer-live-p wl-addrmgr-draft-buffer)))
654 (setq wl-addrmgr-draft-buffer (save-window-excursion
655 (call-interactively 'wl-draft)
657 (with-current-buffer wl-addrmgr-draft-buffer
658 (setq from (std11-field-body "From"))
660 (setq clist (append clist (list (cons "From" from)))))
661 (wl-addrmgr-mark-exec-sub clist))))
663 (defun wl-addrmgr-replace-field (field content)
664 "Insert FIELD with CONTENT to the top of the header fields."
667 (let ((case-fold-search t)
668 (inhibit-read-only t) ;; added by teranisi.
670 (std11-narrow-to-header mail-header-separator)
671 (goto-char (point-min))
672 (while (re-search-forward (concat "^" (regexp-quote field) ":") nil t)
678 (re-search-forward "^[^ \t]" nil 'move)
680 (delete-region beg (point))))
683 (goto-char (point-min))
684 (insert (concat field ": " content "\n")))))))
686 (defun wl-addrmgr-mark-exec-sub (list)
688 (wl-addrmgr-replace-field (car pair) (cdr pair)))
689 ;; from wl-template.el
691 (if wl-highlight-body-too
692 (let ((beg (point-min))
694 (put-text-property beg end 'face nil)
695 (wl-highlight-message beg end t))))
698 (product-provide (provide 'wl-addrmgr) (require 'wl-version))
700 ;;; wl-addrmgr.el ends here