[net.emacs] upgraded rmail.ml

koomen@rochester.UUCP (Hans Koomen) (03/10/84)

From: Hans.Koomen
Because of the overwhelming response to my anouncement a while back, I
am including my version of rmail.ml below. I will not be mailing it to
indivuals! Also, there are too many changes to use diff output.
Enjoy, and be sure to mail bugs, complaints and praise to 

-- Hans (Koomen@Rochester,  {allegra,seismo}!rochester!koomen)

;
; Koomen's rmail:
;
(message "Loading the mail system, please wait...")
(sit-for 0)
; Unix Emacs RMail facility.  implements rmail (read mail) and
; smail (send mail).

; "rmail" is used for reading mail.  Executing it places your incoming
; mailbox into a window and enters a special command interpretation loop.
; The commands that it understands are:
;  p	move to the previous message.
;  n	move to the next message.
;  f	move forward in the current message.
;  b	move backward in the current message.
;  d	delete the current message.
;  u	undelete the last deleted message.
;  r	reply to the current message.
;  q	quit out of RMail, appending all undeleted messages to mbox.

; ************************************************************************
; Fri Feb  3 00:43:21 1984  *  Koomen@Rochester  *  multi-mailfolder upgrade
; To make mail filing easier I gave rmail a multi-mailfolder capability. 
; Messages can be moved from the standard inbox to dedicated message folders 
; but not by appending as was the case but intact. Each message folder is
; implemented as a Unix subdirectory of $user/Messages containing its own
; Directory file. When moving a message rmail asks for a message folder
; (defaulting to the one last used), creates a link of the current message
; in the indicated directory, updates the appropriate Directory file, and
; marks the current message for deletion in the current Directory. One can
; move from message folder to message folder to inspect filed messages.
; The following commands interface with this new capability
;  m    move message to different message folder
;  ^Xr  read different message folder
;  ^Xm  mail new message (used to be 'm')
; Fri Feb  3 00:56:44 1984  *  Koomen@Rochester  *  additional upgrades
; More message header stripping. Only displays To, From, Subject, Date and 
; possibly Reply-To and In-Reply-To lines. Header info is kept, and can be 
; examined by
;  h	restore full message header
;  H	strip message header
; As a bonus I've thrown in display of the current message number on the
; Directory buffer line (useful in combination with the rmail-goto-message
; command 'g').
; I also got rmail to display the directory window at half the size of the
; current message window, and to depict graphically what proportion of the 
; message is currently visible.
; ************************************************************************

; "smail" is used for sending mail.  It places you in a buffer for
; constructing the message and locally defines a few commands:
;  ^X^S	send the mail -- if all went well the window will disappear,
;	otherwise a message indicating which addresses failed will appear
;	at the bottom of the acreen.  Unfortunatly, the way the mailers on
;	Unix work, the message will have been sent to those addresses which
;	succeded and not to the others, so you have to delete some
;	addresses and fix up the others before you resend the message.
;  ^Xt	positions you in the To: field of the message.
;  ^Xc	positions you in the Cc: field of the message, creating it if it
;	doesn't already exist.
; 		The abbrev facility is used for mail address expansion,
; 		the file /usr/local/lib/emacs/RMailAbbrevs should contain
; 		abbrev definitions to expand login names to their
;		proper mail address.  This gets used at CMU since we have
;		7 VAXen, 4 10's and countless 11's;  remembering where a
;		person usually logs in is nearly impossible.
;  ^Xs	positions you in the Subject: field of the message.
;  ^Xa	positions you to the end of the body of the message, ready to
; 	append more text.

