rmailsum.el 40.7 KB
Newer Older
1
;;; rmailsum.el --- make summary buffers for the mail reader
2

3
;; Copyright (C) 1985, 1993 Free Software Foundation, Inc.
Eric S. Raymond's avatar
Eric S. Raymond committed
4

Eric S. Raymond's avatar
Eric S. Raymond committed
5
;; Maintainer: FSF
Eric S. Raymond's avatar
Eric S. Raymond committed
6
;; Keywords: mail
Eric S. Raymond's avatar
Eric S. Raymond committed
7

Jim Blandy's avatar
Jim Blandy committed
8 9 10 11
;; This file is part of GNU Emacs.

;; 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
Eric S. Raymond's avatar
Eric S. Raymond committed
12
;; the Free Software Foundation; either version 2, or (at your option)
Jim Blandy's avatar
Jim Blandy committed
13 14 15 16 17 18 19 20 21 22 23
;; 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
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; 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.

24 25
;;; Commentary:

26 27 28 29
;; Extended by Bob Weiner of Motorola
;;   Provided all commands from rmail-mode in rmail-summary-mode and made key
;;   bindings in both modes wholly compatible.

30 31
;;; Code:

32
;; Entry points for making a summary buffer.
Jim Blandy's avatar
Jim Blandy committed
33

34 35 36 37 38
;; Regenerate the contents of the summary
;; using the same selection criterion as last time.
;; M-x revert-buffer in a summary buffer calls this function.
(defun rmail-update-summary (&rest ignore)
  (apply (car rmail-summary-redo) (cdr rmail-summary-redo)))
Jim Blandy's avatar
Jim Blandy committed
39 40 41 42

