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