Mercurial > emacs
annotate lisp/term/bg-mouse.el @ 783:59dc833c4e0c
*** empty log message ***
author | Eric S. Raymond <esr@snark.thyrsus.com> |
---|---|
date | Wed, 15 Jul 1992 18:48:42 +0000 |
parents | 8a533acedb77 |
children | 4f28bd14272c |
rev | line source |
---|---|
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
1 ;;; bg-mouse.el --- GNU Emacs code for BBN Bitgraph mouse. |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
2 |
187 | 3 ;; Copyright (C) Free Software Foundation, Inc. Oct 1985. |
4 ;; Time stamp <89/03/21 14:27:08 gildea> | |
5 | |
6 ;; This file is part of GNU Emacs. | |
7 | |
8 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
9 ;; it under the terms of the GNU General Public License as published by | |
10 ;; the Free Software Foundation; either version 1, or (at your option) | |
11 ;; any later version. | |
12 | |
13 ;; GNU Emacs is distributed in the hope that it will be useful, | |
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 ;; GNU General Public License for more details. | |
17 | |
18 ;; You should have received a copy of the GNU General Public License | |
19 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
20 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | |
22 | |
23 ;;; Original version by John Robinson (jr@bbn-unix.arpa, bbncca!jr), Oct 1985 | |
24 ;;; Modularized and enhanced by gildea@bbn.com Nov 1987 | |
25 | |
26 ;;; User customization option: | |
27 | |
28 (defvar bg-mouse-fast-select-window nil | |
29 "*Non-nil for mouse hits to select new window, then execute; else just select.") | |
30 | |
31 ;;; These numbers are summed to make the index into the mouse-map. | |
32 ;;; The low three bits correspond to what the mouse actually sends. | |
33 (defconst bg-button-r 1) | |
34 (defconst bg-button-m 2) | |
35 (defconst bg-button-c 2) | |
36 (defconst bg-button-l 4) | |
37 (defconst bg-in-modeline 8) | |
38 (defconst bg-in-scrollbar 16) | |
39 (defconst bg-in-minibuf 24) | |
40 | |
41 ;;; semicolon screws up indenting, so use this instead | |
42 (defconst semicolon ?\;) | |
43 | |
44 ;;; Defuns: | |
45 | |
46 (defun bg-mouse-report (prefix-arg) | |
47 "Read, parse, and execute a BBN BitGraph mouse click. | |
48 | |
49 L-- move point | These apply for mouse click in a window. | |
50 --R set mark | If bg-mouse-fast-select-window is nil, | |
51 L-R kill region | these commands on a nonselected window | |
52 -C- move point and yank | just select that window. | |
53 LC- yank-pop | | |
54 -CR or LCR undo | \"Scroll bar\" is right-hand window column. | |
55 | |
56 on modeline: on \"scroll bar\": in minibuffer: | |
57 L-- scroll-up line to top execute-extended-command | |
58 --R scroll-down line to bottom eval-expression | |
59 -C- proportional goto-char line to middle suspend-emacs | |
60 | |
61 To reinitialize the mouse if the terminal is reset, type ESC : RET" | |
62 (interactive "P") | |
63 (bg-get-tty-num semicolon) | |
64 (let* | |
65 ((screen-mouse-x (min (1- (screen-width)) ;don't hit column 86! | |
66 (/ (bg-get-tty-num semicolon) 9))) | |
67 (screen-mouse-y (- (1- (screen-height)) ;assume default font size. | |
68 (/ (bg-get-tty-num semicolon) 16))) | |
69 (bg-mouse-buttons (% (bg-get-tty-num ?c) 8)) | |
70 (bg-mouse-window (bg-window-from-x-y screen-mouse-x screen-mouse-y)) | |
71 (bg-cursor-window (selected-window)) | |
72 (edges (window-edges bg-mouse-window)) | |
73 (minibuf-p (= screen-mouse-y (1- (screen-height)))) | |
74 (in-modeline-p (and (not minibuf-p) | |
75 (= screen-mouse-y (1- (nth 3 edges))))) | |
76 (in-scrollbar-p (and (not minibuf-p) (not in-modeline-p) | |
77 (>= screen-mouse-x (1- (nth 2 edges))))) | |
78 (same-window-p (eq bg-mouse-window bg-cursor-window)) | |
79 (in-minibuf-p (and minibuf-p | |
80 (not bg-mouse-window))) ;minibuf must be inactive | |
81 (bg-mode-bits (+ (if in-minibuf-p bg-in-minibuf 0) | |
82 (if in-modeline-p bg-in-modeline 0) | |
83 (if in-scrollbar-p bg-in-scrollbar 0))) | |
84 (bg-command | |
85 (lookup-key mouse-map | |
86 (char-to-string (+ bg-mode-bits bg-mouse-buttons)))) | |
87 (bg-mouse-x (- screen-mouse-x (nth 0 edges))) | |
88 (bg-mouse-y (- screen-mouse-y (nth 1 edges)))) | |
89 (cond ((or in-modeline-p in-scrollbar-p) | |
90 (select-window bg-mouse-window) | |
91 (bg-command-execute bg-command) | |
92 (select-window bg-cursor-window)) | |
93 ((or same-window-p in-minibuf-p) | |
94 (bg-command-execute bg-command)) | |
95 (t ;in another window | |
96 (select-window bg-mouse-window) | |
97 (if bg-mouse-fast-select-window | |
98 (bg-command-execute bg-command))) | |
99 ))) | |
100 | |
101 | |
102 ;;; Library of commands: | |
103 | |
104 (defun bg-set-point () | |
105 "Move point to location of BitGraph mouse." | |
106 (interactive) | |
107 (bg-move-point-to-x-y bg-mouse-x bg-mouse-y) | |
108 (setq this-command 'next-line) ;make subsequent line moves work | |
109 (setq temporary-goal-column bg-mouse-x)) | |
110 | |
111 (defun bg-set-mark () | |
112 "Set mark at location of BitGraph mouse." | |
113 (interactive) | |
114 (push-mark) | |
115 (bg-move-point-to-x-y bg-mouse-x bg-mouse-y) | |
116 (exchange-point-and-mark)) | |
117 | |
118 (defun bg-yank () | |
119 "Move point to location of BitGraph mouse and yank." | |
120 (interactive "*") | |
121 (bg-move-point-to-x-y bg-mouse-x bg-mouse-y) | |
122 (setq this-command 'yank) | |
123 (yank)) | |
124 | |
125 (defun yank-pop-1 () | |
126 (interactive "*") | |
127 (yank-pop 1)) | |
128 | |
129 (defun bg-yank-or-pop () | |
130 "Move point to location of BitGraph mouse and yank. If last command | |
131 was a yank, do a yank-pop." | |
132 (interactive "*") | |
133 (if (eql last-command 'yank) | |
134 (yank-pop 1) | |
135 (bg-yank))) | |
136 | |
137 ;;; In 18.51, Emacs Lisp doesn't provide most-positive-fixnum | |
138 (defconst bg-most-positive-fixnum 8388607) | |
139 | |
140 (defun bg-move-by-percentage () | |
141 "Go to location in buffer that is the same percentage of the way | |
142 through the buffer as the BitGraph mouse's X position in the window." | |
143 (interactive) | |
144 ;; check carefully for overflow in intermediate calculations | |
145 (goto-char | |
146 (cond ((zerop bg-mouse-x) | |
147 0) | |
148 ((< (buffer-size) (/ bg-most-positive-fixnum bg-mouse-x)) | |
149 ;; no danger of overflow: compute it exactly | |
150 (/ (* bg-mouse-x (buffer-size)) | |
151 (1- (window-width)))) | |
152 (t | |
153 ;; overflow possible: approximate | |
154 (* (/ (buffer-size) (1- (window-width))) | |
155 bg-mouse-x)))) | |
156 (beginning-of-line) | |
157 (what-cursor-position)) | |
158 | |
159 (defun bg-mouse-line-to-top () | |
160 "Scroll the line pointed to by the BitGraph mouse to the top of the window." | |
161 (interactive) | |
162 (scroll-up bg-mouse-y)) | |
163 | |
164 (defun bg-mouse-line-to-center () | |
165 "Scroll the line pointed to by the BitGraph mouse to the center | |
166 of the window" | |
167 (interactive) | |
168 (scroll-up (/ (+ 2 bg-mouse-y bg-mouse-y (- (window-height))) 2))) | |
169 | |
170 (defun bg-mouse-line-to-bottom () | |
171 "Scroll the line pointed to by the mouse to the bottom of the window." | |
172 (interactive) | |
173 (scroll-up (+ bg-mouse-y (- 2 (window-height))))) | |
174 | |
175 (defun bg-kill-region () | |
176 (interactive "*") | |
177 (kill-region (region-beginning) (region-end))) | |
178 | |
179 (defun bg-insert-moused-sexp () | |
180 "Insert a copy of the word (actually sexp) that the mouse is pointing at. | |
181 Sexp is inserted into the buffer at point (where the text cursor is)." | |
182 (interactive) | |
183 (let ((moused-text | |
184 (save-excursion | |
185 (bg-move-point-to-x-y bg-mouse-x bg-mouse-y) | |
186 (if (looking-at "\\s)") | |
187 (forward-char 1) | |
188 (forward-sexp 1)) | |
189 (buffer-substring (save-excursion (backward-sexp 1) (point)) | |
190 (point))))) | |
191 (select-window bg-cursor-window) | |
192 (delete-horizontal-space) | |
193 (cond | |
194 ((bolp) | |
195 (indent-according-to-mode)) | |
196 ;; In Lisp assume double-quote is closing; in Text assume opening. | |
197 ;; Why? Because it does the right thing most often. | |
198 ((save-excursion (forward-char -1) | |
199 (and (not (looking-at "\\s\"")) | |
200 (looking-at "[`'\"\\]\\|\\s("))) | |
201 nil) | |
202 (t | |
203 (insert-string " "))) | |
204 (insert-string moused-text) | |
205 (or (eolp) | |
206 (looking-at "\\s.\\|\\s)") | |
207 (and (looking-at "'") (looking-at "\\sw")) ;hack for text mode | |
208 (save-excursion (insert-string " "))))) | |
209 | |
210 ;;; Utility functions: | |
211 | |
212 (defun bg-get-tty-num (term-char) | |
213 "Read from terminal until TERM-CHAR is read, and return intervening number. | |
214 If non-numeric not matching TERM-CHAR, reprogram the mouse and signal an error." | |
215 (let | |
216 ((num 0) | |
217 (char (- (read-char) 48))) | |
218 (while (and (>= char 0) | |
219 (<= char 9)) | |
220 (setq num (+ (* num 10) char)) | |
221 (setq char (- (read-char) 48))) | |
222 (or (eq term-char (+ char 48)) | |
223 (progn | |
224 (bg-program-mouse) | |
225 (error | |
226 "Invalid data format in bg-mouse command: mouse reinitialized."))) | |
227 num)) | |
228 | |
229 ;;; Note that this fails in the minibuf because move-to-column doesn't | |
230 ;;; allow for the width of the prompt. | |
231 (defun bg-move-point-to-x-y (x y) | |
232 "Position cursor in window coordinates. | |
233 X and Y are 0-based character positions in the window." | |
234 (move-to-window-line y) | |
235 ;; if not on a wrapped line, zero-column will be 0 | |
236 (let ((zero-column (current-column)) | |
237 (scroll-offset (window-hscroll))) | |
238 ;; scrolling takes up column 0 to display the $ | |
239 (if (> scroll-offset 0) | |
240 (setq scroll-offset (1- scroll-offset))) | |
241 (move-to-column (+ zero-column scroll-offset x)) | |
242 )) | |
243 | |
244 ;;; Returns the window that screen position (x, y) is in or nil if none, | |
245 ;;; meaning we are in the echo area with a non-active minibuffer. | |
246 ;;; If coordinates-in-window-p were not in an X-windows-specific file | |
247 ;;; we could use that. In Emacs 19 can even use locate-window-from-coordinates | |
248 (defun bg-window-from-x-y (x y) | |
249 "Find window corresponding to screen coordinates. | |
250 X and Y are 0-based character positions on the screen." | |
251 (let ((edges (window-edges)) | |
252 (window nil)) | |
253 (while (and (not (eq window (selected-window))) | |
254 (or (< y (nth 1 edges)) | |
255 (>= y (nth 3 edges)) | |
256 (< x (nth 0 edges)) | |
257 (>= x (nth 2 edges)))) | |
258 (setq window (next-window window)) | |
259 (setq edges (window-edges window))) | |
260 (cond ((eq window (selected-window)) | |
261 nil) ;we've looped: not found | |
262 ((not window) | |
263 (selected-window)) ;just starting: current window | |
264 (t | |
265 window)) | |
266 )) | |
267 | |
268 (defun bg-command-execute (bg-command) | |
269 (if (commandp bg-command) | |
270 (command-execute bg-command) | |
271 (ding))) | |
272 | |
273 (defun bg-program-mouse () | |
274 (send-string-to-terminal "\e:0;7;;;360;512;9;16;9;16c")) | |
275 | |
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. | |
279 | |
280 (or (keymapp mouse-map) | |
281 (setq mouse-map (make-keymap))) | |
282 | |
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)) | |
286 | |
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) | |
303 | |
584 | 304 (provide 'bg-mouse) |
305 | |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
306 ;;; bg-mouse.el ends here |