Add standard library headers.
[emacs.git] / lisp / term / bg-mouse.el
blobca3b447de30792437ca4df311cbd29d54eca27d3
1 ;;; bg-mouse.el --- GNU Emacs 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>
7 ;; Maintainer: FSF
8 ;; Keywords: hardware
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)
15 ;; any later version.
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
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;;; Code:
28 ;;; Original version by John Robinson (jr@bbn-unix.arpa, bbncca!jr), Oct 1985
29 ;;; Modularized and enhanced by gildea@bbn.com Nov 1987
30 ;;; Time stamp <89/03/21 14:27:08 gildea>
32 ;;; User customization option:
34 (defvar bg-mouse-fast-select-window nil
35 "*Non-nil for mouse hits to select new window, then execute; else just select.")
37 ;;; These numbers are summed to make the index into the mouse-map.
38 ;;; The low three bits correspond to what the mouse actually sends.
39 (defconst bg-button-r 1)
40 (defconst bg-button-m 2)
41 (defconst bg-button-c 2)
42 (defconst bg-button-l 4)
43 (defconst bg-in-modeline 8)
44 (defconst bg-in-scrollbar 16)
45 (defconst bg-in-minibuf 24)
47 ;;; semicolon screws up indenting, so use this instead
48 (defconst semicolon ?\;)
50 ;;; Defuns:
52 (defun bg-mouse-report (prefix-arg)
53 "Read, parse, and execute a BBN BitGraph mouse click.
55 L-- move point | These apply for mouse click in a window.
56 --R set mark | If bg-mouse-fast-select-window is nil,
57 L-R kill region | these commands on a nonselected window
58 -C- move point and yank | just select that window.
59 LC- yank-pop |
60 -CR or LCR undo | \"Scroll bar\" is right-hand window column.
62 on modeline: on \"scroll bar\": in minibuffer:
63 L-- scroll-up line to top execute-extended-command
64 --R scroll-down line to bottom eval-expression
65 -C- proportional goto-char line to middle suspend-emacs
67 To reinitialize the mouse if the terminal is reset, type ESC : RET"
68 (interactive "P")
69 (bg-get-tty-num semicolon)
70 (let*
71 ((screen-mouse-x (min (1- (frame-width)) ;don't hit column 86!
72 (/ (bg-get-tty-num semicolon) 9)))
73 (screen-mouse-y (- (1- (frame-height)) ;assume default font size.
74 (/ (bg-get-tty-num semicolon) 16)))
75 (bg-mouse-buttons (% (bg-get-tty-num ?c) 8))
76 (bg-mouse-window (bg-window-from-x-y screen-mouse-x screen-mouse-y))
77 (bg-cursor-window (selected-window))
78 (edges (window-edges bg-mouse-window))
79 (minibuf-p (= screen-mouse-y (1- (screen-height))))
80 (in-modeline-p (and (not minibuf-p)
81 (= screen-mouse-y (1- (nth 3 edges)))))
82 (in-scrollbar-p (and (not minibuf-p) (not in-modeline-p)
83 (>= screen-mouse-x (1- (nth 2 edges)))))
84 (same-window-p (eq bg-mouse-window bg-cursor-window))
85 (in-minibuf-p (and minibuf-p
86 (not bg-mouse-window))) ;minibuf must be inactive
87 (bg-mode-bits (+ (if in-minibuf-p bg-in-minibuf 0)
88 (if in-modeline-p bg-in-modeline 0)
89 (if in-scrollbar-p bg-in-scrollbar 0)))
90 (bg-command
91 (lookup-key mouse-map
92 (char-to-string (+ bg-mode-bits bg-mouse-buttons))))
93 (bg-mouse-x (- screen-mouse-x (nth 0 edges)))
94 (bg-mouse-y (- screen-mouse-y (nth 1 edges))))
95 (cond ((or in-modeline-p in-scrollbar-p)
96 (select-window bg-mouse-window)
97 (bg-command-execute bg-command)
98 (select-window bg-cursor-window))
99 ((or same-window-p in-minibuf-p)
100 (bg-command-execute bg-command))
101 (t ;in another window
102 (select-window bg-mouse-window)
103 (if bg-mouse-fast-select-window
104 (bg-command-execute bg-command)))
108 ;;; Library of commands:
110 (defun bg-set-point ()
111 "Move point to location of BitGraph mouse."
112 (interactive)
113 (bg-move-point-to-x-y bg-mouse-x bg-mouse-y)
114 (setq this-command 'next-line) ;make subsequent line moves work
115 (setq temporary-goal-column bg-mouse-x))
117 (defun bg-set-mark ()
118 "Set mark at location of BitGraph mouse."
119 (interactive)
120 (push-mark)
121 (bg-move-point-to-x-y bg-mouse-x bg-mouse-y)
122 (exchange-point-and-mark))
124 (defun bg-yank ()
125 "Move point to location of BitGraph mouse and yank."
126 (interactive "*")
127 (bg-move-point-to-x-y bg-mouse-x bg-mouse-y)
128 (setq this-command 'yank)
129 (yank))
131 (defun yank-pop-1 ()
132 (interactive "*")
133 (yank-pop 1))
135 (defun bg-yank-or-pop ()
136 "Move point to location of BitGraph mouse and yank. If last command
137 was a yank, do a yank-pop."
138 (interactive "*")
139 (if (eql last-command 'yank)
140 (yank-pop 1)
141 (bg-yank)))
143 ;;; In 18.51, Emacs Lisp doesn't provide most-positive-fixnum
144 (defconst bg-most-positive-fixnum 8388607)
146 (defun bg-move-by-percentage ()
147 "Go to location in buffer that is the same percentage of the way
148 through the buffer as the BitGraph mouse's X position in the window."
149 (interactive)
150 ;; check carefully for overflow in intermediate calculations
151 (goto-char
152 (cond ((zerop bg-mouse-x)
154 ((< (buffer-size) (/ bg-most-positive-fixnum bg-mouse-x))
155 ;; no danger of overflow: compute it exactly
156 (/ (* bg-mouse-x (buffer-size))
157 (1- (window-width))))
159 ;; overflow possible: approximate
160 (* (/ (buffer-size) (1- (window-width)))
161 bg-mouse-x))))
162 (beginning-of-line)
163 (what-cursor-position))
165 (defun bg-mouse-line-to-top ()
166 "Scroll the line pointed to by the BitGraph mouse to the top of the window."
167 (interactive)
168 (scroll-up bg-mouse-y))
170 (defun bg-mouse-line-to-center ()
171 "Scroll the line pointed to by the BitGraph mouse to the center
172 of the window"
173 (interactive)
174 (scroll-up (/ (+ 2 bg-mouse-y bg-mouse-y (- (window-height))) 2)))
176 (defun bg-mouse-line-to-bottom ()
177 "Scroll the line pointed to by the mouse to the bottom of the window."
178 (interactive)
179 (scroll-up (+ bg-mouse-y (- 2 (window-height)))))
181 (defun bg-kill-region ()
182 (interactive "*")
183 (kill-region (region-beginning) (region-end)))
185 (defun bg-insert-moused-sexp ()
186 "Insert a copy of the word (actually sexp) that the mouse is pointing at.
187 Sexp is inserted into the buffer at point (where the text cursor is)."
188 (interactive)
189 (let ((moused-text
190 (save-excursion
191 (bg-move-point-to-x-y bg-mouse-x bg-mouse-y)
192 (if (looking-at "\\s)")
193 (forward-char 1)
194 (forward-sexp 1))
195 (buffer-substring (save-excursion (backward-sexp 1) (point))
196 (point)))))
197 (select-window bg-cursor-window)
198 (delete-horizontal-space)
199 (cond
200 ((bolp)
201 (indent-according-to-mode))
202 ;; In Lisp assume double-quote is closing; in Text assume opening.
203 ;; Why? Because it does the right thing most often.
204 ((save-excursion (forward-char -1)
205 (and (not (looking-at "\\s\""))
206 (looking-at "[`'\"\\]\\|\\s(")))
207 nil)
209 (insert-string " ")))
210 (insert-string moused-text)
211 (or (eolp)
212 (looking-at "\\s.\\|\\s)")
213 (and (looking-at "'") (looking-at "\\sw")) ;hack for text mode
214 (save-excursion (insert-string " ")))))
216 ;;; Utility functions:
218 (defun bg-get-tty-num (term-char)
219 "Read from terminal until TERM-CHAR is read, and return intervening number.
220 If non-numeric not matching TERM-CHAR, reprogram the mouse and signal an error."
221 (let
222 ((num 0)
223 (char (- (read-char) 48)))
224 (while (and (>= char 0)
225 (<= char 9))
226 (setq num (+ (* num 10) char))
227 (setq char (- (read-char) 48)))
228 (or (eq term-char (+ char 48))
229 (progn
230 (bg-program-mouse)
231 (error
232 "Invalid data format in bg-mouse command: mouse reinitialized.")))
233 num))
235 ;;; Note that this fails in the minibuf because move-to-column doesn't
236 ;;; allow for the width of the prompt.
237 (defun bg-move-point-to-x-y (x y)
238 "Position cursor in window coordinates.
239 X and Y are 0-based character positions in the window."
240 (move-to-window-line y)
241 ;; if not on a wrapped line, zero-column will be 0
242 (let ((zero-column (current-column))
243 (scroll-offset (window-hscroll)))
244 ;; scrolling takes up column 0 to display the $
245 (if (> scroll-offset 0)
246 (setq scroll-offset (1- scroll-offset)))
247 (move-to-column (+ zero-column scroll-offset x))
250 ;;; Returns the window that screen position (x, y) is in or nil if none,
251 ;;; meaning we are in the echo area with a non-active minibuffer.
252 ;;; If coordinates-in-window-p were not in an X-windows-specific file
253 ;;; we could use that. In Emacs 19 can even use locate-window-from-coordinates
254 (defun bg-window-from-x-y (x y)
255 "Find window corresponding to screen coordinates.
256 X and Y are 0-based character positions on the screen."
257 (let ((edges (window-edges))
258 (window nil))
259 (while (and (not (eq window (selected-window)))
260 (or (< y (nth 1 edges))
261 (>= y (nth 3 edges))
262 (< x (nth 0 edges))
263 (>= x (nth 2 edges))))
264 (setq window (next-window window))
265 (setq edges (window-edges window)))
266 (cond ((eq window (selected-window))
267 nil) ;we've looped: not found
268 ((not window)
269 (selected-window)) ;just starting: current window
271 window))
274 (defun bg-command-execute (bg-command)
275 (if (commandp bg-command)
276 (command-execute bg-command)
277 (ding)))
279 (defun bg-program-mouse ()
280 (send-string-to-terminal "\e:0;7;;;360;512;9;16;9;16c"))
282 ;;; Note that the doc string for mouse-map (as defined in subr.el)
283 ;;; says it is for the X-window mouse. This is wrong; that keymap
284 ;;; should be used for your mouse no matter what terminal you have.
286 (or (keymapp mouse-map)
287 (setq mouse-map (make-keymap)))
289 (defun bind-bg-mouse-click (click-code function)
290 "Bind bg-mouse CLICK-CODE to run FUNCTION."
291 (define-key mouse-map (char-to-string click-code) function))
293 (bind-bg-mouse-click bg-button-l 'bg-set-point)
294 (bind-bg-mouse-click bg-button-m 'bg-yank)
295 (bind-bg-mouse-click bg-button-r 'bg-set-mark)
296 (bind-bg-mouse-click (+ bg-button-l bg-button-m) 'yank-pop-1)
297 (bind-bg-mouse-click (+ bg-button-l bg-button-r) 'bg-kill-region)
298 (bind-bg-mouse-click (+ bg-button-m bg-button-r) 'undo)
299 (bind-bg-mouse-click (+ bg-button-l bg-button-m bg-button-r) 'undo)
300 (bind-bg-mouse-click (+ bg-in-modeline bg-button-l) 'scroll-up)
301 (bind-bg-mouse-click (+ bg-in-modeline bg-button-m) 'bg-move-by-percentage)
302 (bind-bg-mouse-click (+ bg-in-modeline bg-button-r) 'scroll-down)
303 (bind-bg-mouse-click (+ bg-in-scrollbar bg-button-l) 'bg-mouse-line-to-top)
304 (bind-bg-mouse-click (+ bg-in-scrollbar bg-button-m) 'bg-mouse-line-to-center)
305 (bind-bg-mouse-click (+ bg-in-scrollbar bg-button-r) 'bg-mouse-line-to-bottom)
306 (bind-bg-mouse-click (+ bg-in-minibuf bg-button-l) 'execute-extended-command)
307 (bind-bg-mouse-click (+ bg-in-minibuf bg-button-m) 'suspend-emacs)
308 (bind-bg-mouse-click (+ bg-in-minibuf bg-button-r) 'eval-expression)
310 (provide 'bg-mouse)
312 ;;; bg-mouse.el ends here