blob: 5ae0ae38b7f6cd781355bf7c76c0e6420024a50e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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
Bram Moolenaar2c6f3dc2013-07-05 20:09:16 +020021#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
22# define WIN32_LEAN_AND_MEAN
23# include <windows.h>
24# include "winclip.pro"
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010028ui_write(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000029{
30#ifdef FEAT_GUI
31 if (gui.in_use && !gui.dying && !gui.starting)
32 {
33 gui_write(s, len);
34 if (p_wd)
Bram Moolenaarc9e649a2017-12-18 18:14:47 +010035 gui_wait_for_chars(p_wd, typebuf.tb_change_cnt);
Bram Moolenaar071d4272004-06-13 20:20:40 +000036 return;
37 }
38#endif
39#ifndef NO_CONSOLE
40 /* Don't output anything in silent mode ("ex -s") unless 'verbose' set */
41 if (!(silent_mode && p_verbose == 0))
42 {
Bram Moolenaarac360bf2015-09-01 20:31:20 +020043#if defined(FEAT_MBYTE) && !defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +000044 char_u *tofree = NULL;
45
46 if (output_conv.vc_type != CONV_NONE)
47 {
48 /* Convert characters from 'encoding' to 'termencoding'. */
49 tofree = string_convert(&output_conv, s, &len);
50 if (tofree != NULL)
51 s = tofree;
52 }
53#endif
54
55 mch_write(s, len);
56
Bram Moolenaarac360bf2015-09-01 20:31:20 +020057#if defined(FEAT_MBYTE) && !defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +000058 if (output_conv.vc_type != CONV_NONE)
59 vim_free(tofree);
60#endif
61 }
62#endif
63}
64
Bram Moolenaar4b9669f2011-07-07 16:20:52 +020065#if defined(UNIX) || defined(VMS) || defined(PROTO) || defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +000066/*
67 * When executing an external program, there may be some typed characters that
68 * are not consumed by it. Give them back to ui_inchar() and they are stored
69 * here for the next call.
70 */
71static char_u *ta_str = NULL;
72static int ta_off; /* offset for next char to use when ta_str != NULL */
73static int ta_len; /* length of ta_str when it's not NULL*/
74
75 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010076ui_inchar_undo(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000077{
78 char_u *new;
79 int newlen;
80
81 newlen = len;
82 if (ta_str != NULL)
83 newlen += ta_len - ta_off;
84 new = alloc(newlen);
85 if (new != NULL)
86 {
87 if (ta_str != NULL)
88 {
89 mch_memmove(new, ta_str + ta_off, (size_t)(ta_len - ta_off));
90 mch_memmove(new + ta_len - ta_off, s, (size_t)len);
91 vim_free(ta_str);
92 }
93 else
94 mch_memmove(new, s, (size_t)len);
95 ta_str = new;
96 ta_len = newlen;
97 ta_off = 0;
98 }
99}
100#endif
101
102/*
Bram Moolenaarb6101cf2012-10-21 00:58:39 +0200103 * ui_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104 * Get characters from the keyboard.
105 * Return the number of characters that are available.
106 * If "wtime" == 0 do not wait for characters.
107 * If "wtime" == -1 wait forever for characters.
108 * If "wtime" > 0 wait "wtime" milliseconds for a character.
109 *
110 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
111 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
112 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is NULL
113 * otherwise.
114 */
115 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100116ui_inchar(
117 char_u *buf,
118 int maxlen,
119 long wtime, /* don't use "time", MIPS cannot handle it */
120 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121{
122 int retval = 0;
123
124#if defined(FEAT_GUI) && (defined(UNIX) || defined(VMS))
125 /*
126 * Use the typeahead if there is any.
127 */
128 if (ta_str != NULL)
129 {
130 if (maxlen >= ta_len - ta_off)
131 {
132 mch_memmove(buf, ta_str + ta_off, (size_t)ta_len);
Bram Moolenaard23a8232018-02-10 18:45:26 +0100133 VIM_CLEAR(ta_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 return ta_len;
135 }
136 mch_memmove(buf, ta_str + ta_off, (size_t)maxlen);
137 ta_off += maxlen;
138 return maxlen;
139 }
140#endif
141
Bram Moolenaar05159a02005-02-26 23:04:13 +0000142#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000143 if (do_profiling == PROF_YES && wtime != 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000144 prof_inchar_enter();
145#endif
146
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147#ifdef NO_CONSOLE_INPUT
148 /* Don't wait for character input when the window hasn't been opened yet.
149 * Do try reading, this works when redirecting stdin from a file.
150 * Must return something, otherwise we'll loop forever. If we run into
151 * this very often we probably got stuck, exit Vim. */
152 if (no_console_input())
153 {
154 static int count = 0;
155
156# ifndef NO_CONSOLE
Bram Moolenaar43b604c2005-03-22 23:06:55 +0000157 retval = mch_inchar(buf, maxlen, (wtime >= 0 && wtime < 10)
158 ? 10L : wtime, tb_change_cnt);
159 if (retval > 0 || typebuf_changed(tb_change_cnt) || wtime >= 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000160 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161# endif
162 if (wtime == -1 && ++count == 1000)
163 read_error_exit();
164 buf[0] = CAR;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000165 retval = 1;
166 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 }
168#endif
169
Bram Moolenaarc8da3112007-03-08 12:36:46 +0000170 /* If we are going to wait for some time or block... */
171 if (wtime == -1 || wtime > 100L)
172 {
173 /* ... allow signals to kill us. */
174 (void)vim_handle_signal(SIGNAL_UNBLOCK);
175
176 /* ... there is no need for CTRL-C to interrupt something, don't let
177 * it set got_int when it was mapped. */
Bram Moolenaar50008692015-01-14 16:08:32 +0100178 if ((mapped_ctrl_c | curbuf->b_mapped_ctrl_c) & get_real_state())
Bram Moolenaarc8da3112007-03-08 12:36:46 +0000179 ctrl_c_interrupts = FALSE;
180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181
182#ifdef FEAT_GUI
183 if (gui.in_use)
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100184 retval = gui_inchar(buf, maxlen, wtime, tb_change_cnt);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#endif
186#ifndef NO_CONSOLE
187# ifdef FEAT_GUI
188 else
189# endif
190 retval = mch_inchar(buf, maxlen, wtime, tb_change_cnt);
191#endif
192
Bram Moolenaarc8da3112007-03-08 12:36:46 +0000193 if (wtime == -1 || wtime > 100L)
194 /* block SIGHUP et al. */
195 (void)vim_handle_signal(SIGNAL_BLOCK);
196
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 ctrl_c_interrupts = TRUE;
198
Bram Moolenaar05159a02005-02-26 23:04:13 +0000199#ifdef NO_CONSOLE_INPUT
200theend:
201#endif
202#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000203 if (do_profiling == PROF_YES && wtime != 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000204 prof_inchar_exit();
205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 return retval;
207}
208
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100209#if defined(FEAT_TIMERS) || defined(PROT)
210/*
211 * Wait for a timer to fire or "wait_func" to return non-zero.
212 * Returns OK when something was read.
213 * Returns FAIL when it timed out or was interrupted.
214 */
215 int
216ui_wait_for_chars_or_timer(
217 long wtime,
218 int (*wait_func)(long wtime, int *interrupted, int ignore_input),
219 int *interrupted,
220 int ignore_input)
221{
222 int due_time;
223 long remaining = wtime;
224 int tb_change_cnt = typebuf.tb_change_cnt;
225
226 /* When waiting very briefly don't trigger timers. */
227 if (wtime >= 0 && wtime < 10L)
228 return wait_func(wtime, NULL, ignore_input);
229
230 while (wtime < 0 || remaining > 0)
231 {
232 /* Trigger timers and then get the time in wtime until the next one is
233 * due. Wait up to that time. */
234 due_time = check_due_timer();
235 if (typebuf.tb_change_cnt != tb_change_cnt)
236 {
237 /* timer may have used feedkeys() */
238 return FAIL;
239 }
240 if (due_time <= 0 || (wtime > 0 && due_time > remaining))
241 due_time = remaining;
242 if (wait_func(due_time, interrupted, ignore_input))
243 return OK;
244 if (interrupted != NULL && *interrupted)
245 /* Nothing available, but need to return so that side effects get
246 * handled, such as handling a message on a channel. */
Bram Moolenaara338adc2018-01-31 20:51:47 +0100247 return FAIL;
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100248 if (wtime > 0)
249 remaining -= due_time;
250 }
251 return FAIL;
252}
253#endif
254
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255/*
256 * return non-zero if a character is available
257 */
258 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100259ui_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260{
261#ifdef FEAT_GUI
262 if (gui.in_use)
263 {
264 gui_mch_update();
265 return input_available();
266 }
267#endif
268#ifndef NO_CONSOLE
269# ifdef NO_CONSOLE_INPUT
270 if (no_console_input())
271 return 0;
272# endif
273 return mch_char_avail();
274#else
275 return 0;
276#endif
277}
278
279/*
280 * Delay for the given number of milliseconds. If ignoreinput is FALSE then we
281 * cancel the delay if a key is hit.
282 */
283 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100284ui_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285{
286#ifdef FEAT_GUI
287 if (gui.in_use && !ignoreinput)
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100288 gui_wait_for_chars(msec, typebuf.tb_change_cnt);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 else
290#endif
291 mch_delay(msec, ignoreinput);
292}
293
294/*
295 * If the machine has job control, use it to suspend the program,
296 * otherwise fake it by starting a new shell.
297 * When running the GUI iconify the window.
298 */
299 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100300ui_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301{
302#ifdef FEAT_GUI
303 if (gui.in_use)
304 {
305 gui_mch_iconify();
306 return;
307 }
308#endif
309 mch_suspend();
310}
311
312#if !defined(UNIX) || !defined(SIGTSTP) || defined(PROTO) || defined(__BEOS__)
313/*
314 * When the OS can't really suspend, call this function to start a shell.
315 * This is never called in the GUI.
316 */
317 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100318suspend_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319{
320 if (*p_sh == NUL)
321 EMSG(_(e_shellempty));
322 else
323 {
324 MSG_PUTS(_("new shell started\n"));
325 do_shell(NULL, 0);
326 }
327}
328#endif
329
330/*
331 * Try to get the current Vim shell size. Put the result in Rows and Columns.
332 * Use the new sizes as defaults for 'columns' and 'lines'.
333 * Return OK when size could be determined, FAIL otherwise.
334 */
335 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100336ui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337{
338 int retval;
339
340#ifdef FEAT_GUI
341 if (gui.in_use)
342 retval = gui_get_shellsize();
343 else
344#endif
345 retval = mch_get_shellsize();
346
347 check_shellsize();
348
349 /* adjust the default for 'lines' and 'columns' */
350 if (retval == OK)
351 {
352 set_number_default("lines", Rows);
353 set_number_default("columns", Columns);
354 }
355 return retval;
356}
357
358/*
359 * Set the size of the Vim shell according to Rows and Columns, if possible.
360 * The gui_set_shellsize() or mch_set_shellsize() function will try to set the
361 * new size. If this is not possible, it will adjust Rows and Columns.
362 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100364ui_set_shellsize(
365 int mustset UNUSED) /* set by the user */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366{
367#ifdef FEAT_GUI
368 if (gui.in_use)
Bram Moolenaar8968a312013-07-03 16:58:44 +0200369 gui_set_shellsize(mustset, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 else
371#endif
372 mch_set_shellsize();
373}
374
375/*
376 * Called when Rows and/or Columns changed. Adjust scroll region and mouse
377 * region.
378 */
379 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100380ui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381{
382 if (full_screen && !exiting)
383 {
384#ifdef FEAT_GUI
385 if (gui.in_use)
386 gui_new_shellsize();
387 else
388#endif
389 mch_new_shellsize();
390 }
391}
392
393 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100394ui_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200396 ui_breakcheck_force(FALSE);
397}
398
399/*
400 * When "force" is true also check when the terminal is not in raw mode.
401 * This is useful to read input on channels.
402 */
403 void
404ui_breakcheck_force(int force)
405{
Bram Moolenaar48d23bb2018-11-20 02:42:43 +0100406 static int recursive = FALSE;
407 int save_updating_screen = updating_screen;
Bram Moolenaare3caa112017-01-31 22:07:42 +0100408
Bram Moolenaar48d23bb2018-11-20 02:42:43 +0100409 // We could be called recursively if stderr is redirected, calling
410 // fill_input_buf() calls settmode() when stdin isn't a tty. settmode()
411 // calls vgetorpeek() which calls ui_breakcheck() again.
412 if (recursive)
413 return;
414 recursive = TRUE;
415
416 // We do not want gui_resize_shell() to redraw the screen here.
Bram Moolenaare3caa112017-01-31 22:07:42 +0100417 ++updating_screen;
418
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419#ifdef FEAT_GUI
420 if (gui.in_use)
421 gui_mch_update();
422 else
423#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200424 mch_breakcheck(force);
Bram Moolenaare3caa112017-01-31 22:07:42 +0100425
Bram Moolenaar42335f52018-09-13 15:33:43 +0200426 if (save_updating_screen)
427 updating_screen = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200428 else
429 reset_updating_screen(FALSE);
Bram Moolenaar48d23bb2018-11-20 02:42:43 +0100430
431 recursive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432}
433
434/*****************************************************************************
435 * Functions for copying and pasting text between applications.
436 * This is always included in a GUI version, but may also be included when the
437 * clipboard and mouse is available to a terminal version such as xterm.
438 * Note: there are some more functions in ops.c that handle selection stuff.
439 *
440 * Also note that the majority of functions here deal with the X 'primary'
441 * (visible - for Visual mode use) selection, and only that. There are no
442 * versions of these for the 'clipboard' selection, as Visual mode has no use
443 * for them.
444 */
445
446#if defined(FEAT_CLIPBOARD) || defined(PROTO)
447
448/*
449 * Selection stuff using Visual mode, for cutting and pasting text to other
450 * windows.
451 */
452
453/*
454 * Call this to initialise the clipboard. Pass it FALSE if the clipboard code
455 * is included, but the clipboard can not be used, or TRUE if the clipboard can
456 * be used. Eg unix may call this with FALSE, then call it again with TRUE if
457 * the GUI starts.
458 */
459 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100460clip_init(int can_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461{
462 VimClipboard *cb;
463
464 cb = &clip_star;
465 for (;;)
466 {
467 cb->available = can_use;
468 cb->owned = FALSE;
469 cb->start.lnum = 0;
470 cb->start.col = 0;
471 cb->end.lnum = 0;
472 cb->end.col = 0;
473 cb->state = SELECT_CLEARED;
474
475 if (cb == &clip_plus)
476 break;
477 cb = &clip_plus;
478 }
479}
480
481/*
482 * Check whether the VIsual area has changed, and if so try to become the owner
483 * of the selection, and free any old converted selection we may still have
484 * lying around. If the VIsual mode has ended, make a copy of what was
485 * selected so we can still give it to others. Will probably have to make sure
486 * this is called whenever VIsual mode is ended.
487 */
488 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100489clip_update_selection(VimClipboard *clip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490{
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200491 pos_T start, end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492
493 /* If visual mode is only due to a redo command ("."), then ignore it */
494 if (!redo_VIsual_busy && VIsual_active && (State & NORMAL))
495 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100496 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 {
498 start = VIsual;
499 end = curwin->w_cursor;
500#ifdef FEAT_MBYTE
501 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000502 end.col += (*mb_ptr2len)(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503#endif
504 }
505 else
506 {
507 start = curwin->w_cursor;
508 end = VIsual;
509 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100510 if (!EQUAL_POS(clip->start, start)
511 || !EQUAL_POS(clip->end, end)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200512 || clip->vmode != VIsual_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200514 clip_clear_selection(clip);
515 clip->start = start;
516 clip->end = end;
517 clip->vmode = VIsual_mode;
518 clip_free_selection(clip);
519 clip_own_selection(clip);
520 clip_gen_set_selection(clip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 }
522 }
523}
524
525 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100526clip_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527{
528 /*
529 * Also want to check somehow that we are reading from the keyboard rather
530 * than a mapping etc.
531 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532#ifdef FEAT_X11
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200533 /* Always own the selection, we might have lost it without being
Bram Moolenaar62b42182010-09-21 22:09:37 +0200534 * notified, e.g. during a ":sh" command. */
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200535 if (cbd->available)
536 {
537 int was_owned = cbd->owned;
538
539 cbd->owned = (clip_gen_own_selection(cbd) == OK);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200540 if (!was_owned && (cbd == &clip_star || cbd == &clip_plus))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 {
Bram Moolenaarebefac62005-12-28 22:39:57 +0000542 /* May have to show a different kind of highlighting for the
543 * selected area. There is no specific redraw command for this,
544 * just redraw all windows on the current buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 if (cbd->owned
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000546 && (get_real_state() == VISUAL
547 || get_real_state() == SELECTMODE)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200548 && (cbd == &clip_star ? clip_isautosel_star()
549 : clip_isautosel_plus())
Bram Moolenaar8820b482017-03-16 17:23:31 +0100550 && HL_ATTR(HLF_V) != HL_ATTR(HLF_VNC))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 redraw_curbuf_later(INVERTED_ALL);
552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200554#else
Bram Moolenaarb6101cf2012-10-21 00:58:39 +0200555 /* Only own the clipboard when we didn't own it yet. */
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200556 if (!cbd->owned && cbd->available)
557 cbd->owned = (clip_gen_own_selection(cbd) == OK);
558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559}
560
561 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100562clip_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563{
564#ifdef FEAT_X11
565 int was_owned = cbd->owned;
566#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200567 int visual_selection = FALSE;
568
569 if (cbd == &clip_star || cbd == &clip_plus)
570 visual_selection = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571
572 clip_free_selection(cbd);
573 cbd->owned = FALSE;
574 if (visual_selection)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200575 clip_clear_selection(cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 clip_gen_lose_selection(cbd);
577#ifdef FEAT_X11
578 if (visual_selection)
579 {
580 /* May have to show a different kind of highlighting for the selected
581 * area. There is no specific redraw command for this, just redraw all
582 * windows on the current buffer. */
583 if (was_owned
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000584 && (get_real_state() == VISUAL
585 || get_real_state() == SELECTMODE)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200586 && (cbd == &clip_star ?
587 clip_isautosel_star() : clip_isautosel_plus())
Bram Moolenaar8820b482017-03-16 17:23:31 +0100588 && HL_ATTR(HLF_V) != HL_ATTR(HLF_VNC))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 {
590 update_curbuf(INVERTED_ALL);
591 setcursor();
592 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +0100593 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 }
595 }
596#endif
597}
598
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200599 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100600clip_copy_selection(VimClipboard *clip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601{
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200602 if (VIsual_active && (State & NORMAL) && clip->available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200604 clip_update_selection(clip);
605 clip_free_selection(clip);
606 clip_own_selection(clip);
607 if (clip->owned)
608 clip_get_selection(clip);
609 clip_gen_set_selection(clip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 }
611}
612
613/*
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200614 * Save and restore clip_unnamed before doing possibly many changes. This
615 * prevents accessing the clipboard very often which might slow down Vim
616 * considerably.
617 */
Bram Moolenaar73a156b2015-01-27 21:39:05 +0100618static int global_change_count = 0; /* if set, inside a start_global_changes */
Bram Moolenaar3fcfa352017-03-29 19:20:41 +0200619static int clipboard_needs_update = FALSE; /* clipboard needs to be updated */
620static int clip_did_set_selection = TRUE;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200621
622/*
623 * Save clip_unnamed and reset it.
624 */
625 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100626start_global_changes(void)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200627{
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100628 if (++global_change_count > 1)
629 return;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200630 clip_unnamed_saved = clip_unnamed;
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100631 clipboard_needs_update = FALSE;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200632
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100633 if (clip_did_set_selection)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200634 {
635 clip_unnamed = FALSE;
636 clip_did_set_selection = FALSE;
637 }
638}
639
640/*
Bram Moolenaar3fcfa352017-03-29 19:20:41 +0200641 * Return TRUE if setting the clipboard was postponed, it already contains the
642 * right text.
643 */
644 int
645is_clipboard_needs_update()
646{
647 return clipboard_needs_update;
648}
649
650/*
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200651 * Restore clip_unnamed and set the selection when needed.
652 */
653 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100654end_global_changes(void)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200655{
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100656 if (--global_change_count > 0)
657 /* recursive */
658 return;
659 if (!clip_did_set_selection)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200660 {
661 clip_did_set_selection = TRUE;
662 clip_unnamed = clip_unnamed_saved;
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100663 clip_unnamed_saved = FALSE;
664 if (clipboard_needs_update)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200665 {
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100666 /* only store something in the clipboard,
667 * if we have yanked anything to it */
668 if (clip_unnamed & CLIP_UNNAMED)
669 {
670 clip_own_selection(&clip_star);
671 clip_gen_set_selection(&clip_star);
672 }
673 if (clip_unnamed & CLIP_UNNAMED_PLUS)
674 {
675 clip_own_selection(&clip_plus);
676 clip_gen_set_selection(&clip_plus);
677 }
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200678 }
679 }
Bram Moolenaar3fcfa352017-03-29 19:20:41 +0200680 clipboard_needs_update = FALSE;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200681}
682
683/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 * Called when Visual mode is ended: update the selection.
685 */
686 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100687clip_auto_select(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688{
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200689 if (clip_isautosel_star())
690 clip_copy_selection(&clip_star);
691 if (clip_isautosel_plus())
692 clip_copy_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693}
694
695/*
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200696 * Return TRUE if automatic selection of Visual area is desired for the *
697 * register.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 */
699 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100700clip_isautosel_star(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701{
702 return (
703#ifdef FEAT_GUI
704 gui.in_use ? (vim_strchr(p_go, GO_ASEL) != NULL) :
705#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200706 clip_autoselect_star);
707}
708
709/*
710 * Return TRUE if automatic selection of Visual area is desired for the +
711 * register.
712 */
713 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100714clip_isautosel_plus(void)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200715{
716 return (
717#ifdef FEAT_GUI
718 gui.in_use ? (vim_strchr(p_go, GO_ASELPLUS) != NULL) :
719#endif
720 clip_autoselect_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721}
722
723
724/*
725 * Stuff for general mouse selection, without using Visual mode.
726 */
727
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100728static void clip_invert_area(int, int, int, int, int how);
729static void clip_invert_rectangle(int row, int col, int height, int width, int invert);
730static void clip_get_word_boundaries(VimClipboard *, int, int);
731static int clip_get_line_end(int);
732static void clip_update_modeless_selection(VimClipboard *, int, int,
733 int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734
735/* flags for clip_invert_area() */
736#define CLIP_CLEAR 1
737#define CLIP_SET 2
738#define CLIP_TOGGLE 3
739
740/*
741 * Start, continue or end a modeless selection. Used when editing the
742 * command-line and in the cmdline window.
743 */
744 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100745clip_modeless(int button, int is_click, int is_drag)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746{
747 int repeat;
748
749 repeat = ((clip_star.mode == SELECT_MODE_CHAR
750 || clip_star.mode == SELECT_MODE_LINE)
751 && (mod_mask & MOD_MASK_2CLICK))
752 || (clip_star.mode == SELECT_MODE_WORD
753 && (mod_mask & MOD_MASK_3CLICK));
754 if (is_click && button == MOUSE_RIGHT)
755 {
756 /* Right mouse button: If there was no selection, start one.
757 * Otherwise extend the existing selection. */
758 if (clip_star.state == SELECT_CLEARED)
759 clip_start_selection(mouse_col, mouse_row, FALSE);
760 clip_process_selection(button, mouse_col, mouse_row, repeat);
761 }
762 else if (is_click)
763 clip_start_selection(mouse_col, mouse_row, repeat);
764 else if (is_drag)
765 {
766 /* Don't try extending a selection if there isn't one. Happens when
767 * button-down is in the cmdline and them moving mouse upwards. */
768 if (clip_star.state != SELECT_CLEARED)
769 clip_process_selection(button, mouse_col, mouse_row, repeat);
770 }
771 else /* release */
772 clip_process_selection(MOUSE_RELEASE, mouse_col, mouse_row, FALSE);
773}
774
775/*
776 * Compare two screen positions ala strcmp()
777 */
778 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100779clip_compare_pos(
780 int row1,
781 int col1,
782 int row2,
783 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784{
785 if (row1 > row2) return(1);
786 if (row1 < row2) return(-1);
787 if (col1 > col2) return(1);
788 if (col1 < col2) return(-1);
Bram Moolenaar9e341102016-02-23 23:04:36 +0100789 return(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790}
791
792/*
793 * Start the selection
794 */
795 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100796clip_start_selection(int col, int row, int repeated_click)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797{
798 VimClipboard *cb = &clip_star;
799
800 if (cb->state == SELECT_DONE)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200801 clip_clear_selection(cb);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
803 row = check_row(row);
804 col = check_col(col);
805#ifdef FEAT_MBYTE
806 col = mb_fix_col(col, row);
807#endif
808
809 cb->start.lnum = row;
810 cb->start.col = col;
811 cb->end = cb->start;
812 cb->origin_row = (short_u)cb->start.lnum;
813 cb->state = SELECT_IN_PROGRESS;
814
815 if (repeated_click)
816 {
817 if (++cb->mode > SELECT_MODE_LINE)
818 cb->mode = SELECT_MODE_CHAR;
819 }
820 else
821 cb->mode = SELECT_MODE_CHAR;
822
823#ifdef FEAT_GUI
824 /* clear the cursor until the selection is made */
825 if (gui.in_use)
826 gui_undraw_cursor();
827#endif
828
829 switch (cb->mode)
830 {
831 case SELECT_MODE_CHAR:
832 cb->origin_start_col = cb->start.col;
833 cb->word_end_col = clip_get_line_end((int)cb->start.lnum);
834 break;
835
836 case SELECT_MODE_WORD:
837 clip_get_word_boundaries(cb, (int)cb->start.lnum, cb->start.col);
838 cb->origin_start_col = cb->word_start_col;
839 cb->origin_end_col = cb->word_end_col;
840
841 clip_invert_area((int)cb->start.lnum, cb->word_start_col,
842 (int)cb->end.lnum, cb->word_end_col, CLIP_SET);
843 cb->start.col = cb->word_start_col;
844 cb->end.col = cb->word_end_col;
845 break;
846
847 case SELECT_MODE_LINE:
848 clip_invert_area((int)cb->start.lnum, 0, (int)cb->start.lnum,
849 (int)Columns, CLIP_SET);
850 cb->start.col = 0;
851 cb->end.col = Columns;
852 break;
853 }
854
855 cb->prev = cb->start;
856
857#ifdef DEBUG_SELECTION
858 printf("Selection started at (%u,%u)\n", cb->start.lnum, cb->start.col);
859#endif
860}
861
862/*
863 * Continue processing the selection
864 */
865 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100866clip_process_selection(
867 int button,
868 int col,
869 int row,
870 int_u repeated_click)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871{
872 VimClipboard *cb = &clip_star;
873 int diff;
874 int slen = 1; /* cursor shape width */
875
876 if (button == MOUSE_RELEASE)
877 {
878 /* Check to make sure we have something selected */
879 if (cb->start.lnum == cb->end.lnum && cb->start.col == cb->end.col)
880 {
881#ifdef FEAT_GUI
882 if (gui.in_use)
883 gui_update_cursor(FALSE, FALSE);
884#endif
885 cb->state = SELECT_CLEARED;
886 return;
887 }
888
889#ifdef DEBUG_SELECTION
890 printf("Selection ended: (%u,%u) to (%u,%u)\n", cb->start.lnum,
891 cb->start.col, cb->end.lnum, cb->end.col);
892#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200893 if (clip_isautosel_star()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 || (
895#ifdef FEAT_GUI
896 gui.in_use ? (vim_strchr(p_go, GO_ASELML) != NULL) :
897#endif
898 clip_autoselectml))
899 clip_copy_modeless_selection(FALSE);
900#ifdef FEAT_GUI
901 if (gui.in_use)
902 gui_update_cursor(FALSE, FALSE);
903#endif
904
905 cb->state = SELECT_DONE;
906 return;
907 }
908
909 row = check_row(row);
910 col = check_col(col);
911#ifdef FEAT_MBYTE
912 col = mb_fix_col(col, row);
913#endif
914
915 if (col == (int)cb->prev.col && row == cb->prev.lnum && !repeated_click)
916 return;
917
918 /*
919 * When extending the selection with the right mouse button, swap the
920 * start and end if the position is before half the selection
921 */
922 if (cb->state == SELECT_DONE && button == MOUSE_RIGHT)
923 {
924 /*
925 * If the click is before the start, or the click is inside the
926 * selection and the start is the closest side, set the origin to the
927 * end of the selection.
928 */
929 if (clip_compare_pos(row, col, (int)cb->start.lnum, cb->start.col) < 0
930 || (clip_compare_pos(row, col,
931 (int)cb->end.lnum, cb->end.col) < 0
932 && (((cb->start.lnum == cb->end.lnum
933 && cb->end.col - col > col - cb->start.col))
934 || ((diff = (cb->end.lnum - row) -
935 (row - cb->start.lnum)) > 0
936 || (diff == 0 && col < (int)(cb->start.col +
937 cb->end.col) / 2)))))
938 {
939 cb->origin_row = (short_u)cb->end.lnum;
940 cb->origin_start_col = cb->end.col - 1;
941 cb->origin_end_col = cb->end.col;
942 }
943 else
944 {
945 cb->origin_row = (short_u)cb->start.lnum;
946 cb->origin_start_col = cb->start.col;
947 cb->origin_end_col = cb->start.col;
948 }
949 if (cb->mode == SELECT_MODE_WORD && !repeated_click)
950 cb->mode = SELECT_MODE_CHAR;
951 }
952
953 /* set state, for when using the right mouse button */
954 cb->state = SELECT_IN_PROGRESS;
955
956#ifdef DEBUG_SELECTION
957 printf("Selection extending to (%d,%d)\n", row, col);
958#endif
959
960 if (repeated_click && ++cb->mode > SELECT_MODE_LINE)
961 cb->mode = SELECT_MODE_CHAR;
962
963 switch (cb->mode)
964 {
965 case SELECT_MODE_CHAR:
966 /* If we're on a different line, find where the line ends */
967 if (row != cb->prev.lnum)
968 cb->word_end_col = clip_get_line_end(row);
969
970 /* See if we are before or after the origin of the selection */
971 if (clip_compare_pos(row, col, cb->origin_row,
972 cb->origin_start_col) >= 0)
973 {
974 if (col >= (int)cb->word_end_col)
975 clip_update_modeless_selection(cb, cb->origin_row,
976 cb->origin_start_col, row, (int)Columns);
977 else
978 {
979#ifdef FEAT_MBYTE
980 if (has_mbyte && mb_lefthalve(row, col))
981 slen = 2;
982#endif
983 clip_update_modeless_selection(cb, cb->origin_row,
984 cb->origin_start_col, row, col + slen);
985 }
986 }
987 else
988 {
989#ifdef FEAT_MBYTE
990 if (has_mbyte
991 && mb_lefthalve(cb->origin_row, cb->origin_start_col))
992 slen = 2;
993#endif
994 if (col >= (int)cb->word_end_col)
995 clip_update_modeless_selection(cb, row, cb->word_end_col,
996 cb->origin_row, cb->origin_start_col + slen);
997 else
998 clip_update_modeless_selection(cb, row, col,
999 cb->origin_row, cb->origin_start_col + slen);
1000 }
1001 break;
1002
1003 case SELECT_MODE_WORD:
1004 /* If we are still within the same word, do nothing */
1005 if (row == cb->prev.lnum && col >= (int)cb->word_start_col
1006 && col < (int)cb->word_end_col && !repeated_click)
1007 return;
1008
1009 /* Get new word boundaries */
1010 clip_get_word_boundaries(cb, row, col);
1011
1012 /* Handle being after the origin point of selection */
1013 if (clip_compare_pos(row, col, cb->origin_row,
1014 cb->origin_start_col) >= 0)
1015 clip_update_modeless_selection(cb, cb->origin_row,
1016 cb->origin_start_col, row, cb->word_end_col);
1017 else
1018 clip_update_modeless_selection(cb, row, cb->word_start_col,
1019 cb->origin_row, cb->origin_end_col);
1020 break;
1021
1022 case SELECT_MODE_LINE:
1023 if (row == cb->prev.lnum && !repeated_click)
1024 return;
1025
1026 if (clip_compare_pos(row, col, cb->origin_row,
1027 cb->origin_start_col) >= 0)
1028 clip_update_modeless_selection(cb, cb->origin_row, 0, row,
1029 (int)Columns);
1030 else
1031 clip_update_modeless_selection(cb, row, 0, cb->origin_row,
1032 (int)Columns);
1033 break;
1034 }
1035
1036 cb->prev.lnum = row;
1037 cb->prev.col = col;
1038
1039#ifdef DEBUG_SELECTION
1040 printf("Selection is: (%u,%u) to (%u,%u)\n", cb->start.lnum,
1041 cb->start.col, cb->end.lnum, cb->end.col);
1042#endif
1043}
1044
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045# if defined(FEAT_GUI) || defined(PROTO)
1046/*
1047 * Redraw part of the selection if character at "row,col" is inside of it.
1048 * Only used for the GUI.
1049 */
1050 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001051clip_may_redraw_selection(int row, int col, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052{
1053 int start = col;
1054 int end = col + len;
1055
1056 if (clip_star.state != SELECT_CLEARED
1057 && row >= clip_star.start.lnum
1058 && row <= clip_star.end.lnum)
1059 {
1060 if (row == clip_star.start.lnum && start < (int)clip_star.start.col)
1061 start = clip_star.start.col;
1062 if (row == clip_star.end.lnum && end > (int)clip_star.end.col)
1063 end = clip_star.end.col;
1064 if (end > start)
1065 clip_invert_area(row, start, row, end, 0);
1066 }
1067}
1068# endif
1069
1070/*
1071 * Called from outside to clear selected region from the display
1072 */
1073 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001074clip_clear_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001077 if (cbd->state == SELECT_CLEARED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 return;
1079
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001080 clip_invert_area((int)cbd->start.lnum, cbd->start.col, (int)cbd->end.lnum,
1081 cbd->end.col, CLIP_CLEAR);
1082 cbd->state = SELECT_CLEARED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083}
1084
1085/*
1086 * Clear the selection if any lines from "row1" to "row2" are inside of it.
1087 */
1088 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001089clip_may_clear_selection(int row1, int row2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090{
1091 if (clip_star.state == SELECT_DONE
1092 && row2 >= clip_star.start.lnum
1093 && row1 <= clip_star.end.lnum)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001094 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095}
1096
1097/*
1098 * Called before the screen is scrolled up or down. Adjusts the line numbers
1099 * of the selection. Call with big number when clearing the screen.
1100 */
1101 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001102clip_scroll_selection(
1103 int rows) /* negative for scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104{
1105 int lnum;
1106
1107 if (clip_star.state == SELECT_CLEARED)
1108 return;
1109
1110 lnum = clip_star.start.lnum - rows;
1111 if (lnum <= 0)
1112 clip_star.start.lnum = 0;
1113 else if (lnum >= screen_Rows) /* scrolled off of the screen */
1114 clip_star.state = SELECT_CLEARED;
1115 else
1116 clip_star.start.lnum = lnum;
1117
1118 lnum = clip_star.end.lnum - rows;
1119 if (lnum < 0) /* scrolled off of the screen */
1120 clip_star.state = SELECT_CLEARED;
1121 else if (lnum >= screen_Rows)
1122 clip_star.end.lnum = screen_Rows - 1;
1123 else
1124 clip_star.end.lnum = lnum;
1125}
1126
1127/*
1128 * Invert a region of the display between a starting and ending row and column
1129 * Values for "how":
1130 * CLIP_CLEAR: undo inversion
1131 * CLIP_SET: set inversion
1132 * CLIP_TOGGLE: set inversion if pos1 < pos2, undo inversion otherwise.
1133 * 0: invert (GUI only).
1134 */
1135 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001136clip_invert_area(
1137 int row1,
1138 int col1,
1139 int row2,
1140 int col2,
1141 int how)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142{
1143 int invert = FALSE;
1144
1145 if (how == CLIP_SET)
1146 invert = TRUE;
1147
1148 /* Swap the from and to positions so the from is always before */
1149 if (clip_compare_pos(row1, col1, row2, col2) > 0)
1150 {
1151 int tmp_row, tmp_col;
1152
1153 tmp_row = row1;
1154 tmp_col = col1;
1155 row1 = row2;
1156 col1 = col2;
1157 row2 = tmp_row;
1158 col2 = tmp_col;
1159 }
1160 else if (how == CLIP_TOGGLE)
1161 invert = TRUE;
1162
1163 /* If all on the same line, do it the easy way */
1164 if (row1 == row2)
1165 {
1166 clip_invert_rectangle(row1, col1, 1, col2 - col1, invert);
1167 }
1168 else
1169 {
1170 /* Handle a piece of the first line */
1171 if (col1 > 0)
1172 {
1173 clip_invert_rectangle(row1, col1, 1, (int)Columns - col1, invert);
1174 row1++;
1175 }
1176
1177 /* Handle a piece of the last line */
1178 if (col2 < Columns - 1)
1179 {
1180 clip_invert_rectangle(row2, 0, 1, col2, invert);
1181 row2--;
1182 }
1183
1184 /* Handle the rectangle thats left */
1185 if (row2 >= row1)
1186 clip_invert_rectangle(row1, 0, row2 - row1 + 1, (int)Columns,
1187 invert);
1188 }
1189}
1190
1191/*
1192 * Invert or un-invert a rectangle of the screen.
1193 * "invert" is true if the result is inverted.
1194 */
1195 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001196clip_invert_rectangle(
1197 int row,
1198 int col,
1199 int height,
1200 int width,
1201 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202{
1203#ifdef FEAT_GUI
1204 if (gui.in_use)
1205 gui_mch_invert_rectangle(row, col, height, width);
1206 else
1207#endif
1208 screen_draw_rectangle(row, col, height, width, invert);
1209}
1210
1211/*
1212 * Copy the currently selected area into the '*' register so it will be
1213 * available for pasting.
1214 * When "both" is TRUE also copy to the '+' register.
1215 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001217clip_copy_modeless_selection(int both UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218{
1219 char_u *buffer;
1220 char_u *bufp;
1221 int row;
1222 int start_col;
1223 int end_col;
1224 int line_end_col;
1225 int add_newline_flag = FALSE;
1226 int len;
1227#ifdef FEAT_MBYTE
1228 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229#endif
1230 int row1 = clip_star.start.lnum;
1231 int col1 = clip_star.start.col;
1232 int row2 = clip_star.end.lnum;
1233 int col2 = clip_star.end.col;
1234
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001235 /* Can't use ScreenLines unless initialized */
1236 if (ScreenLines == NULL)
1237 return;
1238
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 /*
1240 * Make sure row1 <= row2, and if row1 == row2 that col1 <= col2.
1241 */
1242 if (row1 > row2)
1243 {
1244 row = row1; row1 = row2; row2 = row;
1245 row = col1; col1 = col2; col2 = row;
1246 }
1247 else if (row1 == row2 && col1 > col2)
1248 {
1249 row = col1; col1 = col2; col2 = row;
1250 }
1251#ifdef FEAT_MBYTE
1252 /* correct starting point for being on right halve of double-wide char */
1253 p = ScreenLines + LineOffset[row1];
1254 if (enc_dbcs != 0)
1255 col1 -= (*mb_head_off)(p, p + col1);
1256 else if (enc_utf8 && p[col1] == 0)
1257 --col1;
1258#endif
1259
1260 /* Create a temporary buffer for storing the text */
1261 len = (row2 - row1 + 1) * Columns + 1;
1262#ifdef FEAT_MBYTE
1263 if (enc_dbcs != 0)
1264 len *= 2; /* max. 2 bytes per display cell */
1265 else if (enc_utf8)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001266 len *= MB_MAXBYTES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267#endif
1268 buffer = lalloc((long_u)len, TRUE);
1269 if (buffer == NULL) /* out of memory */
1270 return;
1271
1272 /* Process each row in the selection */
1273 for (bufp = buffer, row = row1; row <= row2; row++)
1274 {
1275 if (row == row1)
1276 start_col = col1;
1277 else
1278 start_col = 0;
1279
1280 if (row == row2)
1281 end_col = col2;
1282 else
1283 end_col = Columns;
1284
1285 line_end_col = clip_get_line_end(row);
1286
1287 /* See if we need to nuke some trailing whitespace */
1288 if (end_col >= Columns && (row < row2 || end_col > line_end_col))
1289 {
1290 /* Get rid of trailing whitespace */
1291 end_col = line_end_col;
1292 if (end_col < start_col)
1293 end_col = start_col;
1294
1295 /* If the last line extended to the end, add an extra newline */
1296 if (row == row2)
1297 add_newline_flag = TRUE;
1298 }
1299
1300 /* If after the first row, we need to always add a newline */
1301 if (row > row1 && !LineWraps[row - 1])
1302 *bufp++ = NL;
1303
1304 if (row < screen_Rows && end_col <= screen_Columns)
1305 {
1306#ifdef FEAT_MBYTE
1307 if (enc_dbcs != 0)
1308 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001309 int i;
1310
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 p = ScreenLines + LineOffset[row];
1312 for (i = start_col; i < end_col; ++i)
1313 if (enc_dbcs == DBCS_JPNU && p[i] == 0x8e)
1314 {
1315 /* single-width double-byte char */
1316 *bufp++ = 0x8e;
1317 *bufp++ = ScreenLines2[LineOffset[row] + i];
1318 }
1319 else
1320 {
1321 *bufp++ = p[i];
1322 if (MB_BYTE2LEN(p[i]) == 2)
1323 *bufp++ = p[++i];
1324 }
1325 }
1326 else if (enc_utf8)
1327 {
1328 int off;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001329 int i;
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001330 int ci;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331
1332 off = LineOffset[row];
1333 for (i = start_col; i < end_col; ++i)
1334 {
1335 /* The base character is either in ScreenLinesUC[] or
1336 * ScreenLines[]. */
1337 if (ScreenLinesUC[off + i] == 0)
1338 *bufp++ = ScreenLines[off + i];
1339 else
1340 {
1341 bufp += utf_char2bytes(ScreenLinesUC[off + i], bufp);
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001342 for (ci = 0; ci < Screen_mco; ++ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001344 /* Add a composing character. */
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001345 if (ScreenLinesC[ci][off + i] == 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001346 break;
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001347 bufp += utf_char2bytes(ScreenLinesC[ci][off + i],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 }
1350 }
1351 /* Skip right halve of double-wide character. */
1352 if (ScreenLines[off + i + 1] == 0)
1353 ++i;
1354 }
1355 }
1356 else
1357#endif
1358 {
1359 STRNCPY(bufp, ScreenLines + LineOffset[row] + start_col,
1360 end_col - start_col);
1361 bufp += end_col - start_col;
1362 }
1363 }
1364 }
1365
1366 /* Add a newline at the end if the selection ended there */
1367 if (add_newline_flag)
1368 *bufp++ = NL;
1369
1370 /* First cleanup any old selection and become the owner. */
1371 clip_free_selection(&clip_star);
1372 clip_own_selection(&clip_star);
1373
1374 /* Yank the text into the '*' register. */
1375 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_star);
1376
1377 /* Make the register contents available to the outside world. */
1378 clip_gen_set_selection(&clip_star);
1379
1380#ifdef FEAT_X11
1381 if (both)
1382 {
1383 /* Do the same for the '+' register. */
1384 clip_free_selection(&clip_plus);
1385 clip_own_selection(&clip_plus);
1386 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_plus);
1387 clip_gen_set_selection(&clip_plus);
1388 }
1389#endif
1390 vim_free(buffer);
1391}
1392
1393/*
1394 * Find the starting and ending positions of the word at the given row and
1395 * column. Only white-separated words are recognized here.
1396 */
1397#define CHAR_CLASS(c) (c <= ' ' ? ' ' : vim_iswordc(c))
1398
1399 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001400clip_get_word_boundaries(VimClipboard *cb, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401{
1402 int start_class;
1403 int temp_col;
1404 char_u *p;
1405#ifdef FEAT_MBYTE
1406 int mboff;
1407#endif
1408
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001409 if (row >= screen_Rows || col >= screen_Columns || ScreenLines == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 return;
1411
1412 p = ScreenLines + LineOffset[row];
1413#ifdef FEAT_MBYTE
1414 /* Correct for starting in the right halve of a double-wide char */
1415 if (enc_dbcs != 0)
1416 col -= dbcs_screen_head_off(p, p + col);
1417 else if (enc_utf8 && p[col] == 0)
1418 --col;
1419#endif
1420 start_class = CHAR_CLASS(p[col]);
1421
1422 temp_col = col;
1423 for ( ; temp_col > 0; temp_col--)
1424#ifdef FEAT_MBYTE
1425 if (enc_dbcs != 0
1426 && (mboff = dbcs_screen_head_off(p, p + temp_col - 1)) > 0)
1427 temp_col -= mboff;
1428 else
1429#endif
1430 if (CHAR_CLASS(p[temp_col - 1]) != start_class
1431#ifdef FEAT_MBYTE
1432 && !(enc_utf8 && p[temp_col - 1] == 0)
1433#endif
1434 )
1435 break;
1436 cb->word_start_col = temp_col;
1437
1438 temp_col = col;
1439 for ( ; temp_col < screen_Columns; temp_col++)
1440#ifdef FEAT_MBYTE
1441 if (enc_dbcs != 0 && dbcs_ptr2cells(p + temp_col) == 2)
1442 ++temp_col;
1443 else
1444#endif
1445 if (CHAR_CLASS(p[temp_col]) != start_class
1446#ifdef FEAT_MBYTE
1447 && !(enc_utf8 && p[temp_col] == 0)
1448#endif
1449 )
1450 break;
1451 cb->word_end_col = temp_col;
1452}
1453
1454/*
1455 * Find the column position for the last non-whitespace character on the given
1456 * line.
1457 */
1458 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001459clip_get_line_end(int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460{
1461 int i;
1462
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001463 if (row >= screen_Rows || ScreenLines == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 return 0;
1465 for (i = screen_Columns; i > 0; i--)
1466 if (ScreenLines[LineOffset[row] + i - 1] != ' ')
1467 break;
1468 return i;
1469}
1470
1471/*
1472 * Update the currently selected region by adding and/or subtracting from the
1473 * beginning or end and inverting the changed area(s).
1474 */
1475 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001476clip_update_modeless_selection(
1477 VimClipboard *cb,
1478 int row1,
1479 int col1,
1480 int row2,
1481 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482{
1483 /* See if we changed at the beginning of the selection */
1484 if (row1 != cb->start.lnum || col1 != (int)cb->start.col)
1485 {
1486 clip_invert_area(row1, col1, (int)cb->start.lnum, cb->start.col,
1487 CLIP_TOGGLE);
1488 cb->start.lnum = row1;
1489 cb->start.col = col1;
1490 }
1491
1492 /* See if we changed at the end of the selection */
1493 if (row2 != cb->end.lnum || col2 != (int)cb->end.col)
1494 {
1495 clip_invert_area((int)cb->end.lnum, cb->end.col, row2, col2,
1496 CLIP_TOGGLE);
1497 cb->end.lnum = row2;
1498 cb->end.col = col2;
1499 }
1500}
1501
1502 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001503clip_gen_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
1505#ifdef FEAT_XCLIPBOARD
1506# ifdef FEAT_GUI
1507 if (gui.in_use)
1508 return clip_mch_own_selection(cbd);
1509 else
1510# endif
1511 return clip_xterm_own_selection(cbd);
1512#else
1513 return clip_mch_own_selection(cbd);
1514#endif
1515}
1516
1517 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001518clip_gen_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519{
1520#ifdef FEAT_XCLIPBOARD
1521# ifdef FEAT_GUI
1522 if (gui.in_use)
1523 clip_mch_lose_selection(cbd);
1524 else
1525# endif
1526 clip_xterm_lose_selection(cbd);
1527#else
1528 clip_mch_lose_selection(cbd);
1529#endif
1530}
1531
1532 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001533clip_gen_set_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534{
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001535 if (!clip_did_set_selection)
1536 {
1537 /* Updating postponed, so that accessing the system clipboard won't
1538 * hang Vim when accessing it many times (e.g. on a :g comand). */
Bram Moolenaar5c27fd12015-01-27 14:09:37 +01001539 if ((cbd == &clip_plus && (clip_unnamed_saved & CLIP_UNNAMED_PLUS))
1540 || (cbd == &clip_star && (clip_unnamed_saved & CLIP_UNNAMED)))
1541 {
1542 clipboard_needs_update = TRUE;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001543 return;
Bram Moolenaar5c27fd12015-01-27 14:09:37 +01001544 }
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546#ifdef FEAT_XCLIPBOARD
1547# ifdef FEAT_GUI
1548 if (gui.in_use)
1549 clip_mch_set_selection(cbd);
1550 else
1551# endif
1552 clip_xterm_set_selection(cbd);
1553#else
1554 clip_mch_set_selection(cbd);
1555#endif
1556}
1557
1558 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001559clip_gen_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
1561#ifdef FEAT_XCLIPBOARD
1562# ifdef FEAT_GUI
1563 if (gui.in_use)
1564 clip_mch_request_selection(cbd);
1565 else
1566# endif
1567 clip_xterm_request_selection(cbd);
1568#else
1569 clip_mch_request_selection(cbd);
1570#endif
1571}
1572
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001573 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001574clip_gen_owner_exists(VimClipboard *cbd UNUSED)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001575{
1576#ifdef FEAT_XCLIPBOARD
1577# ifdef FEAT_GUI_GTK
1578 if (gui.in_use)
1579 return clip_gtk_owner_exists(cbd);
1580 else
1581# endif
1582 return clip_x11_owner_exists(cbd);
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02001583#else
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001584 return TRUE;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02001585#endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001586}
1587
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588#endif /* FEAT_CLIPBOARD */
1589
1590/*****************************************************************************
1591 * Functions that handle the input buffer.
1592 * This is used for any GUI version, and the unix terminal version.
1593 *
1594 * For Unix, the input characters are buffered to be able to check for a
1595 * CTRL-C. This should be done with signals, but I don't know how to do that
1596 * in a portable way for a tty in RAW mode.
1597 *
1598 * For the client-server code in the console the received keys are put in the
1599 * input buffer.
1600 */
1601
1602#if defined(USE_INPUT_BUF) || defined(PROTO)
1603
1604/*
1605 * Internal typeahead buffer. Includes extra space for long key code
1606 * descriptions which would otherwise overflow. The buffer is considered full
1607 * when only this extra space (or part of it) remains.
1608 */
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001609#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_JOB_CHANNEL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 || defined(FEAT_CLIENTSERVER)
1611 /*
1612 * Sun WorkShop and NetBeans stuff debugger commands into the input buffer.
1613 * This requires a larger buffer...
1614 * (Madsen) Go with this for remote input as well ...
1615 */
1616# define INBUFLEN 4096
1617#else
1618# define INBUFLEN 250
1619#endif
1620
1621static char_u inbuf[INBUFLEN + MAX_KEY_CODE_LEN];
1622static int inbufcount = 0; /* number of chars in inbuf[] */
1623
1624/*
1625 * vim_is_input_buf_full(), vim_is_input_buf_empty(), add_to_input_buf(), and
1626 * trash_input_buf() are functions for manipulating the input buffer. These
1627 * are used by the gui_* calls when a GUI is used to handle keyboard input.
1628 */
1629
1630 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001631vim_is_input_buf_full(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632{
1633 return (inbufcount >= INBUFLEN);
1634}
1635
1636 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001637vim_is_input_buf_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638{
1639 return (inbufcount == 0);
1640}
1641
1642#if defined(FEAT_OLE) || defined(PROTO)
1643 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001644vim_free_in_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645{
1646 return (INBUFLEN - inbufcount);
1647}
1648#endif
1649
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001650#if defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001652vim_used_in_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653{
1654 return inbufcount;
1655}
1656#endif
1657
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658/*
1659 * Return the current contents of the input buffer and make it empty.
1660 * The returned pointer must be passed to set_input_buf() later.
1661 */
1662 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001663get_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664{
1665 garray_T *gap;
1666
1667 /* We use a growarray to store the data pointer and the length. */
1668 gap = (garray_T *)alloc((unsigned)sizeof(garray_T));
1669 if (gap != NULL)
1670 {
1671 /* Add one to avoid a zero size. */
1672 gap->ga_data = alloc((unsigned)inbufcount + 1);
1673 if (gap->ga_data != NULL)
1674 mch_memmove(gap->ga_data, inbuf, (size_t)inbufcount);
1675 gap->ga_len = inbufcount;
1676 }
1677 trash_input_buf();
1678 return (char_u *)gap;
1679}
1680
1681/*
1682 * Restore the input buffer with a pointer returned from get_input_buf().
1683 * The allocated memory is freed, this only works once!
1684 */
1685 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001686set_input_buf(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687{
1688 garray_T *gap = (garray_T *)p;
1689
1690 if (gap != NULL)
1691 {
1692 if (gap->ga_data != NULL)
1693 {
1694 mch_memmove(inbuf, gap->ga_data, gap->ga_len);
1695 inbufcount = gap->ga_len;
1696 vim_free(gap->ga_data);
1697 }
1698 vim_free(gap);
1699 }
1700}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702/*
1703 * Add the given bytes to the input buffer
1704 * Special keys start with CSI. A real CSI must have been translated to
1705 * CSI KS_EXTRA KE_CSI. K_SPECIAL doesn't require translation.
1706 */
1707 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001708add_to_input_buf(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709{
1710 if (inbufcount + len > INBUFLEN + MAX_KEY_CODE_LEN)
1711 return; /* Shouldn't ever happen! */
1712
1713#ifdef FEAT_HANGULIN
1714 if ((State & (INSERT|CMDLINE)) && hangul_input_state_get())
1715 if ((len = hangul_input_process(s, len)) == 0)
1716 return;
1717#endif
1718
1719 while (len--)
1720 inbuf[inbufcount++] = *s++;
1721}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723/*
1724 * Add "str[len]" to the input buffer while escaping CSI bytes.
1725 */
1726 void
1727add_to_input_buf_csi(char_u *str, int len)
1728{
1729 int i;
1730 char_u buf[2];
1731
1732 for (i = 0; i < len; ++i)
1733 {
1734 add_to_input_buf(str + i, 1);
1735 if (str[i] == CSI)
1736 {
1737 /* Turn CSI into K_CSI. */
1738 buf[0] = KS_EXTRA;
1739 buf[1] = (int)KE_CSI;
1740 add_to_input_buf(buf, 2);
1741 }
1742 }
1743}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744
1745#if defined(FEAT_HANGULIN) || defined(PROTO)
1746 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001747push_raw_key(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748{
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001749 char_u *tmpbuf;
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001750 char_u *inp = s;
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001751
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001752 /* use the conversion result if possible */
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001753 tmpbuf = hangul_string_convert(s, &len);
1754 if (tmpbuf != NULL)
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001755 inp = tmpbuf;
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001756
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001757 for (; len--; inp++)
1758 {
1759 inbuf[inbufcount++] = *inp;
1760 if (*inp == CSI)
Bram Moolenaar00ded432016-03-03 11:45:15 +01001761 {
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001762 /* Turn CSI into K_CSI. */
1763 inbuf[inbufcount++] = KS_EXTRA;
1764 inbuf[inbufcount++] = (int)KE_CSI;
Bram Moolenaar00ded432016-03-03 11:45:15 +01001765 }
Bram Moolenaar00ded432016-03-03 11:45:15 +01001766 }
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001767 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768}
1769#endif
1770
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771/* Remove everything from the input buffer. Called when ^C is found */
1772 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001773trash_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774{
1775 inbufcount = 0;
1776}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
1778/*
1779 * Read as much data from the input buffer as possible up to maxlen, and store
1780 * it in buf.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 */
1782 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001783read_from_input_buf(char_u *buf, long maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784{
1785 if (inbufcount == 0) /* if the buffer is empty, fill it */
1786 fill_input_buf(TRUE);
1787 if (maxlen > inbufcount)
1788 maxlen = inbufcount;
1789 mch_memmove(buf, inbuf, (size_t)maxlen);
1790 inbufcount -= maxlen;
1791 if (inbufcount)
1792 mch_memmove(inbuf, inbuf + maxlen, (size_t)inbufcount);
1793 return (int)maxlen;
1794}
1795
1796 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001797fill_input_buf(int exit_on_error UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798{
Bram Moolenaard0573012017-10-28 21:11:06 +02001799#if defined(UNIX) || defined(VMS) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 int len;
1801 int try;
1802 static int did_read_something = FALSE;
1803# ifdef FEAT_MBYTE
1804 static char_u *rest = NULL; /* unconverted rest of previous read */
1805 static int restlen = 0;
1806 int unconverted;
1807# endif
1808#endif
1809
1810#ifdef FEAT_GUI
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001811 if (gui.in_use
1812# ifdef NO_CONSOLE_INPUT
1813 /* Don't use the GUI input when the window hasn't been opened yet.
1814 * We get here from ui_inchar() when we should try reading from stdin. */
1815 && !no_console_input()
1816# endif
1817 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 {
1819 gui_mch_update();
1820 return;
1821 }
1822#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02001823#if defined(UNIX) || defined(VMS) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 if (vim_is_input_buf_full())
1825 return;
1826 /*
1827 * Fill_input_buf() is only called when we really need a character.
1828 * If we can't get any, but there is some in the buffer, just return.
1829 * If we can't get any, and there isn't any in the buffer, we give up and
1830 * exit Vim.
1831 */
1832# ifdef __BEOS__
1833 /*
1834 * On the BeBox version (for now), all input is secretly performed within
1835 * beos_select() which is called from RealWaitForChar().
1836 */
1837 while (!vim_is_input_buf_full() && RealWaitForChar(read_cmd_fd, 0, NULL))
1838 ;
1839 len = inbufcount;
1840 inbufcount = 0;
1841# else
1842
Bram Moolenaar85b11762016-02-27 18:13:23 +01001843# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 if (rest != NULL)
1845 {
1846 /* Use remainder of previous call, starts with an invalid character
1847 * that may become valid when reading more. */
1848 if (restlen > INBUFLEN - inbufcount)
1849 unconverted = INBUFLEN - inbufcount;
1850 else
1851 unconverted = restlen;
1852 mch_memmove(inbuf + inbufcount, rest, unconverted);
1853 if (unconverted == restlen)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001854 VIM_CLEAR(rest);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 else
1856 {
1857 restlen -= unconverted;
1858 mch_memmove(rest, rest + unconverted, restlen);
1859 }
1860 inbufcount += unconverted;
1861 }
1862 else
1863 unconverted = 0;
Bram Moolenaar85b11762016-02-27 18:13:23 +01001864# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865
1866 len = 0; /* to avoid gcc warning */
1867 for (try = 0; try < 100; ++try)
1868 {
Bram Moolenaar4e601e32018-04-24 13:29:51 +02001869 size_t readlen = (size_t)((INBUFLEN - inbufcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870# ifdef FEAT_MBYTE
Bram Moolenaar4e601e32018-04-24 13:29:51 +02001871 / input_conv.vc_factor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872# endif
Bram Moolenaar4e601e32018-04-24 13:29:51 +02001873 );
1874# ifdef VMS
Bram Moolenaar49943732018-04-24 20:27:26 +02001875 len = vms_read((char *)inbuf + inbufcount, readlen);
Bram Moolenaar4e601e32018-04-24 13:29:51 +02001876# else
1877 len = read(read_cmd_fd, (char *)inbuf + inbufcount, readlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878# endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001879
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 if (len > 0 || got_int)
1881 break;
1882 /*
1883 * If reading stdin results in an error, continue reading stderr.
1884 * This helps when using "foo | xargs vim".
1885 */
1886 if (!did_read_something && !isatty(read_cmd_fd) && read_cmd_fd == 0)
1887 {
1888 int m = cur_tmode;
1889
1890 /* We probably set the wrong file descriptor to raw mode. Switch
1891 * back to cooked mode, use another descriptor and set the mode to
1892 * what it was. */
1893 settmode(TMODE_COOK);
1894#ifdef HAVE_DUP
1895 /* Use stderr for stdin, also works for shell commands. */
1896 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02001897 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898#else
1899 read_cmd_fd = 2; /* read from stderr instead of stdin */
1900#endif
1901 settmode(m);
1902 }
1903 if (!exit_on_error)
1904 return;
1905 }
1906# endif
1907 if (len <= 0 && !got_int)
1908 read_error_exit();
1909 if (len > 0)
1910 did_read_something = TRUE;
1911 if (got_int)
1912 {
1913 /* Interrupted, pretend a CTRL-C was typed. */
1914 inbuf[0] = 3;
1915 inbufcount = 1;
1916 }
1917 else
1918 {
1919# ifdef FEAT_MBYTE
1920 /*
1921 * May perform conversion on the input characters.
1922 * Include the unconverted rest of the previous call.
1923 * If there is an incomplete char at the end it is kept for the next
1924 * time, reading more bytes should make conversion possible.
1925 * Don't do this in the unlikely event that the input buffer is too
1926 * small ("rest" still contains more bytes).
1927 */
1928 if (input_conv.vc_type != CONV_NONE)
1929 {
1930 inbufcount -= unconverted;
1931 len = convert_input_safe(inbuf + inbufcount,
1932 len + unconverted, INBUFLEN - inbufcount,
1933 rest == NULL ? &rest : NULL, &restlen);
1934 }
1935# endif
1936 while (len-- > 0)
1937 {
1938 /*
1939 * if a CTRL-C was typed, remove it from the buffer and set got_int
1940 */
1941 if (inbuf[inbufcount] == 3 && ctrl_c_interrupts)
1942 {
1943 /* remove everything typed before the CTRL-C */
1944 mch_memmove(inbuf, inbuf + inbufcount, (size_t)(len + 1));
1945 inbufcount = 0;
1946 got_int = TRUE;
1947 }
1948 ++inbufcount;
1949 }
1950 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001951#endif /* UNIX or VMS*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952}
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001953#endif /* defined(UNIX) || defined(FEAT_GUI) || defined(VMS) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954
1955/*
1956 * Exit because of an input read error.
1957 */
1958 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001959read_error_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960{
1961 if (silent_mode) /* Normal way to exit for "ex -s" */
1962 getout(0);
1963 STRCPY(IObuff, _("Vim: Error reading input, exiting...\n"));
1964 preserve_exit();
1965}
1966
1967#if defined(CURSOR_SHAPE) || defined(PROTO)
1968/*
1969 * May update the shape of the cursor.
1970 */
1971 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001972ui_cursor_shape_forced(int forced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973{
1974# ifdef FEAT_GUI
1975 if (gui.in_use)
1976 gui_update_cursor_later();
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001977 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001979 term_cursor_mode(forced);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001980
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981# ifdef MCH_CURSOR_SHAPE
1982 mch_update_cursor();
1983# endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001984
1985# ifdef FEAT_CONCEAL
Bram Moolenaarb9464822018-05-10 15:09:49 +02001986 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001987# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001989
1990 void
1991ui_cursor_shape(void)
1992{
1993 ui_cursor_shape_forced(FALSE);
1994}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995#endif
1996
1997#if defined(FEAT_CLIPBOARD) || defined(FEAT_GUI) || defined(FEAT_RIGHTLEFT) \
Bram Moolenaaraf51e662008-07-14 19:48:05 +00001998 || defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999/*
2000 * Check bounds for column number
2001 */
2002 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002003check_col(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004{
2005 if (col < 0)
2006 return 0;
2007 if (col >= (int)screen_Columns)
2008 return (int)screen_Columns - 1;
2009 return col;
2010}
2011
2012/*
2013 * Check bounds for row number
2014 */
2015 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002016check_row(int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017{
2018 if (row < 0)
2019 return 0;
2020 if (row >= (int)screen_Rows)
2021 return (int)screen_Rows - 1;
2022 return row;
2023}
2024#endif
2025
2026/*
2027 * Stuff for the X clipboard. Shared between VMS and Unix.
2028 */
2029
2030#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) || defined(PROTO)
2031# include <X11/Xatom.h>
2032# include <X11/Intrinsic.h>
2033
2034/*
2035 * Open the application context (if it hasn't been opened yet).
2036 * Used for Motif and Athena GUI and the xterm clipboard.
2037 */
2038 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002039open_app_context(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040{
2041 if (app_context == NULL)
2042 {
2043 XtToolkitInitialize();
2044 app_context = XtCreateApplicationContext();
2045 }
2046}
2047
2048static Atom vim_atom; /* Vim's own special selection format */
2049#ifdef FEAT_MBYTE
2050static Atom vimenc_atom; /* Vim's extended selection format */
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002051static Atom utf8_atom;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052#endif
2053static Atom compound_text_atom;
2054static Atom text_atom;
2055static Atom targets_atom;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002056static Atom timestamp_atom; /* Used to get a timestamp */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057
2058 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002059x11_setup_atoms(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060{
2061 vim_atom = XInternAtom(dpy, VIM_ATOM_NAME, False);
2062#ifdef FEAT_MBYTE
2063 vimenc_atom = XInternAtom(dpy, VIMENC_ATOM_NAME,False);
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002064 utf8_atom = XInternAtom(dpy, "UTF8_STRING", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065#endif
2066 compound_text_atom = XInternAtom(dpy, "COMPOUND_TEXT", False);
2067 text_atom = XInternAtom(dpy, "TEXT", False);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002068 targets_atom = XInternAtom(dpy, "TARGETS", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 clip_star.sel_atom = XA_PRIMARY;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002070 clip_plus.sel_atom = XInternAtom(dpy, "CLIPBOARD", False);
2071 timestamp_atom = XInternAtom(dpy, "TIMESTAMP", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072}
2073
2074/*
2075 * X Selection stuff, for cutting and pasting text to other windows.
2076 */
2077
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002078static Boolean clip_x11_convert_selection_cb(Widget w, Atom *sel_atom, Atom *target, Atom *type, XtPointer *value, long_u *length, int *format);
2079static void clip_x11_lose_ownership_cb(Widget w, Atom *sel_atom);
2080static void clip_x11_notify_cb(Widget w, Atom *sel_atom, Atom *target);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002081
2082/*
2083 * Property callback to get a timestamp for XtOwnSelection.
2084 */
2085 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002086clip_x11_timestamp_cb(
2087 Widget w,
2088 XtPointer n UNUSED,
2089 XEvent *event,
2090 Boolean *cont UNUSED)
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002091{
2092 Atom actual_type;
2093 int format;
2094 unsigned long nitems, bytes_after;
2095 unsigned char *prop=NULL;
2096 XPropertyEvent *xproperty=&event->xproperty;
2097
2098 /* Must be a property notify, state can't be Delete (True), has to be
2099 * one of the supported selection types. */
2100 if (event->type != PropertyNotify || xproperty->state
2101 || (xproperty->atom != clip_star.sel_atom
2102 && xproperty->atom != clip_plus.sel_atom))
2103 return;
2104
2105 if (XGetWindowProperty(xproperty->display, xproperty->window,
2106 xproperty->atom, 0, 0, False, timestamp_atom, &actual_type, &format,
2107 &nitems, &bytes_after, &prop))
2108 return;
2109
2110 if (prop)
2111 XFree(prop);
2112
2113 /* Make sure the property type is "TIMESTAMP" and it's 32 bits. */
2114 if (actual_type != timestamp_atom || format != 32)
2115 return;
2116
2117 /* Get the selection, using the event timestamp. */
Bram Moolenaar62b42182010-09-21 22:09:37 +02002118 if (XtOwnSelection(w, xproperty->atom, xproperty->time,
2119 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002120 clip_x11_notify_cb) == OK)
Bram Moolenaar62b42182010-09-21 22:09:37 +02002121 {
2122 /* Set the "owned" flag now, there may have been a call to
2123 * lose_ownership_cb in between. */
2124 if (xproperty->atom == clip_plus.sel_atom)
2125 clip_plus.owned = TRUE;
2126 else
2127 clip_star.owned = TRUE;
2128 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002129}
2130
2131 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002132x11_setup_selection(Widget w)
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002133{
2134 XtAddEventHandler(w, PropertyChangeMask, False,
2135 /*(XtEventHandler)*/clip_x11_timestamp_cb, (XtPointer)NULL);
2136}
2137
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002139clip_x11_request_selection_cb(
2140 Widget w UNUSED,
2141 XtPointer success,
2142 Atom *sel_atom,
2143 Atom *type,
2144 XtPointer value,
2145 long_u *length,
2146 int *format)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147{
Bram Moolenaard44347f2011-06-19 01:14:29 +02002148 int motion_type = MAUTO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 long_u len;
2150 char_u *p;
2151 char **text_list = NULL;
2152 VimClipboard *cbd;
2153#ifdef FEAT_MBYTE
2154 char_u *tmpbuf = NULL;
2155#endif
2156
2157 if (*sel_atom == clip_plus.sel_atom)
2158 cbd = &clip_plus;
2159 else
2160 cbd = &clip_star;
2161
2162 if (value == NULL || *length == 0)
2163 {
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002164 clip_free_selection(cbd); /* nothing received, clear register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 *(int *)success = FALSE;
2166 return;
2167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 p = (char_u *)value;
2169 len = *length;
2170 if (*type == vim_atom)
2171 {
2172 motion_type = *p++;
2173 len--;
2174 }
2175
2176#ifdef FEAT_MBYTE
2177 else if (*type == vimenc_atom)
2178 {
2179 char_u *enc;
2180 vimconv_T conv;
2181 int convlen;
2182
2183 motion_type = *p++;
2184 --len;
2185
2186 enc = p;
2187 p += STRLEN(p) + 1;
2188 len -= p - enc;
2189
2190 /* If the encoding of the text is different from 'encoding', attempt
2191 * converting it. */
2192 conv.vc_type = CONV_NONE;
2193 convert_setup(&conv, enc, p_enc);
2194 if (conv.vc_type != CONV_NONE)
2195 {
2196 convlen = len; /* Need to use an int here. */
2197 tmpbuf = string_convert(&conv, p, &convlen);
2198 len = convlen;
2199 if (tmpbuf != NULL)
2200 p = tmpbuf;
2201 convert_setup(&conv, NULL, NULL);
2202 }
2203 }
2204#endif
2205
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002206 else if (*type == compound_text_atom
2207#ifdef FEAT_MBYTE
2208 || *type == utf8_atom
2209#endif
2210 || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211#ifdef FEAT_MBYTE
2212 enc_dbcs != 0 &&
2213#endif
2214 *type == text_atom))
2215 {
2216 XTextProperty text_prop;
2217 int n_text = 0;
2218 int status;
2219
2220 text_prop.value = (unsigned char *)value;
2221 text_prop.encoding = *type;
2222 text_prop.format = *format;
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002223 text_prop.nitems = len;
Bram Moolenaar81851112013-04-12 12:27:30 +02002224#if defined(FEAT_MBYTE) && defined(X_HAVE_UTF8_STRING)
Bram Moolenaare2e663f2013-03-07 18:02:30 +01002225 if (*type == utf8_atom)
2226 status = Xutf8TextPropertyToTextList(X_DISPLAY, &text_prop,
2227 &text_list, &n_text);
2228 else
2229#endif
2230 status = XmbTextPropertyToTextList(X_DISPLAY, &text_prop,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 &text_list, &n_text);
2232 if (status != Success || n_text < 1)
2233 {
2234 *(int *)success = FALSE;
2235 return;
2236 }
2237 p = (char_u *)text_list[0];
2238 len = STRLEN(p);
2239 }
2240 clip_yank_selection(motion_type, p, (long)len, cbd);
2241
2242 if (text_list != NULL)
2243 XFreeStringList(text_list);
2244#ifdef FEAT_MBYTE
2245 vim_free(tmpbuf);
2246#endif
2247 XtFree((char *)value);
2248 *(int *)success = TRUE;
2249}
2250
2251 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002252clip_x11_request_selection(
2253 Widget myShell,
2254 Display *dpy,
2255 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256{
2257 XEvent event;
2258 Atom type;
2259 static int success;
2260 int i;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002261 time_t start_time;
2262 int timed_out = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263
2264 for (i =
2265#ifdef FEAT_MBYTE
2266 0
2267#else
2268 1
2269#endif
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002270 ; i < 6; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
2272 switch (i)
2273 {
2274#ifdef FEAT_MBYTE
2275 case 0: type = vimenc_atom; break;
2276#endif
2277 case 1: type = vim_atom; break;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002278#ifdef FEAT_MBYTE
2279 case 2: type = utf8_atom; break;
2280#endif
2281 case 3: type = compound_text_atom; break;
2282 case 4: type = text_atom; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 default: type = XA_STRING;
2284 }
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002285#ifdef FEAT_MBYTE
Bram Moolenaar81851112013-04-12 12:27:30 +02002286 if (type == utf8_atom
2287# if defined(X_HAVE_UTF8_STRING)
2288 && !enc_utf8
2289# endif
2290 )
2291 /* Only request utf-8 when 'encoding' is utf8 and
2292 * Xutf8TextPropertyToTextList is available. */
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002293 continue;
2294#endif
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002295 success = MAYBE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 XtGetSelectionValue(myShell, cbd->sel_atom, type,
2297 clip_x11_request_selection_cb, (XtPointer)&success, CurrentTime);
2298
2299 /* Make sure the request for the selection goes out before waiting for
2300 * a response. */
2301 XFlush(dpy);
2302
2303 /*
2304 * Wait for result of selection request, otherwise if we type more
2305 * characters, then they will appear before the one that requested the
2306 * paste! Don't worry, we will catch up with any other events later.
2307 */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002308 start_time = time(NULL);
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002309 while (success == MAYBE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002311 if (XCheckTypedEvent(dpy, PropertyNotify, &event)
2312 || XCheckTypedEvent(dpy, SelectionNotify, &event)
2313 || XCheckTypedEvent(dpy, SelectionRequest, &event))
Bram Moolenaar89417b92008-09-07 19:48:53 +00002314 {
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002315 /* This is where clip_x11_request_selection_cb() should be
2316 * called. It may actually happen a bit later, so we loop
2317 * until "success" changes.
2318 * We may get a SelectionRequest here and if we don't handle
2319 * it we hang. KDE klipper does this, for example.
2320 * We need to handle a PropertyNotify for large selections. */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002321 XtDispatchEvent(&event);
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002322 continue;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324
Bram Moolenaar89417b92008-09-07 19:48:53 +00002325 /* Time out after 2 to 3 seconds to avoid that we hang when the
2326 * other process doesn't respond. Note that the SelectionNotify
2327 * event may still come later when the selection owner comes back
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002328 * to life and the text gets inserted unexpectedly. Don't know
2329 * why that happens or how to avoid that :-(. */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002330 if (time(NULL) > start_time + 2)
2331 {
2332 timed_out = TRUE;
2333 break;
2334 }
2335
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 /* Do we need this? Probably not. */
2337 XSync(dpy, False);
2338
Bram Moolenaar89417b92008-09-07 19:48:53 +00002339 /* Wait for 1 msec to avoid that we eat up all CPU time. */
2340 ui_delay(1L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 }
2342
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002343 if (success == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 return;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002345
2346 /* don't do a retry with another type after timing out, otherwise we
2347 * hang for 15 seconds. */
2348 if (timed_out)
2349 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 }
2351
2352 /* Final fallback position - use the X CUT_BUFFER0 store */
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002353 yank_cut_buffer0(dpy, cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354}
2355
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 static Boolean
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002357clip_x11_convert_selection_cb(
2358 Widget w UNUSED,
2359 Atom *sel_atom,
2360 Atom *target,
2361 Atom *type,
2362 XtPointer *value,
2363 long_u *length,
2364 int *format)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365{
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002366 static char_u *save_result = NULL;
2367 static long_u save_length = 0;
2368 char_u *string;
2369 int motion_type;
2370 VimClipboard *cbd;
2371 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372
2373 if (*sel_atom == clip_plus.sel_atom)
2374 cbd = &clip_plus;
2375 else
2376 cbd = &clip_star;
2377
2378 if (!cbd->owned)
2379 return False; /* Shouldn't ever happen */
2380
2381 /* requestor wants to know what target types we support */
2382 if (*target == targets_atom)
2383 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002384 static Atom array[7];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 *value = (XtPointer)array;
2387 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 array[i++] = targets_atom;
2389#ifdef FEAT_MBYTE
2390 array[i++] = vimenc_atom;
2391#endif
2392 array[i++] = vim_atom;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002393#ifdef FEAT_MBYTE
2394 if (enc_utf8)
2395 array[i++] = utf8_atom;
2396#endif
2397 array[i++] = XA_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 array[i++] = text_atom;
2399 array[i++] = compound_text_atom;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002400
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 *type = XA_ATOM;
2402 /* This used to be: *format = sizeof(Atom) * 8; but that caused
2403 * crashes on 64 bit machines. (Peter Derr) */
2404 *format = 32;
2405 *length = i;
2406 return True;
2407 }
2408
2409 if ( *target != XA_STRING
2410#ifdef FEAT_MBYTE
2411 && *target != vimenc_atom
Bram Moolenaar980e58f2014-06-12 13:28:30 +02002412 && (*target != utf8_atom || !enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413#endif
2414 && *target != vim_atom
2415 && *target != text_atom
2416 && *target != compound_text_atom)
2417 return False;
2418
2419 clip_get_selection(cbd);
2420 motion_type = clip_convert_selection(&string, length, cbd);
2421 if (motion_type < 0)
2422 return False;
2423
2424 /* For our own format, the first byte contains the motion type */
2425 if (*target == vim_atom)
2426 (*length)++;
2427
2428#ifdef FEAT_MBYTE
2429 /* Our own format with encoding: motion 'encoding' NUL text */
2430 if (*target == vimenc_atom)
2431 *length += STRLEN(p_enc) + 2;
2432#endif
2433
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002434 if (save_length < *length || save_length / 2 >= *length)
2435 *value = XtRealloc((char *)save_result, (Cardinal)*length + 1);
2436 else
2437 *value = save_result;
2438 if (*value == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 {
2440 vim_free(string);
2441 return False;
2442 }
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002443 save_result = (char_u *)*value;
2444 save_length = *length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002446 if (*target == XA_STRING
2447#ifdef FEAT_MBYTE
2448 || (*target == utf8_atom && enc_utf8)
2449#endif
2450 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002452 mch_memmove(save_result, string, (size_t)(*length));
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002453 *type = *target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 }
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002455 else if (*target == compound_text_atom || *target == text_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 {
2457 XTextProperty text_prop;
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002458 char *string_nt = (char *)save_result;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002459 int conv_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460
2461 /* create NUL terminated string which XmbTextListToTextProperty wants */
2462 mch_memmove(string_nt, string, (size_t)*length);
2463 string_nt[*length] = NUL;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002464 conv_result = XmbTextListToTextProperty(X_DISPLAY, (char **)&string_nt,
2465 1, XCompoundTextStyle, &text_prop);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002466 if (conv_result != Success)
2467 {
2468 vim_free(string);
2469 return False;
2470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 *value = (XtPointer)(text_prop.value); /* from plain text */
2472 *length = text_prop.nitems;
2473 *type = compound_text_atom;
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002474 XtFree((char *)save_result);
2475 save_result = (char_u *)*value;
2476 save_length = *length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478#ifdef FEAT_MBYTE
2479 else if (*target == vimenc_atom)
2480 {
2481 int l = STRLEN(p_enc);
2482
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002483 save_result[0] = motion_type;
2484 STRCPY(save_result + 1, p_enc);
2485 mch_memmove(save_result + l + 2, string, (size_t)(*length - l - 2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 *type = vimenc_atom;
2487 }
2488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 else
2490 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002491 save_result[0] = motion_type;
2492 mch_memmove(save_result + 1, string, (size_t)(*length - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 *type = vim_atom;
2494 }
2495 *format = 8; /* 8 bits per char */
2496 vim_free(string);
2497 return True;
2498}
2499
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002501clip_x11_lose_ownership_cb(Widget w UNUSED, Atom *sel_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502{
2503 if (*sel_atom == clip_plus.sel_atom)
2504 clip_lose_selection(&clip_plus);
2505 else
2506 clip_lose_selection(&clip_star);
2507}
2508
2509 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002510clip_x11_lose_selection(Widget myShell, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511{
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002512 XtDisownSelection(myShell, cbd->sel_atom,
2513 XtLastTimestampProcessed(XtDisplay(myShell)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514}
2515
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002516 static void
2517clip_x11_notify_cb(Widget w UNUSED, Atom *sel_atom UNUSED, Atom *target UNUSED)
2518{
2519 /* To prevent automatically freeing the selection value. */
2520}
2521
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002523clip_x11_own_selection(Widget myShell, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524{
Bram Moolenaar62b42182010-09-21 22:09:37 +02002525 /* When using the GUI we have proper timestamps, use the one of the last
2526 * event. When in the console we don't get events (the terminal gets
2527 * them), Get the time by a zero-length append, clip_x11_timestamp_cb will
2528 * be called with the current timestamp. */
2529#ifdef FEAT_GUI
2530 if (gui.in_use)
2531 {
2532 if (XtOwnSelection(myShell, cbd->sel_atom,
2533 XtLastTimestampProcessed(XtDisplay(myShell)),
2534 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002535 clip_x11_notify_cb) == False)
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +01002536 return FAIL;
Bram Moolenaar62b42182010-09-21 22:09:37 +02002537 }
2538 else
2539#endif
2540 {
2541 if (!XChangeProperty(XtDisplay(myShell), XtWindow(myShell),
2542 cbd->sel_atom, timestamp_atom, 32, PropModeAppend, NULL, 0))
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +01002543 return FAIL;
Bram Moolenaar62b42182010-09-21 22:09:37 +02002544 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002545 /* Flush is required in a terminal as nothing else is doing it. */
2546 XFlush(XtDisplay(myShell));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 return OK;
2548}
2549
2550/*
2551 * Send the current selection to the clipboard. Do nothing for X because we
2552 * will fill in the selection only when requested by another app.
2553 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002555clip_x11_set_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556{
2557}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002558
2559 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002560clip_x11_owner_exists(VimClipboard *cbd)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002561{
2562 return XGetSelectionOwner(X_DISPLAY, cbd->sel_atom) != None;
2563}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564#endif
2565
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002566#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) \
2567 || defined(FEAT_GUI_GTK) || defined(PROTO)
2568/*
2569 * Get the contents of the X CUT_BUFFER0 and put it in "cbd".
2570 */
2571 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002572yank_cut_buffer0(Display *dpy, VimClipboard *cbd)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002573{
2574 int nbytes = 0;
2575 char_u *buffer = (char_u *)XFetchBuffer(dpy, &nbytes, 0);
2576
2577 if (nbytes > 0)
2578 {
2579#ifdef FEAT_MBYTE
2580 int done = FALSE;
2581
2582 /* CUT_BUFFER0 is supposed to be always latin1. Convert to 'enc' when
2583 * using a multi-byte encoding. Conversion between two 8-bit
2584 * character sets usually fails and the text might actually be in
2585 * 'enc' anyway. */
2586 if (has_mbyte)
2587 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002588 char_u *conv_buf;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002589 vimconv_T vc;
2590
2591 vc.vc_type = CONV_NONE;
2592 if (convert_setup(&vc, (char_u *)"latin1", p_enc) == OK)
2593 {
2594 conv_buf = string_convert(&vc, buffer, &nbytes);
2595 if (conv_buf != NULL)
2596 {
2597 clip_yank_selection(MCHAR, conv_buf, (long)nbytes, cbd);
2598 vim_free(conv_buf);
2599 done = TRUE;
2600 }
2601 convert_setup(&vc, NULL, NULL);
2602 }
2603 }
2604 if (!done) /* use the text without conversion */
2605#endif
2606 clip_yank_selection(MCHAR, buffer, (long)nbytes, cbd);
2607 XFree((void *)buffer);
2608 if (p_verbose > 0)
2609 {
2610 verbose_enter();
2611 verb_msg((char_u *)_("Used CUT_BUFFER0 instead of empty selection"));
2612 verbose_leave();
2613 }
2614 }
2615}
2616#endif
2617
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618#if defined(FEAT_MOUSE) || defined(PROTO)
2619
2620/*
2621 * Move the cursor to the specified row and column on the screen.
Bram Moolenaar49325942007-05-10 19:19:59 +00002622 * Change current window if necessary. Returns an integer with the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 * CURSOR_MOVED bit set if the cursor has moved or unset otherwise.
2624 *
2625 * The MOUSE_FOLD_CLOSE bit is set when clicked on the '-' in a fold column.
2626 * The MOUSE_FOLD_OPEN bit is set when clicked on the '+' in a fold column.
2627 *
2628 * If flags has MOUSE_FOCUS, then the current window will not be changed, and
2629 * if the mouse is outside the window then the text will scroll, or if the
2630 * mouse was previously on a status line, then the status line may be dragged.
2631 *
2632 * If flags has MOUSE_MAY_VIS, then VIsual mode will be started before the
2633 * cursor is moved unless the cursor was on a status line.
2634 * This function returns one of IN_UNKNOWN, IN_BUFFER, IN_STATUS_LINE or
2635 * IN_SEP_LINE depending on where the cursor was clicked.
2636 *
2637 * If flags has MOUSE_MAY_STOP_VIS, then Visual mode will be stopped, unless
2638 * the mouse is on the status line of the same window.
2639 *
2640 * If flags has MOUSE_DID_MOVE, nothing is done if the mouse didn't move since
2641 * the last call.
2642 *
2643 * If flags has MOUSE_SETPOS, nothing is done, only the current position is
2644 * remembered.
2645 */
2646 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002647jump_to_mouse(
2648 int flags,
2649 int *inclusive, /* used for inclusive operator, can be NULL */
2650 int which_button) /* MOUSE_LEFT, MOUSE_RIGHT, MOUSE_MIDDLE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651{
2652 static int on_status_line = 0; /* #lines below bottom of window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 static int on_sep_line = 0; /* on separator right of window */
Bram Moolenaareb163d72017-09-23 15:08:17 +02002654#ifdef FEAT_MENU
2655 static int in_winbar = FALSE;
2656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 static int prev_row = -1;
2658 static int prev_col = -1;
2659 static win_T *dragwin = NULL; /* window being dragged */
2660 static int did_drag = FALSE; /* drag was noticed */
2661
2662 win_T *wp, *old_curwin;
2663 pos_T old_cursor;
2664 int count;
2665 int first;
2666 int row = mouse_row;
2667 int col = mouse_col;
2668#ifdef FEAT_FOLDING
2669 int mouse_char;
2670#endif
2671
2672 mouse_past_bottom = FALSE;
2673 mouse_past_eol = FALSE;
2674
2675 if (flags & MOUSE_RELEASED)
2676 {
2677 /* On button release we may change window focus if positioned on a
2678 * status line and no dragging happened. */
2679 if (dragwin != NULL && !did_drag)
2680 flags &= ~(MOUSE_FOCUS | MOUSE_DID_MOVE);
2681 dragwin = NULL;
2682 did_drag = FALSE;
2683 }
2684
2685 if ((flags & MOUSE_DID_MOVE)
2686 && prev_row == mouse_row
2687 && prev_col == mouse_col)
2688 {
2689retnomove:
Bram Moolenaar49325942007-05-10 19:19:59 +00002690 /* before moving the cursor for a left click which is NOT in a status
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 * line, stop Visual mode */
2692 if (on_status_line)
2693 return IN_STATUS_LINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 if (on_sep_line)
2695 return IN_SEP_LINE;
Bram Moolenaard327b0c2017-11-12 16:56:12 +01002696#ifdef FEAT_MENU
2697 if (in_winbar)
2698 {
2699 /* A quick second click may arrive as a double-click, but we use it
2700 * as a second click in the WinBar. */
2701 if ((mod_mask & MOD_MASK_MULTI_CLICK) && !(flags & MOUSE_RELEASED))
2702 {
2703 wp = mouse_find_win(&row, &col);
2704 if (wp == NULL)
2705 return IN_UNKNOWN;
2706 winbar_click(wp, col);
2707 }
2708 return IN_OTHER_WIN | MOUSE_WINBAR;
2709 }
2710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 if (flags & MOUSE_MAY_STOP_VIS)
2712 {
2713 end_visual_mode();
2714 redraw_curbuf_later(INVERTED); /* delete the inversion */
2715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2717 /* Continue a modeless selection in another window. */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002718 if (cmdwin_type != 0 && row < curwin->w_winrow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 return IN_OTHER_WIN;
2720#endif
2721 return IN_BUFFER;
2722 }
2723
2724 prev_row = mouse_row;
2725 prev_col = mouse_col;
2726
2727 if (flags & MOUSE_SETPOS)
2728 goto retnomove; /* ugly goto... */
2729
2730#ifdef FEAT_FOLDING
2731 /* Remember the character under the mouse, it might be a '-' or '+' in the
2732 * fold column. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002733 if (row >= 0 && row < Rows && col >= 0 && col <= Columns
2734 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 mouse_char = ScreenLines[LineOffset[row] + col];
2736 else
2737 mouse_char = ' ';
2738#endif
2739
2740 old_curwin = curwin;
2741 old_cursor = curwin->w_cursor;
2742
2743 if (!(flags & MOUSE_FOCUS))
2744 {
2745 if (row < 0 || col < 0) /* check if it makes sense */
2746 return IN_UNKNOWN;
2747
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 /* find the window where the row is in */
2749 wp = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02002750 if (wp == NULL)
2751 return IN_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 dragwin = NULL;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002753
2754#ifdef FEAT_MENU
2755 if (row == -1)
2756 {
2757 /* A click in the window toolbar does not enter another window or
2758 * change Visual highlighting. */
2759 winbar_click(wp, col);
Bram Moolenaareb163d72017-09-23 15:08:17 +02002760 in_winbar = TRUE;
2761 return IN_OTHER_WIN | MOUSE_WINBAR;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002762 }
Bram Moolenaareb163d72017-09-23 15:08:17 +02002763 in_winbar = FALSE;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002764#endif
2765
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 /*
2767 * winpos and height may change in win_enter()!
2768 */
2769 if (row >= wp->w_height) /* In (or below) status line */
2770 {
2771 on_status_line = row - wp->w_height + 1;
2772 dragwin = wp;
2773 }
2774 else
2775 on_status_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 if (col >= wp->w_width) /* In separator line */
2777 {
2778 on_sep_line = col - wp->w_width + 1;
2779 dragwin = wp;
2780 }
2781 else
2782 on_sep_line = 0;
2783
2784 /* The rightmost character of the status line might be a vertical
2785 * separator character if there is no connecting window to the right. */
2786 if (on_status_line && on_sep_line)
2787 {
2788 if (stl_connected(wp))
2789 on_sep_line = 0;
2790 else
2791 on_status_line = 0;
2792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 /* Before jumping to another buffer, or moving the cursor for a left
2795 * click, stop Visual mode. */
2796 if (VIsual_active
2797 && (wp->w_buffer != curwin->w_buffer
Bram Moolenaar4033c552017-09-16 20:54:51 +02002798 || (!on_status_line && !on_sep_line
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002799#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002801# ifdef FEAT_RIGHTLEFT
Bram Moolenaar02631462017-09-22 15:20:32 +02002802 wp->w_p_rl ? col < wp->w_width - wp->w_p_fdc :
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803# endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002804 col >= wp->w_p_fdc
2805# ifdef FEAT_CMDWIN
2806 + (cmdwin_type == 0 && wp == curwin ? 0 : 1)
2807# endif
2808 )
2809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 && (flags & MOUSE_MAY_STOP_VIS))))
2811 {
2812 end_visual_mode();
2813 redraw_curbuf_later(INVERTED); /* delete the inversion */
2814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815#ifdef FEAT_CMDWIN
2816 if (cmdwin_type != 0 && wp != curwin)
2817 {
2818 /* A click outside the command-line window: Use modeless
Bram Moolenaarf679a432010-03-02 18:16:09 +01002819 * selection if possible. Allow dragging the status lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 on_sep_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821# ifdef FEAT_CLIPBOARD
2822 if (on_status_line)
2823 return IN_STATUS_LINE;
2824 return IN_OTHER_WIN;
2825# else
2826 row = 0;
2827 col += wp->w_wincol;
2828 wp = curwin;
2829# endif
2830 }
2831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 /* Only change window focus when not clicking on or dragging the
2833 * status line. Do change focus when releasing the mouse button
2834 * (MOUSE_FOCUS was set above if we dragged first). */
2835 if (dragwin == NULL || (flags & MOUSE_RELEASED))
2836 win_enter(wp, TRUE); /* can make wp invalid! */
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002837
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 if (curwin != old_curwin)
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002839 {
2840#ifdef CHECK_DOUBLE_CLICK
2841 /* set topline, to be able to check for double click ourselves */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 set_mouse_topline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843#endif
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002844#ifdef FEAT_TERMINAL
2845 /* when entering a terminal window may change state */
2846 term_win_entered();
2847#endif
2848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 if (on_status_line) /* In (or below) status line */
2850 {
2851 /* Don't use start_arrow() if we're in the same window */
2852 if (curwin == old_curwin)
2853 return IN_STATUS_LINE;
2854 else
2855 return IN_STATUS_LINE | CURSOR_MOVED;
2856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 if (on_sep_line) /* In (or below) status line */
2858 {
2859 /* Don't use start_arrow() if we're in the same window */
2860 if (curwin == old_curwin)
2861 return IN_SEP_LINE;
2862 else
2863 return IN_SEP_LINE | CURSOR_MOVED;
2864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865
2866 curwin->w_cursor.lnum = curwin->w_topline;
2867#ifdef FEAT_GUI
2868 /* remember topline, needed for double click */
2869 gui_prev_topline = curwin->w_topline;
2870# ifdef FEAT_DIFF
2871 gui_prev_topfill = curwin->w_topfill;
2872# endif
2873#endif
2874 }
2875 else if (on_status_line && which_button == MOUSE_LEFT)
2876 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 if (dragwin != NULL)
2878 {
2879 /* Drag the status line */
2880 count = row - dragwin->w_winrow - dragwin->w_height + 1
2881 - on_status_line;
2882 win_drag_status_line(dragwin, count);
2883 did_drag |= count;
2884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 return IN_STATUS_LINE; /* Cursor didn't move */
2886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 else if (on_sep_line && which_button == MOUSE_LEFT)
2888 {
2889 if (dragwin != NULL)
2890 {
2891 /* Drag the separator column */
2892 count = col - dragwin->w_wincol - dragwin->w_width + 1
2893 - on_sep_line;
2894 win_drag_vsep_line(dragwin, count);
2895 did_drag |= count;
2896 }
2897 return IN_SEP_LINE; /* Cursor didn't move */
2898 }
Bram Moolenaareb163d72017-09-23 15:08:17 +02002899#ifdef FEAT_MENU
2900 else if (in_winbar)
2901 {
2902 /* After a click on the window toolbar don't start Visual mode. */
2903 return IN_OTHER_WIN | MOUSE_WINBAR;
2904 }
2905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 else /* keep_window_focus must be TRUE */
2907 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 /* before moving the cursor for a left click, stop Visual mode */
2909 if (flags & MOUSE_MAY_STOP_VIS)
2910 {
2911 end_visual_mode();
2912 redraw_curbuf_later(INVERTED); /* delete the inversion */
2913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914
2915#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2916 /* Continue a modeless selection in another window. */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002917 if (cmdwin_type != 0 && row < curwin->w_winrow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 return IN_OTHER_WIN;
2919#endif
2920
2921 row -= W_WINROW(curwin);
Bram Moolenaar53f81742017-09-22 14:35:51 +02002922 col -= curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923
2924 /*
2925 * When clicking beyond the end of the window, scroll the screen.
2926 * Scroll by however many rows outside the window we are.
2927 */
2928 if (row < 0)
2929 {
2930 count = 0;
2931 for (first = TRUE; curwin->w_topline > 1; )
2932 {
2933#ifdef FEAT_DIFF
2934 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
2935 ++count;
2936 else
2937#endif
2938 count += plines(curwin->w_topline - 1);
2939 if (!first && count > -row)
2940 break;
2941 first = FALSE;
2942#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002943 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944#endif
2945#ifdef FEAT_DIFF
2946 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
2947 ++curwin->w_topfill;
2948 else
2949#endif
2950 {
2951 --curwin->w_topline;
2952#ifdef FEAT_DIFF
2953 curwin->w_topfill = 0;
2954#endif
2955 }
2956 }
2957#ifdef FEAT_DIFF
2958 check_topfill(curwin, FALSE);
2959#endif
2960 curwin->w_valid &=
2961 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2962 redraw_later(VALID);
2963 row = 0;
2964 }
2965 else if (row >= curwin->w_height)
2966 {
2967 count = 0;
2968 for (first = TRUE; curwin->w_topline < curbuf->b_ml.ml_line_count; )
2969 {
2970#ifdef FEAT_DIFF
2971 if (curwin->w_topfill > 0)
2972 ++count;
2973 else
2974#endif
2975 count += plines(curwin->w_topline);
2976 if (!first && count > row - curwin->w_height + 1)
2977 break;
2978 first = FALSE;
2979#ifdef FEAT_FOLDING
2980 if (hasFolding(curwin->w_topline, NULL, &curwin->w_topline)
2981 && curwin->w_topline == curbuf->b_ml.ml_line_count)
2982 break;
2983#endif
2984#ifdef FEAT_DIFF
2985 if (curwin->w_topfill > 0)
2986 --curwin->w_topfill;
2987 else
2988#endif
2989 {
2990 ++curwin->w_topline;
2991#ifdef FEAT_DIFF
2992 curwin->w_topfill =
2993 diff_check_fill(curwin, curwin->w_topline);
2994#endif
2995 }
2996 }
2997#ifdef FEAT_DIFF
2998 check_topfill(curwin, FALSE);
2999#endif
3000 redraw_later(VALID);
3001 curwin->w_valid &=
3002 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3003 row = curwin->w_height - 1;
3004 }
3005 else if (row == 0)
3006 {
3007 /* When dragging the mouse, while the text has been scrolled up as
3008 * far as it goes, moving the mouse in the top line should scroll
3009 * the text down (done later when recomputing w_topline). */
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003010 if (mouse_dragging > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 && curwin->w_cursor.lnum
3012 == curwin->w_buffer->b_ml.ml_line_count
3013 && curwin->w_cursor.lnum == curwin->w_topline)
3014 curwin->w_valid &= ~(VALID_TOPLINE);
3015 }
3016 }
3017
3018#ifdef FEAT_FOLDING
3019 /* Check for position outside of the fold column. */
3020 if (
3021# ifdef FEAT_RIGHTLEFT
Bram Moolenaar02631462017-09-22 15:20:32 +02003022 curwin->w_p_rl ? col < curwin->w_width - curwin->w_p_fdc :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023# endif
3024 col >= curwin->w_p_fdc
3025# ifdef FEAT_CMDWIN
3026 + (cmdwin_type == 0 ? 0 : 1)
3027# endif
3028 )
3029 mouse_char = ' ';
3030#endif
3031
3032 /* compute the position in the buffer line from the posn on the screen */
3033 if (mouse_comp_pos(curwin, &row, &col, &curwin->w_cursor.lnum))
3034 mouse_past_bottom = TRUE;
3035
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 /* Start Visual mode before coladvance(), for when 'sel' != "old" */
3037 if ((flags & MOUSE_MAY_VIS) && !VIsual_active)
3038 {
3039 check_visual_highlight();
3040 VIsual = old_cursor;
3041 VIsual_active = TRUE;
3042 VIsual_reselect = TRUE;
3043 /* if 'selectmode' contains "mouse", start Select mode */
3044 may_start_select('o');
3045 setmouse();
Bram Moolenaar7df351e2006-01-23 22:30:28 +00003046 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 redraw_cmdline = TRUE; /* show visual mode later */
3048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049
3050 curwin->w_curswant = col;
3051 curwin->w_set_curswant = FALSE; /* May still have been TRUE */
3052 if (coladvance(col) == FAIL) /* Mouse click beyond end of line */
3053 {
3054 if (inclusive != NULL)
3055 *inclusive = TRUE;
3056 mouse_past_eol = TRUE;
3057 }
3058 else if (inclusive != NULL)
3059 *inclusive = FALSE;
3060
3061 count = IN_BUFFER;
3062 if (curwin != old_curwin || curwin->w_cursor.lnum != old_cursor.lnum
3063 || curwin->w_cursor.col != old_cursor.col)
3064 count |= CURSOR_MOVED; /* Cursor has moved */
3065
3066#ifdef FEAT_FOLDING
3067 if (mouse_char == '+')
3068 count |= MOUSE_FOLD_OPEN;
3069 else if (mouse_char != ' ')
3070 count |= MOUSE_FOLD_CLOSE;
3071#endif
3072
3073 return count;
3074}
3075
3076/*
3077 * Compute the position in the buffer line from the posn on the screen in
3078 * window "win".
3079 * Returns TRUE if the position is below the last line.
3080 */
3081 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003082mouse_comp_pos(
3083 win_T *win,
3084 int *rowp,
3085 int *colp,
3086 linenr_T *lnump)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087{
3088 int col = *colp;
3089 int row = *rowp;
3090 linenr_T lnum;
3091 int retval = FALSE;
3092 int off;
3093 int count;
3094
3095#ifdef FEAT_RIGHTLEFT
3096 if (win->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02003097 col = win->w_width - 1 - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098#endif
3099
3100 lnum = win->w_topline;
3101
3102 while (row > 0)
3103 {
3104#ifdef FEAT_DIFF
3105 /* Don't include filler lines in "count" */
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003106 if (win->w_p_diff
3107# ifdef FEAT_FOLDING
3108 && !hasFoldingWin(win, lnum, NULL, NULL, TRUE, NULL)
3109# endif
3110 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 {
3112 if (lnum == win->w_topline)
3113 row -= win->w_topfill;
3114 else
3115 row -= diff_check_fill(win, lnum);
3116 count = plines_win_nofill(win, lnum, TRUE);
3117 }
3118 else
3119#endif
3120 count = plines_win(win, lnum, TRUE);
3121 if (count > row)
3122 break; /* Position is in this buffer line. */
3123#ifdef FEAT_FOLDING
3124 (void)hasFoldingWin(win, lnum, NULL, &lnum, TRUE, NULL);
3125#endif
3126 if (lnum == win->w_buffer->b_ml.ml_line_count)
3127 {
3128 retval = TRUE;
3129 break; /* past end of file */
3130 }
3131 row -= count;
3132 ++lnum;
3133 }
3134
3135 if (!retval)
3136 {
3137 /* Compute the column without wrapping. */
3138 off = win_col_off(win) - win_col_off2(win);
3139 if (col < off)
3140 col = off;
Bram Moolenaar02631462017-09-22 15:20:32 +02003141 col += row * (win->w_width - off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 /* add skip column (for long wrapping line) */
3143 col += win->w_skipcol;
3144 }
3145
3146 if (!win->w_p_wrap)
3147 col += win->w_leftcol;
3148
3149 /* skip line number and fold column in front of the line */
3150 col -= win_col_off(win);
3151 if (col < 0)
3152 {
3153#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003154 netbeans_gutter_click(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155#endif
3156 col = 0;
3157 }
3158
3159 *colp = col;
3160 *rowp = row;
3161 *lnump = lnum;
3162 return retval;
3163}
3164
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165/*
3166 * Find the window at screen position "*rowp" and "*colp". The positions are
3167 * updated to become relative to the top-left of the window.
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003168 * Returns NULL when something is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 win_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003171mouse_find_win(int *rowp, int *colp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172{
3173 frame_T *fp;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003174 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
3176 fp = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003177 *rowp -= firstwin->w_winrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 for (;;)
3179 {
3180 if (fp->fr_layout == FR_LEAF)
3181 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 if (fp->fr_layout == FR_ROW)
3183 {
3184 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
3185 {
3186 if (*colp < fp->fr_width)
3187 break;
3188 *colp -= fp->fr_width;
3189 }
3190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 else /* fr_layout == FR_COL */
3192 {
3193 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
3194 {
3195 if (*rowp < fp->fr_height)
3196 break;
3197 *rowp -= fp->fr_height;
3198 }
3199 }
3200 }
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003201 /* When using a timer that closes a window the window might not actually
3202 * exist. */
3203 FOR_ALL_WINDOWS(wp)
3204 if (wp == fp->fr_win)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003205 {
3206#ifdef FEAT_MENU
3207 *rowp -= wp->w_winbar_height;
3208#endif
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003209 return wp;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003210 }
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003211 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213
Bram Moolenaar860cae12010-06-05 23:22:07 +02003214#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar658a1542018-03-03 19:29:43 +01003216 || defined(FEAT_GUI_PHOTON) || defined(FEAT_TERM_POPUP_MENU) \
3217 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218/*
3219 * Translate window coordinates to buffer position without any side effects
3220 */
3221 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003222get_fpos_of_mouse(pos_T *mpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223{
3224 win_T *wp;
3225 int row = mouse_row;
3226 int col = mouse_col;
3227
3228 if (row < 0 || col < 0) /* check if it makes sense */
3229 return IN_UNKNOWN;
3230
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 /* find the window where the row is in */
3232 wp = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003233 if (wp == NULL)
3234 return IN_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 /*
3236 * winpos and height may change in win_enter()!
3237 */
3238 if (row >= wp->w_height) /* In (or below) status line */
3239 return IN_STATUS_LINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 if (col >= wp->w_width) /* In vertical separator line */
3241 return IN_SEP_LINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242
3243 if (wp != curwin)
3244 return IN_UNKNOWN;
3245
3246 /* compute the position in the buffer line from the posn on the screen */
3247 if (mouse_comp_pos(curwin, &row, &col, &mpos->lnum))
3248 return IN_STATUS_LINE; /* past bottom */
3249
3250 mpos->col = vcol2col(wp, mpos->lnum, col);
3251
3252 if (mpos->col > 0)
3253 --mpos->col;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003254#ifdef FEAT_VIRTUALEDIT
3255 mpos->coladd = 0;
3256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 return IN_BUFFER;
3258}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003261#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MAC) \
3262 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar3767b612018-03-03 19:51:58 +01003263 || defined(FEAT_GUI_PHOTON) || defined(FEAT_BEVAL) \
3264 || defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265/*
3266 * Convert a virtual (screen) column to a character column.
3267 * The first column is one.
3268 */
3269 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003270vcol2col(win_T *wp, linenr_T lnum, int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271{
3272 /* try to advance to the specified column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 int count = 0;
3274 char_u *ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003275 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276
Bram Moolenaar597a4222014-06-25 14:39:50 +02003277 line = ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaarb6101cf2012-10-21 00:58:39 +02003278 while (count < vcol && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003280 count += win_lbr_chartabsize(wp, line, ptr, count, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003281 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02003283 return (int)(ptr - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284}
3285#endif
3286
3287#endif /* FEAT_MOUSE */
3288
3289#if defined(FEAT_GUI) || defined(WIN3264) || defined(PROTO)
3290/*
3291 * Called when focus changed. Used for the GUI or for systems where this can
3292 * be done in the console (Win32).
3293 */
3294 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003295ui_focus_change(
3296 int in_focus) /* TRUE if focus gained. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297{
3298 static time_t last_time = (time_t)0;
3299 int need_redraw = FALSE;
3300
3301 /* When activated: Check if any file was modified outside of Vim.
3302 * Only do this when not done within the last two seconds (could get
3303 * several events in a row). */
3304 if (in_focus && last_time + 2 < time(NULL))
3305 {
3306 need_redraw = check_timestamps(
3307# ifdef FEAT_GUI
3308 gui.in_use
3309# else
3310 FALSE
3311# endif
3312 );
3313 last_time = time(NULL);
3314 }
3315
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 /*
3317 * Fire the focus gained/lost autocommand.
3318 */
3319 need_redraw |= apply_autocmds(in_focus ? EVENT_FOCUSGAINED
3320 : EVENT_FOCUSLOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321
3322 if (need_redraw)
3323 {
3324 /* Something was executed, make sure the cursor is put back where it
3325 * belongs. */
3326 need_wait_return = FALSE;
3327
3328 if (State & CMDLINE)
3329 redrawcmdline();
3330 else if (State == HITRETURN || State == SETWSIZE || State == ASKMORE
3331 || State == EXTERNCMD || State == CONFIRM || exmode_active)
3332 repeat_message();
3333 else if ((State & NORMAL) || (State & INSERT))
3334 {
3335 if (must_redraw != 0)
3336 update_screen(0);
3337 setcursor();
3338 }
3339 cursor_on(); /* redrawing may have switched it off */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003340 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341# ifdef FEAT_GUI
3342 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 gui_update_scrollbars(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344# endif
3345 }
3346#ifdef FEAT_TITLE
3347 /* File may have been changed from 'readonly' to 'noreadonly' */
3348 if (need_maketitle)
3349 maketitle();
3350#endif
3351}
3352#endif
3353
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003354#if defined(HAVE_INPUT_METHOD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355/*
3356 * Save current Input Method status to specified place.
3357 */
3358 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003359im_save_status(long *psave)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360{
3361 /* Don't save when 'imdisable' is set or "xic" is NULL, IM is always
3362 * disabled then (but might start later).
3363 * Also don't save when inside a mapping, vgetc_im_active has not been set
3364 * then.
3365 * And don't save when the keys were stuffed (e.g., for a "." command).
3366 * And don't save when the GUI is running but our window doesn't have
3367 * input focus (e.g., when a find dialog is open). */
3368 if (!p_imdisable && KeyTyped && !KeyStuffed
3369# ifdef FEAT_XIM
3370 && xic != NULL
3371# endif
3372# ifdef FEAT_GUI
3373 && (!gui.in_use || gui.in_focus)
3374# endif
3375 )
3376 {
3377 /* Do save when IM is on, or IM is off and saved status is on. */
3378 if (vgetc_im_active)
3379 *psave = B_IMODE_IM;
3380 else if (*psave == B_IMODE_IM)
3381 *psave = B_IMODE_NONE;
3382 }
3383}
3384#endif