tm 7.27.
[elisp/tm.git] / mh-e / tm-mh-e.el
1 ;;;
2 ;;; tm-mh-e.el --- MIME extender for mh-e
3 ;;;
4 ;;; Copyright (C) 1995 Free Software Foundation, Inc.
5 ;;; Copyright (C) 1993,1994,1995 MORIOKA Tomohiko
6 ;;;
7 ;;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;;;         OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
9 ;;; modified by YAMAOKA Katsumi <yamaoka@ga.sony.co.jp>
10 ;;; Keywords: mail, MH, MIME, multimedia, encoded-word, multilingual
11 ;;;
12 ;;; This file is part of tm (Tools for MIME).
13 ;;;
14
15 ;;; @ require modules
16 ;;;
17
18 (require 'tl-str)
19 (require 'tl-misc)
20 (require 'mh-e)
21 (if (not (boundp 'mh-e-version))
22     (require 'tm-mh-e3)
23   )
24 (require 'tm-view)
25
26
27 ;;; @ version
28 ;;;
29
30 (defconst tm-mh-e/RCS-ID
31   "$Id: tm-mh-e.el,v 7.25 1995/11/19 06:44:32 morioka Exp $")
32
33 (defconst tm-mh-e/version (get-version-string tm-mh-e/RCS-ID))
34
35
36 ;;; @ variable
37 ;;;
38
39 (defvar tm-mh-e/automatic-mime-preview t
40   "If non-nil, show MIME processed message.")
41
42 (defvar tm-mh-e/decode-encoded-word t
43   "If non-nil, decode encoded-word when it is not MIME preview mode.")
44
45 (defvar tm-mh-e/use-forwcomps nil)
46 (defvar tm-mh-e/forwcomps "forwcomps")
47
48
49 ;;; @ functions
50 ;;;
51
52 (if (not (fboundp 'tm-mh-e/original-mh-display-msg))
53     (fset 'tm-mh-e/original-mh-display-msg
54           (symbol-function 'mh-display-msg))
55   )
56
57 (defun mh-display-msg (msg-num folder &optional show-buffer mode)
58   (or mode
59       (setq mode tm-mh-e/automatic-mime-preview)
60       )
61   ;; Display message NUMBER of FOLDER.
62   ;; Sets the current buffer to the show buffer.
63   (set-buffer folder)
64   (or show-buffer
65       (setq show-buffer mh-show-buffer))
66   ;; Bind variables in folder buffer in case they are local
67   (let ((msg-filename (mh-msg-filename msg-num)))
68     (if (not (file-exists-p msg-filename))
69         (error "Message %d does not exist" msg-num))
70     (set-buffer show-buffer)
71     (cond ((not (equal msg-filename buffer-file-name))
72            ;; Buffer does not yet contain message.
73            (clear-visited-file-modtime)
74            (unlock-buffer)
75            (setq buffer-file-name nil)  ; no locking during setup
76            (setq buffer-read-only nil)
77            (erase-buffer)
78            (if mode
79                (let* ((aname (concat "article-" folder))
80                       (abuf (get-buffer aname))
81                       )
82                  (if abuf
83                      (progn
84                        (set-buffer abuf)
85                        (setq buffer-read-only nil)
86                        (erase-buffer)
87                        )
88                    (setq abuf (get-buffer-create aname))
89                    (set-buffer abuf)
90                    )
91                  (let ((file-coding-system-for-read
92                         (if (boundp 'MULE) *noconv*))
93                        kanji-fileio-code)
94                    (insert-file-contents msg-filename)
95                    ;; (goto-char (point-min))
96                    (while (re-search-forward "\r$" nil t)
97                      (replace-match "")
98                      )
99                    )
100                  (set-buffer-modified-p nil)
101                  (setq buffer-read-only t)
102                  (mh-show-mode)
103                  (mime/viewer-mode nil nil nil
104                                    aname (concat "show-" folder))
105                  (goto-char (point-min))
106                  )
107              (let ((clean-message-header mh-clean-message-header)
108                    (invisible-headers mh-invisible-headers)
109                    (visible-headers mh-visible-headers)
110                    )
111                ;; 1995/9/21
112                ;;   modified by ARIURA <ariura@cc.tuat.ac.jp>
113                ;;   to support mhl.
114                (if mhl-formfile
115                    (mh-exec-lib-cmd-output "mhl" "-nobell" "-noclear"
116                                            (if (stringp mhl-formfile)
117                                                (list "-form" mhl-formfile))
118                                            msg-filename)
119                  (insert-file-contents msg-filename))
120                ;; end
121                (goto-char (point-min))
122                (cond (clean-message-header
123                       (mh-clean-msg-header (point-min)
124                                            invisible-headers
125                                            visible-headers)
126                       (goto-char (point-min)))
127                      (t
128                       (mh-start-of-uncleaned-message)))
129                (if tm-mh-e/decode-encoded-word
130                    (mime/decode-message-header)
131                  )
132                (set-buffer-modified-p nil)
133                (setq buffer-read-only t)
134                (mh-show-mode)
135                ))
136            (or (eq buffer-undo-list t)  ;don't save undo info for prev msgs
137                (setq buffer-undo-list nil))
138            (setq buffer-file-name msg-filename)
139            (set-mark nil)
140            (setq mode-line-buffer-identification
141                  (list (format mh-show-buffer-mode-line-buffer-id
142                                folder msg-num)))
143            (set-buffer folder)
144            (setq mh-showing-with-headers nil)))))
145
146 (defun tm-mh-e/view-message (&optional msg)
147   "MIME decode and play this message."
148   (interactive)
149   (if (or (null tm-mh-e/automatic-mime-preview)
150           (null (get-buffer mh-show-buffer))
151           (save-excursion
152             (set-buffer mh-show-buffer)
153             (not (eq major-mode 'mime/viewer-mode))
154             ))
155       (let ((tm-mh-e/automatic-mime-preview t))
156         (mh-invalidate-show-buffer)
157         (mh-show-msg msg)
158         ))
159   (pop-to-buffer mh-show-buffer)
160   )
161
162 (defun tm-mh-e/toggle-decoding-mode (arg)
163   "Toggle MIME processing mode.
164 With arg, turn MIME processing on if arg is positive."
165   (interactive "P")
166   (setq tm-mh-e/automatic-mime-preview
167         (if (null arg)
168             (not tm-mh-e/automatic-mime-preview)
169           arg))
170   (save-excursion
171     (set-buffer mh-show-buffer)
172     (if (null tm-mh-e/automatic-mime-preview)
173         (if (and mime::preview/article-buffer
174                  (get-buffer mime::preview/article-buffer))
175             (kill-buffer mime::preview/article-buffer)
176           )))
177   (mh-invalidate-show-buffer)
178   (mh-show (mh-get-msg-num t))
179   )
180
181 (defun tm-mh-e/show (&optional message)
182   (interactive)
183   (mh-invalidate-show-buffer)
184   (mh-show message)
185   )
186
187 (defun tm-mh-e/header-display ()
188   (interactive)
189   (mh-invalidate-show-buffer)
190   (let (mime-viewer/ignored-field-list
191         tm-mh-e/decode-encoded-word)
192     (mh-header-display)
193     ))
194
195 (defun tm-mh-e/raw-display ()
196   (interactive)
197   (mh-invalidate-show-buffer)
198   (let (tm-mh-e/automatic-mime-preview
199         tm-mh-e/decode-encoded-word)
200     (mh-header-display)
201     ))
202
203
204 ;;; @ for tm-view
205 ;;;
206
207 (fset 'tm-mh-e/code-convert-region-to-emacs
208       (symbol-function 'mime/code-convert-region-to-emacs))
209
210 (defun tm-mh-e/content-header-filter ()
211   (goto-char (point-min))
212   (while (and (re-search-forward mime-viewer/ignored-field-regexp nil t)
213               (progn
214                 (delete-region
215                  (match-beginning 0)
216                  (save-excursion
217                    (and
218                     (re-search-forward "^\\([^ \t]\\|$\\)" nil t)
219                     (match-beginning 0)
220                     )))
221                 t)))
222   (tm-mh-e/code-convert-region-to-emacs (point-min)(point-max)
223                                         mime/default-coding-system)
224   (mime/decode-message-header)
225   (if (featurep 'hilit19)
226       (hilit-rehighlight-buffer-quietly)
227     )
228   )
229
230 (defun tm-mh-e/quitting-method ()
231   (let ((win (get-buffer-window
232               mime/output-buffer-name))
233         (buf (current-buffer))
234         )
235     (if win
236         (delete-window win)
237       )
238     (pop-to-buffer
239      (let ((name (buffer-name buf)))
240        (substring name 5)
241        ))
242     (if (not tm-mh-e/automatic-mime-preview)
243         (mh-invalidate-show-buffer)
244       )
245     (mh-show (mh-get-msg-num t))
246     ))
247
248
249 ;;; @ for tm-partial
250 ;;;
251
252 (call-after-loaded
253  'tm-partial
254  (function
255   (lambda ()
256     (set-atype 'mime/content-decoding-condition
257                '((type . "message/partial")
258                  (method . mime-article/grab-message/partials)
259                  (major-mode . mh-show-mode)
260                  (summary-buffer-exp
261                   . (and (or (string-match "^article-\\(.+\\)$" article-buffer)
262                              (string-match "^show-\\(.+\\)$" article-buffer))
263                          (substring article-buffer
264                                     (match-beginning 1) (match-end 1))
265                          ))
266                  ))
267     (set-alist 'tm-partial/preview-article-method-alist
268                'mh-show-mode
269                (function
270                 (lambda ()
271                   (let ((tm-mh-e/automatic-mime-preview t))
272                     (tm-mh-e/show)
273                     ))))
274     )))
275
276
277 ;;; @ for tm-edit
278 ;;;
279
280 (defun tm-mh-e::make-message (folder number)
281   (vector folder number)
282   )
283
284 (defun tm-mh-e::message/folder (message)
285   (elt message 0)
286   )
287
288 (defun tm-mh-e::message/number (message)
289   (elt message 1)
290   )
291
292 (defun tm-mh-e::message/file-name (message)
293   (expand-file-name
294    (tm-mh-e::message/number message)
295    (mh-expand-file-name (tm-mh-e::message/folder message))
296    ))
297
298 ;;; modified by OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
299 ;;;     1995/11/14 (cf. [tm-ja:1096])
300 (defun tm-mh-e/prompt-for-message (prompt folder &optional default)
301   (let* ((files
302           (directory-files (mh-expand-file-name folder) nil "^[0-9]+$")
303           )
304          (folder-buf (get-buffer folder))
305          (default
306            (if folder-buf
307                (save-excursion
308                  (set-buffer folder-buf)
309                  (let ((show-buffer (get-buffer mh-show-buffer)))
310                    (if show-buffer
311                        (file-name-nondirectory
312                         (buffer-file-name show-buffer))
313                      ))))))
314     (if (or (null default)
315             (not (string-match "^[0-9]+$" default)))
316         (setq default
317               (if (string= folder mh-sent-from-folder)
318                   (int-to-string mh-sent-from-msg)
319                 (car files)
320                 )))
321     (completing-read prompt
322                      (let ((i 0))
323                        (mapcar (function
324                                 (lambda (file)
325                                   (setq i (+ i 1))
326                                   (list file i)
327                                   ))
328                                files)
329                        ) nil nil default)
330     ))
331
332 (defun tm-mh-e/query-message ()
333   (let* ((folder (mh-prompt-for-folder
334                   "Message from" (or mh-sent-from-folder "+inbox") nil))
335          (number (tm-mh-e/prompt-for-message "Message number: " folder))
336          )
337     (tm-mh-e::make-message folder number)
338     ))
339 ;;; end
340
341 ;;; by OKABE Yasuo <okabe@kudpc.kyoto-u.ac.jp>
342 ;;;     1995/11/14 (cf. [tm-ja:1099])
343 (defun tm-mh-e/forward (to cc &optional msg-or-seq)
344   "Forward a message or message sequence as MIME multipart/digest.
345 Defaults to displayed message. If optional prefix argument provided,
346 then prompt for the message sequence. See also documentation for
347 `\\[mh-send]' function."
348   (interactive (progn
349                  (require 'mh-comp)
350                  (list (mh-read-address "To: ")
351                        (mh-read-address "Cc: ")
352                        (if current-prefix-arg
353                            (mh-read-seq-default "Forward" t)
354                          (mh-get-msg-num t)
355                          ))))
356   (or msg-or-seq
357       (setq msg-or-seq (mh-get-msg-num t)))
358   (if (numberp msg-or-seq)
359       (setq msg-or-seq (int-to-string msg-or-seq)))
360   (let* ((folder mh-current-folder)
361          (config (current-window-configuration))
362          ;; use "draft" for compatibility with forw.
363          ;; forw always leaves file in "draft" since it doesn't have -draft
364          (draft-name (expand-file-name "draft" mh-user-path))
365          (draft (cond ((or (not (file-exists-p draft-name))
366                            (y-or-n-p "The file `draft' exists.  Discard it? "))
367                        (if tm-mh-e/use-forwcomps
368                            (mh-exec-cmd "comp"
369                                         "-noedit" "-nowhatnowproc"
370                                         "-form" tm-mh-e/forwcomps
371                                         "-nodraftfolder")
372                          (mh-exec-cmd "comp"
373                                       "-noedit" "-nowhatnowproc"
374                                       "-nodraftfolder")
375                          )
376                        (prog1
377                            (mh-read-draft "" draft-name t)
378                          (mh-insert-fields "To:" to "Cc:" cc)
379                          (set-buffer-modified-p nil)))
380                       (t
381                        (mh-read-draft "" draft-name nil)))))
382     (let (orig-from orig-subject)
383       (require 'tm-edit)
384       (goto-char (point-min))
385       (save-excursion
386         (save-restriction
387           (re-search-forward "^-*\n")
388           ;; modified by Katsumi Yamaoka <yamaoka@ga.sony.co.jp>
389           ;;    1995/11/17 (cf.[tm-ja:1116])
390           (and (< (point) (point-max))
391                (not (re-search-forward "^[\t ]*\n" nil t))
392                (goto-char (point-max))
393                (insert "\n"))
394           (insert "--<<digest>>-{\n")
395           (mh-exec-cmd-output "pick" nil folder msg-or-seq)
396           (narrow-to-region (point) (mark t))
397           (while (re-search-forward "^\\([0-9]+\\)\n" nil t)
398             (let ((forw-msg
399                    (buffer-substring (match-beginning 1) (match-end 1))))
400               (replace-match "--[[message/rfc822]]\n" nil nil)
401               (insert-file (mh-expand-file-name
402                             forw-msg (mh-expand-file-name folder)))
403               (if (not (bolp)) (insert "\n"))
404               (mime-editor/inserted-message-filter))
405             (goto-char (mark t)))
406           (insert-string "--}-<<digest>>")))
407       (re-search-forward "^--\\[\\[message/rfc822\\]")
408       (forward-line 1)
409       (save-restriction
410         (narrow-to-region (point) (point-max))
411         (setq orig-from (mh-get-header-field "From:"))
412         (setq orig-subject (mh-get-header-field "Subject:")))
413       (let ((forw-subject
414              (mh-forwarded-letter-subject orig-from orig-subject)))
415         (mh-insert-fields "Subject:" forw-subject)
416         (goto-char (point-min))
417         (re-search-forward "^--\\[\\[message/rfc822\\]")
418         (forward-line -1)
419         (delete-other-windows)
420         (if (numberp msg-or-seq)
421             (mh-add-msgs-to-seq msg-or-seq 'forwarded t)
422           (mh-add-msgs-to-seq (mh-seq-to-msgs msg-or-seq) 'forwarded t))
423         (mh-compose-and-send-mail draft "" folder msg-or-seq
424                                   to forw-subject cc
425                                   mh-note-forw "Forwarded:"
426                                   config)))))
427 ;;; end
428
429 (defun tm-mh-e/insert-message (&optional message)
430   (if (null message)
431       (setq message (tm-mh-e/query-message))
432     )
433   (insert-file (tm-mh-e::message/file-name message))
434   (mime-editor/inserted-message-filter)
435   )
436
437 (call-after-loaded
438  'tm-edit
439  (function
440   (lambda ()
441     (set-alist
442      'mime-editor/message-inserter-alist
443      'mh-letter-mode (function tm-mh-e/insert-message))
444     (set-alist
445      'mime-editor/mail-inserter-alist
446      'mh-letter-mode (function tm-mh-e/insert-message))
447     (set-alist
448      'mime-editor/mail-inserter-alist
449      'news-reply-mode (function tm-mh-e/insert-message))
450     )))
451
452 (call-after-loaded
453  'mime-setup
454  (function
455   (lambda ()
456     (substitute-key-definition
457      'mh-forward 'tm-mh-e/forward mh-folder-mode-map)
458     )))
459
460
461 ;;; @ set up
462 ;;;
463
464 (define-key mh-folder-mode-map "v" (function tm-mh-e/view-message))
465 (define-key mh-folder-mode-map "\et" (function tm-mh-e/toggle-decoding-mode))
466 (define-key mh-folder-mode-map "." (function tm-mh-e/show))
467 (define-key mh-folder-mode-map "," (function tm-mh-e/header-display))
468 (define-key mh-folder-mode-map "\e," (function tm-mh-e/raw-display))
469 (define-key mh-folder-mode-map "\r"
470   (function (lambda ()
471               (interactive)
472               (scroll-other-window 1)
473               )))
474 (define-key mh-folder-mode-map "\e\r"
475   (function (lambda ()
476               (interactive)
477               (scroll-other-window -1)
478               )))
479
480 (defun tm-mh-e/summary-before-quit ()
481   (let ((buf (get-buffer mh-show-buffer)))
482     (if buf
483         (let ((the-buf (current-buffer)))
484           (switch-to-buffer buf)
485           (if (and mime::article/preview-buffer
486                    (setq buf (get-buffer mime::article/preview-buffer))
487                    )
488               (progn
489                 (switch-to-buffer the-buf)
490                 (kill-buffer buf)
491                 )
492             (switch-to-buffer the-buf)
493             )
494           ))))
495
496 (add-hook 'mh-before-quit-hook (function tm-mh-e/summary-before-quit))
497              
498 (set-alist 'mime-viewer/quitting-method-alist
499            'mh-show-mode
500            (function tm-mh-e/quitting-method))
501
502 (set-alist 'mime-viewer/content-header-filter-alist
503            'mh-show-mode
504            (function tm-mh-e/content-header-filter))
505
506 (set-alist 'mime-viewer/code-converter-alist
507            'mh-show-mode
508            (function tm-mh-e/code-convert-region-to-emacs))
509
510
511 ;;; @ end
512 ;;;
513
514 (provide 'tm-mh-e)
515
516 (run-hooks 'tm-mh-e-load-hook)