* wl-addrmgr.el (wl-addrmgr-replace-field): Use `point-at-bol'.
[elisp/wanderlust.git] / wl / wl-addrmgr.el
index d4e8480..72e6c96 100644 (file)
@@ -34,6 +34,7 @@
 
 (require 'wl-address)
 (require 'wl-draft)
+(eval-when-compile (require 'cl))
 
 ;; Variables
 (defgroup wl-addrmgr nil
@@ -169,7 +170,34 @@ See info under Wanderlust for full documentation.
    (lambda (addr)
      (nth 1 (std11-extract-address-components addr)))
    (wl-parse-addresses
-    (mapconcat 'identity (elmo-multiple-fields-body-list (list field)) ","))))
+    (mapconcat
+     'identity
+     (elmo-multiple-fields-body-list (list field) mail-header-separator)
+     ","))))
+
+(defun wl-addrmgr-pickup-entry-list (buffer)
+  "Return a list of address entiry from BUFFER."
+  (when buffer
+    (with-current-buffer buffer
+      (mapcar
+       (lambda (addr)
+        (let ((structure (std11-extract-address-components addr)))
+          (list (cadr structure)
+                (or (car structure) "")
+                (or (car structure) ""))))
+       (wl-parse-addresses
+       (mapconcat
+        'identity
+        (elmo-multiple-fields-body-list '("to" "cc" "bcc")
+                                        mail-header-separator)
+        ","))))))
+
+(defun wl-addrmgr-merge-entries (base-list append-list)
+  "Return a merged list of address entiry."
+  (dolist (entry append-list)
+    (unless (assoc (car entry) base-list)
+      (setq base-list (nconc base-list (list entry)))))
+  base-list)
 
 ;;;###autoload
 (defun wl-addrmgr ()
@@ -201,7 +229,9 @@ See info under Wanderlust for full documentation.
     (unless wl-addrmgr-sort-order
       (setq wl-addrmgr-sort-order wl-addrmgr-default-sort-order))
     (setq wl-addrmgr-draft-buffer buffer)
-    (setq wl-addrmgr-list (wl-addrmgr-list))
+    (setq wl-addrmgr-list
+         (wl-addrmgr-merge-entries (wl-addrmgr-list)
+                                   (wl-addrmgr-pickup-entry-list buffer)))
     (wl-addrmgr-draw already-list)
     (setq wl-addrmgr-unknown-list already-list)
     (wl-addrmgr-goto-top)))
@@ -362,13 +392,16 @@ Return nil if no ADDRESS exists."
       (forward-char 4)))))
 
 (defun wl-addrmgr-quit-yes ()
-  (if (and wl-addrmgr-draft-buffer
-          (buffer-live-p wl-addrmgr-draft-buffer)
-          (null (get-buffer-window wl-addrmgr-draft-buffer)))
-      (switch-to-buffer wl-addrmgr-draft-buffer)
-    (unless (one-window-p)
-      (delete-window)))
-  (kill-buffer wl-addrmgr-buffer-name))
+  (let ((draft-buffer wl-addrmgr-draft-buffer))
+    (if (and draft-buffer
+            (buffer-live-p draft-buffer)
+            (null (get-buffer-window draft-buffer 'visible)))
+       (switch-to-buffer draft-buffer)
+      (unless (one-window-p)
+       (delete-window)))
+    (kill-buffer wl-addrmgr-buffer-name)
+    (if (and draft-buffer (not (one-window-p)))
+       (switch-to-buffer-other-window draft-buffer))))
 
 (defun wl-addrmgr-quit ()
   "Exit from electric reference mode without inserting reference."
@@ -406,8 +439,7 @@ Return nil if no ADDRESS exists."
   (let ((entry (wl-addrmgr-address-entry))
        buffer-read-only)
     (save-excursion
-      (beginning-of-line)
-      (delete-region (point) (progn (end-of-line)(point)))
+      (delete-region (point-at-bol) (point-at-eol))
       (wl-addrmgr-insert-line entry))
     (set-buffer-modified-p nil)
     (wl-addrmgr-next)))
@@ -519,20 +551,15 @@ Return nil if no ADDRESS exists."
 ;;; Operations.
 
 (defun wl-addrmgr-address-entry ()
-  (save-excursion
-    (end-of-line)
-    (get-text-property (previous-single-property-change
-                       (point) 'wl-addrmgr-entry nil
-                       (progn
-                         (beginning-of-line)
-                         (point)))
-                      'wl-addrmgr-entry)))
+  (get-text-property (previous-single-property-change
+                     (point-at-eol) 'wl-addrmgr-entry nil
+                     (point-at-bol))
+                    'wl-addrmgr-entry))
 
 (defun wl-addrmgr-mark-write (&optional mark)
   "Set MARK to the current address entry."
   (save-excursion
-    (end-of-line)
-    (unless (< (count-lines (point-min) (point)) 3)
+    (unless (< (count-lines (point-min) (point-at-eol)) 3)
       (let ((buffer-read-only nil) beg end)
        (beginning-of-line)
        (delete-char 4)
@@ -541,16 +568,12 @@ Return nil if no ADDRESS exists."
                  (cc "Cc: ")
                  (bcc "Bcc:")
                  (t "    ")))
-       (insert (make-string (- 4 (current-column)) ? ))
-       (beginning-of-line)
-       (setq beg (point))
-       (setq end (progn (end-of-line)
-                        (point)))
+       (insert (make-string (- 4 (current-column)) (string-to-char " ")))
+       (setq beg (point-at-bol))
+       (setq end (point-at-eol))
        (put-text-property beg end 'face nil)
        (wl-highlight-message beg end nil))
-      (set-buffer-modified-p nil)
-      (beginning-of-line)
-      (forward-char 4))))
+      (set-buffer-modified-p nil))))
 
 (defun wl-addrmgr-apply ()
   (interactive)
@@ -577,23 +600,35 @@ Return nil if no ADDRESS exists."
        (setq realname (nth 2 (wl-addrmgr-address-entry)))
        (cond
         ((string= mark "To:")
-         (setq to-list (cons (if full (concat
-                                       (wl-address-quote-specials realname)
-                                       " <" addr">")
-                               addr)
-                             to-list)))
+         (setq to-list (cons
+                        (if (and full
+                                 (not (or (string= realname "")
+                                          (string-match ".*:.*;$" addr))))
+                            (concat
+                             (wl-address-quote-specials realname)
+                             " <" addr">")
+                          addr)
+                        to-list)))
         ((string= mark "Cc:")
-         (setq cc-list (cons (if full (concat
-                                       (wl-address-quote-specials realname)
-                                       " <" addr">")
-                               addr)
-                             cc-list)))
+         (setq cc-list (cons
+                        (if (and full
+                                 (not (or (string= realname "")
+                                          (string-match ".*:.*;$" addr))))
+                            (concat
+                             (wl-address-quote-specials realname)
+                             " <" addr">")
+                          addr)
+                        cc-list)))
         ((string= mark "Bcc:")
-         (setq bcc-list (cons (if full (concat
-                                        (wl-address-quote-specials realname)
-                                        " <" addr">")
-                                addr)
-                              bcc-list)))))
+         (setq bcc-list (cons
+                         (if (and full
+                                  (not (or (string= realname "")
+                                           (string-match ".*:.*;$" addr))))
+                             (concat
+                              (wl-address-quote-specials realname)
+                              " <" addr">")
+                           addr)
+                         bcc-list)))))
       (list to-list cc-list bcc-list))))
 
 (defun wl-addrmgr-apply-exec (rcpt)
@@ -607,8 +642,8 @@ Return nil if no ADDRESS exists."
     (when (or (null wl-addrmgr-draft-buffer)
              (not (buffer-live-p wl-addrmgr-draft-buffer)))
       (setq wl-addrmgr-draft-buffer (save-window-excursion
-                                  (wl-draft)
-                                  (current-buffer))))
+                                     (call-interactively 'wl-draft)
+                                     (current-buffer))))
     (with-current-buffer wl-addrmgr-draft-buffer
       (setq from (std11-field-body "From"))
       (if from
@@ -627,12 +662,10 @@ Return nil if no ADDRESS exists."
        (while (re-search-forward (concat "^" (regexp-quote field) ":") nil t)
          ;; delete field
          (progn
-           (save-excursion
-             (beginning-of-line)
-             (setq beg (point)))
+           (setq beg (point-at-bol))
            (re-search-forward "^[^ \t]" nil 'move)
-           (beginning-of-line)
-           (delete-region beg (point))))
+           (delete-region beg (point-at-bol))
+           (beginning-of-line)))
        (when content
          ;; add field to top.
          (goto-char (point-min))