1 ;;; bg-mouse.el --- SXEmacs code for BBN Bitgraph mouse.
3 ;; Copyright (C) Free Software Foundation, Inc. Oct 1985.
5 ;; Author: John Robinson <jr@bbn-unix.arpa>
6 ;; Stephen Gildea <gildea@bbn.com>
10 ;; This file is part of SXEmacs.
12 ;; SXEmacs 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 3 of the License, or
15 ;; (at your option) any later version.
17 ;; SXEmacs 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 this program. If not, see <http://www.gnu.org/licenses/>.
27 ;;; Original version by John Robinson (jr@bbn-unix.arpa, bbncca!jr), Oct 1985
28 ;;; Modularized and enhanced by gildea@bbn.com Nov 1987
29 ;;; Time stamp <89/03/21 14:27:08 gildea>
32 (globally-declare-boundp
33 '(mouse-map bg-mouse-x bg-mouse-y bg-cursor-window))
34 (globally-declare-fboundp '(window-edges screen-height)))
36 ;;; User customization option:
38 (defvar bg-mouse-fast-select-window nil
39 "*Non-nil for mouse hits to select new window, then execute; else just select.")
41 ;;; These numbers are summed to make the index into the mouse-map.
42 ;;; The low three bits correspond to what the mouse actually sends.
43 (defconst bg-button-r 1)
44 (defconst bg-button-m 2)
45 (defconst bg-button-c 2)
46 (defconst bg-button-l 4)
47 (defconst bg-in-modeline 8)
48 (defconst bg-in-scrollbar 16)
49 (defconst bg-in-minibuf 24)
51 ;;; semicolon screws up indenting, so use this instead
52 (defconst semicolon ?\;)
56 (defun bg-mouse-report (prefix-arg)
57 "Read, parse, and execute a BBN BitGraph mouse click.
59 L-- move point | These apply for mouse click in a window.
60 --R set mark | If bg-mouse-fast-select-window is nil,
61 L-R kill region | these commands on a nonselected window
62 -C- move point and yank | just select that window.
64 -CR or LCR undo | \"Scroll bar\" is right-hand window column.
66 on modeline: on \"scroll bar\": in minibuffer:
67 L-- scroll-up line to top execute-extended-command
68 --R scroll-down line to bottom eval-expression
69 -C- proportional goto-char line to middle suspend-emacs
71 To reinitialize the mouse if the terminal is reset, type ESC : RET"
73 (bg-get-tty-num semicolon)
75 ((screen-mouse-x (min (1- (frame-width)) ;don't hit column 86!
76 (/ (bg-get-tty-num semicolon) 9)))
77 (screen-mouse-y (- (1- (frame-height)) ;assume default font size.
78 (/ (bg-get-tty-num semicolon) 16)))
79 (bg-mouse-buttons (% (bg-get-tty-num ?c) 8))
80 (bg-mouse-window (bg-window-from-x-y screen-mouse-x screen-mouse-y))
81 (bg-cursor-window (selected-window))
82 (edges (window-edges bg-mouse-window))
83 (minibuf-p (= screen-mouse-y (1- (screen-height))))
84 (in-modeline-p (and (not minibuf-p)
85 (= screen-mouse-y (1- (nth 3 edges)))))
86 (in-scrollbar-p (and (not minibuf-p) (not in-modeline-p)
87 (>= screen-mouse-x (1- (nth 2 edges)))))
88 (same-window-p (eq bg-mouse-window bg-cursor-window))
89 (in-minibuf-p (and minibuf-p
90 (not bg-mouse-window))) ;minibuf must be inactive
91 (bg-mode-bits (+ (if in-minibuf-p bg-in-minibuf 0)
92 (if in-modeline-p bg-in-modeline 0)
93 (if in-scrollbar-p bg-in-scrollbar 0)))
96 (char-to-string (+ bg-mode-bits bg-mouse-buttons))))
97 (bg-mouse-x (- screen-mouse-x (nth 0 edges)))
98 (bg-mouse-y (- screen-mouse-y (nth 1 edges))))
99 (cond ((or in-modeline-p in-scrollbar-p)
100 (select-window bg-mouse-window)
101 (bg-command-execute bg-command)
102 (select-window bg-cursor-window))
103 ((or same-window-p in-minibuf-p)
104 (bg-command-execute bg-command))
105 (t ;in another window
106 (select-window bg-mouse-window)
107 (if bg-mouse-fast-select-window
108 (bg-command-execute bg-command)))
112 ;;; Library of commands:
114 (defun bg-set-point ()
115 "Move point to location of BitGraph mouse."
117 (bg-move-point-to-x-y bg-mouse-x bg-mouse-y)
118 (setq this-command 'next-line) ;make subsequent line moves work
119 (setq temporary-goal-column bg-mouse-x))
121 (defun bg-set-mark ()
122 "Set mark at location of BitGraph mouse."
125 (bg-move-point-to-x-y bg-mouse-x bg-mouse-y)
126 (exchange-point-and-mark))
129 "Move point to location of BitGraph mouse and yank."
131 (bg-move-point-to-x-y bg-mouse-x bg-mouse-y)
132 (setq this-command 'yank)
139 (defun bg-yank-or-pop ()
140 "Move point to location of BitGraph mouse and yank. If last command
141 was a yank, do a yank-pop."
143 (if (eql last-command 'yank)
147 ;;; In 18.51, Emacs Lisp doesn't provide most-positive-fixnum
148 (defconst bg-most-positive-fixnum 8388607)
150 (defun bg-move-by-percentage ()
151 "Go to location in buffer that is the same percentage of the way
152 through the buffer as the BitGraph mouse's X position in the window."
154 ;; check carefully for overflow in intermediate calculations
156 (cond ((zerop bg-mouse-x)
158 ((< (buffer-size) (/ bg-most-positive-fixnum bg-mouse-x))
159 ;; no danger of overflow: compute it exactly
160 (/ (* bg-mouse-x (buffer-size))
161 (1- (window-width))))
163 ;; overflow possible: approximate
164 (* (/ (buffer-size) (1- (window-width)))
167 (what-cursor-position))
169 (defun bg-mouse-line-to-top ()
170 "Scroll the line pointed to by the BitGraph mouse to the top of the window."
172 (scroll-up bg-mouse-y))
174 (defun bg-mouse-line-to-center ()
175 "Scroll the line pointed to by the BitGraph mouse to the center
178 (scroll-up (/ (+ 2 bg-mouse-y bg-mouse-y (- (window-height))) 2)))
180 (defun bg-mouse-line-to-bottom ()
181 "Scroll the line pointed to by the mouse to the bottom of the window."
183 (scroll-up (+ bg-mouse-y (- 2 (window-height)))))
185 (defun bg-kill-region ()
187 (kill-region (region-beginning) (region-end)))
189 (defun bg-insert-moused-sexp ()
190 "Insert a copy of the word (actually sexp) that the mouse is pointing at.
191 Sexp is inserted into the buffer at point (where the text cursor is)."
195 (bg-move-point-to-x-y bg-mouse-x bg-mouse-y)
196 (if (looking-at "\\s)")
199 (buffer-substring (save-excursion (backward-sexp 1) (point))
201 (select-window bg-cursor-window)
202 (delete-horizontal-space)
205 (indent-according-to-mode))
206 ;; In Lisp assume double-quote is closing; in Text assume opening.
207 ;; Why? Because it does the right thing most often.
208 ((save-excursion (backward-char 1)
209 (and (not (looking-at "\\s\""))
210 (looking-at "[`'\"\\]\\|\\s(")))
213 (insert-string " ")))
214 (insert-string moused-text)
216 (looking-at "\\s.\\|\\s)")
217 (and (looking-at "'") (looking-at "\\sw")) ;hack for text mode
218 (save-excursion (insert-string " ")))))
220 ;;; Utility functions:
222 (defun bg-get-tty-num (term-char)
223 "Read from terminal until TERM-CHAR is read, and return intervening number.
224 If non-numeric not matching TERM-CHAR, reprogram the mouse and signal an error."
227 (char (- (read-char) 48)))
228 (while (and (>= char 0)
230 (setq num (+ (* num 10) char))
231 (setq char (- (read-char) 48)))
232 (or (eq term-char (+ char 48))
236 "Invalid data format in bg-mouse command: mouse reinitialized.")))
239 ;;; Note that this fails in the minibuf because move-to-column doesn't
240 ;;; allow for the width of the prompt.
241 (defun bg-move-point-to-x-y (x y)
242 "Position cursor in window coordinates.
243 X and Y are 0-based character positions in the window."
244 (move-to-window-line y)
245 ;; if not on a wrapped line, zero-column will be 0
246 (let ((zero-column (current-column))
247 (scroll-offset (window-hscroll)))
248 ;; scrolling takes up column 0 to display the $
249 (if (> scroll-offset 0)
250 (setq scroll-offset (1- scroll-offset)))
251 (move-to-column (+ zero-column scroll-offset x))
254 ;;; Returns the window that screen position (x, y) is in or nil if none,
255 ;;; meaning we are in the echo area with a non-active minibuffer.
256 ;;; If coordinates-in-window-p were not in an X-windows-specific file
257 ;;; we could use that. In Emacs 19 can even use locate-window-from-coordinates
258 (defun bg-window-from-x-y (x y)
259 "Find window corresponding to screen coordinates.
260 X and Y are 0-based character positions on the screen."
261 (let ((edges (window-edges))
263 (while (and (not (eq window (selected-window)))
264 (or (< y (nth 1 edges))
267 (>= x (nth 2 edges))))
268 (setq window (next-window window))
269 (setq edges (window-edges window)))
270 (cond ((eq window (selected-window))
271 nil) ;we've looped: not found
273 (selected-window)) ;just starting: current window
278 (defun bg-command-execute (bg-command)
279 (if (commandp bg-command)
280 (command-execute bg-command)
283 (defun bg-program-mouse ()
284 (send-string-to-terminal "\e:0;7;;;360;512;9;16;9;16c"))
286 ;;; Note that the doc string for mouse-map (as defined in subr.el)
287 ;;; says it is for the X-window mouse. This is wrong; that keymap
288 ;;; should be used for your mouse no matter what terminal you have.
290 (or (keymapp mouse-map)
291 (setq mouse-map (make-keymap)))
293 (defun bind-bg-mouse-click (click-code function)
294 "Bind bg-mouse CLICK-CODE to run FUNCTION."
295 (define-key mouse-map (char-to-string click-code) function))
297 (bind-bg-mouse-click bg-button-l 'bg-set-point)
298 (bind-bg-mouse-click bg-button-m 'bg-yank)
299 (bind-bg-mouse-click bg-button-r 'bg-set-mark)
300 (bind-bg-mouse-click (+ bg-button-l bg-button-m) 'yank-pop-1)
301 (bind-bg-mouse-click (+ bg-button-l bg-button-r) 'bg-kill-region)
302 (bind-bg-mouse-click (+ bg-button-m bg-button-r) 'undo)
303 (bind-bg-mouse-click (+ bg-button-l bg-button-m bg-button-r) 'undo)
304 (bind-bg-mouse-click (+ bg-in-modeline bg-button-l) 'scroll-up)
305 (bind-bg-mouse-click (+ bg-in-modeline bg-button-m) 'bg-move-by-percentage)
306 (bind-bg-mouse-click (+ bg-in-modeline bg-button-r) 'scroll-down)
307 (bind-bg-mouse-click (+ bg-in-scrollbar bg-button-l) 'bg-mouse-line-to-top)
308 (bind-bg-mouse-click (+ bg-in-scrollbar bg-button-m) 'bg-mouse-line-to-center)
309 (bind-bg-mouse-click (+ bg-in-scrollbar bg-button-r) 'bg-mouse-line-to-bottom)
310 (bind-bg-mouse-click (+ bg-in-minibuf bg-button-l) 'execute-extended-command)
311 (bind-bg-mouse-click (+ bg-in-minibuf bg-button-m) 'suspend-emacs)
312 (bind-bg-mouse-click (+ bg-in-minibuf bg-button-r) 'eval-expression)
316 ;;; bg-mouse.el ends here