(defun
    (rmail			; The top level mail reader
		origfolder curfolder savefolder origfolderpath curfolderpath
		entry-name header-is-stripped current-message-number
	(save-window-excursion
	    (setq origfolder "Messages")
	    (setq curfolder origfolder)
	    (setq savefolder "")
	    (setq origfolderpath (concat (getenv "HOME") "/Messages"))
	    (setq curfolderpath origfolderpath)
	    (message "Please wait while I read mail folder " curfolder "...")
	    (sit-for 0)
	    (pop-to-buffer "rmail-directory")
	    (setq case-fold-search 1)
	    (setq needs-checkpointing 0)
	    (delete-other-windows)
	    (split-current-window)
	    (previous-window)
	    (rmail-adjust-dirwindow)
	    (rmail-read-mail-folder)
	    (message "Type ^C to exit rmail; ? for help")
	    (recursive-edit)
	    (pop-to-buffer "rmail-directory")
	    (erase-messages)
	    (if buffer-is-modified (write-current-file))
	)
	(novalue)
    )

    (rmail-adjust-dirwindow dirwinsize curmeswinsize
	(error-occured
	    (save-excursion
		(pop-to-buffer "current-message")
		(setq curmeswinsize (window-height))
		(pop-to-buffer "rmail-directory")
		(setq dirwinsize (/ (+ curmeswinsize (window-height)) 3))
		(if (< dirwinsize 3)
		    (setq dirwinsize 3))
		(while (< dirwinsize (window-height))
		       (shrink-window))))
    )

    (rmail-read-mail-folder
	(if (= curfolder origfolder)
	    (progn
		  (set-mark)
		  (filter-region
		      (concat "/usr/local/lib/emacs/may83/collectmail "
			      curfolderpath
			      " $HOME/BlindBox /usr/spool/mail/$USER"))))
	(read-file (concat curfolderpath "/Directory"))
	(beginning-of-file)
	(while (& (eolp) (! (eobp))) (delete-next-character))
	(set-rmail-directory-mode-line-format)
	(position-on-dirent)
	(pick-up-message)
	(sit-for 0)
	(novalue)
    )

    (rmail-read-new-mail-folder newfolder newfolderpath
	(setq newfolder
	      (get-tty-string (concat "read mail folder [" origfolder "] ")))
	(if (= newfolder "")
	    (progn (setq newfolder origfolder)
		   (setq newfolderpath origfolderpath))
	    (setq newfolderpath (concat origfolderpath "/" newfolder)))
	(if (! (file-exists (concat newfolderpath "/Directory")))
	    (error-message (concat "Can't read mail folder " newfolder))
	    (| (!= newfolder curfolder) (= newfolder origfolder))
	    (progn
		  (erase-messages)
		  (if buffer-is-modified (write-current-file))
		  (setq curfolder newfolder)
		  (setq curfolderpath newfolderpath)
		  (message "Please wait while I read mail folder "
		      curfolder "...")
		  (sit-for 0)
		  (rmail-read-mail-folder)))
	(novalue)
    )

    (position-on-dirent
	(pop-to-buffer "rmail-directory")
	(setq needs-checkpointing 0)
	(if (= (buffer-size) 0)
	    (read-file (concat curfolderpath "/Directory")))
	(if (! (looking-at "^>[^0-9\n]*\\([0-9][0-9]*\\)"))
	    (progn
		  (beginning-of-file)
		  (if (error-occured (re-search-forward ""))
		      (progn
			    (beginning-of-file)
			    (if (error-occured (re-search-forward "^ N"))
				(end-of-file))
			    (rmail-mark)
			    (if (! (looking-at
				       "^>[^0-9\n]*\\([0-9][0-9]*\\)"))
				(error-message "No messages"))))))
	(region-around-match 1)
	(setq entry-name (region-to-string))
	(beginning-of-line)
	(setq current-message-number (current-line))
	(setq mode-string current-message-number)
    )

    (pick-up-message
	(position-on-dirent)
	(save-excursion
	    (pop-to-buffer "current-message")
	    (setq needs-checkpointing 0)
	    (setq case-fold-search 1)
	    (set-rmail-message-mode-line-format)
	    (erase-buffer "current-message")
	    (read-file (concat curfolderpath "/" entry-name))
	    (setq header-is-stripped 0)
	    (beginning-of-file)
	    (rmail-strip-message-header)
	    (setq buffer-is-modified 0)
	)
    )

    (rmail-strip-message-header
	(if (! header-is-stripped)
	    (save-excursion
		(pop-to-buffer "current-message")
		(beginning-of-file)
		(set-mark)
		(search-forward "\n\n")
		(backward-character)
		(copy-region-to-buffer "current-message-header")
		(set-mark)
		(while (! (bobp))
		       (previous-line)
		       (beginning-of-line)
		       (if (| (looking-at "To: ")
			      (looking-at "Cc: ")
			      (looking-at "From: ")
			      (looking-at "Date: ")
			      (looking-at "Subject: ")
			      (looking-at "Reply-To: ")
			      (looking-at "In-Reply-To: "))
			   (set-mark)
			   (| (= ' ' (following-char))
			      (= '\t' (following-char)))
			   (novalue)
			   (delete-to-killbuffer)))
		(setq header-is-stripped 1)
		(set-rmail-message-mode-line-format))
	    (novalue))
    )

    (rmail-restore-message-header
	(if header-is-stripped
	    (save-excursion
		(pop-to-buffer "current-message")
		(setq needs-checkpointing 0)
		(beginning-of-file)
		(set-mark)
		(search-forward "\n\n")
		(backward-character)
		(delete-to-killbuffer)
		(yank-buffer "current-message-header")
		(setq header-is-stripped 0)
		(beginning-of-file)
		(set-rmail-message-mode-line-format))
	    (novalue))
    )

    (erase-messages
	(save-excursion
	    (pop-to-buffer "rmail-directory")
	    (setq needs-checkpointing 0)
	    (beginning-of-file)
	    (error-occured (re-replace-string "^.N" "  "))
	    (error-occured fn
		(while 1
		    (re-search-forward "^.D[^0-9\n]*\\([0-9]*\\)")
		    (region-around-match 1)
		    (unlink-file
			(concat curfolderpath "/" (region-to-string)))
		    (beginning-of-line)
		    (set-mark)
		    (end-of-line)
		    (forward-character)
		    (erase-region))))
    )

    (rmail-move-message direntry mess movefolder movefolderpath newflg 
	(position-on-dirent)
	(save-excursion
	    (set-mark)
	    (end-of-line)
	    (setq direntry (region-to-string)))
	(setq mess ": move message to folder ")
	(if (!= savefolder "")
	    (setq mess (concat mess "[" savefolder "] "))
	    (!= curfolder origfolder)
	    (setq mess (concat mess "[" origfolder "] ")))
	(setq movefolder (get-tty-string mess))
	(if (= movefolder "")
	    (setq movefolder savefolder))
	(if (= movefolder "")
	    (setq movefolder origfolder))
	(if (= movefolder curfolder)
	    (error-message "move canceled."))
	(setq movefolderpath (concat origfolderpath "/" movefolder))
	(if (file-exists movefolderpath)
	    (setq newflg 0)
	    (progn answer
		   (setq answer (get-tty-string
			(concat movefolder ":  new mail folder? ")))
		   (if (& (!= answer "")
			  (!= (substr answer 1 1) "y"))
		       (error-message "move canceled."))
		   (message "creating mail folder " movefolder "...")
		   (sit-for 0)
		   (execute-monitor-command (concat "mkdir " movefolderpath))
		   (if (file-exists movefolderpath)
		       (setq newflg 1)
		       (error-message
			   (concat "couldn't create " movefolder)))))
	(message "moving message to " movefolder "...")
	(sit-for 0)
	(execute-monitor-command (concat "ln " curfolderpath "/" entry-name
					 " " movefolderpath))
	(if (! (file-exists (concat movefolderpath "/" entry-name)))
	    (error-message (concat "failed to move message to " movefolder)))
	(save-excursion
	    (temp-use-buffer "Scratch Stuff")
	    (erase-buffer)
	    (insert-string direntry)
	    (newline)
	    (beginning-of-file)
	    (delete-next-character)
	    (insert-character (if newflg '>' ' '))
	    (if (| (= 'N' (following-char))
		   (= 'D' (following-char)))
		(progn
		      (delete-next-character)
		      (insert-character ' ')))
	    (append-to-file (concat movefolderpath "/Directory")))
	(position-on-dirent)
	(rmail-delete-message)
	(setq savefolder movefolder)
	(message "message moved to " movefolder)
	(novalue)
    )

    (rmail-com
	(argc)
	(rmail)
	(exit-emacs)
    )

    (smail-com i
	(declare-global exit-when-through)
	(pop-to-buffer "send-mail")
	(setq needs-checkpointing 0)
	(setq case-fold-search 1)
	(erase-buffer)
	(if (! (is-bound read-mail-abbrevs))
	    (progn (declare-global read-mail-abbrevs)
		(quietly-read-abbrev-file "/usr/local/lib/emacs/may83/RMailAbbrevs")
	    )
	)
	(use-abbrev-table "RMail")
	(setq i 1)
	(newline)
	(while (> (argc) i)
	    (if (!= (substr (argv i) 1 1) "-")
		(progn
		    (insert-string (argv i))
		    (if (> (argc) i) (insert-character ','))
		)
	    )
	    (setq i (+ i 1))
	)
	(newline)
	(do-mail-setup)
	(exit-emacs)
    )
    
    (rmail-next-page
	(save-excursion
	    (pop-to-buffer "current-message")
	    (setq needs-checkpointing 0)
	    (next-page)
	    (set-rmail-message-mode-line-format)
	)
    )
    
    (rmail-previous-page
	(save-excursion
	    (pop-to-buffer "current-message")
	    (setq needs-checkpointing 0)
	    (previous-page)
	    (set-rmail-message-mode-line-format)
	)
    )

    (set-rmail-directory-mode-line-format
	(save-excursion 
	    (end-of-file)
	    (setq mode-line-format
		(concat "     Mail folder:  " curfolder
			"     #%m  (of " (- (current-line) 1) ")     %M")))
    )

    (set-rmail-message-mode-line-format top bot end s1 s2 len
	(save-excursion
	    (setq len 30)
	    (setq end (+ 1 (buffer-size)))
	    (beginning-of-window)
	    (setq top (/ (+ end -1 (* len (dot))) end))
	    (end-of-window)
	    (setq bot (/ (+ end -1 (* len (dot))) end))
	    (setq s1 "----------------------------------------")
	    (setq s2 "========================================")
	    (setq mode-line-format
		  (concat "        Message  #"
			  current-message-number
			  "                "
			  (substr s1 1 (- top 1))
			  (substr s2 1 (- bot top -1))
			  (substr s1 1 (- len bot))))
	)
    )

    (rmail-next-message
	(position-on-dirent)
	(delete-next-character)
	(insert-character ' ')
	(beginning-of-line)
	(next-line)
	(if (eobp) (progn (previous-line)
			  (message "You're at the last message already")))
	(delete-next-character)
	(insert-character '>')
	(backward-character)
	(pick-up-message)
    )

    (rmail-previous-message
	(position-on-dirent)
	(if (bobp)
	    (message "You're at the first message")
	    (progn
		  (delete-next-character)
		  (insert-character ' ')
		  (beginning-of-line)
		  (previous-line)
		  (delete-next-character)
		  (insert-character '>')
		  (backward-character)
		  (pick-up-message)))
    )

    (rmail-delete-message
	(position-on-dirent)
	(forward-character)
	(delete-next-character)
	(insert-character 'D')
	(beginning-of-line)
    )

    (rmail-undelete-message
	(position-on-dirent)
	(forward-character)
	(delete-next-character)
	(insert-character ' ')
	(beginning-of-line)
    )
)

