0
|
1 ;;; mouse.el --- window system-independent mouse support.
|
|
2 ;; Keywords: hardware
|
|
3
|
|
4 ;; Copyright (C) 1988, 1992, 1993, 1994 Free Software Foundation, Inc.
|
|
5 ;; Copyright (C) 1995 Tinker Systems
|
|
6 ;; Copyright (C) 1995, 1996 Ben Wing.
|
|
7
|
|
8 ;; This file is part of XEmacs.
|
|
9
|
|
10 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
11 ;; under the terms of the GNU General Public License as published by
|
|
12 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
13 ;; any later version.
|
|
14
|
|
15 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
18 ;; General Public License for more details.
|
|
19
|
|
20 ;; You should have received a copy of the GNU General Public License
|
16
|
21 ;; along with XEmacs; see the file COPYING. If not, write to the
|
70
|
22 ;; Free Software Foundation, 59 Temple Place - Suite 330,
|
16
|
23 ;; Boston, MA 02111-1307, USA.
|
0
|
24
|
|
25 ;;; Synched up with: Not synched with FSF. Almost completely divergent.
|
|
26
|
|
27 (provide 'mouse)
|
|
28
|
|
29 (global-set-key 'button1 'mouse-track)
|
|
30 (global-set-key '(shift button1) 'mouse-track-adjust)
|
|
31 (global-set-key '(control button1) 'mouse-track-insert)
|
|
32 (global-set-key '(control shift button1) 'mouse-track-delete-and-insert)
|
|
33 (global-set-key '(meta button1) 'mouse-track-do-rectangle)
|
|
34
|
|
35 (global-set-key 'button2 'mouse-yank)
|
|
36
|
|
37 (defvar mouse-track-rectangle-p nil
|
|
38 "*If true, then dragging out a region with the mouse selects rectangles
|
|
39 instead of simple start/end regions.")
|
|
40
|
|
41 (defvar mouse-yank-at-point nil
|
|
42 "*If non-nil, the function `mouse-yank' will yank text at the cursor location.
|
|
43 Otherwise, the cursor will be moved to the location of the pointer click before
|
|
44 text is inserted.")
|
|
45
|
|
46 (defvar mouse-yank-function 'yank ; x11/x-mouse changes this...
|
|
47 "Function that is called upon by `mouse-yank' to actually insert text.")
|
|
48
|
|
49
|
|
50 (defun mouse-select ()
|
|
51 "Select Emacs window the mouse is on."
|
|
52 (interactive "@"))
|
|
53
|
|
54 (defun mouse-delete-window ()
|
|
55 "Delete the Emacs window the mouse is on."
|
|
56 (interactive "@")
|
|
57 (delete-window))
|
|
58
|
|
59 (defun mouse-keep-one-window ()
|
|
60 "Select Emacs window mouse is on, then kill all other Emacs windows."
|
|
61 (interactive "@")
|
|
62 (delete-other-windows))
|
|
63
|
|
64 (defun mouse-select-and-split ()
|
|
65 "Select Emacs window mouse is on, then split it vertically in half."
|
|
66 (interactive "@")
|
|
67 (split-window-vertically nil))
|
|
68
|
|
69 (defun mouse-set-point (event)
|
|
70 "Select Emacs window mouse is on, and move point to mouse position."
|
|
71 (interactive "@e")
|
|
72 (let ((window (event-window event))
|
|
73 (pos (event-point event))
|
|
74 (close-pos (event-closest-point event)))
|
|
75 (or window (error "not in a window"))
|
|
76 (select-window window)
|
|
77 (if (and pos (> pos 0))
|
|
78 ;; If the event was over a text char, it's easy.
|
|
79 (goto-char (max (min pos (point-max)) (point-min)))
|
|
80 (if (and close-pos (> close-pos 0))
|
|
81 (goto-char (max (min close-pos (point-max)) (point-min)))
|
|
82 ;; When the event occurs outside of the frame directly to the
|
|
83 ;; left or right of a modeline, close-point is nil, but
|
|
84 ;; event-over-modeline is also nil. That will drop us to this
|
|
85 ;; point. So instead of erroring, just return nil.
|
|
86 nil))))
|
|
87
|
|
88 (defun mouse-yank (event)
|
|
89 "Paste text with the mouse.
|
|
90 If the variable `mouse-yank-at-point' is nil, then pasting occurs at the
|
|
91 location of the click; otherwise, pasting occurs at the current cursor
|
|
92 location."
|
|
93 (interactive "e")
|
|
94 (and (not mouse-yank-at-point)
|
|
95 (mouse-set-point event))
|
|
96 (funcall mouse-yank-function))
|
|
97
|
|
98 (defun click-inside-extent-p (click extent)
|
|
99 "Returns non-nil if the button event is within the bounds of the primary
|
|
100 selection-extent, nil otherwise."
|
|
101 ;; stig@hackvan.com
|
|
102 (let ((ewin (event-window click))
|
|
103 (epnt (event-point click)))
|
|
104 (and ewin
|
|
105 epnt
|
|
106 extent
|
|
107 (eq (window-buffer ewin)
|
|
108 (extent-object extent))
|
|
109 (extent-start-position extent)
|
|
110 (> epnt (extent-start-position extent))
|
|
111 (> (extent-end-position extent) epnt))))
|
|
112
|
|
113 (defun click-inside-selection-p (click)
|
|
114 (or (click-inside-extent-p click primary-selection-extent)
|
|
115 (click-inside-extent-p click zmacs-region-extent)
|
|
116 ))
|
|
117
|
|
118 (defun point-inside-extent-p (extent)
|
|
119 "Returns non-nil if the point is within or just after the bounds of the
|
|
120 primary selection-extent, nil otherwise."
|
|
121 ;; stig@hackvan.com
|
|
122 (and extent
|
|
123 (eq (current-buffer)
|
|
124 (extent-object extent))
|
|
125 (> (point) (extent-start-position extent))
|
|
126 (>= (extent-end-position extent) (point))))
|
|
127
|
|
128 (defun point-inside-selection-p ()
|
|
129 ;; by Stig@hackvan.com
|
|
130 (or (point-inside-extent-p primary-selection-extent)
|
|
131 (point-inside-extent-p zmacs-region-extent)))
|
|
132
|
|
133 ;;; #### - finish this...
|
|
134 ;;; (defun mouse-drag-or-yank (event)
|
|
135 ;;; "Either drag or paste the current selection. If the variable
|
|
136 ;;; `mouse-yank-at-point' is non-nil, then moves the cursor to the location of
|
|
137 ;;; the click before pasting."
|
|
138 ;;; (interactive "e")
|
|
139 ;;; (if (click-inside-selection-p event)
|
|
140 ;;; ;; okay, this is a drag
|
|
141 ;;; )
|
|
142 ;;; )
|
|
143
|
|
144 (defun mouse-eval-sexp (click force-window)
|
|
145 "Evaluate the sexp under the mouse. Usually, this is the last sexp before
|
|
146 the click, but if you click on a left paren, then it is the sexp beginning
|
|
147 with the paren that is evaluated. Also, since strings evaluate to themselves,
|
|
148 they're fed to re-search-forward and the matched region is highlighted until
|
|
149 the mouse button is released.
|
|
150
|
|
151 Perhaps the most useful thing about this function is that the evaluation of
|
|
152 the expression which is clicked upon is relative not to the window where you
|
|
153 click, but to the current window and the current position of point. Thus,
|
|
154 you can use `mouse-eval-sexp' to interactively test code that acts upon a
|
|
155 buffer...something you cannot do with the standard `eval-last-sexp' function.
|
|
156 It's also fantastic for debugging regular expressions."
|
|
157 ;; by Stig@hackvan.com
|
|
158 (interactive "e\nP")
|
|
159 (let (exp val result-str)
|
|
160 (setq exp (save-window-excursion
|
|
161 (save-excursion
|
|
162 (mouse-set-point click)
|
|
163 (save-excursion
|
|
164 (or (looking-at "(") (forward-sexp -1))
|
|
165 (read (point-marker))))))
|
|
166 (cond ((stringp exp)
|
|
167 (if (setq val (re-search-forward exp nil t))
|
|
168 (let* ((oo (make-extent (match-beginning 0) (match-end 0))))
|
|
169 (set-extent-face oo 'highlight)
|
|
170 (set-extent-priority oo 1000)
|
|
171 ;; wait for button release...
|
|
172 (setq unread-command-event (next-command-event))
|
|
173 (delete-extent oo))
|
|
174 (message "Regex \"%s\" not found" exp)
|
|
175 (ding nil 'quiet)))
|
|
176 (t (setq val (if (fboundp 'eval-interactive)
|
|
177 (eval-interactive exp)
|
|
178 (eval exp)))))
|
|
179 (setq result-str (prin1-to-string val))
|
|
180 ;; #### -- need better test
|
|
181 (if (and (not force-window)
|
|
182 (<= (length result-str) (window-width (selected-window))))
|
|
183 (message "%s" result-str)
|
|
184 (with-output-to-temp-buffer "*Mouse-Eval*"
|
|
185 (condition-case nil
|
|
186 (pprint val)
|
|
187 (error (prin1 val))))
|
|
188 )))
|
|
189
|
|
190 (defun mouse-line-length (event)
|
|
191 "Print the length of the line indicated by the pointer."
|
|
192 (interactive "@e")
|
|
193 (save-excursion
|
|
194 (mouse-set-point event)
|
|
195 (message "Line length: %d" (- (progn (end-of-line) (point))
|
|
196 (progn (beginning-of-line) (point)))))
|
|
197 (sleep-for 1))
|
|
198
|
|
199 (defun mouse-set-mark (event)
|
|
200 "Select Emacs window mouse is on, and set mark at mouse position.
|
|
201 Display cursor at that position for a second."
|
|
202 (interactive "@e")
|
|
203 (let ((point-save (point)))
|
|
204 (unwind-protect
|
|
205 (progn (mouse-set-point event)
|
|
206 (push-mark nil t)
|
|
207 (sit-for 1))
|
|
208 (goto-char point-save))))
|
|
209
|
|
210 (defun mouse-scroll (event)
|
|
211 "Scroll point to the mouse position."
|
|
212 (interactive "@e")
|
|
213 (save-excursion
|
|
214 (mouse-set-point event)
|
|
215 (recenter 0)
|
|
216 (scroll-right (event-x event))))
|
|
217
|
|
218 (defun mouse-del-char (event)
|
|
219 "Delete the char pointed to by the mouse."
|
|
220 (interactive "@e")
|
|
221 (save-excursion
|
|
222 (mouse-set-point event)
|
|
223 (delete-char 1 nil)))
|
|
224
|
|
225 (defun mouse-kill-line (event)
|
|
226 "Kill the line pointed to by the mouse."
|
|
227 (interactive "@e")
|
|
228 (save-excursion
|
|
229 (mouse-set-point event)
|
|
230 (kill-line nil)))
|
|
231
|
|
232 (defun mouse-bury-buffer (event)
|
|
233 "Bury the buffer pointed to by the mouse, thus selecting the next one."
|
|
234 (interactive "e")
|
|
235 (save-selected-window
|
|
236 (select-window (event-window event))
|
|
237 (bury-buffer)))
|
|
238
|
|
239 (defun mouse-unbury-buffer (event)
|
|
240 "Unbury and select the most recently buried buffer."
|
|
241 (interactive "e")
|
|
242 (save-selected-window
|
|
243 (select-window (event-window event))
|
|
244 (let* ((bufs (buffer-list))
|
|
245 (entry (1- (length bufs)))
|
|
246 val)
|
|
247 (while (not (setq val (nth entry bufs)
|
|
248 val (and (/= (aref (buffer-name val) 0)
|
|
249 ? )
|
|
250 val)))
|
|
251 (setq entry (1- entry)))
|
|
252 (switch-to-buffer val))))
|
|
253
|
|
254 (defun narrow-window-to-region (m n)
|
|
255 "Narrow window to region between point and last mark"
|
|
256 (interactive "r")
|
|
257 (save-excursion
|
|
258 (save-restriction
|
|
259 (if (eq (selected-window) (next-window))
|
|
260 (split-window))
|
|
261 (goto-char m)
|
|
262 (recenter 0)
|
|
263 (if (eq (selected-window)
|
|
264 (if (zerop (minibuffer-depth))
|
|
265 (next-window)))
|
|
266 ()
|
|
267 (shrink-window (- (- (window-height) (count-lines m n)) 1))))))
|
|
268
|
|
269 (defun mouse-window-to-region (event)
|
|
270 "Narrow window to region between cursor and mouse pointer."
|
|
271 (interactive "@e")
|
|
272 (let ((point-save (point)))
|
|
273 (unwind-protect
|
|
274 (progn (mouse-set-point event)
|
|
275 (push-mark nil t)
|
|
276 (sit-for 1))
|
|
277 (goto-char point-save)
|
|
278 (narrow-window-to-region (region-beginning) (region-end)))))
|
|
279
|
|
280 (defun mouse-ignore ()
|
|
281 "Don't do anything."
|
|
282 (interactive))
|
|
283
|
|
284
|
|
285 ;;
|
|
286 ;; Commands for the scroll bar.
|
|
287 ;;
|
|
288
|
|
289 ;; #### this stuff has never ever been used and should be junked.
|
|
290
|
|
291 ;; Vertical bar
|
|
292
|
|
293 (defun mouse-scroll-down (nlines)
|
|
294 "Junk me, please."
|
|
295 (interactive "@p")
|
|
296 (scroll-down nlines))
|
|
297
|
|
298 (defun mouse-scroll-up (nlines)
|
|
299 "Junk me, please."
|
|
300 (interactive "@p")
|
|
301 (scroll-up nlines))
|
|
302
|
|
303 (defun mouse-scroll-down-full ()
|
|
304 "Junk me, please."
|
|
305 (interactive "@")
|
|
306 (scroll-down nil))
|
|
307
|
|
308 (defun mouse-scroll-up-full ()
|
|
309 "Junk me, please."
|
|
310 (interactive "@")
|
|
311 (scroll-up nil))
|
|
312
|
|
313 (defun mouse-scroll-move-cursor (nlines)
|
|
314 "Junk me, please."
|
|
315 (interactive "@p")
|
|
316 (move-to-window-line nlines))
|
|
317
|
|
318 (defun mouse-scroll-absolute (event)
|
|
319 "Junk me, please."
|
|
320 (interactive "@e")
|
|
321 (let* ((position (event-x event))
|
|
322 (length (event-y event))
|
|
323 (size (buffer-size))
|
|
324 (scale-factor (max 1 (/ 8000000 size)))
|
|
325 (newpos (* (/ (* (/ size scale-factor) position) length)
|
|
326 scale-factor)))
|
|
327 (goto-char newpos)
|
|
328 (recenter '(4))))
|
|
329
|
|
330 ;; These scroll while the invoking button is depressed.
|
|
331
|
|
332 (defvar scrolled-lines 0)
|
|
333 (defvar scroll-speed 1)
|
|
334
|
|
335 (defun incr-scroll-down (event)
|
|
336 "Junk me, please."
|
|
337 (interactive "@e")
|
|
338 (setq scrolled-lines 0)
|
|
339 (incremental-scroll scroll-speed))
|
|
340
|
|
341 (defun incr-scroll-up (event)
|
|
342 "Junk me, please."
|
|
343 (interactive "@e")
|
|
344 (setq scrolled-lines 0)
|
|
345 (incremental-scroll (- scroll-speed)))
|
|
346
|
|
347 (defun incremental-scroll (n)
|
|
348 "Junk me, please."
|
|
349 (let ((down t))
|
|
350 (while down
|
|
351 (sit-for mouse-track-scroll-delay)
|
|
352 (cond ((input-pending-p)
|
|
353 (let ((event (next-command-event)))
|
|
354 (if (or (button-press-event-p event)
|
|
355 (button-release-event-p event))
|
|
356 (setq down nil))
|
|
357 (dispatch-event event))))
|
|
358 (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
|
|
359 (scroll-down n))))
|
|
360
|
|
361 (defun incr-scroll-stop (event)
|
|
362 "Junk me, please."
|
|
363 (interactive "@e")
|
|
364 (setq scrolled-lines 0)
|
|
365 (sleep-for 1))
|
|
366
|
|
367
|
|
368 (defun mouse-scroll-left (ncolumns)
|
|
369 "Junk me, please."
|
|
370 (interactive "@p")
|
|
371 (scroll-left ncolumns))
|
|
372
|
|
373 (defun mouse-scroll-right (ncolumns)
|
|
374 "Junk me, please."
|
|
375 (interactive "@p")
|
|
376 (scroll-right ncolumns))
|
|
377
|
|
378 (defun mouse-scroll-left-full ()
|
|
379 "Junk me, please."
|
|
380 (interactive "@")
|
|
381 (scroll-left nil))
|
|
382
|
|
383 (defun mouse-scroll-right-full ()
|
|
384 "Junk me, please."
|
|
385 (interactive "@")
|
|
386 (scroll-right nil))
|
|
387
|
|
388 (defun mouse-scroll-move-cursor-horizontally (ncolumns)
|
|
389 "Junk me, please."
|
|
390 (interactive "@p")
|
|
391 (move-to-column ncolumns))
|
|
392
|
|
393 (defun mouse-scroll-absolute-horizontally (event)
|
|
394 "Junk me, please."
|
|
395 (interactive "@e")
|
|
396 (set-window-hscroll (selected-window) 33))
|
|
397
|
|
398
|
|
399
|
|
400 ;;; mouse/selection tracking
|
|
401 ;;; generalized mouse-track
|
|
402
|
|
403 (defvar mouse-track-down-hook nil
|
|
404 "Function or functions called when the user presses the mouse.
|
|
405 This hook is invoked by `mouse-track'; thus, it will not be called
|
|
406 for any buttons with a different binding. The functions will be
|
|
407 called with two arguments: the button-press event and a click
|
|
408 count (see `mouse-track-click-hook').
|
|
409
|
|
410 If any function returns non-nil, the remaining functions will not be
|
|
411 called.
|
|
412
|
|
413 Note that most applications should take action when the mouse is
|
|
414 released, not when it is pressed.'")
|
|
415
|
|
416 (defvar mouse-track-drag-hook nil
|
|
417 "Function or functions called when the user drags the mouse.
|
|
418 This hook is invoked by `mouse-track'; thus, it will not be called
|
|
419 for any buttons with a different binding. The functions will be
|
|
420 called with three arguments: the mouse-motion event, a click
|
|
421 count (see `mouse-track-click-hook'), and whether the call to
|
|
422 this hook occurred as a result of a drag timeout (see
|
|
423 `mouse-track-scroll-delay').
|
|
424
|
|
425 If any function returns non-nil, the remaining functions will not be
|
|
426 called.
|
|
427
|
|
428 Note that no calls to this function will be made until the user
|
|
429 initiates a drag (i.e. moves the mouse more than a certain
|
|
430 threshold in either the X or the Y direction, as defined by
|
|
431 `mouse-track-x-threshold' and `mouse-track-y-threshold').
|
|
432
|
|
433 See also `mouse-track-drag-up-hook'.")
|
|
434
|
|
435 (defvar mouse-track-drag-up-hook nil
|
|
436 "Function or functions called when the user finishes a drag.
|
|
437 This hook is invoked by `mouse-track'; thus, it will not be called
|
|
438 for any buttons with a different binding. The functions will be
|
|
439 called with two arguments: the button-press event and a click
|
|
440 count (see `mouse-track-click-hook').
|
|
441
|
|
442 If any function returns non-nil, the remaining functions will not be
|
|
443 called.
|
|
444
|
|
445 Note that this hook will not be invoked unless the user has
|
|
446 initiated a drag, i.e. moved the mouse more than a certain threshold
|
|
447 (see `mouse-track-drag-hook'). When this function is invoked,
|
|
448 `mouse-track-drag-hook' will have been invoked at least once.
|
|
449
|
|
450 See also `mouse-track-click-hook'.")
|
|
451
|
|
452 (defvar mouse-track-click-hook nil
|
|
453 "Function or functions called when the user clicks the mouse.
|
|
454 `Clicking' means pressing and releasing the mouse without having
|
|
455 initiated a drag (i.e. without having moved more than a certain
|
|
456 threshold -- see `mouse-track-drag-hook').
|
|
457
|
|
458 This hook is invoked by `mouse-track'; thus, it will not be called
|
|
459 for any buttons with a different binding. The functions will be
|
|
460 called with two arguments: the button-release event and a click
|
|
461 count, which specifies the number of times that the mouse has been
|
|
462 clicked in a series of clicks, each of which is separated by at most
|
|
463 `mouse-track-multi-click-time'. This can be used to implement actions
|
|
464 that are called on double clicks, triple clicks, etc.
|
|
465
|
|
466 If any function returns non-nil, the remaining functions will not be
|
|
467 called.
|
|
468
|
|
469 See also `mouse-track-drag-up-hook.")
|
|
470
|
|
471 (defvar mouse-track-up-hook nil
|
|
472 "Function or functions called when the user releases the mouse.
|
|
473 This hook is invoked by `mouse-track'; thus, it will not be called
|
|
474 for any buttons with a different binding. The functions will be
|
|
475 called with two arguments: the button-release event and a click
|
|
476 count (see `mouse-track-click-hook').
|
|
477
|
|
478 For many applications, it is more appropriate to use one or both
|
|
479 of `mouse-track-click-hook' and `mouse-track-drag-up-hook'.")
|
|
480
|
|
481 (defvar mouse-track-cleanup-hook nil
|
|
482 "Function or functions called when `mouse-track' terminates.
|
|
483 This hook will be called in all circumstances, even upon a
|
|
484 non-local exit out of `mouse-track', and so is useful for
|
|
485 doing cleanup work such as removing extents that may have
|
|
486 been created during the operation of `mouse-track'.
|
|
487
|
|
488 Unlike all of the other mouse-track hooks, this is a \"normal\"
|
|
489 hook: the hook functions are called with no arguments, and
|
|
490 all hook functions are called regardless of their return
|
|
491 values.")
|
|
492
|
|
493 (defvar mouse-track-multi-click-time 400
|
|
494 "Maximum number of milliseconds allowed between clicks for a multi-click.
|
|
495 See `mouse-track-click-hook'.")
|
|
496
|
|
497 (defvar mouse-track-scroll-delay 100
|
|
498 "Maximum of milliseconds between calls to `mouse-track-drag-hook'.
|
|
499 If the user is dragging the mouse (i.e. the button is held down and
|
|
500 a drag has been initiated) and does not move the mouse for this many
|
|
501 milliseconds, the hook will be called with t as the value of the
|
|
502 WAS-TIMEOUT parameter. This can be used to implement scrolling
|
|
503 in a selection when the user drags the mouse out the window it
|
|
504 was in.
|
|
505
|
|
506 A value of nil disables the timeout feature.")
|
|
507
|
|
508 (defvar mouse-track-x-threshold '(face-width 'default)
|
|
509 "Minimum number of pixels in the X direction for a drag to be initiated.
|
|
510 If the mouse is moved more than either the X or Y threshold while the
|
|
511 button is held down (see also `mouse-track-y-threshold'), then a drag
|
|
512 is initiated; otherwise the gesture is considered to be a click.
|
|
513 See `mouse-track'.
|
|
514
|
|
515 The value should be either a number of a form to be evaluated to
|
|
516 produce a number.")
|
|
517
|
|
518 (defvar mouse-track-y-threshold '(face-height 'default)
|
|
519 "Minimum number of pixels in the Y direction for a drag to be initiated.
|
|
520 If the mouse is moved more than either the X or Y threshold while the
|
|
521 button is held down (see also `mouse-track-x-threshold'), then a drag
|
|
522 is initiated; otherwise the gesture is considered to be a click.
|
|
523 See `mouse-track'.
|
|
524
|
|
525 The value should be either a number of a form to be evaluated to
|
|
526 produce a number.")
|
|
527
|
|
528 ;; these variables are private to mouse-track.
|
|
529 (defvar mouse-track-up-time nil)
|
|
530 (defvar mouse-track-up-x nil)
|
|
531 (defvar mouse-track-up-y nil)
|
|
532 (defvar mouse-track-timeout-id nil)
|
|
533 (defvar mouse-track-click-count nil)
|
|
534
|
|
535 (defun mouse-track-set-timeout (event)
|
|
536 (if mouse-track-timeout-id
|
|
537 (disable-timeout mouse-track-timeout-id))
|
|
538 (if mouse-track-scroll-delay
|
|
539 (setq mouse-track-timeout-id
|
|
540 (add-timeout (/ mouse-track-scroll-delay 1000.0)
|
|
541 'mouse-track-scroll-undefined
|
|
542 (copy-event event)))))
|
|
543
|
|
544 (defun mouse-track-run-hook (hook event &rest args)
|
|
545 ;; ugh, can't use run-special-hook-with-args because we
|
|
546 ;; have to get the value using symbol-value-in-buffer.
|
|
547 ;; Doing a save-excursion/set-buffer is wrong because
|
|
548 ;; the hook might want to change the buffer, but just
|
|
549 ;; doing a set-buffer is wrong because the hook might
|
|
550 ;; not want to change the buffer.
|
|
551 (let ((buffer (event-buffer event)))
|
|
552 (if mouse-grabbed-buffer (setq buffer mouse-grabbed-buffer))
|
|
553 (if buffer
|
|
554 (let ((value (symbol-value-in-buffer hook buffer nil)))
|
|
555 (if (and (listp value) (not (eq (car value) 'lambda)))
|
|
556 (let (retval)
|
|
557 (while (and value
|
|
558 (not (setq retval (apply (car value) event args))))
|
|
559 (setq value (cdr value)))
|
|
560 retval)
|
|
561 (apply value event args))))))
|
|
562
|
|
563 (defun mouse-track-scroll-undefined (random)
|
|
564 ;; the old implementation didn't actually define this function,
|
|
565 ;; and in normal use it won't ever be called because the timeout
|
|
566 ;; will either be removed before it fires or will be picked off
|
|
567 ;; with next-event and not dispatched. However, if you're
|
|
568 ;; attempting to debug a click-hook (which is pretty damn
|
|
569 ;; difficult to do), this function may get called.
|
|
570 )
|
|
571
|
|
572 (defun mouse-track (event)
|
|
573 "Make a selection with the mouse. This should be bound to a mouse button.
|
|
574 The behavior of XEmacs during mouse selection is customizable using various
|
|
575 hooks and variables: see `mouse-track-click-hook', `mouse-track-drag-hook',
|
|
576 `mouse-track-drag-up-hook', `mouse-track-down-hook', `mouse-track-up-hook',
|
|
577 `mouse-track-cleanup-hook', `mouse-track-multi-click-time',
|
|
578 `mouse-track-scroll-delay', `mouse-track-x-threshold', and
|
|
579 `mouse-track-y-threshold'.
|
|
580
|
|
581 Default handlers are provided to implement standard selecting/positioning
|
|
582 behavior. You can explicitly request this default behavior, and override
|
|
583 any custom-supplied handlers, by using the function `mouse-track-default'
|
|
584 instead of `mouse-track'.
|
|
585
|
|
586 Default behavior is as follows:
|
|
587
|
|
588 If you click-and-drag, the selection will be set to the region between the
|
|
589 point of the initial click and the point at which you release the button.
|
|
590 These positions need not be ordered.
|
|
591
|
|
592 If you click-and-release without moving the mouse, then the point is moved
|
|
593 and the selection is disowned (there will be no selection owner). The mark
|
|
594 will be set to the previous position of point.
|
|
595
|
|
596 If you double-click, the selection will extend by symbols instead of by
|
|
597 characters. If you triple-click, the selection will extend by lines.
|
|
598
|
|
599 If you drag the mouse off the top or bottom of the window, you can select
|
|
600 pieces of text which are larger than the visible part of the buffer; the
|
|
601 buffer will scroll as necessary.
|
|
602
|
|
603 The selected text becomes the current X Selection. The point will be left
|
|
604 at the position at which you released the button, and the mark will be left
|
|
605 at the initial click position."
|
|
606 (interactive "e")
|
|
607 (let ((mouse-down t)
|
|
608 (xthresh (eval mouse-track-x-threshold))
|
|
609 (ythresh (eval mouse-track-y-threshold))
|
|
610 (orig-x (event-x-pixel event))
|
|
611 (orig-y (event-y-pixel event))
|
|
612 (buffer (event-buffer event))
|
|
613 (mouse-grabbed-buffer (event-buffer event))
|
|
614 mouse-moved)
|
|
615 (if (or (not mouse-track-up-x)
|
|
616 (not mouse-track-up-y)
|
|
617 (not mouse-track-up-time)
|
|
618 (> (- (event-timestamp event) mouse-track-up-time)
|
|
619 mouse-track-multi-click-time)
|
|
620 (> (abs (- mouse-track-up-x orig-x)) xthresh)
|
|
621 (> (abs (- mouse-track-up-y orig-y)) ythresh))
|
|
622 (setq mouse-track-click-count 1)
|
|
623 (setq mouse-track-click-count (1+ mouse-track-click-count)))
|
|
624 (if (not (event-window event))
|
|
625 (error "Not over a window."))
|
|
626 (mouse-track-run-hook 'mouse-track-down-hook
|
|
627 event mouse-track-click-count)
|
|
628 (unwind-protect
|
|
629 (while mouse-down
|
|
630 (setq event (next-event event))
|
|
631 (cond ((motion-event-p event)
|
|
632 (if (and (not mouse-moved)
|
|
633 (or (> (abs (- (event-x-pixel event) orig-x))
|
|
634 xthresh)
|
|
635 (> (abs (- (event-y-pixel event) orig-y))
|
|
636 ythresh)))
|
|
637 (setq mouse-moved t))
|
|
638 (if mouse-moved
|
|
639 (mouse-track-run-hook 'mouse-track-drag-hook
|
|
640 event mouse-track-click-count nil))
|
|
641 (mouse-track-set-timeout event))
|
|
642 ((and (timeout-event-p event)
|
|
643 (eq (event-function event)
|
|
644 'mouse-track-scroll-undefined))
|
|
645 (if mouse-moved
|
|
646 (mouse-track-run-hook 'mouse-track-drag-hook
|
|
647 (event-object event) mouse-track-click-count t))
|
|
648 (mouse-track-set-timeout (event-object event)))
|
|
649 ((button-release-event-p event)
|
|
650 (setq mouse-track-up-time (event-timestamp event))
|
|
651 (setq mouse-track-up-x (event-x-pixel event))
|
|
652 (setq mouse-track-up-y (event-y-pixel event))
|
|
653 (setq mouse-down nil)
|
|
654 (mouse-track-run-hook 'mouse-track-up-hook
|
|
655 event mouse-track-click-count)
|
|
656 (if mouse-moved
|
|
657 (mouse-track-run-hook 'mouse-track-drag-up-hook
|
|
658 event mouse-track-click-count)
|
|
659 (mouse-track-run-hook 'mouse-track-click-hook
|
|
660 event mouse-track-click-count)))
|
|
661 ((key-press-event-p event)
|
|
662 (error "Selection aborted"))
|
|
663 (t
|
|
664 (dispatch-event event))))
|
|
665 ;; protected
|
|
666 (if mouse-track-timeout-id
|
|
667 (disable-timeout mouse-track-timeout-id))
|
|
668 (setq mouse-track-timeout-id nil)
|
|
669 (and buffer
|
|
670 (save-excursion
|
|
671 (set-buffer buffer)
|
|
672 (run-hooks 'mouse-track-cleanup-hook))))))
|
|
673
|
|
674
|
|
675 ;;;;;;;;;;;; default handlers: new version of mouse-track
|
|
676
|
|
677 (defvar default-mouse-track-type nil)
|
|
678 (defvar default-mouse-track-type-list '(char word line))
|
|
679 (defvar default-mouse-track-window nil)
|
|
680 (defvar default-mouse-track-extent nil)
|
|
681 (defvar default-mouse-track-adjust nil)
|
|
682 (defvar default-mouse-track-min-anchor nil)
|
|
683 (defvar default-mouse-track-max-anchor nil)
|
|
684 (defvar default-mouse-track-result nil)
|
|
685 (defvar default-mouse-track-down-event nil)
|
|
686
|
|
687 (defun default-mouse-track-set-point-in-window (event window)
|
|
688 (if (not (and (not (event-over-modeline-p event))
|
|
689 (eq (event-window event) window)
|
|
690 (let ((p (event-closest-point event)))
|
|
691 (and p (pos-visible-in-window-p p window)))))
|
|
692 nil
|
|
693 (mouse-set-point event)
|
|
694 t))
|
|
695
|
|
696 (defun default-mouse-track-scroll-and-set-point (event window)
|
|
697 (select-window window)
|
|
698 (let ((edges (window-pixel-edges window))
|
|
699 (row (event-y-pixel event))
|
|
700 (height (face-height 'default)))
|
|
701 (cond ((< (abs (- row (nth 1 edges))) (abs (- row (nth 3 edges))))
|
|
702 ;; closer to window's top than to bottom, so move up
|
|
703 (let ((delta (max 1 (/ (- (nth 1 edges) row) height))))
|
|
704 (condition-case () (scroll-down delta) (error))
|
|
705 (goto-char (window-start))))
|
|
706 ((>= (point) (point-max)))
|
|
707 (t
|
|
708 ;; scroll by one line if over the modeline or a clipped line
|
|
709 (let ((delta (if (or (event-over-modeline-p event)
|
|
710 (< row (nth 3 edges)))
|
|
711 1
|
|
712 (+ (/ (- row (nth 3 edges)) height) 1)))
|
|
713 (close-pos (event-closest-point event)))
|
|
714 (condition-case () (scroll-up delta) (error))
|
|
715 (if (and close-pos (pos-visible-in-window-p close-pos))
|
|
716 (goto-char close-pos)
|
|
717 (goto-char (window-end))
|
|
718 (vertical-motion delta)
|
|
719 ;; window-end reports the end of the clipped line, even if
|
|
720 ;; scroll-on-clipped-lines is t. compensate.
|
|
721 ;; (If window-end gets fixed this can be removed.)
|
|
722 (if (not (pos-visible-in-window-p (max (1- (point))
|
|
723 (point-min))))
|
|
724 (vertical-motion -1))
|
|
725 (condition-case () (backward-char 1)
|
|
726 (error (end-of-line)))))))))
|
|
727
|
|
728
|
|
729 ;; This remembers the last position at which the user clicked, for the
|
|
730 ;; benefit of mouse-track-adjust (for example, button1; scroll until the
|
|
731 ;; position of the click is off the frame; then Sh-button1 to select the
|
|
732 ;; new region.
|
|
733 (defvar default-mouse-track-previous-point nil)
|
|
734
|
|
735 (defun default-mouse-track-set-point (event window)
|
|
736 (if (default-mouse-track-set-point-in-window event window)
|
|
737 nil
|
|
738 (default-mouse-track-scroll-and-set-point event window)))
|
|
739
|
|
740 (defsubst default-mouse-track-beginning-of-word (symbolp)
|
|
741 (let ((word-constituent (cond ((eq symbolp t) "\\w\\|\\s_\\|\\s'")
|
|
742 ((null symbolp) "\\w")
|
|
743 (t "[^ \t\n]")))
|
|
744 (white-space "[ \t]"))
|
|
745 (cond ((bobp) nil)
|
|
746 ((looking-at word-constituent)
|
|
747 (backward-char)
|
|
748 (while (and (not (bobp)) (looking-at word-constituent))
|
|
749 (backward-char))
|
|
750 (if (or (not (bobp)) (not (looking-at word-constituent)))
|
|
751 (forward-char)))
|
|
752 ((looking-at white-space)
|
|
753 (backward-char)
|
|
754 (while (looking-at white-space)
|
|
755 (backward-char))
|
|
756 (forward-char)))))
|
|
757
|
|
758 (defun default-mouse-track-end-of-word (symbolp)
|
|
759 (let ((word-constituent (cond ((eq symbolp t) "\\w\\|\\s_\\|\\s'")
|
|
760 ((null symbolp) "\\w")
|
|
761 (t "[^ \t\n]")))
|
|
762 (white-space "[ \t]"))
|
|
763 (cond ((looking-at word-constituent) ; word or symbol constituent
|
|
764 (while (looking-at word-constituent)
|
|
765 (forward-char)))
|
|
766 ((looking-at white-space) ; word or symbol constituent
|
|
767 (while (looking-at white-space)
|
|
768 (forward-char))))))
|
|
769
|
|
770 (defun default-mouse-track-normalize-point (type forwardp)
|
|
771 (cond ((eq type 'word)
|
|
772 ;; trap the beginning and end of buffer errors
|
|
773 (condition-case ()
|
74
|
774 (progn
|
|
775 (setq type (char-syntax (char-after (point))))
|
|
776 (if forwardp
|
|
777 (if (= type ?\()
|
|
778 (goto-char (scan-sexps (point) 1))
|
|
779 (if (= type ?\))
|
|
780 (forward-char 1)
|
|
781 (default-mouse-track-end-of-word t)))
|
|
782 (if (= type ?\))
|
|
783 (goto-char (scan-sexps (1+ (point)) -1))
|
|
784 (default-mouse-track-beginning-of-word t))))
|
0
|
785 (error ())))
|
|
786 ((eq type 'line)
|
|
787 (if forwardp (end-of-line) (beginning-of-line)))
|
|
788 ((eq type 'buffer)
|
|
789 (if forwardp (end-of-buffer) (beginning-of-buffer)))))
|
|
790
|
|
791 (defun default-mouse-track-next-move (min-anchor max-anchor extent)
|
|
792 (let ((anchor (if (<= (point) min-anchor) max-anchor min-anchor)))
|
|
793 (default-mouse-track-normalize-point
|
|
794 default-mouse-track-type (> (point) anchor))
|
|
795 (if (consp extent)
|
|
796 (default-mouse-track-next-move-rect anchor (point) extent)
|
|
797 (if extent
|
|
798 (if (<= anchor (point))
|
|
799 (set-extent-endpoints extent anchor (point))
|
|
800 (set-extent-endpoints extent (point) anchor))))))
|
|
801
|
|
802 (defun default-mouse-track-next-move-rect (start end extents &optional pad-p)
|
|
803 (if (< end start)
|
|
804 (let ((tmp start)) (setq start end end tmp)))
|
|
805 (cond
|
|
806 ((= start end) ; never delete the last remaining extent
|
|
807 (mapcar 'delete-extent (cdr extents))
|
|
808 (setcdr extents nil)
|
|
809 (set-extent-endpoints (car extents) start start))
|
|
810 (t
|
|
811 (let ((indent-tabs-mode nil) ; if pad-p, don't use tabs
|
|
812 (rest extents)
|
|
813 left right last p)
|
|
814 (save-excursion
|
|
815 (save-restriction
|
|
816 (goto-char end)
|
|
817 (setq right (current-column))
|
|
818 (goto-char start)
|
|
819 (setq left (current-column))
|
|
820 (if (< right left)
|
|
821 (let ((tmp left))
|
|
822 (setq left right right tmp)
|
|
823 (setq start (- start (- right left))
|
|
824 end (+ end (- right left)))))
|
|
825 ;; End may have been set to a value greater than point-max if drag
|
|
826 ;; or movement extends to end of buffer, so reset it.
|
|
827 (setq end (min end (point-max)))
|
|
828 (beginning-of-line)
|
|
829 (narrow-to-region (point) end)
|
|
830 (goto-char start)
|
|
831 (while (and rest (not (eobp)))
|
|
832 (setq p (point))
|
|
833 (move-to-column right pad-p)
|
|
834 (set-extent-endpoints (car rest) p (point))
|
|
835 ;; this code used to look at the return value
|
|
836 ;; of forward-line, but that doesn't work because
|
|
837 ;; forward-line has bogus behavior: If you're on
|
|
838 ;; the last line of a buffer but not at the very
|
|
839 ;; end, forward-line will move you to the very
|
|
840 ;; end and return 0 instead of 1, like it should.
|
|
841 ;; the result was frequent infinite loops here,
|
|
842 ;; creating very large numbers of extents at
|
|
843 ;; the same position. There was an N^2 sorting
|
|
844 ;; algorithm in extents.c for extents at a
|
|
845 ;; particular position, and the result was very
|
|
846 ;; bad news.
|
|
847 (forward-line 1)
|
|
848 (if (not (eobp))
|
|
849 (move-to-column left pad-p))
|
|
850 (setq last rest
|
|
851 rest (cdr rest)))
|
|
852 (cond (rest
|
|
853 (mapcar 'delete-extent rest)
|
|
854 (setcdr last nil))
|
|
855 ((not (eobp))
|
|
856 (while (not (eobp))
|
|
857 (setq p (point))
|
|
858 (move-to-column right pad-p)
|
|
859 (let ((e (make-extent p (point))))
|
|
860 (set-extent-face e (extent-face (car extents)))
|
|
861 (set-extent-priority e (extent-priority (car extents)))
|
|
862 (setcdr last (cons e nil))
|
|
863 (setq last (cdr last)))
|
|
864 (forward-line 1)
|
|
865 (if (not (eobp))
|
|
866 (move-to-column left pad-p))
|
|
867 )))))
|
|
868 ))))
|
|
869
|
|
870 (defun default-mouse-track-has-selection-p (buffer)
|
|
871 (and (or (not (eq 'x (console-type (selected-console))))
|
|
872 (x-selection-owner-p))
|
|
873 (extent-live-p primary-selection-extent)
|
|
874 (not (extent-detached-p primary-selection-extent))
|
|
875 (eq buffer (extent-object primary-selection-extent))))
|
|
876
|
|
877 (defun default-mouse-track-anchor (adjust previous-point)
|
|
878 (if adjust
|
|
879 (if (default-mouse-track-has-selection-p (current-buffer))
|
|
880 (let ((start (extent-start-position primary-selection-extent))
|
|
881 (end (extent-end-position primary-selection-extent)))
|
|
882 (cond ((< (point) start) end)
|
|
883 ((> (point) end) start)
|
|
884 ((> (- (point) start) (- end (point))) start)
|
|
885 (t end)))
|
|
886 previous-point)
|
|
887 (point)))
|
|
888
|
|
889 (defun default-mouse-track-maybe-own-selection (pair type)
|
|
890 (let ((start (car pair))
|
|
891 (end (cdr pair)))
|
|
892 (or (= start end) (push-mark (if (= (point) start) end start)))
|
|
893 (cond (zmacs-regions
|
|
894 (if (= start end)
|
|
895 nil
|
|
896 ;; #### UTTER KLUDGE.
|
|
897 ;; If we don't have this sit-for here, then triple-clicking
|
|
898 ;; will result in the line not being highlighted as it
|
|
899 ;; should. What appears to be happening is this:
|
|
900 ;;
|
|
901 ;; -- each time the button goes down, the selection is
|
|
902 ;; disowned (see comment "remove the existing selection
|
|
903 ;; to unclutter the display", below).
|
|
904 ;; -- this causes a SelectionClear event to be sent to
|
|
905 ;; XEmacs.
|
|
906 ;; -- each time the button goes up except the first, the
|
|
907 ;; selection is owned again.
|
|
908 ;; -- later, XEmacs processes the SelectionClear event.
|
|
909 ;; The selection code attempts to keep track of the
|
|
910 ;; time that it last asserted the selection, and
|
|
911 ;; compare it to the time of the SelectionClear event,
|
|
912 ;; to see if it's a bogus notification or not (as
|
|
913 ;; is the case here). However, for some unknown
|
|
914 ;; reason this doesn't work in the triple-clicking
|
|
915 ;; case, and the selection code bogusly thinks this
|
|
916 ;; SelectionClear event is the real thing.
|
|
917 ;; -- putting the sit-for in causes the pending
|
|
918 ;; SelectionClear events to get processed before
|
|
919 ;; the selection is reasserted, so everything works
|
|
920 ;; out OK.
|
|
921 ;;
|
|
922 ;; Presumably(?) this means there is a weird timing bug
|
|
923 ;; in the selection code, but there's not a chance in hell
|
|
924 ;; that I have the patience to track it down. Blame the
|
|
925 ;; designers of X for fucking everything up so badly.
|
|
926 ;;
|
|
927 ;; This was originally a sit-for 0 but that wasn't
|
|
928 ;; sufficient to make things work. Even this isn't
|
|
929 ;; always sufficient but it seems to give something
|
|
930 ;; approaching a 99% success rate. Making it higher yet
|
|
931 ;; would help guarantee success with the price that the
|
|
932 ;; delay would start to become noticable.
|
|
933 ;;
|
|
934 (sit-for 0.15 t)
|
|
935 (zmacs-activate-region)))
|
|
936 ((eq 'x (console-type (selected-console)))
|
|
937 (if (= start end)
|
|
938 (x-disown-selection type)
|
|
939 (if (consp default-mouse-track-extent)
|
|
940 ;; own the rectangular region
|
|
941 ;; this is a hack
|
|
942 (let ((r default-mouse-track-extent))
|
|
943 (save-excursion
|
|
944 (set-buffer (get-buffer-create " *rect yank temp buf*"))
|
|
945 (while r
|
|
946 (insert (extent-string (car r)) "\n")
|
|
947 (setq r (cdr r)))
|
|
948 (x-own-selection (buffer-substring (point-min) (point-max)))
|
|
949 (kill-buffer (current-buffer))))
|
|
950 (x-own-selection (cons (set-marker (make-marker) start)
|
|
951 (set-marker (make-marker) end))
|
|
952 type)))))
|
|
953 (if (and (eq 'x (console-type (selected-console)))
|
|
954 (not (= start end)))
|
|
955 ;; I guess cutbuffers should do something with rectangles too.
|
|
956 ;; does anybody use them?
|
|
957 (x-store-cutbuffer (buffer-substring start end)))))
|
|
958
|
|
959 (defun default-mouse-track-deal-with-down-event (click-count)
|
|
960 (let ((event default-mouse-track-down-event))
|
|
961 (if (null event) nil
|
|
962 (select-frame (event-frame event))
|
|
963 (let ((adjust default-mouse-track-adjust)
|
|
964 ;; ####When you click on the splash-screen,
|
|
965 ;; event-{closest-,}point can be out of bounds. Should
|
|
966 ;; event-closest-point really be allowed to return a bad
|
|
967 ;; position like that? Maybe pixel_to_glyph_translation
|
|
968 ;; needs to invalidate its cache when the buffer changes.
|
|
969 ;; -dkindred@cs.cmu.edu
|
|
970 (close-pos (save-excursion
|
|
971 (set-buffer (event-buffer event))
|
|
972 (let ((p (event-closest-point event)))
|
|
973 (and p (min (max p (point-min)) (point-max))))))
|
|
974 extent previous-point)
|
|
975
|
|
976 (if (not (event-window event))
|
|
977 (error "not over window?"))
|
|
978 (setq default-mouse-track-type
|
|
979 (nth (mod (1- click-count)
|
|
980 (length default-mouse-track-type-list))
|
|
981 default-mouse-track-type-list))
|
|
982 (setq default-mouse-track-window (event-window event))
|
|
983 ;; Note that the extent used here is NOT the extent which
|
|
984 ;; ends up as the value of zmacs-region-extent - this one is used
|
|
985 ;; just during mouse-dragging.
|
|
986 (setq default-mouse-track-extent
|
|
987 (make-extent close-pos close-pos (event-buffer event)))
|
|
988 (setq extent default-mouse-track-extent)
|
|
989 (set-extent-face extent 'zmacs-region)
|
|
990 ;; While the selection is being dragged out, give the selection extent
|
|
991 ;; slightly higher priority than any mouse-highlighted extent, so that
|
|
992 ;; the exact endpoints of the selection will be visible while the mouse
|
|
993 ;; is down. Normally, the selection and mouse highlighting have the
|
|
994 ;; same priority, so that conflicts between the two of them are
|
|
995 ;; resolved by the usual size-and-endpoint-comparison method.
|
|
996 (set-extent-priority extent (1+ mouse-highlight-priority))
|
|
997 (if mouse-track-rectangle-p
|
|
998 (setq default-mouse-track-extent
|
|
999 (list default-mouse-track-extent)))
|
|
1000
|
|
1001 (setq previous-point
|
|
1002 (if (and adjust
|
|
1003 (markerp default-mouse-track-previous-point)
|
|
1004 (eq (current-buffer)
|
|
1005 (marker-buffer default-mouse-track-previous-point)))
|
|
1006 (marker-position default-mouse-track-previous-point)
|
|
1007 (point)))
|
|
1008 (default-mouse-track-set-point event default-mouse-track-window)
|
|
1009 (if (not adjust)
|
|
1010 (if (markerp default-mouse-track-previous-point)
|
|
1011 (set-marker default-mouse-track-previous-point (point))
|
|
1012 (setq default-mouse-track-previous-point (point-marker))))
|
|
1013 ;;
|
|
1014 ;; adjust point to a word or line boundary if appropriate
|
|
1015 (let ((anchor (default-mouse-track-anchor adjust previous-point)))
|
|
1016 (setq default-mouse-track-min-anchor
|
|
1017 (save-excursion (goto-char anchor)
|
|
1018 (default-mouse-track-normalize-point
|
|
1019 default-mouse-track-type nil)
|
|
1020 (point)))
|
|
1021 (setq default-mouse-track-max-anchor
|
|
1022 (save-excursion (goto-char anchor)
|
|
1023 (default-mouse-track-normalize-point
|
|
1024 default-mouse-track-type t)
|
|
1025 (point))))
|
|
1026 ;;
|
|
1027 ;; remove the existing selection to unclutter the display
|
|
1028 (if (not adjust)
|
|
1029 (cond (zmacs-regions
|
|
1030 (zmacs-deactivate-region))
|
|
1031 ((eq 'x (console-type (selected-console)))
|
|
1032 (x-disown-selection)))))
|
|
1033 (setq default-mouse-track-down-event nil))))
|
|
1034
|
|
1035 (defun default-mouse-track-down-hook (event click-count)
|
|
1036 (setq default-mouse-track-down-event (copy-event event))
|
|
1037 nil)
|
|
1038
|
104
|
1039 (defun default-mouse-track-cleanup-extents-hook ()
|
|
1040 (remove-hook 'pre-command-hook 'default-mouse-track-cleanup-extents-hook)
|
0
|
1041 (let ((extent default-mouse-track-extent))
|
|
1042 (if (consp extent) ; rectangle-p
|
|
1043 (mapcar 'delete-extent extent)
|
|
1044 (if extent
|
|
1045 (delete-extent extent)))))
|
|
1046
|
104
|
1047 (defun default-mouse-track-cleanup-hook ()
|
|
1048 (if zmacs-regions
|
|
1049 (funcall 'default-mouse-track-cleanup-extents-hook)
|
|
1050 (let ((extent default-mouse-track-extent)
|
110
|
1051 (func #'(lambda (e)
|
|
1052 (and (extent-live-p e)
|
|
1053 (set-extent-face e 'primary-selection)))))
|
104
|
1054 (add-hook 'pre-command-hook 'default-mouse-track-cleanup-extents-hook)
|
|
1055 (if (consp extent) ; rectangle-p
|
|
1056 (mapcar func extent)
|
|
1057 (if extent
|
|
1058 (funcall func extent))))))
|
|
1059
|
0
|
1060 (defun default-mouse-track-cleanup-extent ()
|
|
1061 (let ((dead-func
|
|
1062 (function (lambda (x)
|
|
1063 (or (not (extent-live-p x))
|
|
1064 (extent-detached-p x)))))
|
|
1065 (extent default-mouse-track-extent))
|
|
1066 (if (consp extent)
|
|
1067 (if (some dead-func extent)
|
|
1068 (let (newval)
|
|
1069 (mapcar (function (lambda (x)
|
|
1070 (if (not (funcall dead-func x))
|
|
1071 (setq newval (cons x newval)))))
|
|
1072 extent)
|
|
1073 (setq default-mouse-track-extent (nreverse newval))))
|
|
1074 (if (funcall dead-func extent)
|
|
1075 (setq default-mouse-track-extent nil)))))
|
|
1076
|
|
1077 (defun default-mouse-track-drag-hook (event click-count was-timeout)
|
|
1078 (default-mouse-track-deal-with-down-event click-count)
|
|
1079 (default-mouse-track-set-point event default-mouse-track-window)
|
|
1080 (default-mouse-track-cleanup-extent)
|
|
1081 (default-mouse-track-next-move default-mouse-track-min-anchor
|
|
1082 default-mouse-track-max-anchor
|
|
1083 default-mouse-track-extent)
|
|
1084 t)
|
|
1085
|
|
1086 (defun default-mouse-track-return-dragged-selection (event)
|
|
1087 (default-mouse-track-cleanup-extent)
|
|
1088 (let ((extent default-mouse-track-extent)
|
|
1089 result)
|
|
1090 (default-mouse-track-set-point-in-window event default-mouse-track-window)
|
|
1091 (default-mouse-track-next-move default-mouse-track-min-anchor
|
|
1092 default-mouse-track-max-anchor
|
|
1093 extent)
|
|
1094 (cond ((consp extent) ; rectangle-p
|
|
1095 (let ((first (car extent))
|
|
1096 (last (car (setq extent (nreverse extent)))))
|
|
1097 ;; nreverse is destructive so we need to reset this
|
|
1098 (setq default-mouse-track-extent extent)
|
|
1099 (setq result (cons (extent-start-position first)
|
|
1100 (extent-end-position last)))
|
|
1101 ;; kludge to fix up region when dragging backwards...
|
|
1102 (if (and (/= (point) (extent-start-position first))
|
|
1103 (/= (point) (extent-end-position last))
|
|
1104 (= (point) (extent-end-position first)))
|
|
1105 (goto-char (car result)))))
|
|
1106 (extent
|
|
1107 (setq result (cons (extent-start-position extent)
|
|
1108 (extent-end-position extent)))))
|
|
1109 ;; Minor kludge: if we're selecting in line-mode, include the
|
|
1110 ;; final newline. It's hard to do this in *-normalize-point.
|
|
1111 (if (and result (eq default-mouse-track-type 'line))
|
|
1112 (let ((end-p (= (point) (cdr result))))
|
|
1113 (goto-char (cdr result))
|
|
1114 (if (not (eobp))
|
|
1115 (setcdr result (1+ (cdr result))))
|
|
1116 (goto-char (if end-p (cdr result) (car result)))))
|
|
1117 ;;; ;; Minor kludge sub 2. If in char mode, and we drag the
|
|
1118 ;;; ;; mouse past EOL, include the newline.
|
|
1119 ;;; ;;
|
|
1120 ;;; ;; Major problem: can't easily distinguish between being
|
|
1121 ;;; ;; just past the last char on a line, and well past it,
|
|
1122 ;;; ;; to determine whether or not to include it in the region
|
|
1123 ;;; ;;
|
|
1124 ;;; (if nil ; (eq default-mouse-track-type 'char)
|
|
1125 ;;; (let ((after-end-p (and (not (eobp))
|
|
1126 ;;; (eolp)
|
|
1127 ;;; (> (point) (car result)))))
|
|
1128 ;;; (if after-end-p
|
|
1129 ;;; (progn
|
|
1130 ;;; (setcdr result (1+ (cdr result)))
|
|
1131 ;;; (goto-char (cdr result))))))
|
|
1132 result))
|
|
1133
|
|
1134 (defun default-mouse-track-drag-up-hook (event click-count)
|
|
1135 (let ((result (default-mouse-track-return-dragged-selection event)))
|
|
1136 (if result
|
|
1137 (default-mouse-track-maybe-own-selection result 'PRIMARY)))
|
|
1138 t)
|
|
1139
|
|
1140 (defun default-mouse-track-click-hook (event click-count)
|
|
1141 (default-mouse-track-drag-hook event click-count nil)
|
|
1142 (default-mouse-track-drag-up-hook event click-count)
|
|
1143 t)
|
|
1144
|
|
1145 (add-hook 'mouse-track-down-hook 'default-mouse-track-down-hook)
|
|
1146 (add-hook 'mouse-track-drag-hook 'default-mouse-track-drag-hook)
|
|
1147 (add-hook 'mouse-track-drag-up-hook 'default-mouse-track-drag-up-hook)
|
|
1148 (add-hook 'mouse-track-click-hook 'default-mouse-track-click-hook)
|
|
1149 (add-hook 'mouse-track-cleanup-hook 'default-mouse-track-cleanup-hook)
|
|
1150
|
|
1151
|
|
1152 ;;;;;;;;;;;; other mouse-track stuff (mostly associated with the
|
|
1153 ;;;;;;;;;;;; default handlers)
|
|
1154
|
|
1155 (defun mouse-track-default (event)
|
|
1156 "Invoke `mouse-track' with only the default handlers active."
|
|
1157 (interactive "e")
|
|
1158 (let ((mouse-track-down-hook 'default-mouse-track-down-hook)
|
|
1159 (mouse-track-drag-hook 'default-mouse-track-drag-hook)
|
|
1160 (mouse-track-drag-up-hook 'default-mouse-track-drag-up-hook)
|
|
1161 (mouse-track-click-hook 'default-mouse-track-click-hook)
|
|
1162 (mouse-track-cleanup-hook 'default-mouse-track-cleanup-hook))
|
|
1163 (mouse-track event)))
|
|
1164
|
|
1165 (defun mouse-track-do-rectangle (event)
|
|
1166 "Like `mouse-track' but selects rectangles instead of regions."
|
|
1167 (interactive "e")
|
|
1168 (let ((mouse-track-rectangle-p t))
|
|
1169 (mouse-track event)))
|
|
1170
|
|
1171 (defun mouse-track-adjust (event)
|
|
1172 "Extend the existing selection. This should be bound to a mouse button.
|
|
1173 The selection will be enlarged or shrunk so that the point of the mouse
|
|
1174 click is one of its endpoints. This function in fact behaves fairly
|
|
1175 similarly to `mouse-track', but begins by extending the existing selection
|
|
1176 (or creating a new selection from the previous text cursor position to
|
|
1177 the current mouse position) instead of creating a new, empty selection.
|
|
1178
|
|
1179 The mouse-track handlers are run from this command just like from
|
|
1180 `mouse-track'. Therefore, do not call this command from a mouse-track
|
|
1181 handler!"
|
|
1182 (interactive "e")
|
|
1183 (let ((default-mouse-track-adjust t))
|
|
1184 (mouse-track event)))
|
|
1185
|
|
1186 (defun mouse-track-adjust-default (event)
|
|
1187 "Extend the existing selection, using only the default handlers.
|
|
1188 This is just like `mouse-track-adjust' but will override any
|
|
1189 custom mouse-track handlers that the user may have installed."
|
|
1190 (interactive "e")
|
|
1191 (let ((default-mouse-track-adjust t))
|
|
1192 (mouse-track-default event)))
|
|
1193
|
|
1194 (defvar mouse-track-insert-selected-region nil)
|
|
1195
|
|
1196 (defun mouse-track-insert-drag-up-hook (event click-count)
|
|
1197 (setq mouse-track-insert-selected-region
|
|
1198 (default-mouse-track-return-dragged-selection event)))
|
|
1199
|
|
1200 (defun mouse-track-insert (event &optional delete)
|
|
1201 "Make a selection with the mouse and insert it at point.
|
|
1202 This is exactly the same as the `mouse-track' command on \\[mouse-track],
|
|
1203 except that point is not moved; the selected text is immediately inserted
|
|
1204 after being selected\; and the selection is immediately disowned afterwards."
|
|
1205 (interactive "*e")
|
|
1206 (setq mouse-track-insert-selected-region nil)
|
|
1207 (let ((mouse-track-drag-up-hook 'mouse-track-insert-drag-up-hook)
|
|
1208 (mouse-track-click-hook 'mouse-track-insert-click-hook)
|
|
1209 s)
|
|
1210 (save-excursion
|
|
1211 (save-window-excursion
|
|
1212 (mouse-track event)
|
|
1213 (if (consp mouse-track-insert-selected-region)
|
|
1214 (let ((pair mouse-track-insert-selected-region))
|
|
1215 (setq s (prog1
|
|
1216 (buffer-substring (car pair) (cdr pair))
|
|
1217 (if delete
|
|
1218 (kill-region (car pair) (cdr pair)))))))))
|
|
1219 (or (null s) (equal s "") (insert s))))
|
|
1220
|
|
1221 (defun mouse-track-insert-click-hook (event click-count)
|
|
1222 (default-mouse-track-drag-hook event click-count nil)
|
|
1223 (mouse-track-insert-drag-up-hook event click-count)
|
|
1224 t)
|
|
1225
|
|
1226 (defun mouse-track-delete-and-insert (event)
|
|
1227 "Make a selection with the mouse and insert it at point.
|
|
1228 This is exactly the same as the `mouse-track' command on \\[mouse-track],
|
|
1229 except that point is not moved; the selected text is immediately inserted
|
|
1230 after being selected\; and the text of the selection is deleted."
|
|
1231 (interactive "*e")
|
|
1232 (mouse-track-insert event t))
|
|
1233
|
|
1234 ;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1235
|
|
1236
|
|
1237 (defvar inhibit-help-echo nil
|
|
1238 "Inhibits display of `help-echo' extent properties in the minibuffer.")
|
|
1239 (defvar last-help-echo-object nil)
|
|
1240 (defvar help-echo-owns-message nil)
|
|
1241
|
|
1242 (defun clear-help-echo (&optional ignored-frame)
|
|
1243 (if help-echo-owns-message
|
|
1244 (progn
|
|
1245 (setq help-echo-owns-message nil
|
|
1246 last-help-echo-object nil)
|
|
1247 (clear-message 'help-echo))))
|
|
1248
|
|
1249 (defun show-help-echo (mess)
|
|
1250 ;; (clear-help-echo)
|
|
1251 (setq help-echo-owns-message t)
|
|
1252 (display-message 'help-echo mess))
|
|
1253
|
|
1254 (add-hook 'mouse-leave-frame-hook 'clear-help-echo)
|
|
1255
|
|
1256 (defun default-mouse-motion-handler (event)
|
|
1257 "For use as the value of `mouse-motion-handler'.
|
|
1258 This implements the various pointer-shape variables,
|
|
1259 as well as extent highlighting, help-echo, toolbar up/down,
|
|
1260 and `mode-motion-hook'."
|
|
1261 (let* ((frame (or (event-frame event) (selected-frame)))
|
|
1262 (window (event-window event))
|
|
1263 (buffer (event-buffer event))
|
|
1264 (point (and buffer (event-point event)))
|
|
1265 (modeline-point (and buffer (event-modeline-position event)))
|
|
1266 (extent (and point (extent-at point buffer 'mouse-face)))
|
|
1267 (glyph1 (event-glyph-extent event))
|
|
1268 (glyph (and glyph1 (extent-live-p glyph1) glyph1))
|
|
1269 (user-pointer1 (or (and glyph (extent-property glyph 'pointer))
|
|
1270 (and point
|
|
1271 (condition-case nil
|
|
1272 (extent-at point buffer 'pointer)
|
|
1273 (error nil)))
|
|
1274 (and modeline-point
|
|
1275 (condition-case nil
|
|
1276 (extent-at modeline-point
|
|
1277 (symbol-value-in-buffer
|
|
1278 'generated-modeline-string
|
|
1279 buffer) 'pointer)))))
|
|
1280 (user-pointer (and user-pointer1 (extent-live-p user-pointer1)
|
|
1281 (extent-property user-pointer1 'pointer)))
|
|
1282 (button (event-toolbar-button event))
|
|
1283 (help (or (and glyph (extent-property glyph 'help-echo) glyph)
|
|
1284 (and button (not (null (toolbar-button-help-string button)))
|
|
1285 button)
|
|
1286 (and point
|
|
1287 (condition-case nil
|
|
1288 (extent-at point buffer 'help-echo)
|
|
1289 (error nil)))
|
|
1290 (and modeline-point
|
|
1291 (condition-case nil
|
|
1292 (extent-at modeline-point
|
|
1293 (symbol-value-in-buffer
|
|
1294 'generated-modeline-string
|
|
1295 buffer) 'help-echo)))))
|
|
1296 ;; vars is a list of glyph variables to check for a pointer
|
|
1297 ;; value.
|
|
1298 (vars (cond
|
|
1299 ;; Checking if button is non-nil is not sufficent
|
|
1300 ;; since the pointer could be over a blank portion
|
|
1301 ;; of the toolbar.
|
|
1302 ((event-over-toolbar-p event)
|
|
1303 '(toolbar-pointer-glyph nontext-pointer-glyph
|
|
1304 text-pointer-glyph))
|
|
1305 ((or extent glyph)
|
|
1306 '(selection-pointer-glyph text-pointer-glyph))
|
|
1307 ((event-over-modeline-p event)
|
|
1308 '(modeline-pointer-glyph nontext-pointer-glyph
|
|
1309 text-pointer-glyph))
|
|
1310 (point '(text-pointer-glyph))
|
|
1311 (buffer '(nontext-pointer-glyph text-pointer-glyph))
|
|
1312 (t '(modeline-pointer-glyph nontext-pointer-glyph
|
|
1313 text-pointer-glyph))))
|
|
1314 pointer)
|
|
1315 (if (and user-pointer (glyphp user-pointer))
|
|
1316 (setq vars (cons 'user-pointer vars)))
|
|
1317 (while (and vars (not (pointer-image-instance-p pointer)))
|
|
1318 (setq pointer (glyph-image-instance (symbol-value (car vars))
|
|
1319 (or window frame))
|
|
1320 vars (cdr vars)))
|
|
1321
|
|
1322 (if (pointer-image-instance-p pointer)
|
|
1323 (set-frame-pointer frame pointer))
|
|
1324
|
|
1325 ;; If last-pressed-toolbar-button is not nil, then check and see
|
|
1326 ;; if we have moved to a new button and adjust the down flags
|
|
1327 ;; accordingly.
|
|
1328 (if (and (featurep 'toolbar) toolbar-active)
|
|
1329 (if (not (eq last-pressed-toolbar-button button))
|
|
1330 (progn
|
|
1331 (release-previous-toolbar-button event)
|
|
1332 (and button (press-toolbar-button event)))))
|
|
1333
|
|
1334 (cond (extent (highlight-extent extent t))
|
|
1335 (glyph (highlight-extent glyph t))
|
|
1336 (t (highlight-extent nil nil)))
|
|
1337 (cond ((extentp help)
|
|
1338 (or inhibit-help-echo
|
|
1339 (eq help last-help-echo-object) ;save some time
|
|
1340 (let ((hprop (extent-property help 'help-echo)))
|
|
1341 (setq last-help-echo-object help)
|
|
1342 (or (stringp hprop)
|
|
1343 (setq hprop (funcall hprop help)))
|
|
1344 (and hprop (show-help-echo hprop)))))
|
|
1345 ((and (featurep 'toolbar)
|
|
1346 (toolbar-button-p help)
|
|
1347 (toolbar-button-enabled-p help))
|
|
1348 (or (not toolbar-help-enabled)
|
|
1349 (eq help last-help-echo-object) ;save some time
|
|
1350 (let ((hstring (toolbar-button-help-string button)))
|
|
1351 (setq last-help-echo-object help)
|
|
1352 (or (stringp hstring)
|
|
1353 (setq hstring (funcall hstring help)))
|
|
1354 (show-help-echo hstring))))
|
|
1355 (last-help-echo-object
|
|
1356 (clear-help-echo)))
|
|
1357 (if mouse-grabbed-buffer (setq buffer mouse-grabbed-buffer))
|
|
1358 (if (and buffer (symbol-value-in-buffer 'mode-motion-hook buffer nil))
|
|
1359 (save-window-excursion
|
|
1360 (set-buffer buffer)
|
|
1361 (run-hook-with-args 'mode-motion-hook event)
|
|
1362
|
|
1363 ;; If the mode-motion-hook created a highlightable extent around
|
|
1364 ;; the mouse-point, highlight it right away. Otherwise it wouldn't
|
|
1365 ;; be highlighted until the *next* motion event came in.
|
|
1366 (if (and point
|
|
1367 (null extent)
|
|
1368 (setq extent (extent-at point
|
|
1369 (event-buffer event) ; not buffer
|
|
1370 'mouse-face)))
|
|
1371 (highlight-extent extent t)))))
|
|
1372 nil)
|
|
1373
|
|
1374 (setq mouse-motion-handler 'default-mouse-motion-handler)
|