changeset 101075:164acaa725fe

(pmailhdr): Don't require it. (pmail-fetch-field): Function deleted. Callers use pmail-get-header. (pmail-sort-messages): Replace point-offset with return-to-point, which can be nil. Call pmail-swap-buffers-maybe after that. Don't bind buffer-read-only. Bind inhibit-read-only. Be more careful in making blank line at end of msg. Don't enable undo at end.
author Richard M. Stallman <rms@gnu.org>
date Sat, 10 Jan 2009 21:12:48 +0000
parents cc1abbe3dfe5
children 6c4177798438
files lisp/mail/pmailsort.el
diffstat 1 files changed, 21 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/lisp/mail/pmailsort.el	Sat Jan 10 21:07:18 2009 +0000
+++ b/lisp/mail/pmailsort.el	Sat Jan 10 21:12:48 2009 +0000
@@ -31,8 +31,6 @@
   (require 'sort)
   (require 'pmail))
 
-(require 'pmailhdr)
-
 (autoload 'timezone-make-date-sortable "timezone")
 
 (declare-function pmail-update-summary "pmailsum" (&rest ignore))
@@ -48,7 +46,7 @@
 		       (function
 			(lambda (msg)
 			  (pmail-make-date-sortable
-			   (pmail-fetch-field msg "Date"))))))
+			   (pmail-get-header "Date" msg))))))
 
 ;;;###autoload
 (defun pmail-sort-by-subject (reverse)
@@ -58,7 +56,7 @@
   (pmail-sort-messages reverse
 		       (function
 			(lambda (msg)
-			  (let ((key (or (pmail-fetch-field msg "Subject") ""))
+			  (let ((key (or (pmail-get-header "Subject" msg) ""))
 				(case-fold-search t))
 			    ;; Remove `Re:'
 			    (if (string-match "^\\(re:[ \t]*\\)*" key)
@@ -75,8 +73,8 @@
 			(lambda (msg)
 			  (downcase	;Canonical name
 			   (mail-strip-quoted-names
-			    (or (pmail-fetch-field msg "From")
-				(pmail-fetch-field msg "Sender") "")))))))
+			    (or (pmail-get-header "From" msg)
+				(pmail-get-header "Sender" msg) "")))))))
 
 ;;;###autoload
 (defun pmail-sort-by-recipient (reverse)
@@ -88,8 +86,8 @@
 			(lambda (msg)
 			  (downcase	;Canonical name
 			   (mail-strip-quoted-names
-			    (or (pmail-fetch-field msg "To")
-				(pmail-fetch-field msg "Apparently-To") "")
+			    (or (pmail-get-header "To" msg)
+				(pmail-get-header "Apparently-To" msg) "")
 			    ))))))
 
 ;;;###autoload
@@ -111,7 +109,7 @@
 	    ;; NB despite the name, this lives in mail-utils.el.
 	    (rmail-dont-reply-to
 	     (mail-strip-quoted-names
-	      (or (pmail-fetch-field msg (car fields)) ""))))
+	      (or (pmail-get-header (car fields) msg) ""))))
       (setq fields (cdr fields)))
     ans))
 
@@ -160,12 +158,13 @@
   "Sort messages of current Pmail 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."
-  (pmail-swap-buffers-maybe)
   (with-current-buffer pmail-buffer
-    (let ((buffer-read-only nil)
-	  (point-offset (- (point) (point-min)))
+    (let ((return-to-point
+	   (if (pmail-buffers-swapped-p)
+	       (point)))
 	  (predicate nil)			;< or string-lessp
 	  (sort-lists nil))
+      (pmail-swap-buffers-maybe)
       (message "Finding sort keys...")
       (widen)
       (let ((msgnum 1))
@@ -193,13 +192,15 @@
       ;; Now we enter critical region.  So, keyboard quit is disabled.
       (message "Reordering messages...")
       (let ((inhibit-quit t)		;Inhibit quit
+	    (inhibit-read-only t)
 	    (current-message nil)
 	    (msgnum 1)
 	    (msginfo nil))
 	;; There's little hope that we can easily undo after that.
 	(buffer-disable-undo (current-buffer))
 	(goto-char (pmail-msgbeg 1))
-	;; To force update of all markers.
+	;; To force update of all markers,
+	;; keep the new copies separated from the remaining old messages.
 	(insert-before-markers ?Z)
 	(backward-char 1)
 	;; Now reorder messages.
@@ -208,8 +209,10 @@
 	  (insert-buffer-substring
 	   (current-buffer) (nth 2 msginfo) (nth 3 msginfo))
 	  ;; The last message may not have \n\n after it.
-	  (unless (eq (char-before) ?\n)
-	    (insert "\n\n"))
+	  (unless (bobp)
+	    (insert "\n"))
+	  (unless (looking-back "\n\n")
+	    (insert "\n"))
 	  (delete-region (nth 2 msginfo) (nth 3 msginfo))
 	  ;; Is current message?
 	  (if (nth 1 msginfo)
@@ -217,24 +220,16 @@
 	  (if (zerop (% msgnum 10))
 	      (message "Reordering messages...%d" msgnum))
 	  (setq msgnum (1+ msgnum)))
-	;; Delete the garbage inserted before.
+	;; Delete the dummy separator Z inserted before.
 	(delete-char 1)
 	(setq quit-flag nil)
-	(buffer-enable-undo)
 	(pmail-set-message-counters)
 	(pmail-show-message current-message)
-	(goto-char (+ point-offset (point-min)))
+	(if return-to-point
+	    (goto-char return-to-point))
 	(if (pmail-summary-exists)
 	    (pmail-select-summary (pmail-update-summary)))))))
 
-(defun pmail-fetch-field (msg field)
-  "Return the value of the header FIELD of MSG.
-Arguments are MSG and FIELD."
-  (save-restriction
-    (widen)
-    (narrow-to-region (pmail-msgbeg msg) (pmail-msgend msg))
-    (pmail-header-get-header field)))
-
 (defun pmail-make-date-sortable (date)
   "Make DATE sortable using the function string-lessp."
   ;; Assume the default time zone is GMT.