1 ;;; emh-comp.el --- emh functions for composing messages
3 ;; Copyright (C) 1993,1994,1995,1996,1997,1998 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
7 ;; Created: 1996/2/29 (separated from tm-mh-e.el)
8 ;; Renamed: 1997/2/21 from tmh-comp.el
9 ;; Keywords: mail composing, MH, MIME, mail
11 ;; This file is part of emh.
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 ;; General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
37 (defvar emh-forwcomps "forwcomps"
38 "Name of file to be used as a skeleton for forwarding messages.
39 Default is \"forwcomps\". If not a complete path name, the file
40 is searched for first in the user's MH directory, then in the
41 system MH lib directory.")
43 (defvar emh-message-yank-function 'mh-yank-cur-msg)
49 (defun emh::make-message (folder number)
50 (vector folder number)
53 (defun emh::message/folder (message)
57 (defun emh::message/number (message)
61 (defun emh::message/file-name (message)
63 (emh::message/number message)
64 (mh-expand-file-name (emh::message/folder message))
67 ;;; modified by OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
68 ;;; 1995/11/14 (cf. [tm-ja:1096])
69 (defun emh-prompt-for-message (prompt folder &optional default)
71 (directory-files (mh-expand-file-name folder) nil "^[0-9]+$")
73 (folder-buf (get-buffer folder))
77 (set-buffer folder-buf)
78 (let* ((show-buffer (get-buffer mh-show-buffer))
79 (show-buffer-file-name
80 (buffer-file-name show-buffer)))
81 (if show-buffer-file-name
82 (file-name-nondirectory show-buffer-file-name)))))))
83 (if (or (null default)
84 (not (string-match "^[0-9]+$" default)))
86 (if (and (string= folder mh-sent-from-folder)
88 (int-to-string mh-sent-from-msg)
92 (= 0 (mh-exec-cmd-quiet nil "pick" folder "cur"))
93 (set-buffer mh-temp-buffer)
94 (setq cur-msg (buffer-string))
95 (string-match "^[0-9]+$" cur-msg))
96 (substring cur-msg 0 (match-end 0))
98 (completing-read prompt
109 ;;; modified by OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
110 ;;; 1995/11/14 (cf. [tm-ja:1096])
111 (defun emh-query-message (&optional message)
115 (setq folder (emh::message/folder message))
116 (setq number (emh::message/number message))
119 (setq folder (mh-prompt-for-folder
121 (if (and (stringp mh-sent-from-folder)
122 (string-match "^\\+" mh-sent-from-folder))
123 mh-sent-from-folder "+inbox")
127 (number-to-string number)
128 (emh-prompt-for-message "Message number: " folder)
130 (emh::make-message folder number)
133 (defun emh-insert-message (&optional message)
134 ;; always ignores message
135 (let ((article-buffer
136 (if (not (and (stringp mh-sent-from-folder)
137 (numberp mh-sent-from-msg)
139 (cond ((and (boundp 'gnus-original-article-buffer)
140 (bufferp mh-sent-from-folder)
141 (get-buffer gnus-original-article-buffer)
143 gnus-original-article-buffer)
144 ((and (boundp 'gnus-article-buffer)
145 (get-buffer gnus-article-buffer)
146 (bufferp mh-sent-from-folder)
149 (set-buffer gnus-article-buffer)
150 (if (eq major-mode 'mime-view-mode)
155 (if (null article-buffer)
157 (emh::make-message mh-sent-from-folder mh-sent-from-msg)
159 (insert-buffer article-buffer)
160 (mime-edit-inserted-message-filter)
164 (defun emh-insert-mail (&optional message)
168 (emh::message/file-name (emh-query-message message))))
169 (narrow-to-region (point) (point))
170 (insert-file-contents message-file)
171 (push-mark (point-max))
172 (mime-edit-inserted-message-filter)
175 (set-alist 'mime-edit-message-inserter-alist
176 'mh-letter-mode (function emh-insert-message))
177 (set-alist 'mime-edit-mail-inserter-alist
178 'mh-letter-mode (function emh-insert-mail))
179 (set-alist 'mime-edit-mail-inserter-alist
180 'news-reply-mode (function emh-insert-mail))
182 'mime-edit-split-message-sender-alist
185 (lambda (&optional arg)
187 (write-region (point-min) (point-max)
188 mime-edit-draft-file-name nil 'no-message)
190 (pop-to-buffer "MH mail delivery")
192 (mh-exec-cmd-output mh-send-prog t "-watch" "-nopush"
195 mime-edit-draft-file-name)
196 (goto-char (point-max)) ; show the interesting part
200 (apply 'mh-exec-cmd-quiet t mh-send-prog
202 (list "-nopush" "-nodraftfolder"
203 "-noverbose" "-nowatch"
204 mh-send-args mime-edit-draft-file-name)))))
208 ;;; @ commands using tm-edit features
211 (defun emh-edit-again (msg)
212 "Clean-up a draft or a message previously sent and make it resendable.
213 Default is the current message.
214 The variable mh-new-draft-cleaned-headers specifies the headers to remove.
215 See also documentation for `\\[mh-send]' function."
216 (interactive (list (mh-get-msg-num t)))
218 (let* ((from-folder mh-current-folder)
219 (config (current-window-configuration))
221 (cond ((and mh-draft-folder (equal from-folder mh-draft-folder))
222 (let ((name (format "draft-%d" msg)))
223 (if (get-buffer name)
224 (throw 'tag (pop-to-buffer name))
227 (mh-msg-filename msg mh-draft-folder)
229 (set-buffer (get-buffer-create name))
230 (as-binary-input-file (insert-file-contents filename))
231 (setq buffer-file-name filename)
234 (if (re-search-forward "^-+$" nil t)
239 (let ((flag enable-multibyte-characters))
241 (as-binary-input-file
242 (mh-read-draft "clean-up"
243 (mh-msg-filename msg) nil))
244 (set-buffer-multibyte flag)
247 (goto-char (point-min))
248 (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil)
249 (let ((cs (detect-coding-region (point-min)(point-max))))
250 (set-buffer-file-coding-system
255 (mime-edit-again nil 'no-separator 'not-turn-on)
256 (goto-char (point-min))
257 (mh-compose-and-send-mail draft "" from-folder nil nil nil nil nil nil
261 ;;; by OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
262 ;;; 1996/2/29 (cf. [tm-ja:1643])
263 (defun emh-extract-rejected-mail (msg)
264 "Extract a letter returned by the mail system and make it re-editable.
265 Default is the current message. The variable mh-new-draft-cleaned-headers
266 gives the headers to clean out of the original message."
267 (interactive (list (mh-get-msg-num t)))
268 (let ((from-folder mh-current-folder)
269 (config (current-window-configuration))
270 (draft (mh-read-draft "extraction" (mh-msg-filename msg) nil)))
271 (setq buffer-read-only nil)
272 (goto-char (point-min))
276 (concat "^\\($\\|[Cc]ontent-[Tt]ype:[ \t]+multipart/\\)") nil t)
278 (re-search-forward "boundary=\"\\([^\"]+\\)\"" nil t))
279 (let ((case-fold-search t)
280 (boundary (buffer-substring (match-beginning 1) (match-end 1))))
283 (concat "^--" boundary "\n"
284 "content-type:[ \t]+"
285 "\\(message/rfc822\\|text/rfc822-headers\\)\n"
286 "\\(.+\n\\)*\n") nil t)
287 (delete-region (point-min) (point))
288 (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil)
290 (concat "\n--" boundary "--\n") nil t)
291 (delete-region (match-beginning 0) (point-max)))
293 (message "Seems no message/rfc822 part.")))))
294 ((re-search-forward mh-rejected-letter-start nil t)
295 (skip-chars-forward " \t\n")
296 (delete-region (point-min) (point))
297 (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil))
299 (message "Does not appear to be a rejected letter.")))
300 (goto-char (point-min))
301 (if (re-search-forward "^-+$" nil t)
304 (mime-edit-again nil t t)
305 (goto-char (point-min))
306 (set-buffer-modified-p nil)
307 (mh-compose-and-send-mail draft "" from-folder msg
308 (mh-get-header-field "To:")
309 (mh-get-header-field "From:")
310 (mh-get-header-field "Cc:")
313 ;;; by OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
314 ;;; 1995/11/14 (cf. [tm-ja:1099])
315 (defun emh-forward (to cc &optional msg-or-seq)
316 "Forward a message or message sequence as MIME message/rfc822.
317 Defaults to displayed message. If optional prefix argument provided,
318 then prompt for the message sequence. See also documentation for
319 `\\[mh-send]' function."
320 (interactive (list (mh-read-address "To: ")
321 (mh-read-address "Cc: ")
322 (if current-prefix-arg
323 (mh-read-seq-default "Forward" t)
327 (setq msg-or-seq (mh-get-msg-num t)))
328 (let* ((folder mh-current-folder)
329 (config (current-window-configuration))
330 ;; uses "draft" for compatibility with forw.
331 ;; forw always leaves file in "draft" since it doesn't have -draft
332 (draft-name (expand-file-name "draft" mh-user-path))
333 (draft (cond ((or (not (file-exists-p draft-name))
334 (y-or-n-p "The file `draft' exists. Discard it? "))
336 "-noedit" "-nowhatnowproc"
337 "-form" emh-forwcomps
340 (mh-read-draft "" draft-name t)
341 (mh-insert-fields "To:" to "Cc:" cc)
342 (set-buffer-modified-p nil)))
344 (mh-read-draft "" draft-name nil)))))
345 (let ((msubtype "digest")
346 orig-from orig-subject multipart-flag
349 (regexp-quote (mime-make-tag "message" "rfc822"))))
351 (goto-char (point-min))
354 (goto-char (point-max))
355 (if (not (bolp)) (insert "\n"))
357 (narrow-to-region beg beg)
358 (mh-exec-cmd-output "pick" nil folder msg-or-seq)
359 (if (> (count-lines (point) (point-max)) 1)
360 (setq multipart-flag t)
362 (while (re-search-forward "^\\([0-9]+\\)\n" nil t)
364 (buffer-substring (match-beginning 1) (match-end 1)))
365 (beg (match-beginning 0))
369 (narrow-to-region beg end)
370 ;; modified for Emacs 18
371 (delete-region beg end)
372 (insert-file-contents
373 (mh-expand-file-name forw-msg
374 (mh-expand-file-name folder))
377 (push-mark (point-max))
378 (mime-edit-inserted-message-filter))
379 (goto-char (point-max))
383 (mime-edit-insert-tag "message" "rfc822")
385 (delete-region (point) (point-max))
387 (mime-edit-enclose-digest-region beg (point))
389 (re-search-forward tag-regexp)
392 (narrow-to-region (point) (point-max))
393 (setq orig-from (eword-decode-string
394 (mh-get-header-field "From:")))
395 (setq orig-subject (eword-decode-string
396 (mh-get-header-field "Subject:")))
399 (mh-forwarded-letter-subject orig-from orig-subject)))
400 (mh-insert-fields "Subject:" forw-subject)
401 (goto-char (point-min))
402 (re-search-forward tag-regexp)
404 (delete-other-windows)
405 (if (numberp msg-or-seq)
406 (mh-add-msgs-to-seq msg-or-seq 'forwarded t)
407 (mh-add-msgs-to-seq (mh-seq-to-msgs msg-or-seq) 'forwarded t))
408 (mh-compose-and-send-mail draft "" folder msg-or-seq
410 mh-note-forw "Forwarded:"
413 (cond ((not (featurep 'mh-utils))
414 (defun emh::insert-letter (folder number verbatim)
415 (mh-insert-letter verbatim folder number)
418 ((and (boundp 'mh-e-version)
419 (string-lessp mh-e-version "5"))
420 (defun emh::insert-letter (folder number verbatim)
421 (mh-insert-letter number folder verbatim)
425 (defalias 'emh::insert-letter 'mh-insert-letter)
428 (defun emh-insert-letter (verbatim)
429 "Interface to mh-insert-letter."
432 ((folder (mh-prompt-for-folder
434 (if (and (stringp mh-sent-from-folder)
435 (string-match "^\\+" mh-sent-from-folder))
436 mh-sent-from-folder "+inbox")
438 (number (emh-prompt-for-message "Message number: " folder)))
439 (emh::insert-letter folder number verbatim)))
441 (defun emh-yank-cur-msg-with-no-filter ()
442 "Insert the current message into the draft buffer.
443 This function makes new show-buffer from article-buffer to disable
444 variable `mime-preview-text/plain-hook'. If you don't want to use text
445 filters for replying message, please set it to
446 `emh-message-yank-function'.
447 Prefix each non-blank line in the message with the string in
448 `mh-ins-buf-prefix'. The entire message will be inserted if
449 `mh-yank-from-start-of-msg' is non-nil. If this variable is nil, the
450 portion of the message following the point will be yanked. If
451 `mh-delete-yanked-msg-window' is non-nil, any window displaying the
452 yanked message will be deleted."
454 (if (and mh-sent-from-folder mh-sent-from-msg)
455 (let ((to-point (point))
456 (to-buffer (current-buffer)))
457 (set-buffer mh-sent-from-folder)
458 (if mh-delete-yanked-msg-window
459 (delete-windows-on mh-show-buffer))
460 (set-buffer mh-show-buffer) ; Find displayed message
463 (let (mime-display-text/plain-hook buf)
465 (save-window-excursion
466 (set-buffer mime-raw-buffer)
467 (setq buf (mime-view-mode))
474 (set-buffer to-buffer)
476 (narrow-to-region to-point to-point)
479 (mh-insert-prefix-string mh-ins-buf-prefix)
481 (error "There is no current message")))
483 (defun emh-yank-current-message ()
484 "Insert the current message into the draft buffer.
485 It uses variable `emh-message-yank-function'
486 to select message yanking function."
488 (let ((mh-sent-from-folder mh-sent-from-folder)
489 (mh-sent-from-msg mh-sent-from-msg))
490 (if (and (not (stringp mh-sent-from-folder))
491 (boundp 'gnus-article-buffer)
492 (get-buffer gnus-article-buffer)
493 (bufferp mh-sent-from-folder)
494 ) ; might be called from GNUS
495 (if (boundp 'gnus-article-copy) ; might be sgnus
497 (gnus-copy-article-buffer)
498 (setq mh-sent-from-folder gnus-article-copy)
499 (set-buffer mh-sent-from-folder)
500 (setq mh-show-buffer gnus-article-copy)
503 (setq mh-sent-from-folder gnus-article-buffer)
504 (set-buffer gnus-article-buffer)
505 (setq mh-show-buffer (current-buffer))
507 (funcall emh-message-yank-function)
510 (substitute-key-definition
511 'mh-yank-cur-msg 'emh-yank-current-message mh-letter-mode-map)
512 (substitute-key-definition
513 'mh-insert-letter 'emh-insert-letter mh-letter-mode-map)
522 ;;; emh-comp.el ends here