(wl-summary-incorporate): Enclose `wl-summary-sync-force-update'
[elisp/wanderlust.git] / elmo / mmelmo.el
1 ;;; mmelmo.el -- mm-backend by ELMO.
2
3 ;; Copyright 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
4
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Keywords: mail, net news
7 ;; Time-stamp: <00/03/07 17:00:43 teranisi>
8
9 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
10
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15 ;;
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20 ;;
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25 ;;
26
27 ;;; Commentary:
28 ;; 
29
30 ;;; Code:
31 ;; 
32
33 (require 'elmo-vars)
34 (require 'elmo-util)
35 (require 'mime-parse)
36 (require 'mmbuffer)
37
38 (defvar mmelmo-header-max-column fill-column
39   "*Inserted header is folded with this value.
40 If function is specified, its return value is used.")
41
42 (defvar mmelmo-header-inserted-hook nil
43   "*A hook called when header is inserted.")
44
45 (defvar mmelmo-entity-content-inserted-hook nil
46   "*A hook called when entity-content is inserted.")
47
48 (defun mmelmo-get-original-buffer ()
49   (save-excursion
50     (let (ret-val)
51       (if (setq ret-val 
52                 (get-buffer (concat mmelmo-entity-buffer-name "0")))
53           ret-val
54         (set-buffer (setq ret-val
55                           (get-buffer-create 
56                            (concat mmelmo-entity-buffer-name "0"))))
57         (mmelmo-original-mode)
58         ret-val))))
59
60 (defun mmelmo-cleanup-entity-buffers ()
61   "Cleanup entity buffers of mmelmo."
62   (mapcar (lambda (x)
63             (if (string-match mmelmo-entity-buffer-name x)
64                 (kill-buffer x)))
65           (mapcar 'buffer-name (buffer-list))))
66
67 (defun mmelmo-insert-sorted-header-from-buffer (buffer 
68                                                 start end
69                                                 &optional invisible-fields
70                                                 visible-fields
71                                                 sorted-fields)
72   (let ((the-buf (current-buffer))
73         (mode-obj (mime-find-field-presentation-method 'wide))
74         field-decoder
75         f-b p f-e field-name field field-body
76         vf-alist (sl sorted-fields))
77     (save-excursion
78       (set-buffer buffer)
79       (save-restriction
80         (narrow-to-region start end)
81         (goto-char start)
82         (while (re-search-forward std11-field-head-regexp nil t)
83           (setq f-b (match-beginning 0)
84                 p (match-end 0)
85                 field-name (buffer-substring f-b p)
86                 f-e (std11-field-end))
87           (when (mime-visible-field-p field-name
88                                       visible-fields invisible-fields)
89             (setq field (intern
90                          (capitalize (buffer-substring f-b (1- p))))
91                   field-body (buffer-substring p f-e)
92                   field-decoder (inline (mime-find-field-decoder-internal
93                                          field mode-obj)))
94             (setq vf-alist (append (list
95                                     (cons field-name
96                                           (list field-body field-decoder)))
97                                    vf-alist))))
98         (and vf-alist
99              (setq vf-alist
100                    (sort vf-alist
101                          (function (lambda (s d)
102                                      (let ((n 0) re
103                                            (sf (car s))
104                                            (df (car d)))
105                                        (catch 'done
106                                          (while (setq re (nth n sl))
107                                            (setq n (1+ n))
108                                            (and (string-match re sf)
109                                                 (throw 'done t))
110                                            (and (string-match re df)
111                                                 (throw 'done nil)))
112                                          t)))))))
113         (with-current-buffer the-buf
114           (while vf-alist
115             (let* ((vf (car vf-alist))
116                    (field-name (car vf))
117                    (field-body (car (cdr vf)))
118                    (field-decoder (car (cdr (cdr vf)))))
119               (insert field-name)
120               (insert (if field-decoder
121                           (funcall field-decoder field-body
122                                    (string-width field-name)
123                                    (if (functionp mmelmo-header-max-column)
124                                        (funcall mmelmo-header-max-column)
125                                      mmelmo-header-max-column))
126                         ;; Don't decode
127                         field-body))
128               (insert "\n"))
129             (setq vf-alist (cdr vf-alist)))
130           (run-hooks 'mmelmo-header-inserted-hook))))))
131
132 (defun mmelmo-original-mode ()
133   (setq major-mode 'mmelmo-original-mode)
134   (setq buffer-read-only t)
135   (elmo-set-buffer-multibyte nil)
136   (setq mode-name "MMELMO-Original"))
137
138 ;; For FLIMs without rfc2231 feature .
139 (if (not (fboundp 'mime-parse-parameters-from-list))
140     (defun mime-parse-parameters-from-list (attrlist)
141       (let (ret-val)
142         (if (not (eq (% (length attrlist) 2) 0))
143             (message "Invalid attributes."))
144         (while attrlist
145           (setq ret-val (append ret-val
146                                 (list (cons (downcase (car attrlist))
147                                             (downcase (car (cdr attrlist)))))))
148           (setq attrlist (cdr (cdr attrlist))))
149         ret-val)))
150
151 (provide 'mmelmo) ; for circular dependency.
152 (static-if (fboundp 'luna-define-method)
153     ;; FLIM 1.13 or later
154     (require 'mmelmo-2)
155   ;; FLIM 1.12
156   (require 'mmelmo-1))
157
158
159 ;;; mmelmo.el ends here