(if (error-occured (current-line))
    (defun
	  (current-line lcnt incr	; approx log search
	      (save-excursion
		  (setq lcnt 1)
		  (setq incr (/ (dot) 40))
		  (while (> incr 1)
			 (while (! (error-occured
				       (provide-prefix-argument incr
					   (search-reverse "\^J"))))
				(setq lcnt (+ lcnt incr)))
			 (setq incr (/ incr 4)))
		  (while (! (error-occured (search-reverse "\^J")))
			 (setq lcnt (+ lcnt 1))))
	      lcnt)
    )
)

(autoload "&info" "info.ml")

(defun
    (rmail-help
	(&info "emacs" "rmail")
    )

    (rmail-reply subject dest excess
	(setq subject "")
	(setq dest "")
	(setq excess "")
	(save-window-excursion
	    (pop-to-buffer "current-message")
	    (setq needs-checkpointing 0)
	    (setq case-fold-search 1)
	    (beginning-of-file)
	    (search-forward "\n\n")
	    (set-mark)
	    (beginning-of-file)
	    (narrow-region)
	    (error-occured
		(re-search-forward "^Subject:[ \t]*\\(.*\\)")
		(region-around-match 1)
		(setq subject (region-to-string))
		(if (!= (substr subject 1 3) "Re:")
		    (setq subject (concat "Re: " subject))
		)
	    )
	    (beginning-of-file)
	    (error-occured
		(if (error-occured (re-search-forward
				       "^reply-to:[ \t]*\\(.*\\)"))
		    (re-search-forward "^from:[ \t]*\\(.*\\)"))
		(region-around-match 1)
		(setq dest (region-to-string))
	    )
	    (beginning-of-file)
	    (error-occured edest
		(save-excursion 
		    (temp-use-buffer "Scratch Stuff")
		    (setq needs-checkpointing 0)
		    (erase-buffer)
		    (insert-string dest)
		    (set-mark)
		    (beginning-of-file)
		    (error-occured 
			(re-replace-string
			    "  *at  *[^,\n]*\\| *@ *[^,\n]*\\| *([^)\n]*)\\| *<[^>\n]*>"
			    ""))
		    (setq edest (region-to-string)))
		(re-search-forward "^date:[ \t]*")
		(set-mark)
		(end-of-line)
		(setq excess (concat
				    "In-Reply-To: "
				    edest "'s message of "
				    (region-to-string)
				    "\n"))
	    )
	    (widen-region)
	    (pop-to-buffer "send-mail")
	    (setq needs-checkpointing 0)
	    (setq case-fold-search 1)
	    (erase-buffer)
	    (insert-string subject)
	    (newline)
	    (insert-string dest)
	    (newline)
	    (insert-string excess)
	    (error-occured
		(beginning-of-file)
		(search-forward "In-Reply-To: ")
		(replace-string ":" ""))
	    (do-mail-setup)
	)
	(position-on-dirent)
	(if (looking-at "^>[N ]")
	    (progn
		  (forward-character)
		  (delete-next-character)
		  (insert-character 'A')
		  (beginning-of-line)))
    )

    (rmail-unmark
	(error-occured
	    (position-on-dirent)
	    (delete-next-character)
	    (insert-character ' ')
	    (beginning-of-line))
    )
    
    (rmail-mark
	(if (error-occured
		(beginning-of-line)
		(if (eobp)
		    (re-search-reverse "^.")
		    (progn
			(re-search-forward "^.")
			(beginning-of-line)))
		(delete-next-character)
		(insert-character '>')
		(beginning-of-line)
		(position-on-dirent)
		(pick-up-message)
	    )
	    (message "No messages"))
    )

    (rmail-search-forward
	(rmail-unmark)
	(error-occured (re-search-forward
			   (get-tty-string "Search forward: ")))
	(rmail-mark)
    )

    (rmail-search-reverse
	(rmail-unmark)
	(error-occured (re-search-reverse
			   (get-tty-string "Search reverse: ")))
	(rmail-mark)
    )

    (rmail-goto-message n
	(setq n (get-tty-string "Goto message: "))
	(rmail-unmark)
	(beginning-of-file)
	(provide-prefix-argument (- n 1) (next-line))
	(rmail-mark)
    )

    (rmail-first-message
	(rmail-unmark)
	(beginning-of-file)
	(rmail-mark)
    )

    (rmail-last-message
	(rmail-unmark)
	(end-of-file)
	(rmail-mark)
    )

    (rmail-skip n
	(setq n (get-tty-string "Skip messages: "))
	(rmail-unmark)
	(provide-prefix-argument n (next-line))
	(rmail-mark)
    )

    (rmail-but-mark
	(save-excursion (rmail-unmark))
	(rmail-mark)
    )

    (rmail-but-delete
	(rmail-but-mark)
	(rmail-delete-message)
    )

    (mail-insert
	(mail-append)
	(save-excursion
	    (insert-string "\n_________________________________\n")
	    (yank-buffer "current-message"))
	(re-replace-string "^From \\|^Date:" ">&")
    )

    (mail-noblind-exit
	(setq mail-append-blind 0)
	(exit-emacs)
    )
)

