1 ;;; eword-decode.el --- RFC 2047 based encoded-word decoder for GNU Emacs
3 ;; Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
5 ;; Author: ENAMI Tsugutomo <enami@sys.ptg.sony.co.jp>
6 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; Tanaka Akira <akr@jaist.ac.jp>
8 ;; Maintainer: Tanaka Akira <akr@jaist.ac.jp>
10 ;; Original: 1992/07/20 ENAMI Tsugutomo's `mime.el'.
11 ;; Renamed: 1993/06/03 to tiny-mime.el
12 ;; Renamed: 1995/10/03 from tiny-mime.el (split off encoder)
13 ;; Renamed: 1997/02/22 from tm-ew-d.el
14 ;; Keywords: encoded-word, MIME, multilingual, header, mail, news
16 ;; This file is part of FLAM (Faithful Library About MIME).
18 ;; This program is free software; you can redistribute it and/or
19 ;; modify it under the terms of the GNU General Public License as
20 ;; published by the Free Software Foundation; either version 2, or (at
21 ;; your option) any later version.
23 ;; This program is distributed in the hope that it will be useful, but
24 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 ;; General Public License for more details.
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with GNU Emacs; see the file COPYING. If not, write to the
30 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
31 ;; Boston, MA 02111-1307, USA.
42 (eval-when-compile (require 'cl))
44 (defgroup eword-decode nil
45 "Encoded-word decoding"
50 (defvar rotate-memo nil)
51 (defmacro rotate-memo (var val)
53 (unless (boundp ',var) (setq ,var ()))
54 (setq ,var (cons ,val ,var))
55 (let ((tmp (last ,var (- (length ,var) 100))))
56 (when tmp (setcdr tmp nil)))
62 (defcustom eword-decode-sticked-encoded-word nil
63 "*If non-nil, decode encoded-words sticked on atoms,
64 other encoded-words, etc.
65 however this behaviour violates RFC2047."
69 (defcustom eword-decode-quoted-encoded-word nil
70 "*If non-nil, decode encoded-words in quoted-string
71 however this behaviour violates RFC2047."
75 (defcustom eword-max-size-to-decode 1000
76 "*Max size to decode header field."
78 :type '(choice (integer :tag "Limit (bytes)")
79 (const :tag "Don't limit" nil)))
82 ;;; @ MIME encoded-word definition
85 (defconst eword-encoded-word-prefix-regexp
86 (concat (regexp-quote "=?")
87 "\\(" mime-charset-regexp "\\)"
91 (defconst eword-encoded-word-suffix-regexp
94 (defconst eword-encoded-text-in-unstructured-regexp "[!->@-~]+")
95 (defconst eword-encoded-word-in-unstructured-regexp
96 (concat eword-encoded-word-prefix-regexp
97 "\\(" eword-encoded-text-in-unstructured-regexp "\\)"
98 eword-encoded-word-suffix-regexp))
99 (defconst eword-after-encoded-word-in-unstructured-regexp "\\([ \t]\\|$\\)")
101 (defconst eword-encoded-text-in-phrase-regexp "[-A-Za-z0-9!*+/=_]+")
102 (defconst eword-encoded-word-in-phrase-regexp
103 (concat eword-encoded-word-prefix-regexp
104 "\\(" eword-encoded-text-in-phrase-regexp "\\)"
105 eword-encoded-word-suffix-regexp))
106 (defconst eword-after-encoded-word-in-phrase-regexp "\\([ \t]\\|$\\)")
108 (defconst eword-encoded-text-in-comment-regexp "[]!-'*->@-[^-~]+")
109 (defconst eword-encoded-word-in-comment-regexp
110 (concat eword-encoded-word-prefix-regexp
111 "\\(" eword-encoded-text-in-comment-regexp "\\)"
112 eword-encoded-word-suffix-regexp))
113 (defconst eword-after-encoded-word-in-comment-regexp "\\([ \t()\\\\]\\|$\\)")
115 (defconst eword-encoded-text-in-quoted-string-regexp "[]!#->@-[^-~]+")
116 (defconst eword-encoded-word-in-quoted-string-regexp
117 (concat eword-encoded-word-prefix-regexp
118 "\\(" eword-encoded-text-in-quoted-string-regexp "\\)"
119 eword-encoded-word-suffix-regexp))
120 (defconst eword-after-encoded-word-in-quoted-string-regexp "\\([ \t\"\\\\]\\|$\\)")
123 (defconst eword-encoded-text-regexp eword-encoded-text-in-unstructured-regexp)
124 (defconst eword-encoded-word-regexp eword-encoded-word-in-unstructured-regexp)
127 ;;; @ internal utilities
130 (defun eword-decode-first-encoded-words (string
133 &optional must-unfold)
134 "Decode MIME encoded-words in beginning of STRING.
136 EWORD-REGEXP is the regexp that matches a encoded-word.
138 eword-encoded-word-in-unstructured-regexp,
139 eword-encoded-text-in-phrase-regexp,
140 eword-encoded-word-in-comment-regexp or
141 eword-encoded-word-in-quoted-string-regexp.
143 AFTER-REGEXP is the regexp that matches a after encoded-word.
145 eword-after-encoded-word-in-unstructured-regexp,
146 eword-after-encoded-text-in-phrase-regexp,
147 eword-after-encoded-word-in-comment-regexp or
148 eword-after-encoded-word-in-quoted-string-regexp.
150 If beginning of STRING matches EWORD-REGEXP with AFTER-REGEXP,
151 returns a cons cell of decoded string(sequence of characters) and
152 the rest(sequence of octets).
154 If beginning of STRING does not matches EWORD-REGEXP and AFTER-REGEXP,
157 If an encoded-word is broken or your emacs implementation can not
158 decode the charset included in it, it is returned in decoded part
159 as encoded-word form.
161 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
162 if there are in decoded encoded-words (generated by bad manner MUA
163 such as a version of Net$cape)."
164 (if eword-decode-sticked-encoded-word (setq after-regexp ""))
165 (let* ((between-ewords-regexp
166 (if eword-decode-sticked-encoded-word
169 (between-ewords-eword-after-regexp
170 (concat "\\`\\(" between-ewords-regexp "\\)"
171 "\\(" eword-regexp "\\)"
174 (concat "\\`\\(" eword-regexp "\\)" after-regexp))
175 (src string) ; sequence of octets.
176 (dst "")) ; sequence of characters.
177 (if (string-match eword-after-regexp src)
180 (ew (substring src 0 q))
181 (dw (eword-decode-encoded-word ew must-unfold)))
182 (setq dst (concat dst dw)
183 src (substring src q))
184 (if (not (string= ew dw))
188 (string-match between-ewords-eword-after-regexp src)
190 (setq p (match-end 1)
192 ew (substring src p q)
193 dw (eword-decode-encoded-word ew must-unfold))
196 (setq dst (concat dst (substring src 0 q))
197 src (substring src q))
200 (setq dst (concat dst dw)
201 src (substring src q)))))
205 (defun eword-decode-entire-string (string
210 delimiters ; list of chars.
214 (if (and code-conversion
215 (not (mime-charset-to-coding-system code-conversion)))
216 (setq code-conversion default-mime-charset))
217 (let ((equal-safe-regexp (concat "\\`=?" safe-regexp))
222 (while (< 0 (length src))
223 (let ((ch (aref src 0))
226 (eword-decode-first-encoded-words src
227 eword-regexp after-regexp must-unfold))))
228 (if (and (not (string= buf ""))
229 (or decoded (memq ch delimiters)))
230 (setq dst (concat dst
231 (std11-wrap-as-quoted-pairs
232 (decode-mime-charset-string buf code-conversion)
233 chars-must-be-quote))
237 (setq dst (concat dst
238 (std11-wrap-as-quoted-pairs
240 chars-must-be-quote))
242 ((memq ch delimiters)
243 (setq dst (concat dst (list ch))
244 src (substring src 1)
247 (setq buf (concat buf (list (aref src 1)))
248 src (substring src 2)
250 ((string-match "\\`[ \t\n]+" src)
251 (setq buf (concat buf (substring src 0 (match-end 0)))
252 src (substring src (match-end 0))
254 ((and (string-match equal-safe-regexp src)
256 (setq buf (concat buf (substring src 0 (match-end 0)))
257 src (substring src (match-end 0))
258 ew-enable eword-decode-sticked-encoded-word))
259 (t (error "something wrong")))))
260 (if (not (string= buf ""))
261 (setq dst (concat dst
262 (std11-wrap-as-quoted-pairs
263 (decode-mime-charset-string buf code-conversion)
264 chars-must-be-quote))))
271 (defun eword-decode-unstructured (string code-conversion &optional must-unfold)
272 (eword-decode-entire-string
274 eword-encoded-word-in-unstructured-regexp
275 eword-after-encoded-word-in-unstructured-regexp
283 (defun eword-decode-comment (string code-conversion &optional must-unfold)
284 (eword-decode-entire-string
286 eword-encoded-word-in-comment-regexp
287 eword-after-encoded-word-in-comment-regexp
291 '(?\( ?\) ?\\ ?\r ?\n)
295 (defun eword-decode-quoted-string (string code-conversion &optional must-unfold)
296 (eword-decode-entire-string
298 eword-encoded-word-in-quoted-string-regexp
299 eword-after-encoded-word-in-quoted-string-regexp
307 (defun eword-decode-string (string &optional must-unfold code-conversion)
308 "Decode MIME encoded-words in STRING.
310 STRING is unfolded before decoding.
312 If an encoded-word is broken or your emacs implementation can not
313 decode the charset included in it, it is not decoded.
315 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
316 if there are in decoded encoded-words (generated by bad manner MUA
317 such as a version of Net$cape).
319 If CODE-CONVERSION is nil, it decodes only encoded-words. If it is
320 mime-charset, it decodes non-ASCII bit patterns as the mime-charset.
321 Otherwise it decodes non-ASCII bit patterns as the
322 default-mime-charset."
323 (eword-decode-unstructured
324 (std11-unfold-string string)
328 (defun eword-decode-structured-field-body (string
330 start-column max-column)
331 (let* ((ew-decode-field-default-syntax '(ew-scan-unibyte-std11))
332 (decoded (ew-decode-field "" (ew-lf-crlf-to-crlf string))))
333 (ew-crlf-to-lf decoded)))
335 (defun eword-decode-and-unfold-structured-field-body (string
339 "Decode and unfold STRING as structured field body.
340 It decodes non us-ascii characters in FULL-NAME encoded as
341 encoded-words or invalid \"raw\" string. \"Raw\" non us-ascii
342 characters are regarded as variable `default-mime-charset'.
344 If an encoded-word is broken or your emacs implementation can not
345 decode the charset included in it, it is not decoded."
346 (let* ((decoded (ew-decode-field "" (ew-lf-crlf-to-crlf string))))
347 (ew-crlf-to-lf (ew-crlf-unfold decoded))))
349 (defun eword-decode-and-fold-structured-field-body (string
351 &optional max-column)
353 (setq max-column fill-column))
354 (let* ((field-name (make-string (1- start-column) ?X))
355 (field-body (ew-lf-crlf-to-crlf string))
356 (ew-decode-field-default-syntax '(ew-scan-unibyte-std11))
357 (decoded (ew-decode-field field-name field-body)))
358 (unless (equal field-body decoded)
359 (setq decoded (ew-crlf-refold decoded start-column max-column)))
360 (ew-crlf-to-lf decoded)))
362 (defun eword-decode-unstructured-field-body (string &optional start-column
364 (let ((decoded (ew-decode-field "" (ew-lf-crlf-to-crlf string))))
365 (ew-crlf-to-lf decoded)))
367 (defun eword-decode-and-unfold-unstructured-field-body (string
368 &optional start-column
370 (let ((decoded (ew-decode-field "" (ew-lf-crlf-to-crlf string))))
371 (ew-crlf-to-lf (ew-crlf-unfold decoded))))
373 (defun eword-decode-unfolded-unstructured-field-body (string
374 &optional start-column
376 (let ((decoded (ew-decode-field "" (ew-lf-crlf-to-crlf string))))
377 (ew-crlf-to-lf decoded)))
383 (defun eword-decode-region (start end &optional unfolding must-unfold
385 "Decode MIME encoded-words in region between START and END.
387 If UNFOLDING is not nil, it unfolds before decoding.
389 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
390 if there are in decoded encoded-words (generated by bad manner MUA
391 such as a version of Net$cape).
393 If CODE-CONVERSION is nil, it decodes only encoded-words. If it is
394 mime-charset, it decodes non-ASCII bit patterns as the mime-charset.
395 Otherwise it decodes non-ASCII bit patterns as the
396 default-mime-charset."
398 (rotate-memo args-eword-decode-region
399 (list start end (buffer-substring start end) unfolding must-unfold code-conversion))
402 (narrow-to-region start end)
404 (eword-decode-unfold)
406 (let ((str (eword-decode-unstructured
407 (buffer-substring (point-min) (point-max))
410 (delete-region (point-min) (point-max))
413 (defun eword-decode-unfold ()
414 (goto-char (point-min))
416 (while (re-search-forward std11-field-head-regexp nil t)
417 (setq beg (match-beginning 0)
418 end (std11-field-end))
419 (setq field (buffer-substring beg end))
420 (if (string-match eword-encoded-word-regexp field)
422 (narrow-to-region (goto-char beg) end)
423 (while (re-search-forward "\n\\([ \t]\\)" nil t)
424 (replace-match (match-string 1))
426 (goto-char (point-max))
430 ;;; @ for message header
433 (defvar mime-field-decoder-alist nil)
435 (defvar mime-field-decoder-cache nil)
437 (defvar mime-update-field-decoder-cache 'ew-mime-update-field-decoder-cache
438 "*Field decoder cache update function.")
441 (defun mime-set-field-decoder (field &rest specs)
442 "Set decoder of FILED.
443 SPECS must be like `MODE1 DECODER1 MODE2 DECODER2 ...'.
444 Each mode must be `nil', `plain', `wide', `summary' or `nov'.
445 If mode is `nil', corresponding decoder is set up for every modes."
447 (let ((mode (pop specs))
448 (function (pop specs)))
451 (let ((cell (assq mode mime-field-decoder-alist)))
453 (setcdr cell (put-alist field function (cdr cell)))
454 (setq mime-field-decoder-alist
455 (cons (cons mode (list (cons field function)))
456 mime-field-decoder-alist))
458 (apply (function mime-set-field-decoder) field specs)
460 (mime-set-field-decoder field
468 (defmacro mime-find-field-presentation-method (name)
469 "Return field-presentation-method from NAME.
470 NAME must be `plain', `wide', `summary' or `nov'."
472 `(or (assq 'summary mime-field-decoder-cache)
478 (symbolp (car (cdr name)))
479 (null (cdr (cdr name))))
480 `(or (assq ,name mime-field-decoder-cache)
484 `(or (assq (or ,name 'summary) mime-field-decoder-cache)
485 (cons (or ,name 'summary) nil))
488 (defun mime-find-field-decoder-internal (field &optional mode)
489 "Return function to decode field-body of FIELD in MODE.
490 Optional argument MODE must be object of field-presentation-method."
491 (cdr (or (assq field (cdr mode))
493 (funcall mime-update-field-decoder-cache
496 (cdr (assq (car mode) mime-field-decoder-cache)))
500 (defun mime-find-field-decoder (field &optional mode)
501 "Return function to decode field-body of FIELD in MODE.
502 Optional argument MODE must be object or name of
503 field-presentation-method. Name of field-presentation-method must be
504 `plain', `wide', `summary' or `nov'.
505 Default value of MODE is `summary'."
507 (let ((p (cdr (mime-find-field-presentation-method mode))))
508 (if (and p (setq p (assq field p)))
510 (cdr (funcall mime-update-field-decoder-cache
511 field (or mode 'summary)))))
512 (inline (mime-find-field-decoder-internal field mode))
516 (defun mime-update-field-decoder-cache (field mode &optional function)
517 "Update field decoder cache `mime-field-decoder-cache'."
518 (cond ((eq function 'identity)
523 (cdr (assq (or mode 'summary) mime-field-decoder-alist))))
524 (setq function (cdr (or (assq field decoder-alist)
525 (assq t decoder-alist)))))
527 (let ((cell (assq mode mime-field-decoder-cache))
530 (if (setq ret (assq field (cdr cell)))
531 (setcdr ret function)
532 (setcdr cell (cons (setq ret (cons field function)) (cdr cell))))
533 (setq mime-field-decoder-cache
534 (cons (cons mode (list (setq ret (cons field function))))
535 mime-field-decoder-cache)))
539 (mime-set-field-decoder 'Archive nil nil)
540 (mime-set-field-decoder 'Content-Md5 nil nil)
541 (mime-set-field-decoder 'Control nil nil)
542 (mime-set-field-decoder 'Date nil nil)
543 (mime-set-field-decoder 'Distribution nil nil)
544 (mime-set-field-decoder 'Followup-Host nil nil)
545 (mime-set-field-decoder 'Followup-To nil nil)
546 (mime-set-field-decoder 'Lines nil nil)
547 (mime-set-field-decoder 'Message-Id nil nil)
548 (mime-set-field-decoder 'Newsgroups nil nil)
549 (mime-set-field-decoder 'Nntp-Posting-Host nil nil)
550 (mime-set-field-decoder 'Path nil nil)
551 (mime-set-field-decoder 'Posted-And-Mailed nil nil)
552 (mime-set-field-decoder 'Received nil nil)
553 (mime-set-field-decoder 'Status nil nil)
554 (mime-set-field-decoder 'X-Face nil nil)
555 (mime-set-field-decoder 'X-Face-Version nil nil)
556 (mime-set-field-decoder 'X-Info nil nil)
557 (mime-set-field-decoder 'X-Pgp-Key-Info nil nil)
558 (mime-set-field-decoder 'X-Pgp-Sig nil nil)
559 (mime-set-field-decoder 'X-Pgp-Sig-Version nil nil)
560 (mime-set-field-decoder 'Xref nil nil)
564 '(Reply-To Resent-Reply-To From Resent-From Sender Resent-Sender
565 To Resent-To Cc Resent-Cc Bcc Resent-Bcc Dcc
567 Mime-Version Content-Type Content-Transfer-Encoding
568 Content-Disposition User-Agent))
571 (setq field (pop fields))
572 (mime-set-field-decoder
574 'plain #'eword-decode-structured-field-body
575 'wide #'eword-decode-and-fold-structured-field-body
576 'summary #'eword-decode-and-unfold-structured-field-body
577 'nov #'eword-decode-and-unfold-structured-field-body)
580 ;; unstructured fields (default)
581 (mime-set-field-decoder
583 'plain #'eword-decode-unstructured-field-body
584 'wide #'eword-decode-unstructured-field-body
585 'summary #'eword-decode-and-unfold-unstructured-field-body
586 'nov #'eword-decode-unfolded-unstructured-field-body)
589 (defun ew-mime-update-field-decoder-cache (field mode)
592 (lexical-let ((field-name (symbol-name field)))
593 (lambda (field-body &optional start-column max-column must-unfold)
594 (setq field-body (ew-lf-to-crlf field-body))
595 (let ((res (ew-crlf-to-lf
596 (ew-decode-field field-name field-body))))
599 (list 'original-field-name field-name
600 'original-field-body field-body)
604 (lexical-let ((field-name (symbol-name field)))
605 (lambda (field-body &optional start-column max-column must-unfold)
606 (setq field-body (ew-lf-to-crlf field-body))
607 (let ((res (ew-crlf-to-lf
609 (ew-decode-field field-name field-body)
611 (or max-column fill-column)))))
614 (list 'original-field-name field-name
615 'original-field-body field-body)
619 (lexical-let ((field-name (symbol-name field)))
620 (lambda (field-body &optional start-column max-column must-unfold)
621 (setq field-body (ew-lf-to-crlf field-body))
622 (let ((res (ew-crlf-to-lf
624 (ew-decode-field field-name field-body)))))
627 (list 'original-field-name field-name
628 'original-field-body field-body)
632 (lexical-let ((field-name (symbol-name field)))
633 (lambda (field-body &optional start-column max-column must-unfold)
634 (setq field-body (ew-lf-to-crlf field-body))
636 (let ((ew-ignore-76bytes-limit t))
637 (let ((res (ew-crlf-to-lf
639 (ew-decode-field field-name field-body)))))
642 (list 'original-field-name field-name
643 'original-field-body field-body)
648 (mime-update-field-decoder-cache field mode fun)))
651 (defun mime-decode-field-body (field-body field-name
652 &optional mode max-column)
653 "Decode FIELD-BODY as FIELD-NAME in MODE, and return the result.
654 Optional argument MODE must be `plain', `wide', `summary' or `nov'.
655 Default mode is `summary'.
657 If MODE is `wide' and MAX-COLUMN is non-nil, the result is folded with
660 Non MIME encoded-word part in FILED-BODY is decoded with
661 `default-mime-charset'."
662 (unless mode (setq mode 'summary))
663 (if (symbolp field-name) (setq field-name (symbol-name field-name)))
666 (let ((ew-ignore-76bytes-limit t))
668 field-name (ew-lf-crlf-to-crlf field-body)))
670 field-name (ew-lf-crlf-to-crlf field-body)))))
671 (if (and (eq mode 'wide) max-column)
672 (setq decoded (ew-crlf-refold
674 (1+ (string-width field-name))
676 (if (not (eq mode 'plain))
677 (setq decoded (ew-crlf-unfold decoded))))
678 (setq decoded (ew-crlf-to-lf decoded))
679 (add-text-properties 0 (length decoded)
680 (list 'original-field-name field-name
681 'original-field-body field-body)
686 (defun mime-decode-header-in-region (start end
687 &optional code-conversion)
688 "Decode MIME encoded-words in region between START and END.
689 If CODE-CONVERSION is nil, it decodes only encoded-words. If it is
690 mime-charset, it decodes non-ASCII bit patterns as the mime-charset.
691 Otherwise it decodes non-ASCII bit patterns as the
692 default-mime-charset."
696 (narrow-to-region start end)
697 (let ((default-charset
699 (if (mime-charset-to-coding-system code-conversion)
701 default-mime-charset))))
703 (let ((mode-obj (mime-find-field-presentation-method 'wide))
704 beg p end len field-decoder
705 field-name field-body)
706 (goto-char (point-min))
707 (while (re-search-forward std11-field-head-regexp nil t)
708 (setq beg (match-beginning 0)
710 field-name (buffer-substring beg (1- p))
711 len (string-width field-name)
712 field-decoder (inline
713 (mime-find-field-decoder-internal
714 (intern (capitalize field-name))
717 (setq end (std11-field-end)
718 field-body (buffer-substring p end))
719 (let ((default-mime-charset default-charset))
720 (delete-region p end)
721 (insert (funcall field-decoder field-body (1+ len)))
723 (add-text-properties beg (min (1+ (point)) (point-max))
724 (list 'original-field-name field-name
725 'original-field-body field-body))
727 (eword-decode-region (point-min) (point-max) t)
731 (defun mime-decode-header-in-buffer (&optional code-conversion separator)
732 "Decode MIME encoded-words in header fields.
733 If CODE-CONVERSION is nil, it decodes only encoded-words. If it is
734 mime-charset, it decodes non-ASCII bit patterns as the mime-charset.
735 Otherwise it decodes non-ASCII bit patterns as the
736 default-mime-charset.
737 If SEPARATOR is not nil, it is used as header separator."
739 (mime-decode-header-in-region
742 (goto-char (point-min))
743 (if (re-search-forward
744 (concat "^\\(" (regexp-quote (or separator "")) "\\)?$")
751 (define-obsolete-function-alias 'eword-decode-header
752 'mime-decode-header-in-buffer)
755 ;;; @ encoded-word decoder
758 (defvar eword-decode-encoded-word-error-handler
759 'eword-decode-encoded-word-default-error-handler)
761 (defvar eword-warning-face nil
762 "Face used for invalid encoded-word.")
764 (defun eword-decode-encoded-word-default-error-handler (word signal)
765 (and (add-text-properties 0 (length word)
766 (and eword-warning-face
767 (list 'face eword-warning-face))
771 (defun eword-decode-encoded-word (word &optional must-unfold)
772 "Decode WORD if it is an encoded-word.
774 If your emacs implementation can not decode the charset of WORD, it
775 returns WORD. Similarly the encoded-word is broken, it returns WORD.
777 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
778 if there are in decoded encoded-word (generated by bad manner MUA such
779 as a version of Net$cape)."
780 (or (if (string-match eword-encoded-word-regexp word)
782 (substring word (match-beginning 1) (match-end 1))
786 (substring word (match-beginning 2) (match-end 2))
789 (substring word (match-beginning 3) (match-end 3))
792 (eword-decode-encoded-text charset encoding text must-unfold)
794 (funcall eword-decode-encoded-word-error-handler word err)
800 ;;; @ encoded-text decoder
803 (defun eword-decode-encoded-text (charset encoding string
804 &optional must-unfold)
805 "Decode STRING as an encoded-text.
807 If your emacs implementation can not decode CHARSET, it returns nil.
809 If ENCODING is not \"B\" or \"Q\", it occurs error.
810 So you should write error-handling code if you don't want break by errors.
812 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
813 if there are in decoded encoded-text (generated by bad manner MUA such
814 as a version of Net$cape)."
815 (let ((cs (mime-charset-to-coding-system charset)))
817 (let ((dest (encoded-text-decode-string string encoding)))
819 (setq dest (decode-mime-charset-string dest charset))
823 (cond ((eq chr ?\n) "")
825 (t (char-to-string chr)))
827 (std11-unfold-string dest)
832 ;;; @ lexical analyze
835 (defvar eword-lexical-analyze-cache nil)
836 (defvar eword-lexical-analyze-cache-max 299
837 "*Max position of eword-lexical-analyze-cache.
838 It is max size of eword-lexical-analyze-cache - 1.")
840 (defcustom eword-lexical-analyzers
841 '(eword-analyze-quoted-string
842 eword-analyze-domain-literal
843 eword-analyze-comment
845 eword-analyze-special
846 eword-analyze-encoded-word
848 "*List of functions to return result of lexical analyze.
849 Each function must have two arguments: STRING and MUST-UNFOLD.
850 STRING is the target string to be analyzed.
851 If MUST-UNFOLD is not nil, each function must unfold and eliminate
852 bare-CR and bare-LF from the result even if they are included in
853 content of the encoded-word.
854 Each function must return nil if it can not analyze STRING as its
857 Previous function is preferred to next function. If a function
858 returns nil, next function is used. Otherwise the return value will
861 :type '(repeat function))
863 (defun eword-analyze-quoted-string (string &optional must-unfold)
864 (let ((p (std11-check-enclosure string ?\" ?\")))
866 (cons (cons 'quoted-string
867 (if eword-decode-quoted-encoded-word
868 (eword-decode-quoted-string
869 (substring string 0 p)
870 default-mime-charset)
871 (std11-wrap-as-quoted-string
872 (decode-mime-charset-string
873 (std11-strip-quoted-pair (substring string 1 (1- p)))
874 default-mime-charset))))
875 (substring string p)))
878 (defun eword-analyze-domain-literal (string &optional must-unfold)
879 (std11-analyze-domain-literal string))
881 (defun eword-analyze-comment (string &optional must-unfold)
882 (let ((len (length string)))
883 (if (and (< 0 len) (eq (aref string 0) ?\())
885 (while (and p (< p len) (eq (aref string p) ?\())
886 (setq p (std11-check-enclosure string ?\( ?\) t p)))
889 (eword-decode-comment
890 (std11-unfold-string (substring string 0 p))
891 default-mime-charset))
892 (substring string p)))
896 (defun eword-analyze-spaces (string &optional must-unfold)
897 (std11-analyze-spaces string))
899 (defun eword-analyze-special (string &optional must-unfold)
900 (std11-analyze-special string))
902 (defun eword-analyze-encoded-word (string &optional must-unfold)
903 (let ((decoded (eword-decode-first-encoded-words
905 eword-encoded-word-in-phrase-regexp
906 eword-after-encoded-word-in-phrase-regexp
909 (let ((s (car decoded)))
910 (while (or (string-match std11-atom-regexp s)
911 (string-match std11-spaces-regexp s))
912 (setq s (substring s (match-end 0))))
914 (cons (cons 'atom (car decoded)) (cdr decoded))
915 (cons (cons 'quoted-string
916 (std11-wrap-as-quoted-string (car decoded)))
919 (defun eword-analyze-atom (string &optional must-unfold)
920 (if (string-match std11-atom-regexp (string-as-unibyte string))
921 (let ((end (match-end 0)))
922 (if (and eword-decode-sticked-encoded-word
923 (string-match eword-encoded-word-in-phrase-regexp
924 (substring string 0 end))
925 (< 0 (match-beginning 0)))
926 (setq end (match-beginning 0)))
927 (cons (cons 'atom (decode-mime-charset-string
928 (substring string 0 end)
929 default-mime-charset))
930 (substring string end)
933 (defun eword-lexical-analyze-internal (string must-unfold)
934 (let ((last 'eword-analyze-spaces)
936 (while (not (string-equal string ""))
938 (let ((rest eword-lexical-analyzers)
940 (while (and (setq func (car rest))
943 (not eword-decode-sticked-encoded-word)
944 (not (eq last 'eword-analyze-spaces))
945 (eq func 'eword-analyze-encoded-word))
946 (null (setq r (funcall func string must-unfold))))
948 (setq rest (cdr rest)))
950 (or r `((error . ,string) . ""))
952 (setq dest (cons (car ret) dest))
953 (setq string (cdr ret))
958 (defun eword-lexical-analyze (string &optional must-unfold)
959 "Return lexical analyzed list corresponding STRING.
960 It is like std11-lexical-analyze, but it decodes non us-ascii
961 characters encoded as encoded-words or invalid \"raw\" format.
962 \"Raw\" non us-ascii characters are regarded as variable
963 `default-mime-charset'."
964 (let* ((str (copy-sequence string))
965 (key (cons str (cons default-mime-charset must-unfold)))
967 (set-text-properties 0 (length str) nil str)
968 (if (setq ret (assoc key eword-lexical-analyze-cache))
970 (setq ret (eword-lexical-analyze-internal str must-unfold))
971 (setq eword-lexical-analyze-cache
973 (last eword-lexical-analyze-cache
974 eword-lexical-analyze-cache-max)))
977 (defun eword-decode-token (token)
980 (defun eword-extract-address-components (string)
981 "Extract full name and canonical address from STRING.
982 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
983 If no name can be extracted, FULL-NAME will be nil.
984 It decodes non us-ascii characters in FULL-NAME encoded as
985 encoded-words or invalid \"raw\" string. \"Raw\" non us-ascii
986 characters are regarded as variable `default-mime-charset'."
987 (rotate-memo args-eword-extract-address-components (list string))
988 (let* ((structure (car (std11-parse-address
989 (eword-lexical-analyze
990 (std11-unfold-string string) 'must-unfold))))
991 (phrase (std11-full-name-string structure))
992 (address (std11-address-string structure))
994 (list phrase address)
1001 (provide 'eword-decode)
1003 ;;; eword-decode.el ends here