(mime-show-echo-buffer): Bind `buffer-read-only' to nil, while insert
[elisp/semi.git] / mime-play.el
index 6a2fc1a..70fb849 100644 (file)
 (add-hook 'kill-emacs-hook 'mime-save-acting-situation-examples)
 
 (defun mime-reduce-acting-situation-examples ()
-  (let* ((rest mime-acting-situation-example-list)
-        (min-example (car rest))
-        (min-score (cdr min-example)))
-    (while rest
-      (let* ((example (car rest))
-            (score (cdr example)))
-       (cond ((< score min-score)
-              (setq min-score score
-                    min-example example)
-              )
-             ((= score min-score)
-              (if (<= (length (car example))(length (car min-example)))
-                  (setq min-example example)
-                ))
-             ))
-      (setq rest (cdr rest)))
-    (setq mime-acting-situation-example-list
-         (delq min-example mime-acting-situation-example-list))
-    (setq min-example (car min-example))
-    (let ((examples mime-acting-situation-example-list)
-         (max-score 0)
-         max-examples)
-      (while examples
-       (let* ((ret (mime-compare-situation-with-example min-example
-                                                        (caar examples)))
-              (ret-score (car ret)))
-         (cond ((> ret-score max-score)
-                (setq max-score ret-score
-                      max-examples (list (cdr ret)))
+  (let ((len (length mime-acting-situation-example-list))
+       i ir ic j jr jc ret
+       dest d-i d-j
+       (max-sim 0) sim
+       min-det-ret det-ret
+       min-det-org det-org
+       min-freq freq)
+    (setq i 0
+         ir mime-acting-situation-example-list)
+    (while (< i len)
+      (setq ic (car ir)
+           j 0
+           jr mime-acting-situation-example-list)
+      (while (< j len)
+       (unless (= i j)
+         (setq jc (car jr))
+         (setq ret (mime-compare-situation-with-example (car ic)(car jc))
+               sim (car ret)
+               det-ret (+ (length (car ic))(length (car jc)))
+               det-org (length (cdr ret))
+               freq (+ (cdr ic)(cdr jc)))
+         (cond ((< max-sim sim)
+                (setq max-sim sim
+                      min-det-ret det-ret
+                      min-det-org det-org
+                      min-freq freq
+                      d-i i
+                      d-j j
+                      dest (cons (cdr ret) freq))
                 )
-               ((= ret-score max-score)
-                (setq max-examples (cons (cdr ret) max-examples))
-                )))
-       (setq examples (cdr examples)))
-      (while max-examples
-       (let* ((example (car max-examples))
-              (cell (assoc example mime-acting-situation-example-list)))
-         (if cell
-             (setcdr cell (1+ (cdr cell)))
-           (setq mime-acting-situation-example-list
-                 (cons (cons example 0)
-                       mime-acting-situation-example-list))
-           ))
-       (setq max-examples (cdr max-examples))
-       ))))
+               ((= max-sim sim)
+                (cond ((> min-det-ret det-ret)
+                       (setq min-det-ret det-ret
+                             min-det-org det-org
+                             min-freq freq
+                             d-i i
+                             d-j j
+                             dest (cons (cdr ret) freq))
+                       )
+                      ((= min-det-ret det-ret)
+                       (cond ((> min-det-org det-org)
+                              (setq min-det-org det-org
+                                    min-freq freq
+                                    d-i i
+                                    d-j j
+                                    dest (cons (cdr ret) freq))
+                              )
+                             ((= min-det-org det-org)
+                              (cond ((> min-freq freq)
+                                     (setq min-freq freq
+                                           d-i i
+                                           d-j j
+                                           dest (cons (cdr ret) freq))
+                                     ))
+                              ))
+                       ))
+                ))
+         )
+       (setq jr (cdr jr)
+             j (1+ j)))
+      (setq ir (cdr ir)
+           i (1+ i)))
+    (if (> d-i d-j)
+       (setq i d-i
+             d-i d-j
+             d-j i))
+    (setq jr (nthcdr (1- d-j) mime-acting-situation-example-list))
+    (setcdr jr (cddr jr))
+    (if (= d-i 0)
+       (setq mime-acting-situation-example-list
+             (cdr mime-acting-situation-example-list))
+      (setq ir (nthcdr (1- d-i) mime-acting-situation-example-list))
+      (setcdr ir (cddr ir))
+      )
+    (if (setq ir (assoc (car dest) mime-acting-situation-example-list))
+       (setcdr ir (+ (cdr ir)(cdr dest)))
+      (setq mime-acting-situation-example-list
+           (cons dest mime-acting-situation-example-list))
+      )))
 
 
 ;;; @ content decoder
 ;;;
 
+;;;###autoload
 (defun mime-preview-play-current-entity (&optional ignore-examples mode)
   "Play current entity.
 It decodes current entity to call internal or external method.  The
@@ -124,8 +159,13 @@ If MODE is specified, play as it.  Default MODE is \"play\"."
   (interactive "P")
   (let ((entity (get-text-property (point) 'mime-view-entity)))
     (if entity
-       (mime-play-entity entity nil (or mode "play") ignore-examples)
-      )))
+       (let ((situation (list (cons 'mode (or mode "play")))))
+         (if ignore-examples
+             (setq situation
+                   (cons (cons 'ignore-examples ignore-examples)
+                         situation)))
+         (mime-play-entity entity situation)
+         ))))
 
 (defun mime-sort-situation (situation)
   (sort situation
@@ -193,29 +233,21 @@ If MODE is specified, play as it.  Default MODE is \"play\"."
     (cons match example)
     ))
 
-(defun mime-play-entity (entity &optional situation mode
-                               ignore-examples ignored-method)
+;;;###autoload
+(defun mime-play-entity (entity &optional situation ignored-method)
   "Play entity specified by ENTITY.
 It decodes the entity to call internal or external method.  The method
 is selected from variable `mime-acting-condition'.  If MODE is
 specified, play as it.  Default MODE is \"play\"."
   (let (method ret)
-    (or situation
-       (setq situation (mime-entity-situation entity)))
-    (if mode
-       (setq situation (cons (cons 'mode mode) situation))
-      )
-    (if ignore-examples
-       (or (assq 'ignore-examples situation)
-           (setq situation
-                 (cons (cons 'ignore-examples ignore-examples) situation)))
-      )
+    (in-calist-package 'mime-view)
     (setq ret
          (mime-delq-null-situation
-          (ctree-find-calist mime-acting-condition situation
+          (ctree-find-calist mime-acting-condition
+                             (mime-entity-situation entity situation)
                              mime-view-find-every-acting-situation)
           'method ignored-method))
-    (or ignore-examples
+    (or (assq 'ignore-examples situation)
        (if (cdr ret)
            (let ((rest ret)
                  (max-score 0)
@@ -352,33 +384,46 @@ Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
 window.")
 
 (defun mime-show-echo-buffer (&rest forms)
-  "Show mime-echo buffer to display MIME-playing information."
+  "Show mime-echo buffer to display MIME-playing information.
+It returns the list of window, start and end positions of inserted text.
+A window height of the buffer `mime-echo-buffer-name' will be determined
+by `mime-echo-window-height' (its value or its return value) whenever
+this function is called."
   (get-buffer-create mime-echo-buffer-name)
   (let ((the-win (selected-window))
-       (win (get-buffer-window mime-echo-buffer-name)))
-    (unless win
+       (win (get-buffer-window mime-echo-buffer-name))
+       (height (if (functionp mime-echo-window-height)
+                   (funcall mime-echo-window-height)
+                 mime-echo-window-height))
+       start)
+    (if win
+       (progn
+         (select-window win)
+         (enlarge-window (- height (window-height)))
+         )
       (unless (and mime-echo-window-is-shared-with-bbdb
                   (condition-case nil
-                      (setq win (get-buffer-window bbdb-buffer-name))
+                      (select-window
+                       (setq win (get-buffer-window bbdb-buffer-name))
+                       )
                     (error nil)))
        (select-window (get-buffer-window (or mime-preview-buffer
                                              (current-buffer))))
-       (setq win (split-window-vertically
-                  (- (window-height)
-                     (if (functionp mime-echo-window-height)
-                         (funcall mime-echo-window-height)
-                       mime-echo-window-height)
-                     )))
-       )
-      (set-window-buffer win mime-echo-buffer-name)
-      )
-    (select-window win)
-    (goto-char (point-max))
+       (let ((window-min-height 1))
+         (setq win (split-window-vertically (- (window-height) height)))
+         )
+       (set-window-buffer win mime-echo-buffer-name)
+       (select-window win)
+       ))
+    (goto-char (setq start (point-max)))
     (if forms
-       (insert (apply (function format) forms))
-      )
-    (select-window the-win)
-    ))
+       (let ((buffer-read-only nil))
+         (insert (apply (function format) forms))
+         ))
+    (prog1
+       (list win start (point))
+      (select-window the-win)
+      )))
 
 
 ;;; @ file name
@@ -475,8 +520,6 @@ SUBTYPE is symbol to indicate subtype of media-type.")
                    (put-alist 'subtype subtype
                               (del-alist 'method
                                          (copy-alist situation))))
-        nil
-        (cdr (assq 'ignore-examples situation))
         'mime-detect-content)
       ))
   )
@@ -504,9 +547,7 @@ It is registered to variable `mime-preview-quitting-method-alist'."
         (children (car (mime-entity-children entity))))
     (set-buffer (get-buffer-create new-name))
     (erase-buffer)
-    (insert-buffer-substring (mime-entity-buffer children)
-                            (mime-entity-point-min children)
-                            (mime-entity-point-max children))
+    (mime-insert-entity children)
     (setq mime-message-structure children)
     (setq major-mode 'mime-show-message-mode)
     (mime-view-buffer (current-buffer) nil mother
@@ -518,7 +559,6 @@ It is registered to variable `mime-preview-quitting-method-alist'."
 ;;;
 
 (defun mime-store-message/partial-piece (entity cal)
-  (goto-char (mime-entity-point-min entity))
   (let* ((root-dir
          (expand-file-name
           (concat "m-prts-" (user-login-name)) temporary-file-directory))
@@ -526,7 +566,7 @@ It is registered to variable `mime-preview-quitting-method-alist'."
         (number (cdr (assoc "number" cal)))
         (total (cdr (assoc "total" cal)))
         file
-        (mother mime-preview-buffer)
+        (mother (current-buffer))
         )
     (or (file-exists-p root-dir)
        (make-directory root-dir)
@@ -670,23 +710,21 @@ It is registered to variable `mime-preview-quitting-method-alist'."
 
 (defun mime-view-caesar (entity situation)
   "Internal method for mime-view to display ROT13-47-48 message."
-  (let* ((new-name (format "%s-%s" (buffer-name)
-                          (mime-entity-number entity)))
-        (mother mime-preview-buffer))
-    (let ((pwin (or (get-buffer-window mother)
-                   (get-largest-window)))
-         (buf (get-buffer-create new-name)))
-      (set-window-buffer pwin buf)
-      (set-buffer buf)
-      (select-window pwin)
+  (let ((buf (get-buffer-create
+             (format "%s-%s" (buffer-name) (mime-entity-number entity)))))
+    (with-current-buffer buf
+      (setq buffer-read-only nil)
+      (erase-buffer)
+      (mime-insert-text-content entity)
+      (mule-caesar-region (point-min) (point-max))
+      (set-buffer-modified-p nil)
       )
-    (setq buffer-read-only nil)
-    (erase-buffer)
-    (mime-insert-text-content entity)
-    (mule-caesar-region (point-min) (point-max))
-    (set-buffer-modified-p nil)
-    (set-buffer mother)
-    (view-buffer new-name)
+    (let ((win (get-buffer-window (current-buffer))))
+      (or (eq (selected-window) win)
+         (select-window (or win (get-largest-window)))
+         ))
+    (view-buffer buf)
+    (goto-char (point-min))
     ))