(defun    
    (smail 	        ; initiate mail sending
	(save-window-excursion
	    (pop-to-buffer "send-mail")
	    (setq needs-checkpointing 0)
	    (setq case-fold-search 1)
	    (erase-buffer)
	    (do-mail-setup)
	)
	(novalue)
    )
    
    ; set things up in a mail sending buffer.
    (do-mail-setup mail-do-send mail-append-blind
	(beginning-of-file)     ; makemail is a tiny little C program that
	(set-mark) 	        ; builds a prototype mail header from an
	(end-of-file) 	        ; input subject and destination.
	(setq mode-string "SMail")
	(setq mail-do-send 1)
	(setq mail-append-blind 1)
	(if (! (is-bound read-mail-abbrevs))
	    (progn (declare-global read-mail-abbrevs)
		(quietly-read-abbrev-file "/usr/local/lib/emacs/may83/RMailAbbrevs")
	    )
	)
	(use-abbrev-table "RMail")
	(filter-region "/usr/local/lib/emacs/may83/makemail")
	(beginning-of-file)
	(search-forward "hErE<!}")
	(delete-previous-word)
	(setq abbrev-mode (= (current-column) 5))
	(setq right-margin 72)
	(local-bind-to-key "rmail-help" "\^X?")
	(local-bind-to-key "mail-to" "\^Xt")
	(local-bind-to-key "mail-append" "\^Xa")
	(local-bind-to-key "mail-cc" "\^Xc")
	(local-bind-to-key "mail-subject" "\^Xs")
	(local-bind-to-key "mail-fetch-field" "\^Xf")
	(local-bind-to-key "mail-insert" "\^Xi")
	(local-bind-to-key "mail-noblind-exit" "\^X\^C")
	(local-bind-to-key "exit-emacs" "\^X\^S")
	(local-bind-to-key "exit-emacs" "\^X\^F")
	(local-bind-to-key "mail-abort-send" "\^G")
	(local-bind-to-key "mail-abort-send" "\^X\^A")
	(local-bind-to-key "justify-paragraph" "\ej")
	(message "Type ^C to send the message; ^X^A to abort; ^X? for help.")
	(while mail-do-send
	    (save-excursion
		(recursive-edit)
		(if mail-do-send
		    (progn
			(message "Sending...")
			(sit-for 0))))
	    (if mail-do-send (send-mail)))
    )
)

