blob: 1337807b6485339bb33b7f03ed28840265095397 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ui.c: functions that handle the user interface.
12 * 1. Keyboard input stuff, and a bit of windowing stuff. These are called
13 * before the machine specific stuff (mch_*) so that we can call the GUI
14 * stuff instead if the GUI is running.
15 * 2. Clipboard stuff.
16 * 3. Input buffer stuff.
17 */
18
19#include "vim.h"
20
21 void
22ui_write(s, len)
23 char_u *s;
24 int len;
25{
26#ifdef FEAT_GUI
27 if (gui.in_use && !gui.dying && !gui.starting)
28 {
29 gui_write(s, len);
30 if (p_wd)
31 gui_wait_for_chars(p_wd);
32 return;
33 }
34#endif
35#ifndef NO_CONSOLE
36 /* Don't output anything in silent mode ("ex -s") unless 'verbose' set */
37 if (!(silent_mode && p_verbose == 0))
38 {
39#ifdef FEAT_MBYTE
40 char_u *tofree = NULL;
41
42 if (output_conv.vc_type != CONV_NONE)
43 {
44 /* Convert characters from 'encoding' to 'termencoding'. */
45 tofree = string_convert(&output_conv, s, &len);
46 if (tofree != NULL)
47 s = tofree;
48 }
49#endif
50
51 mch_write(s, len);
52
53#ifdef FEAT_MBYTE
54 if (output_conv.vc_type != CONV_NONE)
55 vim_free(tofree);
56#endif
57 }
58#endif
59}
60
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000061#if defined(UNIX) || defined(VMS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000062/*
63 * When executing an external program, there may be some typed characters that
64 * are not consumed by it. Give them back to ui_inchar() and they are stored
65 * here for the next call.
66 */
67static char_u *ta_str = NULL;
68static int ta_off; /* offset for next char to use when ta_str != NULL */
69static int ta_len; /* length of ta_str when it's not NULL*/
70
71 void
72ui_inchar_undo(s, len)
73 char_u *s;
74 int len;
75{
76 char_u *new;
77 int newlen;
78
79 newlen = len;
80 if (ta_str != NULL)
81 newlen += ta_len - ta_off;
82 new = alloc(newlen);
83 if (new != NULL)
84 {
85 if (ta_str != NULL)
86 {
87 mch_memmove(new, ta_str + ta_off, (size_t)(ta_len - ta_off));
88 mch_memmove(new + ta_len - ta_off, s, (size_t)len);
89 vim_free(ta_str);
90 }
91 else
92 mch_memmove(new, s, (size_t)len);
93 ta_str = new;
94 ta_len = newlen;
95 ta_off = 0;
96 }
97}
98#endif
99
100/*
101 * ui_inchar(): low level input funcion.
102 * Get characters from the keyboard.
103 * Return the number of characters that are available.
104 * If "wtime" == 0 do not wait for characters.
105 * If "wtime" == -1 wait forever for characters.
106 * If "wtime" > 0 wait "wtime" milliseconds for a character.
107 *
108 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
109 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
110 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is NULL
111 * otherwise.
112 */
113 int
114ui_inchar(buf, maxlen, wtime, tb_change_cnt)
115 char_u *buf;
116 int maxlen;
117 long wtime; /* don't use "time", MIPS cannot handle it */
118 int tb_change_cnt;
119{
120 int retval = 0;
121
122#if defined(FEAT_GUI) && (defined(UNIX) || defined(VMS))
123 /*
124 * Use the typeahead if there is any.
125 */
126 if (ta_str != NULL)
127 {
128 if (maxlen >= ta_len - ta_off)
129 {
130 mch_memmove(buf, ta_str + ta_off, (size_t)ta_len);
131 vim_free(ta_str);
132 ta_str = NULL;
133 return ta_len;
134 }
135 mch_memmove(buf, ta_str + ta_off, (size_t)maxlen);
136 ta_off += maxlen;
137 return maxlen;
138 }
139#endif
140
Bram Moolenaar05159a02005-02-26 23:04:13 +0000141#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000142 if (do_profiling == PROF_YES && wtime != 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000143 prof_inchar_enter();
144#endif
145
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146#ifdef NO_CONSOLE_INPUT
147 /* Don't wait for character input when the window hasn't been opened yet.
148 * Do try reading, this works when redirecting stdin from a file.
149 * Must return something, otherwise we'll loop forever. If we run into
150 * this very often we probably got stuck, exit Vim. */
151 if (no_console_input())
152 {
153 static int count = 0;
154
155# ifndef NO_CONSOLE
Bram Moolenaar43b604c2005-03-22 23:06:55 +0000156 retval = mch_inchar(buf, maxlen, (wtime >= 0 && wtime < 10)
157 ? 10L : wtime, tb_change_cnt);
158 if (retval > 0 || typebuf_changed(tb_change_cnt) || wtime >= 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000159 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160# endif
161 if (wtime == -1 && ++count == 1000)
162 read_error_exit();
163 buf[0] = CAR;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000164 retval = 1;
165 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 }
167#endif
168
Bram Moolenaarc8da3112007-03-08 12:36:46 +0000169 /* If we are going to wait for some time or block... */
170 if (wtime == -1 || wtime > 100L)
171 {
172 /* ... allow signals to kill us. */
173 (void)vim_handle_signal(SIGNAL_UNBLOCK);
174
175 /* ... there is no need for CTRL-C to interrupt something, don't let
176 * it set got_int when it was mapped. */
177 if (mapped_ctrl_c)
178 ctrl_c_interrupts = FALSE;
179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180
181#ifdef FEAT_GUI
182 if (gui.in_use)
183 {
184 if (gui_wait_for_chars(wtime) && !typebuf_changed(tb_change_cnt))
185 retval = read_from_input_buf(buf, (long)maxlen);
186 }
187#endif
188#ifndef NO_CONSOLE
189# ifdef FEAT_GUI
190 else
191# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000192 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 retval = mch_inchar(buf, maxlen, wtime, tb_change_cnt);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195#endif
196
Bram Moolenaarc8da3112007-03-08 12:36:46 +0000197 if (wtime == -1 || wtime > 100L)
198 /* block SIGHUP et al. */
199 (void)vim_handle_signal(SIGNAL_BLOCK);
200
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 ctrl_c_interrupts = TRUE;
202
Bram Moolenaar05159a02005-02-26 23:04:13 +0000203#ifdef NO_CONSOLE_INPUT
204theend:
205#endif
206#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000207 if (do_profiling == PROF_YES && wtime != 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000208 prof_inchar_exit();
209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 return retval;
211}
212
213/*
214 * return non-zero if a character is available
215 */
216 int
217ui_char_avail()
218{
219#ifdef FEAT_GUI
220 if (gui.in_use)
221 {
222 gui_mch_update();
223 return input_available();
224 }
225#endif
226#ifndef NO_CONSOLE
227# ifdef NO_CONSOLE_INPUT
228 if (no_console_input())
229 return 0;
230# endif
231 return mch_char_avail();
232#else
233 return 0;
234#endif
235}
236
237/*
238 * Delay for the given number of milliseconds. If ignoreinput is FALSE then we
239 * cancel the delay if a key is hit.
240 */
241 void
242ui_delay(msec, ignoreinput)
243 long msec;
244 int ignoreinput;
245{
246#ifdef FEAT_GUI
247 if (gui.in_use && !ignoreinput)
248 gui_wait_for_chars(msec);
249 else
250#endif
251 mch_delay(msec, ignoreinput);
252}
253
254/*
255 * If the machine has job control, use it to suspend the program,
256 * otherwise fake it by starting a new shell.
257 * When running the GUI iconify the window.
258 */
259 void
260ui_suspend()
261{
262#ifdef FEAT_GUI
263 if (gui.in_use)
264 {
265 gui_mch_iconify();
266 return;
267 }
268#endif
269 mch_suspend();
270}
271
272#if !defined(UNIX) || !defined(SIGTSTP) || defined(PROTO) || defined(__BEOS__)
273/*
274 * When the OS can't really suspend, call this function to start a shell.
275 * This is never called in the GUI.
276 */
277 void
278suspend_shell()
279{
280 if (*p_sh == NUL)
281 EMSG(_(e_shellempty));
282 else
283 {
284 MSG_PUTS(_("new shell started\n"));
285 do_shell(NULL, 0);
286 }
287}
288#endif
289
290/*
291 * Try to get the current Vim shell size. Put the result in Rows and Columns.
292 * Use the new sizes as defaults for 'columns' and 'lines'.
293 * Return OK when size could be determined, FAIL otherwise.
294 */
295 int
296ui_get_shellsize()
297{
298 int retval;
299
300#ifdef FEAT_GUI
301 if (gui.in_use)
302 retval = gui_get_shellsize();
303 else
304#endif
305 retval = mch_get_shellsize();
306
307 check_shellsize();
308
309 /* adjust the default for 'lines' and 'columns' */
310 if (retval == OK)
311 {
312 set_number_default("lines", Rows);
313 set_number_default("columns", Columns);
314 }
315 return retval;
316}
317
318/*
319 * Set the size of the Vim shell according to Rows and Columns, if possible.
320 * The gui_set_shellsize() or mch_set_shellsize() function will try to set the
321 * new size. If this is not possible, it will adjust Rows and Columns.
322 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 void
324ui_set_shellsize(mustset)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000325 int mustset UNUSED; /* set by the user */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326{
327#ifdef FEAT_GUI
328 if (gui.in_use)
329 gui_set_shellsize(mustset,
330# ifdef WIN3264
331 TRUE
332# else
333 FALSE
334# endif
Bram Moolenaar04a9d452006-03-27 21:03:26 +0000335 , RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 else
337#endif
338 mch_set_shellsize();
339}
340
341/*
342 * Called when Rows and/or Columns changed. Adjust scroll region and mouse
343 * region.
344 */
345 void
346ui_new_shellsize()
347{
348 if (full_screen && !exiting)
349 {
350#ifdef FEAT_GUI
351 if (gui.in_use)
352 gui_new_shellsize();
353 else
354#endif
355 mch_new_shellsize();
356 }
357}
358
359 void
360ui_breakcheck()
361{
362#ifdef FEAT_GUI
363 if (gui.in_use)
364 gui_mch_update();
365 else
366#endif
367 mch_breakcheck();
368}
369
370/*****************************************************************************
371 * Functions for copying and pasting text between applications.
372 * This is always included in a GUI version, but may also be included when the
373 * clipboard and mouse is available to a terminal version such as xterm.
374 * Note: there are some more functions in ops.c that handle selection stuff.
375 *
376 * Also note that the majority of functions here deal with the X 'primary'
377 * (visible - for Visual mode use) selection, and only that. There are no
378 * versions of these for the 'clipboard' selection, as Visual mode has no use
379 * for them.
380 */
381
382#if defined(FEAT_CLIPBOARD) || defined(PROTO)
383
384/*
385 * Selection stuff using Visual mode, for cutting and pasting text to other
386 * windows.
387 */
388
389/*
390 * Call this to initialise the clipboard. Pass it FALSE if the clipboard code
391 * is included, but the clipboard can not be used, or TRUE if the clipboard can
392 * be used. Eg unix may call this with FALSE, then call it again with TRUE if
393 * the GUI starts.
394 */
395 void
396clip_init(can_use)
397 int can_use;
398{
399 VimClipboard *cb;
400
401 cb = &clip_star;
402 for (;;)
403 {
404 cb->available = can_use;
405 cb->owned = FALSE;
406 cb->start.lnum = 0;
407 cb->start.col = 0;
408 cb->end.lnum = 0;
409 cb->end.col = 0;
410 cb->state = SELECT_CLEARED;
411
412 if (cb == &clip_plus)
413 break;
414 cb = &clip_plus;
415 }
416}
417
418/*
419 * Check whether the VIsual area has changed, and if so try to become the owner
420 * of the selection, and free any old converted selection we may still have
421 * lying around. If the VIsual mode has ended, make a copy of what was
422 * selected so we can still give it to others. Will probably have to make sure
423 * this is called whenever VIsual mode is ended.
424 */
425 void
426clip_update_selection()
427{
428 pos_T start, end;
429
430 /* If visual mode is only due to a redo command ("."), then ignore it */
431 if (!redo_VIsual_busy && VIsual_active && (State & NORMAL))
432 {
433 if (lt(VIsual, curwin->w_cursor))
434 {
435 start = VIsual;
436 end = curwin->w_cursor;
437#ifdef FEAT_MBYTE
438 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000439 end.col += (*mb_ptr2len)(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440#endif
441 }
442 else
443 {
444 start = curwin->w_cursor;
445 end = VIsual;
446 }
447 if (!equalpos(clip_star.start, start)
448 || !equalpos(clip_star.end, end)
449 || clip_star.vmode != VIsual_mode)
450 {
451 clip_clear_selection();
452 clip_star.start = start;
453 clip_star.end = end;
454 clip_star.vmode = VIsual_mode;
455 clip_free_selection(&clip_star);
456 clip_own_selection(&clip_star);
457 clip_gen_set_selection(&clip_star);
458 }
459 }
460}
461
462 void
463clip_own_selection(cbd)
464 VimClipboard *cbd;
465{
466 /*
467 * Also want to check somehow that we are reading from the keyboard rather
468 * than a mapping etc.
469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470#ifdef FEAT_X11
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200471 /* Always own the selection, we might have lost it without being
472 * notified. */
473 if (cbd->available)
474 {
475 int was_owned = cbd->owned;
476
477 cbd->owned = (clip_gen_own_selection(cbd) == OK);
478 if (!was_owned && cbd == &clip_star)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 {
Bram Moolenaarebefac62005-12-28 22:39:57 +0000480 /* May have to show a different kind of highlighting for the
481 * selected area. There is no specific redraw command for this,
482 * just redraw all windows on the current buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 if (cbd->owned
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000484 && (get_real_state() == VISUAL
485 || get_real_state() == SELECTMODE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 && clip_isautosel()
487 && hl_attr(HLF_V) != hl_attr(HLF_VNC))
488 redraw_curbuf_later(INVERTED_ALL);
489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200491#else
492 /* Only own the clibpard when we didn't own it yet. */
493 if (!cbd->owned && cbd->available)
494 cbd->owned = (clip_gen_own_selection(cbd) == OK);
495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496}
497
498 void
499clip_lose_selection(cbd)
500 VimClipboard *cbd;
501{
502#ifdef FEAT_X11
503 int was_owned = cbd->owned;
504#endif
505 int visual_selection = (cbd == &clip_star);
506
507 clip_free_selection(cbd);
508 cbd->owned = FALSE;
509 if (visual_selection)
510 clip_clear_selection();
511 clip_gen_lose_selection(cbd);
512#ifdef FEAT_X11
513 if (visual_selection)
514 {
515 /* May have to show a different kind of highlighting for the selected
516 * area. There is no specific redraw command for this, just redraw all
517 * windows on the current buffer. */
518 if (was_owned
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000519 && (get_real_state() == VISUAL
520 || get_real_state() == SELECTMODE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 && clip_isautosel()
522 && hl_attr(HLF_V) != hl_attr(HLF_VNC))
523 {
524 update_curbuf(INVERTED_ALL);
525 setcursor();
526 cursor_on();
527 out_flush();
528# ifdef FEAT_GUI
529 if (gui.in_use)
530 gui_update_cursor(TRUE, FALSE);
531# endif
532 }
533 }
534#endif
535}
536
537 void
538clip_copy_selection()
539{
540 if (VIsual_active && (State & NORMAL) && clip_star.available)
541 {
542 if (clip_isautosel())
543 clip_update_selection();
544 clip_free_selection(&clip_star);
545 clip_own_selection(&clip_star);
546 if (clip_star.owned)
547 clip_get_selection(&clip_star);
548 clip_gen_set_selection(&clip_star);
549 }
550}
551
552/*
553 * Called when Visual mode is ended: update the selection.
554 */
555 void
556clip_auto_select()
557{
558 if (clip_isautosel())
559 clip_copy_selection();
560}
561
562/*
563 * Return TRUE if automatic selection of Visual area is desired.
564 */
565 int
566clip_isautosel()
567{
568 return (
569#ifdef FEAT_GUI
570 gui.in_use ? (vim_strchr(p_go, GO_ASEL) != NULL) :
571#endif
572 clip_autoselect);
573}
574
575
576/*
577 * Stuff for general mouse selection, without using Visual mode.
578 */
579
580static int clip_compare_pos __ARGS((int row1, int col1, int row2, int col2));
581static void clip_invert_area __ARGS((int, int, int, int, int how));
582static void clip_invert_rectangle __ARGS((int row, int col, int height, int width, int invert));
583static void clip_get_word_boundaries __ARGS((VimClipboard *, int, int));
584static int clip_get_line_end __ARGS((int));
585static void clip_update_modeless_selection __ARGS((VimClipboard *, int, int,
586 int, int));
587
588/* flags for clip_invert_area() */
589#define CLIP_CLEAR 1
590#define CLIP_SET 2
591#define CLIP_TOGGLE 3
592
593/*
594 * Start, continue or end a modeless selection. Used when editing the
595 * command-line and in the cmdline window.
596 */
597 void
598clip_modeless(button, is_click, is_drag)
599 int button;
600 int is_click;
601 int is_drag;
602{
603 int repeat;
604
605 repeat = ((clip_star.mode == SELECT_MODE_CHAR
606 || clip_star.mode == SELECT_MODE_LINE)
607 && (mod_mask & MOD_MASK_2CLICK))
608 || (clip_star.mode == SELECT_MODE_WORD
609 && (mod_mask & MOD_MASK_3CLICK));
610 if (is_click && button == MOUSE_RIGHT)
611 {
612 /* Right mouse button: If there was no selection, start one.
613 * Otherwise extend the existing selection. */
614 if (clip_star.state == SELECT_CLEARED)
615 clip_start_selection(mouse_col, mouse_row, FALSE);
616 clip_process_selection(button, mouse_col, mouse_row, repeat);
617 }
618 else if (is_click)
619 clip_start_selection(mouse_col, mouse_row, repeat);
620 else if (is_drag)
621 {
622 /* Don't try extending a selection if there isn't one. Happens when
623 * button-down is in the cmdline and them moving mouse upwards. */
624 if (clip_star.state != SELECT_CLEARED)
625 clip_process_selection(button, mouse_col, mouse_row, repeat);
626 }
627 else /* release */
628 clip_process_selection(MOUSE_RELEASE, mouse_col, mouse_row, FALSE);
629}
630
631/*
632 * Compare two screen positions ala strcmp()
633 */
634 static int
635clip_compare_pos(row1, col1, row2, col2)
636 int row1;
637 int col1;
638 int row2;
639 int col2;
640{
641 if (row1 > row2) return(1);
642 if (row1 < row2) return(-1);
643 if (col1 > col2) return(1);
644 if (col1 < col2) return(-1);
645 return(0);
646}
647
648/*
649 * Start the selection
650 */
651 void
652clip_start_selection(col, row, repeated_click)
653 int col;
654 int row;
655 int repeated_click;
656{
657 VimClipboard *cb = &clip_star;
658
659 if (cb->state == SELECT_DONE)
660 clip_clear_selection();
661
662 row = check_row(row);
663 col = check_col(col);
664#ifdef FEAT_MBYTE
665 col = mb_fix_col(col, row);
666#endif
667
668 cb->start.lnum = row;
669 cb->start.col = col;
670 cb->end = cb->start;
671 cb->origin_row = (short_u)cb->start.lnum;
672 cb->state = SELECT_IN_PROGRESS;
673
674 if (repeated_click)
675 {
676 if (++cb->mode > SELECT_MODE_LINE)
677 cb->mode = SELECT_MODE_CHAR;
678 }
679 else
680 cb->mode = SELECT_MODE_CHAR;
681
682#ifdef FEAT_GUI
683 /* clear the cursor until the selection is made */
684 if (gui.in_use)
685 gui_undraw_cursor();
686#endif
687
688 switch (cb->mode)
689 {
690 case SELECT_MODE_CHAR:
691 cb->origin_start_col = cb->start.col;
692 cb->word_end_col = clip_get_line_end((int)cb->start.lnum);
693 break;
694
695 case SELECT_MODE_WORD:
696 clip_get_word_boundaries(cb, (int)cb->start.lnum, cb->start.col);
697 cb->origin_start_col = cb->word_start_col;
698 cb->origin_end_col = cb->word_end_col;
699
700 clip_invert_area((int)cb->start.lnum, cb->word_start_col,
701 (int)cb->end.lnum, cb->word_end_col, CLIP_SET);
702 cb->start.col = cb->word_start_col;
703 cb->end.col = cb->word_end_col;
704 break;
705
706 case SELECT_MODE_LINE:
707 clip_invert_area((int)cb->start.lnum, 0, (int)cb->start.lnum,
708 (int)Columns, CLIP_SET);
709 cb->start.col = 0;
710 cb->end.col = Columns;
711 break;
712 }
713
714 cb->prev = cb->start;
715
716#ifdef DEBUG_SELECTION
717 printf("Selection started at (%u,%u)\n", cb->start.lnum, cb->start.col);
718#endif
719}
720
721/*
722 * Continue processing the selection
723 */
724 void
725clip_process_selection(button, col, row, repeated_click)
726 int button;
727 int col;
728 int row;
729 int_u repeated_click;
730{
731 VimClipboard *cb = &clip_star;
732 int diff;
733 int slen = 1; /* cursor shape width */
734
735 if (button == MOUSE_RELEASE)
736 {
737 /* Check to make sure we have something selected */
738 if (cb->start.lnum == cb->end.lnum && cb->start.col == cb->end.col)
739 {
740#ifdef FEAT_GUI
741 if (gui.in_use)
742 gui_update_cursor(FALSE, FALSE);
743#endif
744 cb->state = SELECT_CLEARED;
745 return;
746 }
747
748#ifdef DEBUG_SELECTION
749 printf("Selection ended: (%u,%u) to (%u,%u)\n", cb->start.lnum,
750 cb->start.col, cb->end.lnum, cb->end.col);
751#endif
752 if (clip_isautosel()
753 || (
754#ifdef FEAT_GUI
755 gui.in_use ? (vim_strchr(p_go, GO_ASELML) != NULL) :
756#endif
757 clip_autoselectml))
758 clip_copy_modeless_selection(FALSE);
759#ifdef FEAT_GUI
760 if (gui.in_use)
761 gui_update_cursor(FALSE, FALSE);
762#endif
763
764 cb->state = SELECT_DONE;
765 return;
766 }
767
768 row = check_row(row);
769 col = check_col(col);
770#ifdef FEAT_MBYTE
771 col = mb_fix_col(col, row);
772#endif
773
774 if (col == (int)cb->prev.col && row == cb->prev.lnum && !repeated_click)
775 return;
776
777 /*
778 * When extending the selection with the right mouse button, swap the
779 * start and end if the position is before half the selection
780 */
781 if (cb->state == SELECT_DONE && button == MOUSE_RIGHT)
782 {
783 /*
784 * If the click is before the start, or the click is inside the
785 * selection and the start is the closest side, set the origin to the
786 * end of the selection.
787 */
788 if (clip_compare_pos(row, col, (int)cb->start.lnum, cb->start.col) < 0
789 || (clip_compare_pos(row, col,
790 (int)cb->end.lnum, cb->end.col) < 0
791 && (((cb->start.lnum == cb->end.lnum
792 && cb->end.col - col > col - cb->start.col))
793 || ((diff = (cb->end.lnum - row) -
794 (row - cb->start.lnum)) > 0
795 || (diff == 0 && col < (int)(cb->start.col +
796 cb->end.col) / 2)))))
797 {
798 cb->origin_row = (short_u)cb->end.lnum;
799 cb->origin_start_col = cb->end.col - 1;
800 cb->origin_end_col = cb->end.col;
801 }
802 else
803 {
804 cb->origin_row = (short_u)cb->start.lnum;
805 cb->origin_start_col = cb->start.col;
806 cb->origin_end_col = cb->start.col;
807 }
808 if (cb->mode == SELECT_MODE_WORD && !repeated_click)
809 cb->mode = SELECT_MODE_CHAR;
810 }
811
812 /* set state, for when using the right mouse button */
813 cb->state = SELECT_IN_PROGRESS;
814
815#ifdef DEBUG_SELECTION
816 printf("Selection extending to (%d,%d)\n", row, col);
817#endif
818
819 if (repeated_click && ++cb->mode > SELECT_MODE_LINE)
820 cb->mode = SELECT_MODE_CHAR;
821
822 switch (cb->mode)
823 {
824 case SELECT_MODE_CHAR:
825 /* If we're on a different line, find where the line ends */
826 if (row != cb->prev.lnum)
827 cb->word_end_col = clip_get_line_end(row);
828
829 /* See if we are before or after the origin of the selection */
830 if (clip_compare_pos(row, col, cb->origin_row,
831 cb->origin_start_col) >= 0)
832 {
833 if (col >= (int)cb->word_end_col)
834 clip_update_modeless_selection(cb, cb->origin_row,
835 cb->origin_start_col, row, (int)Columns);
836 else
837 {
838#ifdef FEAT_MBYTE
839 if (has_mbyte && mb_lefthalve(row, col))
840 slen = 2;
841#endif
842 clip_update_modeless_selection(cb, cb->origin_row,
843 cb->origin_start_col, row, col + slen);
844 }
845 }
846 else
847 {
848#ifdef FEAT_MBYTE
849 if (has_mbyte
850 && mb_lefthalve(cb->origin_row, cb->origin_start_col))
851 slen = 2;
852#endif
853 if (col >= (int)cb->word_end_col)
854 clip_update_modeless_selection(cb, row, cb->word_end_col,
855 cb->origin_row, cb->origin_start_col + slen);
856 else
857 clip_update_modeless_selection(cb, row, col,
858 cb->origin_row, cb->origin_start_col + slen);
859 }
860 break;
861
862 case SELECT_MODE_WORD:
863 /* If we are still within the same word, do nothing */
864 if (row == cb->prev.lnum && col >= (int)cb->word_start_col
865 && col < (int)cb->word_end_col && !repeated_click)
866 return;
867
868 /* Get new word boundaries */
869 clip_get_word_boundaries(cb, row, col);
870
871 /* Handle being after the origin point of selection */
872 if (clip_compare_pos(row, col, cb->origin_row,
873 cb->origin_start_col) >= 0)
874 clip_update_modeless_selection(cb, cb->origin_row,
875 cb->origin_start_col, row, cb->word_end_col);
876 else
877 clip_update_modeless_selection(cb, row, cb->word_start_col,
878 cb->origin_row, cb->origin_end_col);
879 break;
880
881 case SELECT_MODE_LINE:
882 if (row == cb->prev.lnum && !repeated_click)
883 return;
884
885 if (clip_compare_pos(row, col, cb->origin_row,
886 cb->origin_start_col) >= 0)
887 clip_update_modeless_selection(cb, cb->origin_row, 0, row,
888 (int)Columns);
889 else
890 clip_update_modeless_selection(cb, row, 0, cb->origin_row,
891 (int)Columns);
892 break;
893 }
894
895 cb->prev.lnum = row;
896 cb->prev.col = col;
897
898#ifdef DEBUG_SELECTION
899 printf("Selection is: (%u,%u) to (%u,%u)\n", cb->start.lnum,
900 cb->start.col, cb->end.lnum, cb->end.col);
901#endif
902}
903
904#if 0 /* not used */
905/*
906 * Called after an Expose event to redraw the selection
907 */
908 void
909clip_redraw_selection(x, y, w, h)
910 int x;
911 int y;
912 int w;
913 int h;
914{
915 VimClipboard *cb = &clip_star;
916 int row1, col1, row2, col2;
917 int row;
918 int start;
919 int end;
920
921 if (cb->state == SELECT_CLEARED)
922 return;
923
924 row1 = check_row(Y_2_ROW(y));
925 col1 = check_col(X_2_COL(x));
926 row2 = check_row(Y_2_ROW(y + h - 1));
927 col2 = check_col(X_2_COL(x + w - 1));
928
929 /* Limit the rows that need to be re-drawn */
930 if (cb->start.lnum > row1)
931 row1 = cb->start.lnum;
932 if (cb->end.lnum < row2)
933 row2 = cb->end.lnum;
934
935 /* Look at each row that might need to be re-drawn */
936 for (row = row1; row <= row2; row++)
937 {
938 /* For the first selection row, use the starting selection column */
939 if (row == cb->start.lnum)
940 start = cb->start.col;
941 else
942 start = 0;
943
944 /* For the last selection row, use the ending selection column */
945 if (row == cb->end.lnum)
946 end = cb->end.col;
947 else
948 end = Columns;
949
950 if (col1 > start)
951 start = col1;
952
953 if (col2 < end)
954 end = col2 + 1;
955
956 if (end > start)
957 gui_mch_invert_rectangle(row, start, 1, end - start);
958 }
959}
960#endif
961
962# if defined(FEAT_GUI) || defined(PROTO)
963/*
964 * Redraw part of the selection if character at "row,col" is inside of it.
965 * Only used for the GUI.
966 */
967 void
968clip_may_redraw_selection(row, col, len)
969 int row, col;
970 int len;
971{
972 int start = col;
973 int end = col + len;
974
975 if (clip_star.state != SELECT_CLEARED
976 && row >= clip_star.start.lnum
977 && row <= clip_star.end.lnum)
978 {
979 if (row == clip_star.start.lnum && start < (int)clip_star.start.col)
980 start = clip_star.start.col;
981 if (row == clip_star.end.lnum && end > (int)clip_star.end.col)
982 end = clip_star.end.col;
983 if (end > start)
984 clip_invert_area(row, start, row, end, 0);
985 }
986}
987# endif
988
989/*
990 * Called from outside to clear selected region from the display
991 */
992 void
993clip_clear_selection()
994{
995 VimClipboard *cb = &clip_star;
996
997 if (cb->state == SELECT_CLEARED)
998 return;
999
1000 clip_invert_area((int)cb->start.lnum, cb->start.col, (int)cb->end.lnum,
1001 cb->end.col, CLIP_CLEAR);
1002 cb->state = SELECT_CLEARED;
1003}
1004
1005/*
1006 * Clear the selection if any lines from "row1" to "row2" are inside of it.
1007 */
1008 void
1009clip_may_clear_selection(row1, row2)
1010 int row1, row2;
1011{
1012 if (clip_star.state == SELECT_DONE
1013 && row2 >= clip_star.start.lnum
1014 && row1 <= clip_star.end.lnum)
1015 clip_clear_selection();
1016}
1017
1018/*
1019 * Called before the screen is scrolled up or down. Adjusts the line numbers
1020 * of the selection. Call with big number when clearing the screen.
1021 */
1022 void
1023clip_scroll_selection(rows)
1024 int rows; /* negative for scroll down */
1025{
1026 int lnum;
1027
1028 if (clip_star.state == SELECT_CLEARED)
1029 return;
1030
1031 lnum = clip_star.start.lnum - rows;
1032 if (lnum <= 0)
1033 clip_star.start.lnum = 0;
1034 else if (lnum >= screen_Rows) /* scrolled off of the screen */
1035 clip_star.state = SELECT_CLEARED;
1036 else
1037 clip_star.start.lnum = lnum;
1038
1039 lnum = clip_star.end.lnum - rows;
1040 if (lnum < 0) /* scrolled off of the screen */
1041 clip_star.state = SELECT_CLEARED;
1042 else if (lnum >= screen_Rows)
1043 clip_star.end.lnum = screen_Rows - 1;
1044 else
1045 clip_star.end.lnum = lnum;
1046}
1047
1048/*
1049 * Invert a region of the display between a starting and ending row and column
1050 * Values for "how":
1051 * CLIP_CLEAR: undo inversion
1052 * CLIP_SET: set inversion
1053 * CLIP_TOGGLE: set inversion if pos1 < pos2, undo inversion otherwise.
1054 * 0: invert (GUI only).
1055 */
1056 static void
1057clip_invert_area(row1, col1, row2, col2, how)
1058 int row1;
1059 int col1;
1060 int row2;
1061 int col2;
1062 int how;
1063{
1064 int invert = FALSE;
1065
1066 if (how == CLIP_SET)
1067 invert = TRUE;
1068
1069 /* Swap the from and to positions so the from is always before */
1070 if (clip_compare_pos(row1, col1, row2, col2) > 0)
1071 {
1072 int tmp_row, tmp_col;
1073
1074 tmp_row = row1;
1075 tmp_col = col1;
1076 row1 = row2;
1077 col1 = col2;
1078 row2 = tmp_row;
1079 col2 = tmp_col;
1080 }
1081 else if (how == CLIP_TOGGLE)
1082 invert = TRUE;
1083
1084 /* If all on the same line, do it the easy way */
1085 if (row1 == row2)
1086 {
1087 clip_invert_rectangle(row1, col1, 1, col2 - col1, invert);
1088 }
1089 else
1090 {
1091 /* Handle a piece of the first line */
1092 if (col1 > 0)
1093 {
1094 clip_invert_rectangle(row1, col1, 1, (int)Columns - col1, invert);
1095 row1++;
1096 }
1097
1098 /* Handle a piece of the last line */
1099 if (col2 < Columns - 1)
1100 {
1101 clip_invert_rectangle(row2, 0, 1, col2, invert);
1102 row2--;
1103 }
1104
1105 /* Handle the rectangle thats left */
1106 if (row2 >= row1)
1107 clip_invert_rectangle(row1, 0, row2 - row1 + 1, (int)Columns,
1108 invert);
1109 }
1110}
1111
1112/*
1113 * Invert or un-invert a rectangle of the screen.
1114 * "invert" is true if the result is inverted.
1115 */
1116 static void
1117clip_invert_rectangle(row, col, height, width, invert)
1118 int row;
1119 int col;
1120 int height;
1121 int width;
1122 int invert;
1123{
1124#ifdef FEAT_GUI
1125 if (gui.in_use)
1126 gui_mch_invert_rectangle(row, col, height, width);
1127 else
1128#endif
1129 screen_draw_rectangle(row, col, height, width, invert);
1130}
1131
1132/*
1133 * Copy the currently selected area into the '*' register so it will be
1134 * available for pasting.
1135 * When "both" is TRUE also copy to the '+' register.
1136 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 void
1138clip_copy_modeless_selection(both)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001139 int both UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140{
1141 char_u *buffer;
1142 char_u *bufp;
1143 int row;
1144 int start_col;
1145 int end_col;
1146 int line_end_col;
1147 int add_newline_flag = FALSE;
1148 int len;
1149#ifdef FEAT_MBYTE
1150 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151#endif
1152 int row1 = clip_star.start.lnum;
1153 int col1 = clip_star.start.col;
1154 int row2 = clip_star.end.lnum;
1155 int col2 = clip_star.end.col;
1156
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001157 /* Can't use ScreenLines unless initialized */
1158 if (ScreenLines == NULL)
1159 return;
1160
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 /*
1162 * Make sure row1 <= row2, and if row1 == row2 that col1 <= col2.
1163 */
1164 if (row1 > row2)
1165 {
1166 row = row1; row1 = row2; row2 = row;
1167 row = col1; col1 = col2; col2 = row;
1168 }
1169 else if (row1 == row2 && col1 > col2)
1170 {
1171 row = col1; col1 = col2; col2 = row;
1172 }
1173#ifdef FEAT_MBYTE
1174 /* correct starting point for being on right halve of double-wide char */
1175 p = ScreenLines + LineOffset[row1];
1176 if (enc_dbcs != 0)
1177 col1 -= (*mb_head_off)(p, p + col1);
1178 else if (enc_utf8 && p[col1] == 0)
1179 --col1;
1180#endif
1181
1182 /* Create a temporary buffer for storing the text */
1183 len = (row2 - row1 + 1) * Columns + 1;
1184#ifdef FEAT_MBYTE
1185 if (enc_dbcs != 0)
1186 len *= 2; /* max. 2 bytes per display cell */
1187 else if (enc_utf8)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001188 len *= MB_MAXBYTES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189#endif
1190 buffer = lalloc((long_u)len, TRUE);
1191 if (buffer == NULL) /* out of memory */
1192 return;
1193
1194 /* Process each row in the selection */
1195 for (bufp = buffer, row = row1; row <= row2; row++)
1196 {
1197 if (row == row1)
1198 start_col = col1;
1199 else
1200 start_col = 0;
1201
1202 if (row == row2)
1203 end_col = col2;
1204 else
1205 end_col = Columns;
1206
1207 line_end_col = clip_get_line_end(row);
1208
1209 /* See if we need to nuke some trailing whitespace */
1210 if (end_col >= Columns && (row < row2 || end_col > line_end_col))
1211 {
1212 /* Get rid of trailing whitespace */
1213 end_col = line_end_col;
1214 if (end_col < start_col)
1215 end_col = start_col;
1216
1217 /* If the last line extended to the end, add an extra newline */
1218 if (row == row2)
1219 add_newline_flag = TRUE;
1220 }
1221
1222 /* If after the first row, we need to always add a newline */
1223 if (row > row1 && !LineWraps[row - 1])
1224 *bufp++ = NL;
1225
1226 if (row < screen_Rows && end_col <= screen_Columns)
1227 {
1228#ifdef FEAT_MBYTE
1229 if (enc_dbcs != 0)
1230 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001231 int i;
1232
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 p = ScreenLines + LineOffset[row];
1234 for (i = start_col; i < end_col; ++i)
1235 if (enc_dbcs == DBCS_JPNU && p[i] == 0x8e)
1236 {
1237 /* single-width double-byte char */
1238 *bufp++ = 0x8e;
1239 *bufp++ = ScreenLines2[LineOffset[row] + i];
1240 }
1241 else
1242 {
1243 *bufp++ = p[i];
1244 if (MB_BYTE2LEN(p[i]) == 2)
1245 *bufp++ = p[++i];
1246 }
1247 }
1248 else if (enc_utf8)
1249 {
1250 int off;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001251 int i;
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001252 int ci;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253
1254 off = LineOffset[row];
1255 for (i = start_col; i < end_col; ++i)
1256 {
1257 /* The base character is either in ScreenLinesUC[] or
1258 * ScreenLines[]. */
1259 if (ScreenLinesUC[off + i] == 0)
1260 *bufp++ = ScreenLines[off + i];
1261 else
1262 {
1263 bufp += utf_char2bytes(ScreenLinesUC[off + i], bufp);
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001264 for (ci = 0; ci < Screen_mco; ++ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001266 /* Add a composing character. */
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001267 if (ScreenLinesC[ci][off + i] == 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001268 break;
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001269 bufp += utf_char2bytes(ScreenLinesC[ci][off + i],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 }
1272 }
1273 /* Skip right halve of double-wide character. */
1274 if (ScreenLines[off + i + 1] == 0)
1275 ++i;
1276 }
1277 }
1278 else
1279#endif
1280 {
1281 STRNCPY(bufp, ScreenLines + LineOffset[row] + start_col,
1282 end_col - start_col);
1283 bufp += end_col - start_col;
1284 }
1285 }
1286 }
1287
1288 /* Add a newline at the end if the selection ended there */
1289 if (add_newline_flag)
1290 *bufp++ = NL;
1291
1292 /* First cleanup any old selection and become the owner. */
1293 clip_free_selection(&clip_star);
1294 clip_own_selection(&clip_star);
1295
1296 /* Yank the text into the '*' register. */
1297 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_star);
1298
1299 /* Make the register contents available to the outside world. */
1300 clip_gen_set_selection(&clip_star);
1301
1302#ifdef FEAT_X11
1303 if (both)
1304 {
1305 /* Do the same for the '+' register. */
1306 clip_free_selection(&clip_plus);
1307 clip_own_selection(&clip_plus);
1308 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_plus);
1309 clip_gen_set_selection(&clip_plus);
1310 }
1311#endif
1312 vim_free(buffer);
1313}
1314
1315/*
1316 * Find the starting and ending positions of the word at the given row and
1317 * column. Only white-separated words are recognized here.
1318 */
1319#define CHAR_CLASS(c) (c <= ' ' ? ' ' : vim_iswordc(c))
1320
1321 static void
1322clip_get_word_boundaries(cb, row, col)
1323 VimClipboard *cb;
1324 int row;
1325 int col;
1326{
1327 int start_class;
1328 int temp_col;
1329 char_u *p;
1330#ifdef FEAT_MBYTE
1331 int mboff;
1332#endif
1333
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001334 if (row >= screen_Rows || col >= screen_Columns || ScreenLines == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 return;
1336
1337 p = ScreenLines + LineOffset[row];
1338#ifdef FEAT_MBYTE
1339 /* Correct for starting in the right halve of a double-wide char */
1340 if (enc_dbcs != 0)
1341 col -= dbcs_screen_head_off(p, p + col);
1342 else if (enc_utf8 && p[col] == 0)
1343 --col;
1344#endif
1345 start_class = CHAR_CLASS(p[col]);
1346
1347 temp_col = col;
1348 for ( ; temp_col > 0; temp_col--)
1349#ifdef FEAT_MBYTE
1350 if (enc_dbcs != 0
1351 && (mboff = dbcs_screen_head_off(p, p + temp_col - 1)) > 0)
1352 temp_col -= mboff;
1353 else
1354#endif
1355 if (CHAR_CLASS(p[temp_col - 1]) != start_class
1356#ifdef FEAT_MBYTE
1357 && !(enc_utf8 && p[temp_col - 1] == 0)
1358#endif
1359 )
1360 break;
1361 cb->word_start_col = temp_col;
1362
1363 temp_col = col;
1364 for ( ; temp_col < screen_Columns; temp_col++)
1365#ifdef FEAT_MBYTE
1366 if (enc_dbcs != 0 && dbcs_ptr2cells(p + temp_col) == 2)
1367 ++temp_col;
1368 else
1369#endif
1370 if (CHAR_CLASS(p[temp_col]) != start_class
1371#ifdef FEAT_MBYTE
1372 && !(enc_utf8 && p[temp_col] == 0)
1373#endif
1374 )
1375 break;
1376 cb->word_end_col = temp_col;
1377}
1378
1379/*
1380 * Find the column position for the last non-whitespace character on the given
1381 * line.
1382 */
1383 static int
1384clip_get_line_end(row)
1385 int row;
1386{
1387 int i;
1388
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001389 if (row >= screen_Rows || ScreenLines == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 return 0;
1391 for (i = screen_Columns; i > 0; i--)
1392 if (ScreenLines[LineOffset[row] + i - 1] != ' ')
1393 break;
1394 return i;
1395}
1396
1397/*
1398 * Update the currently selected region by adding and/or subtracting from the
1399 * beginning or end and inverting the changed area(s).
1400 */
1401 static void
1402clip_update_modeless_selection(cb, row1, col1, row2, col2)
1403 VimClipboard *cb;
1404 int row1;
1405 int col1;
1406 int row2;
1407 int col2;
1408{
1409 /* See if we changed at the beginning of the selection */
1410 if (row1 != cb->start.lnum || col1 != (int)cb->start.col)
1411 {
1412 clip_invert_area(row1, col1, (int)cb->start.lnum, cb->start.col,
1413 CLIP_TOGGLE);
1414 cb->start.lnum = row1;
1415 cb->start.col = col1;
1416 }
1417
1418 /* See if we changed at the end of the selection */
1419 if (row2 != cb->end.lnum || col2 != (int)cb->end.col)
1420 {
1421 clip_invert_area((int)cb->end.lnum, cb->end.col, row2, col2,
1422 CLIP_TOGGLE);
1423 cb->end.lnum = row2;
1424 cb->end.col = col2;
1425 }
1426}
1427
1428 int
1429clip_gen_own_selection(cbd)
1430 VimClipboard *cbd;
1431{
1432#ifdef FEAT_XCLIPBOARD
1433# ifdef FEAT_GUI
1434 if (gui.in_use)
1435 return clip_mch_own_selection(cbd);
1436 else
1437# endif
1438 return clip_xterm_own_selection(cbd);
1439#else
1440 return clip_mch_own_selection(cbd);
1441#endif
1442}
1443
1444 void
1445clip_gen_lose_selection(cbd)
1446 VimClipboard *cbd;
1447{
1448#ifdef FEAT_XCLIPBOARD
1449# ifdef FEAT_GUI
1450 if (gui.in_use)
1451 clip_mch_lose_selection(cbd);
1452 else
1453# endif
1454 clip_xterm_lose_selection(cbd);
1455#else
1456 clip_mch_lose_selection(cbd);
1457#endif
1458}
1459
1460 void
1461clip_gen_set_selection(cbd)
1462 VimClipboard *cbd;
1463{
1464#ifdef FEAT_XCLIPBOARD
1465# ifdef FEAT_GUI
1466 if (gui.in_use)
1467 clip_mch_set_selection(cbd);
1468 else
1469# endif
1470 clip_xterm_set_selection(cbd);
1471#else
1472 clip_mch_set_selection(cbd);
1473#endif
1474}
1475
1476 void
1477clip_gen_request_selection(cbd)
1478 VimClipboard *cbd;
1479{
1480#ifdef FEAT_XCLIPBOARD
1481# ifdef FEAT_GUI
1482 if (gui.in_use)
1483 clip_mch_request_selection(cbd);
1484 else
1485# endif
1486 clip_xterm_request_selection(cbd);
1487#else
1488 clip_mch_request_selection(cbd);
1489#endif
1490}
1491
1492#endif /* FEAT_CLIPBOARD */
1493
1494/*****************************************************************************
1495 * Functions that handle the input buffer.
1496 * This is used for any GUI version, and the unix terminal version.
1497 *
1498 * For Unix, the input characters are buffered to be able to check for a
1499 * CTRL-C. This should be done with signals, but I don't know how to do that
1500 * in a portable way for a tty in RAW mode.
1501 *
1502 * For the client-server code in the console the received keys are put in the
1503 * input buffer.
1504 */
1505
1506#if defined(USE_INPUT_BUF) || defined(PROTO)
1507
1508/*
1509 * Internal typeahead buffer. Includes extra space for long key code
1510 * descriptions which would otherwise overflow. The buffer is considered full
1511 * when only this extra space (or part of it) remains.
1512 */
1513#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
1514 || defined(FEAT_CLIENTSERVER)
1515 /*
1516 * Sun WorkShop and NetBeans stuff debugger commands into the input buffer.
1517 * This requires a larger buffer...
1518 * (Madsen) Go with this for remote input as well ...
1519 */
1520# define INBUFLEN 4096
1521#else
1522# define INBUFLEN 250
1523#endif
1524
1525static char_u inbuf[INBUFLEN + MAX_KEY_CODE_LEN];
1526static int inbufcount = 0; /* number of chars in inbuf[] */
1527
1528/*
1529 * vim_is_input_buf_full(), vim_is_input_buf_empty(), add_to_input_buf(), and
1530 * trash_input_buf() are functions for manipulating the input buffer. These
1531 * are used by the gui_* calls when a GUI is used to handle keyboard input.
1532 */
1533
1534 int
1535vim_is_input_buf_full()
1536{
1537 return (inbufcount >= INBUFLEN);
1538}
1539
1540 int
1541vim_is_input_buf_empty()
1542{
1543 return (inbufcount == 0);
1544}
1545
1546#if defined(FEAT_OLE) || defined(PROTO)
1547 int
1548vim_free_in_input_buf()
1549{
1550 return (INBUFLEN - inbufcount);
1551}
1552#endif
1553
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001554#if defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 int
1556vim_used_in_input_buf()
1557{
1558 return inbufcount;
1559}
1560#endif
1561
1562#if defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) || defined(PROTO)
1563/*
1564 * Return the current contents of the input buffer and make it empty.
1565 * The returned pointer must be passed to set_input_buf() later.
1566 */
1567 char_u *
1568get_input_buf()
1569{
1570 garray_T *gap;
1571
1572 /* We use a growarray to store the data pointer and the length. */
1573 gap = (garray_T *)alloc((unsigned)sizeof(garray_T));
1574 if (gap != NULL)
1575 {
1576 /* Add one to avoid a zero size. */
1577 gap->ga_data = alloc((unsigned)inbufcount + 1);
1578 if (gap->ga_data != NULL)
1579 mch_memmove(gap->ga_data, inbuf, (size_t)inbufcount);
1580 gap->ga_len = inbufcount;
1581 }
1582 trash_input_buf();
1583 return (char_u *)gap;
1584}
1585
1586/*
1587 * Restore the input buffer with a pointer returned from get_input_buf().
1588 * The allocated memory is freed, this only works once!
1589 */
1590 void
1591set_input_buf(p)
1592 char_u *p;
1593{
1594 garray_T *gap = (garray_T *)p;
1595
1596 if (gap != NULL)
1597 {
1598 if (gap->ga_data != NULL)
1599 {
1600 mch_memmove(inbuf, gap->ga_data, gap->ga_len);
1601 inbufcount = gap->ga_len;
1602 vim_free(gap->ga_data);
1603 }
1604 vim_free(gap);
1605 }
1606}
1607#endif
1608
Bram Moolenaar446cb832008-06-24 21:56:24 +00001609#if defined(FEAT_GUI) \
1610 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 || defined(FEAT_XCLIPBOARD) || defined(VMS) \
Bram Moolenaarf52c7252006-02-10 23:23:57 +00001612 || defined(FEAT_SNIFF) || defined(FEAT_CLIENTSERVER) \
Bram Moolenaarf52c7252006-02-10 23:23:57 +00001613 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614/*
1615 * Add the given bytes to the input buffer
1616 * Special keys start with CSI. A real CSI must have been translated to
1617 * CSI KS_EXTRA KE_CSI. K_SPECIAL doesn't require translation.
1618 */
1619 void
1620add_to_input_buf(s, len)
1621 char_u *s;
1622 int len;
1623{
1624 if (inbufcount + len > INBUFLEN + MAX_KEY_CODE_LEN)
1625 return; /* Shouldn't ever happen! */
1626
1627#ifdef FEAT_HANGULIN
1628 if ((State & (INSERT|CMDLINE)) && hangul_input_state_get())
1629 if ((len = hangul_input_process(s, len)) == 0)
1630 return;
1631#endif
1632
1633 while (len--)
1634 inbuf[inbufcount++] = *s++;
1635}
1636#endif
1637
Bram Moolenaar70c2a632007-08-15 18:08:50 +00001638#if ((defined(FEAT_XIM) || defined(FEAT_DND)) && defined(FEAT_GUI_GTK)) \
1639 || defined(FEAT_GUI_MSWIN) \
1640 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 || (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) \
Bram Moolenaarf52c7252006-02-10 23:23:57 +00001642 || (defined(FEAT_GUI) && (!defined(USE_ON_FLY_SCROLL) \
1643 || defined(FEAT_MENU))) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 || defined(PROTO)
1645/*
1646 * Add "str[len]" to the input buffer while escaping CSI bytes.
1647 */
1648 void
1649add_to_input_buf_csi(char_u *str, int len)
1650{
1651 int i;
1652 char_u buf[2];
1653
1654 for (i = 0; i < len; ++i)
1655 {
1656 add_to_input_buf(str + i, 1);
1657 if (str[i] == CSI)
1658 {
1659 /* Turn CSI into K_CSI. */
1660 buf[0] = KS_EXTRA;
1661 buf[1] = (int)KE_CSI;
1662 add_to_input_buf(buf, 2);
1663 }
1664 }
1665}
1666#endif
1667
1668#if defined(FEAT_HANGULIN) || defined(PROTO)
1669 void
1670push_raw_key (s, len)
1671 char_u *s;
1672 int len;
1673{
1674 while (len--)
1675 inbuf[inbufcount++] = *s++;
1676}
1677#endif
1678
1679#if defined(FEAT_GUI) || defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) \
1680 || defined(PROTO)
1681/* Remove everything from the input buffer. Called when ^C is found */
1682 void
1683trash_input_buf()
1684{
1685 inbufcount = 0;
1686}
1687#endif
1688
1689/*
1690 * Read as much data from the input buffer as possible up to maxlen, and store
1691 * it in buf.
1692 * Note: this function used to be Read() in unix.c
1693 */
1694 int
1695read_from_input_buf(buf, maxlen)
1696 char_u *buf;
1697 long maxlen;
1698{
1699 if (inbufcount == 0) /* if the buffer is empty, fill it */
1700 fill_input_buf(TRUE);
1701 if (maxlen > inbufcount)
1702 maxlen = inbufcount;
1703 mch_memmove(buf, inbuf, (size_t)maxlen);
1704 inbufcount -= maxlen;
1705 if (inbufcount)
1706 mch_memmove(inbuf, inbuf + maxlen, (size_t)inbufcount);
1707 return (int)maxlen;
1708}
1709
1710 void
1711fill_input_buf(exit_on_error)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001712 int exit_on_error UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713{
1714#if defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X_UNIX)
1715 int len;
1716 int try;
1717 static int did_read_something = FALSE;
1718# ifdef FEAT_MBYTE
1719 static char_u *rest = NULL; /* unconverted rest of previous read */
1720 static int restlen = 0;
1721 int unconverted;
1722# endif
1723#endif
1724
1725#ifdef FEAT_GUI
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001726 if (gui.in_use
1727# ifdef NO_CONSOLE_INPUT
1728 /* Don't use the GUI input when the window hasn't been opened yet.
1729 * We get here from ui_inchar() when we should try reading from stdin. */
1730 && !no_console_input()
1731# endif
1732 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 {
1734 gui_mch_update();
1735 return;
1736 }
1737#endif
1738#if defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X_UNIX)
1739 if (vim_is_input_buf_full())
1740 return;
1741 /*
1742 * Fill_input_buf() is only called when we really need a character.
1743 * If we can't get any, but there is some in the buffer, just return.
1744 * If we can't get any, and there isn't any in the buffer, we give up and
1745 * exit Vim.
1746 */
1747# ifdef __BEOS__
1748 /*
1749 * On the BeBox version (for now), all input is secretly performed within
1750 * beos_select() which is called from RealWaitForChar().
1751 */
1752 while (!vim_is_input_buf_full() && RealWaitForChar(read_cmd_fd, 0, NULL))
1753 ;
1754 len = inbufcount;
1755 inbufcount = 0;
1756# else
1757
1758# ifdef FEAT_SNIFF
1759 if (sniff_request_waiting)
1760 {
1761 add_to_input_buf((char_u *)"\233sniff",6); /* results in K_SNIFF */
1762 sniff_request_waiting = 0;
1763 want_sniff_request = 0;
1764 return;
1765 }
1766# endif
1767
1768# ifdef FEAT_MBYTE
1769 if (rest != NULL)
1770 {
1771 /* Use remainder of previous call, starts with an invalid character
1772 * that may become valid when reading more. */
1773 if (restlen > INBUFLEN - inbufcount)
1774 unconverted = INBUFLEN - inbufcount;
1775 else
1776 unconverted = restlen;
1777 mch_memmove(inbuf + inbufcount, rest, unconverted);
1778 if (unconverted == restlen)
1779 {
1780 vim_free(rest);
1781 rest = NULL;
1782 }
1783 else
1784 {
1785 restlen -= unconverted;
1786 mch_memmove(rest, rest + unconverted, restlen);
1787 }
1788 inbufcount += unconverted;
1789 }
1790 else
1791 unconverted = 0;
1792#endif
1793
1794 len = 0; /* to avoid gcc warning */
1795 for (try = 0; try < 100; ++try)
1796 {
1797# ifdef VMS
1798 len = vms_read(
1799# else
1800 len = read(read_cmd_fd,
1801# endif
1802 (char *)inbuf + inbufcount, (size_t)((INBUFLEN - inbufcount)
1803# ifdef FEAT_MBYTE
1804 / input_conv.vc_factor
1805# endif
1806 ));
1807# if 0
1808 ) /* avoid syntax highlight error */
1809# endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001810
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 if (len > 0 || got_int)
1812 break;
1813 /*
1814 * If reading stdin results in an error, continue reading stderr.
1815 * This helps when using "foo | xargs vim".
1816 */
1817 if (!did_read_something && !isatty(read_cmd_fd) && read_cmd_fd == 0)
1818 {
1819 int m = cur_tmode;
1820
1821 /* We probably set the wrong file descriptor to raw mode. Switch
1822 * back to cooked mode, use another descriptor and set the mode to
1823 * what it was. */
1824 settmode(TMODE_COOK);
1825#ifdef HAVE_DUP
1826 /* Use stderr for stdin, also works for shell commands. */
1827 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00001828 ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829#else
1830 read_cmd_fd = 2; /* read from stderr instead of stdin */
1831#endif
1832 settmode(m);
1833 }
1834 if (!exit_on_error)
1835 return;
1836 }
1837# endif
1838 if (len <= 0 && !got_int)
1839 read_error_exit();
1840 if (len > 0)
1841 did_read_something = TRUE;
1842 if (got_int)
1843 {
1844 /* Interrupted, pretend a CTRL-C was typed. */
1845 inbuf[0] = 3;
1846 inbufcount = 1;
1847 }
1848 else
1849 {
1850# ifdef FEAT_MBYTE
1851 /*
1852 * May perform conversion on the input characters.
1853 * Include the unconverted rest of the previous call.
1854 * If there is an incomplete char at the end it is kept for the next
1855 * time, reading more bytes should make conversion possible.
1856 * Don't do this in the unlikely event that the input buffer is too
1857 * small ("rest" still contains more bytes).
1858 */
1859 if (input_conv.vc_type != CONV_NONE)
1860 {
1861 inbufcount -= unconverted;
1862 len = convert_input_safe(inbuf + inbufcount,
1863 len + unconverted, INBUFLEN - inbufcount,
1864 rest == NULL ? &rest : NULL, &restlen);
1865 }
1866# endif
1867 while (len-- > 0)
1868 {
1869 /*
1870 * if a CTRL-C was typed, remove it from the buffer and set got_int
1871 */
1872 if (inbuf[inbufcount] == 3 && ctrl_c_interrupts)
1873 {
1874 /* remove everything typed before the CTRL-C */
1875 mch_memmove(inbuf, inbuf + inbufcount, (size_t)(len + 1));
1876 inbufcount = 0;
1877 got_int = TRUE;
1878 }
1879 ++inbufcount;
1880 }
1881 }
1882#endif /* UNIX or OS2 or VMS*/
1883}
1884#endif /* defined(UNIX) || defined(FEAT_GUI) || defined(OS2) || defined(VMS) */
1885
1886/*
1887 * Exit because of an input read error.
1888 */
1889 void
1890read_error_exit()
1891{
1892 if (silent_mode) /* Normal way to exit for "ex -s" */
1893 getout(0);
1894 STRCPY(IObuff, _("Vim: Error reading input, exiting...\n"));
1895 preserve_exit();
1896}
1897
1898#if defined(CURSOR_SHAPE) || defined(PROTO)
1899/*
1900 * May update the shape of the cursor.
1901 */
1902 void
1903ui_cursor_shape()
1904{
1905# ifdef FEAT_GUI
1906 if (gui.in_use)
1907 gui_update_cursor_later();
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001908 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001910 term_cursor_shape();
1911
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912# ifdef MCH_CURSOR_SHAPE
1913 mch_update_cursor();
1914# endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001915
1916# ifdef FEAT_CONCEAL
1917 conceal_check_cursur_line_redraw();
1918# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919}
1920#endif
1921
1922#if defined(FEAT_CLIPBOARD) || defined(FEAT_GUI) || defined(FEAT_RIGHTLEFT) \
Bram Moolenaaraf51e662008-07-14 19:48:05 +00001923 || defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924/*
1925 * Check bounds for column number
1926 */
1927 int
1928check_col(col)
1929 int col;
1930{
1931 if (col < 0)
1932 return 0;
1933 if (col >= (int)screen_Columns)
1934 return (int)screen_Columns - 1;
1935 return col;
1936}
1937
1938/*
1939 * Check bounds for row number
1940 */
1941 int
1942check_row(row)
1943 int row;
1944{
1945 if (row < 0)
1946 return 0;
1947 if (row >= (int)screen_Rows)
1948 return (int)screen_Rows - 1;
1949 return row;
1950}
1951#endif
1952
1953/*
1954 * Stuff for the X clipboard. Shared between VMS and Unix.
1955 */
1956
1957#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) || defined(PROTO)
1958# include <X11/Xatom.h>
1959# include <X11/Intrinsic.h>
1960
1961/*
1962 * Open the application context (if it hasn't been opened yet).
1963 * Used for Motif and Athena GUI and the xterm clipboard.
1964 */
1965 void
1966open_app_context()
1967{
1968 if (app_context == NULL)
1969 {
1970 XtToolkitInitialize();
1971 app_context = XtCreateApplicationContext();
1972 }
1973}
1974
1975static Atom vim_atom; /* Vim's own special selection format */
1976#ifdef FEAT_MBYTE
1977static Atom vimenc_atom; /* Vim's extended selection format */
1978#endif
1979static Atom compound_text_atom;
1980static Atom text_atom;
1981static Atom targets_atom;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001982static Atom timestamp_atom; /* Used to get a timestamp */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983
1984 void
1985x11_setup_atoms(dpy)
1986 Display *dpy;
1987{
1988 vim_atom = XInternAtom(dpy, VIM_ATOM_NAME, False);
1989#ifdef FEAT_MBYTE
1990 vimenc_atom = XInternAtom(dpy, VIMENC_ATOM_NAME,False);
1991#endif
1992 compound_text_atom = XInternAtom(dpy, "COMPOUND_TEXT", False);
1993 text_atom = XInternAtom(dpy, "TEXT", False);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001994 targets_atom = XInternAtom(dpy, "TARGETS", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 clip_star.sel_atom = XA_PRIMARY;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001996 clip_plus.sel_atom = XInternAtom(dpy, "CLIPBOARD", False);
1997 timestamp_atom = XInternAtom(dpy, "TIMESTAMP", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998}
1999
2000/*
2001 * X Selection stuff, for cutting and pasting text to other windows.
2002 */
2003
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002004static Boolean clip_x11_convert_selection_cb __ARGS((Widget, Atom *, Atom *, Atom *, XtPointer *, long_u *, int *));
2005
2006static void clip_x11_lose_ownership_cb __ARGS((Widget, Atom *));
2007
2008static void clip_x11_timestamp_cb __ARGS((Widget w, XtPointer n, XEvent *event, Boolean *cont));
2009
2010/*
2011 * Property callback to get a timestamp for XtOwnSelection.
2012 */
2013 static void
2014clip_x11_timestamp_cb(w, n, event, cont)
2015 Widget w;
2016 XtPointer n UNUSED;
2017 XEvent *event;
2018 Boolean *cont UNUSED;
2019{
2020 Atom actual_type;
2021 int format;
2022 unsigned long nitems, bytes_after;
2023 unsigned char *prop=NULL;
2024 XPropertyEvent *xproperty=&event->xproperty;
2025
2026 /* Must be a property notify, state can't be Delete (True), has to be
2027 * one of the supported selection types. */
2028 if (event->type != PropertyNotify || xproperty->state
2029 || (xproperty->atom != clip_star.sel_atom
2030 && xproperty->atom != clip_plus.sel_atom))
2031 return;
2032
2033 if (XGetWindowProperty(xproperty->display, xproperty->window,
2034 xproperty->atom, 0, 0, False, timestamp_atom, &actual_type, &format,
2035 &nitems, &bytes_after, &prop))
2036 return;
2037
2038 if (prop)
2039 XFree(prop);
2040
2041 /* Make sure the property type is "TIMESTAMP" and it's 32 bits. */
2042 if (actual_type != timestamp_atom || format != 32)
2043 return;
2044
2045 /* Get the selection, using the event timestamp. */
2046 XtOwnSelection(w, xproperty->atom, xproperty->time,
2047 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb, NULL);
2048}
2049
2050 void
2051x11_setup_selection(w)
2052 Widget w;
2053{
2054 XtAddEventHandler(w, PropertyChangeMask, False,
2055 /*(XtEventHandler)*/clip_x11_timestamp_cb, (XtPointer)NULL);
2056}
2057
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058static void clip_x11_request_selection_cb __ARGS((Widget, XtPointer, Atom *, Atom *, XtPointer, long_u *, int *));
2059
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 static void
2061clip_x11_request_selection_cb(w, success, sel_atom, type, value, length,
2062 format)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002063 Widget w UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 XtPointer success;
2065 Atom *sel_atom;
2066 Atom *type;
2067 XtPointer value;
2068 long_u *length;
2069 int *format;
2070{
2071 int motion_type;
2072 long_u len;
2073 char_u *p;
2074 char **text_list = NULL;
2075 VimClipboard *cbd;
2076#ifdef FEAT_MBYTE
2077 char_u *tmpbuf = NULL;
2078#endif
2079
2080 if (*sel_atom == clip_plus.sel_atom)
2081 cbd = &clip_plus;
2082 else
2083 cbd = &clip_star;
2084
2085 if (value == NULL || *length == 0)
2086 {
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002087 clip_free_selection(cbd); /* nothing received, clear register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 *(int *)success = FALSE;
2089 return;
2090 }
2091 motion_type = MCHAR;
2092 p = (char_u *)value;
2093 len = *length;
2094 if (*type == vim_atom)
2095 {
2096 motion_type = *p++;
2097 len--;
2098 }
2099
2100#ifdef FEAT_MBYTE
2101 else if (*type == vimenc_atom)
2102 {
2103 char_u *enc;
2104 vimconv_T conv;
2105 int convlen;
2106
2107 motion_type = *p++;
2108 --len;
2109
2110 enc = p;
2111 p += STRLEN(p) + 1;
2112 len -= p - enc;
2113
2114 /* If the encoding of the text is different from 'encoding', attempt
2115 * converting it. */
2116 conv.vc_type = CONV_NONE;
2117 convert_setup(&conv, enc, p_enc);
2118 if (conv.vc_type != CONV_NONE)
2119 {
2120 convlen = len; /* Need to use an int here. */
2121 tmpbuf = string_convert(&conv, p, &convlen);
2122 len = convlen;
2123 if (tmpbuf != NULL)
2124 p = tmpbuf;
2125 convert_setup(&conv, NULL, NULL);
2126 }
2127 }
2128#endif
2129
2130 else if (*type == compound_text_atom || (
2131#ifdef FEAT_MBYTE
2132 enc_dbcs != 0 &&
2133#endif
2134 *type == text_atom))
2135 {
2136 XTextProperty text_prop;
2137 int n_text = 0;
2138 int status;
2139
2140 text_prop.value = (unsigned char *)value;
2141 text_prop.encoding = *type;
2142 text_prop.format = *format;
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002143 text_prop.nitems = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 status = XmbTextPropertyToTextList(X_DISPLAY, &text_prop,
2145 &text_list, &n_text);
2146 if (status != Success || n_text < 1)
2147 {
2148 *(int *)success = FALSE;
2149 return;
2150 }
2151 p = (char_u *)text_list[0];
2152 len = STRLEN(p);
2153 }
2154 clip_yank_selection(motion_type, p, (long)len, cbd);
2155
2156 if (text_list != NULL)
2157 XFreeStringList(text_list);
2158#ifdef FEAT_MBYTE
2159 vim_free(tmpbuf);
2160#endif
2161 XtFree((char *)value);
2162 *(int *)success = TRUE;
2163}
2164
2165 void
2166clip_x11_request_selection(myShell, dpy, cbd)
2167 Widget myShell;
2168 Display *dpy;
2169 VimClipboard *cbd;
2170{
2171 XEvent event;
2172 Atom type;
2173 static int success;
2174 int i;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002175 time_t start_time;
2176 int timed_out = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177
2178 for (i =
2179#ifdef FEAT_MBYTE
2180 0
2181#else
2182 1
2183#endif
2184 ; i < 5; i++)
2185 {
2186 switch (i)
2187 {
2188#ifdef FEAT_MBYTE
2189 case 0: type = vimenc_atom; break;
2190#endif
2191 case 1: type = vim_atom; break;
2192 case 2: type = compound_text_atom; break;
2193 case 3: type = text_atom; break;
2194 default: type = XA_STRING;
2195 }
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002196 success = MAYBE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 XtGetSelectionValue(myShell, cbd->sel_atom, type,
2198 clip_x11_request_selection_cb, (XtPointer)&success, CurrentTime);
2199
2200 /* Make sure the request for the selection goes out before waiting for
2201 * a response. */
2202 XFlush(dpy);
2203
2204 /*
2205 * Wait for result of selection request, otherwise if we type more
2206 * characters, then they will appear before the one that requested the
2207 * paste! Don't worry, we will catch up with any other events later.
2208 */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002209 start_time = time(NULL);
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002210 while (success == MAYBE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 {
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002212 if (XCheckTypedEvent(dpy, SelectionNotify, &event)
2213 || XCheckTypedEvent(dpy, SelectionRequest, &event)
2214 || XCheckTypedEvent(dpy, PropertyNotify, &event))
Bram Moolenaar89417b92008-09-07 19:48:53 +00002215 {
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002216 /* This is where clip_x11_request_selection_cb() should be
2217 * called. It may actually happen a bit later, so we loop
2218 * until "success" changes.
2219 * We may get a SelectionRequest here and if we don't handle
2220 * it we hang. KDE klipper does this, for example.
2221 * We need to handle a PropertyNotify for large selections. */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002222 XtDispatchEvent(&event);
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002223 continue;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225
Bram Moolenaar89417b92008-09-07 19:48:53 +00002226 /* Time out after 2 to 3 seconds to avoid that we hang when the
2227 * other process doesn't respond. Note that the SelectionNotify
2228 * event may still come later when the selection owner comes back
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002229 * to life and the text gets inserted unexpectedly. Don't know
2230 * why that happens or how to avoid that :-(. */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002231 if (time(NULL) > start_time + 2)
2232 {
2233 timed_out = TRUE;
2234 break;
2235 }
2236
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 /* Do we need this? Probably not. */
2238 XSync(dpy, False);
2239
Bram Moolenaar89417b92008-09-07 19:48:53 +00002240 /* Wait for 1 msec to avoid that we eat up all CPU time. */
2241 ui_delay(1L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 }
2243
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002244 if (success == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 return;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002246
2247 /* don't do a retry with another type after timing out, otherwise we
2248 * hang for 15 seconds. */
2249 if (timed_out)
2250 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 }
2252
2253 /* Final fallback position - use the X CUT_BUFFER0 store */
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002254 yank_cut_buffer0(dpy, cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255}
2256
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 static Boolean
2258clip_x11_convert_selection_cb(w, sel_atom, target, type, value, length, format)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002259 Widget w UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 Atom *sel_atom;
2261 Atom *target;
2262 Atom *type;
2263 XtPointer *value;
2264 long_u *length;
2265 int *format;
2266{
2267 char_u *string;
2268 char_u *result;
2269 int motion_type;
2270 VimClipboard *cbd;
2271 int i;
2272
2273 if (*sel_atom == clip_plus.sel_atom)
2274 cbd = &clip_plus;
2275 else
2276 cbd = &clip_star;
2277
2278 if (!cbd->owned)
2279 return False; /* Shouldn't ever happen */
2280
2281 /* requestor wants to know what target types we support */
2282 if (*target == targets_atom)
2283 {
2284 Atom *array;
2285
2286 if ((array = (Atom *)XtMalloc((unsigned)(sizeof(Atom) * 6))) == NULL)
2287 return False;
2288 *value = (XtPointer)array;
2289 i = 0;
2290 array[i++] = XA_STRING;
2291 array[i++] = targets_atom;
2292#ifdef FEAT_MBYTE
2293 array[i++] = vimenc_atom;
2294#endif
2295 array[i++] = vim_atom;
2296 array[i++] = text_atom;
2297 array[i++] = compound_text_atom;
2298 *type = XA_ATOM;
2299 /* This used to be: *format = sizeof(Atom) * 8; but that caused
2300 * crashes on 64 bit machines. (Peter Derr) */
2301 *format = 32;
2302 *length = i;
2303 return True;
2304 }
2305
2306 if ( *target != XA_STRING
2307#ifdef FEAT_MBYTE
2308 && *target != vimenc_atom
2309#endif
2310 && *target != vim_atom
2311 && *target != text_atom
2312 && *target != compound_text_atom)
2313 return False;
2314
2315 clip_get_selection(cbd);
2316 motion_type = clip_convert_selection(&string, length, cbd);
2317 if (motion_type < 0)
2318 return False;
2319
2320 /* For our own format, the first byte contains the motion type */
2321 if (*target == vim_atom)
2322 (*length)++;
2323
2324#ifdef FEAT_MBYTE
2325 /* Our own format with encoding: motion 'encoding' NUL text */
2326 if (*target == vimenc_atom)
2327 *length += STRLEN(p_enc) + 2;
2328#endif
2329
2330 *value = XtMalloc((Cardinal)*length);
2331 result = (char_u *)*value;
2332 if (result == NULL)
2333 {
2334 vim_free(string);
2335 return False;
2336 }
2337
2338 if (*target == XA_STRING)
2339 {
2340 mch_memmove(result, string, (size_t)(*length));
2341 *type = XA_STRING;
2342 }
2343 else if (*target == compound_text_atom
2344 || *target == text_atom)
2345 {
2346 XTextProperty text_prop;
2347 char *string_nt = (char *)alloc((unsigned)*length + 1);
2348
2349 /* create NUL terminated string which XmbTextListToTextProperty wants */
2350 mch_memmove(string_nt, string, (size_t)*length);
2351 string_nt[*length] = NUL;
2352 XmbTextListToTextProperty(X_DISPLAY, (char **)&string_nt, 1,
2353 XCompoundTextStyle, &text_prop);
2354 vim_free(string_nt);
2355 XtFree(*value); /* replace with COMPOUND text */
2356 *value = (XtPointer)(text_prop.value); /* from plain text */
2357 *length = text_prop.nitems;
2358 *type = compound_text_atom;
2359 }
2360
2361#ifdef FEAT_MBYTE
2362 else if (*target == vimenc_atom)
2363 {
2364 int l = STRLEN(p_enc);
2365
2366 result[0] = motion_type;
2367 STRCPY(result + 1, p_enc);
2368 mch_memmove(result + l + 2, string, (size_t)(*length - l - 2));
2369 *type = vimenc_atom;
2370 }
2371#endif
2372
2373 else
2374 {
2375 result[0] = motion_type;
2376 mch_memmove(result + 1, string, (size_t)(*length - 1));
2377 *type = vim_atom;
2378 }
2379 *format = 8; /* 8 bits per char */
2380 vim_free(string);
2381 return True;
2382}
2383
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 static void
2385clip_x11_lose_ownership_cb(w, sel_atom)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002386 Widget w UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 Atom *sel_atom;
2388{
2389 if (*sel_atom == clip_plus.sel_atom)
2390 clip_lose_selection(&clip_plus);
2391 else
2392 clip_lose_selection(&clip_star);
2393}
2394
2395 void
2396clip_x11_lose_selection(myShell, cbd)
2397 Widget myShell;
2398 VimClipboard *cbd;
2399{
2400 XtDisownSelection(myShell, cbd->sel_atom, CurrentTime);
2401}
2402
2403 int
2404clip_x11_own_selection(myShell, cbd)
2405 Widget myShell;
2406 VimClipboard *cbd;
2407{
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002408 /* Get the time by a zero-length append, clip_x11_timestamp_cb will be
2409 * called with the current timestamp. */
2410 if (!XChangeProperty(XtDisplay(myShell), XtWindow(myShell), cbd->sel_atom,
2411 timestamp_atom, 32, PropModeAppend, NULL, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 return FAIL;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002413 /* Flush is required in a terminal as nothing else is doing it. */
2414 XFlush(XtDisplay(myShell));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 return OK;
2416}
2417
2418/*
2419 * Send the current selection to the clipboard. Do nothing for X because we
2420 * will fill in the selection only when requested by another app.
2421 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 void
2423clip_x11_set_selection(cbd)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002424 VimClipboard *cbd UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425{
2426}
2427#endif
2428
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002429#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) \
2430 || defined(FEAT_GUI_GTK) || defined(PROTO)
2431/*
2432 * Get the contents of the X CUT_BUFFER0 and put it in "cbd".
2433 */
2434 void
2435yank_cut_buffer0(dpy, cbd)
2436 Display *dpy;
2437 VimClipboard *cbd;
2438{
2439 int nbytes = 0;
2440 char_u *buffer = (char_u *)XFetchBuffer(dpy, &nbytes, 0);
2441
2442 if (nbytes > 0)
2443 {
2444#ifdef FEAT_MBYTE
2445 int done = FALSE;
2446
2447 /* CUT_BUFFER0 is supposed to be always latin1. Convert to 'enc' when
2448 * using a multi-byte encoding. Conversion between two 8-bit
2449 * character sets usually fails and the text might actually be in
2450 * 'enc' anyway. */
2451 if (has_mbyte)
2452 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002453 char_u *conv_buf;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002454 vimconv_T vc;
2455
2456 vc.vc_type = CONV_NONE;
2457 if (convert_setup(&vc, (char_u *)"latin1", p_enc) == OK)
2458 {
2459 conv_buf = string_convert(&vc, buffer, &nbytes);
2460 if (conv_buf != NULL)
2461 {
2462 clip_yank_selection(MCHAR, conv_buf, (long)nbytes, cbd);
2463 vim_free(conv_buf);
2464 done = TRUE;
2465 }
2466 convert_setup(&vc, NULL, NULL);
2467 }
2468 }
2469 if (!done) /* use the text without conversion */
2470#endif
2471 clip_yank_selection(MCHAR, buffer, (long)nbytes, cbd);
2472 XFree((void *)buffer);
2473 if (p_verbose > 0)
2474 {
2475 verbose_enter();
2476 verb_msg((char_u *)_("Used CUT_BUFFER0 instead of empty selection"));
2477 verbose_leave();
2478 }
2479 }
2480}
2481#endif
2482
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483#if defined(FEAT_MOUSE) || defined(PROTO)
2484
2485/*
2486 * Move the cursor to the specified row and column on the screen.
Bram Moolenaar49325942007-05-10 19:19:59 +00002487 * Change current window if necessary. Returns an integer with the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 * CURSOR_MOVED bit set if the cursor has moved or unset otherwise.
2489 *
2490 * The MOUSE_FOLD_CLOSE bit is set when clicked on the '-' in a fold column.
2491 * The MOUSE_FOLD_OPEN bit is set when clicked on the '+' in a fold column.
2492 *
2493 * If flags has MOUSE_FOCUS, then the current window will not be changed, and
2494 * if the mouse is outside the window then the text will scroll, or if the
2495 * mouse was previously on a status line, then the status line may be dragged.
2496 *
2497 * If flags has MOUSE_MAY_VIS, then VIsual mode will be started before the
2498 * cursor is moved unless the cursor was on a status line.
2499 * This function returns one of IN_UNKNOWN, IN_BUFFER, IN_STATUS_LINE or
2500 * IN_SEP_LINE depending on where the cursor was clicked.
2501 *
2502 * If flags has MOUSE_MAY_STOP_VIS, then Visual mode will be stopped, unless
2503 * the mouse is on the status line of the same window.
2504 *
2505 * If flags has MOUSE_DID_MOVE, nothing is done if the mouse didn't move since
2506 * the last call.
2507 *
2508 * If flags has MOUSE_SETPOS, nothing is done, only the current position is
2509 * remembered.
2510 */
2511 int
2512jump_to_mouse(flags, inclusive, which_button)
2513 int flags;
2514 int *inclusive; /* used for inclusive operator, can be NULL */
2515 int which_button; /* MOUSE_LEFT, MOUSE_RIGHT, MOUSE_MIDDLE */
2516{
2517 static int on_status_line = 0; /* #lines below bottom of window */
2518#ifdef FEAT_VERTSPLIT
2519 static int on_sep_line = 0; /* on separator right of window */
2520#endif
2521 static int prev_row = -1;
2522 static int prev_col = -1;
2523 static win_T *dragwin = NULL; /* window being dragged */
2524 static int did_drag = FALSE; /* drag was noticed */
2525
2526 win_T *wp, *old_curwin;
2527 pos_T old_cursor;
2528 int count;
2529 int first;
2530 int row = mouse_row;
2531 int col = mouse_col;
2532#ifdef FEAT_FOLDING
2533 int mouse_char;
2534#endif
2535
2536 mouse_past_bottom = FALSE;
2537 mouse_past_eol = FALSE;
2538
2539 if (flags & MOUSE_RELEASED)
2540 {
2541 /* On button release we may change window focus if positioned on a
2542 * status line and no dragging happened. */
2543 if (dragwin != NULL && !did_drag)
2544 flags &= ~(MOUSE_FOCUS | MOUSE_DID_MOVE);
2545 dragwin = NULL;
2546 did_drag = FALSE;
2547 }
2548
2549 if ((flags & MOUSE_DID_MOVE)
2550 && prev_row == mouse_row
2551 && prev_col == mouse_col)
2552 {
2553retnomove:
Bram Moolenaar49325942007-05-10 19:19:59 +00002554 /* before moving the cursor for a left click which is NOT in a status
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 * line, stop Visual mode */
2556 if (on_status_line)
2557 return IN_STATUS_LINE;
2558#ifdef FEAT_VERTSPLIT
2559 if (on_sep_line)
2560 return IN_SEP_LINE;
2561#endif
2562#ifdef FEAT_VISUAL
2563 if (flags & MOUSE_MAY_STOP_VIS)
2564 {
2565 end_visual_mode();
2566 redraw_curbuf_later(INVERTED); /* delete the inversion */
2567 }
2568#endif
2569#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2570 /* Continue a modeless selection in another window. */
2571 if (cmdwin_type != 0 && row < W_WINROW(curwin))
2572 return IN_OTHER_WIN;
2573#endif
2574 return IN_BUFFER;
2575 }
2576
2577 prev_row = mouse_row;
2578 prev_col = mouse_col;
2579
2580 if (flags & MOUSE_SETPOS)
2581 goto retnomove; /* ugly goto... */
2582
2583#ifdef FEAT_FOLDING
2584 /* Remember the character under the mouse, it might be a '-' or '+' in the
2585 * fold column. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002586 if (row >= 0 && row < Rows && col >= 0 && col <= Columns
2587 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 mouse_char = ScreenLines[LineOffset[row] + col];
2589 else
2590 mouse_char = ' ';
2591#endif
2592
2593 old_curwin = curwin;
2594 old_cursor = curwin->w_cursor;
2595
2596 if (!(flags & MOUSE_FOCUS))
2597 {
2598 if (row < 0 || col < 0) /* check if it makes sense */
2599 return IN_UNKNOWN;
2600
2601#ifdef FEAT_WINDOWS
2602 /* find the window where the row is in */
2603 wp = mouse_find_win(&row, &col);
2604#else
2605 wp = firstwin;
2606#endif
2607 dragwin = NULL;
2608 /*
2609 * winpos and height may change in win_enter()!
2610 */
2611 if (row >= wp->w_height) /* In (or below) status line */
2612 {
2613 on_status_line = row - wp->w_height + 1;
2614 dragwin = wp;
2615 }
2616 else
2617 on_status_line = 0;
2618#ifdef FEAT_VERTSPLIT
2619 if (col >= wp->w_width) /* In separator line */
2620 {
2621 on_sep_line = col - wp->w_width + 1;
2622 dragwin = wp;
2623 }
2624 else
2625 on_sep_line = 0;
2626
2627 /* The rightmost character of the status line might be a vertical
2628 * separator character if there is no connecting window to the right. */
2629 if (on_status_line && on_sep_line)
2630 {
2631 if (stl_connected(wp))
2632 on_sep_line = 0;
2633 else
2634 on_status_line = 0;
2635 }
2636#endif
2637
2638#ifdef FEAT_VISUAL
2639 /* Before jumping to another buffer, or moving the cursor for a left
2640 * click, stop Visual mode. */
2641 if (VIsual_active
2642 && (wp->w_buffer != curwin->w_buffer
2643 || (!on_status_line
2644# ifdef FEAT_VERTSPLIT
2645 && !on_sep_line
2646# endif
2647# ifdef FEAT_FOLDING
2648 && (
2649# ifdef FEAT_RIGHTLEFT
2650 wp->w_p_rl ? col < W_WIDTH(wp) - wp->w_p_fdc :
2651# endif
2652 col >= wp->w_p_fdc
2653# ifdef FEAT_CMDWIN
2654 + (cmdwin_type == 0 && wp == curwin ? 0 : 1)
2655# endif
2656 )
2657# endif
2658 && (flags & MOUSE_MAY_STOP_VIS))))
2659 {
2660 end_visual_mode();
2661 redraw_curbuf_later(INVERTED); /* delete the inversion */
2662 }
2663#endif
2664#ifdef FEAT_CMDWIN
2665 if (cmdwin_type != 0 && wp != curwin)
2666 {
2667 /* A click outside the command-line window: Use modeless
Bram Moolenaarf679a432010-03-02 18:16:09 +01002668 * selection if possible. Allow dragging the status lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669# ifdef FEAT_VERTSPLIT
2670 on_sep_line = 0;
2671# endif
2672# ifdef FEAT_CLIPBOARD
2673 if (on_status_line)
2674 return IN_STATUS_LINE;
2675 return IN_OTHER_WIN;
2676# else
2677 row = 0;
2678 col += wp->w_wincol;
2679 wp = curwin;
2680# endif
2681 }
2682#endif
2683#ifdef FEAT_WINDOWS
2684 /* Only change window focus when not clicking on or dragging the
2685 * status line. Do change focus when releasing the mouse button
2686 * (MOUSE_FOCUS was set above if we dragged first). */
2687 if (dragwin == NULL || (flags & MOUSE_RELEASED))
2688 win_enter(wp, TRUE); /* can make wp invalid! */
2689# ifdef CHECK_DOUBLE_CLICK
2690 /* set topline, to be able to check for double click ourselves */
2691 if (curwin != old_curwin)
2692 set_mouse_topline(curwin);
2693# endif
2694#endif
2695 if (on_status_line) /* In (or below) status line */
2696 {
2697 /* Don't use start_arrow() if we're in the same window */
2698 if (curwin == old_curwin)
2699 return IN_STATUS_LINE;
2700 else
2701 return IN_STATUS_LINE | CURSOR_MOVED;
2702 }
2703#ifdef FEAT_VERTSPLIT
2704 if (on_sep_line) /* In (or below) status line */
2705 {
2706 /* Don't use start_arrow() if we're in the same window */
2707 if (curwin == old_curwin)
2708 return IN_SEP_LINE;
2709 else
2710 return IN_SEP_LINE | CURSOR_MOVED;
2711 }
2712#endif
2713
2714 curwin->w_cursor.lnum = curwin->w_topline;
2715#ifdef FEAT_GUI
2716 /* remember topline, needed for double click */
2717 gui_prev_topline = curwin->w_topline;
2718# ifdef FEAT_DIFF
2719 gui_prev_topfill = curwin->w_topfill;
2720# endif
2721#endif
2722 }
2723 else if (on_status_line && which_button == MOUSE_LEFT)
2724 {
2725#ifdef FEAT_WINDOWS
2726 if (dragwin != NULL)
2727 {
2728 /* Drag the status line */
2729 count = row - dragwin->w_winrow - dragwin->w_height + 1
2730 - on_status_line;
2731 win_drag_status_line(dragwin, count);
2732 did_drag |= count;
2733 }
2734#endif
2735 return IN_STATUS_LINE; /* Cursor didn't move */
2736 }
2737#ifdef FEAT_VERTSPLIT
2738 else if (on_sep_line && which_button == MOUSE_LEFT)
2739 {
2740 if (dragwin != NULL)
2741 {
2742 /* Drag the separator column */
2743 count = col - dragwin->w_wincol - dragwin->w_width + 1
2744 - on_sep_line;
2745 win_drag_vsep_line(dragwin, count);
2746 did_drag |= count;
2747 }
2748 return IN_SEP_LINE; /* Cursor didn't move */
2749 }
2750#endif
2751 else /* keep_window_focus must be TRUE */
2752 {
2753#ifdef FEAT_VISUAL
2754 /* before moving the cursor for a left click, stop Visual mode */
2755 if (flags & MOUSE_MAY_STOP_VIS)
2756 {
2757 end_visual_mode();
2758 redraw_curbuf_later(INVERTED); /* delete the inversion */
2759 }
2760#endif
2761
2762#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2763 /* Continue a modeless selection in another window. */
2764 if (cmdwin_type != 0 && row < W_WINROW(curwin))
2765 return IN_OTHER_WIN;
2766#endif
2767
2768 row -= W_WINROW(curwin);
2769#ifdef FEAT_VERTSPLIT
2770 col -= W_WINCOL(curwin);
2771#endif
2772
2773 /*
2774 * When clicking beyond the end of the window, scroll the screen.
2775 * Scroll by however many rows outside the window we are.
2776 */
2777 if (row < 0)
2778 {
2779 count = 0;
2780 for (first = TRUE; curwin->w_topline > 1; )
2781 {
2782#ifdef FEAT_DIFF
2783 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
2784 ++count;
2785 else
2786#endif
2787 count += plines(curwin->w_topline - 1);
2788 if (!first && count > -row)
2789 break;
2790 first = FALSE;
2791#ifdef FEAT_FOLDING
2792 hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2793#endif
2794#ifdef FEAT_DIFF
2795 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
2796 ++curwin->w_topfill;
2797 else
2798#endif
2799 {
2800 --curwin->w_topline;
2801#ifdef FEAT_DIFF
2802 curwin->w_topfill = 0;
2803#endif
2804 }
2805 }
2806#ifdef FEAT_DIFF
2807 check_topfill(curwin, FALSE);
2808#endif
2809 curwin->w_valid &=
2810 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2811 redraw_later(VALID);
2812 row = 0;
2813 }
2814 else if (row >= curwin->w_height)
2815 {
2816 count = 0;
2817 for (first = TRUE; curwin->w_topline < curbuf->b_ml.ml_line_count; )
2818 {
2819#ifdef FEAT_DIFF
2820 if (curwin->w_topfill > 0)
2821 ++count;
2822 else
2823#endif
2824 count += plines(curwin->w_topline);
2825 if (!first && count > row - curwin->w_height + 1)
2826 break;
2827 first = FALSE;
2828#ifdef FEAT_FOLDING
2829 if (hasFolding(curwin->w_topline, NULL, &curwin->w_topline)
2830 && curwin->w_topline == curbuf->b_ml.ml_line_count)
2831 break;
2832#endif
2833#ifdef FEAT_DIFF
2834 if (curwin->w_topfill > 0)
2835 --curwin->w_topfill;
2836 else
2837#endif
2838 {
2839 ++curwin->w_topline;
2840#ifdef FEAT_DIFF
2841 curwin->w_topfill =
2842 diff_check_fill(curwin, curwin->w_topline);
2843#endif
2844 }
2845 }
2846#ifdef FEAT_DIFF
2847 check_topfill(curwin, FALSE);
2848#endif
2849 redraw_later(VALID);
2850 curwin->w_valid &=
2851 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2852 row = curwin->w_height - 1;
2853 }
2854 else if (row == 0)
2855 {
2856 /* When dragging the mouse, while the text has been scrolled up as
2857 * far as it goes, moving the mouse in the top line should scroll
2858 * the text down (done later when recomputing w_topline). */
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00002859 if (mouse_dragging > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 && curwin->w_cursor.lnum
2861 == curwin->w_buffer->b_ml.ml_line_count
2862 && curwin->w_cursor.lnum == curwin->w_topline)
2863 curwin->w_valid &= ~(VALID_TOPLINE);
2864 }
2865 }
2866
2867#ifdef FEAT_FOLDING
2868 /* Check for position outside of the fold column. */
2869 if (
2870# ifdef FEAT_RIGHTLEFT
2871 curwin->w_p_rl ? col < W_WIDTH(curwin) - curwin->w_p_fdc :
2872# endif
2873 col >= curwin->w_p_fdc
2874# ifdef FEAT_CMDWIN
2875 + (cmdwin_type == 0 ? 0 : 1)
2876# endif
2877 )
2878 mouse_char = ' ';
2879#endif
2880
2881 /* compute the position in the buffer line from the posn on the screen */
2882 if (mouse_comp_pos(curwin, &row, &col, &curwin->w_cursor.lnum))
2883 mouse_past_bottom = TRUE;
2884
2885#ifdef FEAT_VISUAL
2886 /* Start Visual mode before coladvance(), for when 'sel' != "old" */
2887 if ((flags & MOUSE_MAY_VIS) && !VIsual_active)
2888 {
2889 check_visual_highlight();
2890 VIsual = old_cursor;
2891 VIsual_active = TRUE;
2892 VIsual_reselect = TRUE;
2893 /* if 'selectmode' contains "mouse", start Select mode */
2894 may_start_select('o');
2895 setmouse();
Bram Moolenaar7df351e2006-01-23 22:30:28 +00002896 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 redraw_cmdline = TRUE; /* show visual mode later */
2898 }
2899#endif
2900
2901 curwin->w_curswant = col;
2902 curwin->w_set_curswant = FALSE; /* May still have been TRUE */
2903 if (coladvance(col) == FAIL) /* Mouse click beyond end of line */
2904 {
2905 if (inclusive != NULL)
2906 *inclusive = TRUE;
2907 mouse_past_eol = TRUE;
2908 }
2909 else if (inclusive != NULL)
2910 *inclusive = FALSE;
2911
2912 count = IN_BUFFER;
2913 if (curwin != old_curwin || curwin->w_cursor.lnum != old_cursor.lnum
2914 || curwin->w_cursor.col != old_cursor.col)
2915 count |= CURSOR_MOVED; /* Cursor has moved */
2916
2917#ifdef FEAT_FOLDING
2918 if (mouse_char == '+')
2919 count |= MOUSE_FOLD_OPEN;
2920 else if (mouse_char != ' ')
2921 count |= MOUSE_FOLD_CLOSE;
2922#endif
2923
2924 return count;
2925}
2926
2927/*
2928 * Compute the position in the buffer line from the posn on the screen in
2929 * window "win".
2930 * Returns TRUE if the position is below the last line.
2931 */
2932 int
2933mouse_comp_pos(win, rowp, colp, lnump)
2934 win_T *win;
2935 int *rowp;
2936 int *colp;
2937 linenr_T *lnump;
2938{
2939 int col = *colp;
2940 int row = *rowp;
2941 linenr_T lnum;
2942 int retval = FALSE;
2943 int off;
2944 int count;
2945
2946#ifdef FEAT_RIGHTLEFT
2947 if (win->w_p_rl)
2948 col = W_WIDTH(win) - 1 - col;
2949#endif
2950
2951 lnum = win->w_topline;
2952
2953 while (row > 0)
2954 {
2955#ifdef FEAT_DIFF
2956 /* Don't include filler lines in "count" */
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002957 if (win->w_p_diff
2958# ifdef FEAT_FOLDING
2959 && !hasFoldingWin(win, lnum, NULL, NULL, TRUE, NULL)
2960# endif
2961 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 {
2963 if (lnum == win->w_topline)
2964 row -= win->w_topfill;
2965 else
2966 row -= diff_check_fill(win, lnum);
2967 count = plines_win_nofill(win, lnum, TRUE);
2968 }
2969 else
2970#endif
2971 count = plines_win(win, lnum, TRUE);
2972 if (count > row)
2973 break; /* Position is in this buffer line. */
2974#ifdef FEAT_FOLDING
2975 (void)hasFoldingWin(win, lnum, NULL, &lnum, TRUE, NULL);
2976#endif
2977 if (lnum == win->w_buffer->b_ml.ml_line_count)
2978 {
2979 retval = TRUE;
2980 break; /* past end of file */
2981 }
2982 row -= count;
2983 ++lnum;
2984 }
2985
2986 if (!retval)
2987 {
2988 /* Compute the column without wrapping. */
2989 off = win_col_off(win) - win_col_off2(win);
2990 if (col < off)
2991 col = off;
2992 col += row * (W_WIDTH(win) - off);
2993 /* add skip column (for long wrapping line) */
2994 col += win->w_skipcol;
2995 }
2996
2997 if (!win->w_p_wrap)
2998 col += win->w_leftcol;
2999
3000 /* skip line number and fold column in front of the line */
3001 col -= win_col_off(win);
3002 if (col < 0)
3003 {
3004#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003005 netbeans_gutter_click(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006#endif
3007 col = 0;
3008 }
3009
3010 *colp = col;
3011 *rowp = row;
3012 *lnump = lnum;
3013 return retval;
3014}
3015
3016#if defined(FEAT_WINDOWS) || defined(PROTO)
3017/*
3018 * Find the window at screen position "*rowp" and "*colp". The positions are
3019 * updated to become relative to the top-left of the window.
3020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 win_T *
3022mouse_find_win(rowp, colp)
3023 int *rowp;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003024 int *colp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025{
3026 frame_T *fp;
3027
3028 fp = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003029 *rowp -= firstwin->w_winrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 for (;;)
3031 {
3032 if (fp->fr_layout == FR_LEAF)
3033 break;
3034#ifdef FEAT_VERTSPLIT
3035 if (fp->fr_layout == FR_ROW)
3036 {
3037 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
3038 {
3039 if (*colp < fp->fr_width)
3040 break;
3041 *colp -= fp->fr_width;
3042 }
3043 }
3044#endif
3045 else /* fr_layout == FR_COL */
3046 {
3047 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
3048 {
3049 if (*rowp < fp->fr_height)
3050 break;
3051 *rowp -= fp->fr_height;
3052 }
3053 }
3054 }
3055 return fp->fr_win;
3056}
3057#endif
3058
Bram Moolenaar860cae12010-06-05 23:22:07 +02003059#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
3061 || defined(FEAT_GUI_PHOTON) || defined(PROTO)
3062/*
3063 * Translate window coordinates to buffer position without any side effects
3064 */
3065 int
3066get_fpos_of_mouse(mpos)
3067 pos_T *mpos;
3068{
3069 win_T *wp;
3070 int row = mouse_row;
3071 int col = mouse_col;
3072
3073 if (row < 0 || col < 0) /* check if it makes sense */
3074 return IN_UNKNOWN;
3075
3076#ifdef FEAT_WINDOWS
3077 /* find the window where the row is in */
3078 wp = mouse_find_win(&row, &col);
3079#else
3080 wp = firstwin;
3081#endif
3082 /*
3083 * winpos and height may change in win_enter()!
3084 */
3085 if (row >= wp->w_height) /* In (or below) status line */
3086 return IN_STATUS_LINE;
3087#ifdef FEAT_VERTSPLIT
3088 if (col >= wp->w_width) /* In vertical separator line */
3089 return IN_SEP_LINE;
3090#endif
3091
3092 if (wp != curwin)
3093 return IN_UNKNOWN;
3094
3095 /* compute the position in the buffer line from the posn on the screen */
3096 if (mouse_comp_pos(curwin, &row, &col, &mpos->lnum))
3097 return IN_STATUS_LINE; /* past bottom */
3098
3099 mpos->col = vcol2col(wp, mpos->lnum, col);
3100
3101 if (mpos->col > 0)
3102 --mpos->col;
3103 return IN_BUFFER;
3104}
3105
3106/*
3107 * Convert a virtual (screen) column to a character column.
3108 * The first column is one.
3109 */
3110 int
3111vcol2col(wp, lnum, vcol)
3112 win_T *wp;
3113 linenr_T lnum;
3114 int vcol;
3115{
3116 /* try to advance to the specified column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 int count = 0;
3118 char_u *ptr;
Bram Moolenaar86c800a2009-09-11 14:48:27 +00003119 char_u *start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120
Bram Moolenaar86c800a2009-09-11 14:48:27 +00003121 start = ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 while (count <= vcol && *ptr != NUL)
3123 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 count += win_lbr_chartabsize(wp, ptr, count, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003125 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 }
Bram Moolenaar86c800a2009-09-11 14:48:27 +00003127 return (int)(ptr - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128}
3129#endif
3130
3131#endif /* FEAT_MOUSE */
3132
3133#if defined(FEAT_GUI) || defined(WIN3264) || defined(PROTO)
3134/*
3135 * Called when focus changed. Used for the GUI or for systems where this can
3136 * be done in the console (Win32).
3137 */
3138 void
3139ui_focus_change(in_focus)
3140 int in_focus; /* TRUE if focus gained. */
3141{
3142 static time_t last_time = (time_t)0;
3143 int need_redraw = FALSE;
3144
3145 /* When activated: Check if any file was modified outside of Vim.
3146 * Only do this when not done within the last two seconds (could get
3147 * several events in a row). */
3148 if (in_focus && last_time + 2 < time(NULL))
3149 {
3150 need_redraw = check_timestamps(
3151# ifdef FEAT_GUI
3152 gui.in_use
3153# else
3154 FALSE
3155# endif
3156 );
3157 last_time = time(NULL);
3158 }
3159
3160#ifdef FEAT_AUTOCMD
3161 /*
3162 * Fire the focus gained/lost autocommand.
3163 */
3164 need_redraw |= apply_autocmds(in_focus ? EVENT_FOCUSGAINED
3165 : EVENT_FOCUSLOST, NULL, NULL, FALSE, curbuf);
3166#endif
3167
3168 if (need_redraw)
3169 {
3170 /* Something was executed, make sure the cursor is put back where it
3171 * belongs. */
3172 need_wait_return = FALSE;
3173
3174 if (State & CMDLINE)
3175 redrawcmdline();
3176 else if (State == HITRETURN || State == SETWSIZE || State == ASKMORE
3177 || State == EXTERNCMD || State == CONFIRM || exmode_active)
3178 repeat_message();
3179 else if ((State & NORMAL) || (State & INSERT))
3180 {
3181 if (must_redraw != 0)
3182 update_screen(0);
3183 setcursor();
3184 }
3185 cursor_on(); /* redrawing may have switched it off */
3186 out_flush();
3187# ifdef FEAT_GUI
3188 if (gui.in_use)
3189 {
3190 gui_update_cursor(FALSE, TRUE);
3191 gui_update_scrollbars(FALSE);
3192 }
3193# endif
3194 }
3195#ifdef FEAT_TITLE
3196 /* File may have been changed from 'readonly' to 'noreadonly' */
3197 if (need_maketitle)
3198 maketitle();
3199#endif
3200}
3201#endif
3202
3203#if defined(USE_IM_CONTROL) || defined(PROTO)
3204/*
3205 * Save current Input Method status to specified place.
3206 */
3207 void
3208im_save_status(psave)
3209 long *psave;
3210{
3211 /* Don't save when 'imdisable' is set or "xic" is NULL, IM is always
3212 * disabled then (but might start later).
3213 * Also don't save when inside a mapping, vgetc_im_active has not been set
3214 * then.
3215 * And don't save when the keys were stuffed (e.g., for a "." command).
3216 * And don't save when the GUI is running but our window doesn't have
3217 * input focus (e.g., when a find dialog is open). */
3218 if (!p_imdisable && KeyTyped && !KeyStuffed
3219# ifdef FEAT_XIM
3220 && xic != NULL
3221# endif
3222# ifdef FEAT_GUI
3223 && (!gui.in_use || gui.in_focus)
3224# endif
3225 )
3226 {
3227 /* Do save when IM is on, or IM is off and saved status is on. */
3228 if (vgetc_im_active)
3229 *psave = B_IMODE_IM;
3230 else if (*psave == B_IMODE_IM)
3231 *psave = B_IMODE_NONE;
3232 }
3233}
3234#endif