Merge from emacs-24; up to 2014-05-15T16:55:18Z!jan.h.d@swipnet.se
[bpt/emacs.git] / lisp / mail / rmailsum.el
index 612ccbd..af08d0f 100644 (file)
@@ -1,8 +1,9 @@
 ;;; rmailsum.el --- make summary buffers for the mail reader
 
-;; Copyright (C) 1985, 1993-1996, 2000-2012 Free Software Foundation, Inc.
+;; Copyright (C) 1985, 1993-1996, 2000-2014 Free Software Foundation,
+;; Inc.
 
-;; Maintainer: FSF
+;; Maintainer: emacs-devel@gnu.org
 ;; Keywords: mail
 ;; Package: rmail
 
@@ -117,6 +118,7 @@ Setting this option to nil might speed up the generation of summaries."
     (define-key map "<"      'rmail-summary-first-message)
     (define-key map ">"      'rmail-summary-last-message)
     (define-key map " "      'rmail-summary-scroll-msg-up)
+    (define-key map [?\S-\ ] 'rmail-summary-scroll-msg-down)
     (define-key map "\177"   'rmail-summary-scroll-msg-down)
     (define-key map "?"      'describe-mode)
     (define-key map "\C-c\C-n" 'rmail-summary-next-same-subject)
@@ -392,20 +394,24 @@ SENDERS is a string of regexps separated by commas."
 
 (defvar rmail-new-summary-line-count)
 
-(defun rmail-new-summary (desc redo func &rest args)
+(defun rmail-new-summary (desc redo function &rest args)
   "Create a summary of selected messages.
-DESC makes part of the mode line of the summary buffer. REDO is form ...
-For each message, FUNC is applied to the message number and ARGS...
-and if the result is non-nil, that message is included.
-nil for FUNCTION means all messages."
+DESC makes part of the mode line of the summary buffer.
+REDO is what to put in `rmail-summary-redo'; usually
+its car is the function that called `rmail-new-summary'
+and its cdr is the arguments passed to that function.
+
+For each message, applies FUNCTION to the message number and ARGS...,
+and if the result is non-nil, it includes that message in the summary.
+If FUNCTION is nil, includes all messages."
   (message "Computing summary lines...")
   (unless rmail-buffer
     (error "No RMAIL buffer found"))
   (let (mesg was-in-summary sumbuf)
-    (if (eq major-mode 'rmail-summary-mode)
+    (if (derived-mode-p 'rmail-summary-mode)
        (setq was-in-summary t))
     (with-current-buffer rmail-buffer
-      (setq rmail-summary-buffer (rmail-new-summary-1 desc redo func args)
+      (setq rmail-summary-buffer (rmail-new-summary-1 desc redo function args)
            ;; r-s-b is buffer-local.
            sumbuf rmail-summary-buffer
            mesg rmail-current-message))
@@ -428,19 +434,19 @@ nil for FUNCTION means all messages."
       ;; This is how rmail makes the summary buffer reappear.
       ;; We do this here to make the window the proper size.
       (rmail-select-summary nil)
-      (set-buffer rmail-summary-buffer))
+      (set-buffer sumbuf))
     (rmail-summary-goto-msg mesg t t)
     (rmail-summary-construct-io-menu)
     (message "Computing summary lines...done")))
 
-(defun rmail-new-summary-1 (description form function args)
+(defun rmail-new-summary-1 (description redo function args)
   "Filter messages to obtain summary lines.
 DESCRIPTION is added to the mode line.
 
 Return the summary buffer by invoking FUNCTION on each message
-passing the message number and ARGS...
+passing the message number and ARGS.
 
-REDO is a form ...
+REDO is what to put in `rmail-summary-redo'.
 
 The current buffer must be a Rmail buffer either containing a
 collection of mbox formatted messages or displaying a single
@@ -467,8 +473,8 @@ message."
                (widen)
                (goto-char (point-min))
                (while (>= total msgnum)
-                 ;; Go back to the Rmail buffer so
-                 ;; so FUNCTION and rmail-get-summary can see its local vars.
+                 ;; Go back to the Rmail buffer so FUNCTION and
+                 ;; rmail-get-summary can see its local vars.
                  (with-current-buffer main-buffer
                    ;; First test whether to include this message.
                    (if (or (null function)
@@ -488,8 +494,7 @@ message."
     ;; we "don't have" a summary.
     (setq rmail-summary-buffer nil)
     ;; I have not a clue what this clause is doing.  If you read this
-    ;; chunk of code and have a clue, then please email that clue to
-    ;; pmr@pajato.com
+    ;; chunk of code and have a clue, then please  write it here.
     (if rmail-enable-mime
        (with-current-buffer rmail-buffer
          (setq rmail-summary-buffer nil)))
@@ -510,7 +515,7 @@ message."
        (make-local-variable 'minor-mode-alist)
        (setq minor-mode-alist (list (list t (concat ": " description))))
        (setq rmail-buffer rbuf
-             rmail-summary-redo form
+             rmail-summary-redo redo
              rmail-total-messages total)))
     sumbuf))
 
@@ -909,7 +914,10 @@ a negative argument means to delete and move backward."
   (unless (numberp count) (setq count 1))
   (let (end del-msg
            (backward (< count 0)))
-    (while (/= count 0)
+    (while (and (/= count 0)
+               ;; Don't waste time if we are at the beginning
+               ;; and trying to go backward.
+               (not (and backward (bobp))))
       (rmail-summary-goto-msg)
       (with-current-buffer rmail-buffer
        (rmail-delete-message)
@@ -919,11 +927,13 @@ a negative argument means to delete and move backward."
                  (save-excursion (beginning-of-line)
                                  (looking-at " *[0-9]+D")))
        (forward-line (if backward -1 1)))
+      (setq count
+           (if (> count 0) (1- count) (1+ count)))
       ;; It looks ugly to move to the empty line at end of buffer.
+      ;; And don't waste time after hitting the end.
       (and (eobp) (not backward)
-          (forward-line -1))
-      (setq count
-           (if (> count 0) (1- count) (1+ count))))))
+          (progn (setq count 0)
+                 (forward-line -1))))))
 
 (defun rmail-summary-delete-backward (&optional count)
   "Delete this message and move to previous nondeleted one.
@@ -934,8 +944,9 @@ a negative argument means to delete and move forward."
   (rmail-summary-delete-forward (- count)))
 
 (defun rmail-summary-mark-deleted (&optional n undel)
-  ;; Since third arg is t, this only alters the summary, not the Rmail buf.
-  (and n (rmail-summary-goto-msg n t t))
+  (and n (not (eq n (rmail-summary-msg-number)))
+       ;; Since third arg is t, this only alters summary, not the Rmail buf.
+       (rmail-summary-goto-msg n t t))
   (or (eobp)
       (not (overlay-get rmail-summary-overlay 'face))
       (let ((buffer-read-only nil))
@@ -946,9 +957,9 @@ a negative argument means to delete and move forward."
                (progn (delete-char 1) (insert " ")))
          (delete-char 1)
          (insert "D"))
-       ;; Register a new summary line.
+       ;; Discard cached new summary line.
        (with-current-buffer rmail-buffer
-         (aset rmail-summary-vector (1- n) (rmail-create-summary-line n)))))
+         (aset rmail-summary-vector (1- n) nil))))
   (beginning-of-line))
 
 (defun rmail-summary-update-line (n)
@@ -997,7 +1008,7 @@ Optional prefix ARG means undelete ARG previous messages."
                 (set-buffer rmail-buffer)
               (rmail-pop-to-buffer rmail-buffer))
             (and (rmail-message-deleted-p rmail-current-message)
-                 (rmail-undelete-previous-message))
+                 (rmail-undelete-previous-message 1))
             (if rmail-enable-mime
                 (rmail-pop-to-buffer rmail-buffer))
             (rmail-pop-to-buffer rmail-summary-buffer))
@@ -1006,31 +1017,40 @@ Optional prefix ARG means undelete ARG previous messages."
 (defun rmail-summary-undelete-many (&optional n)
   "Undelete all deleted msgs, optional prefix arg N means undelete N prev msgs."
   (interactive "P")
-  (with-current-buffer rmail-buffer
-    (let* ((init-msg (if n rmail-current-message rmail-total-messages))
-          (rmail-current-message init-msg)
-          (n (or n rmail-total-messages))
-          (msgs-undeled 0))
-      (while (and (> rmail-current-message 0)
-                 (< msgs-undeled n))
-       (if (rmail-message-deleted-p rmail-current-message)
-           (progn (rmail-set-attribute rmail-deleted-attr-index nil)
-                  (setq msgs-undeled (1+ msgs-undeled))))
-       (setq rmail-current-message (1- rmail-current-message)))
-      (set-buffer rmail-summary-buffer)
-      (setq rmail-current-message init-msg msgs-undeled 0)
-      (while (and (> rmail-current-message 0)
-                 (< msgs-undeled n))
-       (if (rmail-summary-deleted-p rmail-current-message)
-           (progn (rmail-summary-mark-undeleted rmail-current-message)
-                  (setq msgs-undeled (1+ msgs-undeled))))
-       (setq rmail-current-message (1- rmail-current-message))))
-    (rmail-summary-goto-msg)))
+  (if n
+      (while (and (> n 0) (not (eobp)))
+       (rmail-summary-goto-msg)
+       (let (del-msg)
+         (when (rmail-summary-deleted-p)
+           (with-current-buffer rmail-buffer
+             (rmail-undelete-previous-message 1)
+             (setq del-msg rmail-current-message))
+           (rmail-summary-mark-undeleted del-msg)))
+       (while (and (not (eobp))
+                   (save-excursion (beginning-of-line)
+                                   (looking-at " *[0-9]+ ")))
+         (forward-line 1))
+       (setq n (1- n)))
+    (rmail-summary-goto-msg 1)
+    (dotimes (i rmail-total-messages)
+      (rmail-summary-goto-msg)
+      (let (del-msg)
+       (when (rmail-summary-deleted-p)
+         (with-current-buffer rmail-buffer
+           (rmail-undelete-previous-message 1)
+           (setq del-msg rmail-current-message))
+         (rmail-summary-mark-undeleted del-msg)))
+      (if (not (eobp))
+         (forward-line 1))))
+
+  ;; It looks ugly to move to the empty line at end of buffer.
+  (and (eobp)
+       (forward-line -1)))
 \f
 ;; Rmail Summary mode is suitable only for specially formatted data.
 (put 'rmail-summary-mode 'mode-class 'special)
 
-(defun rmail-summary-mode ()
+(define-derived-mode rmail-summary-mode special-mode "RMAIL Summary"
   "Rmail Summary Mode is invoked from Rmail Mode by using \\<rmail-mode-map>\\[rmail-summary].
 As commands are issued in the summary buffer, they are applied to the
 corresponding mail messages in the rmail buffer.
@@ -1053,10 +1073,6 @@ Commands for sorting the summary:
 \\[rmail-summary-sort-by-correspondent] Sort by correspondent.
 \\[rmail-summary-sort-by-lines] Sort by lines.
 \\[rmail-summary-sort-by-labels] Sort by labels."
-  (interactive)
-  (kill-all-local-variables)
-  (setq major-mode 'rmail-summary-mode)
-  (setq mode-name "RMAIL Summary")
   (setq truncate-lines t)
   (setq buffer-read-only t)
   (set-syntax-table text-mode-syntax-table)
@@ -1069,8 +1085,7 @@ Commands for sorting the summary:
   (make-local-variable 'revert-buffer-function)
   (make-local-variable 'font-lock-defaults)
   (setq font-lock-defaults '(rmail-summary-font-lock-keywords t))
-  (rmail-summary-enable)
-  (run-mode-hooks 'rmail-summary-mode-hook))
+  (rmail-summary-enable))
 
 ;; Summary features need to be disabled during edit mode.
 (defun rmail-summary-disable ()
@@ -1120,57 +1135,59 @@ Search, the `unseen' attribute is restored.")
          (forward-line -1))
       (beginning-of-line)
       (skip-chars-forward " ")
-      (let ((msg-num (string-to-number (buffer-substring
-                                        (point)
-                                        (progn (skip-chars-forward "0-9")
-                                               (point))))))
-       ;; Always leave `unseen' removed
-       ;; if we get out of isearch mode.
-       ;; Don't let a subsequent isearch restore that `unseen'.
-       (if (not isearch-mode)
-           (setq rmail-summary-put-back-unseen nil))
-
-       (or (eq rmail-current-message msg-num)
-           (let ((window (get-buffer-window rmail-buffer t))
-                 (owin (selected-window)))
-             (if isearch-mode
-                 (progn
-                   ;; If we first saw the previous message in this search,
-                   ;; and we have gone to a different message while searching,
-                   ;; put back `unseen' on the former one.
-                   (when rmail-summary-put-back-unseen
-                     (rmail-set-attribute rmail-unseen-attr-index t
-                                          rmail-current-message)
-                     (save-excursion
-                       (goto-char rmail-summary-put-back-unseen)
-                       (rmail-summary-mark-seen rmail-current-message t t)))
-                   ;; Arrange to do that later, for the new current message,
-                   ;; if it still has `unseen'.
-                   (setq rmail-summary-put-back-unseen
-                         (if (rmail-message-unseen-p msg-num)
-                             (point))))
-               (setq rmail-summary-put-back-unseen nil))
-             ;; Go to the desired message.
-             (setq rmail-current-message msg-num)
-             ;; Update the summary to show the message has been seen.
-             (rmail-summary-mark-seen msg-num t)
-             (if window
-                 ;; Using save-window-excursion would cause the new value
-                 ;; of point to get lost.
-                 (unwind-protect
-                     (progn
-                       (select-window window)
-                       (rmail-show-message msg-num t))
-                   (select-window owin))
-               (if (buffer-name rmail-buffer)
-                   (with-current-buffer rmail-buffer
-                     (rmail-show-message msg-num t))))
-             ;; In linum mode, the message buffer must be specially
-             ;; updated (Bug#4878).
-             (and (fboundp 'linum-update)
-                  (buffer-name rmail-buffer)
-                  (linum-update rmail-buffer))))
-       (rmail-summary-update-highlight nil)))))
+      ;; If the summary is empty, don't do anything.
+      (unless (eobp)
+       (let ((msg-num (string-to-number (buffer-substring
+                                         (point)
+                                         (progn (skip-chars-forward "0-9")
+                                                (point))))))
+         ;; Always leave `unseen' removed
+         ;; if we get out of isearch mode.
+         ;; Don't let a subsequent isearch restore that `unseen'.
+         (if (not isearch-mode)
+             (setq rmail-summary-put-back-unseen nil))
+
+         (or (eq rmail-current-message msg-num)
+             (let ((window (get-buffer-window rmail-buffer t))
+                   (owin (selected-window)))
+               (if isearch-mode
+                   (progn
+                     ;; If we first saw the previous message in this search,
+                     ;; and we have gone to a different message while searching,
+                     ;; put back `unseen' on the former one.
+                     (when rmail-summary-put-back-unseen
+                       (rmail-set-attribute rmail-unseen-attr-index t
+                                            rmail-current-message)
+                       (save-excursion
+                         (goto-char rmail-summary-put-back-unseen)
+                         (rmail-summary-mark-seen rmail-current-message t t)))
+                     ;; Arrange to do that later, for the new current message,
+                     ;; if it still has `unseen'.
+                     (setq rmail-summary-put-back-unseen
+                           (if (rmail-message-unseen-p msg-num)
+                               (point))))
+                 (setq rmail-summary-put-back-unseen nil))
+               ;; Go to the desired message.
+               (setq rmail-current-message msg-num)
+               ;; Update the summary to show the message has been seen.
+               (rmail-summary-mark-seen msg-num t)
+               (if window
+                   ;; Using save-window-excursion would cause the new value
+                   ;; of point to get lost.
+                   (unwind-protect
+                       (progn
+                         (select-window window)
+                         (rmail-show-message msg-num t))
+                     (select-window owin))
+                 (if (buffer-name rmail-buffer)
+                     (with-current-buffer rmail-buffer
+                       (rmail-show-message msg-num t))))
+               ;; In linum mode, the message buffer must be specially
+               ;; updated (Bug#4878).
+               (and (fboundp 'linum-update)
+                    (buffer-name rmail-buffer)
+                    (linum-update rmail-buffer))))
+         (rmail-summary-update-highlight nil))))))
 
 (defun rmail-summary-save-buffer ()
   "Save the buffer associated with this RMAIL summary."
@@ -1186,6 +1203,13 @@ Search, the `unseen' attribute is restored.")
   (goto-char (posn-point (event-end event)))
   (rmail-summary-goto-msg))
 
+(defun rmail-summary-msg-number ()
+  (save-excursion
+    (beginning-of-line)
+    (string-to-number
+     (buffer-substring (point)
+                      (min (point-max) (+ 6 (point)))))))
+
 (defun rmail-summary-goto-msg (&optional n nowarn skip-rmail)
   "Go to message N in the summary buffer and the Rmail buffer.
 If N is nil, use the message corresponding to point in the summary
@@ -1206,6 +1230,10 @@ Returns non-nil if message N was found."
                  (buffer-substring (point)
                                    (min (point-max) (+ 6 (point))))))
         (total (with-current-buffer buf rmail-total-messages)))
+    ;; CURMSG should be nil when there's no current summary message
+    ;; (for instance, if the summary is empty).
+    (if (= curmsg 0)
+       (setq curmsg nil))
     ;; If message number N was specified, find that message's line
     ;; or set message-not-found.
     ;; If N wasn't specified or that message can't be found.
@@ -1226,17 +1254,20 @@ Returns non-nil if message N was found."
                 (setq n curmsg)
                 (setq message-not-found t)
                 (goto-char cur))))
-    (rmail-summary-mark-seen n)
-    (rmail-summary-update-highlight message-not-found)
-    (beginning-of-line)
-    (unless skip-rmail
-      (let ((selwin (selected-window)))
-       (unwind-protect
-           (progn (rmail-pop-to-buffer buf)
-                  (rmail-show-message n))
-         (select-window selwin)
-         ;; The actions above can alter the current buffer.  Preserve it.
-         (set-buffer obuf))))
+    ;; N can be nil now, along with CURMSG,
+    ;; if the summary is empty.
+    (when n
+      (rmail-summary-mark-seen n)
+      (rmail-summary-update-highlight message-not-found)
+      (beginning-of-line)
+      (unless skip-rmail
+       (let ((selwin (selected-window)))
+         (unwind-protect
+             (progn (rmail-pop-to-buffer buf)
+                    (rmail-show-message n))
+           (select-window selwin)
+           ;; The actions above can alter the current buffer.  Preserve it.
+           (set-buffer obuf)))))
     (not message-not-found)))
 
 ;; Update the highlighted line in an rmail summary buffer.
@@ -1274,7 +1305,7 @@ advance to the next message."
                (prog1
                    ;; Is EOB visible in the buffer?
                    (save-excursion
-                     (let ((ht (window-height (selected-window))))
+                     (let ((ht (window-height)))
                        (move-to-window-line (- ht 2))
                        (end-of-line)
                        (eobp)))