(What's EMH?): Modify for the new URLs of APEL, FLIM and SEMI.
[elisp/emh.git] / emh-comp.el
1 ;;; emh-comp.el --- emh functions for composing messages
2
3 ;; Copyright (C) 1993,94,95,96,97,98,99,2000 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
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 (require 'emh-def)
33
34 ;; Avoid byte compile warnings.
35 ;; (defvar gnus-article-buffer)
36 ;; (defvar gnus-article-copy)
37 ;; (defvar gnus-original-article-buffer)
38 ;; (eval-when-compile
39 ;;   (fset 'gnus-copy-article-buffer 'ignore)
40 ;;   )
41
42
43 ;;; @ variable
44 ;;;
45
46 (defvar emh-forwcomps "forwcomps"
47   "Name of file to be used as a skeleton for forwarding messages.
48 Default is \"forwcomps\".  If not a complete path name, the file
49 is searched for first in the user's MH directory, then in the
50 system MH lib directory.")
51
52 ;; (defvar emh-message-yank-function 'mh-yank-cur-msg)
53
54
55 ;;; @ for tm-edit
56 ;;;
57
58 (defun emh::make-message (folder number)
59   (vector folder number)
60   )
61
62 (defun emh::message/folder (message)
63   (elt message 0)
64   )
65
66 (defun emh::message/number (message)
67   (elt message 1)
68   )
69
70 (defun emh::message/file-name (message)
71   (expand-file-name
72    (emh::message/number message)
73    (mh-expand-file-name (emh::message/folder message))
74    ))
75
76 ;;; modified by OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
77 ;;;     1995/11/14 (cf. [tm-ja:1096])
78 (defun emh-prompt-for-message (prompt folder &optional default)
79   (let* ((files
80           (directory-files (mh-expand-file-name folder) nil "^[0-9]+$")
81           )
82          (folder-buf (get-buffer folder))
83          (default
84            (if folder-buf
85                (save-excursion
86                  (set-buffer folder-buf)
87                  (let* ((show-buffer (get-buffer mh-show-buffer))
88                         (show-buffer-file-name
89                          (buffer-file-name show-buffer)))
90                    (if show-buffer-file-name
91                        (file-name-nondirectory show-buffer-file-name)))))))
92     (if (or (null default)
93             (not (string-match "^[0-9]+$" default)))
94         (setq default
95               (if (and (string= folder mh-sent-from-folder)
96                        mh-sent-from-msg)
97                   (int-to-string mh-sent-from-msg)
98                 (save-excursion
99                   (let (cur-msg)
100                     (if (and
101                          (= 0 (mh-exec-cmd-quiet nil "pick" folder "cur"))
102                          (set-buffer mh-temp-buffer)
103                          (setq cur-msg (buffer-string))
104                          (string-match "^[0-9]+$" cur-msg))
105                         (substring cur-msg 0 (match-end 0))
106                       (car files)))))))
107     (completing-read prompt
108                      (let ((i 0))
109                        (mapcar (function
110                                 (lambda (file)
111                                   (setq i (+ i 1))
112                                   (list file i)
113                                   ))
114                                files)
115                        ) nil nil default)
116     ))
117
118 ;;; modified by OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
119 ;;;     1995/11/14 (cf. [tm-ja:1096])
120 (defun emh-query-message (&optional message)
121   (let (folder number)
122     (if message
123         (progn
124           (setq folder (emh::message/folder message))
125           (setq number (emh::message/number message))
126           ))
127     (or (stringp folder)
128         (setq folder (mh-prompt-for-folder
129                       "Message from"
130                       (if (and (stringp mh-sent-from-folder)
131                                (string-match "^\\+" mh-sent-from-folder))
132                           mh-sent-from-folder "+inbox")
133                       nil)))
134     (setq number
135           (if (numberp number)
136               (number-to-string number)
137             (emh-prompt-for-message "Message number: " folder)
138             ))
139     (emh::make-message folder number)
140     ))
141
142 (defun emh-insert-message (&optional message)
143   ;; always ignores message
144   ;; (let ((article-buffer
145   ;;        (if (not (and (stringp mh-sent-from-folder)
146   ;;                      (numberp mh-sent-from-msg)
147   ;;                      ))
148   ;;            (cond ((and (boundp 'gnus-original-article-buffer)
149   ;;                        (bufferp mh-sent-from-folder)
150   ;;                        (get-buffer gnus-original-article-buffer)
151   ;;                        )
152   ;;                   gnus-original-article-buffer)
153   ;;                  ((and (boundp 'gnus-article-buffer)
154   ;;                        (get-buffer gnus-article-buffer)
155   ;;                        (bufferp mh-sent-from-folder)
156   ;;                        )
157   ;;                   (save-excursion
158   ;;                     (set-buffer gnus-article-buffer)
159   ;;                     (if (eq major-mode 'mime-view-mode)
160   ;;                         mime-raw-buffer
161   ;;                       (current-buffer)
162   ;;                       )))
163   ;;                  ))))
164   (if (null article-buffer)
165       (emh-insert-mail
166        (emh::make-message mh-sent-from-folder mh-sent-from-msg))
167     ;; (insert-buffer article-buffer)
168     ;; (mime-edit-inserted-message-filter)
169     ;; )
170     ))
171
172 (defun emh-insert-mail (&optional message)
173   (save-excursion
174     (save-restriction
175       (let ((message-file
176              (emh::message/file-name (emh-query-message message))))
177         (narrow-to-region (point) (point))
178         (insert-file-contents message-file)
179         (push-mark (point-max))
180         (mime-edit-inserted-message-filter)
181     ))))
182
183 (set-alist 'mime-edit-message-inserter-alist
184            'mh-letter-mode (function emh-insert-message))
185 (set-alist 'mime-edit-mail-inserter-alist
186            'mh-letter-mode (function emh-insert-mail))
187 (set-alist 'mime-edit-mail-inserter-alist
188            'news-reply-mode (function emh-insert-mail))
189 (set-alist
190  'mime-edit-split-message-sender-alist
191  'mh-letter-mode
192  (function
193   (lambda (&optional arg)
194     (interactive "P")
195     (write-region (point-min) (point-max)
196                   mime-edit-draft-file-name nil 'no-message)
197     (cond (arg
198            (pop-to-buffer "MH mail delivery")
199            (erase-buffer)
200            (mh-exec-cmd-output mh-send-prog t "-watch" "-nopush"
201                                "-nodraftfolder"
202                                mh-send-args
203                                mime-edit-draft-file-name)
204            (goto-char (point-max))      ; show the interesting part
205            (recenter -1)
206            (sit-for 1))
207           (t
208            (apply 'mh-exec-cmd-quiet t mh-send-prog 
209                   (mh-list-to-string
210                    (list "-nopush" "-nodraftfolder"
211                          "-noverbose" "-nowatch"
212                          mh-send-args mime-edit-draft-file-name)))))
213     )))
214
215
216 ;;; @ commands using tm-edit features
217 ;;;
218
219 (defun emh-edit-again (msg)
220   "Clean-up a draft or a message previously sent and make it resendable.
221 Default is the current message.
222 The variable mh-new-draft-cleaned-headers specifies the headers to remove.
223 See also documentation for `\\[mh-send]' function."
224   (interactive (list (mh-get-msg-num t)))
225   (catch 'tag
226     (let* ((from-folder mh-current-folder)
227            (config (current-window-configuration))
228            (draft
229             (cond ((and mh-draft-folder (equal from-folder mh-draft-folder))
230                    (let ((name (format "draft-%d" msg)))
231                      (if (get-buffer name)
232                          (throw 'tag (pop-to-buffer name))
233                        )
234                      (let ((filename
235                             (mh-msg-filename msg mh-draft-folder)
236                             ))
237                        (set-buffer (get-buffer-create name))
238                        (binary-insert-file-contents filename)
239                        (setq buffer-file-name filename)
240                        )
241                      (pop-to-buffer name)
242                      (if (re-search-forward "^-+$" nil t)
243                          (replace-match "")
244                          )
245                      name))
246                   (t
247                    (let ((flag enable-multibyte-characters)
248                          (coding-system-for-read 'binary))
249                      (prog1
250                          (mh-read-draft "clean-up"
251                                         (mh-msg-filename msg) nil)
252                        (set-buffer-multibyte flag)
253                        ))
254                    ))))
255       (goto-char (point-min))
256       (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil)
257       (let ((cs (detect-coding-region (point-min)(point-max))))
258         (set-buffer-file-coding-system
259          (if (listp cs)
260              (car cs)
261            cs)))
262       (save-buffer)
263       (mime-edit-again nil 'no-separator 'not-turn-on)
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 emh-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 emh-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" emh-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 ((tag-regexp
354            (concat "^" (regexp-quote (mime-make-tag "message" "rfc822"))))
355           orig-from orig-subject multipart-flag)
356       (goto-char (point-min))
357       (save-excursion
358         (save-restriction
359           (goto-char (point-max))
360           (if (not (bolp)) (insert "\n"))
361           (let ((beg (point)))
362             (narrow-to-region beg beg)
363             (mh-exec-cmd-output "pick" nil folder msg-or-seq)
364             (if (> (count-lines (point) (point-max)) 1)
365                 (setq multipart-flag t)
366               )
367             (while (re-search-forward "^\\([0-9]+\\)\n" nil t)
368               (let ((forw-msg
369                      (buffer-substring (match-beginning 1) (match-end 1)))
370                     (beg (match-beginning 0))
371                     (end (match-end 0))
372                     )
373                 (save-restriction
374                   (narrow-to-region beg end)
375                   ;; modified for Emacs 18
376                   (delete-region beg end)
377                   (insert-file-contents
378                    (mh-expand-file-name forw-msg
379                                         (mh-expand-file-name folder))
380                    )
381                   (save-excursion
382                     (push-mark (point-max))
383                     (mime-edit-inserted-message-filter))
384                   (goto-char (point-max))
385                   )
386                 (save-excursion
387                   (goto-char beg)
388                   (mime-edit-insert-tag "message" "rfc822")
389                   )))
390             (delete-region (point) (point-max))
391             (if multipart-flag
392                 (mime-edit-enclose-digest-region beg (point))
393               ))))
394       (re-search-forward tag-regexp)
395       (forward-line 1)
396       (save-restriction
397         (narrow-to-region (point) (point-max))
398         (setq orig-from (eword-decode-string
399                          (mh-get-header-field "From:")))
400         (setq orig-subject (eword-decode-string
401                             (mh-get-header-field "Subject:")))
402         )
403       (let ((forw-subject
404              (mh-forwarded-letter-subject orig-from orig-subject)))
405         (mh-insert-fields "Subject:" forw-subject)
406         (goto-char (point-min))
407         (re-search-forward tag-regexp)
408         (forward-line -1)
409         (delete-other-windows)
410         (if (numberp msg-or-seq)
411             (mh-add-msgs-to-seq msg-or-seq 'forwarded t)
412           (mh-add-msgs-to-seq (mh-seq-to-msgs msg-or-seq) 'forwarded t))
413         (mh-compose-and-send-mail draft "" folder msg-or-seq
414                                   to forw-subject cc
415                                   mh-note-forw "Forwarded:"
416                                   config)))))
417
418 (cond ((not (featurep 'mh-utils))
419        (defun emh::insert-letter (folder number verbatim)
420          (mh-insert-letter verbatim folder number)
421          )
422        )
423       ((and (boundp 'mh-e-version)
424             (string-lessp mh-e-version "5"))
425        (defun emh::insert-letter (folder number verbatim)
426          (mh-insert-letter number folder verbatim)
427          )
428        )
429       (t
430        (defalias 'emh::insert-letter 'mh-insert-letter)
431        ))
432
433 (defun emh-insert-letter (verbatim)
434   "Interface to mh-insert-letter."
435   (interactive "P")
436   (let*
437       ((folder (mh-prompt-for-folder
438                 "Message from"
439                 (if (and (stringp mh-sent-from-folder)
440                          (string-match "^\\+" mh-sent-from-folder))
441                     mh-sent-from-folder "+inbox")
442                 nil))
443        (number (emh-prompt-for-message "Message number: " folder)))
444     (emh::insert-letter folder number verbatim)))
445
446 ;; (defun emh-yank-cur-msg-with-no-filter ()
447 ;;   "Insert the current message into the draft buffer.
448 ;; This function makes new show-buffer from article-buffer to disable
449 ;; variable `mime-preview-text/plain-hook'. If you don't want to use text
450 ;; filters for replying message, please set it to
451 ;; `emh-message-yank-function'.
452 ;; Prefix each non-blank line in the message with the string in
453 ;; `mh-ins-buf-prefix'. The entire message will be inserted if
454 ;; `mh-yank-from-start-of-msg' is non-nil. If this variable is nil, the
455 ;; portion of the message following the point will be yanked.  If
456 ;; `mh-delete-yanked-msg-window' is non-nil, any window displaying the
457 ;; yanked message will be deleted."
458 ;;   (interactive)
459 ;;   (if (and mh-sent-from-folder mh-sent-from-msg)
460 ;;       (let ((to-point (point))
461 ;;             (to-buffer (current-buffer)))
462 ;;         (set-buffer mh-sent-from-folder)
463 ;;         (if mh-delete-yanked-msg-window
464 ;;             (delete-windows-on mh-show-buffer))
465 ;;         (set-buffer mh-show-buffer)     ; Find displayed message
466 ;;         (let ((mh-ins-str
467 ;;                (if mime-raw-buffer
468 ;;                    (let (mime-display-text/plain-hook buf)
469 ;;                      (prog1
470 ;;                          (save-window-excursion
471 ;;                            (set-buffer mime-raw-buffer)
472 ;;                            (setq buf (mime-view-mode))
473 ;;                            (buffer-string)
474 ;;                            )
475 ;;                        (kill-buffer buf)
476 ;;                        ))
477 ;;                  (buffer-string)
478 ;;                  )))
479 ;;           (set-buffer to-buffer)
480 ;;           (save-restriction
481 ;;             (narrow-to-region to-point to-point)
482 ;;             (push-mark)
483 ;;             (insert mh-ins-str)
484 ;;             (mh-insert-prefix-string mh-ins-buf-prefix)
485 ;;             (insert "\n"))))
486 ;;     (error "There is no current message")))
487
488 ;; (defun emh-yank-current-message ()
489 ;;   "Insert the current message into the draft buffer.
490 ;; It uses variable `emh-message-yank-function'
491 ;; to select message yanking function."
492 ;;   (interactive)
493 ;;   (let ((mh-sent-from-folder mh-sent-from-folder)
494 ;;         (mh-sent-from-msg mh-sent-from-msg))
495 ;;     (if (and (not (stringp mh-sent-from-folder))
496 ;;              (boundp 'gnus-article-buffer)
497 ;;              (get-buffer gnus-article-buffer)
498 ;;              (bufferp mh-sent-from-folder)
499 ;;              ) ; might be called from GNUS
500 ;;         (if (boundp 'gnus-article-copy) ; might be sgnus
501 ;;             (save-excursion
502 ;;               (gnus-copy-article-buffer)
503 ;;               (setq mh-sent-from-folder gnus-article-copy)
504 ;;               (set-buffer mh-sent-from-folder)
505 ;;               (setq mh-show-buffer gnus-article-copy)
506 ;;               )
507 ;;           (save-excursion
508 ;;             (setq mh-sent-from-folder gnus-article-buffer)
509 ;;             (set-buffer gnus-article-buffer)
510 ;;             (setq mh-show-buffer (current-buffer))
511 ;;             )))
512 ;;     (funcall emh-message-yank-function)
513 ;;     ))
514
515 ;; (substitute-key-definition
516 ;;  'mh-yank-cur-msg 'emh-yank-current-message mh-letter-mode-map)
517 ;; (substitute-key-definition
518 ;;  'mh-insert-letter 'emh-insert-letter mh-letter-mode-map)
519
520
521 ;;; @ end
522 ;;;
523
524 (provide 'emh-comp)
525 (require 'emh)
526
527 ;;; emh-comp.el ends here