blob: 44328521f5b52d9f30cd463ad90e46a7f6cae238 [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) \
631 && (defined(FEAT_GUI) \
632 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)))) \
633 || defined(PROTO)
634/*
635 * Get the window position in pixels, if possible.
636 * Return FAIL when not possible.
637 */
638 int
639ui_get_winpos(int *x, int *y, varnumber_T timeout)
640{
641# ifdef FEAT_GUI
642 if (gui.in_use)
643 return gui_mch_get_winpos(x, y);
644# endif
645# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
646 return term_get_winpos(x, y, timeout);
647# endif
648}
649#endif
650
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100652ui_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200654 ui_breakcheck_force(FALSE);
655}
656
657/*
658 * When "force" is true also check when the terminal is not in raw mode.
659 * This is useful to read input on channels.
660 */
661 void
662ui_breakcheck_force(int force)
663{
Bram Moolenaar48d23bb2018-11-20 02:42:43 +0100664 static int recursive = FALSE;
665 int save_updating_screen = updating_screen;
Bram Moolenaare3caa112017-01-31 22:07:42 +0100666
Bram Moolenaar48d23bb2018-11-20 02:42:43 +0100667 // We could be called recursively if stderr is redirected, calling
668 // fill_input_buf() calls settmode() when stdin isn't a tty. settmode()
669 // calls vgetorpeek() which calls ui_breakcheck() again.
670 if (recursive)
671 return;
672 recursive = TRUE;
673
674 // We do not want gui_resize_shell() to redraw the screen here.
Bram Moolenaare3caa112017-01-31 22:07:42 +0100675 ++updating_screen;
676
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677#ifdef FEAT_GUI
678 if (gui.in_use)
679 gui_mch_update();
680 else
681#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200682 mch_breakcheck(force);
Bram Moolenaare3caa112017-01-31 22:07:42 +0100683
Bram Moolenaar42335f52018-09-13 15:33:43 +0200684 if (save_updating_screen)
685 updating_screen = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200686 else
687 reset_updating_screen(FALSE);
Bram Moolenaar48d23bb2018-11-20 02:42:43 +0100688
689 recursive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690}
691
692/*****************************************************************************
693 * Functions for copying and pasting text between applications.
694 * This is always included in a GUI version, but may also be included when the
695 * clipboard and mouse is available to a terminal version such as xterm.
696 * Note: there are some more functions in ops.c that handle selection stuff.
697 *
698 * Also note that the majority of functions here deal with the X 'primary'
699 * (visible - for Visual mode use) selection, and only that. There are no
700 * versions of these for the 'clipboard' selection, as Visual mode has no use
701 * for them.
702 */
703
704#if defined(FEAT_CLIPBOARD) || defined(PROTO)
705
706/*
707 * Selection stuff using Visual mode, for cutting and pasting text to other
708 * windows.
709 */
710
711/*
712 * Call this to initialise the clipboard. Pass it FALSE if the clipboard code
713 * is included, but the clipboard can not be used, or TRUE if the clipboard can
714 * be used. Eg unix may call this with FALSE, then call it again with TRUE if
715 * the GUI starts.
716 */
717 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100718clip_init(int can_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719{
720 VimClipboard *cb;
721
722 cb = &clip_star;
723 for (;;)
724 {
725 cb->available = can_use;
726 cb->owned = FALSE;
727 cb->start.lnum = 0;
728 cb->start.col = 0;
729 cb->end.lnum = 0;
730 cb->end.col = 0;
731 cb->state = SELECT_CLEARED;
732
733 if (cb == &clip_plus)
734 break;
735 cb = &clip_plus;
736 }
737}
738
739/*
740 * Check whether the VIsual area has changed, and if so try to become the owner
741 * of the selection, and free any old converted selection we may still have
742 * lying around. If the VIsual mode has ended, make a copy of what was
743 * selected so we can still give it to others. Will probably have to make sure
744 * this is called whenever VIsual mode is ended.
745 */
746 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100747clip_update_selection(VimClipboard *clip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748{
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200749 pos_T start, end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750
751 /* If visual mode is only due to a redo command ("."), then ignore it */
752 if (!redo_VIsual_busy && VIsual_active && (State & NORMAL))
753 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100754 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 {
756 start = VIsual;
757 end = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000759 end.col += (*mb_ptr2len)(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 }
761 else
762 {
763 start = curwin->w_cursor;
764 end = VIsual;
765 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100766 if (!EQUAL_POS(clip->start, start)
767 || !EQUAL_POS(clip->end, end)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200768 || clip->vmode != VIsual_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200770 clip_clear_selection(clip);
771 clip->start = start;
772 clip->end = end;
773 clip->vmode = VIsual_mode;
774 clip_free_selection(clip);
775 clip_own_selection(clip);
776 clip_gen_set_selection(clip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 }
778 }
779}
780
781 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100782clip_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783{
784 /*
785 * Also want to check somehow that we are reading from the keyboard rather
786 * than a mapping etc.
787 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788#ifdef FEAT_X11
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200789 /* Always own the selection, we might have lost it without being
Bram Moolenaar62b42182010-09-21 22:09:37 +0200790 * notified, e.g. during a ":sh" command. */
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200791 if (cbd->available)
792 {
793 int was_owned = cbd->owned;
794
795 cbd->owned = (clip_gen_own_selection(cbd) == OK);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200796 if (!was_owned && (cbd == &clip_star || cbd == &clip_plus))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 {
Bram Moolenaarebefac62005-12-28 22:39:57 +0000798 /* May have to show a different kind of highlighting for the
799 * selected area. There is no specific redraw command for this,
800 * just redraw all windows on the current buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 if (cbd->owned
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000802 && (get_real_state() == VISUAL
803 || get_real_state() == SELECTMODE)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200804 && (cbd == &clip_star ? clip_isautosel_star()
805 : clip_isautosel_plus())
Bram Moolenaar8820b482017-03-16 17:23:31 +0100806 && HL_ATTR(HLF_V) != HL_ATTR(HLF_VNC))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 redraw_curbuf_later(INVERTED_ALL);
808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200810#else
Bram Moolenaarb6101cf2012-10-21 00:58:39 +0200811 /* Only own the clipboard when we didn't own it yet. */
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200812 if (!cbd->owned && cbd->available)
813 cbd->owned = (clip_gen_own_selection(cbd) == OK);
814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815}
816
817 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100818clip_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819{
820#ifdef FEAT_X11
821 int was_owned = cbd->owned;
822#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200823 int visual_selection = FALSE;
824
825 if (cbd == &clip_star || cbd == &clip_plus)
826 visual_selection = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827
828 clip_free_selection(cbd);
829 cbd->owned = FALSE;
830 if (visual_selection)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200831 clip_clear_selection(cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 clip_gen_lose_selection(cbd);
833#ifdef FEAT_X11
834 if (visual_selection)
835 {
836 /* May have to show a different kind of highlighting for the selected
837 * area. There is no specific redraw command for this, just redraw all
838 * windows on the current buffer. */
839 if (was_owned
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000840 && (get_real_state() == VISUAL
841 || get_real_state() == SELECTMODE)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200842 && (cbd == &clip_star ?
843 clip_isautosel_star() : clip_isautosel_plus())
Bram Moolenaar8820b482017-03-16 17:23:31 +0100844 && HL_ATTR(HLF_V) != HL_ATTR(HLF_VNC))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 {
846 update_curbuf(INVERTED_ALL);
847 setcursor();
848 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +0100849 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
851 }
852#endif
853}
854
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200855 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100856clip_copy_selection(VimClipboard *clip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857{
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200858 if (VIsual_active && (State & NORMAL) && clip->available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200860 clip_update_selection(clip);
861 clip_free_selection(clip);
862 clip_own_selection(clip);
863 if (clip->owned)
864 clip_get_selection(clip);
865 clip_gen_set_selection(clip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 }
867}
868
869/*
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200870 * Save and restore clip_unnamed before doing possibly many changes. This
871 * prevents accessing the clipboard very often which might slow down Vim
872 * considerably.
873 */
Bram Moolenaar73a156b2015-01-27 21:39:05 +0100874static int global_change_count = 0; /* if set, inside a start_global_changes */
Bram Moolenaar3fcfa352017-03-29 19:20:41 +0200875static int clipboard_needs_update = FALSE; /* clipboard needs to be updated */
876static int clip_did_set_selection = TRUE;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200877
878/*
879 * Save clip_unnamed and reset it.
880 */
881 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100882start_global_changes(void)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200883{
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100884 if (++global_change_count > 1)
885 return;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200886 clip_unnamed_saved = clip_unnamed;
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100887 clipboard_needs_update = FALSE;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200888
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100889 if (clip_did_set_selection)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200890 {
891 clip_unnamed = FALSE;
892 clip_did_set_selection = FALSE;
893 }
894}
895
896/*
Bram Moolenaar3fcfa352017-03-29 19:20:41 +0200897 * Return TRUE if setting the clipboard was postponed, it already contains the
898 * right text.
899 */
900 int
901is_clipboard_needs_update()
902{
903 return clipboard_needs_update;
904}
905
906/*
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200907 * Restore clip_unnamed and set the selection when needed.
908 */
909 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100910end_global_changes(void)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200911{
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100912 if (--global_change_count > 0)
913 /* recursive */
914 return;
915 if (!clip_did_set_selection)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200916 {
917 clip_did_set_selection = TRUE;
918 clip_unnamed = clip_unnamed_saved;
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100919 clip_unnamed_saved = FALSE;
920 if (clipboard_needs_update)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200921 {
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100922 /* only store something in the clipboard,
923 * if we have yanked anything to it */
924 if (clip_unnamed & CLIP_UNNAMED)
925 {
926 clip_own_selection(&clip_star);
927 clip_gen_set_selection(&clip_star);
928 }
929 if (clip_unnamed & CLIP_UNNAMED_PLUS)
930 {
931 clip_own_selection(&clip_plus);
932 clip_gen_set_selection(&clip_plus);
933 }
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200934 }
935 }
Bram Moolenaar3fcfa352017-03-29 19:20:41 +0200936 clipboard_needs_update = FALSE;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200937}
938
939/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 * Called when Visual mode is ended: update the selection.
941 */
942 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100943clip_auto_select(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944{
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200945 if (clip_isautosel_star())
946 clip_copy_selection(&clip_star);
947 if (clip_isautosel_plus())
948 clip_copy_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949}
950
951/*
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200952 * Return TRUE if automatic selection of Visual area is desired for the *
953 * register.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 */
955 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100956clip_isautosel_star(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957{
958 return (
959#ifdef FEAT_GUI
960 gui.in_use ? (vim_strchr(p_go, GO_ASEL) != NULL) :
961#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200962 clip_autoselect_star);
963}
964
965/*
966 * Return TRUE if automatic selection of Visual area is desired for the +
967 * register.
968 */
969 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100970clip_isautosel_plus(void)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200971{
972 return (
973#ifdef FEAT_GUI
974 gui.in_use ? (vim_strchr(p_go, GO_ASELPLUS) != NULL) :
975#endif
976 clip_autoselect_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977}
978
979
980/*
981 * Stuff for general mouse selection, without using Visual mode.
982 */
983
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100984static void clip_invert_area(int, int, int, int, int how);
985static void clip_invert_rectangle(int row, int col, int height, int width, int invert);
986static void clip_get_word_boundaries(VimClipboard *, int, int);
987static int clip_get_line_end(int);
988static void clip_update_modeless_selection(VimClipboard *, int, int,
989 int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990
991/* flags for clip_invert_area() */
992#define CLIP_CLEAR 1
993#define CLIP_SET 2
994#define CLIP_TOGGLE 3
995
996/*
997 * Start, continue or end a modeless selection. Used when editing the
998 * command-line and in the cmdline window.
999 */
1000 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001001clip_modeless(int button, int is_click, int is_drag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002{
1003 int repeat;
1004
1005 repeat = ((clip_star.mode == SELECT_MODE_CHAR
1006 || clip_star.mode == SELECT_MODE_LINE)
1007 && (mod_mask & MOD_MASK_2CLICK))
1008 || (clip_star.mode == SELECT_MODE_WORD
1009 && (mod_mask & MOD_MASK_3CLICK));
1010 if (is_click && button == MOUSE_RIGHT)
1011 {
1012 /* Right mouse button: If there was no selection, start one.
1013 * Otherwise extend the existing selection. */
1014 if (clip_star.state == SELECT_CLEARED)
1015 clip_start_selection(mouse_col, mouse_row, FALSE);
1016 clip_process_selection(button, mouse_col, mouse_row, repeat);
1017 }
1018 else if (is_click)
1019 clip_start_selection(mouse_col, mouse_row, repeat);
1020 else if (is_drag)
1021 {
1022 /* Don't try extending a selection if there isn't one. Happens when
1023 * button-down is in the cmdline and them moving mouse upwards. */
1024 if (clip_star.state != SELECT_CLEARED)
1025 clip_process_selection(button, mouse_col, mouse_row, repeat);
1026 }
1027 else /* release */
1028 clip_process_selection(MOUSE_RELEASE, mouse_col, mouse_row, FALSE);
1029}
1030
1031/*
1032 * Compare two screen positions ala strcmp()
1033 */
1034 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001035clip_compare_pos(
1036 int row1,
1037 int col1,
1038 int row2,
1039 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040{
1041 if (row1 > row2) return(1);
1042 if (row1 < row2) return(-1);
1043 if (col1 > col2) return(1);
1044 if (col1 < col2) return(-1);
Bram Moolenaar9e341102016-02-23 23:04:36 +01001045 return(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046}
1047
1048/*
1049 * Start the selection
1050 */
1051 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001052clip_start_selection(int col, int row, int repeated_click)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053{
1054 VimClipboard *cb = &clip_star;
1055
1056 if (cb->state == SELECT_DONE)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001057 clip_clear_selection(cb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058
1059 row = check_row(row);
1060 col = check_col(col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 col = mb_fix_col(col, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062
1063 cb->start.lnum = row;
1064 cb->start.col = col;
1065 cb->end = cb->start;
1066 cb->origin_row = (short_u)cb->start.lnum;
1067 cb->state = SELECT_IN_PROGRESS;
1068
1069 if (repeated_click)
1070 {
1071 if (++cb->mode > SELECT_MODE_LINE)
1072 cb->mode = SELECT_MODE_CHAR;
1073 }
1074 else
1075 cb->mode = SELECT_MODE_CHAR;
1076
1077#ifdef FEAT_GUI
1078 /* clear the cursor until the selection is made */
1079 if (gui.in_use)
1080 gui_undraw_cursor();
1081#endif
1082
1083 switch (cb->mode)
1084 {
1085 case SELECT_MODE_CHAR:
1086 cb->origin_start_col = cb->start.col;
1087 cb->word_end_col = clip_get_line_end((int)cb->start.lnum);
1088 break;
1089
1090 case SELECT_MODE_WORD:
1091 clip_get_word_boundaries(cb, (int)cb->start.lnum, cb->start.col);
1092 cb->origin_start_col = cb->word_start_col;
1093 cb->origin_end_col = cb->word_end_col;
1094
1095 clip_invert_area((int)cb->start.lnum, cb->word_start_col,
1096 (int)cb->end.lnum, cb->word_end_col, CLIP_SET);
1097 cb->start.col = cb->word_start_col;
1098 cb->end.col = cb->word_end_col;
1099 break;
1100
1101 case SELECT_MODE_LINE:
1102 clip_invert_area((int)cb->start.lnum, 0, (int)cb->start.lnum,
1103 (int)Columns, CLIP_SET);
1104 cb->start.col = 0;
1105 cb->end.col = Columns;
1106 break;
1107 }
1108
1109 cb->prev = cb->start;
1110
1111#ifdef DEBUG_SELECTION
1112 printf("Selection started at (%u,%u)\n", cb->start.lnum, cb->start.col);
1113#endif
1114}
1115
1116/*
1117 * Continue processing the selection
1118 */
1119 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001120clip_process_selection(
1121 int button,
1122 int col,
1123 int row,
1124 int_u repeated_click)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125{
1126 VimClipboard *cb = &clip_star;
1127 int diff;
1128 int slen = 1; /* cursor shape width */
1129
1130 if (button == MOUSE_RELEASE)
1131 {
1132 /* Check to make sure we have something selected */
1133 if (cb->start.lnum == cb->end.lnum && cb->start.col == cb->end.col)
1134 {
1135#ifdef FEAT_GUI
1136 if (gui.in_use)
1137 gui_update_cursor(FALSE, FALSE);
1138#endif
1139 cb->state = SELECT_CLEARED;
1140 return;
1141 }
1142
1143#ifdef DEBUG_SELECTION
1144 printf("Selection ended: (%u,%u) to (%u,%u)\n", cb->start.lnum,
1145 cb->start.col, cb->end.lnum, cb->end.col);
1146#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001147 if (clip_isautosel_star()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 || (
1149#ifdef FEAT_GUI
1150 gui.in_use ? (vim_strchr(p_go, GO_ASELML) != NULL) :
1151#endif
1152 clip_autoselectml))
1153 clip_copy_modeless_selection(FALSE);
1154#ifdef FEAT_GUI
1155 if (gui.in_use)
1156 gui_update_cursor(FALSE, FALSE);
1157#endif
1158
1159 cb->state = SELECT_DONE;
1160 return;
1161 }
1162
1163 row = check_row(row);
1164 col = check_col(col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 col = mb_fix_col(col, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166
1167 if (col == (int)cb->prev.col && row == cb->prev.lnum && !repeated_click)
1168 return;
1169
1170 /*
1171 * When extending the selection with the right mouse button, swap the
1172 * start and end if the position is before half the selection
1173 */
1174 if (cb->state == SELECT_DONE && button == MOUSE_RIGHT)
1175 {
1176 /*
1177 * If the click is before the start, or the click is inside the
1178 * selection and the start is the closest side, set the origin to the
1179 * end of the selection.
1180 */
1181 if (clip_compare_pos(row, col, (int)cb->start.lnum, cb->start.col) < 0
1182 || (clip_compare_pos(row, col,
1183 (int)cb->end.lnum, cb->end.col) < 0
1184 && (((cb->start.lnum == cb->end.lnum
1185 && cb->end.col - col > col - cb->start.col))
1186 || ((diff = (cb->end.lnum - row) -
1187 (row - cb->start.lnum)) > 0
1188 || (diff == 0 && col < (int)(cb->start.col +
1189 cb->end.col) / 2)))))
1190 {
1191 cb->origin_row = (short_u)cb->end.lnum;
1192 cb->origin_start_col = cb->end.col - 1;
1193 cb->origin_end_col = cb->end.col;
1194 }
1195 else
1196 {
1197 cb->origin_row = (short_u)cb->start.lnum;
1198 cb->origin_start_col = cb->start.col;
1199 cb->origin_end_col = cb->start.col;
1200 }
1201 if (cb->mode == SELECT_MODE_WORD && !repeated_click)
1202 cb->mode = SELECT_MODE_CHAR;
1203 }
1204
1205 /* set state, for when using the right mouse button */
1206 cb->state = SELECT_IN_PROGRESS;
1207
1208#ifdef DEBUG_SELECTION
1209 printf("Selection extending to (%d,%d)\n", row, col);
1210#endif
1211
1212 if (repeated_click && ++cb->mode > SELECT_MODE_LINE)
1213 cb->mode = SELECT_MODE_CHAR;
1214
1215 switch (cb->mode)
1216 {
1217 case SELECT_MODE_CHAR:
1218 /* If we're on a different line, find where the line ends */
1219 if (row != cb->prev.lnum)
1220 cb->word_end_col = clip_get_line_end(row);
1221
1222 /* See if we are before or after the origin of the selection */
1223 if (clip_compare_pos(row, col, cb->origin_row,
1224 cb->origin_start_col) >= 0)
1225 {
1226 if (col >= (int)cb->word_end_col)
1227 clip_update_modeless_selection(cb, cb->origin_row,
1228 cb->origin_start_col, row, (int)Columns);
1229 else
1230 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 if (has_mbyte && mb_lefthalve(row, col))
1232 slen = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 clip_update_modeless_selection(cb, cb->origin_row,
1234 cb->origin_start_col, row, col + slen);
1235 }
1236 }
1237 else
1238 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 if (has_mbyte
1240 && mb_lefthalve(cb->origin_row, cb->origin_start_col))
1241 slen = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 if (col >= (int)cb->word_end_col)
1243 clip_update_modeless_selection(cb, row, cb->word_end_col,
1244 cb->origin_row, cb->origin_start_col + slen);
1245 else
1246 clip_update_modeless_selection(cb, row, col,
1247 cb->origin_row, cb->origin_start_col + slen);
1248 }
1249 break;
1250
1251 case SELECT_MODE_WORD:
1252 /* If we are still within the same word, do nothing */
1253 if (row == cb->prev.lnum && col >= (int)cb->word_start_col
1254 && col < (int)cb->word_end_col && !repeated_click)
1255 return;
1256
1257 /* Get new word boundaries */
1258 clip_get_word_boundaries(cb, row, col);
1259
1260 /* Handle being after the origin point of selection */
1261 if (clip_compare_pos(row, col, cb->origin_row,
1262 cb->origin_start_col) >= 0)
1263 clip_update_modeless_selection(cb, cb->origin_row,
1264 cb->origin_start_col, row, cb->word_end_col);
1265 else
1266 clip_update_modeless_selection(cb, row, cb->word_start_col,
1267 cb->origin_row, cb->origin_end_col);
1268 break;
1269
1270 case SELECT_MODE_LINE:
1271 if (row == cb->prev.lnum && !repeated_click)
1272 return;
1273
1274 if (clip_compare_pos(row, col, cb->origin_row,
1275 cb->origin_start_col) >= 0)
1276 clip_update_modeless_selection(cb, cb->origin_row, 0, row,
1277 (int)Columns);
1278 else
1279 clip_update_modeless_selection(cb, row, 0, cb->origin_row,
1280 (int)Columns);
1281 break;
1282 }
1283
1284 cb->prev.lnum = row;
1285 cb->prev.col = col;
1286
1287#ifdef DEBUG_SELECTION
1288 printf("Selection is: (%u,%u) to (%u,%u)\n", cb->start.lnum,
1289 cb->start.col, cb->end.lnum, cb->end.col);
1290#endif
1291}
1292
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293# if defined(FEAT_GUI) || defined(PROTO)
1294/*
1295 * Redraw part of the selection if character at "row,col" is inside of it.
1296 * Only used for the GUI.
1297 */
1298 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001299clip_may_redraw_selection(int row, int col, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300{
1301 int start = col;
1302 int end = col + len;
1303
1304 if (clip_star.state != SELECT_CLEARED
1305 && row >= clip_star.start.lnum
1306 && row <= clip_star.end.lnum)
1307 {
1308 if (row == clip_star.start.lnum && start < (int)clip_star.start.col)
1309 start = clip_star.start.col;
1310 if (row == clip_star.end.lnum && end > (int)clip_star.end.col)
1311 end = clip_star.end.col;
1312 if (end > start)
1313 clip_invert_area(row, start, row, end, 0);
1314 }
1315}
1316# endif
1317
1318/*
1319 * Called from outside to clear selected region from the display
1320 */
1321 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001322clip_clear_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001325 if (cbd->state == SELECT_CLEARED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 return;
1327
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001328 clip_invert_area((int)cbd->start.lnum, cbd->start.col, (int)cbd->end.lnum,
1329 cbd->end.col, CLIP_CLEAR);
1330 cbd->state = SELECT_CLEARED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331}
1332
1333/*
1334 * Clear the selection if any lines from "row1" to "row2" are inside of it.
1335 */
1336 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001337clip_may_clear_selection(int row1, int row2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338{
1339 if (clip_star.state == SELECT_DONE
1340 && row2 >= clip_star.start.lnum
1341 && row1 <= clip_star.end.lnum)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001342 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343}
1344
1345/*
1346 * Called before the screen is scrolled up or down. Adjusts the line numbers
1347 * of the selection. Call with big number when clearing the screen.
1348 */
1349 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001350clip_scroll_selection(
1351 int rows) /* negative for scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352{
1353 int lnum;
1354
1355 if (clip_star.state == SELECT_CLEARED)
1356 return;
1357
1358 lnum = clip_star.start.lnum - rows;
1359 if (lnum <= 0)
1360 clip_star.start.lnum = 0;
1361 else if (lnum >= screen_Rows) /* scrolled off of the screen */
1362 clip_star.state = SELECT_CLEARED;
1363 else
1364 clip_star.start.lnum = lnum;
1365
1366 lnum = clip_star.end.lnum - rows;
1367 if (lnum < 0) /* scrolled off of the screen */
1368 clip_star.state = SELECT_CLEARED;
1369 else if (lnum >= screen_Rows)
1370 clip_star.end.lnum = screen_Rows - 1;
1371 else
1372 clip_star.end.lnum = lnum;
1373}
1374
1375/*
1376 * Invert a region of the display between a starting and ending row and column
1377 * Values for "how":
1378 * CLIP_CLEAR: undo inversion
1379 * CLIP_SET: set inversion
1380 * CLIP_TOGGLE: set inversion if pos1 < pos2, undo inversion otherwise.
1381 * 0: invert (GUI only).
1382 */
1383 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001384clip_invert_area(
1385 int row1,
1386 int col1,
1387 int row2,
1388 int col2,
1389 int how)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390{
1391 int invert = FALSE;
1392
1393 if (how == CLIP_SET)
1394 invert = TRUE;
1395
1396 /* Swap the from and to positions so the from is always before */
1397 if (clip_compare_pos(row1, col1, row2, col2) > 0)
1398 {
1399 int tmp_row, tmp_col;
1400
1401 tmp_row = row1;
1402 tmp_col = col1;
1403 row1 = row2;
1404 col1 = col2;
1405 row2 = tmp_row;
1406 col2 = tmp_col;
1407 }
1408 else if (how == CLIP_TOGGLE)
1409 invert = TRUE;
1410
1411 /* If all on the same line, do it the easy way */
1412 if (row1 == row2)
1413 {
1414 clip_invert_rectangle(row1, col1, 1, col2 - col1, invert);
1415 }
1416 else
1417 {
1418 /* Handle a piece of the first line */
1419 if (col1 > 0)
1420 {
1421 clip_invert_rectangle(row1, col1, 1, (int)Columns - col1, invert);
1422 row1++;
1423 }
1424
1425 /* Handle a piece of the last line */
1426 if (col2 < Columns - 1)
1427 {
1428 clip_invert_rectangle(row2, 0, 1, col2, invert);
1429 row2--;
1430 }
1431
1432 /* Handle the rectangle thats left */
1433 if (row2 >= row1)
1434 clip_invert_rectangle(row1, 0, row2 - row1 + 1, (int)Columns,
1435 invert);
1436 }
1437}
1438
1439/*
1440 * Invert or un-invert a rectangle of the screen.
1441 * "invert" is true if the result is inverted.
1442 */
1443 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001444clip_invert_rectangle(
1445 int row,
1446 int col,
1447 int height,
1448 int width,
1449 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450{
1451#ifdef FEAT_GUI
1452 if (gui.in_use)
1453 gui_mch_invert_rectangle(row, col, height, width);
1454 else
1455#endif
1456 screen_draw_rectangle(row, col, height, width, invert);
1457}
1458
1459/*
1460 * Copy the currently selected area into the '*' register so it will be
1461 * available for pasting.
1462 * When "both" is TRUE also copy to the '+' register.
1463 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001465clip_copy_modeless_selection(int both UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466{
1467 char_u *buffer;
1468 char_u *bufp;
1469 int row;
1470 int start_col;
1471 int end_col;
1472 int line_end_col;
1473 int add_newline_flag = FALSE;
1474 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 int row1 = clip_star.start.lnum;
1477 int col1 = clip_star.start.col;
1478 int row2 = clip_star.end.lnum;
1479 int col2 = clip_star.end.col;
1480
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001481 /* Can't use ScreenLines unless initialized */
1482 if (ScreenLines == NULL)
1483 return;
1484
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 /*
1486 * Make sure row1 <= row2, and if row1 == row2 that col1 <= col2.
1487 */
1488 if (row1 > row2)
1489 {
1490 row = row1; row1 = row2; row2 = row;
1491 row = col1; col1 = col2; col2 = row;
1492 }
1493 else if (row1 == row2 && col1 > col2)
1494 {
1495 row = col1; col1 = col2; col2 = row;
1496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 /* correct starting point for being on right halve of double-wide char */
1498 p = ScreenLines + LineOffset[row1];
1499 if (enc_dbcs != 0)
1500 col1 -= (*mb_head_off)(p, p + col1);
1501 else if (enc_utf8 && p[col1] == 0)
1502 --col1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503
1504 /* Create a temporary buffer for storing the text */
1505 len = (row2 - row1 + 1) * Columns + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 if (enc_dbcs != 0)
1507 len *= 2; /* max. 2 bytes per display cell */
1508 else if (enc_utf8)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001509 len *= MB_MAXBYTES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 buffer = lalloc((long_u)len, TRUE);
1511 if (buffer == NULL) /* out of memory */
1512 return;
1513
1514 /* Process each row in the selection */
1515 for (bufp = buffer, row = row1; row <= row2; row++)
1516 {
1517 if (row == row1)
1518 start_col = col1;
1519 else
1520 start_col = 0;
1521
1522 if (row == row2)
1523 end_col = col2;
1524 else
1525 end_col = Columns;
1526
1527 line_end_col = clip_get_line_end(row);
1528
1529 /* See if we need to nuke some trailing whitespace */
1530 if (end_col >= Columns && (row < row2 || end_col > line_end_col))
1531 {
1532 /* Get rid of trailing whitespace */
1533 end_col = line_end_col;
1534 if (end_col < start_col)
1535 end_col = start_col;
1536
1537 /* If the last line extended to the end, add an extra newline */
1538 if (row == row2)
1539 add_newline_flag = TRUE;
1540 }
1541
1542 /* If after the first row, we need to always add a newline */
1543 if (row > row1 && !LineWraps[row - 1])
1544 *bufp++ = NL;
1545
1546 if (row < screen_Rows && end_col <= screen_Columns)
1547 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 if (enc_dbcs != 0)
1549 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001550 int i;
1551
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 p = ScreenLines + LineOffset[row];
1553 for (i = start_col; i < end_col; ++i)
1554 if (enc_dbcs == DBCS_JPNU && p[i] == 0x8e)
1555 {
1556 /* single-width double-byte char */
1557 *bufp++ = 0x8e;
1558 *bufp++ = ScreenLines2[LineOffset[row] + i];
1559 }
1560 else
1561 {
1562 *bufp++ = p[i];
1563 if (MB_BYTE2LEN(p[i]) == 2)
1564 *bufp++ = p[++i];
1565 }
1566 }
1567 else if (enc_utf8)
1568 {
1569 int off;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001570 int i;
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001571 int ci;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572
1573 off = LineOffset[row];
1574 for (i = start_col; i < end_col; ++i)
1575 {
1576 /* The base character is either in ScreenLinesUC[] or
1577 * ScreenLines[]. */
1578 if (ScreenLinesUC[off + i] == 0)
1579 *bufp++ = ScreenLines[off + i];
1580 else
1581 {
1582 bufp += utf_char2bytes(ScreenLinesUC[off + i], bufp);
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001583 for (ci = 0; ci < Screen_mco; ++ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001585 /* Add a composing character. */
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001586 if (ScreenLinesC[ci][off + i] == 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001587 break;
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001588 bufp += utf_char2bytes(ScreenLinesC[ci][off + i],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 }
1591 }
1592 /* Skip right halve of double-wide character. */
1593 if (ScreenLines[off + i + 1] == 0)
1594 ++i;
1595 }
1596 }
1597 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 {
1599 STRNCPY(bufp, ScreenLines + LineOffset[row] + start_col,
1600 end_col - start_col);
1601 bufp += end_col - start_col;
1602 }
1603 }
1604 }
1605
1606 /* Add a newline at the end if the selection ended there */
1607 if (add_newline_flag)
1608 *bufp++ = NL;
1609
1610 /* First cleanup any old selection and become the owner. */
1611 clip_free_selection(&clip_star);
1612 clip_own_selection(&clip_star);
1613
1614 /* Yank the text into the '*' register. */
1615 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_star);
1616
1617 /* Make the register contents available to the outside world. */
1618 clip_gen_set_selection(&clip_star);
1619
1620#ifdef FEAT_X11
1621 if (both)
1622 {
1623 /* Do the same for the '+' register. */
1624 clip_free_selection(&clip_plus);
1625 clip_own_selection(&clip_plus);
1626 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_plus);
1627 clip_gen_set_selection(&clip_plus);
1628 }
1629#endif
1630 vim_free(buffer);
1631}
1632
1633/*
1634 * Find the starting and ending positions of the word at the given row and
1635 * column. Only white-separated words are recognized here.
1636 */
1637#define CHAR_CLASS(c) (c <= ' ' ? ' ' : vim_iswordc(c))
1638
1639 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001640clip_get_word_boundaries(VimClipboard *cb, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641{
1642 int start_class;
1643 int temp_col;
1644 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 int mboff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001647 if (row >= screen_Rows || col >= screen_Columns || ScreenLines == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 return;
1649
1650 p = ScreenLines + LineOffset[row];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 /* Correct for starting in the right halve of a double-wide char */
1652 if (enc_dbcs != 0)
1653 col -= dbcs_screen_head_off(p, p + col);
1654 else if (enc_utf8 && p[col] == 0)
1655 --col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 start_class = CHAR_CLASS(p[col]);
1657
1658 temp_col = col;
1659 for ( ; temp_col > 0; temp_col--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 if (enc_dbcs != 0
1661 && (mboff = dbcs_screen_head_off(p, p + temp_col - 1)) > 0)
1662 temp_col -= mboff;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01001663 else if (CHAR_CLASS(p[temp_col - 1]) != start_class
1664 && !(enc_utf8 && p[temp_col - 1] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 break;
1666 cb->word_start_col = temp_col;
1667
1668 temp_col = col;
1669 for ( ; temp_col < screen_Columns; temp_col++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 if (enc_dbcs != 0 && dbcs_ptr2cells(p + temp_col) == 2)
1671 ++temp_col;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01001672 else if (CHAR_CLASS(p[temp_col]) != start_class
1673 && !(enc_utf8 && p[temp_col] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 break;
1675 cb->word_end_col = temp_col;
1676}
1677
1678/*
1679 * Find the column position for the last non-whitespace character on the given
1680 * line.
1681 */
1682 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001683clip_get_line_end(int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684{
1685 int i;
1686
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001687 if (row >= screen_Rows || ScreenLines == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 return 0;
1689 for (i = screen_Columns; i > 0; i--)
1690 if (ScreenLines[LineOffset[row] + i - 1] != ' ')
1691 break;
1692 return i;
1693}
1694
1695/*
1696 * Update the currently selected region by adding and/or subtracting from the
1697 * beginning or end and inverting the changed area(s).
1698 */
1699 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001700clip_update_modeless_selection(
1701 VimClipboard *cb,
1702 int row1,
1703 int col1,
1704 int row2,
1705 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706{
1707 /* See if we changed at the beginning of the selection */
1708 if (row1 != cb->start.lnum || col1 != (int)cb->start.col)
1709 {
1710 clip_invert_area(row1, col1, (int)cb->start.lnum, cb->start.col,
1711 CLIP_TOGGLE);
1712 cb->start.lnum = row1;
1713 cb->start.col = col1;
1714 }
1715
1716 /* See if we changed at the end of the selection */
1717 if (row2 != cb->end.lnum || col2 != (int)cb->end.col)
1718 {
1719 clip_invert_area((int)cb->end.lnum, cb->end.col, row2, col2,
1720 CLIP_TOGGLE);
1721 cb->end.lnum = row2;
1722 cb->end.col = col2;
1723 }
1724}
1725
1726 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001727clip_gen_own_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728{
1729#ifdef FEAT_XCLIPBOARD
1730# ifdef FEAT_GUI
1731 if (gui.in_use)
1732 return clip_mch_own_selection(cbd);
1733 else
1734# endif
1735 return clip_xterm_own_selection(cbd);
1736#else
1737 return clip_mch_own_selection(cbd);
1738#endif
1739}
1740
1741 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001742clip_gen_lose_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743{
1744#ifdef FEAT_XCLIPBOARD
1745# ifdef FEAT_GUI
1746 if (gui.in_use)
1747 clip_mch_lose_selection(cbd);
1748 else
1749# endif
1750 clip_xterm_lose_selection(cbd);
1751#else
1752 clip_mch_lose_selection(cbd);
1753#endif
1754}
1755
1756 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001757clip_gen_set_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758{
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001759 if (!clip_did_set_selection)
1760 {
1761 /* Updating postponed, so that accessing the system clipboard won't
Bram Moolenaarbdace832019-03-02 10:13:42 +01001762 * hang Vim when accessing it many times (e.g. on a :g command). */
Bram Moolenaar5c27fd12015-01-27 14:09:37 +01001763 if ((cbd == &clip_plus && (clip_unnamed_saved & CLIP_UNNAMED_PLUS))
1764 || (cbd == &clip_star && (clip_unnamed_saved & CLIP_UNNAMED)))
1765 {
1766 clipboard_needs_update = TRUE;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001767 return;
Bram Moolenaar5c27fd12015-01-27 14:09:37 +01001768 }
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770#ifdef FEAT_XCLIPBOARD
1771# ifdef FEAT_GUI
1772 if (gui.in_use)
1773 clip_mch_set_selection(cbd);
1774 else
1775# endif
1776 clip_xterm_set_selection(cbd);
1777#else
1778 clip_mch_set_selection(cbd);
1779#endif
1780}
1781
1782 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001783clip_gen_request_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784{
1785#ifdef FEAT_XCLIPBOARD
1786# ifdef FEAT_GUI
1787 if (gui.in_use)
1788 clip_mch_request_selection(cbd);
1789 else
1790# endif
1791 clip_xterm_request_selection(cbd);
1792#else
1793 clip_mch_request_selection(cbd);
1794#endif
1795}
1796
Bram Moolenaar113e1072019-01-20 15:30:40 +01001797#if (defined(FEAT_X11) && defined(USE_SYSTEM)) || defined(PROTO)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001798 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001799clip_gen_owner_exists(VimClipboard *cbd UNUSED)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001800{
1801#ifdef FEAT_XCLIPBOARD
1802# ifdef FEAT_GUI_GTK
1803 if (gui.in_use)
1804 return clip_gtk_owner_exists(cbd);
1805 else
1806# endif
1807 return clip_x11_owner_exists(cbd);
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02001808#else
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001809 return TRUE;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02001810#endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001811}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001812#endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001813
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814#endif /* FEAT_CLIPBOARD */
1815
1816/*****************************************************************************
1817 * Functions that handle the input buffer.
1818 * This is used for any GUI version, and the unix terminal version.
1819 *
1820 * For Unix, the input characters are buffered to be able to check for a
1821 * CTRL-C. This should be done with signals, but I don't know how to do that
1822 * in a portable way for a tty in RAW mode.
1823 *
1824 * For the client-server code in the console the received keys are put in the
1825 * input buffer.
1826 */
1827
1828#if defined(USE_INPUT_BUF) || defined(PROTO)
1829
1830/*
1831 * Internal typeahead buffer. Includes extra space for long key code
1832 * descriptions which would otherwise overflow. The buffer is considered full
1833 * when only this extra space (or part of it) remains.
1834 */
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001835#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_CLIENTSERVER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 /*
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001837 * NetBeans stuffs debugger commands into the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 * This requires a larger buffer...
1839 * (Madsen) Go with this for remote input as well ...
1840 */
1841# define INBUFLEN 4096
1842#else
1843# define INBUFLEN 250
1844#endif
1845
1846static char_u inbuf[INBUFLEN + MAX_KEY_CODE_LEN];
1847static int inbufcount = 0; /* number of chars in inbuf[] */
1848
1849/*
1850 * vim_is_input_buf_full(), vim_is_input_buf_empty(), add_to_input_buf(), and
1851 * trash_input_buf() are functions for manipulating the input buffer. These
1852 * are used by the gui_* calls when a GUI is used to handle keyboard input.
1853 */
1854
1855 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001856vim_is_input_buf_full(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857{
1858 return (inbufcount >= INBUFLEN);
1859}
1860
1861 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001862vim_is_input_buf_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863{
1864 return (inbufcount == 0);
1865}
1866
1867#if defined(FEAT_OLE) || defined(PROTO)
1868 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001869vim_free_in_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870{
1871 return (INBUFLEN - inbufcount);
1872}
1873#endif
1874
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001875#if defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001877vim_used_in_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878{
1879 return inbufcount;
1880}
1881#endif
1882
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883/*
1884 * Return the current contents of the input buffer and make it empty.
1885 * The returned pointer must be passed to set_input_buf() later.
1886 */
1887 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001888get_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889{
1890 garray_T *gap;
1891
1892 /* We use a growarray to store the data pointer and the length. */
1893 gap = (garray_T *)alloc((unsigned)sizeof(garray_T));
1894 if (gap != NULL)
1895 {
1896 /* Add one to avoid a zero size. */
1897 gap->ga_data = alloc((unsigned)inbufcount + 1);
1898 if (gap->ga_data != NULL)
1899 mch_memmove(gap->ga_data, inbuf, (size_t)inbufcount);
1900 gap->ga_len = inbufcount;
1901 }
1902 trash_input_buf();
1903 return (char_u *)gap;
1904}
1905
1906/*
1907 * Restore the input buffer with a pointer returned from get_input_buf().
1908 * The allocated memory is freed, this only works once!
1909 */
1910 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001911set_input_buf(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912{
1913 garray_T *gap = (garray_T *)p;
1914
1915 if (gap != NULL)
1916 {
1917 if (gap->ga_data != NULL)
1918 {
1919 mch_memmove(inbuf, gap->ga_data, gap->ga_len);
1920 inbufcount = gap->ga_len;
1921 vim_free(gap->ga_data);
1922 }
1923 vim_free(gap);
1924 }
1925}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927/*
1928 * Add the given bytes to the input buffer
1929 * Special keys start with CSI. A real CSI must have been translated to
1930 * CSI KS_EXTRA KE_CSI. K_SPECIAL doesn't require translation.
1931 */
1932 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001933add_to_input_buf(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934{
1935 if (inbufcount + len > INBUFLEN + MAX_KEY_CODE_LEN)
1936 return; /* Shouldn't ever happen! */
1937
1938#ifdef FEAT_HANGULIN
1939 if ((State & (INSERT|CMDLINE)) && hangul_input_state_get())
1940 if ((len = hangul_input_process(s, len)) == 0)
1941 return;
1942#endif
1943
1944 while (len--)
1945 inbuf[inbufcount++] = *s++;
1946}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948/*
1949 * Add "str[len]" to the input buffer while escaping CSI bytes.
1950 */
1951 void
1952add_to_input_buf_csi(char_u *str, int len)
1953{
1954 int i;
1955 char_u buf[2];
1956
1957 for (i = 0; i < len; ++i)
1958 {
1959 add_to_input_buf(str + i, 1);
1960 if (str[i] == CSI)
1961 {
1962 /* Turn CSI into K_CSI. */
1963 buf[0] = KS_EXTRA;
1964 buf[1] = (int)KE_CSI;
1965 add_to_input_buf(buf, 2);
1966 }
1967 }
1968}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969
1970#if defined(FEAT_HANGULIN) || defined(PROTO)
1971 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001972push_raw_key(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973{
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001974 char_u *tmpbuf;
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001975 char_u *inp = s;
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001976
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001977 /* use the conversion result if possible */
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001978 tmpbuf = hangul_string_convert(s, &len);
1979 if (tmpbuf != NULL)
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001980 inp = tmpbuf;
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01001981
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001982 for (; len--; inp++)
1983 {
1984 inbuf[inbufcount++] = *inp;
1985 if (*inp == CSI)
Bram Moolenaar00ded432016-03-03 11:45:15 +01001986 {
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001987 /* Turn CSI into K_CSI. */
1988 inbuf[inbufcount++] = KS_EXTRA;
1989 inbuf[inbufcount++] = (int)KE_CSI;
Bram Moolenaar00ded432016-03-03 11:45:15 +01001990 }
Bram Moolenaar00ded432016-03-03 11:45:15 +01001991 }
Bram Moolenaar179f1b92016-03-04 22:52:34 +01001992 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993}
1994#endif
1995
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996/* Remove everything from the input buffer. Called when ^C is found */
1997 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001998trash_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999{
2000 inbufcount = 0;
2001}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002
2003/*
2004 * Read as much data from the input buffer as possible up to maxlen, and store
2005 * it in buf.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 */
2007 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002008read_from_input_buf(char_u *buf, long maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009{
2010 if (inbufcount == 0) /* if the buffer is empty, fill it */
2011 fill_input_buf(TRUE);
2012 if (maxlen > inbufcount)
2013 maxlen = inbufcount;
2014 mch_memmove(buf, inbuf, (size_t)maxlen);
2015 inbufcount -= maxlen;
2016 if (inbufcount)
2017 mch_memmove(inbuf, inbuf + maxlen, (size_t)inbufcount);
2018 return (int)maxlen;
2019}
2020
2021 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002022fill_input_buf(int exit_on_error UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023{
Bram Moolenaard0573012017-10-28 21:11:06 +02002024#if defined(UNIX) || defined(VMS) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 int len;
2026 int try;
2027 static int did_read_something = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 static char_u *rest = NULL; /* unconverted rest of previous read */
2029 static int restlen = 0;
2030 int unconverted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031#endif
2032
2033#ifdef FEAT_GUI
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002034 if (gui.in_use
2035# ifdef NO_CONSOLE_INPUT
2036 /* Don't use the GUI input when the window hasn't been opened yet.
2037 * We get here from ui_inchar() when we should try reading from stdin. */
2038 && !no_console_input()
2039# endif
2040 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 {
2042 gui_mch_update();
2043 return;
2044 }
2045#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02002046#if defined(UNIX) || defined(VMS) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 if (vim_is_input_buf_full())
2048 return;
2049 /*
2050 * Fill_input_buf() is only called when we really need a character.
2051 * If we can't get any, but there is some in the buffer, just return.
2052 * If we can't get any, and there isn't any in the buffer, we give up and
2053 * exit Vim.
2054 */
2055# ifdef __BEOS__
2056 /*
2057 * On the BeBox version (for now), all input is secretly performed within
2058 * beos_select() which is called from RealWaitForChar().
2059 */
2060 while (!vim_is_input_buf_full() && RealWaitForChar(read_cmd_fd, 0, NULL))
2061 ;
2062 len = inbufcount;
2063 inbufcount = 0;
2064# else
2065
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 if (rest != NULL)
2067 {
2068 /* Use remainder of previous call, starts with an invalid character
2069 * that may become valid when reading more. */
2070 if (restlen > INBUFLEN - inbufcount)
2071 unconverted = INBUFLEN - inbufcount;
2072 else
2073 unconverted = restlen;
2074 mch_memmove(inbuf + inbufcount, rest, unconverted);
2075 if (unconverted == restlen)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002076 VIM_CLEAR(rest);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 else
2078 {
2079 restlen -= unconverted;
2080 mch_memmove(rest, rest + unconverted, restlen);
2081 }
2082 inbufcount += unconverted;
2083 }
2084 else
2085 unconverted = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086
2087 len = 0; /* to avoid gcc warning */
2088 for (try = 0; try < 100; ++try)
2089 {
Bram Moolenaar4e601e32018-04-24 13:29:51 +02002090 size_t readlen = (size_t)((INBUFLEN - inbufcount)
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002091 / input_conv.vc_factor);
Bram Moolenaar4e601e32018-04-24 13:29:51 +02002092# ifdef VMS
Bram Moolenaar49943732018-04-24 20:27:26 +02002093 len = vms_read((char *)inbuf + inbufcount, readlen);
Bram Moolenaar4e601e32018-04-24 13:29:51 +02002094# else
2095 len = read(read_cmd_fd, (char *)inbuf + inbufcount, readlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096# endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002097
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 if (len > 0 || got_int)
2099 break;
2100 /*
2101 * If reading stdin results in an error, continue reading stderr.
2102 * This helps when using "foo | xargs vim".
2103 */
2104 if (!did_read_something && !isatty(read_cmd_fd) && read_cmd_fd == 0)
2105 {
2106 int m = cur_tmode;
2107
2108 /* We probably set the wrong file descriptor to raw mode. Switch
2109 * back to cooked mode, use another descriptor and set the mode to
2110 * what it was. */
2111 settmode(TMODE_COOK);
2112#ifdef HAVE_DUP
2113 /* Use stderr for stdin, also works for shell commands. */
2114 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002115 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116#else
2117 read_cmd_fd = 2; /* read from stderr instead of stdin */
2118#endif
2119 settmode(m);
2120 }
2121 if (!exit_on_error)
2122 return;
2123 }
2124# endif
2125 if (len <= 0 && !got_int)
2126 read_error_exit();
2127 if (len > 0)
2128 did_read_something = TRUE;
2129 if (got_int)
2130 {
2131 /* Interrupted, pretend a CTRL-C was typed. */
2132 inbuf[0] = 3;
2133 inbufcount = 1;
2134 }
2135 else
2136 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 /*
2138 * May perform conversion on the input characters.
2139 * Include the unconverted rest of the previous call.
2140 * If there is an incomplete char at the end it is kept for the next
2141 * time, reading more bytes should make conversion possible.
2142 * Don't do this in the unlikely event that the input buffer is too
2143 * small ("rest" still contains more bytes).
2144 */
2145 if (input_conv.vc_type != CONV_NONE)
2146 {
2147 inbufcount -= unconverted;
2148 len = convert_input_safe(inbuf + inbufcount,
2149 len + unconverted, INBUFLEN - inbufcount,
2150 rest == NULL ? &rest : NULL, &restlen);
2151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 while (len-- > 0)
2153 {
2154 /*
2155 * if a CTRL-C was typed, remove it from the buffer and set got_int
2156 */
2157 if (inbuf[inbufcount] == 3 && ctrl_c_interrupts)
2158 {
2159 /* remove everything typed before the CTRL-C */
2160 mch_memmove(inbuf, inbuf + inbufcount, (size_t)(len + 1));
2161 inbufcount = 0;
2162 got_int = TRUE;
2163 }
2164 ++inbufcount;
2165 }
2166 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002167#endif /* UNIX or VMS*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168}
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002169#endif /* defined(UNIX) || defined(FEAT_GUI) || defined(VMS) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170
2171/*
2172 * Exit because of an input read error.
2173 */
2174 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002175read_error_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176{
2177 if (silent_mode) /* Normal way to exit for "ex -s" */
2178 getout(0);
2179 STRCPY(IObuff, _("Vim: Error reading input, exiting...\n"));
2180 preserve_exit();
2181}
2182
2183#if defined(CURSOR_SHAPE) || defined(PROTO)
2184/*
2185 * May update the shape of the cursor.
2186 */
2187 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002188ui_cursor_shape_forced(int forced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189{
2190# ifdef FEAT_GUI
2191 if (gui.in_use)
2192 gui_update_cursor_later();
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002193 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002195 term_cursor_mode(forced);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002196
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197# ifdef MCH_CURSOR_SHAPE
2198 mch_update_cursor();
2199# endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002200
2201# ifdef FEAT_CONCEAL
Bram Moolenaarb9464822018-05-10 15:09:49 +02002202 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002203# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002205
2206 void
2207ui_cursor_shape(void)
2208{
2209 ui_cursor_shape_forced(FALSE);
2210}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211#endif
2212
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213/*
2214 * Check bounds for column number
2215 */
2216 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002217check_col(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218{
2219 if (col < 0)
2220 return 0;
2221 if (col >= (int)screen_Columns)
2222 return (int)screen_Columns - 1;
2223 return col;
2224}
2225
2226/*
2227 * Check bounds for row number
2228 */
2229 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002230check_row(int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231{
2232 if (row < 0)
2233 return 0;
2234 if (row >= (int)screen_Rows)
2235 return (int)screen_Rows - 1;
2236 return row;
2237}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238
2239/*
2240 * Stuff for the X clipboard. Shared between VMS and Unix.
2241 */
2242
2243#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) || defined(PROTO)
2244# include <X11/Xatom.h>
2245# include <X11/Intrinsic.h>
2246
2247/*
2248 * Open the application context (if it hasn't been opened yet).
2249 * Used for Motif and Athena GUI and the xterm clipboard.
2250 */
2251 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002252open_app_context(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253{
2254 if (app_context == NULL)
2255 {
2256 XtToolkitInitialize();
2257 app_context = XtCreateApplicationContext();
2258 }
2259}
2260
2261static Atom vim_atom; /* Vim's own special selection format */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262static Atom vimenc_atom; /* Vim's extended selection format */
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002263static Atom utf8_atom;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264static Atom compound_text_atom;
2265static Atom text_atom;
2266static Atom targets_atom;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002267static Atom timestamp_atom; /* Used to get a timestamp */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268
2269 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002270x11_setup_atoms(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271{
2272 vim_atom = XInternAtom(dpy, VIM_ATOM_NAME, False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 vimenc_atom = XInternAtom(dpy, VIMENC_ATOM_NAME,False);
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002274 utf8_atom = XInternAtom(dpy, "UTF8_STRING", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 compound_text_atom = XInternAtom(dpy, "COMPOUND_TEXT", False);
2276 text_atom = XInternAtom(dpy, "TEXT", False);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002277 targets_atom = XInternAtom(dpy, "TARGETS", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 clip_star.sel_atom = XA_PRIMARY;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002279 clip_plus.sel_atom = XInternAtom(dpy, "CLIPBOARD", False);
2280 timestamp_atom = XInternAtom(dpy, "TIMESTAMP", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281}
2282
2283/*
2284 * X Selection stuff, for cutting and pasting text to other windows.
2285 */
2286
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002287static Boolean clip_x11_convert_selection_cb(Widget w, Atom *sel_atom, Atom *target, Atom *type, XtPointer *value, long_u *length, int *format);
2288static void clip_x11_lose_ownership_cb(Widget w, Atom *sel_atom);
2289static void clip_x11_notify_cb(Widget w, Atom *sel_atom, Atom *target);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002290
2291/*
2292 * Property callback to get a timestamp for XtOwnSelection.
2293 */
2294 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002295clip_x11_timestamp_cb(
2296 Widget w,
2297 XtPointer n UNUSED,
2298 XEvent *event,
2299 Boolean *cont UNUSED)
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002300{
2301 Atom actual_type;
2302 int format;
2303 unsigned long nitems, bytes_after;
2304 unsigned char *prop=NULL;
2305 XPropertyEvent *xproperty=&event->xproperty;
2306
2307 /* Must be a property notify, state can't be Delete (True), has to be
2308 * one of the supported selection types. */
2309 if (event->type != PropertyNotify || xproperty->state
2310 || (xproperty->atom != clip_star.sel_atom
2311 && xproperty->atom != clip_plus.sel_atom))
2312 return;
2313
2314 if (XGetWindowProperty(xproperty->display, xproperty->window,
2315 xproperty->atom, 0, 0, False, timestamp_atom, &actual_type, &format,
2316 &nitems, &bytes_after, &prop))
2317 return;
2318
2319 if (prop)
2320 XFree(prop);
2321
2322 /* Make sure the property type is "TIMESTAMP" and it's 32 bits. */
2323 if (actual_type != timestamp_atom || format != 32)
2324 return;
2325
2326 /* Get the selection, using the event timestamp. */
Bram Moolenaar62b42182010-09-21 22:09:37 +02002327 if (XtOwnSelection(w, xproperty->atom, xproperty->time,
2328 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002329 clip_x11_notify_cb) == OK)
Bram Moolenaar62b42182010-09-21 22:09:37 +02002330 {
2331 /* Set the "owned" flag now, there may have been a call to
2332 * lose_ownership_cb in between. */
2333 if (xproperty->atom == clip_plus.sel_atom)
2334 clip_plus.owned = TRUE;
2335 else
2336 clip_star.owned = TRUE;
2337 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002338}
2339
2340 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002341x11_setup_selection(Widget w)
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002342{
2343 XtAddEventHandler(w, PropertyChangeMask, False,
2344 /*(XtEventHandler)*/clip_x11_timestamp_cb, (XtPointer)NULL);
2345}
2346
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002348clip_x11_request_selection_cb(
2349 Widget w UNUSED,
2350 XtPointer success,
2351 Atom *sel_atom,
2352 Atom *type,
2353 XtPointer value,
2354 long_u *length,
2355 int *format)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356{
Bram Moolenaard44347f2011-06-19 01:14:29 +02002357 int motion_type = MAUTO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 long_u len;
2359 char_u *p;
2360 char **text_list = NULL;
2361 VimClipboard *cbd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363
2364 if (*sel_atom == clip_plus.sel_atom)
2365 cbd = &clip_plus;
2366 else
2367 cbd = &clip_star;
2368
2369 if (value == NULL || *length == 0)
2370 {
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002371 clip_free_selection(cbd); /* nothing received, clear register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 *(int *)success = FALSE;
2373 return;
2374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 p = (char_u *)value;
2376 len = *length;
2377 if (*type == vim_atom)
2378 {
2379 motion_type = *p++;
2380 len--;
2381 }
2382
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 else if (*type == vimenc_atom)
2384 {
2385 char_u *enc;
2386 vimconv_T conv;
2387 int convlen;
2388
2389 motion_type = *p++;
2390 --len;
2391
2392 enc = p;
2393 p += STRLEN(p) + 1;
2394 len -= p - enc;
2395
2396 /* If the encoding of the text is different from 'encoding', attempt
2397 * converting it. */
2398 conv.vc_type = CONV_NONE;
2399 convert_setup(&conv, enc, p_enc);
2400 if (conv.vc_type != CONV_NONE)
2401 {
2402 convlen = len; /* Need to use an int here. */
2403 tmpbuf = string_convert(&conv, p, &convlen);
2404 len = convlen;
2405 if (tmpbuf != NULL)
2406 p = tmpbuf;
2407 convert_setup(&conv, NULL, NULL);
2408 }
2409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002411 else if (*type == compound_text_atom
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002412 || *type == utf8_atom
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002413 || (enc_dbcs != 0 && *type == text_atom))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 {
2415 XTextProperty text_prop;
2416 int n_text = 0;
2417 int status;
2418
2419 text_prop.value = (unsigned char *)value;
2420 text_prop.encoding = *type;
2421 text_prop.format = *format;
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002422 text_prop.nitems = len;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002423#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaare2e663f2013-03-07 18:02:30 +01002424 if (*type == utf8_atom)
2425 status = Xutf8TextPropertyToTextList(X_DISPLAY, &text_prop,
2426 &text_list, &n_text);
2427 else
2428#endif
2429 status = XmbTextPropertyToTextList(X_DISPLAY, &text_prop,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 &text_list, &n_text);
2431 if (status != Success || n_text < 1)
2432 {
2433 *(int *)success = FALSE;
2434 return;
2435 }
2436 p = (char_u *)text_list[0];
2437 len = STRLEN(p);
2438 }
2439 clip_yank_selection(motion_type, p, (long)len, cbd);
2440
2441 if (text_list != NULL)
2442 XFreeStringList(text_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 XtFree((char *)value);
2445 *(int *)success = TRUE;
2446}
2447
2448 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002449clip_x11_request_selection(
2450 Widget myShell,
2451 Display *dpy,
2452 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453{
2454 XEvent event;
2455 Atom type;
2456 static int success;
2457 int i;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002458 time_t start_time;
2459 int timed_out = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002461 for (i = 0; i < 6; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 {
2463 switch (i)
2464 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 case 0: type = vimenc_atom; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 case 1: type = vim_atom; break;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002467 case 2: type = utf8_atom; break;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002468 case 3: type = compound_text_atom; break;
2469 case 4: type = text_atom; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 default: type = XA_STRING;
2471 }
Bram Moolenaar81851112013-04-12 12:27:30 +02002472 if (type == utf8_atom
2473# if defined(X_HAVE_UTF8_STRING)
2474 && !enc_utf8
2475# endif
2476 )
2477 /* Only request utf-8 when 'encoding' is utf8 and
2478 * Xutf8TextPropertyToTextList is available. */
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002479 continue;
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002480 success = MAYBE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 XtGetSelectionValue(myShell, cbd->sel_atom, type,
2482 clip_x11_request_selection_cb, (XtPointer)&success, CurrentTime);
2483
2484 /* Make sure the request for the selection goes out before waiting for
2485 * a response. */
2486 XFlush(dpy);
2487
2488 /*
2489 * Wait for result of selection request, otherwise if we type more
2490 * characters, then they will appear before the one that requested the
2491 * paste! Don't worry, we will catch up with any other events later.
2492 */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002493 start_time = time(NULL);
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002494 while (success == MAYBE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002496 if (XCheckTypedEvent(dpy, PropertyNotify, &event)
2497 || XCheckTypedEvent(dpy, SelectionNotify, &event)
2498 || XCheckTypedEvent(dpy, SelectionRequest, &event))
Bram Moolenaar89417b92008-09-07 19:48:53 +00002499 {
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002500 /* This is where clip_x11_request_selection_cb() should be
2501 * called. It may actually happen a bit later, so we loop
2502 * until "success" changes.
2503 * We may get a SelectionRequest here and if we don't handle
2504 * it we hang. KDE klipper does this, for example.
2505 * We need to handle a PropertyNotify for large selections. */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002506 XtDispatchEvent(&event);
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002507 continue;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509
Bram Moolenaar89417b92008-09-07 19:48:53 +00002510 /* Time out after 2 to 3 seconds to avoid that we hang when the
2511 * other process doesn't respond. Note that the SelectionNotify
2512 * event may still come later when the selection owner comes back
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002513 * to life and the text gets inserted unexpectedly. Don't know
2514 * why that happens or how to avoid that :-(. */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002515 if (time(NULL) > start_time + 2)
2516 {
2517 timed_out = TRUE;
2518 break;
2519 }
2520
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 /* Do we need this? Probably not. */
2522 XSync(dpy, False);
2523
Bram Moolenaar89417b92008-09-07 19:48:53 +00002524 /* Wait for 1 msec to avoid that we eat up all CPU time. */
2525 ui_delay(1L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 }
2527
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002528 if (success == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 return;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002530
2531 /* don't do a retry with another type after timing out, otherwise we
2532 * hang for 15 seconds. */
2533 if (timed_out)
2534 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 }
2536
2537 /* Final fallback position - use the X CUT_BUFFER0 store */
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002538 yank_cut_buffer0(dpy, cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539}
2540
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 static Boolean
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002542clip_x11_convert_selection_cb(
2543 Widget w UNUSED,
2544 Atom *sel_atom,
2545 Atom *target,
2546 Atom *type,
2547 XtPointer *value,
2548 long_u *length,
2549 int *format)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550{
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002551 static char_u *save_result = NULL;
2552 static long_u save_length = 0;
2553 char_u *string;
2554 int motion_type;
2555 VimClipboard *cbd;
2556 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
2558 if (*sel_atom == clip_plus.sel_atom)
2559 cbd = &clip_plus;
2560 else
2561 cbd = &clip_star;
2562
2563 if (!cbd->owned)
2564 return False; /* Shouldn't ever happen */
2565
2566 /* requestor wants to know what target types we support */
2567 if (*target == targets_atom)
2568 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002569 static Atom array[7];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 *value = (XtPointer)array;
2572 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 array[i++] = targets_atom;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 array[i++] = vimenc_atom;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 array[i++] = vim_atom;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002576 if (enc_utf8)
2577 array[i++] = utf8_atom;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002578 array[i++] = XA_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 array[i++] = text_atom;
2580 array[i++] = compound_text_atom;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002581
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 *type = XA_ATOM;
2583 /* This used to be: *format = sizeof(Atom) * 8; but that caused
2584 * crashes on 64 bit machines. (Peter Derr) */
2585 *format = 32;
2586 *length = i;
2587 return True;
2588 }
2589
2590 if ( *target != XA_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 && *target != vimenc_atom
Bram Moolenaar980e58f2014-06-12 13:28:30 +02002592 && (*target != utf8_atom || !enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 && *target != vim_atom
2594 && *target != text_atom
2595 && *target != compound_text_atom)
2596 return False;
2597
2598 clip_get_selection(cbd);
2599 motion_type = clip_convert_selection(&string, length, cbd);
2600 if (motion_type < 0)
2601 return False;
2602
2603 /* For our own format, the first byte contains the motion type */
2604 if (*target == vim_atom)
2605 (*length)++;
2606
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 /* Our own format with encoding: motion 'encoding' NUL text */
2608 if (*target == vimenc_atom)
2609 *length += STRLEN(p_enc) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002611 if (save_length < *length || save_length / 2 >= *length)
2612 *value = XtRealloc((char *)save_result, (Cardinal)*length + 1);
2613 else
2614 *value = save_result;
2615 if (*value == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 {
2617 vim_free(string);
2618 return False;
2619 }
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002620 save_result = (char_u *)*value;
2621 save_length = *length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002623 if (*target == XA_STRING || (*target == utf8_atom && enc_utf8))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002625 mch_memmove(save_result, string, (size_t)(*length));
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002626 *type = *target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 }
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002628 else if (*target == compound_text_atom || *target == text_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 {
2630 XTextProperty text_prop;
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002631 char *string_nt = (char *)save_result;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002632 int conv_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633
2634 /* create NUL terminated string which XmbTextListToTextProperty wants */
2635 mch_memmove(string_nt, string, (size_t)*length);
2636 string_nt[*length] = NUL;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002637 conv_result = XmbTextListToTextProperty(X_DISPLAY, (char **)&string_nt,
2638 1, XCompoundTextStyle, &text_prop);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002639 if (conv_result != Success)
2640 {
2641 vim_free(string);
2642 return False;
2643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 *value = (XtPointer)(text_prop.value); /* from plain text */
2645 *length = text_prop.nitems;
2646 *type = compound_text_atom;
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002647 XtFree((char *)save_result);
2648 save_result = (char_u *)*value;
2649 save_length = *length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 else if (*target == vimenc_atom)
2652 {
2653 int l = STRLEN(p_enc);
2654
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002655 save_result[0] = motion_type;
2656 STRCPY(save_result + 1, p_enc);
2657 mch_memmove(save_result + l + 2, string, (size_t)(*length - l - 2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 *type = vimenc_atom;
2659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 else
2661 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002662 save_result[0] = motion_type;
2663 mch_memmove(save_result + 1, string, (size_t)(*length - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 *type = vim_atom;
2665 }
2666 *format = 8; /* 8 bits per char */
2667 vim_free(string);
2668 return True;
2669}
2670
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002672clip_x11_lose_ownership_cb(Widget w UNUSED, Atom *sel_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673{
2674 if (*sel_atom == clip_plus.sel_atom)
2675 clip_lose_selection(&clip_plus);
2676 else
2677 clip_lose_selection(&clip_star);
2678}
2679
2680 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002681clip_x11_lose_selection(Widget myShell, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682{
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002683 XtDisownSelection(myShell, cbd->sel_atom,
2684 XtLastTimestampProcessed(XtDisplay(myShell)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685}
2686
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002687 static void
2688clip_x11_notify_cb(Widget w UNUSED, Atom *sel_atom UNUSED, Atom *target UNUSED)
2689{
2690 /* To prevent automatically freeing the selection value. */
2691}
2692
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002694clip_x11_own_selection(Widget myShell, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695{
Bram Moolenaar62b42182010-09-21 22:09:37 +02002696 /* When using the GUI we have proper timestamps, use the one of the last
2697 * event. When in the console we don't get events (the terminal gets
2698 * them), Get the time by a zero-length append, clip_x11_timestamp_cb will
2699 * be called with the current timestamp. */
2700#ifdef FEAT_GUI
2701 if (gui.in_use)
2702 {
2703 if (XtOwnSelection(myShell, cbd->sel_atom,
2704 XtLastTimestampProcessed(XtDisplay(myShell)),
2705 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002706 clip_x11_notify_cb) == False)
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +01002707 return FAIL;
Bram Moolenaar62b42182010-09-21 22:09:37 +02002708 }
2709 else
2710#endif
2711 {
2712 if (!XChangeProperty(XtDisplay(myShell), XtWindow(myShell),
2713 cbd->sel_atom, timestamp_atom, 32, PropModeAppend, NULL, 0))
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +01002714 return FAIL;
Bram Moolenaar62b42182010-09-21 22:09:37 +02002715 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002716 /* Flush is required in a terminal as nothing else is doing it. */
2717 XFlush(XtDisplay(myShell));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 return OK;
2719}
2720
2721/*
2722 * Send the current selection to the clipboard. Do nothing for X because we
2723 * will fill in the selection only when requested by another app.
2724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002726clip_x11_set_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727{
2728}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002729
Bram Moolenaar113e1072019-01-20 15:30:40 +01002730#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD) && defined(USE_SYSTEM)) \
2731 || defined(PROTO)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002732 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002733clip_x11_owner_exists(VimClipboard *cbd)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002734{
2735 return XGetSelectionOwner(X_DISPLAY, cbd->sel_atom) != None;
2736}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737#endif
Bram Moolenaar113e1072019-01-20 15:30:40 +01002738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002740#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) \
2741 || defined(FEAT_GUI_GTK) || defined(PROTO)
2742/*
2743 * Get the contents of the X CUT_BUFFER0 and put it in "cbd".
2744 */
2745 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002746yank_cut_buffer0(Display *dpy, VimClipboard *cbd)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002747{
2748 int nbytes = 0;
2749 char_u *buffer = (char_u *)XFetchBuffer(dpy, &nbytes, 0);
2750
2751 if (nbytes > 0)
2752 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002753 int done = FALSE;
2754
2755 /* CUT_BUFFER0 is supposed to be always latin1. Convert to 'enc' when
2756 * using a multi-byte encoding. Conversion between two 8-bit
2757 * character sets usually fails and the text might actually be in
2758 * 'enc' anyway. */
2759 if (has_mbyte)
2760 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002761 char_u *conv_buf;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002762 vimconv_T vc;
2763
2764 vc.vc_type = CONV_NONE;
2765 if (convert_setup(&vc, (char_u *)"latin1", p_enc) == OK)
2766 {
2767 conv_buf = string_convert(&vc, buffer, &nbytes);
2768 if (conv_buf != NULL)
2769 {
2770 clip_yank_selection(MCHAR, conv_buf, (long)nbytes, cbd);
2771 vim_free(conv_buf);
2772 done = TRUE;
2773 }
2774 convert_setup(&vc, NULL, NULL);
2775 }
2776 }
2777 if (!done) /* use the text without conversion */
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002778 clip_yank_selection(MCHAR, buffer, (long)nbytes, cbd);
2779 XFree((void *)buffer);
2780 if (p_verbose > 0)
2781 {
2782 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002783 verb_msg(_("Used CUT_BUFFER0 instead of empty selection"));
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002784 verbose_leave();
2785 }
2786 }
2787}
2788#endif
2789
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790#if defined(FEAT_MOUSE) || defined(PROTO)
2791
2792/*
2793 * Move the cursor to the specified row and column on the screen.
Bram Moolenaar49325942007-05-10 19:19:59 +00002794 * Change current window if necessary. Returns an integer with the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 * CURSOR_MOVED bit set if the cursor has moved or unset otherwise.
2796 *
2797 * The MOUSE_FOLD_CLOSE bit is set when clicked on the '-' in a fold column.
2798 * The MOUSE_FOLD_OPEN bit is set when clicked on the '+' in a fold column.
2799 *
2800 * If flags has MOUSE_FOCUS, then the current window will not be changed, and
2801 * if the mouse is outside the window then the text will scroll, or if the
2802 * mouse was previously on a status line, then the status line may be dragged.
2803 *
2804 * If flags has MOUSE_MAY_VIS, then VIsual mode will be started before the
2805 * cursor is moved unless the cursor was on a status line.
2806 * This function returns one of IN_UNKNOWN, IN_BUFFER, IN_STATUS_LINE or
2807 * IN_SEP_LINE depending on where the cursor was clicked.
2808 *
2809 * If flags has MOUSE_MAY_STOP_VIS, then Visual mode will be stopped, unless
2810 * the mouse is on the status line of the same window.
2811 *
2812 * If flags has MOUSE_DID_MOVE, nothing is done if the mouse didn't move since
2813 * the last call.
2814 *
2815 * If flags has MOUSE_SETPOS, nothing is done, only the current position is
2816 * remembered.
2817 */
2818 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002819jump_to_mouse(
2820 int flags,
2821 int *inclusive, /* used for inclusive operator, can be NULL */
2822 int which_button) /* MOUSE_LEFT, MOUSE_RIGHT, MOUSE_MIDDLE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823{
2824 static int on_status_line = 0; /* #lines below bottom of window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 static int on_sep_line = 0; /* on separator right of window */
Bram Moolenaareb163d72017-09-23 15:08:17 +02002826#ifdef FEAT_MENU
2827 static int in_winbar = FALSE;
2828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 static int prev_row = -1;
2830 static int prev_col = -1;
2831 static win_T *dragwin = NULL; /* window being dragged */
2832 static int did_drag = FALSE; /* drag was noticed */
2833
2834 win_T *wp, *old_curwin;
2835 pos_T old_cursor;
2836 int count;
2837 int first;
2838 int row = mouse_row;
2839 int col = mouse_col;
2840#ifdef FEAT_FOLDING
2841 int mouse_char;
2842#endif
2843
2844 mouse_past_bottom = FALSE;
2845 mouse_past_eol = FALSE;
2846
2847 if (flags & MOUSE_RELEASED)
2848 {
2849 /* On button release we may change window focus if positioned on a
2850 * status line and no dragging happened. */
2851 if (dragwin != NULL && !did_drag)
2852 flags &= ~(MOUSE_FOCUS | MOUSE_DID_MOVE);
2853 dragwin = NULL;
2854 did_drag = FALSE;
2855 }
2856
2857 if ((flags & MOUSE_DID_MOVE)
2858 && prev_row == mouse_row
2859 && prev_col == mouse_col)
2860 {
2861retnomove:
Bram Moolenaar49325942007-05-10 19:19:59 +00002862 /* before moving the cursor for a left click which is NOT in a status
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 * line, stop Visual mode */
2864 if (on_status_line)
2865 return IN_STATUS_LINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 if (on_sep_line)
2867 return IN_SEP_LINE;
Bram Moolenaard327b0c2017-11-12 16:56:12 +01002868#ifdef FEAT_MENU
2869 if (in_winbar)
2870 {
2871 /* A quick second click may arrive as a double-click, but we use it
2872 * as a second click in the WinBar. */
2873 if ((mod_mask & MOD_MASK_MULTI_CLICK) && !(flags & MOUSE_RELEASED))
2874 {
2875 wp = mouse_find_win(&row, &col);
2876 if (wp == NULL)
2877 return IN_UNKNOWN;
2878 winbar_click(wp, col);
2879 }
2880 return IN_OTHER_WIN | MOUSE_WINBAR;
2881 }
2882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 if (flags & MOUSE_MAY_STOP_VIS)
2884 {
2885 end_visual_mode();
2886 redraw_curbuf_later(INVERTED); /* delete the inversion */
2887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2889 /* Continue a modeless selection in another window. */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002890 if (cmdwin_type != 0 && row < curwin->w_winrow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 return IN_OTHER_WIN;
2892#endif
2893 return IN_BUFFER;
2894 }
2895
2896 prev_row = mouse_row;
2897 prev_col = mouse_col;
2898
2899 if (flags & MOUSE_SETPOS)
2900 goto retnomove; /* ugly goto... */
2901
2902#ifdef FEAT_FOLDING
2903 /* Remember the character under the mouse, it might be a '-' or '+' in the
2904 * fold column. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002905 if (row >= 0 && row < Rows && col >= 0 && col <= Columns
2906 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 mouse_char = ScreenLines[LineOffset[row] + col];
2908 else
2909 mouse_char = ' ';
2910#endif
2911
2912 old_curwin = curwin;
2913 old_cursor = curwin->w_cursor;
2914
2915 if (!(flags & MOUSE_FOCUS))
2916 {
2917 if (row < 0 || col < 0) /* check if it makes sense */
2918 return IN_UNKNOWN;
2919
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 /* find the window where the row is in */
2921 wp = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02002922 if (wp == NULL)
2923 return IN_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 dragwin = NULL;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002925
2926#ifdef FEAT_MENU
2927 if (row == -1)
2928 {
2929 /* A click in the window toolbar does not enter another window or
2930 * change Visual highlighting. */
2931 winbar_click(wp, col);
Bram Moolenaareb163d72017-09-23 15:08:17 +02002932 in_winbar = TRUE;
2933 return IN_OTHER_WIN | MOUSE_WINBAR;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002934 }
Bram Moolenaareb163d72017-09-23 15:08:17 +02002935 in_winbar = FALSE;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002936#endif
2937
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 /*
2939 * winpos and height may change in win_enter()!
2940 */
2941 if (row >= wp->w_height) /* In (or below) status line */
2942 {
2943 on_status_line = row - wp->w_height + 1;
2944 dragwin = wp;
2945 }
2946 else
2947 on_status_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 if (col >= wp->w_width) /* In separator line */
2949 {
2950 on_sep_line = col - wp->w_width + 1;
2951 dragwin = wp;
2952 }
2953 else
2954 on_sep_line = 0;
2955
2956 /* The rightmost character of the status line might be a vertical
2957 * separator character if there is no connecting window to the right. */
2958 if (on_status_line && on_sep_line)
2959 {
2960 if (stl_connected(wp))
2961 on_sep_line = 0;
2962 else
2963 on_status_line = 0;
2964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 /* Before jumping to another buffer, or moving the cursor for a left
2967 * click, stop Visual mode. */
2968 if (VIsual_active
2969 && (wp->w_buffer != curwin->w_buffer
Bram Moolenaar4033c552017-09-16 20:54:51 +02002970 || (!on_status_line && !on_sep_line
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002971#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002973# ifdef FEAT_RIGHTLEFT
Bram Moolenaar02631462017-09-22 15:20:32 +02002974 wp->w_p_rl ? col < wp->w_width - wp->w_p_fdc :
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975# endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002976 col >= wp->w_p_fdc
2977# ifdef FEAT_CMDWIN
2978 + (cmdwin_type == 0 && wp == curwin ? 0 : 1)
2979# endif
2980 )
2981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 && (flags & MOUSE_MAY_STOP_VIS))))
2983 {
2984 end_visual_mode();
2985 redraw_curbuf_later(INVERTED); /* delete the inversion */
2986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987#ifdef FEAT_CMDWIN
2988 if (cmdwin_type != 0 && wp != curwin)
2989 {
2990 /* A click outside the command-line window: Use modeless
Bram Moolenaarf679a432010-03-02 18:16:09 +01002991 * selection if possible. Allow dragging the status lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 on_sep_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993# ifdef FEAT_CLIPBOARD
2994 if (on_status_line)
2995 return IN_STATUS_LINE;
2996 return IN_OTHER_WIN;
2997# else
2998 row = 0;
2999 col += wp->w_wincol;
3000 wp = curwin;
3001# endif
3002 }
3003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 /* Only change window focus when not clicking on or dragging the
3005 * status line. Do change focus when releasing the mouse button
3006 * (MOUSE_FOCUS was set above if we dragged first). */
3007 if (dragwin == NULL || (flags & MOUSE_RELEASED))
3008 win_enter(wp, TRUE); /* can make wp invalid! */
Bram Moolenaarc48369c2018-03-11 19:30:45 +01003009
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 if (curwin != old_curwin)
Bram Moolenaarc48369c2018-03-11 19:30:45 +01003011 {
3012#ifdef CHECK_DOUBLE_CLICK
3013 /* set topline, to be able to check for double click ourselves */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 set_mouse_topline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015#endif
Bram Moolenaarc48369c2018-03-11 19:30:45 +01003016#ifdef FEAT_TERMINAL
3017 /* when entering a terminal window may change state */
3018 term_win_entered();
3019#endif
3020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 if (on_status_line) /* In (or below) status line */
3022 {
3023 /* Don't use start_arrow() if we're in the same window */
3024 if (curwin == old_curwin)
3025 return IN_STATUS_LINE;
3026 else
3027 return IN_STATUS_LINE | CURSOR_MOVED;
3028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 if (on_sep_line) /* In (or below) status line */
3030 {
3031 /* Don't use start_arrow() if we're in the same window */
3032 if (curwin == old_curwin)
3033 return IN_SEP_LINE;
3034 else
3035 return IN_SEP_LINE | CURSOR_MOVED;
3036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037
3038 curwin->w_cursor.lnum = curwin->w_topline;
3039#ifdef FEAT_GUI
3040 /* remember topline, needed for double click */
3041 gui_prev_topline = curwin->w_topline;
3042# ifdef FEAT_DIFF
3043 gui_prev_topfill = curwin->w_topfill;
3044# endif
3045#endif
3046 }
3047 else if (on_status_line && which_button == MOUSE_LEFT)
3048 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 if (dragwin != NULL)
3050 {
3051 /* Drag the status line */
3052 count = row - dragwin->w_winrow - dragwin->w_height + 1
3053 - on_status_line;
3054 win_drag_status_line(dragwin, count);
3055 did_drag |= count;
3056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 return IN_STATUS_LINE; /* Cursor didn't move */
3058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 else if (on_sep_line && which_button == MOUSE_LEFT)
3060 {
3061 if (dragwin != NULL)
3062 {
3063 /* Drag the separator column */
3064 count = col - dragwin->w_wincol - dragwin->w_width + 1
3065 - on_sep_line;
3066 win_drag_vsep_line(dragwin, count);
3067 did_drag |= count;
3068 }
3069 return IN_SEP_LINE; /* Cursor didn't move */
3070 }
Bram Moolenaareb163d72017-09-23 15:08:17 +02003071#ifdef FEAT_MENU
3072 else if (in_winbar)
3073 {
3074 /* After a click on the window toolbar don't start Visual mode. */
3075 return IN_OTHER_WIN | MOUSE_WINBAR;
3076 }
3077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 else /* keep_window_focus must be TRUE */
3079 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 /* before moving the cursor for a left click, stop Visual mode */
3081 if (flags & MOUSE_MAY_STOP_VIS)
3082 {
3083 end_visual_mode();
3084 redraw_curbuf_later(INVERTED); /* delete the inversion */
3085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086
3087#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
3088 /* Continue a modeless selection in another window. */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003089 if (cmdwin_type != 0 && row < curwin->w_winrow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 return IN_OTHER_WIN;
3091#endif
3092
3093 row -= W_WINROW(curwin);
Bram Moolenaar53f81742017-09-22 14:35:51 +02003094 col -= curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095
3096 /*
3097 * When clicking beyond the end of the window, scroll the screen.
3098 * Scroll by however many rows outside the window we are.
3099 */
3100 if (row < 0)
3101 {
3102 count = 0;
3103 for (first = TRUE; curwin->w_topline > 1; )
3104 {
3105#ifdef FEAT_DIFF
3106 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
3107 ++count;
3108 else
3109#endif
3110 count += plines(curwin->w_topline - 1);
3111 if (!first && count > -row)
3112 break;
3113 first = FALSE;
3114#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02003115 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116#endif
3117#ifdef FEAT_DIFF
3118 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
3119 ++curwin->w_topfill;
3120 else
3121#endif
3122 {
3123 --curwin->w_topline;
3124#ifdef FEAT_DIFF
3125 curwin->w_topfill = 0;
3126#endif
3127 }
3128 }
3129#ifdef FEAT_DIFF
3130 check_topfill(curwin, FALSE);
3131#endif
3132 curwin->w_valid &=
3133 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3134 redraw_later(VALID);
3135 row = 0;
3136 }
3137 else if (row >= curwin->w_height)
3138 {
3139 count = 0;
3140 for (first = TRUE; curwin->w_topline < curbuf->b_ml.ml_line_count; )
3141 {
3142#ifdef FEAT_DIFF
3143 if (curwin->w_topfill > 0)
3144 ++count;
3145 else
3146#endif
3147 count += plines(curwin->w_topline);
3148 if (!first && count > row - curwin->w_height + 1)
3149 break;
3150 first = FALSE;
3151#ifdef FEAT_FOLDING
3152 if (hasFolding(curwin->w_topline, NULL, &curwin->w_topline)
3153 && curwin->w_topline == curbuf->b_ml.ml_line_count)
3154 break;
3155#endif
3156#ifdef FEAT_DIFF
3157 if (curwin->w_topfill > 0)
3158 --curwin->w_topfill;
3159 else
3160#endif
3161 {
3162 ++curwin->w_topline;
3163#ifdef FEAT_DIFF
3164 curwin->w_topfill =
3165 diff_check_fill(curwin, curwin->w_topline);
3166#endif
3167 }
3168 }
3169#ifdef FEAT_DIFF
3170 check_topfill(curwin, FALSE);
3171#endif
3172 redraw_later(VALID);
3173 curwin->w_valid &=
3174 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3175 row = curwin->w_height - 1;
3176 }
3177 else if (row == 0)
3178 {
3179 /* When dragging the mouse, while the text has been scrolled up as
3180 * far as it goes, moving the mouse in the top line should scroll
3181 * the text down (done later when recomputing w_topline). */
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003182 if (mouse_dragging > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 && curwin->w_cursor.lnum
3184 == curwin->w_buffer->b_ml.ml_line_count
3185 && curwin->w_cursor.lnum == curwin->w_topline)
3186 curwin->w_valid &= ~(VALID_TOPLINE);
3187 }
3188 }
3189
3190#ifdef FEAT_FOLDING
3191 /* Check for position outside of the fold column. */
3192 if (
3193# ifdef FEAT_RIGHTLEFT
Bram Moolenaar02631462017-09-22 15:20:32 +02003194 curwin->w_p_rl ? col < curwin->w_width - curwin->w_p_fdc :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195# endif
3196 col >= curwin->w_p_fdc
3197# ifdef FEAT_CMDWIN
3198 + (cmdwin_type == 0 ? 0 : 1)
3199# endif
3200 )
3201 mouse_char = ' ';
3202#endif
3203
3204 /* compute the position in the buffer line from the posn on the screen */
3205 if (mouse_comp_pos(curwin, &row, &col, &curwin->w_cursor.lnum))
3206 mouse_past_bottom = TRUE;
3207
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 /* Start Visual mode before coladvance(), for when 'sel' != "old" */
3209 if ((flags & MOUSE_MAY_VIS) && !VIsual_active)
3210 {
3211 check_visual_highlight();
3212 VIsual = old_cursor;
3213 VIsual_active = TRUE;
3214 VIsual_reselect = TRUE;
3215 /* if 'selectmode' contains "mouse", start Select mode */
3216 may_start_select('o');
3217 setmouse();
Bram Moolenaar7df351e2006-01-23 22:30:28 +00003218 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 redraw_cmdline = TRUE; /* show visual mode later */
3220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221
3222 curwin->w_curswant = col;
3223 curwin->w_set_curswant = FALSE; /* May still have been TRUE */
3224 if (coladvance(col) == FAIL) /* Mouse click beyond end of line */
3225 {
3226 if (inclusive != NULL)
3227 *inclusive = TRUE;
3228 mouse_past_eol = TRUE;
3229 }
3230 else if (inclusive != NULL)
3231 *inclusive = FALSE;
3232
3233 count = IN_BUFFER;
3234 if (curwin != old_curwin || curwin->w_cursor.lnum != old_cursor.lnum
3235 || curwin->w_cursor.col != old_cursor.col)
3236 count |= CURSOR_MOVED; /* Cursor has moved */
3237
3238#ifdef FEAT_FOLDING
3239 if (mouse_char == '+')
3240 count |= MOUSE_FOLD_OPEN;
3241 else if (mouse_char != ' ')
3242 count |= MOUSE_FOLD_CLOSE;
3243#endif
3244
3245 return count;
3246}
3247
3248/*
3249 * Compute the position in the buffer line from the posn on the screen in
3250 * window "win".
3251 * Returns TRUE if the position is below the last line.
3252 */
3253 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003254mouse_comp_pos(
3255 win_T *win,
3256 int *rowp,
3257 int *colp,
3258 linenr_T *lnump)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259{
3260 int col = *colp;
3261 int row = *rowp;
3262 linenr_T lnum;
3263 int retval = FALSE;
3264 int off;
3265 int count;
3266
3267#ifdef FEAT_RIGHTLEFT
3268 if (win->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02003269 col = win->w_width - 1 - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270#endif
3271
3272 lnum = win->w_topline;
3273
3274 while (row > 0)
3275 {
3276#ifdef FEAT_DIFF
3277 /* Don't include filler lines in "count" */
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003278 if (win->w_p_diff
3279# ifdef FEAT_FOLDING
3280 && !hasFoldingWin(win, lnum, NULL, NULL, TRUE, NULL)
3281# endif
3282 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 {
3284 if (lnum == win->w_topline)
3285 row -= win->w_topfill;
3286 else
3287 row -= diff_check_fill(win, lnum);
3288 count = plines_win_nofill(win, lnum, TRUE);
3289 }
3290 else
3291#endif
3292 count = plines_win(win, lnum, TRUE);
3293 if (count > row)
3294 break; /* Position is in this buffer line. */
3295#ifdef FEAT_FOLDING
3296 (void)hasFoldingWin(win, lnum, NULL, &lnum, TRUE, NULL);
3297#endif
3298 if (lnum == win->w_buffer->b_ml.ml_line_count)
3299 {
3300 retval = TRUE;
3301 break; /* past end of file */
3302 }
3303 row -= count;
3304 ++lnum;
3305 }
3306
3307 if (!retval)
3308 {
3309 /* Compute the column without wrapping. */
3310 off = win_col_off(win) - win_col_off2(win);
3311 if (col < off)
3312 col = off;
Bram Moolenaar02631462017-09-22 15:20:32 +02003313 col += row * (win->w_width - off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 /* add skip column (for long wrapping line) */
3315 col += win->w_skipcol;
3316 }
3317
3318 if (!win->w_p_wrap)
3319 col += win->w_leftcol;
3320
3321 /* skip line number and fold column in front of the line */
3322 col -= win_col_off(win);
3323 if (col < 0)
3324 {
3325#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003326 netbeans_gutter_click(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327#endif
3328 col = 0;
3329 }
3330
3331 *colp = col;
3332 *rowp = row;
3333 *lnump = lnum;
3334 return retval;
3335}
3336
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337/*
3338 * Find the window at screen position "*rowp" and "*colp". The positions are
3339 * updated to become relative to the top-left of the window.
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003340 * Returns NULL when something is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 win_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003343mouse_find_win(int *rowp, int *colp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344{
3345 frame_T *fp;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003346 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347
3348 fp = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003349 *rowp -= firstwin->w_winrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 for (;;)
3351 {
3352 if (fp->fr_layout == FR_LEAF)
3353 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 if (fp->fr_layout == FR_ROW)
3355 {
3356 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
3357 {
3358 if (*colp < fp->fr_width)
3359 break;
3360 *colp -= fp->fr_width;
3361 }
3362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 else /* fr_layout == FR_COL */
3364 {
3365 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
3366 {
3367 if (*rowp < fp->fr_height)
3368 break;
3369 *rowp -= fp->fr_height;
3370 }
3371 }
3372 }
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003373 /* When using a timer that closes a window the window might not actually
3374 * exist. */
3375 FOR_ALL_WINDOWS(wp)
3376 if (wp == fp->fr_win)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003377 {
3378#ifdef FEAT_MENU
3379 *rowp -= wp->w_winbar_height;
3380#endif
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003381 return wp;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003382 }
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003383 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385
Bram Moolenaar860cae12010-06-05 23:22:07 +02003386#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar658a1542018-03-03 19:29:43 +01003388 || defined(FEAT_GUI_PHOTON) || defined(FEAT_TERM_POPUP_MENU) \
3389 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390/*
3391 * Translate window coordinates to buffer position without any side effects
3392 */
3393 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003394get_fpos_of_mouse(pos_T *mpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395{
3396 win_T *wp;
3397 int row = mouse_row;
3398 int col = mouse_col;
3399
3400 if (row < 0 || col < 0) /* check if it makes sense */
3401 return IN_UNKNOWN;
3402
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 /* find the window where the row is in */
3404 wp = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003405 if (wp == NULL)
3406 return IN_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 /*
3408 * winpos and height may change in win_enter()!
3409 */
3410 if (row >= wp->w_height) /* In (or below) status line */
3411 return IN_STATUS_LINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 if (col >= wp->w_width) /* In vertical separator line */
3413 return IN_SEP_LINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414
3415 if (wp != curwin)
3416 return IN_UNKNOWN;
3417
3418 /* compute the position in the buffer line from the posn on the screen */
3419 if (mouse_comp_pos(curwin, &row, &col, &mpos->lnum))
3420 return IN_STATUS_LINE; /* past bottom */
3421
3422 mpos->col = vcol2col(wp, mpos->lnum, col);
3423
3424 if (mpos->col > 0)
3425 --mpos->col;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003426 mpos->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 return IN_BUFFER;
3428}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003431#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MAC) \
3432 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar3767b612018-03-03 19:51:58 +01003433 || defined(FEAT_GUI_PHOTON) || defined(FEAT_BEVAL) \
3434 || defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435/*
3436 * Convert a virtual (screen) column to a character column.
3437 * The first column is one.
3438 */
3439 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003440vcol2col(win_T *wp, linenr_T lnum, int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441{
3442 /* try to advance to the specified column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 int count = 0;
3444 char_u *ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003445 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
Bram Moolenaar597a4222014-06-25 14:39:50 +02003447 line = ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaarb6101cf2012-10-21 00:58:39 +02003448 while (count < vcol && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003450 count += win_lbr_chartabsize(wp, line, ptr, count, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003451 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02003453 return (int)(ptr - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454}
3455#endif
3456
3457#endif /* FEAT_MOUSE */
3458
Bram Moolenaar4f974752019-02-17 17:44:42 +01003459#if defined(FEAT_GUI) || defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460/*
3461 * Called when focus changed. Used for the GUI or for systems where this can
3462 * be done in the console (Win32).
3463 */
3464 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003465ui_focus_change(
3466 int in_focus) /* TRUE if focus gained. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467{
3468 static time_t last_time = (time_t)0;
3469 int need_redraw = FALSE;
3470
3471 /* When activated: Check if any file was modified outside of Vim.
3472 * Only do this when not done within the last two seconds (could get
3473 * several events in a row). */
3474 if (in_focus && last_time + 2 < time(NULL))
3475 {
3476 need_redraw = check_timestamps(
3477# ifdef FEAT_GUI
3478 gui.in_use
3479# else
3480 FALSE
3481# endif
3482 );
3483 last_time = time(NULL);
3484 }
3485
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 /*
3487 * Fire the focus gained/lost autocommand.
3488 */
3489 need_redraw |= apply_autocmds(in_focus ? EVENT_FOCUSGAINED
3490 : EVENT_FOCUSLOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491
3492 if (need_redraw)
3493 {
3494 /* Something was executed, make sure the cursor is put back where it
3495 * belongs. */
3496 need_wait_return = FALSE;
3497
3498 if (State & CMDLINE)
3499 redrawcmdline();
3500 else if (State == HITRETURN || State == SETWSIZE || State == ASKMORE
3501 || State == EXTERNCMD || State == CONFIRM || exmode_active)
3502 repeat_message();
3503 else if ((State & NORMAL) || (State & INSERT))
3504 {
3505 if (must_redraw != 0)
3506 update_screen(0);
3507 setcursor();
3508 }
3509 cursor_on(); /* redrawing may have switched it off */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003510 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511# ifdef FEAT_GUI
3512 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 gui_update_scrollbars(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514# endif
3515 }
3516#ifdef FEAT_TITLE
3517 /* File may have been changed from 'readonly' to 'noreadonly' */
3518 if (need_maketitle)
3519 maketitle();
3520#endif
3521}
3522#endif
3523
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003524#if defined(HAVE_INPUT_METHOD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525/*
3526 * Save current Input Method status to specified place.
3527 */
3528 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003529im_save_status(long *psave)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530{
3531 /* Don't save when 'imdisable' is set or "xic" is NULL, IM is always
3532 * disabled then (but might start later).
3533 * Also don't save when inside a mapping, vgetc_im_active has not been set
3534 * then.
3535 * And don't save when the keys were stuffed (e.g., for a "." command).
3536 * And don't save when the GUI is running but our window doesn't have
3537 * input focus (e.g., when a find dialog is open). */
3538 if (!p_imdisable && KeyTyped && !KeyStuffed
3539# ifdef FEAT_XIM
3540 && xic != NULL
3541# endif
3542# ifdef FEAT_GUI
3543 && (!gui.in_use || gui.in_focus)
3544# endif
3545 )
3546 {
3547 /* Do save when IM is on, or IM is off and saved status is on. */
3548 if (vgetc_im_active)
3549 *psave = B_IMODE_IM;
3550 else if (*psave == B_IMODE_IM)
3551 *psave = B_IMODE_NONE;
3552 }
3553}
3554#endif