(defun
    (mail-abort-send
	(if (!= "y" (substr (get-tty-string
				"Do you really want to abort the message? ")
			    1 1))
	    (error-message "message aborted."))
	(setq mail-do-send 0)
	(exit-emacs))
    
    (mail-to 		        ; move to the "To:" field
	(abbrev-expand)
	(setq abbrev-mode 1)
	(beginning-of-file)
	(re-search-forward "^to:.*")
	(setq left-margin 10)
    )
    
    (mail-subject 	        ; move to the "Subject:" field
	(abbrev-expand)
	(setq abbrev-mode 0)
	(beginning-of-file)
	(re-search-forward "^subject:.*")
	(setq left-margin 10)
    )
    
    (mail-fetch-field		; fetch a field from the current message
	field-name field-contents
	(setq field-name (get-tty-string ": field name "))
	(save-excursion
	    (temp-use-buffer "current-message")
	    (beginning-of-file)
	    (setq case-fold-search 1)
	    (re-search-forward (concat "^" field-name "[^\n:]*:[ \t]*"))
	    (mail-extend-field)
	    (setq field-contents (region-to-string))
	)
	(insert-string field-contents)
    )
    
    (mail-append 	        ; move to the body
	(abbrev-expand)
	(setq abbrev-mode 0)
	(end-of-file)
	(setq left-margin 1)
    )
    
    (mail-cc 		        ; move to the "cc:" field.
	(abbrev-expand)
	(setq abbrev-mode 1)
	(beginning-of-file)
	(if (error-occured (re-search-forward "^cc:.*"))
	    (progn (re-search-forward "\n\n\\|^to:.*\n.")
		   (backward-character)
		   (backward-character)
		   (while (looking-at "^[ \t]")
			  (progn (beginning-of-line)
				 (next-line)))
		   (insert-string "\nCc: ")
	    )
	)
    )
    
    (mail-extend-field
	(set-mark)
	(while (progn (end-of-line)
		      (looking-at "\n[ \t]"))
	       (forward-character))
    )

    (send-mail addresses		; finally send the mail
	(abbrev-expand)
	(beginning-of-file)
	(if (error-occured (re-search-forward "^$"))
	    (end-of-file))
	(set-mark)
	(beginning-of-file)
	(narrow-region)
	(setq addresses "")
	(error-occured
	    (re-search-forward "^to:[ \t]*")
	    (mail-extend-field)
	    (setq addresses (region-to-string)))
	(beginning-of-file)
	(error-occured
	    (re-search-forward "^cc:[ \t]*")
	    (mail-extend-field)
	    (setq addresses (concat addresses ","
				    (region-to-string))))
	(widen-region)
	(message "sending...")
	(save-excursion
	    (temp-use-buffer "Scratch Stuff")
	    (setq needs-checkpointing 0)
	    (erase-buffer)
	    (insert-string addresses)
	    (beginning-of-file)
	    (error-occured (re-replace-string "[\t\n]" " "))
	    (error-occured (re-replace-string " *([^(]*)" ""))
	    (error-occured (re-replace-string "[^,]*<\\([^>,]*\\)>" "\\1"))
	    (error-occured (re-replace-string "  *at  *" "@"))
	    (error-occured (re-replace-string "  *" "."))
	    (error-occured (re-replace-string "\\.*,\\.*" " "))
	    (error-occured (replace-string "!" "\\!"))
	    (error-occured (re-replace-string "^  *\\|[ .][ .]*$" ""))
	    (set-mark)
	    (end-of-file)
	    (setq addresses (region-to-string))
	)
	(end-of-file)
	(if (! (bolp)) (newline))
	(beginning-of-file)
	(if mail-append-blind
	    (progn
		  (insert-string "\3\n")
		  (append-to-file "~/BlindBox")
		  (delete-previous-word)))
	(set-mark)
	(end-of-file)
	(filter-region (concat "/usr/lib/csmail -i "	;  changed from
			       addresses))		; delivermail by lee

	(if (= (buffer-size) 0)
	    (progn
		  (setq mail-do-send 0)
		  (message (concat "Sent to " addresses)))
	    (progn T
		  (beginning-of-file)
		  (set-mark)
		  (end-of-file)
		  (copy-region-to-buffer "Delivery-errors")
		  (beginning-of-file)
		  (error-occured (re-replace-string "\n\n* *" "; "))
		  (end-of-line)
		  (setq T (region-to-string))
		  (erase-buffer)
		  (yank-from-killbuffer)
		  (beginning-of-file)
		  (error-occured 
		      (re-search-forward "^to:[^>\n]*"))
		  (message T))
	)
    )
    
    (abbrev-expand
	(insert-character ' ')
	(delete-previous-character)
    )
)

