(vertical_scroll_bar_type): Add enumerated
[bpt/emacs.git] / lisp / mouse.el
index 3531329..8da7095 100644 (file)
@@ -1,25 +1,26 @@
-;;; mouse.el --- window system-independent mouse support.
+;;; mouse.el --- window system-independent mouse support
 
-;;; Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
+;; Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
 
 ;; Maintainer: FSF
 ;; Keywords: hardware
 
-;;; This file is part of GNU Emacs.
+;; This file is part of GNU Emacs.
 
-;;; GNU Emacs is free software; you can redistribute it and/or modify
-;;; it under the terms of the GNU General Public License as published by
-;;; the Free Software Foundation; either version 2, or (at your option)
-;;; any later version.
+;; GNU Emacs is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 2, or (at your option)
+;; any later version.
 
-;;; GNU Emacs is distributed in the hope that it will be useful,
-;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
-;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-;;; GNU General Public License for more details.
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; 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.
+;; 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, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
 
 ;;; Commentary:
 
   ;; Switch to the window clicked on, because otherwise
   ;; the mode's commands may not make sense.
   (interactive "@e")
-  (let ((newmap (make-sparse-keymap))
-       (unread-command-events (list event)))
-    ;; Make a keymap in which our last command leads to a menu
-    (define-key newmap (vector (car event))
-      (nconc (make-sparse-keymap "Menu")
-            (mouse-major-mode-menu-1
-             (and (current-local-map)
-                  (lookup-key (current-local-map) [menu-bar])))))
-    (mouse-major-mode-menu-compute-equiv-keys newmap)
-    ;; Make NEWMAP override the usual definition
-    ;; of the mouse button that got us here.
-    ;; Then read the user's menu choice.
-    (let* ((minor-mode-map-alist
-           (cons (cons t newmap) minor-mode-map-alist))
-          ;; read-key-sequence quits if the user aborts the menu.
-          ;; If that happens, do nothing silently.
-          (keyseq (condition-case nil
-                      (read-key-sequence "")
-                    (quit nil)))
-          (command (if keyseq (lookup-key newmap keyseq))))
-      (if command
-         (command-execute command)))))
+  (let (;; This is where mouse-major-mode-menu-prefix
+       ;; returns the prefix we should use (after menu-bar).
+       ;; It is either nil or (SOME-SYMBOL).
+       (mouse-major-mode-menu-prefix nil)
+       ;; Make a keymap in which our last command leads to a menu
+       (newmap (make-sparse-keymap (concat mode-name " Mode")))
+       result)
+    ;; Make our menu inherit from the desired keymap
+    ;; which we want to display as the menu now.
+    (set-keymap-parent newmap
+                      (mouse-major-mode-menu-1
+                       (and (current-local-map)
+                            (lookup-key (current-local-map) [menu-bar]))))
+    (setq result (x-popup-menu t (list newmap)))
+    (if result
+       (let ((command (key-binding
+                       (apply 'vector (append '(menu-bar)
+                                              mouse-major-mode-menu-prefix
+                                              result)))))
+         (if command
+             (command-execute command))))))
 
 ;; Compute and cache the equivalent keys in MENU and all its submenus.
