1 ;;; emh-comp.el --- emh functions for composing messages
3 ;; Copyright (C) 1993,1994,1995,1996,1997 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
7 ;; Maintainer: MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;; Created: 1996/2/29 (separated from tm-mh-e.el)
9 ;; Renamed: 1997/2/21 from tmh-comp.el
10 ;; Version: $Id: emh-comp.el,v 0.4 1997-09-25 15:41:51 morioka Exp $
11 ;; Keywords: mail composing, MH, MIME, mail
13 ;; This file is part of emh.
15 ;; This program is free software; you can redistribute it and/or
16 ;; modify it under the terms of the GNU General Public License as
17 ;; published by the Free Software Foundation; either version 2, or (at
18 ;; your option) any later version.
20 ;; This program is distributed in the hope that it will be useful, but
21 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 ;; General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
39 (defvar emh-forwcomps "forwcomps"
40 "Name of file to be used as a skeleton for forwarding messages.
41 Default is \"forwcomps\". If not a complete path name, the file
42 is searched for first in the user's MH directory, then in the
43 system MH lib directory.")
45 (defvar emh-message-yank-function 'mh-yank-cur-msg)
51 (defun emh::make-message (folder number)
52 (vector folder number)
55 (defun emh::message/folder (message)
59 (defun emh::message/number (message)
63 (defun emh::message/file-name (message)
65 (emh::message/number message)
66 (mh-expand-file-name (emh::message/folder message))
69 ;;; modified by OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
70 ;;; 1995/11/14 (cf. [tm-ja:1096])
71 (defun emh-prompt-for-message (prompt folder &optional default)
73 (directory-files (mh-expand-file-name folder) nil "^[0-9]+$")
75 (folder-buf (get-buffer folder))
79 (set-buffer folder-buf)
80 (let* ((show-buffer (get-buffer mh-show-buffer))
81 (show-buffer-file-name
82 (buffer-file-name show-buffer)))
83 (if show-buffer-file-name
84 (file-name-nondirectory show-buffer-file-name)))))))
85 (if (or (null default)
86 (not (string-match "^[0-9]+$" default)))
88 (if (and (string= folder mh-sent-from-folder)
90 (int-to-string mh-sent-from-msg)
94 (= 0 (mh-exec-cmd-quiet nil "pick" folder "cur"))
95 (set-buffer mh-temp-buffer)
96 (setq cur-msg (buffer-string))
97 (string-match "^[0-9]+$" cur-msg))
98 (substring cur-msg 0 (match-end 0))
100 (completing-read prompt
111 ;;; modified by OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
112 ;;; 1995/11/14 (cf. [tm-ja:1096])
113 (defun emh-query-message (&optional message)
117 (setq folder (emh::message/folder message))
118 (setq number (emh::message/number message))
121 (setq folder (mh-prompt-for-folder
123 (if (and (stringp mh-sent-from-folder)
124 (string-match "^\\+" mh-sent-from-folder))
125 mh-sent-from-folder "+inbox")
129 (number-to-string number)
130 (emh-prompt-for-message "Message number: " folder)
132 (emh::make-message folder number)
135 (defun emh-insert-message (&optional message)
136 ;; always ignores message
137 (let ((article-buffer
138 (if (not (and (stringp mh-sent-from-folder)
139 (numberp mh-sent-from-msg)
141 (cond ((and (boundp 'gnus-original-article-buffer)
142 (bufferp mh-sent-from-folder)
143 (get-buffer gnus-original-article-buffer)
145 gnus-original-article-buffer)
146 ((and (boundp 'gnus-article-buffer)
147 (get-buffer gnus-article-buffer)
148 (bufferp mh-sent-from-folder)
151 (set-buffer gnus-article-buffer)
152 (if (eq major-mode 'mime-view-mode)
157 (if (null article-buffer)
159 (emh::make-message mh-sent-from-folder mh-sent-from-msg)
161 (insert-buffer article-buffer)
162 (mime-edit-inserted-message-filter)
166 (defun emh-insert-mail (&optional message)
170 (emh::message/file-name (emh-query-message message))))
171 (narrow-to-region (point) (point))
172 (insert-file-contents message-file)
173 (push-mark (point-max))
174 (mime-edit-inserted-message-filter)
177 (set-alist 'mime-edit-message-inserter-alist
178 'mh-letter-mode (function emh-insert-message))
179 (set-alist 'mime-edit-mail-inserter-alist
180 'mh-letter-mode (function emh-insert-mail))
181 (set-alist 'mime-edit-mail-inserter-alist
182 'news-reply-mode (function emh-insert-mail))
184 'mime-edit-split-message-sender-alist
187 (lambda (&optional arg)
189 (write-region (point-min) (point-max)
190 mime-edit-draft-file-name nil 'no-message)
192 (pop-to-buffer "MH mail delivery")
194 (mh-exec-cmd-output mh-send-prog t "-watch" "-nopush"
197 mime-edit-draft-file-name)
198 (goto-char (point-max)) ; show the interesting part
202 (apply 'mh-exec-cmd-quiet t mh-send-prog
204 (list "-nopush" "-nodraftfolder"
205 "-noverbose" "-nowatch"
206 mh-send-args mime-edit-draft-file-name)))))
210 ;;; @ commands using tm-edit features
213 (defun emh-edit-again (msg)
214 "Clean-up a draft or a message previously sent and make it resendable.
215 Default is the current message.
216 The variable mh-new-draft-cleaned-headers specifies the headers to remove.
217 See also documentation for `\\[mh-send]' function."
218 (interactive (list (mh-get-msg-num t)))
220 (let* ((from-folder mh-current-folder)
221 (config (current-window-configuration))
223 (cond ((and mh-draft-folder (equal from-folder mh-draft-folder))
224 (let ((name (format "draft-%d" msg)))
225 (if (get-buffer name)
226 (throw 'tag (pop-to-buffer name))
229 (mh-msg-filename msg mh-draft-folder)
231 (set-buffer (get-buffer-create name))
232 (as-binary-input-file (insert-file-contents filename))
233 (setq buffer-file-name filename)
236 (if (re-search-forward "^-+$" nil t)
241 (let ((flag enable-multibyte-characters))
243 (as-binary-input-file
244 (mh-read-draft "clean-up"
245 (mh-msg-filename msg) nil))
246 (setq enable-multibyte-characters flag)
249 (goto-char (point-min))
250 (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil)
251 (let ((cs (detect-coding-region (point-min)(point-max))))
252 (set-buffer-file-coding-system
257 (mime-edit-again nil 'no-separator 'not-turn-on)
258 (goto-char (point-min))
259 (mh-compose-and-send-mail draft "" from-folder nil nil nil nil nil nil
263 ;;; by OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
264 ;;; 1996/2/29 (cf. [tm-ja:1643])
265 (defun emh-extract-rejected-mail (msg)
266 "Extract a letter returned by the mail system and make it re-editable.
267 Default is the current message. The variable mh-new-draft-cleaned-headers
268 gives the headers to clean out of the original message."
269 (interactive (list (mh-get-msg-num t)))
270 (let ((from-folder mh-current-folder)
271 (config (current-window-configuration))
272 (draft (mh-read-draft "extraction" (mh-msg-filename msg) nil)))
273 (setq buffer-read-only nil)
274 (goto-char (point-min))
278 (concat "^\\($\\|[Cc]ontent-[Tt]ype:[ \t]+multipart/\\)") nil t)
280 (re-search-forward "boundary=\"\\([^\"]+\\)\"" nil t))
281 (let ((case-fold-search t)
282 (boundary (buffer-substring (match-beginning 1) (match-end 1))))
285 (concat "^--" boundary "\n"
286 "content-type:[ \t]+"
287 "\\(message/rfc822\\|text/rfc822-headers\\)\n"
288 "\\(.+\n\\)*\n") nil t)
289 (delete-region (point-min) (point))
290 (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil)
292 (concat "\n--" boundary "--\n") nil t)
293 (delete-region (match-beginning 0) (point-max)))
295 (message "Seems no message/rfc822 part.")))))
296 ((re-search-forward mh-rejected-letter-start nil t)
297 (skip-chars-forward " \t\n")
298 (delete-region (point-min) (point))
299 (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil))
301 (message "Does not appear to be a rejected letter.")))
302 (goto-char (point-min))
303 (if (re-search-forward "^-+$" nil t)
306 (mime-edit-again nil t t)
307 (goto-char (point-min))
308 (set-buffer-modified-p nil)
309 (mh-compose-and-send-mail draft "" from-folder msg
310 (mh-get-header-field "To:")
311 (mh-get-header-field "From:")
312 (mh-get-header-field "Cc:")
315 ;;; by OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
316 ;;; 1995/11/14 (cf. [tm-ja:1099])
317 (defun emh-forward (to cc &optional msg-or-seq)
318 "Forward a message or message sequence as MIME message/rfc822.
319 Defaults to displayed message. If optional prefix argument provided,
320 then prompt for the message sequence. See also documentation for
321 `\\[mh-send]' function."
322 (interactive (list (mh-read-address "To: ")
323 (mh-read-address "Cc: ")
324 (if current-prefix-arg
325 (mh-read-seq-default "Forward" t)
329 (setq msg-or-seq (mh-get-msg-num t)))
330 (let* ((folder mh-current-folder)
331 (config (current-window-configuration))
332 ;; uses "draft" for compatibility with forw.
333 ;; forw always leaves file in "draft" since it doesn't have -draft
334 (draft-name (expand-file-name "draft" mh-user-path))
335 (draft (cond ((or (not (file-exists-p draft-name))
336 (y-or-n-p "The file `draft' exists. Discard it? "))
338 "-noedit" "-nowhatnowproc"
339 "-form" emh-forwcomps
342 (mh-read-draft "" draft-name t)
343 (mh-insert-fields "To:" to "Cc:" cc)
344 (set-buffer-modified-p nil)))
346 (mh-read-draft "" draft-name nil)))))
347 (let ((msubtype "digest")
348 orig-from orig-subject multipart-flag
351 (regexp-quote (mime-make-tag "message" "rfc822"))))
353 (goto-char (point-min))
356 (goto-char (point-max))
357 (if (not (bolp)) (insert "\n"))
359 (narrow-to-region beg beg)
360 (mh-exec-cmd-output "pick" nil folder msg-or-seq)
361 (if (> (count-lines (point) (point-max)) 1)
362 (setq multipart-flag t)
364 (while (re-search-forward "^\\([0-9]+\\)\n" nil t)
366 (buffer-substring (match-beginning 1) (match-end 1)))
367 (beg (match-beginning 0))
371 (narrow-to-region beg end)
372 ;; modified for Emacs 18
373 (delete-region beg end)
374 (insert-file-contents
375 (mh-expand-file-name forw-msg
376 (mh-expand-file-name folder))
379 (push-mark (point-max))
380 (mime-edit-inserted-message-filter))
381 (goto-char (point-max))
385 (mime-edit-insert-tag "message" "rfc822")
387 (delete-region (point) (point-max))
389 (mime-edit-enclose-region "digest" beg (point))
391 (re-search-forward tag-regexp)
394 (narrow-to-region (point) (point-max))
395 (setq orig-from (eword-decode-string
396 (mh-get-header-field "From:")))
397 (setq orig-subject (eword-decode-string
398 (mh-get-header-field "Subject:")))
401 (mh-forwarded-letter-subject orig-from orig-subject)))
402 (mh-insert-fields "Subject:" forw-subject)
403 (goto-char (point-min))
404 (re-search-forward tag-regexp)
406 (delete-other-windows)
407 (if (numberp msg-or-seq)
408 (mh-add-msgs-to-seq msg-or-seq 'forwarded t)
409 (mh-add-msgs-to-seq (mh-seq-to-msgs msg-or-seq) 'forwarded t))
410 (mh-compose-and-send-mail draft "" folder msg-or-seq
412 mh-note-forw "Forwarded:"
415 (cond ((not (featurep 'mh-utils))
416 (defun emh::insert-letter (folder number verbatim)
417 (mh-insert-letter verbatim folder number)
420 ((and (boundp 'mh-e-version)
421 (string-lessp mh-e-version "5"))
422 (defun emh::insert-letter (folder number verbatim)
423 (mh-insert-letter number folder verbatim)
427 (defalias 'emh::insert-letter 'mh-insert-letter)
430 (defun emh-insert-letter (verbatim)
431 "Interface to mh-insert-letter."
434 ((folder (mh-prompt-for-folder
436 (if (and (stringp mh-sent-from-folder)
437 (string-match "^\\+" mh-sent-from-folder))
438 mh-sent-from-folder "+inbox")
440 (number (emh-prompt-for-message "Message number: " folder)))
441 (emh::insert-letter folder number verbatim)))
443 (defun emh-yank-cur-msg-with-no-filter ()
444 "Insert the current message into the draft buffer.
445 This function makes new show-buffer from article-buffer to disable
446 variable `mime-view-plain-text-preview-hook'. If you don't want to
447 use text filters for replying message, please set it to
448 `emh-message-yank-function'.
449 Prefix each non-blank line in the message with the string in
450 `mh-ins-buf-prefix'. The entire message will be inserted if
451 `mh-yank-from-start-of-msg' is non-nil. If this variable is nil, the
452 portion of the message following the point will be yanked. If
453 `mh-delete-yanked-msg-window' is non-nil, any window displaying the
454 yanked message will be deleted."
456 (if (and mh-sent-from-folder mh-sent-from-msg)
457 (let ((to-point (point))
458 (to-buffer (current-buffer)))
459 (set-buffer mh-sent-from-folder)
460 (if mh-delete-yanked-msg-window
461 (delete-windows-on mh-show-buffer))
462 (set-buffer mh-show-buffer) ; Find displayed message
465 (let (mime-view-plain-text-preview-hook buf)
467 (save-window-excursion
468 (set-buffer mime-raw-buffer)
469 (setq buf (mime-view-mode))
476 (set-buffer to-buffer)
478 (narrow-to-region to-point to-point)
481 (mh-insert-prefix-string mh-ins-buf-prefix)
483 (error "There is no current message")))
485 (defun emh-yank-current-message ()
486 "Insert the current message into the draft buffer.
487 It uses variable `emh-message-yank-function'
488 to select message yanking function."
490 (let ((mh-sent-from-folder mh-sent-from-folder)
491 (mh-sent-from-msg mh-sent-from-msg))
492 (if (and (not (stringp mh-sent-from-folder))
493 (boundp 'gnus-article-buffer)
494 (get-buffer gnus-article-buffer)
495 (bufferp mh-sent-from-folder)
496 ) ; might be called from GNUS
497 (if (boundp 'gnus-article-copy) ; might be sgnus
499 (gnus-copy-article-buffer)
500 (setq mh-sent-from-folder gnus-article-copy)
501 (set-buffer mh-sent-from-folder)
502 (setq mh-show-buffer gnus-article-copy)
505 (setq mh-sent-from-folder gnus-article-buffer)
506 (set-buffer gnus-article-buffer)
507 (setq mh-show-buffer (current-buffer))
509 (funcall emh-message-yank-function)
512 (substitute-key-definition
513 'mh-yank-cur-msg 'emh-yank-current-message mh-letter-mode-map)
514 (substitute-key-definition
515 'mh-insert-letter 'emh-insert-letter mh-letter-mode-map)
524 ;;; emh-comp.el ends here