(defun rmail-summary ()
  "Display a summary of all messages, one line per message."
  (interactive)
43
  (rmail-new-summary "All" '(rmail-summary) nil))
Jim Blandy's avatar
Jim Blandy committed
44 45 46 47 48 49 50 51 52 53

(defun rmail-summary-by-labels (labels)
  "Display a summary of all messages with one or more LABELS.
LABELS should be a string containing the desired labels, separated by commas."
  (interactive "sLabels to summarize by: ")
  (if (string= labels "")
      (setq labels (or rmail-last-multi-labels
		       (error "No label specified"))))
  (setq rmail-last-multi-labels labels)
  (rmail-new-summary (concat "labels " labels)
54
		     (list 'rmail-summary-by-labels labels)
Jim Blandy's avatar
Jim Blandy committed
55 56 57 58 59 60 61 62
		     'rmail-message-labels-p
		     (concat ", \\(" (mail-comma-list-regexp labels) "\\),")))

(defun rmail-summary-by-recipients (recipients &optional primary-only)
  "Display a summary of all messages with the given RECIPIENTS.
Normally checks the To, From and Cc fields of headers;
but if PRIMARY-ONLY is non-nil (prefix arg given),
 only look in the To and From fields.
63
RECIPIENTS is a string of regexps separated by commas."
Jim Blandy's avatar
Jim Blandy committed
64 65 66
  (interactive "sRecipients to summarize by: \nP")
  (rmail-new-summary
   (concat "recipients " recipients)
67
   (list 'rmail-summary-by-recipients recipients primary-only)
Jim Blandy's avatar
Jim Blandy committed
68 69 70 71 72 73 74 75 76 77 78
   'rmail-message-recipients-p
   (mail-comma-list-regexp recipients) primary-only))

(defun rmail-summary-by-regexp (regexp)
  "Display a summary of all messages according to regexp REGEXP.
If the regular expression is found in the header of the message
\(including in the date and other lines, as well as the subject line),
Emacs will list the header line in the RMAIL-summary."
  (interactive "sRegexp to summarize by: ")
  (if (string= regexp "")
      (setq regexp (or rmail-last-regexp
79
			 (error "No regexp specified."))))
Jim Blandy's avatar
Jim Blandy committed
80 81
  (setq rmail-last-regexp regexp)
  (rmail-new-summary (concat "regexp " regexp)
82
		     (list 'rmail-summary-by-regexp regexp)
Jim Blandy's avatar
Jim Blandy committed
83 84 85
		     'rmail-message-regexp-p
                     regexp))

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
;; rmail-summary-by-topic
;; 1989 R.A. Schnitzler

(defun rmail-summary-by-topic (subject &optional whole-message)
  "Display a summary of all messages with the given SUBJECT.
Normally checks the Subject field of headers;
but if WHOLE-MESSAGE is non-nil (prefix arg given), 
 look in the whole message.
SUBJECT is a string of regexps separated by commas."
  (interactive "sTopics to summarize by: \nP")
  (rmail-new-summary
   (concat "about " subject)
   (list 'rmail-summary-by-topic subject whole-message)
   'rmail-message-subject-p
   (mail-comma-list-regexp subject) whole-message))

(defun rmail-message-subject-p (msg subject &optional whole-message)
  (save-restriction
    (goto-char (rmail-msgbeg msg))
    (search-forward "\n*** EOOH ***\n")
    (narrow-to-region
     (point)
108
     (progn (search-forward (if whole-message "\^_" "\n\n")) (point)))
109 110 111
    (goto-char (point-min))
    (if whole-message (re-search-forward subject nil t)
      (string-match subject (or (mail-fetch-field "Subject") "")) )))
112 113 114 115 116 117 118

(defun rmail-summary-by-senders (senders)
  "Display a summary of all messages with the given SENDERS.
SENDERS is a string of names separated by commas."
  (interactive "sSenders to summarize by: ")
  (rmail-new-summary
   (concat "senders " senders)
119
   (list 'rmail-summary-by-senders senders)
120 121 122 123 124 125 126 127 128
   'rmail-message-senders-p
   (mail-comma-list-regexp senders)))

(defun rmail-message-senders-p (msg senders)
  (save-restriction
    (goto-char (rmail-msgbeg msg))
    (search-forward "\n*** EOOH ***\n")
    (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
    (string-match senders (or (mail-fetch-field "From") ""))))
Jim Blandy's avatar
Jim Blandy committed
129

130 131 132 133 134
;; General making of a summary buffer.

(defvar rmail-summary-symbol-number 0)

(defun rmail-new-summary (description redo-form function &rest args)
Jim Blandy's avatar
Jim Blandy committed
135 136 137 138 139 140
  "Create a summary of selected messages.
DESCRIPTION makes part of the mode line of the summary buffer.
For each message, FUNCTION 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."
  (message "Computing summary lines...")
141 142 143 144 145 146 147 148
  (let (sumbuf mesg was-in-summary)
    (save-excursion
      ;; Go to the Rmail buffer.
      (if (eq major-mode 'rmail-summary-mode)
	  (progn
	    (setq was-in-summary t)
	    (set-buffer rmail-buffer)))
      ;; Find its summary buffer, or make one.
149 150 151 152 153
      (setq sumbuf
	    (if (and rmail-summary-buffer
		     (buffer-name rmail-summary-buffer))
		rmail-summary-buffer
	      (generate-new-buffer (concat (buffer-name) "-summary"))))
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
      (setq mesg rmail-current-message)
      ;; Filter the messages; make or get their summary lines.
      (let ((summary-msgs ())
	    (new-summary-line-count 0))
	(let ((msgnum 1)
	      (buffer-read-only nil))
	  (save-restriction
	    (save-excursion
	      (widen)
	      (goto-char (point-min))
	      (while (>= rmail-total-messages msgnum)
		(if (or (null function)
			(apply function (cons msgnum args)))
		    (setq summary-msgs
			  (cons (cons msgnum (rmail-make-summary-line msgnum))
				summary-msgs)))
		(setq msgnum (1+ msgnum)))
	      (setq summary-msgs (nreverse summary-msgs)))))
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
	;; Temporarily, while summary buffer is unfinished,
	;; we "don't have" a summary.
	(setq rmail-summary-buffer nil)
	(save-excursion
	  (let ((rbuf (current-buffer))
		(total rmail-total-messages))
	    (set-buffer sumbuf)
	    ;; Set up the summary buffer's contents.
	    (let ((buffer-read-only nil))
	      (erase-buffer)
	      (while summary-msgs
		(princ (cdr (car summary-msgs)) sumbuf)
		(setq summary-msgs (cdr summary-msgs)))
	      (goto-char (point-min)))
	    ;; Set up the rest of its state and local variables.
	    (setq buffer-read-only t)
	    (rmail-summary-mode)
	    (make-local-variable 'minor-mode-alist)
190
	    (setq minor-mode-alist (list '(t (concat ": " description))))
191 192 193 194
	    (setq rmail-buffer rbuf
		  rmail-summary-redo redo-form
		  rmail-total-messages total))))
      (setq rmail-summary-buffer sumbuf))
195 196 197 198 199 200 201
    ;; Now display the summary buffer and go to the right place in it.
    (or was-in-summary
	(pop-to-buffer sumbuf))
    (rmail-summary-goto-msg mesg t t)
    (message "Computing summary lines...done")))

;; Low levels of generating a summary.
Jim Blandy's avatar
Jim Blandy committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

(defun rmail-make-summary-line (msg)
  (let ((line (or (aref rmail-summary-vector (1- msg))
		  (progn
		    (setq new-summary-line-count
			  (1+ new-summary-line-count))
		    (if (zerop (% new-summary-line-count 10))
			(message "Computing summary lines...%d"
				 new-summary-line-count))
		    (rmail-make-summary-line-1 msg)))))
    ;; Fix up the part of the summary that says "deleted" or "unseen".
    (aset line 4
	  (if (rmail-message-deleted-p msg) ?\D
	    (if (= ?0 (char-after (+ 3 (rmail-msgbeg msg))))
		?\- ?\ )))
    line))

(defun rmail-make-summary-line-1 (msg)
  (goto-char (rmail-msgbeg msg))
  (let* ((lim (save-excursion (forward-line 2) (point)))
	 pos
	 (labels
	  (progn
	    (forward-char 3)
	    (concat
;	     (if (save-excursion (re-search-forward ",answered," lim t))
;		 "*" "")
;	     (if (save-excursion (re-search-forward ",filed," lim t))
;		 "!" "")
	     (if (progn (search-forward ",,") (eolp))
		 ""
	       (concat "{"
		       (buffer-substring (point)
					 (progn (end-of-line) (point)))
		       "} ")))))
	 (line
	  (progn
	    (forward-line 1)
	    (if (looking-at "Summary-line: ")
		(progn
		  (goto-char (match-end 0))
		  (setq line
			(buffer-substring (point)
					  (progn (forward-line 1) (point)))))))))
    ;; Obsolete status lines lacking a # should be flushed.
    (and line
	 (not (string-match "#" line))
	 (progn
	   (delete-region (point)
			  (progn (forward-line -1) (point)))
	   (setq line nil)))
    ;; If we didn't get a valid status line from the message,
    ;; make a new one and put it in the message.
    (or line
	(let* ((case-fold-search t)
	       (next (rmail-msgend msg))
	       (beg (if (progn (goto-char (rmail-msgbeg msg))
			       (search-forward "\n*** EOOH ***\n" next t))
			(point)
		      (forward-line 1)
		      (point)))
	       (end (progn (search-forward "\n\n" nil t) (point))))
	  (save-restriction
	    (narrow-to-region beg end)
	    (goto-char beg)
	    (setq line (rmail-make-basic-summary-line)))
	  (goto-char (rmail-msgbeg msg))
	  (forward-line 2)
	  (insert "Summary-line: " line)))
    (setq pos (string-match "#" line))
    (aset rmail-summary-vector (1- msg)
	  (concat (format "%4d  " msg)
		  (substring line 0 pos)
		  labels
		  (substring line (1+ pos))))))

(defun rmail-make-basic-summary-line ()
  (goto-char (point-min))
  (concat (save-excursion
	    (if (not (re-search-forward "^Date:" nil t))
		"      "
	      (cond ((re-search-forward "\\([^0-9:]\\)\\([0-3]?[0-9]\\)\\([- \t_]+\\)\\([adfjmnos][aceopu][bcglnprtvy]\\)"
		      (save-excursion (end-of-line) (point)) t)
		     (format "%2d-%3s"
			     (string-to-int (buffer-substring
					     (match-beginning 2)
					     (match-end 2)))
			     (buffer-substring
			      (match-beginning 4) (match-end 4))))
		    ((re-search-forward "\\([^a-z]\\)\\([adfjmnos][acepou][bcglnprtvy]\\)\\([-a-z \t_]*\\)\\([0-9][0-9]?\\)"
		      (save-excursion (end-of-line) (point)) t)
		     (format "%2d-%3s"
			     (string-to-int (buffer-substring
					     (match-beginning 4)
					     (match-end 4)))
			     (buffer-substring
			      (match-beginning 2) (match-end 2))))
		    (t "??????"))))
	  "  "
	  (save-excursion
	    (if (not (re-search-forward "^From:[ \t]*" nil t))
		"                         "
	      (let* ((from (mail-strip-quoted-names
			    (buffer-substring
			     (1- (point))
			     (progn (end-of-line)
				    (skip-chars-backward " \t")
				    (point)))))
		     len mch lo)
		(if (string-match (concat "^"
					  (regexp-quote (user-login-name))
					  "\\($\\|@\\)")
				  from)
		    (save-excursion
		      (goto-char (point-min))
		      (if (not (re-search-forward "^To:[ \t]*" nil t))
			  nil
			(setq from
			      (concat "to: "
				      (mail-strip-quoted-names
				       (buffer-substring
					(point)
					(progn (end-of-line)
					       (skip-chars-backward " \t")
					       (point)))))))))
		(setq len (length from))
		(setq mch (string-match "[@%]" from))
		(format "%25s"
			(if (or (not mch) (<= len 25))
			    (substring from (max 0 (- len 25)))
			  (substring from
				     (setq lo (cond ((< (- mch 9) 0) 0)
						    ((< len (+ mch 16))
						     (- len 25))
						    (t (- mch 9))))
				     (min len (+ lo 25))))))))
	  "  #"
	  (if (re-search-forward "^Subject:" nil t)
	      (progn (skip-chars-forward " \t")
		     (buffer-substring (point)
				       (progn (end-of-line)
					      (point))))
	    (re-search-forward "[\n][\n]+" nil t)
	    (buffer-substring (point) (progn (end-of-line) (point))))
	  "\n"))
347 348

;; Simple motion in a summary buffer.
Jim Blandy's avatar
Jim Blandy committed
349 350 351

(defun rmail-summary-next-all (&optional number)
  (interactive "p")
352 353
  (forward-line (if number number 1))
  (display-buffer rmail-buffer))
Jim Blandy's avatar
Jim Blandy committed
354 355 356

(defun rmail-summary-previous-all (&optional number)
  (interactive "p")
357 358
  (forward-line (- (if number number 1)))
  (display-buffer rmail-buffer))
Jim Blandy's avatar
Jim Blandy committed
359 360

(defun rmail-summary-next-msg (&optional number)
361 362 363
  "Display next non-deleted msg from rmail file.
With optional prefix argument NUMBER, moves forward this number of non-deleted
messages, or backward if NUMBER is negative."
Jim Blandy's avatar
Jim Blandy committed
364 365
  (interactive "p")
  (forward-line 0)
366
  (and (> number 0) (end-of-line))
Jim Blandy's avatar
Jim Blandy committed
367 368
  (let ((count (if (< number 0) (- number) number))
	(search (if (> number 0) 're-search-forward 're-search-backward))
369 370
	(non-del-msg-found nil))
    (while (and (> count 0) (setq non-del-msg-found
371
				  (or (funcall search "^....[^D]" nil t)
372
				      non-del-msg-found)))
373
      (setq count (1- count))))
374
  (beginning-of-line)
375
  (display-buffer rmail-buffer))
Jim Blandy's avatar
Jim Blandy committed
376 377 378 379 380

(defun rmail-summary-previous-msg (&optional number)
  (interactive "p")
  (rmail-summary-next-msg (- (if number number 1))))

381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
(defun rmail-summary-next-labeled-message (n labels)
  "Show next message with LABEL.  Defaults to last labels used.
With prefix argument N moves forward N messages with these labels."
  (interactive "p\nsMove to next msg with labels: ")
  (save-excursion
    (set-buffer rmail-buffer)
    (rmail-next-labeled-message n labels)))

(defun rmail-summary-previous-labeled-message (n labels)
  "Show previous message with LABEL.  Defaults to last labels used.
With prefix argument N moves backward N messages with these labels."
  (interactive "p\nsMove to previous msg with labels: ")
  (save-excursion
    (set-buffer rmail-buffer)
    (rmail-previous-labeled-message n labels)))

;; Delete and undelete summary commands.

(defun rmail-summary-delete-forward (&optional backward)
  "Delete this message and move to next nondeleted one.
Deleted messages stay in the file until the \\[rmail-expunge] command is given.
With prefix argument, delete and move backward."
  (interactive "P")
Jim Blandy's avatar
Jim Blandy committed
404 405 406
  (let (end)
    (rmail-summary-goto-msg)
    (pop-to-buffer rmail-buffer)
407 408 409 410 411
    (rmail-delete-message)
    (let ((del-msg rmail-current-message))
      (pop-to-buffer rmail-summary-buffer)
      (rmail-summary-mark-deleted del-msg)
      (while (and (not (if backward (bobp) (eobp)))
412 413
		  (save-excursion (beginning-of-line)
				  (looking-at " +[0-9]+D")))
414
	(forward-line (if backward -1 1))))))
Jim Blandy's avatar
Jim Blandy committed
415 416

(defun rmail-summary-delete-backward ()
417 418
  "Delete this message and move to previous nondeleted one.
Deleted messages stay in the file until the \\[rmail-expunge] command is given."
Jim Blandy's avatar
Jim Blandy committed
419
  (interactive)
420
  (rmail-summary-delete-forward t))
Jim Blandy's avatar
Jim Blandy committed
421

422
(defun rmail-summary-mark-deleted (&optional n undel)
423 424 425 426 427 428 429 430 431 432
  (and n (rmail-summary-goto-msg n t t))
  (or (eobp)
      (let ((buffer-read-only nil))
	(skip-chars-forward " ")
	(skip-chars-forward "[0-9]")
	(if undel
	    (if (looking-at "D")
		(progn (delete-char 1) (insert " ")))
	  (delete-char 1)
	  (insert "D"))))
433 434 435 436 437 438 439 440 441 442 443
  (beginning-of-line))

(defun rmail-summary-mark-undeleted (n)
  (rmail-summary-mark-deleted n t))

(defun rmail-summary-deleted-p (&optional n)
  (save-excursion
    (and n (rmail-summary-goto-msg n nil t))
    (skip-chars-forward " ")
    (skip-chars-forward "[0-9]")
    (looking-at "D")))
Jim Blandy's avatar
Jim Blandy committed
444

445 446 447 448 449 450
(defun rmail-summary-undelete (&optional arg)
  "Undelete current message.
Optional prefix ARG means undelete ARG previous messages."
  (interactive "p")
  (if (/= arg 1)
      (rmail-summary-undelete-many arg)
451 452
    (let ((buffer-read-only nil)
	  (opoint (point)))
453 454 455 456 457 458 459
      (end-of-line)
      (cond ((re-search-backward "\\(^ *[0-9]*\\)\\(D\\)" nil t)
	     (replace-match "\\1 ")
	     (rmail-summary-goto-msg)
	     (pop-to-buffer rmail-buffer)
	     (and (rmail-message-deleted-p rmail-current-message)
		  (rmail-undelete-previous-message))
460 461
	     (pop-to-buffer rmail-summary-buffer))
	    (t (goto-char opoint))))))
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487

(defun rmail-summary-undelete-many (&optional n)
  "Undelete all deleted msgs, optional prefix arg N means undelete N prev msgs."
  (interactive "P")
  (save-excursion
    (set-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 "deleted" 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)))

Jim Blandy's avatar
Jim Blandy committed
488 489 490 491
;; Rmail Summary mode is suitable only for specially formatted data.
(put 'rmail-summary-mode 'mode-class 'special)

(defun rmail-summary-mode ()
492 493 494 495 496
  "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.

All normal editing commands are turned off.
497 498
Instead, nearly all the Rmail mode commands are available,
though many of them move only among the messages in the summary.
499

500 501 502 503 504 505 506 507 508 509 510 511
These additional commands exist:

\\[rmail-summary-undelete-many]	Undelete all or prefix arg deleted messages.
\\[rmail-summary-wipe] Delete the summary and go to the Rmail buffer.

Commands for sorting the summary:

\\[rmail-summary-sort-by-date] Sort by date.
\\[rmail-summary-sort-by-subject] Sort by subject.
\\[rmail-summary-sort-by-author] Sort by author.
\\[rmail-summary-sort-by-recipient] Sort by recipient.
\\[rmail-summary-sort-by-correspondent] Sort by correspondent.
512 513
\\[rmail-summary-sort-by-lines] Sort by lines.
\\[rmail-summary-sort-by-keywords] Sort by keywords."
Jim Blandy's avatar
Jim Blandy committed
514 515 516 517 518 519 520
  (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)
521 522 523 524 525 526 527 528
  (make-local-variable 'rmail-buffer)
  (make-local-variable 'rmail-total-messages)
  (make-local-variable 'rmail-current-message)
  (setq rmail-current-message nil)
  (make-local-variable 'rmail-summary-redo)
  (setq rmail-summary-redo nil)
  (make-local-variable 'revert-buffer-function)
  (make-local-variable 'post-command-hook)
529
  (rmail-summary-enable)
Jim Blandy's avatar
Jim Blandy committed
530 531
  (run-hooks 'rmail-summary-mode-hook))

532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
;; Summary features need to be disabled during edit mode.
(defun rmail-summary-disable ()
  (save-excursion
    (set-buffer rmail-summary-buffer)
    (use-local-map text-mode-map)
    (remove-hook 'post-command-hook 'rmail-summary-rmail-update)
    (setq revert-buffer-function nil)))

(defun rmail-summary-enable ()
  (save-excursion
    (set-buffer rmail-summary-buffer)
    (use-local-map rmail-summary-mode-map)
    (add-hook 'post-command-hook 'rmail-summary-rmail-update)
    (setq revert-buffer-function 'rmail-update-summary)))

547 548
;; Show in Rmail the message described by the summary line that point is on,
;; but only if the Rmail buffer is already visible.
549 550
;; This is a post-command-hook in summary buffers.
(defun rmail-summary-rmail-update ()
551 552 553
  (if (get-buffer-window rmail-buffer)
      (let (buffer-read-only)
	(save-excursion
554 555 556
	  ;; If at end of buffer, pretend we are on the last text line.
	  (if (eobp)
	      (forward-line -1))
557 558 559 560 561 562 563 564
	  (beginning-of-line)
	  (skip-chars-forward " ")
	  (let ((beg (point))
		msg-num
		(buf rmail-buffer))
	    (skip-chars-forward "0-9")
	    (setq msg-num (string-to-int (buffer-substring beg (point))))
	    (or (eq rmail-current-message msg-num)
565
		(let (go-where window (owin (selected-window)))
566 567 568 569 570 571
		  (setq rmail-current-message msg-num)
		  (if (= (following-char) ?-)
		      (progn
			(delete-char 1)
			(insert " ")))
		  (setq window (display-buffer rmail-buffer))
572 573 574 575 576 577 578
		  ;; Using save-window-excursion caused the new value
		  ;; of point to get lost.
		  (unwind-protect
		      (progn
			(select-window window)
			(rmail-show-message msg-num))
		    (select-window owin)))))))))
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627

(defvar rmail-summary-mode-map nil)

(if rmail-summary-mode-map
    nil
  (setq rmail-summary-mode-map (make-keymap))
  (suppress-keymap rmail-summary-mode-map)
  (define-key rmail-summary-mode-map "a"      'rmail-summary-add-label)
  (define-key rmail-summary-mode-map "c"      'rmail-summary-continue)
  (define-key rmail-summary-mode-map "d"      'rmail-summary-delete-forward)
  (define-key rmail-summary-mode-map "\C-d"   'rmail-summary-delete-backward)
  (define-key rmail-summary-mode-map "e"      'rmail-summary-edit-current-message)
  (define-key rmail-summary-mode-map "f"      'rmail-summary-forward)
  (define-key rmail-summary-mode-map "g"      'rmail-summary-get-new-mail)
  (define-key rmail-summary-mode-map "h"      'rmail-summary)
  (define-key rmail-summary-mode-map "i"      'rmail-summary-input)
  (define-key rmail-summary-mode-map "j"      'rmail-summary-goto-msg)
  (define-key rmail-summary-mode-map "k"      'rmail-summary-kill-label)
  (define-key rmail-summary-mode-map "l"      'rmail-summary-by-labels)
  (define-key rmail-summary-mode-map "\e\C-h" 'rmail-summary)
  (define-key rmail-summary-mode-map "\e\C-l" 'rmail-summary-by-labels)
  (define-key rmail-summary-mode-map "\e\C-r" 'rmail-summary-by-recipients)
  (define-key rmail-summary-mode-map "\e\C-s" 'rmail-summary-by-regexp)
  (define-key rmail-summary-mode-map "\e\C-t" 'rmail-summary-by-topic)
  (define-key rmail-summary-mode-map "m"      'rmail-summary-mail)
  (define-key rmail-summary-mode-map "\M-m"   'rmail-summary-retry-failure)
  (define-key rmail-summary-mode-map "n"      'rmail-summary-next-msg)
  (define-key rmail-summary-mode-map "\en"    'rmail-summary-next-all)
  (define-key rmail-summary-mode-map "\e\C-n" 'rmail-summary-next-labeled-message)
  (define-key rmail-summary-mode-map "o"      'rmail-summary-output-to-rmail-file)
  (define-key rmail-summary-mode-map "\C-o"   'rmail-summary-output)
  (define-key rmail-summary-mode-map "p"      'rmail-summary-previous-msg)
  (define-key rmail-summary-mode-map "\ep"    'rmail-summary-previous-all)
  (define-key rmail-summary-mode-map "\e\C-p" 'rmail-summary-previous-labeled-message)
  (define-key rmail-summary-mode-map "q"      'rmail-summary-quit)
  (define-key rmail-summary-mode-map "r"      'rmail-summary-reply)
  (define-key rmail-summary-mode-map "s"      'rmail-summary-expunge-and-save)
  (define-key rmail-summary-mode-map "\es"    'rmail-summary-search)
  (define-key rmail-summary-mode-map "t"      'rmail-summary-toggle-header)
  (define-key rmail-summary-mode-map "u"      'rmail-summary-undelete)
  (define-key rmail-summary-mode-map "\M-u"   'rmail-summary-undelete-many)
  (define-key rmail-summary-mode-map "w"      'rmail-summary-wipe)
  (define-key rmail-summary-mode-map "x"      'rmail-summary-expunge)
  (define-key rmail-summary-mode-map "."      'rmail-summary-beginning-of-message)
  (define-key rmail-summary-mode-map "<"      'rmail-summary-first-message)
  (define-key rmail-summary-mode-map ">"      'rmail-summary-last-message)
  (define-key rmail-summary-mode-map " "      'rmail-summary-scroll-msg-up)
  (define-key rmail-summary-mode-map "\177"   'rmail-summary-scroll-msg-down)
  (define-key rmail-summary-mode-map "?"      'describe-mode)
628 629 630 631 632 633 634 635 636 637 638 639
  (define-key rmail-summary-mode-map "\C-c\C-s\C-d"
    'rmail-summary-sort-by-date)
  (define-key rmail-summary-mode-map "\C-c\C-s\C-s"
    'rmail-summary-sort-by-subject)
  (define-key rmail-summary-mode-map "\C-c\C-s\C-a"
    'rmail-summary-sort-by-author)
  (define-key rmail-summary-mode-map "\C-c\C-s\C-r"
    'rmail-summary-sort-by-recipient)
  (define-key rmail-summary-mode-map "\C-c\C-s\C-c"
    'rmail-summary-sort-by-correspondent)
  (define-key rmail-summary-mode-map "\C-c\C-s\C-l"
    'rmail-summary-sort-by-lines)
640 641
  (define-key rmail-summary-mode-map "\C-c\C-s\C-k"
    'rmail-summary-sort-by-keywords)
642 643
  )

644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
;;; Menu bar bindings.

(define-key rmail-summary-mode-map [menu-bar] (make-sparse-keymap))

(define-key rmail-summary-mode-map [menu-bar classify]
  (cons "Classify" (make-sparse-keymap "Classify")))

(define-key rmail-summary-mode-map [menu-bar classify output-inbox]
  '("Output (inbox)" . rmail-summary-output))

(define-key rmail-summary-mode-map [menu-bar classify output]
  '("Output (Rmail)" . rmail-summary-output-to-rmail-file))

(define-key rmail-summary-mode-map [menu-bar classify kill-label]
  '("Kill Label" . rmail-summary-kill-label))

(define-key rmail-summary-mode-map [menu-bar classify add-label]
  '("Add Label" . rmail-summary-add-label))

(define-key rmail-summary-mode-map [menu-bar summary]
  (cons "Summary" (make-sparse-keymap "Summary")))

(define-key rmail-summary-mode-map [menu-bar summary labels]
  '("By Labels" . rmail-summary-by-labels))

(define-key rmail-summary-mode-map [menu-bar summary recipients]
  '("By Recipients" . rmail-summary-by-recipients))

(define-key rmail-summary-mode-map [menu-bar summary topic]
  '("By Topic" . rmail-summary-by-topic))

(define-key rmail-summary-mode-map [menu-bar summary regexp]
  '("By Regexp" . rmail-summary-by-regexp))

(define-key rmail-summary-mode-map [menu-bar summary all]
  '("All" . rmail-summary))

(define-key rmail-summary-mode-map [menu-bar mail]
  (cons "Mail" (make-sparse-keymap "Mail")))

(define-key rmail-summary-mode-map [menu-bar mail continue]
  '("Continue" . rmail-summary-continue))

(define-key rmail-summary-mode-map [menu-bar mail forward]
  '("Forward" . rmail-summary-forward))

(define-key rmail-summary-mode-map [menu-bar mail retry]
  '("Retry" . rmail-summary-retry-failure))

(define-key rmail-summary-mode-map [menu-bar mail reply]
  '("Reply" . rmail-summary-reply))

(define-key rmail-summary-mode-map [menu-bar mail mail]
  '("Mail" . rmail-summary-mail))

(define-key rmail-summary-mode-map [menu-bar delete]
  (cons "Delete" (make-sparse-keymap "Delete")))

(define-key rmail-summary-mode-map [menu-bar delete expunge/save]
  '("Expunge/Save" . rmail-summary-expunge-and-save))

(define-key rmail-summary-mode-map [menu-bar delete expunge]
  '("Expunge" . rmail-summary-expunge))

(define-key rmail-summary-mode-map [menu-bar delete undelete]
  '("Undelete" . rmail-summary-undelete))

(define-key rmail-summary-mode-map [menu-bar delete delete]
  '("Delete" . rmail-summary-delete-forward))

(define-key rmail-summary-mode-map [menu-bar move]
  (cons "Move" (make-sparse-keymap "Move")))

(define-key rmail-summary-mode-map [menu-bar move search-back]
  '("Search Back" . rmail-summary-search-backward))

(define-key rmail-summary-mode-map [menu-bar move search]
  '("Search" . rmail-summary-search))

(define-key rmail-summary-mode-map [menu-bar move previous]
  '("Previous Nondeleted" . rmail-summary-previous-msg))

(define-key rmail-summary-mode-map [menu-bar move next]
  '("Next Nondeleted" . rmail-summary-next-msg))

(define-key rmail-summary-mode-map [menu-bar move last]
  '("Last" . rmail-summary-last-message))

(define-key rmail-summary-mode-map [menu-bar move first]
  '("First" . rmail-summary-first-message))

(define-key rmail-summary-mode-map [menu-bar move previous]
  '("Previous" . rmail-summary-previous-all))

(define-key rmail-summary-mode-map [menu-bar move next]
  '("Next" . rmail-summary-next-all))

741 742
(defvar rmail-summary-overlay nil)

743
(defun rmail-summary-goto-msg (&optional n nowarn skip-rmail)
Jim Blandy's avatar
Jim Blandy committed
744 745 746 747 748 749
  (interactive "P")
  (if (consp n) (setq n (prefix-numeric-value n)))
  (if (eobp) (forward-line -1))
  (beginning-of-line)
  (let ((buf rmail-buffer)
	(cur (point))
750
	message-not-found
Jim Blandy's avatar
Jim Blandy committed
751 752 753
	(curmsg (string-to-int
		 (buffer-substring (point)
				   (min (point-max) (+ 5 (point)))))))
754 755 756 757
    ;; 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.
    ;; set N by default.
Jim Blandy's avatar
Jim Blandy committed
758 759 760 761 762 763 764 765 766 767 768 769 770
    (if (not n)
	(setq n curmsg)
      (if (< n 1)
	  (progn (message "No preceding message")
		 (setq n 1)))
      (if (> n rmail-total-messages)
	  (progn (message "No following message")
		 (goto-char (point-max))
		 (rmail-summary-goto-msg)))
      (goto-char (point-min))
      (if (not (re-search-forward (concat "^ *" (int-to-string n)) nil t))
	  (progn (or nowarn (message "Message %d not found" n))
		 (setq n curmsg)
771
		 (setq message-not-found t)
Jim Blandy's avatar
Jim Blandy committed
772 773 774 775 776 777 778 779
		 (goto-char cur))))
    (beginning-of-line)
    (skip-chars-forward " ")
    (skip-chars-forward "0-9")
    (save-excursion (if (= (following-char) ?-)
			(let ((buffer-read-only nil))
			  (delete-char 1)
			  (insert " "))))
780 781 782 783 784 785 786 787 788 789 790
    ;; Make sure we have an overlay to use.
    (or rmail-summary-overlay
	(progn
	  (make-local-variable 'rmail-summary-overlay)
	  (setq rmail-summary-overlay (make-overlay (point) (point)))))
    ;; If this message is in the summary, use the overlay to highlight it.
    ;; Otherwise, don't highlight anything.
    (if message-not-found
	(overlay-put rmail-summary-overlay 'face nil)
      (move-overlay rmail-summary-overlay
		    (save-excursion (beginning-of-line) (1+ (point)))
791
		    (save-excursion (end-of-line) (point)))
792
      (overlay-put rmail-summary-overlay 'face 'highlight))
Jim Blandy's avatar
Jim Blandy committed
793
    (beginning-of-line)
794 795
    (if skip-rmail
	nil
796 797 798 799 800
      (let ((selwin (selected-window)))
	(unwind-protect
	    (progn (pop-to-buffer buf)
		   (rmail-show-message n))
	  (select-window selwin))))))
801

Jim Blandy's avatar
Jim Blandy committed
802 803 804
(defun rmail-summary-scroll-msg-up (&optional dist)
  "Scroll other window forward."
  (interactive "P")
805
  (scroll-other-window dist))
Jim Blandy's avatar
Jim Blandy committed
806 807 808 809

(defun rmail-summary-scroll-msg-down (&optional dist)
  "Scroll other window backward."
  (interactive "P")
810 811 812 813 814 815 816 817 818 819 820
  (scroll-other-window
   (cond ((eq dist '-) nil)
	 ((null dist) '-)
	 (t (- (prefix-numeric-value dist))))))

(defun rmail-summary-beginning-of-message ()
  "Show current message from the beginning."
  (interactive)
  (pop-to-buffer rmail-buffer)
  (beginning-of-buffer)
  (pop-to-buffer rmail-summary-buffer))
Jim Blandy's avatar
Jim Blandy committed
821 822

(defun rmail-summary-quit ()
823
  "Quit out of Rmail and Rmail summary."
Jim Blandy's avatar
Jim Blandy committed
824
  (interactive)
825
  (rmail-summary-wipe)
Jim Blandy's avatar
Jim Blandy committed
826 827
  (rmail-quit))

828 829
(defun rmail-summary-wipe ()
  "Kill and wipe away Rmail summary, remaining within Rmail."
Jim Blandy's avatar
Jim Blandy committed
830
  (interactive)
831
  (save-excursion (set-buffer rmail-buffer) (setq rmail-summary-buffer nil))
832
  (let ((local-rmail-buffer rmail-buffer))
833 834 835 836
    (kill-buffer (current-buffer))
    ;; Delete window if not only one.
    (if (not (eq (selected-window) (next-window nil 'no-minibuf)))
	(delete-window))
837 838
    ;; Switch windows to the rmail buffer, or switch to it in this window.
    (pop-to-buffer local-rmail-buffer)))
839 840 841 842 843 844 845 846 847 848 849 850 851 852

(defun rmail-summary-expunge ()
  "Actually erase all deleted messages and recompute summary headers."
  (interactive)
  (save-excursion
    (set-buffer rmail-buffer)
    (rmail-only-expunge))
  (rmail-update-summary))

(defun rmail-summary-expunge-and-save ()
  "Expunge and save RMAIL file."
  (interactive)
  (save-excursion
    (set-buffer rmail-buffer)
853 854 855
    (rmail-only-expunge))
  (rmail-update-summary)
  (save-excursion
856
    (set-buffer rmail-buffer)
857 858
    (save-buffer))
  (set-buffer-modified-p nil))
859 860 861 862

(defun rmail-summary-get-new-mail ()
  "Get new mail and recompute summary headers."
  (interactive)
863 864 865 866 867 868 869 870
  (let (msg)
    (save-excursion
      (set-buffer rmail-buffer)
      (rmail-get-new-mail)
      ;; Get the proper new message number.
      (setq msg rmail-current-message))
    ;; Make sure that message is displayed.
    (rmail-summary-goto-msg msg)))
871 872 873 874

(defun rmail-summary-input (filename)
  "Run Rmail on file FILENAME."
  (interactive "FRun rmail on RMAIL file: ")
875 876 877
  ;; We switch windows here, then display the other Rmail file there.
  (pop-to-buffer rmail-buffer)
  (rmail filename))
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893

(defun rmail-summary-first-message ()
  "Show first message in Rmail file from summary buffer."
  (interactive)
  (beginning-of-buffer))

(defun rmail-summary-last-message ()
  "Show last message in Rmail file from summary buffer."
  (interactive)
  (end-of-buffer)
  (forward-line -1))

(defvar rmail-summary-edit-map nil)
(if rmail-summary-edit-map
    nil
  (setq rmail-summary-edit-map
894
	(nconc (make-sparse-keymap) text-mode-map))
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
  (define-key rmail-summary-edit-map "\C-c\C-c" 'rmail-cease-edit)
  (define-key rmail-summary-edit-map "\C-c\C-]" 'rmail-abort-edit))

(defun rmail-summary-edit-current-message ()
  "Edit the contents of this message."
  (interactive)
  (pop-to-buffer rmail-buffer)
  (rmail-edit-current-message)
  (use-local-map rmail-summary-edit-map))

(defun rmail-summary-cease-edit ()
  "Finish editing message, then go back to Rmail summary buffer."
  (interactive)
  (rmail-cease-edit)
  (pop-to-buffer rmail-summary-buffer))

(defun rmail-summary-abort-edit ()
  "Abort edit of current message; restore original contents.
Go back to summary buffer."
  (interactive)
  (rmail-abort-edit)
  (pop-to-buffer rmail-summary-buffer))

918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
(defun rmail-summary-search-backward (regexp &optional n)
  "Show message containing next match for REGEXP.
Prefix argument gives repeat count; negative argument means search
backwards (through earlier messages).
Interactively, empty argument means use same regexp used last time."
  (interactive
    (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
	   (prompt
	    (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
	   regexp)
      (if rmail-search-last-regexp
	  (setq prompt (concat prompt
			       "(default "
			       rmail-search-last-regexp
			       ") ")))
      (setq regexp (read-string prompt))
      (cond ((not (equal regexp ""))
	     (setq rmail-search-last-regexp regexp))
	    ((not rmail-search-last-regexp)
	     (error "No previous Rmail search string")))
      (list rmail-search-last-regexp
	    (prefix-numeric-value current-prefix-arg))))
  ;; Don't use save-excursion because that prevents point from moving
  ;; properly in the summary buffer.
  (let ((buffer (current-buffer)))
    (unwind-protect
	(progn
	  (set-buffer rmail-buffer)
	  (rmail-search regexp (- n)))
      (set-buffer buffer))))

949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
(defun rmail-summary-search (regexp &optional n)
  "Show message containing next match for REGEXP.
Prefix argument gives repeat count; negative argument means search
backwards (through earlier messages).
Interactively, empty argument means use same regexp used last time."
  (interactive
    (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
	   (prompt
	    (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
	   regexp)
      (if rmail-search-last-regexp
	  (setq prompt (concat prompt
			       "(default "
			       rmail-search-last-regexp
			       ") ")))
      (setq regexp (read-string prompt))
      (cond ((not (equal regexp ""))
	     (setq rmail-search-last-regexp regexp))
	    ((not rmail-search-last-regexp)
	     (error "No previous Rmail search string")))
      (list rmail-search-last-regexp
	    (prefix-numeric-value current-prefix-arg))))
971 972 973 974 975 976 977 978
  ;; Don't use save-excursion because that prevents point from moving
  ;; properly in the summary buffer.
  (let ((buffer (current-buffer)))
    (unwind-protect
	(progn
	  (set-buffer rmail-buffer)
	  (rmail-search regexp n))
      (set-buffer buffer))))
979 980 981 982 983 984 985 986 987 988 989

(defun rmail-summary-toggle-header ()
  "Show original message header if pruned header currently shown, or vice versa."
  (interactive)
  (save-excursion
    (set-buffer rmail-buffer)
    (rmail-toggle-header)))

(defun rmail-summary-add-label (label)
  "Add LABEL to labels associated with current Rmail message.
Completion is performed over known labels when reading."
990 991 992
  (interactive (list (save-excursion
		       (set-buffer rmail-buffer)
		       (rmail-read-label "Add label"))))
993 994 995 996 997 998 999
  (save-excursion
    (set-buffer rmail-buffer)
    (rmail-add-label label)))

(defun rmail-summary-kill-label (label)
  "Remove LABEL from labels associated with current Rmail message.
Completion is performed over known labels when reading."
1000 1001 1002
  (interactive (list (save-excursion
		       (set-buffer rmail-buffer)
		       (rmail-read-label "Kill label"))))
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
  (save-excursion
    (set-buffer rmail-buffer)
    (rmail-set-label label nil)))

;;;; *** Rmail Summary Mailing Commands ***

(defun rmail-summary-mail ()
  "Send mail in another window.
While composing the message, use \\[mail-yank-original] to yank the
original message into it."
  (interactive)
1014
  (rmail-start-mail nil nil nil nil nil rmail-buffer)
1015 1016 1017 1018 1019 1020 1021
  (use-local-map (copy-keymap (current-local-map)))
  (define-key (current-local-map)
    "\C-c\C-c" 'rmail-summary-send-and-exit))

(defun rmail-summary-continue ()
  "Continue composing outgoing message previously being composed."
  (interactive)
1022
  (rmail-start-mail t))
1023 1024 1025 1026

(defun rmail-summary-reply (just-sender)
  "Reply to the current message.
Normally include CC: to all other recipients of original message;
1027 1028
prefix argument means ignore them.  While composing the reply,
use \\[mail-yank-original] to yank the original message into it."
1029
  (interactive "P")
1030 1031 1032 1033 1034
  (set-buffer rmail-buffer)
  (rmail-reply just-sender)
  (use-local-map (copy-keymap (current-local-map)))
  (define-key (current-local-map)
    "\C-c\C-c" 'rmail-summary-send-and-exit))
1035 1036 1037 1038 1039 1040

(defun rmail-summary-retry-failure ()
  "Edit a mail message which is based on the contents of the current message.
For a message rejected by the mail system, extract the interesting headers and
the body of the original message; otherwise copy the current message."
  (interactive)
1041 1042 1043 1044 1045
  (set-buffer rmail-buffer)
  (rmail-retry-failure)
  (use-local-map (copy-keymap (current-local-map)))
  (define-key (current-local-map)
    "\C-c\C-c" 'rmail-summary-send-and-exit))
1046 1047 1048 1049 1050 1051

(defun rmail-summary-send-and-exit ()
  "Send mail reply and return to summary buffer."
  (interactive)
  (mail-send-and-exit t))

1052 1053 1054 1055 1056
(defun rmail-summary-forward (resend)
  "Forward the current message to another user.
With prefix argument, \"resend\" the message instead of forwarding it;
see the documentation of `rmail-resend'."
  (interactive "P")
1057 1058
  (save-excursion
    (set-buffer rmail-buffer)
1059
    (rmail-forward resend)
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
    (use-local-map (copy-keymap (current-local-map)))
    (define-key (current-local-map)
      "\C-c\C-c" 'rmail-summary-send-and-exit)))

;; Summary output commands.

(defun rmail-summary-output-to-rmail-file ()
  "Append the current message to an Rmail file named FILE-NAME.
If the file does not exist, ask if it should be created.
If file is being visited, the message is appended to the Emacs
buffer visiting that file."
  (interactive)
  (save-excursion
    (set-buffer rmail-buffer)
1074 1075 1076
    (let ((rmail-delete-after-output nil))
      (call-interactively 'rmail-output-to-rmail-file)))
  (if rmail-delete-after-output
1077
      (rmail-summary-delete-forward nil)))
1078 1079 1080 1081 1082 1083

(defun rmail-summary-output ()
  "Append this message to Unix mail file named FILE-NAME."
  (interactive)
  (save-excursion
    (set-buffer rmail-buffer)
1084 1085 1086
    (let ((rmail-delete-after-output nil))
      (call-interactively 'rmail-output)))
  (if rmail-delete-after-output
1087
      (rmail-summary-delete-forward nil)))
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126

;; Sorting messages in Rmail Summary buffer.

(defun rmail-summary-sort-by-date (reverse)
  "Sort messages of current Rmail summary by date.
If prefix argument REVERSE is non-nil, sort them in reverse order."
  (interactive "P")
  (rmail-sort-from-summary (function rmail-sort-by-date) reverse))

(defun rmail-summary-sort-by-subject (reverse)
  "Sort messages of current Rmail summary by subject.
If prefix argument REVERSE is non-nil, sort them in reverse order."
  (interactive "P")
  (rmail-sort-from-summary (function rmail-sort-by-subject) reverse))

(defun rmail-summary-sort-by-author (reverse)
  "Sort messages of current Rmail summary by author.
If prefix argument REVERSE is non-nil, sort them in reverse order."
  (interactive "P")
  (rmail-sort-from-summary (function rmail-sort-by-author) reverse))

(defun rmail-summary-sort-by-recipient (reverse)
  "Sort messages of current Rmail summary by recipient.
If prefix argument REVERSE is non-nil, sort them in reverse order."
  (interactive "P")
  (rmail-sort-from-summary (function rmail-sort-by-recipient) reverse))

(defun rmail-summary-sort-by-correspondent (reverse)
  "Sort messages of current Rmail summary by other correspondent.
If prefix argument REVERSE is non-nil, sort them in reverse order."
  (interactive "P")
  (rmail-sort-from-summary (function rmail-sort-by-correspondent) reverse))

(defun rmail-summary-sort-by-lines (reverse)
  "Sort messages of current Rmail summary by lines of the message.
If prefix argument REVERSE is non-nil, sort them in reverse order."
  (interactive "P")
  (rmail-sort-from-summary (function rmail-sort-by-lines) reverse))

1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
(defun rmail-summary-sort-by-keywords (reverse labels)
  "Sort messages of current Rmail summary by keywords.
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: ")
  (rmail-sort-from-summary
   (function (lambda (reverse)
	       (rmail-sort-by-keywords reverse labels)))
   reverse))

1137 1138 1139
(defun rmail-sort-from-summary (sortfun reverse)
  "Sort Rmail messages from Summary buffer and update it after sorting."
  (require 'rmailsort)
1140 1141 1142 1143 1144
  (let ((selwin (selected-window)))
    (unwind-protect
	(progn (pop-to-buffer rmail-buffer)
	       (funcall sortfun reverse))
      (select-window selwin))))
Eric S. Raymond's avatar
Eric S. Raymond committed
1145 1146

;;; rmailsum.el ends here