Sync with the main trunk.
[elisp/emh.git] / emh.el
1 ;;; emh.el --- MIME extender for mh-e
2
3 ;; Copyright (C) 1995,1996,1997,1998,2000 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: 1993/11/21
9 ;;      Renamed: 1993/11/27 from mh-e-mime.el
10 ;;      Renamed: 1997/02/21 from tm-mh-e.el
11 ;; Keywords: MH, MIME, multimedia, encoded-word, multilingual, 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-e)
33 (require 'alist)
34 (require 'mime-view)
35
36
37 ;;; @ version
38 ;;;
39
40 (defconst emh-version "1.13.0")
41
42
43 ;;; @ variable
44 ;;;
45
46 (defgroup emh nil
47   "MIME Extension for mh-e"
48   :group 'mime
49   :group 'mh)
50
51 (defcustom emh-automatic-mime-preview t
52   "*If non-nil, show MIME processed message."
53   :group 'emh
54   :type 'boolean)
55
56 (defcustom emh-decode-encoded-word t
57   "*If non-nil, decode encoded-word when it is not MIME preview mode."
58   :group 'emh
59   :type 'boolean)
60
61
62 ;;; @ functions
63 ;;;
64
65 (defsubst emh-raw-buffer (folder-buffer)
66   (concat "article-" (if (bufferp folder-buffer)
67                          (buffer-name folder-buffer)
68                        folder-buffer)))
69
70 (defun mh-display-msg (msg-num folder &optional show-buffer mode)
71   "Display message number MSG-NUM of FOLDER.
72 This function uses `mime-view-mode' if MODE is not nil.  If MODE is
73 nil, `emh-automatic-mime-preview' is used as default value."
74   (or mode
75       (setq mode emh-automatic-mime-preview)
76       )
77   ;; Display message NUMBER of FOLDER.
78   ;; Sets the current buffer to the show buffer.
79   (set-buffer folder)
80   (or show-buffer
81       (setq show-buffer mh-show-buffer))
82   ;; Bind variables in folder buffer in case they are local
83   (let ((msg-filename (mh-msg-filename msg-num)))
84     (if (not (file-exists-p msg-filename))
85         (error "Message %d does not exist" msg-num))
86     (set-buffer show-buffer)
87     (cond ((not (equal msg-filename buffer-file-name))
88            ;; Buffer does not yet contain message.
89            (clear-visited-file-modtime)
90            (unlock-buffer)
91            (setq buffer-file-name nil)  ; no locking during setup
92            (setq buffer-read-only nil)
93            (erase-buffer)
94            (if mode
95                (let* ((aname (emh-raw-buffer folder))
96                       (abuf (get-buffer aname)))
97                  (if abuf
98                      (progn
99                        (set-buffer abuf)
100                        (setq buffer-read-only nil)
101                        (erase-buffer)
102                        )
103                    (setq abuf (get-buffer-create aname))
104                    (set-buffer abuf)
105                    (set-buffer-multibyte nil)
106                    )
107                  (insert-file-contents-as-raw-text msg-filename)
108                  (set-buffer-modified-p nil)
109                  (setq buffer-read-only t)
110                  (setq buffer-file-name msg-filename)
111                  (mh-show-mode)
112                  (mime-display-message (mime-open-entity 'buffer aname)
113                                        (concat "show-" folder))
114                  (goto-char (point-min))
115                  )
116              (let ((clean-message-header mh-clean-message-header)
117                    (invisible-headers mh-invisible-headers)
118                    (visible-headers mh-visible-headers))
119                ;; 1995/9/21
120                ;;   modified by ARIURA <ariura@cc.tuat.ac.jp>
121                ;;   to support mhl.
122                (if mhl-formfile
123                    (mh-exec-lib-cmd-output "mhl" "-nobell" "-noclear"
124                                            (if (stringp mhl-formfile)
125                                                (list "-form" mhl-formfile))
126                                            msg-filename)
127                  (insert-file-contents msg-filename))
128                ;; end
129                (goto-char (point-min))
130                (cond (clean-message-header
131                       (mh-clean-msg-header (point-min)
132                                            invisible-headers
133                                            visible-headers)
134                       (goto-char (point-min)))
135                      (t
136                       (mh-start-of-uncleaned-message)))
137                (if emh-decode-encoded-word
138                    (eword-decode-header)
139                  )
140                (set-buffer-modified-p nil)
141                (setq buffer-read-only t)
142                (setq buffer-file-name msg-filename)
143                (mh-show-mode)
144                ))
145            (or (eq buffer-undo-list t)  ;don't save undo info for prev msgs
146                (setq buffer-undo-list nil))
147 ;;; Added by itokon (02/19/96)
148            (setq buffer-file-name msg-filename)
149 ;;;
150            (set-mark nil)
151            (setq mode-line-buffer-identification
152                  (list (format mh-show-buffer-mode-line-buffer-id
153                                folder msg-num)))
154            (set-buffer folder)
155            (setq mh-showing-with-headers nil)))))
156
157 (defun emh-view-message (&optional msg)
158   "MIME decode and play this message."
159   (interactive)
160   (if (or (null emh-automatic-mime-preview)
161           (null (get-buffer mh-show-buffer))
162           (save-excursion
163             (set-buffer mh-show-buffer)
164             (not (eq major-mode 'mime-view-mode))
165             ))
166       (let ((emh-automatic-mime-preview t))
167         (mh-invalidate-show-buffer)
168         (mh-show-msg msg)
169         ))
170   (pop-to-buffer mh-show-buffer)
171   )
172
173 (defun emh-toggle-decoding-mode (arg)
174   "Toggle MIME processing mode.
175 With arg, turn MIME processing on if arg is positive."
176   (interactive "P")
177   (setq emh-automatic-mime-preview
178         (if (null arg)
179             (not emh-automatic-mime-preview)
180           arg))
181   (let ((raw-buffer (emh-raw-buffer (current-buffer))))
182     (if (get-buffer raw-buffer)
183         (kill-buffer raw-buffer)
184       ))
185   (mh-invalidate-show-buffer)
186   (mh-show (mh-get-msg-num t))
187   )
188
189 (defun emh-show (&optional message)
190   (interactive)
191   (mh-invalidate-show-buffer)
192   (mh-show message)
193   )
194
195 (defun emh-header-display ()
196   (interactive)
197   (mh-invalidate-show-buffer)
198   (let (mime-view-ignored-field-list
199         mime-view-visible-field-list
200         emh-decode-encoded-word)
201     (mh-header-display)
202     ))
203
204 (defun emh-raw-display ()
205   (interactive)
206   (mh-invalidate-show-buffer)
207   (let (emh-automatic-mime-preview
208         emh-decode-encoded-word)
209     (mh-header-display)
210     ))
211
212 (defun emh-burst-multipart/digest ()
213   "Burst apart the current message, which should be a multipart/digest.
214 The message is replaced by its table of contents and the letters from the
215 digest are inserted into the folder after that message."
216   (interactive)
217   (let ((digest (mh-get-msg-num t)))
218     (mh-process-or-undo-commands mh-current-folder)
219     (mh-set-folder-modified-p t)                ; lock folder while bursting
220     (message "Bursting digest...")
221     (mh-exec-cmd "mhn" "-store" mh-current-folder digest)
222     (mh-scan-folder mh-current-folder (format "%d-last" mh-first-msg-num))
223     (message "Bursting digest...done")
224     ))
225
226
227 ;;; @ for mime-view
228 ;;;
229
230 (defvar emh-display-header-hook (if window-system '(emh-highlight-header))
231   "Hook for header filtering.")
232
233 (autoload 'emh-highlight-header "emh-face")
234
235 (defun emh-header-presentation-method (entity situation)
236   (mime-insert-header entity
237                       mime-view-ignored-field-list
238                       mime-view-visible-field-list)
239   (run-hooks 'emh-display-header-hook)
240   )
241
242 (set-alist 'mime-header-presentation-method-alist
243            'mh-show-mode #'emh-header-presentation-method)
244
245
246 (defun emh-quitting-method ()
247   (let ((buf (current-buffer)))
248     (mime-maybe-hide-echo-buffer)
249     (pop-to-buffer
250      (let ((name (buffer-name buf)))
251        (substring name 5)
252        ))
253     (if (not emh-automatic-mime-preview)
254         (mh-invalidate-show-buffer)
255       )
256     (mh-show (mh-get-msg-num t))
257     ))
258
259 (set-alist 'mime-preview-quitting-method-alist
260            'mh-show-mode #'emh-quitting-method)
261
262
263 (defun emh-following-method (buf)
264   (save-excursion
265     (set-buffer buf)
266     (goto-char (point-max))
267     (setq mh-show-buffer buf)
268     (apply (function mh-send)
269            (std11-field-bodies '("From" "cc" "Subject") ""))
270     (setq mh-sent-from-folder buf)
271     (setq mh-sent-from-msg 1)
272     (let ((last (point)))
273       (mh-yank-cur-msg)
274       (goto-char last)
275       )))
276
277 (set-alist 'mime-preview-following-method-alist
278            'mh-show-mode #'emh-following-method)
279
280
281 ;;; @@ for mime-partial
282 ;;;
283
284 (defun emh-request-partial-message ()
285   (let ((msg-filename (mh-msg-filename (mh-get-msg-num t)))
286         (show-buffer mh-show-buffer))
287     (set-buffer (get-buffer-create " *Partial Article*"))
288     (erase-buffer)
289     (setq mime-preview-buffer show-buffer)
290     (insert-file-contents-as-raw-text msg-filename)
291     (mime-parse-buffer)
292     ))
293
294 (defun emh-get-folder-buffer ()
295   (let ((buffer-name (buffer-name (current-buffer))))
296     (and (or (string-match "^article-\\(.+\\)$" buffer-name)
297              (string-match "^show-\\(.+\\)$" buffer-name))
298          (substring buffer-name
299                     (match-beginning 1) (match-end 1))
300          )))
301
302 (autoload 'mime-combine-message/partial-pieces-automatically
303   "mime-partial"
304   "Internal method to combine message/partial messages automatically.")
305
306 (mime-add-condition
307  'action
308  '((type . message)(subtype . partial)
309    (major-mode . mh-show-mode)
310    (method . mime-combine-message/partial-pieces-automatically)
311    (summary-buffer-exp . (emh-get-folder-buffer))
312    (request-partial-message-method . emh-request-partial-message)
313    ))
314
315
316 ;;; @ set up
317 ;;;
318
319 (define-key mh-folder-mode-map "v" (function emh-view-message))
320 (define-key mh-folder-mode-map "\et" (function emh-toggle-decoding-mode))
321 (define-key mh-folder-mode-map "." (function emh-show))
322 (define-key mh-folder-mode-map "," (function emh-header-display))
323 (define-key mh-folder-mode-map "\e," (function emh-raw-display))
324 (define-key mh-folder-mode-map "\C-c\C-b"
325   (function emh-burst-multipart/digest))
326
327 (defun emh-summary-before-quit ()
328   (let ((buf (get-buffer mh-show-buffer)))
329     (if buf
330         (let ((the-buf (current-buffer)))
331           (switch-to-buffer buf)
332           (if (and mime-preview-buffer
333                    (setq buf (get-buffer mime-preview-buffer))
334                    )
335               (progn
336                 (switch-to-buffer the-buf)
337                 (kill-buffer buf)
338                 )
339             (switch-to-buffer the-buf)
340             )
341           ))))
342
343 (add-hook 'mh-before-quit-hook (function emh-summary-before-quit))
344
345
346 ;;; @ for BBDB
347 ;;;
348
349 (eval-after-load "bbdb" '(require 'mime-bbdb))
350
351
352 ;;; @ end
353 ;;;
354
355 (provide 'emh)
356
357 (run-hooks 'emh-load-hook)
358
359 ;;; emh.el ends here