*** empty log message ***
[emacs.git] / man / emacs-mime.texi
blobe416118324e744459d86e49b2512bb738bf57603
1 \input texinfo                  @c -*-mode: texinfo; coding: latin-1 -*-
3 @setfilename ../info/emacs-mime
4 @settitle Emacs MIME Manual
5 @synindex fn cp
6 @synindex vr cp
7 @synindex pg cp
8 @dircategory Emacs
9 @direntry
10 * Emacs MIME: (emacs-mime).   The MIME de/composition library.
11 @end direntry
12 @iftex
13 @finalout
14 @end iftex
15 @setchapternewpage odd
17 @ifnottex
19 This file documents the Emacs MIME interface functionality.
21 Copyright (C) 1998,99,2000 Free Software Foundation, Inc.
23 Permission is granted to copy, distribute and/or modify this document
24 under the terms of the GNU Free Documentation License, Version 1.1 or
25 any later version published by the Free Software Foundation; with no
26 Invariant Sections, with the Front-Cover texts being ``A GNU
27 Manual,'' and with the Back-Cover Texts as in (a) below.  A copy of the
28 license is included in the section entitled ``GNU Free Documentation
29 License'' in the Emacs manual.
31 (a) The FSF's Back-Cover Text is: ``You have freedom to copy and modify
32 this GNU Manual, like GNU software.  Copies published by the Free
33 Software Foundation raise funds for GNU development.''
35 This document is part of a collection distributed under the GNU Free
36 Documentation License.  If you want to distribute this document
37 separately from the collection, you can do so by adding a copy of the
38 license to the document, as described in section 6 of the license.
39 @end ifnottex
41 @tex
43 @titlepage
44 @title Emacs MIME Manual
46 @author by Lars Magne Ingebrigtsen
47 @page
49 @vskip 0pt plus 1filll
50 Copyright @copyright{} 1998,99,2000 Free Software Foundation, Inc.
52 Permission is granted to copy, distribute and/or modify this document
53 under the terms of the GNU Free Documentation License, Version 1.1 or
54 any later version published by the Free Software Foundation; with the
55 Invariant Sections being none, with the Front-Cover texts being ``A GNU
56 Manual'', and with the Back-Cover Texts as in (a) below.  A copy of the
57 license is included in the section entitled ``GNU Free Documentation
58 License'' in the Emacs manual.
60 (a) The FSF's Back-Cover Text is: ``You have freedom to copy and modify
61 this GNU Manual, like GNU software.  Copies published by the Free
62 Software Foundation raise funds for GNU development.''
64 This document is part of a collection distributed under the GNU Free
65 Documentation License.  If you want to distribute this document
66 separately from the collection, you can do so by adding a copy of the
67 license to the document, as described in section 6 of the license.
68 @end titlepage
69 @page
71 @end tex
73 @node Top
74 @top Emacs MIME
76 This manual documents the libraries used to compose and display
77 @sc{mime} messages.
79 This is not a manual meant for users; it's a manual directed at people
80 who want to write functions and commands that manipulate @sc{mime}
81 elements.
83 @sc{mime} is short for @dfn{Multipurpose Internet Mail Extensions}.
84 This standard is documented in a number of RFCs; mainly RFC2045 (Format
85 of Internet Message Bodies), RFC2046 (Media Types), RFC2047 (Message
86 Header Extensions for Non-ASCII Text), RFC2048 (Registration
87 Procedures), RFC2049 (Conformance Criteria and Examples).  It is highly
88 recommended that anyone who intends writing @sc{mime}-compliant software
89 read at least RFC2045 and RFC2047.
91 @menu
92 * Interface Functions::   An abstraction over the basic functions.
93 * Basic Functions::       Utility and basic parsing functions.
94 * Decoding and Viewing::  A framework for decoding and viewing.
95 * Composing::             MML; a language for describing MIME parts.
96 * Standards::             A summary of RFCs and working documents used.
97 * Index::                 Function and variable index.
98 @end menu
101 @node Interface Functions
102 @chapter Interface Functions
103 @cindex interface functions
104 @cindex mail-parse
106 The @code{mail-parse} library is an abstraction over the actual
107 low-level libraries that are described in the next chapter.
109 Standards change, and so programs have to change to fit in the new
110 mold.  For instance, RFC2045 describes a syntax for the
111 @code{Content-Type} header that only allows @sc{ascii} characters in the
112 parameter list.  RFC2231 expands on RFC2045 syntax to provide a scheme
113 for continuation headers and non-@sc{ascii} characters.
115 The traditional way to deal with this is just to update the library
116 functions to parse the new syntax.  However, this is sometimes the wrong
117 thing to do.  In some instances it may be vital to be able to understand
118 both the old syntax as well as the new syntax, and if there is only one
119 library, one must choose between the old version of the library and the
120 new version of the library.
122 The Emacs MIME library takes a different tack.  It defines a series of
123 low-level libraries (@file{rfc2047.el}, @file{rfc2231.el} and so on)
124 that parses strictly according to the corresponding standard.  However,
125 normal programs would not use the functions provided by these libraries
126 directly, but instead use the functions provided by the
127 @code{mail-parse} library.  The functions in this library are just
128 aliases to the corresponding functions in the latest low-level
129 libraries.  Using this scheme, programs get a consistent interface they
130 can use, and library developers are free to create write code that
131 handles new standards.
133 The following functions are defined by this library:
135 @defun mail-header-parse-content-type string
136 Parse @var{string}, a @code{Content-Type} header, and return a
137 content-type list in the following format:
139 @lisp
140 ("type/subtype"
141  (attribute1 . value1)
142  (attribute2 . value2)
143  @dots{})
144 @end lisp
146 Here's an example:
148 @example
149 (mail-header-parse-content-type
150  "image/gif; name=\"b980912.gif\"")
151 @result{} ("image/gif" (name . "b980912.gif"))
152 @end example
153 @end defun
155 @defun mail-header-parse-content-disposition string
156 Parse @var{string}, a @code{Content-Disposition} header, and return a
157 content-type list in the format above.
158 @end defun
160 @defun mail-content-type-get ct attribute
161 @findex mail-content-type-get
162 Returns the value of the given @var{attribute} from the content-type
163 list @var{ct}.
165 @example
166 (mail-content-type-get
167  '("image/gif" (name . "b980912.gif")) 'name)
168 @result{} "b980912.gif"
169 @end example
170 @end defun
172 @defun mail-header-encode-parameter param value
173 Takes a parameter string @samp{@var{param}=@var{value}} and returns an
174 encoded version of it.  This is used for parameters in headers like
175 @samp{Content-Type} and @samp{Content-Disposition}.
176 @end defun
178 @defun mail-header-remove-comments string
179 Return a comment-free version of @var{string}.
181 @example
182 (mail-header-remove-comments
183  "Gnus/5.070027 (Pterodactyl Gnus v0.27) (Finnish Landrace)")
184 @result{} "Gnus/5.070027  "
185 @end example
186 @end defun
188 @defun mail-header-remove-whitespace string
189 Remove linear white space from @var{string}.  Space inside quoted
190 strings and comments is preserved.
192 @example
193 (mail-header-remove-whitespace
194  "image/gif; name=\"Name with spaces\"")
195 @result{} "image/gif;name=\"Name with spaces\""
196 @end example
197 @end defun
199 @defun mail-header-get-comment string
200 Return the last comment in @var{string}.
202 @example
203 (mail-header-get-comment
204  "Gnus/5.070027 (Pterodactyl Gnus v0.27) (Finnish Landrace)")
205 @result{} "Finnish Landrace"
206 @end example
207 @end defun
210 @defun mail-header-parse-address string
211 Parse an address string @var{string} and return a list containing the
212 mailbox and the plaintext name.
214 @example
215 (mail-header-parse-address
216  "Hrvoje Niksic <hniksic@@srce.hr>")
217 @result{} ("hniksic@@srce.hr" . "Hrvoje Niksic")
218 @end example
219 @end defun
221 @defun mail-header-parse-addresses string
222 Parse @var{string} as a list of addresses and return a list of elements
223 like the one described above.
225 @example
226 (mail-header-parse-addresses
227  "Hrvoje Niksic <hniksic@@srce.hr>, Steinar Bang <sb@@metis.no>")
228 @result{} (("hniksic@@srce.hr" . "Hrvoje Niksic")
229      ("sb@@metis.no" . "Steinar Bang"))
230 @end example
231 @end defun
233 @defun mail-header-parse-date string
234 Parse a date @var{string} and return an Emacs time structure.
235 @end defun
237 @defun mail-narrow-to-head
238 Narrow the buffer to the header section of the buffer.  Point is placed
239 at the beginning of the narrowed buffer.
240 @end defun
242 @defun mail-header-narrow-to-field
243 Narrow the buffer to the header under point.
244 @end defun
246 @defun mail-encode-encoded-word-region start end
247 Encode the non-@sc{ascii} words in the region @var{start}to @var{end}.  For
248 instance, @samp{Naïve} is encoded as @samp{=?iso-8859-1?q?Na=EFve?=}.
249 @end defun
251 @defun mail-encode-encoded-word-buffer
252 Encode the non-@sc{ascii} words in the current buffer.  This function is
253 meant to be called with the buffer narrowed to the headers of a message.
254 @end defun
256 @defun mail-encode-encoded-word-string string
257 Encode the words that need encoding in @var{string}, and return the
258 result.
260 @example
261 (mail-encode-encoded-word-string
262  "This is naïve, baby")
263 @result{} "This is =?iso-8859-1?q?na=EFve,?= baby"
264 @end example
265 @end defun
267 @defun mail-decode-encoded-word-region start end
268 Decode the encoded words in the region @var{start}to @var{end}.
269 @end defun
271 @defun mail-decode-encoded-word-string string
272 Decode the encoded words in @var{string} and return the result.
274 @example
275 (mail-decode-encoded-word-string
276  "This is =?iso-8859-1?q?na=EFve,?= baby")
277 @result{} "This is naïve, baby"
278 @end example
279 @end defun
281 Currently, @code{mail-parse} is an abstraction over @code{ietf-drums},
282 @code{rfc2047}, @code{rfc2045} and @code{rfc2231}.  These are documented
283 in the subsequent sections.
287 @node Basic Functions
288 @chapter Basic Functions
290 This chapter describes the basic, ground-level functions for parsing and
291 handling.  Covered here is parsing @code{From} lines, removing comments
292 from header lines, decoding encoded words, parsing date headers and so
293 on.  High-level functionality is dealt with in the next chapter
294 (@pxref{Decoding and Viewing}).
296 @menu
297 * rfc2045::      Encoding @code{Content-Type} headers.
298 * rfc2231::      Parsing @code{Content-Type} headers.
299 * ietf-drums::   Handling mail headers defined by RFC822bis.
300 * rfc2047::      En/decoding encoded words in headers.
301 * time-date::    Functions for parsing dates and manipulating time.
302 * qp::           Quoted-Printable en/decoding.
303 * base64::       Base64 en/decoding.
304 * binhex::       Binhex decoding.
305 * uudecode::     Uuencode decoding.
306 * rfc1843::      Decoding HZ-encoded text.
307 * mailcap::      How parts are displayed is specified by mailcap files
308 @end menu
311 @node rfc2045
312 @section rfc2045
314 RFC2045 is the ``main'' @sc{mime} document, and as such, one would
315 imagine that there would be a lot to implement.  But there isn't, since
316 most of the implementation details are delegated to the subsequent
317 RFCs.
319 So @file{rfc2045.el} has only a single function:
321 @defun rfc2045-encode-string parameter value
322 @findex rfc2045-encode-string
323 Takes a @var{parameter} and a @var{value} and returns a
324 @samp{@var{param}=@var{value}} string.  @var{value} will be quoted if
325 there are non-safe characters in it.
326 @end defun
329 @node rfc2231
330 @section rfc2231
332 RFC2231 defines a syntax for the @samp{Content-Type} and
333 @samp{Content-Disposition} headers.  Its snappy name is @dfn{MIME
334 Parameter Value and Encoded Word Extensions: Character Sets, Languages,
335 and Continuations}.
337 In short, these headers look something like this:
339 @example
340 Content-Type: application/x-stuff;
341  title*0*=us-ascii'en'This%20is%20even%20more%20;
342  title*1*=%2A%2A%2Afun%2A%2A%2A%20;
343  title*2="isn't it!"
344 @end example
346 They usually aren't this bad, though.
348 The following functions are defined by this library:
350 @defun rfc2231-parse-string string
351 Parse a @samp{Content-Type} header @var{string} and return a list
352 describing its elements.
354 @example
355 (rfc2231-parse-string
356  "application/x-stuff;
357  title*0*=us-ascii'en'This%20is%20even%20more%20;
358  title*1*=%2A%2A%2Afun%2A%2A%2A%20;
359  title*2=\"isn't it!\"")
360 @result{} ("application/x-stuff"
361     (title . "This is even more ***fun*** isn't it!"))
362 @end example
363 @end defun
365 @defun rfc2231-get-value ct attribute
366 Takes a list @var{ct} of the format above and returns the value of the
367 specified @var{attribute}.
368 @end defun
370 @defun rfc2231-encode-string parameter value
371 Encode the string @samp{@var{parameter}=@var{value}} for inclusion in
372 headers likes @samp{Content-Type} and @samp{Content-Disposition}.
373 @end defun
375 @node ietf-drums
376 @section ietf-drums
378 @dfn{drums} is an IETF working group that is working on the replacement
379 for RFC822.
381 The functions provided by this library include:
383 @defun ietf-drums-remove-comments string
384 Remove the comments from @var{string} and return the result.
385 @end defun
387 @defun ietf-drums-remove-whitespace string
388 Remove linear white space from @var{string} and return the result.
389 Spaces inside quoted strings and comments are left untouched.
390 @end defun
392 @defun ietf-drums-get-comment string
393 Return the last most comment from @var{string}.
394 @end defun
396 @defun ietf-drums-parse-address string
397 Parse an address @var{string} and return a list of the mailbox and the
398 plain text name.
399 @end defun
401 @defun ietf-drums-parse-addresses string
402 Parse @var{string}, containing any number of comma-separated addresses,
403 and return a list of mailbox/plain text pairs.
404 @end defun
406 @defun ietf-drums-parse-date string
407 Parse the date @var{string} and return an Emacs time structure.
408 @end defun
410 @defun ietf-drums-narrow-to-header
411 Narrow the buffer to the header section of the current buffer.
412 @end defun
415 @node rfc2047
416 @section rfc2047
418 RFC2047 (Message Header Extensions for Non-ASCII Text) specifies how
419 non-@sc{ascii} text in headers are to be encoded.  This is actually rather
420 complicated, so a number of variables are necessary to tweak what this
421 library does.
423 The following variables are tweakable:
425 @defvar rfc2047-default-charset
426 Characters in this charset should not be decoded by this library.
427 This defaults to @samp{iso-8859-1}.
428 @end defvar
430 @defvar rfc2047-header-encoding-list
431 This is an alist of header / encoding-type pairs.  Its main purpose is
432 to prevent encoding of certain headers.
433 @end defvar
435 The keys can either be header regexps, or @code{t}.
437 The values can be either @code{nil}, in which case the header(s) in
438 question won't be encoded, or @code{mime}, which means that they will be
439 encoded.
441 @defvar rfc2047-charset-encoding-alist
442 RFC2047 specifies two forms of encoding---@code{Q} (a
443 Quoted-Printable-like encoding) and @code{B} (base64).  This alist
444 specifies which charset should use which encoding.
445 @end defvar
447 @defvar rfc2047-encoding-function-alist
448 This is an alist of encoding / function pairs.  The encodings are
449 @code{Q}, @code{B} and @code{nil}.
450 @end defvar
452 @defvar rfc2047-q-encoding-alist
453 The @code{Q} encoding isn't quite the same for all headers.  Some
454 headers allow a narrower range of characters, and that is what this
455 variable is for.  It's an alist of header regexps and allowable character
456 ranges.
457 @end defvar
459 @defvar rfc2047-encoded-word-regexp
460 When decoding words, this library looks for matches to this regexp.
461 @end defvar
463 Those were the variables, and these are the functions:
465 @defun rfc2047-narrow-to-field
466 Narrow the buffer to the header on the current line.
467 @end defun
469 @defun rfc2047-encode-message-header
470 Should be called narrowed to the header of a message.  Encodes according
471 to @code{rfc2047-header-encoding-alist}.
472 @end defun
474 @defun rfc2047-encode-region start end
475 Encodes all encodable words in the region @var{start} to @var{end}.
476 @end defun
478 @defun rfc2047-encode-string string
479 Encode @var{string} and return the result.
480 @end defun
482 @defun rfc2047-decode-region start end
483 Decode the encoded words in the region @var{start} to @var{end}.
484 @end defun
486 @defun rfc2047-decode-string string
487 Decode @var{string} and return the result.
488 @end defun
492 @node time-date
493 @section time-date
495 While not really a part of the @sc{mime} library, it is convenient to
496 document this library here.  It deals with parsing @samp{Date} headers
497 and manipulating time.  (Not by using tesseracts, though, I'm sorry to
498 say.)
500 These functions convert between five formats: a date string, an Emacs
501 time structure, a decoded time list, a number of seconds, and a day number.
503 The functions have quite self-explanatory names, so the following just
504 gives an overview of which functions are available.
506 @findex parse-time-string
507 @findex date-to-time
508 @findex time-to-seconds
509 @findex seconds-to-time
510 @findex time-to-day
511 @findex days-to-time
512 @findex time-since
513 @findex time-less-p
514 @findex subtract-time
515 @findex days-between
516 @findex date-leap-year-p
517 @findex time-to-day-in-year
518 @example
519 (parse-time-string "Sat Sep 12 12:21:54 1998 +0200")
520 @result{} (54 21 12 12 9 1998 6 nil 7200)
522 (date-to-time "Sat Sep 12 12:21:54 1998 +0200")
523 @result{} (13818 19266)
525 (time-to-seconds '(13818 19266))
526 @result{} 905595714.0
528 (seconds-to-time 905595714.0)
529 @result{} (13818 19266 0)
531 (time-to-day '(13818 19266))
532 @result{} 729644
534 (days-to-time 729644)
535 @result{} (961933 65536)
537 (time-since '(13818 19266))
538 @result{} (0 430)
540 (time-less-p '(13818 19266) '(13818 19145))
541 @result{} nil
543 (subtract-time '(13818 19266) '(13818 19145))
544 @result{} (0 121)
546 (days-between "Sat Sep 12 12:21:54 1998 +0200"
547               "Sat Sep 07 12:21:54 1998 +0200")
548 @result{} 5
550 (date-leap-year-p 2000)
551 @result{} t
553 (time-to-day-in-year '(13818 19266))
554 @result{} 255
555 @end example
557 @findex safe-date-to-time
558 And finally, we have @code{safe-date-to-time}, which does the same as
559 @code{date-to-time}, but returns a zero time if the date is
560 syntactically malformed.
564 @node qp
565 @section qp
567 This library deals with decoding and encoding Quoted-Printable text.
569 Very briefly explained, QP encoding means translating all 8-bit
570 characters (and lots of control characters) into things that look like
571 @samp{=EF}; that is, an equal sign followed by the byte encoded as a hex
572 string.  It is defined in RFC 2045.
574 The following functions are defined by the library:
576 @deffn Command quoted-printable-decode-region @var{from} @var{to} &optional @var{coding-system}
577 QP-decode all the encoded text in the region.  If @var{coding-system}
578 is non-nil, decode bytes into characters with that coding-system.  It
579 is probably better not to use @var{coding-system}; instead decode into
580 a unibyte buffer, decode that appropriately and then interpret it as
581 multibyte.
582 @end deffn
584 @defun quoted-printable-decode-string @var{string} &optional @var{coding-system}
585 Return a QP-encoded copy of @var{string}.  If @var{coding-system} is
586 non-nil, decode bytes into characters with that coding-system.
587 @end defun
589 @deffn Command quoted-printable-encode-region @var{from} @var{to} &optional @var{fold} @var{class}
590 QP-encode all the region.  If @var{fold} is non-@var{nil}, fold lines
591 at 76 characters, as required by the RFC.  If @var{class} is
592 non-@code{nil}, translate the characters not matched by that regexp
593 class, which should be in the form expected by
594 @var{skip-chars-forward} and should probably not contain literal
595 eight-bit characters.  Specifying @var{class} makes sense to do extra
596 encoding in header fields.
598 If variable @var{mm-use-ultra-safe-encoding} is defined and
599 non-@code{nil}, fold lines unconditionally and encode @samp{From } and
600 @samp{-} at the start of lines..
601 @end deffn
603 @defun quoted-printable-encode-string string
604 Return a QP-encoded copy of @var{string}.
605 @end defun
607 @node base64
608 @section base64
609 @cindex base64
611 Base64 is an encoding that encodes three bytes into four characters,
612 thereby increasing the size by about 33%.  The alphabet used for
613 encoding is very resistant to mangling during transit.  @xref{Base
614 64,,Base 64 Encoding, elisp, The Emacs Lisp Reference Manual}.
616 @node binhex
617 @section binhex
618 @cindex binhex
619 @cindex Apple
620 @cindex Macintosh
622 Binhex is an encoding that originated in Macintosh environments.
623 The following function is supplied to deal with these:
625 @defun binhex-decode-region start end &optional header-only
626 Decode the encoded text in the region @var{start} to @var{end}.  If
627 @var{header-only} is non-@code{nil}, only decode the @samp{binhex}
628 header and return the file name.
629 @end defun
632 @node uudecode
633 @section uudecode
634 @cindex uuencode
635 @cindex uudecode
637 Uuencoding is probably still the most popular encoding of binaries
638 used on Usenet, although Base64 rules the mail world.
640 The following function is supplied by this package:
642 @defun uudecode-decode-region start end &optional file-name
643 Decode the text in the region @var{start} to @var{end}.  If
644 @var{file-name} is non-@code{nil}, save the result to @var{file-name}.
645 @end defun
648 @node rfc1843
649 @section rfc1843
650 @cindex rfc1843
651 @cindex HZ
652 @cindex Chinese
654 RFC1843 deals with mixing Chinese and @sc{ascii} characters in messages.  In
655 essence, RFC1843 switches between @sc{ascii} and Chinese by doing this:
657 @example
658 This sentence is in ASCII.
659 The next sentence is in GB.~@{<:Ky2;S@{#,NpJ)l6HK!#~@}Bye.
660 @end example
662 Simple enough, and widely used in China.
664 The following functions are available to handle this encoding:
666 @defun rfc1843-decode-region start end
667 Decode HZ-encoded text in the region @var{start} to @var{end}.
668 @end defun
670 @defun rfc1843-decode-string string
671 Decode the HZ-encoded @var{string} and return the result.
672 @end defun
675 @node mailcap
676 @section mailcap
678 As specified by RFC 1524, @sc{mime}-aware message handlers parse
679 @dfn{mailcap} files from a default list, which can be overridden by the
680 @code{MAILCAP} environment variable.  These describe how elements are
681 supposed to be displayed.  Here's an example file:
683 @example
684 image/*; gimp -8 %s
685 audio/wav; wavplayer %s
686 @end example
688 This says that all image files should be displayed with @command{gimp},
689 and that WAVE audio files should be played by @code{wavplayer}.
691 The @code{mailcap} library parses such files, and provides functions for
692 matching types.
694 @defvar mailcap-mime-data
695 This variable is an alist of alists containing backup viewing rules for
696 @sc{mime} types.  These are overridden by rules for a type found in
697 mailcap files.  The outer alist is keyed on the major content-type and
698 the inner alists are keyed on the minor content-type (which can be a
699 regular expression).
701 @c Fixme: document this properly!
702 For example:
703 @example
704 (("application"
705   ("octet-stream"
706    (viewer . mailcap-save-binary-file)
707    (non-viewer . t)
708    (type . "application/octet-stream"))
709   ("plain"
710    (viewer . view-mode)
711    (test fboundp 'view-mode)
712    (type . "text/plain")))
713 @end example
714 @end defvar
716 @defopt mailcap-default-mime-data
717 This variable is the default value of @code{mailcap-mime-data}.  It
718 exists to allow setting the value using Custom.  It is merged with
719 values from mailcap files by @code{mailcap-parse-mailcaps}.
720 @end defopt
722 Although it is not specified by the RFC, @sc{mime} tools normally use a
723 common means of associating file extensions with defualt @sc{mime} types
724 in the absence of other information about the type of a file.  The
725 information is found in per-user files @file{~/.mime.types} and system
726 @file{mime.types} files found in quasi-standard places.  Here is an
727 example:
729 @example
730 application/x-dvi       dvi
731 audio/mpeg              mpga mpega mp2 mp3
732 image/jpeg              jpeg jpg jpe
733 @end example
736 @defvar mailcap-mime-extensions
737 This variable is an alist @sc{mime} types keyed by file extensions.
738 This is overridden by entries found in @file{mime.types} files.
739 @end defvar
741 @defopt mailcap-default-mime-extensions
742 This variable is the default value of @code{mailcap-mime-extensions}.
743 It exists to allow setting the value using Custom.  It is merged with
744 values from mailcap files by @code{mailcap-parse-mimetypes}.
745 @end defopt
747 Interface functions:
749 @defun mailcap-parse-mailcaps &optional path force
750 Parse all the mailcap files specified in a path string @var{path} and
751 merge them with the values from @code{mailcap-mime-data}.  Components of
752 @var{path} are separated by the @code{path-separator} character
753 appropriate for the system.  If @var{force} is non-@code{nil}, the files
754 are re-parsed even if they have been parsed already.  If @var{path} is
755 omitted, use the value of environment variable @code{MAILCAPS} if it is
756 set; otherwise (on GNU and Unix) use the path defined in RFC 1524, plus
757 @file{/usr/local/etc/mailcap}.
758 @end defun
760 @defun mailcap-parse-mimetypes &optional path force
761 Parse all the mimetypes specified in a path string @var{path}
762 and merge them with the values from @code{mailcap-mime-extensions}.
763 Components of @var{path} are separated by the @code{path-separator}
764 character appropriate for the system.  If @var{path} is omitted, use the
765 value of environment variable @code{MIMETYPES} if set; otherwise use a
766 default path consistent with that used by @code{mailcap-parse-mailcaps}.
767 If @var{force} is non-@code{nil}, the files are re-parsed even if they
768 have been parsed already.
769 @end defun
771 @defun mailcap-mime-info string &optional request
772 Gets the viewer command for content-type @var{string}.  @code{nil} is
773 returned if none is found.  Expects @var{string} to be a complete
774 content-type header line.
776 If @var{request} is non-@code{nil} it specifies what information to
777 return.  If it is nil or the empty string, the viewer (second field of
778 the mailcap entry) will be returned.  If it is a string, then the
779 mailcap field corresponding to that string will be returned
780 (@samp{print}, @samp{description}, whatever).  If it is a number, all
781 the information for this viewer is returned.  If it is @code{all}, then
782 all possible viewers for this type is returned.
783 @end defun
785 @defun mailcap-mime-types
786 This function returns a list of all the defined media types.
787 @end defun
789 @defun mailcap-extension-to-mime extension
790 This function returns the content type defined for a file with the given
791 @var{extension}.
792 @end defun
795 @node Decoding and Viewing
796 @chapter Decoding and Viewing
798 This chapter deals with decoding and viewing @sc{mime} messages on a
799 higher level.
801 The main idea is to first analyze a @sc{mime} article, and then allow
802 other programs to do things based on the list of @dfn{handles} that are
803 returned as a result of this analysis.
805 @menu
806 * Dissection::     Analyzing a @sc{mime} message.
807 * Handles::        Handle manipulations.
808 * Display::        Displaying handles.
809 * Customization::  Variables that affect display.
810 * New Viewers::    How to write your own viewers.
811 @end menu
814 @node Dissection
815 @section Dissection
817 The @code{mm-dissect-buffer} is the function responsible for dissecting
818 a @sc{mime} article.  If given a multipart message, it will recursively
819 descend the message, following the structure, and return a tree of
820 @sc{mime} handles that describes the structure of the message.
823 @node Handles
824 @section Handles
826 A @sc{mime} handle is a list that fully describes a @sc{mime} component.
828 The following macros can be used to access elements from the
829 @var{handle} argument:
831 @defmac mm-handle-buffer handle
832 Return the buffer that holds the contents of the undecoded @sc{mime}
833 part.
834 @end defmac
836 @defmac mm-handle-type handle
837 Return the parsed @samp{Content-Type} of the part.
838 @end defmac
840 @defmac mm-handle-encoding handle
841 Return the @samp{Content-Transfer-Encoding} of the part.
842 @end defmac
844 @defmac mm-handle-undisplayer handle
845 Return the function that can be used to remove the displayed part (if it
846 has been displayed).
847 @end defmac
849 @defmac mm-handle-set-undisplayer handle function
850 Set the undisplayer function for the part to function.
851 @end defmac
853 @defmac mm-handle-disposition
854 Return the parsed @samp{Content-Disposition} of the part.
855 @end defmac
857 @defmac mm-handle-disposition
858 Return the description of the part.
859 @end defmac
861 @defmac mm-get-content-id id
862 Returns the handle(s) referred to by @var{id}, the @samp{Content-ID} of
863 the part.
864 @end defmac
867 @node Display
868 @section Display
870 Functions for displaying, removing and saving.  In the descriptions
871 below, `the part' means the @sc{mime} part represented by the
872 @var{handle} argument.
874 @defun mm-display-part handle &optional no-default
875 Display the part.  Return @code{nil} if the part is removed,
876 @code{inline} if it is displayed inline or @code{external} if it is
877 displayed externally.  If @var{no-default} is non-@code{nil}, the part
878 is not displayed unless the @sc{mime} type of @var{handle} is defined to
879 be displayed inline or there is an display method defined for it; i.e.@:
880 no default external method will be used.
881 @end defun
883 @defun mm-remove-part handle
884 Remove the part if it has been displayed.
885 @end defun
887 @defun mm-inlinable-p handle
888 Return non-@code{nil} if the part can be displayed inline.
889 @end defun
891 @defun mm-automatic-display-p handle
892 Return non-@code{nil} if the user has requested automatic display of the
893 @sc{mime} type of the part.
894 @end defun
896 @defun mm-destroy-part handle
897 Free all the resources used by the part.
898 @end defun
900 @defun mm-save-part handle
901 Save the part to a file.  The user is prompted for a file name to use.
902 @end defun
904 @defun mm-pipe-part handle
905 Pipe the part through a shell command.  The user is prompted for the
906 command to use.
907 @end defun
909 @defun mm-interactively-view-part handle
910 Prompt for a mailcap method to use to view the part and display it
911 externally using that method.
912 @end defun
915 @node Customization
916 @section Customization
918 The display of @sc{mime} types may be customized with the following
919 options.
921 @defopt mm-inline-media-tests
922 This is an alist where the key is a @sc{mime} type, the second element
923 is a function to display the part @dfn{inline} (i.e., inside Emacs), and 
924 the third element is a form to be @code{eval}ed to say whether the part
925 can be displayed inline.
927 This variable specifies whether a part @emph{can} be displayed inline,
928 and, if so, how to do it.  It does not say whether parts are
929 @emph{actually} displayed inline.
930 @end defopt
932 @defopt mm-inlined-types
933 This, on the other hand, says what types are to be displayed inline, if
934 they satisfy the conditions set by the variable above.  It's a list of
935 @sc{mime} media types.
936 @end defopt
938 @defopt mm-automatic-display
939 This is a list of types that are to be displayed ``automatically'', but
940 only if the above variable allows it.  That is, only inlinable parts can
941 be displayed automatically.
942 @end defopt
944 @defopt mm-attachment-override-types
945 Some @sc{mime} agents create parts that have a content-disposition of
946 @samp{attachment}.  This variable allows overriding that disposition and 
947 displaying the part inline.  (Note that the disposition is only
948 overridden if we are able to, and want to, display the part inline.)
949 @end defopt
951 @defopt mm-discouraged-alternatives
952 List of @sc{mime} types that are discouraged when viewing
953 @samp{multipart/alternative}.  Viewing agents are supposed to view the
954 last possible part of a message, as that is supposed to be the richest.
955 However, users may prefer other types instead, and this list says what
956 types are most unwanted.  If, for instance, @samp{text/html} parts are
957 very unwanted, and @samp{text/richtech} parts are somewhat unwanted,
958 then the value of this variable should be set to:
960 @lisp
961 ("text/html" "text/richtext")
962 @end lisp
963 @end defopt
965 @defopt mm-inline-large-images-p
966 When displaying inline images that are larger than the window, XEmacs
967 does not enable scrolling, which means that you cannot see the whole
968 image.  To prevent this, the library tries to determine the image size
969 before displaying it inline, and if it doesn't fit the window, the
970 library will display it externally (e.g. with @samp{ImageMagick} or
971 @samp{xv}).  Setting this variable to @code{t} disables this check and
972 makes the library display all inline images as inline, regardless of
973 their size.
974 @end defopt
976 @defopt mm-inline-override-p
977 @code{mm-inlined-types} may include regular expressions, for example to
978 specify that all @samp{text/.*} parts be displayed inline.  If a user
979 prefers to have a type that matches such a regular expression be treated
980 as an attachment, that can be accomplished by setting this variable to a
981 list containing that type.  For example assuming @code{mm-inlined-types}
982 includes @samp{text/.*}, then including @samp{text/html} in this
983 variable will cause @samp{text/html} parts to be treated as attachments.
984 @end defopt
987 @node New Viewers
988 @section New Viewers
990 Here's an example viewer for displaying @samp{text/enriched} inline:
992 @lisp
993 (defun mm-display-enriched-inline (handle)
994   (let (text)
995     (with-temp-buffer
996       (mm-insert-part handle)
997       (save-window-excursion
998         (enriched-decode (point-min) (point-max))
999         (setq text (buffer-string))))
1000     (mm-insert-inline handle text)))
1001 @end lisp
1003 We see that the function takes a @sc{mime} handle as its parameter.  It
1004 then goes to a temporary buffer, inserts the text of the part, does some 
1005 work on the text, stores the result, goes back to the buffer it was
1006 called from and inserts the result.
1008 The two important helper functions here are @code{mm-insert-part} and
1009 @code{mm-insert-inline}.  The first function inserts the text of the
1010 handle in the current buffer.  It handles charset and/or content
1011 transfer decoding.  The second function just inserts whatever text you
1012 tell it to insert, but it also sets things up so that the text can be
1013 ``undisplayed' in a convenient manner.
1016 @node Composing
1017 @chapter Composing
1018 @cindex Composing
1019 @cindex MIME Composing
1020 @cindex MML
1021 @cindex MIME Meta Language
1023 Creating a @sc{mime} message is boring and non-trivial.  Therefore, a
1024 library called @code{mml} has been defined that parses a language called
1025 MML (@sc{mime} Meta Language) and generates @sc{mime} messages.
1027 @findex mml-generate-mime
1028 The main interface function is @code{mml-generate-mime}.  It will
1029 examine the contents of the current (narrowed-to) buffer and return a
1030 string containing the @sc{mime} message.
1032 @menu
1033 * Simple MML Example::             An example MML document.
1034 * MML Definition::                 All valid MML elements.
1035 * Advanced MML Example::           Another example MML document.
1036 * Charset Translation::            How charsets are mapped from Mule to MIME.
1037 * Conversion::                     Going from @sc{mime} to MML and vice versa.
1038 @end menu
1041 @node Simple MML Example
1042 @section Simple MML Example
1044 Here's a simple @samp{multipart/alternative}:
1046 @example
1047 <#multipart type=alternative>
1048 This is a plain text part.
1049 <#part type=text/enriched>
1050 <center>This is a centered enriched part</center>
1051 <#/multipart>
1052 @end example
1054 After running this through @code{mml-generate-mime}, we get this:
1056 @example
1057 Content-Type: multipart/alternative; boundary="=-=-="
1060 --=-=-=
1063 This is a plain text part.
1065 --=-=-=
1066 Content-Type: text/enriched
1069 <center>This is a centered enriched part</center>
1071 --=-=-=--
1072 @end example
1075 @node MML Definition
1076 @section MML Definition
1078 The MML language is very simple.  It looks a bit like an SGML
1079 application, but it's not.
1081 The main concept of MML is the @dfn{part}.  Each part can be of a
1082 different type or use a different charset.  The way to delineate a part
1083 is with a @samp{<#part ...>} tag.  Multipart parts can be introduced
1084 with the @samp{<#multipart ...>} tag.  Parts are ended by the
1085 @samp{<#/part>} or @samp{<#/multipart>} tags.  Parts started with the
1086 @samp{<#part ...>} tags are also closed by the next open tag.
1088 There's also the @samp{<#external ...>} tag.  These introduce
1089 @samp{external/message-body} parts.
1091 Each tag can contain zero or more parameters on the form
1092 @samp{parameter=value}.  The values may be enclosed in quotation marks,
1093 but that's not necessary unless the value contains white space.  So
1094 @samp{filename=/home/user/#hello$^yes} is perfectly valid.
1096 The following parameters have meaning in MML; parameters that have no
1097 meaning are ignored.  The MML parameter names are the same as the
1098 @sc{mime} parameter names; the things in the parentheses say which
1099 header it will be used in.
1101 @table @samp
1102 @item type
1103 The @sc{mime} type of the part (@samp{Content-Type}).
1105 @item filename
1106 Use the contents of the file in the body of the part
1107 (@samp{Content-Disposition}).
1109 @item charset
1110 The contents of the body of the part are to be encoded in the character
1111 set speficied (@samp{Content-Type}).
1113 @item name
1114 Might be used to suggest a file name if the part is to be saved
1115 to a file (@samp{Content-Type}).
1117 @item disposition
1118 Valid values are @samp{inline} and @samp{attachment}
1119 (@samp{Content-Disposition}).
1121 @item encoding
1122 Valid values are @samp{7bit}, @samp{8bit}, @samp{quoted-printable} and
1123 @samp{base64} (@samp{Content-Transfer-Encoding}).
1125 @item description
1126 A description of the part (@samp{Content-Description}).
1128 @item creation-date
1129 RFC822 date when the part was created (@samp{Content-Disposition}).
1131 @item modification-date
1132 RFC822 date when the part was modified (@samp{Content-Disposition}).
1134 @item read-date
1135 RFC822 date when the part was read (@samp{Content-Disposition}).
1137 @item size
1138 The size (in octets) of the part (@samp{Content-Disposition}).
1140 @end table
1142 Parameters for @samp{application/octet-stream}:
1144 @table @samp
1145 @item type
1146 Type of the part; informal---meant for human readers
1147 (@samp{Content-Type}).
1148 @end table
1150 Parameters for @samp{message/external-body}:
1152 @table @samp
1153 @item access-type
1154 A word indicating the supported access mechanism by which the file may
1155 be obtained.  Values include @samp{ftp}, @samp{anon-ftp}, @samp{tftp},
1156 @samp{localfile}, and @samp{mailserver}.  (@samp{Content-Type}.)
1158 @item expiration
1159 The RFC822 date after which the file may no longer be fetched.
1160 (@samp{Content-Type}.)
1162 @item size
1163 The size (in octets) of the file.  (@samp{Content-Type}.)
1165 @item permission
1166 Valid values are @samp{read} and @samp{read-write}
1167 (@samp{Content-Type}).
1169 @end table
1172 @node Advanced MML Example
1173 @section Advanced MML Example
1175 Here's a complex multipart message.  It's a @samp{multipart/mixed} that
1176 contains many parts, one of which is a @samp{multipart/alternative}.
1178 @example
1179 <#multipart type=mixed>
1180 <#part type=image/jpeg filename=~/rms.jpg disposition=inline>
1181 <#multipart type=alternative>
1182 This is a plain text part.
1183 <#part type=text/enriched name=enriched.txt>
1184 <center>This is a centered enriched part</center>
1185 <#/multipart>
1186 This is a new plain text part.
1187 <#part disposition=attachment>
1188 This plain text part is an attachment.
1189 <#/multipart>
1190 @end example
1192 And this is the resulting @sc{mime} message:
1194 @example
1195 Content-Type: multipart/mixed; boundary="=-=-="
1198 --=-=-=
1202 --=-=-=
1203 Content-Type: image/jpeg;
1204  filename="~/rms.jpg"
1205 Content-Disposition: inline;
1206  filename="~/rms.jpg"
1207 Content-Transfer-Encoding: base64
1209 /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRof
1210 Hh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/wAALCAAwADABAREA/8QAHwAA
1211 AQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQR
1212 BRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RF
1213 RkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ip
1214 qrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/9oACAEB
1215 AAA/AO/rifFHjldNuGsrDa0qcSSHkA+gHrXKw+LtWLrMb+RgTyhbr+HSug07xNqV9fQtZrNI
1216 AyiaE/NuBPOOOP0rvRNE880KOC8TbXXGCv1FPqjrF4LDR7u5L7SkTFT/ALWOP1xXgTuXfc7E
1217 sx6nua6rwp4IvvEM8chCxWxOdzn7wz6V9AaB4S07w9p5itow0rDLSY5Pt9K43xO66P4xs71m
1218 2QXiGCbA4yOVJ9+1aYORkdK434lyNH4ahCnG66VT9Nj15JFbPdX0MS43M4VQf5/yr2vSpLnw
1219 5ZW8dlCZ8KFXjOPX0/mK6rSPEGt3Angu44fNEReHYNvIH3TzXDeKNO8RX+kSX2ouZkicTIOc
1220 L+g7E810ulFjpVtv3bwgB3HJyK5L4quY/C9sVxk3ij/xx6850u7t1mtp/wDlpEw3An3Jr3Dw
1221 34gsbWza4nBlhC5LDsaW6+IFgupQyCF3iHH7gA7c9R9ay7zx6t7aX9jHC4smhfBkGCvHGfrm
1222 tLQ7hbnRrV1GPkAP1x1/Hr+Ncr8Vzjwrbf8AX6v/AKA9eQRyYlQk8Yx9K6XTNbkgia2ciSIn
1223 7p5Ga9Atte0LTLKO6it4i7dVRFJDcZ4PvXN+JvEMF9bILVGXJLSZ4zkjivRPDaeX4b08HOTC
1224 pOffmua+KkbS+GLVUGT9tT/0B68eeIpIFYjB70+OOVXyoOM9+M1eaWeCLzHPyHGO/NVWvJJm
1225 jQ8KGH1NfQWhXSXmh2c8eArRLwO3HSv/2Q==
1227 --=-=-=
1228 Content-Type: multipart/alternative; boundary="==-=-="
1231 --==-=-=
1234 This is a plain text part.
1236 --==-=-=
1237 Content-Type: text/enriched;
1238  name="enriched.txt"
1241 <center>This is a centered enriched part</center>
1243 --==-=-=--
1245 --=-=-=
1247 This is a new plain text part.
1249 --=-=-=
1250 Content-Disposition: attachment
1253 This plain text part is an attachment.
1255 --=-=-=--
1256 @end example
1258 @node Charset Translation
1259 @section Charset Translation
1260 @cindex charsets
1262 During translation from MML to @sc{mime}, for each @sc{mime} part which
1263 has been composed inside Emacs, an appropriate @sc{mime} charset has to
1264 be chosen.
1266 @vindex mail-parse-charset
1267 If you are running a non-Mule Emacs, this process is simple: if the part
1268 contains any non-@sc{ascii} (8-bit) characters, the @sc{mime} charset
1269 given by @code{mail-parse-charset} (a symbol) is used.  (Never set this
1270 variable directly, though.  If you want to change the default charset,
1271 please consult the documentation of the package which you use to process
1272 @sc{mime} messages.  @xref{Various Message Variables, , Various Message
1273 Variables, message, Message Manual}, for example.)  If there are only
1274 @sc{ascii} characters, the @sc{mime} charset @samp{US-ASCII} is used, of
1275 course.
1277 @cindex Mule
1278 @cindex UTF-8
1279 @cindex Unicode
1280 @vindex mm-mime-mule-charset-alist
1281 Things are slightly more complicated when running Emacs with Mule
1282 support.  In this case, a list of the Mule charsets used in the part is
1283 obtained, and the corresponding @sc{mime} charsets are determined.  If
1284 this results in a single @sc{mime} charset, this is used to encode the
1285 part.  But if the resulting list of @sc{mime} charsets contains more
1286 than one element, two things can happen: if it is possible to encode the
1287 part via UTF-8, this charset is used.  (For this, Emacs must support the
1288 @code{utf-8} coding system, and the part must consist entirely of
1289 characters which have Unicode counterparts.)  If UTF-8 is not available,
1290 the part is split into several, so that each one can be encoded with a
1291 single @sc{mime} charset.  The part can only be split at line
1292 boundaries, though---if more than one @sc{mime} charset is required to
1293 encode a single line, it is not possible to encode the part.
1295 @node Conversion
1296 @section Conversion
1298 @findex mime-to-mml
1299 A (multipart) @sc{mime} message can be converted to MML with the
1300 @code{mime-to-mml} function.  It works on the message in the current
1301 buffer, and substitutes MML markup for @sc{mime} boundaries.
1302 Non-textual parts do not have their contents in the buffer, but instead
1303 have the contents in separate buffers that are referred to from the MML
1304 tags.
1306 @findex mml-to-mime
1307 An MML message can be converted back to @sc{mime} by the
1308 @code{mml-to-mime} function.
1310 These functions are in certain senses ``lossy''---you will not get back
1311 an identical message if you run @sc{mime-to-mml} and then
1312 @sc{mml-to-mime}.  Not only will trivial things like the order of the
1313 headers differ, but the contents of the headers may also be different.
1314 For instance, the original message may use base64 encoding on text,
1315 while @sc{mml-to-mime} may decide to use quoted-printable encoding, and
1316 so on.
1318 In essence, however, these two functions should be the inverse of each
1319 other.  The resulting contents of the message should remain equivalent,
1320 if not identical.
1323 @node Standards
1324 @chapter Standards
1326 The Emacs @sc{mime} library implements handling of various elements
1327 according to a (somewhat) large number of RFCs, drafts and standards
1328 documents.  This chapter lists the relevant ones.  They can all be
1329 fetched from @samp{http://quimby.gnus.org/notes/}.
1331 @table @dfn
1332 @item RFC822
1333 @itemx STD11
1334 Standard for the Format of ARPA Internet Text Messages.
1336 @item RFC1036
1337 Standard for Interchange of USENET Messages
1339 @item RFC1524
1340 A User Agent Configuration Mechanism For Multimedia Mail Format
1341 Information
1343 @item RFC2045
1344 Format of Internet Message Bodies
1346 @item RFC2046
1347 Media Types
1349 @item RFC2047
1350 Message Header Extensions for Non-ASCII Text
1352 @item RFC2048
1353 Registration Procedures
1355 @item RFC2049
1356 Conformance Criteria and Examples
1358 @item RFC2231
1359 MIME Parameter Value and Encoded Word Extensions: Character Sets,
1360 Languages, and Continuations
1362 @item RFC1843
1363 HZ - A Data Format for Exchanging Files of Arbitrarily Mixed Chinese and
1364 ASCII characters
1366 @item draft-ietf-drums-msg-fmt-05.txt
1367 Draft for the successor of RFC822
1369 @item RFC2112
1370 The MIME Multipart/Related Content-type
1372 @item RFC1892
1373 The Multipart/Report Content Type for the Reporting of Mail System
1374 Administrative Messages
1376 @item RFC2183
1377 Communicating Presentation Information in Internet Messages: The
1378 Content-Disposition Header Field
1380 @end table
1383 @node Index
1384 @chapter Index
1385 @printindex cp
1386 @printindex fn
1388 @summarycontents
1389 @contents
1390 @bye
1392 @c End: