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