(wl-summary-previous-message-number): New function.
[elisp/wanderlust.git] / wl / wl-summary.el
index 590e403..8ea2d49 100644 (file)
@@ -37,6 +37,7 @@
 
 (require 'elmo)
 (require 'elmo-multi)
+(eval-when-compile (require 'elmo-filter))
 (require 'wl-message)
 (require 'wl-vars)
 (require 'wl-highlight)
@@ -69,8 +70,8 @@
 (defvar wl-summary-buffer-elmo-folder nil)
 
 (defmacro wl-summary-buffer-folder-name ()
-  (` (and wl-summary-buffer-elmo-folder
-         (elmo-folder-name-internal wl-summary-buffer-elmo-folder))))
+  `(and wl-summary-buffer-elmo-folder
+       (elmo-folder-name-internal wl-summary-buffer-elmo-folder)))
 
 (defvar wl-summary-buffer-disp-msg    nil)
 (defvar wl-summary-buffer-disp-folder nil)
 (defvar wl-persistent-mark)
 
 (defmacro wl-summary-sticky-buffer-name (name)
-  (` (concat wl-summary-buffer-name ":" (, name))))
+  `(concat wl-summary-buffer-name ":" ,name))
 
 (defun wl-summary-default-subject (subject-string)
   (if (string-match "^[ \t]*\\[[^:]+[,: ][0-9]+\\][ \t]*" subject-string)
@@ -1003,7 +1004,7 @@ Entering Folder mode calls the value of `wl-summary-mode-hook'."
               (count (elmo-find-list-match-value
                       elmo-mailing-list-count-spec-list
                       getter)))
-         (cons name (and count (string-to-int count)))))))
+         (cons name (and count (string-to-number count)))))))
 
 (defun wl-summary-overview-entity-compare-by-list-info (x y)
   "Compare entity X and Y by mailing-list info."
@@ -1017,41 +1018,30 @@ Entering Folder mode calls the value of `wl-summary-mode-hook'."
       (string< (or (car list-info-x) "")
               (or (car list-info-y) "")))))
 
-(defun wl-summary-sort-by-date (reverse)
-  "Sort summary lines into the order by message date; argument means descending order."
-  (interactive "P")
-  (wl-summary-rescan "date" reverse))
-(defun wl-summary-sort-by-number (reverse)
-  "Sort summary lines into the order by message number; argument means descending order."
-  (interactive "P")
-  (wl-summary-rescan "number" reverse))
-(defun wl-summary-sort-by-subject (reverse)
-  "Sort summary lines into the order by subject; argument means descending order."
-  (interactive "P")
-  (wl-summary-rescan "subject" reverse))
-(defun wl-summary-sort-by-from (reverse)
-  "Sort summary lines into the order by from; argument means descending order."
-  (interactive "P")
-  (wl-summary-rescan "from" reverse))
-(defun wl-summary-sort-by-list-info (reverse)
-  "Sort summary lines into the order by mailing list info; argument means descending order."
-  (interactive "P")
-  (wl-summary-rescan "list-info" reverse))
-(defun wl-summary-sort-by-size (reverse)
-  "Sort summary lines into the order by message size; argument means descending order."
-  (interactive "P")
-  (wl-summary-rescan "size" reverse))
+(defun wl-summary-define-sort-command ()
+  "Define functions to sort summary lines by `wl-summary-sort-specs'."
+  (interactive)
+  (dolist (sort-by wl-summary-sort-specs)
+    (fset (intern (format "wl-summary-sort-by-%s" sort-by))
+         `(lambda (&optional reverse)
+            ,(format "\
+Sort summary lines into the order by %s.
+If optional argument REVERSE is non-nil, sort into descending order.
+
+This function is defined by `wl-summary-define-sort-command'." sort-by)
+            (interactive "P")
+            (wl-summary-rescan ,(symbol-name sort-by) reverse)))))
 
 (defun wl-summary-sort-function-from-spec (spec reverse)
-  (let (funtion)
+  (let (function)
     (when (string-match "^!\\(.+\\)$" spec)
       (setq spec (match-string 1 spec)
            reverse (not reverse)))
-    (setq funtion
+    (setq function
          (intern (format "wl-summary-overview-entity-compare-by-%s" spec)))
     (if reverse
-       `(lambda (x y) (not (,funtion x y)))
-      funtion)))
+       `(lambda (x y) (not (,function x y)))
+      function)))
 
 (defun wl-summary-sort-messages (numbers sort-by reverse)
   (let* ((functions (mapcar
@@ -1091,7 +1081,6 @@ Entering Folder mode calls the value of `wl-summary-mode-hook'."
         (and disable-thread wl-summary-search-parent-by-subject-regexp))
        (wl-summary-divide-thread-when-subject-changed
         (and disable-thread wl-summary-divide-thread-when-subject-changed))
-       (i 0)
        num
        expunged)
     (erase-buffer)
@@ -1112,20 +1101,14 @@ Entering Folder mode calls the value of `wl-summary-mode-hook'."
          wl-summary-buffer-temp-mark-list nil
          wl-summary-delayed-update nil)
     (elmo-kill-buffer wl-summary-search-buf-name)
-    (while numbers
-      (wl-summary-insert-message (elmo-message-entity
-                                 wl-summary-buffer-elmo-folder
-                                 (car numbers))
-                                wl-summary-buffer-elmo-folder
-                                nil)
-      (setq numbers (cdr numbers))
-      (when (> num elmo-display-progress-threshold)
-       (setq i (+ i 1))
-       (if (or (zerop (% i 5)) (= i num))
-           (elmo-display-progress
-            'wl-summary-rescan "Constructing summary structure..."
-            (/ (* i 100) num)))))
-    (when wl-summary-delayed-update
+    (elmo-with-progress-display (wl-summary-insert-line num)
+       "Constructing summary structure"
+      (dolist (number numbers)
+       (wl-summary-insert-message (elmo-message-entity
+                                   wl-summary-buffer-elmo-folder
+                                   number)
+                                  wl-summary-buffer-elmo-folder
+                                  nil))
       (while wl-summary-delayed-update
        (message "Parent (%d) of message %d is no entity"
                 (caar wl-summary-delayed-update)
@@ -1135,12 +1118,8 @@ Entering Folder mode calls the value of `wl-summary-mode-hook'."
         (cdar wl-summary-delayed-update)
         wl-summary-buffer-elmo-folder nil t)
        (setq wl-summary-delayed-update (cdr wl-summary-delayed-update))))
-    (message "Constructing summary structure...done")
-    (if (eq wl-summary-buffer-view 'thread)
-       (progn
-         (message "Inserting thread...")
-         (wl-thread-insert-top)
-         (message "Inserting thread...done")))
+    (when (eq wl-summary-buffer-view 'thread)
+      (wl-thread-insert-top))
     (when wl-use-scoring
       (wl-summary-score-headers (wl-summary-rescore-msgs
                                 wl-summary-buffer-number-list)
@@ -1271,9 +1250,9 @@ Entering Folder mode calls the value of `wl-summary-mode-hook'."
   (when wl-summary-buffer-temp-mark-list
     (wl-summary-exec-with-confirmation
      (format "Execute marks in %s? (answer \"n\" to discard them) "
-            (wl-summary-buffer-folder-name)))
-    (wl-summary-delete-all-temp-marks 'no-msg)
-    (setq wl-summary-scored nil)))
+            (wl-summary-buffer-folder-name))))
+  (wl-summary-delete-all-temp-marks 'no-msg)
+  (setq wl-summary-scored nil))
 
 ;; a subroutine for wl-summary-exit/wl-save-status
 ;; Note that folder is not commited here.
@@ -1401,6 +1380,8 @@ Entering Folder mode calls the value of `wl-summary-mode-hook'."
         (msgdb-dir (elmo-folder-msgdb-path folder))
         (range (or force-range (wl-summary-input-range
                                 (elmo-folder-name-internal folder)))))
+    (when (symbolp range)
+      (setq range (symbol-name range)))
     (cond ((string-match "rescan" range)
           (let ((msg (wl-summary-message-number))
                 (wl-use-scoring (if (string-match "noscore" range)
@@ -1560,25 +1541,27 @@ If ARG is non-nil, checking is omitted."
   "All uncached messages are cached."
   (interactive)
   (unless (elmo-folder-local-p wl-summary-buffer-elmo-folder)
-    (let ((targets (elmo-folder-list-flagged wl-summary-buffer-elmo-folder
-                                            'uncached 'in-msgdb))
-         (count 0)
-         wl-prefetch-confirm
-         wl-prefetch-threshold
-         (elmo-inhibit-display-retrieval-progress t)
-         length msg)
+    (let* ((targets (elmo-folder-list-flagged wl-summary-buffer-elmo-folder
+                                             'uncached 'in-msgdb))
+          (count 0)
+          wl-prefetch-confirm
+          wl-prefetch-threshold
+          (length (length targets))
+          msg)
       (save-excursion
-       (goto-char (point-min))
-       (setq length (length targets))
-       (dolist (target targets)
-         (when (if (not (wl-thread-entity-parent-invisible-p
-                         (wl-thread-get-entity target)))
-                   (progn
-                     (wl-summary-jump-to-msg target)
-                     (wl-summary-prefetch-msg
-                      (wl-summary-message-number)))
-                 (wl-summary-prefetch-msg target))
-           (message "Retrieving... %d/%d" (incf count) length)))
+       (elmo-with-progress-display (wl-summary-prefetch-message length)
+           "Retrieving"
+         (goto-char (point-min))
+         (dolist (target targets)
+           (when (if (not (wl-thread-entity-parent-invisible-p
+                           (wl-thread-get-entity target)))
+                     (progn
+                       (wl-summary-jump-to-msg target)
+                       (wl-summary-prefetch-msg
+                        (wl-summary-message-number)))
+                   (wl-summary-prefetch-msg target))
+             (incf count))
+           (elmo-progress-notify 'wl-summary-prefetch-message)))
        (message "Retrieved %d/%d message(s)" count length)))))
 
 (defun wl-summary-prefetch-msg (number &optional arg)
@@ -1847,7 +1830,7 @@ If ARG is non-nil, checking is omitted."
       (wl-summary-update-modeline)
       (message "Resuming cache status...done"))))
 
-(defun wl-summary-delete-messages-on-buffer (msgs &optional deleting-info)
+(defun wl-summary-delete-messages-on-buffer (msgs)
   (interactive)
   (save-excursion
     (let ((inhibit-read-only t)
@@ -1855,7 +1838,6 @@ If ARG is non-nil, checking is omitted."
          (msgs2 msgs)
          (len (length msgs))
          (i 0)
-         ;(deleting-info (or deleting-info "Deleting..."))
          update-list)
       (elmo-kill-buffer wl-summary-search-buf-name)
       (while msgs
@@ -1873,17 +1855,13 @@ If ARG is non-nil, checking is omitted."
                (delete-char 1) ; delete '\n'
                (setq wl-summary-buffer-number-list
                      (delq (car msgs) wl-summary-buffer-number-list)))))
-;      (when (> len elmo-display-progress-threshold)
-;        (setq i (1+ i))
-;        (if (or (zerop (% i 5)) (= i len))
-;            (elmo-display-progress
-;             'wl-summary-delete-messages-on-buffer deleting-info
-;             (/ (* i 100) len))))
        (setq msgs (cdr msgs)))
       (when (eq wl-summary-buffer-view 'thread)
-       (wl-thread-update-line-msgs (elmo-uniq-list update-list))
-       (wl-thread-cleanup-symbols msgs2))
-      ;;(message (concat deleting-info "done"))
+       (let ((updates (elmo-uniq-list update-list)))
+         (elmo-with-progress-display (wl-thread-update-line (length updates))
+             "Updating deleted thread"
+           (wl-thread-update-line-msgs updates)
+           (wl-thread-cleanup-symbols msgs2))))
       (wl-summary-count-unread)
       (wl-summary-update-modeline)
       (wl-summary-folder-info-update))))
@@ -1995,8 +1973,7 @@ This function is defined for `window-scroll-functions'"
                          (not wl-summary-lazy-highlight)))
                    append-list delete-list
                    update-thread update-top-list
-                   num diff entity
-                   (i 0))
+                   num diff entity)
                ;; Setup sync-all
                (if sync-all (wl-summary-sync-all-init))
                (setq diff (elmo-list-diff (elmo-folder-list-messages
@@ -2018,28 +1995,22 @@ This function is defined for `window-scroll-functions'"
                (setq num (length append-list))
                (setq wl-summary-delayed-update nil)
                (elmo-kill-buffer wl-summary-search-buf-name)
-               (dolist (number append-list)
-                 (setq entity (elmo-message-entity folder number))
-                 (when (setq update-thread
-                             (wl-summary-insert-message
-                              entity folder
-                              (not sync-all)))
-                   (wl-append update-top-list update-thread))
-                 (if elmo-use-database
-                     (elmo-database-msgid-put
-                      (elmo-message-entity-field entity 'message-id)
-                      (elmo-folder-name-internal folder)
-                      (elmo-message-entity-number entity)))
-                 (when (> num elmo-display-progress-threshold)
-                   (setq i (+ i 1))
-                   (if (or (zerop (% i 5)) (= i num))
-                       (elmo-display-progress
-                        'wl-summary-sync-update
-                        (if (eq wl-summary-buffer-view 'thread)
-                            "Making thread..."
-                          "Inserting message...")
-                        (/ (* i 100) num)))))
-               (when wl-summary-delayed-update
+               (elmo-with-progress-display (wl-summary-insert-line num)
+                   (if (eq wl-summary-buffer-view 'thread)
+                       "Making thread"
+                     "Inserting message")
+                 (dolist (number append-list)
+                   (setq entity (elmo-message-entity folder number))
+                   (when (setq update-thread
+                               (wl-summary-insert-message
+                                entity folder
+                                (not sync-all)))
+                     (wl-append update-top-list update-thread))
+                   (if elmo-use-database
+                       (elmo-database-msgid-put
+                        (elmo-message-entity-field entity 'message-id)
+                        (elmo-folder-name-internal folder)
+                        (elmo-message-entity-number entity))))
                  (while wl-summary-delayed-update
                    (message "Parent (%d) of message %d is no entity"
                             (caar wl-summary-delayed-update)
@@ -2057,16 +2028,11 @@ This function is defined for `window-scroll-functions'"
                           update-top-list)
                  (wl-thread-update-indent-string-thread
                   (elmo-uniq-list update-top-list)))
-               (message (if (eq wl-summary-buffer-view 'thread)
-                            "Making thread...done"
-                          "Inserting message...done"))
                (when (or delete-list append-list)
                  (wl-summary-set-message-modified))
                (when (and sync-all (eq wl-summary-buffer-view 'thread))
                  (elmo-kill-buffer wl-summary-search-buf-name)
-                 (message "Inserting message...")
-                 (wl-thread-insert-top)
-                 (message "Inserting message...done"))
+                 (wl-thread-insert-top))
                (if elmo-use-database
                    (elmo-database-close))
                (run-hooks 'wl-summary-sync-updated-hook)
@@ -2175,28 +2141,20 @@ This function is defined for `window-scroll-functions'"
 
 (defun wl-summary-highlight-msgs (msgs)
   (save-excursion
-    (let ((len (length msgs))
-         i)
-      (message "Hilighting...")
-      (setq i 0)
+    (elmo-with-progress-display (wl-summary-highlight-line (length msgs))
+       "Hilighting"
       (while msgs
        (if (wl-summary-jump-to-msg (car msgs))
            (wl-highlight-summary-current-line))
        (setq msgs (cdr msgs))
-       (when (> len elmo-display-progress-threshold)
-         (setq i (+ i 1))
-         (if (or (zerop (% i 5)) (= i len))
-             (elmo-display-progress
-              'wl-summary-highlight-msgs "Highlighting..."
-              (/ (* i 100) len)))))
-      (message "Highlighting...done"))))
+       (elmo-progress-notify 'wl-summary-highlight-line)))))
 
 (defun wl-summary-message-number ()
   (save-excursion
     (beginning-of-line)
     (if (or (re-search-forward "\r\\(-?[0-9]+\\)" (point-at-eol) t)
            (re-search-forward "^ *\\(-?[0-9]+\\)" (point-at-eol) t))
-       (string-to-int (wl-match-buffer 1))
+       (string-to-number (wl-match-buffer 1))
       nil)))
 
 (defun wl-summary-delete-all-msgs ()
@@ -2340,7 +2298,7 @@ If ARG, without confirm."
     (wl-summary-mode)
     (wl-summary-buffer-set-folder folder)
     (let ((buffer-read-only nil))
-      (insert-buffer cur-buf))
+      (insert-buffer-substring cur-buf))
     (set-buffer-modified-p nil)
     (while copy-variables
       (set (car copy-variables)
@@ -2421,10 +2379,13 @@ 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)
-      (if (or force-exit (not (wl-summary-sticky-p)))
+      (let ((discard-contents (or force-exit (not (wl-summary-sticky-p)))))
+       (when discard-contents
          (wl-summary-cleanup-temp-marks))
-      (wl-summary-save-view)
-      (elmo-folder-commit wl-summary-buffer-elmo-folder)
+       (wl-summary-save-view)
+       (if discard-contents
+           (elmo-folder-close 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)
@@ -2640,6 +2601,7 @@ If ARG, without confirm."
        (save-excursion (beginning-of-line)(point))
        (save-excursion (end-of-line)(point))
        'mouse-face nil))
+  (elmo-progress-notify 'wl-summary-insert-line)
   (ignore-errors
     (run-hooks 'wl-summary-line-inserted-hook)))
 
@@ -2667,13 +2629,13 @@ If ARG, without confirm."
           (funcall wl-summary-subject-filter-function subject2)))
 
 (defmacro wl-summary-put-alike (alike)
-  (` (elmo-set-hash-val (format "#%d" (wl-count-lines))
-                       (, alike)
-                       wl-summary-alike-hashtb)))
+  `(elmo-set-hash-val (format "#%d" (wl-count-lines))
+                     ,alike
+                     wl-summary-alike-hashtb))
 
 (defmacro wl-summary-get-alike ()
-  (` (elmo-get-hash-val (format "#%d" (wl-count-lines))
-                       wl-summary-alike-hashtb)))
+  `(elmo-get-hash-val (format "#%d" (wl-count-lines))
+                     wl-summary-alike-hashtb))
 
 (defun wl-summary-insert-headers (folder func &optional mime-decode)
   (let ((numbers (elmo-folder-list-messages folder 'visible t))
@@ -4259,8 +4221,7 @@ Return t if message exists."
             nil)))))
 
 (defun wl-summary-reply (&optional arg without-setup-hook)
-  "Reply to current message. Default is \"wide\" reply.
-Reply to author if invoked with ARG."
+  "Reply to current message. See also `wl-draft-reply'."
   (interactive "P")
   (let ((folder wl-summary-buffer-elmo-folder)
        (number (wl-summary-message-number))
@@ -4838,8 +4799,8 @@ If ARG is numeric number, decode message as following:
   (interactive (list current-prefix-arg nil))
   (if (null (wl-summary-message-number))
       (message "No message.")
-    (setq command (read-string "Shell command on message: "
-                              wl-summary-shell-command-last))
+    (setq command (wl-read-shell-command "Shell command on message: "
+                                        wl-summary-shell-command-last))
     (if (y-or-n-p "Send this message to pipe? ")
        (wl-summary-pipe-message-subr prefix command))))
 
@@ -4848,8 +4809,9 @@ If ARG is numeric number, decode message as following:
   (interactive (list current-prefix-arg nil))
   (if (null wl-summary-buffer-target-mark-list)
       (message "No marked message.")
-    (setq command (read-string "Shell command on each marked message: "
-                              wl-summary-shell-command-last))
+    (setq command (wl-read-shell-command
+                  "Shell command on each marked message: "
+                  wl-summary-shell-command-last))
     (when (y-or-n-p "Send each marked message to pipe? ")
       (while (car wl-summary-buffer-target-mark-list)
        (let ((num (car wl-summary-buffer-target-mark-list)))
@@ -5066,14 +5028,27 @@ If ARG is numeric number, decode message as following:
 ;;                                         sum))
 ;;     (message "Dropping...done"))))
 
+(defun wl-summary-previous-message-number (msg)
+  "Return a message number previous to the message specified by MSG."
+  (let ((list wl-summary-buffer-number-list)
+       previous)
+    (while (and list (not (eq msg (car list))))
+      (setq previous (car list))
+      (setq list (cdr list)))
+    previous))
+
+(defun wl-summary-next-message-number (msg)
+  "Return a message number next to the message specified by MSG."
+  (cadr (memq msg wl-summary-buffer-number-list)))
+
 (defun wl-summary-default-get-next-msg (msg)
   (or (wl-summary-next-message msg
                               (if wl-summary-move-direction-downward 'down
                                 'up)
                               nil)
-      (cadr (memq msg (if wl-summary-move-direction-downward
-                         wl-summary-buffer-number-list
-                       (reverse wl-summary-buffer-number-list))))))
+      (if wl-summary-move-direction-downward
+         (wl-summary-next-message-number msg)
+       (wl-summary-previous-message-number msg))))
 
 (defun wl-summary-save-current-message ()
   "Save current message for `wl-summary-yank-saved-message'."