-(defun mouse-major-mode-menu-compute-equiv-keys (menu)
-  (and (eq (car menu) 'keymap)
-       (x-popup-menu nil menu))
-  (while menu
-    (and (consp (car menu))
-        (consp (cdr (car menu)))
-        (let ((tail (cdr (car menu))))
-          (while (and (consp tail)
-                      (not (eq (car tail) 'keymap)))
-            (setq tail (cdr tail)))
-          (if (consp tail)
-              (mouse-major-mode-menu-compute-equiv-keys tail))))
-    (setq menu (cdr menu))))
+;;;(defun mouse-major-mode-menu-compute-equiv-keys (menu)
+;;;  (and (eq (car menu) 'keymap)
+;;;       (x-popup-menu nil menu))
+;;;  (while menu
+;;;    (and (consp (car menu))
+;;;     (consp (cdr (car menu)))
+;;;     (let ((tail (cdr (car menu))))
+;;;       (while (and (consp tail)
+;;;                   (not (eq (car tail) 'keymap)))
+;;;         (setq tail (cdr tail)))
+;;;       (if (consp tail)
+;;;           (mouse-major-mode-menu-compute-equiv-keys tail))))
+;;;    (setq menu (cdr menu))))
 
 ;; Given a mode's menu bar keymap,
 ;; if it defines exactly one menu bar menu,
          (if (consp (car tail))
              (if submap
                  (setq submap t)
-               (setq submap (cdr (car tail)))))
+               (setq submap (car tail))))
          (setq tail (cdr tail)))
-       (if (eq submap t) menubar
-         submap))))
+       (if (eq submap t)
+           menubar
+         (setq mouse-major-mode-menu-prefix (list (car submap)))
+         (cdr (cdr submap))))))
 \f
 ;; Commands that operate on windows.
 
