Debian Codegraph @

mouse.c

raw
1 /**************************************************************************
2 * This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is *
3 * provided by Jonathan and Jovehacks without charge and without *
4 * warranty. You may copy, modify, and/or distribute JOVE, provided that *
5 * this notice is included in all the source files and documentation. *
6 **************************************************************************/
7
8 /* This file contains all functions associated with mouse buttons being
9 * pressed and any movement of the mouse (except on the Macintosh).
10 * These procedures will only be called if JOVE is run under xterm,
11 * xJove, or Jovetool.
12 */
13
14 #include "jove.h"
15
16 #ifdef MOUSE /* the body is the rest of this file */
17
18 /* #include "argcount.h" */
19 #include "commands.h" /* for cmdproc_t */
20 #include "disp.h"
21 #include "misc.h"
22 #include "ask.h"
23 #include "chars.h"
24 #include "delete.h"
25 #include "fmt.h"
26 #include "insert.h" /* for lfreelist, in MouseLine */
27 #include "marks.h"
28 #include "move.h"
29 #include "wind.h"
30 #include "term.h" /* for M_SR */
31 #include "fp.h" /* for putstr */
32 #include "jctype.h"
33 #include "mouse.h"
34 #include "xjove/mousemsg.h"
35
36 /* xterm control sequences, including those related to mousing, are described
37 * in a file called ctlseqs.ms which is part of the X Distribution.
38 *
39 * We document some xterm bugs/features here: a good place
40 * to capture the hard-won knowledge.
41 *
42 * - In Hilite mode, after a button 1 press (producing ^[[M xy), and after
43 * the program has replied with ^[f;sx;sy;fr;lrT (as required in Hilite
44 * mode), it treats the next character read as if it had been preceded by
45 * "^[[". For example, if that next character happens to be A, B, C or D
46 * you therefore get a cursor movement. See the #ifdef XTERMHLBUG below
47 * for the workaround.
48 *
49 * - mouse tracking and highlight tracking modes do not support modifiers
50 * other than CTRL, contrary to ctlseqs.ms. See the #ifdef NEVER below.
51 *
52 * - mouse hilite tracking mode (MHTM) only seems to work for button 1.
53 *
54 * - MHTM does not seem to yield mouse-button-1-up events (but if
55 * a non-empty drag occurred, that will be reported)
56 *
57 * - The documentation specifies that the ^[ [ T x y x y x y message
58 * should only be generated by MHTM when the user has dragged outside
59 * the specified bounds. It seems to also be generated:
60 * + by a multiclick (of button 1, of course)
61 * + by a drag to the left or up
62 * + when the button is released to the right of the end of a text line
63 * (but xterm's idea of this may well differ from JOVE's since JOVE
64 * optimizes screen output)
65 *
66 * - When the mouse is enabled, xterm interprets ^[[ ... T as an
67 * Initiate Hilite Mouse Tracking command. Otherwise, recent
68 * versions interpret it as Parameterized Scroll Down. Some recent
69 * termcap/terminfo xterm entries define the SR capability using
70 * this sequence. But it doesn't work all the time!
71 * It is hard to say what the best fix would be:
72 * + get X to distinguish the two T commands by the number of parameters
73 * + correctly document the limitation and remove SR from the
74 * xterm termcap and terminfo datebases
75 *
76 * In any case, the problem will be in the wild for some time
77 * so JOVE now has an ugly kludge to dodge the bug.
78 * KLUDGE: suppress M_SR (our name for SR) if in mouse mode and M_SR
79 * ends in "T".
80 *
81 * First observed and analyzed in Fedora Core 4 in 2005 September.
82 *
83 * - xterm has gained more capabilities over the years. Some misguided
84 * people of revised the termcap/terminfo entries for xterm to exploit
85 * new features. This has unfortunate consequences when the xterm
86 * is on a different system from the termcap/terminfo database:
87 * if they don't match, JOVE will potentially curdle the screen.
88 * JOVE cannot work around this problem, the user must.
89 */
90
91 jbool XtermMouse = NO; /* VAR: should we enable xterm mouse? */
92 private jbool xtMouseState = NO; /* have we enabled the mouse? */
93
94 /* sequences to enable/disable mouse hilite tracking in xterm */
95 private const char
96 xtMouseEnable[] = "\033[?1001h",
97 xtMouseDisable[] = "\033[?1001l";
98
99 private int but_state; /* button state (and more) at mouse event */
100 private int x_coord; /* mouse x-coordinate, in pixels, origin 0 */
101 private int y_coord; /* mouse y-coordinate, in characters, origin 0 */
102 private int /* xterm drag range coordinates */
103 startx, starty,
104 endx, endy;
105 private int font_width; /* width of a character in pixels */
106 private Window *oldwind;
107 private Mark *oldpos = NULL; /* Use a Mark so it is adjusted automatically */
108
109 #define LMA_NONE 0000
110 #define LMA_COPY 0001
111 #define LMA_CUT 0002
112 #define LMA_PASTE 0010
113 #define LMA_CHAR 0020
114 #define LMA_WORD 0030
115 #define LMA_LINE 0040
116 private int last_mouse_act = LMA_NONE;
117
118 private const char *saved_M_SR = NULL; /* KLUDGE for xterm/termcap bug */
119
120 void
121 MouseOn(void)
122 {
123 if (XtermMouse != xtMouseState) {
124 /* KLUDGE for xterm/termcap bug */
125 if (XtermMouse && M_SR != NULL) {
126 size_t len = strlen(M_SR);
127
128 saved_M_SR = M_SR;
129 if (len > 0 && M_SR[len-1] == 'T')
130 M_SR = NULL;
131 }
132 /* end if KLUDGE */
133 putstr(XtermMouse? xtMouseEnable : xtMouseDisable);
134 xtMouseState = XtermMouse;
135 }
136 }
137
138 void
139 MouseOff(void)
140 {
141 if (xtMouseState) {
142 putstr(xtMouseDisable);
143 xtMouseState = NO;
144 M_SR = saved_M_SR; /* KLUDGE for xterm/termcap bug */
145 }
146 }
147
148 /* Set cursor position to that of mouse pointer. */
149
150 private void
151 SetCursor(void)
152 {
153 int line_pos = in_window(curwind, curline);
154 int offset = PhysScreen[y_coord].s_offset;
155 int num_moves;
156
157 if (line_pos < 0) {
158 SetLine(curwind->w_top);
159 line_pos = in_window(curwind, curline);
160 }
161 num_moves = y_coord - line_pos;
162 if (num_moves > 0)
163 line_move(FORWARD, num_moves, NO);
164 if (num_moves < 0)
165 line_move(BACKWARD, -num_moves, NO);
166
167 curchar = how_far(curline,
168 (x_coord + font_width/2) / font_width + offset
169 - (W_NUMWIDTH(curwind) + SIWIDTH(offset)));
170 }
171
172 private void
173 ScrollToMouse(void)
174 {
175 int lc;
176 const int
177 width = (CO - 1 - (4 * SG)) * font_width;
178 /* `width' must match size in ModeLine */
179 int top;
180
181 /* This code ought to match the scroll-bar layout computed by
182 * WindowRange().
183 * - a click in the first column ought to force top-of-file
184 * - a click in the last column ought to force end-of-file
185 * - don't waste space by displaying less than a screenful at end-of-file.
186 * - After clicking at a given point in the scroll bar, the cursor
187 * should be found to be exactly in the middle of the highlighted
188 * region (except near the ends, of course).
189 */
190 lc = LinesTo(curbuf->b_first, (LinePtr)NULL);
191 top = 1 + (long)(x_coord-font_width) * (lc-2) / (width-2*font_width)
192 - WSIZE(curwind)/2;
193 if (top > lc - WSIZE(curwind))
194 top = lc - WSIZE(curwind);
195 if (top < 0)
196 top = 0;
197
198 SetTop(curwind, next_line(curbuf->b_first, top));
199 if (in_window(curwind, curline) == -1)
200 SetLine(next_line(curwind->w_top, WSIZE(curwind)/2));
201 }
202
203 private jbool
204 ObeyProc(cmdproc_t p)
205 {
206 if (BufMinorMode(curbuf, BReadOnly)) {
207 rbell();
208 message("[Buffer is read-only]");
209 return NO;
210 } else {
211 (*p)();
212 return YES;
213 }
214 }
215
216 /* Get next value in number sequence */
217
218 private int
219 NextValue(void)
220 {
221 int val;
222
223 (void) waitchar();
224 Digit();
225 val = arg_value();
226 clr_arg_value();
227 return val;
228 }
229
230 /* Select appropriate window */
231
232 private int
233 SelectWind(Window *winforce) /* if non-null, must be within this window */
234 {
235 Window *wp = fwind;
236 int total_lines = wp->w_height;
237
238 /* Find which window mouse pointer is in. */
239 while (y_coord >= total_lines) {
240 wp = wp->w_next;
241 if (wp == fwind)
242 return -1;
243
244 total_lines += wp->w_height;
245 }
246 if (winforce != NULL && wp != winforce)
247 return -1;
248
249 SetWind(wp); /* Set current window */
250 return (total_lines - y_coord - 1); /* Cursor pos within window */
251 }
252
253 /* get an origin-0 coordinate in funny representation used by xterm */
254
255 private int
256 xtGetCoord(int upb)
257 {
258 ZXchar c = waitchar(); /* coordinate */
259
260 /* undo MetaKey if we think it was done */
261 if (c == ESC && MetaKey)
262 c = waitchar() | METABIT;
263
264 /* It appears that mouse events near the extreme right hand edge of
265 * a window can give coordinates as large as LI. Perhaps this is
266 * true for CO too. So the range is inclusive.
267 */
268 return '!' <= c && c - '!' <= upb? c - '!' : -1;
269 }
270
271 /* get some X Y pair from xterm; return indication of success */
272
273 private jbool
274 xtGetXY(int *xp, int *yp)
275 {
276 int x = xtGetCoord(CO);
277
278 if (x != -1) {
279 int y = xtGetCoord(LI);
280
281 if (y != -1) {
282 *xp = x;
283 *yp = y;
284 font_width = 1;
285 return YES;
286 }
287 }
288 return NO;
289 }
290
291 #define MPROTO_XTERM 0
292 #define MPROTO_XTDRAG 1
293 #define MPROTO_JOVETOOL 2
294
295 #define XtermProto(p) ((p) <= MPROTO_XTDRAG)
296
297 /* Format of command to xterm to start or stop mouse hilite tracking:
298 * ^[ [ func ; startx ; starty ; firstrow ; lastrow T
299 *
300 * [1997 September] All xterm's up until now have a bug whereby
301 * the character sent after this sequence is interpreted as if
302 * it were preceded by an ESC. We pad with an "x" character to
303 * be ignored.
304 *
305 * [1998 Sept 21] XFree86 3.2's xterm fixes the original bug
306 * so we have to refine our work-around: make sure that the
307 * character to be ignored has no semantics (DEL).
308 *
309 * [2004 July 11] XFree86's xterm no longer ignores DEL!
310 * This is an xterm bug. A fix to xterm has been accepted
311 * by XFree86 and been submitted to x.org. Even so,
312 * this bug will be in the field for a long time, so we
313 * need to live with it.
314 * The only character that seems to be reliably ignored
315 * in XFree86-4.0.1's xterm is NUL.
316 *
317 * This fudge is intended to be harmless on xterms
318 * with or without these bugs.
319 */
320 #define XTERMHLBUG 1 /* always enable: we think that it is safe */
321
322 static void
323 hl_mode(int hl_setting, int startx, int starty, int endx, int endy)
324 {
325 static const char hl_fmt[] = "\033[%d;%d;%d;%d;%dT";
326 char buf[sizeof(hl_fmt) + 4*(5-2)];
327
328 swritef(buf, sizeof(buf), hl_fmt, hl_setting, startx, starty, endx, endy);
329 putstr(buf);
330 #ifdef XTERMHLBUG
331 scr_putchar('\0');
332 #endif
333 }
334
335 private jbool
336 MouseParams(int mproto)
337 {
338 /* The mouse commands will be invoked by hitting a mouse
339 * button but can also be invoked by typing the escape sequence
340 * CTRL-Xm so the following validation checks are necessary.
341 */
342
343 static int wind_pos; /* reverse y-coordinate within window */
344 static jbool mode_mode = NO; /* true while doing modeline */
345
346 /* up_expected is an extended jbool: it can be YES, NO, and -1!
347 * -1 signifies that we are in xterm mouse hilite tracking mode
348 * which appears to fail to yield an up event if it deems the
349 * event uninteresting (i.e. no motion).
350 */
351 static int up_expected = NO; /* true while button held down */
352 static int estartx, estarty; /* from last enable */
353
354 jbool input_good = NO;
355
356 /* This switch reads and decodes the control sequence.
357 * - input_good is set to YES if the sequence looks valid.
358 * - but_state, x_coord, and y_coord are changed to reflect the new state
359 * - if the event could follow an "initiate hilite tracking"
360 * and up_expected is -1, the decoder should adjust up_expected.
361 */
362 switch (mproto) {
363 case MPROTO_XTERM:
364 {
365 ZXchar cb = LastKeyStruck;
366
367 switch (cb) {
368 default:
369 if (' ' <= cb && cb < ' '+040 && xtGetXY(&x_coord, &y_coord)) {
370 /* ^[ [ M buttoninfo mousex mousey
371 * Button event with coordinates.
372 * On a release event we aren't told which button
373 * is being released.
374 */
375 cb -= ' ';
376 if ((cb & 03) == 03) {
377 /* guess that released button is last depressed */
378 but_state = (but_state & JT_BUTMASK) | JT_UPEVENT;
379 /* We are welcome in xterm mouse hilite tracking mode */
380 if (up_expected == -1)
381 up_expected = YES;
382 } else {
383 static const int butcode[] = { JT_LEFT, JT_MIDDLE, JT_RIGHT };
384
385 but_state = butcode[cb & 03] | JT_DOWNEVENT;
386 /* We are welcome in xterm mouse hilite tracking mode */
387 if (up_expected == -1)
388 up_expected = NO;
389 }
390 #ifdef NEVER /* surprise: xterm won't generate these modifiers! */
391 if (cb & 04)
392 but_state |= JT_SHIFT;
393 if (cb & 010)
394 but_state |= JT_META;
395 #endif
396 if (cb & 020)
397 but_state |= JT_CONTROL;
398 input_good = YES;
399 }
400 break;
401 }
402 }
403 break;
404
405 case MPROTO_XTDRAG:
406 /* handle xterm hilite tracking drag event: ^[ [ t or ^[ [ T
407 * These are only generated for left-button acts.
408 * Each is some drag-like event.
409 */
410 switch (LastKeyStruck) {
411 case 'T':
412 /* ^[ [ T startx starty endx endy mousex mousey
413 * returned on Left Button release beyond end-of-line,
414 * or (undocumented) a drag to left or up, or
415 * (undocumented) for a multi-click select. These are
416 * transformed into three kinds of event, distinguished
417 * by JT_CLICK2 and JT_CLICK3. The JT_CLICKn are used to
418 * indicate that it was a multi-click select (by
419 * eliminating the other cases). Note that a double click
420 * beyond end-of-line (or on an empty line) is not reported
421 * even though a third such click is. Oh the joys of
422 * relying on undocumented features!
423 */
424 if (xtGetXY(&startx, &starty)
425 && xtGetXY(&endx, &endy)
426 && xtGetXY(&x_coord, &y_coord))
427 {
428 input_good = YES;
429 if ((( endy != estarty || endx != estartx
430 || y_coord > estarty
431 || (y_coord == estarty && x_coord >= estartx)
432 ) /* it isn't a drag left or up */
433 && ( endx != 0 || startx == endx
434 ) /* it isn't a drag across an e-o-l */
435 )
436 || (but_state & JT_CLICKMASK) == JT_CLICK2)
437 /* it is clearly a third click */
438 {
439 /* a complex selection */
440 if ((but_state & JT_CLICKMASK) == JT_CLICK2 || endx == 0)
441 /* this is a third click:
442 * endx == 0 implies that we
443 * missed the second one
444 */
445 but_state = JT_LEFT | JT_DRAGEVENT | JT_CLICK3;
446 else
447 but_state = JT_LEFT | JT_DRAGEVENT | JT_CLICK2;
448 } else {
449 /* A simple drag.
450 * Note: code is the same as for case 't' below.
451 */
452 but_state = JT_LEFT | JT_DRAGEVENT;
453 }
454 }
455 break;
456
457 case 't':
458 /* ^[ [ t mousex mousey
459 * returned on Left Button release within valid text.
460 * A simple drag.
461 * Note: code is duplicated above as part of case 'T'.
462 */
463 if (xtGetXY(&x_coord, &y_coord)) {
464 but_state = JT_LEFT | JT_DRAGEVENT;
465 input_good = YES;
466 }
467 break;
468 }
469 /* We are welcome in xterm mouse hilite tracking mode */
470 if (input_good && up_expected == -1)
471 up_expected = YES;
472 break;
473
474 case MPROTO_JOVETOOL:
475 if (waitchar() == '(') {
476 but_state = NextValue(); /* Read in parameters */
477 switch (waitchar()) {
478 case ' ':
479 {
480 int x = NextValue();
481
482 if (waitchar() == ' ') {
483 int y = NextValue();
484
485 if (waitchar() == ' ') {
486 font_width = NextValue();
487 if (waitchar() == ')'
488 && waitchar() == '\r')
489 {
490 input_good = YES;
491 x_coord = x;
492 y_coord = y;
493 }
494 }
495 }
496 }
497 break;
498 case ')':
499 input_good = (waitchar() == '\r');
500 break;
501 }
502 }
503 break;
504 }
505 this_cmd = OTHER_CMD; /* no longer gathering args */
506
507 if (!input_good) {
508 if (XtermProto(mproto) && but_state == (JT_LEFT | JT_DOWNEVENT)) {
509 /* abort Hilite mode to prevent hangups */
510 hl_mode(0,1,1,1,1);
511 }
512 complain("[mouse input of wrong format]");
513 /* NOTREACHED */
514 }
515
516 /* Note: at this point, up_expected still reflects previous state.
517 * Unfortunately, xterm seems to elide some button-up events!
518 * For this reason, some cases above adjust up_expected to
519 * prevent the following sanity check from going off.
520 */
521
522 /* check: button_(was)_held iff currently event is UP or DRAG */
523 if (up_expected != ((but_state & JT_EVENTMASK)!=JT_DOWNEVENT)
524 || (up_expected == YES && last_cmd != MOUSE_CMD)) {
525 if (XtermProto(mproto) && but_state == (JT_LEFT | JT_DOWNEVENT)) {
526 /* abort Hilite mode to prevent hangups */
527 hl_mode(0,1,1,1,1);
528 }
529 up_expected = NO; /* resynch in neutral */
530 complain("[Mouse events out of order]");
531 /* NOTREACHED */
532 }
533
534 /* update up_expected to reflect new state */
535 up_expected = XtermProto(mproto) && (but_state&JT_EVENTMASK) == JT_DRAGEVENT
536 ? -1 : (but_state & JT_EVENTMASK) != JT_UPEVENT;
537 if (up_expected != NO)
538 this_cmd = MOUSE_CMD;
539
540 if ((but_state & JT_EVENTMASK) == JT_DOWNEVENT
541 && (but_state & JT_PASTEMASK) && (but_state & JT_CSMASK) == 0)
542 {
543 /* Hugh: ??? is this leak possible? -- checking code added as a probe. */
544 /* CHL believes assert oldpos==NULL */
545 if (oldpos != NULL) {
546 complain("[internal error: mark leak from MouseParams]");
547 /* NOTREACHED */
548 }
549 oldpos = MakeMark(curline, curchar);
550 }
551
552 /* UP events in xterm should do nothing in foreign windows */
553 if (XtermProto(mproto)
554 && (but_state & JT_EVENTMASK) == JT_UPEVENT
555 && curwind != oldwind
556 && !mode_mode)
557 return NO;
558
559 if ((but_state & (JT_CLICKMASK | JT_EVENTMASK)) == JT_DOWNEVENT) {
560 /* button down, and first click at that:
561 * - ok to changing window
562 * - ok to move in or out of modeline (scroll bar)
563 */
564 oldwind = curwind;
565 wind_pos = SelectWind((Window *) NULL);
566 mode_mode = (wind_pos == 0);
567 if (XtermProto(mproto) && but_state == (JT_LEFT | JT_DOWNEVENT)) {
568 /* Initiate or abort mouse hilite tracking. We use hilite
569 * tracking if we are not on the modeline.
570 * When hilite tracking is used, apparently the up event is
571 * elided if it would report no change in location.
572 */
573 jbool use_hilite = !mode_mode;
574
575 if (use_hilite)
576 up_expected = -1; /* half-expect an up */
577 hl_mode(use_hilite,
578 (estartx = x_coord)+1,
579 (estarty = y_coord)+1,
580 y_coord + wind_pos - curwind->w_height + 2,
581 y_coord + wind_pos + 1);
582 }
583 } else if (!mode_mode) {
584 /* can only stay within the window, and cannot switch into modeline */
585 wind_pos = SelectWind(oldwind);
586 }
587
588 if (mode_mode) {
589 /* An event in mode_mode only scrolls or resizes, with no
590 * action (unless it were a first down event, when it might
591 * have caused a new window to be selected). Shifts and
592 * multiclicks indicate a confused user.
593 */
594 if ((but_state & JT_BUTMASK) == JT_LEFT
595 && (but_state & JT_EVENTMASK) != JT_UPEVENT)
596 {
597 ScrollToMouse();
598 if (but_state & (JT_CSMASK | JT_CLICKMASK)) {
599 complain("[You are just scrolling a window]");
600 /* NOTREACHED */
601 }
602 } else if ((but_state & JT_BUTMASK) == JT_MIDDLE
603 && (but_state & (JT_UPEVENT | JT_DRAGEVENT)) != 0)
604 {
605 oldwind = curwind;
606 if ((wind_pos = SelectWind((Window *) NULL)) < 0)
607 return NO;
608
609 if (curwind == oldwind->w_next) {
610 wind_pos -= curwind->w_height;
611 SetWind(oldwind);
612 }
613 if (curwind == oldwind && curwind->w_next != fwind)
614 WindSize(curwind->w_next, wind_pos);
615 if (but_state & (JT_CSMASK | JT_CLICKMASK)) {
616 complain("[You are just resizing a window]");
617 /* NOTREACHED */
618 }
619 }
620 } else if ((but_state & JT_PASTEMASK) && (but_state & JT_CSMASK) == 0) {
621 /* With JT_PASTE/CUT, window switching is allowed. */
622 return YES;
623 } else if (curwind != oldwind) {
624 /* Clicking within a different window (as opposed to its
625 * mode line) only switches to that window, with no action.
626 * Other shifts and multiclicks indicate a confused user.
627 */
628 if (but_state & (JT_CSMASK | JT_CLICKMASK)) {
629 complain("[You were just changing windows]");
630 /* NOTREACHED */
631 }
632 } else if (wind_pos <= 0) {
633 /* ignore out-of-window events */
634 } else {
635 /* We've run the gauntlet. Give the OK for action. */
636 return YES;
637 }
638 last_mouse_act = LMA_NONE;
639 if (oldpos != NULL) {
640 SetWind(oldwind);
641 DelMark(oldpos);
642 oldpos = NULL;
643 }
644 return NO;
645 }
646
647 private void
648 MousePoint(int mproto)
649 {
650 last_mouse_act = LMA_NONE;
651 if (MouseParams(mproto))
652 SetCursor();
653 }
654
655 void
656 xjMousePoint(void)
657 {
658 MousePoint(MPROTO_JOVETOOL);
659 }
660
661 void
662 xtMousePoint(void)
663 {
664 MousePoint(MPROTO_XTERM);
665 }
666
667 private void
668 MouseMark(int mproto)
669 {
670 last_mouse_act = LMA_NONE;
671 if (MouseParams(mproto)) {
672 SetCursor();
673 set_mark();
674 }
675 }
676
677 void
678 xjMouseMark(void)
679 {
680 MouseMark(MPROTO_JOVETOOL);
681 }
682
683 void
684 xtMouseMark(void)
685 {
686 MouseMark(MPROTO_XTERM);
687 }
688
689 /* xtMouseYank is a reduced xtMousePointYank.
690 * Although xtMousePointYank should be more useful,
691 * xtMouseYank is more like XTerm's native behavior.
692 */
693 void
694 xtMouseYank(void)
695 {
696 last_mouse_act = LMA_NONE;
697 if (MouseParams(MPROTO_XTERM)) {
698 ObeyProc(Yank);
699 this_cmd = MOUSE_CMD;
700 }
701 }
702
703 void
704 xtMousePointYank(void)
705 {
706 last_mouse_act = LMA_NONE;
707 if (MouseParams(MPROTO_XTERM)) {
708 SetCursor();
709 ObeyProc(Yank);
710 this_cmd = MOUSE_CMD;
711 }
712 }
713
714 void
715 xtMouseCutPointYank(void)
716 {
717 Mark *m;
718
719 last_mouse_act = LMA_NONE;
720 if (MouseParams(MPROTO_XTERM) && ObeyProc(set_mark)) {
721 SetCursor();
722 set_mark();
723 m = curmark;
724 PopMark(); /* pop our 2nd set_mark */
725 PopMark(); /* pop our 1st set_mark */
726 DelReg(); /* because we are (usually) about to
727 yank the same region again */
728 ToMark(m);
729 Yank();
730 this_cmd = MOUSE_CMD;
731 }
732 }
733
734 void
735 xtMouseNull(void)
736 {
737 MouseParams(MPROTO_XTERM);
738 }
739
740 /* Double clicking selects either an identifier (according to the current
741 * major mode) or, if the current char is not part of an identifier, it
742 * selects the gap between two identifiers. However, the selection is always
743 * within one line. startMouseWord and endMouseWord are called to locate the
744 * two ends of the selected (un)identifier. Note that the selection can be
745 * empty.
746 */
747 private void
748 startMouseWord(void)
749 {
750 jbool in_id;
751
752 if (eolp() && !bolp())
753 curchar -= 1;
754 in_id = jisident(linebuf[curchar]);
755 while (!bolp() && jisident(linebuf[curchar-1]) == in_id)
756 curchar -= 1;
757 }
758
759 private void
760 endMouseWord(void)
761 {
762 jbool in_id;
763
764 if (eolp() && !bolp())
765 curchar -= 1;
766 in_id = jisident(linebuf[curchar]);
767 while (!eolp() && jisident(linebuf[curchar]) == in_id)
768 curchar += 1;
769 }
770
771 private void
772 doMouseWord(void)
773 {
774 startMouseWord();
775 set_mark();
776 endMouseWord();
777 }
778
779 private void
780 doMouseLine(void)
781 {
782 Bol();
783 set_mark();
784 line_move(FORWARD, 1, NO);
785 }
786
787 /*
788 * This command extends the region, at either end, in units of chars, words,
789 * or lines depending on last_mouse_act (i.e. on whether the region was
790 * selected by dragging, single or double clicking). If used to shrink the
791 * region, it is the point end (not the mark end) that gets moved.
792 */
793 private jbool
794 doMouseExtend(void)
795 {
796 jbool region_forward, new_forward;
797
798 if (last_mouse_act == LMA_NONE)
799 return NO; /* treat as xtMouseNull */
800
801 region_forward = !inorder(curline, curchar, curmark->m_line, curmark->m_char);
802 ExchPtMark(); /* for better effect when shrinking region */
803 set_mark();
804 SetCursor();
805 new_forward = !inorder(curline, curchar, curmark->m_line, curmark->m_char);
806
807 switch (last_mouse_act) {
808 case LMA_CHAR:
809 if (region_forward != new_forward) {
810 PopMark();
811 SetCursor();
812 }
813 break;
814 case LMA_WORD:
815 if (region_forward != new_forward) {
816 PopMark();
817 SetCursor();
818 }
819 if (new_forward)
820 endMouseWord();
821 else
822 startMouseWord();
823 break;
824 case LMA_LINE:
825 if (region_forward != new_forward) {
826 PopMark();
827 SetCursor();
828 }
829 Bol();
830 if (new_forward)
831 line_move(FORWARD, 1, NO);
832 break;
833 }
834 return !(curmark->m_line==curline && curmark->m_char==curchar);
835 }
836
837 /* This command is intended to be bound to ^[ [ t and ^[ [ T, which
838 * are the button UP events for the left button in Hilite mode.
839 * It is not clear whether or not these codes should be produced
840 * after a simple click, so the following code plays safe.
841 *
842 * Should a multiclick select what XTerm shows ("visual fidelity"),
843 * thus using XTerm's definition of "word", or should it select based
844 * on JOVE's version of "word"? To select the latter, define XTJOVEWORD.
845 *
846 * This choice also affects how JOVE decides at which end of the selection
847 * "point" should be placed.
848 *
849 * - When JOVE uses its own definition of word or line (they are of a piece)
850 * it selects the current unit, and then, if necessary, extends to envelop
851 * the current mouse position. If no extension is needed the point will be
852 * at the right. If extension is needed (usually because a drag was used)
853 * point will be at the far end of the extension. This sounds complicated,
854 * but feels quite nice.
855 *
856 * - When JOVE uses XTerm's selection range, it places point at the end
857 * closest to the final mouse position. This sounds simple, but it may
858 * be slightly surprising when no drag was done.
859 */
860 void
861 xtMouseMarkDragPointCopy(void)
862 {
863 if (MouseParams(MPROTO_XTDRAG)) {
864 /* assert((but_state & JT_EVENTMASK) == JT_DRAGEVENT) */
865 if ((but_state & JT_CLICKMASK) == 0) {
866 /* simple (character-based) drag (from point to mouse) */
867 set_mark();
868 SetCursor();
869 CopyRegion();
870 last_mouse_act = LMA_CHAR;
871 #ifdef XTJOVEWORD
872 } else if ((but_state & JT_CLICKMASK) == JT_CLICK2) {
873 /* select based on JOVE's notion of words */
874 doMouseWord();
875 last_mouse_act = LMA_WORD;
876 if (doMouseExtend())
877 CopyRegion();
878
879 } else /* ((but_state & JT_CLICKMASK) == JT_CLICK3) */ {
880 /* select lines */
881 if (last_mouse_act == LMA_WORD) {
882 /* Second click was not missed.
883 * Avoid nasty mark and kill buildup.
884 */
885 PopMark();
886 DelKillRing();
887 }
888 doMouseLine();
889 last_mouse_act = LMA_LINE;
890 if (doMouseExtend())
891 CopyRegion();
892 }
893 #else
894 } else {
895 /* select based on xterm's selection: startxy through endxy
896 * Set point to whichever is closer to mouse position
897 * and set mark to farther.
898 */
899 int which_end; /* distance difference: negative if closer to start */
900
901 if (last_mouse_act != LMA_NONE) {
902 /* avoid nasty mark and kill buildup */
903 PopMark();
904 DelKillRing();
905 }
906 which_end = (y_coord - starty) - (endy - y_coord);
907 if (which_end == 0)
908 which_end = (x_coord - startx) - (endx - x_coord);
909 x_coord = startx;
910 y_coord = starty;
911 SetCursor();
912 set_mark();
913 x_coord = endx;
914 y_coord = endy;
915 SetCursor();
916 if (which_end < 0)
917 ExchPtMark();
918 CopyRegion();
919 last_mouse_act = last_mouse_act == LMA_WORD? LMA_LINE : LMA_WORD;
920 }
921 #endif
922 }
923 }
924
925 /* Extend the region to the pointed-at character, word or line
926 * (depending on last_mouse_act). Only valid after an event which
927 * selects and copies a region (e.g. xtMouseMarkDragPointCop).
928 */
929 void
930 xtMouseExtend(void)
931 {
932 if (MouseParams(MPROTO_XTERM)) {
933 if (doMouseExtend()) {
934 DelKillRing();
935 CopyRegion();
936 }
937 }
938 }
939
940 /* undo effect of preceding MouseCopyCut, if any */
941 private void
942 MouseUndo(void)
943 {
944 if (last_mouse_act & LMA_PASTE) {
945 ObeyProc(DelReg); /* at old curwind/line/char */
946 this_cmd = MOUSE_CMD;
947 PopMark();
948 }
949 SelectWind((Window *)NULL);
950 switch (last_mouse_act & ~LMA_PASTE) {
951 case LMA_CUT:
952 ToMark(CurMark());
953 Yank();
954 this_cmd = MOUSE_CMD;
955 PopMark();
956 /*FALLTHROUGH*/
957 case LMA_COPY:
958 /* remove the entry we previously put in the kill-ring */
959 ToMark(CurMark());
960 DelKillRing();
961 }
962 PopMark();
963 last_mouse_act = LMA_NONE;
964 }
965
966 void
967 xjMouseWord(void)
968 {
969 if (MouseParams(MPROTO_JOVETOOL)) {
970 MouseUndo();
971 doMouseWord();
972 }
973 }
974
975 void
976 xjMouseLine(void)
977 {
978 if (MouseParams(MPROTO_JOVETOOL)) {
979 MouseUndo();
980 SetCursor();
981 doMouseLine();
982 }
983 }
984
985 void
986 xjMouseYank(void)
987 {
988 last_mouse_act = LMA_NONE;
989 if (MouseParams(MPROTO_JOVETOOL)) {
990 SetCursor();
991 if (but_state & JT_CONTROL)
992 ObeyProc(Yank);
993 }
994 }
995
996 void
997 xjMouseCopyCut(void)
998 {
999 Mark *mp = curmark;
1000
1001 if (MouseParams(MPROTO_JOVETOOL)) {
1002 if (mp!=NULL
1003 && (mp->m_line != curline || mp->m_char != curchar))
1004 {
1005 switch(but_state & (JT_CSMASK | JT_PASTEMASK)) {
1006 case JT_CONTROL:
1007 CopyRegion();
1008 last_mouse_act = LMA_COPY;
1009 break;
1010 case JT_CONTROL | JT_SHIFT:
1011 last_mouse_act = ObeyProc(DelReg)? LMA_CUT : LMA_NONE;
1012 break;
1013 case JT_PASTE:
1014 CopyRegion();
1015 SetWind(oldwind);
1016 ToMark(oldpos);
1017 ObeyProc(Yank);
1018 DelMark(oldpos);
1019 oldpos = NULL;
1020 last_mouse_act = LMA_COPY | LMA_PASTE;
1021 break;
1022 case JT_CUT:
1023 ObeyProc(DelReg);
1024 SetWind(oldwind);
1025 ToMark(oldpos);
1026 ObeyProc(Yank);
1027 DelMark(oldpos);
1028 oldpos = NULL;
1029 last_mouse_act = LMA_CUT | LMA_PASTE;
1030 break;
1031 /* default: ignore */
1032 }
1033 } else if (but_state & JT_PASTEMASK) {
1034 SetWind(oldwind);
1035 ToMark(oldpos);
1036 DelMark(oldpos);
1037 oldpos = NULL;
1038 }
1039 }
1040 }
1041
1042 #endif /* MOUSE */