blob: d341448e3b2d23b5d6fecca51d532847204f404f [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 Moolenaar13410242018-11-26 21:19:11 +0100157 retval = mch_inchar(buf, maxlen, wtime, tb_change_cnt);
Bram Moolenaar43b604c2005-03-22 23:06:55 +0000158 if (retval > 0 || typebuf_changed(tb_change_cnt) || wtime >= 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000159 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160# endif
161 if (wtime == -1 && ++count == 1000)
162 read_error_exit();
163 buf[0] = CAR;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000164 retval = 1;
165 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 }
167#endif
168
Bram Moolenaarc8da3112007-03-08 12:36:46 +0000169 /* If we are going to wait for some time or block... */
170 if (wtime == -1 || wtime > 100L)
171 {
172 /* ... allow signals to kill us. */
173 (void)vim_handle_signal(SIGNAL_UNBLOCK);
174
175 /* ... there is no need for CTRL-C to interrupt something, don't let
176 * it set got_int when it was mapped. */
Bram Moolenaar50008692015-01-14 16:08:32 +0100177 if ((mapped_ctrl_c | curbuf->b_mapped_ctrl_c) & get_real_state())
Bram Moolenaarc8da3112007-03-08 12:36:46 +0000178 ctrl_c_interrupts = FALSE;
179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180
181#ifdef FEAT_GUI
182 if (gui.in_use)
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100183 retval = gui_inchar(buf, maxlen, wtime, tb_change_cnt);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184#endif
185#ifndef NO_CONSOLE
186# ifdef FEAT_GUI
187 else
188# endif
189 retval = mch_inchar(buf, maxlen, wtime, tb_change_cnt);
190#endif
191
Bram Moolenaarc8da3112007-03-08 12:36:46 +0000192 if (wtime == -1 || wtime > 100L)
193 /* block SIGHUP et al. */
194 (void)vim_handle_signal(SIGNAL_BLOCK);
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 ctrl_c_interrupts = TRUE;
197
Bram Moolenaar05159a02005-02-26 23:04:13 +0000198#ifdef NO_CONSOLE_INPUT
199theend:
200#endif
201#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000202 if (do_profiling == PROF_YES && wtime != 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000203 prof_inchar_exit();
204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 return retval;
206}
207
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100208#if defined(FEAT_TIMERS) || defined(PROT)
209/*
210 * Wait for a timer to fire or "wait_func" to return non-zero.
211 * Returns OK when something was read.
212 * Returns FAIL when it timed out or was interrupted.
213 */
214 int
215ui_wait_for_chars_or_timer(
216 long wtime,
217 int (*wait_func)(long wtime, int *interrupted, int ignore_input),
218 int *interrupted,
219 int ignore_input)
220{
221 int due_time;
222 long remaining = wtime;
223 int tb_change_cnt = typebuf.tb_change_cnt;
224
225 /* When waiting very briefly don't trigger timers. */
226 if (wtime >= 0 && wtime < 10L)
227 return wait_func(wtime, NULL, ignore_input);
228
229 while (wtime < 0 || remaining > 0)
230 {
231 /* Trigger timers and then get the time in wtime until the next one is
232 * due. Wait up to that time. */
233 due_time = check_due_timer();
234 if (typebuf.tb_change_cnt != tb_change_cnt)
235 {
236 /* timer may have used feedkeys() */
237 return FAIL;
238 }
239 if (due_time <= 0 || (wtime > 0 && due_time > remaining))
240 due_time = remaining;
241 if (wait_func(due_time, interrupted, ignore_input))
242 return OK;
243 if (interrupted != NULL && *interrupted)
244 /* Nothing available, but need to return so that side effects get
245 * handled, such as handling a message on a channel. */
Bram Moolenaara338adc2018-01-31 20:51:47 +0100246 return FAIL;
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100247 if (wtime > 0)
248 remaining -= due_time;
249 }
250 return FAIL;
251}
252#endif
253
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254/*
255 * return non-zero if a character is available
256 */
257 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100258ui_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259{
260#ifdef FEAT_GUI
261 if (gui.in_use)
262 {
263 gui_mch_update();
264 return input_available();
265 }
266#endif
267#ifndef NO_CONSOLE
268# ifdef NO_CONSOLE_INPUT
269 if (no_console_input())
270 return 0;
271# endif
272 return mch_char_avail();
273#else
274 return 0;
275#endif
276}
277
278/*
279 * Delay for the given number of milliseconds. If ignoreinput is FALSE then we
280 * cancel the delay if a key is hit.
281 */
282 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100283ui_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284{
285#ifdef FEAT_GUI
286 if (gui.in_use && !ignoreinput)
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100287 gui_wait_for_chars(msec, typebuf.tb_change_cnt);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 else
289#endif
290 mch_delay(msec, ignoreinput);
291}
292
293/*
294 * If the machine has job control, use it to suspend the program,
295 * otherwise fake it by starting a new shell.
296 * When running the GUI iconify the window.
297 */
298 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100299ui_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300{
301#ifdef FEAT_GUI
302 if (gui.in_use)
303 {
304 gui_mch_iconify();
305 return;
306 }
307#endif
308 mch_suspend();
309}
310
311#if !defined(UNIX) || !defined(SIGTSTP) || defined(PROTO) || defined(__BEOS__)
312/*
313 * When the OS can't really suspend, call this function to start a shell.
314 * This is never called in the GUI.
315 */
316 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100317suspend_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318{
319 if (*p_sh == NUL)
320 EMSG(_(e_shellempty));
321 else
322 {
323 MSG_PUTS(_("new shell started\n"));
324 do_shell(NULL, 0);
325 }
326}
327#endif
328
329/*
330 * Try to get the current Vim shell size. Put the result in Rows and Columns.
331 * Use the new sizes as defaults for 'columns' and 'lines'.
332 * Return OK when size could be determined, FAIL otherwise.
333 */
334 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100335ui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336{
337 int retval;
338
339#ifdef FEAT_GUI
340 if (gui.in_use)
341 retval = gui_get_shellsize();
342 else
343#endif
344 retval = mch_get_shellsize();
345
346 check_shellsize();
347
348 /* adjust the default for 'lines' and 'columns' */
349 if (retval == OK)
350 {
351 set_number_default("lines", Rows);
352 set_number_default("columns", Columns);
353 }
354 return retval;
355}
356
357/*
358 * Set the size of the Vim shell according to Rows and Columns, if possible.
359 * The gui_set_shellsize() or mch_set_shellsize() function will try to set the
360 * new size. If this is not possible, it will adjust Rows and Columns.
361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100363ui_set_shellsize(
364 int mustset UNUSED) /* set by the user */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365{
366#ifdef FEAT_GUI
367 if (gui.in_use)
Bram Moolenaar8968a312013-07-03 16:58:44 +0200368 gui_set_shellsize(mustset, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 else
370#endif
371 mch_set_shellsize();
372}
373
374/*
375 * Called when Rows and/or Columns changed. Adjust scroll region and mouse
376 * region.
377 */
378 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100379ui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380{
381 if (full_screen && !exiting)
382 {
383#ifdef FEAT_GUI
384 if (gui.in_use)
385 gui_new_shellsize();
386 else
387#endif
388 mch_new_shellsize();
389 }
390}
391
392 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100393ui_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200395 ui_breakcheck_force(FALSE);
396}
397
398/*
399 * When "force" is true also check when the terminal is not in raw mode.
400 * This is useful to read input on channels.
401 */
402 void
403ui_breakcheck_force(int force)
404{
Bram Moolenaar48d23bb2018-11-20 02:42:43 +0100405 static int recursive = FALSE;
406 int save_updating_screen = updating_screen;
Bram Moolenaare3caa112017-01-31 22:07:42 +0100407
Bram Moolenaar48d23bb2018-11-20 02:42:43 +0100408 // We could be called recursively if stderr is redirected, calling
409 // fill_input_buf() calls settmode() when stdin isn't a tty. settmode()
410 // calls vgetorpeek() which calls ui_breakcheck() again.
411 if (recursive)
412 return;
413 recursive = TRUE;
414
415 // We do not want gui_resize_shell() to redraw the screen here.
Bram Moolenaare3caa112017-01-31 22:07:42 +0100416 ++updating_screen;
417
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418#ifdef FEAT_GUI
419 if (gui.in_use)
420 gui_mch_update();
421 else
422#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200423 mch_breakcheck(force);
Bram Moolenaare3caa112017-01-31 22:07:42 +0100424
Bram Moolenaar42335f52018-09-13 15:33:43 +0200425 if (save_updating_screen)
426 updating_screen = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200427 else
428 reset_updating_screen(FALSE);
Bram Moolenaar48d23bb2018-11-20 02:42:43 +0100429
430 recursive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431}
432
433/*****************************************************************************
434 * Functions for copying and pasting text between applications.
435 * This is always included in a GUI version, but may also be included when the
436 * clipboard and mouse is available to a terminal version such as xterm.
437 * Note: there are some more functions in ops.c that handle selection stuff.
438 *
439 * Also note that the majority of functions here deal with the X 'primary'
440 * (visible - for Visual mode use) selection, and only that. There are no
441 * versions of these for the 'clipboard' selection, as Visual mode has no use
442 * for them.
443 */
444
445#if defined(FEAT_CLIPBOARD) || defined(PROTO)
446
447/*
448 * Selection stuff using Visual mode, for cutting and pasting text to other
449 * windows.
450 */
451
452/*
453 * Call this to initialise the clipboard. Pass it FALSE if the clipboard code
454 * is included, but the clipboard can not be used, or TRUE if the clipboard can
455 * be used. Eg unix may call this with FALSE, then call it again with TRUE if
456 * the GUI starts.
457 */
458 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100459clip_init(int can_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460{
461 VimClipboard *cb;
462
463 cb = &clip_star;
464 for (;;)
465 {
466 cb->available = can_use;
467 cb->owned = FALSE;
468 cb->start.lnum = 0;
469 cb->start.col = 0;
470 cb->end.lnum = 0;
471 cb->end.col = 0;
472 cb->state = SELECT_CLEARED;
473
474 if (cb == &clip_plus)
475 break;
476 cb = &clip_plus;
477 }
478}
479
480/*
481 * Check whether the VIsual area has changed, and if so try to become the owner
482 * of the selection, and free any old converted selection we may still have
483 * lying around. If the VIsual mode has ended, make a copy of what was
484 * selected so we can still give it to others. Will probably have to make sure
485 * this is called whenever VIsual mode is ended.
486 */
487 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100488clip_update_selection(VimClipboard *clip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489{
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200490 pos_T start, end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491
492 /* If visual mode is only due to a redo command ("."), then ignore it */
493 if (!redo_VIsual_busy && VIsual_active && (State & NORMAL))
494 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100495 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 {
497 start = VIsual;
498 end = curwin->w_cursor;
499#ifdef FEAT_MBYTE
500 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000501 end.col += (*mb_ptr2len)(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502#endif
503 }
504 else
505 {
506 start = curwin->w_cursor;
507 end = VIsual;
508 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100509 if (!EQUAL_POS(clip->start, start)
510 || !EQUAL_POS(clip->end, end)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200511 || clip->vmode != VIsual_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200513 clip_clear_selection(clip);
514 clip->start = start;
515 clip->end = end;
516 clip->vmode = VIsual_mode;
517 clip_free_selection(clip);
518 clip_own_selection(clip);
519 clip_gen_set_selection(clip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 }
521 }
522}
523
524 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100525clip_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526{
527 /*
528 * Also want to check somehow that we are reading from the keyboard rather
529 * than a mapping etc.
530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531#ifdef FEAT_X11
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200532 /* Always own the selection, we might have lost it without being
Bram Moolenaar62b42182010-09-21 22:09:37 +0200533 * notified, e.g. during a ":sh" command. */
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200534 if (cbd->available)
535 {
536 int was_owned = cbd->owned;
537
538 cbd->owned = (clip_gen_own_selection(cbd) == OK);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200539 if (!was_owned && (cbd == &clip_star || cbd == &clip_plus))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 {
Bram Moolenaarebefac62005-12-28 22:39:57 +0000541 /* May have to show a different kind of highlighting for the
542 * selected area. There is no specific redraw command for this,
543 * just redraw all windows on the current buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 if (cbd->owned
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000545 && (get_real_state() == VISUAL
546 || get_real_state() == SELECTMODE)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200547 && (cbd == &clip_star ? clip_isautosel_star()
548 : clip_isautosel_plus())
Bram Moolenaar8820b482017-03-16 17:23:31 +0100549 && HL_ATTR(HLF_V) != HL_ATTR(HLF_VNC))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 redraw_curbuf_later(INVERTED_ALL);
551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200553#else
Bram Moolenaarb6101cf2012-10-21 00:58:39 +0200554 /* Only own the clipboard when we didn't own it yet. */
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200555 if (!cbd->owned && cbd->available)
556 cbd->owned = (clip_gen_own_selection(cbd) == OK);
557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558}
559
560 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100561clip_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562{
563#ifdef FEAT_X11
564 int was_owned = cbd->owned;
565#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200566 int visual_selection = FALSE;
567
568 if (cbd == &clip_star || cbd == &clip_plus)
569 visual_selection = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570
571 clip_free_selection(cbd);
572 cbd->owned = FALSE;
573 if (visual_selection)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200574 clip_clear_selection(cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 clip_gen_lose_selection(cbd);
576#ifdef FEAT_X11
577 if (visual_selection)
578 {
579 /* May have to show a different kind of highlighting for the selected
580 * area. There is no specific redraw command for this, just redraw all
581 * windows on the current buffer. */
582 if (was_owned
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000583 && (get_real_state() == VISUAL
584 || get_real_state() == SELECTMODE)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200585 && (cbd == &clip_star ?
586 clip_isautosel_star() : clip_isautosel_plus())
Bram Moolenaar8820b482017-03-16 17:23:31 +0100587 && HL_ATTR(HLF_V) != HL_ATTR(HLF_VNC))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 {
589 update_curbuf(INVERTED_ALL);
590 setcursor();
591 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +0100592 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 }
594 }
595#endif
596}
597
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200598 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100599clip_copy_selection(VimClipboard *clip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600{
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200601 if (VIsual_active && (State & NORMAL) && clip->available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200603 clip_update_selection(clip);
604 clip_free_selection(clip);
605 clip_own_selection(clip);
606 if (clip->owned)
607 clip_get_selection(clip);
608 clip_gen_set_selection(clip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 }
610}
611
612/*
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200613 * Save and restore clip_unnamed before doing possibly many changes. This
614 * prevents accessing the clipboard very often which might slow down Vim
615 * considerably.
616 */
Bram Moolenaar73a156b2015-01-27 21:39:05 +0100617static int global_change_count = 0; /* if set, inside a start_global_changes */
Bram Moolenaar3fcfa352017-03-29 19:20:41 +0200618static int clipboard_needs_update = FALSE; /* clipboard needs to be updated */
619static int clip_did_set_selection = TRUE;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200620
621/*
622 * Save clip_unnamed and reset it.
623 */
624 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100625start_global_changes(void)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200626{
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100627 if (++global_change_count > 1)
628 return;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200629 clip_unnamed_saved = clip_unnamed;
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100630 clipboard_needs_update = FALSE;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200631
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100632 if (clip_did_set_selection)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200633 {
634 clip_unnamed = FALSE;
635 clip_did_set_selection = FALSE;
636 }
637}
638
639/*
Bram Moolenaar3fcfa352017-03-29 19:20:41 +0200640 * Return TRUE if setting the clipboard was postponed, it already contains the
641 * right text.
642 */
643 int
644is_clipboard_needs_update()
645{
646 return clipboard_needs_update;
647}
648
649/*
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200650 * Restore clip_unnamed and set the selection when needed.
651 */
652 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100653end_global_changes(void)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200654{
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100655 if (--global_change_count > 0)
656 /* recursive */
657 return;
658 if (!clip_did_set_selection)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200659 {
660 clip_did_set_selection = TRUE;
661 clip_unnamed = clip_unnamed_saved;
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100662 clip_unnamed_saved = FALSE;
663 if (clipboard_needs_update)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200664 {
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100665 /* only store something in the clipboard,
666 * if we have yanked anything to it */
667 if (clip_unnamed & CLIP_UNNAMED)
668 {
669 clip_own_selection(&clip_star);
670 clip_gen_set_selection(&clip_star);
671 }
672 if (clip_unnamed & CLIP_UNNAMED_PLUS)
673 {
674 clip_own_selection(&clip_plus);
675 clip_gen_set_selection(&clip_plus);
676 }
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200677 }
678 }
Bram Moolenaar3fcfa352017-03-29 19:20:41 +0200679 clipboard_needs_update = FALSE;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200680}
681
682/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 * Called when Visual mode is ended: update the selection.
684 */
685 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100686clip_auto_select(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687{
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200688 if (clip_isautosel_star())
689 clip_copy_selection(&clip_star);
690 if (clip_isautosel_plus())
691 clip_copy_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692}
693
694/*
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200695 * Return TRUE if automatic selection of Visual area is desired for the *
696 * register.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 */
698 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100699clip_isautosel_star(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700{
701 return (
702#ifdef FEAT_GUI
703 gui.in_use ? (vim_strchr(p_go, GO_ASEL) != NULL) :
704#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200705 clip_autoselect_star);
706}
707
708/*
709 * Return TRUE if automatic selection of Visual area is desired for the +
710 * register.
711 */
712 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100713clip_isautosel_plus(void)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200714{
715 return (
716#ifdef FEAT_GUI
717 gui.in_use ? (vim_strchr(p_go, GO_ASELPLUS) != NULL) :
718#endif
719 clip_autoselect_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720}
721
722
723/*
724 * Stuff for general mouse selection, without using Visual mode.
725 */
726
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100727static void clip_invert_area(int, int, int, int, int how);
728static void clip_invert_rectangle(int row, int col, int height, int width, int invert);
729static void clip_get_word_boundaries(VimClipboard *, int, int);
730static int clip_get_line_end(int);
731static void clip_update_modeless_selection(VimClipboard *, int, int,
732 int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733
734/* flags for clip_invert_area() */
735#define CLIP_CLEAR 1
736#define CLIP_SET 2
737#define CLIP_TOGGLE 3
738
739/*
740 * Start, continue or end a modeless selection. Used when editing the
741 * command-line and in the cmdline window.
742 */
743 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100744clip_modeless(int button, int is_click, int is_drag)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745{
746 int repeat;
747
748 repeat = ((clip_star.mode == SELECT_MODE_CHAR
749 || clip_star.mode == SELECT_MODE_LINE)
750 && (mod_mask & MOD_MASK_2CLICK))
751 || (clip_star.mode == SELECT_MODE_WORD
752 && (mod_mask & MOD_MASK_3CLICK));
753 if (is_click && button == MOUSE_RIGHT)
754 {
755 /* Right mouse button: If there was no selection, start one.
756 * Otherwise extend the existing selection. */
757 if (clip_star.state == SELECT_CLEARED)
758 clip_start_selection(mouse_col, mouse_row, FALSE);
759 clip_process_selection(button, mouse_col, mouse_row, repeat);
760 }
761 else if (is_click)
762 clip_start_selection(mouse_col, mouse_row, repeat);
763 else if (is_drag)
764 {
765 /* Don't try extending a selection if there isn't one. Happens when
766 * button-down is in the cmdline and them moving mouse upwards. */
767 if (clip_star.state != SELECT_CLEARED)
768 clip_process_selection(button, mouse_col, mouse_row, repeat);
769 }
770 else /* release */
771 clip_process_selection(MOUSE_RELEASE, mouse_col, mouse_row, FALSE);
772}
773
774/*
775 * Compare two screen positions ala strcmp()
776 */
777 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100778clip_compare_pos(
779 int row1,
780 int col1,
781 int row2,
782 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783{
784 if (row1 > row2) return(1);
785 if (row1 < row2) return(-1);
786 if (col1 > col2) return(1);
787 if (col1 < col2) return(-1);
Bram Moolenaar9e341102016-02-23 23:04:36 +0100788 return(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789}
790
791/*
792 * Start the selection
793 */
794 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100795clip_start_selection(int col, int row, int repeated_click)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796{
797 VimClipboard *cb = &clip_star;
798
799 if (cb->state == SELECT_DONE)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200800 clip_clear_selection(cb);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801
802 row = check_row(row);
803 col = check_col(col);
804#ifdef FEAT_MBYTE
805 col = mb_fix_col(col, row);
806#endif
807
808 cb->start.lnum = row;
809 cb->start.col = col;
810 cb->end = cb->start;
811 cb->origin_row = (short_u)cb->start.lnum;
812 cb->state = SELECT_IN_PROGRESS;
813
814 if (repeated_click)
815 {
816 if (++cb->mode > SELECT_MODE_LINE)
817 cb->mode = SELECT_MODE_CHAR;
818 }
819 else
820 cb->mode = SELECT_MODE_CHAR;
821
822#ifdef FEAT_GUI
823 /* clear the cursor until the selection is made */
824 if (gui.in_use)
825 gui_undraw_cursor();
826#endif
827
828 switch (cb->mode)
829 {
830 case SELECT_MODE_CHAR:
831 cb->origin_start_col = cb->start.col;
832 cb->word_end_col = clip_get_line_end((int)cb->start.lnum);
833 break;
834
835 case SELECT_MODE_WORD:
836 clip_get_word_boundaries(cb, (int)cb->start.lnum, cb->start.col);
837 cb->origin_start_col = cb->word_start_col;
838 cb->origin_end_col = cb->word_end_col;
839
840 clip_invert_area((int)cb->start.lnum, cb->word_start_col,
841 (int)cb->end.lnum, cb->word_end_col, CLIP_SET);
842 cb->start.col = cb->word_start_col;
843 cb->end.col = cb->word_end_col;
844 break;
845
846 case SELECT_MODE_LINE:
847 clip_invert_area((int)cb->start.lnum, 0, (int)cb->start.lnum,
848 (int)Columns, CLIP_SET);
849 cb->start.col = 0;
850 cb->end.col = Columns;
851 break;
852 }
853
854 cb->prev = cb->start;
855
856#ifdef DEBUG_SELECTION
857 printf("Selection started at (%u,%u)\n", cb->start.lnum, cb->start.col);
858#endif
859}
860
861/*
862 * Continue processing the selection
863 */
864 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100865clip_process_selection(
866 int button,
867 int col,
868 int row,
869 int_u repeated_click)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870{
871 VimClipboard *cb = &clip_star;
872 int diff;
873 int slen = 1; /* cursor shape width */
874
875 if (button == MOUSE_RELEASE)
876 {
877 /* Check to make sure we have something selected */
878 if (cb->start.lnum == cb->end.lnum && cb->start.col == cb->end.col)
879 {
880#ifdef FEAT_GUI
881 if (gui.in_use)
882 gui_update_cursor(FALSE, FALSE);
883#endif
884 cb->state = SELECT_CLEARED;
885 return;
886 }
887
888#ifdef DEBUG_SELECTION
889 printf("Selection ended: (%u,%u) to (%u,%u)\n", cb->start.lnum,
890 cb->start.col, cb->end.lnum, cb->end.col);
891#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200892 if (clip_isautosel_star()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 || (
894#ifdef FEAT_GUI
895 gui.in_use ? (vim_strchr(p_go, GO_ASELML) != NULL) :
896#endif
897 clip_autoselectml))
898 clip_copy_modeless_selection(FALSE);
899#ifdef FEAT_GUI
900 if (gui.in_use)
901 gui_update_cursor(FALSE, FALSE);
902#endif
903
904 cb->state = SELECT_DONE;
905 return;
906 }
907
908 row = check_row(row);
909 col = check_col(col);
910#ifdef FEAT_MBYTE
911 col = mb_fix_col(col, row);
912#endif
913
914 if (col == (int)cb->prev.col && row == cb->prev.lnum && !repeated_click)
915 return;
916
917 /*
918 * When extending the selection with the right mouse button, swap the
919 * start and end if the position is before half the selection
920 */
921 if (cb->state == SELECT_DONE && button == MOUSE_RIGHT)
922 {
923 /*
924 * If the click is before the start, or the click is inside the
925 * selection and the start is the closest side, set the origin to the
926 * end of the selection.
927 */
928 if (clip_compare_pos(row, col, (int)cb->start.lnum, cb->start.col) < 0
929 || (clip_compare_pos(row, col,
930 (int)cb->end.lnum, cb->end.col) < 0
931 && (((cb->start.lnum == cb->end.lnum
932 && cb->end.col - col > col - cb->start.col))
933 || ((diff = (cb->end.lnum - row) -
934 (row - cb->start.lnum)) > 0
935 || (diff == 0 && col < (int)(cb->start.col +
936 cb->end.col) / 2)))))
937 {
938 cb->origin_row = (short_u)cb->end.lnum;
939 cb->origin_start_col = cb->end.col - 1;
940 cb->origin_end_col = cb->end.col;
941 }
942 else
943 {
944 cb->origin_row = (short_u)cb->start.lnum;
945 cb->origin_start_col = cb->start.col;
946 cb->origin_end_col = cb->start.col;
947 }
948 if (cb->mode == SELECT_MODE_WORD && !repeated_click)
949 cb->mode = SELECT_MODE_CHAR;
950 }
951
952 /* set state, for when using the right mouse button */
953 cb->state = SELECT_IN_PROGRESS;
954
955#ifdef DEBUG_SELECTION
956 printf("Selection extending to (%d,%d)\n", row, col);
957#endif
958
959 if (repeated_click && ++cb->mode > SELECT_MODE_LINE)
960 cb->mode = SELECT_MODE_CHAR;
961
962 switch (cb->mode)
963 {
964 case SELECT_MODE_CHAR:
965 /* If we're on a different line, find where the line ends */
966 if (row != cb->prev.lnum)
967 cb->word_end_col = clip_get_line_end(row);
968
969 /* See if we are before or after the origin of the selection */
970 if (clip_compare_pos(row, col, cb->origin_row,
971 cb->origin_start_col) >= 0)
972 {
973 if (col >= (int)cb->word_end_col)
974 clip_update_modeless_selection(cb, cb->origin_row,
975 cb->origin_start_col, row, (int)Columns);
976 else
977 {
978#ifdef FEAT_MBYTE
979 if (has_mbyte && mb_lefthalve(row, col))
980 slen = 2;
981#endif
982 clip_update_modeless_selection(cb, cb->origin_row,
983 cb->origin_start_col, row, col + slen);
984 }
985 }
986 else
987 {
988#ifdef FEAT_MBYTE
989 if (has_mbyte
990 && mb_lefthalve(cb->origin_row, cb->origin_start_col))
991 slen = 2;
992#endif
993 if (col >= (int)cb->word_end_col)
994 clip_update_modeless_selection(cb, row, cb->word_end_col,
995 cb->origin_row, cb->origin_start_col + slen);
996 else
997 clip_update_modeless_selection(cb, row, col,
998 cb->origin_row, cb->origin_start_col + slen);
999 }
1000 break;
1001
1002 case SELECT_MODE_WORD:
1003 /* If we are still within the same word, do nothing */
1004 if (row == cb->prev.lnum && col >= (int)cb->word_start_col
1005 && col < (int)cb->word_end_col && !repeated_click)
1006 return;
1007
1008 /* Get new word boundaries */
1009 clip_get_word_boundaries(cb, row, col);
1010
1011 /* Handle being after the origin point of selection */
1012 if (clip_compare_pos(row, col, cb->origin_row,
1013 cb->origin_start_col) >= 0)
1014 clip_update_modeless_selection(cb, cb->origin_row,
1015 cb->origin_start_col, row, cb->word_end_col);
1016 else
1017 clip_update_modeless_selection(cb, row, cb->word_start_col,
1018 cb->origin_row, cb->origin_end_col);
1019 break;
1020
1021 case SELECT_MODE_LINE:
1022 if (row == cb->prev.lnum && !repeated_click)
1023 return;
1024
1025 if (clip_compare_pos(row, col, cb->origin_row,
1026 cb->origin_start_col) >= 0)
1027 clip_update_modeless_selection(cb, cb->origin_row, 0, row,
1028 (int)Columns);
1029 else
1030 clip_update_modeless_selection(cb, row, 0, cb->origin_row,
1031 (int)Columns);
1032 break;
1033 }
1034
1035 cb->prev.lnum = row;
1036 cb->prev.col = col;
1037
1038#ifdef DEBUG_SELECTION
1039 printf("Selection is: (%u,%u) to (%u,%u)\n", cb->start.lnum,
1040 cb->start.col, cb->end.lnum, cb->end.col);
1041#endif
1042}
1043
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044# if defined(FEAT_GUI) || defined(PROTO)
1045/*
1046 * Redraw part of the selection if character at "row,col" is inside of it.
1047 * Only used for the GUI.
1048 */
1049 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001050clip_may_redraw_selection(int row, int col, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051{
1052 int start = col;
1053 int end = col + len;
1054
1055 if (clip_star.state != SELECT_CLEARED
1056 && row >= clip_star.start.lnum
1057 && row <= clip_star.end.lnum)
1058 {
1059 if (row == clip_star.start.lnum && start < (int)clip_star.start.col)
1060 start = clip_star.start.col;
1061 if (row == clip_star.end.lnum && end > (int)clip_star.end.col)
1062 end = clip_star.end.col;
1063 if (end > start)
1064 clip_invert_area(row, start, row, end, 0);
1065 }
1066}
1067# endif
1068
1069/*
1070 * Called from outside to clear selected region from the display
1071 */
1072 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001073clip_clear_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001076 if (cbd->state == SELECT_CLEARED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 return;
1078
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001079 clip_invert_area((int)cbd->start.lnum, cbd->start.col, (int)cbd->end.lnum,
1080 cbd->end.col, CLIP_CLEAR);
1081 cbd->state = SELECT_CLEARED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082}
1083
1084/*
1085 * Clear the selection if any lines from "row1" to "row2" are inside of it.
1086 */
1087 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001088clip_may_clear_selection(int row1, int row2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089{
1090 if (clip_star.state == SELECT_DONE
1091 && row2 >= clip_star.start.lnum
1092 && row1 <= clip_star.end.lnum)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001093 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094}
1095
1096/*
1097 * Called before the screen is scrolled up or down. Adjusts the line numbers
1098 * of the selection. Call with big number when clearing the screen.
1099 */
1100 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001101clip_scroll_selection(
1102 int rows) /* negative for scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103{
1104 int lnum;
1105
1106 if (clip_star.state == SELECT_CLEARED)
1107 return;
1108
1109 lnum = clip_star.start.lnum - rows;
1110 if (lnum <= 0)
1111 clip_star.start.lnum = 0;
1112 else if (lnum >= screen_Rows) /* scrolled off of the screen */
1113 clip_star.state = SELECT_CLEARED;
1114 else
1115 clip_star.start.lnum = lnum;
1116
1117 lnum = clip_star.end.lnum - rows;
1118 if (lnum < 0) /* scrolled off of the screen */
1119 clip_star.state = SELECT_CLEARED;
1120 else if (lnum >= screen_Rows)
1121 clip_star.end.lnum = screen_Rows - 1;
1122 else
1123 clip_star.end.lnum = lnum;
1124}
1125
1126/*
1127 * Invert a region of the display between a starting and ending row and column
1128 * Values for "how":
1129 * CLIP_CLEAR: undo inversion
1130 * CLIP_SET: set inversion
1131 * CLIP_TOGGLE: set inversion if pos1 < pos2, undo inversion otherwise.
1132 * 0: invert (GUI only).
1133 */
1134 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001135clip_invert_area(
1136 int row1,
1137 int col1,
1138 int row2,
1139 int col2,
1140 int how)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141{
1142 int invert = FALSE;
1143
1144 if (how == CLIP_SET)
1145 invert = TRUE;
1146
1147 /* Swap the from and to positions so the from is always before */
1148 if (clip_compare_pos(row1, col1, row2, col2) > 0)
1149 {
1150 int tmp_row, tmp_col;
1151
1152 tmp_row = row1;
1153 tmp_col = col1;
1154 row1 = row2;
1155 col1 = col2;
1156 row2 = tmp_row;
1157 col2 = tmp_col;
1158 }
1159 else if (how == CLIP_TOGGLE)
1160 invert = TRUE;
1161
1162 /* If all on the same line, do it the easy way */
1163 if (row1 == row2)
1164 {
1165 clip_invert_rectangle(row1, col1, 1, col2 - col1, invert);
1166 }
1167 else
1168 {
1169 /* Handle a piece of the first line */
1170 if (col1 > 0)
1171 {
1172 clip_invert_rectangle(row1, col1, 1, (int)Columns - col1, invert);
1173 row1++;
1174 }
1175
1176 /* Handle a piece of the last line */
1177 if (col2 < Columns - 1)
1178 {
1179 clip_invert_rectangle(row2, 0, 1, col2, invert);
1180 row2--;
1181 }
1182
1183 /* Handle the rectangle thats left */
1184 if (row2 >= row1)
1185 clip_invert_rectangle(row1, 0, row2 - row1 + 1, (int)Columns,
1186 invert);
1187 }
1188}
1189
1190/*
1191 * Invert or un-invert a rectangle of the screen.
1192 * "invert" is true if the result is inverted.
1193 */
1194 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001195clip_invert_rectangle(
1196 int row,
1197 int col,
1198 int height,
1199 int width,
1200 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201{
1202#ifdef FEAT_GUI
1203 if (gui.in_use)
1204 gui_mch_invert_rectangle(row, col, height, width);
1205 else
1206#endif
1207 screen_draw_rectangle(row, col, height, width, invert);
1208}
1209
1210/*
1211 * Copy the currently selected area into the '*' register so it will be
1212 * available for pasting.
1213 * When "both" is TRUE also copy to the '+' register.
1214 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001216clip_copy_modeless_selection(int both UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217{
1218 char_u *buffer;
1219 char_u *bufp;
1220 int row;
1221 int start_col;
1222 int end_col;
1223 int line_end_col;
1224 int add_newline_flag = FALSE;
1225 int len;
1226#ifdef FEAT_MBYTE
1227 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228#endif
1229 int row1 = clip_star.start.lnum;
1230 int col1 = clip_star.start.col;
1231 int row2 = clip_star.end.lnum;
1232 int col2 = clip_star.end.col;
1233
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001234 /* Can't use ScreenLines unless initialized */
1235 if (ScreenLines == NULL)
1236 return;
1237
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 /*
1239 * Make sure row1 <= row2, and if row1 == row2 that col1 <= col2.
1240 */
1241 if (row1 > row2)
1242 {
1243 row = row1; row1 = row2; row2 = row;
1244 row = col1; col1 = col2; col2 = row;
1245 }
1246 else if (row1 == row2 && col1 > col2)
1247 {
1248 row = col1; col1 = col2; col2 = row;
1249 }
1250#ifdef FEAT_MBYTE
1251 /* correct starting point for being on right halve of double-wide char */
1252 p = ScreenLines + LineOffset[row1];
1253 if (enc_dbcs != 0)
1254 col1 -= (*mb_head_off)(p, p + col1);
1255 else if (enc_utf8 && p[col1] == 0)
1256 --col1;
1257#endif
1258
1259 /* Create a temporary buffer for storing the text */
1260 len = (row2 - row1 + 1) * Columns + 1;
1261#ifdef FEAT_MBYTE
1262 if (enc_dbcs != 0)
1263 len *= 2; /* max. 2 bytes per display cell */
1264 else if (enc_utf8)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001265 len *= MB_MAXBYTES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266#endif
1267 buffer = lalloc((long_u)len, TRUE);
1268 if (buffer == NULL) /* out of memory */
1269 return;
1270
1271 /* Process each row in the selection */
1272 for (bufp = buffer, row = row1; row <= row2; row++)
1273 {
1274 if (row == row1)
1275 start_col = col1;
1276 else
1277 start_col = 0;
1278
1279 if (row == row2)
1280 end_col = col2;
1281 else
1282 end_col = Columns;
1283
1284 line_end_col = clip_get_line_end(row);
1285
1286 /* See if we need to nuke some trailing whitespace */
1287 if (end_col >= Columns && (row < row2 || end_col > line_end_col))
1288 {
1289 /* Get rid of trailing whitespace */
1290 end_col = line_end_col;
1291 if (end_col < start_col)
1292 end_col = start_col;
1293
1294 /* If the last line extended to the end, add an extra newline */
1295 if (row == row2)
1296 add_newline_flag = TRUE;
1297 }
1298
1299 /* If after the first row, we need to always add a newline */
1300 if (row > row1 && !LineWraps[row - 1])
1301 *bufp++ = NL;
1302
1303 if (row < screen_Rows && end_col <= screen_Columns)
1304 {
1305#ifdef FEAT_MBYTE
1306 if (enc_dbcs != 0)
1307 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001308 int i;
1309
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 p = ScreenLines + LineOffset[row];
1311 for (i = start_col; i < end_col; ++i)
1312 if (enc_dbcs == DBCS_JPNU && p[i] == 0x8e)
1313 {
1314 /* single-width double-byte char */
1315 *bufp++ = 0x8e;
1316 *bufp++ = ScreenLines2[LineOffset[row] + i];
1317 }
1318 else
1319 {
1320 *bufp++ = p[i];
1321 if (MB_BYTE2LEN(p[i]) == 2)
1322 *bufp++ = p[++i];
1323 }
1324 }
1325 else if (enc_utf8)
1326 {
1327 int off;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001328 int i;
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001329 int ci;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330
1331 off = LineOffset[row];
1332 for (i = start_col; i < end_col; ++i)
1333 {
1334 /* The base character is either in ScreenLinesUC[] or
1335 * ScreenLines[]. */
1336 if (ScreenLinesUC[off + i] == 0)
1337 *bufp++ = ScreenLines[off + i];
1338 else
1339 {
1340 bufp += utf_char2bytes(ScreenLinesUC[off + i], bufp);
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001341 for (ci = 0; ci < Screen_mco; ++ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001343 /* Add a composing character. */
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001344 if (ScreenLinesC[ci][off + i] == 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001345 break;
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001346 bufp += utf_char2bytes(ScreenLinesC[ci][off + i],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 }
1349 }
1350 /* Skip right halve of double-wide character. */
1351 if (ScreenLines[off + i + 1] == 0)
1352 ++i;
1353 }
1354 }
1355 else
1356#endif
1357 {
1358 STRNCPY(bufp, ScreenLines + LineOffset[row] + start_col,
1359 end_col - start_col);
1360 bufp += end_col - start_col;
1361 }
1362 }
1363 }
1364
1365 /* Add a newline at the end if the selection ended there */
1366 if (add_newline_flag)
1367 *bufp++ = NL;
1368
1369 /* First cleanup any old selection and become the owner. */
1370 clip_free_selection(&clip_star);
1371 clip_own_selection(&clip_star);
1372
1373 /* Yank the text into the '*' register. */
1374 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_star);
1375
1376 /* Make the register contents available to the outside world. */
1377 clip_gen_set_selection(&clip_star);
1378
1379#ifdef FEAT_X11
1380 if (both)
1381 {
1382 /* Do the same for the '+' register. */
1383 clip_free_selection(&clip_plus);
1384 clip_own_selection(&clip_plus);
1385 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_plus);
1386 clip_gen_set_selection(&clip_plus);
1387 }
1388#endif
1389 vim_free(buffer);
1390}
1391
1392/*
1393 * Find the starting and ending positions of the word at the given row and
1394 * column. Only white-separated words are recognized here.
1395 */
1396#define CHAR_CLASS(c) (c <= ' ' ? ' ' : vim_iswordc(c))
1397
1398 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001399clip_get_word_boundaries(VimClipboard *cb, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400{
1401 int start_class;
1402 int temp_col;
1403 char_u *p;
1404#ifdef FEAT_MBYTE
1405 int mboff;
1406#endif
1407
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001408 if (row >= screen_Rows || col >= screen_Columns || ScreenLines == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 return;
1410
1411 p = ScreenLines + LineOffset[row];
1412#ifdef FEAT_MBYTE
1413 /* Correct for starting in the right halve of a double-wide char */
1414 if (enc_dbcs != 0)
1415 col -= dbcs_screen_head_off(p, p + col);
1416 else if (enc_utf8 && p[col] == 0)
1417 --col;
1418#endif
1419 start_class = CHAR_CLASS(p[col]);
1420
1421 temp_col = col;
1422 for ( ; temp_col > 0; temp_col--)
1423#ifdef FEAT_MBYTE
1424 if (enc_dbcs != 0
1425 && (mboff = dbcs_screen_head_off(p, p + temp_col - 1)) > 0)
1426 temp_col -= mboff;
1427 else
1428#endif
1429 if (CHAR_CLASS(p[temp_col - 1]) != start_class
1430#ifdef FEAT_MBYTE
1431 && !(enc_utf8 && p[temp_col - 1] == 0)
1432#endif
1433 )
1434 break;
1435 cb->word_start_col = temp_col;
1436
1437 temp_col = col;
1438 for ( ; temp_col < screen_Columns; temp_col++)
1439#ifdef FEAT_MBYTE
1440 if (enc_dbcs != 0 && dbcs_ptr2cells(p + temp_col) == 2)
1441 ++temp_col;
1442 else
1443#endif
1444 if (CHAR_CLASS(p[temp_col]) != start_class
1445#ifdef FEAT_MBYTE
1446 && !(enc_utf8 && p[temp_col] == 0)
1447#endif
1448 )
1449 break;
1450 cb->word_end_col = temp_col;
1451}
1452
1453/*
1454 * Find the column position for the last non-whitespace character on the given
1455 * line.
1456 */
1457 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001458clip_get_line_end(int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459{
1460 int i;
1461
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001462 if (row >= screen_Rows || ScreenLines == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 return 0;
1464 for (i = screen_Columns; i > 0; i--)
1465 if (ScreenLines[LineOffset[row] + i - 1] != ' ')
1466 break;
1467 return i;
1468}
1469
1470/*
1471 * Update the currently selected region by adding and/or subtracting from the
1472 * beginning or end and inverting the changed area(s).
1473 */
1474 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001475clip_update_modeless_selection(
1476 VimClipboard *cb,
1477 int row1,
1478 int col1,
1479 int row2,
1480 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481{
1482 /* See if we changed at the beginning of the selection */
1483 if (row1 != cb->start.lnum || col1 != (int)cb->start.col)
1484 {
1485 clip_invert_area(row1, col1, (int)cb->start.lnum, cb->start.col,
1486 CLIP_TOGGLE);
1487 cb->start.lnum = row1;
1488 cb->start.col = col1;
1489 }
1490
1491 /* See if we changed at the end of the selection */
1492 if (row2 != cb->end.lnum || col2 != (int)cb->end.col)
1493 {
1494 clip_invert_area((int)cb->end.lnum, cb->end.col, row2, col2,
1495 CLIP_TOGGLE);
1496 cb->end.lnum = row2;
1497 cb->end.col = col2;
1498 }
1499}
1500
1501 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001502clip_gen_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503{
1504#ifdef FEAT_XCLIPBOARD
1505# ifdef FEAT_GUI
1506 if (gui.in_use)
1507 return clip_mch_own_selection(cbd);
1508 else
1509# endif
1510 return clip_xterm_own_selection(cbd);
1511#else
1512 return clip_mch_own_selection(cbd);
1513#endif
1514}
1515
1516 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001517clip_gen_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518{
1519#ifdef FEAT_XCLIPBOARD
1520# ifdef FEAT_GUI
1521 if (gui.in_use)
1522 clip_mch_lose_selection(cbd);
1523 else
1524# endif
1525 clip_xterm_lose_selection(cbd);
1526#else
1527 clip_mch_lose_selection(cbd);
1528#endif
1529}
1530
1531 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001532clip_gen_set_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533{
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001534 if (!clip_did_set_selection)
1535 {
1536 /* Updating postponed, so that accessing the system clipboard won't
1537 * hang Vim when accessing it many times (e.g. on a :g comand). */
Bram Moolenaar5c27fd12015-01-27 14:09:37 +01001538 if ((cbd == &clip_plus && (clip_unnamed_saved & CLIP_UNNAMED_PLUS))
1539 || (cbd == &clip_star && (clip_unnamed_saved & CLIP_UNNAMED)))
1540 {
1541 clipboard_needs_update = TRUE;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001542 return;
Bram Moolenaar5c27fd12015-01-27 14:09:37 +01001543 }
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545#ifdef FEAT_XCLIPBOARD
1546# ifdef FEAT_GUI
1547 if (gui.in_use)
1548 clip_mch_set_selection(cbd);
1549 else
1550# endif
1551 clip_xterm_set_selection(cbd);
1552#else
1553 clip_mch_set_selection(cbd);
1554#endif
1555}
1556
1557 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001558clip_gen_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559{
1560#ifdef FEAT_XCLIPBOARD
1561# ifdef FEAT_GUI
1562 if (gui.in_use)
1563 clip_mch_request_selection(cbd);
1564 else
1565# endif
1566 clip_xterm_request_selection(cbd);
1567#else
1568 clip_mch_request_selection(cbd);
1569#endif
1570}
1571
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001572 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001573clip_gen_owner_exists(VimClipboard *cbd UNUSED)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001574{
1575#ifdef FEAT_XCLIPBOARD
1576# ifdef FEAT_GUI_GTK
1577 if (gui.in_use)
1578 return clip_gtk_owner_exists(cbd);
1579 else
1580# endif
1581 return clip_x11_owner_exists(cbd);
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02001582#else
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001583 return TRUE;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02001584#endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001585}
1586
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587#endif /* FEAT_CLIPBOARD */
1588
1589/*****************************************************************************
1590 * Functions that handle the input buffer.
1591 * This is used for any GUI version, and the unix terminal version.
1592 *
1593 * For Unix, the input characters are buffered to be able to check for a
1594 * CTRL-C. This should be done with signals, but I don't know how to do that
1595 * in a portable way for a tty in RAW mode.
1596 *
1597 * For the client-server code in the console the received keys are put in the
1598 * input buffer.
1599 */
1600
1601#if defined(USE_INPUT_BUF) || defined(PROTO)
1602
1603/*
1604 * Internal typeahead buffer. Includes extra space for long key code
1605 * descriptions which would otherwise overflow. The buffer is considered full
1606 * when only this extra space (or part of it) remains.
1607 */
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001608#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_JOB_CHANNEL) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 || defined(FEAT_CLIENTSERVER)
1610 /*
1611 * Sun WorkShop and NetBeans stuff debugger commands into the input buffer.
1612 * This requires a larger buffer...
1613 * (Madsen) Go with this for remote input as well ...
1614 */
1615# define INBUFLEN 4096
1616#else
1617# define INBUFLEN 250
1618#endif
1619
1620static char_u inbuf[INBUFLEN + MAX_KEY_CODE_LEN];
1621static int inbufcount = 0; /* number of chars in inbuf[] */
1622
1623/*
1624 * vim_is_input_buf_full(), vim_is_input_buf_empty(), add_to_input_buf(), and
1625 * trash_input_buf() are functions for manipulating the input buffer. These
1626 * are used by the gui_* calls when a GUI is used to handle keyboard input.
1627 */
1628
1629 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001630vim_is_input_buf_full(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631{
1632 return (inbufcount >= INBUFLEN);
1633}
1634
1635 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001636vim_is_input_buf_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637{
1638 return (inbufcount == 0);
1639}
1640
1641#if defined(FEAT_OLE) || defined(PROTO)
1642 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001643vim_free_in_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644{
1645 return (INBUFLEN - inbufcount);
1646}
1647#endif
1648
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001649#if defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001651vim_used_in_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652{
1653 return inbufcount;
1654}
1655#endif
1656
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657/*
1658 * Return the current contents of the input buffer and make it empty.
1659 * The returned pointer must be passed to set_input_buf() later.
1660 */
1661 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001662get_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663{
1664 garray_T *gap;
1665
1666 /* We use a growarray to store the data pointer and the length. */
1667 gap = (garray_T *)alloc((unsigned)sizeof(garray_T));
1668 if (gap != NULL)
1669 {
1670 /* Add one to avoid a zero size. */
1671 gap->ga_data = alloc((unsigned)inbufcount + 1);
1672 if (gap->ga_data != NULL)
1673 mch_memmove(gap->ga_data, inbuf, (size_t)inbufcount);
1674 gap->ga_len = inbufcount;
1675 }
1676 trash_input_buf();
1677 return (char_u *)gap;
1678}
1679
1680/*
1681 * Restore the input buffer with a pointer returned from get_input_buf().
1682 * The allocated memory is freed, this only works once!
1683 */
1684 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001685set_input_buf(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686{
1687 garray_T *gap = (garray_T *)p;
1688
1689 if (gap != NULL)
1690 {
1691 if (gap->ga_data != NULL)
1692 {
1693 mch_memmove(inbuf, gap->ga_data, gap->ga_len);
1694 inbufcount = gap->ga_len;
1695 vim_free(gap->ga_data);
1696 }
1697 vim_free(gap);
1698 }
1699}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701/*
1702 * Add the given bytes to the input buffer
1703 * Special keys start with CSI. A real CSI must have been translated to
1704 * CSI KS_EXTRA KE_CSI. K_SPECIAL doesn't require translation.
1705 */
1706 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001707add_to_input_buf(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708{
1709 if (inbufcount + len > INBUFLEN + MAX_KEY_CODE_LEN)
1710 return; /* Shouldn't ever happen! */
1711
1712#ifdef FEAT_HANGULIN
1713 if ((State & (INSERT|CMDLINE)) && hangul_input_state_get())
1714 if ((len = hangul_input_process(s, len)) == 0)
1715 return;
1716#endif
1717
1718 while (len--)
1719 inbuf[inbufcount++] = *s++;
1720}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722/*
1723 * Add "str[len]" to the input buffer while escaping CSI bytes.
1724 */
1725 void
1726add_to_input_buf_csi(char_u *str, int len)
1727{
1728 int i;
1729 char_u buf[2];
1730
1731 for (i = 0; i < len; ++i)
1732 {
1733 add_to_input_buf(str + i, 1);
1734 if (str[i] == CSI)
1735 {
1736 /* Turn CSI into K_CSI. */
1737 buf[0] = KS_EXTRA;
1738 buf[1] = (int)KE_CSI;
1739 add_to_input_buf(buf, 2);
1740 }
1741 }
1742}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743
1744#if defined(FEAT_HANGULIN) || defined(PROTO)
1745 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001746push_raw_key(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747{
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001748 char_u *tmpbuf;
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001749 char_u *inp = s;
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001750
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001751 /* use the conversion result if possible */
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001752 tmpbuf = hangul_string_convert(s, &len);
1753 if (tmpbuf != NULL)
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001754 inp = tmpbuf;
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001755
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001756 for (; len--; inp++)
1757 {
1758 inbuf[inbufcount++] = *inp;
1759 if (*inp == CSI)
Bram Moolenaar00ded432016-03-03 11:45:15 +01001760 {
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001761 /* Turn CSI into K_CSI. */
1762 inbuf[inbufcount++] = KS_EXTRA;
1763 inbuf[inbufcount++] = (int)KE_CSI;
Bram Moolenaar00ded432016-03-03 11:45:15 +01001764 }
Bram Moolenaar00ded432016-03-03 11:45:15 +01001765 }
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001766 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767}
1768#endif
1769
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770/* Remove everything from the input buffer. Called when ^C is found */
1771 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001772trash_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773{
1774 inbufcount = 0;
1775}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
1777/*
1778 * Read as much data from the input buffer as possible up to maxlen, and store
1779 * it in buf.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 */
1781 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001782read_from_input_buf(char_u *buf, long maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783{
1784 if (inbufcount == 0) /* if the buffer is empty, fill it */
1785 fill_input_buf(TRUE);
1786 if (maxlen > inbufcount)
1787 maxlen = inbufcount;
1788 mch_memmove(buf, inbuf, (size_t)maxlen);
1789 inbufcount -= maxlen;
1790 if (inbufcount)
1791 mch_memmove(inbuf, inbuf + maxlen, (size_t)inbufcount);
1792 return (int)maxlen;
1793}
1794
1795 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001796fill_input_buf(int exit_on_error UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797{
Bram Moolenaard0573012017-10-28 21:11:06 +02001798#if defined(UNIX) || defined(VMS) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 int len;
1800 int try;
1801 static int did_read_something = FALSE;
1802# ifdef FEAT_MBYTE
1803 static char_u *rest = NULL; /* unconverted rest of previous read */
1804 static int restlen = 0;
1805 int unconverted;
1806# endif
1807#endif
1808
1809#ifdef FEAT_GUI
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001810 if (gui.in_use
1811# ifdef NO_CONSOLE_INPUT
1812 /* Don't use the GUI input when the window hasn't been opened yet.
1813 * We get here from ui_inchar() when we should try reading from stdin. */
1814 && !no_console_input()
1815# endif
1816 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 {
1818 gui_mch_update();
1819 return;
1820 }
1821#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02001822#if defined(UNIX) || defined(VMS) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 if (vim_is_input_buf_full())
1824 return;
1825 /*
1826 * Fill_input_buf() is only called when we really need a character.
1827 * If we can't get any, but there is some in the buffer, just return.
1828 * If we can't get any, and there isn't any in the buffer, we give up and
1829 * exit Vim.
1830 */
1831# ifdef __BEOS__
1832 /*
1833 * On the BeBox version (for now), all input is secretly performed within
1834 * beos_select() which is called from RealWaitForChar().
1835 */
1836 while (!vim_is_input_buf_full() && RealWaitForChar(read_cmd_fd, 0, NULL))
1837 ;
1838 len = inbufcount;
1839 inbufcount = 0;
1840# else
1841
Bram Moolenaar85b11762016-02-27 18:13:23 +01001842# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 if (rest != NULL)
1844 {
1845 /* Use remainder of previous call, starts with an invalid character
1846 * that may become valid when reading more. */
1847 if (restlen > INBUFLEN - inbufcount)
1848 unconverted = INBUFLEN - inbufcount;
1849 else
1850 unconverted = restlen;
1851 mch_memmove(inbuf + inbufcount, rest, unconverted);
1852 if (unconverted == restlen)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001853 VIM_CLEAR(rest);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 else
1855 {
1856 restlen -= unconverted;
1857 mch_memmove(rest, rest + unconverted, restlen);
1858 }
1859 inbufcount += unconverted;
1860 }
1861 else
1862 unconverted = 0;
Bram Moolenaar85b11762016-02-27 18:13:23 +01001863# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864
1865 len = 0; /* to avoid gcc warning */
1866 for (try = 0; try < 100; ++try)
1867 {
Bram Moolenaar4e601e32018-04-24 13:29:51 +02001868 size_t readlen = (size_t)((INBUFLEN - inbufcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869# ifdef FEAT_MBYTE
Bram Moolenaar4e601e32018-04-24 13:29:51 +02001870 / input_conv.vc_factor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871# endif
Bram Moolenaar4e601e32018-04-24 13:29:51 +02001872 );
1873# ifdef VMS
Bram Moolenaar49943732018-04-24 20:27:26 +02001874 len = vms_read((char *)inbuf + inbufcount, readlen);
Bram Moolenaar4e601e32018-04-24 13:29:51 +02001875# else
1876 len = read(read_cmd_fd, (char *)inbuf + inbufcount, readlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877# endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001878
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 if (len > 0 || got_int)
1880 break;
1881 /*
1882 * If reading stdin results in an error, continue reading stderr.
1883 * This helps when using "foo | xargs vim".
1884 */
1885 if (!did_read_something && !isatty(read_cmd_fd) && read_cmd_fd == 0)
1886 {
1887 int m = cur_tmode;
1888
1889 /* We probably set the wrong file descriptor to raw mode. Switch
1890 * back to cooked mode, use another descriptor and set the mode to
1891 * what it was. */
1892 settmode(TMODE_COOK);
1893#ifdef HAVE_DUP
1894 /* Use stderr for stdin, also works for shell commands. */
1895 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02001896 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897#else
1898 read_cmd_fd = 2; /* read from stderr instead of stdin */
1899#endif
1900 settmode(m);
1901 }
1902 if (!exit_on_error)
1903 return;
1904 }
1905# endif
1906 if (len <= 0 && !got_int)
1907 read_error_exit();
1908 if (len > 0)
1909 did_read_something = TRUE;
1910 if (got_int)
1911 {
1912 /* Interrupted, pretend a CTRL-C was typed. */
1913 inbuf[0] = 3;
1914 inbufcount = 1;
1915 }
1916 else
1917 {
1918# ifdef FEAT_MBYTE
1919 /*
1920 * May perform conversion on the input characters.
1921 * Include the unconverted rest of the previous call.
1922 * If there is an incomplete char at the end it is kept for the next
1923 * time, reading more bytes should make conversion possible.
1924 * Don't do this in the unlikely event that the input buffer is too
1925 * small ("rest" still contains more bytes).
1926 */
1927 if (input_conv.vc_type != CONV_NONE)
1928 {
1929 inbufcount -= unconverted;
1930 len = convert_input_safe(inbuf + inbufcount,
1931 len + unconverted, INBUFLEN - inbufcount,
1932 rest == NULL ? &rest : NULL, &restlen);
1933 }
1934# endif
1935 while (len-- > 0)
1936 {
1937 /*
1938 * if a CTRL-C was typed, remove it from the buffer and set got_int
1939 */
1940 if (inbuf[inbufcount] == 3 && ctrl_c_interrupts)
1941 {
1942 /* remove everything typed before the CTRL-C */
1943 mch_memmove(inbuf, inbuf + inbufcount, (size_t)(len + 1));
1944 inbufcount = 0;
1945 got_int = TRUE;
1946 }
1947 ++inbufcount;
1948 }
1949 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001950#endif /* UNIX or VMS*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951}
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001952#endif /* defined(UNIX) || defined(FEAT_GUI) || defined(VMS) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953
1954/*
1955 * Exit because of an input read error.
1956 */
1957 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001958read_error_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959{
1960 if (silent_mode) /* Normal way to exit for "ex -s" */
1961 getout(0);
1962 STRCPY(IObuff, _("Vim: Error reading input, exiting...\n"));
1963 preserve_exit();
1964}
1965
1966#if defined(CURSOR_SHAPE) || defined(PROTO)
1967/*
1968 * May update the shape of the cursor.
1969 */
1970 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001971ui_cursor_shape_forced(int forced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972{
1973# ifdef FEAT_GUI
1974 if (gui.in_use)
1975 gui_update_cursor_later();
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001976 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001978 term_cursor_mode(forced);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001979
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980# ifdef MCH_CURSOR_SHAPE
1981 mch_update_cursor();
1982# endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001983
1984# ifdef FEAT_CONCEAL
Bram Moolenaarb9464822018-05-10 15:09:49 +02001985 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001986# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001988
1989 void
1990ui_cursor_shape(void)
1991{
1992 ui_cursor_shape_forced(FALSE);
1993}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994#endif
1995
1996#if defined(FEAT_CLIPBOARD) || defined(FEAT_GUI) || defined(FEAT_RIGHTLEFT) \
Bram Moolenaaraf51e662008-07-14 19:48:05 +00001997 || defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998/*
1999 * Check bounds for column number
2000 */
2001 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002002check_col(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003{
2004 if (col < 0)
2005 return 0;
2006 if (col >= (int)screen_Columns)
2007 return (int)screen_Columns - 1;
2008 return col;
2009}
2010
2011/*
2012 * Check bounds for row number
2013 */
2014 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002015check_row(int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016{
2017 if (row < 0)
2018 return 0;
2019 if (row >= (int)screen_Rows)
2020 return (int)screen_Rows - 1;
2021 return row;
2022}
2023#endif
2024
2025/*
2026 * Stuff for the X clipboard. Shared between VMS and Unix.
2027 */
2028
2029#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) || defined(PROTO)
2030# include <X11/Xatom.h>
2031# include <X11/Intrinsic.h>
2032
2033/*
2034 * Open the application context (if it hasn't been opened yet).
2035 * Used for Motif and Athena GUI and the xterm clipboard.
2036 */
2037 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002038open_app_context(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039{
2040 if (app_context == NULL)
2041 {
2042 XtToolkitInitialize();
2043 app_context = XtCreateApplicationContext();
2044 }
2045}
2046
2047static Atom vim_atom; /* Vim's own special selection format */
2048#ifdef FEAT_MBYTE
2049static Atom vimenc_atom; /* Vim's extended selection format */
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002050static Atom utf8_atom;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051#endif
2052static Atom compound_text_atom;
2053static Atom text_atom;
2054static Atom targets_atom;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002055static Atom timestamp_atom; /* Used to get a timestamp */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056
2057 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002058x11_setup_atoms(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059{
2060 vim_atom = XInternAtom(dpy, VIM_ATOM_NAME, False);
2061#ifdef FEAT_MBYTE
2062 vimenc_atom = XInternAtom(dpy, VIMENC_ATOM_NAME,False);
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002063 utf8_atom = XInternAtom(dpy, "UTF8_STRING", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064#endif
2065 compound_text_atom = XInternAtom(dpy, "COMPOUND_TEXT", False);
2066 text_atom = XInternAtom(dpy, "TEXT", False);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002067 targets_atom = XInternAtom(dpy, "TARGETS", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 clip_star.sel_atom = XA_PRIMARY;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002069 clip_plus.sel_atom = XInternAtom(dpy, "CLIPBOARD", False);
2070 timestamp_atom = XInternAtom(dpy, "TIMESTAMP", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071}
2072
2073/*
2074 * X Selection stuff, for cutting and pasting text to other windows.
2075 */
2076
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002077static Boolean clip_x11_convert_selection_cb(Widget w, Atom *sel_atom, Atom *target, Atom *type, XtPointer *value, long_u *length, int *format);
2078static void clip_x11_lose_ownership_cb(Widget w, Atom *sel_atom);
2079static void clip_x11_notify_cb(Widget w, Atom *sel_atom, Atom *target);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002080
2081/*
2082 * Property callback to get a timestamp for XtOwnSelection.
2083 */
2084 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002085clip_x11_timestamp_cb(
2086 Widget w,
2087 XtPointer n UNUSED,
2088 XEvent *event,
2089 Boolean *cont UNUSED)
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002090{
2091 Atom actual_type;
2092 int format;
2093 unsigned long nitems, bytes_after;
2094 unsigned char *prop=NULL;
2095 XPropertyEvent *xproperty=&event->xproperty;
2096
2097 /* Must be a property notify, state can't be Delete (True), has to be
2098 * one of the supported selection types. */
2099 if (event->type != PropertyNotify || xproperty->state
2100 || (xproperty->atom != clip_star.sel_atom
2101 && xproperty->atom != clip_plus.sel_atom))
2102 return;
2103
2104 if (XGetWindowProperty(xproperty->display, xproperty->window,
2105 xproperty->atom, 0, 0, False, timestamp_atom, &actual_type, &format,
2106 &nitems, &bytes_after, &prop))
2107 return;
2108
2109 if (prop)
2110 XFree(prop);
2111
2112 /* Make sure the property type is "TIMESTAMP" and it's 32 bits. */
2113 if (actual_type != timestamp_atom || format != 32)
2114 return;
2115
2116 /* Get the selection, using the event timestamp. */
Bram Moolenaar62b42182010-09-21 22:09:37 +02002117 if (XtOwnSelection(w, xproperty->atom, xproperty->time,
2118 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002119 clip_x11_notify_cb) == OK)
Bram Moolenaar62b42182010-09-21 22:09:37 +02002120 {
2121 /* Set the "owned" flag now, there may have been a call to
2122 * lose_ownership_cb in between. */
2123 if (xproperty->atom == clip_plus.sel_atom)
2124 clip_plus.owned = TRUE;
2125 else
2126 clip_star.owned = TRUE;
2127 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002128}
2129
2130 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002131x11_setup_selection(Widget w)
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002132{
2133 XtAddEventHandler(w, PropertyChangeMask, False,
2134 /*(XtEventHandler)*/clip_x11_timestamp_cb, (XtPointer)NULL);
2135}
2136
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002138clip_x11_request_selection_cb(
2139 Widget w UNUSED,
2140 XtPointer success,
2141 Atom *sel_atom,
2142 Atom *type,
2143 XtPointer value,
2144 long_u *length,
2145 int *format)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146{
Bram Moolenaard44347f2011-06-19 01:14:29 +02002147 int motion_type = MAUTO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 long_u len;
2149 char_u *p;
2150 char **text_list = NULL;
2151 VimClipboard *cbd;
2152#ifdef FEAT_MBYTE
2153 char_u *tmpbuf = NULL;
2154#endif
2155
2156 if (*sel_atom == clip_plus.sel_atom)
2157 cbd = &clip_plus;
2158 else
2159 cbd = &clip_star;
2160
2161 if (value == NULL || *length == 0)
2162 {
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002163 clip_free_selection(cbd); /* nothing received, clear register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 *(int *)success = FALSE;
2165 return;
2166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 p = (char_u *)value;
2168 len = *length;
2169 if (*type == vim_atom)
2170 {
2171 motion_type = *p++;
2172 len--;
2173 }
2174
2175#ifdef FEAT_MBYTE
2176 else if (*type == vimenc_atom)
2177 {
2178 char_u *enc;
2179 vimconv_T conv;
2180 int convlen;
2181
2182 motion_type = *p++;
2183 --len;
2184
2185 enc = p;
2186 p += STRLEN(p) + 1;
2187 len -= p - enc;
2188
2189 /* If the encoding of the text is different from 'encoding', attempt
2190 * converting it. */
2191 conv.vc_type = CONV_NONE;
2192 convert_setup(&conv, enc, p_enc);
2193 if (conv.vc_type != CONV_NONE)
2194 {
2195 convlen = len; /* Need to use an int here. */
2196 tmpbuf = string_convert(&conv, p, &convlen);
2197 len = convlen;
2198 if (tmpbuf != NULL)
2199 p = tmpbuf;
2200 convert_setup(&conv, NULL, NULL);
2201 }
2202 }
2203#endif
2204
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002205 else if (*type == compound_text_atom
2206#ifdef FEAT_MBYTE
2207 || *type == utf8_atom
2208#endif
2209 || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210#ifdef FEAT_MBYTE
2211 enc_dbcs != 0 &&
2212#endif
2213 *type == text_atom))
2214 {
2215 XTextProperty text_prop;
2216 int n_text = 0;
2217 int status;
2218
2219 text_prop.value = (unsigned char *)value;
2220 text_prop.encoding = *type;
2221 text_prop.format = *format;
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002222 text_prop.nitems = len;
Bram Moolenaar81851112013-04-12 12:27:30 +02002223#if defined(FEAT_MBYTE) && defined(X_HAVE_UTF8_STRING)
Bram Moolenaare2e663f2013-03-07 18:02:30 +01002224 if (*type == utf8_atom)
2225 status = Xutf8TextPropertyToTextList(X_DISPLAY, &text_prop,
2226 &text_list, &n_text);
2227 else
2228#endif
2229 status = XmbTextPropertyToTextList(X_DISPLAY, &text_prop,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 &text_list, &n_text);
2231 if (status != Success || n_text < 1)
2232 {
2233 *(int *)success = FALSE;
2234 return;
2235 }
2236 p = (char_u *)text_list[0];
2237 len = STRLEN(p);
2238 }
2239 clip_yank_selection(motion_type, p, (long)len, cbd);
2240
2241 if (text_list != NULL)
2242 XFreeStringList(text_list);
2243#ifdef FEAT_MBYTE
2244 vim_free(tmpbuf);
2245#endif
2246 XtFree((char *)value);
2247 *(int *)success = TRUE;
2248}
2249
2250 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002251clip_x11_request_selection(
2252 Widget myShell,
2253 Display *dpy,
2254 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255{
2256 XEvent event;
2257 Atom type;
2258 static int success;
2259 int i;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002260 time_t start_time;
2261 int timed_out = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262
2263 for (i =
2264#ifdef FEAT_MBYTE
2265 0
2266#else
2267 1
2268#endif
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002269 ; i < 6; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 {
2271 switch (i)
2272 {
2273#ifdef FEAT_MBYTE
2274 case 0: type = vimenc_atom; break;
2275#endif
2276 case 1: type = vim_atom; break;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002277#ifdef FEAT_MBYTE
2278 case 2: type = utf8_atom; break;
2279#endif
2280 case 3: type = compound_text_atom; break;
2281 case 4: type = text_atom; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 default: type = XA_STRING;
2283 }
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002284#ifdef FEAT_MBYTE
Bram Moolenaar81851112013-04-12 12:27:30 +02002285 if (type == utf8_atom
2286# if defined(X_HAVE_UTF8_STRING)
2287 && !enc_utf8
2288# endif
2289 )
2290 /* Only request utf-8 when 'encoding' is utf8 and
2291 * Xutf8TextPropertyToTextList is available. */
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002292 continue;
2293#endif
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002294 success = MAYBE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 XtGetSelectionValue(myShell, cbd->sel_atom, type,
2296 clip_x11_request_selection_cb, (XtPointer)&success, CurrentTime);
2297
2298 /* Make sure the request for the selection goes out before waiting for
2299 * a response. */
2300 XFlush(dpy);
2301
2302 /*
2303 * Wait for result of selection request, otherwise if we type more
2304 * characters, then they will appear before the one that requested the
2305 * paste! Don't worry, we will catch up with any other events later.
2306 */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002307 start_time = time(NULL);
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002308 while (success == MAYBE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002310 if (XCheckTypedEvent(dpy, PropertyNotify, &event)
2311 || XCheckTypedEvent(dpy, SelectionNotify, &event)
2312 || XCheckTypedEvent(dpy, SelectionRequest, &event))
Bram Moolenaar89417b92008-09-07 19:48:53 +00002313 {
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002314 /* This is where clip_x11_request_selection_cb() should be
2315 * called. It may actually happen a bit later, so we loop
2316 * until "success" changes.
2317 * We may get a SelectionRequest here and if we don't handle
2318 * it we hang. KDE klipper does this, for example.
2319 * We need to handle a PropertyNotify for large selections. */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002320 XtDispatchEvent(&event);
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002321 continue;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323
Bram Moolenaar89417b92008-09-07 19:48:53 +00002324 /* Time out after 2 to 3 seconds to avoid that we hang when the
2325 * other process doesn't respond. Note that the SelectionNotify
2326 * event may still come later when the selection owner comes back
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002327 * to life and the text gets inserted unexpectedly. Don't know
2328 * why that happens or how to avoid that :-(. */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002329 if (time(NULL) > start_time + 2)
2330 {
2331 timed_out = TRUE;
2332 break;
2333 }
2334
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 /* Do we need this? Probably not. */
2336 XSync(dpy, False);
2337
Bram Moolenaar89417b92008-09-07 19:48:53 +00002338 /* Wait for 1 msec to avoid that we eat up all CPU time. */
2339 ui_delay(1L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 }
2341
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002342 if (success == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 return;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002344
2345 /* don't do a retry with another type after timing out, otherwise we
2346 * hang for 15 seconds. */
2347 if (timed_out)
2348 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 }
2350
2351 /* Final fallback position - use the X CUT_BUFFER0 store */
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002352 yank_cut_buffer0(dpy, cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353}
2354
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 static Boolean
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002356clip_x11_convert_selection_cb(
2357 Widget w UNUSED,
2358 Atom *sel_atom,
2359 Atom *target,
2360 Atom *type,
2361 XtPointer *value,
2362 long_u *length,
2363 int *format)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364{
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002365 static char_u *save_result = NULL;
2366 static long_u save_length = 0;
2367 char_u *string;
2368 int motion_type;
2369 VimClipboard *cbd;
2370 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371
2372 if (*sel_atom == clip_plus.sel_atom)
2373 cbd = &clip_plus;
2374 else
2375 cbd = &clip_star;
2376
2377 if (!cbd->owned)
2378 return False; /* Shouldn't ever happen */
2379
2380 /* requestor wants to know what target types we support */
2381 if (*target == targets_atom)
2382 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002383 static Atom array[7];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 *value = (XtPointer)array;
2386 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 array[i++] = targets_atom;
2388#ifdef FEAT_MBYTE
2389 array[i++] = vimenc_atom;
2390#endif
2391 array[i++] = vim_atom;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002392#ifdef FEAT_MBYTE
2393 if (enc_utf8)
2394 array[i++] = utf8_atom;
2395#endif
2396 array[i++] = XA_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 array[i++] = text_atom;
2398 array[i++] = compound_text_atom;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002399
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 *type = XA_ATOM;
2401 /* This used to be: *format = sizeof(Atom) * 8; but that caused
2402 * crashes on 64 bit machines. (Peter Derr) */
2403 *format = 32;
2404 *length = i;
2405 return True;
2406 }
2407
2408 if ( *target != XA_STRING
2409#ifdef FEAT_MBYTE
2410 && *target != vimenc_atom
Bram Moolenaar980e58f2014-06-12 13:28:30 +02002411 && (*target != utf8_atom || !enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412#endif
2413 && *target != vim_atom
2414 && *target != text_atom
2415 && *target != compound_text_atom)
2416 return False;
2417
2418 clip_get_selection(cbd);
2419 motion_type = clip_convert_selection(&string, length, cbd);
2420 if (motion_type < 0)
2421 return False;
2422
2423 /* For our own format, the first byte contains the motion type */
2424 if (*target == vim_atom)
2425 (*length)++;
2426
2427#ifdef FEAT_MBYTE
2428 /* Our own format with encoding: motion 'encoding' NUL text */
2429 if (*target == vimenc_atom)
2430 *length += STRLEN(p_enc) + 2;
2431#endif
2432
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002433 if (save_length < *length || save_length / 2 >= *length)
2434 *value = XtRealloc((char *)save_result, (Cardinal)*length + 1);
2435 else
2436 *value = save_result;
2437 if (*value == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 {
2439 vim_free(string);
2440 return False;
2441 }
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002442 save_result = (char_u *)*value;
2443 save_length = *length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002445 if (*target == XA_STRING
2446#ifdef FEAT_MBYTE
2447 || (*target == utf8_atom && enc_utf8)
2448#endif
2449 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002451 mch_memmove(save_result, string, (size_t)(*length));
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002452 *type = *target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 }
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002454 else if (*target == compound_text_atom || *target == text_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 {
2456 XTextProperty text_prop;
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002457 char *string_nt = (char *)save_result;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002458 int conv_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459
2460 /* create NUL terminated string which XmbTextListToTextProperty wants */
2461 mch_memmove(string_nt, string, (size_t)*length);
2462 string_nt[*length] = NUL;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002463 conv_result = XmbTextListToTextProperty(X_DISPLAY, (char **)&string_nt,
2464 1, XCompoundTextStyle, &text_prop);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002465 if (conv_result != Success)
2466 {
2467 vim_free(string);
2468 return False;
2469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 *value = (XtPointer)(text_prop.value); /* from plain text */
2471 *length = text_prop.nitems;
2472 *type = compound_text_atom;
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002473 XtFree((char *)save_result);
2474 save_result = (char_u *)*value;
2475 save_length = *length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477#ifdef FEAT_MBYTE
2478 else if (*target == vimenc_atom)
2479 {
2480 int l = STRLEN(p_enc);
2481
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002482 save_result[0] = motion_type;
2483 STRCPY(save_result + 1, p_enc);
2484 mch_memmove(save_result + l + 2, string, (size_t)(*length - l - 2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 *type = vimenc_atom;
2486 }
2487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 else
2489 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002490 save_result[0] = motion_type;
2491 mch_memmove(save_result + 1, string, (size_t)(*length - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 *type = vim_atom;
2493 }
2494 *format = 8; /* 8 bits per char */
2495 vim_free(string);
2496 return True;
2497}
2498
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002500clip_x11_lose_ownership_cb(Widget w UNUSED, Atom *sel_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501{
2502 if (*sel_atom == clip_plus.sel_atom)
2503 clip_lose_selection(&clip_plus);
2504 else
2505 clip_lose_selection(&clip_star);
2506}
2507
2508 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002509clip_x11_lose_selection(Widget myShell, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510{
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002511 XtDisownSelection(myShell, cbd->sel_atom,
2512 XtLastTimestampProcessed(XtDisplay(myShell)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513}
2514
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002515 static void
2516clip_x11_notify_cb(Widget w UNUSED, Atom *sel_atom UNUSED, Atom *target UNUSED)
2517{
2518 /* To prevent automatically freeing the selection value. */
2519}
2520
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002522clip_x11_own_selection(Widget myShell, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523{
Bram Moolenaar62b42182010-09-21 22:09:37 +02002524 /* When using the GUI we have proper timestamps, use the one of the last
2525 * event. When in the console we don't get events (the terminal gets
2526 * them), Get the time by a zero-length append, clip_x11_timestamp_cb will
2527 * be called with the current timestamp. */
2528#ifdef FEAT_GUI
2529 if (gui.in_use)
2530 {
2531 if (XtOwnSelection(myShell, cbd->sel_atom,
2532 XtLastTimestampProcessed(XtDisplay(myShell)),
2533 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002534 clip_x11_notify_cb) == False)
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +01002535 return FAIL;
Bram Moolenaar62b42182010-09-21 22:09:37 +02002536 }
2537 else
2538#endif
2539 {
2540 if (!XChangeProperty(XtDisplay(myShell), XtWindow(myShell),
2541 cbd->sel_atom, timestamp_atom, 32, PropModeAppend, NULL, 0))
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +01002542 return FAIL;
Bram Moolenaar62b42182010-09-21 22:09:37 +02002543 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002544 /* Flush is required in a terminal as nothing else is doing it. */
2545 XFlush(XtDisplay(myShell));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 return OK;
2547}
2548
2549/*
2550 * Send the current selection to the clipboard. Do nothing for X because we
2551 * will fill in the selection only when requested by another app.
2552 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002554clip_x11_set_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555{
2556}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002557
2558 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002559clip_x11_owner_exists(VimClipboard *cbd)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002560{
2561 return XGetSelectionOwner(X_DISPLAY, cbd->sel_atom) != None;
2562}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563#endif
2564
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002565#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) \
2566 || defined(FEAT_GUI_GTK) || defined(PROTO)
2567/*
2568 * Get the contents of the X CUT_BUFFER0 and put it in "cbd".
2569 */
2570 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002571yank_cut_buffer0(Display *dpy, VimClipboard *cbd)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002572{
2573 int nbytes = 0;
2574 char_u *buffer = (char_u *)XFetchBuffer(dpy, &nbytes, 0);
2575
2576 if (nbytes > 0)
2577 {
2578#ifdef FEAT_MBYTE
2579 int done = FALSE;
2580
2581 /* CUT_BUFFER0 is supposed to be always latin1. Convert to 'enc' when
2582 * using a multi-byte encoding. Conversion between two 8-bit
2583 * character sets usually fails and the text might actually be in
2584 * 'enc' anyway. */
2585 if (has_mbyte)
2586 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002587 char_u *conv_buf;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002588 vimconv_T vc;
2589
2590 vc.vc_type = CONV_NONE;
2591 if (convert_setup(&vc, (char_u *)"latin1", p_enc) == OK)
2592 {
2593 conv_buf = string_convert(&vc, buffer, &nbytes);
2594 if (conv_buf != NULL)
2595 {
2596 clip_yank_selection(MCHAR, conv_buf, (long)nbytes, cbd);
2597 vim_free(conv_buf);
2598 done = TRUE;
2599 }
2600 convert_setup(&vc, NULL, NULL);
2601 }
2602 }
2603 if (!done) /* use the text without conversion */
2604#endif
2605 clip_yank_selection(MCHAR, buffer, (long)nbytes, cbd);
2606 XFree((void *)buffer);
2607 if (p_verbose > 0)
2608 {
2609 verbose_enter();
2610 verb_msg((char_u *)_("Used CUT_BUFFER0 instead of empty selection"));
2611 verbose_leave();
2612 }
2613 }
2614}
2615#endif
2616
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617#if defined(FEAT_MOUSE) || defined(PROTO)
2618
2619/*
2620 * Move the cursor to the specified row and column on the screen.
Bram Moolenaar49325942007-05-10 19:19:59 +00002621 * Change current window if necessary. Returns an integer with the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 * CURSOR_MOVED bit set if the cursor has moved or unset otherwise.
2623 *
2624 * The MOUSE_FOLD_CLOSE bit is set when clicked on the '-' in a fold column.
2625 * The MOUSE_FOLD_OPEN bit is set when clicked on the '+' in a fold column.
2626 *
2627 * If flags has MOUSE_FOCUS, then the current window will not be changed, and
2628 * if the mouse is outside the window then the text will scroll, or if the
2629 * mouse was previously on a status line, then the status line may be dragged.
2630 *
2631 * If flags has MOUSE_MAY_VIS, then VIsual mode will be started before the
2632 * cursor is moved unless the cursor was on a status line.
2633 * This function returns one of IN_UNKNOWN, IN_BUFFER, IN_STATUS_LINE or
2634 * IN_SEP_LINE depending on where the cursor was clicked.
2635 *
2636 * If flags has MOUSE_MAY_STOP_VIS, then Visual mode will be stopped, unless
2637 * the mouse is on the status line of the same window.
2638 *
2639 * If flags has MOUSE_DID_MOVE, nothing is done if the mouse didn't move since
2640 * the last call.
2641 *
2642 * If flags has MOUSE_SETPOS, nothing is done, only the current position is
2643 * remembered.
2644 */
2645 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002646jump_to_mouse(
2647 int flags,
2648 int *inclusive, /* used for inclusive operator, can be NULL */
2649 int which_button) /* MOUSE_LEFT, MOUSE_RIGHT, MOUSE_MIDDLE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650{
2651 static int on_status_line = 0; /* #lines below bottom of window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 static int on_sep_line = 0; /* on separator right of window */
Bram Moolenaareb163d72017-09-23 15:08:17 +02002653#ifdef FEAT_MENU
2654 static int in_winbar = FALSE;
2655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 static int prev_row = -1;
2657 static int prev_col = -1;
2658 static win_T *dragwin = NULL; /* window being dragged */
2659 static int did_drag = FALSE; /* drag was noticed */
2660
2661 win_T *wp, *old_curwin;
2662 pos_T old_cursor;
2663 int count;
2664 int first;
2665 int row = mouse_row;
2666 int col = mouse_col;
2667#ifdef FEAT_FOLDING
2668 int mouse_char;
2669#endif
2670
2671 mouse_past_bottom = FALSE;
2672 mouse_past_eol = FALSE;
2673
2674 if (flags & MOUSE_RELEASED)
2675 {
2676 /* On button release we may change window focus if positioned on a
2677 * status line and no dragging happened. */
2678 if (dragwin != NULL && !did_drag)
2679 flags &= ~(MOUSE_FOCUS | MOUSE_DID_MOVE);
2680 dragwin = NULL;
2681 did_drag = FALSE;
2682 }
2683
2684 if ((flags & MOUSE_DID_MOVE)
2685 && prev_row == mouse_row
2686 && prev_col == mouse_col)
2687 {
2688retnomove:
Bram Moolenaar49325942007-05-10 19:19:59 +00002689 /* before moving the cursor for a left click which is NOT in a status
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 * line, stop Visual mode */
2691 if (on_status_line)
2692 return IN_STATUS_LINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 if (on_sep_line)
2694 return IN_SEP_LINE;
Bram Moolenaard327b0c2017-11-12 16:56:12 +01002695#ifdef FEAT_MENU
2696 if (in_winbar)
2697 {
2698 /* A quick second click may arrive as a double-click, but we use it
2699 * as a second click in the WinBar. */
2700 if ((mod_mask & MOD_MASK_MULTI_CLICK) && !(flags & MOUSE_RELEASED))
2701 {
2702 wp = mouse_find_win(&row, &col);
2703 if (wp == NULL)
2704 return IN_UNKNOWN;
2705 winbar_click(wp, col);
2706 }
2707 return IN_OTHER_WIN | MOUSE_WINBAR;
2708 }
2709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 if (flags & MOUSE_MAY_STOP_VIS)
2711 {
2712 end_visual_mode();
2713 redraw_curbuf_later(INVERTED); /* delete the inversion */
2714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2716 /* Continue a modeless selection in another window. */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002717 if (cmdwin_type != 0 && row < curwin->w_winrow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 return IN_OTHER_WIN;
2719#endif
2720 return IN_BUFFER;
2721 }
2722
2723 prev_row = mouse_row;
2724 prev_col = mouse_col;
2725
2726 if (flags & MOUSE_SETPOS)
2727 goto retnomove; /* ugly goto... */
2728
2729#ifdef FEAT_FOLDING
2730 /* Remember the character under the mouse, it might be a '-' or '+' in the
2731 * fold column. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002732 if (row >= 0 && row < Rows && col >= 0 && col <= Columns
2733 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 mouse_char = ScreenLines[LineOffset[row] + col];
2735 else
2736 mouse_char = ' ';
2737#endif
2738
2739 old_curwin = curwin;
2740 old_cursor = curwin->w_cursor;
2741
2742 if (!(flags & MOUSE_FOCUS))
2743 {
2744 if (row < 0 || col < 0) /* check if it makes sense */
2745 return IN_UNKNOWN;
2746
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 /* find the window where the row is in */
2748 wp = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02002749 if (wp == NULL)
2750 return IN_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 dragwin = NULL;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002752
2753#ifdef FEAT_MENU
2754 if (row == -1)
2755 {
2756 /* A click in the window toolbar does not enter another window or
2757 * change Visual highlighting. */
2758 winbar_click(wp, col);
Bram Moolenaareb163d72017-09-23 15:08:17 +02002759 in_winbar = TRUE;
2760 return IN_OTHER_WIN | MOUSE_WINBAR;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002761 }
Bram Moolenaareb163d72017-09-23 15:08:17 +02002762 in_winbar = FALSE;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002763#endif
2764
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 /*
2766 * winpos and height may change in win_enter()!
2767 */
2768 if (row >= wp->w_height) /* In (or below) status line */
2769 {
2770 on_status_line = row - wp->w_height + 1;
2771 dragwin = wp;
2772 }
2773 else
2774 on_status_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 if (col >= wp->w_width) /* In separator line */
2776 {
2777 on_sep_line = col - wp->w_width + 1;
2778 dragwin = wp;
2779 }
2780 else
2781 on_sep_line = 0;
2782
2783 /* The rightmost character of the status line might be a vertical
2784 * separator character if there is no connecting window to the right. */
2785 if (on_status_line && on_sep_line)
2786 {
2787 if (stl_connected(wp))
2788 on_sep_line = 0;
2789 else
2790 on_status_line = 0;
2791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 /* Before jumping to another buffer, or moving the cursor for a left
2794 * click, stop Visual mode. */
2795 if (VIsual_active
2796 && (wp->w_buffer != curwin->w_buffer
Bram Moolenaar4033c552017-09-16 20:54:51 +02002797 || (!on_status_line && !on_sep_line
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002798#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002800# ifdef FEAT_RIGHTLEFT
Bram Moolenaar02631462017-09-22 15:20:32 +02002801 wp->w_p_rl ? col < wp->w_width - wp->w_p_fdc :
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802# endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002803 col >= wp->w_p_fdc
2804# ifdef FEAT_CMDWIN
2805 + (cmdwin_type == 0 && wp == curwin ? 0 : 1)
2806# endif
2807 )
2808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 && (flags & MOUSE_MAY_STOP_VIS))))
2810 {
2811 end_visual_mode();
2812 redraw_curbuf_later(INVERTED); /* delete the inversion */
2813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814#ifdef FEAT_CMDWIN
2815 if (cmdwin_type != 0 && wp != curwin)
2816 {
2817 /* A click outside the command-line window: Use modeless
Bram Moolenaarf679a432010-03-02 18:16:09 +01002818 * selection if possible. Allow dragging the status lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 on_sep_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820# ifdef FEAT_CLIPBOARD
2821 if (on_status_line)
2822 return IN_STATUS_LINE;
2823 return IN_OTHER_WIN;
2824# else
2825 row = 0;
2826 col += wp->w_wincol;
2827 wp = curwin;
2828# endif
2829 }
2830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 /* Only change window focus when not clicking on or dragging the
2832 * status line. Do change focus when releasing the mouse button
2833 * (MOUSE_FOCUS was set above if we dragged first). */
2834 if (dragwin == NULL || (flags & MOUSE_RELEASED))
2835 win_enter(wp, TRUE); /* can make wp invalid! */
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002836
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 if (curwin != old_curwin)
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002838 {
2839#ifdef CHECK_DOUBLE_CLICK
2840 /* set topline, to be able to check for double click ourselves */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 set_mouse_topline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842#endif
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002843#ifdef FEAT_TERMINAL
2844 /* when entering a terminal window may change state */
2845 term_win_entered();
2846#endif
2847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 if (on_status_line) /* In (or below) status line */
2849 {
2850 /* Don't use start_arrow() if we're in the same window */
2851 if (curwin == old_curwin)
2852 return IN_STATUS_LINE;
2853 else
2854 return IN_STATUS_LINE | CURSOR_MOVED;
2855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 if (on_sep_line) /* In (or below) status line */
2857 {
2858 /* Don't use start_arrow() if we're in the same window */
2859 if (curwin == old_curwin)
2860 return IN_SEP_LINE;
2861 else
2862 return IN_SEP_LINE | CURSOR_MOVED;
2863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864
2865 curwin->w_cursor.lnum = curwin->w_topline;
2866#ifdef FEAT_GUI
2867 /* remember topline, needed for double click */
2868 gui_prev_topline = curwin->w_topline;
2869# ifdef FEAT_DIFF
2870 gui_prev_topfill = curwin->w_topfill;
2871# endif
2872#endif
2873 }
2874 else if (on_status_line && which_button == MOUSE_LEFT)
2875 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 if (dragwin != NULL)
2877 {
2878 /* Drag the status line */
2879 count = row - dragwin->w_winrow - dragwin->w_height + 1
2880 - on_status_line;
2881 win_drag_status_line(dragwin, count);
2882 did_drag |= count;
2883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 return IN_STATUS_LINE; /* Cursor didn't move */
2885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 else if (on_sep_line && which_button == MOUSE_LEFT)
2887 {
2888 if (dragwin != NULL)
2889 {
2890 /* Drag the separator column */
2891 count = col - dragwin->w_wincol - dragwin->w_width + 1
2892 - on_sep_line;
2893 win_drag_vsep_line(dragwin, count);
2894 did_drag |= count;
2895 }
2896 return IN_SEP_LINE; /* Cursor didn't move */
2897 }
Bram Moolenaareb163d72017-09-23 15:08:17 +02002898#ifdef FEAT_MENU
2899 else if (in_winbar)
2900 {
2901 /* After a click on the window toolbar don't start Visual mode. */
2902 return IN_OTHER_WIN | MOUSE_WINBAR;
2903 }
2904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 else /* keep_window_focus must be TRUE */
2906 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 /* before moving the cursor for a left click, stop Visual mode */
2908 if (flags & MOUSE_MAY_STOP_VIS)
2909 {
2910 end_visual_mode();
2911 redraw_curbuf_later(INVERTED); /* delete the inversion */
2912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913
2914#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2915 /* Continue a modeless selection in another window. */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002916 if (cmdwin_type != 0 && row < curwin->w_winrow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 return IN_OTHER_WIN;
2918#endif
2919
2920 row -= W_WINROW(curwin);
Bram Moolenaar53f81742017-09-22 14:35:51 +02002921 col -= curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922
2923 /*
2924 * When clicking beyond the end of the window, scroll the screen.
2925 * Scroll by however many rows outside the window we are.
2926 */
2927 if (row < 0)
2928 {
2929 count = 0;
2930 for (first = TRUE; curwin->w_topline > 1; )
2931 {
2932#ifdef FEAT_DIFF
2933 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
2934 ++count;
2935 else
2936#endif
2937 count += plines(curwin->w_topline - 1);
2938 if (!first && count > -row)
2939 break;
2940 first = FALSE;
2941#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002942 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943#endif
2944#ifdef FEAT_DIFF
2945 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
2946 ++curwin->w_topfill;
2947 else
2948#endif
2949 {
2950 --curwin->w_topline;
2951#ifdef FEAT_DIFF
2952 curwin->w_topfill = 0;
2953#endif
2954 }
2955 }
2956#ifdef FEAT_DIFF
2957 check_topfill(curwin, FALSE);
2958#endif
2959 curwin->w_valid &=
2960 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2961 redraw_later(VALID);
2962 row = 0;
2963 }
2964 else if (row >= curwin->w_height)
2965 {
2966 count = 0;
2967 for (first = TRUE; curwin->w_topline < curbuf->b_ml.ml_line_count; )
2968 {
2969#ifdef FEAT_DIFF
2970 if (curwin->w_topfill > 0)
2971 ++count;
2972 else
2973#endif
2974 count += plines(curwin->w_topline);
2975 if (!first && count > row - curwin->w_height + 1)
2976 break;
2977 first = FALSE;
2978#ifdef FEAT_FOLDING
2979 if (hasFolding(curwin->w_topline, NULL, &curwin->w_topline)
2980 && curwin->w_topline == curbuf->b_ml.ml_line_count)
2981 break;
2982#endif
2983#ifdef FEAT_DIFF
2984 if (curwin->w_topfill > 0)
2985 --curwin->w_topfill;
2986 else
2987#endif
2988 {
2989 ++curwin->w_topline;
2990#ifdef FEAT_DIFF
2991 curwin->w_topfill =
2992 diff_check_fill(curwin, curwin->w_topline);
2993#endif
2994 }
2995 }
2996#ifdef FEAT_DIFF
2997 check_topfill(curwin, FALSE);
2998#endif
2999 redraw_later(VALID);
3000 curwin->w_valid &=
3001 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3002 row = curwin->w_height - 1;
3003 }
3004 else if (row == 0)
3005 {
3006 /* When dragging the mouse, while the text has been scrolled up as
3007 * far as it goes, moving the mouse in the top line should scroll
3008 * the text down (done later when recomputing w_topline). */
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003009 if (mouse_dragging > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 && curwin->w_cursor.lnum
3011 == curwin->w_buffer->b_ml.ml_line_count
3012 && curwin->w_cursor.lnum == curwin->w_topline)
3013 curwin->w_valid &= ~(VALID_TOPLINE);
3014 }
3015 }
3016
3017#ifdef FEAT_FOLDING
3018 /* Check for position outside of the fold column. */
3019 if (
3020# ifdef FEAT_RIGHTLEFT
Bram Moolenaar02631462017-09-22 15:20:32 +02003021 curwin->w_p_rl ? col < curwin->w_width - curwin->w_p_fdc :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022# endif
3023 col >= curwin->w_p_fdc
3024# ifdef FEAT_CMDWIN
3025 + (cmdwin_type == 0 ? 0 : 1)
3026# endif
3027 )
3028 mouse_char = ' ';
3029#endif
3030
3031 /* compute the position in the buffer line from the posn on the screen */
3032 if (mouse_comp_pos(curwin, &row, &col, &curwin->w_cursor.lnum))
3033 mouse_past_bottom = TRUE;
3034
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 /* Start Visual mode before coladvance(), for when 'sel' != "old" */
3036 if ((flags & MOUSE_MAY_VIS) && !VIsual_active)
3037 {
3038 check_visual_highlight();
3039 VIsual = old_cursor;
3040 VIsual_active = TRUE;
3041 VIsual_reselect = TRUE;
3042 /* if 'selectmode' contains "mouse", start Select mode */
3043 may_start_select('o');
3044 setmouse();
Bram Moolenaar7df351e2006-01-23 22:30:28 +00003045 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 redraw_cmdline = TRUE; /* show visual mode later */
3047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048
3049 curwin->w_curswant = col;
3050 curwin->w_set_curswant = FALSE; /* May still have been TRUE */
3051 if (coladvance(col) == FAIL) /* Mouse click beyond end of line */
3052 {
3053 if (inclusive != NULL)
3054 *inclusive = TRUE;
3055 mouse_past_eol = TRUE;
3056 }
3057 else if (inclusive != NULL)
3058 *inclusive = FALSE;
3059
3060 count = IN_BUFFER;
3061 if (curwin != old_curwin || curwin->w_cursor.lnum != old_cursor.lnum
3062 || curwin->w_cursor.col != old_cursor.col)
3063 count |= CURSOR_MOVED; /* Cursor has moved */
3064
3065#ifdef FEAT_FOLDING
3066 if (mouse_char == '+')
3067 count |= MOUSE_FOLD_OPEN;
3068 else if (mouse_char != ' ')
3069 count |= MOUSE_FOLD_CLOSE;
3070#endif
3071
3072 return count;
3073}
3074
3075/*
3076 * Compute the position in the buffer line from the posn on the screen in
3077 * window "win".
3078 * Returns TRUE if the position is below the last line.
3079 */
3080 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003081mouse_comp_pos(
3082 win_T *win,
3083 int *rowp,
3084 int *colp,
3085 linenr_T *lnump)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086{
3087 int col = *colp;
3088 int row = *rowp;
3089 linenr_T lnum;
3090 int retval = FALSE;
3091 int off;
3092 int count;
3093
3094#ifdef FEAT_RIGHTLEFT
3095 if (win->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02003096 col = win->w_width - 1 - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097#endif
3098
3099 lnum = win->w_topline;
3100
3101 while (row > 0)
3102 {
3103#ifdef FEAT_DIFF
3104 /* Don't include filler lines in "count" */
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003105 if (win->w_p_diff
3106# ifdef FEAT_FOLDING
3107 && !hasFoldingWin(win, lnum, NULL, NULL, TRUE, NULL)
3108# endif
3109 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 {
3111 if (lnum == win->w_topline)
3112 row -= win->w_topfill;
3113 else
3114 row -= diff_check_fill(win, lnum);
3115 count = plines_win_nofill(win, lnum, TRUE);
3116 }
3117 else
3118#endif
3119 count = plines_win(win, lnum, TRUE);
3120 if (count > row)
3121 break; /* Position is in this buffer line. */
3122#ifdef FEAT_FOLDING
3123 (void)hasFoldingWin(win, lnum, NULL, &lnum, TRUE, NULL);
3124#endif
3125 if (lnum == win->w_buffer->b_ml.ml_line_count)
3126 {
3127 retval = TRUE;
3128 break; /* past end of file */
3129 }
3130 row -= count;
3131 ++lnum;
3132 }
3133
3134 if (!retval)
3135 {
3136 /* Compute the column without wrapping. */
3137 off = win_col_off(win) - win_col_off2(win);
3138 if (col < off)
3139 col = off;
Bram Moolenaar02631462017-09-22 15:20:32 +02003140 col += row * (win->w_width - off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 /* add skip column (for long wrapping line) */
3142 col += win->w_skipcol;
3143 }
3144
3145 if (!win->w_p_wrap)
3146 col += win->w_leftcol;
3147
3148 /* skip line number and fold column in front of the line */
3149 col -= win_col_off(win);
3150 if (col < 0)
3151 {
3152#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003153 netbeans_gutter_click(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154#endif
3155 col = 0;
3156 }
3157
3158 *colp = col;
3159 *rowp = row;
3160 *lnump = lnum;
3161 return retval;
3162}
3163
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164/*
3165 * Find the window at screen position "*rowp" and "*colp". The positions are
3166 * updated to become relative to the top-left of the window.
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003167 * Returns NULL when something is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 win_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003170mouse_find_win(int *rowp, int *colp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171{
3172 frame_T *fp;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003173 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174
3175 fp = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003176 *rowp -= firstwin->w_winrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 for (;;)
3178 {
3179 if (fp->fr_layout == FR_LEAF)
3180 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 if (fp->fr_layout == FR_ROW)
3182 {
3183 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
3184 {
3185 if (*colp < fp->fr_width)
3186 break;
3187 *colp -= fp->fr_width;
3188 }
3189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 else /* fr_layout == FR_COL */
3191 {
3192 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
3193 {
3194 if (*rowp < fp->fr_height)
3195 break;
3196 *rowp -= fp->fr_height;
3197 }
3198 }
3199 }
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003200 /* When using a timer that closes a window the window might not actually
3201 * exist. */
3202 FOR_ALL_WINDOWS(wp)
3203 if (wp == fp->fr_win)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003204 {
3205#ifdef FEAT_MENU
3206 *rowp -= wp->w_winbar_height;
3207#endif
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003208 return wp;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003209 }
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003210 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212
Bram Moolenaar860cae12010-06-05 23:22:07 +02003213#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar658a1542018-03-03 19:29:43 +01003215 || defined(FEAT_GUI_PHOTON) || defined(FEAT_TERM_POPUP_MENU) \
3216 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217/*
3218 * Translate window coordinates to buffer position without any side effects
3219 */
3220 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003221get_fpos_of_mouse(pos_T *mpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222{
3223 win_T *wp;
3224 int row = mouse_row;
3225 int col = mouse_col;
3226
3227 if (row < 0 || col < 0) /* check if it makes sense */
3228 return IN_UNKNOWN;
3229
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 /* find the window where the row is in */
3231 wp = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003232 if (wp == NULL)
3233 return IN_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 /*
3235 * winpos and height may change in win_enter()!
3236 */
3237 if (row >= wp->w_height) /* In (or below) status line */
3238 return IN_STATUS_LINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 if (col >= wp->w_width) /* In vertical separator line */
3240 return IN_SEP_LINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241
3242 if (wp != curwin)
3243 return IN_UNKNOWN;
3244
3245 /* compute the position in the buffer line from the posn on the screen */
3246 if (mouse_comp_pos(curwin, &row, &col, &mpos->lnum))
3247 return IN_STATUS_LINE; /* past bottom */
3248
3249 mpos->col = vcol2col(wp, mpos->lnum, col);
3250
3251 if (mpos->col > 0)
3252 --mpos->col;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003253#ifdef FEAT_VIRTUALEDIT
3254 mpos->coladd = 0;
3255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 return IN_BUFFER;
3257}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003260#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MAC) \
3261 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar3767b612018-03-03 19:51:58 +01003262 || defined(FEAT_GUI_PHOTON) || defined(FEAT_BEVAL) \
3263 || defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264/*
3265 * Convert a virtual (screen) column to a character column.
3266 * The first column is one.
3267 */
3268 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003269vcol2col(win_T *wp, linenr_T lnum, int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270{
3271 /* try to advance to the specified column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 int count = 0;
3273 char_u *ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003274 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
Bram Moolenaar597a4222014-06-25 14:39:50 +02003276 line = ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaarb6101cf2012-10-21 00:58:39 +02003277 while (count < vcol && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003279 count += win_lbr_chartabsize(wp, line, ptr, count, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003280 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02003282 return (int)(ptr - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283}
3284#endif
3285
3286#endif /* FEAT_MOUSE */
3287
3288#if defined(FEAT_GUI) || defined(WIN3264) || defined(PROTO)
3289/*
3290 * Called when focus changed. Used for the GUI or for systems where this can
3291 * be done in the console (Win32).
3292 */
3293 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003294ui_focus_change(
3295 int in_focus) /* TRUE if focus gained. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296{
3297 static time_t last_time = (time_t)0;
3298 int need_redraw = FALSE;
3299
3300 /* When activated: Check if any file was modified outside of Vim.
3301 * Only do this when not done within the last two seconds (could get
3302 * several events in a row). */
3303 if (in_focus && last_time + 2 < time(NULL))
3304 {
3305 need_redraw = check_timestamps(
3306# ifdef FEAT_GUI
3307 gui.in_use
3308# else
3309 FALSE
3310# endif
3311 );
3312 last_time = time(NULL);
3313 }
3314
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 /*
3316 * Fire the focus gained/lost autocommand.
3317 */
3318 need_redraw |= apply_autocmds(in_focus ? EVENT_FOCUSGAINED
3319 : EVENT_FOCUSLOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320
3321 if (need_redraw)
3322 {
3323 /* Something was executed, make sure the cursor is put back where it
3324 * belongs. */
3325 need_wait_return = FALSE;
3326
3327 if (State & CMDLINE)
3328 redrawcmdline();
3329 else if (State == HITRETURN || State == SETWSIZE || State == ASKMORE
3330 || State == EXTERNCMD || State == CONFIRM || exmode_active)
3331 repeat_message();
3332 else if ((State & NORMAL) || (State & INSERT))
3333 {
3334 if (must_redraw != 0)
3335 update_screen(0);
3336 setcursor();
3337 }
3338 cursor_on(); /* redrawing may have switched it off */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003339 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340# ifdef FEAT_GUI
3341 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 gui_update_scrollbars(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343# endif
3344 }
3345#ifdef FEAT_TITLE
3346 /* File may have been changed from 'readonly' to 'noreadonly' */
3347 if (need_maketitle)
3348 maketitle();
3349#endif
3350}
3351#endif
3352
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003353#if defined(HAVE_INPUT_METHOD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354/*
3355 * Save current Input Method status to specified place.
3356 */
3357 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003358im_save_status(long *psave)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359{
3360 /* Don't save when 'imdisable' is set or "xic" is NULL, IM is always
3361 * disabled then (but might start later).
3362 * Also don't save when inside a mapping, vgetc_im_active has not been set
3363 * then.
3364 * And don't save when the keys were stuffed (e.g., for a "." command).
3365 * And don't save when the GUI is running but our window doesn't have
3366 * input focus (e.g., when a find dialog is open). */
3367 if (!p_imdisable && KeyTyped && !KeyStuffed
3368# ifdef FEAT_XIM
3369 && xic != NULL
3370# endif
3371# ifdef FEAT_GUI
3372 && (!gui.in_use || gui.in_focus)
3373# endif
3374 )
3375 {
3376 /* Do save when IM is on, or IM is off and saved status is on. */
3377 if (vgetc_im_active)
3378 *psave = B_IMODE_IM;
3379 else if (*psave == B_IMODE_IM)
3380 *psave = B_IMODE_NONE;
3381 }
3382}
3383#endif