blob: 89ab747da25042f8c650144ed18d91fb277c971a [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 Moolenaar4b9669f2011-07-07 16:20:52 +020061#if defined(UNIX) || defined(VMS) || defined(PROTO) || defined(WIN3264)
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/*
Bram Moolenaarb6101cf2012-10-21 00:58:39 +0200101 * ui_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102 * 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)
Bram Moolenaar8968a312013-07-03 16:58:44 +0200329 gui_set_shellsize(mustset, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 else
331#endif
332 mch_set_shellsize();
333}
334
335/*
336 * Called when Rows and/or Columns changed. Adjust scroll region and mouse
337 * region.
338 */
339 void
340ui_new_shellsize()
341{
342 if (full_screen && !exiting)
343 {
344#ifdef FEAT_GUI
345 if (gui.in_use)
346 gui_new_shellsize();
347 else
348#endif
349 mch_new_shellsize();
350 }
351}
352
353 void
354ui_breakcheck()
355{
356#ifdef FEAT_GUI
357 if (gui.in_use)
358 gui_mch_update();
359 else
360#endif
361 mch_breakcheck();
362}
363
364/*****************************************************************************
365 * Functions for copying and pasting text between applications.
366 * This is always included in a GUI version, but may also be included when the
367 * clipboard and mouse is available to a terminal version such as xterm.
368 * Note: there are some more functions in ops.c that handle selection stuff.
369 *
370 * Also note that the majority of functions here deal with the X 'primary'
371 * (visible - for Visual mode use) selection, and only that. There are no
372 * versions of these for the 'clipboard' selection, as Visual mode has no use
373 * for them.
374 */
375
376#if defined(FEAT_CLIPBOARD) || defined(PROTO)
377
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200378static void clip_copy_selection __ARGS((VimClipboard *clip));
379
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380/*
381 * Selection stuff using Visual mode, for cutting and pasting text to other
382 * windows.
383 */
384
385/*
386 * Call this to initialise the clipboard. Pass it FALSE if the clipboard code
387 * is included, but the clipboard can not be used, or TRUE if the clipboard can
388 * be used. Eg unix may call this with FALSE, then call it again with TRUE if
389 * the GUI starts.
390 */
391 void
392clip_init(can_use)
393 int can_use;
394{
395 VimClipboard *cb;
396
397 cb = &clip_star;
398 for (;;)
399 {
400 cb->available = can_use;
401 cb->owned = FALSE;
402 cb->start.lnum = 0;
403 cb->start.col = 0;
404 cb->end.lnum = 0;
405 cb->end.col = 0;
406 cb->state = SELECT_CLEARED;
407
408 if (cb == &clip_plus)
409 break;
410 cb = &clip_plus;
411 }
412}
413
414/*
415 * Check whether the VIsual area has changed, and if so try to become the owner
416 * of the selection, and free any old converted selection we may still have
417 * lying around. If the VIsual mode has ended, make a copy of what was
418 * selected so we can still give it to others. Will probably have to make sure
419 * this is called whenever VIsual mode is ended.
420 */
421 void
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200422clip_update_selection(clip)
423 VimClipboard *clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424{
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200425 pos_T start, end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426
427 /* If visual mode is only due to a redo command ("."), then ignore it */
428 if (!redo_VIsual_busy && VIsual_active && (State & NORMAL))
429 {
430 if (lt(VIsual, curwin->w_cursor))
431 {
432 start = VIsual;
433 end = curwin->w_cursor;
434#ifdef FEAT_MBYTE
435 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000436 end.col += (*mb_ptr2len)(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437#endif
438 }
439 else
440 {
441 start = curwin->w_cursor;
442 end = VIsual;
443 }
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200444 if (!equalpos(clip->start, start)
445 || !equalpos(clip->end, end)
446 || clip->vmode != VIsual_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200448 clip_clear_selection(clip);
449 clip->start = start;
450 clip->end = end;
451 clip->vmode = VIsual_mode;
452 clip_free_selection(clip);
453 clip_own_selection(clip);
454 clip_gen_set_selection(clip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 }
456 }
457}
458
459 void
460clip_own_selection(cbd)
461 VimClipboard *cbd;
462{
463 /*
464 * Also want to check somehow that we are reading from the keyboard rather
465 * than a mapping etc.
466 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467#ifdef FEAT_X11
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200468 /* Always own the selection, we might have lost it without being
Bram Moolenaar62b42182010-09-21 22:09:37 +0200469 * notified, e.g. during a ":sh" command. */
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200470 if (cbd->available)
471 {
472 int was_owned = cbd->owned;
473
474 cbd->owned = (clip_gen_own_selection(cbd) == OK);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200475 if (!was_owned && (cbd == &clip_star || cbd == &clip_plus))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 {
Bram Moolenaarebefac62005-12-28 22:39:57 +0000477 /* May have to show a different kind of highlighting for the
478 * selected area. There is no specific redraw command for this,
479 * just redraw all windows on the current buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 if (cbd->owned
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000481 && (get_real_state() == VISUAL
482 || get_real_state() == SELECTMODE)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200483 && (cbd == &clip_star ? clip_isautosel_star()
484 : clip_isautosel_plus())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 && hl_attr(HLF_V) != hl_attr(HLF_VNC))
486 redraw_curbuf_later(INVERTED_ALL);
487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200489#else
Bram Moolenaarb6101cf2012-10-21 00:58:39 +0200490 /* Only own the clipboard when we didn't own it yet. */
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200491 if (!cbd->owned && cbd->available)
492 cbd->owned = (clip_gen_own_selection(cbd) == OK);
493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494}
495
496 void
497clip_lose_selection(cbd)
498 VimClipboard *cbd;
499{
500#ifdef FEAT_X11
501 int was_owned = cbd->owned;
502#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200503 int visual_selection = FALSE;
504
505 if (cbd == &clip_star || cbd == &clip_plus)
506 visual_selection = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507
508 clip_free_selection(cbd);
509 cbd->owned = FALSE;
510 if (visual_selection)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200511 clip_clear_selection(cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 clip_gen_lose_selection(cbd);
513#ifdef FEAT_X11
514 if (visual_selection)
515 {
516 /* May have to show a different kind of highlighting for the selected
517 * area. There is no specific redraw command for this, just redraw all
518 * windows on the current buffer. */
519 if (was_owned
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000520 && (get_real_state() == VISUAL
521 || get_real_state() == SELECTMODE)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200522 && (cbd == &clip_star ?
523 clip_isautosel_star() : clip_isautosel_plus())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 && hl_attr(HLF_V) != hl_attr(HLF_VNC))
525 {
526 update_curbuf(INVERTED_ALL);
527 setcursor();
528 cursor_on();
529 out_flush();
530# ifdef FEAT_GUI
531 if (gui.in_use)
532 gui_update_cursor(TRUE, FALSE);
533# endif
534 }
535 }
536#endif
537}
538
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200539 static void
540clip_copy_selection(clip)
541 VimClipboard *clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542{
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200543 if (VIsual_active && (State & NORMAL) && clip->available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200545 clip_update_selection(clip);
546 clip_free_selection(clip);
547 clip_own_selection(clip);
548 if (clip->owned)
549 clip_get_selection(clip);
550 clip_gen_set_selection(clip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 }
552}
553
554/*
555 * Called when Visual mode is ended: update the selection.
556 */
557 void
558clip_auto_select()
559{
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200560 if (clip_isautosel_star())
561 clip_copy_selection(&clip_star);
562 if (clip_isautosel_plus())
563 clip_copy_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564}
565
566/*
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200567 * Return TRUE if automatic selection of Visual area is desired for the *
568 * register.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 */
570 int
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200571clip_isautosel_star()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572{
573 return (
574#ifdef FEAT_GUI
575 gui.in_use ? (vim_strchr(p_go, GO_ASEL) != NULL) :
576#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200577 clip_autoselect_star);
578}
579
580/*
581 * Return TRUE if automatic selection of Visual area is desired for the +
582 * register.
583 */
584 int
585clip_isautosel_plus()
586{
587 return (
588#ifdef FEAT_GUI
589 gui.in_use ? (vim_strchr(p_go, GO_ASELPLUS) != NULL) :
590#endif
591 clip_autoselect_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592}
593
594
595/*
596 * Stuff for general mouse selection, without using Visual mode.
597 */
598
599static int clip_compare_pos __ARGS((int row1, int col1, int row2, int col2));
600static void clip_invert_area __ARGS((int, int, int, int, int how));
601static void clip_invert_rectangle __ARGS((int row, int col, int height, int width, int invert));
602static void clip_get_word_boundaries __ARGS((VimClipboard *, int, int));
603static int clip_get_line_end __ARGS((int));
604static void clip_update_modeless_selection __ARGS((VimClipboard *, int, int,
605 int, int));
606
607/* flags for clip_invert_area() */
608#define CLIP_CLEAR 1
609#define CLIP_SET 2
610#define CLIP_TOGGLE 3
611
612/*
613 * Start, continue or end a modeless selection. Used when editing the
614 * command-line and in the cmdline window.
615 */
616 void
617clip_modeless(button, is_click, is_drag)
618 int button;
619 int is_click;
620 int is_drag;
621{
622 int repeat;
623
624 repeat = ((clip_star.mode == SELECT_MODE_CHAR
625 || clip_star.mode == SELECT_MODE_LINE)
626 && (mod_mask & MOD_MASK_2CLICK))
627 || (clip_star.mode == SELECT_MODE_WORD
628 && (mod_mask & MOD_MASK_3CLICK));
629 if (is_click && button == MOUSE_RIGHT)
630 {
631 /* Right mouse button: If there was no selection, start one.
632 * Otherwise extend the existing selection. */
633 if (clip_star.state == SELECT_CLEARED)
634 clip_start_selection(mouse_col, mouse_row, FALSE);
635 clip_process_selection(button, mouse_col, mouse_row, repeat);
636 }
637 else if (is_click)
638 clip_start_selection(mouse_col, mouse_row, repeat);
639 else if (is_drag)
640 {
641 /* Don't try extending a selection if there isn't one. Happens when
642 * button-down is in the cmdline and them moving mouse upwards. */
643 if (clip_star.state != SELECT_CLEARED)
644 clip_process_selection(button, mouse_col, mouse_row, repeat);
645 }
646 else /* release */
647 clip_process_selection(MOUSE_RELEASE, mouse_col, mouse_row, FALSE);
648}
649
650/*
651 * Compare two screen positions ala strcmp()
652 */
653 static int
654clip_compare_pos(row1, col1, row2, col2)
655 int row1;
656 int col1;
657 int row2;
658 int col2;
659{
660 if (row1 > row2) return(1);
661 if (row1 < row2) return(-1);
662 if (col1 > col2) return(1);
663 if (col1 < col2) return(-1);
664 return(0);
665}
666
667/*
668 * Start the selection
669 */
670 void
671clip_start_selection(col, row, repeated_click)
672 int col;
673 int row;
674 int repeated_click;
675{
676 VimClipboard *cb = &clip_star;
677
678 if (cb->state == SELECT_DONE)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200679 clip_clear_selection(cb);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680
681 row = check_row(row);
682 col = check_col(col);
683#ifdef FEAT_MBYTE
684 col = mb_fix_col(col, row);
685#endif
686
687 cb->start.lnum = row;
688 cb->start.col = col;
689 cb->end = cb->start;
690 cb->origin_row = (short_u)cb->start.lnum;
691 cb->state = SELECT_IN_PROGRESS;
692
693 if (repeated_click)
694 {
695 if (++cb->mode > SELECT_MODE_LINE)
696 cb->mode = SELECT_MODE_CHAR;
697 }
698 else
699 cb->mode = SELECT_MODE_CHAR;
700
701#ifdef FEAT_GUI
702 /* clear the cursor until the selection is made */
703 if (gui.in_use)
704 gui_undraw_cursor();
705#endif
706
707 switch (cb->mode)
708 {
709 case SELECT_MODE_CHAR:
710 cb->origin_start_col = cb->start.col;
711 cb->word_end_col = clip_get_line_end((int)cb->start.lnum);
712 break;
713
714 case SELECT_MODE_WORD:
715 clip_get_word_boundaries(cb, (int)cb->start.lnum, cb->start.col);
716 cb->origin_start_col = cb->word_start_col;
717 cb->origin_end_col = cb->word_end_col;
718
719 clip_invert_area((int)cb->start.lnum, cb->word_start_col,
720 (int)cb->end.lnum, cb->word_end_col, CLIP_SET);
721 cb->start.col = cb->word_start_col;
722 cb->end.col = cb->word_end_col;
723 break;
724
725 case SELECT_MODE_LINE:
726 clip_invert_area((int)cb->start.lnum, 0, (int)cb->start.lnum,
727 (int)Columns, CLIP_SET);
728 cb->start.col = 0;
729 cb->end.col = Columns;
730 break;
731 }
732
733 cb->prev = cb->start;
734
735#ifdef DEBUG_SELECTION
736 printf("Selection started at (%u,%u)\n", cb->start.lnum, cb->start.col);
737#endif
738}
739
740/*
741 * Continue processing the selection
742 */
743 void
744clip_process_selection(button, col, row, repeated_click)
745 int button;
746 int col;
747 int row;
748 int_u repeated_click;
749{
750 VimClipboard *cb = &clip_star;
751 int diff;
752 int slen = 1; /* cursor shape width */
753
754 if (button == MOUSE_RELEASE)
755 {
756 /* Check to make sure we have something selected */
757 if (cb->start.lnum == cb->end.lnum && cb->start.col == cb->end.col)
758 {
759#ifdef FEAT_GUI
760 if (gui.in_use)
761 gui_update_cursor(FALSE, FALSE);
762#endif
763 cb->state = SELECT_CLEARED;
764 return;
765 }
766
767#ifdef DEBUG_SELECTION
768 printf("Selection ended: (%u,%u) to (%u,%u)\n", cb->start.lnum,
769 cb->start.col, cb->end.lnum, cb->end.col);
770#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200771 if (clip_isautosel_star()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 || (
773#ifdef FEAT_GUI
774 gui.in_use ? (vim_strchr(p_go, GO_ASELML) != NULL) :
775#endif
776 clip_autoselectml))
777 clip_copy_modeless_selection(FALSE);
778#ifdef FEAT_GUI
779 if (gui.in_use)
780 gui_update_cursor(FALSE, FALSE);
781#endif
782
783 cb->state = SELECT_DONE;
784 return;
785 }
786
787 row = check_row(row);
788 col = check_col(col);
789#ifdef FEAT_MBYTE
790 col = mb_fix_col(col, row);
791#endif
792
793 if (col == (int)cb->prev.col && row == cb->prev.lnum && !repeated_click)
794 return;
795
796 /*
797 * When extending the selection with the right mouse button, swap the
798 * start and end if the position is before half the selection
799 */
800 if (cb->state == SELECT_DONE && button == MOUSE_RIGHT)
801 {
802 /*
803 * If the click is before the start, or the click is inside the
804 * selection and the start is the closest side, set the origin to the
805 * end of the selection.
806 */
807 if (clip_compare_pos(row, col, (int)cb->start.lnum, cb->start.col) < 0
808 || (clip_compare_pos(row, col,
809 (int)cb->end.lnum, cb->end.col) < 0
810 && (((cb->start.lnum == cb->end.lnum
811 && cb->end.col - col > col - cb->start.col))
812 || ((diff = (cb->end.lnum - row) -
813 (row - cb->start.lnum)) > 0
814 || (diff == 0 && col < (int)(cb->start.col +
815 cb->end.col) / 2)))))
816 {
817 cb->origin_row = (short_u)cb->end.lnum;
818 cb->origin_start_col = cb->end.col - 1;
819 cb->origin_end_col = cb->end.col;
820 }
821 else
822 {
823 cb->origin_row = (short_u)cb->start.lnum;
824 cb->origin_start_col = cb->start.col;
825 cb->origin_end_col = cb->start.col;
826 }
827 if (cb->mode == SELECT_MODE_WORD && !repeated_click)
828 cb->mode = SELECT_MODE_CHAR;
829 }
830
831 /* set state, for when using the right mouse button */
832 cb->state = SELECT_IN_PROGRESS;
833
834#ifdef DEBUG_SELECTION
835 printf("Selection extending to (%d,%d)\n", row, col);
836#endif
837
838 if (repeated_click && ++cb->mode > SELECT_MODE_LINE)
839 cb->mode = SELECT_MODE_CHAR;
840
841 switch (cb->mode)
842 {
843 case SELECT_MODE_CHAR:
844 /* If we're on a different line, find where the line ends */
845 if (row != cb->prev.lnum)
846 cb->word_end_col = clip_get_line_end(row);
847
848 /* See if we are before or after the origin of the selection */
849 if (clip_compare_pos(row, col, cb->origin_row,
850 cb->origin_start_col) >= 0)
851 {
852 if (col >= (int)cb->word_end_col)
853 clip_update_modeless_selection(cb, cb->origin_row,
854 cb->origin_start_col, row, (int)Columns);
855 else
856 {
857#ifdef FEAT_MBYTE
858 if (has_mbyte && mb_lefthalve(row, col))
859 slen = 2;
860#endif
861 clip_update_modeless_selection(cb, cb->origin_row,
862 cb->origin_start_col, row, col + slen);
863 }
864 }
865 else
866 {
867#ifdef FEAT_MBYTE
868 if (has_mbyte
869 && mb_lefthalve(cb->origin_row, cb->origin_start_col))
870 slen = 2;
871#endif
872 if (col >= (int)cb->word_end_col)
873 clip_update_modeless_selection(cb, row, cb->word_end_col,
874 cb->origin_row, cb->origin_start_col + slen);
875 else
876 clip_update_modeless_selection(cb, row, col,
877 cb->origin_row, cb->origin_start_col + slen);
878 }
879 break;
880
881 case SELECT_MODE_WORD:
882 /* If we are still within the same word, do nothing */
883 if (row == cb->prev.lnum && col >= (int)cb->word_start_col
884 && col < (int)cb->word_end_col && !repeated_click)
885 return;
886
887 /* Get new word boundaries */
888 clip_get_word_boundaries(cb, row, col);
889
890 /* Handle being after the origin point of selection */
891 if (clip_compare_pos(row, col, cb->origin_row,
892 cb->origin_start_col) >= 0)
893 clip_update_modeless_selection(cb, cb->origin_row,
894 cb->origin_start_col, row, cb->word_end_col);
895 else
896 clip_update_modeless_selection(cb, row, cb->word_start_col,
897 cb->origin_row, cb->origin_end_col);
898 break;
899
900 case SELECT_MODE_LINE:
901 if (row == cb->prev.lnum && !repeated_click)
902 return;
903
904 if (clip_compare_pos(row, col, cb->origin_row,
905 cb->origin_start_col) >= 0)
906 clip_update_modeless_selection(cb, cb->origin_row, 0, row,
907 (int)Columns);
908 else
909 clip_update_modeless_selection(cb, row, 0, cb->origin_row,
910 (int)Columns);
911 break;
912 }
913
914 cb->prev.lnum = row;
915 cb->prev.col = col;
916
917#ifdef DEBUG_SELECTION
918 printf("Selection is: (%u,%u) to (%u,%u)\n", cb->start.lnum,
919 cb->start.col, cb->end.lnum, cb->end.col);
920#endif
921}
922
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923# if defined(FEAT_GUI) || defined(PROTO)
924/*
925 * Redraw part of the selection if character at "row,col" is inside of it.
926 * Only used for the GUI.
927 */
928 void
929clip_may_redraw_selection(row, col, len)
930 int row, col;
931 int len;
932{
933 int start = col;
934 int end = col + len;
935
936 if (clip_star.state != SELECT_CLEARED
937 && row >= clip_star.start.lnum
938 && row <= clip_star.end.lnum)
939 {
940 if (row == clip_star.start.lnum && start < (int)clip_star.start.col)
941 start = clip_star.start.col;
942 if (row == clip_star.end.lnum && end > (int)clip_star.end.col)
943 end = clip_star.end.col;
944 if (end > start)
945 clip_invert_area(row, start, row, end, 0);
946 }
947}
948# endif
949
950/*
951 * Called from outside to clear selected region from the display
952 */
953 void
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200954clip_clear_selection(cbd)
955 VimClipboard *cbd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200958 if (cbd->state == SELECT_CLEARED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 return;
960
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200961 clip_invert_area((int)cbd->start.lnum, cbd->start.col, (int)cbd->end.lnum,
962 cbd->end.col, CLIP_CLEAR);
963 cbd->state = SELECT_CLEARED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964}
965
966/*
967 * Clear the selection if any lines from "row1" to "row2" are inside of it.
968 */
969 void
970clip_may_clear_selection(row1, row2)
971 int row1, row2;
972{
973 if (clip_star.state == SELECT_DONE
974 && row2 >= clip_star.start.lnum
975 && row1 <= clip_star.end.lnum)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200976 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977}
978
979/*
980 * Called before the screen is scrolled up or down. Adjusts the line numbers
981 * of the selection. Call with big number when clearing the screen.
982 */
983 void
984clip_scroll_selection(rows)
985 int rows; /* negative for scroll down */
986{
987 int lnum;
988
989 if (clip_star.state == SELECT_CLEARED)
990 return;
991
992 lnum = clip_star.start.lnum - rows;
993 if (lnum <= 0)
994 clip_star.start.lnum = 0;
995 else if (lnum >= screen_Rows) /* scrolled off of the screen */
996 clip_star.state = SELECT_CLEARED;
997 else
998 clip_star.start.lnum = lnum;
999
1000 lnum = clip_star.end.lnum - rows;
1001 if (lnum < 0) /* scrolled off of the screen */
1002 clip_star.state = SELECT_CLEARED;
1003 else if (lnum >= screen_Rows)
1004 clip_star.end.lnum = screen_Rows - 1;
1005 else
1006 clip_star.end.lnum = lnum;
1007}
1008
1009/*
1010 * Invert a region of the display between a starting and ending row and column
1011 * Values for "how":
1012 * CLIP_CLEAR: undo inversion
1013 * CLIP_SET: set inversion
1014 * CLIP_TOGGLE: set inversion if pos1 < pos2, undo inversion otherwise.
1015 * 0: invert (GUI only).
1016 */
1017 static void
1018clip_invert_area(row1, col1, row2, col2, how)
1019 int row1;
1020 int col1;
1021 int row2;
1022 int col2;
1023 int how;
1024{
1025 int invert = FALSE;
1026
1027 if (how == CLIP_SET)
1028 invert = TRUE;
1029
1030 /* Swap the from and to positions so the from is always before */
1031 if (clip_compare_pos(row1, col1, row2, col2) > 0)
1032 {
1033 int tmp_row, tmp_col;
1034
1035 tmp_row = row1;
1036 tmp_col = col1;
1037 row1 = row2;
1038 col1 = col2;
1039 row2 = tmp_row;
1040 col2 = tmp_col;
1041 }
1042 else if (how == CLIP_TOGGLE)
1043 invert = TRUE;
1044
1045 /* If all on the same line, do it the easy way */
1046 if (row1 == row2)
1047 {
1048 clip_invert_rectangle(row1, col1, 1, col2 - col1, invert);
1049 }
1050 else
1051 {
1052 /* Handle a piece of the first line */
1053 if (col1 > 0)
1054 {
1055 clip_invert_rectangle(row1, col1, 1, (int)Columns - col1, invert);
1056 row1++;
1057 }
1058
1059 /* Handle a piece of the last line */
1060 if (col2 < Columns - 1)
1061 {
1062 clip_invert_rectangle(row2, 0, 1, col2, invert);
1063 row2--;
1064 }
1065
1066 /* Handle the rectangle thats left */
1067 if (row2 >= row1)
1068 clip_invert_rectangle(row1, 0, row2 - row1 + 1, (int)Columns,
1069 invert);
1070 }
1071}
1072
1073/*
1074 * Invert or un-invert a rectangle of the screen.
1075 * "invert" is true if the result is inverted.
1076 */
1077 static void
1078clip_invert_rectangle(row, col, height, width, invert)
1079 int row;
1080 int col;
1081 int height;
1082 int width;
1083 int invert;
1084{
1085#ifdef FEAT_GUI
1086 if (gui.in_use)
1087 gui_mch_invert_rectangle(row, col, height, width);
1088 else
1089#endif
1090 screen_draw_rectangle(row, col, height, width, invert);
1091}
1092
1093/*
1094 * Copy the currently selected area into the '*' register so it will be
1095 * available for pasting.
1096 * When "both" is TRUE also copy to the '+' register.
1097 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 void
1099clip_copy_modeless_selection(both)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001100 int both UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101{
1102 char_u *buffer;
1103 char_u *bufp;
1104 int row;
1105 int start_col;
1106 int end_col;
1107 int line_end_col;
1108 int add_newline_flag = FALSE;
1109 int len;
1110#ifdef FEAT_MBYTE
1111 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112#endif
1113 int row1 = clip_star.start.lnum;
1114 int col1 = clip_star.start.col;
1115 int row2 = clip_star.end.lnum;
1116 int col2 = clip_star.end.col;
1117
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001118 /* Can't use ScreenLines unless initialized */
1119 if (ScreenLines == NULL)
1120 return;
1121
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 /*
1123 * Make sure row1 <= row2, and if row1 == row2 that col1 <= col2.
1124 */
1125 if (row1 > row2)
1126 {
1127 row = row1; row1 = row2; row2 = row;
1128 row = col1; col1 = col2; col2 = row;
1129 }
1130 else if (row1 == row2 && col1 > col2)
1131 {
1132 row = col1; col1 = col2; col2 = row;
1133 }
1134#ifdef FEAT_MBYTE
1135 /* correct starting point for being on right halve of double-wide char */
1136 p = ScreenLines + LineOffset[row1];
1137 if (enc_dbcs != 0)
1138 col1 -= (*mb_head_off)(p, p + col1);
1139 else if (enc_utf8 && p[col1] == 0)
1140 --col1;
1141#endif
1142
1143 /* Create a temporary buffer for storing the text */
1144 len = (row2 - row1 + 1) * Columns + 1;
1145#ifdef FEAT_MBYTE
1146 if (enc_dbcs != 0)
1147 len *= 2; /* max. 2 bytes per display cell */
1148 else if (enc_utf8)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001149 len *= MB_MAXBYTES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150#endif
1151 buffer = lalloc((long_u)len, TRUE);
1152 if (buffer == NULL) /* out of memory */
1153 return;
1154
1155 /* Process each row in the selection */
1156 for (bufp = buffer, row = row1; row <= row2; row++)
1157 {
1158 if (row == row1)
1159 start_col = col1;
1160 else
1161 start_col = 0;
1162
1163 if (row == row2)
1164 end_col = col2;
1165 else
1166 end_col = Columns;
1167
1168 line_end_col = clip_get_line_end(row);
1169
1170 /* See if we need to nuke some trailing whitespace */
1171 if (end_col >= Columns && (row < row2 || end_col > line_end_col))
1172 {
1173 /* Get rid of trailing whitespace */
1174 end_col = line_end_col;
1175 if (end_col < start_col)
1176 end_col = start_col;
1177
1178 /* If the last line extended to the end, add an extra newline */
1179 if (row == row2)
1180 add_newline_flag = TRUE;
1181 }
1182
1183 /* If after the first row, we need to always add a newline */
1184 if (row > row1 && !LineWraps[row - 1])
1185 *bufp++ = NL;
1186
1187 if (row < screen_Rows && end_col <= screen_Columns)
1188 {
1189#ifdef FEAT_MBYTE
1190 if (enc_dbcs != 0)
1191 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001192 int i;
1193
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 p = ScreenLines + LineOffset[row];
1195 for (i = start_col; i < end_col; ++i)
1196 if (enc_dbcs == DBCS_JPNU && p[i] == 0x8e)
1197 {
1198 /* single-width double-byte char */
1199 *bufp++ = 0x8e;
1200 *bufp++ = ScreenLines2[LineOffset[row] + i];
1201 }
1202 else
1203 {
1204 *bufp++ = p[i];
1205 if (MB_BYTE2LEN(p[i]) == 2)
1206 *bufp++ = p[++i];
1207 }
1208 }
1209 else if (enc_utf8)
1210 {
1211 int off;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001212 int i;
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001213 int ci;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214
1215 off = LineOffset[row];
1216 for (i = start_col; i < end_col; ++i)
1217 {
1218 /* The base character is either in ScreenLinesUC[] or
1219 * ScreenLines[]. */
1220 if (ScreenLinesUC[off + i] == 0)
1221 *bufp++ = ScreenLines[off + i];
1222 else
1223 {
1224 bufp += utf_char2bytes(ScreenLinesUC[off + i], bufp);
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001225 for (ci = 0; ci < Screen_mco; ++ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001227 /* Add a composing character. */
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001228 if (ScreenLinesC[ci][off + i] == 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001229 break;
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001230 bufp += utf_char2bytes(ScreenLinesC[ci][off + i],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 }
1233 }
1234 /* Skip right halve of double-wide character. */
1235 if (ScreenLines[off + i + 1] == 0)
1236 ++i;
1237 }
1238 }
1239 else
1240#endif
1241 {
1242 STRNCPY(bufp, ScreenLines + LineOffset[row] + start_col,
1243 end_col - start_col);
1244 bufp += end_col - start_col;
1245 }
1246 }
1247 }
1248
1249 /* Add a newline at the end if the selection ended there */
1250 if (add_newline_flag)
1251 *bufp++ = NL;
1252
1253 /* First cleanup any old selection and become the owner. */
1254 clip_free_selection(&clip_star);
1255 clip_own_selection(&clip_star);
1256
1257 /* Yank the text into the '*' register. */
1258 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_star);
1259
1260 /* Make the register contents available to the outside world. */
1261 clip_gen_set_selection(&clip_star);
1262
1263#ifdef FEAT_X11
1264 if (both)
1265 {
1266 /* Do the same for the '+' register. */
1267 clip_free_selection(&clip_plus);
1268 clip_own_selection(&clip_plus);
1269 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_plus);
1270 clip_gen_set_selection(&clip_plus);
1271 }
1272#endif
1273 vim_free(buffer);
1274}
1275
1276/*
1277 * Find the starting and ending positions of the word at the given row and
1278 * column. Only white-separated words are recognized here.
1279 */
1280#define CHAR_CLASS(c) (c <= ' ' ? ' ' : vim_iswordc(c))
1281
1282 static void
1283clip_get_word_boundaries(cb, row, col)
1284 VimClipboard *cb;
1285 int row;
1286 int col;
1287{
1288 int start_class;
1289 int temp_col;
1290 char_u *p;
1291#ifdef FEAT_MBYTE
1292 int mboff;
1293#endif
1294
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001295 if (row >= screen_Rows || col >= screen_Columns || ScreenLines == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 return;
1297
1298 p = ScreenLines + LineOffset[row];
1299#ifdef FEAT_MBYTE
1300 /* Correct for starting in the right halve of a double-wide char */
1301 if (enc_dbcs != 0)
1302 col -= dbcs_screen_head_off(p, p + col);
1303 else if (enc_utf8 && p[col] == 0)
1304 --col;
1305#endif
1306 start_class = CHAR_CLASS(p[col]);
1307
1308 temp_col = col;
1309 for ( ; temp_col > 0; temp_col--)
1310#ifdef FEAT_MBYTE
1311 if (enc_dbcs != 0
1312 && (mboff = dbcs_screen_head_off(p, p + temp_col - 1)) > 0)
1313 temp_col -= mboff;
1314 else
1315#endif
1316 if (CHAR_CLASS(p[temp_col - 1]) != start_class
1317#ifdef FEAT_MBYTE
1318 && !(enc_utf8 && p[temp_col - 1] == 0)
1319#endif
1320 )
1321 break;
1322 cb->word_start_col = temp_col;
1323
1324 temp_col = col;
1325 for ( ; temp_col < screen_Columns; temp_col++)
1326#ifdef FEAT_MBYTE
1327 if (enc_dbcs != 0 && dbcs_ptr2cells(p + temp_col) == 2)
1328 ++temp_col;
1329 else
1330#endif
1331 if (CHAR_CLASS(p[temp_col]) != start_class
1332#ifdef FEAT_MBYTE
1333 && !(enc_utf8 && p[temp_col] == 0)
1334#endif
1335 )
1336 break;
1337 cb->word_end_col = temp_col;
1338}
1339
1340/*
1341 * Find the column position for the last non-whitespace character on the given
1342 * line.
1343 */
1344 static int
1345clip_get_line_end(row)
1346 int row;
1347{
1348 int i;
1349
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001350 if (row >= screen_Rows || ScreenLines == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 return 0;
1352 for (i = screen_Columns; i > 0; i--)
1353 if (ScreenLines[LineOffset[row] + i - 1] != ' ')
1354 break;
1355 return i;
1356}
1357
1358/*
1359 * Update the currently selected region by adding and/or subtracting from the
1360 * beginning or end and inverting the changed area(s).
1361 */
1362 static void
1363clip_update_modeless_selection(cb, row1, col1, row2, col2)
1364 VimClipboard *cb;
1365 int row1;
1366 int col1;
1367 int row2;
1368 int col2;
1369{
1370 /* See if we changed at the beginning of the selection */
1371 if (row1 != cb->start.lnum || col1 != (int)cb->start.col)
1372 {
1373 clip_invert_area(row1, col1, (int)cb->start.lnum, cb->start.col,
1374 CLIP_TOGGLE);
1375 cb->start.lnum = row1;
1376 cb->start.col = col1;
1377 }
1378
1379 /* See if we changed at the end of the selection */
1380 if (row2 != cb->end.lnum || col2 != (int)cb->end.col)
1381 {
1382 clip_invert_area((int)cb->end.lnum, cb->end.col, row2, col2,
1383 CLIP_TOGGLE);
1384 cb->end.lnum = row2;
1385 cb->end.col = col2;
1386 }
1387}
1388
1389 int
1390clip_gen_own_selection(cbd)
1391 VimClipboard *cbd;
1392{
1393#ifdef FEAT_XCLIPBOARD
1394# ifdef FEAT_GUI
1395 if (gui.in_use)
1396 return clip_mch_own_selection(cbd);
1397 else
1398# endif
1399 return clip_xterm_own_selection(cbd);
1400#else
1401 return clip_mch_own_selection(cbd);
1402#endif
1403}
1404
1405 void
1406clip_gen_lose_selection(cbd)
1407 VimClipboard *cbd;
1408{
1409#ifdef FEAT_XCLIPBOARD
1410# ifdef FEAT_GUI
1411 if (gui.in_use)
1412 clip_mch_lose_selection(cbd);
1413 else
1414# endif
1415 clip_xterm_lose_selection(cbd);
1416#else
1417 clip_mch_lose_selection(cbd);
1418#endif
1419}
1420
1421 void
1422clip_gen_set_selection(cbd)
1423 VimClipboard *cbd;
1424{
1425#ifdef FEAT_XCLIPBOARD
1426# ifdef FEAT_GUI
1427 if (gui.in_use)
1428 clip_mch_set_selection(cbd);
1429 else
1430# endif
1431 clip_xterm_set_selection(cbd);
1432#else
1433 clip_mch_set_selection(cbd);
1434#endif
1435}
1436
1437 void
1438clip_gen_request_selection(cbd)
1439 VimClipboard *cbd;
1440{
1441#ifdef FEAT_XCLIPBOARD
1442# ifdef FEAT_GUI
1443 if (gui.in_use)
1444 clip_mch_request_selection(cbd);
1445 else
1446# endif
1447 clip_xterm_request_selection(cbd);
1448#else
1449 clip_mch_request_selection(cbd);
1450#endif
1451}
1452
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001453 int
1454clip_gen_owner_exists(cbd)
Bram Moolenaar81851112013-04-12 12:27:30 +02001455 VimClipboard *cbd UNUSED;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001456{
1457#ifdef FEAT_XCLIPBOARD
1458# ifdef FEAT_GUI_GTK
1459 if (gui.in_use)
1460 return clip_gtk_owner_exists(cbd);
1461 else
1462# endif
1463 return clip_x11_owner_exists(cbd);
1464#endif
1465 return TRUE;
1466}
1467
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468#endif /* FEAT_CLIPBOARD */
1469
1470/*****************************************************************************
1471 * Functions that handle the input buffer.
1472 * This is used for any GUI version, and the unix terminal version.
1473 *
1474 * For Unix, the input characters are buffered to be able to check for a
1475 * CTRL-C. This should be done with signals, but I don't know how to do that
1476 * in a portable way for a tty in RAW mode.
1477 *
1478 * For the client-server code in the console the received keys are put in the
1479 * input buffer.
1480 */
1481
1482#if defined(USE_INPUT_BUF) || defined(PROTO)
1483
1484/*
1485 * Internal typeahead buffer. Includes extra space for long key code
1486 * descriptions which would otherwise overflow. The buffer is considered full
1487 * when only this extra space (or part of it) remains.
1488 */
1489#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
1490 || defined(FEAT_CLIENTSERVER)
1491 /*
1492 * Sun WorkShop and NetBeans stuff debugger commands into the input buffer.
1493 * This requires a larger buffer...
1494 * (Madsen) Go with this for remote input as well ...
1495 */
1496# define INBUFLEN 4096
1497#else
1498# define INBUFLEN 250
1499#endif
1500
1501static char_u inbuf[INBUFLEN + MAX_KEY_CODE_LEN];
1502static int inbufcount = 0; /* number of chars in inbuf[] */
1503
1504/*
1505 * vim_is_input_buf_full(), vim_is_input_buf_empty(), add_to_input_buf(), and
1506 * trash_input_buf() are functions for manipulating the input buffer. These
1507 * are used by the gui_* calls when a GUI is used to handle keyboard input.
1508 */
1509
1510 int
1511vim_is_input_buf_full()
1512{
1513 return (inbufcount >= INBUFLEN);
1514}
1515
1516 int
1517vim_is_input_buf_empty()
1518{
1519 return (inbufcount == 0);
1520}
1521
1522#if defined(FEAT_OLE) || defined(PROTO)
1523 int
1524vim_free_in_input_buf()
1525{
1526 return (INBUFLEN - inbufcount);
1527}
1528#endif
1529
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001530#if defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 int
1532vim_used_in_input_buf()
1533{
1534 return inbufcount;
1535}
1536#endif
1537
1538#if defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) || defined(PROTO)
1539/*
1540 * Return the current contents of the input buffer and make it empty.
1541 * The returned pointer must be passed to set_input_buf() later.
1542 */
1543 char_u *
1544get_input_buf()
1545{
1546 garray_T *gap;
1547
1548 /* We use a growarray to store the data pointer and the length. */
1549 gap = (garray_T *)alloc((unsigned)sizeof(garray_T));
1550 if (gap != NULL)
1551 {
1552 /* Add one to avoid a zero size. */
1553 gap->ga_data = alloc((unsigned)inbufcount + 1);
1554 if (gap->ga_data != NULL)
1555 mch_memmove(gap->ga_data, inbuf, (size_t)inbufcount);
1556 gap->ga_len = inbufcount;
1557 }
1558 trash_input_buf();
1559 return (char_u *)gap;
1560}
1561
1562/*
1563 * Restore the input buffer with a pointer returned from get_input_buf().
1564 * The allocated memory is freed, this only works once!
1565 */
1566 void
1567set_input_buf(p)
1568 char_u *p;
1569{
1570 garray_T *gap = (garray_T *)p;
1571
1572 if (gap != NULL)
1573 {
1574 if (gap->ga_data != NULL)
1575 {
1576 mch_memmove(inbuf, gap->ga_data, gap->ga_len);
1577 inbufcount = gap->ga_len;
1578 vim_free(gap->ga_data);
1579 }
1580 vim_free(gap);
1581 }
1582}
1583#endif
1584
Bram Moolenaar446cb832008-06-24 21:56:24 +00001585#if defined(FEAT_GUI) \
1586 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 || defined(FEAT_XCLIPBOARD) || defined(VMS) \
Bram Moolenaarf52c7252006-02-10 23:23:57 +00001588 || defined(FEAT_SNIFF) || defined(FEAT_CLIENTSERVER) \
Bram Moolenaarf52c7252006-02-10 23:23:57 +00001589 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590/*
1591 * Add the given bytes to the input buffer
1592 * Special keys start with CSI. A real CSI must have been translated to
1593 * CSI KS_EXTRA KE_CSI. K_SPECIAL doesn't require translation.
1594 */
1595 void
1596add_to_input_buf(s, len)
1597 char_u *s;
1598 int len;
1599{
1600 if (inbufcount + len > INBUFLEN + MAX_KEY_CODE_LEN)
1601 return; /* Shouldn't ever happen! */
1602
1603#ifdef FEAT_HANGULIN
1604 if ((State & (INSERT|CMDLINE)) && hangul_input_state_get())
1605 if ((len = hangul_input_process(s, len)) == 0)
1606 return;
1607#endif
1608
1609 while (len--)
1610 inbuf[inbufcount++] = *s++;
1611}
1612#endif
1613
Bram Moolenaar70c2a632007-08-15 18:08:50 +00001614#if ((defined(FEAT_XIM) || defined(FEAT_DND)) && defined(FEAT_GUI_GTK)) \
1615 || defined(FEAT_GUI_MSWIN) \
1616 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 || (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) \
Bram Moolenaarf52c7252006-02-10 23:23:57 +00001618 || (defined(FEAT_GUI) && (!defined(USE_ON_FLY_SCROLL) \
1619 || defined(FEAT_MENU))) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 || defined(PROTO)
1621/*
1622 * Add "str[len]" to the input buffer while escaping CSI bytes.
1623 */
1624 void
1625add_to_input_buf_csi(char_u *str, int len)
1626{
1627 int i;
1628 char_u buf[2];
1629
1630 for (i = 0; i < len; ++i)
1631 {
1632 add_to_input_buf(str + i, 1);
1633 if (str[i] == CSI)
1634 {
1635 /* Turn CSI into K_CSI. */
1636 buf[0] = KS_EXTRA;
1637 buf[1] = (int)KE_CSI;
1638 add_to_input_buf(buf, 2);
1639 }
1640 }
1641}
1642#endif
1643
1644#if defined(FEAT_HANGULIN) || defined(PROTO)
1645 void
Bram Moolenaard44347f2011-06-19 01:14:29 +02001646push_raw_key(s, len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 char_u *s;
1648 int len;
1649{
1650 while (len--)
1651 inbuf[inbufcount++] = *s++;
1652}
1653#endif
1654
1655#if defined(FEAT_GUI) || defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) \
1656 || defined(PROTO)
1657/* Remove everything from the input buffer. Called when ^C is found */
1658 void
1659trash_input_buf()
1660{
1661 inbufcount = 0;
1662}
1663#endif
1664
1665/*
1666 * Read as much data from the input buffer as possible up to maxlen, and store
1667 * it in buf.
1668 * Note: this function used to be Read() in unix.c
1669 */
1670 int
1671read_from_input_buf(buf, maxlen)
1672 char_u *buf;
1673 long maxlen;
1674{
1675 if (inbufcount == 0) /* if the buffer is empty, fill it */
1676 fill_input_buf(TRUE);
1677 if (maxlen > inbufcount)
1678 maxlen = inbufcount;
1679 mch_memmove(buf, inbuf, (size_t)maxlen);
1680 inbufcount -= maxlen;
1681 if (inbufcount)
1682 mch_memmove(inbuf, inbuf + maxlen, (size_t)inbufcount);
1683 return (int)maxlen;
1684}
1685
1686 void
1687fill_input_buf(exit_on_error)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001688 int exit_on_error UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689{
1690#if defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X_UNIX)
1691 int len;
1692 int try;
1693 static int did_read_something = FALSE;
1694# ifdef FEAT_MBYTE
1695 static char_u *rest = NULL; /* unconverted rest of previous read */
1696 static int restlen = 0;
1697 int unconverted;
1698# endif
1699#endif
1700
1701#ifdef FEAT_GUI
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001702 if (gui.in_use
1703# ifdef NO_CONSOLE_INPUT
1704 /* Don't use the GUI input when the window hasn't been opened yet.
1705 * We get here from ui_inchar() when we should try reading from stdin. */
1706 && !no_console_input()
1707# endif
1708 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 {
1710 gui_mch_update();
1711 return;
1712 }
1713#endif
1714#if defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X_UNIX)
1715 if (vim_is_input_buf_full())
1716 return;
1717 /*
1718 * Fill_input_buf() is only called when we really need a character.
1719 * If we can't get any, but there is some in the buffer, just return.
1720 * If we can't get any, and there isn't any in the buffer, we give up and
1721 * exit Vim.
1722 */
1723# ifdef __BEOS__
1724 /*
1725 * On the BeBox version (for now), all input is secretly performed within
1726 * beos_select() which is called from RealWaitForChar().
1727 */
1728 while (!vim_is_input_buf_full() && RealWaitForChar(read_cmd_fd, 0, NULL))
1729 ;
1730 len = inbufcount;
1731 inbufcount = 0;
1732# else
1733
1734# ifdef FEAT_SNIFF
1735 if (sniff_request_waiting)
1736 {
1737 add_to_input_buf((char_u *)"\233sniff",6); /* results in K_SNIFF */
1738 sniff_request_waiting = 0;
1739 want_sniff_request = 0;
1740 return;
1741 }
1742# endif
1743
1744# ifdef FEAT_MBYTE
1745 if (rest != NULL)
1746 {
1747 /* Use remainder of previous call, starts with an invalid character
1748 * that may become valid when reading more. */
1749 if (restlen > INBUFLEN - inbufcount)
1750 unconverted = INBUFLEN - inbufcount;
1751 else
1752 unconverted = restlen;
1753 mch_memmove(inbuf + inbufcount, rest, unconverted);
1754 if (unconverted == restlen)
1755 {
1756 vim_free(rest);
1757 rest = NULL;
1758 }
1759 else
1760 {
1761 restlen -= unconverted;
1762 mch_memmove(rest, rest + unconverted, restlen);
1763 }
1764 inbufcount += unconverted;
1765 }
1766 else
1767 unconverted = 0;
1768#endif
1769
1770 len = 0; /* to avoid gcc warning */
1771 for (try = 0; try < 100; ++try)
1772 {
1773# ifdef VMS
1774 len = vms_read(
1775# else
1776 len = read(read_cmd_fd,
1777# endif
1778 (char *)inbuf + inbufcount, (size_t)((INBUFLEN - inbufcount)
1779# ifdef FEAT_MBYTE
1780 / input_conv.vc_factor
1781# endif
1782 ));
1783# if 0
1784 ) /* avoid syntax highlight error */
1785# endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001786
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 if (len > 0 || got_int)
1788 break;
1789 /*
1790 * If reading stdin results in an error, continue reading stderr.
1791 * This helps when using "foo | xargs vim".
1792 */
1793 if (!did_read_something && !isatty(read_cmd_fd) && read_cmd_fd == 0)
1794 {
1795 int m = cur_tmode;
1796
1797 /* We probably set the wrong file descriptor to raw mode. Switch
1798 * back to cooked mode, use another descriptor and set the mode to
1799 * what it was. */
1800 settmode(TMODE_COOK);
1801#ifdef HAVE_DUP
1802 /* Use stderr for stdin, also works for shell commands. */
1803 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00001804 ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805#else
1806 read_cmd_fd = 2; /* read from stderr instead of stdin */
1807#endif
1808 settmode(m);
1809 }
1810 if (!exit_on_error)
1811 return;
1812 }
1813# endif
1814 if (len <= 0 && !got_int)
1815 read_error_exit();
1816 if (len > 0)
1817 did_read_something = TRUE;
1818 if (got_int)
1819 {
1820 /* Interrupted, pretend a CTRL-C was typed. */
1821 inbuf[0] = 3;
1822 inbufcount = 1;
1823 }
1824 else
1825 {
1826# ifdef FEAT_MBYTE
1827 /*
1828 * May perform conversion on the input characters.
1829 * Include the unconverted rest of the previous call.
1830 * If there is an incomplete char at the end it is kept for the next
1831 * time, reading more bytes should make conversion possible.
1832 * Don't do this in the unlikely event that the input buffer is too
1833 * small ("rest" still contains more bytes).
1834 */
1835 if (input_conv.vc_type != CONV_NONE)
1836 {
1837 inbufcount -= unconverted;
1838 len = convert_input_safe(inbuf + inbufcount,
1839 len + unconverted, INBUFLEN - inbufcount,
1840 rest == NULL ? &rest : NULL, &restlen);
1841 }
1842# endif
1843 while (len-- > 0)
1844 {
1845 /*
1846 * if a CTRL-C was typed, remove it from the buffer and set got_int
1847 */
1848 if (inbuf[inbufcount] == 3 && ctrl_c_interrupts)
1849 {
1850 /* remove everything typed before the CTRL-C */
1851 mch_memmove(inbuf, inbuf + inbufcount, (size_t)(len + 1));
1852 inbufcount = 0;
1853 got_int = TRUE;
1854 }
1855 ++inbufcount;
1856 }
1857 }
1858#endif /* UNIX or OS2 or VMS*/
1859}
1860#endif /* defined(UNIX) || defined(FEAT_GUI) || defined(OS2) || defined(VMS) */
1861
1862/*
1863 * Exit because of an input read error.
1864 */
1865 void
1866read_error_exit()
1867{
1868 if (silent_mode) /* Normal way to exit for "ex -s" */
1869 getout(0);
1870 STRCPY(IObuff, _("Vim: Error reading input, exiting...\n"));
1871 preserve_exit();
1872}
1873
1874#if defined(CURSOR_SHAPE) || defined(PROTO)
1875/*
1876 * May update the shape of the cursor.
1877 */
1878 void
1879ui_cursor_shape()
1880{
1881# ifdef FEAT_GUI
1882 if (gui.in_use)
1883 gui_update_cursor_later();
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001884 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001886 term_cursor_shape();
1887
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888# ifdef MCH_CURSOR_SHAPE
1889 mch_update_cursor();
1890# endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001891
1892# ifdef FEAT_CONCEAL
Bram Moolenaar8e469272010-07-28 19:38:16 +02001893 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001894# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895}
1896#endif
1897
1898#if defined(FEAT_CLIPBOARD) || defined(FEAT_GUI) || defined(FEAT_RIGHTLEFT) \
Bram Moolenaaraf51e662008-07-14 19:48:05 +00001899 || defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900/*
1901 * Check bounds for column number
1902 */
1903 int
1904check_col(col)
1905 int col;
1906{
1907 if (col < 0)
1908 return 0;
1909 if (col >= (int)screen_Columns)
1910 return (int)screen_Columns - 1;
1911 return col;
1912}
1913
1914/*
1915 * Check bounds for row number
1916 */
1917 int
1918check_row(row)
1919 int row;
1920{
1921 if (row < 0)
1922 return 0;
1923 if (row >= (int)screen_Rows)
1924 return (int)screen_Rows - 1;
1925 return row;
1926}
1927#endif
1928
1929/*
1930 * Stuff for the X clipboard. Shared between VMS and Unix.
1931 */
1932
1933#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) || defined(PROTO)
1934# include <X11/Xatom.h>
1935# include <X11/Intrinsic.h>
1936
1937/*
1938 * Open the application context (if it hasn't been opened yet).
1939 * Used for Motif and Athena GUI and the xterm clipboard.
1940 */
1941 void
1942open_app_context()
1943{
1944 if (app_context == NULL)
1945 {
1946 XtToolkitInitialize();
1947 app_context = XtCreateApplicationContext();
1948 }
1949}
1950
1951static Atom vim_atom; /* Vim's own special selection format */
1952#ifdef FEAT_MBYTE
1953static Atom vimenc_atom; /* Vim's extended selection format */
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01001954static Atom utf8_atom;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955#endif
1956static Atom compound_text_atom;
1957static Atom text_atom;
1958static Atom targets_atom;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001959static Atom timestamp_atom; /* Used to get a timestamp */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960
1961 void
1962x11_setup_atoms(dpy)
1963 Display *dpy;
1964{
1965 vim_atom = XInternAtom(dpy, VIM_ATOM_NAME, False);
1966#ifdef FEAT_MBYTE
1967 vimenc_atom = XInternAtom(dpy, VIMENC_ATOM_NAME,False);
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01001968 utf8_atom = XInternAtom(dpy, "UTF8_STRING", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969#endif
1970 compound_text_atom = XInternAtom(dpy, "COMPOUND_TEXT", False);
1971 text_atom = XInternAtom(dpy, "TEXT", False);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001972 targets_atom = XInternAtom(dpy, "TARGETS", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 clip_star.sel_atom = XA_PRIMARY;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001974 clip_plus.sel_atom = XInternAtom(dpy, "CLIPBOARD", False);
1975 timestamp_atom = XInternAtom(dpy, "TIMESTAMP", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976}
1977
1978/*
1979 * X Selection stuff, for cutting and pasting text to other windows.
1980 */
1981
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001982static Boolean clip_x11_convert_selection_cb __ARGS((Widget, Atom *, Atom *, Atom *, XtPointer *, long_u *, int *));
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001983static void clip_x11_lose_ownership_cb __ARGS((Widget, Atom *));
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001984static void clip_x11_timestamp_cb __ARGS((Widget w, XtPointer n, XEvent *event, Boolean *cont));
Bram Moolenaar62b42182010-09-21 22:09:37 +02001985static void clip_x11_request_selection_cb __ARGS((Widget, XtPointer, Atom *, Atom *, XtPointer, long_u *, int *));
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001986
1987/*
1988 * Property callback to get a timestamp for XtOwnSelection.
1989 */
1990 static void
1991clip_x11_timestamp_cb(w, n, event, cont)
1992 Widget w;
1993 XtPointer n UNUSED;
1994 XEvent *event;
1995 Boolean *cont UNUSED;
1996{
1997 Atom actual_type;
1998 int format;
1999 unsigned long nitems, bytes_after;
2000 unsigned char *prop=NULL;
2001 XPropertyEvent *xproperty=&event->xproperty;
2002
2003 /* Must be a property notify, state can't be Delete (True), has to be
2004 * one of the supported selection types. */
2005 if (event->type != PropertyNotify || xproperty->state
2006 || (xproperty->atom != clip_star.sel_atom
2007 && xproperty->atom != clip_plus.sel_atom))
2008 return;
2009
2010 if (XGetWindowProperty(xproperty->display, xproperty->window,
2011 xproperty->atom, 0, 0, False, timestamp_atom, &actual_type, &format,
2012 &nitems, &bytes_after, &prop))
2013 return;
2014
2015 if (prop)
2016 XFree(prop);
2017
2018 /* Make sure the property type is "TIMESTAMP" and it's 32 bits. */
2019 if (actual_type != timestamp_atom || format != 32)
2020 return;
2021
2022 /* Get the selection, using the event timestamp. */
Bram Moolenaar62b42182010-09-21 22:09:37 +02002023 if (XtOwnSelection(w, xproperty->atom, xproperty->time,
2024 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
2025 NULL) == OK)
2026 {
2027 /* Set the "owned" flag now, there may have been a call to
2028 * lose_ownership_cb in between. */
2029 if (xproperty->atom == clip_plus.sel_atom)
2030 clip_plus.owned = TRUE;
2031 else
2032 clip_star.owned = TRUE;
2033 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002034}
2035
2036 void
2037x11_setup_selection(w)
2038 Widget w;
2039{
2040 XtAddEventHandler(w, PropertyChangeMask, False,
2041 /*(XtEventHandler)*/clip_x11_timestamp_cb, (XtPointer)NULL);
2042}
2043
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 static void
2045clip_x11_request_selection_cb(w, success, sel_atom, type, value, length,
2046 format)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002047 Widget w UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 XtPointer success;
2049 Atom *sel_atom;
2050 Atom *type;
2051 XtPointer value;
2052 long_u *length;
2053 int *format;
2054{
Bram Moolenaard44347f2011-06-19 01:14:29 +02002055 int motion_type = MAUTO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 long_u len;
2057 char_u *p;
2058 char **text_list = NULL;
2059 VimClipboard *cbd;
2060#ifdef FEAT_MBYTE
2061 char_u *tmpbuf = NULL;
2062#endif
2063
2064 if (*sel_atom == clip_plus.sel_atom)
2065 cbd = &clip_plus;
2066 else
2067 cbd = &clip_star;
2068
2069 if (value == NULL || *length == 0)
2070 {
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002071 clip_free_selection(cbd); /* nothing received, clear register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 *(int *)success = FALSE;
2073 return;
2074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 p = (char_u *)value;
2076 len = *length;
2077 if (*type == vim_atom)
2078 {
2079 motion_type = *p++;
2080 len--;
2081 }
2082
2083#ifdef FEAT_MBYTE
2084 else if (*type == vimenc_atom)
2085 {
2086 char_u *enc;
2087 vimconv_T conv;
2088 int convlen;
2089
2090 motion_type = *p++;
2091 --len;
2092
2093 enc = p;
2094 p += STRLEN(p) + 1;
2095 len -= p - enc;
2096
2097 /* If the encoding of the text is different from 'encoding', attempt
2098 * converting it. */
2099 conv.vc_type = CONV_NONE;
2100 convert_setup(&conv, enc, p_enc);
2101 if (conv.vc_type != CONV_NONE)
2102 {
2103 convlen = len; /* Need to use an int here. */
2104 tmpbuf = string_convert(&conv, p, &convlen);
2105 len = convlen;
2106 if (tmpbuf != NULL)
2107 p = tmpbuf;
2108 convert_setup(&conv, NULL, NULL);
2109 }
2110 }
2111#endif
2112
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002113 else if (*type == compound_text_atom
2114#ifdef FEAT_MBYTE
2115 || *type == utf8_atom
2116#endif
2117 || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118#ifdef FEAT_MBYTE
2119 enc_dbcs != 0 &&
2120#endif
2121 *type == text_atom))
2122 {
2123 XTextProperty text_prop;
2124 int n_text = 0;
2125 int status;
2126
2127 text_prop.value = (unsigned char *)value;
2128 text_prop.encoding = *type;
2129 text_prop.format = *format;
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002130 text_prop.nitems = len;
Bram Moolenaar81851112013-04-12 12:27:30 +02002131#if defined(FEAT_MBYTE) && defined(X_HAVE_UTF8_STRING)
Bram Moolenaare2e663f2013-03-07 18:02:30 +01002132 if (*type == utf8_atom)
2133 status = Xutf8TextPropertyToTextList(X_DISPLAY, &text_prop,
2134 &text_list, &n_text);
2135 else
2136#endif
2137 status = XmbTextPropertyToTextList(X_DISPLAY, &text_prop,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 &text_list, &n_text);
2139 if (status != Success || n_text < 1)
2140 {
2141 *(int *)success = FALSE;
2142 return;
2143 }
2144 p = (char_u *)text_list[0];
2145 len = STRLEN(p);
2146 }
2147 clip_yank_selection(motion_type, p, (long)len, cbd);
2148
2149 if (text_list != NULL)
2150 XFreeStringList(text_list);
2151#ifdef FEAT_MBYTE
2152 vim_free(tmpbuf);
2153#endif
2154 XtFree((char *)value);
2155 *(int *)success = TRUE;
2156}
2157
2158 void
2159clip_x11_request_selection(myShell, dpy, cbd)
2160 Widget myShell;
2161 Display *dpy;
2162 VimClipboard *cbd;
2163{
2164 XEvent event;
2165 Atom type;
2166 static int success;
2167 int i;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002168 time_t start_time;
2169 int timed_out = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170
2171 for (i =
2172#ifdef FEAT_MBYTE
2173 0
2174#else
2175 1
2176#endif
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002177 ; i < 6; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 {
2179 switch (i)
2180 {
2181#ifdef FEAT_MBYTE
2182 case 0: type = vimenc_atom; break;
2183#endif
2184 case 1: type = vim_atom; break;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002185#ifdef FEAT_MBYTE
2186 case 2: type = utf8_atom; break;
2187#endif
2188 case 3: type = compound_text_atom; break;
2189 case 4: type = text_atom; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 default: type = XA_STRING;
2191 }
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002192#ifdef FEAT_MBYTE
Bram Moolenaar81851112013-04-12 12:27:30 +02002193 if (type == utf8_atom
2194# if defined(X_HAVE_UTF8_STRING)
2195 && !enc_utf8
2196# endif
2197 )
2198 /* Only request utf-8 when 'encoding' is utf8 and
2199 * Xutf8TextPropertyToTextList is available. */
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002200 continue;
2201#endif
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002202 success = MAYBE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 XtGetSelectionValue(myShell, cbd->sel_atom, type,
2204 clip_x11_request_selection_cb, (XtPointer)&success, CurrentTime);
2205
2206 /* Make sure the request for the selection goes out before waiting for
2207 * a response. */
2208 XFlush(dpy);
2209
2210 /*
2211 * Wait for result of selection request, otherwise if we type more
2212 * characters, then they will appear before the one that requested the
2213 * paste! Don't worry, we will catch up with any other events later.
2214 */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002215 start_time = time(NULL);
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002216 while (success == MAYBE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 {
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002218 if (XCheckTypedEvent(dpy, SelectionNotify, &event)
2219 || XCheckTypedEvent(dpy, SelectionRequest, &event)
2220 || XCheckTypedEvent(dpy, PropertyNotify, &event))
Bram Moolenaar89417b92008-09-07 19:48:53 +00002221 {
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002222 /* This is where clip_x11_request_selection_cb() should be
2223 * called. It may actually happen a bit later, so we loop
2224 * until "success" changes.
2225 * We may get a SelectionRequest here and if we don't handle
2226 * it we hang. KDE klipper does this, for example.
2227 * We need to handle a PropertyNotify for large selections. */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002228 XtDispatchEvent(&event);
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002229 continue;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231
Bram Moolenaar89417b92008-09-07 19:48:53 +00002232 /* Time out after 2 to 3 seconds to avoid that we hang when the
2233 * other process doesn't respond. Note that the SelectionNotify
2234 * event may still come later when the selection owner comes back
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002235 * to life and the text gets inserted unexpectedly. Don't know
2236 * why that happens or how to avoid that :-(. */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002237 if (time(NULL) > start_time + 2)
2238 {
2239 timed_out = TRUE;
2240 break;
2241 }
2242
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 /* Do we need this? Probably not. */
2244 XSync(dpy, False);
2245
Bram Moolenaar89417b92008-09-07 19:48:53 +00002246 /* Wait for 1 msec to avoid that we eat up all CPU time. */
2247 ui_delay(1L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 }
2249
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002250 if (success == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 return;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002252
2253 /* don't do a retry with another type after timing out, otherwise we
2254 * hang for 15 seconds. */
2255 if (timed_out)
2256 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 }
2258
2259 /* Final fallback position - use the X CUT_BUFFER0 store */
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002260 yank_cut_buffer0(dpy, cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261}
2262
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 static Boolean
2264clip_x11_convert_selection_cb(w, sel_atom, target, type, value, length, format)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002265 Widget w UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 Atom *sel_atom;
2267 Atom *target;
2268 Atom *type;
2269 XtPointer *value;
2270 long_u *length;
2271 int *format;
2272{
2273 char_u *string;
2274 char_u *result;
2275 int motion_type;
2276 VimClipboard *cbd;
2277 int i;
2278
2279 if (*sel_atom == clip_plus.sel_atom)
2280 cbd = &clip_plus;
2281 else
2282 cbd = &clip_star;
2283
2284 if (!cbd->owned)
2285 return False; /* Shouldn't ever happen */
2286
2287 /* requestor wants to know what target types we support */
2288 if (*target == targets_atom)
2289 {
2290 Atom *array;
2291
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002292 if ((array = (Atom *)XtMalloc((unsigned)(sizeof(Atom) * 7))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 return False;
2294 *value = (XtPointer)array;
2295 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 array[i++] = targets_atom;
2297#ifdef FEAT_MBYTE
2298 array[i++] = vimenc_atom;
2299#endif
2300 array[i++] = vim_atom;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002301#ifdef FEAT_MBYTE
2302 if (enc_utf8)
2303 array[i++] = utf8_atom;
2304#endif
2305 array[i++] = XA_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 array[i++] = text_atom;
2307 array[i++] = compound_text_atom;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002308
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 *type = XA_ATOM;
2310 /* This used to be: *format = sizeof(Atom) * 8; but that caused
2311 * crashes on 64 bit machines. (Peter Derr) */
2312 *format = 32;
2313 *length = i;
2314 return True;
2315 }
2316
2317 if ( *target != XA_STRING
2318#ifdef FEAT_MBYTE
2319 && *target != vimenc_atom
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002320 && *target != utf8_atom
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321#endif
2322 && *target != vim_atom
2323 && *target != text_atom
2324 && *target != compound_text_atom)
2325 return False;
2326
2327 clip_get_selection(cbd);
2328 motion_type = clip_convert_selection(&string, length, cbd);
2329 if (motion_type < 0)
2330 return False;
2331
2332 /* For our own format, the first byte contains the motion type */
2333 if (*target == vim_atom)
2334 (*length)++;
2335
2336#ifdef FEAT_MBYTE
2337 /* Our own format with encoding: motion 'encoding' NUL text */
2338 if (*target == vimenc_atom)
2339 *length += STRLEN(p_enc) + 2;
2340#endif
2341
2342 *value = XtMalloc((Cardinal)*length);
2343 result = (char_u *)*value;
2344 if (result == NULL)
2345 {
2346 vim_free(string);
2347 return False;
2348 }
2349
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002350 if (*target == XA_STRING
2351#ifdef FEAT_MBYTE
2352 || (*target == utf8_atom && enc_utf8)
2353#endif
2354 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 {
2356 mch_memmove(result, string, (size_t)(*length));
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002357 *type = *target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 }
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002359 else if (*target == compound_text_atom || *target == text_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 {
2361 XTextProperty text_prop;
2362 char *string_nt = (char *)alloc((unsigned)*length + 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002363 int conv_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364
2365 /* create NUL terminated string which XmbTextListToTextProperty wants */
2366 mch_memmove(string_nt, string, (size_t)*length);
2367 string_nt[*length] = NUL;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002368 conv_result = XmbTextListToTextProperty(X_DISPLAY, (char **)&string_nt,
2369 1, XCompoundTextStyle, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 vim_free(string_nt);
2371 XtFree(*value); /* replace with COMPOUND text */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002372 if (conv_result != Success)
2373 {
2374 vim_free(string);
2375 return False;
2376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 *value = (XtPointer)(text_prop.value); /* from plain text */
2378 *length = text_prop.nitems;
2379 *type = compound_text_atom;
2380 }
2381
2382#ifdef FEAT_MBYTE
2383 else if (*target == vimenc_atom)
2384 {
2385 int l = STRLEN(p_enc);
2386
2387 result[0] = motion_type;
2388 STRCPY(result + 1, p_enc);
2389 mch_memmove(result + l + 2, string, (size_t)(*length - l - 2));
2390 *type = vimenc_atom;
2391 }
2392#endif
2393
2394 else
2395 {
2396 result[0] = motion_type;
2397 mch_memmove(result + 1, string, (size_t)(*length - 1));
2398 *type = vim_atom;
2399 }
2400 *format = 8; /* 8 bits per char */
2401 vim_free(string);
2402 return True;
2403}
2404
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 static void
2406clip_x11_lose_ownership_cb(w, sel_atom)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002407 Widget w UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 Atom *sel_atom;
2409{
2410 if (*sel_atom == clip_plus.sel_atom)
2411 clip_lose_selection(&clip_plus);
2412 else
2413 clip_lose_selection(&clip_star);
2414}
2415
2416 void
2417clip_x11_lose_selection(myShell, cbd)
Bram Moolenaar62b42182010-09-21 22:09:37 +02002418 Widget myShell;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 VimClipboard *cbd;
2420{
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002421 XtDisownSelection(myShell, cbd->sel_atom,
2422 XtLastTimestampProcessed(XtDisplay(myShell)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423}
2424
2425 int
2426clip_x11_own_selection(myShell, cbd)
Bram Moolenaar62b42182010-09-21 22:09:37 +02002427 Widget myShell;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 VimClipboard *cbd;
2429{
Bram Moolenaar62b42182010-09-21 22:09:37 +02002430 /* When using the GUI we have proper timestamps, use the one of the last
2431 * event. When in the console we don't get events (the terminal gets
2432 * them), Get the time by a zero-length append, clip_x11_timestamp_cb will
2433 * be called with the current timestamp. */
2434#ifdef FEAT_GUI
2435 if (gui.in_use)
2436 {
2437 if (XtOwnSelection(myShell, cbd->sel_atom,
2438 XtLastTimestampProcessed(XtDisplay(myShell)),
2439 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
2440 NULL) == False)
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +01002441 return FAIL;
Bram Moolenaar62b42182010-09-21 22:09:37 +02002442 }
2443 else
2444#endif
2445 {
2446 if (!XChangeProperty(XtDisplay(myShell), XtWindow(myShell),
2447 cbd->sel_atom, timestamp_atom, 32, PropModeAppend, NULL, 0))
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +01002448 return FAIL;
Bram Moolenaar62b42182010-09-21 22:09:37 +02002449 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002450 /* Flush is required in a terminal as nothing else is doing it. */
2451 XFlush(XtDisplay(myShell));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 return OK;
2453}
2454
2455/*
2456 * Send the current selection to the clipboard. Do nothing for X because we
2457 * will fill in the selection only when requested by another app.
2458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 void
2460clip_x11_set_selection(cbd)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002461 VimClipboard *cbd UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462{
2463}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002464
2465 int
2466clip_x11_owner_exists(cbd)
2467 VimClipboard *cbd;
2468{
2469 return XGetSelectionOwner(X_DISPLAY, cbd->sel_atom) != None;
2470}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471#endif
2472
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002473#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) \
2474 || defined(FEAT_GUI_GTK) || defined(PROTO)
2475/*
2476 * Get the contents of the X CUT_BUFFER0 and put it in "cbd".
2477 */
2478 void
2479yank_cut_buffer0(dpy, cbd)
2480 Display *dpy;
2481 VimClipboard *cbd;
2482{
2483 int nbytes = 0;
2484 char_u *buffer = (char_u *)XFetchBuffer(dpy, &nbytes, 0);
2485
2486 if (nbytes > 0)
2487 {
2488#ifdef FEAT_MBYTE
2489 int done = FALSE;
2490
2491 /* CUT_BUFFER0 is supposed to be always latin1. Convert to 'enc' when
2492 * using a multi-byte encoding. Conversion between two 8-bit
2493 * character sets usually fails and the text might actually be in
2494 * 'enc' anyway. */
2495 if (has_mbyte)
2496 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002497 char_u *conv_buf;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002498 vimconv_T vc;
2499
2500 vc.vc_type = CONV_NONE;
2501 if (convert_setup(&vc, (char_u *)"latin1", p_enc) == OK)
2502 {
2503 conv_buf = string_convert(&vc, buffer, &nbytes);
2504 if (conv_buf != NULL)
2505 {
2506 clip_yank_selection(MCHAR, conv_buf, (long)nbytes, cbd);
2507 vim_free(conv_buf);
2508 done = TRUE;
2509 }
2510 convert_setup(&vc, NULL, NULL);
2511 }
2512 }
2513 if (!done) /* use the text without conversion */
2514#endif
2515 clip_yank_selection(MCHAR, buffer, (long)nbytes, cbd);
2516 XFree((void *)buffer);
2517 if (p_verbose > 0)
2518 {
2519 verbose_enter();
2520 verb_msg((char_u *)_("Used CUT_BUFFER0 instead of empty selection"));
2521 verbose_leave();
2522 }
2523 }
2524}
2525#endif
2526
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527#if defined(FEAT_MOUSE) || defined(PROTO)
2528
2529/*
2530 * Move the cursor to the specified row and column on the screen.
Bram Moolenaar49325942007-05-10 19:19:59 +00002531 * Change current window if necessary. Returns an integer with the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 * CURSOR_MOVED bit set if the cursor has moved or unset otherwise.
2533 *
2534 * The MOUSE_FOLD_CLOSE bit is set when clicked on the '-' in a fold column.
2535 * The MOUSE_FOLD_OPEN bit is set when clicked on the '+' in a fold column.
2536 *
2537 * If flags has MOUSE_FOCUS, then the current window will not be changed, and
2538 * if the mouse is outside the window then the text will scroll, or if the
2539 * mouse was previously on a status line, then the status line may be dragged.
2540 *
2541 * If flags has MOUSE_MAY_VIS, then VIsual mode will be started before the
2542 * cursor is moved unless the cursor was on a status line.
2543 * This function returns one of IN_UNKNOWN, IN_BUFFER, IN_STATUS_LINE or
2544 * IN_SEP_LINE depending on where the cursor was clicked.
2545 *
2546 * If flags has MOUSE_MAY_STOP_VIS, then Visual mode will be stopped, unless
2547 * the mouse is on the status line of the same window.
2548 *
2549 * If flags has MOUSE_DID_MOVE, nothing is done if the mouse didn't move since
2550 * the last call.
2551 *
2552 * If flags has MOUSE_SETPOS, nothing is done, only the current position is
2553 * remembered.
2554 */
2555 int
2556jump_to_mouse(flags, inclusive, which_button)
2557 int flags;
2558 int *inclusive; /* used for inclusive operator, can be NULL */
2559 int which_button; /* MOUSE_LEFT, MOUSE_RIGHT, MOUSE_MIDDLE */
2560{
2561 static int on_status_line = 0; /* #lines below bottom of window */
2562#ifdef FEAT_VERTSPLIT
2563 static int on_sep_line = 0; /* on separator right of window */
2564#endif
2565 static int prev_row = -1;
2566 static int prev_col = -1;
2567 static win_T *dragwin = NULL; /* window being dragged */
2568 static int did_drag = FALSE; /* drag was noticed */
2569
2570 win_T *wp, *old_curwin;
2571 pos_T old_cursor;
2572 int count;
2573 int first;
2574 int row = mouse_row;
2575 int col = mouse_col;
2576#ifdef FEAT_FOLDING
2577 int mouse_char;
2578#endif
2579
2580 mouse_past_bottom = FALSE;
2581 mouse_past_eol = FALSE;
2582
2583 if (flags & MOUSE_RELEASED)
2584 {
2585 /* On button release we may change window focus if positioned on a
2586 * status line and no dragging happened. */
2587 if (dragwin != NULL && !did_drag)
2588 flags &= ~(MOUSE_FOCUS | MOUSE_DID_MOVE);
2589 dragwin = NULL;
2590 did_drag = FALSE;
2591 }
2592
2593 if ((flags & MOUSE_DID_MOVE)
2594 && prev_row == mouse_row
2595 && prev_col == mouse_col)
2596 {
2597retnomove:
Bram Moolenaar49325942007-05-10 19:19:59 +00002598 /* before moving the cursor for a left click which is NOT in a status
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 * line, stop Visual mode */
2600 if (on_status_line)
2601 return IN_STATUS_LINE;
2602#ifdef FEAT_VERTSPLIT
2603 if (on_sep_line)
2604 return IN_SEP_LINE;
2605#endif
2606#ifdef FEAT_VISUAL
2607 if (flags & MOUSE_MAY_STOP_VIS)
2608 {
2609 end_visual_mode();
2610 redraw_curbuf_later(INVERTED); /* delete the inversion */
2611 }
2612#endif
2613#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2614 /* Continue a modeless selection in another window. */
2615 if (cmdwin_type != 0 && row < W_WINROW(curwin))
2616 return IN_OTHER_WIN;
2617#endif
2618 return IN_BUFFER;
2619 }
2620
2621 prev_row = mouse_row;
2622 prev_col = mouse_col;
2623
2624 if (flags & MOUSE_SETPOS)
2625 goto retnomove; /* ugly goto... */
2626
2627#ifdef FEAT_FOLDING
2628 /* Remember the character under the mouse, it might be a '-' or '+' in the
2629 * fold column. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002630 if (row >= 0 && row < Rows && col >= 0 && col <= Columns
2631 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 mouse_char = ScreenLines[LineOffset[row] + col];
2633 else
2634 mouse_char = ' ';
2635#endif
2636
2637 old_curwin = curwin;
2638 old_cursor = curwin->w_cursor;
2639
2640 if (!(flags & MOUSE_FOCUS))
2641 {
2642 if (row < 0 || col < 0) /* check if it makes sense */
2643 return IN_UNKNOWN;
2644
2645#ifdef FEAT_WINDOWS
2646 /* find the window where the row is in */
2647 wp = mouse_find_win(&row, &col);
2648#else
2649 wp = firstwin;
2650#endif
2651 dragwin = NULL;
2652 /*
2653 * winpos and height may change in win_enter()!
2654 */
2655 if (row >= wp->w_height) /* In (or below) status line */
2656 {
2657 on_status_line = row - wp->w_height + 1;
2658 dragwin = wp;
2659 }
2660 else
2661 on_status_line = 0;
2662#ifdef FEAT_VERTSPLIT
2663 if (col >= wp->w_width) /* In separator line */
2664 {
2665 on_sep_line = col - wp->w_width + 1;
2666 dragwin = wp;
2667 }
2668 else
2669 on_sep_line = 0;
2670
2671 /* The rightmost character of the status line might be a vertical
2672 * separator character if there is no connecting window to the right. */
2673 if (on_status_line && on_sep_line)
2674 {
2675 if (stl_connected(wp))
2676 on_sep_line = 0;
2677 else
2678 on_status_line = 0;
2679 }
2680#endif
2681
2682#ifdef FEAT_VISUAL
2683 /* Before jumping to another buffer, or moving the cursor for a left
2684 * click, stop Visual mode. */
2685 if (VIsual_active
2686 && (wp->w_buffer != curwin->w_buffer
2687 || (!on_status_line
2688# ifdef FEAT_VERTSPLIT
2689 && !on_sep_line
2690# endif
2691# ifdef FEAT_FOLDING
2692 && (
2693# ifdef FEAT_RIGHTLEFT
2694 wp->w_p_rl ? col < W_WIDTH(wp) - wp->w_p_fdc :
2695# endif
2696 col >= wp->w_p_fdc
2697# ifdef FEAT_CMDWIN
2698 + (cmdwin_type == 0 && wp == curwin ? 0 : 1)
2699# endif
2700 )
2701# endif
2702 && (flags & MOUSE_MAY_STOP_VIS))))
2703 {
2704 end_visual_mode();
2705 redraw_curbuf_later(INVERTED); /* delete the inversion */
2706 }
2707#endif
2708#ifdef FEAT_CMDWIN
2709 if (cmdwin_type != 0 && wp != curwin)
2710 {
2711 /* A click outside the command-line window: Use modeless
Bram Moolenaarf679a432010-03-02 18:16:09 +01002712 * selection if possible. Allow dragging the status lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713# ifdef FEAT_VERTSPLIT
2714 on_sep_line = 0;
2715# endif
2716# ifdef FEAT_CLIPBOARD
2717 if (on_status_line)
2718 return IN_STATUS_LINE;
2719 return IN_OTHER_WIN;
2720# else
2721 row = 0;
2722 col += wp->w_wincol;
2723 wp = curwin;
2724# endif
2725 }
2726#endif
2727#ifdef FEAT_WINDOWS
2728 /* Only change window focus when not clicking on or dragging the
2729 * status line. Do change focus when releasing the mouse button
2730 * (MOUSE_FOCUS was set above if we dragged first). */
2731 if (dragwin == NULL || (flags & MOUSE_RELEASED))
2732 win_enter(wp, TRUE); /* can make wp invalid! */
2733# ifdef CHECK_DOUBLE_CLICK
2734 /* set topline, to be able to check for double click ourselves */
2735 if (curwin != old_curwin)
2736 set_mouse_topline(curwin);
2737# endif
2738#endif
2739 if (on_status_line) /* In (or below) status line */
2740 {
2741 /* Don't use start_arrow() if we're in the same window */
2742 if (curwin == old_curwin)
2743 return IN_STATUS_LINE;
2744 else
2745 return IN_STATUS_LINE | CURSOR_MOVED;
2746 }
2747#ifdef FEAT_VERTSPLIT
2748 if (on_sep_line) /* In (or below) status line */
2749 {
2750 /* Don't use start_arrow() if we're in the same window */
2751 if (curwin == old_curwin)
2752 return IN_SEP_LINE;
2753 else
2754 return IN_SEP_LINE | CURSOR_MOVED;
2755 }
2756#endif
2757
2758 curwin->w_cursor.lnum = curwin->w_topline;
2759#ifdef FEAT_GUI
2760 /* remember topline, needed for double click */
2761 gui_prev_topline = curwin->w_topline;
2762# ifdef FEAT_DIFF
2763 gui_prev_topfill = curwin->w_topfill;
2764# endif
2765#endif
2766 }
2767 else if (on_status_line && which_button == MOUSE_LEFT)
2768 {
2769#ifdef FEAT_WINDOWS
2770 if (dragwin != NULL)
2771 {
2772 /* Drag the status line */
2773 count = row - dragwin->w_winrow - dragwin->w_height + 1
2774 - on_status_line;
2775 win_drag_status_line(dragwin, count);
2776 did_drag |= count;
2777 }
2778#endif
2779 return IN_STATUS_LINE; /* Cursor didn't move */
2780 }
2781#ifdef FEAT_VERTSPLIT
2782 else if (on_sep_line && which_button == MOUSE_LEFT)
2783 {
2784 if (dragwin != NULL)
2785 {
2786 /* Drag the separator column */
2787 count = col - dragwin->w_wincol - dragwin->w_width + 1
2788 - on_sep_line;
2789 win_drag_vsep_line(dragwin, count);
2790 did_drag |= count;
2791 }
2792 return IN_SEP_LINE; /* Cursor didn't move */
2793 }
2794#endif
2795 else /* keep_window_focus must be TRUE */
2796 {
2797#ifdef FEAT_VISUAL
2798 /* before moving the cursor for a left click, stop Visual mode */
2799 if (flags & MOUSE_MAY_STOP_VIS)
2800 {
2801 end_visual_mode();
2802 redraw_curbuf_later(INVERTED); /* delete the inversion */
2803 }
2804#endif
2805
2806#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2807 /* Continue a modeless selection in another window. */
2808 if (cmdwin_type != 0 && row < W_WINROW(curwin))
2809 return IN_OTHER_WIN;
2810#endif
2811
2812 row -= W_WINROW(curwin);
2813#ifdef FEAT_VERTSPLIT
2814 col -= W_WINCOL(curwin);
2815#endif
2816
2817 /*
2818 * When clicking beyond the end of the window, scroll the screen.
2819 * Scroll by however many rows outside the window we are.
2820 */
2821 if (row < 0)
2822 {
2823 count = 0;
2824 for (first = TRUE; curwin->w_topline > 1; )
2825 {
2826#ifdef FEAT_DIFF
2827 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
2828 ++count;
2829 else
2830#endif
2831 count += plines(curwin->w_topline - 1);
2832 if (!first && count > -row)
2833 break;
2834 first = FALSE;
2835#ifdef FEAT_FOLDING
2836 hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2837#endif
2838#ifdef FEAT_DIFF
2839 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
2840 ++curwin->w_topfill;
2841 else
2842#endif
2843 {
2844 --curwin->w_topline;
2845#ifdef FEAT_DIFF
2846 curwin->w_topfill = 0;
2847#endif
2848 }
2849 }
2850#ifdef FEAT_DIFF
2851 check_topfill(curwin, FALSE);
2852#endif
2853 curwin->w_valid &=
2854 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2855 redraw_later(VALID);
2856 row = 0;
2857 }
2858 else if (row >= curwin->w_height)
2859 {
2860 count = 0;
2861 for (first = TRUE; curwin->w_topline < curbuf->b_ml.ml_line_count; )
2862 {
2863#ifdef FEAT_DIFF
2864 if (curwin->w_topfill > 0)
2865 ++count;
2866 else
2867#endif
2868 count += plines(curwin->w_topline);
2869 if (!first && count > row - curwin->w_height + 1)
2870 break;
2871 first = FALSE;
2872#ifdef FEAT_FOLDING
2873 if (hasFolding(curwin->w_topline, NULL, &curwin->w_topline)
2874 && curwin->w_topline == curbuf->b_ml.ml_line_count)
2875 break;
2876#endif
2877#ifdef FEAT_DIFF
2878 if (curwin->w_topfill > 0)
2879 --curwin->w_topfill;
2880 else
2881#endif
2882 {
2883 ++curwin->w_topline;
2884#ifdef FEAT_DIFF
2885 curwin->w_topfill =
2886 diff_check_fill(curwin, curwin->w_topline);
2887#endif
2888 }
2889 }
2890#ifdef FEAT_DIFF
2891 check_topfill(curwin, FALSE);
2892#endif
2893 redraw_later(VALID);
2894 curwin->w_valid &=
2895 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2896 row = curwin->w_height - 1;
2897 }
2898 else if (row == 0)
2899 {
2900 /* When dragging the mouse, while the text has been scrolled up as
2901 * far as it goes, moving the mouse in the top line should scroll
2902 * the text down (done later when recomputing w_topline). */
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00002903 if (mouse_dragging > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 && curwin->w_cursor.lnum
2905 == curwin->w_buffer->b_ml.ml_line_count
2906 && curwin->w_cursor.lnum == curwin->w_topline)
2907 curwin->w_valid &= ~(VALID_TOPLINE);
2908 }
2909 }
2910
2911#ifdef FEAT_FOLDING
2912 /* Check for position outside of the fold column. */
2913 if (
2914# ifdef FEAT_RIGHTLEFT
2915 curwin->w_p_rl ? col < W_WIDTH(curwin) - curwin->w_p_fdc :
2916# endif
2917 col >= curwin->w_p_fdc
2918# ifdef FEAT_CMDWIN
2919 + (cmdwin_type == 0 ? 0 : 1)
2920# endif
2921 )
2922 mouse_char = ' ';
2923#endif
2924
2925 /* compute the position in the buffer line from the posn on the screen */
2926 if (mouse_comp_pos(curwin, &row, &col, &curwin->w_cursor.lnum))
2927 mouse_past_bottom = TRUE;
2928
2929#ifdef FEAT_VISUAL
2930 /* Start Visual mode before coladvance(), for when 'sel' != "old" */
2931 if ((flags & MOUSE_MAY_VIS) && !VIsual_active)
2932 {
2933 check_visual_highlight();
2934 VIsual = old_cursor;
2935 VIsual_active = TRUE;
2936 VIsual_reselect = TRUE;
2937 /* if 'selectmode' contains "mouse", start Select mode */
2938 may_start_select('o');
2939 setmouse();
Bram Moolenaar7df351e2006-01-23 22:30:28 +00002940 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 redraw_cmdline = TRUE; /* show visual mode later */
2942 }
2943#endif
2944
2945 curwin->w_curswant = col;
2946 curwin->w_set_curswant = FALSE; /* May still have been TRUE */
2947 if (coladvance(col) == FAIL) /* Mouse click beyond end of line */
2948 {
2949 if (inclusive != NULL)
2950 *inclusive = TRUE;
2951 mouse_past_eol = TRUE;
2952 }
2953 else if (inclusive != NULL)
2954 *inclusive = FALSE;
2955
2956 count = IN_BUFFER;
2957 if (curwin != old_curwin || curwin->w_cursor.lnum != old_cursor.lnum
2958 || curwin->w_cursor.col != old_cursor.col)
2959 count |= CURSOR_MOVED; /* Cursor has moved */
2960
2961#ifdef FEAT_FOLDING
2962 if (mouse_char == '+')
2963 count |= MOUSE_FOLD_OPEN;
2964 else if (mouse_char != ' ')
2965 count |= MOUSE_FOLD_CLOSE;
2966#endif
2967
2968 return count;
2969}
2970
2971/*
2972 * Compute the position in the buffer line from the posn on the screen in
2973 * window "win".
2974 * Returns TRUE if the position is below the last line.
2975 */
2976 int
2977mouse_comp_pos(win, rowp, colp, lnump)
2978 win_T *win;
2979 int *rowp;
2980 int *colp;
2981 linenr_T *lnump;
2982{
2983 int col = *colp;
2984 int row = *rowp;
2985 linenr_T lnum;
2986 int retval = FALSE;
2987 int off;
2988 int count;
2989
2990#ifdef FEAT_RIGHTLEFT
2991 if (win->w_p_rl)
2992 col = W_WIDTH(win) - 1 - col;
2993#endif
2994
2995 lnum = win->w_topline;
2996
2997 while (row > 0)
2998 {
2999#ifdef FEAT_DIFF
3000 /* Don't include filler lines in "count" */
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003001 if (win->w_p_diff
3002# ifdef FEAT_FOLDING
3003 && !hasFoldingWin(win, lnum, NULL, NULL, TRUE, NULL)
3004# endif
3005 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 {
3007 if (lnum == win->w_topline)
3008 row -= win->w_topfill;
3009 else
3010 row -= diff_check_fill(win, lnum);
3011 count = plines_win_nofill(win, lnum, TRUE);
3012 }
3013 else
3014#endif
3015 count = plines_win(win, lnum, TRUE);
3016 if (count > row)
3017 break; /* Position is in this buffer line. */
3018#ifdef FEAT_FOLDING
3019 (void)hasFoldingWin(win, lnum, NULL, &lnum, TRUE, NULL);
3020#endif
3021 if (lnum == win->w_buffer->b_ml.ml_line_count)
3022 {
3023 retval = TRUE;
3024 break; /* past end of file */
3025 }
3026 row -= count;
3027 ++lnum;
3028 }
3029
3030 if (!retval)
3031 {
3032 /* Compute the column without wrapping. */
3033 off = win_col_off(win) - win_col_off2(win);
3034 if (col < off)
3035 col = off;
3036 col += row * (W_WIDTH(win) - off);
3037 /* add skip column (for long wrapping line) */
3038 col += win->w_skipcol;
3039 }
3040
3041 if (!win->w_p_wrap)
3042 col += win->w_leftcol;
3043
3044 /* skip line number and fold column in front of the line */
3045 col -= win_col_off(win);
3046 if (col < 0)
3047 {
3048#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003049 netbeans_gutter_click(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050#endif
3051 col = 0;
3052 }
3053
3054 *colp = col;
3055 *rowp = row;
3056 *lnump = lnum;
3057 return retval;
3058}
3059
3060#if defined(FEAT_WINDOWS) || defined(PROTO)
3061/*
3062 * Find the window at screen position "*rowp" and "*colp". The positions are
3063 * updated to become relative to the top-left of the window.
3064 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 win_T *
3066mouse_find_win(rowp, colp)
3067 int *rowp;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003068 int *colp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069{
3070 frame_T *fp;
3071
3072 fp = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003073 *rowp -= firstwin->w_winrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 for (;;)
3075 {
3076 if (fp->fr_layout == FR_LEAF)
3077 break;
3078#ifdef FEAT_VERTSPLIT
3079 if (fp->fr_layout == FR_ROW)
3080 {
3081 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
3082 {
3083 if (*colp < fp->fr_width)
3084 break;
3085 *colp -= fp->fr_width;
3086 }
3087 }
3088#endif
3089 else /* fr_layout == FR_COL */
3090 {
3091 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
3092 {
3093 if (*rowp < fp->fr_height)
3094 break;
3095 *rowp -= fp->fr_height;
3096 }
3097 }
3098 }
3099 return fp->fr_win;
3100}
3101#endif
3102
Bram Moolenaar860cae12010-06-05 23:22:07 +02003103#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
3105 || defined(FEAT_GUI_PHOTON) || defined(PROTO)
3106/*
3107 * Translate window coordinates to buffer position without any side effects
3108 */
3109 int
3110get_fpos_of_mouse(mpos)
3111 pos_T *mpos;
3112{
3113 win_T *wp;
3114 int row = mouse_row;
3115 int col = mouse_col;
3116
3117 if (row < 0 || col < 0) /* check if it makes sense */
3118 return IN_UNKNOWN;
3119
3120#ifdef FEAT_WINDOWS
3121 /* find the window where the row is in */
3122 wp = mouse_find_win(&row, &col);
3123#else
3124 wp = firstwin;
3125#endif
3126 /*
3127 * winpos and height may change in win_enter()!
3128 */
3129 if (row >= wp->w_height) /* In (or below) status line */
3130 return IN_STATUS_LINE;
3131#ifdef FEAT_VERTSPLIT
3132 if (col >= wp->w_width) /* In vertical separator line */
3133 return IN_SEP_LINE;
3134#endif
3135
3136 if (wp != curwin)
3137 return IN_UNKNOWN;
3138
3139 /* compute the position in the buffer line from the posn on the screen */
3140 if (mouse_comp_pos(curwin, &row, &col, &mpos->lnum))
3141 return IN_STATUS_LINE; /* past bottom */
3142
3143 mpos->col = vcol2col(wp, mpos->lnum, col);
3144
3145 if (mpos->col > 0)
3146 --mpos->col;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003147#ifdef FEAT_VIRTUALEDIT
3148 mpos->coladd = 0;
3149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 return IN_BUFFER;
3151}
3152
3153/*
3154 * Convert a virtual (screen) column to a character column.
3155 * The first column is one.
3156 */
3157 int
3158vcol2col(wp, lnum, vcol)
3159 win_T *wp;
3160 linenr_T lnum;
3161 int vcol;
3162{
3163 /* try to advance to the specified column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 int count = 0;
3165 char_u *ptr;
Bram Moolenaar86c800a2009-09-11 14:48:27 +00003166 char_u *start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167
Bram Moolenaar86c800a2009-09-11 14:48:27 +00003168 start = ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaarb6101cf2012-10-21 00:58:39 +02003169 while (count < vcol && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 count += win_lbr_chartabsize(wp, ptr, count, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003172 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 }
Bram Moolenaar86c800a2009-09-11 14:48:27 +00003174 return (int)(ptr - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175}
3176#endif
3177
3178#endif /* FEAT_MOUSE */
3179
3180#if defined(FEAT_GUI) || defined(WIN3264) || defined(PROTO)
3181/*
3182 * Called when focus changed. Used for the GUI or for systems where this can
3183 * be done in the console (Win32).
3184 */
3185 void
3186ui_focus_change(in_focus)
3187 int in_focus; /* TRUE if focus gained. */
3188{
3189 static time_t last_time = (time_t)0;
3190 int need_redraw = FALSE;
3191
3192 /* When activated: Check if any file was modified outside of Vim.
3193 * Only do this when not done within the last two seconds (could get
3194 * several events in a row). */
3195 if (in_focus && last_time + 2 < time(NULL))
3196 {
3197 need_redraw = check_timestamps(
3198# ifdef FEAT_GUI
3199 gui.in_use
3200# else
3201 FALSE
3202# endif
3203 );
3204 last_time = time(NULL);
3205 }
3206
3207#ifdef FEAT_AUTOCMD
3208 /*
3209 * Fire the focus gained/lost autocommand.
3210 */
3211 need_redraw |= apply_autocmds(in_focus ? EVENT_FOCUSGAINED
3212 : EVENT_FOCUSLOST, NULL, NULL, FALSE, curbuf);
3213#endif
3214
3215 if (need_redraw)
3216 {
3217 /* Something was executed, make sure the cursor is put back where it
3218 * belongs. */
3219 need_wait_return = FALSE;
3220
3221 if (State & CMDLINE)
3222 redrawcmdline();
3223 else if (State == HITRETURN || State == SETWSIZE || State == ASKMORE
3224 || State == EXTERNCMD || State == CONFIRM || exmode_active)
3225 repeat_message();
3226 else if ((State & NORMAL) || (State & INSERT))
3227 {
3228 if (must_redraw != 0)
3229 update_screen(0);
3230 setcursor();
3231 }
3232 cursor_on(); /* redrawing may have switched it off */
3233 out_flush();
3234# ifdef FEAT_GUI
3235 if (gui.in_use)
3236 {
3237 gui_update_cursor(FALSE, TRUE);
3238 gui_update_scrollbars(FALSE);
3239 }
3240# endif
3241 }
3242#ifdef FEAT_TITLE
3243 /* File may have been changed from 'readonly' to 'noreadonly' */
3244 if (need_maketitle)
3245 maketitle();
3246#endif
3247}
3248#endif
3249
3250#if defined(USE_IM_CONTROL) || defined(PROTO)
3251/*
3252 * Save current Input Method status to specified place.
3253 */
3254 void
3255im_save_status(psave)
3256 long *psave;
3257{
3258 /* Don't save when 'imdisable' is set or "xic" is NULL, IM is always
3259 * disabled then (but might start later).
3260 * Also don't save when inside a mapping, vgetc_im_active has not been set
3261 * then.
3262 * And don't save when the keys were stuffed (e.g., for a "." command).
3263 * And don't save when the GUI is running but our window doesn't have
3264 * input focus (e.g., when a find dialog is open). */
3265 if (!p_imdisable && KeyTyped && !KeyStuffed
3266# ifdef FEAT_XIM
3267 && xic != NULL
3268# endif
3269# ifdef FEAT_GUI
3270 && (!gui.in_use || gui.in_focus)
3271# endif
3272 )
3273 {
3274 /* Do save when IM is on, or IM is off and saved status is on. */
3275 if (vgetc_im_active)
3276 *psave = B_IMODE_IM;
3277 else if (*psave == B_IMODE_IM)
3278 *psave = B_IMODE_NONE;
3279 }
3280}
3281#endif