(easy-menu-create-keymaps): Add menu-alias property.
[bpt/emacs.git] / lisp / ediff-wind.el
index 6d75113..34775a9 100644 (file)
@@ -1,5 +1,6 @@
 ;;; ediff-wind.el --- window manipulation utilities
-;;; Copyright (C) 1994, 1995 Free Software Foundation, Inc.
+
+;; Copyright (C) 1994, 1995 Free Software Foundation, Inc.
 
 ;; Author: Michael Kifer <kifer@cs.sunysb.edu>
 
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to
-;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
 
+;;; Code:
 
 (require 'ediff-init)
 
 
-(defvar ediff-window-setup-function (if (not window-system)
-                                       'ediff-setup-windows-plain
-                                     'ediff-setup-windows-multiframe)
+(defvar ediff-window-setup-function (if (ediff-window-display-p)
+                                       'ediff-setup-windows-multiframe
+                                     'ediff-setup-windows-plain)
   "*Function called to set up windows.
 Ediff provides a choice of two functions: ediff-setup-windows-plain, for
 doing everything in one frame, and ediff-setup-windows-multiframe,
@@ -82,37 +85,52 @@ This variable has no effect when buffer-A/B/C are shown in different frames.
 In this case, Ediff will use those frames to display these buffers.")
 
 (defconst ediff-control-frame-parameters
-  (if window-system
-      (list 
-       '(name . "Ediff")
-       ;;'(unsplittable . t)
-       '(minibuffer . nil)
-       '(vertical-scroll-bars . nil)  ; FSF only
-       '(scrollbar-width . 0)         ; XEmacs only
-       '(menu-bar-lines . 0)          ; FSF only
-       ;; don't lower and auto-raise
-       '(auto-lower . nil)
-       '(auto-raise . t)
-       ;; this blocks queries from  window manager as to where to put
-       ;; ediff's control frame. we put the frame outside the display,
-       ;; so the initial frame won't jump all over the screen
-       (cons 'top  (if (fboundp 'x-display-pixel-height)
-                      (1+ (x-display-pixel-height))
-                    3000))
-       (cons 'left (if (fboundp 'x-display-pixel-width)
-                      (1+ (x-display-pixel-width))
-                    3000))
-       ))
+  (list 
+   '(name . "Ediff")
+   ;;'(unsplittable . t)
+   '(minibuffer . nil)
+   '(user-position . t)              ; Emacs only
+   '(vertical-scroll-bars . nil)  ; Emacs only
+   '(scrollbar-width . 0)         ; XEmacs only
+   '(menu-bar-lines . 0)          ; Emacs only
+   '(visibility . nil)       ; doesn't work for XEmacs yet
+   ;; don't lower and auto-raise
+   '(auto-lower . nil)
+   '(auto-raise . t)
+   ;; this blocks queries from  window manager as to where to put
+   ;; ediff's control frame. we put the frame outside the display,
+   ;; so the initial frame won't jump all over the screen
+   (cons 'top  (if (fboundp 'ediff-display-pixel-height)
+                  (1+ (ediff-display-pixel-height))
+                3000))
+   (cons 'left (if (fboundp 'ediff-display-pixel-width)
+                  (1+ (ediff-display-pixel-width))
+                3000))
+   )
   "Frame parameters for displaying Ediff Control Panel.
 Do not specify width and height here. These are computed automatically.")
 
