(What's EMH?): Modify for SEMI 1.7.
[elisp/emh.git] / emh-comp.el
1 ;;; emh-comp.el --- emh functions for composing messages
2
3 ;; Copyright (C) 1993,1994,1995,1996,1997,1998 Free Software Foundation, Inc.
4
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
10
11 ;; This file is part of emh.
12
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.
17
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.
22
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.
27
28 ;;; Code:
29
30 (require 'mh-comp)
31 (require 'mime-edit)
32
33
34 ;;; @ variable
35 ;;;
36
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.")
42
43 (defvar emh-message-yank-function 'mh-yank-cur-msg)
44
45
46 ;;; @ for tm-edit
47 ;;;
48
49 (defun emh::make-message (folder number)
50   (vector folder number)
51   )
52
53 (defun emh::message/folder (message)
54   (elt message 0)
55   )
56
57 (defun emh::message/number (message)
58   (elt message 1)
59   )
60
61 (defun emh::message/file-name (message)
62   (expand-file-name
63    (emh::message/number message)
64    (mh-expand-file-name (emh::message/folder message))
65    ))
66
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)
70   (let* ((files
71           (directory-files (mh-expand-file-name folder) nil "^[0-9]+$")
72           )
73          (folder-buf (get-buffer folder))
74          (default
75            (if folder-buf
76                (save-excursion
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)))
85         (setq default
86               (if (and (string= folder mh-sent-from-folder)
87                        mh-sent-from-msg)
88                   (int-to-string mh-sent-from-msg)
89                 (save-excursion
90                   (let (cur-msg)
91                     (if (and
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))
97                       (car files)))))))
98     (completing-read prompt
99                      (let ((i 0))
100                        (mapcar (function
101                                 (lambda (file)
102                                   (setq i (+ i 1))
103                                   (list file i)
104                                   ))
105                                files)
106                        ) nil nil default)
107     ))
108
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)
112   (let (folder number)
113     (if message
114         (progn
115           (setq folder (emh::message/folder message))
116           (setq number (emh::message/number message))
117           ))
118     (or (stringp folder)
119         (setq folder (mh-prompt-for-folder
120                       "Message from"
121                       (if (and (stringp mh-sent-from-folder)
122                                (string-match "^\\+" mh-sent-from-folder))
123                           mh-sent-from-folder "+inbox")
124                       nil)))
125     (setq number
126           (if (numberp number)
127               (number-to-string number)
128             (emh-prompt-for-message "Message number: " folder)
129             ))
130     (emh::make-message folder number)
131     ))
132
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)
138                        ))
139              (cond ((and (boundp 'gnus-original-article-buffer)
140                          (bufferp mh-sent-from-folder)
141                          (get-buffer gnus-original-article-buffer)
142                          )
143                     gnus-original-article-buffer)
144                    ((and (boundp 'gnus-article-buffer)
145                          (get-buffer gnus-article-buffer)
146                          (bufferp mh-sent-from-folder)
147                          )
148                     (save-excursion
149                       (set-buffer gnus-article-buffer)
150                       (if (eq major-mode 'mime-view-mode)
151                           mime-raw-buffer
152                         (current-buffer)
153                         )))
154                    ))))
155     (if (null article-buffer)
156         (emh-insert-mail
157          (emh::make-message mh-sent-from-folder mh-sent-from-msg)
158          )
159       (insert-buffer article-buffer)
160       (mime-edit-inserted-message-filter)
161       )
162     ))
163
164 (defun emh-insert-mail (&optional message)
165   (save-excursion
166     (save-restriction
167       (let ((message-file
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)
173     ))))
174
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))
181 (set-alist
182  'mime-edit-split-message-sender-alist
183  'mh-letter-mode
184  (function
185   (lambda (&optional arg)
186     (interactive "P")
187     (write-region (point-min) (point-max)
188                   mime-edit-draft-file-name nil 'no-message)
189     (cond (arg
190            (pop-to-buffer "MH mail delivery")
191            (erase-buffer)
192            (mh-exec-cmd-output mh-send-prog t "-watch" "-nopush"
193                                "-nodraftfolder"
194                                mh-send-args
195                                mime-edit-draft-file-name)
196            (goto-char (point-max))      ; show the interesting part
197            (recenter -1)
198            (sit-for 1))
199           (t
200            (apply 'mh-exec-cmd-quiet t mh-send-prog 
201                   (mh-list-to-string
202                    (list "-nopush" "-nodraftfolder"
203                          "-noverbose" "-nowatch"
204                          mh-send-args mime-edit-draft-file-name)))))
205     )))
206
207
208 ;;; @ commands using tm-edit features
209 ;;;
210
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)))
217   (catch 'tag
218     (let* ((from-folder mh-current-folder)
219            (config (current-window-configuration))
220            (draft
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))
225                        )
226                      (let ((filename
227                             (mh-msg-filename msg mh-draft-folder)
228                             ))
229                        (set-buffer (get-buffer-create name))
230                        (as-binary-input-file (insert-file-contents filename))
231                        (setq buffer-file-name filename)
232                        )
233                      (pop-to-buffer name)
234                      (if (re-search-forward "^-+$" nil t)
235                          (replace-match "")
236                          )
237                      name))
238                   (t
239                    (let ((flag enable-multibyte-characters))
240                      (prog1
241                          (as-binary-input-file
242                           (mh-read-draft "clean-up"
243                                          (mh-msg-filename msg) nil))
244                        (set-buffer-multibyte flag)
245                        ))
246                    ))))
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
251          (if (listp cs)
252              (car cs)
253            cs)))
254       (save-buffer)
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
258                                 config)
259       )))
260
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))
273     (cond 
274      ((and
275        (re-search-forward
276         (concat "^\\($\\|[Cc]ontent-[Tt]ype:[ \t]+multipart/\\)") nil t)
277        (not (bolp))
278        (re-search-forward "boundary=\"\\([^\"]+\\)\"" nil t))
279       (let ((case-fold-search t)
280             (boundary (buffer-substring (match-beginning 1) (match-end 1))))
281         (cond
282          ((re-search-forward
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)
289           (search-forward
290            (concat "\n--" boundary "--\n") nil t)
291           (delete-region (match-beginning 0) (point-max)))
292          (t
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))
298      (t
299       (message "Does not appear to be a rejected letter.")))
300     (goto-char (point-min))
301     (if (re-search-forward "^-+$" nil t)
302         (replace-match "")
303       )
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:")
311                               nil nil config)))
312
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)
324                        (mh-get-msg-num t)
325                        )))
326   (or msg-or-seq
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? "))
335                        (mh-exec-cmd "comp"
336                                     "-noedit" "-nowhatnowproc"
337                                     "-form" emh-forwcomps
338                                     "-nodraftfolder")
339                        (prog1
340                            (mh-read-draft "" draft-name t)
341                          (mh-insert-fields "To:" to "Cc:" cc)
342                          (set-buffer-modified-p nil)))
343                       (t
344                        (mh-read-draft "" draft-name nil)))))
345     (let ((msubtype "digest")
346           orig-from orig-subject multipart-flag
347           (tag-regexp
348            (concat "^"
349                    (regexp-quote (mime-make-tag "message" "rfc822"))))
350           )
351       (goto-char (point-min))
352       (save-excursion
353         (save-restriction
354           (goto-char (point-max))
355           (if (not (bolp)) (insert "\n"))
356           (let ((beg (point)))
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)
361               )
362             (while (re-search-forward "^\\([0-9]+\\)\n" nil t)
363               (let ((forw-msg
364                      (buffer-substring (match-beginning 1) (match-end 1)))
365                     (beg (match-beginning 0))
366                     (end (match-end 0))
367                     )
368                 (save-restriction
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))
375                    )
376                   (save-excursion
377                     (push-mark (point-max))
378                     (mime-edit-inserted-message-filter))
379                   (goto-char (point-max))
380                   )
381                 (save-excursion
382                   (goto-char beg)
383                   (mime-edit-insert-tag "message" "rfc822")
384                   )))
385             (delete-region (point) (point-max))
386             (if multipart-flag
387                 (mime-edit-enclose-digest-region beg (point))
388               ))))
389       (re-search-forward tag-regexp)
390       (forward-line 1)
391       (save-restriction
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:")))
397         )
398       (let ((forw-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)
403         (forward-line -1)
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
409                                   to forw-subject cc
410                                   mh-note-forw "Forwarded:"
411                                   config)))))
412
413 (cond ((not (featurep 'mh-utils))
414        (defun emh::insert-letter (folder number verbatim)
415          (mh-insert-letter verbatim folder number)
416          )
417        )
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)
422          )
423        )
424       (t
425        (defalias 'emh::insert-letter 'mh-insert-letter)
426        ))
427
428 (defun emh-insert-letter (verbatim)
429   "Interface to mh-insert-letter."
430   (interactive "P")
431   (let*
432       ((folder (mh-prompt-for-folder
433                 "Message from"
434                 (if (and (stringp mh-sent-from-folder)
435                          (string-match "^\\+" mh-sent-from-folder))
436                     mh-sent-from-folder "+inbox")
437                 nil))
438        (number (emh-prompt-for-message "Message number: " folder)))
439     (emh::insert-letter folder number verbatim)))
440
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."
453   (interactive)
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
461         (let ((mh-ins-str
462                (if mime-raw-buffer
463                    (let (mime-display-text/plain-hook buf)
464                      (prog1
465                          (save-window-excursion
466                            (set-buffer mime-raw-buffer)
467                            (setq buf (mime-view-mode))
468                            (buffer-string)
469                            )
470                        (kill-buffer buf)
471                        ))
472                  (buffer-string)
473                  )))
474           (set-buffer to-buffer)
475           (save-restriction
476             (narrow-to-region to-point to-point)
477             (push-mark)
478             (insert mh-ins-str)
479             (mh-insert-prefix-string mh-ins-buf-prefix)
480             (insert "\n"))))
481     (error "There is no current message")))
482
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."
487   (interactive)
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
496             (save-excursion
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)
501               )
502           (save-excursion
503             (setq mh-sent-from-folder gnus-article-buffer)
504             (set-buffer gnus-article-buffer)
505             (setq mh-show-buffer (current-buffer))
506             )))
507     (funcall emh-message-yank-function)
508     ))
509
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)
514
515
516 ;;; @ end
517 ;;;
518
519 (provide 'emh-comp)
520 (require 'emh)
521
522 ;;; emh-comp.el ends here