1 ;;; bg-mouse.el --- GNU Emacs code for BBN Bitgraph mouse
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 ;; Author: John Robinson <jr@bbn-unix.arpa>
6 ;; Stephen Gildea <gildea@bbn.com>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
31 ;;; Original version by John Robinson (jr@bbn-unix.arpa, bbncca!jr), Oct 1985
32 ;;; Modularized and enhanced by gildea@bbn.com Nov 1987
33 ;;; Time stamp <89/03/21 14:27:08 gildea>
35 ;;; User customization option:
37 (defvar bg-mouse-fast-select-window nil
38 "*Non-nil for mouse hits to select new window, then execute; else just select.")
40 ;;; These numbers are summed to make the index into the mouse-map.
41 ;;; The low three bits correspond to what the mouse actually sends.
42 (defconst bg-button-r 1)
43 (defconst bg-button-m 2)
44 (defconst bg-button-c 2)
45 (defconst bg-button-l 4)
46 (defconst bg-in-modeline 8)
47 (defconst bg-in-scrollbar 16)
48 (defconst bg-in-minibuf 24)
50 ;;; semicolon screws up indenting, so use this instead
51 (defconst semicolon ?\;)
55 (defvar bg-cursor-window)
56 ;; This variable does not exist since 1991, so it's a safe bet
57 ;; this package is not really used anymore. Still...
62 (defun bg-mouse-report (prefix-arg)
63 "Read, parse, and execute a BBN BitGraph mouse click.
65 L-- move point | These apply for mouse click in a window.
66 --R set mark | If bg-mouse-fast-select-window is nil,
67 L-R kill region | these commands on a nonselected window
68 -C- move point and yank | just select that window.
70 -CR or LCR undo | \"Scroll bar\" is right-hand window column.
72 on modeline: on \"scroll bar\": in minibuffer:
73 L-- scroll-up line to top execute-extended-command
74 --R scroll-down line to bottom eval-expression
75 -C- proportional goto-char line to middle suspend-emacs
77 To reinitialize the mouse if the terminal is reset, type ESC : RET"
79 (bg-get-tty-num semicolon)
81 ((screen-mouse-x (min (1- (frame-width)) ;don't hit column 86!
82 (/ (bg-get-tty-num semicolon) 9)))
83 (screen-mouse-y (- (1- (frame-height)) ;assume default font size.
84 (/ (bg-get-tty-num semicolon) 16)))
85 (bg-mouse-buttons (% (bg-get-tty-num ?c) 8))
86 (bg-mouse-window (bg-window-from-x-y screen-mouse-x screen-mouse-y))
87 (bg-cursor-window (selected-window))
88 (edges (window-edges bg-mouse-window))
89 (minibuf-p (= screen-mouse-y (1- (frame-height))))
90 (in-modeline-p (and (not minibuf-p)
91 (= screen-mouse-y (1- (nth 3 edges)))))
92 (in-scrollbar-p (and (not minibuf-p) (not in-modeline-p)
93 (>= screen-mouse-x (1- (nth 2 edges)))))
94 (same-window-p (eq bg-mouse-window bg-cursor-window))
95 (in-minibuf-p (and minibuf-p
96 (not bg-mouse-window))) ;minibuf must be inactive
97 (bg-mode-bits (+ (if in-minibuf-p bg-in-minibuf 0)
98 (if in-modeline-p bg-in-modeline 0)
99 (if in-scrollbar-p bg-in-scrollbar 0)))
101 (lookup-key mouse-map
102 (char-to-string (+ bg-mode-bits bg-mouse-buttons))))
103 (bg-mouse-x (- screen-mouse-x (nth 0 edges)))
104 (bg-mouse-y (- screen-mouse-y (nth 1 edges))))
105 (cond ((or in-modeline-p in-scrollbar-p)
106 (select-window bg-mouse-window)
107 (bg-command-execute bg-command)
108 (select-window bg-cursor-window))
109 ((or same-window-p in-minibuf-p)
110 (bg-command-execute bg-command))
111 (t ;in another window
112 (select-window bg-mouse-window)
113 (if bg-mouse-fast-select-window
114 (bg-command-execute bg-command)))
118 ;;; Library of commands:
120 (defun bg-set-point ()
121 "Move point to location of BitGraph mouse."
123 (bg-move-point-to-x-y bg-mouse-x bg-mouse-y)
124 (setq this-command 'next-line) ;make subsequent line moves work
125 (setq temporary-goal-column bg-mouse-x))
127 (defun bg-set-mark ()
128 "Set mark at location of BitGraph mouse."
131 (bg-move-point-to-x-y bg-mouse-x bg-mouse-y)
132 (exchange-point-and-mark))
135 "Move point to location of BitGraph mouse and yank."
137 (bg-move-point-to-x-y bg-mouse-x bg-mouse-y)
138 (setq this-command 'yank)
145 (defun bg-yank-or-pop ()
146 "Move point to location of BitGraph mouse and yank. If last command
147 was a yank, do a yank-pop."
149 (if (eq last-command 'yank)
153 ;;; In 18.51, Emacs Lisp doesn't provide most-positive-fixnum
154 (defconst bg-most-positive-fixnum 8388607)
156 (defun bg-move-by-percentage ()
157 "Go to location in buffer that is the same percentage of the way
158 through the buffer as the BitGraph mouse's X position in the window."
160 ;; check carefully for overflow in intermediate calculations
162 (cond ((zerop bg-mouse-x)
164 ((< (buffer-size) (/ bg-most-positive-fixnum bg-mouse-x))
165 ;; no danger of overflow: compute it exactly
166 (/ (* bg-mouse-x (buffer-size))
167 (1- (window-width))))
169 ;; overflow possible: approximate
170 (* (/ (buffer-size) (1- (window-width)))
173 (what-cursor-position))
175 (defun bg-mouse-line-to-top ()
176 "Scroll the line pointed to by the BitGraph mouse to the top of the window."
178 (scroll-up bg-mouse-y))
180 (defun bg-mouse-line-to-center ()
181 "Scroll the line pointed to by the BitGraph mouse to the center
184 (scroll-up (/ (+ 2 bg-mouse-y bg-mouse-y (- (window-height))) 2)))
186 (defun bg-mouse-line-to-bottom ()
187 "Scroll the line pointed to by the mouse to the bottom of the window."
189 (scroll-up (+ bg-mouse-y (- 2 (window-height)))))
191 (defun bg-kill-region ()
193 (kill-region (region-beginning) (region-end)))
195 (defun bg-insert-moused-sexp ()
196 "Insert a copy of the word (actually sexp) that the mouse is pointing at.
197 Sexp is inserted into the buffer at point (where the text cursor is)."
201 (bg-move-point-to-x-y bg-mouse-x bg-mouse-y)
202 (if (looking-at "\\s)")
205 (buffer-substring (save-excursion (backward-sexp 1) (point))
207 (select-window bg-cursor-window)
208 (delete-horizontal-space)
211 (indent-according-to-mode))
212 ;; In Lisp assume double-quote is closing; in Text assume opening.
213 ;; Why? Because it does the right thing most often.
214 ((save-excursion (forward-char -1)
215 (and (not (looking-at "\\s\""))
216 (looking-at "[`'\"\\]\\|\\s(")))
222 (looking-at "\\s.\\|\\s)")
223 (and (looking-at "'") (looking-at "\\sw")) ;hack for text mode
224 (save-excursion (insert " ")))))
226 ;;; Utility functions:
228 (defun bg-get-tty-num (term-char)
229 "Read from terminal until TERM-CHAR is read, and return intervening number.
230 If non-numeric not matching TERM-CHAR, reprogram the mouse and signal an error."
233 (char (- (read-char) 48)))
234 (while (and (>= char 0)
236 (setq num (+ (* num 10) char))
237 (setq char (- (read-char) 48)))
238 (or (eq term-char (+ char 48))
242 "Invalid data format in bg-mouse command: mouse reinitialized.")))
245 ;;; Note that this fails in the minibuf because move-to-column doesn't
246 ;;; allow for the width of the prompt.
247 (defun bg-move-point-to-x-y (x y)
248 "Position cursor in window coordinates.
249 X and Y are 0-based character positions in the window."
250 (move-to-window-line y)
251 ;; if not on a wrapped line, zero-column will be 0
252 (let ((zero-column (current-column))
253 (scroll-offset (window-hscroll)))
254 ;; scrolling takes up column 0 to display the $
255 (if (> scroll-offset 0)
256 (setq scroll-offset (1- scroll-offset)))
257 (move-to-column (+ zero-column scroll-offset x))
260 ;;; Returns the window that screen position (x, y) is in or nil if none,
261 ;;; meaning we are in the echo area with a non-active minibuffer.
262 (defun bg-window-from-x-y (x y)
263 "Find window corresponding to screen coordinates.
264 X and Y are 0-based character positions on the screen."
265 (get-window-with-predicate (lambda (w)
266 (coordinates-in-window-p (cons x y) w))))
268 (defun bg-command-execute (bg-command)
269 (if (commandp bg-command)
270 (command-execute bg-command)
273 (defun bg-program-mouse ()
274 (send-string-to-terminal "\e:0;7;;;360;512;9;16;9;16c"))
276 ;;; Note that the doc string for mouse-map (as defined in subr.el)
277 ;;; says it is for the X-window mouse. This is wrong; that keymap
278 ;;; should be used for your mouse no matter what terminal you have.
280 (or (keymapp mouse-map)
281 (setq mouse-map (make-keymap)))
283 (defun bind-bg-mouse-click (click-code function)
284 "Bind bg-mouse CLICK-CODE to run FUNCTION."
285 (define-key mouse-map (char-to-string click-code) function))
287 (bind-bg-mouse-click bg-button-l 'bg-set-point)
288 (bind-bg-mouse-click bg-button-m 'bg-yank)
289 (bind-bg-mouse-click bg-button-r 'bg-set-mark)
290 (bind-bg-mouse-click (+ bg-button-l bg-button-m) 'yank-pop-1)
291 (bind-bg-mouse-click (+ bg-button-l bg-button-r) 'bg-kill-region)
292 (bind-bg-mouse-click (+ bg-button-m bg-button-r) 'undo)
293 (bind-bg-mouse-click (+ bg-button-l bg-button-m bg-button-r) 'undo)
294 (bind-bg-mouse-click (+ bg-in-modeline bg-button-l) 'scroll-up)
295 (bind-bg-mouse-click (+ bg-in-modeline bg-button-m) 'bg-move-by-percentage)
296 (bind-bg-mouse-click (+ bg-in-modeline bg-button-r) 'scroll-down)
297 (bind-bg-mouse-click (+ bg-in-scrollbar bg-button-l) 'bg-mouse-line-to-top)
298 (bind-bg-mouse-click (+ bg-in-scrollbar bg-button-m) 'bg-mouse-line-to-center)
299 (bind-bg-mouse-click (+ bg-in-scrollbar bg-button-r) 'bg-mouse-line-to-bottom)
300 (bind-bg-mouse-click (+ bg-in-minibuf bg-button-l) 'execute-extended-command)
301 (bind-bg-mouse-click (+ bg-in-minibuf bg-button-m) 'suspend-emacs)
302 (bind-bg-mouse-click (+ bg-in-minibuf bg-button-r) 'eval-expression)
306 ;;; arch-tag: b3d06605-2971-44b1-be2c-e49c24e1a8d3
307 ;;; bg-mouse.el ends here