*** empty log message ***
[gnus] / lisp / message.el
1 ;;; message.el --- composing mail and news messages
2 ;; Copyright (C) 1996 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: mail, news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;; This mode provides mail-sending facilities from within Emacs.  It
27 ;; consists mainly of large chunks of code from the sendmail.el,
28 ;; gnus-msg.el and rnewspost.el files.
29
30 ;;; Code:
31
32 (eval-when-compile 
33   (require 'cl))
34 (require 'mail-header)
35 (require 'nnheader)
36 (require 'timezone)
37 (require 'easymenu)
38
39 ;;;###autoload
40 (defvar message-fcc-handler-function 'rmail-output
41   "*A function called to save outgoing articles.
42 This function will be called with the name of the file to store the
43 article in. The default function is `rmail-output' which saves in Unix
44 mailbox format.")
45
46 ;;;###autoload
47 (defvar message-courtesy-message
48   "The following message is a courtesy copy of an article\nthat has been posted as well.\n\n"
49   "*This is inserted at the start of a mailed copy of a posted message.
50 If this variable is nil, no such courtesy message will be added.")
51
52 ;;;###autoload
53 (defvar message-ignored-bounced-headers "^\\(Received\\|Return-Path\\):"
54   "*Regexp that matches headers to be removed in resent bounced mail.")
55
56 ;;;###autoload
57 (defvar message-from-style 'default
58   "*Specifies how \"From\" headers look.
59
60 If `nil', they contain just the return address like:
61         king@grassland.com
62 If `parens', they look like:
63         king@grassland.com (Elvis Parsley)
64 If `angles', they look like:
65         Elvis Parsley <king@grassland.com>
66
67 Otherwise, most addresses look like `angles', but they look like
68 `parens' if `angles' would need quoting and `parens' would not.")
69
70 ;;;###autoload
71 (defvar message-syntax-checks nil
72   "Controls what syntax checks should not be performed on outgoing posts.
73 To disable checking of long signatures, for instance, add
74  `(signature . disabled)' to this list.
75
76 Don't touch this variable unless you really know what you're doing.
77
78 Checks include subject-cmsg multiple-headers sendsys message-id from
79 long-lines control-chars size new-text redirected-followup signature
80 approved sender empty empty-headers message-id from subject.")
81
82 ;;;###autoload
83 (defvar message-required-news-headers
84   '(From Newsgroups Subject Date Message-ID 
85          (optional . Organization) Lines 
86          (optional . X-Newsreader))
87   "*Headers to be generated or prompted for when posting an article.
88 RFC977 and RFC1036 require From, Date, Newsgroups, Subject,
89 Message-ID.  Organization, Lines, In-Reply-To, Expires, and
90 X-Newsreader are optional.  If don't you want message to insert some
91 header, remove it from this list.")
92
93 ;;;###autoload
94 (defvar message-required-mail-headers 
95   '(From Subject Date (optional . In-Reply-To) Message-ID Lines
96          (optional . X-Mailer))
97   "*Headers to be generated or prompted for when mailing a message.
98 RFC822 required that From, Date, To, Subject and Message-ID be
99 included.  Organization, Lines and X-Mailer are optional.")
100
101 ;;;###autoload
102 (defvar message-deletable-headers '(Message-ID Date)
103   "*Headers to be deleted if they already exist and were generated by message previously.")
104
105 ;;;###autoload
106 (defvar message-ignored-news-headers 
107   "^NNTP-Posting-Host:\\|^Xref:\\|^Bcc:\\|^Gcc:\\|^Fcc:"
108   "*Regexp of headers to be removed unconditionally before posting.")
109
110 ;;;###autoload
111 (defvar message-ignored-mail-headers "^Gcc:\\|^Fcc:"
112   "*Regexp of headers to be removed unconditionally before mailing.")
113
114 ;;;###autoload
115 (defvar message-ignored-supersedes-headers
116   "^Path:\\|^Date\\|^NNTP-Posting-Host:\\|^Xref:\\|^Lines:\\|^Received:\\|^X-From-Line:\\|Return-Path:"
117   "*Header lines matching this regexp will be deleted before posting.
118 It's best to delete old Path and Date headers before posting to avoid
119 any confusion.")
120
121 ;;;###autoload
122 (defvar message-signature-separator "^-- *$"
123   "Regexp matching the signature separator.")
124
125 ;;;###autoload
126 (defvar message-interactive nil 
127   "Non-nil means when sending a message wait for and display errors.
128 nil means let mailer mail back a message to report errors.")
129
130 ;;;###autoload
131 (defvar message-generate-new-buffers nil
132   "*Non-nil means that a new message buffer will be created whenever `mail-setup' is called.")
133
134 ;;;###autoload
135 (defvar message-kill-buffer-on-exit nil
136   "*Non-nil means that the message buffer will be killed after sending a message.")
137
138 (defvar gnus-local-organization)
139 ;;;###autoload
140 (defvar message-user-organization 
141   (or (and (boundp 'gnus-local-organization)
142            gnus-local-organization)
143       (getenv "ORGANIZATION")
144       t)
145   "*String to be used as an Organization header.
146 If t, use `message-user-organization-file'.")
147
148 ;;;###autoload
149 (defvar message-user-organization-file "/usr/lib/news/organization"
150   "*Local news organization file.")
151
152 ;;;###autoload
153 (defvar message-autosave-directory "~/Mail/drafts/"
154   "*Directory where message autosaves buffers.
155 If nil, message won't autosave.")
156
157 (defvar message-forward-start-separator 
158   "------- Start of forwarded message -------\n"
159   "*Delimiter inserted before forwarded messages.")
160
161 (defvar message-forward-end-separator
162   "------- End of forwarded message -------\n"
163   "*Delimiter inserted after forwarded messages.")
164
165 ;;;###autoload
166 (defvar message-signature-before-forwarded-message t
167   "*If non-nil, put the signature before any included forwarded message.")
168
169 ;;;###autoload
170 (defvar message-included-forward-headers 
171   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-\\|^Message-ID:\\|^References:"
172   "*Regexp matching headers to be included in forwarded messages.")
173
174 ;;;###autoload
175 (defvar message-ignored-resent-headers "^Return-receipt"
176   "*All headers that match this regexp will be deleted when resending a message.")
177
178 ;;;###autoload
179 (defvar message-ignored-cited-headers "."
180   "Delete these headers from the messages you yank.")
181
182 ;; Useful to set in site-init.el
183 ;;;###autoload
184 (defvar message-send-mail-function 'message-send-mail 
185   "Function to call to send the current buffer as mail.
186 The headers should be delimited by a line whose contents match the
187 variable `mail-header-separator'.")
188
189 ;;;###autoload
190 (defvar message-send-news-function 'message-send-news
191   "Function to call to send the current buffer as news.
192 The headers should be delimited by a line whose contents match the
193 variable `message-header-separator'.")
194
195 ;;;###autoload
196 (defvar message-reply-to-function nil
197   "Function that should return a list of headers.
198 This function should pick out addresses from the To, Cc, and From headers
199 and respond with new To and Cc headers.")
200
201 ;;;###autoload
202 (defvar message-wide-reply-to-function nil
203   "Function that should return a list of headers.
204 This function should pick out addresses from the To, Cc, and From headers
205 and respond with new To and Cc headers.")
206
207 ;;;###autoload
208 (defvar message-followup-to-function nil
209   "Function that should return a list of headers.
210 This function should pick out addresses from the To, Cc, and From headers
211 and respond with new To and Cc headers.")
212
213 ;;;###autoload
214 (defvar message-use-followup-to 'ask
215   "*Specifies what to do with Followup-To header.
216 If nil, ignore the header. If it is t, use its value, but query before
217 using the \"poster\" value.  If it is the symbol `ask', query the user
218 whether to ignore the \"poster\" value.  If it is the symbol `use',
219 always use the value.")
220
221 (defvar gnus-post-method)
222 (defvar gnus-select-method)
223 ;;;###autoload
224 (defvar message-post-method 
225   (cond ((and (boundp 'gnus-post-method)
226               gnus-post-method)
227          gnus-post-method)
228         ((boundp 'gnus-select-method)
229          gnus-select-method)
230         (t '(nnspool "")))
231   "Method used to post news.")
232
233 ;;;###autoload
234 (defvar message-generate-headers-first nil
235   "*If non-nil, generate all possible headers before composing.")
236
237 ;;;###autoload
238 (defvar message-header-separator "--text follows this line--" 
239   "*Line used to separate headers from text in messages being composed.")
240
241 (defvar message-setup-hook nil
242   "Normal hook, run each time a new outgoing message is initialized.
243 The function `message-setup' runs this hook.")
244
245 (defvar message-header-setup-hook nil
246   "Hook called narrowed to the headers when setting up a message buffer.")
247
248 ;;;###autoload
249 (defvar message-citation-line-function 'message-insert-citation-line
250   "*Function called to insert the \"Whomever writes:\" line.")
251
252 ;;;###autoload
253 (defvar message-yank-prefix "> "
254   "*Prefix inserted on the lines of yanked messages.
255 nil means use indentation.")
256
257 (defvar message-indentation-spaces 3
258   "*Number of spaces to insert at the beginning of each cited line.
259 Used by `message-yank-original' via `message-yank-cite'.")
260
261 ;;;###autoload
262 (defvar message-cite-function 'message-cite-original
263   "*Function for citing an original message.")
264
265 ;;;###autoload
266 (defvar message-indent-citation-function 'message-indent-citation
267   "*Function for modifying a citation just inserted in the mail buffer.
268 This can also be a list of functions.  Each function can find the
269 citation between (point) and (mark t).  And each function should leave
270 point and mark around the citation text as modified.")
271
272 (defvar message-abbrevs-loaded nil)
273
274 ;;;###autoload
275 (defvar message-signature t
276   "*String to be inserted at the end of the message buffer.
277 If t, the `message-signature-file' file will be inserted instead.
278 If a function, the result from the function will be used instead.
279 If a form, the result from the form will be used instead.")
280
281 ;;;###autoload
282 (defvar message-signature-file "~/.signature"
283   "*File containing the text inserted at end of message. buffer.")
284
285 (defvar message-distribution-function nil
286   "*Function called to return a Distribution header.")
287
288 (defvar message-expires 14
289   "*Number of days before your article expires.")
290
291 (defvar message-user-path nil
292   "If nil, use the NNTP server name in the Path header.
293 If stringp, use this; if non-nil, use no host name (user name only).")
294
295 (defvar message-reply-buffer nil)
296 (defvar message-reply-headers nil)
297 (defvar message-newsreader nil)
298 (defvar message-mailer nil)
299 (defvar message-sent-message-via nil)
300 (defvar message-checksum nil)
301 (defvar message-send-actions nil
302   "A list of actions to be performed upon successful sending of a message.")
303
304 ;;;###autoload
305 (defvar message-default-headers nil
306   "*A string containing header lines to be inserted in outgoing messages.
307 It is inserted before you edit the message, so you can edit or delete
308 these lines.")
309
310 ;;;###autoload
311 (defvar message-default-mail-headers nil
312   "*A string of header lines to be inserted in outgoing mails.")
313
314 ;;;###autoload
315 (defvar message-default-news-headers nil
316   "*A string of header lines to be inserted in outgoing news articles.")
317
318 ;; Note: could use /usr/ucb/mail instead of sendmail;
319 ;; options -t, and -v if not interactive.
320 (defvar message-mailer-swallows-blank-line
321   (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)" 
322                          system-configuration)
323            (file-readable-p "/etc/sendmail.cf")
324            (let ((buffer (get-buffer-create " *temp*")))
325              (unwind-protect
326                  (save-excursion
327                    (set-buffer buffer)
328                    (insert-file-contents "/etc/sendmail.cf")
329                    (goto-char (point-min))
330                    (let ((case-fold-search nil))
331                      (re-search-forward "^OR\\>" nil t)))
332                (kill-buffer buffer))))
333       ;; According to RFC822, "The field-name must be composed of printable
334       ;; ASCII characters (i.e. characters that have decimal values between
335       ;; 33 and 126, except colon)", i.e. any chars except ctl chars,
336       ;; space, or colon.
337       '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
338   "Set this non-nil if the system's mailer runs the header and body together.
339 \(This problem exists on Sunos 4 when sendmail is run in remote mode.)
340 The value should be an expression to test whether the problem will
341 actually occur.")
342
343 (defvar message-mode-syntax-table 
344   (let ((table (copy-syntax-table text-mode-syntax-table)))
345     (modify-syntax-entry ?% ". " table)
346     table)
347   "Syntax table used while in Message mode.")
348
349 (defvar message-font-lock-keywords
350   (let* ((cite-prefix "A-Za-z") (cite-suffix (concat cite-prefix "0-9_.@-")))
351     (list '("^To:" . font-lock-function-name-face)
352           '("^[GBF]?[Cc][Cc]:\\|^Reply-To:" . font-lock-keyword-face)
353           '("^\\(Subject:\\)[ \t]*\\(.+\\)?"
354             (1 font-lock-comment-face) (2 font-lock-type-face nil t))
355           (list (concat "^\\(" (regexp-quote mail-header-separator) "\\)$")
356                 1 'font-lock-comment-face)
357           (cons (concat "^[ \t]*"
358                         "\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
359                         "[>|}].*")
360                 'font-lock-reference-face)
361           '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\):.*"
362             . font-lock-string-face)))
363   "Additional expressions to highlight in Message mode.")
364
365 (defvar message-face-alist
366   '((bold . bold-region)
367     (underline . underline-region)
368     (default . (lambda (b e) 
369                  (unbold-region b e)
370                  (ununderline-region b e))))
371   "Alist of mail and news faces for facemenu.
372 The cdr of ech entry is a function for applying the face to a region.")
373
374 (defvar message-send-hook nil
375   "Hook run before sending messages.")
376
377 (defvar message-sent-hook nil
378   "Hook run after sending messages.")
379
380 ;;; Internal variables.
381
382 ;;; Regexp matching the delimiter of messages in UNIX mail format
383 ;;; (UNIX From lines), minus the initial ^.  
384 (defvar message-unix-mail-delimiter
385   (let ((time-zone-regexp
386          (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
387                  "\\|[-+]?[0-9][0-9][0-9][0-9]"
388                  "\\|"
389                  "\\) *")))
390     (concat
391      "From "
392
393      ;; Username, perhaps with a quoted section that can contain spaces.
394      "\\("
395      "[^ \n]*"
396      "\\(\\|\".*\"[^ \n]*\\)"
397      "\\|<[^<>\n]+>"
398      "\\)  ?"
399
400      ;; The time the message was sent.
401      "\\([^ \n]*\\) *"                  ; day of the week
402      "\\([^ ]*\\) *"                    ; month
403      "\\([0-9]*\\) *"                   ; day of month
404      "\\([0-9:]*\\) *"                  ; time of day
405
406      ;; Perhaps a time zone, specified by an abbreviation, or by a
407      ;; numeric offset.
408      time-zone-regexp
409
410      ;; The year.
411      " [0-9][0-9]\\([0-9]*\\) *"
412
413      ;; On some systems the time zone can appear after the year, too.
414      time-zone-regexp
415
416      ;; Old uucp cruft.
417      "\\(remote from .*\\)?"
418
419      "\n")))
420
421 (defvar message-unsent-separator
422   (concat "^ *---+ +Unsent message follows +---+ *$\\|"
423           "^ *---+ +Returned message +---+ *$\\|"
424           "^Start of returned message$\\|"
425           "^ *---+ +Original message +---+ *$\\|"
426           "^ *--+ +begin message +--+ *$\\|"
427           "^ *---+ +Original message follows +---+ *$\\|"
428           "^|? *---+ +Message text follows: +---+ *|?$")
429   "A regexp that matches the separator before the text of a failed message.")
430
431 (defvar message-header-format-alist 
432   `((Newsgroups)
433     (To . message-fill-header) 
434     (Cc . message-fill-header)
435     (Subject)
436     (In-Reply-To)
437     (Fcc)
438     (Bcc)
439     (Date)
440     (Organization)
441     (Distribution)
442     (Lines)
443     (Expires)
444     (Message-ID)
445     (References . message-fill-header)
446     (X-Mailer)
447     (X-Newsreader))
448   "Alist used for formatting headers.")
449
450 (eval-and-compile
451   (autoload 'message-setup-toolbar "message-xmas"))
452
453 \f
454
455 ;;; 
456 ;;; Utility functions.
457 ;;;
458
459 (defun message-point-at-bol ()
460   "Return point at the beginning of the line."
461   (let ((p (point)))
462     (beginning-of-line)
463     (prog1
464         (point)
465       (goto-char p))))
466
467 (defun message-point-at-eol ()
468   "Return point at the end of the line."
469   (let ((p (point)))
470     (end-of-line)
471     (prog1
472         (point)
473       (goto-char p))))
474
475 ;; Delete the current line (and the next N lines.);
476 (defmacro message-delete-line (&optional n)
477   `(delete-region (progn (beginning-of-line) (point))
478                   (progn (forward-line ,(or n 1)) (point))))
479
480 (defun message-tokenize-header (header &optional separator)
481   "Split HEADER into a list of header elements.
482 \",\" is used as the separator."
483   (let* ((beg 0)
484          (separator (or separator ","))
485          (regexp
486           (format "[ \t]*\\([^%s]+\\)?\\([%s]+\\|\\'\\)" separator separator))
487          elems)
488     (while (and (string-match regexp header beg)
489                 (< beg (length header)))
490       (when (match-beginning 1)
491         (push (match-string 1 header) elems))
492       (setq beg (match-end 0)))
493     (nreverse elems)))
494
495 (defun message-fetch-reply-field (header)
496   "Fetch FIELD from the message we're replying to."
497   (when (and message-reply-buffer
498              (buffer-name message-reply-buffer))
499     (save-excursion
500       (set-buffer message-reply-buffer)
501       (mail-fetch-field header))))
502
503 (defun message-set-work-buffer ()
504   (if (get-buffer " *message work*")
505       (progn
506         (set-buffer " *message work*")
507         (erase-buffer))
508     (set-buffer (get-buffer-create " *message work*"))
509     (kill-all-local-variables)
510     (buffer-disable-undo (current-buffer))))
511
512 (defun message-functionp (form)
513   "Return non-nil if FORM is funcallable."
514   (or (and (symbolp form) (fboundp form))
515       (and (listp form) (eq (car form) 'lambda))))
516
517 (defun message-strip-subject-re (subject)
518   "Remove \"Re:\" from subject lines."
519   (if (string-match "^[Rr][Ee]: *" subject)
520       (substring subject (match-end 0))
521     subject))
522
523 (defun message-remove-header (header &optional is-regexp first reverse)
524   "Remove HEADER in the narrowed buffer.
525 If REGEXP, HEADER is a regular expression.
526 If FIRST, only remove the first instance of the header.
527 Return the number of headers removed."
528   (goto-char (point-min))
529   (let ((regexp (if is-regexp header (concat "^" header ":")))
530         (number 0)
531         (case-fold-search t)
532         last)
533     (while (and (not (eobp))
534                 (not last))
535       (if (if reverse
536               (not (looking-at regexp))
537             (looking-at regexp))
538           (progn
539             (incf number)
540             (when first
541               (setq last t))
542             (delete-region
543              (point)
544              ;; There might be a continuation header, so we have to search
545              ;; until we find a new non-continuation line.
546              (progn
547                (forward-line 1)
548                (if (re-search-forward "^[^ \t]" nil t)
549                    (goto-char (match-beginning 0))
550                  (point-max)))))
551         (forward-line 1)
552         (if (re-search-forward "^[^ \t]" nil t)
553             (goto-char (match-beginning 0))
554           (point-max))))
555     number))
556
557 (defun message-narrow-to-headers ()
558   "Narrow the buffer to the head of the message."
559   (widen)
560   (narrow-to-region
561    (goto-char (point-min))
562    (if (re-search-forward
563         (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
564        (match-beginning 0)
565      (point-max)))
566   (goto-char (point-min)))
567
568 (defun message-narrow-to-head ()
569   "Narrow the buffer to the head of the message."
570   (widen)
571   (narrow-to-region
572    (goto-char (point-min))
573    (if (search-forward "\n\n" nil 1)
574        (1- (point))
575      (point-max)))
576   (goto-char (point-min)))
577
578 (defun message-news-p ()
579   "Say whether the current buffer contains a news message."
580   (save-excursion
581     (save-restriction
582       (message-narrow-to-headers)
583       (mail-fetch-field "newsgroups"))))
584
585 (defun message-mail-p ()
586   "Say whether the current buffer contains a mail message."
587   (save-excursion
588     (save-restriction
589       (message-narrow-to-headers)
590       (or (mail-fetch-field "to")
591           (mail-fetch-field "cc")
592           (mail-fetch-field "bcc")))))
593
594 (defun message-next-header ()
595   "Go to the beginning of the next header."
596   (beginning-of-line)
597   (or (eobp) (forward-char 1))
598   (not (if (re-search-forward "^[^ \t]" nil t)
599            (beginning-of-line)
600          (goto-char (point-max)))))
601     
602 (defun message-sort-headers-1 ()
603   "Sort the buffer as headers using `message-rank' text props."
604   (goto-char (point-min))
605   (sort-subr 
606    nil 'message-next-header 
607    (lambda ()
608      (message-next-header)
609      (unless (bobp)
610        (forward-char -1)))
611    (lambda ()
612      (or (get-text-property (point) 'message-rank)
613          0))))
614
615 (defun message-sort-headers ()
616   "Sort the headers of the current message according to `message-header-format-alist'."
617   (interactive)
618   (save-excursion
619     (save-restriction
620       (let ((max (1+ (length message-header-format-alist)))
621             rank)
622         (message-narrow-to-headers)
623         (while (re-search-forward "^[^ ]+:" nil t)
624           (put-text-property
625            (match-beginning 0) (1+ (match-beginning 0))
626            'message-rank
627            (if (setq rank (length (memq (assq (intern (buffer-substring
628                                                        (match-beginning 0)
629                                                        (1- (match-end 0))))
630                                               message-header-format-alist)
631                                         message-header-format-alist)))
632                (- max rank)
633              (1+ max)))))
634       (message-sort-headers-1))))
635
636 \f
637
638 ;;;
639 ;;; Message mode
640 ;;;
641
642 ;;; Set up keymap.
643
644 (defvar message-mode-map nil)
645
646 (unless message-mode-map
647   (setq message-mode-map (copy-keymap text-mode-map))
648   (define-key message-mode-map "\C-c?" 'describe-mode)
649
650   (define-key message-mode-map "\C-c\C-f\C-t" 'message-goto-to)
651   (define-key message-mode-map "\C-c\C-f\C-b" 'message-goto-bcc)
652   (define-key message-mode-map "\C-c\C-f\C-w" 'message-goto-fcc)
653   (define-key message-mode-map "\C-c\C-f\C-c" 'message-goto-cc)
654   (define-key message-mode-map "\C-c\C-f\C-s" 'message-goto-subject)
655   (define-key message-mode-map "\C-c\C-f\C-r" 'message-goto-reply-to)
656   (define-key message-mode-map "\C-c\C-f\C-n" 'message-goto-newsgroups)
657   (define-key message-mode-map "\C-c\C-f\C-d" 'message-goto-distribution)
658   (define-key message-mode-map "\C-c\C-f\C-f" 'message-goto-followup-to)
659   (define-key message-mode-map "\C-c\C-f\C-k" 'message-goto-keywords)
660   (define-key message-mode-map "\C-c\C-f\C-u" 'message-goto-summary)
661   (define-key message-mode-map "\C-c\C-b" 'message-goto-body)
662   (define-key message-mode-map "\C-c\C-i" 'message-goto-signature)
663
664   (define-key message-mode-map "\C-c\C-t" 'message-insert-to)
665   (define-key message-mode-map "\C-c\C-n" 'message-insert-newsgroups)
666   
667   (define-key message-mode-map "\C-c\C-y" 'message-yank-original)
668   (define-key message-mode-map "\C-c\C-q" 'message-fill-yanked-message)
669   (define-key message-mode-map "\C-c\C-w" 'message-insert-signature)
670   (define-key message-mode-map "\C-c\C-r" 'message-caesar-buffer-body)
671   (define-key message-mode-map "\C-c\C-o" 'message-sort-headers)
672
673   (define-key message-mode-map "\C-c\C-c" 'message-send-and-exit)
674   (define-key message-mode-map "\C-c\C-s" 'message-send)
675   (define-key message-mode-map "\C-c\C-k" 'message-dont-send))
676
677 (easy-menu-define message-mode-menu message-mode-map
678   "Message Menu."
679   '("Message"
680     "Go to Field:"
681     "----"
682     ["To:" message-goto-to t]
683     ["Subject:" message-goto-subject t]
684     ["Summary:" message-goto-summary t]
685     ["Keywords:" message-goto-keywords t]
686     ["Newsgroups:" message-goto-newsgroups t]
687     ["Followup-To:" message-goto-followup-to t]
688     ["Distribution:" message-goto-distribution t]
689     ["Body" message-goto-body t]
690     ["Signature" message-goto-signature t]
691     "----"
692     "Miscellaneous Commands:"
693     "----"
694     ["Sort Headers" message-sort-headers t]
695     ["Yank Original" message-yank-original t]
696     ["Fill Yanked Message" message-fill-yanked-message t]
697     ;;  ["Insert Signature"         news-reply-signature     t]
698     ["Caesar (rot13) Message" message-caesar-buffer-body t]
699     "----"
700     ["Post Message" message-send-and-exit t]
701     ["Abort Message" message-dont-send t]))
702
703 ;;;###autoload
704 (defun message-mode ()
705   "Major mode for editing mail and news to be sent.
706 Like Text Mode but with these additional commands:
707 C-c C-s  message-send (send the message)    C-c C-c  message-send-and-exit
708 C-c C-f  move to a header field (and create it if there isn't):
709          C-c C-f C-t  move to To        C-c C-f C-s  move to Subject
710          C-c C-f C-c  move to Cc        C-c C-f C-b  move to Bcc
711          C-c C-f C-f  move to Fcc       C-c C-f C-r  move to Reply-To
712          C-c C-f C-u  move to Summary   C-c C-f C-n  move to Newsgroups
713          C-c C-f C-k  move to Keywords  C-c C-f C-d  move to Distribution
714          C-c C-f C-o  move to Followup-To
715 C-c C-t  message-insert-to (add a To header to a news followup)
716 C-c C-n  message-insert-newsgroups (add a Newsgroup header to a news reply)
717 C-c C-b  message-goto-body (move to beginning of message text).
718 C-c C-i  message-goto-signature (move to the beginning of the signature).
719 C-c C-w  message-insert-signature (insert `message-signature-file' file).
720 C-c C-y  message-yank-original (insert current message, if any).
721 C-c C-q  message-fill-yanked-message (fill what was yanked).
722 C-c C-r  message-ceasar-buffer-body (rot13 the message body)."
723   (interactive)
724   (kill-all-local-variables)
725   (make-local-variable 'message-reply-buffer)
726   (setq message-reply-buffer nil)
727   (make-local-variable 'message-send-actions)
728   (set-syntax-table message-mode-syntax-table)
729   (use-local-map message-mode-map)
730   (setq local-abbrev-table text-mode-abbrev-table)
731   (setq major-mode 'message-mode)
732   (setq mode-name "Message")
733   (setq buffer-offer-save t)
734   (make-local-variable 'font-lock-defaults)
735   (setq font-lock-defaults '(message-font-lock-keywords t))
736   (make-local-variable 'facemenu-add-face-function)
737   (make-local-variable 'facemenu-remove-face-function)
738   (setq facemenu-add-face-function
739         (lambda (face end)
740           (let ((face-fun (cdr (assq face message-face-alist))))
741             (if face-fun
742                 (funcall face-fun (point) end)
743               (error "Face %s not configured for %s mode" face mode-name)))
744           "")
745         facemenu-remove-face-function t)
746   (make-local-variable 'paragraph-separate)
747   (make-local-variable 'paragraph-start)
748   (setq paragraph-start (concat (regexp-quote mail-header-separator)
749                                 "$\\|[ \t]*[-_][-_][-_]+$\\|"
750                                 paragraph-start))
751   (setq paragraph-separate (concat (regexp-quote mail-header-separator)
752                                    "$\\|[ \t]*[-_][-_][-_]+$\\|"
753                                    paragraph-separate))
754   (make-local-variable 'message-reply-headers)
755   (setq message-reply-headers nil)
756   (make-local-variable 'message-newsreader)
757   (make-local-variable 'message-mailer)
758   (make-local-variable 'message-post-method)
759   (make-local-variable 'message-sent-message-via)
760   (setq message-sent-message-via nil)
761   (make-local-variable 'message-checksum)
762   (setq message-checksum nil)
763   (when (fboundp 'mail-hist-define-keys)
764     (mail-hist-define-keys))
765   (when (string-match "XEmacs\\|Lucid" emacs-version)
766     (message-setup-toolbar))
767   (easy-menu-add message-mode-menu message-mode-map)
768   (run-hooks 'text-mode-hook 'message-mode-hook))
769
770 \f
771
772 ;;;
773 ;;; Message mode commands
774 ;;;
775
776 ;;; Movement commands
777
778 (defun message-goto-to ()
779   "Move point to the To header."
780   (interactive)
781   (message-position-on-field "To"))
782
783 (defun message-goto-subject ()
784   "Move point to the Subject header."
785   (interactive)
786   (message-position-on-field "Subject"))
787
788 (defun message-goto-cc ()
789   "Move point to the Cc header."
790   (interactive)
791   (message-position-on-field "Cc" "To"))
792
793 (defun message-goto-bcc ()
794   "Move point to the Bcc  header."
795   (interactive)
796   (message-position-on-field "Bcc" "Cc" "To"))
797
798 (defun message-goto-fcc ()
799   "Move point to the Fcc header."
800   (interactive)
801   (message-position-on-field "Fcc" "To" "Newsgroups"))
802
803 (defun message-goto-reply-to ()
804   "Move point to the Reply-To header."
805   (interactive)
806   (message-position-on-field "Reply-To" "Subject"))
807
808 (defun message-goto-newsgroups ()
809   "Move point to the Newsgroups header."
810   (interactive)
811   (message-position-on-field "Newsgroups"))
812
813 (defun message-goto-distribution ()
814   "Move point to the Distribution header."
815   (interactive)
816   (message-position-on-field "Distribution"))
817
818 (defun message-goto-followup-to ()
819   "Move point to the Followup-To header."
820   (interactive)
821   (message-position-on-field "Followup-To" "Newsgroups"))
822
823 (defun message-goto-keywords ()
824   "Move point to the Keywords header."
825   (interactive)
826   (message-position-on-field "Keywords" "Subject"))
827
828 (defun message-goto-summary ()
829   "Move point to the Summary header."
830   (interactive)
831   (message-position-on-field "Summary" "Subject"))
832
833 (defun message-goto-body ()
834   "Move point to the beginning of the message body."
835   (interactive)
836   (goto-char (point-min))
837   (search-forward (concat "\n" mail-header-separator "\n") nil t))
838
839 (defun message-goto-signature ()
840   "Move point to the beginning of the message signature."
841   (interactive)
842   (goto-char (point-min))
843   (or (re-search-forward message-signature-separator nil t)
844       (goto-char (point-max))))
845
846 \f
847
848 (defun message-insert-to ()
849   "Insert a To header that points to the author of the article being replied to."
850   (interactive)
851   (when (message-position-on-field "To")
852     (insert ", "))
853   (insert (or (message-fetch-reply-field "reply-to")
854               (message-fetch-reply-field "from") "")))
855
856 (defun message-insert-newsgroups ()
857   "Insert the Newsgroups header from the article being replied to."
858   (interactive)
859   (when (message-position-on-field "Newsgroups")
860     (insert ","))
861   (insert (or (message-fetch-reply-field "newsgroups") "")))
862
863 \f
864
865 ;;; Various commands
866
867 (defun message-insert-signature (&optional force)
868   "Insert a signature.  See documentation for the `message-signature' variable."
869   (interactive (list t))
870   (let* ((signature 
871           (cond ((and (null message-signature)
872                       force)
873                  t)
874                 ((message-functionp message-signature)
875                  (funcall message-signature))
876                 ((listp message-signature)
877                  (eval message-signature))
878                 (t message-signature)))
879          (signature
880           (cond ((stringp signature)
881                  signature)
882                 ((and (eq t signature)
883                       message-signature-file
884                       (file-exists-p message-signature-file))
885                  signature))))
886     (when signature
887       ;; Remove blank lines at the end of the message.
888       (goto-char (point-max))
889       (skip-chars-backward " \t\n")
890       (end-of-line)
891       (delete-region (point) (point-max))
892       ;; Insert the signature.
893       (insert "\n\n-- \n")
894       (if (eq signature t)
895           (insert-file-contents message-signature-file)
896         (insert signature))
897       (goto-char (point-max))
898       (or (bolp) (insert "\n")))))
899
900 (defvar message-caesar-translation-table nil)
901
902 (defun message-caesar-region (b e &optional n)
903   "Caesar rotation of region by N, default 13, for decrypting netnews."
904   (interactive
905    (list
906     (min (point) (or (mark t) (point)))
907     (max (point) (or (mark t) (point)))
908     (when current-prefix-arg
909       (prefix-numeric-value current-prefix-arg))))
910
911   (setq n (if (numberp n) (mod n 26) 13)) ;canonize N
912   (unless (or (zerop n)                 ; no action needed for a rot of 0
913               (= b e))                  ; no region to rotate
914     ;; We build the table, if necessary.
915     (when (or (not message-caesar-translation-table)
916               (/= (aref message-caesar-translation-table ?a) (+ ?a n)))
917       (let ((i -1) 
918             (table (make-string 256 0)))
919         (while (< (incf i) 256)
920           (aset table i i))
921         (setq table
922               (concat
923                (substring table 0 ?A)
924                (substring table (+ ?A n) (+ ?A n (- 26 n)))
925                (substring table ?A (+ ?A n))
926                (substring table (+ ?A 26) ?a)
927                (substring table (+ ?a n) (+ ?a n (- 26 n)))
928                (substring table ?a (+ ?a n))
929                (substring table (+ ?a 26) 255)))
930         (setq message-caesar-translation-table table)))
931     ;; Then we translate the region.  Do it this way to retain 
932     ;; text properties.
933     (while (< b e)
934       (subst-char-in-region 
935        b (1+ b) (char-after b)
936        (aref message-caesar-translation-table (char-after b)))
937       (incf b))))
938
939 (defun message-caesar-buffer-body (&optional rotnum)
940   "Caesar rotates all letters in the current buffer by 13 places.
941 Used to encode/decode possibly offensive messages (commonly in net.jokes).
942 With prefix arg, specifies the number of places to rotate each letter forward.
943 Mail and USENET news headers are not rotated."
944   (interactive (if current-prefix-arg
945                    (list (prefix-numeric-value current-prefix-arg))
946                  (list nil)))
947   (save-excursion
948     (save-restriction
949       (when (message-goto-body)
950         (narrow-to-region (point) (point-max)))
951       (message-caesar-region (point-min) (point-max) rotnum))))
952
953 (defun message-fill-yanked-message (&optional justifyp)
954   "Fill the paragraphs of a message yanked into this one.
955 Numeric argument means justify as well."
956   (interactive "P")
957   (save-excursion
958     (goto-char (point-min))
959     (search-forward (concat "\n" mail-header-separator "\n") nil t)
960     (let ((fill-prefix message-yank-prefix))
961       (fill-individual-paragraphs (point) (point-max) justifyp t))))
962
963 (defun message-indent-citation ()
964   "Modify text just inserted from a message to be cited.
965 The inserted text should be the region.
966 When this function returns, the region is again around the modified text.
967
968 Normally, indent each nonblank line `message-indentation-spaces' spaces.
969 However, if `message-yank-prefix' is non-nil, insert that prefix on each line."
970   (let ((start (point)))
971     ;; Remove unwanted headers.
972     (when message-ignored-cited-headers
973       (save-restriction
974         (narrow-to-region 
975          (goto-char start)
976          (if (search-forward "\n\n" nil t)
977              (1- (point))
978            (point)))
979         (message-remove-header message-ignored-cited-headers t)))
980     ;; Do the indentation.
981     (if (null message-yank-prefix)
982         (indent-rigidly start (mark t) message-indentation-spaces)
983       (save-excursion
984         (goto-char start)
985         (while (< (point) (mark t))
986           (insert message-yank-prefix)
987           (forward-line 1)))
988       (goto-char start))))
989
990 (defun message-yank-original (&optional arg)
991   "Insert the message being replied to, if any.
992 Puts point before the text and mark after.
993 Normally indents each nonblank line ARG spaces (default 3).  However,
994 if `message-yank-prefix' is non-nil, insert that prefix on each line.
995
996 Just \\[universal-argument] as argument means don't indent, insert no
997 prefix, and don't delete any headers."
998   (interactive "P")
999   (let ((modified (buffer-modified-p)))
1000     (when (and message-reply-buffer
1001                message-cite-function)
1002       (delete-windows-on message-reply-buffer t)
1003       (insert-buffer message-reply-buffer)
1004       (funcall message-cite-function)
1005       (exchange-point-and-mark)
1006       (unless (bolp)
1007         (insert ?\n))
1008       (unless modified
1009         (setq message-checksum (message-checksum))))))
1010
1011 (defun message-cite-original ()    
1012   (let ((start (point))
1013         (functions 
1014          (when message-indent-citation-function
1015            (if (listp message-indent-citation-function)
1016                message-indent-citation-function
1017              (list message-indent-citation-function)))))
1018     (goto-char start)
1019     (while functions
1020       (funcall (pop functions)))
1021     (when message-citation-line-function
1022       (unless (bolp)
1023         (insert "\n"))
1024       (funcall message-citation-line-function))))
1025
1026 (defun message-insert-citation-line ()
1027   "Function that inserts a simple citation line."
1028   (when message-reply-headers
1029     (insert (mail-header-from message-reply-headers) " writes:\n\n")))
1030
1031 (defun message-position-on-field (header &rest afters)
1032   (let ((case-fold-search t))
1033     (save-restriction
1034       (narrow-to-region
1035        (goto-char (point-min))
1036        (progn
1037          (re-search-forward 
1038           (concat "^" (regexp-quote mail-header-separator) "$"))
1039          (match-beginning 0)))
1040       (goto-char (point-min))
1041       (if (re-search-forward (concat "^" (regexp-quote header) ":") nil t)
1042           (progn
1043             (re-search-forward "^[^ \t]" nil 'move)
1044             (beginning-of-line)
1045             (skip-chars-backward "\n")
1046             t)
1047         (while (and afters
1048                     (not (re-search-forward 
1049                           (concat "^" (regexp-quote (car afters)) ":")
1050                           nil t)))
1051           (pop afters))
1052         (when afters
1053           (re-search-forward "^[^ \t]" nil 'move)
1054           (beginning-of-line))
1055         (insert header ": \n")
1056         (forward-char -1)
1057         nil))))
1058
1059 (defun message-remove-signature ()
1060   "Remove the signature from the text between point and mark.
1061 The text will also be indented the normal way."
1062   (save-excursion
1063     (let ((start (point))
1064           mark)
1065     (if (not (re-search-forward message-signature-separator (mark t) t))
1066         ;; No signature here, so we just indent the cited text.
1067         (message-indent-citation)
1068       ;; Find the last non-empty line.
1069       (forward-line -1)
1070       (while (looking-at "[ \t]*$")
1071         (forward-line -1))
1072       (forward-line 1)
1073       (setq mark (set-marker (make-marker) (point)))
1074       (goto-char start)
1075       (message-indent-citation)
1076       ;; Enable undoing the deletion.
1077       (undo-boundary)
1078       (delete-region mark (mark t))
1079       (set-marker mark nil)))))
1080
1081 \f
1082
1083 ;;;
1084 ;;; Sending messages
1085 ;;;
1086
1087 (defun message-send-and-exit (&optional arg)
1088   "Send message like `message-send', then, if no errors, exit from mail buffer."
1089   (interactive "P")
1090   (let ((buf (current-buffer)))
1091     (when (and (message-send arg)
1092                (buffer-name buf))
1093       (if message-kill-buffer-on-exit
1094           (kill-buffer buf)
1095         (bury-buffer buf)
1096         (when (eq buf (current-buffer))
1097           (message-bury buf))))))
1098
1099 (defun message-dont-send ()
1100   "Don't send the message you have been editing."
1101   (interactive)
1102   (message-bury (current-buffer)))
1103
1104 (defun message-bury (buffer)
1105   "Bury this mail buffer."
1106   (let ((newbuf (other-buffer buffer)))
1107     (bury-buffer buffer)
1108     (if (and (fboundp 'frame-parameters)
1109              (cdr (assq 'dedicated (frame-parameters)))
1110              (not (null (delq (selected-frame) (visible-frame-list)))))
1111         (delete-frame (selected-frame))
1112       (switch-to-buffer newbuf))))
1113
1114 (defun message-send (&optional arg)
1115   "Send the message in the current buffer.
1116 If `message-interactive' is non-nil, wait for success indication
1117 or error messages, and inform user.
1118 Otherwise any failure is reported in a message back to
1119 the user from the mailer."
1120   (interactive "P")
1121   (when (if buffer-file-name
1122             (y-or-n-p (format "Send buffer contents as %s message? "
1123                               (if (message-mail-p)
1124                                   (if (message-news-p) "mail and news" "mail")
1125                                 "news")))
1126           (or (buffer-modified-p)
1127               (y-or-n-p "No changes in the buffer; really send? ")))
1128     ;; Make it possible to undo the coming changes.
1129     (undo-boundary)
1130     (run-hooks 'message-send-hook)
1131     (message "Sending...")
1132     (when (and (or (not (message-news-p))
1133                    (and (or (not (memq 'news message-sent-message-via))
1134                             (y-or-n-p
1135                              "Already sent message via news; resend? "))
1136                         (funcall message-send-news-function arg)))
1137                (or (not (message-mail-p))
1138                    (and (or (not (memq 'mail message-sent-message-via))
1139                             (y-or-n-p
1140                              "Already sent message via mail; resend? "))
1141                         (funcall message-send-mail-function arg))))
1142       (message-do-fcc)
1143       (when (fboundp 'mail-hist-put-headers-into-history)
1144         (mail-hist-put-headers-into-history))
1145       (run-hooks 'message-sent-hook)
1146       (message "Sending...done")
1147       ;; If buffer has no file, mark it as unmodified and delete autosave.
1148       (unless buffer-file-name
1149         (set-buffer-modified-p nil)
1150         (delete-auto-save-file-if-necessary t))
1151       ;; Now perform actions on successful sending.
1152       (let ((actions message-send-actions))
1153         (while actions
1154           (condition-case nil
1155               (apply (caar actions) (cdar actions))
1156             (error))
1157           (pop actions)))
1158       ;; Return success.
1159       t)))
1160
1161 (defun message-send-mail (&optional arg)
1162   (require 'mail-utils)
1163   (let ((errbuf (if message-interactive
1164                     (generate-new-buffer " sendmail errors")
1165                   0))
1166         (tembuf (generate-new-buffer " message temp"))
1167         (case-fold-search nil)
1168         (news (message-news-p))
1169         resend-to-addresses delimline
1170         (mailbuf (current-buffer)))
1171     (save-restriction
1172       (message-narrow-to-headers)
1173       (setq resend-to-addresses (mail-fetch-field "resent-to"))
1174       ;; Insert some headers.
1175       (message-generate-headers message-required-mail-headers)
1176       ;; Let the user do all of the above.
1177       (run-hooks 'message-header-hook))
1178     (unwind-protect
1179         (save-excursion
1180           (set-buffer tembuf)
1181           (erase-buffer)
1182           (insert-buffer-substring mailbuf)
1183           ;; Remove some headers.
1184           (save-restriction
1185             (message-narrow-to-headers)
1186             ;; Remove some headers.
1187             (message-remove-header message-ignored-mail-headers t))
1188           (goto-char (point-max))
1189           ;; require one newline at the end.
1190           (or (= (preceding-char) ?\n)
1191               (insert ?\n))
1192           (when (and news
1193                      (or (mail-fetch-field "cc")
1194                          (mail-fetch-field "to")))
1195             (message-insert-courtesy-copy))
1196           (let ((case-fold-search t))
1197             ;; Change header-delimiter to be what sendmail expects.
1198             (goto-char (point-min))
1199             (re-search-forward
1200              (concat "^" (regexp-quote mail-header-separator) "\n"))
1201             (replace-match "\n")
1202             (backward-char 1)
1203             (setq delimline (point-marker))
1204             ;; Insert an extra newline if we need it to work around
1205             ;; Sun's bug that swallows newlines.
1206             (goto-char (1+ delimline))
1207             (when (eval message-mailer-swallows-blank-line)
1208               (newline))
1209             (when message-interactive
1210               (save-excursion
1211                 (set-buffer errbuf)
1212                 (erase-buffer))))
1213           (let ((default-directory "/"))
1214             (apply 'call-process-region
1215                    (append (list (point-min) (point-max)
1216                                  (if (boundp 'sendmail-program)
1217                                      sendmail-program
1218                                    "/usr/lib/sendmail")
1219                                  nil errbuf nil "-oi")
1220                            ;; Always specify who from,
1221                            ;; since some systems have broken sendmails.
1222                            (list "-f" (user-login-name))
1223                            ;; These mean "report errors by mail"
1224                            ;; and "deliver in background".
1225                            (if (null message-interactive) '("-oem" "-odb"))
1226                            ;; Get the addresses from the message
1227                            ;; unless this is a resend.
1228                            ;; We must not do that for a resend
1229                            ;; because we would find the original addresses.
1230                            ;; For a resend, include the specific addresses.
1231                            (if resend-to-addresses
1232                                (list resend-to-addresses)
1233                              '("-t")))))
1234           (when message-interactive
1235             (save-excursion
1236               (set-buffer errbuf)
1237               (goto-char (point-min))
1238               (while (re-search-forward "\n\n* *" nil t)
1239                 (replace-match "; "))
1240               (if (not (zerop (buffer-size)))
1241                   (error "Sending...failed to %s"
1242                          (buffer-substring (point-min) (point-max)))))))
1243       (kill-buffer tembuf)
1244       (when (bufferp errbuf)
1245         (kill-buffer errbuf)))
1246     (set-buffer mailbuf)
1247     (push 'mail message-sent-message-via)))
1248
1249 (defun message-send-news (&optional arg)
1250   (let ((tembuf (generate-new-buffer " *message temp*"))
1251         (case-fold-search nil)
1252         (method (if (message-functionp message-post-method)
1253                     (funcall message-post-method arg)
1254                   message-post-method))
1255         (messbuf (current-buffer)))
1256     (save-restriction
1257       (message-narrow-to-headers)
1258       ;; Insert some headers.
1259       (message-generate-headers message-required-news-headers)
1260       ;; Let the user do all of the above.
1261       (run-hooks 'message-header-hook))
1262     (when (message-check-news-syntax)
1263       (unwind-protect
1264           (save-excursion
1265             (set-buffer tembuf)
1266             (buffer-disable-undo (current-buffer))
1267             (erase-buffer) 
1268             (insert-buffer-substring messbuf)
1269             ;; Remove some headers.
1270             (save-restriction
1271               (message-narrow-to-headers)
1272               ;; Remove some headers.
1273               (message-remove-header message-ignored-news-headers t))
1274             (goto-char (point-max))
1275             ;; require one newline at the end.
1276             (or (= (preceding-char) ?\n)
1277                 (insert ?\n))
1278             (let ((case-fold-search t))
1279               ;; Remove the delimeter.
1280               (goto-char (point-min))
1281               (re-search-forward
1282                (concat "^" (regexp-quote mail-header-separator) "\n"))
1283               (replace-match "\n")
1284               (backward-char 1))
1285             (require (car method))
1286             (funcall (intern (format "%s-open-server" (car method)))
1287                      (cadr method) (cddr method))
1288             (funcall (intern (format "%s-request-post"
1289                                      (car method)))))
1290         (kill-buffer tembuf))
1291       (set-buffer messbuf)
1292       (push 'news message-sent-message-via))))
1293
1294 ;;;
1295 ;;; Header generation & syntax checking.
1296 ;;;
1297
1298 (defun message-check-news-syntax ()
1299   "Check the syntax of the message."
1300   (and 
1301    ;; We narrow to the headers and check them first.
1302    (save-excursion
1303      (save-restriction
1304        (message-narrow-to-headers)
1305        (and 
1306         ;; Check for commands in Subject.
1307         (or 
1308          (message-check-element 'subject-cmsg)
1309          (save-excursion
1310            (if (string-match "^cmsg " (mail-fetch-field "subject"))
1311                (y-or-n-p
1312                 "The control code \"cmsg \" is in the subject. Really post? ")
1313              t)))
1314         ;; Check for multiple identical headers.
1315         (or (message-check-element 'multiple-headers)
1316             (save-excursion
1317               (let (found)
1318                 (while (and (not found) 
1319                             (re-search-forward "^[^ \t:]+: " nil t))
1320                   (save-excursion
1321                     (or (re-search-forward 
1322                          (concat "^" (setq found
1323                                            (buffer-substring 
1324                                             (match-beginning 0) 
1325                                             (- (match-end 0) 2))))
1326                          nil t)
1327                         (setq found nil))))
1328                 (if found
1329                     (y-or-n-p 
1330                      (format "Multiple %s headers. Really post? " found))
1331                   t))))
1332         ;; Check for Version and Sendsys.
1333         (or (message-check-element 'sendsys)
1334             (save-excursion
1335               (if (re-search-forward "^Sendsys:\\|^Version:" nil t)
1336                   (y-or-n-p
1337                    (format "The article contains a %s command. Really post? "
1338                            (buffer-substring (match-beginning 0) 
1339                                              (1- (match-end 0)))))
1340                 t)))
1341         ;; See whether we can shorten Followup-To.
1342         (or (message-check-element 'shorten-followup-to)
1343             (let ((newsgroups (mail-fetch-field "newsgroups"))
1344                   (followup-to (mail-fetch-field "followup-to"))
1345                   to)
1346               (when (and newsgroups (string-match "," newsgroups)
1347                          (not followup-to)
1348                          (not
1349                           (zerop
1350                            (length
1351                             (setq to (completing-read 
1352                                       "Followups to: (default all groups) " 
1353                                       (mapcar (lambda (g) (list g))
1354                                               (cons "poster" 
1355                                                     (message-tokenize-header 
1356                                                      newsgroups)))))))))
1357                 (goto-char (point-min))
1358                 (insert "Followup-To: " to "\n"))
1359               t))
1360
1361         ;; Check for Approved.
1362         (or (message-check-element 'approved)
1363             (save-excursion
1364               (if (re-search-forward "^Approved:" nil t)
1365                   (y-or-n-p
1366                    "The article contains an Approved header. Really post? ")
1367                 t)))
1368         ;; Check the Message-Id header.
1369         (or (message-check-element 'message-id)
1370             (save-excursion
1371               (let* ((case-fold-search t)
1372                      (message-id (mail-fetch-field "message-id")))
1373                 (or (not message-id)
1374                     (and (string-match "@" message-id)
1375                          (string-match "@[^\\.]*\\." message-id))
1376                     (y-or-n-p
1377                      (format 
1378                       "The Message-ID looks strange: \"%s\". Really post? "
1379                       message-id))))))
1380         ;; Check the Subject header.
1381         (or 
1382          (message-check-element 'subject)
1383          (save-excursion
1384            (let* ((case-fold-search t)
1385                   (subject (mail-fetch-field "subject")))
1386              (or
1387               (and subject
1388                    (not (string-match "\\`[ \t]*\\'" subject)))
1389               (progn
1390                 (message 
1391                  "The subject field is empty or missing.  Posting is denied.")
1392                 nil)))))
1393         ;; Check the From header.
1394         (or (message-check-element 'from)
1395             (save-excursion
1396               (let* ((case-fold-search t)
1397                      (from (mail-fetch-field "from")))
1398                 (cond
1399                  ((not from)
1400                   (message "There is no From line.  Posting is denied.")
1401                   nil)
1402                  ((not (string-match "@[^\\.]*\\." from))
1403                   (message
1404                    "Denied posting -- the From looks strange: \"%s\"." from)
1405                   nil)
1406                  ((string-match "@[^@]*@" from)
1407                   (message 
1408                    "Denied posting -- two \"@\"'s in the From header: %s."
1409                    from)
1410                   nil)
1411                  ((string-match "(.*).*(.*)" from)
1412                   (message
1413                    "Denied posting -- the From header looks strange: \"%s\"." 
1414                    from)
1415                   nil)
1416                  (t t))))))))
1417    ;; Check for long lines.
1418    (or (message-check-element 'long-lines)
1419        (save-excursion
1420          (goto-char (point-min))
1421          (re-search-forward
1422           (concat "^" (regexp-quote mail-header-separator) "$"))
1423          (while (and
1424                  (progn
1425                    (end-of-line)
1426                    (< (current-column) 80))
1427                  (zerop (forward-line 1))))
1428          (or (bolp)
1429              (eobp)
1430              (y-or-n-p
1431               "You have lines longer than 79 characters.  Really post? "))))
1432    ;; Check whether the article is empty.
1433    (or (message-check-element 'empty)
1434        (save-excursion
1435          (goto-char (point-min))
1436          (re-search-forward
1437           (concat "^" (regexp-quote mail-header-separator) "$"))
1438          (forward-line 1)
1439          (let ((b (point)))
1440            (or (re-search-forward message-signature-separator nil t)
1441                (goto-char (point-max)))
1442            (beginning-of-line)
1443            (or (re-search-backward "[^ \n\t]" b t)
1444                (y-or-n-p "Empty article.  Really post? ")))))
1445    ;; Check for control characters.
1446    (or (message-check-element 'control-chars)
1447        (save-excursion
1448          (if (re-search-forward "[\000-\007\013\015-\037\200-\237]" nil t)
1449              (y-or-n-p 
1450               "The article contains control characters. Really post? ")
1451            t)))
1452    ;; Check excessive size.
1453    (or (message-check-element 'size)
1454        (if (> (buffer-size) 60000)
1455            (y-or-n-p
1456             (format "The article is %d octets long. Really post? "
1457                     (buffer-size)))
1458          t))
1459    ;; Check whether any new text has been added.
1460    (or (message-check-element 'new-text)
1461        (not message-checksum)
1462        (not (eq (message-checksum) message-checksum))
1463        (y-or-n-p
1464         "It looks like no new text has been added.  Really post? "))
1465    ;; Check the length of the signature.
1466    (or (message-check-element 'signature)
1467        (progn
1468          (goto-char (point-max))
1469          (if (not (re-search-backward "^-- $" nil t))
1470              t
1471            (if (> (count-lines (point) (point-max)) 5)
1472                (y-or-n-p
1473                 (format
1474                  "Your .sig is %d lines; it should be max 4.  Really post? "
1475                  (count-lines (point) (point-max))))
1476              t))))))
1477
1478 (defun message-check-element (type)
1479   "Returns non-nil if this type is not to be checked."
1480   (if (eq message-syntax-checks 'dont-check-for-anything-just-trust-me)
1481       t
1482     (let ((able (assq type message-syntax-checks)))
1483       (and (consp able)
1484            (eq (cdr able) 'disabled)))))
1485
1486 (defun message-checksum ()
1487   "Return a \"checksum\" for the current buffer."
1488   (let ((sum 0))
1489     (save-excursion
1490       (goto-char (point-min))
1491       (re-search-forward
1492        (concat "^" (regexp-quote mail-header-separator) "$"))
1493       (while (not (eobp))
1494         (setq sum (logxor sum (following-char)))
1495         (forward-char 1)))
1496     sum))
1497
1498 (defun message-do-fcc ()
1499   "Process Fcc headers in the current buffer."
1500   (let ((case-fold-search t)
1501         (buf (current-buffer))
1502         list file)
1503     (save-excursion
1504       (set-buffer (get-buffer-create " *message temp*"))
1505       (buffer-disable-undo (current-buffer))
1506       (erase-buffer)
1507       (insert-buffer-substring buf)
1508       (save-restriction
1509         (message-narrow-to-headers)
1510         (while (setq file (mail-fetch-field "fcc"))
1511           (push file list)
1512           (message-remove-header "fcc" nil t)))
1513       (goto-char (point-min))
1514       (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1515       (replace-match "" t t)
1516       ;; Process FCC operations.
1517       (while list
1518         (setq file (pop list))
1519         (if (string-match "^[ \t]*|[ \t]*\\(.*\\)[ \t]*$" file)
1520             ;; Pipe the article to the program in question.
1521             (call-process-region (point-min) (point-max) shell-file-name
1522                                  nil nil nil "-c" (match-string 1 file))
1523           ;; Save the article.
1524           (setq file (expand-file-name file))
1525           (unless (file-exists-p (file-name-directory file))
1526             (make-directory (file-name-directory file) t))
1527           (if (and message-fcc-handler-function
1528                    (not (eq message-fcc-handler-function 'rmail-output)))
1529               (funcall message-fcc-handler-function file)
1530             (if (and (file-readable-p file) (mail-file-babyl-p file))
1531                 (rmail-output file 1)
1532               (let ((mail-use-rfc822 t))
1533                 (rmail-output file 1 t t))))))
1534       (kill-buffer (current-buffer)))))
1535
1536 (defun message-cleanup-headers ()
1537   "Do various automatic cleanups of the headers."
1538   ;; Remove empty lines in the header.
1539   (save-restriction
1540     (message-narrow-to-headers)
1541     (while (re-search-forward "^[ \t]*\n" nil t)
1542       (replace-match "" t t)))
1543
1544   ;; Correct Newsgroups and Followup-To headers: change sequence of
1545   ;; spaces to comma and eliminate spaces around commas.  Eliminate
1546   ;; embedded line breaks.
1547   (goto-char (point-min))
1548   (while (re-search-forward "^\\(Newsgroups\\|Followup-To\\): +" nil t)
1549     (save-restriction
1550       (narrow-to-region
1551        (point)
1552        (if (re-search-forward "^[^ \t]" nil t)
1553            (match-beginning 0)
1554          (forward-line 1)
1555          (point)))
1556       (goto-char (point-min))
1557       (while (re-search-forward "\n[ \t]+" nil t)
1558         (replace-match " " t t))        ;No line breaks (too confusing)
1559       (goto-char (point-min))
1560       (while (re-search-forward "[ \t\n]*,[ \t\n]*\\|[ \t]+" nil t)
1561         (replace-match "," t t))
1562       (goto-char (point-min))
1563       ;; Remove trailing commas.
1564       (when (re-search-forward ",+$" nil t)
1565         (replace-match "" t t)))))
1566
1567 (defun message-make-date ()
1568   "Make a valid data header."
1569   (let ((now (current-time)))
1570     (timezone-make-date-arpa-standard 
1571      (current-time-string now) (current-time-zone now))))
1572
1573 (defun message-make-message-id ()
1574   "Make a unique Message-ID."
1575   (concat "<" (message-unique-id) 
1576           (let ((psubject (save-excursion (mail-fetch-field "subject"))))
1577             (if (and message-reply-headers
1578                      (mail-header-references message-reply-headers)
1579                      (mail-header-subject message-reply-headers)
1580                      psubject
1581                      (mail-header-subject message-reply-headers)
1582                      (not (string= 
1583                            (message-strip-subject-re
1584                             (mail-header-subject message-reply-headers))
1585                            (message-strip-subject-re psubject))))
1586                 "_-_" ""))
1587           "@" (message-make-fqdm) ">"))
1588
1589 (defvar message-unique-id-char nil)
1590
1591 ;; If you ever change this function, make sure the new version
1592 ;; cannot generate IDs that the old version could.
1593 ;; You might for example insert a "." somewhere (not next to another dot
1594 ;; or string boundary), or modify the "fsf" string.
1595 (defun message-unique-id ()
1596   ;; Don't use microseconds from (current-time), they may be unsupported.
1597   ;; Instead we use this randomly inited counter.
1598   (setq message-unique-id-char
1599         (% (1+ (or message-unique-id-char (logand (random t) (1- (lsh 1 20)))))
1600            ;; (current-time) returns 16-bit ints,
1601            ;; and 2^16*25 just fits into 4 digits i base 36.
1602            (* 25 25)))
1603   (let ((tm (current-time)))
1604     (concat
1605      (if (memq system-type '(ms-dos emx vax-vms))
1606          (let ((user (downcase (user-login-name))))
1607            (while (string-match "[^a-z0-9_]" user)
1608              (aset user (match-beginning 0) ?_))
1609            user)
1610        (message-number-base36 (user-uid) -1))
1611      (message-number-base36 (+ (car   tm) 
1612                                (lsh (% message-unique-id-char 25) 16)) 4)
1613      (message-number-base36 (+ (nth 1 tm)
1614                                (lsh (/ message-unique-id-char 25) 16)) 4)
1615      ;; Append the newsreader name, because while the generated
1616      ;; ID is unique to this newsreader, other newsreaders might
1617      ;; otherwise generate the same ID via another algorithm.
1618      ".fsf")))
1619
1620 (defun message-number-base36 (num len)
1621   (if (if (< len 0) (<= num 0) (= len 0))
1622       ""
1623     (concat (message-number-base36 (/ num 36) (1- len))
1624             (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
1625                                   (% num 36))))))
1626
1627 (defun message-make-organization ()
1628   "Make an Organization header."
1629   (let* ((organization 
1630           (or (getenv "ORGANIZATION")
1631               (when message-user-organization
1632                 (if (message-functionp message-user-organization)
1633                     (funcall message-user-organization)
1634                   message-user-organization)))))
1635     (save-excursion
1636       (message-set-work-buffer)
1637       (cond ((stringp organization)
1638              (insert organization))
1639             ((and (eq t organization)
1640                   message-user-organization-file
1641                   (file-exists-p message-user-organization-file))
1642              (insert-file-contents message-user-organization-file)))
1643       (goto-char (point-min))
1644       (while (re-search-forward "[\t\n]+" nil t)
1645         (replace-match "" t t))
1646       (unless (zerop (buffer-size))
1647         (buffer-string)))))
1648
1649 (defun message-make-lines ()
1650   "Count the number of lines and return numeric string."
1651   (save-excursion
1652     (save-restriction
1653       (widen)
1654       (goto-char (point-min))
1655       (re-search-forward 
1656        (concat "^" (regexp-quote mail-header-separator) "$"))
1657       (forward-line 1)
1658       (int-to-string (count-lines (point) (point-max))))))
1659
1660 (defun message-make-in-reply-to ()
1661   "Return the In-Reply-To header for this message."
1662   (when message-reply-headers
1663     (let ((from (mail-header-from message-reply-headers))
1664           (date (mail-header-date message-reply-headers)))
1665       (when from
1666         (let ((stop-pos 
1667                (string-match "  *at \\|  *@ \\| *(\\| *<" from)))
1668           (concat (if stop-pos (substring from 0 stop-pos) from)
1669                   "'s message of " 
1670                   (if (or (not date) (string= date ""))
1671                       "(unknown date)" date)))))))
1672
1673 (defun message-make-distribution ()
1674   "Make a Distribution header."
1675   (let ((orig-distribution (message-fetch-reply-field "distribution")))
1676     (cond ((message-functionp message-distribution-function)
1677            (funcall message-distribution-function))
1678           (t orig-distribution))))
1679
1680 (defun message-make-expires ()
1681   "Return an Expires header based on `message-expires'."
1682   (let ((current (current-time))
1683         (future (* 1.0 message-expires 60 60 24)))
1684     ;; Add the future to current.
1685     (setcar current (+ (car current) (round (/ future (expt 2 16)))))
1686     (setcar (cdr current) (+ (nth 1 current) (% (round future) (expt 2 16))))
1687     ;; Return the date in the future in UT.
1688     (timezone-make-date-arpa-standard 
1689      (current-time-string current) (current-time-zone current) '(0 "UT"))))
1690
1691 (defun message-make-path ()
1692   "Return uucp path."
1693   (let ((login-name (user-login-name)))
1694     (cond ((null message-user-path)
1695            (concat (system-name) "!" login-name))
1696           ((stringp message-user-path)
1697            ;; Support GENERICPATH.  Suggested by vixie@decwrl.dec.com.
1698            (concat message-user-path "!" login-name))
1699           (t login-name))))
1700
1701 (defun message-make-from ()
1702   "Make a From header."
1703   (let* ((login (message-make-address))
1704          (fullname (user-full-name)))
1705     (when (string= fullname "&")
1706       (setq fullname (user-login-name)))
1707     (save-excursion
1708       (message-set-work-buffer)
1709       (cond 
1710        ((or (null message-from-style)
1711             (equal fullname ""))
1712         (insert login))
1713        ((or (eq message-from-style 'angles)
1714             (and (not (eq message-from-style 'parens))
1715                  ;; Use angles if no quoting is needed, or if parens would
1716                  ;; need quoting too.
1717                  (or (not (string-match "[^- !#-'*+/-9=?A-Z^-~]" fullname))
1718                      (let ((tmp (concat fullname nil)))
1719                        (while (string-match "([^()]*)" tmp)
1720                          (aset tmp (match-beginning 0) ?-)
1721                          (aset tmp (1- (match-end 0)) ?-))
1722                        (string-match "[\\()]" tmp)))))
1723         (insert fullname)
1724         (goto-char (point-min))
1725         ;; Look for a character that cannot appear unquoted
1726         ;; according to RFC 822.
1727         (when (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]" nil 1)
1728           ;; Quote fullname, escaping specials.
1729           (goto-char (point-min))
1730           (insert "\"")
1731           (while (re-search-forward "[\"\\]" nil 1)
1732             (replace-match "\\\\\\&" t))
1733           (insert "\""))
1734         (insert " <" login ">"))
1735        (t                               ; 'parens or default
1736         (insert login " (")
1737         (let ((fullname-start (point)))
1738           (insert fullname)
1739           (goto-char fullname-start)
1740           ;; RFC 822 says \ and nonmatching parentheses
1741           ;; must be escaped in comments.
1742           ;; Escape every instance of ()\ ...
1743           (while (re-search-forward "[()\\]" nil 1)
1744             (replace-match "\\\\\\&" t))
1745           ;; ... then undo escaping of matching parentheses,
1746           ;; including matching nested parentheses.
1747           (goto-char fullname-start)
1748           (while (re-search-forward 
1749                   "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
1750                     nil 1)
1751             (replace-match "\\1(\\3)" t)
1752             (goto-char fullname-start)))
1753         (insert ")")))
1754       (buffer-string))))
1755
1756 (defun message-make-sender ()
1757   "Return the \"real\" user address.
1758 This function tries to ignore all user modifications, and 
1759 give as trustworthy answer as possible."
1760   (concat (user-login-name) "@" (system-name)))
1761
1762 (defun message-make-address ()
1763   "Make the address of the user."
1764   (or (message-user-mail-address)
1765       (concat (user-login-name) "@" (message-make-domain))))
1766
1767 (defun message-user-mail-address ()
1768   "Return the pertinent part of `user-mail-address'."
1769   (when user-mail-address
1770     (nth 1 (mail-extract-address-components user-mail-address))))
1771
1772 (defun message-make-fqdm ()
1773   "Return user's fully qualified domain name."
1774   (let ((system-name (system-name)))
1775     (cond 
1776      ((string-match "[^.]\\.[^.]" system-name)
1777       ;; `system-name' returned the right result.
1778       system-name)
1779      ;; We try `user-mail-address' as a backup.
1780      ((string-match "@\\(.*\\)\\'" (message-user-mail-address))
1781       (match-string 1 user-mail-address))
1782      ;; Try `mail-host-address'.
1783      ((and (boundp 'mail-host-address)
1784            mail-host-address)
1785       mail-host-address)
1786      ;; Default to this bogus thing.
1787      (t
1788       (concat system-name ".i-have-a-misconfigured-system-so-shoot-me")))))
1789
1790 (defun message-make-host-name ()
1791   "Return the name of the host."
1792   (let ((fqdm (message-make-fqdm)))
1793     (string-match "^[^.]+\\." fqdm)
1794     (substring fqdm 0 (1- (match-end 0)))))
1795
1796 (defun message-make-domain ()
1797   "Return the domain name."
1798   (or mail-host-address
1799       (message-make-fqdm)))
1800
1801 (defun message-generate-headers (headers)
1802   "Prepare article HEADERS.
1803 Headers already prepared in the buffer are not modified."
1804   (save-restriction
1805     (message-narrow-to-headers)
1806     (let* ((Date (message-make-date))
1807            (Message-ID (message-make-message-id))
1808            (Organization (message-make-organization))
1809            (From (message-make-from))
1810            (Path (message-make-path))
1811            (Subject nil)
1812            (Newsgroups nil)
1813            (In-Reply-To (message-make-in-reply-to))
1814            (To nil)
1815            (Distribution (message-make-distribution))
1816            (Lines (message-make-lines))
1817            (X-Newsreader message-newsreader)
1818            (X-Mailer (and (not (mail-fetch-field "X-Newsreader"))
1819                           message-mailer))
1820            (Expires (message-make-expires))
1821            (case-fold-search t)
1822            header value elem)
1823       ;; First we remove any old generated headers.
1824       (let ((headers message-deletable-headers))
1825         (while headers
1826           (goto-char (point-min))
1827           (and (re-search-forward 
1828                 (concat "^" (symbol-name (car headers)) ": *") nil t)
1829                (get-text-property (1+ (match-beginning 0)) 'message-deletable)
1830                (message-delete-line))
1831           (pop headers)))
1832       ;; Go through all the required headers and see if they are in the
1833       ;; articles already. If they are not, or are empty, they are
1834       ;; inserted automatically - except for Subject, Newsgroups and
1835       ;; Distribution. 
1836       (while headers
1837         (goto-char (point-min))
1838         (setq elem (pop headers))
1839         (if (consp elem)
1840             (if (eq (car elem) 'optional)
1841                 (setq header (cdr elem))
1842               (setq header (car elem)))
1843           (setq header elem))
1844         (when (or (not (re-search-forward 
1845                         (concat "^" (downcase (symbol-name header)) ":") 
1846                         nil t))
1847                   (progn
1848                     ;; The header was found. We insert a space after the
1849                     ;; colon, if there is none.
1850                     (if (/= (following-char) ? ) (insert " ") (forward-char 1))
1851                     ;; Find out whether the header is empty...
1852                     (looking-at "[ \t]*$")))
1853           ;; So we find out what value we should insert.
1854           (setq value
1855                 (cond 
1856                  ((and (consp elem) (eq (car elem) 'optional))
1857                   ;; This is an optional header.  If the cdr of this
1858                   ;; is something that is nil, then we do not insert
1859                   ;; this header.
1860                   (setq header (cdr elem))
1861                   (or (and (fboundp (cdr elem)) (funcall (cdr elem)))
1862                       (and (boundp (cdr elem)) (symbol-value (cdr elem)))))
1863                  ((consp elem)
1864                   ;; The element is a cons.  Either the cdr is a
1865                   ;; string to be inserted verbatim, or it is a
1866                   ;; function, and we insert the value returned from
1867                   ;; this function.
1868                   (or (and (stringp (cdr elem)) (cdr elem))
1869                       (and (fboundp (cdr elem)) (funcall (cdr elem)))))
1870                  ((and (boundp header) (symbol-value header))
1871                   ;; The element is a symbol.  We insert the value
1872                   ;; of this symbol, if any.
1873                   (symbol-value header))
1874                  (t
1875                   ;; We couldn't generate a value for this header,
1876                   ;; so we just ask the user.
1877                   (read-from-minibuffer
1878                    (format "Empty header for %s; enter value: " header)))))
1879           ;; Finally insert the header.
1880           (when (and value 
1881                      (not (equal value "")))
1882             (save-excursion
1883               (if (bolp)
1884                   (progn
1885                     ;; This header didn't exist, so we insert it.
1886                     (goto-char (point-max))
1887                     (insert (symbol-name header) ": " value "\n")
1888                     (forward-line -1))
1889                 ;; The value of this header was empty, so we clear
1890                 ;; totally and insert the new value.
1891                 (delete-region (point) (message-point-at-eol))
1892                 (insert value))
1893               ;; Add the deletable property to the headers that require it.
1894               (and (memq header message-deletable-headers)
1895                    (progn (beginning-of-line) (looking-at "[^:]+: "))
1896                    (add-text-properties 
1897                     (point) (match-end 0)
1898                     '(message-deletable t face italic) (current-buffer)))))))
1899       ;; Insert new Sender if the From is strange. 
1900       (let ((from (mail-fetch-field "from"))
1901             (sender (mail-fetch-field "sender"))
1902             (secure-sender (message-make-sender)))
1903         (when (and from 
1904                    (not (message-check-element 'sender))
1905                    (not (string=
1906                          (downcase
1907                           (cadr (mail-extract-address-components from)))
1908                          (downcase secure-sender)))
1909                    (or (null sender)
1910                        (not 
1911                         (string=
1912                          (downcase
1913                           (cadr (mail-extract-address-components sender)))
1914                          (downcase secure-sender)))))
1915           (goto-char (point-min))    
1916           ;; Rename any old Sender headers to Original-Sender.
1917           (when (re-search-forward "^Sender:" nil t)
1918             (beginning-of-line)
1919             (insert "Original-")
1920             (beginning-of-line))
1921           (insert "Sender: " secure-sender "\n"))))))
1922
1923 (defun message-insert-courtesy-copy ()
1924   "Insert a courtesy message in mail copies of combined messages."
1925   (save-excursion
1926     (save-restriction
1927       (message-narrow-to-headers)
1928       (let ((newsgroups (mail-fetch-field "newsgroups")))
1929         (when newsgroups
1930           (goto-char (point-max))
1931           (insert "Posted-To: " newsgroups "\n"))))
1932     (forward-line 1)
1933     (insert message-courtesy-message)))
1934     
1935 ;;;
1936 ;;; Setting up a message buffer
1937 ;;;
1938
1939 (defun message-fill-header (header value)
1940   (let ((begin (point))
1941         (fill-column 78)
1942         (fill-prefix "\t"))
1943     (insert (capitalize (symbol-name header))
1944             ": "
1945             (if (consp value) (car value) value)
1946             "\n")
1947     (save-restriction
1948       (narrow-to-region begin (point))
1949       (fill-region-as-paragraph begin (point))
1950       ;; Tapdance around looong Message-IDs.
1951       (forward-line -1)
1952       (when (looking-at "[ \t]*$")
1953         (message-delete-line))
1954       (goto-char begin)
1955       (re-search-forward ":" nil t)
1956       (when (looking-at "\n[ \t]+")
1957         (replace-match " " t t))
1958       (goto-char (point-max)))))
1959
1960 (defun message-position-point ()
1961   "Move point to where the user probably wants to find it."
1962   (message-narrow-to-headers)
1963   (cond 
1964    ((re-search-forward "^[^:]+:[ \t]*$" nil t)
1965     (search-backward ":" )
1966     (widen)
1967     (forward-char 1)
1968     (if (= (following-char) ? )
1969         (forward-char 1)
1970       (insert " ")))
1971    (t
1972     (goto-char (point-max))
1973     (widen)
1974     (forward-line 1)
1975     (unless (looking-at "$")
1976       (forward-line 2)))
1977    (sit-for 0)))
1978
1979 (defun message-pop-to-buffer (name)
1980   "Pop to buffer NAME, and warn if it already exists and is modified."
1981   (if message-generate-new-buffers
1982       (set-buffer (pop-to-buffer (generate-new-buffer name)))
1983     (let ((buffer (get-buffer name)))
1984       (if (and buffer
1985                (buffer-name buffer))
1986           (progn
1987             (set-buffer (pop-to-buffer buffer))
1988             (when (and (buffer-modified-p)
1989                        (not (y-or-n-p
1990                              "Message already being composed; erase? ")))
1991               (error "Message being composed")))
1992         (set-buffer (pop-to-buffer name)))))
1993   (erase-buffer)
1994   (message-mode))
1995
1996 (defun message-setup (headers &optional replybuffer actions)
1997   (setq message-send-actions actions)
1998   (setq message-reply-buffer replybuffer)
1999   (goto-char (point-min))
2000   ;; Insert all the headers.
2001   (mail-header-format 
2002    (let ((h headers)
2003          (alist message-header-format-alist))
2004      (while h
2005        (unless (assq (caar h) message-header-format-alist)
2006          (push (list (caar h)) alist))
2007        (pop h))
2008      alist)
2009    headers)
2010   (forward-line -1)
2011   (when message-default-headers
2012     (insert message-default-headers))
2013   (insert mail-header-separator "\n")
2014   (forward-line -1)
2015   (when (message-news-p)
2016     (when message-default-news-headers
2017       (insert message-default-news-headers))
2018     (when message-generate-headers-first
2019       (message-generate-headers
2020        (delq 'Lines
2021              (delq 'Subject
2022                    (copy-sequence message-required-news-headers))))))
2023   (when (message-mail-p)
2024     (when message-default-mail-headers
2025       (insert message-default-mail-headers))
2026     (when message-generate-headers-first
2027       (message-generate-headers
2028        (delq 'Lines
2029              (delq 'Subject
2030                    (copy-sequence message-required-mail-headers))))))
2031   (message-insert-signature)
2032   (message-set-auto-save-file-name)
2033   (save-restriction
2034     (message-narrow-to-headers)
2035     (run-hooks 'message-header-setup-hook))
2036   ;; Allow mail alias things.
2037   (if (fboundp 'mail-abbrevs-setup)
2038       (mail-abbrevs-setup)
2039     (funcall (intern "mail-aliases-setup")))
2040   (set-buffer-modified-p nil)
2041   (run-hooks 'message-setup-hook)
2042   (message-position-point)
2043   (undo-boundary))
2044
2045 (defun message-set-auto-save-file-name ()
2046   "Associate the message buffer with a file in the drafts directory."
2047   (when message-autosave-directory
2048     (unless (file-exists-p message-autosave-directory)
2049       (make-directory message-autosave-directory t))
2050     (let ((name (make-temp-name
2051                  (concat (file-name-as-directory message-autosave-directory)
2052                          "msg."))))
2053       (setq buffer-auto-save-file-name
2054             (save-excursion
2055               (prog1
2056                   (progn
2057                     (set-buffer (get-buffer-create " *draft tmp*"))
2058                     (setq buffer-file-name name)
2059                     (make-auto-save-file-name))
2060                 (kill-buffer (current-buffer)))))
2061       (clear-visited-file-modtime))))
2062
2063 \f
2064
2065 ;;;
2066 ;;; Commands for interfacing with message
2067 ;;;
2068
2069 ;;;###autoload
2070 (defun message-mail (&optional to subject)
2071   "Start editing a mail message to be sent."
2072   (interactive)
2073   (message-pop-to-buffer "*mail message*")
2074   (message-setup `((To . ,(or to "")) (Subject . ,(or subject "")))))
2075
2076 ;;;###autoload
2077 (defun message-news (&optional newsgroups subject)
2078   "Start editing a news article to be sent."
2079   (interactive)
2080   (message-pop-to-buffer "*news message*")
2081   (message-setup `((Newsgroups . ,(or newsgroups "")) 
2082                    (Subject . ,(or subject "")))))
2083
2084 ;;;###autoload
2085 (defun message-reply (&optional to-address wide ignore-reply-to)
2086   "Start editing a reply to the article in the current buffer."
2087   (interactive)
2088   (let ((cur (current-buffer))
2089         from subject date reply-to to cc
2090         references message-id follow-to 
2091         mct never-mct gnus-warning)
2092     (save-restriction
2093       (narrow-to-region
2094        (goto-char (point-min))
2095        (if (search-forward "\n\n" nil t)
2096            (1- (point))
2097          (point-max)))
2098       ;; Allow customizations to have their say.
2099       (if (not wide)
2100           ;; This is a regular reply.
2101           (if (message-functionp message-reply-to-function)
2102               (setq follow-to (funcall message-reply-to-function)))
2103         ;; This is a followup.
2104         (if (message-functionp message-wide-reply-to-function)
2105             (save-excursion
2106               (setq follow-to
2107                     (funcall message-wide-reply-to-function)))))
2108       ;; Find all relevant headers we need.
2109       (setq from (mail-fetch-field "from")
2110             date (mail-fetch-field "date") 
2111             subject (or (mail-fetch-field "subject") "none")
2112             to (mail-fetch-field "to")
2113             cc (mail-fetch-field "cc")
2114             mct (mail-fetch-field "mail-copies-to")
2115             reply-to (unless ignore-reply-to (mail-fetch-field "reply-to"))
2116             references (mail-fetch-field "references")
2117             message-id (mail-fetch-field "message-id"))
2118       ;; Remove any (buggy) Re:'s that are present and make a
2119       ;; proper one.
2120       (when (string-match "^[ \t]*[Rr][Ee]:[ \t]*" subject)
2121         (setq subject (substring subject (match-end 0))))
2122       (setq subject (concat "Re: " subject))
2123
2124       (when (and (setq gnus-warning (mail-fetch-field "gnus-warning"))
2125                  (string-match "<[^>]+>" gnus-warning))
2126         (setq message-id (match-string 0 gnus-warning)))
2127             
2128       ;; Handle special values of Mail-Copies-To.
2129       (when mct
2130         (cond ((equal (downcase mct) "never")
2131                (setq never-mct t)
2132                (setq mct nil))
2133               ((equal (downcase mct) "always")
2134                (setq mct (or reply-to from)))))
2135
2136       (unless follow-to
2137         (if (or (not wide)
2138                 to-address)
2139             (setq follow-to (list (cons 'To (or to-address reply-to from))))
2140           (let (ccalist)
2141             (save-excursion
2142               (message-set-work-buffer)
2143               (unless never-mct
2144                 (insert (or reply-to from "")))
2145               (insert 
2146                (if (bolp) "" ", ") (or to "")
2147                (if mct (concat (if (bolp) "" ", ") mct) "")
2148                (if cc (concat (if (bolp) "" ", ") cc) ""))
2149               ;; Remove addresses that match `rmail-dont-reply-to-names'. 
2150               (insert (prog1 (rmail-dont-reply-to (buffer-string))
2151                         (erase-buffer)))
2152               (goto-char (point-min))
2153               (setq ccalist
2154                     (mapcar
2155                      (lambda (addr)
2156                        (cons (mail-strip-quoted-names addr) addr))
2157                      (nreverse (mail-parse-comma-list))))
2158               (let ((s ccalist))
2159                 (while s
2160                   (setq ccalist (delq (assoc (car (pop s)) s) ccalist)))))
2161             (setq follow-to (list (cons 'To (cdr (pop ccalist)))))
2162             (when ccalist
2163               (push (cons 'Cc
2164                           (mapconcat (lambda (addr) (cdr addr)) ccalist ", "))
2165                     follow-to)))))
2166       (widen))
2167
2168     (message-pop-to-buffer "*mail message*")
2169
2170     (setq message-reply-headers
2171           (vector 0 subject from date message-id references 0 0 ""))
2172
2173     (message-setup
2174      `((Subject . ,subject)
2175        ,@follow-to 
2176        ,@(if (or references message-id)
2177              `((References . ,(concat (or references "") (and references " ")
2178                                       (or message-id ""))))
2179            nil))
2180      cur)))
2181
2182 ;;;###autoload
2183 (defun message-wide-reply (&optional to-address)
2184   (interactive)
2185   (message-reply to-address t))
2186
2187 ;;;###autoload
2188 (defun message-followup ()
2189   (interactive)
2190   (let ((cur (current-buffer))
2191         from subject date reply-to mct
2192         references message-id follow-to 
2193         followup-to distribution newsgroups gnus-warning)
2194     (save-restriction
2195       (narrow-to-region
2196        (goto-char (point-min))
2197        (if (search-forward "\n\n" nil t)
2198            (1- (point))
2199          (point-max)))
2200       (when (message-functionp message-followup-to-function)
2201         (setq follow-to
2202               (funcall message-followup-to-function)))
2203       (setq from (mail-fetch-field "from")
2204             date (mail-fetch-field "date") 
2205             subject (or (mail-fetch-field "subject") "none")
2206             references (mail-fetch-field "references")
2207             message-id (mail-fetch-field "message-id")
2208             followup-to (mail-fetch-field "followup-to")
2209             newsgroups (mail-fetch-field "newsgroups")
2210             reply-to (mail-fetch-field "reply-to")
2211             distribution (mail-fetch-field "distribution")
2212             mct (mail-fetch-field "mail-copies-to"))
2213       (when (and (setq gnus-warning (mail-fetch-field "gnus-warning"))
2214                  (string-match "<[^>]+>" gnus-warning))
2215         (setq message-id (match-string 0 gnus-warning)))
2216       ;; Remove bogus distribution.
2217       (and (stringp distribution)
2218            (string-match "world" distribution)
2219            (setq distribution nil))
2220       ;; Remove any (buggy) Re:'s that are present and make a
2221       ;; proper one.
2222       (when (string-match "^[ \t]*[Rr][Ee]:[ \t]*" subject)
2223         (setq subject (substring subject (match-end 0))))
2224       (setq subject (concat "Re: " subject))
2225       (widen))
2226
2227     (message-pop-to-buffer "*news message*")
2228
2229     (message-setup
2230      `((Subject . ,subject)
2231        ,@(cond 
2232           (follow-to follow-to)
2233           ((and followup-to message-use-followup-to)
2234            (list
2235             (cond 
2236              ((equal (downcase followup-to) "poster")
2237               (if (or (eq message-use-followup-to 'use)
2238                       (y-or-n-p "Use Followup-To \"poster\"? "))
2239                   (cons 'To (or reply-to from ""))
2240                 (cons 'Newsgroups newsgroups)))
2241              (t
2242               (if (or (equal followup-to newsgroups)
2243                       (not (eq message-use-followup-to 'ask))
2244                       (y-or-n-p 
2245                        (format "Use Followup-To %s? " followup-to)))
2246                   (cons 'Newsgroups followup-to)
2247                 (cons 'Newsgroups newsgroups))))))
2248           (t
2249            `((Newsgroups . ,newsgroups))))
2250        ,@(and distribution (list (cons 'Distribution distribution)))
2251        (References . ,(concat (or references "") (and references " ")
2252                               (or message-id "")))
2253        ,@(when (and mct
2254                     (not (equal (downcase mct) "never")))
2255            (list (cons 'Cc (if (equal (downcase mct) "always")
2256                                (or reply-to from "")
2257                              mct)))))
2258
2259      cur)
2260
2261     (setq message-reply-headers
2262           (vector 0 subject from date message-id references 0 0 ""))))
2263
2264
2265 ;;;###autoload
2266 (defun message-cancel-news ()
2267   "Cancel an article you posted."
2268   (interactive)
2269   (unless (message-news-p)
2270     (error "This is not a news article; canceling is impossible"))
2271   (when (yes-or-no-p "Do you really want to cancel this article? "))
2272   (let (from newsgroups message-id distribution buf)
2273     (save-excursion
2274       ;; Get header info. from original article.
2275       (save-restriction
2276         (message-narrow-to-head)
2277         (setq from (mail-fetch-field "from")
2278               newsgroups (mail-fetch-field "newsgroups")
2279               message-id (mail-fetch-field "message-id")
2280               distribution (mail-fetch-field "distribution")))
2281       ;; Make sure that this article was written by the user.
2282       (unless (string-equal
2283                (downcase (mail-strip-quoted-names from))
2284                (downcase (message-make-address)))
2285         (error "This article is not yours"))
2286       ;; Make control message.
2287       (setq buf (set-buffer (get-buffer-create " *message cancel*")))
2288       (buffer-disable-undo (current-buffer))
2289       (erase-buffer)
2290       (insert "Newsgroups: " newsgroups "\n"
2291               "From: " (message-make-from) "\n"
2292               "Subject: cmsg cancel " message-id "\n"
2293               "Control: cancel " message-id "\n"
2294               (if distribution
2295                   (concat "Distribution: " distribution "\n")
2296                 "")
2297               mail-header-separator "\n"
2298               "This is a cancel message from " from ".\n")
2299       (message "Canceling your article...")
2300       (let ((message-syntax-checks 'dont-check-for-anything-just-trust-me))
2301         (funcall message-send-news-function))
2302       (message "Canceling your article...done")
2303       (kill-buffer buf))))
2304
2305 ;;;###autoload
2306 (defun message-supersede ()
2307   "Start composing a message to supersede the current message.
2308 This is done simply by taking the old article and adding a Supersedes
2309 header line with the old Message-ID."
2310   (interactive)
2311   (let ((cur (current-buffer)))
2312     ;; Check whether the user owns the article that is to be superseded. 
2313     (unless (string-equal
2314              (downcase (mail-strip-quoted-names (mail-fetch-field "from")))
2315              (downcase (mail-strip-quoted-names (message-make-address))))
2316       (error "This article is not yours"))
2317     ;; Get a normal message buffer.
2318     (message-pop-to-buffer "*supersede message*")
2319     (insert-buffer-substring cur)
2320     (message-narrow-to-head)
2321     ;; Remove unwanted headers.
2322     (when message-ignored-supersedes-headers
2323       (message-remove-header message-ignored-supersedes-headers t))
2324     (goto-char (point-min))
2325     (if (not (re-search-forward "^Message-ID: " nil t))
2326         (error "No Message-ID in this article")
2327       (replace-match "Supersedes: " t t))
2328     (goto-char (point-max))
2329     (insert mail-header-separator)
2330     (widen)
2331     (forward-line 1)))
2332
2333 ;;;###autoload
2334 (defun message-recover ()
2335   "Reread contents of current buffer from its last auto-save file."
2336   (interactive)
2337   (let ((file-name (make-auto-save-file-name)))
2338     (cond ((save-window-excursion
2339              (if (not (eq system-type 'vax-vms))
2340                  (with-output-to-temp-buffer "*Directory*"
2341                    (buffer-disable-undo standard-output)
2342                    (let ((default-directory "/"))
2343                      (call-process
2344                       "ls" nil standard-output nil "-l" file-name))))
2345              (yes-or-no-p (format "Recover auto save file %s? " file-name)))
2346            (let ((buffer-read-only nil))
2347              (erase-buffer)
2348              (insert-file-contents file-name nil)))
2349           (t (error "message-recover cancelled")))))
2350
2351 ;;; Forwarding messages.
2352
2353 (defun message-make-forward-subject ()
2354   "Return a Subject header suitable for the message in the current buffer."
2355   (concat "[" (mail-fetch-field (if (message-news-p) "newsgroups" "from"))
2356           "] " (or (mail-fetch-field "Subject") "")))
2357
2358 ;;;###autoload
2359 (defun message-forward (&optional news)
2360   "Forward the current message via mail.  
2361 Optional NEWS will use news to forward instead of mail."
2362   (interactive "P")
2363   (let ((cur (current-buffer))
2364         (subject (message-make-forward-subject)))
2365     (if news (message-news nil subject) (message-mail nil subject))
2366     ;; Put point where we want it before inserting the forwarded
2367     ;; message. 
2368     (if message-signature-before-forwarded-message
2369         (goto-char (point-max))
2370       (message-goto-body))
2371     ;; Narrow to the area we are to insert.
2372     (narrow-to-region (point) (point))
2373     ;; Insert the separators and the forwarded buffer.
2374     (insert message-forward-start-separator)
2375     (insert-buffer-substring cur)
2376     (goto-char (point-max))
2377     (insert message-forward-end-separator)
2378     (set-text-properties (point-min) (point-max) nil)
2379     ;; Remove all unwanted headers.
2380     (goto-char (point-min))
2381     (forward-line 1)
2382     (narrow-to-region (point) (if (search-forward "\n\n" nil t)
2383                                   (1- (point))
2384                                 (point)))
2385     (goto-char (point-min))
2386     (message-remove-header message-included-forward-headers t nil t)
2387     (widen)
2388     (message-position-point)))
2389
2390 ;;;###autoload
2391 (defun message-resend (address)
2392   "Resend the current article to ADDRESS."
2393   (interactive "sResend message to: ")
2394   (save-excursion
2395     (let ((cur (current-buffer))
2396           beg)
2397       ;; We first set up a normal mail buffer.
2398       (set-buffer (get-buffer-create " *message resend*"))
2399       (buffer-disable-undo (current-buffer))
2400       (erase-buffer)
2401       (message-setup `((To . ,address)))
2402       ;; Insert our usual headers.
2403       (message-generate-headers '(From Date To))
2404       (message-narrow-to-headers)
2405       ;; Rename them all to "Resent-*".
2406       (while (re-search-forward "^[A-Za-z]" nil t)
2407         (forward-char -1)
2408         (insert "Resent-"))
2409       (widen)
2410       (forward-line)
2411       (delete-region (point) (point-max))
2412       (setq beg (point))
2413       ;; Insert the message to be resent.
2414       (insert-buffer-substring cur)
2415       (goto-char (point-min))
2416       (search-forward "\n\n")
2417       (forward-char -1)
2418       (save-restriction
2419         (narrow-to-region beg (point))
2420         (message-remove-header message-ignored-resent-headers t)
2421         (goto-char (point-max)))
2422       (insert mail-header-separator)
2423       ;; Rename all old ("Also-")Resent headers.
2424       (while (re-search-backward "^\\(Also-\\)?Resent-" beg t)
2425         (beginning-of-line)
2426         (insert "Also-"))
2427       ;; Send it.
2428       (funcall message-send-mail-function)
2429       (kill-buffer (current-buffer)))))
2430
2431 ;;;###autoload
2432 (defun message-bounce ()
2433   "Re-mail the current message.
2434 This only makes sense if the current message is a bounce message than
2435 contains some mail you have written which has been bounced back to
2436 you."
2437   (interactive)
2438   (let ((cur (current-buffer))
2439         boundary)
2440     (message-pop-to-buffer "*mail message*")
2441     (insert-buffer-substring cur)
2442     (undo-boundary)
2443     (message-narrow-to-head)
2444     (if (and (mail-fetch-field "Mime-Version")
2445              (setq boundary (mail-fetch-field "Content-Type")))
2446         (if (string-match "boundary=\"\\([^\"]+\\)\"" boundary)
2447             (setq boundary (concat (match-string 1 boundary) " *\n"
2448                                    "Content-Type: message/rfc822"))
2449           (setq boundary nil)))
2450     (widen)
2451     (goto-char (point-min))
2452     (search-forward "\n\n" nil t)
2453     (or (and boundary
2454              (re-search-forward boundary nil t)
2455              (forward-line 2))
2456         (and (re-search-forward message-unsent-separator nil t)
2457              (forward-line 1))
2458         (and (search-forward "\n\n" nil t)
2459              (re-search-forward "^Return-Path:.*\n" nil t)))
2460     ;; We remove everything before the bounced mail.
2461     (delete-region 
2462      (point-min)
2463      (if (re-search-forward "[^ \t]*:" nil t)
2464          (match-beginning 0)
2465        (point)))
2466     (save-restriction
2467       (message-narrow-to-head)
2468       (message-remove-header message-ignored-bounced-headers t)
2469       (goto-char (point-max))
2470       (insert mail-header-separator))
2471     (message-position-point)))
2472
2473 ;;;
2474 ;;; Interactive entry points for new message buffers.
2475 ;;;
2476
2477 ;;;###autoload
2478 (defun message-mail-other-window (&optional to subject)
2479   "Like `message-mail' command, but display mail buffer in another window."
2480   (interactive)
2481   (let ((pop-up-windows t)
2482         (special-display-buffer-names nil)
2483         (special-display-regexps nil)
2484         (same-window-buffer-names nil)
2485         (same-window-regexps nil))
2486     (message-pop-to-buffer "*mail message*"))
2487   (message-setup `((To . ,(or to "")) (Subject . ,(or subject "")))))
2488
2489 ;;;###autoload
2490 (defun message-mail-other-frame (&optional to subject)
2491   "Like `message-mail' command, but display mail buffer in another frame."
2492   (interactive)
2493   (let ((pop-up-frames t)
2494         (special-display-buffer-names nil)
2495         (special-display-regexps nil)
2496         (same-window-buffer-names nil)
2497         (same-window-regexps nil))
2498     (message-pop-to-buffer "*mail message*"))
2499   (message-setup `((To . ,(or to "")) (Subject . ,(or subject "")))))
2500
2501 ;;;###autoload
2502 (defun message-news-other-window (&optional newsgroups subject)
2503   "Start editing a news article to be sent."
2504   (interactive)
2505   (let ((pop-up-windows t)
2506         (special-display-buffer-names nil)
2507         (special-display-regexps nil)
2508         (same-window-buffer-names nil)
2509         (same-window-regexps nil))
2510     (message-pop-to-buffer "*news message*"))
2511   (message-setup `((Newsgroups . ,(or newsgroups "")) 
2512                    (Subject . ,(or subject "")))))
2513
2514 ;;;###autoload
2515 (defun message-news-other-frame (&optional newsgroups subject)
2516   "Start editing a news article to be sent."
2517   (interactive)
2518   (let ((pop-up-frames t)
2519         (special-display-buffer-names nil)
2520         (special-display-regexps nil)
2521         (same-window-buffer-names nil)
2522         (same-window-regexps nil))
2523     (message-pop-to-buffer "*news message*"))
2524   (message-setup `((Newsgroups . ,(or newsgroups "")) 
2525                    (Subject . ,(or subject "")))))
2526
2527 ;;; underline.el
2528
2529 ;; This code should be moved to underline.el (from which it is stolen). 
2530
2531 ;;;###autoload
2532 (defun bold-region (start end)
2533   "Bold all nonblank characters in the region.
2534 Works by overstriking characters.
2535 Called from program, takes two arguments START and END
2536 which specify the range to operate on."
2537   (interactive "r")
2538   (save-excursion
2539    (let ((end1 (make-marker)))
2540      (move-marker end1 (max start end))
2541      (goto-char (min start end))
2542      (while (< (point) end1)
2543        (or (looking-at "[_\^@- ]")
2544            (insert (following-char) "\b"))
2545        (forward-char 1)))))
2546
2547 ;;;###autoload
2548 (defun unbold-region (start end)
2549   "Remove all boldness (overstruck characters) in the region.
2550 Called from program, takes two arguments START and END
2551 which specify the range to operate on."
2552   (interactive "r")
2553   (save-excursion
2554    (let ((end1 (make-marker)))
2555      (move-marker end1 (max start end))
2556      (goto-char (min start end)) 
2557      (while (re-search-forward "\b" end1 t)
2558        (if (eq (following-char) (char-after (- (point) 2)))
2559            (delete-char -2))))))
2560
2561 ;; Support for toolbar
2562 (when (string-match "XEmacs\\|Lucid" emacs-version)
2563   (require 'message-xmas))
2564
2565 (provide 'message)
2566
2567 ;;; message.el ends here