4eea5ef4865965735affaae4261903ca9d1166aa
[elisp/tm.git] / mh-e / tmh-comp.el
1 ;;; tm-mh-e.el --- tm-mh-e functions for composing messages
2
3 ;; Copyright (C) 1993,1994,1995,1996 Free Software Foundation, Inc.
4
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 ;; Version:
10 ;;      $Id: tmh-comp.el,v 7.6 1996/09/03 15:27:18 morioka Exp $
11 ;; Keywords: mail, MH, MIME, multimedia, encoded-word, multilingual
12
13 ;; This file is part of tm (Tools for MIME).
14
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.
19
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.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with This program; see the file COPYING.  If not, write to
27 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
29
30 ;;; Code:
31
32 (require 'mh-comp)
33 (require 'tm-edit)
34
35
36 ;;; @ variable
37 ;;;
38
39 (defvar tm-mh-e/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.")
44
45 (defvar tm-mh-e/message-yank-function 'mh-yank-cur-msg)
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 (set-alist
184  'mime-editor/split-message-sender-alist
185  'mh-letter-mode
186  (function
187   (lambda (&optional arg)
188     (interactive "P")
189     (write-region (point-min) (point-max)
190                   mime-editor/draft-file-name nil 'no-message)
191     (cond (arg
192            (pop-to-buffer "MH mail delivery")
193            (erase-buffer)
194            (mh-exec-cmd-output mh-send-prog t "-watch" "-nopush"
195                                "-nodraftfolder"
196                                mh-send-args
197                                mime-editor/draft-file-name)
198            (goto-char (point-max))      ; show the interesting part
199            (recenter -1)
200            (sit-for 1))
201           (t
202            (apply 'mh-exec-cmd-quiet t mh-send-prog 
203                   (mh-list-to-string
204                    (list "-nopush" "-nodraftfolder"
205                          "-noverbose" "-nowatch"
206                          mh-send-args mime-editor/draft-file-name)))))
207     )))
208
209
210 ;;; @ commands using tm-edit features
211 ;;;
212
213 (defun tm-mh-e/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)))
219   (catch 'tag
220     (let* ((from-folder mh-current-folder)
221            (config (current-window-configuration))
222            code-conversion
223            (draft
224             (cond ((and mh-draft-folder (equal from-folder mh-draft-folder))
225                    (let ((name (format "draft-%d" msg)))
226                      (if (get-buffer name)
227                          (throw 'tag (pop-to-buffer name))
228                        )
229                      (let ((file-coding-system-for-read *noconv*)
230                            (filename
231                             (mh-msg-filename msg mh-draft-folder)
232                             ))
233                        (set-buffer (get-buffer-create name))
234                        (insert-file-contents filename)
235                        (setq buffer-file-name filename)
236                        (setq code-conversion t)
237                        )
238                      (pop-to-buffer name)
239                      (if (re-search-forward "\\(^$\\|^-+$\\)" nil t)
240                          (replace-match "")
241                          )
242                      name))
243                   (t
244                    (prog1
245                        (let ((file-coding-system-for-read *noconv*))
246                          (mh-read-draft "clean-up" (mh-msg-filename msg) nil)
247                          )
248                      (setq code-conversion t)
249                      ))))
250            )
251       (goto-char (point-min))
252       (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil)
253       (if code-conversion
254           (let ((cs (code-detect-region (point-min)(point-max))))
255             (set-file-coding-system
256              (if (listp cs)
257                  (car cs)
258                cs))
259             ))
260       (save-buffer)
261       (mime/edit-again code-conversion t t)
262       (goto-char (point-min))
263       (mh-compose-and-send-mail draft "" from-folder nil nil nil nil nil nil
264                                 config)
265       )))
266
267 ;;; by OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
268 ;;;     1996/2/29 (cf. [tm-ja:1643])
269 (defun tm-mh-e/extract-rejected-mail (msg)
270   "Extract a letter returned by the mail system and make it re-editable.
271 Default is the current message.  The variable mh-new-draft-cleaned-headers
272 gives the headers to clean out of the original message."
273   (interactive (list (mh-get-msg-num t)))
274   (let ((from-folder mh-current-folder)
275         (config (current-window-configuration))
276         (draft (mh-read-draft "extraction" (mh-msg-filename msg) nil)))
277     (setq buffer-read-only nil)
278     (goto-char (point-min))
279     (cond 
280      ((and
281        (re-search-forward
282         (concat "^\\($\\|[Cc]ontent-[Tt]ype:[ \t]+multipart/\\)") nil t)
283        (not (bolp))
284        (re-search-forward "boundary=\"\\([^\"]+\\)\"" nil t))
285       (let ((case-fold-search t)
286             (boundary (buffer-substring (match-beginning 1) (match-end 1))))
287         (cond
288          ((re-search-forward
289            (concat "^--" boundary "\n"
290                    "content-type:[ \t]+"
291                    "\\(message/rfc822\\|text/rfc822-headers\\)\n"
292                    "\\(.+\n\\)*\n") nil t)
293           (delete-region (point-min) (point))
294           (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil)
295           (search-forward
296            (concat "\n--" boundary "--\n") nil t)
297           (delete-region (match-beginning 0) (point-max)))
298          (t
299           (message "Seems no message/rfc822 part.")))))
300      ((re-search-forward mh-rejected-letter-start nil t)
301       (skip-chars-forward " \t\n")
302       (delete-region (point-min) (point))
303       (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil))
304      (t
305       (message "Does not appear to be a rejected letter.")))
306     (goto-char (point-min))
307     (if (re-search-forward "^-+$" nil t)
308         (replace-match "")
309       )
310     (mime/edit-again nil t t)
311     (goto-char (point-min))
312     (set-buffer-modified-p nil)
313     (mh-compose-and-send-mail draft "" from-folder msg
314                               (mh-get-header-field "To:")
315                               (mh-get-header-field "From:")
316                               (mh-get-header-field "Cc:")
317                               nil nil config)))
318
319 ;;; by OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
320 ;;;     1995/11/14 (cf. [tm-ja:1099])
321 (defun tm-mh-e/forward (to cc &optional msg-or-seq)
322   "Forward a message or message sequence as MIME message/rfc822.
323 Defaults to displayed message. If optional prefix argument provided,
324 then prompt for the message sequence. See also documentation for
325 `\\[mh-send]' function."
326   (interactive (list (mh-read-address "To: ")
327                      (mh-read-address "Cc: ")
328                      (if current-prefix-arg
329                          (mh-read-seq-default "Forward" t)
330                        (mh-get-msg-num t)
331                        )))
332   (or msg-or-seq
333       (setq msg-or-seq (mh-get-msg-num t)))
334   (let* ((folder mh-current-folder)
335          (config (current-window-configuration))
336          ;; uses "draft" for compatibility with forw.
337          ;; forw always leaves file in "draft" since it doesn't have -draft
338          (draft-name (expand-file-name "draft" mh-user-path))
339          (draft (cond ((or (not (file-exists-p draft-name))
340                            (y-or-n-p "The file `draft' exists.  Discard it? "))
341                        (mh-exec-cmd "comp"
342                                     "-noedit" "-nowhatnowproc"
343                                     "-form" tm-mh-e/forwcomps
344                                     "-nodraftfolder")
345                        (prog1
346                            (mh-read-draft "" draft-name t)
347                          (mh-insert-fields "To:" to "Cc:" cc)
348                          (set-buffer-modified-p nil)))
349                       (t
350                        (mh-read-draft "" draft-name nil)))))
351     (let ((msubtype "digest")
352           orig-from orig-subject multipart-flag
353           (tag-regexp
354            (concat "^"
355                    (regexp-quote (mime-make-tag "message" "rfc822"))))
356           )
357       (goto-char (point-min))
358       (save-excursion
359         (save-restriction
360           (goto-char (point-max))
361           (if (not (bolp)) (insert "\n"))
362           (let ((beg (point)))
363             (narrow-to-region beg beg)
364             (mh-exec-cmd-output "pick" nil folder msg-or-seq)
365             (if (> (count-lines (point) (point-max)) 1)
366                 (setq multipart-flag t)
367               )
368             (while (re-search-forward "^\\([0-9]+\\)\n" nil t)
369               (let ((forw-msg
370                      (buffer-substring (match-beginning 1) (match-end 1)))
371                     (beg (match-beginning 0))
372                     (end (match-end 0))
373                     )
374                 (save-restriction
375                   (narrow-to-region beg end)
376                   ;; modified for Emacs 18
377                   (delete-region beg end)
378                   (insert-file-contents
379                    (mh-expand-file-name forw-msg
380                                         (mh-expand-file-name folder))
381                    )
382                   (save-excursion
383                     (push-mark (point-max))
384                     (mime-editor/inserted-message-filter))
385                   (goto-char (point-max))
386                   )
387                 (save-excursion
388                   (goto-char beg)
389                   (mime-editor/insert-tag "message" "rfc822")
390                   )))
391             (delete-region (point) (point-max))
392             (if multipart-flag
393                 (mime-editor/enclose-region "digest" beg (point))
394               ))))
395       (re-search-forward tag-regexp)
396       (forward-line 1)
397       (save-restriction
398         (narrow-to-region (point) (point-max))
399         (setq orig-from (mime-eword/decode-string
400                          (mh-get-header-field "From:")))
401         (setq orig-subject (mime-eword/decode-string
402                             (mh-get-header-field "Subject:")))
403         )
404       (let ((forw-subject
405              (mh-forwarded-letter-subject orig-from orig-subject)))
406         (mh-insert-fields "Subject:" forw-subject)
407         (goto-char (point-min))
408         (re-search-forward tag-regexp)
409         (forward-line -1)
410         (delete-other-windows)
411         (if (numberp msg-or-seq)
412             (mh-add-msgs-to-seq msg-or-seq 'forwarded t)
413           (mh-add-msgs-to-seq (mh-seq-to-msgs msg-or-seq) 'forwarded t))
414         (mh-compose-and-send-mail draft "" folder msg-or-seq
415                                   to forw-subject cc
416                                   mh-note-forw "Forwarded:"
417                                   config)))))
418
419 (cond ((not (featurep 'mh-utils))
420        (defun tm-mh-e::insert-letter (folder number verbatim)
421          (mh-insert-letter verbatim folder number)
422          )
423        )
424       ((and (boundp 'mh-e-version)
425             (string-lessp mh-e-version "5"))
426        (defun tm-mh-e::insert-letter (folder number verbatim)
427          (mh-insert-letter number folder verbatim)
428          )
429        )
430       (t
431        (defalias 'tm-mh-e::insert-letter 'mh-insert-letter)
432        ))
433
434 (defun tm-mh-e/insert-letter (verbatim)
435   "Interface to mh-insert-letter."
436   (interactive "P")
437   (let*
438       ((folder (mh-prompt-for-folder
439                 "Message from"
440                 (if (and (stringp mh-sent-from-folder)
441                          (string-match "^\\+" mh-sent-from-folder))
442                     mh-sent-from-folder "+inbox")
443                 nil))
444        (number (tm-mh-e/prompt-for-message "Message number: " folder)))
445     (tm-mh-e::insert-letter folder number verbatim)))
446
447 (defun tm-mh-e/yank-cur-msg-with-no-filter ()
448   "Insert the current message into the draft buffer.
449 This function makes new show-buffer from article-buffer to disable
450 variable `mime-viewer/plain-text-preview-hook'. If you don't want to
451 use text filters for replying message, please set it to
452 `tm-mh-e/message-yank-function'.
453 Prefix each non-blank line in the message with the string in
454 `mh-ins-buf-prefix'. The entire message will be inserted if
455 `mh-yank-from-start-of-msg' is non-nil. If this variable is nil, the
456 portion of the message following the point will be yanked.  If
457 `mh-delete-yanked-msg-window' is non-nil, any window displaying the
458 yanked message will be deleted."
459   (interactive)
460   (if (and mh-sent-from-folder mh-sent-from-msg)
461       (let ((to-point (point))
462             (to-buffer (current-buffer)))
463         (set-buffer mh-sent-from-folder)
464         (if mh-delete-yanked-msg-window
465             (delete-windows-on mh-show-buffer))
466         (set-buffer mh-show-buffer)     ; Find displayed message
467         (let ((mh-ins-str
468                (let (mime-viewer/plain-text-preview-hook buf)
469                  (prog1
470                      (save-window-excursion
471                        (set-buffer mime::preview/article-buffer)
472                        (setq buf (mime/viewer-mode))
473                        (buffer-string)
474                        )
475                    (kill-buffer buf)))))
476           (set-buffer to-buffer)
477           (save-restriction
478             (narrow-to-region to-point to-point)
479             (push-mark)
480             (insert mh-ins-str)
481             (mh-insert-prefix-string mh-ins-buf-prefix)
482             (insert "\n"))))
483     (error "There is no current message")))
484
485 (defun tm-mh-e/yank-current-message ()
486   "Insert the current message into the draft buffer.
487 It uses variable `tm-mh-e/message-yank-function'
488 to select message yanking function."
489   (interactive)
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
498             (save-excursion
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)
503               )
504           (save-excursion
505             (setq mh-sent-from-folder gnus-article-buffer)
506             (set-buffer gnus-article-buffer)
507             (setq mh-show-buffer (current-buffer))
508             )))
509     (funcall tm-mh-e/message-yank-function)
510     ))
511
512 (substitute-key-definition
513  'mh-yank-cur-msg 'tm-mh-e/yank-current-message mh-letter-mode-map)
514 (substitute-key-definition
515  'mh-insert-letter 'tm-mh-e/insert-letter mh-letter-mode-map)
516
517
518 ;;; @ end
519 ;;;
520
521 (provide 'tmh-comp)
522 (require 'tm-mh-e)
523
524 ;;; tmh-comp.el ends here