1 ;;; message.el --- composing mail and news messages
2 ;; Copyright (C) 1996 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: mail, news
7 ;; This file is part of GNU Emacs.
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)
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.
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.
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.
39 (if (string-match "XEmacs\\|Lucid" emacs-version)
40 (require 'mail-abbrevs)
41 (require 'mailabbrev))
44 (defvar message-directory "~/Mail/"
45 "*Directory from which all other mail file variables are derived.")
47 (defvar message-max-buffers 10
48 "*How many buffers to keep before starting to kill them off.")
51 (defvar message-fcc-handler-function 'rmail-output
52 "*A function called to save outgoing articles.
53 This function will be called with the name of the file to store the
54 article in. The default function is `rmail-output' which saves in Unix
58 (defvar message-courtesy-message
59 "The following message is a courtesy copy of an article\nthat has been posted as well.\n\n"
60 "*This is inserted at the start of a mailed copy of a posted message.
61 If this variable is nil, no such courtesy message will be added.")
64 (defvar message-ignored-bounced-headers "^\\(Received\\|Return-Path\\):"
65 "*Regexp that matches headers to be removed in resent bounced mail.")
68 (defvar message-from-style 'default
69 "*Specifies how \"From\" headers look.
71 If `nil', they contain just the return address like:
73 If `parens', they look like:
74 king@grassland.com (Elvis Parsley)
75 If `angles', they look like:
76 Elvis Parsley <king@grassland.com>
78 Otherwise, most addresses look like `angles', but they look like
79 `parens' if `angles' would need quoting and `parens' would not.")
82 (defvar message-syntax-checks nil
83 "Controls what syntax checks should not be performed on outgoing posts.
84 To disable checking of long signatures, for instance, add
85 `(signature . disabled)' to this list.
87 Don't touch this variable unless you really know what you're doing.
89 Checks include subject-cmsg multiple-headers sendsys message-id from
90 long-lines control-chars size new-text redirected-followup signature
91 approved sender empty empty-headers message-id from subject.")
94 (defvar message-required-news-headers
95 '(From Newsgroups Subject Date Message-ID
96 (optional . Organization) Lines
97 (optional . X-Newsreader))
98 "*Headers to be generated or prompted for when posting an article.
99 RFC977 and RFC1036 require From, Date, Newsgroups, Subject,
100 Message-ID. Organization, Lines, In-Reply-To, Expires, and
101 X-Newsreader are optional. If don't you want message to insert some
102 header, remove it from this list.")
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.")
113 (defvar message-deletable-headers '(Message-ID Date)
114 "*Headers to be deleted if they already exist and were generated by message previously.")
117 (defvar message-ignored-news-headers
118 "^NNTP-Posting-Host:\\|^Xref:\\|^Bcc:\\|^Gcc:\\|^Fcc:"
119 "*Regexp of headers to be removed unconditionally before posting.")
122 (defvar message-ignored-mail-headers "^Gcc:\\|^Fcc:"
123 "*Regexp of headers to be removed unconditionally before mailing.")
126 (defvar message-ignored-supersedes-headers "^Path:\\|^Date\\|^NNTP-Posting-Host:\\|^Xref:\\|^Lines:\\|^Received:\\|^X-From-Line:\\|Return-Path:\\|^Supersedes:"
127 "*Header lines matching this regexp will be deleted before posting.
128 It's best to delete old Path and Date headers before posting to avoid
132 (defvar message-signature-separator "^-- *$"
133 "Regexp matching the signature separator.")
136 (defvar message-interactive nil
137 "Non-nil means when sending a message wait for and display errors.
138 nil means let mailer mail back a message to report errors.")
141 (defvar message-generate-new-buffers t
142 "*Non-nil means that a new message buffer will be created whenever `mail-setup' is called.")
145 (defvar message-kill-buffer-on-exit nil
146 "*Non-nil means that the message buffer will be killed after sending a message.")
148 (defvar gnus-local-organization)
150 (defvar message-user-organization
151 (or (and (boundp 'gnus-local-organization)
152 gnus-local-organization)
153 (getenv "ORGANIZATION")
155 "*String to be used as an Organization header.
156 If t, use `message-user-organization-file'.")
159 (defvar message-user-organization-file "/usr/lib/news/organization"
160 "*Local news organization file.")
163 (defvar message-autosave-directory
164 (concat (file-name-as-directory message-directory) "drafts/")
165 "*Directory where message autosaves buffers.
166 If nil, message won't autosave.")
168 (defvar message-forward-start-separator
169 "------- Start of forwarded message -------\n"
170 "*Delimiter inserted before forwarded messages.")
172 (defvar message-forward-end-separator
173 "------- End of forwarded message -------\n"
174 "*Delimiter inserted after forwarded messages.")
177 (defvar message-signature-before-forwarded-message t
178 "*If non-nil, put the signature before any included forwarded message.")
181 (defvar message-included-forward-headers
182 "^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:"
183 "*Regexp matching headers to be included in forwarded messages.")
186 (defvar message-ignored-resent-headers "^Return-receipt"
187 "*All headers that match this regexp will be deleted when resending a message.")
190 (defvar message-ignored-cited-headers "."
191 "Delete these headers from the messages you yank.")
193 ;; Useful to set in site-init.el
195 (defvar message-send-mail-function 'message-send-mail-with-sendmail
196 "Function to call to send the current buffer as mail.
197 The headers should be delimited by a line whose contents match the
198 variable `mail-header-separator'.
200 Legal values include `message-send-mail-with-mh' and
201 `message-send-mail-with-sendmail', which is the default.")
204 (defvar message-send-news-function 'message-send-news
205 "Function to call to send the current buffer as news.
206 The headers should be delimited by a line whose contents match the
207 variable `mail-header-separator'.")
210 (defvar message-reply-to-function nil
211 "Function that should return a list of headers.
212 This function should pick out addresses from the To, Cc, and From headers
213 and respond with new To and Cc headers.")
216 (defvar message-wide-reply-to-function nil
217 "Function that should return a list of headers.
218 This function should pick out addresses from the To, Cc, and From headers
219 and respond with new To and Cc headers.")
222 (defvar message-followup-to-function nil
223 "Function that should return a list of headers.
224 This function should pick out addresses from the To, Cc, and From headers
225 and respond with new To and Cc headers.")
228 (defvar message-use-followup-to 'ask
229 "*Specifies what to do with Followup-To header.
230 If nil, ignore the header. If it is t, use its value, but query before
231 using the \"poster\" value. If it is the symbol `ask', query the user
232 whether to ignore the \"poster\" value. If it is the symbol `use',
233 always use the value.")
235 (defvar gnus-post-method)
236 (defvar gnus-select-method)
238 (defvar message-post-method
239 (cond ((and (boundp 'gnus-post-method)
242 ((boundp 'gnus-select-method)
245 "Method used to post news.")
248 (defvar message-generate-headers-first nil
249 "*If non-nil, generate all possible headers before composing.")
251 (defvar message-setup-hook nil
252 "Normal hook, run each time a new outgoing message is initialized.
253 The function `message-setup' runs this hook.")
255 (defvar message-signature-setup-hook nil
256 "Normal hook, run each time a new outgoing message is initialized.
257 It is run after the headers have been inserted and before
258 the signature is inserted.")
260 (defvar message-mode-hook nil
261 "Hook run in message mode buffers.")
263 (defvar message-header-hook nil
264 "Hook run in a message mode buffer narrowed to the headers.")
266 (defvar message-header-setup-hook nil
267 "Hook called narrowed to the headers when setting up a message buffer.")
270 (defvar message-citation-line-function 'message-insert-citation-line
271 "*Function called to insert the \"Whomever writes:\" line.")
274 (defvar message-yank-prefix "> "
275 "*Prefix inserted on the lines of yanked messages.
276 nil means use indentation.")
278 (defvar message-indentation-spaces 3
279 "*Number of spaces to insert at the beginning of each cited line.
280 Used by `message-yank-original' via `message-yank-cite'.")
283 (defvar message-cite-function 'message-cite-original
284 "*Function for citing an original message.")
287 (defvar message-indent-citation-function 'message-indent-citation
288 "*Function for modifying a citation just inserted in the mail buffer.
289 This can also be a list of functions. Each function can find the
290 citation between (point) and (mark t). And each function should leave
291 point and mark around the citation text as modified.")
293 (defvar message-abbrevs-loaded nil)
296 (defvar message-signature t
297 "*String to be inserted at the end of the message buffer.
298 If t, the `message-signature-file' file will be inserted instead.
299 If a function, the result from the function will be used instead.
300 If a form, the result from the form will be used instead.")
303 (defvar message-signature-file "~/.signature"
304 "*File containing the text inserted at end of message. buffer.")
306 (defvar message-distribution-function nil
307 "*Function called to return a Distribution header.")
309 (defvar message-expires 14
310 "*Number of days before your article expires.")
312 (defvar message-user-path nil
313 "If nil, use the NNTP server name in the Path header.
314 If stringp, use this; if non-nil, use no host name (user name only).")
316 (defvar message-reply-buffer nil)
317 (defvar message-reply-headers nil)
318 (defvar message-newsreader nil)
319 (defvar message-mailer nil)
320 (defvar message-sent-message-via nil)
321 (defvar message-checksum nil)
322 (defvar message-send-actions nil
323 "A list of actions to be performed upon successful sending of a message.")
324 (defvar message-exit-actions nil
325 "A list of actions to be performed upon exiting after sending a message.")
326 (defvar message-kill-actions nil
327 "A list of actions to be performed before killing a message buffer.")
328 (defvar message-postpone-actions nil
329 "A list of actions to be performed after postponing a message.")
332 (defvar message-default-headers nil
333 "*A string containing header lines to be inserted in outgoing messages.
334 It is inserted before you edit the message, so you can edit or delete
338 (defvar message-default-mail-headers nil
339 "*A string of header lines to be inserted in outgoing mails.")
342 (defvar message-default-news-headers nil
343 "*A string of header lines to be inserted in outgoing news articles.")
345 ;; Note: could use /usr/ucb/mail instead of sendmail;
346 ;; options -t, and -v if not interactive.
347 (defvar message-mailer-swallows-blank-line
348 (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)"
349 system-configuration)
350 (file-readable-p "/etc/sendmail.cf")
351 (let ((buffer (get-buffer-create " *temp*")))
355 (insert-file-contents "/etc/sendmail.cf")
356 (goto-char (point-min))
357 (let ((case-fold-search nil))
358 (re-search-forward "^OR\\>" nil t)))
359 (kill-buffer buffer))))
360 ;; According to RFC822, "The field-name must be composed of printable
361 ;; ASCII characters (i.e. characters that have decimal values between
362 ;; 33 and 126, except colon)", i.e. any chars except ctl chars,
364 '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
365 "Set this non-nil if the system's mailer runs the header and body together.
366 \(This problem exists on Sunos 4 when sendmail is run in remote mode.)
367 The value should be an expression to test whether the problem will
370 (defvar message-mode-syntax-table
371 (let ((table (copy-syntax-table text-mode-syntax-table)))
372 (modify-syntax-entry ?% ". " table)
374 "Syntax table used while in Message mode.")
376 (defvar message-font-lock-keywords
377 (let* ((cite-prefix "A-Za-z") (cite-suffix (concat cite-prefix "0-9_.@-")))
378 (list '("^To:" . font-lock-function-name-face)
379 '("^[GBF]?[Cc][Cc]:\\|^Reply-To:" . font-lock-keyword-face)
380 '("^\\(Subject:\\)[ \t]*\\(.+\\)?"
381 (1 font-lock-comment-face) (2 font-lock-type-face nil t))
382 (list (concat "^\\(" (regexp-quote mail-header-separator) "\\)$")
383 1 'font-lock-comment-face)
384 (cons (concat "^[ \t]*"
385 "\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
387 'font-lock-reference-face)
388 '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\):.*"
389 . font-lock-string-face)))
390 "Additional expressions to highlight in Message mode.")
392 (defvar message-face-alist
393 '((bold . bold-region)
394 (underline . underline-region)
395 (default . (lambda (b e)
397 (ununderline-region b e))))
398 "Alist of mail and news faces for facemenu.
399 The cdr of ech entry is a function for applying the face to a region.")
401 (defvar message-send-hook nil
402 "Hook run before sending messages.")
404 (defvar message-sent-hook nil
405 "Hook run after sending messages.")
407 ;;; Internal variables.
409 (defvar message-buffer-list nil)
411 ;;; Regexp matching the delimiter of messages in UNIX mail format
412 ;;; (UNIX From lines), minus the initial ^.
413 (defvar message-unix-mail-delimiter
414 (let ((time-zone-regexp
415 (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
416 "\\|[-+]?[0-9][0-9][0-9][0-9]"
422 ;; Username, perhaps with a quoted section that can contain spaces.
425 "\\(\\|\".*\"[^ \n]*\\)"
429 ;; The time the message was sent.
430 "\\([^ \n]*\\) *" ; day of the week
431 "\\([^ ]*\\) *" ; month
432 "\\([0-9]*\\) *" ; day of month
433 "\\([0-9:]*\\) *" ; time of day
435 ;; Perhaps a time zone, specified by an abbreviation, or by a
440 " [0-9][0-9]\\([0-9]*\\) *"
442 ;; On some systems the time zone can appear after the year, too.
446 "\\(remote from .*\\)?"
450 (defvar message-unsent-separator
451 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
452 "^ *---+ +Returned message +---+ *$\\|"
453 "^Start of returned message$\\|"
454 "^ *---+ +Original message +---+ *$\\|"
455 "^ *--+ +begin message +--+ *$\\|"
456 "^ *---+ +Original message follows +---+ *$\\|"
457 "^|? *---+ +Message text follows: +---+ *|?$")
458 "A regexp that matches the separator before the text of a failed message.")
460 (defvar message-header-format-alist
462 (To . message-fill-address)
463 (Cc . message-fill-address)
474 (References . message-fill-header)
477 "Alist used for formatting headers.")
480 (autoload 'message-setup-toolbar "messagexmas")
481 (autoload 'mh-send-letter "mh-comp"))
486 ;;; Utility functions.
489 (defun message-point-at-bol ()
490 "Return point at the beginning of the line."
497 (defun message-point-at-eol ()
498 "Return point at the end of the line."
505 ;; Delete the current line (and the next N lines.);
506 (defmacro message-delete-line (&optional n)
507 `(delete-region (progn (beginning-of-line) (point))
508 (progn (forward-line ,(or n 1)) (point))))
510 (defun message-tokenize-header (header &optional separator)
511 "Split HEADER into a list of header elements.
512 \",\" is used as the separator."
514 (separator (or separator ","))
516 (format "[ \t]*\\([^%s]+\\)?\\([%s]+\\|\\'\\)" separator separator))
518 (while (and (string-match regexp header beg)
519 (< beg (length header)))
520 (when (match-beginning 1)
521 (push (match-string 1 header) elems))
522 (setq beg (match-end 0)))
525 (defun message-fetch-field (header)
526 "The same as `mail-fetch-field', only remove all newlines."
527 (let ((value (mail-fetch-field header)))
529 (nnheader-replace-chars-in-string value ?\n ? ))))
531 (defun message-fetch-reply-field (header)
532 "Fetch FIELD from the message we're replying to."
533 (when (and message-reply-buffer
534 (buffer-name message-reply-buffer))
536 (set-buffer message-reply-buffer)
537 (message-fetch-field header))))
539 (defun message-set-work-buffer ()
540 (if (get-buffer " *message work*")
542 (set-buffer " *message work*")
544 (set-buffer (get-buffer-create " *message work*"))
545 (kill-all-local-variables)
546 (buffer-disable-undo (current-buffer))))
548 (defun message-functionp (form)
549 "Return non-nil if FORM is funcallable."
550 (or (and (symbolp form) (fboundp form))
551 (and (listp form) (eq (car form) 'lambda))))
553 (defun message-strip-subject-re (subject)
554 "Remove \"Re:\" from subject lines."
555 (if (string-match "^[Rr][Ee]: *" subject)
556 (substring subject (match-end 0))
559 (defun message-remove-header (header &optional is-regexp first reverse)
560 "Remove HEADER in the narrowed buffer.
561 If REGEXP, HEADER is a regular expression.
562 If FIRST, only remove the first instance of the header.
563 Return the number of headers removed."
564 (goto-char (point-min))
565 (let ((regexp (if is-regexp header (concat "^" header ":")))
569 (while (and (not (eobp))
572 (not (looking-at regexp))
580 ;; There might be a continuation header, so we have to search
581 ;; until we find a new non-continuation line.
584 (if (re-search-forward "^[^ \t]" nil t)
585 (goto-char (match-beginning 0))
588 (if (re-search-forward "^[^ \t]" nil t)
589 (goto-char (match-beginning 0))
593 (defun message-narrow-to-headers ()
594 "Narrow the buffer to the head of the message."
597 (goto-char (point-min))
598 (if (re-search-forward
599 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
602 (goto-char (point-min)))
604 (defun message-narrow-to-head ()
605 "Narrow the buffer to the head of the message."
608 (goto-char (point-min))
609 (if (search-forward "\n\n" nil 1)
612 (goto-char (point-min)))
614 (defun message-news-p ()
615 "Say whether the current buffer contains a news message."
618 (message-narrow-to-headers)
619 (message-fetch-field "newsgroups"))))
621 (defun message-mail-p ()
622 "Say whether the current buffer contains a mail message."
625 (message-narrow-to-headers)
626 (or (message-fetch-field "to")
627 (message-fetch-field "cc")
628 (message-fetch-field "bcc")))))
630 (defun message-next-header ()
631 "Go to the beginning of the next header."
633 (or (eobp) (forward-char 1))
634 (not (if (re-search-forward "^[^ \t]" nil t)
636 (goto-char (point-max)))))
638 (defun message-sort-headers-1 ()
639 "Sort the buffer as headers using `message-rank' text props."
640 (goto-char (point-min))
642 nil 'message-next-header
644 (message-next-header)
648 (or (get-text-property (point) 'message-rank)
651 (defun message-sort-headers ()
652 "Sort the headers of the current message according to `message-header-format-alist'."
656 (let ((max (1+ (length message-header-format-alist)))
658 (message-narrow-to-headers)
659 (while (re-search-forward "^[^ \n]+:" nil t)
661 (match-beginning 0) (1+ (match-beginning 0))
663 (if (setq rank (length (memq (assq (intern (buffer-substring
666 message-header-format-alist)
667 message-header-format-alist)))
670 (message-sort-headers-1))))
680 (defvar message-mode-map nil)
682 (unless message-mode-map
683 (setq message-mode-map (copy-keymap text-mode-map))
684 (define-key message-mode-map "\C-c?" 'describe-mode)
686 (define-key message-mode-map "\C-c\C-f\C-t" 'message-goto-to)
687 (define-key message-mode-map "\C-c\C-f\C-b" 'message-goto-bcc)
688 (define-key message-mode-map "\C-c\C-f\C-w" 'message-goto-fcc)
689 (define-key message-mode-map "\C-c\C-f\C-c" 'message-goto-cc)
690 (define-key message-mode-map "\C-c\C-f\C-s" 'message-goto-subject)
691 (define-key message-mode-map "\C-c\C-f\C-r" 'message-goto-reply-to)
692 (define-key message-mode-map "\C-c\C-f\C-n" 'message-goto-newsgroups)
693 (define-key message-mode-map "\C-c\C-f\C-d" 'message-goto-distribution)
694 (define-key message-mode-map "\C-c\C-f\C-f" 'message-goto-followup-to)
695 (define-key message-mode-map "\C-c\C-f\C-k" 'message-goto-keywords)
696 (define-key message-mode-map "\C-c\C-f\C-u" 'message-goto-summary)
697 (define-key message-mode-map "\C-c\C-b" 'message-goto-body)
698 (define-key message-mode-map "\C-c\C-i" 'message-goto-signature)
700 (define-key message-mode-map "\C-c\C-t" 'message-insert-to)
701 (define-key message-mode-map "\C-c\C-n" 'message-insert-newsgroups)
703 (define-key message-mode-map "\C-c\C-y" 'message-yank-original)
704 (define-key message-mode-map "\C-c\C-q" 'message-fill-yanked-message)
705 (define-key message-mode-map "\C-c\C-w" 'message-insert-signature)
706 (define-key message-mode-map "\C-c\C-r" 'message-caesar-buffer-body)
707 (define-key message-mode-map "\C-c\C-o" 'message-sort-headers)
708 (define-key message-mode-map "\C-c\M-r" 'message-rename-buffer)
710 (define-key message-mode-map "\C-c\C-c" 'message-send-and-exit)
711 (define-key message-mode-map "\C-c\C-s" 'message-send)
712 (define-key message-mode-map "\C-c\C-k" 'message-kill-buffer)
713 (define-key message-mode-map "\C-c\C-d" 'message-dont-send)
715 (define-key message-mode-map "\t" 'message-tab))
717 (easy-menu-define message-mode-menu message-mode-map
722 ["To" message-goto-to t]
723 ["Subject" message-goto-subject t]
724 ["Cc" message-goto-cc t]
725 ["Reply-to" message-goto-reply-to t]
726 ["Summary" message-goto-summary t]
727 ["Keywords" message-goto-keywords t]
728 ["Newsgroups" message-goto-newsgroups t]
729 ["Followup-To" message-goto-followup-to t]
730 ["Distribution" message-goto-distribution t]
731 ["Body" message-goto-body t]
732 ["Signature" message-goto-signature t]
734 "Miscellaneous Commands:"
736 ["Sort Headers" message-sort-headers t]
737 ["Yank Original" message-yank-original t]
738 ["Fill Yanked Message" message-fill-yanked-message t]
739 ["Insert Signature" message-insert-signature t]
740 ["Caesar (rot13) Message" message-caesar-buffer-body t]
741 ["Rename buffer" message-rename-buffer t]
742 ["Spellcheck" ispell-message t]
744 ["Send Message" message-send-and-exit t]
745 ["Abort Message" message-dont-send t]))
747 (defvar facemenu-add-face-function)
748 (defvar facemenu-remove-face-function)
751 (defun message-mode ()
752 "Major mode for editing mail and news to be sent.
753 Like Text Mode but with these additional commands:
754 C-c C-s message-send (send the message) C-c C-c message-send-and-exit
755 C-c C-f move to a header field (and create it if there isn't):
756 C-c C-f C-t move to To C-c C-f C-s move to Subject
757 C-c C-f C-c move to Cc C-c C-f C-b move to Bcc
758 C-c C-f C-f move to Fcc C-c C-f C-r move to Reply-To
759 C-c C-f C-u move to Summary C-c C-f C-n move to Newsgroups
760 C-c C-f C-k move to Keywords C-c C-f C-d move to Distribution
761 C-c C-f C-o move to Followup-To
762 C-c C-t message-insert-to (add a To header to a news followup)
763 C-c C-n message-insert-newsgroups (add a Newsgroup header to a news reply)
764 C-c C-b message-goto-body (move to beginning of message text).
765 C-c C-i message-goto-signature (move to the beginning of the signature).
766 C-c C-w message-insert-signature (insert `message-signature-file' file).
767 C-c C-y message-yank-original (insert current message, if any).
768 C-c C-q message-fill-yanked-message (fill what was yanked).
769 C-c C-r message-ceasar-buffer-body (rot13 the message body)."
771 (kill-all-local-variables)
772 (make-local-variable 'message-reply-buffer)
773 (setq message-reply-buffer nil)
774 (make-local-variable 'message-send-actions)
775 (make-local-variable 'message-exit-actions)
776 (make-local-variable 'message-kill-actions)
777 (make-local-variable 'message-postpone-actions)
778 (set-syntax-table message-mode-syntax-table)
779 (use-local-map message-mode-map)
780 (setq local-abbrev-table text-mode-abbrev-table)
781 (setq major-mode 'message-mode)
782 (setq mode-name "Message")
783 (setq buffer-offer-save t)
784 (make-local-variable 'font-lock-defaults)
785 (setq font-lock-defaults '(message-font-lock-keywords t))
786 (make-local-variable 'facemenu-add-face-function)
787 (make-local-variable 'facemenu-remove-face-function)
788 (setq facemenu-add-face-function
790 (let ((face-fun (cdr (assq face message-face-alist))))
792 (funcall face-fun (point) end)
793 (error "Face %s not configured for %s mode" face mode-name)))
795 facemenu-remove-face-function t)
796 (make-local-variable 'paragraph-separate)
797 (make-local-variable 'paragraph-start)
798 (setq paragraph-start (concat (regexp-quote mail-header-separator)
799 "$\\|[ \t]*[-_][-_][-_]+$\\|"
801 (setq paragraph-separate (concat (regexp-quote mail-header-separator)
802 "$\\|[ \t]*[-_][-_][-_]+$\\|"
804 (make-local-variable 'message-reply-headers)
805 (setq message-reply-headers nil)
806 (make-local-variable 'message-newsreader)
807 (make-local-variable 'message-mailer)
808 (make-local-variable 'message-post-method)
809 (make-local-variable 'message-sent-message-via)
810 (setq message-sent-message-via nil)
811 (make-local-variable 'message-checksum)
812 (setq message-checksum nil)
813 (when (fboundp 'mail-hist-define-keys)
814 (mail-hist-define-keys))
815 (when (string-match "XEmacs\\|Lucid" emacs-version)
816 (message-setup-toolbar))
817 (easy-menu-add message-mode-menu message-mode-map)
818 ;; Allow mail alias things.
819 (if (fboundp 'mail-abbrevs-setup)
821 (funcall (intern "mail-aliases-setup")))
822 (run-hooks 'text-mode-hook 'message-mode-hook))
827 ;;; Message mode commands
830 ;;; Movement commands
832 (defun message-goto-to ()
833 "Move point to the To header."
835 (message-position-on-field "To"))
837 (defun message-goto-subject ()
838 "Move point to the Subject header."
840 (message-position-on-field "Subject"))
842 (defun message-goto-cc ()
843 "Move point to the Cc header."
845 (message-position-on-field "Cc" "To"))
847 (defun message-goto-bcc ()
848 "Move point to the Bcc header."
850 (message-position-on-field "Bcc" "Cc" "To"))
852 (defun message-goto-fcc ()
853 "Move point to the Fcc header."
855 (message-position-on-field "Fcc" "To" "Newsgroups"))
857 (defun message-goto-reply-to ()
858 "Move point to the Reply-To header."
860 (message-position-on-field "Reply-To" "Subject"))
862 (defun message-goto-newsgroups ()
863 "Move point to the Newsgroups header."
865 (message-position-on-field "Newsgroups"))
867 (defun message-goto-distribution ()
868 "Move point to the Distribution header."
870 (message-position-on-field "Distribution"))
872 (defun message-goto-followup-to ()
873 "Move point to the Followup-To header."
875 (message-position-on-field "Followup-To" "Newsgroups"))
877 (defun message-goto-keywords ()
878 "Move point to the Keywords header."
880 (message-position-on-field "Keywords" "Subject"))
882 (defun message-goto-summary ()
883 "Move point to the Summary header."
885 (message-position-on-field "Summary" "Subject"))
887 (defun message-goto-body ()
888 "Move point to the beginning of the message body."
890 (if (looking-at "[ \t]*\n") (expand-abbrev))
891 (goto-char (point-min))
892 (search-forward (concat "\n" mail-header-separator "\n") nil t))
894 (defun message-goto-signature ()
895 "Move point to the beginning of the message signature."
897 (goto-char (point-min))
898 (or (re-search-forward message-signature-separator nil t)
899 (goto-char (point-max))))
903 (defun message-insert-to ()
904 "Insert a To header that points to the author of the article being replied to."
906 (when (message-position-on-field "To")
908 (insert (or (message-fetch-reply-field "reply-to")
909 (message-fetch-reply-field "from") "")))
911 (defun message-insert-newsgroups ()
912 "Insert the Newsgroups header from the article being replied to."
914 (when (message-position-on-field "Newsgroups")
916 (insert (or (message-fetch-reply-field "newsgroups") "")))
922 (defun message-insert-signature (&optional force)
923 "Insert a signature. See documentation for the `message-signature' variable."
924 (interactive (list t))
926 (cond ((and (null message-signature)
929 ((message-functionp message-signature)
930 (funcall message-signature))
931 ((listp message-signature)
932 (eval message-signature))
933 (t message-signature)))
935 (cond ((stringp signature)
937 ((and (eq t signature)
938 message-signature-file
939 (file-exists-p message-signature-file))
942 ;; Remove blank lines at the end of the message.
943 (goto-char (point-max))
944 (skip-chars-backward " \t\n")
946 (delete-region (point) (point-max))
947 ;; Insert the signature.
950 (insert-file-contents message-signature-file)
952 (goto-char (point-max))
953 (or (bolp) (insert "\n")))))
955 (defvar message-caesar-translation-table nil)
957 (defun message-caesar-region (b e &optional n)
958 "Caesar rotation of region by N, default 13, for decrypting netnews."
961 (min (point) (or (mark t) (point)))
962 (max (point) (or (mark t) (point)))
963 (when current-prefix-arg
964 (prefix-numeric-value current-prefix-arg))))
966 (setq n (if (numberp n) (mod n 26) 13)) ;canonize N
967 (unless (or (zerop n) ; no action needed for a rot of 0
968 (= b e)) ; no region to rotate
969 ;; We build the table, if necessary.
970 (when (or (not message-caesar-translation-table)
971 (/= (aref message-caesar-translation-table ?a) (+ ?a n)))
973 (table (make-string 256 0)))
974 (while (< (incf i) 256)
978 (substring table 0 ?A)
979 (substring table (+ ?A n) (+ ?A n (- 26 n)))
980 (substring table ?A (+ ?A n))
981 (substring table (+ ?A 26) ?a)
982 (substring table (+ ?a n) (+ ?a n (- 26 n)))
983 (substring table ?a (+ ?a n))
984 (substring table (+ ?a 26) 255)))
985 (setq message-caesar-translation-table table)))
986 ;; Then we translate the region. Do it this way to retain
989 (subst-char-in-region
990 b (1+ b) (char-after b)
991 (aref message-caesar-translation-table (char-after b)))
994 (defun message-caesar-buffer-body (&optional rotnum)
995 "Caesar rotates all letters in the current buffer by 13 places.
996 Used to encode/decode possibly offensive messages (commonly in net.jokes).
997 With prefix arg, specifies the number of places to rotate each letter forward.
998 Mail and USENET news headers are not rotated."
999 (interactive (if current-prefix-arg
1000 (list (prefix-numeric-value current-prefix-arg))
1004 (when (message-goto-body)
1005 (narrow-to-region (point) (point-max)))
1006 (message-caesar-region (point-min) (point-max) rotnum))))
1008 (defun message-rename-buffer (&optional enter-string)
1009 "Rename the *message* buffer to \"*message* RECIPIENT\".
1010 If the function is run with a prefix, it will ask for a new buffer
1011 name, rather than giving an automatic name."
1012 (interactive "Pbuffer name: ")
1015 (goto-char (point-min))
1016 (narrow-to-region (point)
1017 (search-forward mail-header-separator nil 'end))
1018 (let* ((mail-to (if (message-news-p) (message-fetch-field "Newsgroups")
1019 (message-fetch-field "To")))
1021 (if (string-match "," mail-to)
1022 (concat (substring mail-to 0 (match-beginning 0)) ", ...")
1024 (name-default (concat "*message* " mail-trimmed-to))
1025 (name (if enter-string
1026 (read-string "New buffer name: " name-default)
1028 (rename-buffer name t)))))
1030 (defun message-fill-yanked-message (&optional justifyp)
1031 "Fill the paragraphs of a message yanked into this one.
1032 Numeric argument means justify as well."
1035 (goto-char (point-min))
1036 (search-forward (concat "\n" mail-header-separator "\n") nil t)
1037 (let ((fill-prefix message-yank-prefix))
1038 (fill-individual-paragraphs (point) (point-max) justifyp t))))
1040 (defun message-indent-citation ()
1041 "Modify text just inserted from a message to be cited.
1042 The inserted text should be the region.
1043 When this function returns, the region is again around the modified text.
1045 Normally, indent each nonblank line `message-indentation-spaces' spaces.
1046 However, if `message-yank-prefix' is non-nil, insert that prefix on each line."
1047 (let ((start (point)))
1048 ;; Remove unwanted headers.
1049 (when message-ignored-cited-headers
1053 (if (search-forward "\n\n" nil t)
1056 (message-remove-header message-ignored-cited-headers t)))
1057 ;; Do the indentation.
1058 (if (null message-yank-prefix)
1059 (indent-rigidly start (mark t) message-indentation-spaces)
1062 (while (< (point) (mark t))
1063 (insert message-yank-prefix)
1065 (goto-char start))))
1067 (defun message-yank-original (&optional arg)
1068 "Insert the message being replied to, if any.
1069 Puts point before the text and mark after.
1070 Normally indents each nonblank line ARG spaces (default 3). However,
1071 if `message-yank-prefix' is non-nil, insert that prefix on each line.
1073 Just \\[universal-argument] as argument means don't indent, insert no
1074 prefix, and don't delete any headers."
1076 (let ((modified (buffer-modified-p)))
1077 (when (and message-reply-buffer
1078 message-cite-function)
1079 (delete-windows-on message-reply-buffer t)
1080 (insert-buffer message-reply-buffer)
1081 (funcall message-cite-function)
1082 (exchange-point-and-mark)
1086 (setq message-checksum (message-checksum))))))
1088 (defun message-cite-original ()
1089 (let ((start (point))
1091 (when message-indent-citation-function
1092 (if (listp message-indent-citation-function)
1093 message-indent-citation-function
1094 (list message-indent-citation-function)))))
1097 (funcall (pop functions)))
1098 (when message-citation-line-function
1101 (funcall message-citation-line-function))))
1103 (defun message-insert-citation-line ()
1104 "Function that inserts a simple citation line."
1105 (when message-reply-headers
1106 (insert (mail-header-from message-reply-headers) " writes:\n\n")))
1108 (defun message-position-on-field (header &rest afters)
1109 (let ((case-fold-search t))
1112 (goto-char (point-min))
1115 (concat "^" (regexp-quote mail-header-separator) "$"))
1116 (match-beginning 0)))
1117 (goto-char (point-min))
1118 (if (re-search-forward (concat "^" (regexp-quote header) ":") nil t)
1120 (re-search-forward "^[^ \t]" nil 'move)
1122 (skip-chars-backward "\n")
1125 (not (re-search-forward
1126 (concat "^" (regexp-quote (car afters)) ":")
1130 (re-search-forward "^[^ \t]" nil 'move)
1131 (beginning-of-line))
1132 (insert header ": \n")
1136 (defun message-remove-signature ()
1137 "Remove the signature from the text between point and mark.
1138 The text will also be indented the normal way."
1140 (let ((start (point))
1142 (if (not (re-search-forward message-signature-separator (mark t) t))
1143 ;; No signature here, so we just indent the cited text.
1144 (message-indent-citation)
1145 ;; Find the last non-empty line.
1147 (while (looking-at "[ \t]*$")
1150 (setq mark (set-marker (make-marker) (point)))
1152 (message-indent-citation)
1153 ;; Enable undoing the deletion.
1155 (delete-region mark (mark t))
1156 (set-marker mark nil)))))
1161 ;;; Sending messages
1164 (defun message-send-and-exit (&optional arg)
1165 "Send message like `message-send', then, if no errors, exit from mail buffer."
1167 (let ((buf (current-buffer))
1168 (actions message-exit-actions))
1169 (when (and (message-send arg)
1171 (if message-kill-buffer-on-exit
1174 (when (eq buf (current-buffer))
1175 (message-bury buf)))
1176 (message-do-actions actions))))
1178 (defun message-dont-send ()
1179 "Don't send the message you have been editing."
1181 (message-bury (current-buffer))
1182 (message-do-actions message-postpone-actions))
1184 (defun message-kill-buffer ()
1185 "Kill the current buffer."
1187 (let ((actions message-kill-actions))
1188 (kill-buffer (current-buffer))
1189 (message-do-actions actions)))
1191 (defun message-bury (buffer)
1192 "Bury this mail buffer."
1193 (let ((newbuf (other-buffer buffer)))
1194 (bury-buffer buffer)
1195 (if (and (fboundp 'frame-parameters)
1196 (cdr (assq 'dedicated (frame-parameters)))
1197 (not (null (delq (selected-frame) (visible-frame-list)))))
1198 (delete-frame (selected-frame))
1199 (switch-to-buffer newbuf))))
1201 (defun message-send (&optional arg)
1202 "Send the message in the current buffer.
1203 If `message-interactive' is non-nil, wait for success indication
1204 or error messages, and inform user.
1205 Otherwise any failure is reported in a message back to
1206 the user from the mailer."
1208 (when (if buffer-file-name
1209 (y-or-n-p (format "Send buffer contents as %s message? "
1210 (if (message-mail-p)
1211 (if (message-news-p) "mail and news" "mail")
1213 (or (buffer-modified-p)
1214 (y-or-n-p "No changes in the buffer; really send? ")))
1215 ;; Make it possible to undo the coming changes.
1217 (message-fix-before-sending)
1218 (run-hooks 'message-send-hook)
1219 (message "Sending...")
1220 (when (and (or (not (message-news-p))
1221 (and (or (not (memq 'news message-sent-message-via))
1223 "Already sent message via news; resend? "))
1224 (funcall message-send-news-function arg)))
1225 (or (not (message-mail-p))
1226 (and (or (not (memq 'mail message-sent-message-via))
1228 "Already sent message via mail; resend? "))
1229 (message-send-mail arg))))
1231 (when (fboundp 'mail-hist-put-headers-into-history)
1232 (mail-hist-put-headers-into-history))
1233 (run-hooks 'message-sent-hook)
1234 (message "Sending...done")
1235 ;; If buffer has no file, mark it as unmodified and delete autosave.
1236 (unless buffer-file-name
1237 (set-buffer-modified-p nil)
1238 (delete-auto-save-file-if-necessary t))
1239 ;; Delete other mail buffers and stuff.
1240 (message-do-send-housekeeping)
1241 (message-do-actions message-send-actions)
1245 (defun message-fix-before-sending ()
1246 "Do various things to make the message nice before sending it."
1247 ;; Make sure there's a newline at the end of the message.
1248 (goto-char (point-max))
1252 (defun message-add-action (action &rest types)
1253 "Add ACTION to be performed when doing an exit of type TYPES."
1256 (set (setq var (intern (format "message-%s-actions" (pop types))))
1257 (nconc (symbol-value var) (list action))))))
1259 (defun message-do-actions (actions)
1260 "Perform all actions in ACTIONS."
1261 ;; Now perform actions on successful sending.
1265 ;; A simple function.
1266 ((message-functionp (car actions))
1267 (funcall (car actions)))
1268 ;; Something to be evaled.
1270 (eval (car actions))))
1274 (defun message-send-mail (&optional arg)
1275 (require 'mail-utils)
1276 (let ((tembuf (generate-new-buffer " message temp"))
1277 (case-fold-search nil)
1278 (news (message-news-p))
1279 (mailbuf (current-buffer)))
1281 (message-narrow-to-headers)
1282 ;; Insert some headers.
1283 (let ((message-deletable-headers
1284 (if news nil message-deletable-headers)))
1285 (message-generate-headers message-required-mail-headers))
1286 ;; Let the user do all of the above.
1287 (run-hooks 'message-header-hook))
1292 (insert-buffer-substring mailbuf)
1293 ;; Remove some headers.
1295 (message-narrow-to-headers)
1296 ;; Remove some headers.
1297 (message-remove-header message-ignored-mail-headers t))
1298 (goto-char (point-max))
1299 ;; require one newline at the end.
1300 (or (= (preceding-char) ?\n)
1303 (or (message-fetch-field "cc")
1304 (message-fetch-field "to")))
1305 (message-insert-courtesy-copy))
1306 (funcall message-send-mail-function))
1307 (kill-buffer tembuf))
1308 (set-buffer mailbuf)
1309 (push 'mail message-sent-message-via)))
1311 (defun message-send-mail-with-sendmail ()
1312 "Send off the prepared buffer with sendmail."
1313 (let ((errbuf (if message-interactive
1314 (generate-new-buffer " sendmail errors")
1316 resend-to-addresses delimline)
1317 (let ((case-fold-search t))
1319 (message-narrow-to-headers)
1320 (setq resend-to-addresses (message-fetch-field "resent-to")))
1321 ;; Change header-delimiter to be what sendmail expects.
1322 (goto-char (point-min))
1324 (concat "^" (regexp-quote mail-header-separator) "\n"))
1325 (replace-match "\n")
1327 (setq delimline (point-marker))
1328 ;; Insert an extra newline if we need it to work around
1329 ;; Sun's bug that swallows newlines.
1330 (goto-char (1+ delimline))
1331 (when (eval message-mailer-swallows-blank-line)
1333 (when message-interactive
1337 (let ((default-directory "/"))
1338 (apply 'call-process-region
1339 (append (list (point-min) (point-max)
1340 (if (boundp 'sendmail-program)
1342 "/usr/lib/sendmail")
1343 nil errbuf nil "-oi")
1344 ;; Always specify who from,
1345 ;; since some systems have broken sendmails.
1346 (list "-f" (user-login-name))
1347 ;; These mean "report errors by mail"
1348 ;; and "deliver in background".
1349 (if (null message-interactive) '("-oem" "-odb"))
1350 ;; Get the addresses from the message
1351 ;; unless this is a resend.
1352 ;; We must not do that for a resend
1353 ;; because we would find the original addresses.
1354 ;; For a resend, include the specific addresses.
1355 (if resend-to-addresses
1356 (list resend-to-addresses)
1358 (when message-interactive
1361 (goto-char (point-min))
1362 (while (re-search-forward "\n\n* *" nil t)
1363 (replace-match "; "))
1364 (if (not (zerop (buffer-size)))
1365 (error "Sending...failed to %s"
1366 (buffer-substring (point-min) (point-max)))))
1367 (when (bufferp errbuf)
1368 (kill-buffer errbuf)))))
1370 (defun message-send-mail-with-mh ()
1371 "Send the prepared message buffer with mh."
1372 (let ((mh-previous-window-config nil)
1373 (name (make-temp-name
1374 (concat (file-name-as-directory message-autosave-directory)
1376 (setq buffer-file-name name)
1382 (defun message-send-news (&optional arg)
1383 (let ((tembuf (generate-new-buffer " *message temp*"))
1384 (case-fold-search nil)
1385 (method (if (message-functionp message-post-method)
1386 (funcall message-post-method arg)
1387 message-post-method))
1388 (messbuf (current-buffer))
1391 (message-narrow-to-headers)
1392 ;; Insert some headers.
1393 (message-generate-headers message-required-news-headers)
1394 ;; Let the user do all of the above.
1395 (run-hooks 'message-header-hook))
1396 (when (message-check-news-syntax)
1400 (buffer-disable-undo (current-buffer))
1402 (insert-buffer-substring messbuf)
1403 ;; Remove some headers.
1405 (message-narrow-to-headers)
1406 ;; Remove some headers.
1407 (message-remove-header message-ignored-news-headers t))
1408 (goto-char (point-max))
1409 ;; require one newline at the end.
1410 (or (= (preceding-char) ?\n)
1412 (let ((case-fold-search t))
1413 ;; Remove the delimeter.
1414 (goto-char (point-min))
1416 (concat "^" (regexp-quote mail-header-separator) "\n"))
1417 (replace-match "\n")
1419 (require (car method))
1420 (funcall (intern (format "%s-open-server" (car method)))
1421 (cadr method) (cddr method))
1423 (funcall (intern (format "%s-request-post" (car method))))))
1424 (kill-buffer tembuf))
1425 (set-buffer messbuf)
1427 (push 'news message-sent-message-via)
1428 (message "Couldn't send message via news: %s"
1429 (nnheader-get-report (car method)))
1433 ;;; Header generation & syntax checking.
1436 (defun message-check-news-syntax ()
1437 "Check the syntax of the message."
1439 ;; We narrow to the headers and check them first.
1442 (message-narrow-to-headers)
1444 ;; Check for commands in Subject.
1446 (message-check-element 'subject-cmsg)
1448 (if (string-match "^cmsg " (message-fetch-field "subject"))
1450 "The control code \"cmsg \" is in the subject. Really post? ")
1452 ;; Check for multiple identical headers.
1453 (or (message-check-element 'multiple-headers)
1456 (while (and (not found)
1457 (re-search-forward "^[^ \t:]+: " nil t))
1459 (or (re-search-forward
1460 (concat "^" (setq found
1463 (- (match-end 0) 2))))
1468 (format "Multiple %s headers. Really post? " found))
1470 ;; Check for Version and Sendsys.
1471 (or (message-check-element 'sendsys)
1473 (if (re-search-forward "^Sendsys:\\|^Version:" nil t)
1475 (format "The article contains a %s command. Really post? "
1476 (buffer-substring (match-beginning 0)
1477 (1- (match-end 0)))))
1479 ;; See whether we can shorten Followup-To.
1480 (or (message-check-element 'shorten-followup-to)
1481 (let ((newsgroups (message-fetch-field "newsgroups"))
1482 (followup-to (message-fetch-field "followup-to"))
1484 (when (and newsgroups (string-match "," newsgroups)
1489 (setq to (completing-read
1490 "Followups to: (default all groups) "
1491 (mapcar (lambda (g) (list g))
1493 (message-tokenize-header
1495 (goto-char (point-min))
1496 (insert "Followup-To: " to "\n"))
1499 ;; Check for Approved.
1500 (or (message-check-element 'approved)
1502 (if (re-search-forward "^Approved:" nil t)
1504 "The article contains an Approved header. Really post? ")
1506 ;; Check the Message-Id header.
1507 (or (message-check-element 'message-id)
1509 (let* ((case-fold-search t)
1510 (message-id (message-fetch-field "message-id")))
1511 (or (not message-id)
1512 (and (string-match "@" message-id)
1513 (string-match "@[^\\.]*\\." message-id))
1516 "The Message-ID looks strange: \"%s\". Really post? "
1518 ;; Check the Subject header.
1520 (message-check-element 'subject)
1522 (let* ((case-fold-search t)
1523 (subject (message-fetch-field "subject")))
1526 (not (string-match "\\`[ \t]*\\'" subject)))
1529 "The subject field is empty or missing. Posting is denied.")
1531 ;; Check the Newsgroups & Followup-To headers.
1533 (message-check-element 'existing-newsgroups)
1534 (let* ((case-fold-search t)
1535 (newsgroups (message-fetch-field "newsgroups"))
1536 (followup-to (message-fetch-field "followup-to"))
1537 (groups (message-tokenize-header
1539 (concat newsgroups "," followup-to)
1541 (hashtb (and (boundp 'gnus-active-hashtb)
1542 gnus-active-hashtb))
1547 (when (and (not (boundp (intern (car groups) hashtb)))
1548 (not (equal (car groups) "poster")))
1549 (push (car groups) errors))
1555 "Really post to %s unknown group%s: %s "
1556 (if (= (length errors) 1) "this" "these")
1557 (if (= (length errors) 1) "" "s")
1558 (mapconcat 'identity errors ", ")))))))
1559 ;; Check the Newsgroups & Followup-To headers for syntax errors.
1561 (message-check-element 'valid-newsgroups)
1562 (let ((case-fold-search t)
1563 (headers '("Newsgroups" "Followup-To"))
1565 (while (and headers (not error))
1566 (when (setq header (mail-fetch-field (car headers)))
1570 "\\`\\([-+_&.a-zA-Z0-9]+\\)?\\(,[-.a-zA-Z0-9]+\\)*\\'"
1575 (not (string-match "\\.\\'\\|\\.\\." g)))
1576 (message-tokenize-header header ","))))
1583 (format "The %s header looks odd: \"%s\". Really post? "
1584 (car headers) header)))))
1585 ;; Check the From header.
1587 (message-check-element 'from)
1589 (let* ((case-fold-search t)
1590 (from (message-fetch-field "from")))
1593 (message "There is no From line. Posting is denied.")
1595 ((not (string-match "@[^\\.]*\\." from))
1597 "Denied posting -- the From looks strange: \"%s\"." from)
1599 ((string-match "@[^@]*@" from)
1601 "Denied posting -- two \"@\"'s in the From header: %s." from)
1603 ((string-match "(.*).*(.*)" from)
1605 "Denied posting -- the From header looks strange: \"%s\"."
1609 ;; Check for long lines.
1610 (or (message-check-element 'long-lines)
1612 (goto-char (point-min))
1614 (concat "^" (regexp-quote mail-header-separator) "$"))
1618 (< (current-column) 80))
1619 (zerop (forward-line 1))))
1623 "You have lines longer than 79 characters. Really post? "))))
1624 ;; Check whether the article is empty.
1625 (or (message-check-element 'empty)
1627 (goto-char (point-min))
1629 (concat "^" (regexp-quote mail-header-separator) "$"))
1632 (or (re-search-forward message-signature-separator nil t)
1633 (goto-char (point-max)))
1635 (or (re-search-backward "[^ \n\t]" b t)
1636 (y-or-n-p "Empty article. Really post? ")))))
1637 ;; Check for control characters.
1638 (or (message-check-element 'control-chars)
1640 (if (re-search-forward "[\000-\007\013\015-\037\200-\237]" nil t)
1642 "The article contains control characters. Really post? ")
1644 ;; Check excessive size.
1645 (or (message-check-element 'size)
1646 (if (> (buffer-size) 60000)
1648 (format "The article is %d octets long. Really post? "
1651 ;; Check whether any new text has been added.
1652 (or (message-check-element 'new-text)
1653 (not message-checksum)
1654 (not (eq (message-checksum) message-checksum))
1656 "It looks like no new text has been added. Really post? "))
1657 ;; Check the length of the signature.
1659 (message-check-element 'signature)
1661 (goto-char (point-max))
1662 (if (or (not (re-search-backward "^-- $" nil t))
1663 (search-forward message-forward-end-separator nil t))
1665 (if (> (count-lines (point) (point-max)) 5)
1668 "Your .sig is %d lines; it should be max 4. Really post? "
1669 (count-lines (point) (point-max))))
1672 (defun message-check-element (type)
1673 "Returns non-nil if this type is not to be checked."
1674 (if (eq message-syntax-checks 'dont-check-for-anything-just-trust-me)
1676 (let ((able (assq type message-syntax-checks)))
1678 (eq (cdr able) 'disabled)))))
1680 (defun message-checksum ()
1681 "Return a \"checksum\" for the current buffer."
1684 (goto-char (point-min))
1686 (concat "^" (regexp-quote mail-header-separator) "$"))
1688 (setq sum (logxor sum (following-char)))
1692 (defun message-do-fcc ()
1693 "Process Fcc headers in the current buffer."
1694 (let ((case-fold-search t)
1695 (buf (current-buffer))
1698 (set-buffer (get-buffer-create " *message temp*"))
1699 (buffer-disable-undo (current-buffer))
1701 (insert-buffer-substring buf)
1703 (message-narrow-to-headers)
1704 (while (setq file (message-fetch-field "fcc"))
1706 (message-remove-header "fcc" nil t)))
1707 (goto-char (point-min))
1708 (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1709 (replace-match "" t t)
1710 ;; Process FCC operations.
1712 (setq file (pop list))
1713 (if (string-match "^[ \t]*|[ \t]*\\(.*\\)[ \t]*$" file)
1714 ;; Pipe the article to the program in question.
1715 (call-process-region (point-min) (point-max) shell-file-name
1716 nil nil nil "-c" (match-string 1 file))
1717 ;; Save the article.
1718 (setq file (expand-file-name file))
1719 (unless (file-exists-p (file-name-directory file))
1720 (make-directory (file-name-directory file) t))
1721 (if (and message-fcc-handler-function
1722 (not (eq message-fcc-handler-function 'rmail-output)))
1723 (funcall message-fcc-handler-function file)
1724 (if (and (file-readable-p file) (mail-file-babyl-p file))
1725 (rmail-output file 1)
1726 (let ((mail-use-rfc822 t))
1727 (rmail-output file 1 t t))))))
1728 (kill-buffer (current-buffer)))))
1730 (defun message-cleanup-headers ()
1731 "Do various automatic cleanups of the headers."
1732 ;; Remove empty lines in the header.
1734 (message-narrow-to-headers)
1735 (while (re-search-forward "^[ \t]*\n" nil t)
1736 (replace-match "" t t)))
1738 ;; Correct Newsgroups and Followup-To headers: change sequence of
1739 ;; spaces to comma and eliminate spaces around commas. Eliminate
1740 ;; embedded line breaks.
1741 (goto-char (point-min))
1742 (while (re-search-forward "^\\(Newsgroups\\|Followup-To\\): +" nil t)
1746 (if (re-search-forward "^[^ \t]" nil t)
1750 (goto-char (point-min))
1751 (while (re-search-forward "\n[ \t]+" nil t)
1752 (replace-match " " t t)) ;No line breaks (too confusing)
1753 (goto-char (point-min))
1754 (while (re-search-forward "[ \t\n]*,[ \t\n]*\\|[ \t]+" nil t)
1755 (replace-match "," t t))
1756 (goto-char (point-min))
1757 ;; Remove trailing commas.
1758 (when (re-search-forward ",+$" nil t)
1759 (replace-match "" t t)))))
1761 (defun message-make-date ()
1762 "Make a valid data header."
1763 (let ((now (current-time)))
1764 (timezone-make-date-arpa-standard
1765 (current-time-string now) (current-time-zone now))))
1767 (defun message-make-message-id ()
1768 "Make a unique Message-ID."
1769 (concat "<" (message-unique-id)
1770 (let ((psubject (save-excursion (message-fetch-field "subject"))))
1771 (if (and message-reply-headers
1772 (mail-header-references message-reply-headers)
1773 (mail-header-subject message-reply-headers)
1775 (mail-header-subject message-reply-headers)
1777 (message-strip-subject-re
1778 (mail-header-subject message-reply-headers))
1779 (message-strip-subject-re psubject))))
1781 "@" (message-make-fqdm) ">"))
1783 (defvar message-unique-id-char nil)
1785 ;; If you ever change this function, make sure the new version
1786 ;; cannot generate IDs that the old version could.
1787 ;; You might for example insert a "." somewhere (not next to another dot
1788 ;; or string boundary), or modify the "fsf" string.
1789 (defun message-unique-id ()
1790 ;; Don't use microseconds from (current-time), they may be unsupported.
1791 ;; Instead we use this randomly inited counter.
1792 (setq message-unique-id-char
1793 (% (1+ (or message-unique-id-char (logand (random t) (1- (lsh 1 20)))))
1794 ;; (current-time) returns 16-bit ints,
1795 ;; and 2^16*25 just fits into 4 digits i base 36.
1797 (let ((tm (current-time)))
1799 (if (memq system-type '(ms-dos emx vax-vms))
1800 (let ((user (downcase (user-login-name))))
1801 (while (string-match "[^a-z0-9_]" user)
1802 (aset user (match-beginning 0) ?_))
1804 (message-number-base36 (user-uid) -1))
1805 (message-number-base36 (+ (car tm)
1806 (lsh (% message-unique-id-char 25) 16)) 4)
1807 (message-number-base36 (+ (nth 1 tm)
1808 (lsh (/ message-unique-id-char 25) 16)) 4)
1809 ;; Append the newsreader name, because while the generated
1810 ;; ID is unique to this newsreader, other newsreaders might
1811 ;; otherwise generate the same ID via another algorithm.
1814 (defun message-number-base36 (num len)
1815 (if (if (< len 0) (<= num 0) (= len 0))
1817 (concat (message-number-base36 (/ num 36) (1- len))
1818 (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
1821 (defun message-make-organization ()
1822 "Make an Organization header."
1823 (let* ((organization
1824 (or (getenv "ORGANIZATION")
1825 (when message-user-organization
1826 (if (message-functionp message-user-organization)
1827 (funcall message-user-organization)
1828 message-user-organization)))))
1830 (message-set-work-buffer)
1831 (cond ((stringp organization)
1832 (insert organization))
1833 ((and (eq t organization)
1834 message-user-organization-file
1835 (file-exists-p message-user-organization-file))
1836 (insert-file-contents message-user-organization-file)))
1837 (goto-char (point-min))
1838 (while (re-search-forward "[\t\n]+" nil t)
1839 (replace-match "" t t))
1840 (unless (zerop (buffer-size))
1843 (defun message-make-lines ()
1844 "Count the number of lines and return numeric string."
1848 (goto-char (point-min))
1850 (concat "^" (regexp-quote mail-header-separator) "$"))
1852 (int-to-string (count-lines (point) (point-max))))))
1854 (defun message-make-in-reply-to ()
1855 "Return the In-Reply-To header for this message."
1856 (when message-reply-headers
1857 (let ((from (mail-header-from message-reply-headers))
1858 (date (mail-header-date message-reply-headers)))
1861 (string-match " *at \\| *@ \\| *(\\| *<" from)))
1862 (concat (if stop-pos (substring from 0 stop-pos) from)
1864 (if (or (not date) (string= date ""))
1865 "(unknown date)" date)))))))
1867 (defun message-make-distribution ()
1868 "Make a Distribution header."
1869 (let ((orig-distribution (message-fetch-reply-field "distribution")))
1870 (cond ((message-functionp message-distribution-function)
1871 (funcall message-distribution-function))
1872 (t orig-distribution))))
1874 (defun message-make-expires ()
1875 "Return an Expires header based on `message-expires'."
1876 (let ((current (current-time))
1877 (future (* 1.0 message-expires 60 60 24)))
1878 ;; Add the future to current.
1879 (setcar current (+ (car current) (round (/ future (expt 2 16)))))
1880 (setcar (cdr current) (+ (nth 1 current) (% (round future) (expt 2 16))))
1881 ;; Return the date in the future in UT.
1882 (timezone-make-date-arpa-standard
1883 (current-time-string current) (current-time-zone current) '(0 "UT"))))
1885 (defun message-make-path ()
1887 (let ((login-name (user-login-name)))
1888 (cond ((null message-user-path)
1889 (concat (system-name) "!" login-name))
1890 ((stringp message-user-path)
1891 ;; Support GENERICPATH. Suggested by vixie@decwrl.dec.com.
1892 (concat message-user-path "!" login-name))
1895 (defun message-make-from ()
1896 "Make a From header."
1897 (let* ((login (message-make-address))
1899 (or (and (boundp 'user-full-name)
1902 (when (string= fullname "&")
1903 (setq fullname (user-login-name)))
1905 (message-set-work-buffer)
1907 ((or (null message-from-style)
1908 (equal fullname ""))
1910 ((or (eq message-from-style 'angles)
1911 (and (not (eq message-from-style 'parens))
1912 ;; Use angles if no quoting is needed, or if parens would
1913 ;; need quoting too.
1914 (or (not (string-match "[^- !#-'*+/-9=?A-Z^-~]" fullname))
1915 (let ((tmp (concat fullname nil)))
1916 (while (string-match "([^()]*)" tmp)
1917 (aset tmp (match-beginning 0) ?-)
1918 (aset tmp (1- (match-end 0)) ?-))
1919 (string-match "[\\()]" tmp)))))
1921 (goto-char (point-min))
1922 ;; Look for a character that cannot appear unquoted
1923 ;; according to RFC 822.
1924 (when (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]" nil 1)
1925 ;; Quote fullname, escaping specials.
1926 (goto-char (point-min))
1928 (while (re-search-forward "[\"\\]" nil 1)
1929 (replace-match "\\\\\\&" t))
1931 (insert " <" login ">"))
1932 (t ; 'parens or default
1934 (let ((fullname-start (point)))
1936 (goto-char fullname-start)
1937 ;; RFC 822 says \ and nonmatching parentheses
1938 ;; must be escaped in comments.
1939 ;; Escape every instance of ()\ ...
1940 (while (re-search-forward "[()\\]" nil 1)
1941 (replace-match "\\\\\\&" t))
1942 ;; ... then undo escaping of matching parentheses,
1943 ;; including matching nested parentheses.
1944 (goto-char fullname-start)
1945 (while (re-search-forward
1946 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
1948 (replace-match "\\1(\\3)" t)
1949 (goto-char fullname-start)))
1953 (defun message-make-sender ()
1954 "Return the \"real\" user address.
1955 This function tries to ignore all user modifications, and
1956 give as trustworthy answer as possible."
1957 (concat (user-login-name) "@" (system-name)))
1959 (defun message-make-address ()
1960 "Make the address of the user."
1961 (or (message-user-mail-address)
1962 (concat (user-login-name) "@" (message-make-domain))))
1964 (defun message-user-mail-address ()
1965 "Return the pertinent part of `user-mail-address'."
1966 (when user-mail-address
1967 (nth 1 (mail-extract-address-components user-mail-address))))
1969 (defun message-make-fqdm ()
1970 "Return user's fully qualified domain name."
1971 (let ((system-name (system-name)))
1973 ((string-match "[^.]\\.[^.]" system-name)
1974 ;; `system-name' returned the right result.
1976 ;; We try `user-mail-address' as a backup.
1977 ((string-match "@\\(.*\\)\\'" (message-user-mail-address))
1978 (match-string 1 user-mail-address))
1979 ;; Try `mail-host-address'.
1980 ((and (boundp 'mail-host-address)
1983 ;; Default to this bogus thing.
1985 (concat system-name ".i-have-a-misconfigured-system-so-shoot-me")))))
1987 (defun message-make-host-name ()
1988 "Return the name of the host."
1989 (let ((fqdm (message-make-fqdm)))
1990 (string-match "^[^.]+\\." fqdm)
1991 (substring fqdm 0 (1- (match-end 0)))))
1993 (defun message-make-domain ()
1994 "Return the domain name."
1995 (or mail-host-address
1996 (message-make-fqdm)))
1998 (defun message-generate-headers (headers)
1999 "Prepare article HEADERS.
2000 Headers already prepared in the buffer are not modified."
2002 (message-narrow-to-headers)
2003 (let* ((Date (message-make-date))
2004 (Message-ID (message-make-message-id))
2005 (Organization (message-make-organization))
2006 (From (message-make-from))
2007 (Path (message-make-path))
2010 (In-Reply-To (message-make-in-reply-to))
2012 (Distribution (message-make-distribution))
2013 (Lines (message-make-lines))
2014 (X-Newsreader message-newsreader)
2015 (X-Mailer (and (not (message-fetch-field "X-Newsreader"))
2017 (Expires (message-make-expires))
2018 (case-fold-search t)
2020 ;; First we remove any old generated headers.
2021 (let ((headers message-deletable-headers))
2023 (goto-char (point-min))
2024 (and (re-search-forward
2025 (concat "^" (symbol-name (car headers)) ": *") nil t)
2026 (get-text-property (1+ (match-beginning 0)) 'message-deletable)
2027 (message-delete-line))
2029 ;; Go through all the required headers and see if they are in the
2030 ;; articles already. If they are not, or are empty, they are
2031 ;; inserted automatically - except for Subject, Newsgroups and
2034 (goto-char (point-min))
2035 (setq elem (pop headers))
2037 (if (eq (car elem) 'optional)
2038 (setq header (cdr elem))
2039 (setq header (car elem)))
2041 (when (or (not (re-search-forward
2042 (concat "^" (downcase (symbol-name header)) ":")
2045 ;; The header was found. We insert a space after the
2046 ;; colon, if there is none.
2047 (if (/= (following-char) ? ) (insert " ") (forward-char 1))
2048 ;; Find out whether the header is empty...
2049 (looking-at "[ \t]*$")))
2050 ;; So we find out what value we should insert.
2053 ((and (consp elem) (eq (car elem) 'optional))
2054 ;; This is an optional header. If the cdr of this
2055 ;; is something that is nil, then we do not insert
2057 (setq header (cdr elem))
2058 (or (and (fboundp (cdr elem)) (funcall (cdr elem)))
2059 (and (boundp (cdr elem)) (symbol-value (cdr elem)))))
2061 ;; The element is a cons. Either the cdr is a
2062 ;; string to be inserted verbatim, or it is a
2063 ;; function, and we insert the value returned from
2065 (or (and (stringp (cdr elem)) (cdr elem))
2066 (and (fboundp (cdr elem)) (funcall (cdr elem)))))
2067 ((and (boundp header) (symbol-value header))
2068 ;; The element is a symbol. We insert the value
2069 ;; of this symbol, if any.
2070 (symbol-value header))
2072 ;; We couldn't generate a value for this header,
2073 ;; so we just ask the user.
2074 (read-from-minibuffer
2075 (format "Empty header for %s; enter value: " header)))))
2076 ;; Finally insert the header.
2078 (not (equal value "")))
2082 ;; This header didn't exist, so we insert it.
2083 (goto-char (point-max))
2084 (insert (symbol-name header) ": " value "\n")
2086 ;; The value of this header was empty, so we clear
2087 ;; totally and insert the new value.
2088 (delete-region (point) (message-point-at-eol))
2090 ;; Add the deletable property to the headers that require it.
2091 (and (memq header message-deletable-headers)
2092 (progn (beginning-of-line) (looking-at "[^:]+: "))
2093 (add-text-properties
2094 (point) (match-end 0)
2095 '(message-deletable t face italic) (current-buffer)))))))
2096 ;; Insert new Sender if the From is strange.
2097 (let ((from (message-fetch-field "from"))
2098 (sender (message-fetch-field "sender"))
2099 (secure-sender (message-make-sender)))
2101 (not (message-check-element 'sender))
2104 (cadr (mail-extract-address-components from)))
2105 (downcase secure-sender)))
2110 (cadr (mail-extract-address-components sender)))
2111 (downcase secure-sender)))))
2112 (goto-char (point-min))
2113 ;; Rename any old Sender headers to Original-Sender.
2114 (when (re-search-forward "^Sender:" nil t)
2116 (insert "Original-")
2117 (beginning-of-line))
2118 (insert "Sender: " secure-sender "\n"))))))
2120 (defun message-insert-courtesy-copy ()
2121 "Insert a courtesy message in mail copies of combined messages."
2124 (message-narrow-to-headers)
2125 (let ((newsgroups (message-fetch-field "newsgroups")))
2127 (goto-char (point-max))
2128 (insert "Posted-To: " newsgroups "\n"))))
2130 (insert message-courtesy-message)))
2133 ;;; Setting up a message buffer
2136 (defun message-fill-address (header value)
2138 (narrow-to-region (point) (point))
2139 (insert (capitalize (symbol-name header))
2141 (if (consp value) (car value) value)
2143 (narrow-to-region (point-min) (1- (point-max)))
2145 (goto-char (point-min))
2147 (skip-chars-forward "^,\"" (point-max))
2148 (if (or (= (following-char) ?,)
2151 (if (and (> (current-column) 78)
2157 (setq last (1+ (point))))
2158 (setq last (1+ (point)))))
2159 (setq quoted (not quoted)))
2162 (goto-char (point-max))
2166 (defun message-fill-header (header value)
2167 (let ((begin (point))
2170 (insert (capitalize (symbol-name header))
2172 (if (consp value) (car value) value)
2175 (narrow-to-region begin (point))
2176 (fill-region-as-paragraph begin (point))
2177 ;; Tapdance around looong Message-IDs.
2179 (when (looking-at "[ \t]*$")
2180 (message-delete-line))
2182 (re-search-forward ":" nil t)
2183 (when (looking-at "\n[ \t]+")
2184 (replace-match " " t t))
2185 (goto-char (point-max)))))
2187 (defun message-position-point ()
2188 "Move point to where the user probably wants to find it."
2189 (message-narrow-to-headers)
2191 ((re-search-forward "^[^:]+:[ \t]*$" nil t)
2192 (search-backward ":" )
2195 (if (= (following-char) ? )
2199 (goto-char (point-max))
2202 (unless (looking-at "$")
2206 (defun message-buffer-name (type &optional to group)
2207 "Return a new (unique) buffer name based on TYPE and TO."
2208 (if message-generate-new-buffers
2209 (generate-new-buffer-name
2213 (or (car (mail-extract-address-components to))
2216 (if group (concat " on " group) "")
2218 (format "*%s message*" type)))
2220 (defun message-pop-to-buffer (name)
2221 "Pop to buffer NAME, and warn if it already exists and is modified."
2222 (let ((buffer (get-buffer name)))
2224 (buffer-name buffer))
2226 (set-buffer (pop-to-buffer buffer))
2227 (when (and (buffer-modified-p)
2229 "Message already being composed; erase? ")))
2230 (error "Message being composed")))
2231 (set-buffer (pop-to-buffer name))))
2235 (defun message-do-send-housekeeping ()
2236 "Kill old message buffers."
2237 ;; We might have sent this buffer already. Delete it from the
2239 (setq message-buffer-list (delq (current-buffer) message-buffer-list))
2240 (when (and message-max-buffers
2241 (>= (length message-buffer-list) message-max-buffers))
2242 ;; Kill the oldest buffer -- unless it has been changed.
2243 (let ((buffer (pop message-buffer-list)))
2244 (when (and (buffer-name buffer)
2245 (not (buffer-modified-p buffer)))
2246 (kill-buffer buffer))))
2247 ;; Rename the buffer.
2248 (when (string-match "\\`\\*" (buffer-name))
2250 (concat "*sent " (substring (buffer-name) (match-end 0))) t))
2251 ;; Push the current buffer onto the list.
2252 (when message-max-buffers
2253 (setq message-buffer-list
2254 (nconc message-buffer-list (list (current-buffer))))))
2256 (defun message-setup (headers &optional replybuffer actions)
2257 (when (and (boundp 'mc-modes-alist)
2258 (not (assq 'message-mode mc-modes-alist)))
2259 (push '(message-mode (encrypt . mc-encrypt-message)
2260 (sign . mc-sign-message))
2263 (setq message-send-actions actions))
2264 (setq message-reply-buffer replybuffer)
2265 (goto-char (point-min))
2266 ;; Insert all the headers.
2269 (alist message-header-format-alist))
2271 (unless (assq (caar h) message-header-format-alist)
2272 (push (list (caar h)) alist))
2277 (when message-default-headers
2278 (insert message-default-headers))
2282 (insert mail-header-separator "\n")
2286 (when (message-news-p)
2287 (when message-default-news-headers
2288 (insert message-default-news-headers))
2289 (when message-generate-headers-first
2290 (message-generate-headers
2293 (copy-sequence message-required-news-headers))))))
2294 (when (message-mail-p)
2295 (when message-default-mail-headers
2296 (insert message-default-mail-headers))
2297 (when message-generate-headers-first
2298 (message-generate-headers
2301 (copy-sequence message-required-mail-headers))))))
2302 (run-hooks 'message-signature-setup-hook)
2303 (message-insert-signature)
2304 (message-set-auto-save-file-name)
2306 (message-narrow-to-headers)
2307 (run-hooks 'message-header-setup-hook))
2308 (set-buffer-modified-p nil)
2309 (run-hooks 'message-setup-hook)
2310 (message-position-point)
2313 (defun message-set-auto-save-file-name ()
2314 "Associate the message buffer with a file in the drafts directory."
2315 (when message-autosave-directory
2316 (unless (file-exists-p message-autosave-directory)
2317 (make-directory message-autosave-directory t))
2318 (let ((name (make-temp-name
2319 (concat (file-name-as-directory message-autosave-directory)
2321 (setq buffer-auto-save-file-name
2325 (set-buffer (get-buffer-create " *draft tmp*"))
2326 (setq buffer-file-name name)
2327 (make-auto-save-file-name))
2328 (kill-buffer (current-buffer)))))
2329 (clear-visited-file-modtime))))
2334 ;;; Commands for interfacing with message
2338 (defun message-mail (&optional to subject)
2339 "Start editing a mail message to be sent."
2341 (message-pop-to-buffer (message-buffer-name "mail" to))
2342 (message-setup `((To . ,(or to "")) (Subject . ,(or subject "")))))
2345 (defun message-news (&optional newsgroups subject)
2346 "Start editing a news article to be sent."
2348 (message-pop-to-buffer (message-buffer-name "news" nil newsgroups))
2349 (message-setup `((Newsgroups . ,(or newsgroups ""))
2350 (Subject . ,(or subject "")))))
2353 (defun message-reply (&optional to-address wide ignore-reply-to)
2354 "Start editing a reply to the article in the current buffer."
2356 (let ((cur (current-buffer))
2357 from subject date reply-to to cc
2358 references message-id follow-to
2359 mct never-mct gnus-warning)
2362 (goto-char (point-min))
2363 (if (search-forward "\n\n" nil t)
2366 ;; Allow customizations to have their say.
2368 ;; This is a regular reply.
2369 (if (message-functionp message-reply-to-function)
2370 (setq follow-to (funcall message-reply-to-function)))
2371 ;; This is a followup.
2372 (if (message-functionp message-wide-reply-to-function)
2375 (funcall message-wide-reply-to-function)))))
2376 ;; Find all relevant headers we need.
2377 (setq from (message-fetch-field "from")
2378 date (message-fetch-field "date")
2379 subject (or (message-fetch-field "subject") "none")
2380 to (message-fetch-field "to")
2381 cc (message-fetch-field "cc")
2382 mct (message-fetch-field "mail-copies-to")
2383 reply-to (unless ignore-reply-to (message-fetch-field "reply-to"))
2384 references (message-fetch-field "references")
2385 message-id (message-fetch-field "message-id"))
2386 ;; Remove any (buggy) Re:'s that are present and make a
2388 (when (string-match "^[ \t]*[Rr][Ee]:[ \t]*" subject)
2389 (setq subject (substring subject (match-end 0))))
2390 (setq subject (concat "Re: " subject))
2392 (when (and (setq gnus-warning (message-fetch-field "gnus-warning"))
2393 (string-match "<[^>]+>" gnus-warning))
2394 (setq message-id (match-string 0 gnus-warning)))
2396 ;; Handle special values of Mail-Copies-To.
2398 (cond ((equal (downcase mct) "never")
2401 ((equal (downcase mct) "always")
2402 (setq mct (or reply-to from)))))
2407 (setq follow-to (list (cons 'To (or to-address reply-to from))))
2410 (message-set-work-buffer)
2412 (insert (or reply-to from "")))
2414 (if (bolp) "" ", ") (or to "")
2415 (if mct (concat (if (bolp) "" ", ") mct) "")
2416 (if cc (concat (if (bolp) "" ", ") cc) ""))
2417 ;; Remove addresses that match `rmail-dont-reply-to-names'.
2418 (insert (prog1 (rmail-dont-reply-to (buffer-string))
2420 (goto-char (point-min))
2424 (cons (mail-strip-quoted-names addr) addr))
2425 (nreverse (mail-parse-comma-list))))
2428 (setq ccalist (delq (assoc (car (pop s)) s) ccalist)))))
2429 (setq follow-to (list (cons 'To (cdr (pop ccalist)))))
2432 (mapconcat (lambda (addr) (cdr addr)) ccalist ", "))
2436 (message-pop-to-buffer (message-buffer-name "reply" from))
2438 (setq message-reply-headers
2439 (vector 0 subject from date message-id references 0 0 ""))
2442 `((Subject . ,subject)
2444 ,@(if (or references message-id)
2445 `((References . ,(concat (or references "") (and references " ")
2446 (or message-id ""))))
2451 (defun message-wide-reply (&optional to-address)
2453 (message-reply to-address t))
2456 (defun message-followup ()
2458 (let ((cur (current-buffer))
2459 from subject date reply-to mct
2460 references message-id follow-to
2461 followup-to distribution newsgroups gnus-warning)
2464 (goto-char (point-min))
2465 (if (search-forward "\n\n" nil t)
2468 (when (message-functionp message-followup-to-function)
2470 (funcall message-followup-to-function)))
2471 (setq from (message-fetch-field "from")
2472 date (message-fetch-field "date")
2473 subject (or (message-fetch-field "subject") "none")
2474 references (message-fetch-field "references")
2475 message-id (message-fetch-field "message-id")
2476 followup-to (message-fetch-field "followup-to")
2477 newsgroups (message-fetch-field "newsgroups")
2478 reply-to (message-fetch-field "reply-to")
2479 distribution (message-fetch-field "distribution")
2480 mct (message-fetch-field "mail-copies-to"))
2481 (when (and (setq gnus-warning (message-fetch-field "gnus-warning"))
2482 (string-match "<[^>]+>" gnus-warning))
2483 (setq message-id (match-string 0 gnus-warning)))
2484 ;; Remove bogus distribution.
2485 (and (stringp distribution)
2486 (string-match "world" distribution)
2487 (setq distribution nil))
2488 ;; Remove any (buggy) Re:'s that are present and make a
2490 (when (string-match "^[ \t]*[Rr][Ee]:[ \t]*" subject)
2491 (setq subject (substring subject (match-end 0))))
2492 (setq subject (concat "Re: " subject))
2495 (message-pop-to-buffer (message-buffer-name "followup" from newsgroups))
2498 `((Subject . ,subject)
2500 (follow-to follow-to)
2501 ((and followup-to message-use-followup-to)
2504 ((equal (downcase followup-to) "poster")
2505 (if (or (eq message-use-followup-to 'use)
2506 (message-y-or-n-p "Obey Followup-To: poster? " t "\
2507 You should normally obey the Followup-To: header.
2509 `Followup-To: poster' sends your response via e-mail instead of news.
2511 A typical situation where `Followup-To: poster' is used is when the poster
2512 does not read the newsgroup, so he wouldn't see any replies sent to it."))
2513 (cons 'To (or reply-to from ""))
2514 (cons 'Newsgroups newsgroups)))
2516 (if (or (equal followup-to newsgroups)
2517 (not (eq message-use-followup-to 'ask))
2519 (concat "Obey Followup-To: " followup-to "? ") t "\
2520 You should normally obey the Followup-To: header.
2522 `Followup-To: " followup-to "'
2523 directs your response to " (if (string-match "," followup-to)
2524 "the specified newsgroups"
2525 "that newsgroup only") ".
2527 If a message is posted to several newsgroups, Followup-To is often
2528 used to direct the following discussion to one newsgroup only,
2529 because discussions that are spread over several newsgroup tend to
2530 be fragmented and very difficult to follow.
2532 Also, some source/announcment newsgroups are not indented for discussion;
2533 responses here are directed to other newsgroups."))
2534 (cons 'Newsgroups followup-to)
2535 (cons 'Newsgroups newsgroups))))))
2537 `((Newsgroups . ,newsgroups))))
2538 ,@(and distribution (list (cons 'Distribution distribution)))
2539 (References . ,(concat (or references "") (and references " ")
2540 (or message-id "")))
2542 (not (equal (downcase mct) "never")))
2543 (list (cons 'Cc (if (equal (downcase mct) "always")
2544 (or reply-to from "")
2549 (setq message-reply-headers
2550 (vector 0 subject from date message-id references 0 0 ""))))
2554 (defun message-cancel-news ()
2555 "Cancel an article you posted."
2557 (unless (message-news-p)
2558 (error "This is not a news article; canceling is impossible"))
2559 (when (yes-or-no-p "Do you really want to cancel this article? ")
2560 (let (from newsgroups message-id distribution buf)
2562 ;; Get header info. from original article.
2564 (message-narrow-to-head)
2565 (setq from (message-fetch-field "from")
2566 newsgroups (message-fetch-field "newsgroups")
2567 message-id (message-fetch-field "message-id")
2568 distribution (message-fetch-field "distribution")))
2569 ;; Make sure that this article was written by the user.
2570 (unless (string-equal
2571 (downcase (cadr (mail-extract-address-components from)))
2572 (downcase (message-make-address)))
2573 (error "This article is not yours"))
2574 ;; Make control message.
2575 (setq buf (set-buffer (get-buffer-create " *message cancel*")))
2576 (buffer-disable-undo (current-buffer))
2578 (insert "Newsgroups: " newsgroups "\n"
2579 "From: " (message-make-from) "\n"
2580 "Subject: cmsg cancel " message-id "\n"
2581 "Control: cancel " message-id "\n"
2583 (concat "Distribution: " distribution "\n")
2585 mail-header-separator "\n"
2586 "This is a cancel message from " from ".\n")
2587 (message "Canceling your article...")
2588 (let ((message-syntax-checks 'dont-check-for-anything-just-trust-me))
2589 (funcall message-send-news-function))
2590 (message "Canceling your article...done")
2591 (kill-buffer buf)))))
2594 (defun message-supersede ()
2595 "Start composing a message to supersede the current message.
2596 This is done simply by taking the old article and adding a Supersedes
2597 header line with the old Message-ID."
2599 (let ((cur (current-buffer)))
2600 ;; Check whether the user owns the article that is to be superseded.
2601 (unless (string-equal
2602 (downcase (cadr (mail-extract-address-components
2603 (message-fetch-field "from"))))
2604 (downcase (message-make-address)))
2605 (error "This article is not yours"))
2606 ;; Get a normal message buffer.
2607 (message-pop-to-buffer (message-buffer-name "supersede"))
2608 (insert-buffer-substring cur)
2609 (message-narrow-to-head)
2610 ;; Remove unwanted headers.
2611 (when message-ignored-supersedes-headers
2612 (message-remove-header message-ignored-supersedes-headers t))
2613 (goto-char (point-min))
2614 (if (not (re-search-forward "^Message-ID: " nil t))
2615 (error "No Message-ID in this article")
2616 (replace-match "Supersedes: " t t))
2617 (goto-char (point-max))
2618 (insert mail-header-separator)
2623 (defun message-recover ()
2624 "Reread contents of current buffer from its last auto-save file."
2626 (let ((file-name (make-auto-save-file-name)))
2627 (cond ((save-window-excursion
2628 (if (not (eq system-type 'vax-vms))
2629 (with-output-to-temp-buffer "*Directory*"
2630 (buffer-disable-undo standard-output)
2631 (let ((default-directory "/"))
2633 "ls" nil standard-output nil "-l" file-name))))
2634 (yes-or-no-p (format "Recover auto save file %s? " file-name)))
2635 (let ((buffer-read-only nil))
2637 (insert-file-contents file-name nil)))
2638 (t (error "message-recover cancelled")))))
2640 ;;; Forwarding messages.
2642 (defun message-make-forward-subject ()
2643 "Return a Subject header suitable for the message in the current buffer."
2644 (concat "[" (or (message-fetch-field (if (message-news-p) "newsgroups" "from"))
2646 "] " (or (message-fetch-field "Subject") "")))
2649 (defun message-forward (&optional news)
2650 "Forward the current message via mail.
2651 Optional NEWS will use news to forward instead of mail."
2653 (let ((cur (current-buffer))
2654 (subject (message-make-forward-subject)))
2655 (if news (message-news nil subject) (message-mail nil subject))
2656 ;; Put point where we want it before inserting the forwarded
2658 (if message-signature-before-forwarded-message
2659 (goto-char (point-max))
2660 (message-goto-body))
2661 ;; Make sure we're at the start of the line.
2664 ;; Narrow to the area we are to insert.
2665 (narrow-to-region (point) (point))
2666 ;; Insert the separators and the forwarded buffer.
2667 (insert message-forward-start-separator)
2668 (insert-buffer-substring cur)
2669 (goto-char (point-max))
2670 (insert message-forward-end-separator)
2671 (set-text-properties (point-min) (point-max) nil)
2672 ;; Remove all unwanted headers.
2673 (goto-char (point-min))
2675 (narrow-to-region (point) (if (search-forward "\n\n" nil t)
2678 (goto-char (point-min))
2679 (message-remove-header message-included-forward-headers t nil t)
2681 (message-position-point)))
2684 (defun message-resend (address)
2685 "Resend the current article to ADDRESS."
2686 (interactive "sResend message to: ")
2688 (let ((cur (current-buffer))
2690 ;; We first set up a normal mail buffer.
2691 (set-buffer (get-buffer-create " *message resend*"))
2692 (buffer-disable-undo (current-buffer))
2694 (message-setup `((To . ,address)))
2695 ;; Insert our usual headers.
2696 (message-generate-headers '(From Date To))
2697 (message-narrow-to-headers)
2698 ;; Rename them all to "Resent-*".
2699 (while (re-search-forward "^[A-Za-z]" nil t)
2704 (delete-region (point) (point-max))
2706 ;; Insert the message to be resent.
2707 (insert-buffer-substring cur)
2708 (goto-char (point-min))
2709 (search-forward "\n\n")
2712 (narrow-to-region beg (point))
2713 (message-remove-header message-ignored-resent-headers t)
2714 (goto-char (point-max)))
2715 (insert mail-header-separator)
2716 ;; Rename all old ("Also-")Resent headers.
2717 (while (re-search-backward "^\\(Also-\\)?Resent-" beg t)
2722 (kill-buffer (current-buffer)))))
2725 (defun message-bounce ()
2726 "Re-mail the current message.
2727 This only makes sense if the current message is a bounce message than
2728 contains some mail you have written which has been bounced back to
2731 (let ((cur (current-buffer))
2733 (message-pop-to-buffer (message-buffer-name "bounce"))
2734 (insert-buffer-substring cur)
2736 (message-narrow-to-head)
2737 (if (and (message-fetch-field "Mime-Version")
2738 (setq boundary (message-fetch-field "Content-Type")))
2739 (if (string-match "boundary=\"\\([^\"]+\\)\"" boundary)
2740 (setq boundary (concat (match-string 1 boundary) " *\n"
2741 "Content-Type: message/rfc822"))
2742 (setq boundary nil)))
2744 (goto-char (point-min))
2745 (search-forward "\n\n" nil t)
2747 (re-search-forward boundary nil t)
2749 (and (re-search-forward message-unsent-separator nil t)
2751 (and (search-forward "\n\n" nil t)
2752 (re-search-forward "^Return-Path:.*\n" nil t)))
2753 ;; We remove everything before the bounced mail.
2756 (if (re-search-forward "[^ \t]*:" nil t)
2760 (message-narrow-to-head)
2761 (message-remove-header message-ignored-bounced-headers t)
2762 (goto-char (point-max))
2763 (insert mail-header-separator))
2764 (message-position-point)))
2767 ;;; Interactive entry points for new message buffers.
2771 (defun message-mail-other-window (&optional to subject)
2772 "Like `message-mail' command, but display mail buffer in another window."
2774 (let ((pop-up-windows t)
2775 (special-display-buffer-names nil)
2776 (special-display-regexps nil)
2777 (same-window-buffer-names nil)
2778 (same-window-regexps nil))
2779 (message-pop-to-buffer (message-buffer-name "mail" to)))
2780 (message-setup `((To . ,(or to "")) (Subject . ,(or subject "")))))
2783 (defun message-mail-other-frame (&optional to subject)
2784 "Like `message-mail' command, but display mail buffer in another frame."
2786 (let ((pop-up-frames t)
2787 (special-display-buffer-names nil)
2788 (special-display-regexps nil)
2789 (same-window-buffer-names nil)
2790 (same-window-regexps nil))
2791 (message-pop-to-buffer (message-buffer-name "mail" to)))
2792 (message-setup `((To . ,(or to "")) (Subject . ,(or subject "")))))
2795 (defun message-news-other-window (&optional newsgroups subject)
2796 "Start editing a news article to be sent."
2798 (let ((pop-up-windows t)
2799 (special-display-buffer-names nil)
2800 (special-display-regexps nil)
2801 (same-window-buffer-names nil)
2802 (same-window-regexps nil))
2803 (message-pop-to-buffer (message-buffer-name "news" nil newsgroups)))
2804 (message-setup `((Newsgroups . ,(or newsgroups ""))
2805 (Subject . ,(or subject "")))))
2808 (defun message-news-other-frame (&optional newsgroups subject)
2809 "Start editing a news article to be sent."
2811 (let ((pop-up-frames t)
2812 (special-display-buffer-names nil)
2813 (special-display-regexps nil)
2814 (same-window-buffer-names nil)
2815 (same-window-regexps nil))
2816 (message-pop-to-buffer (message-buffer-name "news" nil newsgroups)))
2817 (message-setup `((Newsgroups . ,(or newsgroups ""))
2818 (Subject . ,(or subject "")))))
2822 ;; This code should be moved to underline.el (from which it is stolen).
2825 (defun bold-region (start end)
2826 "Bold all nonblank characters in the region.
2827 Works by overstriking characters.
2828 Called from program, takes two arguments START and END
2829 which specify the range to operate on."
2832 (let ((end1 (make-marker)))
2833 (move-marker end1 (max start end))
2834 (goto-char (min start end))
2835 (while (< (point) end1)
2836 (or (looking-at "[_\^@- ]")
2837 (insert (following-char) "\b"))
2838 (forward-char 1)))))
2841 (defun unbold-region (start end)
2842 "Remove all boldness (overstruck characters) in the region.
2843 Called from program, takes two arguments START and END
2844 which specify the range to operate on."
2847 (let ((end1 (make-marker)))
2848 (move-marker end1 (max start end))
2849 (goto-char (min start end))
2850 (while (re-search-forward "\b" end1 t)
2851 (if (eq (following-char) (char-after (- (point) 2)))
2852 (delete-char -2))))))
2854 ;; Support for toolbar
2855 (when (string-match "XEmacs\\|Lucid" emacs-version)
2856 (require 'messagexmas))
2858 ;;; Group name completion.
2860 (defvar message-newgroups-header-regexp
2861 "^\\(Newsgroups\\|Followup-To\\|Posted-To\\):"
2862 "Regexp that match headers that lists groups.")
2864 (defun message-tab ()
2865 "Expand group names in Newsgroups and Followup-To headers.
2866 Do a `tab-to-tab-stop' if not in those headers."
2868 (if (let ((mail-abbrev-mode-regexp message-newgroups-header-regexp))
2869 (mail-abbrev-in-expansion-header-p))
2870 (message-expand-group)
2873 (defvar gnus-active-hashtb)
2874 (defun message-expand-group ()
2875 (let* ((b (save-excursion (skip-chars-backward "^, :\t\n") (point)))
2876 (completion-ignore-case t)
2877 (string (buffer-substring b (point)))
2878 (hashtb (and (boundp 'gnus-active-hashtb) gnus-active-hashtb))
2879 (completions (all-completions string hashtb))
2880 (cur (current-buffer))
2882 (delete-region b (point))
2884 ((= (length completions) 1)
2885 (if (string= (car completions) string)
2888 (message "Only matching group"))
2889 (insert (car completions))))
2890 ((and (setq comp (try-completion string hashtb))
2891 (not (string= comp string)))
2896 (message "No matching groups")
2897 (pop-to-buffer "*Completions*")
2898 (buffer-disable-undo (current-buffer))
2899 (let ((buffer-read-only nil))
2901 (let ((standard-output (current-buffer)))
2902 (display-completion-list (sort completions 'string<)))
2903 (goto-char (point-min))
2904 (pop-to-buffer cur)))))))
2908 (defmacro message-y-or-n-p (question show &rest text)
2909 "Ask QUESTION, displaying the rest of the arguments in a temporary buffer."
2910 `(message-talkative-question 'y-or-n-p ,question ,show ,@text))
2912 (defun message-talkative-question (ask question show &rest text)
2913 "Call FUNCTION with argument QUESTION, displaying the rest of the arguments in a temporary buffer if SHOW.
2914 The following arguments may contain lists of values."
2916 (setq text (message-flatten-list text)))
2917 (save-window-excursion
2919 (with-output-to-temp-buffer " *MESSAGE information message*"
2920 (set-buffer " *MESSAGE information message*")
2921 (mapcar 'princ text)
2922 (goto-char (point-min))))
2923 (funcall ask question))
2924 (funcall ask question)))
2926 (defun message-flatten-list (&rest list)
2927 (message-flatten-list-1 list))
2929 (defun message-flatten-list-1 (list)
2931 (apply 'append (mapcar 'message-flatten-list-1 list)))
2935 (run-hooks 'message-load-hook)
2939 ;;; message.el ends here