*** empty log message ***
[elisp/emh.git] / emh-comp.el
1 ;;; emh-comp.el --- emh functions for composing messages
2
3 ;; Copyright (C) 1993,1994,1995,1996,1997 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 ;;      Renamed: 1997/2/21 from tmh-comp.el
10 ;; Version: $Id: emh-comp.el,v 0.2 1997-03-07 14:25:21 morioka Exp $
11 ;; Keywords: mail composing, MH, MIME, mail
12
13 ;; This file is part of emh.
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 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.
29
30 ;;; Code:
31
32 (require 'mh-comp)
33 (require 'mime-edit)
34
35
36 ;;; @ variable
37 ;;;
38
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.")
44
45 (defvar emh-message-yank-function 'mh-yank-cur-msg)
46
47
48 ;;; @ for tm-edit
49 ;;;
50
51 (defun emh::make-message (folder number)
52   (vector folder number)
53   )
54
55 (defun emh::message/folder (message)
56   (elt message 0)
57   )
58
59 (defun emh::message/number (message)
60   (elt message 1)
61   )
62
63 (defun emh::message/file-name (message)
64   (expand-file-name
65    (emh::message/number message)
66    (mh-expand-file-name (emh::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 emh-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 emh-query-message (&optional message)
114   (let (folder number)
115     (if message
116         (progn
117           (setq folder (emh::message/folder message))
118           (setq number (emh::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             (emh-prompt-for-message "Message number: " folder)
131             ))
132     (emh::make-message folder number)
133     ))
134
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)
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-view-mode)
153                           mime::preview/article-buffer
154                         (current-buffer)
155                         )))
156                    ))))
157     (if (null article-buffer)
158         (emh-insert-mail
159          (emh::make-message mh-sent-from-folder mh-sent-from-msg)
160          )
161       (insert-buffer article-buffer)
162       (mime-edit-inserted-message-filter)
163       )
164     ))
165
166 (defun emh-insert-mail (&optional message)
167   (save-excursion
168     (save-restriction
169       (let ((message-file
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)
175     ))))
176
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))
183 (set-alist
184  'mime-edit-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-edit-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-edit-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-edit-draft-file-name)))))
207     )))
208
209
210 ;;; @ commands using tm-edit features
211 ;;;
212
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)))
219   (catch 'tag
220     (let* ((from-folder mh-current-folder)
221            (config (current-window-configuration))
222            (draft
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))
227                        )
228                      (let ((filename
229                             (mh-msg-filename msg mh-draft-folder)
230                             ))
231                        (set-buffer (get-buffer-create name))
232                        (as-binary-input-file (insert-file-contents filename))
233                        (setq buffer-file-name filename)
234                        )
235                      (pop-to-buffer name)
236                      (if (re-search-forward "^-+$" nil t)
237                          (replace-match "")
238                          )
239                      name))
240                   (t
241                    (as-binary-input-file
242                     (mh-read-draft "clean-up" (mh-msg-filename msg) nil)
243                     )
244                    )))
245            )
246       (goto-char (point-min))
247       (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil)
248       (let ((cs (detect-coding-region (point-min)(point-max))))
249         (set-buffer-file-coding-system
250          (if (listp cs)
251              (car cs)
252            cs)))
253       (save-buffer)
254       (mime-edit-again nil 'no-separator 'not-turn-on)
255       (goto-char (point-min))
256       (mh-compose-and-send-mail draft "" from-folder nil nil nil nil nil nil
257                                 config)
258       )))
259
260 ;;; by OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
261 ;;;     1996/2/29 (cf. [tm-ja:1643])
262 (defun emh-extract-rejected-mail (msg)
263   "Extract a letter returned by the mail system and make it re-editable.
264 Default is the current message.  The variable mh-new-draft-cleaned-headers
265 gives the headers to clean out of the original message."
266   (interactive (list (mh-get-msg-num t)))
267   (let ((from-folder mh-current-folder)
268         (config (current-window-configuration))
269         (draft (mh-read-draft "extraction" (mh-msg-filename msg) nil)))
270     (setq buffer-read-only nil)
271     (goto-char (point-min))
272     (cond 
273      ((and
274        (re-search-forward
275         (concat "^\\($\\|[Cc]ontent-[Tt]ype:[ \t]+multipart/\\)") nil t)
276        (not (bolp))
277        (re-search-forward "boundary=\"\\([^\"]+\\)\"" nil t))
278       (let ((case-fold-search t)
279             (boundary (buffer-substring (match-beginning 1) (match-end 1))))
280         (cond
281          ((re-search-forward
282            (concat "^--" boundary "\n"
283                    "content-type:[ \t]+"
284                    "\\(message/rfc822\\|text/rfc822-headers\\)\n"
285                    "\\(.+\n\\)*\n") nil t)
286           (delete-region (point-min) (point))
287           (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil)
288           (search-forward
289            (concat "\n--" boundary "--\n") nil t)
290           (delete-region (match-beginning 0) (point-max)))
291          (t
292           (message "Seems no message/rfc822 part.")))))
293      ((re-search-forward mh-rejected-letter-start nil t)
294       (skip-chars-forward " \t\n")
295       (delete-region (point-min) (point))
296       (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil))
297      (t
298       (message "Does not appear to be a rejected letter.")))
299     (goto-char (point-min))
300     (if (re-search-forward "^-+$" nil t)
301         (replace-match "")
302       )
303     (mime-edit-again nil t t)
304     (goto-char (point-min))
305     (set-buffer-modified-p nil)
306     (mh-compose-and-send-mail draft "" from-folder msg
307                               (mh-get-header-field "To:")
308                               (mh-get-header-field "From:")
309                               (mh-get-header-field "Cc:")
310                               nil nil config)))
311
312 ;;; by OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
313 ;;;     1995/11/14 (cf. [tm-ja:1099])
314 (defun emh-forward (to cc &optional msg-or-seq)
315   "Forward a message or message sequence as MIME message/rfc822.
316 Defaults to displayed message. If optional prefix argument provided,
317 then prompt for the message sequence. See also documentation for
318 `\\[mh-send]' function."
319   (interactive (list (mh-read-address "To: ")
320                      (mh-read-address "Cc: ")
321                      (if current-prefix-arg
322                          (mh-read-seq-default "Forward" t)
323                        (mh-get-msg-num t)
324                        )))
325   (or msg-or-seq
326       (setq msg-or-seq (mh-get-msg-num t)))
327   (let* ((folder mh-current-folder)
328          (config (current-window-configuration))
329          ;; uses "draft" for compatibility with forw.
330          ;; forw always leaves file in "draft" since it doesn't have -draft
331          (draft-name (expand-file-name "draft" mh-user-path))
332          (draft (cond ((or (not (file-exists-p draft-name))
333                            (y-or-n-p "The file `draft' exists.  Discard it? "))
334                        (mh-exec-cmd "comp"
335                                     "-noedit" "-nowhatnowproc"
336                                     "-form" emh-forwcomps
337                                     "-nodraftfolder")
338                        (prog1
339                            (mh-read-draft "" draft-name t)
340                          (mh-insert-fields "To:" to "Cc:" cc)
341                          (set-buffer-modified-p nil)))
342                       (t
343                        (mh-read-draft "" draft-name nil)))))
344     (let ((msubtype "digest")
345           orig-from orig-subject multipart-flag
346           (tag-regexp
347            (concat "^"
348                    (regexp-quote (mime-make-tag "message" "rfc822"))))
349           )
350       (goto-char (point-min))
351       (save-excursion
352         (save-restriction
353           (goto-char (point-max))
354           (if (not (bolp)) (insert "\n"))
355           (let ((beg (point)))
356             (narrow-to-region beg beg)
357             (mh-exec-cmd-output "pick" nil folder msg-or-seq)
358             (if (> (count-lines (point) (point-max)) 1)
359                 (setq multipart-flag t)
360               )
361             (while (re-search-forward "^\\([0-9]+\\)\n" nil t)
362               (let ((forw-msg
363                      (buffer-substring (match-beginning 1) (match-end 1)))
364                     (beg (match-beginning 0))
365                     (end (match-end 0))
366                     )
367                 (save-restriction
368                   (narrow-to-region beg end)
369                   ;; modified for Emacs 18
370                   (delete-region beg end)
371                   (insert-file-contents
372                    (mh-expand-file-name forw-msg
373                                         (mh-expand-file-name folder))
374                    )
375                   (save-excursion
376                     (push-mark (point-max))
377                     (mime-edit-inserted-message-filter))
378                   (goto-char (point-max))
379                   )
380                 (save-excursion
381                   (goto-char beg)
382                   (mime-edit-insert-tag "message" "rfc822")
383                   )))
384             (delete-region (point) (point-max))
385             (if multipart-flag
386                 (mime-edit-enclose-region "digest" beg (point))
387               ))))
388       (re-search-forward tag-regexp)
389       (forward-line 1)
390       (save-restriction
391         (narrow-to-region (point) (point-max))
392         (setq orig-from (eword-decode-string
393                          (mh-get-header-field "From:")))
394         (setq orig-subject (eword-decode-string
395                             (mh-get-header-field "Subject:")))
396         )
397       (let ((forw-subject
398              (mh-forwarded-letter-subject orig-from orig-subject)))
399         (mh-insert-fields "Subject:" forw-subject)
400         (goto-char (point-min))
401         (re-search-forward tag-regexp)
402         (forward-line -1)
403         (delete-other-windows)
404         (if (numberp msg-or-seq)
405             (mh-add-msgs-to-seq msg-or-seq 'forwarded t)
406           (mh-add-msgs-to-seq (mh-seq-to-msgs msg-or-seq) 'forwarded t))
407         (mh-compose-and-send-mail draft "" folder msg-or-seq
408                                   to forw-subject cc
409                                   mh-note-forw "Forwarded:"
410                                   config)))))
411
412 (cond ((not (featurep 'mh-utils))
413        (defun emh::insert-letter (folder number verbatim)
414          (mh-insert-letter verbatim folder number)
415          )
416        )
417       ((and (boundp 'mh-e-version)
418             (string-lessp mh-e-version "5"))
419        (defun emh::insert-letter (folder number verbatim)
420          (mh-insert-letter number folder verbatim)
421          )
422        )
423       (t
424        (defalias 'emh::insert-letter 'mh-insert-letter)
425        ))
426
427 (defun emh-insert-letter (verbatim)
428   "Interface to mh-insert-letter."
429   (interactive "P")
430   (let*
431       ((folder (mh-prompt-for-folder
432                 "Message from"
433                 (if (and (stringp mh-sent-from-folder)
434                          (string-match "^\\+" mh-sent-from-folder))
435                     mh-sent-from-folder "+inbox")
436                 nil))
437        (number (emh-prompt-for-message "Message number: " folder)))
438     (emh::insert-letter folder number verbatim)))
439
440 (defun emh-yank-cur-msg-with-no-filter ()
441   "Insert the current message into the draft buffer.
442 This function makes new show-buffer from article-buffer to disable
443 variable `mime-view-plain-text-preview-hook'. If you don't want to
444 use text filters for replying message, please set it to
445 `emh-message-yank-function'.
446 Prefix each non-blank line in the message with the string in
447 `mh-ins-buf-prefix'. The entire message will be inserted if
448 `mh-yank-from-start-of-msg' is non-nil. If this variable is nil, the
449 portion of the message following the point will be yanked.  If
450 `mh-delete-yanked-msg-window' is non-nil, any window displaying the
451 yanked message will be deleted."
452   (interactive)
453   (if (and mh-sent-from-folder mh-sent-from-msg)
454       (let ((to-point (point))
455             (to-buffer (current-buffer)))
456         (set-buffer mh-sent-from-folder)
457         (if mh-delete-yanked-msg-window
458             (delete-windows-on mh-show-buffer))
459         (set-buffer mh-show-buffer)     ; Find displayed message
460         (let ((mh-ins-str
461                (if mime::preview/article-buffer
462                    (let (mime-view-plain-text-preview-hook buf)
463                      (prog1
464                          (save-window-excursion
465                            (set-buffer mime::preview/article-buffer)
466                            (setq buf (mime-view-mode))
467                            (buffer-string)
468                            )
469                        (kill-buffer buf)
470                        ))
471                  (buffer-string)
472                  )))
473           (set-buffer to-buffer)
474           (save-restriction
475             (narrow-to-region to-point to-point)
476             (push-mark)
477             (insert mh-ins-str)
478             (mh-insert-prefix-string mh-ins-buf-prefix)
479             (insert "\n"))))
480     (error "There is no current message")))
481
482 (defun emh-yank-current-message ()
483   "Insert the current message into the draft buffer.
484 It uses variable `emh-message-yank-function'
485 to select message yanking function."
486   (interactive)
487   (let ((mh-sent-from-folder mh-sent-from-folder)
488         (mh-sent-from-msg mh-sent-from-msg))
489     (if (and (not (stringp mh-sent-from-folder))
490              (boundp 'gnus-article-buffer)
491              (get-buffer gnus-article-buffer)
492              (bufferp mh-sent-from-folder)
493              ) ; might be called from GNUS
494         (if (boundp 'gnus-article-copy) ; might be sgnus
495             (save-excursion
496               (gnus-copy-article-buffer)
497               (setq mh-sent-from-folder gnus-article-copy)
498               (set-buffer mh-sent-from-folder)
499               (setq mh-show-buffer gnus-article-copy)
500               )
501           (save-excursion
502             (setq mh-sent-from-folder gnus-article-buffer)
503             (set-buffer gnus-article-buffer)
504             (setq mh-show-buffer (current-buffer))
505             )))
506     (funcall emh-message-yank-function)
507     ))
508
509 (substitute-key-definition
510  'mh-yank-cur-msg 'emh-yank-current-message mh-letter-mode-map)
511 (substitute-key-definition
512  'mh-insert-letter 'emh-insert-letter mh-letter-mode-map)
513
514
515 ;;; @ end
516 ;;;
517
518 (provide 'emh-comp)
519 (require 'emh)
520
521 ;;; emh-comp.el ends here