* wl-summary.el (wl-summary-write-current-folder): Call wl-draft
[elisp/wanderlust.git] / wl / wl-summary.el
index 645d932..f724f55 100644 (file)
     "----"
     ("Writing Messages"
      ["Write a message" wl-summary-write t]
+     ["Write for current folder" wl-summary-write-current-folder t]
      ["Reply" wl-summary-reply t]
      ["Reply with citation" wl-summary-reply-with-citation t]
      ["Forward" wl-summary-forward t])
   (define-key wl-summary-mode-map "-"    'wl-summary-prev-line-content)
   (define-key wl-summary-mode-map "\e\r" 'wl-summary-prev-line-content)
   (define-key wl-summary-mode-map "g"    'wl-summary-goto-folder)
+  (define-key wl-summary-mode-map "G"    'wl-summary-goto-folder-sticky)
   (define-key wl-summary-mode-map "c"    'wl-summary-mark-as-read-all)
 ;  (define-key wl-summary-mode-map "D"    'wl-summary-drop-unsync)
 
   (define-key wl-summary-mode-map "e"     'wl-summary-save)
   (define-key wl-summary-mode-map "\C-c\C-o" 'wl-jump-to-draft-buffer)
   (define-key wl-summary-mode-map "\C-c\C-a" 'wl-addrmgr)
+  (define-key wl-summary-mode-map "\C-c\C-p" 'wl-summary-next-buffer)
+  (define-key wl-summary-mode-map "\C-c\C-n" 'wl-summary-previous-buffer)
   (define-key wl-summary-mode-map "H"    'wl-summary-redisplay-all-header)
   (define-key wl-summary-mode-map "M"    'wl-summary-redisplay-no-mime)
   (define-key wl-summary-mode-map "B"    'wl-summary-burst)
   (define-key wl-summary-mode-map "\M-t" 'wl-toggle-plugged)
   (define-key wl-summary-mode-map "\C-t" 'wl-plugged-change)
   ;;
+  (define-key wl-summary-mode-map "\C-x\C-s" 'wl-summary-save-status)
   (wl-summary-setup-mouse)
   (easy-menu-define
    wl-summary-mode-menu
     (if (eq major-mode 'wl-summary-mode)
        (setq wl-summary-buffer-new-count new
              wl-summary-buffer-unread-count unread))
-    (+ new unread)))
+    (cons new unread)))
 
 (defun wl-summary-message-string (&optional use-cache)
   "Return full body string of current message.
@@ -595,8 +600,8 @@ If ARG is non-nil, Supersedes message"
       (wl-summary-supersedes-message)
     (if (string= (wl-summary-buffer-folder-name) wl-draft-folder)
        (if (wl-summary-message-number)
-           (unwind-protect
-               (wl-draft-reedit (wl-summary-message-number))
+           (progn
+             (wl-draft-reedit (wl-summary-message-number))
              (if (wl-message-news-p)
                  (mail-position-on-field "Newsgroups")
                (mail-position-on-field "To"))
@@ -1006,6 +1011,16 @@ Entering Folder mode calls the value of `wl-summary-mode-hook'."
                 (wl-summary-thread-modified-p)))
        (wl-summary-save-view-cache))))
 
+(defun wl-summary-save-status ()
+  "Save summary view and msgdb."
+  (interactive)
+  (if (interactive-p) (message "Saving summary status..."))
+  (wl-summary-save-view)
+  (elmo-folder-commit wl-summary-buffer-elmo-folder)
+  (elmo-folder-check wl-summary-buffer-elmo-folder)
+  (if wl-use-scoring (wl-score-save))
+  (if (interactive-p) (message "Saving summary status...done.")))
+
 (defun wl-summary-force-exit ()
   "Exit current summary.  Buffer is deleted even the buffer is sticky."
   (interactive)
@@ -1022,7 +1037,8 @@ Entering Folder mode calls the value of `wl-summary-mode-hook'."
     (run-hooks 'wl-summary-exit-pre-hook)
     (if wl-summary-buffer-exit-function
        (funcall wl-summary-buffer-exit-function)
-      (wl-summary-cleanup-temp-marks sticky)
+      (if (or force-exit (not sticky))
+         (wl-summary-cleanup-temp-marks sticky))
       (unwind-protect
          ;; save summary status
          (progn
@@ -1116,12 +1132,15 @@ Entering Folder mode calls the value of `wl-summary-mode-hook'."
                 wl-use-scoring)
             (wl-summary-rescan)
             (and msg (wl-summary-jump-to-msg msg))))
+         ((string= range "cache-status")
+          (let ((msg (wl-summary-message-number)))
+            (wl-summary-resume-cache-status)
+            (and msg (wl-summary-jump-to-msg msg))))
          ((or (string-match "last:" range)
               (string-match "first:" range))
-          (wl-summary-goto-folder-subr
-           (wl-folder-get-elmo-folder (concat "/" range "/"
-                                              (elmo-folder-name-internal
-                                               folder)))
+          (wl-summary-goto-folder-subr (concat "/" range "/"
+                                               (elmo-folder-name-internal
+                                                folder))
            'force-update nil nil t))
          (t
           (wl-summary-sync-update unset-cursor
@@ -1161,8 +1180,8 @@ Entering Folder mode calls the value of `wl-summary-mode-hook'."
   ;; returns nil if there's no change.
   (if (elmo-get-hash-val (downcase the-email) wl-address-petname-hash)
       (let (char)
-       (message (format "'%s' already exists. (e)dit/(d)elete/(c)ancel?"
-                        the-email))
+       (message "'%s' already exists. (e)dit/(d)elete/(c)ancel?"
+                the-email)
        (while (not (or (eq (setq char (read-char)) ?\r)
                        (eq char ?\n)
                        (eq char ? )
@@ -2170,9 +2189,10 @@ If ARG is non-nil, checking is omitted."
       (wl-folder-set-folder-updated
        (elmo-folder-name-internal folder)
        (list 0
-            (wl-summary-count-unread
-             (elmo-msgdb-get-mark-alist
-              (elmo-folder-msgdb folder)))
+            (let ((pair (wl-summary-count-unread
+                         (elmo-msgdb-get-mark-alist
+                          (elmo-folder-msgdb folder)))))
+              (+ (car pair) (cdr pair)))
             (elmo-folder-messages folder)))
       (wl-summary-update-modeline)
       (wl-summary-buffer-number-column-detect t)
@@ -2349,7 +2369,11 @@ If ARG, without confirm."
 
 (defun wl-summary-goto-folder (&optional arg)
   (interactive "P")
-  (wl-summary-goto-folder-subr nil nil nil arg t))
+  (wl-summary-goto-folder-subr nil nil nil nil t nil arg))
+
+(defun wl-summary-goto-folder-sticky ()
+  (interactive)
+  (wl-summary-goto-folder-subr nil nil nil t t))
 
 (defun wl-summary-goto-last-visited-folder ()
   (interactive)
@@ -2483,7 +2507,8 @@ If ARG, without confirm."
                                           folder)))))
 
 (defun wl-summary-goto-folder-subr (&optional name scan-type other-window
-                                             sticky interactive scoring)
+                                             sticky interactive scoring
+                                             force-exit)
   "Display target folder on summary."
   (interactive)
   (let* ((keep-cursor (memq this-command
@@ -2501,9 +2526,12 @@ If ARG, without confirm."
               (eq major-mode 'wl-summary-mode)) ; called in summary.
       (setq wl-summary-last-visited-folder (wl-summary-buffer-folder-name))
       (run-hooks 'wl-summary-exit-pre-hook)
-      (wl-summary-cleanup-temp-marks (wl-summary-sticky-p))
+      (if (or force-exit (not (wl-summary-sticky-p)))
+         (wl-summary-cleanup-temp-marks (wl-summary-sticky-p)))
       (wl-summary-save-view)
-      (elmo-folder-commit wl-summary-buffer-elmo-folder))
+      (elmo-folder-commit wl-summary-buffer-elmo-folder)
+      (if (and (wl-summary-sticky-p) force-exit)
+         (kill-buffer (current-buffer))))
     (setq buf (wl-summary-get-buffer-create (elmo-folder-name-internal folder)
                                            sticky))
     (setq reuse-buf
@@ -2597,6 +2625,11 @@ If ARG, without confirm."
                       ;; wl-auto-select-first is non-nil and
                       ;; unreadp is non-nil but not important
                       (setq retval 'disp-msg))
+                     ((and wl-auto-prefetch-first
+                           (wl-summary-auto-select-msg-p unreadp))
+                      ;; wl-auto-select-first is non-nil and
+                      ;; unreadp is non-nil but not important
+                      (setq retval 'prefetch-msg))
                      ((not (wl-summary-auto-select-msg-p unreadp))
                       ;; unreadp is nil or important
                       (setq retval 'more-next))))
@@ -2622,6 +2655,13 @@ If ARG, without confirm."
                (wl-highlight-summary (point-min) (point-max))))
          (if (eq retval 'disp-msg)
              (wl-summary-redisplay))
+         (if (eq retval 'prefetch-msg)
+             (wl-message-buffer-prefetch
+              folder
+              (wl-summary-message-number)
+              wl-message-buffer-prefetch-depth
+              (current-buffer)
+              wl-summary-buffer-mime-charset))
          (if mes (message "%s" mes))
          (if (and interactive wl-summary-recenter)
              (recenter (/ (- (window-height) 2) 2))))))
@@ -3662,33 +3702,44 @@ If optional argument NUMBER is specified, mark message specified by NUMBER."
     (wl-summary-target-mark-region beg end)))
 
 (defun wl-summary-target-mark-msgs (msgs)
-  (while msgs
-    (if (eq wl-summary-buffer-view 'thread)
-       (wl-thread-jump-to-msg (car msgs))
-      (wl-summary-jump-to-msg (car msgs)))
-    (wl-summary-target-mark (wl-summary-message-number))
-    (setq msgs (cdr msgs))))
+  "Return the number of marked messages."
+  (let ((i 0) num)
+    (while msgs
+      (if (eq wl-summary-buffer-view 'thread)
+         (wl-thread-jump-to-msg (car msgs))
+       (wl-summary-jump-to-msg (car msgs)))
+      (setq num (wl-summary-message-number))
+      (when (eq num (car msgs))
+       (wl-summary-target-mark num)
+       (setq i (1+ i)))
+      (setq msgs (cdr msgs)))
+    i))
 
 (defun wl-summary-pick (&optional from-list delete-marks)
   (interactive)
-  (let* ((condition (car (elmo-parse-search-condition
-                         (elmo-read-search-condition
-                          wl-summary-pick-field-default))))
-        (result (elmo-folder-search wl-summary-buffer-elmo-folder
-                                    condition
-                                    from-list)))
-    (if delete-marks
-      (let ((mlist wl-summary-buffer-target-mark-list))
-       (while mlist
-         (when (wl-summary-jump-to-msg (car mlist))
-           (wl-summary-unmark))
-         (setq mlist (cdr mlist)))
-       (setq wl-summary-buffer-target-mark-list nil)))
-    (if result
-       (progn
-         (wl-summary-target-mark-msgs result)
-         (message "%d message(s) are picked." (length result)))
-      (message "No message was picked."))))
+  (save-excursion
+    (let* ((condition (car (elmo-parse-search-condition
+                           (elmo-read-search-condition
+                            wl-summary-pick-field-default))))
+          (result (elmo-folder-search wl-summary-buffer-elmo-folder
+                                      condition
+                                      from-list))
+          num)
+      (if delete-marks
+         (let ((mlist wl-summary-buffer-target-mark-list))
+           (while mlist
+             (when (wl-summary-jump-to-msg (car mlist))
+               (wl-summary-unmark))
+             (setq mlist (cdr mlist)))
+           (setq wl-summary-buffer-target-mark-list nil)))
+      (if (and result
+              (setq num (wl-summary-target-mark-msgs result))
+              (> num 0))
+         (if (= num (length result))
+             (message "%d message(s) are picked." num)
+           (message "%d(%d) message(s) are picked." num
+                    (- (length result) num)))
+       (message "No message was picked.")))))
 
 (defun wl-summary-unvirtual ()
   "Exit from current virtual folder."
@@ -3715,19 +3766,26 @@ If ARG, exit virtual folder."
                                         (wl-summary-buffer-folder-name))
                                 'update nil nil t)))
 
-(defun wl-summary-delete-all-temp-marks ()
+(defun wl-summary-delete-all-temp-marks (&optional no-msg)
+  "Erase all temp marks from buffer."
   (interactive)
-  (save-excursion
-    (goto-char (point-min))
-    (message "Unmarking...")
-    (while (not (eobp))
-      (wl-summary-unmark)
-      (forward-line))
-    (message "Unmarking...done")
-    (setq wl-summary-buffer-target-mark-list nil)
-    (setq wl-summary-buffer-delete-list nil)
-    (setq wl-summary-buffer-refile-list nil)
-    (setq wl-summary-buffer-copy-list nil)))
+  (when (or wl-summary-buffer-target-mark-list
+           wl-summary-buffer-delete-list
+           wl-summary-buffer-refile-list
+           wl-summary-buffer-copy-list)
+    (save-excursion
+      (goto-char (point-min))
+      (unless no-msg
+       (message "Unmarking..."))
+      (while (not (eobp))
+       (wl-summary-unmark)
+       (forward-line))
+      (unless no-msg
+       (message "Unmarking...done"))
+      (setq wl-summary-buffer-target-mark-list nil)
+      (setq wl-summary-buffer-delete-list nil)
+      (setq wl-summary-buffer-refile-list nil)
+      (setq wl-summary-buffer-copy-list nil))))
 
 (defun wl-summary-delete-mark (number)
   "Delete temporary mark of the message specified by NUMBER."
@@ -3865,6 +3923,28 @@ If ARG, exit virtual folder."
                (delq (car mlist) wl-summary-buffer-target-mark-list))
          (setq mlist (cdr mlist)))))))
 
+(defun wl-summary-next-buffer ()
+  "Switch to next summary buffer."
+  (interactive)
+  (let ((buffers (sort (wl-collect-summary)
+                      (lambda (buffer1 buffer2)
+                        (string-lessp (buffer-name buffer1)
+                                      (buffer-name buffer2))))))
+    (switch-to-buffer
+     (or (cadr (memq (current-buffer) buffers))
+        (car buffers)))))
+
+(defun wl-summary-previous-buffer ()
+  "Switch to previous summary buffer."
+  (interactive)
+  (let ((buffers (sort (wl-collect-summary)
+                      (lambda (buffer1 buffer2)
+                        (not (string-lessp (buffer-name buffer1)
+                                           (buffer-name buffer2)))))))
+    (switch-to-buffer
+     (or (cadr (memq (current-buffer) buffers))
+        (car buffers)))))
+
 (defun wl-summary-target-mark-copy ()
   (interactive)
   (wl-summary-target-mark-refile-subr "copy"))
@@ -4147,7 +4227,7 @@ If ARG, exit virtual folder."
                    (elmo-msgdb-mark-set mark-alist
                                         number
                                         wl-summary-important-mark))
-             (if (elmo-file-cache-exists-p message-id)
+             (if (eq (elmo-file-cache-exists-p message-id) 'entire)
                  (elmo-folder-mark-as-read folder (list number))
                ;; Force cache message.
                (elmo-message-encache folder number 'read))
@@ -4373,6 +4453,10 @@ If ARG, exit virtual folder."
           (cache (expand-file-name wl-summary-cache-file dir))
           (view (expand-file-name wl-summary-view-file dir))
           (save-view wl-summary-buffer-view)
+          (mark-list (copy-sequence wl-summary-buffer-target-mark-list))
+          (refile-list (copy-sequence wl-summary-buffer-refile-list))
+          (copy-list (copy-sequence wl-summary-buffer-copy-list))
+          (delete-list (copy-sequence wl-summary-buffer-delete-list))
           (tmp-buffer (get-buffer-create " *wl-summary-save-view-cache*"))
           (charset wl-summary-buffer-mime-charset))
       (if (file-directory-p dir)
@@ -4388,6 +4472,11 @@ If ARG, exit virtual folder."
              (copy-to-buffer tmp-buffer (point-min) (point-max))
              (with-current-buffer tmp-buffer
                (widen)
+               (setq wl-summary-buffer-target-mark-list mark-list
+                     wl-summary-buffer-refile-list refile-list
+                     wl-summary-buffer-copy-list copy-list
+                     wl-summary-buffer-delete-list delete-list)
+               (wl-summary-delete-all-temp-marks 'no-msg)
                (encode-mime-charset-region
                 (point-min) (point-max) charset)
                (write-region-as-binary (point-min)(point-max)
@@ -4415,6 +4504,7 @@ If ARG, exit virtual folder."
   "returns update or all or rescan."
   ;; for the case when parts are expanded in the bottom of the folder
   (let ((input-range-list '("update" "all" "rescan" "first:" "last:"
+                           "cache-status"
                            "no-sync" "rescan-noscore" "all-visible"))
        (default (or (wl-get-assoc-list-value
                      wl-folder-sync-range-alist
@@ -4580,11 +4670,19 @@ If ARG, exit virtual folder."
 
 (defun wl-summary-next-page ()
   (interactive)
-  (wl-message-next-page))
+  (let ((cur-buf (current-buffer)))
+    (wl-summary-toggle-disp-msg 'on)
+    (when (wl-summary-set-message-buffer-or-redisplay 'ignore-original)
+      (set-buffer cur-buf)
+      (wl-message-next-page))))
 
 (defun wl-summary-prev-page ()
   (interactive)
-  (wl-message-prev-page))
+  (let ((cur-buf (current-buffer)))
+    (wl-summary-toggle-disp-msg 'on)
+    (when (wl-summary-set-message-buffer-or-redisplay 'ignore-original)
+      (set-buffer cur-buf)
+      (wl-message-prev-page))))
 
 (defsubst wl-summary-no-mime-p (folder)
   (wl-string-match-member (elmo-folder-name-internal folder)
@@ -4608,7 +4706,7 @@ Return t if message exists."
          (set-buffer wl-message-buffer)
          t)
       (if (wl-summary-no-mime-p folder)
-         (wl-summary-redisplay-no-mime folder number)
+         (wl-summary-redisplay-no-mime-internal folder number)
        (wl-summary-redisplay-internal folder number))
       (when (buffer-live-p wl-message-buffer)
        (set-buffer wl-message-buffer))
@@ -4890,7 +4988,7 @@ Reply to author if invoked with ARG."
   "Write a new draft from Summary."
   (interactive)
   (wl-draft nil nil nil nil nil
-           nil nil nil nil nil nil (current-buffer)
+           nil nil nil nil nil nil
            nil (wl-summary-buffer-folder-name))
   (run-hooks 'wl-mail-setup-hook)
   (mail-position-on-field "To"))
@@ -4917,20 +5015,20 @@ Use function list is `wl-summary-write-current-folder-functions'."
          (setq func-list (cdr func-list))
        (setq guess-func (car func-list))
        (setq func-list nil)))
-    (when (null guess-func)
-      (error "Can't guess by folder %s" folder))
-    (unless (or (stringp (nth 0 guess-list))
-               (stringp (nth 1 guess-list))
-               (stringp (nth 2 guess-list)))
-      (error "Invalid value return guess function `%s'"
-            (symbol-name guess-func)))
-    (wl-draft (nth 0 guess-list) nil nil ; To:
-             (nth 1 guess-list) nil    ; Cc:
-             (nth 2 guess-list)        ; Newsgroups:
-             nil nil nil nil nil nil nil
-             folder)
-    (run-hooks 'wl-mail-setup-hook)
-    (mail-position-on-field "Subject")))
+    (if (null guess-func)
+       (wl-draft)
+      (unless (or (stringp (nth 0 guess-list))
+                 (stringp (nth 1 guess-list))
+                 (stringp (nth 2 guess-list)))
+       (error "Invalid value return guess function `%s'"
+              (symbol-name guess-func)))
+      (wl-draft (nth 0 guess-list) nil nil ; To:
+               (nth 1 guess-list) nil  ; Cc:
+               (nth 2 guess-list)      ; Newsgroups:
+               nil nil nil nil nil nil
+               folder)
+      (run-hooks 'wl-mail-setup-hook)
+      (mail-position-on-field "Subject"))))
 
 (defun wl-summary-forward (&optional without-setup-hook)
   ""
@@ -5216,21 +5314,25 @@ Use function list is `wl-summary-write-current-folder-functions'."
            (if (not wl-summary-indent-length-limit)
                (wl-horizontal-recenter)))
          (wl-highlight-summary-displaying)
-         (when wl-message-buffer-prefetch-depth
-           (if (not (< wl-message-buffer-prefetch-depth
-                       wl-message-buffer-cache-size))
-               (error (concat
-                       "`wl-message-buffer-prefetch-depth' must be smaller than "
-                       "`wl-message-buffer-cache-size' - 1.")))
-           (wl-message-buffer-prefetch-next folder num
-                                            wl-message-buffer-prefetch-depth
-                                            (current-buffer)
-                                            wl-summary-buffer-mime-charset))
+         (wl-message-buffer-prefetch-next folder num
+                                          wl-message-buffer-prefetch-depth
+                                          (current-buffer)
+                                          wl-summary-buffer-mime-charset)
          (run-hooks 'wl-summary-redisplay-hook))
       (message "No message to display."))))
 
-(defun wl-summary-redisplay-no-mime (&optional folder number)
-  (interactive)
+(defun wl-summary-redisplay-no-mime (&optional ask-coding)
+  "Display message without MIME decoding.
+If ASK-CODING is non-nil, coding-system for the message is asked."
+  (interactive "P")
+  (let ((elmo-mime-display-as-is-coding-system
+        (if ask-coding
+            (or (read-coding-system "Coding system: ")
+                elmo-mime-display-as-is-coding-system)
+          elmo-mime-display-as-is-coding-system)))
+    (wl-summary-redisplay-no-mime-internal)))
+
+(defun wl-summary-redisplay-no-mime-internal (&optional folder number)
   (let* ((fld (or folder wl-summary-buffer-elmo-folder))
         (num (or number (wl-summary-message-number)))
         wl-break-pages)
@@ -5371,7 +5473,7 @@ Use function list is `wl-summary-write-current-folder-functions'."
                      "Supersedes: " message-id "\n"
                      (and followup-to
                           (concat "Followup-To: " followup-to "\n")))))
-    (if message-buf (set-buffer message-buf))
+       (if message-buf (set-buffer message-buf))
        (wl-draft-edit-string (buffer-substring (point-min) (point-max)))))))
 
 (defun wl-summary-save (&optional arg wl-save-dir)