Fix comment typo.
[bpt/emacs.git] / lisp / mail / rmailsort.el
index d23433c..a4de5a6 100644 (file)
@@ -1,17 +1,18 @@
-;;; rmailsort.el --- Rmail: sort messages.
+;;; rmailsort.el --- Rmail: sort messages
 
-;; Copyright (C) 1990, 1993 Free Software Foundation, Inc.
+;; Copyright (C) 1990, 1993, 1994, 2001, 2002, 2003, 2004,
+;;   2005, 2006, 2007, 2008 Free Software Foundation, Inc.
 
 ;; Author: Masanobu UMEDA <umerin@mse.kyutech.ac.jp>
-;; Version: $Header: /home/fsf/rms/e19/lisp/RCS/rmailsort.el,v 1.15 1993/06/22 05:55:41 rms Exp rms $
+;; Maintainer: FSF
 ;; Keywords: mail
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to
-;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
 
 ;;; Code:
 
 (require 'sort)
 
+;; For rmail-select-summary
+(require 'rmail)
+
 (autoload 'timezone-make-date-sortable "timezone")
 
 ;; Sorting messages in Rmail buffer
 
+;;;###autoload
 (defun rmail-sort-by-date (reverse)
   "Sort messages of current Rmail file by date.
 If prefix argument REVERSE is non-nil, sort them in reverse order."
@@ -40,6 +46,7 @@ If prefix argument REVERSE is non-nil, sort them in reverse order."
                          (rmail-make-date-sortable
                           (rmail-fetch-field msg "Date"))))))
 
+;;;###autoload
 (defun rmail-sort-by-subject (reverse)
   "Sort messages of current Rmail file by subject.
 If prefix argument REVERSE is non-nil, sort them in reverse order."
@@ -50,9 +57,11 @@ If prefix argument REVERSE is non-nil, sort them in reverse order."
                          (let ((key (or (rmail-fetch-field msg "Subject") ""))
                                (case-fold-search t))
                            ;; Remove `Re:'
-                           (if (string-match "^\\(re:[ \t]+\\)*" key)
-                               (substring key (match-end 0)) key))))))
+                           (if (string-match "^\\(re:[ \t]*\\)*" key)
+                               (substring key (match-end 0))
+                             key))))))
 
+;;;###autoload
 (defun rmail-sort-by-author (reverse)
   "Sort messages of current Rmail file by author.
 If prefix argument REVERSE is non-nil, sort them in reverse order."
@@ -65,6 +74,7 @@ If prefix argument REVERSE is non-nil, sort them in reverse order."
                            (or (rmail-fetch-field msg "From")
                                (rmail-fetch-field msg "Sender") "")))))))
 
+;;;###autoload
 (defun rmail-sort-by-recipient (reverse)
   "Sort messages of current Rmail file by recipient.
 If prefix argument REVERSE is non-nil, sort them in reverse order."
@@ -78,6 +88,7 @@ If prefix argument REVERSE is non-nil, sort them in reverse order."
                                (rmail-fetch-field msg "Apparently-To") "")
                            ))))))
 
+;;;###autoload
 (defun rmail-sort-by-correspondent (reverse)
   "Sort messages of current Rmail file by other correspondent.
 If prefix argument REVERSE is non-nil, sort them in reverse order."
@@ -99,6 +110,7 @@ If prefix argument REVERSE is non-nil, sort them in reverse order."
       (setq fields (cdr fields)))
     ans))
 
+;;;###autoload
 (defun rmail-sort-by-lines (reverse)
   "Sort messages of current Rmail file by number of lines.
 If prefix argument REVERSE is non-nil, sort them in reverse order."
@@ -106,20 +118,50 @@ If prefix argument REVERSE is non-nil, sort them in reverse order."
   (rmail-sort-messages reverse
                       (function
                        (lambda (msg)
-                         (count-lines (rmail-msgbeg msgnum)
-                                      (rmail-msgend msgnum))))))
+                         (count-lines (rmail-msgbeg msg)
+                                      (rmail-msgend msg))))))
+
+;;;###autoload
+(defun rmail-sort-by-labels (reverse labels)
+  "Sort messages of current Rmail file by labels.
+If prefix argument REVERSE is non-nil, sort them in reverse order.
+KEYWORDS is a comma-separated list of labels."
+  (interactive "P\nsSort by labels: ")
+  (or (string-match "[^ \t]" labels)
+      (error "No labels specified"))
+  (setq labels (concat (substring labels (match-beginning 0)) ","))
+  (let (labelvec)
+    (while (string-match "[ \t]*,[ \t]*" labels)
+      (setq labelvec (cons
+                     (concat ", ?\\("
+                             (substring labels 0 (match-beginning 0))
+                             "\\),")
+                     labelvec))
+      (setq labels (substring labels (match-end 0))))
+    (setq labelvec (apply 'vector (nreverse labelvec)))
+    (rmail-sort-messages reverse
+                        (function
+                         (lambda (msg)
+                           (let ((n 0))
+                             (while (and (< n (length labelvec))
+                                         (not (rmail-message-labels-p
+                                               msg (aref labelvec n))))
+                               (setq n (1+ n)))
+                             n))))))
 \f
 ;; Basic functions
+(declare-function rmail-update-summary "rmailsum" (&rest ignore))
 
 (defun rmail-sort-messages (reverse keyfun)
   "Sort messages of current Rmail file.
 If 1st argument REVERSE is non-nil, sort them in reverse order.
 2nd argument KEYFUN is called with a message number, and should return a key."
-  (save-excursion
+  (save-current-buffer
     ;; If we are in a summary buffer, operate on the Rmail buffer.
     (if (eq major-mode 'rmail-summary-mode)
        (set-buffer rmail-buffer))
     (let ((buffer-read-only nil)
+         (point-offset (- (point) (point-min)))
          (predicate nil)                       ;< or string-lessp
          (sort-lists nil))
       (message "Finding sort keys...")
@@ -153,7 +195,7 @@ If 1st argument REVERSE is non-nil, sort them in reverse order.
            (msgnum 1)
            (msginfo nil))
        ;; There's little hope that we can easily undo after that.
-       (buffer-flush-undo (current-buffer))
+       (buffer-disable-undo (current-buffer))
        (goto-char (rmail-msgbeg 1))
        ;; To force update of all markers.
        (insert-before-markers ?Z)
@@ -177,7 +219,11 @@ If 1st argument REVERSE is non-nil, sort them in reverse order.
        (setq quit-flag nil)
        (buffer-enable-undo)
        (rmail-set-message-counters)
-       (rmail-show-message current-message)))))
+       (rmail-show-message current-message)
+       (goto-char (+ point-offset (point-min)))
+       (if (rmail-summary-exists)
+           (rmail-select-summary
+            (rmail-update-summary)))))))
 
 (defun rmail-fetch-field (msg field)
   "Return the value of the header FIELD of MSG.
@@ -200,4 +246,5 @@ Arguments are MSG and FIELD."
 
 (provide 'rmailsort)
 
+;; arch-tag: 0d90896b-0c35-46ac-b240-38be5ada2360
 ;;; rmailsort.el ends here