@@ -128,8 +130,7 @@ This must be bound to a mouse click."
     (raise-frame frame)
     (select-frame frame)
     (or (eq frame oframe)
-       (set-mouse-position (selected-frame) (1- (frame-width)) 0))
-    (unfocus-frame)))
+       (set-mouse-position (selected-frame) (1- (frame-width)) 0))))
 
 (defun mouse-tear-off-window (click)
   "Delete the window clicked on, and create a new frame displaying its buffer."
@@ -400,8 +401,7 @@ This should be bound to a mouse drag event."
     ;; If mark is highlighted, no need to bounce the cursor.
     ;; On X, we highlight while dragging, thus once again no need to bounce.
     (or transient-mark-mode
-       (eq (framep (selected-frame)) 'x)
-       (eq (framep (selected-frame)) 'pc)
+       (memq (framep (selected-frame)) '(x pc ms-windows))
        (sit-for 1))
     (push-mark)
     (set-mark (point))
@@ -483,11 +483,12 @@ Upon exit, point is at the far edge of the newly visible text."
   "Set the region to the text that the mouse is dragged over.
 Highlight the drag area as you move the mouse.
 This must be bound to a button-down mouse event.
-In Transient Mark mode, the highlighting remains once you
-release the mouse button.  Otherwise, it does not."
+In Transient Mark mode, the highlighting remains as long as the mark
+remains active.  Otherwise, it remains until the next input event."
   (interactive "e")
   (mouse-minibuffer-check start-event)
-  (let* ((start-posn (event-start start-event))
+  (let* ((echo-keystrokes 0)
+        (start-posn (event-start start-event))
         (start-point (posn-point start-posn))
         (start-window (posn-window start-posn))
         (start-frame (window-frame start-window))
@@ -513,7 +514,7 @@ release the mouse button.  Otherwise, it does not."
     ;; end-of-range is used only in the single-click case.
     ;; It is the place where the drag has reached so far
     ;; (but not outside the window where the drag started).
-    (let (event end end-point (end-of-range (point)))
+    (let (event end end-point last-end-point (end-of-range (point)))
       (track-mouse
        (while (progn
                 (setq event (read-event))
@@ -523,6 +524,8 @@ release the mouse button.  Otherwise, it does not."
              nil
            (setq end (event-end event)
                  end-point (posn-point end))
+           (if end-point
+               (setq last-end-point end-point))
 
            (cond
             ;; Are we moving within the original window?
@@ -557,7 +560,7 @@ release the mouse button.  Otherwise, it does not."
            ;; Run the binding of the terminating up-event, if possible.
            ;; In the case of a multiple click, it gives the wrong results,
            ;; because it would fail to set up a region.
-           (if (and (= (mod mouse-selection-click-count 3) 0) (fboundp fun))
+           (if nil ;; (and (= (mod mouse-selection-click-count 3) 0) (fboundp fun))
                ;; In this case, we can just let the up-event execute normally.
                (let ((end (event-end event)))
                  ;; Set the position in the event before we replay it,
@@ -571,12 +574,32 @@ release the mouse button.  Otherwise, it does not."
                        (cons event unread-command-events)))
              (if (not (= (overlay-start mouse-drag-overlay)
                          (overlay-end mouse-drag-overlay)))
-                 (let (last-command this-command)
-                   (push-mark (overlay-start mouse-drag-overlay) t t)
-                   (goto-char (overlay-end mouse-drag-overlay))
-                   (delete-overlay mouse-drag-overlay)
+                 (let* ((stop-point (or (posn-point (event-end event)) last-end-point))
+                        ;; The end that comes from where we ended the drag.
+                        ;; Point goes here.
+                        (region-termination
+                         (if (and stop-point (< stop-point start-point))
+                             (overlay-start mouse-drag-overlay)
+                           (overlay-end mouse-drag-overlay)))
+                        ;; The end that comes from where we started the drag.
+                        ;; Mark goes there.
+                        (region-commencement
+                         (- (+ (overlay-end mouse-drag-overlay)
+                               (overlay-start mouse-drag-overlay))
+                            region-termination))
+                        last-command this-command)
+                   (push-mark region-commencement t t)
+                   (goto-char region-termination)
                    (copy-region-as-kill (point) (mark t))
-                   (mouse-set-region-1))
+                   (let ((buffer (current-buffer)))
+                     (mouse-show-mark)
+                     ;; mouse-show-mark can call read-event,
+                     ;; and that means the Emacs server could switch buffers
+                     ;; under us.  If that happened, 
+                     ;; avoid trying to use the region.
+                     (and (mark t) mark-active
+                          (eq buffer (current-buffer))
+                          (mouse-set-region-1))))
                (goto-char (overlay-end mouse-drag-overlay))
                (setq this-command 'mouse-set-point)
                (delete-overlay mouse-drag-overlay))))
@@ -635,6 +658,31 @@ If DIR is positive skip forward; if negative, skip backward."
                 (backward-sexp 1)
                 (point))
               (1+ start)))
+       ((and (= mode 1)
+              (= start end)
+             (char-after start)
+              (= (char-syntax (char-after start)) ?\"))
+        (let ((open (or (eq start (point-min))
+                        (save-excursion
+                          (goto-char (- start 1))
+                          (looking-at "\\s(\\|\\s \\|\\s>")))))
+          (if open
+              (list start
+                    (save-excursion
+                      (condition-case nil
+                          (progn 
+                            (goto-char start)
+                            (forward-sexp 1)
+                            (point))
+                        (error end))))
+            (list (1+ start)
+                  (save-excursion
+                    (condition-case nil
+                        (progn
+                          (goto-char (1+ start))
+                          (backward-sexp 1)
+                          (point))
+                      (error end)))))))
         ((= mode 1)
         (list (save-excursion
                 (goto-char start)
@@ -663,12 +711,66 @@ If DIR is positive skip forward; if negative, skip backward."
     (if (numberp (posn-point posn))
        (push-mark (posn-point posn) t t))))
 
+(defun mouse-undouble-last-event (events)
+  (let* ((index (1- (length events)))
+        (last (nthcdr index events))
+        (event (car last))
+        (basic (event-basic-type event))
+        (modifiers (delq 'double (delq 'triple (copy-sequence (event-modifiers event)))))
+        (new
+         (if (consp event)
+             (cons (event-convert-list (nreverse (cons basic modifiers)))
+                   (cdr event))
+           event)))
+    (setcar last new)
+    (if (key-binding (apply 'vector events))
+       t
+      (setcar last event)
+      nil)))
+
 ;; Momentarily show where the mark is, if highlighting doesn't show it. 
+
+(defvar mouse-region-delete-keys '([delete])
+  "List of keys which shall cause the mouse region to be deleted.")
+
 (defun mouse-show-mark ()
-  (or transient-mark-mode
+  (if transient-mark-mode
+      (if window-system
+         (delete-overlay mouse-drag-overlay))
+    (if window-system
+       (let ((inhibit-quit t)
+             (echo-keystrokes 0)
+             event events key ignore
+             x-lost-selection-hooks)
+         (add-hook 'x-lost-selection-hooks
+                   '(lambda (seltype)
+                      (if (eq seltype 'PRIMARY)
+                          (progn (setq ignore t)
+                                 (throw 'mouse-show-mark t)))))
+         (move-overlay mouse-drag-overlay (point) (mark t))
+         (catch 'mouse-show-mark
+           (while (progn (setq event (read-event))
+                         (setq events (append events (list event)))
+                         (setq key (apply 'vector events))
+                         (and (memq 'down (event-modifiers event))
+                              (not (key-binding key))
+                              (not (member key mouse-region-delete-keys))
+                              (not (mouse-undouble-last-event events))))))
+         ;; If we lost the selection, just turn off the highlighting.
+         (if ignore
+             nil
+           ;; For certain special keys, delete the region.
+           (if (member key mouse-region-delete-keys)
+               (delete-region (overlay-start mouse-drag-overlay)
+                              (overlay-end mouse-drag-overlay))
+             ;; Otherwise, unread the key so it gets executed normally.
+             (setq unread-command-events
+                   (nconc events unread-command-events))))
+         (setq quit-flag nil)
+         (delete-overlay mouse-drag-overlay))
       (save-excursion
-       (goto-char (mark t))
-       (sit-for 1))))
+       (goto-char (mark t))
+       (sit-for 1)))))
 
 (defun mouse-set-mark (click)
   "Set mark at the position clicked on with the mouse.
@@ -826,6 +928,8 @@ If you do this twice in the same position, the selection is killed."
              (mouse-save-then-kill-delete-region (point) (mark))
              ;; After we kill, another click counts as "the first time".
              (setq mouse-save-then-kill-posn nil))
+         ;; This is not a repetition.
+         ;; We are adjusting an old selection or creating a new one.
          (if (or (and (eq last-command 'mouse-save-then-kill)
                       mouse-save-then-kill-posn)
                  (and mark-active transient-mark-mode)
@@ -852,7 +956,9 @@ If you do this twice in the same position, the selection is killed."
            (if before-scroll
                (goto-char before-scroll))
            (exchange-point-and-mark)
-           (kill-new (buffer-substring (point) (mark t))))
+           (kill-new (buffer-substring (point) (mark t)))
+           (if window-system
+               (mouse-show-mark)))
          (mouse-set-region-1)
          (setq mouse-save-then-kill-posn
                (list (car kill-ring) (point) click-posn)))))))
@@ -915,7 +1021,8 @@ Highlight the drag area as you move the mouse.
 This must be bound to a button-down mouse event."
   (interactive "e")
   (mouse-minibuffer-check start-event)
-  (let* ((start-posn (event-start start-event))
+  (let* ((echo-keystrokes 0)
+        (start-posn (event-start start-event))
         (start-point (posn-point start-posn))
         (start-window (posn-window start-posn))
         (start-frame (window-frame start-window))
@@ -1151,50 +1258,74 @@ again.  If you do this twice in the same position, it kills the selection."
                            (overlay-start mouse-secondary-overlay)
                            (overlay-end mouse-secondary-overlay)))))))
 \f
+(defvar mouse-menu-buffer-maxlen 20
+  "*Number of buffers in one pane (submenu) of the buffer menu.
+If we have lots of buffers, divide them into groups of
+`mouse-menu-buffer-maxlen' and make a pane (or submenu) for each one.")
+
 (defun mouse-buffer-menu (event)
   "Pop up a menu of buffers for selection with the mouse.
 This switches buffers in the window that you clicked on,
 and selects that window."
   (interactive "e")
   (mouse-minibuffer-check event)
-  (let ((menu
-        (list "Buffer Menu"
-              (cons "Select Buffer"
-                    (let ((tail (buffer-list))
-                          (maxbuf 0)
-                          head)
-                      (while tail
-                        (or (eq ?\ (aref (buffer-name (car tail)) 0))
-                            (setq maxbuf
-                                  (max maxbuf
-                                       (length (buffer-name (car tail))))))
-                        (setq tail (cdr tail)))
-                      (setq tail (buffer-list))
-                      (while tail
-                        (let ((elt (car tail)))
-                          (if (not (string-match "^ "
-                                                 (buffer-name elt)))
-                              (setq head
-                               (cons
-                                (cons
-                                 (format
-                                  (format "%%%ds  %%s%%s  %%s" maxbuf)
-                                  (buffer-name elt)
-                                  (if (buffer-modified-p elt) "*" " ")
-                                  (save-excursion
-                                    (set-buffer elt)
-                                    (if buffer-read-only "%" " "))
-                                  (or (buffer-file-name elt) 
-                                      (save-excursion
-                                        (set-buffer elt)
-                                        (if list-buffers-directory
-                                            (expand-file-name
-                                             list-buffers-directory)))
-                                      ""))
-                                 elt)
-                                head))))
-                        (setq tail (cdr tail)))
-                      (reverse head))))))
+  (let* ((buffers
+         ;; Make an alist of (MENU-ITEM . BUFFER).
+         (let ((tail (buffer-list))
+               (maxlen 0)
+               head)
+           (while tail
+             (or (eq ?\ (aref (buffer-name (car tail)) 0))
+                 (setq maxlen
+                       (max maxlen
+                            (length (buffer-name (car tail))))))
+             (setq tail (cdr tail)))
+           (setq tail (buffer-list))
+           (while tail
+             (let ((elt (car tail)))
+               (if (/= (aref (buffer-name elt) 0) ?\ )
+                   (setq head
+                         (cons
+                          (cons
+                           (format
+                            (format "%%%ds  %%s%%s  %%s" maxlen)
+                            (buffer-name elt)
+                            (if (buffer-modified-p elt) "*" " ")
+                            (save-excursion
+                              (set-buffer elt)
+                              (if buffer-read-only "%" " "))
+                            (or (buffer-file-name elt) 
+                                (save-excursion
+                                  (set-buffer elt)
+                                  (if list-buffers-directory
+                                      (expand-file-name
+                                       list-buffers-directory)))
+                                ""))
+                           elt)
+                          head))))
+             (setq tail (cdr tail)))
+           ;; Compensate for the reversal that the above loop does.
+           (nreverse head)))
+        (menu
+         ;; If we have lots of buffers, divide them into groups of 20
+         ;; and make a pane (or submenu) for each one.
+         (if (> (length buffers) (/ (* mouse-menu-buffer-maxlen 3) 2))
+             (let ((buffers buffers) sublists next
+                   (i 1))
+               (while buffers
+                 ;; Pull off the next mouse-menu-buffer-maxlen buffers
+                 ;; and make them the next element of sublist.
+                 (setq next (nthcdr mouse-menu-buffer-maxlen buffers))
+                 (if next
+                     (setcdr (nthcdr (1- mouse-menu-buffer-maxlen) buffers)
+                             nil))
+                 (setq sublists (cons (cons (format "Buffers %d" i) buffers)
+                                      sublists))
+                 (setq i (1+ i))
+                 (setq buffers next))
+               (cons "Buffer Menu" (nreverse sublists)))
+           ;; Few buffers--put them all in one pane.
+           (list "Buffer Menu" (cons "Select Buffer" buffers)))))
     (let ((buf (x-popup-menu event menu))
          (window (posn-window (event-start event))))
       (if buf