3e328033b0279bcd12744ba46d9fea89503a5ecd
[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 7.5 1996/07/31 03:07:43 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 (defvar tm-mh-e/message-yank-function 'mh-yank-cur-msg)
48
49
50 ;;; @ for tm-edit
51 ;;;
52
53 (defun tm-mh-e::make-message (folder number)
54   (vector folder number)
55   )
56
57 (defun tm-mh-e::message/folder (message)
58   (elt message 0)
59   )
60
61 (defun tm-mh-e::message/number (message)
62   (elt message 1)
63   )
64
65 (defun tm-mh-e::message/file-name (message)
66   (expand-file-name
67    (tm-mh-e::message/number message)
68    (mh-expand-file-name (tm-mh-e::message/folder message))
69    ))
70
71 ;;; modified by OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
72 ;;;     1995/11/14 (cf. [tm-ja:1096])
73 (defun tm-mh-e/prompt-for-message (prompt folder &optional default)
74   (let* ((files
75           (directory-files (mh-expand-file-name folder) nil "^[0-9]+$")
76           )
77          (folder-buf (get-buffer folder))
78          (default
79            (if folder-buf
80                (save-excursion
81                  (set-buffer folder-buf)
82                  (let* ((show-buffer (get-buffer mh-show-buffer))
83                         (show-buffer-file-name
84                          (buffer-file-name show-buffer)))
85                    (if show-buffer-file-name
86                        (file-name-nondirectory show-buffer-file-name)))))))
87     (if (or (null default)
88             (not (string-match "^[0-9]+$" default)))
89         (setq default
90               (if (and (string= folder mh-sent-from-folder)
91                        mh-sent-from-msg)
92                   (int-to-string mh-sent-from-msg)
93                 (save-excursion
94                   (let (cur-msg)
95                     (if (and
96                          (= 0 (mh-exec-cmd-quiet nil "pick" folder "cur"))
97                          (set-buffer mh-temp-buffer)
98                          (setq cur-msg (buffer-string))
99                          (string-match "^[0-9]+$" cur-msg))
100                         (substring cur-msg 0 (match-end 0))
101                       (car files)))))))
102     (completing-read prompt
103                      (let ((i 0))
104                        (mapcar (function
105                                 (lambda (file)
106                                   (setq i (+ i 1))
107                                   (list file i)
108                                   ))
109                                files)
110                        ) nil nil default)
111     ))
112
113 ;;; modified by OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
114 ;;;     1995/11/14 (cf. [tm-ja:1096])
115 (defun tm-mh-e/query-message (&optional message)
116   (let (folder number)
117     (if message
118         (progn
119           (setq folder (tm-mh-e::message/folder message))
120           (setq number (tm-mh-e::message/number message))
121           ))
122     (or (stringp folder)
123         (setq folder (mh-prompt-for-folder
124                       "Message from"
125                       (if (and (stringp mh-sent-from-folder)
126                                (string-match "^\\+" mh-sent-from-folder))
127                           mh-sent-from-folder "+inbox")
128                       nil)))
129     (setq number
130           (if (numberp number)
131               (number-to-string number)
132             (tm-mh-e/prompt-for-message "Message number: " folder)
133             ))
134     (tm-mh-e::make-message folder number)
135     ))
136
137 (defun tm-mh-e/insert-message (&optional message)
138   ;; always ignores message
139   (let ((article-buffer
140          (if (not (and (stringp mh-sent-from-folder)
141                        (numberp mh-sent-from-msg)
142                        ))
143              (cond ((and (boundp 'gnus-original-article-buffer)
144                          (bufferp mh-sent-from-folder)
145                          (get-buffer gnus-original-article-buffer)
146                          )
147                     gnus-original-article-buffer)
148                    ((and (boundp 'gnus-article-buffer)
149                          (get-buffer gnus-article-buffer)
150                          (bufferp mh-sent-from-folder)
151                          )
152                     (save-excursion
153                       (set-buffer gnus-article-buffer)
154                       (if (eq major-mode 'mime/viewer-mode)
155                           mime::preview/article-buffer
156                         (current-buffer)
157                         )))
158                    ))))
159     (if (null article-buffer)
160         (tm-mh-e/insert-mail
161          (tm-mh-e::make-message mh-sent-from-folder mh-sent-from-msg)
162          )
163       (insert-buffer article-buffer)
164       (mime-editor/inserted-message-filter)
165       )
166     ))
167
168 (defun tm-mh-e/insert-mail (&optional message)
169   (save-excursion
170     (save-restriction
171       (let ((message-file
172              (tm-mh-e::message/file-name (tm-mh-e/query-message message))))
173         (narrow-to-region (point) (point))
174         (insert-file-contents message-file)
175         (push-mark (point-max))
176         (mime-editor/inserted-message-filter)
177     ))))
178
179 (set-alist 'mime-editor/message-inserter-alist
180            'mh-letter-mode (function tm-mh-e/insert-message))
181 (set-alist 'mime-editor/mail-inserter-alist
182            'mh-letter-mode (function tm-mh-e/insert-mail))
183 (set-alist 'mime-editor/mail-inserter-alist
184            'news-reply-mode (function tm-mh-e/insert-mail))
185 (set-alist
186  'mime-editor/split-message-sender-alist
187  'mh-letter-mode
188  (function
189   (lambda (&optional arg)
190     (interactive "P")
191     (write-region (point-min) (point-max)
192                   mime-editor/draft-file-name nil 'no-message)
193     (cond (arg
194            (pop-to-buffer "MH mail delivery")
195            (erase-buffer)
196            (mh-exec-cmd-output mh-send-prog t "-watch" "-nopush"
197                                "-nodraftfolder"
198                                mh-send-args
199                                mime-editor/draft-file-name)
200            (goto-char (point-max))      ; show the interesting part
201            (recenter -1)
202            (sit-for 1))
203           (t
204            (apply 'mh-exec-cmd-quiet t mh-send-prog 
205                   (mh-list-to-string
206                    (list "-nopush" "-nodraftfolder"
207                          "-noverbose" "-nowatch"
208                          mh-send-args mime-editor/draft-file-name)))))
209     )))
210
211
212 ;;; @ commands using tm-edit features
213 ;;;
214
215 (defun tm-mh-e/edit-again (msg)
216   "Clean-up a draft or a message previously sent and make it resendable.
217 Default is the current message.
218 The variable mh-new-draft-cleaned-headers specifies the headers to remove.
219 See also documentation for `\\[mh-send]' function."
220   (interactive (list (mh-get-msg-num t)))
221   (catch 'tag
222     (let* ((from-folder mh-current-folder)
223            (config (current-window-configuration))
224            code-conversion
225            (draft
226             (cond ((and mh-draft-folder (equal from-folder mh-draft-folder))
227                    (let ((name (format "draft-%d" msg)))
228                      (if (get-buffer name)
229                          (throw 'tag (pop-to-buffer name))
230                        )
231                      (let ((file-coding-system-for-read *noconv*)
232                            (filename
233                             (mh-msg-filename msg mh-draft-folder)
234                             ))
235                        (set-buffer (get-buffer-create name))
236                        (insert-file-contents filename)
237                        (setq buffer-file-name filename)
238                        (setq code-conversion t)
239                        )
240                      (pop-to-buffer name)
241                      (if (re-search-forward "\\(^$\\|^-+$\\)" nil t)
242                          (replace-match "")
243                          )
244                      name))
245                   (t
246                    (prog1
247                        (let ((file-coding-system-for-read *noconv*))
248                          (mh-read-draft "clean-up" (mh-msg-filename msg) nil)
249                          )
250                      (setq code-conversion t)
251                      ))))
252            )
253       (goto-char (point-min))
254       (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil)
255       (if code-conversion
256           (let ((cs (code-detect-region (point-min)(point-max))))
257             (set-file-coding-system
258              (if (listp cs)
259                  (car cs)
260                cs))
261             ))
262       (save-buffer)
263       (mime/edit-again code-conversion t t)
264       (goto-char (point-min))
265       (mh-compose-and-send-mail draft "" from-folder nil nil nil nil nil nil
266                                 config)
267       )))
268
269 ;;; by OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
270 ;;;     1996/2/29 (cf. [tm-ja:1643])
271 (defun tm-mh-e/extract-rejected-mail (msg)
272   "Extract a letter returned by the mail system and make it re-editable.
273 Default is the current message.  The variable mh-new-draft-cleaned-headers
274 gives the headers to clean out of the original message."
275   (interactive (list (mh-get-msg-num t)))
276   (let ((from-folder mh-current-folder)
277         (config (current-window-configuration))
278         (draft (mh-read-draft "extraction" (mh-msg-filename msg) nil)))
279     (setq buffer-read-only nil)
280     (goto-char (point-min))
281     (cond 
282      ((and
283        (re-search-forward
284         (concat "^\\($\\|[Cc]ontent-[Tt]ype:[ \t]+multipart/\\)") nil t)
285        (not (bolp))
286        (re-search-forward "boundary=\"\\([^\"]+\\)\"" nil t))
287       (let ((case-fold-search t)
288             (boundary (buffer-substring (match-beginning 1) (match-end 1))))
289         (cond
290          ((re-search-forward
291            (concat "^--" boundary "\n"
292                    "content-type:[ \t]+"
293                    "\\(message/rfc822\\|text/rfc822-headers\\)\n"
294                    "\\(.+\n\\)*\n") nil t)
295           (delete-region (point-min) (point))
296           (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil)
297           (search-forward
298            (concat "\n--" boundary "--\n") nil t)
299           (delete-region (match-beginning 0) (point-max)))
300          (t
301           (message "Seems no message/rfc822 part.")))))
302      ((re-search-forward mh-rejected-letter-start nil t)
303       (skip-chars-forward " \t\n")
304       (delete-region (point-min) (point))
305       (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil))
306      (t
307       (message "Does not appear to be a rejected letter.")))
308     (goto-char (point-min))
309     (if (re-search-forward "^-+$" nil t)
310         (replace-match "")
311       )
312     (mime/edit-again nil t t)
313     (goto-char (point-min))
314     (set-buffer-modified-p nil)
315     (mh-compose-and-send-mail draft "" from-folder msg
316                               (mh-get-header-field "To:")
317                               (mh-get-header-field "From:")
318                               (mh-get-header-field "Cc:")
319                               nil nil config)))
320
321 ;;; by OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
322 ;;;     1995/11/14 (cf. [tm-ja:1099])
323 (defun tm-mh-e/forward (to cc &optional msg-or-seq)
324   "Forward a message or message sequence as MIME message/rfc822.
325 Defaults to displayed message. If optional prefix argument provided,
326 then prompt for the message sequence. See also documentation for
327 `\\[mh-send]' function."
328   (interactive (list (mh-read-address "To: ")
329                      (mh-read-address "Cc: ")
330                      (if current-prefix-arg
331                          (mh-read-seq-default "Forward" t)
332                        (mh-get-msg-num t)
333                        )))
334   (or msg-or-seq
335       (setq msg-or-seq (mh-get-msg-num t)))
336   (let* ((folder mh-current-folder)
337          (config (current-window-configuration))
338          ;; uses "draft" for compatibility with forw.
339          ;; forw always leaves file in "draft" since it doesn't have -draft
340          (draft-name (expand-file-name "draft" mh-user-path))
341          (draft (cond ((or (not (file-exists-p draft-name))
342                            (y-or-n-p "The file `draft' exists.  Discard it? "))
343                        (mh-exec-cmd "comp"
344                                     "-noedit" "-nowhatnowproc"
345                                     "-form" tm-mh-e/forwcomps
346                                     "-nodraftfolder")
347                        (prog1
348                            (mh-read-draft "" draft-name t)
349                          (mh-insert-fields "To:" to "Cc:" cc)
350                          (set-buffer-modified-p nil)))
351                       (t
352                        (mh-read-draft "" draft-name nil)))))
353     (let ((msubtype "digest")
354           orig-from orig-subject multipart-flag
355           (tag-regexp
356            (concat "^"
357                    (regexp-quote (mime-make-tag "message" "rfc822"))))
358           )
359       (goto-char (point-min))
360       (save-excursion
361         (save-restriction
362           (goto-char (point-max))
363           (if (not (bolp)) (insert "\n"))
364           (let ((beg (point)))
365             (narrow-to-region beg beg)
366             (mh-exec-cmd-output "pick" nil folder msg-or-seq)
367             (if (> (count-lines (point) (point-max)) 1)
368                 (setq multipart-flag t)
369               )
370             (while (re-search-forward "^\\([0-9]+\\)\n" nil t)
371               (let ((forw-msg
372                      (buffer-substring (match-beginning 1) (match-end 1)))
373                     (beg (match-beginning 0))
374                     (end (match-end 0))
375                     )
376                 (save-restriction
377                   (narrow-to-region beg end)
378                   ;; modified for Emacs 18
379                   (delete-region beg end)
380                   (insert-file-contents
381                    (mh-expand-file-name forw-msg
382                                         (mh-expand-file-name folder))
383                    )
384                   (save-excursion
385                     (push-mark (point-max))
386                     (mime-editor/inserted-message-filter))
387                   (goto-char (point-max))
388                   )
389                 (save-excursion
390                   (goto-char beg)
391                   (mime-editor/insert-tag "message" "rfc822")
392                   )))
393             (delete-region (point) (point-max))
394             (if multipart-flag
395                 (mime-editor/enclose-region "digest" beg (point))
396               ))))
397       (re-search-forward tag-regexp)
398       (forward-line 1)
399       (save-restriction
400         (narrow-to-region (point) (point-max))
401         (setq orig-from (mime-eword/decode-string
402                          (mh-get-header-field "From:")))
403         (setq orig-subject (mime-eword/decode-string
404                             (mh-get-header-field "Subject:")))
405         )
406       (let ((forw-subject
407              (mh-forwarded-letter-subject orig-from orig-subject)))
408         (mh-insert-fields "Subject:" forw-subject)
409         (goto-char (point-min))
410         (re-search-forward tag-regexp)
411         (forward-line -1)
412         (delete-other-windows)
413         (if (numberp msg-or-seq)
414             (mh-add-msgs-to-seq msg-or-seq 'forwarded t)
415           (mh-add-msgs-to-seq (mh-seq-to-msgs msg-or-seq) 'forwarded t))
416         (mh-compose-and-send-mail draft "" folder msg-or-seq
417                                   to forw-subject cc
418                                   mh-note-forw "Forwarded:"
419                                   config)))))
420
421 (cond ((not (featurep 'mh-utils))
422        (defun tm-mh-e::insert-letter (folder number verbatim)
423          (mh-insert-letter verbatim folder number)
424          )
425        )
426       ((and (boundp 'mh-e-version)
427             (string-lessp mh-e-version "5"))
428        (defun tm-mh-e::insert-letter (folder number verbatim)
429          (mh-insert-letter number folder verbatim)
430          )
431        )
432       (t
433        (defalias 'tm-mh-e::insert-letter 'mh-insert-letter)
434        ))
435
436 (defun tm-mh-e/insert-letter (verbatim)
437   "Interface to mh-insert-letter."
438   (interactive "P")
439   (let*
440       ((folder (mh-prompt-for-folder
441                 "Message from"
442                 (if (and (stringp mh-sent-from-folder)
443                          (string-match "^\\+" mh-sent-from-folder))
444                     mh-sent-from-folder "+inbox")
445                 nil))
446        (number (tm-mh-e/prompt-for-message "Message number: " folder)))
447     (tm-mh-e::insert-letter folder number verbatim)))
448
449 (defun tm-mh-e/yank-cur-msg-with-no-filter ()
450   "Insert the current message into the draft buffer.
451 This function makes new show-buffer from article-buffer to disable
452 variable `mime-viewer/plain-text-preview-hook'. If you don't want to
453 use text filters for replying message, please set it to
454 `tm-mh-e/message-yank-function'.
455 Prefix each non-blank line in the message with the string in
456 `mh-ins-buf-prefix'. The entire message will be inserted if
457 `mh-yank-from-start-of-msg' is non-nil. If this variable is nil, the
458 portion of the message following the point will be yanked.  If
459 `mh-delete-yanked-msg-window' is non-nil, any window displaying the
460 yanked message will be deleted."
461   (interactive)
462   (if (and mh-sent-from-folder mh-sent-from-msg)
463       (let ((to-point (point))
464             (to-buffer (current-buffer)))
465         (set-buffer mh-sent-from-folder)
466         (if mh-delete-yanked-msg-window
467             (delete-windows-on mh-show-buffer))
468         (set-buffer mh-show-buffer)     ; Find displayed message
469         (let ((mh-ins-str
470                (let (mime-viewer/plain-text-preview-hook buf)
471                  (prog1
472                      (save-window-excursion
473                        (set-buffer mime::preview/article-buffer)
474                        (setq buf (mime/viewer-mode))
475                        (buffer-string)
476                        )
477                    (kill-buffer buf)))))
478           (set-buffer to-buffer)
479           (save-restriction
480             (narrow-to-region to-point to-point)
481             (push-mark)
482             (insert mh-ins-str)
483             (mh-insert-prefix-string mh-ins-buf-prefix)
484             (insert "\n"))))
485     (error "There is no current message")))
486
487 (defun tm-mh-e/yank-current-message ()
488   "Insert the current message into the draft buffer.
489 It uses variable `tm-mh-e/message-yank-function'
490 to select message yanking function."
491   (interactive)
492   (let ((mh-sent-from-folder mh-sent-from-folder)
493         (mh-sent-from-msg mh-sent-from-msg))
494     (if (and (not (stringp mh-sent-from-folder))
495              (boundp 'gnus-article-buffer)
496              (get-buffer gnus-article-buffer)
497              (bufferp mh-sent-from-folder)
498              ) ; might be called from GNUS
499         (if (boundp 'gnus-article-copy) ; might be sgnus
500             (save-excursion
501               (gnus-copy-article-buffer)
502               (setq mh-sent-from-folder gnus-article-copy)
503               (set-buffer mh-sent-from-folder)
504               (setq mh-show-buffer gnus-article-copy)
505               )
506           (save-excursion
507             (setq mh-sent-from-folder gnus-article-buffer)
508             (set-buffer gnus-article-buffer)
509             (setq mh-show-buffer (current-buffer))
510             )))
511     (funcall tm-mh-e/message-yank-function)
512     ))
513
514 (substitute-key-definition
515  'mh-yank-cur-msg 'tm-mh-e/yank-current-message mh-letter-mode-map)
516 (substitute-key-definition
517  'mh-insert-letter 'tm-mh-e/insert-letter mh-letter-mode-map)
518
519
520 ;;; @ end
521 ;;;
522
523 (provide 'tmh-comp)
524 (require 'tm-mh-e)
525
526 ;;; tmh-comp.el ends here