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