tm 7.46.1.
[elisp/tm.git] / mh-e / tmh-comp.el
1 ;;;
2 ;;; tm-mh-e.el --- tm-mh-e functions for composing messages
3 ;;;
4 ;;; Copyright (C) 1995 Free Software Foundation, Inc.
5 ;;; Copyright (C) 1993 .. 1996 MORIOKA Tomohiko
6 ;;; Copyright (C) 1995,1996 OKABE Yasuo
7 ;;;
8 ;;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
9 ;;;         OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
10 ;;; Maintainer: MORIOKA Tomohiko <morioka@jaist.ac.jp>
11 ;;; Created: 1996/2/29 (separated from tm-mh-e.el)
12 ;;; Version:
13 ;;;     $Id: tmh-comp.el,v 2.0 1996/02/29 15:11:19 morioka Exp $
14 ;;; Keywords: mail, MH, MIME, multimedia, encoded-word, multilingual
15 ;;;
16 ;;; This file is part of tm (Tools for MIME).
17 ;;;
18 ;;; This program is free software; you can redistribute it and/or
19 ;;; modify it under the terms of the GNU General Public License as
20 ;;; published by the Free Software Foundation; either version 2, or
21 ;;; (at your option) any later version.
22 ;;;
23 ;;; This program is distributed in the hope that it will be useful,
24 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26 ;;; General Public License for more details.
27 ;;;
28 ;;; You should have received a copy of the GNU General Public License
29 ;;; along with This program.  If not, write to the Free Software
30 ;;; Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
31 ;;;
32 ;;; Code:
33
34 (require 'mh-comp)
35 (require 'tm-edit)
36
37
38 ;;; @ variable
39 ;;;
40
41 (defvar tm-mh-e/forwcomps "forwcomps"
42   "Name of file to be used as a skeleton for forwarding messages.
43 Default is \"forwcomps\".  If not a complete path name, the file
44 is searched for first in the user's MH directory, then in the
45 system MH lib directory.")
46
47
48 ;;; @ for tm-edit
49 ;;;
50
51 (defun tm-mh-e::make-message (folder number)
52   (vector folder number)
53   )
54
55 (defun tm-mh-e::message/folder (message)
56   (elt message 0)
57   )
58
59 (defun tm-mh-e::message/number (message)
60   (elt message 1)
61   )
62
63 (defun tm-mh-e::message/file-name (message)
64   (expand-file-name
65    (tm-mh-e::message/number message)
66    (mh-expand-file-name (tm-mh-e::message/folder message))
67    ))
68
69 ;;; modified by OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
70 ;;;     1995/11/14 (cf. [tm-ja:1096])
71 (defun tm-mh-e/prompt-for-message (prompt folder &optional default)
72   (let* ((files
73           (directory-files (mh-expand-file-name folder) nil "^[0-9]+$")
74           )
75          (folder-buf (get-buffer folder))
76          (default
77            (if folder-buf
78                (save-excursion
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)))
87         (setq default
88               (if (and (string= folder mh-sent-from-folder)
89                        mh-sent-from-msg)
90                   (int-to-string mh-sent-from-msg)
91                 (save-excursion
92                   (let (cur-msg)
93                     (if (and
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))
99                       (car files)))))))
100     (completing-read prompt
101                      (let ((i 0))
102                        (mapcar (function
103                                 (lambda (file)
104                                   (setq i (+ i 1))
105                                   (list file i)
106                                   ))
107                                files)
108                        ) nil nil default)
109     ))
110
111 ;;; modified by OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
112 ;;;     1995/11/14 (cf. [tm-ja:1096])
113 (defun tm-mh-e/query-message (&optional message)
114   (let (folder number)
115     (if message
116         (progn
117           (setq folder (tm-mh-e::message/folder message))
118           (setq number (tm-mh-e::message/number message))
119           ))
120     (or (stringp folder)
121         (setq folder (mh-prompt-for-folder
122                       "Message from"
123                       (if (and (stringp mh-sent-from-folder)
124                                (string-match "^\\+" mh-sent-from-folder))
125                           mh-sent-from-folder "+inbox")
126                       nil)))
127     (setq number
128           (if (numberp number)
129               (number-to-string number)
130             (tm-mh-e/prompt-for-message "Message number: " folder)
131             ))
132     (tm-mh-e::make-message folder number)
133     ))
134
135 (defun tm-mh-e/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)
140                        ))
141              (cond ((and (boundp 'gnus-original-article-buffer)
142                          (bufferp mh-sent-from-folder)
143                          (get-buffer gnus-original-article-buffer)
144                          )
145                     gnus-original-article-buffer)
146                    ((and (boundp 'gnus-article-buffer)
147                          (get-buffer gnus-article-buffer)
148                          (bufferp mh-sent-from-folder)
149                          )
150                     (save-excursion
151                       (set-buffer gnus-article-buffer)
152                       (if (eq major-mode 'mime/viewer-mode)
153                           mime::preview/article-buffer
154                         (current-buffer)
155                         )))
156                    ))))
157     (if (null article-buffer)
158         (tm-mh-e/insert-mail
159          (tm-mh-e::make-message mh-sent-from-folder mh-sent-from-msg)
160          )
161       (insert-buffer article-buffer)
162       (mime-editor/inserted-message-filter)
163       )
164     ))
165
166 (defun tm-mh-e/insert-mail (&optional message)
167   (save-excursion
168     (save-restriction
169       (let ((message-file
170              (tm-mh-e::message/file-name (tm-mh-e/query-message message))))
171         (narrow-to-region (point) (point))
172         (insert-file-contents message-file)
173         (push-mark (point-max))
174         (mime-editor/inserted-message-filter)
175     ))))
176
177 (set-alist 'mime-editor/message-inserter-alist
178            'mh-letter-mode (function tm-mh-e/insert-message))
179 (set-alist 'mime-editor/mail-inserter-alist
180            'mh-letter-mode (function tm-mh-e/insert-mail))
181 (set-alist 'mime-editor/mail-inserter-alist
182            'news-reply-mode (function tm-mh-e/insert-mail))
183
184
185 ;;; @ commands using tm-edit features
186 ;;;
187
188 (defun tm-mh-e/edit-again (msg)
189   "Clean-up a draft or a message previously sent and make it resendable.
190 Default is the current message.
191 The variable mh-new-draft-cleaned-headers specifies the headers to remove.
192 See also documentation for `\\[mh-send]' function."
193   (interactive (list (mh-get-msg-num t)))
194   (catch 'tag
195     (let* ((from-folder mh-current-folder)
196            (config (current-window-configuration))
197            code-conversion
198            (draft
199             (cond ((and mh-draft-folder (equal from-folder mh-draft-folder))
200                    (let ((name (format "draft-%d" msg)))
201                      (if (get-buffer name)
202                          (throw 'tag (pop-to-buffer name))
203                        )
204                      (let ((file-coding-system-for-read *noconv*)
205                            (filename
206                             (mh-msg-filename msg mh-draft-folder)
207                             ))
208                        (set-buffer (get-buffer-create name))
209                        (insert-file-contents filename)
210                        (setq buffer-file-name filename)
211                        (setq code-conversion t)
212                        )
213                      (pop-to-buffer name)
214                      (if (re-search-forward "\\(^$\\|^-+$\\)" nil t)
215                          (replace-match "")
216                          )
217                      name))
218                   (t
219                    (prog1
220                        (let ((file-coding-system-for-read *noconv*))
221                          (mh-read-draft "clean-up" (mh-msg-filename msg) nil)
222                          )
223                      (setq code-conversion t)
224                      ))))
225            )
226       (goto-char (point-min))
227       (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil)
228       (if code-conversion
229           (let ((cs (code-detect-region (point-min)(point-max))))
230             (set-file-coding-system
231              (if (listp cs)
232                  (car cs)
233                cs))
234             ))
235       (save-buffer)
236       (mime/edit-again code-conversion t t)
237       (goto-char (point-min))
238       (mh-compose-and-send-mail draft "" from-folder nil nil nil nil nil nil
239                                 config)
240       )))
241
242 ;;; by OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
243 ;;;     1996/2/29 (cf. [tm-ja:1643])
244 (defun tm-mh-e/extract-rejected-mail (msg)
245   "Extract a letter returned by the mail system and make it re-editable.
246 Default is the current message.  The variable mh-new-draft-cleaned-headers
247 gives the headers to clean out of the original message."
248   (interactive (list (mh-get-msg-num t)))
249   (let ((from-folder mh-current-folder)
250         (config (current-window-configuration))
251         (draft (mh-read-draft "extraction" (mh-msg-filename msg) nil)))
252     (setq buffer-read-only nil)
253     (goto-char (point-min))
254     (cond 
255      ((and
256        (re-search-forward
257         (concat "^\\($\\|[Cc]ontent-[Tt]ype:[ \t]+multipart/\\)") nil t)
258        (not (bolp))
259        (re-search-forward "boundary=\"\\([^\"]+\\)\"" nil t))
260       (let ((case-fold-search t)
261             (boundary (buffer-substring (match-beginning 1) (match-end 1))))
262         (cond
263          ((re-search-forward
264            (concat "^--" boundary "\n"
265                    "content-type:[ \t]+"
266                    "\\(message/rfc822\\|text/rfc822-headers\\)\n"
267                    "\\(.+\n\\)*\n") nil t)
268           (delete-region (point-min) (point))
269           (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil)
270           (search-forward
271            (concat "\n--" boundary "--\n") nil t)
272           (delete-region (match-beginning 0) (point-max)))
273          (t
274           (message "Seems no message/rfc822 part.")))))
275      ((re-search-forward mh-rejected-letter-start nil t)
276       (skip-chars-forward " \t\n")
277       (delete-region (point-min) (point))
278       (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil))
279      (t
280       (message "Does not appear to be a rejected letter.")))
281     (goto-char (point-min))
282     (if (re-search-forward "^-+$" nil t)
283         (replace-match "")
284       )
285     (mime/edit-again nil t t)
286     (goto-char (point-min))
287     (set-buffer-modified-p nil)
288     (mh-compose-and-send-mail draft "" from-folder msg
289                               (mh-get-header-field "To:")
290                               (mh-get-header-field "From:")
291                               (mh-get-header-field "Cc:")
292                               nil nil config)))
293
294 ;;; by OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
295 ;;;     1995/11/14 (cf. [tm-ja:1099])
296 (defun tm-mh-e/forward (to cc &optional msg-or-seq)
297   "Forward a message or message sequence as MIME message/rfc822.
298 Defaults to displayed message. If optional prefix argument provided,
299 then prompt for the message sequence. See also documentation for
300 `\\[mh-send]' function."
301   (interactive (progn
302                  (require 'mh-comp)
303                  (list (mh-read-address "To: ")
304                        (mh-read-address "Cc: ")
305                        (if current-prefix-arg
306                            (mh-read-seq-default "Forward" t)
307                          (mh-get-msg-num t)
308                          ))))
309   (or msg-or-seq
310       (setq msg-or-seq (mh-get-msg-num t)))
311   (let* ((folder mh-current-folder)
312          (config (current-window-configuration))
313          ;; uses "draft" for compatibility with forw.
314          ;; forw always leaves file in "draft" since it doesn't have -draft
315          (draft-name (expand-file-name "draft" mh-user-path))
316          (draft (cond ((or (not (file-exists-p draft-name))
317                            (y-or-n-p "The file `draft' exists.  Discard it? "))
318                        (mh-exec-cmd "comp"
319                                     "-noedit" "-nowhatnowproc"
320                                     "-form" tm-mh-e/forwcomps
321                                     "-nodraftfolder")
322                        (prog1
323                            (mh-read-draft "" draft-name t)
324                          (mh-insert-fields "To:" to "Cc:" cc)
325                          (set-buffer-modified-p nil)))
326                       (t
327                        (mh-read-draft "" draft-name nil)))))
328     (require 'tm-edit)
329     (let ((msubtype "digest")
330           orig-from orig-subject multipart-flag
331           (tag-regexp
332            (concat "^"
333                    (regexp-quote (mime-make-tag "message" "rfc822"))))
334           )
335       (goto-char (point-min))
336       (save-excursion
337         (save-restriction
338           (goto-char (point-max))
339           (if (not (bolp)) (insert "\n"))
340           (let ((beg (point)))
341             (narrow-to-region beg beg)
342             (mh-exec-cmd-output "pick" nil folder msg-or-seq)
343             (if (> (count-lines (point) (point-max)) 1)
344                 (setq multipart-flag t)
345               )
346             (while (re-search-forward "^\\([0-9]+\\)\n" nil t)
347               (let ((forw-msg
348                      (buffer-substring (match-beginning 1) (match-end 1)))
349                     (beg (match-beginning 0))
350                     (end (match-end 0))
351                     )
352                 (save-restriction
353                   (narrow-to-region beg end)
354                   ;; modified for Emacs 18
355                   (delete-region beg end)
356                   (insert-file-contents
357                    (mh-expand-file-name forw-msg
358                                         (mh-expand-file-name folder))
359                    )
360                   (save-excursion
361                     (push-mark (point-max))
362                     (mime-editor/inserted-message-filter))
363                   (goto-char (point-max))
364                   )
365                 (save-excursion
366                   (goto-char beg)
367                   (mime-editor/insert-tag "message" "rfc822")
368                   )))
369             (delete-region (point) (point-max))
370             (if multipart-flag
371                 (mime-editor/enclose-region "digest" beg (point))
372               ))))
373       (re-search-forward tag-regexp)
374       (forward-line 1)
375       (save-restriction
376         (narrow-to-region (point) (point-max))
377         (setq orig-from (mh-get-header-field "From:"))
378         (setq orig-subject (mh-get-header-field "Subject:")))
379       (let ((forw-subject
380              (mh-forwarded-letter-subject orig-from orig-subject)))
381         (mh-insert-fields "Subject:" forw-subject)
382         (goto-char (point-min))
383         (re-search-forward tag-regexp)
384         (forward-line -1)
385         (delete-other-windows)
386         (if (numberp msg-or-seq)
387             (mh-add-msgs-to-seq msg-or-seq 'forwarded t)
388           (mh-add-msgs-to-seq (mh-seq-to-msgs msg-or-seq) 'forwarded t))
389         (mh-compose-and-send-mail draft "" folder msg-or-seq
390                                   to forw-subject cc
391                                   mh-note-forw "Forwarded:"
392                                   config)))))
393
394 (defun tm-mh-e/insert-letter (verbatim)
395   "Interface to mh-insert-letter."
396   (interactive "P")
397   (let*
398       ((folder (mh-prompt-for-folder
399                 "Message from"
400                 (if (and (stringp mh-sent-from-folder)
401                          (string-match "^\\+" mh-sent-from-folder))
402                     mh-sent-from-folder "+inbox")
403                 nil))
404        (number (tm-mh-e/prompt-for-message "Message number: " folder)))
405     (mh-insert-letter folder number verbatim)))
406
407 (defun tm-mh-e/yank-cur-msg ()
408   "Interface to mh-yank-cur-msg."
409   (interactive)
410   (let ((mh-sent-from-folder mh-sent-from-folder)
411         (mh-sent-from-msg mh-sent-from-msg))
412     (if (not (stringp mh-sent-from-folder))
413         (cond ((and (boundp 'gnus-article-buffer)
414                     (get-buffer gnus-article-buffer)
415                     (bufferp mh-sent-from-folder)
416                     ) ; might be called from GNUS
417                (if (boundp 'gnus-article-copy) ; might be sgnus
418                    (save-excursion
419                      (gnus-copy-article-buffer)
420                      (setq mh-sent-from-folder gnus-article-copy)
421                      (set-buffer mh-sent-from-folder)
422                      (setq mh-show-buffer gnus-article-copy))
423                  (save-excursion
424                    (setq mh-sent-from-folder gnus-article-buffer)
425                    (set-buffer gnus-article-buffer)
426                    (setq mh-show-buffer (current-buffer)))))
427               (t
428                (error "There is no current message"))))
429     (mh-yank-cur-msg)))
430
431 (substitute-key-definition
432  'mh-yank-cur-msg 'tm-mh-e/yank-cur-msg mh-letter-mode-map)
433 (substitute-key-definition
434  'mh-insert-letter 'tm-mh-e/insert-letter mh-letter-mode-map)
435
436
437 ;;; @ end
438 ;;;
439
440 (provide 'tmh-comp)
441 (require 'tm-mh-e)
442
443 ;;; tmh-comp.el ends here