(declare-global rmail-default-log)
(setq rmail-default-log "~/TextMessage")

(defun
    (rmail-append file
	(setq file (get-tty-string (concat ": append-message-to-file ["
				       rmail-default-log "]  ")))
	(if (= file "") (setq file rmail-default-log))
	(save-excursion 
	    (temp-use-buffer "current-message")
	    (append-to-file file))
	(setq rmail-default-log file)
    )
    
    (rmail-shell
	(save-window-excursion
	    (shell)
	    (message "Type ^C to resume mail reading")
	    (recursive-edit))
    )
)

(defun (rmail-query-exit
	   (if (!= "y" (substr (get-tty-string
			   "Do you really want to abort mail reading? ")
			       1 1))
	       (error-message "exit ignored."))
	   (exit-emacs)
       )
)

(save-excursion i
    (temp-use-buffer "rmail-directory")
    (define-keymap "rmail-commands")
    (use-local-map "rmail-commands")
    (setq i 0)
    (while (< i 128)
	(local-bind-to-key "illegal-operation" i)
	(setq i (+ i 1)))
    (local-bind-to-key "rmail-but-mark" "\eA")
    (local-bind-to-key "rmail-but-mark" "\eB")
    (local-bind-to-key "rmail-but-delete" "\eC")
    (local-bind-to-key "rmail-but-mark" "\eD")
    (local-bind-to-key "rmail-next-page" 'f')
    (local-bind-to-key "rmail-previous-page" 'b')
    (local-bind-to-key "rmail-next-page" '^f')
    (local-bind-to-key "rmail-previous-page" '^b')
    (local-bind-to-key "rmail-next-page" ' ')
    (local-bind-to-key "rmail-previous-page" '^H')
    (local-bind-to-key "rmail-next-page" '^v')
    (local-bind-to-key "rmail-read-new-mail-folder" "\^Xr")
    (local-bind-to-key "rmail-next-message" 'n')
    (local-bind-to-key "rmail-previous-message" 'p')
    (local-bind-to-key "rmail-next-message" '^n')
    (local-bind-to-key "rmail-previous-message" '^p')
    (local-bind-to-key "rmail-delete-message" 'd')
    (local-bind-to-key "rmail-delete-message" '\177')
    (local-bind-to-key "rmail-delete-message" '^d')
    (local-bind-to-key "rmail-undelete-message" 'u')
    (local-bind-to-key "rmail-help" '?')
    (local-bind-to-key "rmail-query-exit" "\^G")
    (local-bind-to-key "exit-emacs" 'q')
    (local-bind-to-key "rmail-reply" 'r')
    (local-bind-to-key "smail" "\^Xm")
    (local-bind-to-key "rmail-move-message" 'm')
    (local-bind-to-key "rmail-search-forward" '^s')
    (local-bind-to-key "rmail-search-reverse" '^r')
    (local-bind-to-key "rmail-goto-message" 'g')
    (local-bind-to-key "rmail-strip-message-header" 'H')
    (local-bind-to-key "rmail-restore-message-header" 'h')
    (local-bind-to-key "rmail-first-message" '<')
    (local-bind-to-key "rmail-last-message" '>')
    (local-bind-to-key "rmail-skip" 's')
    (local-bind-to-key "rmail-append" 'a')
    (local-bind-to-key "rmail-shell" '!')
    (local-bind-to-key "execute-extended-command" ':')
    (local-bind-to-key "exit-emacs" '^c')
    (local-bind-to-key "redraw-display" "\^L")
)
;
; End of Koomen's rmail.ml
;