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