blob: 08dc9d0a0c0359aaf70d181e52b280b81f1f9489 [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 Moolenaarc46af532019-01-09 22:24:49 +0100208#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100209/*
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;
Bram Moolenaarc46af532019-01-09 22:24:49 +0100224# ifdef FEAT_JOB_CHANNEL
Bram Moolenaare299bbd2019-01-17 14:12:02 +0100225 int brief_wait = FALSE;
Bram Moolenaarc46af532019-01-09 22:24:49 +0100226# endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100227
Bram Moolenaarc46af532019-01-09 22:24:49 +0100228 // When waiting very briefly don't trigger timers.
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100229 if (wtime >= 0 && wtime < 10L)
230 return wait_func(wtime, NULL, ignore_input);
231
232 while (wtime < 0 || remaining > 0)
233 {
Bram Moolenaarc46af532019-01-09 22:24:49 +0100234 // Trigger timers and then get the time in wtime until the next one is
235 // due. Wait up to that time.
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100236 due_time = check_due_timer();
237 if (typebuf.tb_change_cnt != tb_change_cnt)
238 {
239 /* timer may have used feedkeys() */
240 return FAIL;
241 }
242 if (due_time <= 0 || (wtime > 0 && due_time > remaining))
243 due_time = remaining;
Bram Moolenaarc46af532019-01-09 22:24:49 +0100244# ifdef FEAT_JOB_CHANNEL
245 if ((due_time < 0 || due_time > 10L)
246# ifdef FEAT_GUI
247 && !gui.in_use
248# endif
249 && (has_pending_job() || channel_any_readahead()))
250 {
251 // There is a pending job or channel, should return soon in order
252 // to handle them ASAP. Do check for input briefly.
253 due_time = 10L;
254 brief_wait = TRUE;
255 }
256# endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100257 if (wait_func(due_time, interrupted, ignore_input))
258 return OK;
Bram Moolenaarc46af532019-01-09 22:24:49 +0100259 if ((interrupted != NULL && *interrupted)
260# ifdef FEAT_JOB_CHANNEL
261 || brief_wait
262# endif
263 )
264 // Nothing available, but need to return so that side effects get
265 // handled, such as handling a message on a channel.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100266 return FAIL;
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100267 if (wtime > 0)
268 remaining -= due_time;
269 }
270 return FAIL;
271}
272#endif
273
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274/*
Bram Moolenaarc46af532019-01-09 22:24:49 +0100275 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 */
277 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100278ui_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279{
280#ifdef FEAT_GUI
281 if (gui.in_use)
282 {
283 gui_mch_update();
284 return input_available();
285 }
286#endif
287#ifndef NO_CONSOLE
288# ifdef NO_CONSOLE_INPUT
289 if (no_console_input())
290 return 0;
291# endif
292 return mch_char_avail();
293#else
294 return 0;
295#endif
296}
297
298/*
299 * Delay for the given number of milliseconds. If ignoreinput is FALSE then we
300 * cancel the delay if a key is hit.
301 */
302 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100303ui_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304{
305#ifdef FEAT_GUI
306 if (gui.in_use && !ignoreinput)
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100307 gui_wait_for_chars(msec, typebuf.tb_change_cnt);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 else
309#endif
310 mch_delay(msec, ignoreinput);
311}
312
313/*
314 * If the machine has job control, use it to suspend the program,
315 * otherwise fake it by starting a new shell.
316 * When running the GUI iconify the window.
317 */
318 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100319ui_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320{
321#ifdef FEAT_GUI
322 if (gui.in_use)
323 {
324 gui_mch_iconify();
325 return;
326 }
327#endif
328 mch_suspend();
329}
330
331#if !defined(UNIX) || !defined(SIGTSTP) || defined(PROTO) || defined(__BEOS__)
332/*
333 * When the OS can't really suspend, call this function to start a shell.
334 * This is never called in the GUI.
335 */
336 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100337suspend_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338{
339 if (*p_sh == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100340 emsg(_(e_shellempty));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 else
342 {
343 MSG_PUTS(_("new shell started\n"));
344 do_shell(NULL, 0);
345 }
346}
347#endif
348
349/*
350 * Try to get the current Vim shell size. Put the result in Rows and Columns.
351 * Use the new sizes as defaults for 'columns' and 'lines'.
352 * Return OK when size could be determined, FAIL otherwise.
353 */
354 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100355ui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356{
357 int retval;
358
359#ifdef FEAT_GUI
360 if (gui.in_use)
361 retval = gui_get_shellsize();
362 else
363#endif
364 retval = mch_get_shellsize();
365
366 check_shellsize();
367
368 /* adjust the default for 'lines' and 'columns' */
369 if (retval == OK)
370 {
371 set_number_default("lines", Rows);
372 set_number_default("columns", Columns);
373 }
374 return retval;
375}
376
377/*
378 * Set the size of the Vim shell according to Rows and Columns, if possible.
379 * The gui_set_shellsize() or mch_set_shellsize() function will try to set the
380 * new size. If this is not possible, it will adjust Rows and Columns.
381 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100383ui_set_shellsize(
384 int mustset UNUSED) /* set by the user */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385{
386#ifdef FEAT_GUI
387 if (gui.in_use)
Bram Moolenaar8968a312013-07-03 16:58:44 +0200388 gui_set_shellsize(mustset, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 else
390#endif
391 mch_set_shellsize();
392}
393
394/*
395 * Called when Rows and/or Columns changed. Adjust scroll region and mouse
396 * region.
397 */
398 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100399ui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400{
401 if (full_screen && !exiting)
402 {
403#ifdef FEAT_GUI
404 if (gui.in_use)
405 gui_new_shellsize();
406 else
407#endif
408 mch_new_shellsize();
409 }
410}
411
412 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100413ui_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200415 ui_breakcheck_force(FALSE);
416}
417
418/*
419 * When "force" is true also check when the terminal is not in raw mode.
420 * This is useful to read input on channels.
421 */
422 void
423ui_breakcheck_force(int force)
424{
Bram Moolenaar48d23bb2018-11-20 02:42:43 +0100425 static int recursive = FALSE;
426 int save_updating_screen = updating_screen;
Bram Moolenaare3caa112017-01-31 22:07:42 +0100427
Bram Moolenaar48d23bb2018-11-20 02:42:43 +0100428 // We could be called recursively if stderr is redirected, calling
429 // fill_input_buf() calls settmode() when stdin isn't a tty. settmode()
430 // calls vgetorpeek() which calls ui_breakcheck() again.
431 if (recursive)
432 return;
433 recursive = TRUE;
434
435 // We do not want gui_resize_shell() to redraw the screen here.
Bram Moolenaare3caa112017-01-31 22:07:42 +0100436 ++updating_screen;
437
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438#ifdef FEAT_GUI
439 if (gui.in_use)
440 gui_mch_update();
441 else
442#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200443 mch_breakcheck(force);
Bram Moolenaare3caa112017-01-31 22:07:42 +0100444
Bram Moolenaar42335f52018-09-13 15:33:43 +0200445 if (save_updating_screen)
446 updating_screen = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200447 else
448 reset_updating_screen(FALSE);
Bram Moolenaar48d23bb2018-11-20 02:42:43 +0100449
450 recursive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451}
452
453/*****************************************************************************
454 * Functions for copying and pasting text between applications.
455 * This is always included in a GUI version, but may also be included when the
456 * clipboard and mouse is available to a terminal version such as xterm.
457 * Note: there are some more functions in ops.c that handle selection stuff.
458 *
459 * Also note that the majority of functions here deal with the X 'primary'
460 * (visible - for Visual mode use) selection, and only that. There are no
461 * versions of these for the 'clipboard' selection, as Visual mode has no use
462 * for them.
463 */
464
465#if defined(FEAT_CLIPBOARD) || defined(PROTO)
466
467/*
468 * Selection stuff using Visual mode, for cutting and pasting text to other
469 * windows.
470 */
471
472/*
473 * Call this to initialise the clipboard. Pass it FALSE if the clipboard code
474 * is included, but the clipboard can not be used, or TRUE if the clipboard can
475 * be used. Eg unix may call this with FALSE, then call it again with TRUE if
476 * the GUI starts.
477 */
478 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100479clip_init(int can_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480{
481 VimClipboard *cb;
482
483 cb = &clip_star;
484 for (;;)
485 {
486 cb->available = can_use;
487 cb->owned = FALSE;
488 cb->start.lnum = 0;
489 cb->start.col = 0;
490 cb->end.lnum = 0;
491 cb->end.col = 0;
492 cb->state = SELECT_CLEARED;
493
494 if (cb == &clip_plus)
495 break;
496 cb = &clip_plus;
497 }
498}
499
500/*
501 * Check whether the VIsual area has changed, and if so try to become the owner
502 * of the selection, and free any old converted selection we may still have
503 * lying around. If the VIsual mode has ended, make a copy of what was
504 * selected so we can still give it to others. Will probably have to make sure
505 * this is called whenever VIsual mode is ended.
506 */
507 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100508clip_update_selection(VimClipboard *clip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509{
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200510 pos_T start, end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511
512 /* If visual mode is only due to a redo command ("."), then ignore it */
513 if (!redo_VIsual_busy && VIsual_active && (State & NORMAL))
514 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100515 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 {
517 start = VIsual;
518 end = curwin->w_cursor;
519#ifdef FEAT_MBYTE
520 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000521 end.col += (*mb_ptr2len)(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522#endif
523 }
524 else
525 {
526 start = curwin->w_cursor;
527 end = VIsual;
528 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100529 if (!EQUAL_POS(clip->start, start)
530 || !EQUAL_POS(clip->end, end)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200531 || clip->vmode != VIsual_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200533 clip_clear_selection(clip);
534 clip->start = start;
535 clip->end = end;
536 clip->vmode = VIsual_mode;
537 clip_free_selection(clip);
538 clip_own_selection(clip);
539 clip_gen_set_selection(clip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 }
541 }
542}
543
544 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100545clip_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546{
547 /*
548 * Also want to check somehow that we are reading from the keyboard rather
549 * than a mapping etc.
550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551#ifdef FEAT_X11
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200552 /* Always own the selection, we might have lost it without being
Bram Moolenaar62b42182010-09-21 22:09:37 +0200553 * notified, e.g. during a ":sh" command. */
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200554 if (cbd->available)
555 {
556 int was_owned = cbd->owned;
557
558 cbd->owned = (clip_gen_own_selection(cbd) == OK);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200559 if (!was_owned && (cbd == &clip_star || cbd == &clip_plus))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 {
Bram Moolenaarebefac62005-12-28 22:39:57 +0000561 /* May have to show a different kind of highlighting for the
562 * selected area. There is no specific redraw command for this,
563 * just redraw all windows on the current buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 if (cbd->owned
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000565 && (get_real_state() == VISUAL
566 || get_real_state() == SELECTMODE)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200567 && (cbd == &clip_star ? clip_isautosel_star()
568 : clip_isautosel_plus())
Bram Moolenaar8820b482017-03-16 17:23:31 +0100569 && HL_ATTR(HLF_V) != HL_ATTR(HLF_VNC))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 redraw_curbuf_later(INVERTED_ALL);
571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200573#else
Bram Moolenaarb6101cf2012-10-21 00:58:39 +0200574 /* Only own the clipboard when we didn't own it yet. */
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200575 if (!cbd->owned && cbd->available)
576 cbd->owned = (clip_gen_own_selection(cbd) == OK);
577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578}
579
580 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100581clip_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582{
583#ifdef FEAT_X11
584 int was_owned = cbd->owned;
585#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200586 int visual_selection = FALSE;
587
588 if (cbd == &clip_star || cbd == &clip_plus)
589 visual_selection = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590
591 clip_free_selection(cbd);
592 cbd->owned = FALSE;
593 if (visual_selection)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200594 clip_clear_selection(cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 clip_gen_lose_selection(cbd);
596#ifdef FEAT_X11
597 if (visual_selection)
598 {
599 /* May have to show a different kind of highlighting for the selected
600 * area. There is no specific redraw command for this, just redraw all
601 * windows on the current buffer. */
602 if (was_owned
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000603 && (get_real_state() == VISUAL
604 || get_real_state() == SELECTMODE)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200605 && (cbd == &clip_star ?
606 clip_isautosel_star() : clip_isautosel_plus())
Bram Moolenaar8820b482017-03-16 17:23:31 +0100607 && HL_ATTR(HLF_V) != HL_ATTR(HLF_VNC))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 {
609 update_curbuf(INVERTED_ALL);
610 setcursor();
611 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +0100612 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 }
614 }
615#endif
616}
617
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200618 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100619clip_copy_selection(VimClipboard *clip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200621 if (VIsual_active && (State & NORMAL) && clip->available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200623 clip_update_selection(clip);
624 clip_free_selection(clip);
625 clip_own_selection(clip);
626 if (clip->owned)
627 clip_get_selection(clip);
628 clip_gen_set_selection(clip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 }
630}
631
632/*
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200633 * Save and restore clip_unnamed before doing possibly many changes. This
634 * prevents accessing the clipboard very often which might slow down Vim
635 * considerably.
636 */
Bram Moolenaar73a156b2015-01-27 21:39:05 +0100637static int global_change_count = 0; /* if set, inside a start_global_changes */
Bram Moolenaar3fcfa352017-03-29 19:20:41 +0200638static int clipboard_needs_update = FALSE; /* clipboard needs to be updated */
639static int clip_did_set_selection = TRUE;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200640
641/*
642 * Save clip_unnamed and reset it.
643 */
644 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100645start_global_changes(void)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200646{
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100647 if (++global_change_count > 1)
648 return;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200649 clip_unnamed_saved = clip_unnamed;
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100650 clipboard_needs_update = FALSE;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200651
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100652 if (clip_did_set_selection)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200653 {
654 clip_unnamed = FALSE;
655 clip_did_set_selection = FALSE;
656 }
657}
658
659/*
Bram Moolenaar3fcfa352017-03-29 19:20:41 +0200660 * Return TRUE if setting the clipboard was postponed, it already contains the
661 * right text.
662 */
663 int
664is_clipboard_needs_update()
665{
666 return clipboard_needs_update;
667}
668
669/*
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200670 * Restore clip_unnamed and set the selection when needed.
671 */
672 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100673end_global_changes(void)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200674{
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100675 if (--global_change_count > 0)
676 /* recursive */
677 return;
678 if (!clip_did_set_selection)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200679 {
680 clip_did_set_selection = TRUE;
681 clip_unnamed = clip_unnamed_saved;
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100682 clip_unnamed_saved = FALSE;
683 if (clipboard_needs_update)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200684 {
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100685 /* only store something in the clipboard,
686 * if we have yanked anything to it */
687 if (clip_unnamed & CLIP_UNNAMED)
688 {
689 clip_own_selection(&clip_star);
690 clip_gen_set_selection(&clip_star);
691 }
692 if (clip_unnamed & CLIP_UNNAMED_PLUS)
693 {
694 clip_own_selection(&clip_plus);
695 clip_gen_set_selection(&clip_plus);
696 }
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200697 }
698 }
Bram Moolenaar3fcfa352017-03-29 19:20:41 +0200699 clipboard_needs_update = FALSE;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200700}
701
702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 * Called when Visual mode is ended: update the selection.
704 */
705 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100706clip_auto_select(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707{
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200708 if (clip_isautosel_star())
709 clip_copy_selection(&clip_star);
710 if (clip_isautosel_plus())
711 clip_copy_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712}
713
714/*
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200715 * Return TRUE if automatic selection of Visual area is desired for the *
716 * register.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 */
718 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100719clip_isautosel_star(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720{
721 return (
722#ifdef FEAT_GUI
723 gui.in_use ? (vim_strchr(p_go, GO_ASEL) != NULL) :
724#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200725 clip_autoselect_star);
726}
727
728/*
729 * Return TRUE if automatic selection of Visual area is desired for the +
730 * register.
731 */
732 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100733clip_isautosel_plus(void)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200734{
735 return (
736#ifdef FEAT_GUI
737 gui.in_use ? (vim_strchr(p_go, GO_ASELPLUS) != NULL) :
738#endif
739 clip_autoselect_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740}
741
742
743/*
744 * Stuff for general mouse selection, without using Visual mode.
745 */
746
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100747static void clip_invert_area(int, int, int, int, int how);
748static void clip_invert_rectangle(int row, int col, int height, int width, int invert);
749static void clip_get_word_boundaries(VimClipboard *, int, int);
750static int clip_get_line_end(int);
751static void clip_update_modeless_selection(VimClipboard *, int, int,
752 int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753
754/* flags for clip_invert_area() */
755#define CLIP_CLEAR 1
756#define CLIP_SET 2
757#define CLIP_TOGGLE 3
758
759/*
760 * Start, continue or end a modeless selection. Used when editing the
761 * command-line and in the cmdline window.
762 */
763 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100764clip_modeless(int button, int is_click, int is_drag)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765{
766 int repeat;
767
768 repeat = ((clip_star.mode == SELECT_MODE_CHAR
769 || clip_star.mode == SELECT_MODE_LINE)
770 && (mod_mask & MOD_MASK_2CLICK))
771 || (clip_star.mode == SELECT_MODE_WORD
772 && (mod_mask & MOD_MASK_3CLICK));
773 if (is_click && button == MOUSE_RIGHT)
774 {
775 /* Right mouse button: If there was no selection, start one.
776 * Otherwise extend the existing selection. */
777 if (clip_star.state == SELECT_CLEARED)
778 clip_start_selection(mouse_col, mouse_row, FALSE);
779 clip_process_selection(button, mouse_col, mouse_row, repeat);
780 }
781 else if (is_click)
782 clip_start_selection(mouse_col, mouse_row, repeat);
783 else if (is_drag)
784 {
785 /* Don't try extending a selection if there isn't one. Happens when
786 * button-down is in the cmdline and them moving mouse upwards. */
787 if (clip_star.state != SELECT_CLEARED)
788 clip_process_selection(button, mouse_col, mouse_row, repeat);
789 }
790 else /* release */
791 clip_process_selection(MOUSE_RELEASE, mouse_col, mouse_row, FALSE);
792}
793
794/*
795 * Compare two screen positions ala strcmp()
796 */
797 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100798clip_compare_pos(
799 int row1,
800 int col1,
801 int row2,
802 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803{
804 if (row1 > row2) return(1);
805 if (row1 < row2) return(-1);
806 if (col1 > col2) return(1);
807 if (col1 < col2) return(-1);
Bram Moolenaar9e341102016-02-23 23:04:36 +0100808 return(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809}
810
811/*
812 * Start the selection
813 */
814 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100815clip_start_selection(int col, int row, int repeated_click)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816{
817 VimClipboard *cb = &clip_star;
818
819 if (cb->state == SELECT_DONE)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200820 clip_clear_selection(cb);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821
822 row = check_row(row);
823 col = check_col(col);
824#ifdef FEAT_MBYTE
825 col = mb_fix_col(col, row);
826#endif
827
828 cb->start.lnum = row;
829 cb->start.col = col;
830 cb->end = cb->start;
831 cb->origin_row = (short_u)cb->start.lnum;
832 cb->state = SELECT_IN_PROGRESS;
833
834 if (repeated_click)
835 {
836 if (++cb->mode > SELECT_MODE_LINE)
837 cb->mode = SELECT_MODE_CHAR;
838 }
839 else
840 cb->mode = SELECT_MODE_CHAR;
841
842#ifdef FEAT_GUI
843 /* clear the cursor until the selection is made */
844 if (gui.in_use)
845 gui_undraw_cursor();
846#endif
847
848 switch (cb->mode)
849 {
850 case SELECT_MODE_CHAR:
851 cb->origin_start_col = cb->start.col;
852 cb->word_end_col = clip_get_line_end((int)cb->start.lnum);
853 break;
854
855 case SELECT_MODE_WORD:
856 clip_get_word_boundaries(cb, (int)cb->start.lnum, cb->start.col);
857 cb->origin_start_col = cb->word_start_col;
858 cb->origin_end_col = cb->word_end_col;
859
860 clip_invert_area((int)cb->start.lnum, cb->word_start_col,
861 (int)cb->end.lnum, cb->word_end_col, CLIP_SET);
862 cb->start.col = cb->word_start_col;
863 cb->end.col = cb->word_end_col;
864 break;
865
866 case SELECT_MODE_LINE:
867 clip_invert_area((int)cb->start.lnum, 0, (int)cb->start.lnum,
868 (int)Columns, CLIP_SET);
869 cb->start.col = 0;
870 cb->end.col = Columns;
871 break;
872 }
873
874 cb->prev = cb->start;
875
876#ifdef DEBUG_SELECTION
877 printf("Selection started at (%u,%u)\n", cb->start.lnum, cb->start.col);
878#endif
879}
880
881/*
882 * Continue processing the selection
883 */
884 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100885clip_process_selection(
886 int button,
887 int col,
888 int row,
889 int_u repeated_click)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890{
891 VimClipboard *cb = &clip_star;
892 int diff;
893 int slen = 1; /* cursor shape width */
894
895 if (button == MOUSE_RELEASE)
896 {
897 /* Check to make sure we have something selected */
898 if (cb->start.lnum == cb->end.lnum && cb->start.col == cb->end.col)
899 {
900#ifdef FEAT_GUI
901 if (gui.in_use)
902 gui_update_cursor(FALSE, FALSE);
903#endif
904 cb->state = SELECT_CLEARED;
905 return;
906 }
907
908#ifdef DEBUG_SELECTION
909 printf("Selection ended: (%u,%u) to (%u,%u)\n", cb->start.lnum,
910 cb->start.col, cb->end.lnum, cb->end.col);
911#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200912 if (clip_isautosel_star()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 || (
914#ifdef FEAT_GUI
915 gui.in_use ? (vim_strchr(p_go, GO_ASELML) != NULL) :
916#endif
917 clip_autoselectml))
918 clip_copy_modeless_selection(FALSE);
919#ifdef FEAT_GUI
920 if (gui.in_use)
921 gui_update_cursor(FALSE, FALSE);
922#endif
923
924 cb->state = SELECT_DONE;
925 return;
926 }
927
928 row = check_row(row);
929 col = check_col(col);
930#ifdef FEAT_MBYTE
931 col = mb_fix_col(col, row);
932#endif
933
934 if (col == (int)cb->prev.col && row == cb->prev.lnum && !repeated_click)
935 return;
936
937 /*
938 * When extending the selection with the right mouse button, swap the
939 * start and end if the position is before half the selection
940 */
941 if (cb->state == SELECT_DONE && button == MOUSE_RIGHT)
942 {
943 /*
944 * If the click is before the start, or the click is inside the
945 * selection and the start is the closest side, set the origin to the
946 * end of the selection.
947 */
948 if (clip_compare_pos(row, col, (int)cb->start.lnum, cb->start.col) < 0
949 || (clip_compare_pos(row, col,
950 (int)cb->end.lnum, cb->end.col) < 0
951 && (((cb->start.lnum == cb->end.lnum
952 && cb->end.col - col > col - cb->start.col))
953 || ((diff = (cb->end.lnum - row) -
954 (row - cb->start.lnum)) > 0
955 || (diff == 0 && col < (int)(cb->start.col +
956 cb->end.col) / 2)))))
957 {
958 cb->origin_row = (short_u)cb->end.lnum;
959 cb->origin_start_col = cb->end.col - 1;
960 cb->origin_end_col = cb->end.col;
961 }
962 else
963 {
964 cb->origin_row = (short_u)cb->start.lnum;
965 cb->origin_start_col = cb->start.col;
966 cb->origin_end_col = cb->start.col;
967 }
968 if (cb->mode == SELECT_MODE_WORD && !repeated_click)
969 cb->mode = SELECT_MODE_CHAR;
970 }
971
972 /* set state, for when using the right mouse button */
973 cb->state = SELECT_IN_PROGRESS;
974
975#ifdef DEBUG_SELECTION
976 printf("Selection extending to (%d,%d)\n", row, col);
977#endif
978
979 if (repeated_click && ++cb->mode > SELECT_MODE_LINE)
980 cb->mode = SELECT_MODE_CHAR;
981
982 switch (cb->mode)
983 {
984 case SELECT_MODE_CHAR:
985 /* If we're on a different line, find where the line ends */
986 if (row != cb->prev.lnum)
987 cb->word_end_col = clip_get_line_end(row);
988
989 /* See if we are before or after the origin of the selection */
990 if (clip_compare_pos(row, col, cb->origin_row,
991 cb->origin_start_col) >= 0)
992 {
993 if (col >= (int)cb->word_end_col)
994 clip_update_modeless_selection(cb, cb->origin_row,
995 cb->origin_start_col, row, (int)Columns);
996 else
997 {
998#ifdef FEAT_MBYTE
999 if (has_mbyte && mb_lefthalve(row, col))
1000 slen = 2;
1001#endif
1002 clip_update_modeless_selection(cb, cb->origin_row,
1003 cb->origin_start_col, row, col + slen);
1004 }
1005 }
1006 else
1007 {
1008#ifdef FEAT_MBYTE
1009 if (has_mbyte
1010 && mb_lefthalve(cb->origin_row, cb->origin_start_col))
1011 slen = 2;
1012#endif
1013 if (col >= (int)cb->word_end_col)
1014 clip_update_modeless_selection(cb, row, cb->word_end_col,
1015 cb->origin_row, cb->origin_start_col + slen);
1016 else
1017 clip_update_modeless_selection(cb, row, col,
1018 cb->origin_row, cb->origin_start_col + slen);
1019 }
1020 break;
1021
1022 case SELECT_MODE_WORD:
1023 /* If we are still within the same word, do nothing */
1024 if (row == cb->prev.lnum && col >= (int)cb->word_start_col
1025 && col < (int)cb->word_end_col && !repeated_click)
1026 return;
1027
1028 /* Get new word boundaries */
1029 clip_get_word_boundaries(cb, row, col);
1030
1031 /* Handle being after the origin point of selection */
1032 if (clip_compare_pos(row, col, cb->origin_row,
1033 cb->origin_start_col) >= 0)
1034 clip_update_modeless_selection(cb, cb->origin_row,
1035 cb->origin_start_col, row, cb->word_end_col);
1036 else
1037 clip_update_modeless_selection(cb, row, cb->word_start_col,
1038 cb->origin_row, cb->origin_end_col);
1039 break;
1040
1041 case SELECT_MODE_LINE:
1042 if (row == cb->prev.lnum && !repeated_click)
1043 return;
1044
1045 if (clip_compare_pos(row, col, cb->origin_row,
1046 cb->origin_start_col) >= 0)
1047 clip_update_modeless_selection(cb, cb->origin_row, 0, row,
1048 (int)Columns);
1049 else
1050 clip_update_modeless_selection(cb, row, 0, cb->origin_row,
1051 (int)Columns);
1052 break;
1053 }
1054
1055 cb->prev.lnum = row;
1056 cb->prev.col = col;
1057
1058#ifdef DEBUG_SELECTION
1059 printf("Selection is: (%u,%u) to (%u,%u)\n", cb->start.lnum,
1060 cb->start.col, cb->end.lnum, cb->end.col);
1061#endif
1062}
1063
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064# if defined(FEAT_GUI) || defined(PROTO)
1065/*
1066 * Redraw part of the selection if character at "row,col" is inside of it.
1067 * Only used for the GUI.
1068 */
1069 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001070clip_may_redraw_selection(int row, int col, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071{
1072 int start = col;
1073 int end = col + len;
1074
1075 if (clip_star.state != SELECT_CLEARED
1076 && row >= clip_star.start.lnum
1077 && row <= clip_star.end.lnum)
1078 {
1079 if (row == clip_star.start.lnum && start < (int)clip_star.start.col)
1080 start = clip_star.start.col;
1081 if (row == clip_star.end.lnum && end > (int)clip_star.end.col)
1082 end = clip_star.end.col;
1083 if (end > start)
1084 clip_invert_area(row, start, row, end, 0);
1085 }
1086}
1087# endif
1088
1089/*
1090 * Called from outside to clear selected region from the display
1091 */
1092 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001093clip_clear_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001096 if (cbd->state == SELECT_CLEARED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 return;
1098
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001099 clip_invert_area((int)cbd->start.lnum, cbd->start.col, (int)cbd->end.lnum,
1100 cbd->end.col, CLIP_CLEAR);
1101 cbd->state = SELECT_CLEARED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102}
1103
1104/*
1105 * Clear the selection if any lines from "row1" to "row2" are inside of it.
1106 */
1107 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001108clip_may_clear_selection(int row1, int row2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109{
1110 if (clip_star.state == SELECT_DONE
1111 && row2 >= clip_star.start.lnum
1112 && row1 <= clip_star.end.lnum)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001113 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114}
1115
1116/*
1117 * Called before the screen is scrolled up or down. Adjusts the line numbers
1118 * of the selection. Call with big number when clearing the screen.
1119 */
1120 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001121clip_scroll_selection(
1122 int rows) /* negative for scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123{
1124 int lnum;
1125
1126 if (clip_star.state == SELECT_CLEARED)
1127 return;
1128
1129 lnum = clip_star.start.lnum - rows;
1130 if (lnum <= 0)
1131 clip_star.start.lnum = 0;
1132 else if (lnum >= screen_Rows) /* scrolled off of the screen */
1133 clip_star.state = SELECT_CLEARED;
1134 else
1135 clip_star.start.lnum = lnum;
1136
1137 lnum = clip_star.end.lnum - rows;
1138 if (lnum < 0) /* scrolled off of the screen */
1139 clip_star.state = SELECT_CLEARED;
1140 else if (lnum >= screen_Rows)
1141 clip_star.end.lnum = screen_Rows - 1;
1142 else
1143 clip_star.end.lnum = lnum;
1144}
1145
1146/*
1147 * Invert a region of the display between a starting and ending row and column
1148 * Values for "how":
1149 * CLIP_CLEAR: undo inversion
1150 * CLIP_SET: set inversion
1151 * CLIP_TOGGLE: set inversion if pos1 < pos2, undo inversion otherwise.
1152 * 0: invert (GUI only).
1153 */
1154 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001155clip_invert_area(
1156 int row1,
1157 int col1,
1158 int row2,
1159 int col2,
1160 int how)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161{
1162 int invert = FALSE;
1163
1164 if (how == CLIP_SET)
1165 invert = TRUE;
1166
1167 /* Swap the from and to positions so the from is always before */
1168 if (clip_compare_pos(row1, col1, row2, col2) > 0)
1169 {
1170 int tmp_row, tmp_col;
1171
1172 tmp_row = row1;
1173 tmp_col = col1;
1174 row1 = row2;
1175 col1 = col2;
1176 row2 = tmp_row;
1177 col2 = tmp_col;
1178 }
1179 else if (how == CLIP_TOGGLE)
1180 invert = TRUE;
1181
1182 /* If all on the same line, do it the easy way */
1183 if (row1 == row2)
1184 {
1185 clip_invert_rectangle(row1, col1, 1, col2 - col1, invert);
1186 }
1187 else
1188 {
1189 /* Handle a piece of the first line */
1190 if (col1 > 0)
1191 {
1192 clip_invert_rectangle(row1, col1, 1, (int)Columns - col1, invert);
1193 row1++;
1194 }
1195
1196 /* Handle a piece of the last line */
1197 if (col2 < Columns - 1)
1198 {
1199 clip_invert_rectangle(row2, 0, 1, col2, invert);
1200 row2--;
1201 }
1202
1203 /* Handle the rectangle thats left */
1204 if (row2 >= row1)
1205 clip_invert_rectangle(row1, 0, row2 - row1 + 1, (int)Columns,
1206 invert);
1207 }
1208}
1209
1210/*
1211 * Invert or un-invert a rectangle of the screen.
1212 * "invert" is true if the result is inverted.
1213 */
1214 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001215clip_invert_rectangle(
1216 int row,
1217 int col,
1218 int height,
1219 int width,
1220 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221{
1222#ifdef FEAT_GUI
1223 if (gui.in_use)
1224 gui_mch_invert_rectangle(row, col, height, width);
1225 else
1226#endif
1227 screen_draw_rectangle(row, col, height, width, invert);
1228}
1229
1230/*
1231 * Copy the currently selected area into the '*' register so it will be
1232 * available for pasting.
1233 * When "both" is TRUE also copy to the '+' register.
1234 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001236clip_copy_modeless_selection(int both UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237{
1238 char_u *buffer;
1239 char_u *bufp;
1240 int row;
1241 int start_col;
1242 int end_col;
1243 int line_end_col;
1244 int add_newline_flag = FALSE;
1245 int len;
1246#ifdef FEAT_MBYTE
1247 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248#endif
1249 int row1 = clip_star.start.lnum;
1250 int col1 = clip_star.start.col;
1251 int row2 = clip_star.end.lnum;
1252 int col2 = clip_star.end.col;
1253
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001254 /* Can't use ScreenLines unless initialized */
1255 if (ScreenLines == NULL)
1256 return;
1257
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 /*
1259 * Make sure row1 <= row2, and if row1 == row2 that col1 <= col2.
1260 */
1261 if (row1 > row2)
1262 {
1263 row = row1; row1 = row2; row2 = row;
1264 row = col1; col1 = col2; col2 = row;
1265 }
1266 else if (row1 == row2 && col1 > col2)
1267 {
1268 row = col1; col1 = col2; col2 = row;
1269 }
1270#ifdef FEAT_MBYTE
1271 /* correct starting point for being on right halve of double-wide char */
1272 p = ScreenLines + LineOffset[row1];
1273 if (enc_dbcs != 0)
1274 col1 -= (*mb_head_off)(p, p + col1);
1275 else if (enc_utf8 && p[col1] == 0)
1276 --col1;
1277#endif
1278
1279 /* Create a temporary buffer for storing the text */
1280 len = (row2 - row1 + 1) * Columns + 1;
1281#ifdef FEAT_MBYTE
1282 if (enc_dbcs != 0)
1283 len *= 2; /* max. 2 bytes per display cell */
1284 else if (enc_utf8)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001285 len *= MB_MAXBYTES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286#endif
1287 buffer = lalloc((long_u)len, TRUE);
1288 if (buffer == NULL) /* out of memory */
1289 return;
1290
1291 /* Process each row in the selection */
1292 for (bufp = buffer, row = row1; row <= row2; row++)
1293 {
1294 if (row == row1)
1295 start_col = col1;
1296 else
1297 start_col = 0;
1298
1299 if (row == row2)
1300 end_col = col2;
1301 else
1302 end_col = Columns;
1303
1304 line_end_col = clip_get_line_end(row);
1305
1306 /* See if we need to nuke some trailing whitespace */
1307 if (end_col >= Columns && (row < row2 || end_col > line_end_col))
1308 {
1309 /* Get rid of trailing whitespace */
1310 end_col = line_end_col;
1311 if (end_col < start_col)
1312 end_col = start_col;
1313
1314 /* If the last line extended to the end, add an extra newline */
1315 if (row == row2)
1316 add_newline_flag = TRUE;
1317 }
1318
1319 /* If after the first row, we need to always add a newline */
1320 if (row > row1 && !LineWraps[row - 1])
1321 *bufp++ = NL;
1322
1323 if (row < screen_Rows && end_col <= screen_Columns)
1324 {
1325#ifdef FEAT_MBYTE
1326 if (enc_dbcs != 0)
1327 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001328 int i;
1329
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 p = ScreenLines + LineOffset[row];
1331 for (i = start_col; i < end_col; ++i)
1332 if (enc_dbcs == DBCS_JPNU && p[i] == 0x8e)
1333 {
1334 /* single-width double-byte char */
1335 *bufp++ = 0x8e;
1336 *bufp++ = ScreenLines2[LineOffset[row] + i];
1337 }
1338 else
1339 {
1340 *bufp++ = p[i];
1341 if (MB_BYTE2LEN(p[i]) == 2)
1342 *bufp++ = p[++i];
1343 }
1344 }
1345 else if (enc_utf8)
1346 {
1347 int off;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001348 int i;
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001349 int ci;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350
1351 off = LineOffset[row];
1352 for (i = start_col; i < end_col; ++i)
1353 {
1354 /* The base character is either in ScreenLinesUC[] or
1355 * ScreenLines[]. */
1356 if (ScreenLinesUC[off + i] == 0)
1357 *bufp++ = ScreenLines[off + i];
1358 else
1359 {
1360 bufp += utf_char2bytes(ScreenLinesUC[off + i], bufp);
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001361 for (ci = 0; ci < Screen_mco; ++ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001363 /* Add a composing character. */
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001364 if (ScreenLinesC[ci][off + i] == 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001365 break;
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001366 bufp += utf_char2bytes(ScreenLinesC[ci][off + i],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 }
1369 }
1370 /* Skip right halve of double-wide character. */
1371 if (ScreenLines[off + i + 1] == 0)
1372 ++i;
1373 }
1374 }
1375 else
1376#endif
1377 {
1378 STRNCPY(bufp, ScreenLines + LineOffset[row] + start_col,
1379 end_col - start_col);
1380 bufp += end_col - start_col;
1381 }
1382 }
1383 }
1384
1385 /* Add a newline at the end if the selection ended there */
1386 if (add_newline_flag)
1387 *bufp++ = NL;
1388
1389 /* First cleanup any old selection and become the owner. */
1390 clip_free_selection(&clip_star);
1391 clip_own_selection(&clip_star);
1392
1393 /* Yank the text into the '*' register. */
1394 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_star);
1395
1396 /* Make the register contents available to the outside world. */
1397 clip_gen_set_selection(&clip_star);
1398
1399#ifdef FEAT_X11
1400 if (both)
1401 {
1402 /* Do the same for the '+' register. */
1403 clip_free_selection(&clip_plus);
1404 clip_own_selection(&clip_plus);
1405 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_plus);
1406 clip_gen_set_selection(&clip_plus);
1407 }
1408#endif
1409 vim_free(buffer);
1410}
1411
1412/*
1413 * Find the starting and ending positions of the word at the given row and
1414 * column. Only white-separated words are recognized here.
1415 */
1416#define CHAR_CLASS(c) (c <= ' ' ? ' ' : vim_iswordc(c))
1417
1418 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001419clip_get_word_boundaries(VimClipboard *cb, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420{
1421 int start_class;
1422 int temp_col;
1423 char_u *p;
1424#ifdef FEAT_MBYTE
1425 int mboff;
1426#endif
1427
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001428 if (row >= screen_Rows || col >= screen_Columns || ScreenLines == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 return;
1430
1431 p = ScreenLines + LineOffset[row];
1432#ifdef FEAT_MBYTE
1433 /* Correct for starting in the right halve of a double-wide char */
1434 if (enc_dbcs != 0)
1435 col -= dbcs_screen_head_off(p, p + col);
1436 else if (enc_utf8 && p[col] == 0)
1437 --col;
1438#endif
1439 start_class = CHAR_CLASS(p[col]);
1440
1441 temp_col = col;
1442 for ( ; temp_col > 0; temp_col--)
1443#ifdef FEAT_MBYTE
1444 if (enc_dbcs != 0
1445 && (mboff = dbcs_screen_head_off(p, p + temp_col - 1)) > 0)
1446 temp_col -= mboff;
1447 else
1448#endif
1449 if (CHAR_CLASS(p[temp_col - 1]) != start_class
1450#ifdef FEAT_MBYTE
1451 && !(enc_utf8 && p[temp_col - 1] == 0)
1452#endif
1453 )
1454 break;
1455 cb->word_start_col = temp_col;
1456
1457 temp_col = col;
1458 for ( ; temp_col < screen_Columns; temp_col++)
1459#ifdef FEAT_MBYTE
1460 if (enc_dbcs != 0 && dbcs_ptr2cells(p + temp_col) == 2)
1461 ++temp_col;
1462 else
1463#endif
1464 if (CHAR_CLASS(p[temp_col]) != start_class
1465#ifdef FEAT_MBYTE
1466 && !(enc_utf8 && p[temp_col] == 0)
1467#endif
1468 )
1469 break;
1470 cb->word_end_col = temp_col;
1471}
1472
1473/*
1474 * Find the column position for the last non-whitespace character on the given
1475 * line.
1476 */
1477 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001478clip_get_line_end(int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479{
1480 int i;
1481
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001482 if (row >= screen_Rows || ScreenLines == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 return 0;
1484 for (i = screen_Columns; i > 0; i--)
1485 if (ScreenLines[LineOffset[row] + i - 1] != ' ')
1486 break;
1487 return i;
1488}
1489
1490/*
1491 * Update the currently selected region by adding and/or subtracting from the
1492 * beginning or end and inverting the changed area(s).
1493 */
1494 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001495clip_update_modeless_selection(
1496 VimClipboard *cb,
1497 int row1,
1498 int col1,
1499 int row2,
1500 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501{
1502 /* See if we changed at the beginning of the selection */
1503 if (row1 != cb->start.lnum || col1 != (int)cb->start.col)
1504 {
1505 clip_invert_area(row1, col1, (int)cb->start.lnum, cb->start.col,
1506 CLIP_TOGGLE);
1507 cb->start.lnum = row1;
1508 cb->start.col = col1;
1509 }
1510
1511 /* See if we changed at the end of the selection */
1512 if (row2 != cb->end.lnum || col2 != (int)cb->end.col)
1513 {
1514 clip_invert_area((int)cb->end.lnum, cb->end.col, row2, col2,
1515 CLIP_TOGGLE);
1516 cb->end.lnum = row2;
1517 cb->end.col = col2;
1518 }
1519}
1520
1521 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001522clip_gen_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523{
1524#ifdef FEAT_XCLIPBOARD
1525# ifdef FEAT_GUI
1526 if (gui.in_use)
1527 return clip_mch_own_selection(cbd);
1528 else
1529# endif
1530 return clip_xterm_own_selection(cbd);
1531#else
1532 return clip_mch_own_selection(cbd);
1533#endif
1534}
1535
1536 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001537clip_gen_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538{
1539#ifdef FEAT_XCLIPBOARD
1540# ifdef FEAT_GUI
1541 if (gui.in_use)
1542 clip_mch_lose_selection(cbd);
1543 else
1544# endif
1545 clip_xterm_lose_selection(cbd);
1546#else
1547 clip_mch_lose_selection(cbd);
1548#endif
1549}
1550
1551 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001552clip_gen_set_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553{
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001554 if (!clip_did_set_selection)
1555 {
1556 /* Updating postponed, so that accessing the system clipboard won't
1557 * hang Vim when accessing it many times (e.g. on a :g comand). */
Bram Moolenaar5c27fd12015-01-27 14:09:37 +01001558 if ((cbd == &clip_plus && (clip_unnamed_saved & CLIP_UNNAMED_PLUS))
1559 || (cbd == &clip_star && (clip_unnamed_saved & CLIP_UNNAMED)))
1560 {
1561 clipboard_needs_update = TRUE;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001562 return;
Bram Moolenaar5c27fd12015-01-27 14:09:37 +01001563 }
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565#ifdef FEAT_XCLIPBOARD
1566# ifdef FEAT_GUI
1567 if (gui.in_use)
1568 clip_mch_set_selection(cbd);
1569 else
1570# endif
1571 clip_xterm_set_selection(cbd);
1572#else
1573 clip_mch_set_selection(cbd);
1574#endif
1575}
1576
1577 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001578clip_gen_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579{
1580#ifdef FEAT_XCLIPBOARD
1581# ifdef FEAT_GUI
1582 if (gui.in_use)
1583 clip_mch_request_selection(cbd);
1584 else
1585# endif
1586 clip_xterm_request_selection(cbd);
1587#else
1588 clip_mch_request_selection(cbd);
1589#endif
1590}
1591
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001592 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001593clip_gen_owner_exists(VimClipboard *cbd UNUSED)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001594{
1595#ifdef FEAT_XCLIPBOARD
1596# ifdef FEAT_GUI_GTK
1597 if (gui.in_use)
1598 return clip_gtk_owner_exists(cbd);
1599 else
1600# endif
1601 return clip_x11_owner_exists(cbd);
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02001602#else
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001603 return TRUE;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02001604#endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001605}
1606
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607#endif /* FEAT_CLIPBOARD */
1608
1609/*****************************************************************************
1610 * Functions that handle the input buffer.
1611 * This is used for any GUI version, and the unix terminal version.
1612 *
1613 * For Unix, the input characters are buffered to be able to check for a
1614 * CTRL-C. This should be done with signals, but I don't know how to do that
1615 * in a portable way for a tty in RAW mode.
1616 *
1617 * For the client-server code in the console the received keys are put in the
1618 * input buffer.
1619 */
1620
1621#if defined(USE_INPUT_BUF) || defined(PROTO)
1622
1623/*
1624 * Internal typeahead buffer. Includes extra space for long key code
1625 * descriptions which would otherwise overflow. The buffer is considered full
1626 * when only this extra space (or part of it) remains.
1627 */
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001628#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_CLIENTSERVER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 /*
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001630 * NetBeans stuffs debugger commands into the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 * This requires a larger buffer...
1632 * (Madsen) Go with this for remote input as well ...
1633 */
1634# define INBUFLEN 4096
1635#else
1636# define INBUFLEN 250
1637#endif
1638
1639static char_u inbuf[INBUFLEN + MAX_KEY_CODE_LEN];
1640static int inbufcount = 0; /* number of chars in inbuf[] */
1641
1642/*
1643 * vim_is_input_buf_full(), vim_is_input_buf_empty(), add_to_input_buf(), and
1644 * trash_input_buf() are functions for manipulating the input buffer. These
1645 * are used by the gui_* calls when a GUI is used to handle keyboard input.
1646 */
1647
1648 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001649vim_is_input_buf_full(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650{
1651 return (inbufcount >= INBUFLEN);
1652}
1653
1654 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001655vim_is_input_buf_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656{
1657 return (inbufcount == 0);
1658}
1659
1660#if defined(FEAT_OLE) || defined(PROTO)
1661 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001662vim_free_in_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663{
1664 return (INBUFLEN - inbufcount);
1665}
1666#endif
1667
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001668#if defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001670vim_used_in_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671{
1672 return inbufcount;
1673}
1674#endif
1675
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676/*
1677 * Return the current contents of the input buffer and make it empty.
1678 * The returned pointer must be passed to set_input_buf() later.
1679 */
1680 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001681get_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682{
1683 garray_T *gap;
1684
1685 /* We use a growarray to store the data pointer and the length. */
1686 gap = (garray_T *)alloc((unsigned)sizeof(garray_T));
1687 if (gap != NULL)
1688 {
1689 /* Add one to avoid a zero size. */
1690 gap->ga_data = alloc((unsigned)inbufcount + 1);
1691 if (gap->ga_data != NULL)
1692 mch_memmove(gap->ga_data, inbuf, (size_t)inbufcount);
1693 gap->ga_len = inbufcount;
1694 }
1695 trash_input_buf();
1696 return (char_u *)gap;
1697}
1698
1699/*
1700 * Restore the input buffer with a pointer returned from get_input_buf().
1701 * The allocated memory is freed, this only works once!
1702 */
1703 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001704set_input_buf(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705{
1706 garray_T *gap = (garray_T *)p;
1707
1708 if (gap != NULL)
1709 {
1710 if (gap->ga_data != NULL)
1711 {
1712 mch_memmove(inbuf, gap->ga_data, gap->ga_len);
1713 inbufcount = gap->ga_len;
1714 vim_free(gap->ga_data);
1715 }
1716 vim_free(gap);
1717 }
1718}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720/*
1721 * Add the given bytes to the input buffer
1722 * Special keys start with CSI. A real CSI must have been translated to
1723 * CSI KS_EXTRA KE_CSI. K_SPECIAL doesn't require translation.
1724 */
1725 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001726add_to_input_buf(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727{
1728 if (inbufcount + len > INBUFLEN + MAX_KEY_CODE_LEN)
1729 return; /* Shouldn't ever happen! */
1730
1731#ifdef FEAT_HANGULIN
1732 if ((State & (INSERT|CMDLINE)) && hangul_input_state_get())
1733 if ((len = hangul_input_process(s, len)) == 0)
1734 return;
1735#endif
1736
1737 while (len--)
1738 inbuf[inbufcount++] = *s++;
1739}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741/*
1742 * Add "str[len]" to the input buffer while escaping CSI bytes.
1743 */
1744 void
1745add_to_input_buf_csi(char_u *str, int len)
1746{
1747 int i;
1748 char_u buf[2];
1749
1750 for (i = 0; i < len; ++i)
1751 {
1752 add_to_input_buf(str + i, 1);
1753 if (str[i] == CSI)
1754 {
1755 /* Turn CSI into K_CSI. */
1756 buf[0] = KS_EXTRA;
1757 buf[1] = (int)KE_CSI;
1758 add_to_input_buf(buf, 2);
1759 }
1760 }
1761}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762
1763#if defined(FEAT_HANGULIN) || defined(PROTO)
1764 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001765push_raw_key(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766{
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001767 char_u *tmpbuf;
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001768 char_u *inp = s;
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001769
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001770 /* use the conversion result if possible */
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001771 tmpbuf = hangul_string_convert(s, &len);
1772 if (tmpbuf != NULL)
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001773 inp = tmpbuf;
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001774
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001775 for (; len--; inp++)
1776 {
1777 inbuf[inbufcount++] = *inp;
1778 if (*inp == CSI)
Bram Moolenaar00ded432016-03-03 11:45:15 +01001779 {
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001780 /* Turn CSI into K_CSI. */
1781 inbuf[inbufcount++] = KS_EXTRA;
1782 inbuf[inbufcount++] = (int)KE_CSI;
Bram Moolenaar00ded432016-03-03 11:45:15 +01001783 }
Bram Moolenaar00ded432016-03-03 11:45:15 +01001784 }
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001785 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786}
1787#endif
1788
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789/* Remove everything from the input buffer. Called when ^C is found */
1790 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001791trash_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792{
1793 inbufcount = 0;
1794}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
1796/*
1797 * Read as much data from the input buffer as possible up to maxlen, and store
1798 * it in buf.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 */
1800 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001801read_from_input_buf(char_u *buf, long maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802{
1803 if (inbufcount == 0) /* if the buffer is empty, fill it */
1804 fill_input_buf(TRUE);
1805 if (maxlen > inbufcount)
1806 maxlen = inbufcount;
1807 mch_memmove(buf, inbuf, (size_t)maxlen);
1808 inbufcount -= maxlen;
1809 if (inbufcount)
1810 mch_memmove(inbuf, inbuf + maxlen, (size_t)inbufcount);
1811 return (int)maxlen;
1812}
1813
1814 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001815fill_input_buf(int exit_on_error UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816{
Bram Moolenaard0573012017-10-28 21:11:06 +02001817#if defined(UNIX) || defined(VMS) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 int len;
1819 int try;
1820 static int did_read_something = FALSE;
1821# ifdef FEAT_MBYTE
1822 static char_u *rest = NULL; /* unconverted rest of previous read */
1823 static int restlen = 0;
1824 int unconverted;
1825# endif
1826#endif
1827
1828#ifdef FEAT_GUI
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001829 if (gui.in_use
1830# ifdef NO_CONSOLE_INPUT
1831 /* Don't use the GUI input when the window hasn't been opened yet.
1832 * We get here from ui_inchar() when we should try reading from stdin. */
1833 && !no_console_input()
1834# endif
1835 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 {
1837 gui_mch_update();
1838 return;
1839 }
1840#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02001841#if defined(UNIX) || defined(VMS) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 if (vim_is_input_buf_full())
1843 return;
1844 /*
1845 * Fill_input_buf() is only called when we really need a character.
1846 * If we can't get any, but there is some in the buffer, just return.
1847 * If we can't get any, and there isn't any in the buffer, we give up and
1848 * exit Vim.
1849 */
1850# ifdef __BEOS__
1851 /*
1852 * On the BeBox version (for now), all input is secretly performed within
1853 * beos_select() which is called from RealWaitForChar().
1854 */
1855 while (!vim_is_input_buf_full() && RealWaitForChar(read_cmd_fd, 0, NULL))
1856 ;
1857 len = inbufcount;
1858 inbufcount = 0;
1859# else
1860
Bram Moolenaar85b11762016-02-27 18:13:23 +01001861# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 if (rest != NULL)
1863 {
1864 /* Use remainder of previous call, starts with an invalid character
1865 * that may become valid when reading more. */
1866 if (restlen > INBUFLEN - inbufcount)
1867 unconverted = INBUFLEN - inbufcount;
1868 else
1869 unconverted = restlen;
1870 mch_memmove(inbuf + inbufcount, rest, unconverted);
1871 if (unconverted == restlen)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001872 VIM_CLEAR(rest);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 else
1874 {
1875 restlen -= unconverted;
1876 mch_memmove(rest, rest + unconverted, restlen);
1877 }
1878 inbufcount += unconverted;
1879 }
1880 else
1881 unconverted = 0;
Bram Moolenaar85b11762016-02-27 18:13:23 +01001882# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883
1884 len = 0; /* to avoid gcc warning */
1885 for (try = 0; try < 100; ++try)
1886 {
Bram Moolenaar4e601e32018-04-24 13:29:51 +02001887 size_t readlen = (size_t)((INBUFLEN - inbufcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888# ifdef FEAT_MBYTE
Bram Moolenaar4e601e32018-04-24 13:29:51 +02001889 / input_conv.vc_factor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890# endif
Bram Moolenaar4e601e32018-04-24 13:29:51 +02001891 );
1892# ifdef VMS
Bram Moolenaar49943732018-04-24 20:27:26 +02001893 len = vms_read((char *)inbuf + inbufcount, readlen);
Bram Moolenaar4e601e32018-04-24 13:29:51 +02001894# else
1895 len = read(read_cmd_fd, (char *)inbuf + inbufcount, readlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896# endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001897
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 if (len > 0 || got_int)
1899 break;
1900 /*
1901 * If reading stdin results in an error, continue reading stderr.
1902 * This helps when using "foo | xargs vim".
1903 */
1904 if (!did_read_something && !isatty(read_cmd_fd) && read_cmd_fd == 0)
1905 {
1906 int m = cur_tmode;
1907
1908 /* We probably set the wrong file descriptor to raw mode. Switch
1909 * back to cooked mode, use another descriptor and set the mode to
1910 * what it was. */
1911 settmode(TMODE_COOK);
1912#ifdef HAVE_DUP
1913 /* Use stderr for stdin, also works for shell commands. */
1914 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02001915 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916#else
1917 read_cmd_fd = 2; /* read from stderr instead of stdin */
1918#endif
1919 settmode(m);
1920 }
1921 if (!exit_on_error)
1922 return;
1923 }
1924# endif
1925 if (len <= 0 && !got_int)
1926 read_error_exit();
1927 if (len > 0)
1928 did_read_something = TRUE;
1929 if (got_int)
1930 {
1931 /* Interrupted, pretend a CTRL-C was typed. */
1932 inbuf[0] = 3;
1933 inbufcount = 1;
1934 }
1935 else
1936 {
1937# ifdef FEAT_MBYTE
1938 /*
1939 * May perform conversion on the input characters.
1940 * Include the unconverted rest of the previous call.
1941 * If there is an incomplete char at the end it is kept for the next
1942 * time, reading more bytes should make conversion possible.
1943 * Don't do this in the unlikely event that the input buffer is too
1944 * small ("rest" still contains more bytes).
1945 */
1946 if (input_conv.vc_type != CONV_NONE)
1947 {
1948 inbufcount -= unconverted;
1949 len = convert_input_safe(inbuf + inbufcount,
1950 len + unconverted, INBUFLEN - inbufcount,
1951 rest == NULL ? &rest : NULL, &restlen);
1952 }
1953# endif
1954 while (len-- > 0)
1955 {
1956 /*
1957 * if a CTRL-C was typed, remove it from the buffer and set got_int
1958 */
1959 if (inbuf[inbufcount] == 3 && ctrl_c_interrupts)
1960 {
1961 /* remove everything typed before the CTRL-C */
1962 mch_memmove(inbuf, inbuf + inbufcount, (size_t)(len + 1));
1963 inbufcount = 0;
1964 got_int = TRUE;
1965 }
1966 ++inbufcount;
1967 }
1968 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001969#endif /* UNIX or VMS*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970}
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001971#endif /* defined(UNIX) || defined(FEAT_GUI) || defined(VMS) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972
1973/*
1974 * Exit because of an input read error.
1975 */
1976 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001977read_error_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978{
1979 if (silent_mode) /* Normal way to exit for "ex -s" */
1980 getout(0);
1981 STRCPY(IObuff, _("Vim: Error reading input, exiting...\n"));
1982 preserve_exit();
1983}
1984
1985#if defined(CURSOR_SHAPE) || defined(PROTO)
1986/*
1987 * May update the shape of the cursor.
1988 */
1989 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001990ui_cursor_shape_forced(int forced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991{
1992# ifdef FEAT_GUI
1993 if (gui.in_use)
1994 gui_update_cursor_later();
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001995 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001997 term_cursor_mode(forced);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001998
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999# ifdef MCH_CURSOR_SHAPE
2000 mch_update_cursor();
2001# endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002002
2003# ifdef FEAT_CONCEAL
Bram Moolenaarb9464822018-05-10 15:09:49 +02002004 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002005# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002007
2008 void
2009ui_cursor_shape(void)
2010{
2011 ui_cursor_shape_forced(FALSE);
2012}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013#endif
2014
2015#if defined(FEAT_CLIPBOARD) || defined(FEAT_GUI) || defined(FEAT_RIGHTLEFT) \
Bram Moolenaaraf51e662008-07-14 19:48:05 +00002016 || defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017/*
2018 * Check bounds for column number
2019 */
2020 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002021check_col(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022{
2023 if (col < 0)
2024 return 0;
2025 if (col >= (int)screen_Columns)
2026 return (int)screen_Columns - 1;
2027 return col;
2028}
2029
2030/*
2031 * Check bounds for row number
2032 */
2033 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002034check_row(int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035{
2036 if (row < 0)
2037 return 0;
2038 if (row >= (int)screen_Rows)
2039 return (int)screen_Rows - 1;
2040 return row;
2041}
2042#endif
2043
2044/*
2045 * Stuff for the X clipboard. Shared between VMS and Unix.
2046 */
2047
2048#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) || defined(PROTO)
2049# include <X11/Xatom.h>
2050# include <X11/Intrinsic.h>
2051
2052/*
2053 * Open the application context (if it hasn't been opened yet).
2054 * Used for Motif and Athena GUI and the xterm clipboard.
2055 */
2056 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002057open_app_context(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058{
2059 if (app_context == NULL)
2060 {
2061 XtToolkitInitialize();
2062 app_context = XtCreateApplicationContext();
2063 }
2064}
2065
2066static Atom vim_atom; /* Vim's own special selection format */
2067#ifdef FEAT_MBYTE
2068static Atom vimenc_atom; /* Vim's extended selection format */
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002069static Atom utf8_atom;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070#endif
2071static Atom compound_text_atom;
2072static Atom text_atom;
2073static Atom targets_atom;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002074static Atom timestamp_atom; /* Used to get a timestamp */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075
2076 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002077x11_setup_atoms(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078{
2079 vim_atom = XInternAtom(dpy, VIM_ATOM_NAME, False);
2080#ifdef FEAT_MBYTE
2081 vimenc_atom = XInternAtom(dpy, VIMENC_ATOM_NAME,False);
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002082 utf8_atom = XInternAtom(dpy, "UTF8_STRING", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083#endif
2084 compound_text_atom = XInternAtom(dpy, "COMPOUND_TEXT", False);
2085 text_atom = XInternAtom(dpy, "TEXT", False);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002086 targets_atom = XInternAtom(dpy, "TARGETS", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 clip_star.sel_atom = XA_PRIMARY;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002088 clip_plus.sel_atom = XInternAtom(dpy, "CLIPBOARD", False);
2089 timestamp_atom = XInternAtom(dpy, "TIMESTAMP", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090}
2091
2092/*
2093 * X Selection stuff, for cutting and pasting text to other windows.
2094 */
2095
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002096static Boolean clip_x11_convert_selection_cb(Widget w, Atom *sel_atom, Atom *target, Atom *type, XtPointer *value, long_u *length, int *format);
2097static void clip_x11_lose_ownership_cb(Widget w, Atom *sel_atom);
2098static void clip_x11_notify_cb(Widget w, Atom *sel_atom, Atom *target);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002099
2100/*
2101 * Property callback to get a timestamp for XtOwnSelection.
2102 */
2103 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002104clip_x11_timestamp_cb(
2105 Widget w,
2106 XtPointer n UNUSED,
2107 XEvent *event,
2108 Boolean *cont UNUSED)
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002109{
2110 Atom actual_type;
2111 int format;
2112 unsigned long nitems, bytes_after;
2113 unsigned char *prop=NULL;
2114 XPropertyEvent *xproperty=&event->xproperty;
2115
2116 /* Must be a property notify, state can't be Delete (True), has to be
2117 * one of the supported selection types. */
2118 if (event->type != PropertyNotify || xproperty->state
2119 || (xproperty->atom != clip_star.sel_atom
2120 && xproperty->atom != clip_plus.sel_atom))
2121 return;
2122
2123 if (XGetWindowProperty(xproperty->display, xproperty->window,
2124 xproperty->atom, 0, 0, False, timestamp_atom, &actual_type, &format,
2125 &nitems, &bytes_after, &prop))
2126 return;
2127
2128 if (prop)
2129 XFree(prop);
2130
2131 /* Make sure the property type is "TIMESTAMP" and it's 32 bits. */
2132 if (actual_type != timestamp_atom || format != 32)
2133 return;
2134
2135 /* Get the selection, using the event timestamp. */
Bram Moolenaar62b42182010-09-21 22:09:37 +02002136 if (XtOwnSelection(w, xproperty->atom, xproperty->time,
2137 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002138 clip_x11_notify_cb) == OK)
Bram Moolenaar62b42182010-09-21 22:09:37 +02002139 {
2140 /* Set the "owned" flag now, there may have been a call to
2141 * lose_ownership_cb in between. */
2142 if (xproperty->atom == clip_plus.sel_atom)
2143 clip_plus.owned = TRUE;
2144 else
2145 clip_star.owned = TRUE;
2146 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002147}
2148
2149 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002150x11_setup_selection(Widget w)
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002151{
2152 XtAddEventHandler(w, PropertyChangeMask, False,
2153 /*(XtEventHandler)*/clip_x11_timestamp_cb, (XtPointer)NULL);
2154}
2155
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002157clip_x11_request_selection_cb(
2158 Widget w UNUSED,
2159 XtPointer success,
2160 Atom *sel_atom,
2161 Atom *type,
2162 XtPointer value,
2163 long_u *length,
2164 int *format)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165{
Bram Moolenaard44347f2011-06-19 01:14:29 +02002166 int motion_type = MAUTO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 long_u len;
2168 char_u *p;
2169 char **text_list = NULL;
2170 VimClipboard *cbd;
2171#ifdef FEAT_MBYTE
2172 char_u *tmpbuf = NULL;
2173#endif
2174
2175 if (*sel_atom == clip_plus.sel_atom)
2176 cbd = &clip_plus;
2177 else
2178 cbd = &clip_star;
2179
2180 if (value == NULL || *length == 0)
2181 {
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002182 clip_free_selection(cbd); /* nothing received, clear register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 *(int *)success = FALSE;
2184 return;
2185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 p = (char_u *)value;
2187 len = *length;
2188 if (*type == vim_atom)
2189 {
2190 motion_type = *p++;
2191 len--;
2192 }
2193
2194#ifdef FEAT_MBYTE
2195 else if (*type == vimenc_atom)
2196 {
2197 char_u *enc;
2198 vimconv_T conv;
2199 int convlen;
2200
2201 motion_type = *p++;
2202 --len;
2203
2204 enc = p;
2205 p += STRLEN(p) + 1;
2206 len -= p - enc;
2207
2208 /* If the encoding of the text is different from 'encoding', attempt
2209 * converting it. */
2210 conv.vc_type = CONV_NONE;
2211 convert_setup(&conv, enc, p_enc);
2212 if (conv.vc_type != CONV_NONE)
2213 {
2214 convlen = len; /* Need to use an int here. */
2215 tmpbuf = string_convert(&conv, p, &convlen);
2216 len = convlen;
2217 if (tmpbuf != NULL)
2218 p = tmpbuf;
2219 convert_setup(&conv, NULL, NULL);
2220 }
2221 }
2222#endif
2223
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002224 else if (*type == compound_text_atom
2225#ifdef FEAT_MBYTE
2226 || *type == utf8_atom
2227#endif
2228 || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229#ifdef FEAT_MBYTE
2230 enc_dbcs != 0 &&
2231#endif
2232 *type == text_atom))
2233 {
2234 XTextProperty text_prop;
2235 int n_text = 0;
2236 int status;
2237
2238 text_prop.value = (unsigned char *)value;
2239 text_prop.encoding = *type;
2240 text_prop.format = *format;
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002241 text_prop.nitems = len;
Bram Moolenaar81851112013-04-12 12:27:30 +02002242#if defined(FEAT_MBYTE) && defined(X_HAVE_UTF8_STRING)
Bram Moolenaare2e663f2013-03-07 18:02:30 +01002243 if (*type == utf8_atom)
2244 status = Xutf8TextPropertyToTextList(X_DISPLAY, &text_prop,
2245 &text_list, &n_text);
2246 else
2247#endif
2248 status = XmbTextPropertyToTextList(X_DISPLAY, &text_prop,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 &text_list, &n_text);
2250 if (status != Success || n_text < 1)
2251 {
2252 *(int *)success = FALSE;
2253 return;
2254 }
2255 p = (char_u *)text_list[0];
2256 len = STRLEN(p);
2257 }
2258 clip_yank_selection(motion_type, p, (long)len, cbd);
2259
2260 if (text_list != NULL)
2261 XFreeStringList(text_list);
2262#ifdef FEAT_MBYTE
2263 vim_free(tmpbuf);
2264#endif
2265 XtFree((char *)value);
2266 *(int *)success = TRUE;
2267}
2268
2269 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002270clip_x11_request_selection(
2271 Widget myShell,
2272 Display *dpy,
2273 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274{
2275 XEvent event;
2276 Atom type;
2277 static int success;
2278 int i;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002279 time_t start_time;
2280 int timed_out = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281
2282 for (i =
2283#ifdef FEAT_MBYTE
2284 0
2285#else
2286 1
2287#endif
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002288 ; i < 6; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
2290 switch (i)
2291 {
2292#ifdef FEAT_MBYTE
2293 case 0: type = vimenc_atom; break;
2294#endif
2295 case 1: type = vim_atom; break;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002296#ifdef FEAT_MBYTE
2297 case 2: type = utf8_atom; break;
2298#endif
2299 case 3: type = compound_text_atom; break;
2300 case 4: type = text_atom; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 default: type = XA_STRING;
2302 }
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002303#ifdef FEAT_MBYTE
Bram Moolenaar81851112013-04-12 12:27:30 +02002304 if (type == utf8_atom
2305# if defined(X_HAVE_UTF8_STRING)
2306 && !enc_utf8
2307# endif
2308 )
2309 /* Only request utf-8 when 'encoding' is utf8 and
2310 * Xutf8TextPropertyToTextList is available. */
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002311 continue;
2312#endif
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002313 success = MAYBE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 XtGetSelectionValue(myShell, cbd->sel_atom, type,
2315 clip_x11_request_selection_cb, (XtPointer)&success, CurrentTime);
2316
2317 /* Make sure the request for the selection goes out before waiting for
2318 * a response. */
2319 XFlush(dpy);
2320
2321 /*
2322 * Wait for result of selection request, otherwise if we type more
2323 * characters, then they will appear before the one that requested the
2324 * paste! Don't worry, we will catch up with any other events later.
2325 */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002326 start_time = time(NULL);
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002327 while (success == MAYBE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002329 if (XCheckTypedEvent(dpy, PropertyNotify, &event)
2330 || XCheckTypedEvent(dpy, SelectionNotify, &event)
2331 || XCheckTypedEvent(dpy, SelectionRequest, &event))
Bram Moolenaar89417b92008-09-07 19:48:53 +00002332 {
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002333 /* This is where clip_x11_request_selection_cb() should be
2334 * called. It may actually happen a bit later, so we loop
2335 * until "success" changes.
2336 * We may get a SelectionRequest here and if we don't handle
2337 * it we hang. KDE klipper does this, for example.
2338 * We need to handle a PropertyNotify for large selections. */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002339 XtDispatchEvent(&event);
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002340 continue;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342
Bram Moolenaar89417b92008-09-07 19:48:53 +00002343 /* Time out after 2 to 3 seconds to avoid that we hang when the
2344 * other process doesn't respond. Note that the SelectionNotify
2345 * event may still come later when the selection owner comes back
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002346 * to life and the text gets inserted unexpectedly. Don't know
2347 * why that happens or how to avoid that :-(. */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002348 if (time(NULL) > start_time + 2)
2349 {
2350 timed_out = TRUE;
2351 break;
2352 }
2353
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 /* Do we need this? Probably not. */
2355 XSync(dpy, False);
2356
Bram Moolenaar89417b92008-09-07 19:48:53 +00002357 /* Wait for 1 msec to avoid that we eat up all CPU time. */
2358 ui_delay(1L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 }
2360
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002361 if (success == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 return;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002363
2364 /* don't do a retry with another type after timing out, otherwise we
2365 * hang for 15 seconds. */
2366 if (timed_out)
2367 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 }
2369
2370 /* Final fallback position - use the X CUT_BUFFER0 store */
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002371 yank_cut_buffer0(dpy, cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372}
2373
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 static Boolean
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002375clip_x11_convert_selection_cb(
2376 Widget w UNUSED,
2377 Atom *sel_atom,
2378 Atom *target,
2379 Atom *type,
2380 XtPointer *value,
2381 long_u *length,
2382 int *format)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383{
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002384 static char_u *save_result = NULL;
2385 static long_u save_length = 0;
2386 char_u *string;
2387 int motion_type;
2388 VimClipboard *cbd;
2389 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390
2391 if (*sel_atom == clip_plus.sel_atom)
2392 cbd = &clip_plus;
2393 else
2394 cbd = &clip_star;
2395
2396 if (!cbd->owned)
2397 return False; /* Shouldn't ever happen */
2398
2399 /* requestor wants to know what target types we support */
2400 if (*target == targets_atom)
2401 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002402 static Atom array[7];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 *value = (XtPointer)array;
2405 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 array[i++] = targets_atom;
2407#ifdef FEAT_MBYTE
2408 array[i++] = vimenc_atom;
2409#endif
2410 array[i++] = vim_atom;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002411#ifdef FEAT_MBYTE
2412 if (enc_utf8)
2413 array[i++] = utf8_atom;
2414#endif
2415 array[i++] = XA_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 array[i++] = text_atom;
2417 array[i++] = compound_text_atom;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002418
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 *type = XA_ATOM;
2420 /* This used to be: *format = sizeof(Atom) * 8; but that caused
2421 * crashes on 64 bit machines. (Peter Derr) */
2422 *format = 32;
2423 *length = i;
2424 return True;
2425 }
2426
2427 if ( *target != XA_STRING
2428#ifdef FEAT_MBYTE
2429 && *target != vimenc_atom
Bram Moolenaar980e58f2014-06-12 13:28:30 +02002430 && (*target != utf8_atom || !enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431#endif
2432 && *target != vim_atom
2433 && *target != text_atom
2434 && *target != compound_text_atom)
2435 return False;
2436
2437 clip_get_selection(cbd);
2438 motion_type = clip_convert_selection(&string, length, cbd);
2439 if (motion_type < 0)
2440 return False;
2441
2442 /* For our own format, the first byte contains the motion type */
2443 if (*target == vim_atom)
2444 (*length)++;
2445
2446#ifdef FEAT_MBYTE
2447 /* Our own format with encoding: motion 'encoding' NUL text */
2448 if (*target == vimenc_atom)
2449 *length += STRLEN(p_enc) + 2;
2450#endif
2451
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002452 if (save_length < *length || save_length / 2 >= *length)
2453 *value = XtRealloc((char *)save_result, (Cardinal)*length + 1);
2454 else
2455 *value = save_result;
2456 if (*value == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 {
2458 vim_free(string);
2459 return False;
2460 }
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002461 save_result = (char_u *)*value;
2462 save_length = *length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002464 if (*target == XA_STRING
2465#ifdef FEAT_MBYTE
2466 || (*target == utf8_atom && enc_utf8)
2467#endif
2468 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002470 mch_memmove(save_result, string, (size_t)(*length));
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002471 *type = *target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 }
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002473 else if (*target == compound_text_atom || *target == text_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 {
2475 XTextProperty text_prop;
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002476 char *string_nt = (char *)save_result;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002477 int conv_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478
2479 /* create NUL terminated string which XmbTextListToTextProperty wants */
2480 mch_memmove(string_nt, string, (size_t)*length);
2481 string_nt[*length] = NUL;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002482 conv_result = XmbTextListToTextProperty(X_DISPLAY, (char **)&string_nt,
2483 1, XCompoundTextStyle, &text_prop);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002484 if (conv_result != Success)
2485 {
2486 vim_free(string);
2487 return False;
2488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 *value = (XtPointer)(text_prop.value); /* from plain text */
2490 *length = text_prop.nitems;
2491 *type = compound_text_atom;
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002492 XtFree((char *)save_result);
2493 save_result = (char_u *)*value;
2494 save_length = *length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496#ifdef FEAT_MBYTE
2497 else if (*target == vimenc_atom)
2498 {
2499 int l = STRLEN(p_enc);
2500
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002501 save_result[0] = motion_type;
2502 STRCPY(save_result + 1, p_enc);
2503 mch_memmove(save_result + l + 2, string, (size_t)(*length - l - 2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 *type = vimenc_atom;
2505 }
2506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 else
2508 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002509 save_result[0] = motion_type;
2510 mch_memmove(save_result + 1, string, (size_t)(*length - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 *type = vim_atom;
2512 }
2513 *format = 8; /* 8 bits per char */
2514 vim_free(string);
2515 return True;
2516}
2517
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002519clip_x11_lose_ownership_cb(Widget w UNUSED, Atom *sel_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520{
2521 if (*sel_atom == clip_plus.sel_atom)
2522 clip_lose_selection(&clip_plus);
2523 else
2524 clip_lose_selection(&clip_star);
2525}
2526
2527 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002528clip_x11_lose_selection(Widget myShell, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529{
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002530 XtDisownSelection(myShell, cbd->sel_atom,
2531 XtLastTimestampProcessed(XtDisplay(myShell)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532}
2533
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002534 static void
2535clip_x11_notify_cb(Widget w UNUSED, Atom *sel_atom UNUSED, Atom *target UNUSED)
2536{
2537 /* To prevent automatically freeing the selection value. */
2538}
2539
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002541clip_x11_own_selection(Widget myShell, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542{
Bram Moolenaar62b42182010-09-21 22:09:37 +02002543 /* When using the GUI we have proper timestamps, use the one of the last
2544 * event. When in the console we don't get events (the terminal gets
2545 * them), Get the time by a zero-length append, clip_x11_timestamp_cb will
2546 * be called with the current timestamp. */
2547#ifdef FEAT_GUI
2548 if (gui.in_use)
2549 {
2550 if (XtOwnSelection(myShell, cbd->sel_atom,
2551 XtLastTimestampProcessed(XtDisplay(myShell)),
2552 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002553 clip_x11_notify_cb) == False)
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +01002554 return FAIL;
Bram Moolenaar62b42182010-09-21 22:09:37 +02002555 }
2556 else
2557#endif
2558 {
2559 if (!XChangeProperty(XtDisplay(myShell), XtWindow(myShell),
2560 cbd->sel_atom, timestamp_atom, 32, PropModeAppend, NULL, 0))
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +01002561 return FAIL;
Bram Moolenaar62b42182010-09-21 22:09:37 +02002562 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002563 /* Flush is required in a terminal as nothing else is doing it. */
2564 XFlush(XtDisplay(myShell));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 return OK;
2566}
2567
2568/*
2569 * Send the current selection to the clipboard. Do nothing for X because we
2570 * will fill in the selection only when requested by another app.
2571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002573clip_x11_set_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574{
2575}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002576
2577 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002578clip_x11_owner_exists(VimClipboard *cbd)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002579{
2580 return XGetSelectionOwner(X_DISPLAY, cbd->sel_atom) != None;
2581}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582#endif
2583
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002584#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) \
2585 || defined(FEAT_GUI_GTK) || defined(PROTO)
2586/*
2587 * Get the contents of the X CUT_BUFFER0 and put it in "cbd".
2588 */
2589 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002590yank_cut_buffer0(Display *dpy, VimClipboard *cbd)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002591{
2592 int nbytes = 0;
2593 char_u *buffer = (char_u *)XFetchBuffer(dpy, &nbytes, 0);
2594
2595 if (nbytes > 0)
2596 {
2597#ifdef FEAT_MBYTE
2598 int done = FALSE;
2599
2600 /* CUT_BUFFER0 is supposed to be always latin1. Convert to 'enc' when
2601 * using a multi-byte encoding. Conversion between two 8-bit
2602 * character sets usually fails and the text might actually be in
2603 * 'enc' anyway. */
2604 if (has_mbyte)
2605 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002606 char_u *conv_buf;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002607 vimconv_T vc;
2608
2609 vc.vc_type = CONV_NONE;
2610 if (convert_setup(&vc, (char_u *)"latin1", p_enc) == OK)
2611 {
2612 conv_buf = string_convert(&vc, buffer, &nbytes);
2613 if (conv_buf != NULL)
2614 {
2615 clip_yank_selection(MCHAR, conv_buf, (long)nbytes, cbd);
2616 vim_free(conv_buf);
2617 done = TRUE;
2618 }
2619 convert_setup(&vc, NULL, NULL);
2620 }
2621 }
2622 if (!done) /* use the text without conversion */
2623#endif
2624 clip_yank_selection(MCHAR, buffer, (long)nbytes, cbd);
2625 XFree((void *)buffer);
2626 if (p_verbose > 0)
2627 {
2628 verbose_enter();
2629 verb_msg((char_u *)_("Used CUT_BUFFER0 instead of empty selection"));
2630 verbose_leave();
2631 }
2632 }
2633}
2634#endif
2635
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636#if defined(FEAT_MOUSE) || defined(PROTO)
2637
2638/*
2639 * Move the cursor to the specified row and column on the screen.
Bram Moolenaar49325942007-05-10 19:19:59 +00002640 * Change current window if necessary. Returns an integer with the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 * CURSOR_MOVED bit set if the cursor has moved or unset otherwise.
2642 *
2643 * The MOUSE_FOLD_CLOSE bit is set when clicked on the '-' in a fold column.
2644 * The MOUSE_FOLD_OPEN bit is set when clicked on the '+' in a fold column.
2645 *
2646 * If flags has MOUSE_FOCUS, then the current window will not be changed, and
2647 * if the mouse is outside the window then the text will scroll, or if the
2648 * mouse was previously on a status line, then the status line may be dragged.
2649 *
2650 * If flags has MOUSE_MAY_VIS, then VIsual mode will be started before the
2651 * cursor is moved unless the cursor was on a status line.
2652 * This function returns one of IN_UNKNOWN, IN_BUFFER, IN_STATUS_LINE or
2653 * IN_SEP_LINE depending on where the cursor was clicked.
2654 *
2655 * If flags has MOUSE_MAY_STOP_VIS, then Visual mode will be stopped, unless
2656 * the mouse is on the status line of the same window.
2657 *
2658 * If flags has MOUSE_DID_MOVE, nothing is done if the mouse didn't move since
2659 * the last call.
2660 *
2661 * If flags has MOUSE_SETPOS, nothing is done, only the current position is
2662 * remembered.
2663 */
2664 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002665jump_to_mouse(
2666 int flags,
2667 int *inclusive, /* used for inclusive operator, can be NULL */
2668 int which_button) /* MOUSE_LEFT, MOUSE_RIGHT, MOUSE_MIDDLE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669{
2670 static int on_status_line = 0; /* #lines below bottom of window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 static int on_sep_line = 0; /* on separator right of window */
Bram Moolenaareb163d72017-09-23 15:08:17 +02002672#ifdef FEAT_MENU
2673 static int in_winbar = FALSE;
2674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 static int prev_row = -1;
2676 static int prev_col = -1;
2677 static win_T *dragwin = NULL; /* window being dragged */
2678 static int did_drag = FALSE; /* drag was noticed */
2679
2680 win_T *wp, *old_curwin;
2681 pos_T old_cursor;
2682 int count;
2683 int first;
2684 int row = mouse_row;
2685 int col = mouse_col;
2686#ifdef FEAT_FOLDING
2687 int mouse_char;
2688#endif
2689
2690 mouse_past_bottom = FALSE;
2691 mouse_past_eol = FALSE;
2692
2693 if (flags & MOUSE_RELEASED)
2694 {
2695 /* On button release we may change window focus if positioned on a
2696 * status line and no dragging happened. */
2697 if (dragwin != NULL && !did_drag)
2698 flags &= ~(MOUSE_FOCUS | MOUSE_DID_MOVE);
2699 dragwin = NULL;
2700 did_drag = FALSE;
2701 }
2702
2703 if ((flags & MOUSE_DID_MOVE)
2704 && prev_row == mouse_row
2705 && prev_col == mouse_col)
2706 {
2707retnomove:
Bram Moolenaar49325942007-05-10 19:19:59 +00002708 /* before moving the cursor for a left click which is NOT in a status
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 * line, stop Visual mode */
2710 if (on_status_line)
2711 return IN_STATUS_LINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 if (on_sep_line)
2713 return IN_SEP_LINE;
Bram Moolenaard327b0c2017-11-12 16:56:12 +01002714#ifdef FEAT_MENU
2715 if (in_winbar)
2716 {
2717 /* A quick second click may arrive as a double-click, but we use it
2718 * as a second click in the WinBar. */
2719 if ((mod_mask & MOD_MASK_MULTI_CLICK) && !(flags & MOUSE_RELEASED))
2720 {
2721 wp = mouse_find_win(&row, &col);
2722 if (wp == NULL)
2723 return IN_UNKNOWN;
2724 winbar_click(wp, col);
2725 }
2726 return IN_OTHER_WIN | MOUSE_WINBAR;
2727 }
2728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 if (flags & MOUSE_MAY_STOP_VIS)
2730 {
2731 end_visual_mode();
2732 redraw_curbuf_later(INVERTED); /* delete the inversion */
2733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2735 /* Continue a modeless selection in another window. */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002736 if (cmdwin_type != 0 && row < curwin->w_winrow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 return IN_OTHER_WIN;
2738#endif
2739 return IN_BUFFER;
2740 }
2741
2742 prev_row = mouse_row;
2743 prev_col = mouse_col;
2744
2745 if (flags & MOUSE_SETPOS)
2746 goto retnomove; /* ugly goto... */
2747
2748#ifdef FEAT_FOLDING
2749 /* Remember the character under the mouse, it might be a '-' or '+' in the
2750 * fold column. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002751 if (row >= 0 && row < Rows && col >= 0 && col <= Columns
2752 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 mouse_char = ScreenLines[LineOffset[row] + col];
2754 else
2755 mouse_char = ' ';
2756#endif
2757
2758 old_curwin = curwin;
2759 old_cursor = curwin->w_cursor;
2760
2761 if (!(flags & MOUSE_FOCUS))
2762 {
2763 if (row < 0 || col < 0) /* check if it makes sense */
2764 return IN_UNKNOWN;
2765
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 /* find the window where the row is in */
2767 wp = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02002768 if (wp == NULL)
2769 return IN_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 dragwin = NULL;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002771
2772#ifdef FEAT_MENU
2773 if (row == -1)
2774 {
2775 /* A click in the window toolbar does not enter another window or
2776 * change Visual highlighting. */
2777 winbar_click(wp, col);
Bram Moolenaareb163d72017-09-23 15:08:17 +02002778 in_winbar = TRUE;
2779 return IN_OTHER_WIN | MOUSE_WINBAR;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002780 }
Bram Moolenaareb163d72017-09-23 15:08:17 +02002781 in_winbar = FALSE;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002782#endif
2783
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 /*
2785 * winpos and height may change in win_enter()!
2786 */
2787 if (row >= wp->w_height) /* In (or below) status line */
2788 {
2789 on_status_line = row - wp->w_height + 1;
2790 dragwin = wp;
2791 }
2792 else
2793 on_status_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 if (col >= wp->w_width) /* In separator line */
2795 {
2796 on_sep_line = col - wp->w_width + 1;
2797 dragwin = wp;
2798 }
2799 else
2800 on_sep_line = 0;
2801
2802 /* The rightmost character of the status line might be a vertical
2803 * separator character if there is no connecting window to the right. */
2804 if (on_status_line && on_sep_line)
2805 {
2806 if (stl_connected(wp))
2807 on_sep_line = 0;
2808 else
2809 on_status_line = 0;
2810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 /* Before jumping to another buffer, or moving the cursor for a left
2813 * click, stop Visual mode. */
2814 if (VIsual_active
2815 && (wp->w_buffer != curwin->w_buffer
Bram Moolenaar4033c552017-09-16 20:54:51 +02002816 || (!on_status_line && !on_sep_line
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002817#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002819# ifdef FEAT_RIGHTLEFT
Bram Moolenaar02631462017-09-22 15:20:32 +02002820 wp->w_p_rl ? col < wp->w_width - wp->w_p_fdc :
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821# endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002822 col >= wp->w_p_fdc
2823# ifdef FEAT_CMDWIN
2824 + (cmdwin_type == 0 && wp == curwin ? 0 : 1)
2825# endif
2826 )
2827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 && (flags & MOUSE_MAY_STOP_VIS))))
2829 {
2830 end_visual_mode();
2831 redraw_curbuf_later(INVERTED); /* delete the inversion */
2832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833#ifdef FEAT_CMDWIN
2834 if (cmdwin_type != 0 && wp != curwin)
2835 {
2836 /* A click outside the command-line window: Use modeless
Bram Moolenaarf679a432010-03-02 18:16:09 +01002837 * selection if possible. Allow dragging the status lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 on_sep_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839# ifdef FEAT_CLIPBOARD
2840 if (on_status_line)
2841 return IN_STATUS_LINE;
2842 return IN_OTHER_WIN;
2843# else
2844 row = 0;
2845 col += wp->w_wincol;
2846 wp = curwin;
2847# endif
2848 }
2849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 /* Only change window focus when not clicking on or dragging the
2851 * status line. Do change focus when releasing the mouse button
2852 * (MOUSE_FOCUS was set above if we dragged first). */
2853 if (dragwin == NULL || (flags & MOUSE_RELEASED))
2854 win_enter(wp, TRUE); /* can make wp invalid! */
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002855
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 if (curwin != old_curwin)
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002857 {
2858#ifdef CHECK_DOUBLE_CLICK
2859 /* set topline, to be able to check for double click ourselves */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 set_mouse_topline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861#endif
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002862#ifdef FEAT_TERMINAL
2863 /* when entering a terminal window may change state */
2864 term_win_entered();
2865#endif
2866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 if (on_status_line) /* In (or below) status line */
2868 {
2869 /* Don't use start_arrow() if we're in the same window */
2870 if (curwin == old_curwin)
2871 return IN_STATUS_LINE;
2872 else
2873 return IN_STATUS_LINE | CURSOR_MOVED;
2874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 if (on_sep_line) /* In (or below) status line */
2876 {
2877 /* Don't use start_arrow() if we're in the same window */
2878 if (curwin == old_curwin)
2879 return IN_SEP_LINE;
2880 else
2881 return IN_SEP_LINE | CURSOR_MOVED;
2882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883
2884 curwin->w_cursor.lnum = curwin->w_topline;
2885#ifdef FEAT_GUI
2886 /* remember topline, needed for double click */
2887 gui_prev_topline = curwin->w_topline;
2888# ifdef FEAT_DIFF
2889 gui_prev_topfill = curwin->w_topfill;
2890# endif
2891#endif
2892 }
2893 else if (on_status_line && which_button == MOUSE_LEFT)
2894 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 if (dragwin != NULL)
2896 {
2897 /* Drag the status line */
2898 count = row - dragwin->w_winrow - dragwin->w_height + 1
2899 - on_status_line;
2900 win_drag_status_line(dragwin, count);
2901 did_drag |= count;
2902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 return IN_STATUS_LINE; /* Cursor didn't move */
2904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 else if (on_sep_line && which_button == MOUSE_LEFT)
2906 {
2907 if (dragwin != NULL)
2908 {
2909 /* Drag the separator column */
2910 count = col - dragwin->w_wincol - dragwin->w_width + 1
2911 - on_sep_line;
2912 win_drag_vsep_line(dragwin, count);
2913 did_drag |= count;
2914 }
2915 return IN_SEP_LINE; /* Cursor didn't move */
2916 }
Bram Moolenaareb163d72017-09-23 15:08:17 +02002917#ifdef FEAT_MENU
2918 else if (in_winbar)
2919 {
2920 /* After a click on the window toolbar don't start Visual mode. */
2921 return IN_OTHER_WIN | MOUSE_WINBAR;
2922 }
2923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 else /* keep_window_focus must be TRUE */
2925 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 /* before moving the cursor for a left click, stop Visual mode */
2927 if (flags & MOUSE_MAY_STOP_VIS)
2928 {
2929 end_visual_mode();
2930 redraw_curbuf_later(INVERTED); /* delete the inversion */
2931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932
2933#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2934 /* Continue a modeless selection in another window. */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002935 if (cmdwin_type != 0 && row < curwin->w_winrow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 return IN_OTHER_WIN;
2937#endif
2938
2939 row -= W_WINROW(curwin);
Bram Moolenaar53f81742017-09-22 14:35:51 +02002940 col -= curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941
2942 /*
2943 * When clicking beyond the end of the window, scroll the screen.
2944 * Scroll by however many rows outside the window we are.
2945 */
2946 if (row < 0)
2947 {
2948 count = 0;
2949 for (first = TRUE; curwin->w_topline > 1; )
2950 {
2951#ifdef FEAT_DIFF
2952 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
2953 ++count;
2954 else
2955#endif
2956 count += plines(curwin->w_topline - 1);
2957 if (!first && count > -row)
2958 break;
2959 first = FALSE;
2960#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02002961 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962#endif
2963#ifdef FEAT_DIFF
2964 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
2965 ++curwin->w_topfill;
2966 else
2967#endif
2968 {
2969 --curwin->w_topline;
2970#ifdef FEAT_DIFF
2971 curwin->w_topfill = 0;
2972#endif
2973 }
2974 }
2975#ifdef FEAT_DIFF
2976 check_topfill(curwin, FALSE);
2977#endif
2978 curwin->w_valid &=
2979 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2980 redraw_later(VALID);
2981 row = 0;
2982 }
2983 else if (row >= curwin->w_height)
2984 {
2985 count = 0;
2986 for (first = TRUE; curwin->w_topline < curbuf->b_ml.ml_line_count; )
2987 {
2988#ifdef FEAT_DIFF
2989 if (curwin->w_topfill > 0)
2990 ++count;
2991 else
2992#endif
2993 count += plines(curwin->w_topline);
2994 if (!first && count > row - curwin->w_height + 1)
2995 break;
2996 first = FALSE;
2997#ifdef FEAT_FOLDING
2998 if (hasFolding(curwin->w_topline, NULL, &curwin->w_topline)
2999 && curwin->w_topline == curbuf->b_ml.ml_line_count)
3000 break;
3001#endif
3002#ifdef FEAT_DIFF
3003 if (curwin->w_topfill > 0)
3004 --curwin->w_topfill;
3005 else
3006#endif
3007 {
3008 ++curwin->w_topline;
3009#ifdef FEAT_DIFF
3010 curwin->w_topfill =
3011 diff_check_fill(curwin, curwin->w_topline);
3012#endif
3013 }
3014 }
3015#ifdef FEAT_DIFF
3016 check_topfill(curwin, FALSE);
3017#endif
3018 redraw_later(VALID);
3019 curwin->w_valid &=
3020 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3021 row = curwin->w_height - 1;
3022 }
3023 else if (row == 0)
3024 {
3025 /* When dragging the mouse, while the text has been scrolled up as
3026 * far as it goes, moving the mouse in the top line should scroll
3027 * the text down (done later when recomputing w_topline). */
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003028 if (mouse_dragging > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 && curwin->w_cursor.lnum
3030 == curwin->w_buffer->b_ml.ml_line_count
3031 && curwin->w_cursor.lnum == curwin->w_topline)
3032 curwin->w_valid &= ~(VALID_TOPLINE);
3033 }
3034 }
3035
3036#ifdef FEAT_FOLDING
3037 /* Check for position outside of the fold column. */
3038 if (
3039# ifdef FEAT_RIGHTLEFT
Bram Moolenaar02631462017-09-22 15:20:32 +02003040 curwin->w_p_rl ? col < curwin->w_width - curwin->w_p_fdc :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041# endif
3042 col >= curwin->w_p_fdc
3043# ifdef FEAT_CMDWIN
3044 + (cmdwin_type == 0 ? 0 : 1)
3045# endif
3046 )
3047 mouse_char = ' ';
3048#endif
3049
3050 /* compute the position in the buffer line from the posn on the screen */
3051 if (mouse_comp_pos(curwin, &row, &col, &curwin->w_cursor.lnum))
3052 mouse_past_bottom = TRUE;
3053
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 /* Start Visual mode before coladvance(), for when 'sel' != "old" */
3055 if ((flags & MOUSE_MAY_VIS) && !VIsual_active)
3056 {
3057 check_visual_highlight();
3058 VIsual = old_cursor;
3059 VIsual_active = TRUE;
3060 VIsual_reselect = TRUE;
3061 /* if 'selectmode' contains "mouse", start Select mode */
3062 may_start_select('o');
3063 setmouse();
Bram Moolenaar7df351e2006-01-23 22:30:28 +00003064 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 redraw_cmdline = TRUE; /* show visual mode later */
3066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067
3068 curwin->w_curswant = col;
3069 curwin->w_set_curswant = FALSE; /* May still have been TRUE */
3070 if (coladvance(col) == FAIL) /* Mouse click beyond end of line */
3071 {
3072 if (inclusive != NULL)
3073 *inclusive = TRUE;
3074 mouse_past_eol = TRUE;
3075 }
3076 else if (inclusive != NULL)
3077 *inclusive = FALSE;
3078
3079 count = IN_BUFFER;
3080 if (curwin != old_curwin || curwin->w_cursor.lnum != old_cursor.lnum
3081 || curwin->w_cursor.col != old_cursor.col)
3082 count |= CURSOR_MOVED; /* Cursor has moved */
3083
3084#ifdef FEAT_FOLDING
3085 if (mouse_char == '+')
3086 count |= MOUSE_FOLD_OPEN;
3087 else if (mouse_char != ' ')
3088 count |= MOUSE_FOLD_CLOSE;
3089#endif
3090
3091 return count;
3092}
3093
3094/*
3095 * Compute the position in the buffer line from the posn on the screen in
3096 * window "win".
3097 * Returns TRUE if the position is below the last line.
3098 */
3099 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003100mouse_comp_pos(
3101 win_T *win,
3102 int *rowp,
3103 int *colp,
3104 linenr_T *lnump)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105{
3106 int col = *colp;
3107 int row = *rowp;
3108 linenr_T lnum;
3109 int retval = FALSE;
3110 int off;
3111 int count;
3112
3113#ifdef FEAT_RIGHTLEFT
3114 if (win->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02003115 col = win->w_width - 1 - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116#endif
3117
3118 lnum = win->w_topline;
3119
3120 while (row > 0)
3121 {
3122#ifdef FEAT_DIFF
3123 /* Don't include filler lines in "count" */
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003124 if (win->w_p_diff
3125# ifdef FEAT_FOLDING
3126 && !hasFoldingWin(win, lnum, NULL, NULL, TRUE, NULL)
3127# endif
3128 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 {
3130 if (lnum == win->w_topline)
3131 row -= win->w_topfill;
3132 else
3133 row -= diff_check_fill(win, lnum);
3134 count = plines_win_nofill(win, lnum, TRUE);
3135 }
3136 else
3137#endif
3138 count = plines_win(win, lnum, TRUE);
3139 if (count > row)
3140 break; /* Position is in this buffer line. */
3141#ifdef FEAT_FOLDING
3142 (void)hasFoldingWin(win, lnum, NULL, &lnum, TRUE, NULL);
3143#endif
3144 if (lnum == win->w_buffer->b_ml.ml_line_count)
3145 {
3146 retval = TRUE;
3147 break; /* past end of file */
3148 }
3149 row -= count;
3150 ++lnum;
3151 }
3152
3153 if (!retval)
3154 {
3155 /* Compute the column without wrapping. */
3156 off = win_col_off(win) - win_col_off2(win);
3157 if (col < off)
3158 col = off;
Bram Moolenaar02631462017-09-22 15:20:32 +02003159 col += row * (win->w_width - off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 /* add skip column (for long wrapping line) */
3161 col += win->w_skipcol;
3162 }
3163
3164 if (!win->w_p_wrap)
3165 col += win->w_leftcol;
3166
3167 /* skip line number and fold column in front of the line */
3168 col -= win_col_off(win);
3169 if (col < 0)
3170 {
3171#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003172 netbeans_gutter_click(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173#endif
3174 col = 0;
3175 }
3176
3177 *colp = col;
3178 *rowp = row;
3179 *lnump = lnum;
3180 return retval;
3181}
3182
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183/*
3184 * Find the window at screen position "*rowp" and "*colp". The positions are
3185 * updated to become relative to the top-left of the window.
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003186 * Returns NULL when something is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 win_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003189mouse_find_win(int *rowp, int *colp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190{
3191 frame_T *fp;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003192 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193
3194 fp = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003195 *rowp -= firstwin->w_winrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 for (;;)
3197 {
3198 if (fp->fr_layout == FR_LEAF)
3199 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 if (fp->fr_layout == FR_ROW)
3201 {
3202 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
3203 {
3204 if (*colp < fp->fr_width)
3205 break;
3206 *colp -= fp->fr_width;
3207 }
3208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 else /* fr_layout == FR_COL */
3210 {
3211 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
3212 {
3213 if (*rowp < fp->fr_height)
3214 break;
3215 *rowp -= fp->fr_height;
3216 }
3217 }
3218 }
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003219 /* When using a timer that closes a window the window might not actually
3220 * exist. */
3221 FOR_ALL_WINDOWS(wp)
3222 if (wp == fp->fr_win)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003223 {
3224#ifdef FEAT_MENU
3225 *rowp -= wp->w_winbar_height;
3226#endif
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003227 return wp;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003228 }
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003229 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231
Bram Moolenaar860cae12010-06-05 23:22:07 +02003232#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar658a1542018-03-03 19:29:43 +01003234 || defined(FEAT_GUI_PHOTON) || defined(FEAT_TERM_POPUP_MENU) \
3235 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236/*
3237 * Translate window coordinates to buffer position without any side effects
3238 */
3239 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003240get_fpos_of_mouse(pos_T *mpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241{
3242 win_T *wp;
3243 int row = mouse_row;
3244 int col = mouse_col;
3245
3246 if (row < 0 || col < 0) /* check if it makes sense */
3247 return IN_UNKNOWN;
3248
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 /* find the window where the row is in */
3250 wp = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003251 if (wp == NULL)
3252 return IN_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 /*
3254 * winpos and height may change in win_enter()!
3255 */
3256 if (row >= wp->w_height) /* In (or below) status line */
3257 return IN_STATUS_LINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 if (col >= wp->w_width) /* In vertical separator line */
3259 return IN_SEP_LINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260
3261 if (wp != curwin)
3262 return IN_UNKNOWN;
3263
3264 /* compute the position in the buffer line from the posn on the screen */
3265 if (mouse_comp_pos(curwin, &row, &col, &mpos->lnum))
3266 return IN_STATUS_LINE; /* past bottom */
3267
3268 mpos->col = vcol2col(wp, mpos->lnum, col);
3269
3270 if (mpos->col > 0)
3271 --mpos->col;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003272#ifdef FEAT_VIRTUALEDIT
3273 mpos->coladd = 0;
3274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 return IN_BUFFER;
3276}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003279#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MAC) \
3280 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar3767b612018-03-03 19:51:58 +01003281 || defined(FEAT_GUI_PHOTON) || defined(FEAT_BEVAL) \
3282 || defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283/*
3284 * Convert a virtual (screen) column to a character column.
3285 * The first column is one.
3286 */
3287 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003288vcol2col(win_T *wp, linenr_T lnum, int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289{
3290 /* try to advance to the specified column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 int count = 0;
3292 char_u *ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003293 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294
Bram Moolenaar597a4222014-06-25 14:39:50 +02003295 line = ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaarb6101cf2012-10-21 00:58:39 +02003296 while (count < vcol && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003298 count += win_lbr_chartabsize(wp, line, ptr, count, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003299 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02003301 return (int)(ptr - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302}
3303#endif
3304
3305#endif /* FEAT_MOUSE */
3306
3307#if defined(FEAT_GUI) || defined(WIN3264) || defined(PROTO)
3308/*
3309 * Called when focus changed. Used for the GUI or for systems where this can
3310 * be done in the console (Win32).
3311 */
3312 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003313ui_focus_change(
3314 int in_focus) /* TRUE if focus gained. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315{
3316 static time_t last_time = (time_t)0;
3317 int need_redraw = FALSE;
3318
3319 /* When activated: Check if any file was modified outside of Vim.
3320 * Only do this when not done within the last two seconds (could get
3321 * several events in a row). */
3322 if (in_focus && last_time + 2 < time(NULL))
3323 {
3324 need_redraw = check_timestamps(
3325# ifdef FEAT_GUI
3326 gui.in_use
3327# else
3328 FALSE
3329# endif
3330 );
3331 last_time = time(NULL);
3332 }
3333
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 /*
3335 * Fire the focus gained/lost autocommand.
3336 */
3337 need_redraw |= apply_autocmds(in_focus ? EVENT_FOCUSGAINED
3338 : EVENT_FOCUSLOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339
3340 if (need_redraw)
3341 {
3342 /* Something was executed, make sure the cursor is put back where it
3343 * belongs. */
3344 need_wait_return = FALSE;
3345
3346 if (State & CMDLINE)
3347 redrawcmdline();
3348 else if (State == HITRETURN || State == SETWSIZE || State == ASKMORE
3349 || State == EXTERNCMD || State == CONFIRM || exmode_active)
3350 repeat_message();
3351 else if ((State & NORMAL) || (State & INSERT))
3352 {
3353 if (must_redraw != 0)
3354 update_screen(0);
3355 setcursor();
3356 }
3357 cursor_on(); /* redrawing may have switched it off */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003358 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359# ifdef FEAT_GUI
3360 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 gui_update_scrollbars(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362# endif
3363 }
3364#ifdef FEAT_TITLE
3365 /* File may have been changed from 'readonly' to 'noreadonly' */
3366 if (need_maketitle)
3367 maketitle();
3368#endif
3369}
3370#endif
3371
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003372#if defined(HAVE_INPUT_METHOD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373/*
3374 * Save current Input Method status to specified place.
3375 */
3376 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003377im_save_status(long *psave)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378{
3379 /* Don't save when 'imdisable' is set or "xic" is NULL, IM is always
3380 * disabled then (but might start later).
3381 * Also don't save when inside a mapping, vgetc_im_active has not been set
3382 * then.
3383 * And don't save when the keys were stuffed (e.g., for a "." command).
3384 * And don't save when the GUI is running but our window doesn't have
3385 * input focus (e.g., when a find dialog is open). */
3386 if (!p_imdisable && KeyTyped && !KeyStuffed
3387# ifdef FEAT_XIM
3388 && xic != NULL
3389# endif
3390# ifdef FEAT_GUI
3391 && (!gui.in_use || gui.in_focus)
3392# endif
3393 )
3394 {
3395 /* Do save when IM is on, or IM is off and saved status is on. */
3396 if (vgetc_im_active)
3397 *psave = B_IMODE_IM;
3398 else if (*psave == B_IMODE_IM)
3399 *psave = B_IMODE_NONE;
3400 }
3401}
3402#endif