add HELO for sendmail
[elisp/flim.git] / mailcap.el
index 416b977..25595f0 100644 (file)
@@ -25,6 +25,9 @@
 
 ;;; Code:
 
+(require 'mime-def)
+
+
 ;;; @ comment
 ;;;
 
@@ -83,7 +86,8 @@
 
 (defsubst mailcap-look-at-schar ()
   (let ((chr (char-after (point))))
-    (if (and (>= chr ? )
+    (if (and chr
+            (>= chr ? )
             (/= chr ?\;)
             (/= chr ?\\)
             )
          (forward-char)))))
 
 (defsubst mailcap-look-at-qchar ()
-  (let ((chr (char-after (point))))
-    (when (eq chr ?\\)
-      (forward-char 2)
-      (char-before (point))
-      )))
+  (when (eq (char-after (point)) ?\\)
+    (prog2
+       (forward-char)
+       (char-after (point))
+      (forward-char))))
 
 (defsubst mailcap-look-at-mtext ()
   (let ((beg (point)))
@@ -167,8 +171,11 @@ order.  Otherwise result is not sorted."
            (t entries)
            ))))
 
-(defvar mailcap-file "~/.mailcap"
-  "*File name of user's mailcap file.")
+
+(defcustom mailcap-file "~/.mailcap"
+  "*File name of user's mailcap file."
+  :group 'mime
+  :type 'file)
 
 (defun mailcap-parse-file (&optional filename order)
   "Parse FILENAME as a mailcap, and return the result.
@@ -182,6 +189,78 @@ order.  Otherwise result is not sorted."
     (mailcap-parse-buffer (current-buffer) order)
     ))
 
+(defun mailcap-format-command (mtext situation)
+  "Return formated command string from MTEXT and SITUATION.
+
+MTEXT is a command text of mailcap specification, such as
+view-command.
+
+SITUATION is an association-list about information of entity.  Its key
+may be:
+
+       'type           primary media-type
+       'subtype        media-subtype
+       'filename       filename
+       STRING          parameter of Content-Type field"
+  (let ((i 0)
+       (len (length mtext))
+       (p 0)
+       dest)
+    (while (< i len)
+      (let ((chr (aref mtext i)))
+       (cond ((eq chr ?%)
+              (setq i (1+ i)
+                    chr (aref mtext i))
+              (cond ((eq chr ?s)
+                     (let ((file (cdr (assq 'filename situation))))
+                       (if (null file)
+                           (error "'filename is not specified in situation.")
+                         (setq dest (concat dest
+                                            (substring mtext p (1- i))
+                                            file)
+                               i (1+ i)
+                               p i)
+                         )))
+                    ((eq chr ?t)
+                     (let ((type (or (mime-type/subtype-string
+                                      (cdr (assq 'type situation))
+                                      (cdr (assq 'subtype situation)))
+                                     "text/plain")))
+                       (setq dest (concat dest
+                                          (substring mtext p (1- i))
+                                          type)
+                             i (1+ i)
+                             p i)
+                       ))
+                    ((eq chr ?\{)
+                     (setq i (1+ i))
+                     (if (not (string-match "}" mtext i))
+                         (error "parse error!!!")
+                       (let* ((me (match-end 0))
+                              (attribute (substring mtext i (1- me)))
+                              (parameter (cdr (assoc attribute situation))))
+                         (if (null parameter)
+                             (error "\"%s\" is not specified in situation."
+                                    attribute)
+                           (setq dest (concat dest
+                                              (substring mtext p (- i 2))
+                                              parameter)
+                                 i me
+                                 p i)
+                           )
+                         )))
+                    (t (error "Invalid sequence `%%%c'." chr))
+                    ))
+             ((eq chr ?\\)
+              (setq dest (concat dest (substring mtext p i))
+                    p (1+ i)
+                    i (+ i 2))
+              )
+             (t (setq i (1+ i)))
+             )))
+    (concat dest (substring mtext p))
+    ))
+
 
 ;;; @ end
 ;;;