Initial git import
[sxemacs] / lisp / term / bg-mouse.el
1 ;;; bg-mouse.el --- SXEmacs code for BBN Bitgraph mouse.
2
3 ;; Copyright (C) Free Software Foundation, Inc. Oct 1985.
4
5 ;; Author: John Robinson <jr@bbn-unix.arpa>
6 ;;      Stephen Gildea <gildea@bbn.com>
7 ;; Maintainer: FSF
8 ;; Keywords: hardware
9
10 ;; This file is part of SXEmacs.
11
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.
16
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.
21
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/>.
24
25 ;;; Code:
26
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>
30
31 (eval-when-compile
32   (globally-declare-boundp
33    '(mouse-map bg-mouse-x bg-mouse-y bg-cursor-window))
34   (globally-declare-fboundp '(window-edges screen-height)))
35
36 ;;;  User customization option:
37
38 (defvar bg-mouse-fast-select-window nil
39   "*Non-nil for mouse hits to select new window, then execute; else just select.")
40
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)
50
51 ;;; semicolon screws up indenting, so use this instead
52 (defconst semicolon ?\;)
53
54 ;;;  Defuns:
55
56 (defun bg-mouse-report (prefix-arg)
57   "Read, parse, and execute a BBN BitGraph mouse click.
58
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.
63 LC- yank-pop               |
64 -CR or LCR undo            | \"Scroll bar\" is right-hand window column.
65
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
70
71 To reinitialize the mouse if the terminal is reset, type ESC : RET"
72   (interactive "P")
73   (bg-get-tty-num semicolon)
74   (let*
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)))
94        (bg-command
95          (lookup-key mouse-map
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)))
109           )))
110
111 \f
112 ;;; Library of commands:
113
114 (defun bg-set-point ()
115   "Move point to location of BitGraph mouse."
116   (interactive)
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))
120
121 (defun bg-set-mark ()
122   "Set mark at location of BitGraph mouse."
123   (interactive)
124   (push-mark)
125   (bg-move-point-to-x-y bg-mouse-x bg-mouse-y)
126   (exchange-point-and-mark))
127
128 (defun bg-yank ()
129   "Move point to location of BitGraph mouse and yank."
130   (interactive "*")
131   (bg-move-point-to-x-y bg-mouse-x bg-mouse-y)
132   (setq this-command 'yank)
133   (yank))
134
135 (defun yank-pop-1 ()
136   (interactive "*")
137   (yank-pop 1))
138
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."
142   (interactive "*")
143   (if (eql last-command 'yank)
144       (yank-pop 1)
145     (bg-yank)))
146
147 ;;; In 18.51, Emacs Lisp doesn't provide most-positive-fixnum
148 (defconst bg-most-positive-fixnum 8388607)
149
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."
153   (interactive)
154   ;; check carefully for overflow in intermediate calculations
155   (goto-char
156    (cond ((zerop bg-mouse-x)
157           0)
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))))
162          (t
163           ;; overflow possible: approximate
164           (* (/ (buffer-size) (1- (window-width)))
165              bg-mouse-x))))
166   (beginning-of-line)
167   (what-cursor-position))
168
169 (defun bg-mouse-line-to-top ()
170   "Scroll the line pointed to by the BitGraph mouse to the top of the window."
171   (interactive)
172   (scroll-up bg-mouse-y))
173
174 (defun bg-mouse-line-to-center ()
175   "Scroll the line pointed to by the BitGraph mouse to the center
176 of the window."
177   (interactive)
178   (scroll-up (/ (+ 2 bg-mouse-y bg-mouse-y (- (window-height))) 2)))
179
180 (defun bg-mouse-line-to-bottom ()
181   "Scroll the line pointed to by the mouse to the bottom of the window."
182   (interactive)
183   (scroll-up (+ bg-mouse-y (- 2 (window-height)))))
184
185 (defun bg-kill-region ()
186   (interactive "*")
187   (kill-region (region-beginning) (region-end)))
188
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)."
192   (interactive)
193   (let ((moused-text
194           (save-excursion
195             (bg-move-point-to-x-y bg-mouse-x bg-mouse-y)
196             (if (looking-at "\\s)")
197                 (forward-char 1)
198               (forward-sexp 1))
199             (buffer-substring (save-excursion (backward-sexp 1) (point))
200                               (point)))))
201     (select-window bg-cursor-window)
202     (delete-horizontal-space)
203     (cond
204      ((bolp)
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(")))
211       nil)
212      (t
213       (insert-string " ")))
214     (insert-string moused-text)
215     (or (eolp)
216         (looking-at "\\s.\\|\\s)")
217         (and (looking-at "'") (looking-at "\\sw")) ;hack for text mode
218         (save-excursion (insert-string " ")))))
219 \f
220 ;;; Utility functions:
221
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."
225   (let
226       ((num 0)
227        (char (- (read-char) 48)))
228     (while (and (>= char 0)
229                 (<= char 9))
230       (setq num (+ (* num 10) char))
231       (setq char (- (read-char) 48)))
232     (or (eq term-char (+ char 48))
233         (progn
234           (bg-program-mouse)
235           (error
236             "Invalid data format in bg-mouse command: mouse reinitialized.")))
237     num))
238
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))
252     ))
253
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))
262         (window nil))
263     (while (and (not (eq window (selected-window)))
264                 (or (<  y (nth 1 edges))
265                     (>= y (nth 3 edges))
266                     (<  x (nth 0 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
272           ((not window)
273            (selected-window))           ;just starting: current window
274           (t
275             window))
276     ))
277
278 (defun bg-command-execute (bg-command)
279   (if (commandp bg-command)
280       (command-execute bg-command)
281     (ding)))
282
283 (defun bg-program-mouse ()
284   (send-string-to-terminal "\e:0;7;;;360;512;9;16;9;16c"))
285
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.
289
290 (or (keymapp mouse-map)
291     (setq mouse-map (make-keymap)))
292
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))
296
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)
313
314 (provide 'bg-mouse)
315
316 ;;; bg-mouse.el ends here