+;; position of the mouse; used to decide whether to warp the mouse into ctl
+;; frame
+(ediff-defvar-local ediff-mouse-pixel-position nil "")
+
+;; not used for now
+(defvar ediff-mouse-pixel-threshold 30
+  "If the user moves mouse more than this many pixels, Ediff won't warp mouse into control window.")
+
+(defvar ediff-grab-mouse t
+  "*If t, Ediff will always grab the mouse and put it in the control frame.
+If 'maybe, Ediff will do it sometimes, but not after operations that require
+relatively long time. If nil, the mouse will be entirely user's
+responsibility.")
+
 (defvar ediff-control-frame-position-function 'ediff-make-frame-position
   "Function to call to determine the desired location for the control panel.
 Expects three parameters: the control buffer, the desired width and height
 of the control frame. It returns an association list
 of the form \(\(top . <position>\) \(left . <position>\)\)")
 
-(defvar ediff-control-frame-upward-shift (if ediff-xemacs-p 36 4)
+(defvar ediff-control-frame-upward-shift (if ediff-xemacs-p 42 14)
   "*The upward shift of control frame from the top of buffer A's frame.
 Measured in pixels.
 This is used by the default control frame positioning function,
@@ -160,7 +178,7 @@ the frame used for the wide display.")
 (defvar ediff-prefer-iconified-control-frame nil
   "*If t, keep control panel iconified when help message is off.
 This has effect only on a windowing system.
-If t, hiting `?' to toggle control panel off iconifies it.
+If t, hitting `?' to toggle control panel off iconifies it.
 
 This is only useful in Emacs and only for certain kinds of window managers,
 such as TWM and its derivatives, since the window manager must permit
@@ -186,28 +204,28 @@ into icons, regardless of the window manager.")
 
 ;; Select the lowest window on the frame.
 (defun ediff-select-lowest-window ()
-  (let* ((lowest-window (selected-window))
-        (bottom-edge (car (cdr (cdr (cdr (window-edges))))))
-        ;;(last-window (previous-window))
-         (last-window (save-excursion
-                       (other-window -1) (selected-window)))
-         (window-search t))
-    (while window-search
-      (let* ((this-window (next-window))
-             (next-bottom-edge (car (cdr (cdr (cdr 
-                                               (window-edges this-window)))))))
-        (if (< bottom-edge next-bottom-edge)
-            (progn
-              (setq bottom-edge next-bottom-edge)
-              (setq lowest-window this-window)))
-
-        (select-window this-window)
-        (if (eq last-window this-window)
-            (progn
-              (select-window lowest-window)
-              (setq window-search nil)))))))
+  (if ediff-xemacs-p
+      (select-window (frame-lowest-window))
+    (let* ((lowest-window (selected-window))
+          (bottom-edge (car (cdr (cdr (cdr (window-edges))))))
+          (last-window (save-excursion
+                         (other-window -1) (selected-window)))
+          (window-search t))
+      (while window-search
+       (let* ((this-window (next-window))
+              (next-bottom-edge
+               (car (cdr (cdr (cdr (window-edges this-window)))))))
+         (if (< bottom-edge next-bottom-edge)
+             (progn
+               (setq bottom-edge next-bottom-edge)
+               (setq lowest-window this-window)))
+         
+         (select-window this-window)
+         (if (eq last-window this-window)
+             (progn
+               (select-window lowest-window)
+               (setq window-search nil))))))))
 
 
 ;;; Common window setup routines
 
@@ -218,17 +236,19 @@ into icons, regardless of the window manager.")
 (defun ediff-setup-windows (buffer-A buffer-B buffer-C control-buffer)
   ;; Make sure we are not in the minibuffer window when we try to delete
   ;; all other windows.
-  (run-hooks 'ediff-before-setup-windows-hooks)
+  (run-hooks 'ediff-before-setup-windows-hook)
   (if (eq (selected-window) (minibuffer-window))
       (other-window 1))
       
-  ;; when support for buf C is added, the actual window setup function will
-  ;; depend on ediff-job-name
+  ;; in case user did a no-no on a tty
+  (or (ediff-window-display-p)
+      (setq ediff-window-setup-function 'ediff-setup-windows-plain))
+  
   (or (ediff-keep-window-config control-buffer)
       (funcall 
        (ediff-eval-in-buffer control-buffer ediff-window-setup-function)
        buffer-A buffer-B buffer-C control-buffer))
-  (run-hooks 'ediff-after-setup-windows-hooks))
+  (run-hooks 'ediff-after-setup-windows-hook))
 
 ;; Just set up 3 windows.
 ;; Usually used without windowing systems
@@ -243,14 +263,8 @@ into icons, regardless of the window manager.")
      buffer-A buffer-B buffer-C control-buffer)))
      
 (defun ediff-setup-windows-plain-merge (buf-A buf-B buf-C control-buffer)
-    ;; skip dedicated and unsplittable frames
-  (ediff-eval-in-buffer control-buffer
-    (if (and window-system (ediff-frame-live-p ediff-control-frame))
-       (progn
-         (redraw-display)
-         (ediff-delete-frame ediff-control-frame)
-         (setq ediff-control-frame nil))))
-  (ediff-skip-unsuitable-frames)
+  ;; skip dedicated and unsplittable frames
+  (ediff-destroy-control-frame control-buffer)
   (let ((window-min-height 1)
        split-window-function 
        merge-window-share merge-window-lines
@@ -271,9 +285,10 @@ into icons, regardless of the window manager.")
     (switch-to-buffer buf-A)
     (setq wind-A (selected-window))
     
-    ;; XEmacs seems to have a lot of trouble with display
-    ;; It won't set things right unless we tell it to sit still
-    (if ediff-xemacs-p (sit-for 0))
+    ;; XEmacs used to have a lot of trouble with display
+    ;; It did't set things right unless we tell it to sit still
+    ;; 19.12 seems ok.
+    ;;(if ediff-xemacs-p (sit-for 0))
     
     (split-window-vertically (max 2 (- (window-height) merge-window-lines)))
     (if (eq (selected-window) wind-A) 
@@ -302,17 +317,11 @@ into icons, regardless of the window manager.")
 ;; This function handles all comparison jobs, including 3way jobs
 (defun ediff-setup-windows-plain-compare (buf-A buf-B buf-C control-buffer)
   ;; skip dedicated and unsplittable frames
-  (ediff-eval-in-buffer control-buffer
-    (if (and window-system (ediff-frame-live-p ediff-control-frame))
-       (progn
-         (redraw-display)
-         (ediff-delete-frame ediff-control-frame)
-         (setq ediff-control-frame nil))))
-  (ediff-skip-unsuitable-frames)
+  (ediff-destroy-control-frame control-buffer)
   (let ((window-min-height 1)
        split-window-function wind-width-or-height
        three-way-comparison
-       job wind-A-start wind-B-start wind-A wind-B wind-C)
+       wind-A-start wind-B-start wind-A wind-B wind-C)
     (ediff-eval-in-buffer control-buffer
       (setq wind-A-start (ediff-overlay-start
                          (ediff-get-value-according-to-buffer-type
@@ -320,7 +329,6 @@ into icons, regardless of the window manager.")
            wind-B-start (ediff-overlay-start
                          (ediff-get-value-according-to-buffer-type
                           'B  ediff-narrow-bounds))
-           job ediff-job-name
            ;; this lets us have local versions of ediff-split-window-function
            split-window-function ediff-split-window-function
            three-way-comparison ediff-3way-comparison-job))
@@ -340,9 +348,10 @@ into icons, regardless of the window manager.")
                   (window-width wind-A))
                 3)))
     
-    ;; XEmacs seems to have a lot of trouble with display
-    ;; It won't set things right unless we tell it to sit still
-    (if ediff-xemacs-p (sit-for 0))
+    ;; XEmacs used to have a lot of trouble with display
+    ;; It did't set things right unless we told it to sit still
+    ;; 19.12 seems ok.
+    ;;(if ediff-xemacs-p (sit-for 0))
     
     (funcall split-window-function wind-width-or-height)
                  
@@ -366,7 +375,7 @@ into icons, regardless of the window manager.")
     
     ;; It is unlikely that we will want to implement 3way window comparison.
     ;; So, only buffers A and B are used here.
-    (if (eq job 'ediff-windows)
+    (if ediff-windows-job
        (progn
          (set-window-start wind-A wind-A-start)
          (set-window-start wind-B wind-B-start)))
@@ -376,7 +385,7 @@ into icons, regardless of the window manager.")
     ))
 
     
-;; dispatch the appropriate window setup function
+;; dispatch an appropriate window setup function
 (defun ediff-setup-windows-multiframe (buf-A buf-B buf-C control-buf)
   (ediff-eval-in-buffer control-buf
     (setq ediff-multiframe t))
@@ -394,17 +403,17 @@ into icons, regardless of the window manager.")
 ;;;   If buffers A, B, C are is separate frames, use them to display these
 ;;;   buffers.
 
-  ;;   Skip dedicated or iconified frames.  Unsplittable frames are taken
-  ;;   care of later.
+  ;;   Skip dedicated or iconified frames. 
+  ;;   Unsplittable frames are taken care of later.
   (ediff-skip-unsuitable-frames 'ok-unsplittable)
   
   (let* ((window-min-height 1)
         (wind-A (ediff-get-visible-buffer-window buf-A))
         (wind-B (ediff-get-visible-buffer-window buf-B))
         (wind-C (ediff-get-visible-buffer-window buf-C))
-        (frame-A (if wind-A (ediff-window-frame wind-A)))
-        (frame-B (if wind-B (ediff-window-frame wind-B)))
-        (frame-C (if wind-C (ediff-window-frame wind-C)))
+        (frame-A (if wind-A (window-frame wind-A)))
+        (frame-B (if wind-B (window-frame wind-B)))
+        (frame-C (if wind-C (window-frame wind-C)))
         ;; on wide display, do things in one frame
         (force-one-frame 
          (ediff-eval-in-buffer control-buf ediff-wide-display-p))
@@ -412,12 +421,14 @@ into icons, regardless of the window manager.")
         (split-window-function 
          (ediff-eval-in-buffer control-buf ediff-split-window-function))
         (orig-wind (selected-window))
-        (orig-frame (ediff-selected-frame))
+        (orig-frame (selected-frame))
         (use-same-frame (or force-one-frame
                             (eq frame-A (or frame-C orig-frame))
                             (eq frame-B (or frame-C orig-frame))
-                            (not (ediff-frame-live-p frame-A))
-                            (not (ediff-frame-live-p frame-B))
+                            (not (frame-live-p frame-A))
+                            (not (frame-live-p frame-B))
+                            (and (eq frame-A frame-B)
+                                 (not (frame-live-p frame-C)))
                             ))
         (use-same-frame-for-AB (and (not use-same-frame)
                                     (eq frame-A frame-B)))
@@ -458,7 +469,7 @@ into icons, regardless of the window manager.")
     
     (if use-same-frame-for-AB
        (progn 
-         (ediff-select-frame frame-A)
+         (select-frame frame-A)
          (switch-to-buffer buf-A)
          (delete-other-windows)
          (setq wind-A (selected-window))
@@ -473,11 +484,11 @@ into icons, regardless of the window manager.")
                done-B t)))
     
     (if use-same-frame
-       (let ((curr-frame (ediff-selected-frame))
+       (let ((curr-frame (selected-frame))
              (window-min-height 1))
          ;; avoid dedicated and non-splittable windows
          (ediff-skip-unsuitable-frames)
-         (or (eq curr-frame (ediff-selected-frame))
+         (or (eq curr-frame (selected-frame))
              (setq wind-A nil
                    wind-B nil
                    wind-C nil
@@ -494,9 +505,9 @@ into icons, regardless of the window manager.")
          (switch-to-buffer buf-A)
          (setq wind-A (selected-window))
          
-         ;; XEmacs seems to have a lot of trouble with display
-         ;; It won't set things right unless we tell it to catch breath
-         (if ediff-xemacs-p (sit-for 0))
+         ;; XEmacs used to have a lot of trouble with display
+         ;; It did't set things right unless we told it to catch breath
+         ;;(if ediff-xemacs-p (sit-for 0))
          
          (split-window-vertically
           (max 2 (- (window-height) merge-window-lines)))
@@ -545,12 +556,11 @@ into icons, regardless of the window manager.")
       (setq ediff-window-A wind-A
            ediff-window-B wind-B
            ediff-window-C wind-C)
-           
-      (setq frame-A (ediff-window-frame ediff-window-A)
-           designated-minibuffer-frame (ediff-window-frame
-                                        (minibuffer-window frame-A))))
-
-    (ediff-setup-control-frame control-buf)
+      (setq frame-A (window-frame ediff-window-A)
+           designated-minibuffer-frame
+           (window-frame (minibuffer-window frame-A))))
+    
+    (ediff-setup-control-frame control-buf designated-minibuffer-frame)
     ))
 
   
@@ -576,11 +586,11 @@ into icons, regardless of the window manager.")
         (wind-A (ediff-get-visible-buffer-window buf-A))
         (wind-B (ediff-get-visible-buffer-window buf-B))
         (wind-C (ediff-get-visible-buffer-window buf-C))
-        (frame-A (if wind-A (ediff-window-frame wind-A)))
-        (frame-B (if wind-B (ediff-window-frame wind-B)))
-        (frame-C (if wind-C (ediff-window-frame wind-C)))
+        (frame-A (if wind-A (window-frame wind-A)))
+        (frame-B (if wind-B (window-frame wind-B)))
+        (frame-C (if wind-C (window-frame wind-C)))
         (ctl-frame-exists-p (ediff-eval-in-buffer control-buf
-                              (ediff-frame-live-p ediff-control-frame)))
+                              (frame-live-p ediff-control-frame)))
         ;; on wide display, do things in one frame
         (force-one-frame 
          (ediff-eval-in-buffer control-buf ediff-wide-display-p))
@@ -595,16 +605,16 @@ into icons, regardless of the window manager.")
                             (if three-way-comparison
                                 (or (eq frame-A frame-C)
                                     (eq frame-B frame-C)
-                                    (not (ediff-frame-live-p frame-A))
-                                    (not (ediff-frame-live-p frame-B))
-                                    (not (ediff-frame-live-p frame-C))))
-                            (and (not (ediff-frame-live-p frame-B))
+                                    (not (frame-live-p frame-A))
+                                    (not (frame-live-p frame-B))
+                                    (not (frame-live-p frame-C))))
+                            (and (not (frame-live-p frame-B))
                                  (or ctl-frame-exists-p
-                                     (eq frame-A (ediff-selected-frame))))
-                            (and (not (ediff-frame-live-p frame-A))
+                                     (eq frame-A (selected-frame))))
+                            (and (not (frame-live-p frame-A))
                                  (or ctl-frame-exists-p
-                                     (eq frame-B (ediff-selected-frame))))))
-        wind-A-start wind-B-start job-name
+                                     (eq frame-B (selected-frame))))))
+        wind-A-start wind-B-start 
         designated-minibuffer-frame
         done-A done-B done-C)
     
@@ -614,8 +624,7 @@ into icons, regardless of the window manager.")
                           'A ediff-narrow-bounds))
            wind-B-start (ediff-overlay-start
                          (ediff-get-value-according-to-buffer-type
-                          'B ediff-narrow-bounds))
-           job-name ediff-job-name))
+                          'B ediff-narrow-bounds))))
     
     (if (and (window-live-p wind-A) (null use-same-frame)) ; buf-A on its own
        (progn
@@ -642,12 +651,12 @@ into icons, regardless of the window manager.")
          (setq done-C t)))
     
     (if use-same-frame
-       (let ((curr-frame (ediff-selected-frame))
+       (let ((curr-frame (selected-frame))
              ;; this affects 3way setups only
              wind-width-or-height)
          ;; avoid dedicated and non-splittable windows
          (ediff-skip-unsuitable-frames)
-         (or (eq curr-frame (ediff-selected-frame))
+         (or (eq curr-frame (selected-frame))
              (setq wind-A nil
                    wind-B nil
                    wind-C nil
@@ -662,9 +671,9 @@ into icons, regardless of the window manager.")
          (switch-to-buffer buf-A)
          (setq wind-A (selected-window))
          
-         ;; XEmacs seems to have a lot of trouble with display
-         ;; It won't set things right unless we tell it to catch breath
-         (if ediff-xemacs-p (sit-for 0))
+         ;; XEmacs used to have a lot of trouble with display
+         ;; It didn't set things right unless we told it to catch breath
+         ;;(if ediff-xemacs-p (sit-for 0))
          
          (if three-way-comparison
              (setq wind-width-or-height
@@ -720,33 +729,35 @@ into icons, regardless of the window manager.")
       (setq ediff-window-A wind-A
            ediff-window-B wind-B
            ediff-window-C wind-C)
-           
-      (setq frame-A (ediff-window-frame ediff-window-A)
-           designated-minibuffer-frame (ediff-window-frame
-                                        (minibuffer-window frame-A))))
+      
+      (setq frame-A (window-frame ediff-window-A)
+           designated-minibuffer-frame
+           (window-frame (minibuffer-window frame-A))))
     
     ;; It is unlikely that we'll implement ediff-windows that would compare
     ;; 3 windows at once. So, we don't use buffer C here.
-    (if (eq job-name 'ediff-windows)
+    (if ediff-windows-job
        (progn
          (set-window-start wind-A wind-A-start)
          (set-window-start wind-B wind-B-start)))
     
-    (ediff-setup-control-frame control-buf)
+    (ediff-setup-control-frame control-buf designated-minibuffer-frame)
     ))
 
 ;; skip unsplittable and dedicated windows
 ;; create a new splittable frame if none is found
 (defun ediff-skip-unsuitable-frames (&optional ok-unsplittable)
-  (if window-system
+  (if (ediff-window-display-p)
       (let (last-window)
        (while (and (not (eq (selected-window) last-window))
                    (or
                     (window-dedicated-p (selected-window))
-                    (ediff-frame-iconified-p (ediff-selected-frame))
+                    (ediff-frame-iconified-p (selected-frame))
+                    (< (frame-height (selected-frame))
+                       (* 3 window-min-height))
                     (if ok-unsplittable
                         nil
-                      (ediff-frame-unsplittable-p (ediff-selected-frame)))))
+                      (ediff-frame-unsplittable-p (selected-frame)))))
          ;; remember where started
          (or last-window (setq last-window (selected-window)))
          ;; try new window
@@ -754,46 +765,41 @@ into icons, regardless of the window manager.")
        (if (eq (selected-window) last-window)
            ;; fed up, no appropriate frame
            (progn
-             (redraw-display)
-             (ediff-select-frame (ediff-make-frame '((unsplittable)))))))))
+             ;;(redraw-display)
+             (select-frame (make-frame '((unsplittable)))))))))
 
 ;; Prepare or refresh control frame
-(defun ediff-setup-control-frame (ctl-buffer)
+(defun ediff-setup-control-frame (ctl-buffer designated-minibuffer-frame)
   (let ((window-min-height 1)
        ctl-frame-iconified-p dont-iconify-ctl-frame deiconify-ctl-frame
-       ctl-frame old-ctl-frame lines 
+       ctl-frame old-ctl-frame lines user-grabbed-mouse
        fheight fwidth adjusted-parameters) 
        
     (ediff-eval-in-buffer ctl-buffer
-      (run-hooks 'ediff-before-setup-control-frame-hooks))
+      (if ediff-xemacs-p (set-buffer-menubar nil))
+      ;;(setq user-grabbed-mouse (ediff-user-grabbed-mouse))
+      (run-hooks 'ediff-before-setup-control-frame-hook))
   
     (setq old-ctl-frame (ediff-eval-in-buffer ctl-buffer ediff-control-frame))
-    (if (and (ediff-frame-live-p old-ctl-frame)
-            (eq (window-frame (cdr (assq 'minibuffer (frame-parameters old-ctl-frame))))
-                designated-minibuffer-frame))
-       (setq ctl-frame old-ctl-frame)
-      (redraw-display)
-      ;; Make the frame while ctl-buff is current, so that
-      ;; ediff-control-frame-parameters will have the right value.
-      (ediff-eval-in-buffer ctl-buffer
-       (let ((default-minibuffer-frame designated-minibuffer-frame))
-         (setq ctl-frame (ediff-make-frame 
-                          ediff-control-frame-parameters))))
-      (ediff-eval-in-buffer ctl-buffer (setq ediff-control-frame ctl-frame)))
+    (ediff-eval-in-buffer ctl-buffer
+      (setq ctl-frame (if (frame-live-p old-ctl-frame)
+                         old-ctl-frame
+                       (make-frame ediff-control-frame-parameters))
+           ediff-control-frame ctl-frame))
     
     (setq ctl-frame-iconified-p (ediff-frame-iconified-p ctl-frame))
-    (ediff-select-frame ctl-frame)
+    (select-frame ctl-frame)
     (if (window-dedicated-p (selected-window))
        ()
       (delete-other-windows)
       (switch-to-buffer ctl-buffer))
       
     ;; must be before ediff-setup-control-buffer
-    (if ediff-xemacs-p
-       ;; just a precaution--we should be in ctl-buffer already
-       (ediff-eval-in-buffer ctl-buffer
-         (make-local-variable 'screen-title-format)
-         (make-local-variable 'screen-icon-title-format)))
+    ;; just a precaution--we should be in ctl-buffer already
+    (ediff-eval-in-buffer ctl-buffer
+      (make-local-variable 'frame-title-format)
+      (make-local-variable 'frame-icon-title-format)   ; XEmacs
+      (make-local-variable 'icon-title-format))        ; Emacs
     
     (ediff-setup-control-buffer ctl-buffer)
     (setq dont-iconify-ctl-frame
@@ -803,83 +809,116 @@ into icons, regardless of the window manager.")
               dont-iconify-ctl-frame))
     
     ;; 1 more line for the modeline
-    (setq lines (if ediff-xemacs-p
-                   (+ 2 (count-lines (point-min) (point-max)))
-                 (1+ (count-lines (point-min) (point-max))))
+    (setq lines (1+ (count-lines (point-min) (point-max)))
          fheight lines
          fwidth (+ (ediff-help-message-line-length) 2)
          adjusted-parameters (append (list
+                                      '(visibility . t)
+                                      ;; possibly change surrogate minibuffer
+                                      (cons 'minibuffer
+                                            (minibuffer-window
+                                             designated-minibuffer-frame))
                                       (cons 'width fwidth)
                                       (cons 'height fheight))
                                      (funcall
                                       ediff-control-frame-position-function
                                       ctl-buffer fwidth fheight)))
+    (if ediff-use-long-help-message
+       (setq adjusted-parameters
+             (cons '(auto-raise . nil) adjusted-parameters)))
     
     ;; In XEmacs, buffer menubar needs to be killed before frame parameters
-    ;; are changed XEmacs needs to redisplay, as it has trouble setting
-    ;; height correctly otherwise.
-    (if (and ediff-xemacs-p (ediff-frame-has-menubar))
-       (progn (set-buffer-menubar nil)(sit-for 0)))
+    ;; are changed. 
+    (if ediff-xemacs-p
+       (progn
+         (set-specifier top-toolbar-height (list ctl-frame 0))
+         (set-specifier bottom-toolbar-height (list ctl-frame 0))
+         (set-specifier left-toolbar-width (list ctl-frame 0))
+         (set-specifier right-toolbar-width (list ctl-frame 0))
+         ;; XEmacs needed a redisplay, as it had trouble setting
+         ;; height correctly otherwise.
+         ;;(sit-for 0)
+         ))
     
-    ;; Under OS/2 (emx) we have to call modify frame parameters twice, in
-    ;; order to make sure that at least once we do it for non-iconified
-    ;; frame. If appears that in the OS/2 port of Emacs, one can't modify
-    ;; frame parameters of iconified frames.
-    (if (eq system-type 'emx)
-       (ediff-modify-frame-parameters ctl-frame adjusted-parameters))
+    ;; Under OS/2 (emx) we have to call modify frame parameters twice, in order
+    ;; to make sure that at least once we do it for non-iconified frame. If
+    ;; appears that in the OS/2 port of Emacs, one can't modify frame
+    ;; parameters of iconified frames. As a precaution, we do likewise for
+    ;; windows-nt.
+    (if (memq system-type '(emx windows-nt windows-95))
+       (modify-frame-parameters ctl-frame adjusted-parameters))
       
     (goto-char (point-min))
     
-    (cond ((and ediff-prefer-iconified-control-frame
-               (not ctl-frame-iconified-p)
-               (not dont-iconify-ctl-frame))
-          (ediff-iconify-frame ctl-frame))
-         ((or deiconify-ctl-frame
-              (not ctl-frame-iconified-p))
-          (ediff-raise-frame ctl-frame)))
+    (modify-frame-parameters ctl-frame adjusted-parameters)
     
     ;; This works around a bug in 19.25 and earlier. There, if frame gets
     ;; iconified, the current buffer changes to that of the frame that
     ;; becomes exposed as a result of this iconification.
     ;; So, we make sure the current buffer doesn't change.
-    (ediff-select-frame ctl-frame)
+    (select-frame ctl-frame)
     (ediff-refresh-control-frame)
     
-    (ediff-modify-frame-parameters ctl-frame adjusted-parameters)
+    (cond ((and ediff-prefer-iconified-control-frame
+               (not ctl-frame-iconified-p) (not dont-iconify-ctl-frame))
+          (iconify-frame ctl-frame))
+         ((or deiconify-ctl-frame (not ctl-frame-iconified-p))
+          (raise-frame ctl-frame)))
     
     (if ediff-xemacs-p
        (set-window-buffer-dedicated (selected-window) ctl-buffer)
       (set-window-dedicated-p (selected-window) t))
       
-    (or ediff-xemacs-p (sit-for 0 200)) ; emacs has trouble here, needs time
+    ;; resynch so the cursor will move to control frame
+    ;; per RMS suggestion
+    (if (ediff-window-display-p)
+       (let ((count 7))
+         (sit-for .1)
+         (while (and (not (frame-visible-p ctl-frame)) (> count 0))
+           (setq count (1- count))
+           (sit-for .3))))
+
     (or (ediff-frame-iconified-p ctl-frame)
-       (set-mouse-position ctl-frame 1 0))
-    (if (ediff-check-version '< 19 11 'xemacs) (sit-for 0))
-    (or ediff-xemacs-p (unfocus-frame))
+       ;; don't warp the mouse, unless ediff-grab-mouse = t
+       (ediff-reset-mouse ctl-frame
+                          (or (eq this-command 'ediff-quit)
+                              (not (eq ediff-grab-mouse t)))))
        
     (if ediff-xemacs-p
        (ediff-eval-in-buffer ctl-buffer
-         (make-local-variable 'select-screen-hook)
-         (add-hook 'select-screen-hook 'ediff-xemacs-select-screen-hook)
+         (make-local-variable 'select-frame-hook)
+         (add-hook 'select-frame-hook 'ediff-xemacs-select-frame-hook)
          ))
        
     (ediff-eval-in-buffer ctl-buffer
-      (run-hooks 'ediff-after-setup-control-frame-hooks))
-  
-    ))  
+      (run-hooks 'ediff-after-setup-control-frame-hook))
+    ))
+    
+(defun ediff-destroy-control-frame (ctl-buffer)
+  (ediff-eval-in-buffer ctl-buffer
+    (if (and (ediff-window-display-p) (frame-live-p ediff-control-frame))
+       (let ((ctl-frame ediff-control-frame))
+         (if ediff-xemacs-p
+             (set-buffer-menubar default-menubar))
+         (setq ediff-control-frame nil)
+         (delete-frame ctl-frame)
+         )))
+  (ediff-skip-unsuitable-frames)
+  ;;(ediff-reset-mouse nil)
+  )
     
 
 ;; finds a good place to clip control frame
 (defun ediff-make-frame-position (ctl-buffer ctl-frame-width ctl-frame-height)
   (ediff-eval-in-buffer ctl-buffer
-    (let* ((frame-A (ediff-window-frame ediff-window-A))
-          (frame-A-parameters (ediff-frame-parameters frame-A))
-          (frame-A-top (cdr (assoc 'top frame-A-parameters)))
-          (frame-A-left (cdr (assoc 'left frame-A-parameters)))
-          (frame-A-width (ediff-frame-width frame-A))
+    (let* ((frame-A (window-frame ediff-window-A))
+          (frame-A-parameters (frame-parameters frame-A))
+          (frame-A-top (eval (cdr (assoc 'top frame-A-parameters))))
+          (frame-A-left (eval (cdr (assoc 'left frame-A-parameters))))
+          (frame-A-width (frame-width frame-A))
           (ctl-frame ediff-control-frame)
           horizontal-adjustment upward-adjustment
-          ctl-frame-top) 
+          ctl-frame-top ctl-frame-left
       
       ;; Multiple control frames are clipped based on the value of
       ;; ediff-control-buffer-number. This is done in order not to obscure
@@ -887,26 +926,40 @@ into icons, regardless of the window manager.")
       (setq horizontal-adjustment (* 2 ediff-control-buffer-number)
            upward-adjustment (* -14 ediff-control-buffer-number))
 
-      (setq ctl-frame-top (- frame-A-top
-                            upward-adjustment
-                            ediff-control-frame-upward-shift))
-      (list
-       (cons 'top (if (> ctl-frame-top 0) ctl-frame-top 1))
-       (cons 'left (+ frame-A-left
-                     (if ediff-prefer-long-help-message
-                         (* (ediff-frame-char-width ctl-frame)
-                            (+ ediff-wide-control-frame-rightward-shift
-                               horizontal-adjustment))
-                       (- (* frame-A-width
-                             (ediff-frame-char-width frame-A))
-                          (* (ediff-frame-char-width ctl-frame)
-                             (+ ctl-frame-width
-                                ediff-narrow-control-frame-leftward-shift
-                                horizontal-adjustment))))))))))
+      (setq ctl-frame-top
+           (- frame-A-top upward-adjustment ediff-control-frame-upward-shift)
+           ctl-frame-left
+           (+ frame-A-left
+              (if ediff-use-long-help-message
+                  (* (ediff-frame-char-width ctl-frame)
+                     (+ ediff-wide-control-frame-rightward-shift
+                        horizontal-adjustment))
+                (- (* frame-A-width (ediff-frame-char-width frame-A))
+                   (* (ediff-frame-char-width ctl-frame)
+                      (+ ctl-frame-width
+                         ediff-narrow-control-frame-leftward-shift
+                         horizontal-adjustment))))))
+      (setq ctl-frame-top
+           (min ctl-frame-top
+                (- (ediff-display-pixel-height)
+                   (* 2 ctl-frame-height
+                      (ediff-frame-char-height ctl-frame))))
+           ctl-frame-left
+           (min ctl-frame-left
+                (- (ediff-display-pixel-width)
+                   (* ctl-frame-width (ediff-frame-char-width ctl-frame)))))
+      ;; keep ctl frame within the visible bounds
+      (setq ctl-frame-top (max ctl-frame-top 1)
+           ctl-frame-left (max ctl-frame-left 1))
+      
+      (list (cons 'top ctl-frame-top)
+           (cons 'left ctl-frame-left))
+      )))
                               
-(defun ediff-xemacs-select-screen-hook ()
-  (if (equal (ediff-selected-frame) ediff-control-frame)
-      (ediff-raise-frame ediff-control-frame)))
+(defun ediff-xemacs-select-frame-hook ()
+  (if (and (equal (selected-frame) ediff-control-frame)
+          (not ediff-use-long-help-message))
+      (raise-frame ediff-control-frame)))
        
 (defun ediff-make-wide-display ()
   "Construct an alist of parameters for the wide display.
@@ -914,18 +967,17 @@ Saves the old frame parameters in `ediff-wide-display-orig-parameters'.
 The frame to be resized is kept in `ediff-wide-display-frame'.
 This function modifies only the left margin and the width of the display.
 It assumes that it is called from within the control buffer."
-  (if (not (fboundp 'x-display-pixel-width))
-      (error
-       "Can't determine display width. Please upgrade your version of Emacs"))
-  (let* ((frame-A (ediff-window-frame ediff-window-A))
-        (frame-A-params (ediff-frame-parameters frame-A))
+  (if (not (fboundp 'ediff-display-pixel-width))
+      (error "Can't determine display width."))
+  (let* ((frame-A (window-frame ediff-window-A))
+        (frame-A-params (frame-parameters frame-A))
         (cw (ediff-frame-char-width frame-A))
-        (wd (- (/ (x-display-pixel-width) cw) 5)))
+        (wd (- (/ (ediff-display-pixel-width) cw) 5)))
     (setq ediff-wide-display-orig-parameters 
-         (list (cons 'left (max 0 (cdr (assoc 'left frame-A-params))))
+         (list (cons 'left (max 0 (eval (cdr (assoc 'left frame-A-params)))))
                (cons 'width (cdr (assoc 'width frame-A-params))))
          ediff-wide-display-frame frame-A)
-    (ediff-modify-frame-parameters frame-A (list (cons 'left cw)
+    (modify-frame-parameters frame-A (list (cons 'left cw)
                                                 (cons 'width wd)))))
   
       
@@ -950,11 +1002,16 @@ It assumes that it is called from within the control buffer."
                            "")
         buf-C-state-diff (if (and (ediff-buffer-live-p ediff-buffer-C)
                                   (or buf-C-state-diff buf-C-state-merge))
-                             (format "[%s%s] "
+                             (format "[%s%s%s] "
                                      (or buf-C-state-diff "")
                                      (if buf-C-state-merge
                                          (concat " " buf-C-state-merge)
-                                       ""))
+                                       "")
+                                     (if (ediff-get-state-of-ancestor 
+                                          ediff-current-difference)
+                                         " AncestorEmpty"
+                                       "")
+                                     )
                            ""))
       (setq buf-A-state-diff ""
            buf-B-state-diff ""
@@ -973,7 +1030,7 @@ It assumes that it is called from within the control buffer."
     ;; Force mode-line redisplay
     (force-mode-line-update)
     
-    (if (and window-system (ediff-frame-live-p ediff-control-frame))
+    (if (and (ediff-window-display-p) (frame-live-p ediff-control-frame))
        (ediff-refresh-control-frame))
     
     (ediff-eval-in-buffer ediff-buffer-A
@@ -995,20 +1052,37 @@ It assumes that it is called from within the control buffer."
          (setq mode-line-format
                (list " C: " 'ediff-diff-status mode-line-format))
          (force-mode-line-update)))
+    (if (ediff-buffer-live-p ediff-ancestor-buffer)
+       (ediff-eval-in-buffer ediff-ancestor-buffer
+         (ediff-strip-mode-line-format)
+         ;; we keep the second dummy string in the mode line format of the
+         ;; ancestor, since for other buffers Ediff prepends 2 strings and
+         ;; ediff-strip-mode-line-format expects that.
+         (setq mode-line-format
+               (list " Ancestor: "
+                     (cond ((not (stringp buf-C-state-merge))
+                            "")
+                           ((string-match "prefer-A" buf-C-state-merge)
+                            "[=diff(B)] ")
+                           ((string-match "prefer-B" buf-C-state-merge)
+                            "[=diff(A)] ")
+                           (t ""))
+                     mode-line-format))))
     ))
     
   
 (defun ediff-refresh-control-frame ()
-  (if ediff-xemacs-p
-      (progn
-       (setq screen-title-format (ediff-make-narrow-control-buffer-id)
-             screen-icon-title-format (ediff-make-narrow-control-buffer-id))
-       ;; this forces update of the screen title
-       (ediff-modify-frame-parameters ediff-control-frame '(())))
-    (ediff-modify-frame-parameters
-     ediff-control-frame
-     (list (cons 'name (ediff-make-narrow-control-buffer-id))))
-     ))
+  (setq frame-title-format (ediff-make-narrow-control-buffer-id)
+       frame-icon-title-format (ediff-make-narrow-control-buffer-id) ; XEmacs
+       icon-title-format (ediff-make-narrow-control-buffer-id))      ; Emacs
+  ;; the emacs part will be modified once the 'name and 'title
+  ;; frame parameters are separated
+  (if ediff-emacs-p
+      (modify-frame-parameters
+       ediff-control-frame
+       (list (cons 'name (ediff-make-narrow-control-buffer-id))))
+    ;; force an update of the frame title
+    (modify-frame-parameters ediff-control-frame '(()))))
    
   
 (defun ediff-make-narrow-control-buffer-id (&optional skip-name)
@@ -1071,9 +1145,11 @@ It assumes that it is called from within the control buffer."
            (or (not ediff-3way-job)
                (eq (window-buffer C-wind) ediff-buffer-C))
            (string= ediff-window-config-saved
-                    (format "%S%S%S%S%S"
+                    (format "%S%S%S%S%S%S%S"
                             ctl-wind A-wind B-wind C-wind
-                            ediff-split-window-function)))))))
+                            ediff-split-window-function
+                            (ediff-multiframe-setup-p)
+                            ediff-wide-display-p)))))))
 
 
 (provide 'ediff-wind)