blob: f78c570c8df6c1747ceaaa271943b32c593766ea [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 Moolenaar4f974752019-02-17 17:44:42 +010043#if !defined(MSWIN)
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 Moolenaar4f974752019-02-17 17:44:42 +010057# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000058 if (output_conv.vc_type != CONV_NONE)
59 vim_free(tofree);
Bram Moolenaar264b74f2019-01-24 17:18:42 +010060# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000061 }
62#endif
63}
64
Bram Moolenaar4f974752019-02-17 17:44:42 +010065#if defined(UNIX) || defined(VMS) || defined(PROTO) || defined(MSWIN)
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
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100181 /*
182 * Here we call gui_inchar() or mch_inchar(), the GUI or machine-dependent
183 * input function. The functionality they implement is like this:
184 *
185 * while (not timed out)
186 * {
187 * handle-resize;
188 * parse-queued-messages;
189 * if (waited for 'updatetime')
190 * trigger-cursorhold;
191 * ui_wait_for_chars_or_timer()
192 * if (character available)
193 * break;
194 * }
195 *
196 * ui_wait_for_chars_or_timer() does:
197 *
198 * while (not timed out)
199 * {
200 * if (any-timer-triggered)
201 * invoke-timer-callback;
202 * wait-for-character();
203 * if (character available)
204 * break;
205 * }
206 *
207 * wait-for-character() does:
208 * while (not timed out)
209 * {
210 * Wait for event;
211 * if (something on channel)
212 * read/write channel;
213 * else if (resized)
214 * handle_resize();
215 * else if (system event)
216 * deal-with-system-event;
217 * else if (character available)
218 * break;
219 * }
220 *
221 */
222
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223#ifdef FEAT_GUI
224 if (gui.in_use)
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100225 retval = gui_inchar(buf, maxlen, wtime, tb_change_cnt);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#endif
227#ifndef NO_CONSOLE
228# ifdef FEAT_GUI
229 else
230# endif
231 retval = mch_inchar(buf, maxlen, wtime, tb_change_cnt);
232#endif
233
Bram Moolenaarc8da3112007-03-08 12:36:46 +0000234 if (wtime == -1 || wtime > 100L)
235 /* block SIGHUP et al. */
236 (void)vim_handle_signal(SIGNAL_BLOCK);
237
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 ctrl_c_interrupts = TRUE;
239
Bram Moolenaar05159a02005-02-26 23:04:13 +0000240#ifdef NO_CONSOLE_INPUT
241theend:
242#endif
243#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000244 if (do_profiling == PROF_YES && wtime != 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000245 prof_inchar_exit();
246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 return retval;
248}
249
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100250#if defined(UNIX) || defined(FEAT_GUI) || defined(PROTO)
251/*
252 * Common code for mch_inchar() and gui_inchar(): Wait for a while or
253 * indefinitely until characters are available, dealing with timers and
254 * messages on channels.
255 *
256 * "buf" may be NULL if the available characters are not to be returned, only
257 * check if they are available.
258 *
259 * Return the number of characters that are available.
260 * If "wtime" == 0 do not wait for characters.
261 * If "wtime" == n wait a short time for characters.
262 * If "wtime" == -1 wait forever for characters.
263 */
264 int
265inchar_loop(
266 char_u *buf,
267 int maxlen,
268 long wtime, // don't use "time", MIPS cannot handle it
269 int tb_change_cnt,
270 int (*wait_func)(long wtime, int *interrupted, int ignore_input),
271 int (*resize_func)(int check_only))
272{
273 int len;
274 int interrupted = FALSE;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +0100275 int did_call_wait_func = FALSE;
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100276 int did_start_blocking = FALSE;
277 long wait_time;
278 long elapsed_time = 0;
279#ifdef ELAPSED_FUNC
280 elapsed_T start_tv;
281
282 ELAPSED_INIT(start_tv);
283#endif
284
285 /* repeat until we got a character or waited long enough */
286 for (;;)
287 {
288 /* Check if window changed size while we were busy, perhaps the ":set
289 * columns=99" command was used. */
290 if (resize_func != NULL)
291 resize_func(FALSE);
292
293#ifdef MESSAGE_QUEUE
294 // Only process messages when waiting.
295 if (wtime != 0)
296 {
297 parse_queued_messages();
298 // If input was put directly in typeahead buffer bail out here.
299 if (typebuf_changed(tb_change_cnt))
300 return 0;
301 }
302#endif
303 if (wtime < 0 && did_start_blocking)
304 // blocking and already waited for p_ut
305 wait_time = -1;
306 else
307 {
308 if (wtime >= 0)
309 wait_time = wtime;
310 else
311 // going to block after p_ut
312 wait_time = p_ut;
313#ifdef ELAPSED_FUNC
314 elapsed_time = ELAPSED_FUNC(start_tv);
315#endif
316 wait_time -= elapsed_time;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +0100317
318 // If the waiting time is now zero or less, we timed out. However,
319 // loop at least once to check for characters and events. Matters
320 // when "wtime" is zero.
321 if (wait_time <= 0 && did_call_wait_func)
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100322 {
323 if (wtime >= 0)
324 // no character available within "wtime"
325 return 0;
326
327 // No character available within 'updatetime'.
328 did_start_blocking = TRUE;
329 if (trigger_cursorhold() && maxlen >= 3
330 && !typebuf_changed(tb_change_cnt))
331 {
332 // Put K_CURSORHOLD in the input buffer or return it.
333 if (buf == NULL)
334 {
335 char_u ibuf[3];
336
337 ibuf[0] = CSI;
338 ibuf[1] = KS_EXTRA;
339 ibuf[2] = (int)KE_CURSORHOLD;
340 add_to_input_buf(ibuf, 3);
341 }
342 else
343 {
344 buf[0] = K_SPECIAL;
345 buf[1] = KS_EXTRA;
346 buf[2] = (int)KE_CURSORHOLD;
347 }
348 return 3;
349 }
350
351 // There is no character available within 'updatetime' seconds:
352 // flush all the swap files to disk. Also done when
353 // interrupted by SIGWINCH.
354 before_blocking();
355 continue;
356 }
357 }
358
359#ifdef FEAT_JOB_CHANNEL
360 if (wait_time < 0 || wait_time > 100L)
361 {
362 // Checking if a job ended requires polling. Do this at least
363 // every 100 msec.
364 if (has_pending_job())
365 wait_time = 100L;
366
367 // If there is readahead then parse_queued_messages() timed out and
368 // we should call it again soon.
369 if (channel_any_readahead())
370 wait_time = 10L;
371 }
372#endif
373#ifdef FEAT_BEVAL_GUI
374 if (p_beval && wait_time > 100L)
375 // The 'balloonexpr' may indirectly invoke a callback while waiting
376 // for a character, need to check often.
377 wait_time = 100L;
378#endif
379
380 // Wait for a character to be typed or another event, such as the winch
381 // signal or an event on the monitored file descriptors.
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +0100382 did_call_wait_func = TRUE;
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100383 if (wait_func(wait_time, &interrupted, FALSE))
384 {
385 // If input was put directly in typeahead buffer bail out here.
386 if (typebuf_changed(tb_change_cnt))
387 return 0;
388
389 // We might have something to return now.
390 if (buf == NULL)
391 // "buf" is NULL, we were just waiting, not actually getting
392 // input.
393 return input_available();
394
395 len = read_from_input_buf(buf, (long)maxlen);
396 if (len > 0)
397 return len;
398 continue;
399 }
400 // Timed out or interrupted with no character available.
401
402#ifndef ELAPSED_FUNC
403 // estimate the elapsed time
404 elapsed_time += wait_time;
405#endif
406
407 if ((resize_func != NULL && resize_func(TRUE))
Bram Moolenaar3e9d4d82019-01-27 17:08:40 +0100408#if defined(FEAT_CLIENTSERVER) && defined(UNIX)
Bram Moolenaare40b9d42019-01-27 16:55:47 +0100409 || server_waiting()
410#endif
411#ifdef MESSAGE_QUEUE
412 || interrupted
413#endif
414 || wait_time > 0
415 || (wtime < 0 && !did_start_blocking))
416 // no character available, but something to be done, keep going
417 continue;
418
419 // no character available or interrupted, return zero
420 break;
421 }
422 return 0;
423}
424#endif
425
Bram Moolenaarc46af532019-01-09 22:24:49 +0100426#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100427/*
428 * Wait for a timer to fire or "wait_func" to return non-zero.
429 * Returns OK when something was read.
430 * Returns FAIL when it timed out or was interrupted.
431 */
432 int
433ui_wait_for_chars_or_timer(
434 long wtime,
435 int (*wait_func)(long wtime, int *interrupted, int ignore_input),
436 int *interrupted,
437 int ignore_input)
438{
439 int due_time;
440 long remaining = wtime;
441 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaarc46af532019-01-09 22:24:49 +0100442# ifdef FEAT_JOB_CHANNEL
Bram Moolenaare299bbd2019-01-17 14:12:02 +0100443 int brief_wait = FALSE;
Bram Moolenaarc46af532019-01-09 22:24:49 +0100444# endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100445
Bram Moolenaarc46af532019-01-09 22:24:49 +0100446 // When waiting very briefly don't trigger timers.
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100447 if (wtime >= 0 && wtime < 10L)
448 return wait_func(wtime, NULL, ignore_input);
449
450 while (wtime < 0 || remaining > 0)
451 {
Bram Moolenaarc46af532019-01-09 22:24:49 +0100452 // Trigger timers and then get the time in wtime until the next one is
453 // due. Wait up to that time.
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100454 due_time = check_due_timer();
455 if (typebuf.tb_change_cnt != tb_change_cnt)
456 {
457 /* timer may have used feedkeys() */
458 return FAIL;
459 }
460 if (due_time <= 0 || (wtime > 0 && due_time > remaining))
461 due_time = remaining;
Bram Moolenaarc46af532019-01-09 22:24:49 +0100462# ifdef FEAT_JOB_CHANNEL
463 if ((due_time < 0 || due_time > 10L)
464# ifdef FEAT_GUI
465 && !gui.in_use
466# endif
467 && (has_pending_job() || channel_any_readahead()))
468 {
469 // There is a pending job or channel, should return soon in order
470 // to handle them ASAP. Do check for input briefly.
471 due_time = 10L;
472 brief_wait = TRUE;
473 }
474# endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100475 if (wait_func(due_time, interrupted, ignore_input))
476 return OK;
Bram Moolenaarc46af532019-01-09 22:24:49 +0100477 if ((interrupted != NULL && *interrupted)
478# ifdef FEAT_JOB_CHANNEL
479 || brief_wait
480# endif
481 )
482 // Nothing available, but need to return so that side effects get
483 // handled, such as handling a message on a channel.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100484 return FAIL;
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100485 if (wtime > 0)
486 remaining -= due_time;
487 }
488 return FAIL;
489}
490#endif
491
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492/*
Bram Moolenaarc46af532019-01-09 22:24:49 +0100493 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 */
495 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100496ui_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497{
498#ifdef FEAT_GUI
499 if (gui.in_use)
500 {
501 gui_mch_update();
502 return input_available();
503 }
504#endif
505#ifndef NO_CONSOLE
506# ifdef NO_CONSOLE_INPUT
507 if (no_console_input())
508 return 0;
509# endif
510 return mch_char_avail();
511#else
512 return 0;
513#endif
514}
515
516/*
517 * Delay for the given number of milliseconds. If ignoreinput is FALSE then we
518 * cancel the delay if a key is hit.
519 */
520 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100521ui_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522{
523#ifdef FEAT_GUI
524 if (gui.in_use && !ignoreinput)
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100525 gui_wait_for_chars(msec, typebuf.tb_change_cnt);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 else
527#endif
528 mch_delay(msec, ignoreinput);
529}
530
531/*
532 * If the machine has job control, use it to suspend the program,
533 * otherwise fake it by starting a new shell.
534 * When running the GUI iconify the window.
535 */
536 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100537ui_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538{
539#ifdef FEAT_GUI
540 if (gui.in_use)
541 {
542 gui_mch_iconify();
543 return;
544 }
545#endif
546 mch_suspend();
547}
548
549#if !defined(UNIX) || !defined(SIGTSTP) || defined(PROTO) || defined(__BEOS__)
550/*
551 * When the OS can't really suspend, call this function to start a shell.
552 * This is never called in the GUI.
553 */
554 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100555suspend_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556{
557 if (*p_sh == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100558 emsg(_(e_shellempty));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 else
560 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100561 msg_puts(_("new shell started\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 do_shell(NULL, 0);
563 }
564}
565#endif
566
567/*
568 * Try to get the current Vim shell size. Put the result in Rows and Columns.
569 * Use the new sizes as defaults for 'columns' and 'lines'.
570 * Return OK when size could be determined, FAIL otherwise.
571 */
572 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100573ui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574{
575 int retval;
576
577#ifdef FEAT_GUI
578 if (gui.in_use)
579 retval = gui_get_shellsize();
580 else
581#endif
582 retval = mch_get_shellsize();
583
584 check_shellsize();
585
586 /* adjust the default for 'lines' and 'columns' */
587 if (retval == OK)
588 {
589 set_number_default("lines", Rows);
590 set_number_default("columns", Columns);
591 }
592 return retval;
593}
594
595/*
596 * Set the size of the Vim shell according to Rows and Columns, if possible.
597 * The gui_set_shellsize() or mch_set_shellsize() function will try to set the
598 * new size. If this is not possible, it will adjust Rows and Columns.
599 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100601ui_set_shellsize(
602 int mustset UNUSED) /* set by the user */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603{
604#ifdef FEAT_GUI
605 if (gui.in_use)
Bram Moolenaar8968a312013-07-03 16:58:44 +0200606 gui_set_shellsize(mustset, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 else
608#endif
609 mch_set_shellsize();
610}
611
612/*
613 * Called when Rows and/or Columns changed. Adjust scroll region and mouse
614 * region.
615 */
616 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100617ui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
619 if (full_screen && !exiting)
620 {
621#ifdef FEAT_GUI
622 if (gui.in_use)
623 gui_new_shellsize();
624 else
625#endif
626 mch_new_shellsize();
627 }
628}
629
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +0200630#if (defined(FEAT_EVAL) \
Bram Moolenaar3d3f2172019-04-06 17:56:05 +0200631 && (defined(FEAT_GUI) \
632 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)))) \
633 || defined(FEAT_TERMINAL) \
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +0200634 || defined(PROTO)
635/*
636 * Get the window position in pixels, if possible.
637 * Return FAIL when not possible.
638 */
639 int
640ui_get_winpos(int *x, int *y, varnumber_T timeout)
641{
642# ifdef FEAT_GUI
643 if (gui.in_use)
644 return gui_mch_get_winpos(x, y);
645# endif
646# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
647 return term_get_winpos(x, y, timeout);
648# endif
649}
650#endif
651
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100653ui_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200655 ui_breakcheck_force(FALSE);
656}
657
658/*
659 * When "force" is true also check when the terminal is not in raw mode.
660 * This is useful to read input on channels.
661 */
662 void
663ui_breakcheck_force(int force)
664{
Bram Moolenaar48d23bb2018-11-20 02:42:43 +0100665 static int recursive = FALSE;
666 int save_updating_screen = updating_screen;
Bram Moolenaare3caa112017-01-31 22:07:42 +0100667
Bram Moolenaar48d23bb2018-11-20 02:42:43 +0100668 // We could be called recursively if stderr is redirected, calling
669 // fill_input_buf() calls settmode() when stdin isn't a tty. settmode()
670 // calls vgetorpeek() which calls ui_breakcheck() again.
671 if (recursive)
672 return;
673 recursive = TRUE;
674
675 // We do not want gui_resize_shell() to redraw the screen here.
Bram Moolenaare3caa112017-01-31 22:07:42 +0100676 ++updating_screen;
677
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678#ifdef FEAT_GUI
679 if (gui.in_use)
680 gui_mch_update();
681 else
682#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200683 mch_breakcheck(force);
Bram Moolenaare3caa112017-01-31 22:07:42 +0100684
Bram Moolenaar42335f52018-09-13 15:33:43 +0200685 if (save_updating_screen)
686 updating_screen = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200687 else
688 reset_updating_screen(FALSE);
Bram Moolenaar48d23bb2018-11-20 02:42:43 +0100689
690 recursive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691}
692
693/*****************************************************************************
694 * Functions for copying and pasting text between applications.
695 * This is always included in a GUI version, but may also be included when the
696 * clipboard and mouse is available to a terminal version such as xterm.
697 * Note: there are some more functions in ops.c that handle selection stuff.
698 *
699 * Also note that the majority of functions here deal with the X 'primary'
700 * (visible - for Visual mode use) selection, and only that. There are no
701 * versions of these for the 'clipboard' selection, as Visual mode has no use
702 * for them.
703 */
704
705#if defined(FEAT_CLIPBOARD) || defined(PROTO)
706
707/*
708 * Selection stuff using Visual mode, for cutting and pasting text to other
709 * windows.
710 */
711
712/*
713 * Call this to initialise the clipboard. Pass it FALSE if the clipboard code
714 * is included, but the clipboard can not be used, or TRUE if the clipboard can
715 * be used. Eg unix may call this with FALSE, then call it again with TRUE if
716 * the GUI starts.
717 */
718 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100719clip_init(int can_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720{
721 VimClipboard *cb;
722
723 cb = &clip_star;
724 for (;;)
725 {
726 cb->available = can_use;
727 cb->owned = FALSE;
728 cb->start.lnum = 0;
729 cb->start.col = 0;
730 cb->end.lnum = 0;
731 cb->end.col = 0;
732 cb->state = SELECT_CLEARED;
733
734 if (cb == &clip_plus)
735 break;
736 cb = &clip_plus;
737 }
738}
739
740/*
741 * Check whether the VIsual area has changed, and if so try to become the owner
742 * of the selection, and free any old converted selection we may still have
743 * lying around. If the VIsual mode has ended, make a copy of what was
744 * selected so we can still give it to others. Will probably have to make sure
745 * this is called whenever VIsual mode is ended.
746 */
747 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100748clip_update_selection(VimClipboard *clip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749{
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200750 pos_T start, end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751
752 /* If visual mode is only due to a redo command ("."), then ignore it */
753 if (!redo_VIsual_busy && VIsual_active && (State & NORMAL))
754 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100755 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 {
757 start = VIsual;
758 end = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000760 end.col += (*mb_ptr2len)(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 }
762 else
763 {
764 start = curwin->w_cursor;
765 end = VIsual;
766 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100767 if (!EQUAL_POS(clip->start, start)
768 || !EQUAL_POS(clip->end, end)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200769 || clip->vmode != VIsual_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200771 clip_clear_selection(clip);
772 clip->start = start;
773 clip->end = end;
774 clip->vmode = VIsual_mode;
775 clip_free_selection(clip);
776 clip_own_selection(clip);
777 clip_gen_set_selection(clip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 }
779 }
780}
781
782 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100783clip_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784{
785 /*
786 * Also want to check somehow that we are reading from the keyboard rather
787 * than a mapping etc.
788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789#ifdef FEAT_X11
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200790 /* Always own the selection, we might have lost it without being
Bram Moolenaar62b42182010-09-21 22:09:37 +0200791 * notified, e.g. during a ":sh" command. */
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200792 if (cbd->available)
793 {
794 int was_owned = cbd->owned;
795
796 cbd->owned = (clip_gen_own_selection(cbd) == OK);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200797 if (!was_owned && (cbd == &clip_star || cbd == &clip_plus))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 {
Bram Moolenaarebefac62005-12-28 22:39:57 +0000799 /* May have to show a different kind of highlighting for the
800 * selected area. There is no specific redraw command for this,
801 * just redraw all windows on the current buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 if (cbd->owned
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000803 && (get_real_state() == VISUAL
804 || get_real_state() == SELECTMODE)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200805 && (cbd == &clip_star ? clip_isautosel_star()
806 : clip_isautosel_plus())
Bram Moolenaar8820b482017-03-16 17:23:31 +0100807 && HL_ATTR(HLF_V) != HL_ATTR(HLF_VNC))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 redraw_curbuf_later(INVERTED_ALL);
809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200811#else
Bram Moolenaarb6101cf2012-10-21 00:58:39 +0200812 /* Only own the clipboard when we didn't own it yet. */
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200813 if (!cbd->owned && cbd->available)
814 cbd->owned = (clip_gen_own_selection(cbd) == OK);
815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816}
817
818 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100819clip_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820{
821#ifdef FEAT_X11
822 int was_owned = cbd->owned;
823#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200824 int visual_selection = FALSE;
825
826 if (cbd == &clip_star || cbd == &clip_plus)
827 visual_selection = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828
829 clip_free_selection(cbd);
830 cbd->owned = FALSE;
831 if (visual_selection)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200832 clip_clear_selection(cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 clip_gen_lose_selection(cbd);
834#ifdef FEAT_X11
835 if (visual_selection)
836 {
837 /* May have to show a different kind of highlighting for the selected
838 * area. There is no specific redraw command for this, just redraw all
839 * windows on the current buffer. */
840 if (was_owned
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000841 && (get_real_state() == VISUAL
842 || get_real_state() == SELECTMODE)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200843 && (cbd == &clip_star ?
844 clip_isautosel_star() : clip_isautosel_plus())
Bram Moolenaar8820b482017-03-16 17:23:31 +0100845 && HL_ATTR(HLF_V) != HL_ATTR(HLF_VNC))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 {
847 update_curbuf(INVERTED_ALL);
848 setcursor();
849 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +0100850 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 }
852 }
853#endif
854}
855
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200856 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100857clip_copy_selection(VimClipboard *clip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858{
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200859 if (VIsual_active && (State & NORMAL) && clip->available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200861 clip_update_selection(clip);
862 clip_free_selection(clip);
863 clip_own_selection(clip);
864 if (clip->owned)
865 clip_get_selection(clip);
866 clip_gen_set_selection(clip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 }
868}
869
870/*
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200871 * Save and restore clip_unnamed before doing possibly many changes. This
872 * prevents accessing the clipboard very often which might slow down Vim
873 * considerably.
874 */
Bram Moolenaar73a156b2015-01-27 21:39:05 +0100875static int global_change_count = 0; /* if set, inside a start_global_changes */
Bram Moolenaar3fcfa352017-03-29 19:20:41 +0200876static int clipboard_needs_update = FALSE; /* clipboard needs to be updated */
877static int clip_did_set_selection = TRUE;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200878
879/*
880 * Save clip_unnamed and reset it.
881 */
882 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100883start_global_changes(void)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200884{
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100885 if (++global_change_count > 1)
886 return;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200887 clip_unnamed_saved = clip_unnamed;
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100888 clipboard_needs_update = FALSE;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200889
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100890 if (clip_did_set_selection)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200891 {
892 clip_unnamed = FALSE;
893 clip_did_set_selection = FALSE;
894 }
895}
896
897/*
Bram Moolenaar3fcfa352017-03-29 19:20:41 +0200898 * Return TRUE if setting the clipboard was postponed, it already contains the
899 * right text.
900 */
901 int
902is_clipboard_needs_update()
903{
904 return clipboard_needs_update;
905}
906
907/*
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200908 * Restore clip_unnamed and set the selection when needed.
909 */
910 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100911end_global_changes(void)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200912{
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100913 if (--global_change_count > 0)
914 /* recursive */
915 return;
916 if (!clip_did_set_selection)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200917 {
918 clip_did_set_selection = TRUE;
919 clip_unnamed = clip_unnamed_saved;
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100920 clip_unnamed_saved = FALSE;
921 if (clipboard_needs_update)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200922 {
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100923 /* only store something in the clipboard,
924 * if we have yanked anything to it */
925 if (clip_unnamed & CLIP_UNNAMED)
926 {
927 clip_own_selection(&clip_star);
928 clip_gen_set_selection(&clip_star);
929 }
930 if (clip_unnamed & CLIP_UNNAMED_PLUS)
931 {
932 clip_own_selection(&clip_plus);
933 clip_gen_set_selection(&clip_plus);
934 }
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200935 }
936 }
Bram Moolenaar3fcfa352017-03-29 19:20:41 +0200937 clipboard_needs_update = FALSE;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200938}
939
940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 * Called when Visual mode is ended: update the selection.
942 */
943 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100944clip_auto_select(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945{
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200946 if (clip_isautosel_star())
947 clip_copy_selection(&clip_star);
948 if (clip_isautosel_plus())
949 clip_copy_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950}
951
952/*
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200953 * Return TRUE if automatic selection of Visual area is desired for the *
954 * register.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 */
956 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100957clip_isautosel_star(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958{
959 return (
960#ifdef FEAT_GUI
961 gui.in_use ? (vim_strchr(p_go, GO_ASEL) != NULL) :
962#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200963 clip_autoselect_star);
964}
965
966/*
967 * Return TRUE if automatic selection of Visual area is desired for the +
968 * register.
969 */
970 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100971clip_isautosel_plus(void)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200972{
973 return (
974#ifdef FEAT_GUI
975 gui.in_use ? (vim_strchr(p_go, GO_ASELPLUS) != NULL) :
976#endif
977 clip_autoselect_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978}
979
980
981/*
982 * Stuff for general mouse selection, without using Visual mode.
983 */
984
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100985static void clip_invert_area(int, int, int, int, int how);
986static void clip_invert_rectangle(int row, int col, int height, int width, int invert);
987static void clip_get_word_boundaries(VimClipboard *, int, int);
988static int clip_get_line_end(int);
989static void clip_update_modeless_selection(VimClipboard *, int, int,
990 int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991
992/* flags for clip_invert_area() */
993#define CLIP_CLEAR 1
994#define CLIP_SET 2
995#define CLIP_TOGGLE 3
996
997/*
998 * Start, continue or end a modeless selection. Used when editing the
999 * command-line and in the cmdline window.
1000 */
1001 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001002clip_modeless(int button, int is_click, int is_drag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003{
1004 int repeat;
1005
1006 repeat = ((clip_star.mode == SELECT_MODE_CHAR
1007 || clip_star.mode == SELECT_MODE_LINE)
1008 && (mod_mask & MOD_MASK_2CLICK))
1009 || (clip_star.mode == SELECT_MODE_WORD
1010 && (mod_mask & MOD_MASK_3CLICK));
1011 if (is_click && button == MOUSE_RIGHT)
1012 {
1013 /* Right mouse button: If there was no selection, start one.
1014 * Otherwise extend the existing selection. */
1015 if (clip_star.state == SELECT_CLEARED)
1016 clip_start_selection(mouse_col, mouse_row, FALSE);
1017 clip_process_selection(button, mouse_col, mouse_row, repeat);
1018 }
1019 else if (is_click)
1020 clip_start_selection(mouse_col, mouse_row, repeat);
1021 else if (is_drag)
1022 {
1023 /* Don't try extending a selection if there isn't one. Happens when
1024 * button-down is in the cmdline and them moving mouse upwards. */
1025 if (clip_star.state != SELECT_CLEARED)
1026 clip_process_selection(button, mouse_col, mouse_row, repeat);
1027 }
1028 else /* release */
1029 clip_process_selection(MOUSE_RELEASE, mouse_col, mouse_row, FALSE);
1030}
1031
1032/*
1033 * Compare two screen positions ala strcmp()
1034 */
1035 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001036clip_compare_pos(
1037 int row1,
1038 int col1,
1039 int row2,
1040 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041{
1042 if (row1 > row2) return(1);
1043 if (row1 < row2) return(-1);
1044 if (col1 > col2) return(1);
1045 if (col1 < col2) return(-1);
Bram Moolenaar9e341102016-02-23 23:04:36 +01001046 return(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047}
1048
1049/*
1050 * Start the selection
1051 */
1052 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001053clip_start_selection(int col, int row, int repeated_click)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054{
1055 VimClipboard *cb = &clip_star;
1056
1057 if (cb->state == SELECT_DONE)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001058 clip_clear_selection(cb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059
1060 row = check_row(row);
1061 col = check_col(col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 col = mb_fix_col(col, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063
1064 cb->start.lnum = row;
1065 cb->start.col = col;
1066 cb->end = cb->start;
1067 cb->origin_row = (short_u)cb->start.lnum;
1068 cb->state = SELECT_IN_PROGRESS;
1069
1070 if (repeated_click)
1071 {
1072 if (++cb->mode > SELECT_MODE_LINE)
1073 cb->mode = SELECT_MODE_CHAR;
1074 }
1075 else
1076 cb->mode = SELECT_MODE_CHAR;
1077
1078#ifdef FEAT_GUI
1079 /* clear the cursor until the selection is made */
1080 if (gui.in_use)
1081 gui_undraw_cursor();
1082#endif
1083
1084 switch (cb->mode)
1085 {
1086 case SELECT_MODE_CHAR:
1087 cb->origin_start_col = cb->start.col;
1088 cb->word_end_col = clip_get_line_end((int)cb->start.lnum);
1089 break;
1090
1091 case SELECT_MODE_WORD:
1092 clip_get_word_boundaries(cb, (int)cb->start.lnum, cb->start.col);
1093 cb->origin_start_col = cb->word_start_col;
1094 cb->origin_end_col = cb->word_end_col;
1095
1096 clip_invert_area((int)cb->start.lnum, cb->word_start_col,
1097 (int)cb->end.lnum, cb->word_end_col, CLIP_SET);
1098 cb->start.col = cb->word_start_col;
1099 cb->end.col = cb->word_end_col;
1100 break;
1101
1102 case SELECT_MODE_LINE:
1103 clip_invert_area((int)cb->start.lnum, 0, (int)cb->start.lnum,
1104 (int)Columns, CLIP_SET);
1105 cb->start.col = 0;
1106 cb->end.col = Columns;
1107 break;
1108 }
1109
1110 cb->prev = cb->start;
1111
1112#ifdef DEBUG_SELECTION
1113 printf("Selection started at (%u,%u)\n", cb->start.lnum, cb->start.col);
1114#endif
1115}
1116
1117/*
1118 * Continue processing the selection
1119 */
1120 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001121clip_process_selection(
1122 int button,
1123 int col,
1124 int row,
1125 int_u repeated_click)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126{
1127 VimClipboard *cb = &clip_star;
1128 int diff;
1129 int slen = 1; /* cursor shape width */
1130
1131 if (button == MOUSE_RELEASE)
1132 {
1133 /* Check to make sure we have something selected */
1134 if (cb->start.lnum == cb->end.lnum && cb->start.col == cb->end.col)
1135 {
1136#ifdef FEAT_GUI
1137 if (gui.in_use)
1138 gui_update_cursor(FALSE, FALSE);
1139#endif
1140 cb->state = SELECT_CLEARED;
1141 return;
1142 }
1143
1144#ifdef DEBUG_SELECTION
1145 printf("Selection ended: (%u,%u) to (%u,%u)\n", cb->start.lnum,
1146 cb->start.col, cb->end.lnum, cb->end.col);
1147#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001148 if (clip_isautosel_star()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 || (
1150#ifdef FEAT_GUI
1151 gui.in_use ? (vim_strchr(p_go, GO_ASELML) != NULL) :
1152#endif
1153 clip_autoselectml))
1154 clip_copy_modeless_selection(FALSE);
1155#ifdef FEAT_GUI
1156 if (gui.in_use)
1157 gui_update_cursor(FALSE, FALSE);
1158#endif
1159
1160 cb->state = SELECT_DONE;
1161 return;
1162 }
1163
1164 row = check_row(row);
1165 col = check_col(col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 col = mb_fix_col(col, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167
1168 if (col == (int)cb->prev.col && row == cb->prev.lnum && !repeated_click)
1169 return;
1170
1171 /*
1172 * When extending the selection with the right mouse button, swap the
1173 * start and end if the position is before half the selection
1174 */
1175 if (cb->state == SELECT_DONE && button == MOUSE_RIGHT)
1176 {
1177 /*
1178 * If the click is before the start, or the click is inside the
1179 * selection and the start is the closest side, set the origin to the
1180 * end of the selection.
1181 */
1182 if (clip_compare_pos(row, col, (int)cb->start.lnum, cb->start.col) < 0
1183 || (clip_compare_pos(row, col,
1184 (int)cb->end.lnum, cb->end.col) < 0
1185 && (((cb->start.lnum == cb->end.lnum
1186 && cb->end.col - col > col - cb->start.col))
1187 || ((diff = (cb->end.lnum - row) -
1188 (row - cb->start.lnum)) > 0
1189 || (diff == 0 && col < (int)(cb->start.col +
1190 cb->end.col) / 2)))))
1191 {
1192 cb->origin_row = (short_u)cb->end.lnum;
1193 cb->origin_start_col = cb->end.col - 1;
1194 cb->origin_end_col = cb->end.col;
1195 }
1196 else
1197 {
1198 cb->origin_row = (short_u)cb->start.lnum;
1199 cb->origin_start_col = cb->start.col;
1200 cb->origin_end_col = cb->start.col;
1201 }
1202 if (cb->mode == SELECT_MODE_WORD && !repeated_click)
1203 cb->mode = SELECT_MODE_CHAR;
1204 }
1205
1206 /* set state, for when using the right mouse button */
1207 cb->state = SELECT_IN_PROGRESS;
1208
1209#ifdef DEBUG_SELECTION
1210 printf("Selection extending to (%d,%d)\n", row, col);
1211#endif
1212
1213 if (repeated_click && ++cb->mode > SELECT_MODE_LINE)
1214 cb->mode = SELECT_MODE_CHAR;
1215
1216 switch (cb->mode)
1217 {
1218 case SELECT_MODE_CHAR:
1219 /* If we're on a different line, find where the line ends */
1220 if (row != cb->prev.lnum)
1221 cb->word_end_col = clip_get_line_end(row);
1222
1223 /* See if we are before or after the origin of the selection */
1224 if (clip_compare_pos(row, col, cb->origin_row,
1225 cb->origin_start_col) >= 0)
1226 {
1227 if (col >= (int)cb->word_end_col)
1228 clip_update_modeless_selection(cb, cb->origin_row,
1229 cb->origin_start_col, row, (int)Columns);
1230 else
1231 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 if (has_mbyte && mb_lefthalve(row, col))
1233 slen = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 clip_update_modeless_selection(cb, cb->origin_row,
1235 cb->origin_start_col, row, col + slen);
1236 }
1237 }
1238 else
1239 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 if (has_mbyte
1241 && mb_lefthalve(cb->origin_row, cb->origin_start_col))
1242 slen = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 if (col >= (int)cb->word_end_col)
1244 clip_update_modeless_selection(cb, row, cb->word_end_col,
1245 cb->origin_row, cb->origin_start_col + slen);
1246 else
1247 clip_update_modeless_selection(cb, row, col,
1248 cb->origin_row, cb->origin_start_col + slen);
1249 }
1250 break;
1251
1252 case SELECT_MODE_WORD:
1253 /* If we are still within the same word, do nothing */
1254 if (row == cb->prev.lnum && col >= (int)cb->word_start_col
1255 && col < (int)cb->word_end_col && !repeated_click)
1256 return;
1257
1258 /* Get new word boundaries */
1259 clip_get_word_boundaries(cb, row, col);
1260
1261 /* Handle being after the origin point of selection */
1262 if (clip_compare_pos(row, col, cb->origin_row,
1263 cb->origin_start_col) >= 0)
1264 clip_update_modeless_selection(cb, cb->origin_row,
1265 cb->origin_start_col, row, cb->word_end_col);
1266 else
1267 clip_update_modeless_selection(cb, row, cb->word_start_col,
1268 cb->origin_row, cb->origin_end_col);
1269 break;
1270
1271 case SELECT_MODE_LINE:
1272 if (row == cb->prev.lnum && !repeated_click)
1273 return;
1274
1275 if (clip_compare_pos(row, col, cb->origin_row,
1276 cb->origin_start_col) >= 0)
1277 clip_update_modeless_selection(cb, cb->origin_row, 0, row,
1278 (int)Columns);
1279 else
1280 clip_update_modeless_selection(cb, row, 0, cb->origin_row,
1281 (int)Columns);
1282 break;
1283 }
1284
1285 cb->prev.lnum = row;
1286 cb->prev.col = col;
1287
1288#ifdef DEBUG_SELECTION
1289 printf("Selection is: (%u,%u) to (%u,%u)\n", cb->start.lnum,
1290 cb->start.col, cb->end.lnum, cb->end.col);
1291#endif
1292}
1293
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294# if defined(FEAT_GUI) || defined(PROTO)
1295/*
1296 * Redraw part of the selection if character at "row,col" is inside of it.
1297 * Only used for the GUI.
1298 */
1299 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001300clip_may_redraw_selection(int row, int col, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301{
1302 int start = col;
1303 int end = col + len;
1304
1305 if (clip_star.state != SELECT_CLEARED
1306 && row >= clip_star.start.lnum
1307 && row <= clip_star.end.lnum)
1308 {
1309 if (row == clip_star.start.lnum && start < (int)clip_star.start.col)
1310 start = clip_star.start.col;
1311 if (row == clip_star.end.lnum && end > (int)clip_star.end.col)
1312 end = clip_star.end.col;
1313 if (end > start)
1314 clip_invert_area(row, start, row, end, 0);
1315 }
1316}
1317# endif
1318
1319/*
1320 * Called from outside to clear selected region from the display
1321 */
1322 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001323clip_clear_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001326 if (cbd->state == SELECT_CLEARED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 return;
1328
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001329 clip_invert_area((int)cbd->start.lnum, cbd->start.col, (int)cbd->end.lnum,
1330 cbd->end.col, CLIP_CLEAR);
1331 cbd->state = SELECT_CLEARED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332}
1333
1334/*
1335 * Clear the selection if any lines from "row1" to "row2" are inside of it.
1336 */
1337 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001338clip_may_clear_selection(int row1, int row2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339{
1340 if (clip_star.state == SELECT_DONE
1341 && row2 >= clip_star.start.lnum
1342 && row1 <= clip_star.end.lnum)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001343 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344}
1345
1346/*
1347 * Called before the screen is scrolled up or down. Adjusts the line numbers
1348 * of the selection. Call with big number when clearing the screen.
1349 */
1350 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001351clip_scroll_selection(
1352 int rows) /* negative for scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353{
1354 int lnum;
1355
1356 if (clip_star.state == SELECT_CLEARED)
1357 return;
1358
1359 lnum = clip_star.start.lnum - rows;
1360 if (lnum <= 0)
1361 clip_star.start.lnum = 0;
1362 else if (lnum >= screen_Rows) /* scrolled off of the screen */
1363 clip_star.state = SELECT_CLEARED;
1364 else
1365 clip_star.start.lnum = lnum;
1366
1367 lnum = clip_star.end.lnum - rows;
1368 if (lnum < 0) /* scrolled off of the screen */
1369 clip_star.state = SELECT_CLEARED;
1370 else if (lnum >= screen_Rows)
1371 clip_star.end.lnum = screen_Rows - 1;
1372 else
1373 clip_star.end.lnum = lnum;
1374}
1375
1376/*
1377 * Invert a region of the display between a starting and ending row and column
1378 * Values for "how":
1379 * CLIP_CLEAR: undo inversion
1380 * CLIP_SET: set inversion
1381 * CLIP_TOGGLE: set inversion if pos1 < pos2, undo inversion otherwise.
1382 * 0: invert (GUI only).
1383 */
1384 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001385clip_invert_area(
1386 int row1,
1387 int col1,
1388 int row2,
1389 int col2,
1390 int how)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391{
1392 int invert = FALSE;
1393
1394 if (how == CLIP_SET)
1395 invert = TRUE;
1396
1397 /* Swap the from and to positions so the from is always before */
1398 if (clip_compare_pos(row1, col1, row2, col2) > 0)
1399 {
1400 int tmp_row, tmp_col;
1401
1402 tmp_row = row1;
1403 tmp_col = col1;
1404 row1 = row2;
1405 col1 = col2;
1406 row2 = tmp_row;
1407 col2 = tmp_col;
1408 }
1409 else if (how == CLIP_TOGGLE)
1410 invert = TRUE;
1411
1412 /* If all on the same line, do it the easy way */
1413 if (row1 == row2)
1414 {
1415 clip_invert_rectangle(row1, col1, 1, col2 - col1, invert);
1416 }
1417 else
1418 {
1419 /* Handle a piece of the first line */
1420 if (col1 > 0)
1421 {
1422 clip_invert_rectangle(row1, col1, 1, (int)Columns - col1, invert);
1423 row1++;
1424 }
1425
1426 /* Handle a piece of the last line */
1427 if (col2 < Columns - 1)
1428 {
1429 clip_invert_rectangle(row2, 0, 1, col2, invert);
1430 row2--;
1431 }
1432
1433 /* Handle the rectangle thats left */
1434 if (row2 >= row1)
1435 clip_invert_rectangle(row1, 0, row2 - row1 + 1, (int)Columns,
1436 invert);
1437 }
1438}
1439
1440/*
1441 * Invert or un-invert a rectangle of the screen.
1442 * "invert" is true if the result is inverted.
1443 */
1444 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001445clip_invert_rectangle(
1446 int row,
1447 int col,
1448 int height,
1449 int width,
1450 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451{
1452#ifdef FEAT_GUI
1453 if (gui.in_use)
1454 gui_mch_invert_rectangle(row, col, height, width);
1455 else
1456#endif
1457 screen_draw_rectangle(row, col, height, width, invert);
1458}
1459
1460/*
1461 * Copy the currently selected area into the '*' register so it will be
1462 * available for pasting.
1463 * When "both" is TRUE also copy to the '+' register.
1464 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001466clip_copy_modeless_selection(int both UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467{
1468 char_u *buffer;
1469 char_u *bufp;
1470 int row;
1471 int start_col;
1472 int end_col;
1473 int line_end_col;
1474 int add_newline_flag = FALSE;
1475 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 int row1 = clip_star.start.lnum;
1478 int col1 = clip_star.start.col;
1479 int row2 = clip_star.end.lnum;
1480 int col2 = clip_star.end.col;
1481
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001482 /* Can't use ScreenLines unless initialized */
1483 if (ScreenLines == NULL)
1484 return;
1485
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 /*
1487 * Make sure row1 <= row2, and if row1 == row2 that col1 <= col2.
1488 */
1489 if (row1 > row2)
1490 {
1491 row = row1; row1 = row2; row2 = row;
1492 row = col1; col1 = col2; col2 = row;
1493 }
1494 else if (row1 == row2 && col1 > col2)
1495 {
1496 row = col1; col1 = col2; col2 = row;
1497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 /* correct starting point for being on right halve of double-wide char */
1499 p = ScreenLines + LineOffset[row1];
1500 if (enc_dbcs != 0)
1501 col1 -= (*mb_head_off)(p, p + col1);
1502 else if (enc_utf8 && p[col1] == 0)
1503 --col1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504
1505 /* Create a temporary buffer for storing the text */
1506 len = (row2 - row1 + 1) * Columns + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 if (enc_dbcs != 0)
1508 len *= 2; /* max. 2 bytes per display cell */
1509 else if (enc_utf8)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001510 len *= MB_MAXBYTES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 buffer = lalloc((long_u)len, TRUE);
1512 if (buffer == NULL) /* out of memory */
1513 return;
1514
1515 /* Process each row in the selection */
1516 for (bufp = buffer, row = row1; row <= row2; row++)
1517 {
1518 if (row == row1)
1519 start_col = col1;
1520 else
1521 start_col = 0;
1522
1523 if (row == row2)
1524 end_col = col2;
1525 else
1526 end_col = Columns;
1527
1528 line_end_col = clip_get_line_end(row);
1529
1530 /* See if we need to nuke some trailing whitespace */
1531 if (end_col >= Columns && (row < row2 || end_col > line_end_col))
1532 {
1533 /* Get rid of trailing whitespace */
1534 end_col = line_end_col;
1535 if (end_col < start_col)
1536 end_col = start_col;
1537
1538 /* If the last line extended to the end, add an extra newline */
1539 if (row == row2)
1540 add_newline_flag = TRUE;
1541 }
1542
1543 /* If after the first row, we need to always add a newline */
1544 if (row > row1 && !LineWraps[row - 1])
1545 *bufp++ = NL;
1546
1547 if (row < screen_Rows && end_col <= screen_Columns)
1548 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 if (enc_dbcs != 0)
1550 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001551 int i;
1552
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 p = ScreenLines + LineOffset[row];
1554 for (i = start_col; i < end_col; ++i)
1555 if (enc_dbcs == DBCS_JPNU && p[i] == 0x8e)
1556 {
1557 /* single-width double-byte char */
1558 *bufp++ = 0x8e;
1559 *bufp++ = ScreenLines2[LineOffset[row] + i];
1560 }
1561 else
1562 {
1563 *bufp++ = p[i];
1564 if (MB_BYTE2LEN(p[i]) == 2)
1565 *bufp++ = p[++i];
1566 }
1567 }
1568 else if (enc_utf8)
1569 {
1570 int off;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001571 int i;
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001572 int ci;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
1574 off = LineOffset[row];
1575 for (i = start_col; i < end_col; ++i)
1576 {
1577 /* The base character is either in ScreenLinesUC[] or
1578 * ScreenLines[]. */
1579 if (ScreenLinesUC[off + i] == 0)
1580 *bufp++ = ScreenLines[off + i];
1581 else
1582 {
1583 bufp += utf_char2bytes(ScreenLinesUC[off + i], bufp);
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001584 for (ci = 0; ci < Screen_mco; ++ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001586 /* Add a composing character. */
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001587 if (ScreenLinesC[ci][off + i] == 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001588 break;
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001589 bufp += utf_char2bytes(ScreenLinesC[ci][off + i],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 }
1592 }
1593 /* Skip right halve of double-wide character. */
1594 if (ScreenLines[off + i + 1] == 0)
1595 ++i;
1596 }
1597 }
1598 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 {
1600 STRNCPY(bufp, ScreenLines + LineOffset[row] + start_col,
1601 end_col - start_col);
1602 bufp += end_col - start_col;
1603 }
1604 }
1605 }
1606
1607 /* Add a newline at the end if the selection ended there */
1608 if (add_newline_flag)
1609 *bufp++ = NL;
1610
1611 /* First cleanup any old selection and become the owner. */
1612 clip_free_selection(&clip_star);
1613 clip_own_selection(&clip_star);
1614
1615 /* Yank the text into the '*' register. */
1616 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_star);
1617
1618 /* Make the register contents available to the outside world. */
1619 clip_gen_set_selection(&clip_star);
1620
1621#ifdef FEAT_X11
1622 if (both)
1623 {
1624 /* Do the same for the '+' register. */
1625 clip_free_selection(&clip_plus);
1626 clip_own_selection(&clip_plus);
1627 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_plus);
1628 clip_gen_set_selection(&clip_plus);
1629 }
1630#endif
1631 vim_free(buffer);
1632}
1633
1634/*
1635 * Find the starting and ending positions of the word at the given row and
1636 * column. Only white-separated words are recognized here.
1637 */
1638#define CHAR_CLASS(c) (c <= ' ' ? ' ' : vim_iswordc(c))
1639
1640 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001641clip_get_word_boundaries(VimClipboard *cb, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642{
1643 int start_class;
1644 int temp_col;
1645 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 int mboff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001648 if (row >= screen_Rows || col >= screen_Columns || ScreenLines == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 return;
1650
1651 p = ScreenLines + LineOffset[row];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 /* Correct for starting in the right halve of a double-wide char */
1653 if (enc_dbcs != 0)
1654 col -= dbcs_screen_head_off(p, p + col);
1655 else if (enc_utf8 && p[col] == 0)
1656 --col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 start_class = CHAR_CLASS(p[col]);
1658
1659 temp_col = col;
1660 for ( ; temp_col > 0; temp_col--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 if (enc_dbcs != 0
1662 && (mboff = dbcs_screen_head_off(p, p + temp_col - 1)) > 0)
1663 temp_col -= mboff;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01001664 else if (CHAR_CLASS(p[temp_col - 1]) != start_class
1665 && !(enc_utf8 && p[temp_col - 1] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 break;
1667 cb->word_start_col = temp_col;
1668
1669 temp_col = col;
1670 for ( ; temp_col < screen_Columns; temp_col++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 if (enc_dbcs != 0 && dbcs_ptr2cells(p + temp_col) == 2)
1672 ++temp_col;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01001673 else if (CHAR_CLASS(p[temp_col]) != start_class
1674 && !(enc_utf8 && p[temp_col] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 break;
1676 cb->word_end_col = temp_col;
1677}
1678
1679/*
1680 * Find the column position for the last non-whitespace character on the given
1681 * line.
1682 */
1683 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001684clip_get_line_end(int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685{
1686 int i;
1687
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001688 if (row >= screen_Rows || ScreenLines == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 return 0;
1690 for (i = screen_Columns; i > 0; i--)
1691 if (ScreenLines[LineOffset[row] + i - 1] != ' ')
1692 break;
1693 return i;
1694}
1695
1696/*
1697 * Update the currently selected region by adding and/or subtracting from the
1698 * beginning or end and inverting the changed area(s).
1699 */
1700 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001701clip_update_modeless_selection(
1702 VimClipboard *cb,
1703 int row1,
1704 int col1,
1705 int row2,
1706 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707{
1708 /* See if we changed at the beginning of the selection */
1709 if (row1 != cb->start.lnum || col1 != (int)cb->start.col)
1710 {
1711 clip_invert_area(row1, col1, (int)cb->start.lnum, cb->start.col,
1712 CLIP_TOGGLE);
1713 cb->start.lnum = row1;
1714 cb->start.col = col1;
1715 }
1716
1717 /* See if we changed at the end of the selection */
1718 if (row2 != cb->end.lnum || col2 != (int)cb->end.col)
1719 {
1720 clip_invert_area((int)cb->end.lnum, cb->end.col, row2, col2,
1721 CLIP_TOGGLE);
1722 cb->end.lnum = row2;
1723 cb->end.col = col2;
1724 }
1725}
1726
1727 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001728clip_gen_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729{
1730#ifdef FEAT_XCLIPBOARD
1731# ifdef FEAT_GUI
1732 if (gui.in_use)
1733 return clip_mch_own_selection(cbd);
1734 else
1735# endif
1736 return clip_xterm_own_selection(cbd);
1737#else
1738 return clip_mch_own_selection(cbd);
1739#endif
1740}
1741
1742 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001743clip_gen_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744{
1745#ifdef FEAT_XCLIPBOARD
1746# ifdef FEAT_GUI
1747 if (gui.in_use)
1748 clip_mch_lose_selection(cbd);
1749 else
1750# endif
1751 clip_xterm_lose_selection(cbd);
1752#else
1753 clip_mch_lose_selection(cbd);
1754#endif
1755}
1756
1757 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001758clip_gen_set_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759{
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001760 if (!clip_did_set_selection)
1761 {
1762 /* Updating postponed, so that accessing the system clipboard won't
Bram Moolenaarbdace832019-03-02 10:13:42 +01001763 * hang Vim when accessing it many times (e.g. on a :g command). */
Bram Moolenaar5c27fd12015-01-27 14:09:37 +01001764 if ((cbd == &clip_plus && (clip_unnamed_saved & CLIP_UNNAMED_PLUS))
1765 || (cbd == &clip_star && (clip_unnamed_saved & CLIP_UNNAMED)))
1766 {
1767 clipboard_needs_update = TRUE;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001768 return;
Bram Moolenaar5c27fd12015-01-27 14:09:37 +01001769 }
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771#ifdef FEAT_XCLIPBOARD
1772# ifdef FEAT_GUI
1773 if (gui.in_use)
1774 clip_mch_set_selection(cbd);
1775 else
1776# endif
1777 clip_xterm_set_selection(cbd);
1778#else
1779 clip_mch_set_selection(cbd);
1780#endif
1781}
1782
1783 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001784clip_gen_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785{
1786#ifdef FEAT_XCLIPBOARD
1787# ifdef FEAT_GUI
1788 if (gui.in_use)
1789 clip_mch_request_selection(cbd);
1790 else
1791# endif
1792 clip_xterm_request_selection(cbd);
1793#else
1794 clip_mch_request_selection(cbd);
1795#endif
1796}
1797
Bram Moolenaar113e1072019-01-20 15:30:40 +01001798#if (defined(FEAT_X11) && defined(USE_SYSTEM)) || defined(PROTO)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001799 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001800clip_gen_owner_exists(VimClipboard *cbd UNUSED)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001801{
1802#ifdef FEAT_XCLIPBOARD
1803# ifdef FEAT_GUI_GTK
1804 if (gui.in_use)
1805 return clip_gtk_owner_exists(cbd);
1806 else
1807# endif
1808 return clip_x11_owner_exists(cbd);
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02001809#else
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001810 return TRUE;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02001811#endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001812}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001813#endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001814
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815#endif /* FEAT_CLIPBOARD */
1816
1817/*****************************************************************************
1818 * Functions that handle the input buffer.
1819 * This is used for any GUI version, and the unix terminal version.
1820 *
1821 * For Unix, the input characters are buffered to be able to check for a
1822 * CTRL-C. This should be done with signals, but I don't know how to do that
1823 * in a portable way for a tty in RAW mode.
1824 *
1825 * For the client-server code in the console the received keys are put in the
1826 * input buffer.
1827 */
1828
1829#if defined(USE_INPUT_BUF) || defined(PROTO)
1830
1831/*
1832 * Internal typeahead buffer. Includes extra space for long key code
1833 * descriptions which would otherwise overflow. The buffer is considered full
1834 * when only this extra space (or part of it) remains.
1835 */
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001836#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_CLIENTSERVER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 /*
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001838 * NetBeans stuffs debugger commands into the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 * This requires a larger buffer...
1840 * (Madsen) Go with this for remote input as well ...
1841 */
1842# define INBUFLEN 4096
1843#else
1844# define INBUFLEN 250
1845#endif
1846
1847static char_u inbuf[INBUFLEN + MAX_KEY_CODE_LEN];
1848static int inbufcount = 0; /* number of chars in inbuf[] */
1849
1850/*
1851 * vim_is_input_buf_full(), vim_is_input_buf_empty(), add_to_input_buf(), and
1852 * trash_input_buf() are functions for manipulating the input buffer. These
1853 * are used by the gui_* calls when a GUI is used to handle keyboard input.
1854 */
1855
1856 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001857vim_is_input_buf_full(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858{
1859 return (inbufcount >= INBUFLEN);
1860}
1861
1862 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001863vim_is_input_buf_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864{
1865 return (inbufcount == 0);
1866}
1867
1868#if defined(FEAT_OLE) || defined(PROTO)
1869 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001870vim_free_in_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871{
1872 return (INBUFLEN - inbufcount);
1873}
1874#endif
1875
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001876#if defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001878vim_used_in_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879{
1880 return inbufcount;
1881}
1882#endif
1883
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884/*
1885 * Return the current contents of the input buffer and make it empty.
1886 * The returned pointer must be passed to set_input_buf() later.
1887 */
1888 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001889get_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890{
1891 garray_T *gap;
1892
1893 /* We use a growarray to store the data pointer and the length. */
1894 gap = (garray_T *)alloc((unsigned)sizeof(garray_T));
1895 if (gap != NULL)
1896 {
1897 /* Add one to avoid a zero size. */
1898 gap->ga_data = alloc((unsigned)inbufcount + 1);
1899 if (gap->ga_data != NULL)
1900 mch_memmove(gap->ga_data, inbuf, (size_t)inbufcount);
1901 gap->ga_len = inbufcount;
1902 }
1903 trash_input_buf();
1904 return (char_u *)gap;
1905}
1906
1907/*
1908 * Restore the input buffer with a pointer returned from get_input_buf().
1909 * The allocated memory is freed, this only works once!
1910 */
1911 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001912set_input_buf(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913{
1914 garray_T *gap = (garray_T *)p;
1915
1916 if (gap != NULL)
1917 {
1918 if (gap->ga_data != NULL)
1919 {
1920 mch_memmove(inbuf, gap->ga_data, gap->ga_len);
1921 inbufcount = gap->ga_len;
1922 vim_free(gap->ga_data);
1923 }
1924 vim_free(gap);
1925 }
1926}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928/*
1929 * Add the given bytes to the input buffer
1930 * Special keys start with CSI. A real CSI must have been translated to
1931 * CSI KS_EXTRA KE_CSI. K_SPECIAL doesn't require translation.
1932 */
1933 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001934add_to_input_buf(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935{
1936 if (inbufcount + len > INBUFLEN + MAX_KEY_CODE_LEN)
1937 return; /* Shouldn't ever happen! */
1938
1939#ifdef FEAT_HANGULIN
1940 if ((State & (INSERT|CMDLINE)) && hangul_input_state_get())
1941 if ((len = hangul_input_process(s, len)) == 0)
1942 return;
1943#endif
1944
1945 while (len--)
1946 inbuf[inbufcount++] = *s++;
1947}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949/*
1950 * Add "str[len]" to the input buffer while escaping CSI bytes.
1951 */
1952 void
1953add_to_input_buf_csi(char_u *str, int len)
1954{
1955 int i;
1956 char_u buf[2];
1957
1958 for (i = 0; i < len; ++i)
1959 {
1960 add_to_input_buf(str + i, 1);
1961 if (str[i] == CSI)
1962 {
1963 /* Turn CSI into K_CSI. */
1964 buf[0] = KS_EXTRA;
1965 buf[1] = (int)KE_CSI;
1966 add_to_input_buf(buf, 2);
1967 }
1968 }
1969}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970
1971#if defined(FEAT_HANGULIN) || defined(PROTO)
1972 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001973push_raw_key(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974{
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001975 char_u *tmpbuf;
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001976 char_u *inp = s;
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001977
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001978 /* use the conversion result if possible */
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001979 tmpbuf = hangul_string_convert(s, &len);
1980 if (tmpbuf != NULL)
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001981 inp = tmpbuf;
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001982
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001983 for (; len--; inp++)
1984 {
1985 inbuf[inbufcount++] = *inp;
1986 if (*inp == CSI)
Bram Moolenaar00ded432016-03-03 11:45:15 +01001987 {
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001988 /* Turn CSI into K_CSI. */
1989 inbuf[inbufcount++] = KS_EXTRA;
1990 inbuf[inbufcount++] = (int)KE_CSI;
Bram Moolenaar00ded432016-03-03 11:45:15 +01001991 }
Bram Moolenaar00ded432016-03-03 11:45:15 +01001992 }
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001993 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994}
1995#endif
1996
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997/* Remove everything from the input buffer. Called when ^C is found */
1998 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001999trash_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000{
2001 inbufcount = 0;
2002}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003
2004/*
2005 * Read as much data from the input buffer as possible up to maxlen, and store
2006 * it in buf.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 */
2008 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002009read_from_input_buf(char_u *buf, long maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010{
2011 if (inbufcount == 0) /* if the buffer is empty, fill it */
2012 fill_input_buf(TRUE);
2013 if (maxlen > inbufcount)
2014 maxlen = inbufcount;
2015 mch_memmove(buf, inbuf, (size_t)maxlen);
2016 inbufcount -= maxlen;
2017 if (inbufcount)
2018 mch_memmove(inbuf, inbuf + maxlen, (size_t)inbufcount);
2019 return (int)maxlen;
2020}
2021
2022 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002023fill_input_buf(int exit_on_error UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024{
Bram Moolenaard0573012017-10-28 21:11:06 +02002025#if defined(UNIX) || defined(VMS) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 int len;
2027 int try;
2028 static int did_read_something = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 static char_u *rest = NULL; /* unconverted rest of previous read */
2030 static int restlen = 0;
2031 int unconverted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032#endif
2033
2034#ifdef FEAT_GUI
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002035 if (gui.in_use
2036# ifdef NO_CONSOLE_INPUT
2037 /* Don't use the GUI input when the window hasn't been opened yet.
2038 * We get here from ui_inchar() when we should try reading from stdin. */
2039 && !no_console_input()
2040# endif
2041 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 {
2043 gui_mch_update();
2044 return;
2045 }
2046#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02002047#if defined(UNIX) || defined(VMS) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 if (vim_is_input_buf_full())
2049 return;
2050 /*
2051 * Fill_input_buf() is only called when we really need a character.
2052 * If we can't get any, but there is some in the buffer, just return.
2053 * If we can't get any, and there isn't any in the buffer, we give up and
2054 * exit Vim.
2055 */
2056# ifdef __BEOS__
2057 /*
2058 * On the BeBox version (for now), all input is secretly performed within
2059 * beos_select() which is called from RealWaitForChar().
2060 */
2061 while (!vim_is_input_buf_full() && RealWaitForChar(read_cmd_fd, 0, NULL))
2062 ;
2063 len = inbufcount;
2064 inbufcount = 0;
2065# else
2066
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 if (rest != NULL)
2068 {
2069 /* Use remainder of previous call, starts with an invalid character
2070 * that may become valid when reading more. */
2071 if (restlen > INBUFLEN - inbufcount)
2072 unconverted = INBUFLEN - inbufcount;
2073 else
2074 unconverted = restlen;
2075 mch_memmove(inbuf + inbufcount, rest, unconverted);
2076 if (unconverted == restlen)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002077 VIM_CLEAR(rest);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 else
2079 {
2080 restlen -= unconverted;
2081 mch_memmove(rest, rest + unconverted, restlen);
2082 }
2083 inbufcount += unconverted;
2084 }
2085 else
2086 unconverted = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087
2088 len = 0; /* to avoid gcc warning */
2089 for (try = 0; try < 100; ++try)
2090 {
Bram Moolenaar4e601e32018-04-24 13:29:51 +02002091 size_t readlen = (size_t)((INBUFLEN - inbufcount)
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002092 / input_conv.vc_factor);
Bram Moolenaar4e601e32018-04-24 13:29:51 +02002093# ifdef VMS
Bram Moolenaar49943732018-04-24 20:27:26 +02002094 len = vms_read((char *)inbuf + inbufcount, readlen);
Bram Moolenaar4e601e32018-04-24 13:29:51 +02002095# else
2096 len = read(read_cmd_fd, (char *)inbuf + inbufcount, readlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097# endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002098
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 if (len > 0 || got_int)
2100 break;
2101 /*
2102 * If reading stdin results in an error, continue reading stderr.
2103 * This helps when using "foo | xargs vim".
2104 */
2105 if (!did_read_something && !isatty(read_cmd_fd) && read_cmd_fd == 0)
2106 {
2107 int m = cur_tmode;
2108
2109 /* We probably set the wrong file descriptor to raw mode. Switch
2110 * back to cooked mode, use another descriptor and set the mode to
2111 * what it was. */
2112 settmode(TMODE_COOK);
2113#ifdef HAVE_DUP
2114 /* Use stderr for stdin, also works for shell commands. */
2115 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002116 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117#else
2118 read_cmd_fd = 2; /* read from stderr instead of stdin */
2119#endif
2120 settmode(m);
2121 }
2122 if (!exit_on_error)
2123 return;
2124 }
2125# endif
2126 if (len <= 0 && !got_int)
2127 read_error_exit();
2128 if (len > 0)
2129 did_read_something = TRUE;
2130 if (got_int)
2131 {
2132 /* Interrupted, pretend a CTRL-C was typed. */
2133 inbuf[0] = 3;
2134 inbufcount = 1;
2135 }
2136 else
2137 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 /*
2139 * May perform conversion on the input characters.
2140 * Include the unconverted rest of the previous call.
2141 * If there is an incomplete char at the end it is kept for the next
2142 * time, reading more bytes should make conversion possible.
2143 * Don't do this in the unlikely event that the input buffer is too
2144 * small ("rest" still contains more bytes).
2145 */
2146 if (input_conv.vc_type != CONV_NONE)
2147 {
2148 inbufcount -= unconverted;
2149 len = convert_input_safe(inbuf + inbufcount,
2150 len + unconverted, INBUFLEN - inbufcount,
2151 rest == NULL ? &rest : NULL, &restlen);
2152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 while (len-- > 0)
2154 {
2155 /*
2156 * if a CTRL-C was typed, remove it from the buffer and set got_int
2157 */
2158 if (inbuf[inbufcount] == 3 && ctrl_c_interrupts)
2159 {
2160 /* remove everything typed before the CTRL-C */
2161 mch_memmove(inbuf, inbuf + inbufcount, (size_t)(len + 1));
2162 inbufcount = 0;
2163 got_int = TRUE;
2164 }
2165 ++inbufcount;
2166 }
2167 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002168#endif /* UNIX or VMS*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169}
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002170#endif /* defined(UNIX) || defined(FEAT_GUI) || defined(VMS) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171
2172/*
2173 * Exit because of an input read error.
2174 */
2175 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002176read_error_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177{
2178 if (silent_mode) /* Normal way to exit for "ex -s" */
2179 getout(0);
2180 STRCPY(IObuff, _("Vim: Error reading input, exiting...\n"));
2181 preserve_exit();
2182}
2183
2184#if defined(CURSOR_SHAPE) || defined(PROTO)
2185/*
2186 * May update the shape of the cursor.
2187 */
2188 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002189ui_cursor_shape_forced(int forced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190{
2191# ifdef FEAT_GUI
2192 if (gui.in_use)
2193 gui_update_cursor_later();
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002194 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002196 term_cursor_mode(forced);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002197
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198# ifdef MCH_CURSOR_SHAPE
2199 mch_update_cursor();
2200# endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002201
2202# ifdef FEAT_CONCEAL
Bram Moolenaarb9464822018-05-10 15:09:49 +02002203 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002204# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002206
2207 void
2208ui_cursor_shape(void)
2209{
2210 ui_cursor_shape_forced(FALSE);
2211}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212#endif
2213
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214/*
2215 * Check bounds for column number
2216 */
2217 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002218check_col(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219{
2220 if (col < 0)
2221 return 0;
2222 if (col >= (int)screen_Columns)
2223 return (int)screen_Columns - 1;
2224 return col;
2225}
2226
2227/*
2228 * Check bounds for row number
2229 */
2230 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002231check_row(int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232{
2233 if (row < 0)
2234 return 0;
2235 if (row >= (int)screen_Rows)
2236 return (int)screen_Rows - 1;
2237 return row;
2238}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239
2240/*
2241 * Stuff for the X clipboard. Shared between VMS and Unix.
2242 */
2243
2244#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) || defined(PROTO)
2245# include <X11/Xatom.h>
2246# include <X11/Intrinsic.h>
2247
2248/*
2249 * Open the application context (if it hasn't been opened yet).
2250 * Used for Motif and Athena GUI and the xterm clipboard.
2251 */
2252 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002253open_app_context(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254{
2255 if (app_context == NULL)
2256 {
2257 XtToolkitInitialize();
2258 app_context = XtCreateApplicationContext();
2259 }
2260}
2261
2262static Atom vim_atom; /* Vim's own special selection format */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263static Atom vimenc_atom; /* Vim's extended selection format */
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002264static Atom utf8_atom;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265static Atom compound_text_atom;
2266static Atom text_atom;
2267static Atom targets_atom;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002268static Atom timestamp_atom; /* Used to get a timestamp */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269
2270 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002271x11_setup_atoms(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272{
2273 vim_atom = XInternAtom(dpy, VIM_ATOM_NAME, False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 vimenc_atom = XInternAtom(dpy, VIMENC_ATOM_NAME,False);
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002275 utf8_atom = XInternAtom(dpy, "UTF8_STRING", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 compound_text_atom = XInternAtom(dpy, "COMPOUND_TEXT", False);
2277 text_atom = XInternAtom(dpy, "TEXT", False);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002278 targets_atom = XInternAtom(dpy, "TARGETS", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 clip_star.sel_atom = XA_PRIMARY;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002280 clip_plus.sel_atom = XInternAtom(dpy, "CLIPBOARD", False);
2281 timestamp_atom = XInternAtom(dpy, "TIMESTAMP", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282}
2283
2284/*
2285 * X Selection stuff, for cutting and pasting text to other windows.
2286 */
2287
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002288static Boolean clip_x11_convert_selection_cb(Widget w, Atom *sel_atom, Atom *target, Atom *type, XtPointer *value, long_u *length, int *format);
2289static void clip_x11_lose_ownership_cb(Widget w, Atom *sel_atom);
2290static void clip_x11_notify_cb(Widget w, Atom *sel_atom, Atom *target);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002291
2292/*
2293 * Property callback to get a timestamp for XtOwnSelection.
2294 */
2295 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002296clip_x11_timestamp_cb(
2297 Widget w,
2298 XtPointer n UNUSED,
2299 XEvent *event,
2300 Boolean *cont UNUSED)
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002301{
2302 Atom actual_type;
2303 int format;
2304 unsigned long nitems, bytes_after;
2305 unsigned char *prop=NULL;
2306 XPropertyEvent *xproperty=&event->xproperty;
2307
2308 /* Must be a property notify, state can't be Delete (True), has to be
2309 * one of the supported selection types. */
2310 if (event->type != PropertyNotify || xproperty->state
2311 || (xproperty->atom != clip_star.sel_atom
2312 && xproperty->atom != clip_plus.sel_atom))
2313 return;
2314
2315 if (XGetWindowProperty(xproperty->display, xproperty->window,
2316 xproperty->atom, 0, 0, False, timestamp_atom, &actual_type, &format,
2317 &nitems, &bytes_after, &prop))
2318 return;
2319
2320 if (prop)
2321 XFree(prop);
2322
2323 /* Make sure the property type is "TIMESTAMP" and it's 32 bits. */
2324 if (actual_type != timestamp_atom || format != 32)
2325 return;
2326
2327 /* Get the selection, using the event timestamp. */
Bram Moolenaar62b42182010-09-21 22:09:37 +02002328 if (XtOwnSelection(w, xproperty->atom, xproperty->time,
2329 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002330 clip_x11_notify_cb) == OK)
Bram Moolenaar62b42182010-09-21 22:09:37 +02002331 {
2332 /* Set the "owned" flag now, there may have been a call to
2333 * lose_ownership_cb in between. */
2334 if (xproperty->atom == clip_plus.sel_atom)
2335 clip_plus.owned = TRUE;
2336 else
2337 clip_star.owned = TRUE;
2338 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002339}
2340
2341 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002342x11_setup_selection(Widget w)
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002343{
2344 XtAddEventHandler(w, PropertyChangeMask, False,
2345 /*(XtEventHandler)*/clip_x11_timestamp_cb, (XtPointer)NULL);
2346}
2347
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002349clip_x11_request_selection_cb(
2350 Widget w UNUSED,
2351 XtPointer success,
2352 Atom *sel_atom,
2353 Atom *type,
2354 XtPointer value,
2355 long_u *length,
2356 int *format)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357{
Bram Moolenaard44347f2011-06-19 01:14:29 +02002358 int motion_type = MAUTO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 long_u len;
2360 char_u *p;
2361 char **text_list = NULL;
2362 VimClipboard *cbd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364
2365 if (*sel_atom == clip_plus.sel_atom)
2366 cbd = &clip_plus;
2367 else
2368 cbd = &clip_star;
2369
2370 if (value == NULL || *length == 0)
2371 {
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002372 clip_free_selection(cbd); /* nothing received, clear register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 *(int *)success = FALSE;
2374 return;
2375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 p = (char_u *)value;
2377 len = *length;
2378 if (*type == vim_atom)
2379 {
2380 motion_type = *p++;
2381 len--;
2382 }
2383
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 else if (*type == vimenc_atom)
2385 {
2386 char_u *enc;
2387 vimconv_T conv;
2388 int convlen;
2389
2390 motion_type = *p++;
2391 --len;
2392
2393 enc = p;
2394 p += STRLEN(p) + 1;
2395 len -= p - enc;
2396
2397 /* If the encoding of the text is different from 'encoding', attempt
2398 * converting it. */
2399 conv.vc_type = CONV_NONE;
2400 convert_setup(&conv, enc, p_enc);
2401 if (conv.vc_type != CONV_NONE)
2402 {
2403 convlen = len; /* Need to use an int here. */
2404 tmpbuf = string_convert(&conv, p, &convlen);
2405 len = convlen;
2406 if (tmpbuf != NULL)
2407 p = tmpbuf;
2408 convert_setup(&conv, NULL, NULL);
2409 }
2410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002412 else if (*type == compound_text_atom
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002413 || *type == utf8_atom
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002414 || (enc_dbcs != 0 && *type == text_atom))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 {
2416 XTextProperty text_prop;
2417 int n_text = 0;
2418 int status;
2419
2420 text_prop.value = (unsigned char *)value;
2421 text_prop.encoding = *type;
2422 text_prop.format = *format;
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002423 text_prop.nitems = len;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002424#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaare2e663f2013-03-07 18:02:30 +01002425 if (*type == utf8_atom)
2426 status = Xutf8TextPropertyToTextList(X_DISPLAY, &text_prop,
2427 &text_list, &n_text);
2428 else
2429#endif
2430 status = XmbTextPropertyToTextList(X_DISPLAY, &text_prop,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 &text_list, &n_text);
2432 if (status != Success || n_text < 1)
2433 {
2434 *(int *)success = FALSE;
2435 return;
2436 }
2437 p = (char_u *)text_list[0];
2438 len = STRLEN(p);
2439 }
2440 clip_yank_selection(motion_type, p, (long)len, cbd);
2441
2442 if (text_list != NULL)
2443 XFreeStringList(text_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 XtFree((char *)value);
2446 *(int *)success = TRUE;
2447}
2448
2449 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002450clip_x11_request_selection(
2451 Widget myShell,
2452 Display *dpy,
2453 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454{
2455 XEvent event;
2456 Atom type;
2457 static int success;
2458 int i;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002459 time_t start_time;
2460 int timed_out = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002462 for (i = 0; i < 6; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 {
2464 switch (i)
2465 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 case 0: type = vimenc_atom; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 case 1: type = vim_atom; break;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002468 case 2: type = utf8_atom; break;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002469 case 3: type = compound_text_atom; break;
2470 case 4: type = text_atom; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 default: type = XA_STRING;
2472 }
Bram Moolenaar81851112013-04-12 12:27:30 +02002473 if (type == utf8_atom
2474# if defined(X_HAVE_UTF8_STRING)
2475 && !enc_utf8
2476# endif
2477 )
2478 /* Only request utf-8 when 'encoding' is utf8 and
2479 * Xutf8TextPropertyToTextList is available. */
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002480 continue;
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002481 success = MAYBE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 XtGetSelectionValue(myShell, cbd->sel_atom, type,
2483 clip_x11_request_selection_cb, (XtPointer)&success, CurrentTime);
2484
2485 /* Make sure the request for the selection goes out before waiting for
2486 * a response. */
2487 XFlush(dpy);
2488
2489 /*
2490 * Wait for result of selection request, otherwise if we type more
2491 * characters, then they will appear before the one that requested the
2492 * paste! Don't worry, we will catch up with any other events later.
2493 */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002494 start_time = time(NULL);
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002495 while (success == MAYBE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002497 if (XCheckTypedEvent(dpy, PropertyNotify, &event)
2498 || XCheckTypedEvent(dpy, SelectionNotify, &event)
2499 || XCheckTypedEvent(dpy, SelectionRequest, &event))
Bram Moolenaar89417b92008-09-07 19:48:53 +00002500 {
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002501 /* This is where clip_x11_request_selection_cb() should be
2502 * called. It may actually happen a bit later, so we loop
2503 * until "success" changes.
2504 * We may get a SelectionRequest here and if we don't handle
2505 * it we hang. KDE klipper does this, for example.
2506 * We need to handle a PropertyNotify for large selections. */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002507 XtDispatchEvent(&event);
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002508 continue;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510
Bram Moolenaar89417b92008-09-07 19:48:53 +00002511 /* Time out after 2 to 3 seconds to avoid that we hang when the
2512 * other process doesn't respond. Note that the SelectionNotify
2513 * event may still come later when the selection owner comes back
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002514 * to life and the text gets inserted unexpectedly. Don't know
2515 * why that happens or how to avoid that :-(. */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002516 if (time(NULL) > start_time + 2)
2517 {
2518 timed_out = TRUE;
2519 break;
2520 }
2521
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 /* Do we need this? Probably not. */
2523 XSync(dpy, False);
2524
Bram Moolenaar89417b92008-09-07 19:48:53 +00002525 /* Wait for 1 msec to avoid that we eat up all CPU time. */
2526 ui_delay(1L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 }
2528
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002529 if (success == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 return;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002531
2532 /* don't do a retry with another type after timing out, otherwise we
2533 * hang for 15 seconds. */
2534 if (timed_out)
2535 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 }
2537
2538 /* Final fallback position - use the X CUT_BUFFER0 store */
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002539 yank_cut_buffer0(dpy, cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540}
2541
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 static Boolean
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002543clip_x11_convert_selection_cb(
2544 Widget w UNUSED,
2545 Atom *sel_atom,
2546 Atom *target,
2547 Atom *type,
2548 XtPointer *value,
2549 long_u *length,
2550 int *format)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551{
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002552 static char_u *save_result = NULL;
2553 static long_u save_length = 0;
2554 char_u *string;
2555 int motion_type;
2556 VimClipboard *cbd;
2557 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
2559 if (*sel_atom == clip_plus.sel_atom)
2560 cbd = &clip_plus;
2561 else
2562 cbd = &clip_star;
2563
2564 if (!cbd->owned)
2565 return False; /* Shouldn't ever happen */
2566
2567 /* requestor wants to know what target types we support */
2568 if (*target == targets_atom)
2569 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002570 static Atom array[7];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 *value = (XtPointer)array;
2573 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 array[i++] = targets_atom;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 array[i++] = vimenc_atom;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 array[i++] = vim_atom;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002577 if (enc_utf8)
2578 array[i++] = utf8_atom;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002579 array[i++] = XA_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 array[i++] = text_atom;
2581 array[i++] = compound_text_atom;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002582
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 *type = XA_ATOM;
2584 /* This used to be: *format = sizeof(Atom) * 8; but that caused
2585 * crashes on 64 bit machines. (Peter Derr) */
2586 *format = 32;
2587 *length = i;
2588 return True;
2589 }
2590
2591 if ( *target != XA_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 && *target != vimenc_atom
Bram Moolenaar980e58f2014-06-12 13:28:30 +02002593 && (*target != utf8_atom || !enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 && *target != vim_atom
2595 && *target != text_atom
2596 && *target != compound_text_atom)
2597 return False;
2598
2599 clip_get_selection(cbd);
2600 motion_type = clip_convert_selection(&string, length, cbd);
2601 if (motion_type < 0)
2602 return False;
2603
2604 /* For our own format, the first byte contains the motion type */
2605 if (*target == vim_atom)
2606 (*length)++;
2607
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 /* Our own format with encoding: motion 'encoding' NUL text */
2609 if (*target == vimenc_atom)
2610 *length += STRLEN(p_enc) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002612 if (save_length < *length || save_length / 2 >= *length)
2613 *value = XtRealloc((char *)save_result, (Cardinal)*length + 1);
2614 else
2615 *value = save_result;
2616 if (*value == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 {
2618 vim_free(string);
2619 return False;
2620 }
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002621 save_result = (char_u *)*value;
2622 save_length = *length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002624 if (*target == XA_STRING || (*target == utf8_atom && enc_utf8))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002626 mch_memmove(save_result, string, (size_t)(*length));
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002627 *type = *target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 }
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002629 else if (*target == compound_text_atom || *target == text_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 {
2631 XTextProperty text_prop;
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002632 char *string_nt = (char *)save_result;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002633 int conv_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634
2635 /* create NUL terminated string which XmbTextListToTextProperty wants */
2636 mch_memmove(string_nt, string, (size_t)*length);
2637 string_nt[*length] = NUL;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002638 conv_result = XmbTextListToTextProperty(X_DISPLAY, (char **)&string_nt,
2639 1, XCompoundTextStyle, &text_prop);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002640 if (conv_result != Success)
2641 {
2642 vim_free(string);
2643 return False;
2644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 *value = (XtPointer)(text_prop.value); /* from plain text */
2646 *length = text_prop.nitems;
2647 *type = compound_text_atom;
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002648 XtFree((char *)save_result);
2649 save_result = (char_u *)*value;
2650 save_length = *length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 else if (*target == vimenc_atom)
2653 {
2654 int l = STRLEN(p_enc);
2655
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002656 save_result[0] = motion_type;
2657 STRCPY(save_result + 1, p_enc);
2658 mch_memmove(save_result + l + 2, string, (size_t)(*length - l - 2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 *type = vimenc_atom;
2660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 else
2662 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002663 save_result[0] = motion_type;
2664 mch_memmove(save_result + 1, string, (size_t)(*length - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 *type = vim_atom;
2666 }
2667 *format = 8; /* 8 bits per char */
2668 vim_free(string);
2669 return True;
2670}
2671
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002673clip_x11_lose_ownership_cb(Widget w UNUSED, Atom *sel_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674{
2675 if (*sel_atom == clip_plus.sel_atom)
2676 clip_lose_selection(&clip_plus);
2677 else
2678 clip_lose_selection(&clip_star);
2679}
2680
2681 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002682clip_x11_lose_selection(Widget myShell, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683{
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002684 XtDisownSelection(myShell, cbd->sel_atom,
2685 XtLastTimestampProcessed(XtDisplay(myShell)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686}
2687
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002688 static void
2689clip_x11_notify_cb(Widget w UNUSED, Atom *sel_atom UNUSED, Atom *target UNUSED)
2690{
2691 /* To prevent automatically freeing the selection value. */
2692}
2693
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002695clip_x11_own_selection(Widget myShell, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696{
Bram Moolenaar62b42182010-09-21 22:09:37 +02002697 /* When using the GUI we have proper timestamps, use the one of the last
2698 * event. When in the console we don't get events (the terminal gets
2699 * them), Get the time by a zero-length append, clip_x11_timestamp_cb will
2700 * be called with the current timestamp. */
2701#ifdef FEAT_GUI
2702 if (gui.in_use)
2703 {
2704 if (XtOwnSelection(myShell, cbd->sel_atom,
2705 XtLastTimestampProcessed(XtDisplay(myShell)),
2706 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002707 clip_x11_notify_cb) == False)
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +01002708 return FAIL;
Bram Moolenaar62b42182010-09-21 22:09:37 +02002709 }
2710 else
2711#endif
2712 {
2713 if (!XChangeProperty(XtDisplay(myShell), XtWindow(myShell),
2714 cbd->sel_atom, timestamp_atom, 32, PropModeAppend, NULL, 0))
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +01002715 return FAIL;
Bram Moolenaar62b42182010-09-21 22:09:37 +02002716 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002717 /* Flush is required in a terminal as nothing else is doing it. */
2718 XFlush(XtDisplay(myShell));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 return OK;
2720}
2721
2722/*
2723 * Send the current selection to the clipboard. Do nothing for X because we
2724 * will fill in the selection only when requested by another app.
2725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002727clip_x11_set_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728{
2729}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002730
Bram Moolenaar113e1072019-01-20 15:30:40 +01002731#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD) && defined(USE_SYSTEM)) \
2732 || defined(PROTO)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002733 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002734clip_x11_owner_exists(VimClipboard *cbd)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002735{
2736 return XGetSelectionOwner(X_DISPLAY, cbd->sel_atom) != None;
2737}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738#endif
Bram Moolenaar113e1072019-01-20 15:30:40 +01002739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002741#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) \
2742 || defined(FEAT_GUI_GTK) || defined(PROTO)
2743/*
2744 * Get the contents of the X CUT_BUFFER0 and put it in "cbd".
2745 */
2746 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002747yank_cut_buffer0(Display *dpy, VimClipboard *cbd)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002748{
2749 int nbytes = 0;
2750 char_u *buffer = (char_u *)XFetchBuffer(dpy, &nbytes, 0);
2751
2752 if (nbytes > 0)
2753 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002754 int done = FALSE;
2755
2756 /* CUT_BUFFER0 is supposed to be always latin1. Convert to 'enc' when
2757 * using a multi-byte encoding. Conversion between two 8-bit
2758 * character sets usually fails and the text might actually be in
2759 * 'enc' anyway. */
2760 if (has_mbyte)
2761 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002762 char_u *conv_buf;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002763 vimconv_T vc;
2764
2765 vc.vc_type = CONV_NONE;
2766 if (convert_setup(&vc, (char_u *)"latin1", p_enc) == OK)
2767 {
2768 conv_buf = string_convert(&vc, buffer, &nbytes);
2769 if (conv_buf != NULL)
2770 {
2771 clip_yank_selection(MCHAR, conv_buf, (long)nbytes, cbd);
2772 vim_free(conv_buf);
2773 done = TRUE;
2774 }
2775 convert_setup(&vc, NULL, NULL);
2776 }
2777 }
2778 if (!done) /* use the text without conversion */
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002779 clip_yank_selection(MCHAR, buffer, (long)nbytes, cbd);
2780 XFree((void *)buffer);
2781 if (p_verbose > 0)
2782 {
2783 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002784 verb_msg(_("Used CUT_BUFFER0 instead of empty selection"));
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002785 verbose_leave();
2786 }
2787 }
2788}
2789#endif
2790
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791#if defined(FEAT_MOUSE) || defined(PROTO)
2792
2793/*
2794 * Move the cursor to the specified row and column on the screen.
Bram Moolenaar49325942007-05-10 19:19:59 +00002795 * Change current window if necessary. Returns an integer with the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 * CURSOR_MOVED bit set if the cursor has moved or unset otherwise.
2797 *
2798 * The MOUSE_FOLD_CLOSE bit is set when clicked on the '-' in a fold column.
2799 * The MOUSE_FOLD_OPEN bit is set when clicked on the '+' in a fold column.
2800 *
2801 * If flags has MOUSE_FOCUS, then the current window will not be changed, and
2802 * if the mouse is outside the window then the text will scroll, or if the
2803 * mouse was previously on a status line, then the status line may be dragged.
2804 *
2805 * If flags has MOUSE_MAY_VIS, then VIsual mode will be started before the
2806 * cursor is moved unless the cursor was on a status line.
2807 * This function returns one of IN_UNKNOWN, IN_BUFFER, IN_STATUS_LINE or
2808 * IN_SEP_LINE depending on where the cursor was clicked.
2809 *
2810 * If flags has MOUSE_MAY_STOP_VIS, then Visual mode will be stopped, unless
2811 * the mouse is on the status line of the same window.
2812 *
2813 * If flags has MOUSE_DID_MOVE, nothing is done if the mouse didn't move since
2814 * the last call.
2815 *
2816 * If flags has MOUSE_SETPOS, nothing is done, only the current position is
2817 * remembered.
2818 */
2819 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002820jump_to_mouse(
2821 int flags,
2822 int *inclusive, /* used for inclusive operator, can be NULL */
2823 int which_button) /* MOUSE_LEFT, MOUSE_RIGHT, MOUSE_MIDDLE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824{
2825 static int on_status_line = 0; /* #lines below bottom of window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 static int on_sep_line = 0; /* on separator right of window */
Bram Moolenaareb163d72017-09-23 15:08:17 +02002827#ifdef FEAT_MENU
2828 static int in_winbar = FALSE;
2829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 static int prev_row = -1;
2831 static int prev_col = -1;
2832 static win_T *dragwin = NULL; /* window being dragged */
2833 static int did_drag = FALSE; /* drag was noticed */
2834
2835 win_T *wp, *old_curwin;
2836 pos_T old_cursor;
2837 int count;
2838 int first;
2839 int row = mouse_row;
2840 int col = mouse_col;
2841#ifdef FEAT_FOLDING
2842 int mouse_char;
2843#endif
2844
2845 mouse_past_bottom = FALSE;
2846 mouse_past_eol = FALSE;
2847
2848 if (flags & MOUSE_RELEASED)
2849 {
2850 /* On button release we may change window focus if positioned on a
2851 * status line and no dragging happened. */
2852 if (dragwin != NULL && !did_drag)
2853 flags &= ~(MOUSE_FOCUS | MOUSE_DID_MOVE);
2854 dragwin = NULL;
2855 did_drag = FALSE;
2856 }
2857
2858 if ((flags & MOUSE_DID_MOVE)
2859 && prev_row == mouse_row
2860 && prev_col == mouse_col)
2861 {
2862retnomove:
Bram Moolenaar49325942007-05-10 19:19:59 +00002863 /* before moving the cursor for a left click which is NOT in a status
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 * line, stop Visual mode */
2865 if (on_status_line)
2866 return IN_STATUS_LINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 if (on_sep_line)
2868 return IN_SEP_LINE;
Bram Moolenaard327b0c2017-11-12 16:56:12 +01002869#ifdef FEAT_MENU
2870 if (in_winbar)
2871 {
2872 /* A quick second click may arrive as a double-click, but we use it
2873 * as a second click in the WinBar. */
2874 if ((mod_mask & MOD_MASK_MULTI_CLICK) && !(flags & MOUSE_RELEASED))
2875 {
2876 wp = mouse_find_win(&row, &col);
2877 if (wp == NULL)
2878 return IN_UNKNOWN;
2879 winbar_click(wp, col);
2880 }
2881 return IN_OTHER_WIN | MOUSE_WINBAR;
2882 }
2883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 if (flags & MOUSE_MAY_STOP_VIS)
2885 {
2886 end_visual_mode();
2887 redraw_curbuf_later(INVERTED); /* delete the inversion */
2888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2890 /* Continue a modeless selection in another window. */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002891 if (cmdwin_type != 0 && row < curwin->w_winrow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 return IN_OTHER_WIN;
2893#endif
2894 return IN_BUFFER;
2895 }
2896
2897 prev_row = mouse_row;
2898 prev_col = mouse_col;
2899
2900 if (flags & MOUSE_SETPOS)
2901 goto retnomove; /* ugly goto... */
2902
2903#ifdef FEAT_FOLDING
2904 /* Remember the character under the mouse, it might be a '-' or '+' in the
2905 * fold column. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002906 if (row >= 0 && row < Rows && col >= 0 && col <= Columns
2907 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 mouse_char = ScreenLines[LineOffset[row] + col];
2909 else
2910 mouse_char = ' ';
2911#endif
2912
2913 old_curwin = curwin;
2914 old_cursor = curwin->w_cursor;
2915
2916 if (!(flags & MOUSE_FOCUS))
2917 {
2918 if (row < 0 || col < 0) /* check if it makes sense */
2919 return IN_UNKNOWN;
2920
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 /* find the window where the row is in */
2922 wp = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02002923 if (wp == NULL)
2924 return IN_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 dragwin = NULL;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002926
2927#ifdef FEAT_MENU
2928 if (row == -1)
2929 {
2930 /* A click in the window toolbar does not enter another window or
2931 * change Visual highlighting. */
2932 winbar_click(wp, col);
Bram Moolenaareb163d72017-09-23 15:08:17 +02002933 in_winbar = TRUE;
2934 return IN_OTHER_WIN | MOUSE_WINBAR;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002935 }
Bram Moolenaareb163d72017-09-23 15:08:17 +02002936 in_winbar = FALSE;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002937#endif
2938
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 /*
2940 * winpos and height may change in win_enter()!
2941 */
2942 if (row >= wp->w_height) /* In (or below) status line */
2943 {
2944 on_status_line = row - wp->w_height + 1;
2945 dragwin = wp;
2946 }
2947 else
2948 on_status_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 if (col >= wp->w_width) /* In separator line */
2950 {
2951 on_sep_line = col - wp->w_width + 1;
2952 dragwin = wp;
2953 }
2954 else
2955 on_sep_line = 0;
2956
2957 /* The rightmost character of the status line might be a vertical
2958 * separator character if there is no connecting window to the right. */
2959 if (on_status_line && on_sep_line)
2960 {
2961 if (stl_connected(wp))
2962 on_sep_line = 0;
2963 else
2964 on_status_line = 0;
2965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 /* Before jumping to another buffer, or moving the cursor for a left
2968 * click, stop Visual mode. */
2969 if (VIsual_active
2970 && (wp->w_buffer != curwin->w_buffer
Bram Moolenaar4033c552017-09-16 20:54:51 +02002971 || (!on_status_line && !on_sep_line
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002972#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002974# ifdef FEAT_RIGHTLEFT
Bram Moolenaar02631462017-09-22 15:20:32 +02002975 wp->w_p_rl ? col < wp->w_width - wp->w_p_fdc :
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976# endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002977 col >= wp->w_p_fdc
2978# ifdef FEAT_CMDWIN
2979 + (cmdwin_type == 0 && wp == curwin ? 0 : 1)
2980# endif
2981 )
2982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 && (flags & MOUSE_MAY_STOP_VIS))))
2984 {
2985 end_visual_mode();
2986 redraw_curbuf_later(INVERTED); /* delete the inversion */
2987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988#ifdef FEAT_CMDWIN
2989 if (cmdwin_type != 0 && wp != curwin)
2990 {
2991 /* A click outside the command-line window: Use modeless
Bram Moolenaarf679a432010-03-02 18:16:09 +01002992 * selection if possible. Allow dragging the status lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 on_sep_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994# ifdef FEAT_CLIPBOARD
2995 if (on_status_line)
2996 return IN_STATUS_LINE;
2997 return IN_OTHER_WIN;
2998# else
2999 row = 0;
3000 col += wp->w_wincol;
3001 wp = curwin;
3002# endif
3003 }
3004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 /* Only change window focus when not clicking on or dragging the
3006 * status line. Do change focus when releasing the mouse button
3007 * (MOUSE_FOCUS was set above if we dragged first). */
3008 if (dragwin == NULL || (flags & MOUSE_RELEASED))
3009 win_enter(wp, TRUE); /* can make wp invalid! */
Bram Moolenaarc48369c2018-03-11 19:30:45 +01003010
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 if (curwin != old_curwin)
Bram Moolenaarc48369c2018-03-11 19:30:45 +01003012 {
3013#ifdef CHECK_DOUBLE_CLICK
3014 /* set topline, to be able to check for double click ourselves */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 set_mouse_topline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016#endif
Bram Moolenaarc48369c2018-03-11 19:30:45 +01003017#ifdef FEAT_TERMINAL
3018 /* when entering a terminal window may change state */
3019 term_win_entered();
3020#endif
3021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 if (on_status_line) /* In (or below) status line */
3023 {
3024 /* Don't use start_arrow() if we're in the same window */
3025 if (curwin == old_curwin)
3026 return IN_STATUS_LINE;
3027 else
3028 return IN_STATUS_LINE | CURSOR_MOVED;
3029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 if (on_sep_line) /* In (or below) status line */
3031 {
3032 /* Don't use start_arrow() if we're in the same window */
3033 if (curwin == old_curwin)
3034 return IN_SEP_LINE;
3035 else
3036 return IN_SEP_LINE | CURSOR_MOVED;
3037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038
3039 curwin->w_cursor.lnum = curwin->w_topline;
3040#ifdef FEAT_GUI
3041 /* remember topline, needed for double click */
3042 gui_prev_topline = curwin->w_topline;
3043# ifdef FEAT_DIFF
3044 gui_prev_topfill = curwin->w_topfill;
3045# endif
3046#endif
3047 }
3048 else if (on_status_line && which_button == MOUSE_LEFT)
3049 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 if (dragwin != NULL)
3051 {
3052 /* Drag the status line */
3053 count = row - dragwin->w_winrow - dragwin->w_height + 1
3054 - on_status_line;
3055 win_drag_status_line(dragwin, count);
3056 did_drag |= count;
3057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 return IN_STATUS_LINE; /* Cursor didn't move */
3059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 else if (on_sep_line && which_button == MOUSE_LEFT)
3061 {
3062 if (dragwin != NULL)
3063 {
3064 /* Drag the separator column */
3065 count = col - dragwin->w_wincol - dragwin->w_width + 1
3066 - on_sep_line;
3067 win_drag_vsep_line(dragwin, count);
3068 did_drag |= count;
3069 }
3070 return IN_SEP_LINE; /* Cursor didn't move */
3071 }
Bram Moolenaareb163d72017-09-23 15:08:17 +02003072#ifdef FEAT_MENU
3073 else if (in_winbar)
3074 {
3075 /* After a click on the window toolbar don't start Visual mode. */
3076 return IN_OTHER_WIN | MOUSE_WINBAR;
3077 }
3078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 else /* keep_window_focus must be TRUE */
3080 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 /* before moving the cursor for a left click, stop Visual mode */
3082 if (flags & MOUSE_MAY_STOP_VIS)
3083 {
3084 end_visual_mode();
3085 redraw_curbuf_later(INVERTED); /* delete the inversion */
3086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087
3088#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
3089 /* Continue a modeless selection in another window. */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003090 if (cmdwin_type != 0 && row < curwin->w_winrow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 return IN_OTHER_WIN;
3092#endif
3093
3094 row -= W_WINROW(curwin);
Bram Moolenaar53f81742017-09-22 14:35:51 +02003095 col -= curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096
3097 /*
3098 * When clicking beyond the end of the window, scroll the screen.
3099 * Scroll by however many rows outside the window we are.
3100 */
3101 if (row < 0)
3102 {
3103 count = 0;
3104 for (first = TRUE; curwin->w_topline > 1; )
3105 {
3106#ifdef FEAT_DIFF
3107 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
3108 ++count;
3109 else
3110#endif
3111 count += plines(curwin->w_topline - 1);
3112 if (!first && count > -row)
3113 break;
3114 first = FALSE;
3115#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02003116 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117#endif
3118#ifdef FEAT_DIFF
3119 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
3120 ++curwin->w_topfill;
3121 else
3122#endif
3123 {
3124 --curwin->w_topline;
3125#ifdef FEAT_DIFF
3126 curwin->w_topfill = 0;
3127#endif
3128 }
3129 }
3130#ifdef FEAT_DIFF
3131 check_topfill(curwin, FALSE);
3132#endif
3133 curwin->w_valid &=
3134 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3135 redraw_later(VALID);
3136 row = 0;
3137 }
3138 else if (row >= curwin->w_height)
3139 {
3140 count = 0;
3141 for (first = TRUE; curwin->w_topline < curbuf->b_ml.ml_line_count; )
3142 {
3143#ifdef FEAT_DIFF
3144 if (curwin->w_topfill > 0)
3145 ++count;
3146 else
3147#endif
3148 count += plines(curwin->w_topline);
3149 if (!first && count > row - curwin->w_height + 1)
3150 break;
3151 first = FALSE;
3152#ifdef FEAT_FOLDING
3153 if (hasFolding(curwin->w_topline, NULL, &curwin->w_topline)
3154 && curwin->w_topline == curbuf->b_ml.ml_line_count)
3155 break;
3156#endif
3157#ifdef FEAT_DIFF
3158 if (curwin->w_topfill > 0)
3159 --curwin->w_topfill;
3160 else
3161#endif
3162 {
3163 ++curwin->w_topline;
3164#ifdef FEAT_DIFF
3165 curwin->w_topfill =
3166 diff_check_fill(curwin, curwin->w_topline);
3167#endif
3168 }
3169 }
3170#ifdef FEAT_DIFF
3171 check_topfill(curwin, FALSE);
3172#endif
3173 redraw_later(VALID);
3174 curwin->w_valid &=
3175 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3176 row = curwin->w_height - 1;
3177 }
3178 else if (row == 0)
3179 {
3180 /* When dragging the mouse, while the text has been scrolled up as
3181 * far as it goes, moving the mouse in the top line should scroll
3182 * the text down (done later when recomputing w_topline). */
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003183 if (mouse_dragging > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 && curwin->w_cursor.lnum
3185 == curwin->w_buffer->b_ml.ml_line_count
3186 && curwin->w_cursor.lnum == curwin->w_topline)
3187 curwin->w_valid &= ~(VALID_TOPLINE);
3188 }
3189 }
3190
3191#ifdef FEAT_FOLDING
3192 /* Check for position outside of the fold column. */
3193 if (
3194# ifdef FEAT_RIGHTLEFT
Bram Moolenaar02631462017-09-22 15:20:32 +02003195 curwin->w_p_rl ? col < curwin->w_width - curwin->w_p_fdc :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196# endif
3197 col >= curwin->w_p_fdc
3198# ifdef FEAT_CMDWIN
3199 + (cmdwin_type == 0 ? 0 : 1)
3200# endif
3201 )
3202 mouse_char = ' ';
3203#endif
3204
3205 /* compute the position in the buffer line from the posn on the screen */
3206 if (mouse_comp_pos(curwin, &row, &col, &curwin->w_cursor.lnum))
3207 mouse_past_bottom = TRUE;
3208
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 /* Start Visual mode before coladvance(), for when 'sel' != "old" */
3210 if ((flags & MOUSE_MAY_VIS) && !VIsual_active)
3211 {
3212 check_visual_highlight();
3213 VIsual = old_cursor;
3214 VIsual_active = TRUE;
3215 VIsual_reselect = TRUE;
3216 /* if 'selectmode' contains "mouse", start Select mode */
3217 may_start_select('o');
3218 setmouse();
Bram Moolenaar7df351e2006-01-23 22:30:28 +00003219 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 redraw_cmdline = TRUE; /* show visual mode later */
3221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222
3223 curwin->w_curswant = col;
3224 curwin->w_set_curswant = FALSE; /* May still have been TRUE */
3225 if (coladvance(col) == FAIL) /* Mouse click beyond end of line */
3226 {
3227 if (inclusive != NULL)
3228 *inclusive = TRUE;
3229 mouse_past_eol = TRUE;
3230 }
3231 else if (inclusive != NULL)
3232 *inclusive = FALSE;
3233
3234 count = IN_BUFFER;
3235 if (curwin != old_curwin || curwin->w_cursor.lnum != old_cursor.lnum
3236 || curwin->w_cursor.col != old_cursor.col)
3237 count |= CURSOR_MOVED; /* Cursor has moved */
3238
3239#ifdef FEAT_FOLDING
3240 if (mouse_char == '+')
3241 count |= MOUSE_FOLD_OPEN;
3242 else if (mouse_char != ' ')
3243 count |= MOUSE_FOLD_CLOSE;
3244#endif
3245
3246 return count;
3247}
3248
3249/*
3250 * Compute the position in the buffer line from the posn on the screen in
3251 * window "win".
3252 * Returns TRUE if the position is below the last line.
3253 */
3254 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003255mouse_comp_pos(
3256 win_T *win,
3257 int *rowp,
3258 int *colp,
3259 linenr_T *lnump)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260{
3261 int col = *colp;
3262 int row = *rowp;
3263 linenr_T lnum;
3264 int retval = FALSE;
3265 int off;
3266 int count;
3267
3268#ifdef FEAT_RIGHTLEFT
3269 if (win->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02003270 col = win->w_width - 1 - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271#endif
3272
3273 lnum = win->w_topline;
3274
3275 while (row > 0)
3276 {
3277#ifdef FEAT_DIFF
3278 /* Don't include filler lines in "count" */
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003279 if (win->w_p_diff
3280# ifdef FEAT_FOLDING
3281 && !hasFoldingWin(win, lnum, NULL, NULL, TRUE, NULL)
3282# endif
3283 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 {
3285 if (lnum == win->w_topline)
3286 row -= win->w_topfill;
3287 else
3288 row -= diff_check_fill(win, lnum);
3289 count = plines_win_nofill(win, lnum, TRUE);
3290 }
3291 else
3292#endif
3293 count = plines_win(win, lnum, TRUE);
3294 if (count > row)
3295 break; /* Position is in this buffer line. */
3296#ifdef FEAT_FOLDING
3297 (void)hasFoldingWin(win, lnum, NULL, &lnum, TRUE, NULL);
3298#endif
3299 if (lnum == win->w_buffer->b_ml.ml_line_count)
3300 {
3301 retval = TRUE;
3302 break; /* past end of file */
3303 }
3304 row -= count;
3305 ++lnum;
3306 }
3307
3308 if (!retval)
3309 {
3310 /* Compute the column without wrapping. */
3311 off = win_col_off(win) - win_col_off2(win);
3312 if (col < off)
3313 col = off;
Bram Moolenaar02631462017-09-22 15:20:32 +02003314 col += row * (win->w_width - off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 /* add skip column (for long wrapping line) */
3316 col += win->w_skipcol;
3317 }
3318
3319 if (!win->w_p_wrap)
3320 col += win->w_leftcol;
3321
3322 /* skip line number and fold column in front of the line */
3323 col -= win_col_off(win);
3324 if (col < 0)
3325 {
3326#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003327 netbeans_gutter_click(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328#endif
3329 col = 0;
3330 }
3331
3332 *colp = col;
3333 *rowp = row;
3334 *lnump = lnum;
3335 return retval;
3336}
3337
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338/*
3339 * Find the window at screen position "*rowp" and "*colp". The positions are
3340 * updated to become relative to the top-left of the window.
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003341 * Returns NULL when something is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 win_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003344mouse_find_win(int *rowp, int *colp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345{
3346 frame_T *fp;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003347 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348
3349 fp = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003350 *rowp -= firstwin->w_winrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 for (;;)
3352 {
3353 if (fp->fr_layout == FR_LEAF)
3354 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 if (fp->fr_layout == FR_ROW)
3356 {
3357 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
3358 {
3359 if (*colp < fp->fr_width)
3360 break;
3361 *colp -= fp->fr_width;
3362 }
3363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 else /* fr_layout == FR_COL */
3365 {
3366 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
3367 {
3368 if (*rowp < fp->fr_height)
3369 break;
3370 *rowp -= fp->fr_height;
3371 }
3372 }
3373 }
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003374 /* When using a timer that closes a window the window might not actually
3375 * exist. */
3376 FOR_ALL_WINDOWS(wp)
3377 if (wp == fp->fr_win)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003378 {
3379#ifdef FEAT_MENU
3380 *rowp -= wp->w_winbar_height;
3381#endif
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003382 return wp;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003383 }
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003384 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386
Bram Moolenaar860cae12010-06-05 23:22:07 +02003387#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar658a1542018-03-03 19:29:43 +01003389 || defined(FEAT_GUI_PHOTON) || defined(FEAT_TERM_POPUP_MENU) \
3390 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391/*
3392 * Translate window coordinates to buffer position without any side effects
3393 */
3394 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003395get_fpos_of_mouse(pos_T *mpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396{
3397 win_T *wp;
3398 int row = mouse_row;
3399 int col = mouse_col;
3400
3401 if (row < 0 || col < 0) /* check if it makes sense */
3402 return IN_UNKNOWN;
3403
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 /* find the window where the row is in */
3405 wp = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003406 if (wp == NULL)
3407 return IN_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 /*
3409 * winpos and height may change in win_enter()!
3410 */
3411 if (row >= wp->w_height) /* In (or below) status line */
3412 return IN_STATUS_LINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 if (col >= wp->w_width) /* In vertical separator line */
3414 return IN_SEP_LINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415
3416 if (wp != curwin)
3417 return IN_UNKNOWN;
3418
3419 /* compute the position in the buffer line from the posn on the screen */
3420 if (mouse_comp_pos(curwin, &row, &col, &mpos->lnum))
3421 return IN_STATUS_LINE; /* past bottom */
3422
3423 mpos->col = vcol2col(wp, mpos->lnum, col);
3424
3425 if (mpos->col > 0)
3426 --mpos->col;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003427 mpos->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 return IN_BUFFER;
3429}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003432#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MAC) \
3433 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar3767b612018-03-03 19:51:58 +01003434 || defined(FEAT_GUI_PHOTON) || defined(FEAT_BEVAL) \
3435 || defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436/*
3437 * Convert a virtual (screen) column to a character column.
3438 * The first column is one.
3439 */
3440 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003441vcol2col(win_T *wp, linenr_T lnum, int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442{
3443 /* try to advance to the specified column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 int count = 0;
3445 char_u *ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003446 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447
Bram Moolenaar597a4222014-06-25 14:39:50 +02003448 line = ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaarb6101cf2012-10-21 00:58:39 +02003449 while (count < vcol && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003451 count += win_lbr_chartabsize(wp, line, ptr, count, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003452 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02003454 return (int)(ptr - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455}
3456#endif
3457
3458#endif /* FEAT_MOUSE */
3459
Bram Moolenaar4f974752019-02-17 17:44:42 +01003460#if defined(FEAT_GUI) || defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461/*
3462 * Called when focus changed. Used for the GUI or for systems where this can
3463 * be done in the console (Win32).
3464 */
3465 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003466ui_focus_change(
3467 int in_focus) /* TRUE if focus gained. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468{
3469 static time_t last_time = (time_t)0;
3470 int need_redraw = FALSE;
3471
3472 /* When activated: Check if any file was modified outside of Vim.
3473 * Only do this when not done within the last two seconds (could get
3474 * several events in a row). */
3475 if (in_focus && last_time + 2 < time(NULL))
3476 {
3477 need_redraw = check_timestamps(
3478# ifdef FEAT_GUI
3479 gui.in_use
3480# else
3481 FALSE
3482# endif
3483 );
3484 last_time = time(NULL);
3485 }
3486
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 /*
3488 * Fire the focus gained/lost autocommand.
3489 */
3490 need_redraw |= apply_autocmds(in_focus ? EVENT_FOCUSGAINED
3491 : EVENT_FOCUSLOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
3493 if (need_redraw)
3494 {
3495 /* Something was executed, make sure the cursor is put back where it
3496 * belongs. */
3497 need_wait_return = FALSE;
3498
3499 if (State & CMDLINE)
3500 redrawcmdline();
3501 else if (State == HITRETURN || State == SETWSIZE || State == ASKMORE
3502 || State == EXTERNCMD || State == CONFIRM || exmode_active)
3503 repeat_message();
3504 else if ((State & NORMAL) || (State & INSERT))
3505 {
3506 if (must_redraw != 0)
3507 update_screen(0);
3508 setcursor();
3509 }
3510 cursor_on(); /* redrawing may have switched it off */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003511 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512# ifdef FEAT_GUI
3513 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 gui_update_scrollbars(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515# endif
3516 }
3517#ifdef FEAT_TITLE
3518 /* File may have been changed from 'readonly' to 'noreadonly' */
3519 if (need_maketitle)
3520 maketitle();
3521#endif
3522}
3523#endif
3524
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003525#if defined(HAVE_INPUT_METHOD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526/*
3527 * Save current Input Method status to specified place.
3528 */
3529 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003530im_save_status(long *psave)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531{
3532 /* Don't save when 'imdisable' is set or "xic" is NULL, IM is always
3533 * disabled then (but might start later).
3534 * Also don't save when inside a mapping, vgetc_im_active has not been set
3535 * then.
3536 * And don't save when the keys were stuffed (e.g., for a "." command).
3537 * And don't save when the GUI is running but our window doesn't have
3538 * input focus (e.g., when a find dialog is open). */
3539 if (!p_imdisable && KeyTyped && !KeyStuffed
3540# ifdef FEAT_XIM
3541 && xic != NULL
3542# endif
3543# ifdef FEAT_GUI
3544 && (!gui.in_use || gui.in_focus)
3545# endif
3546 )
3547 {
3548 /* Do save when IM is on, or IM is off and saved status is on. */
3549 if (vgetc_im_active)
3550 *psave = B_IMODE_IM;
3551 else if (*psave == B_IMODE_IM)
3552 *psave = B_IMODE_NONE;
3553 }
3554}
3555#endif