blob: 446cefcded8f0f753473acf87c8c026cc3e61c8d [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 Moolenaar28e67e02019-08-15 23:05:49 +0200462# if defined(FEAT_JOB_CHANNEL) || defined(FEAT_SOUND_CANBERRA)
463 if ((due_time < 0 || due_time > 10L) && (
464# if defined(FEAT_JOB_CHANNEL)
465 (
466# if defined(FEAT_GUI)
467 !gui.in_use &&
468# endif
469 (has_pending_job() || channel_any_readahead()))
470# ifdef FEAT_SOUND_CANBERRA
471 ||
472# endif
Bram Moolenaarc46af532019-01-09 22:24:49 +0100473# endif
Bram Moolenaar28e67e02019-08-15 23:05:49 +0200474# ifdef FEAT_SOUND_CANBERRA
475 has_any_sound_callback()
476# endif
477 ))
Bram Moolenaarc46af532019-01-09 22:24:49 +0100478 {
479 // There is a pending job or channel, should return soon in order
480 // to handle them ASAP. Do check for input briefly.
481 due_time = 10L;
482 brief_wait = TRUE;
483 }
484# endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100485 if (wait_func(due_time, interrupted, ignore_input))
486 return OK;
Bram Moolenaarc46af532019-01-09 22:24:49 +0100487 if ((interrupted != NULL && *interrupted)
488# ifdef FEAT_JOB_CHANNEL
489 || brief_wait
490# endif
491 )
492 // Nothing available, but need to return so that side effects get
493 // handled, such as handling a message on a channel.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100494 return FAIL;
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100495 if (wtime > 0)
496 remaining -= due_time;
497 }
498 return FAIL;
499}
500#endif
501
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502/*
Bram Moolenaarc46af532019-01-09 22:24:49 +0100503 * Return non-zero if a character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 */
505 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100506ui_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507{
508#ifdef FEAT_GUI
509 if (gui.in_use)
510 {
511 gui_mch_update();
512 return input_available();
513 }
514#endif
515#ifndef NO_CONSOLE
516# ifdef NO_CONSOLE_INPUT
517 if (no_console_input())
518 return 0;
519# endif
520 return mch_char_avail();
521#else
522 return 0;
523#endif
524}
525
526/*
527 * Delay for the given number of milliseconds. If ignoreinput is FALSE then we
528 * cancel the delay if a key is hit.
529 */
530 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100531ui_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532{
533#ifdef FEAT_GUI
534 if (gui.in_use && !ignoreinput)
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100535 gui_wait_for_chars(msec, typebuf.tb_change_cnt);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 else
537#endif
538 mch_delay(msec, ignoreinput);
539}
540
541/*
542 * If the machine has job control, use it to suspend the program,
543 * otherwise fake it by starting a new shell.
544 * When running the GUI iconify the window.
545 */
546 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100547ui_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548{
549#ifdef FEAT_GUI
550 if (gui.in_use)
551 {
552 gui_mch_iconify();
553 return;
554 }
555#endif
556 mch_suspend();
557}
558
559#if !defined(UNIX) || !defined(SIGTSTP) || defined(PROTO) || defined(__BEOS__)
560/*
561 * When the OS can't really suspend, call this function to start a shell.
562 * This is never called in the GUI.
563 */
564 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100565suspend_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566{
567 if (*p_sh == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100568 emsg(_(e_shellempty));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 else
570 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100571 msg_puts(_("new shell started\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 do_shell(NULL, 0);
573 }
574}
575#endif
576
577/*
578 * Try to get the current Vim shell size. Put the result in Rows and Columns.
579 * Use the new sizes as defaults for 'columns' and 'lines'.
580 * Return OK when size could be determined, FAIL otherwise.
581 */
582 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100583ui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584{
585 int retval;
586
587#ifdef FEAT_GUI
588 if (gui.in_use)
589 retval = gui_get_shellsize();
590 else
591#endif
592 retval = mch_get_shellsize();
593
594 check_shellsize();
595
596 /* adjust the default for 'lines' and 'columns' */
597 if (retval == OK)
598 {
599 set_number_default("lines", Rows);
600 set_number_default("columns", Columns);
601 }
602 return retval;
603}
604
605/*
606 * Set the size of the Vim shell according to Rows and Columns, if possible.
607 * The gui_set_shellsize() or mch_set_shellsize() function will try to set the
608 * new size. If this is not possible, it will adjust Rows and Columns.
609 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100611ui_set_shellsize(
612 int mustset UNUSED) /* set by the user */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613{
614#ifdef FEAT_GUI
615 if (gui.in_use)
Bram Moolenaar8968a312013-07-03 16:58:44 +0200616 gui_set_shellsize(mustset, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 else
618#endif
619 mch_set_shellsize();
620}
621
622/*
623 * Called when Rows and/or Columns changed. Adjust scroll region and mouse
624 * region.
625 */
626 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100627ui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628{
629 if (full_screen && !exiting)
630 {
631#ifdef FEAT_GUI
632 if (gui.in_use)
633 gui_new_shellsize();
634 else
635#endif
636 mch_new_shellsize();
637 }
638}
639
Bram Moolenaar6bc93052019-04-06 20:00:19 +0200640#if ((defined(FEAT_EVAL) || defined(FEAT_TERMINAL)) \
Bram Moolenaar3d3f2172019-04-06 17:56:05 +0200641 && (defined(FEAT_GUI) \
Bram Moolenaar16c34c32019-04-06 22:01:24 +0200642 || defined(MSWIN) \
Bram Moolenaar3d3f2172019-04-06 17:56:05 +0200643 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)))) \
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +0200644 || defined(PROTO)
645/*
646 * Get the window position in pixels, if possible.
647 * Return FAIL when not possible.
648 */
649 int
650ui_get_winpos(int *x, int *y, varnumber_T timeout)
651{
652# ifdef FEAT_GUI
653 if (gui.in_use)
654 return gui_mch_get_winpos(x, y);
655# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200656# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar16c34c32019-04-06 22:01:24 +0200657 return mch_get_winpos(x, y);
Bram Moolenaar6bc93052019-04-06 20:00:19 +0200658# else
Bram Moolenaar16c34c32019-04-06 22:01:24 +0200659# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
660 return term_get_winpos(x, y, timeout);
661# else
Bram Moolenaar6bc93052019-04-06 20:00:19 +0200662 return FAIL;
Bram Moolenaar16c34c32019-04-06 22:01:24 +0200663# endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +0200664# endif
665}
666#endif
667
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100669ui_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670{
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200671 ui_breakcheck_force(FALSE);
672}
673
674/*
675 * When "force" is true also check when the terminal is not in raw mode.
676 * This is useful to read input on channels.
677 */
678 void
679ui_breakcheck_force(int force)
680{
Bram Moolenaar48d23bb2018-11-20 02:42:43 +0100681 static int recursive = FALSE;
682 int save_updating_screen = updating_screen;
Bram Moolenaare3caa112017-01-31 22:07:42 +0100683
Bram Moolenaar48d23bb2018-11-20 02:42:43 +0100684 // We could be called recursively if stderr is redirected, calling
685 // fill_input_buf() calls settmode() when stdin isn't a tty. settmode()
686 // calls vgetorpeek() which calls ui_breakcheck() again.
687 if (recursive)
688 return;
689 recursive = TRUE;
690
691 // We do not want gui_resize_shell() to redraw the screen here.
Bram Moolenaare3caa112017-01-31 22:07:42 +0100692 ++updating_screen;
693
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694#ifdef FEAT_GUI
695 if (gui.in_use)
696 gui_mch_update();
697 else
698#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200699 mch_breakcheck(force);
Bram Moolenaare3caa112017-01-31 22:07:42 +0100700
Bram Moolenaar42335f52018-09-13 15:33:43 +0200701 if (save_updating_screen)
702 updating_screen = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200703 else
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200704 after_updating_screen(FALSE);
Bram Moolenaar48d23bb2018-11-20 02:42:43 +0100705
706 recursive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707}
708
709/*****************************************************************************
710 * Functions for copying and pasting text between applications.
711 * This is always included in a GUI version, but may also be included when the
712 * clipboard and mouse is available to a terminal version such as xterm.
713 * Note: there are some more functions in ops.c that handle selection stuff.
714 *
715 * Also note that the majority of functions here deal with the X 'primary'
716 * (visible - for Visual mode use) selection, and only that. There are no
717 * versions of these for the 'clipboard' selection, as Visual mode has no use
718 * for them.
719 */
720
721#if defined(FEAT_CLIPBOARD) || defined(PROTO)
722
723/*
724 * Selection stuff using Visual mode, for cutting and pasting text to other
725 * windows.
726 */
727
728/*
729 * Call this to initialise the clipboard. Pass it FALSE if the clipboard code
730 * is included, but the clipboard can not be used, or TRUE if the clipboard can
731 * be used. Eg unix may call this with FALSE, then call it again with TRUE if
732 * the GUI starts.
733 */
734 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100735clip_init(int can_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736{
Bram Moolenaar0554fa42019-06-14 21:36:54 +0200737 Clipboard_T *cb;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738
739 cb = &clip_star;
740 for (;;)
741 {
742 cb->available = can_use;
743 cb->owned = FALSE;
744 cb->start.lnum = 0;
745 cb->start.col = 0;
746 cb->end.lnum = 0;
747 cb->end.col = 0;
748 cb->state = SELECT_CLEARED;
749
750 if (cb == &clip_plus)
751 break;
752 cb = &clip_plus;
753 }
754}
755
756/*
757 * Check whether the VIsual area has changed, and if so try to become the owner
758 * of the selection, and free any old converted selection we may still have
759 * lying around. If the VIsual mode has ended, make a copy of what was
760 * selected so we can still give it to others. Will probably have to make sure
761 * this is called whenever VIsual mode is ended.
762 */
763 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +0200764clip_update_selection(Clipboard_T *clip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765{
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200766 pos_T start, end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767
768 /* If visual mode is only due to a redo command ("."), then ignore it */
769 if (!redo_VIsual_busy && VIsual_active && (State & NORMAL))
770 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100771 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 {
773 start = VIsual;
774 end = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000776 end.col += (*mb_ptr2len)(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 }
778 else
779 {
780 start = curwin->w_cursor;
781 end = VIsual;
782 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100783 if (!EQUAL_POS(clip->start, start)
784 || !EQUAL_POS(clip->end, end)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200785 || clip->vmode != VIsual_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200787 clip_clear_selection(clip);
788 clip->start = start;
789 clip->end = end;
790 clip->vmode = VIsual_mode;
791 clip_free_selection(clip);
792 clip_own_selection(clip);
793 clip_gen_set_selection(clip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 }
795 }
796}
797
798 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +0200799clip_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800{
801 /*
802 * Also want to check somehow that we are reading from the keyboard rather
803 * than a mapping etc.
804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805#ifdef FEAT_X11
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200806 /* Always own the selection, we might have lost it without being
Bram Moolenaar62b42182010-09-21 22:09:37 +0200807 * notified, e.g. during a ":sh" command. */
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200808 if (cbd->available)
809 {
810 int was_owned = cbd->owned;
811
812 cbd->owned = (clip_gen_own_selection(cbd) == OK);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200813 if (!was_owned && (cbd == &clip_star || cbd == &clip_plus))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 {
Bram Moolenaarebefac62005-12-28 22:39:57 +0000815 /* May have to show a different kind of highlighting for the
816 * selected area. There is no specific redraw command for this,
817 * just redraw all windows on the current buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 if (cbd->owned
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000819 && (get_real_state() == VISUAL
820 || get_real_state() == SELECTMODE)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200821 && (cbd == &clip_star ? clip_isautosel_star()
822 : clip_isautosel_plus())
Bram Moolenaar8820b482017-03-16 17:23:31 +0100823 && HL_ATTR(HLF_V) != HL_ATTR(HLF_VNC))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 redraw_curbuf_later(INVERTED_ALL);
825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200827#else
Bram Moolenaarb6101cf2012-10-21 00:58:39 +0200828 /* Only own the clipboard when we didn't own it yet. */
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200829 if (!cbd->owned && cbd->available)
830 cbd->owned = (clip_gen_own_selection(cbd) == OK);
831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832}
833
834 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +0200835clip_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836{
837#ifdef FEAT_X11
838 int was_owned = cbd->owned;
839#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200840 int visual_selection = FALSE;
841
842 if (cbd == &clip_star || cbd == &clip_plus)
843 visual_selection = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844
845 clip_free_selection(cbd);
846 cbd->owned = FALSE;
847 if (visual_selection)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200848 clip_clear_selection(cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 clip_gen_lose_selection(cbd);
850#ifdef FEAT_X11
851 if (visual_selection)
852 {
853 /* May have to show a different kind of highlighting for the selected
854 * area. There is no specific redraw command for this, just redraw all
855 * windows on the current buffer. */
856 if (was_owned
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000857 && (get_real_state() == VISUAL
858 || get_real_state() == SELECTMODE)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200859 && (cbd == &clip_star ?
860 clip_isautosel_star() : clip_isautosel_plus())
Bram Moolenaar8820b482017-03-16 17:23:31 +0100861 && HL_ATTR(HLF_V) != HL_ATTR(HLF_VNC))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 {
863 update_curbuf(INVERTED_ALL);
864 setcursor();
865 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +0100866 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 }
868 }
869#endif
870}
871
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200872 static void
Bram Moolenaar0554fa42019-06-14 21:36:54 +0200873clip_copy_selection(Clipboard_T *clip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874{
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200875 if (VIsual_active && (State & NORMAL) && clip->available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200877 clip_update_selection(clip);
878 clip_free_selection(clip);
879 clip_own_selection(clip);
880 if (clip->owned)
881 clip_get_selection(clip);
882 clip_gen_set_selection(clip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 }
884}
885
886/*
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200887 * Save and restore clip_unnamed before doing possibly many changes. This
888 * prevents accessing the clipboard very often which might slow down Vim
889 * considerably.
890 */
Bram Moolenaar73a156b2015-01-27 21:39:05 +0100891static int global_change_count = 0; /* if set, inside a start_global_changes */
Bram Moolenaar3fcfa352017-03-29 19:20:41 +0200892static int clipboard_needs_update = FALSE; /* clipboard needs to be updated */
893static int clip_did_set_selection = TRUE;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200894
895/*
896 * Save clip_unnamed and reset it.
897 */
898 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100899start_global_changes(void)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200900{
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100901 if (++global_change_count > 1)
902 return;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200903 clip_unnamed_saved = clip_unnamed;
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100904 clipboard_needs_update = FALSE;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200905
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100906 if (clip_did_set_selection)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200907 {
908 clip_unnamed = FALSE;
909 clip_did_set_selection = FALSE;
910 }
911}
912
913/*
Bram Moolenaar3fcfa352017-03-29 19:20:41 +0200914 * Return TRUE if setting the clipboard was postponed, it already contains the
915 * right text.
916 */
917 int
918is_clipboard_needs_update()
919{
920 return clipboard_needs_update;
921}
922
923/*
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200924 * Restore clip_unnamed and set the selection when needed.
925 */
926 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100927end_global_changes(void)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200928{
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100929 if (--global_change_count > 0)
930 /* recursive */
931 return;
932 if (!clip_did_set_selection)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200933 {
934 clip_did_set_selection = TRUE;
935 clip_unnamed = clip_unnamed_saved;
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100936 clip_unnamed_saved = FALSE;
937 if (clipboard_needs_update)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200938 {
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100939 /* only store something in the clipboard,
940 * if we have yanked anything to it */
941 if (clip_unnamed & CLIP_UNNAMED)
942 {
943 clip_own_selection(&clip_star);
944 clip_gen_set_selection(&clip_star);
945 }
946 if (clip_unnamed & CLIP_UNNAMED_PLUS)
947 {
948 clip_own_selection(&clip_plus);
949 clip_gen_set_selection(&clip_plus);
950 }
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200951 }
952 }
Bram Moolenaar3fcfa352017-03-29 19:20:41 +0200953 clipboard_needs_update = FALSE;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200954}
955
956/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 * Called when Visual mode is ended: update the selection.
958 */
959 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100960clip_auto_select(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961{
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200962 if (clip_isautosel_star())
963 clip_copy_selection(&clip_star);
964 if (clip_isautosel_plus())
965 clip_copy_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966}
967
968/*
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200969 * Return TRUE if automatic selection of Visual area is desired for the *
970 * register.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 */
972 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100973clip_isautosel_star(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974{
975 return (
976#ifdef FEAT_GUI
977 gui.in_use ? (vim_strchr(p_go, GO_ASEL) != NULL) :
978#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200979 clip_autoselect_star);
980}
981
982/*
983 * Return TRUE if automatic selection of Visual area is desired for the +
984 * register.
985 */
986 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100987clip_isautosel_plus(void)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200988{
989 return (
990#ifdef FEAT_GUI
991 gui.in_use ? (vim_strchr(p_go, GO_ASELPLUS) != NULL) :
992#endif
993 clip_autoselect_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994}
995
996
997/*
998 * Stuff for general mouse selection, without using Visual mode.
999 */
1000
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001001static void clip_invert_area(Clipboard_T *, int, int, int, int, int how);
1002static void clip_invert_rectangle(Clipboard_T *, int row, int col, int height, int width, int invert);
Bram Moolenaar0554fa42019-06-14 21:36:54 +02001003static void clip_get_word_boundaries(Clipboard_T *, int, int);
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001004static int clip_get_line_end(Clipboard_T *, int);
Bram Moolenaar0554fa42019-06-14 21:36:54 +02001005static void clip_update_modeless_selection(Clipboard_T *, int, int, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001007// "how" flags for clip_invert_area()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008#define CLIP_CLEAR 1
1009#define CLIP_SET 2
1010#define CLIP_TOGGLE 3
1011
1012/*
1013 * Start, continue or end a modeless selection. Used when editing the
Bram Moolenaarb53fb312019-06-13 23:59:52 +02001014 * command-line, in the cmdline window and when the mouse is in a popup window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 */
1016 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001017clip_modeless(int button, int is_click, int is_drag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018{
1019 int repeat;
1020
1021 repeat = ((clip_star.mode == SELECT_MODE_CHAR
1022 || clip_star.mode == SELECT_MODE_LINE)
1023 && (mod_mask & MOD_MASK_2CLICK))
1024 || (clip_star.mode == SELECT_MODE_WORD
1025 && (mod_mask & MOD_MASK_3CLICK));
1026 if (is_click && button == MOUSE_RIGHT)
1027 {
1028 /* Right mouse button: If there was no selection, start one.
1029 * Otherwise extend the existing selection. */
1030 if (clip_star.state == SELECT_CLEARED)
1031 clip_start_selection(mouse_col, mouse_row, FALSE);
1032 clip_process_selection(button, mouse_col, mouse_row, repeat);
1033 }
1034 else if (is_click)
1035 clip_start_selection(mouse_col, mouse_row, repeat);
1036 else if (is_drag)
1037 {
1038 /* Don't try extending a selection if there isn't one. Happens when
1039 * button-down is in the cmdline and them moving mouse upwards. */
1040 if (clip_star.state != SELECT_CLEARED)
1041 clip_process_selection(button, mouse_col, mouse_row, repeat);
1042 }
1043 else /* release */
1044 clip_process_selection(MOUSE_RELEASE, mouse_col, mouse_row, FALSE);
1045}
1046
1047/*
1048 * Compare two screen positions ala strcmp()
1049 */
1050 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001051clip_compare_pos(
1052 int row1,
1053 int col1,
1054 int row2,
1055 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056{
1057 if (row1 > row2) return(1);
1058 if (row1 < row2) return(-1);
1059 if (col1 > col2) return(1);
1060 if (col1 < col2) return(-1);
Bram Moolenaar9e341102016-02-23 23:04:36 +01001061 return(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062}
1063
1064/*
1065 * Start the selection
1066 */
1067 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001068clip_start_selection(int col, int row, int repeated_click)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069{
Bram Moolenaar0554fa42019-06-14 21:36:54 +02001070 Clipboard_T *cb = &clip_star;
Bram Moolenaar13b11ed2019-08-01 15:52:45 +02001071#ifdef FEAT_TEXT_PROP
1072 win_T *wp;
1073 int row_cp = row;
1074 int col_cp = col;
1075
1076 wp = mouse_find_win(&row_cp, &col_cp, FIND_POPUP);
1077 if (wp != NULL && WIN_IS_POPUP(wp)
1078 && popup_is_in_scrollbar(wp, row_cp, col_cp))
1079 // click or double click in scrollbar does not start a selection
1080 return;
1081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082
1083 if (cb->state == SELECT_DONE)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001084 clip_clear_selection(cb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085
1086 row = check_row(row);
1087 col = check_col(col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 col = mb_fix_col(col, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089
1090 cb->start.lnum = row;
1091 cb->start.col = col;
1092 cb->end = cb->start;
1093 cb->origin_row = (short_u)cb->start.lnum;
1094 cb->state = SELECT_IN_PROGRESS;
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001095#ifdef FEAT_TEXT_PROP
Bram Moolenaar13b11ed2019-08-01 15:52:45 +02001096 if (wp != NULL && WIN_IS_POPUP(wp))
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001097 {
Bram Moolenaar13b11ed2019-08-01 15:52:45 +02001098 // Click in a popup window restricts selection to that window,
1099 // excluding the border.
1100 cb->min_col = wp->w_wincol + wp->w_popup_border[3];
Bram Moolenaar4dd751b2019-08-17 19:10:53 +02001101 cb->max_col = wp->w_wincol + popup_width(wp)
1102 - wp->w_popup_border[1] - wp->w_has_scrollbar;
Bram Moolenaarff9f27c2019-08-17 16:15:53 +02001103 if (cb->max_col > screen_Columns)
1104 cb->max_col = screen_Columns;
Bram Moolenaar13b11ed2019-08-01 15:52:45 +02001105 cb->min_row = wp->w_winrow + wp->w_popup_border[0];
1106 cb->max_row = wp->w_winrow + popup_height(wp) - 1
1107 - wp->w_popup_border[2];
1108 }
1109 else
1110 {
1111 cb->min_col = 0;
1112 cb->max_col = screen_Columns;
1113 cb->min_row = 0;
1114 cb->max_row = screen_Rows;
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001115 }
1116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117
1118 if (repeated_click)
1119 {
1120 if (++cb->mode > SELECT_MODE_LINE)
1121 cb->mode = SELECT_MODE_CHAR;
1122 }
1123 else
1124 cb->mode = SELECT_MODE_CHAR;
1125
1126#ifdef FEAT_GUI
1127 /* clear the cursor until the selection is made */
1128 if (gui.in_use)
1129 gui_undraw_cursor();
1130#endif
1131
1132 switch (cb->mode)
1133 {
1134 case SELECT_MODE_CHAR:
1135 cb->origin_start_col = cb->start.col;
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001136 cb->word_end_col = clip_get_line_end(cb, (int)cb->start.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 break;
1138
1139 case SELECT_MODE_WORD:
1140 clip_get_word_boundaries(cb, (int)cb->start.lnum, cb->start.col);
1141 cb->origin_start_col = cb->word_start_col;
1142 cb->origin_end_col = cb->word_end_col;
1143
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001144 clip_invert_area(cb, (int)cb->start.lnum, cb->word_start_col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 (int)cb->end.lnum, cb->word_end_col, CLIP_SET);
1146 cb->start.col = cb->word_start_col;
1147 cb->end.col = cb->word_end_col;
1148 break;
1149
1150 case SELECT_MODE_LINE:
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001151 clip_invert_area(cb, (int)cb->start.lnum, 0, (int)cb->start.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 (int)Columns, CLIP_SET);
1153 cb->start.col = 0;
1154 cb->end.col = Columns;
1155 break;
1156 }
1157
1158 cb->prev = cb->start;
1159
1160#ifdef DEBUG_SELECTION
1161 printf("Selection started at (%u,%u)\n", cb->start.lnum, cb->start.col);
1162#endif
1163}
1164
1165/*
1166 * Continue processing the selection
1167 */
1168 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001169clip_process_selection(
1170 int button,
1171 int col,
1172 int row,
1173 int_u repeated_click)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174{
Bram Moolenaar0554fa42019-06-14 21:36:54 +02001175 Clipboard_T *cb = &clip_star;
1176 int diff;
1177 int slen = 1; // cursor shape width
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178
1179 if (button == MOUSE_RELEASE)
1180 {
1181 /* Check to make sure we have something selected */
1182 if (cb->start.lnum == cb->end.lnum && cb->start.col == cb->end.col)
1183 {
1184#ifdef FEAT_GUI
1185 if (gui.in_use)
1186 gui_update_cursor(FALSE, FALSE);
1187#endif
1188 cb->state = SELECT_CLEARED;
1189 return;
1190 }
1191
1192#ifdef DEBUG_SELECTION
1193 printf("Selection ended: (%u,%u) to (%u,%u)\n", cb->start.lnum,
1194 cb->start.col, cb->end.lnum, cb->end.col);
1195#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001196 if (clip_isautosel_star()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 || (
1198#ifdef FEAT_GUI
1199 gui.in_use ? (vim_strchr(p_go, GO_ASELML) != NULL) :
1200#endif
1201 clip_autoselectml))
1202 clip_copy_modeless_selection(FALSE);
1203#ifdef FEAT_GUI
1204 if (gui.in_use)
1205 gui_update_cursor(FALSE, FALSE);
1206#endif
1207
1208 cb->state = SELECT_DONE;
1209 return;
1210 }
1211
1212 row = check_row(row);
1213 col = check_col(col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 col = mb_fix_col(col, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215
1216 if (col == (int)cb->prev.col && row == cb->prev.lnum && !repeated_click)
1217 return;
1218
1219 /*
1220 * When extending the selection with the right mouse button, swap the
1221 * start and end if the position is before half the selection
1222 */
1223 if (cb->state == SELECT_DONE && button == MOUSE_RIGHT)
1224 {
1225 /*
1226 * If the click is before the start, or the click is inside the
1227 * selection and the start is the closest side, set the origin to the
1228 * end of the selection.
1229 */
1230 if (clip_compare_pos(row, col, (int)cb->start.lnum, cb->start.col) < 0
1231 || (clip_compare_pos(row, col,
1232 (int)cb->end.lnum, cb->end.col) < 0
1233 && (((cb->start.lnum == cb->end.lnum
1234 && cb->end.col - col > col - cb->start.col))
1235 || ((diff = (cb->end.lnum - row) -
1236 (row - cb->start.lnum)) > 0
1237 || (diff == 0 && col < (int)(cb->start.col +
1238 cb->end.col) / 2)))))
1239 {
1240 cb->origin_row = (short_u)cb->end.lnum;
1241 cb->origin_start_col = cb->end.col - 1;
1242 cb->origin_end_col = cb->end.col;
1243 }
1244 else
1245 {
1246 cb->origin_row = (short_u)cb->start.lnum;
1247 cb->origin_start_col = cb->start.col;
1248 cb->origin_end_col = cb->start.col;
1249 }
1250 if (cb->mode == SELECT_MODE_WORD && !repeated_click)
1251 cb->mode = SELECT_MODE_CHAR;
1252 }
1253
1254 /* set state, for when using the right mouse button */
1255 cb->state = SELECT_IN_PROGRESS;
1256
1257#ifdef DEBUG_SELECTION
1258 printf("Selection extending to (%d,%d)\n", row, col);
1259#endif
1260
1261 if (repeated_click && ++cb->mode > SELECT_MODE_LINE)
1262 cb->mode = SELECT_MODE_CHAR;
1263
1264 switch (cb->mode)
1265 {
1266 case SELECT_MODE_CHAR:
1267 /* If we're on a different line, find where the line ends */
1268 if (row != cb->prev.lnum)
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001269 cb->word_end_col = clip_get_line_end(cb, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270
1271 /* See if we are before or after the origin of the selection */
1272 if (clip_compare_pos(row, col, cb->origin_row,
1273 cb->origin_start_col) >= 0)
1274 {
1275 if (col >= (int)cb->word_end_col)
1276 clip_update_modeless_selection(cb, cb->origin_row,
1277 cb->origin_start_col, row, (int)Columns);
1278 else
1279 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 if (has_mbyte && mb_lefthalve(row, col))
1281 slen = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 clip_update_modeless_selection(cb, cb->origin_row,
1283 cb->origin_start_col, row, col + slen);
1284 }
1285 }
1286 else
1287 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 if (has_mbyte
1289 && mb_lefthalve(cb->origin_row, cb->origin_start_col))
1290 slen = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 if (col >= (int)cb->word_end_col)
1292 clip_update_modeless_selection(cb, row, cb->word_end_col,
1293 cb->origin_row, cb->origin_start_col + slen);
1294 else
1295 clip_update_modeless_selection(cb, row, col,
1296 cb->origin_row, cb->origin_start_col + slen);
1297 }
1298 break;
1299
1300 case SELECT_MODE_WORD:
1301 /* If we are still within the same word, do nothing */
1302 if (row == cb->prev.lnum && col >= (int)cb->word_start_col
1303 && col < (int)cb->word_end_col && !repeated_click)
1304 return;
1305
1306 /* Get new word boundaries */
1307 clip_get_word_boundaries(cb, row, col);
1308
1309 /* Handle being after the origin point of selection */
1310 if (clip_compare_pos(row, col, cb->origin_row,
1311 cb->origin_start_col) >= 0)
1312 clip_update_modeless_selection(cb, cb->origin_row,
1313 cb->origin_start_col, row, cb->word_end_col);
1314 else
1315 clip_update_modeless_selection(cb, row, cb->word_start_col,
1316 cb->origin_row, cb->origin_end_col);
1317 break;
1318
1319 case SELECT_MODE_LINE:
1320 if (row == cb->prev.lnum && !repeated_click)
1321 return;
1322
1323 if (clip_compare_pos(row, col, cb->origin_row,
1324 cb->origin_start_col) >= 0)
1325 clip_update_modeless_selection(cb, cb->origin_row, 0, row,
1326 (int)Columns);
1327 else
1328 clip_update_modeless_selection(cb, row, 0, cb->origin_row,
1329 (int)Columns);
1330 break;
1331 }
1332
1333 cb->prev.lnum = row;
1334 cb->prev.col = col;
1335
1336#ifdef DEBUG_SELECTION
1337 printf("Selection is: (%u,%u) to (%u,%u)\n", cb->start.lnum,
1338 cb->start.col, cb->end.lnum, cb->end.col);
1339#endif
1340}
1341
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342# if defined(FEAT_GUI) || defined(PROTO)
1343/*
1344 * Redraw part of the selection if character at "row,col" is inside of it.
1345 * Only used for the GUI.
1346 */
1347 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001348clip_may_redraw_selection(int row, int col, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349{
1350 int start = col;
1351 int end = col + len;
1352
1353 if (clip_star.state != SELECT_CLEARED
1354 && row >= clip_star.start.lnum
1355 && row <= clip_star.end.lnum)
1356 {
1357 if (row == clip_star.start.lnum && start < (int)clip_star.start.col)
1358 start = clip_star.start.col;
1359 if (row == clip_star.end.lnum && end > (int)clip_star.end.col)
1360 end = clip_star.end.col;
1361 if (end > start)
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001362 clip_invert_area(&clip_star, row, start, row, end, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 }
1364}
1365# endif
1366
1367/*
1368 * Called from outside to clear selected region from the display
1369 */
1370 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02001371clip_clear_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001374 if (cbd->state == SELECT_CLEARED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 return;
1376
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001377 clip_invert_area(cbd, (int)cbd->start.lnum, cbd->start.col,
1378 (int)cbd->end.lnum, cbd->end.col, CLIP_CLEAR);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001379 cbd->state = SELECT_CLEARED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380}
1381
1382/*
1383 * Clear the selection if any lines from "row1" to "row2" are inside of it.
1384 */
1385 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001386clip_may_clear_selection(int row1, int row2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387{
1388 if (clip_star.state == SELECT_DONE
1389 && row2 >= clip_star.start.lnum
1390 && row1 <= clip_star.end.lnum)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001391 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392}
1393
1394/*
1395 * Called before the screen is scrolled up or down. Adjusts the line numbers
1396 * of the selection. Call with big number when clearing the screen.
1397 */
1398 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001399clip_scroll_selection(
1400 int rows) /* negative for scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401{
1402 int lnum;
1403
1404 if (clip_star.state == SELECT_CLEARED)
1405 return;
1406
1407 lnum = clip_star.start.lnum - rows;
1408 if (lnum <= 0)
1409 clip_star.start.lnum = 0;
1410 else if (lnum >= screen_Rows) /* scrolled off of the screen */
1411 clip_star.state = SELECT_CLEARED;
1412 else
1413 clip_star.start.lnum = lnum;
1414
1415 lnum = clip_star.end.lnum - rows;
1416 if (lnum < 0) /* scrolled off of the screen */
1417 clip_star.state = SELECT_CLEARED;
1418 else if (lnum >= screen_Rows)
1419 clip_star.end.lnum = screen_Rows - 1;
1420 else
1421 clip_star.end.lnum = lnum;
1422}
1423
1424/*
1425 * Invert a region of the display between a starting and ending row and column
1426 * Values for "how":
1427 * CLIP_CLEAR: undo inversion
1428 * CLIP_SET: set inversion
1429 * CLIP_TOGGLE: set inversion if pos1 < pos2, undo inversion otherwise.
1430 * 0: invert (GUI only).
1431 */
1432 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001433clip_invert_area(
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001434 Clipboard_T *cbd,
1435 int row1,
1436 int col1,
1437 int row2,
1438 int col2,
1439 int how)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440{
1441 int invert = FALSE;
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001442 int max_col;
1443
1444#ifdef FEAT_TEXT_PROP
Bram Moolenaarff9f27c2019-08-17 16:15:53 +02001445 max_col = cbd->max_col - 1;
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001446#else
1447 max_col = Columns - 1;
1448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449
1450 if (how == CLIP_SET)
1451 invert = TRUE;
1452
1453 /* Swap the from and to positions so the from is always before */
1454 if (clip_compare_pos(row1, col1, row2, col2) > 0)
1455 {
1456 int tmp_row, tmp_col;
1457
1458 tmp_row = row1;
1459 tmp_col = col1;
1460 row1 = row2;
1461 col1 = col2;
1462 row2 = tmp_row;
1463 col2 = tmp_col;
1464 }
1465 else if (how == CLIP_TOGGLE)
1466 invert = TRUE;
1467
1468 /* If all on the same line, do it the easy way */
1469 if (row1 == row2)
1470 {
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001471 clip_invert_rectangle(cbd, row1, col1, 1, col2 - col1, invert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 }
1473 else
1474 {
1475 /* Handle a piece of the first line */
1476 if (col1 > 0)
1477 {
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001478 clip_invert_rectangle(cbd, row1, col1, 1,
1479 (int)Columns - col1, invert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 row1++;
1481 }
1482
1483 /* Handle a piece of the last line */
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001484 if (col2 < max_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 {
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001486 clip_invert_rectangle(cbd, row2, 0, 1, col2, invert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 row2--;
1488 }
1489
1490 /* Handle the rectangle thats left */
1491 if (row2 >= row1)
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001492 clip_invert_rectangle(cbd, row1, 0, row2 - row1 + 1,
1493 (int)Columns, invert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 }
1495}
1496
1497/*
1498 * Invert or un-invert a rectangle of the screen.
1499 * "invert" is true if the result is inverted.
1500 */
1501 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001502clip_invert_rectangle(
Bram Moolenaar405bb422019-06-21 00:12:29 +02001503 Clipboard_T *cbd UNUSED,
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001504 int row_arg,
1505 int col_arg,
1506 int height_arg,
1507 int width_arg,
1508 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509{
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001510 int row = row_arg;
1511 int col = col_arg;
1512 int height = height_arg;
1513 int width = width_arg;
1514
Bram Moolenaar451d4b52019-06-12 20:22:27 +02001515#ifdef FEAT_TEXT_PROP
1516 // this goes on top of all popup windows
Bram Moolenaarc662ec92019-06-23 00:15:57 +02001517 screen_zindex = CLIP_ZINDEX;
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001518
1519 if (col < cbd->min_col)
1520 {
1521 width -= cbd->min_col - col;
1522 col = cbd->min_col;
1523 }
Bram Moolenaarff9f27c2019-08-17 16:15:53 +02001524 if (width > cbd->max_col - col)
1525 width = cbd->max_col - col;
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001526 if (row < cbd->min_row)
1527 {
1528 height -= cbd->min_row - row;
1529 row = cbd->min_row;
1530 }
1531 if (height > cbd->max_row - row + 1)
1532 height = cbd->max_row - row + 1;
Bram Moolenaar451d4b52019-06-12 20:22:27 +02001533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534#ifdef FEAT_GUI
1535 if (gui.in_use)
1536 gui_mch_invert_rectangle(row, col, height, width);
1537 else
1538#endif
1539 screen_draw_rectangle(row, col, height, width, invert);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02001540#ifdef FEAT_TEXT_PROP
1541 screen_zindex = 0;
1542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543}
1544
1545/*
1546 * Copy the currently selected area into the '*' register so it will be
1547 * available for pasting.
1548 * When "both" is TRUE also copy to the '+' register.
1549 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001551clip_copy_modeless_selection(int both UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552{
1553 char_u *buffer;
1554 char_u *bufp;
1555 int row;
1556 int start_col;
1557 int end_col;
1558 int line_end_col;
1559 int add_newline_flag = FALSE;
1560 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 int row1 = clip_star.start.lnum;
1563 int col1 = clip_star.start.col;
1564 int row2 = clip_star.end.lnum;
1565 int col2 = clip_star.end.col;
1566
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001567 /* Can't use ScreenLines unless initialized */
1568 if (ScreenLines == NULL)
1569 return;
1570
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 /*
1572 * Make sure row1 <= row2, and if row1 == row2 that col1 <= col2.
1573 */
1574 if (row1 > row2)
1575 {
1576 row = row1; row1 = row2; row2 = row;
1577 row = col1; col1 = col2; col2 = row;
1578 }
1579 else if (row1 == row2 && col1 > col2)
1580 {
1581 row = col1; col1 = col2; col2 = row;
1582 }
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001583#ifdef FEAT_TEXT_PROP
1584 if (col1 < clip_star.min_col)
1585 col1 = clip_star.min_col;
Bram Moolenaarff9f27c2019-08-17 16:15:53 +02001586 if (col2 > clip_star.max_col)
1587 col2 = clip_star.max_col;
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001588 if (row1 < clip_star.min_row)
1589 row1 = clip_star.min_row;
1590 if (row2 > clip_star.max_row)
1591 row2 = clip_star.max_row;
1592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 /* correct starting point for being on right halve of double-wide char */
1594 p = ScreenLines + LineOffset[row1];
1595 if (enc_dbcs != 0)
1596 col1 -= (*mb_head_off)(p, p + col1);
1597 else if (enc_utf8 && p[col1] == 0)
1598 --col1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599
1600 /* Create a temporary buffer for storing the text */
1601 len = (row2 - row1 + 1) * Columns + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 if (enc_dbcs != 0)
1603 len *= 2; /* max. 2 bytes per display cell */
1604 else if (enc_utf8)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001605 len *= MB_MAXBYTES;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001606 buffer = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 if (buffer == NULL) /* out of memory */
1608 return;
1609
1610 /* Process each row in the selection */
1611 for (bufp = buffer, row = row1; row <= row2; row++)
1612 {
1613 if (row == row1)
1614 start_col = col1;
1615 else
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001616#ifdef FEAT_TEXT_PROP
1617 start_col = clip_star.min_col;
1618#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 start_col = 0;
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621
1622 if (row == row2)
1623 end_col = col2;
Bram Moolenaard5cf8982019-08-16 23:09:11 +02001624 else
Bram Moolenaarff9f27c2019-08-17 16:15:53 +02001625#ifdef FEAT_TEXT_PROP
1626 end_col = clip_star.max_col;
1627#else
Bram Moolenaard5cf8982019-08-16 23:09:11 +02001628 end_col = Columns;
Bram Moolenaarff9f27c2019-08-17 16:15:53 +02001629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001631 line_end_col = clip_get_line_end(&clip_star, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
1633 /* See if we need to nuke some trailing whitespace */
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001634 if (end_col >=
1635#ifdef FEAT_TEXT_PROP
Bram Moolenaarff9f27c2019-08-17 16:15:53 +02001636 clip_star.max_col
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001637#else
1638 Columns
1639#endif
1640 && (row < row2 || end_col > line_end_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 {
1642 /* Get rid of trailing whitespace */
1643 end_col = line_end_col;
1644 if (end_col < start_col)
1645 end_col = start_col;
1646
1647 /* If the last line extended to the end, add an extra newline */
1648 if (row == row2)
1649 add_newline_flag = TRUE;
1650 }
1651
1652 /* If after the first row, we need to always add a newline */
1653 if (row > row1 && !LineWraps[row - 1])
1654 *bufp++ = NL;
1655
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001656 // Safetey check for in case resizing went wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 if (row < screen_Rows && end_col <= screen_Columns)
1658 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 if (enc_dbcs != 0)
1660 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001661 int i;
1662
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 p = ScreenLines + LineOffset[row];
1664 for (i = start_col; i < end_col; ++i)
1665 if (enc_dbcs == DBCS_JPNU && p[i] == 0x8e)
1666 {
1667 /* single-width double-byte char */
1668 *bufp++ = 0x8e;
1669 *bufp++ = ScreenLines2[LineOffset[row] + i];
1670 }
1671 else
1672 {
1673 *bufp++ = p[i];
1674 if (MB_BYTE2LEN(p[i]) == 2)
1675 *bufp++ = p[++i];
1676 }
1677 }
1678 else if (enc_utf8)
1679 {
1680 int off;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001681 int i;
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001682 int ci;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683
1684 off = LineOffset[row];
1685 for (i = start_col; i < end_col; ++i)
1686 {
1687 /* The base character is either in ScreenLinesUC[] or
1688 * ScreenLines[]. */
1689 if (ScreenLinesUC[off + i] == 0)
1690 *bufp++ = ScreenLines[off + i];
1691 else
1692 {
1693 bufp += utf_char2bytes(ScreenLinesUC[off + i], bufp);
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001694 for (ci = 0; ci < Screen_mco; ++ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001696 /* Add a composing character. */
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001697 if (ScreenLinesC[ci][off + i] == 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001698 break;
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001699 bufp += utf_char2bytes(ScreenLinesC[ci][off + i],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 }
1702 }
1703 /* Skip right halve of double-wide character. */
1704 if (ScreenLines[off + i + 1] == 0)
1705 ++i;
1706 }
1707 }
1708 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 {
1710 STRNCPY(bufp, ScreenLines + LineOffset[row] + start_col,
1711 end_col - start_col);
1712 bufp += end_col - start_col;
1713 }
1714 }
1715 }
1716
1717 /* Add a newline at the end if the selection ended there */
1718 if (add_newline_flag)
1719 *bufp++ = NL;
1720
1721 /* First cleanup any old selection and become the owner. */
1722 clip_free_selection(&clip_star);
1723 clip_own_selection(&clip_star);
1724
1725 /* Yank the text into the '*' register. */
1726 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_star);
1727
1728 /* Make the register contents available to the outside world. */
1729 clip_gen_set_selection(&clip_star);
1730
1731#ifdef FEAT_X11
1732 if (both)
1733 {
1734 /* Do the same for the '+' register. */
1735 clip_free_selection(&clip_plus);
1736 clip_own_selection(&clip_plus);
1737 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_plus);
1738 clip_gen_set_selection(&clip_plus);
1739 }
1740#endif
1741 vim_free(buffer);
1742}
1743
1744/*
1745 * Find the starting and ending positions of the word at the given row and
1746 * column. Only white-separated words are recognized here.
1747 */
1748#define CHAR_CLASS(c) (c <= ' ' ? ' ' : vim_iswordc(c))
1749
1750 static void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02001751clip_get_word_boundaries(Clipboard_T *cb, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752{
1753 int start_class;
1754 int temp_col;
1755 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 int mboff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001758 if (row >= screen_Rows || col >= screen_Columns || ScreenLines == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 return;
1760
1761 p = ScreenLines + LineOffset[row];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 /* Correct for starting in the right halve of a double-wide char */
1763 if (enc_dbcs != 0)
1764 col -= dbcs_screen_head_off(p, p + col);
1765 else if (enc_utf8 && p[col] == 0)
1766 --col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 start_class = CHAR_CLASS(p[col]);
1768
1769 temp_col = col;
1770 for ( ; temp_col > 0; temp_col--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 if (enc_dbcs != 0
1772 && (mboff = dbcs_screen_head_off(p, p + temp_col - 1)) > 0)
1773 temp_col -= mboff;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01001774 else if (CHAR_CLASS(p[temp_col - 1]) != start_class
1775 && !(enc_utf8 && p[temp_col - 1] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 break;
1777 cb->word_start_col = temp_col;
1778
1779 temp_col = col;
1780 for ( ; temp_col < screen_Columns; temp_col++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 if (enc_dbcs != 0 && dbcs_ptr2cells(p + temp_col) == 2)
1782 ++temp_col;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01001783 else if (CHAR_CLASS(p[temp_col]) != start_class
1784 && !(enc_utf8 && p[temp_col] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 break;
1786 cb->word_end_col = temp_col;
1787}
1788
1789/*
1790 * Find the column position for the last non-whitespace character on the given
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001791 * line at or before start_col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 */
1793 static int
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001794clip_get_line_end(Clipboard_T *cbd UNUSED, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795{
1796 int i;
1797
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001798 if (row >= screen_Rows || ScreenLines == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 return 0;
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001800 for (i =
1801#ifdef FEAT_TEXT_PROP
Bram Moolenaarff9f27c2019-08-17 16:15:53 +02001802 cbd->max_col;
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001803#else
1804 screen_Columns;
1805#endif
1806 i > 0; i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 if (ScreenLines[LineOffset[row] + i - 1] != ' ')
1808 break;
1809 return i;
1810}
1811
1812/*
1813 * Update the currently selected region by adding and/or subtracting from the
1814 * beginning or end and inverting the changed area(s).
1815 */
1816 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001817clip_update_modeless_selection(
Bram Moolenaar0554fa42019-06-14 21:36:54 +02001818 Clipboard_T *cb,
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001819 int row1,
1820 int col1,
1821 int row2,
1822 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823{
1824 /* See if we changed at the beginning of the selection */
1825 if (row1 != cb->start.lnum || col1 != (int)cb->start.col)
1826 {
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001827 clip_invert_area(cb, row1, col1, (int)cb->start.lnum, cb->start.col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 CLIP_TOGGLE);
1829 cb->start.lnum = row1;
1830 cb->start.col = col1;
1831 }
1832
1833 /* See if we changed at the end of the selection */
1834 if (row2 != cb->end.lnum || col2 != (int)cb->end.col)
1835 {
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001836 clip_invert_area(cb, (int)cb->end.lnum, cb->end.col, row2, col2,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 CLIP_TOGGLE);
1838 cb->end.lnum = row2;
1839 cb->end.col = col2;
1840 }
1841}
1842
1843 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02001844clip_gen_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845{
1846#ifdef FEAT_XCLIPBOARD
1847# ifdef FEAT_GUI
1848 if (gui.in_use)
1849 return clip_mch_own_selection(cbd);
1850 else
1851# endif
1852 return clip_xterm_own_selection(cbd);
1853#else
1854 return clip_mch_own_selection(cbd);
1855#endif
1856}
1857
1858 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02001859clip_gen_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860{
1861#ifdef FEAT_XCLIPBOARD
1862# ifdef FEAT_GUI
1863 if (gui.in_use)
1864 clip_mch_lose_selection(cbd);
1865 else
1866# endif
1867 clip_xterm_lose_selection(cbd);
1868#else
1869 clip_mch_lose_selection(cbd);
1870#endif
1871}
1872
1873 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02001874clip_gen_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875{
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001876 if (!clip_did_set_selection)
1877 {
1878 /* Updating postponed, so that accessing the system clipboard won't
Bram Moolenaarbdace832019-03-02 10:13:42 +01001879 * hang Vim when accessing it many times (e.g. on a :g command). */
Bram Moolenaar5c27fd12015-01-27 14:09:37 +01001880 if ((cbd == &clip_plus && (clip_unnamed_saved & CLIP_UNNAMED_PLUS))
1881 || (cbd == &clip_star && (clip_unnamed_saved & CLIP_UNNAMED)))
1882 {
1883 clipboard_needs_update = TRUE;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001884 return;
Bram Moolenaar5c27fd12015-01-27 14:09:37 +01001885 }
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887#ifdef FEAT_XCLIPBOARD
1888# ifdef FEAT_GUI
1889 if (gui.in_use)
1890 clip_mch_set_selection(cbd);
1891 else
1892# endif
1893 clip_xterm_set_selection(cbd);
1894#else
1895 clip_mch_set_selection(cbd);
1896#endif
1897}
1898
1899 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02001900clip_gen_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901{
1902#ifdef FEAT_XCLIPBOARD
1903# ifdef FEAT_GUI
1904 if (gui.in_use)
1905 clip_mch_request_selection(cbd);
1906 else
1907# endif
1908 clip_xterm_request_selection(cbd);
1909#else
1910 clip_mch_request_selection(cbd);
1911#endif
1912}
1913
Bram Moolenaar113e1072019-01-20 15:30:40 +01001914#if (defined(FEAT_X11) && defined(USE_SYSTEM)) || defined(PROTO)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001915 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02001916clip_gen_owner_exists(Clipboard_T *cbd UNUSED)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001917{
1918#ifdef FEAT_XCLIPBOARD
1919# ifdef FEAT_GUI_GTK
1920 if (gui.in_use)
1921 return clip_gtk_owner_exists(cbd);
1922 else
1923# endif
1924 return clip_x11_owner_exists(cbd);
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02001925#else
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001926 return TRUE;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02001927#endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001928}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001929#endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001930
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931#endif /* FEAT_CLIPBOARD */
1932
1933/*****************************************************************************
1934 * Functions that handle the input buffer.
1935 * This is used for any GUI version, and the unix terminal version.
1936 *
1937 * For Unix, the input characters are buffered to be able to check for a
1938 * CTRL-C. This should be done with signals, but I don't know how to do that
1939 * in a portable way for a tty in RAW mode.
1940 *
1941 * For the client-server code in the console the received keys are put in the
1942 * input buffer.
1943 */
1944
1945#if defined(USE_INPUT_BUF) || defined(PROTO)
1946
1947/*
1948 * Internal typeahead buffer. Includes extra space for long key code
1949 * descriptions which would otherwise overflow. The buffer is considered full
1950 * when only this extra space (or part of it) remains.
1951 */
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001952#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_CLIENTSERVER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 /*
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001954 * NetBeans stuffs debugger commands into the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 * This requires a larger buffer...
1956 * (Madsen) Go with this for remote input as well ...
1957 */
1958# define INBUFLEN 4096
1959#else
1960# define INBUFLEN 250
1961#endif
1962
1963static char_u inbuf[INBUFLEN + MAX_KEY_CODE_LEN];
1964static int inbufcount = 0; /* number of chars in inbuf[] */
1965
1966/*
1967 * vim_is_input_buf_full(), vim_is_input_buf_empty(), add_to_input_buf(), and
1968 * trash_input_buf() are functions for manipulating the input buffer. These
1969 * are used by the gui_* calls when a GUI is used to handle keyboard input.
1970 */
1971
1972 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001973vim_is_input_buf_full(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974{
1975 return (inbufcount >= INBUFLEN);
1976}
1977
1978 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001979vim_is_input_buf_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980{
1981 return (inbufcount == 0);
1982}
1983
1984#if defined(FEAT_OLE) || defined(PROTO)
1985 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001986vim_free_in_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987{
1988 return (INBUFLEN - inbufcount);
1989}
1990#endif
1991
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001992#if defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001994vim_used_in_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995{
1996 return inbufcount;
1997}
1998#endif
1999
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000/*
2001 * Return the current contents of the input buffer and make it empty.
2002 * The returned pointer must be passed to set_input_buf() later.
2003 */
2004 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002005get_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006{
2007 garray_T *gap;
2008
2009 /* We use a growarray to store the data pointer and the length. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002010 gap = ALLOC_ONE(garray_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 if (gap != NULL)
2012 {
2013 /* Add one to avoid a zero size. */
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02002014 gap->ga_data = alloc(inbufcount + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 if (gap->ga_data != NULL)
2016 mch_memmove(gap->ga_data, inbuf, (size_t)inbufcount);
2017 gap->ga_len = inbufcount;
2018 }
2019 trash_input_buf();
2020 return (char_u *)gap;
2021}
2022
2023/*
2024 * Restore the input buffer with a pointer returned from get_input_buf().
2025 * The allocated memory is freed, this only works once!
2026 */
2027 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002028set_input_buf(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029{
2030 garray_T *gap = (garray_T *)p;
2031
2032 if (gap != NULL)
2033 {
2034 if (gap->ga_data != NULL)
2035 {
2036 mch_memmove(inbuf, gap->ga_data, gap->ga_len);
2037 inbufcount = gap->ga_len;
2038 vim_free(gap->ga_data);
2039 }
2040 vim_free(gap);
2041 }
2042}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044/*
2045 * Add the given bytes to the input buffer
2046 * Special keys start with CSI. A real CSI must have been translated to
2047 * CSI KS_EXTRA KE_CSI. K_SPECIAL doesn't require translation.
2048 */
2049 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002050add_to_input_buf(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051{
2052 if (inbufcount + len > INBUFLEN + MAX_KEY_CODE_LEN)
2053 return; /* Shouldn't ever happen! */
2054
2055#ifdef FEAT_HANGULIN
2056 if ((State & (INSERT|CMDLINE)) && hangul_input_state_get())
2057 if ((len = hangul_input_process(s, len)) == 0)
2058 return;
2059#endif
2060
2061 while (len--)
2062 inbuf[inbufcount++] = *s++;
2063}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065/*
2066 * Add "str[len]" to the input buffer while escaping CSI bytes.
2067 */
2068 void
2069add_to_input_buf_csi(char_u *str, int len)
2070{
2071 int i;
2072 char_u buf[2];
2073
2074 for (i = 0; i < len; ++i)
2075 {
2076 add_to_input_buf(str + i, 1);
2077 if (str[i] == CSI)
2078 {
2079 /* Turn CSI into K_CSI. */
2080 buf[0] = KS_EXTRA;
2081 buf[1] = (int)KE_CSI;
2082 add_to_input_buf(buf, 2);
2083 }
2084 }
2085}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086
2087#if defined(FEAT_HANGULIN) || defined(PROTO)
2088 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002089push_raw_key(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090{
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01002091 char_u *tmpbuf;
Bram Moolenaar179f1b92016-03-04 22:52:34 +01002092 char_u *inp = s;
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01002093
Bram Moolenaar179f1b92016-03-04 22:52:34 +01002094 /* use the conversion result if possible */
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01002095 tmpbuf = hangul_string_convert(s, &len);
2096 if (tmpbuf != NULL)
Bram Moolenaar179f1b92016-03-04 22:52:34 +01002097 inp = tmpbuf;
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01002098
Bram Moolenaar179f1b92016-03-04 22:52:34 +01002099 for (; len--; inp++)
2100 {
2101 inbuf[inbufcount++] = *inp;
2102 if (*inp == CSI)
Bram Moolenaar00ded432016-03-03 11:45:15 +01002103 {
Bram Moolenaar179f1b92016-03-04 22:52:34 +01002104 /* Turn CSI into K_CSI. */
2105 inbuf[inbufcount++] = KS_EXTRA;
2106 inbuf[inbufcount++] = (int)KE_CSI;
Bram Moolenaar00ded432016-03-03 11:45:15 +01002107 }
Bram Moolenaar00ded432016-03-03 11:45:15 +01002108 }
Bram Moolenaar179f1b92016-03-04 22:52:34 +01002109 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110}
2111#endif
2112
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113/* Remove everything from the input buffer. Called when ^C is found */
2114 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002115trash_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116{
2117 inbufcount = 0;
2118}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119
2120/*
2121 * Read as much data from the input buffer as possible up to maxlen, and store
2122 * it in buf.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 */
2124 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002125read_from_input_buf(char_u *buf, long maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126{
2127 if (inbufcount == 0) /* if the buffer is empty, fill it */
2128 fill_input_buf(TRUE);
2129 if (maxlen > inbufcount)
2130 maxlen = inbufcount;
2131 mch_memmove(buf, inbuf, (size_t)maxlen);
2132 inbufcount -= maxlen;
2133 if (inbufcount)
2134 mch_memmove(inbuf, inbuf + maxlen, (size_t)inbufcount);
2135 return (int)maxlen;
2136}
2137
2138 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002139fill_input_buf(int exit_on_error UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140{
Bram Moolenaard0573012017-10-28 21:11:06 +02002141#if defined(UNIX) || defined(VMS) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 int len;
2143 int try;
2144 static int did_read_something = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 static char_u *rest = NULL; /* unconverted rest of previous read */
2146 static int restlen = 0;
2147 int unconverted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148#endif
2149
2150#ifdef FEAT_GUI
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002151 if (gui.in_use
2152# ifdef NO_CONSOLE_INPUT
2153 /* Don't use the GUI input when the window hasn't been opened yet.
2154 * We get here from ui_inchar() when we should try reading from stdin. */
2155 && !no_console_input()
2156# endif
2157 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 {
2159 gui_mch_update();
2160 return;
2161 }
2162#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02002163#if defined(UNIX) || defined(VMS) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 if (vim_is_input_buf_full())
2165 return;
2166 /*
2167 * Fill_input_buf() is only called when we really need a character.
2168 * If we can't get any, but there is some in the buffer, just return.
2169 * If we can't get any, and there isn't any in the buffer, we give up and
2170 * exit Vim.
2171 */
2172# ifdef __BEOS__
2173 /*
2174 * On the BeBox version (for now), all input is secretly performed within
2175 * beos_select() which is called from RealWaitForChar().
2176 */
2177 while (!vim_is_input_buf_full() && RealWaitForChar(read_cmd_fd, 0, NULL))
2178 ;
2179 len = inbufcount;
2180 inbufcount = 0;
2181# else
2182
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 if (rest != NULL)
2184 {
2185 /* Use remainder of previous call, starts with an invalid character
2186 * that may become valid when reading more. */
2187 if (restlen > INBUFLEN - inbufcount)
2188 unconverted = INBUFLEN - inbufcount;
2189 else
2190 unconverted = restlen;
2191 mch_memmove(inbuf + inbufcount, rest, unconverted);
2192 if (unconverted == restlen)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002193 VIM_CLEAR(rest);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 else
2195 {
2196 restlen -= unconverted;
2197 mch_memmove(rest, rest + unconverted, restlen);
2198 }
2199 inbufcount += unconverted;
2200 }
2201 else
2202 unconverted = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203
2204 len = 0; /* to avoid gcc warning */
2205 for (try = 0; try < 100; ++try)
2206 {
Bram Moolenaar4e601e32018-04-24 13:29:51 +02002207 size_t readlen = (size_t)((INBUFLEN - inbufcount)
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002208 / input_conv.vc_factor);
Bram Moolenaar4e601e32018-04-24 13:29:51 +02002209# ifdef VMS
Bram Moolenaar49943732018-04-24 20:27:26 +02002210 len = vms_read((char *)inbuf + inbufcount, readlen);
Bram Moolenaar4e601e32018-04-24 13:29:51 +02002211# else
2212 len = read(read_cmd_fd, (char *)inbuf + inbufcount, readlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213# endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002214
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 if (len > 0 || got_int)
2216 break;
2217 /*
2218 * If reading stdin results in an error, continue reading stderr.
2219 * This helps when using "foo | xargs vim".
2220 */
2221 if (!did_read_something && !isatty(read_cmd_fd) && read_cmd_fd == 0)
2222 {
2223 int m = cur_tmode;
2224
2225 /* We probably set the wrong file descriptor to raw mode. Switch
2226 * back to cooked mode, use another descriptor and set the mode to
2227 * what it was. */
2228 settmode(TMODE_COOK);
2229#ifdef HAVE_DUP
2230 /* Use stderr for stdin, also works for shell commands. */
2231 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002232 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233#else
2234 read_cmd_fd = 2; /* read from stderr instead of stdin */
2235#endif
2236 settmode(m);
2237 }
2238 if (!exit_on_error)
2239 return;
2240 }
2241# endif
2242 if (len <= 0 && !got_int)
2243 read_error_exit();
2244 if (len > 0)
2245 did_read_something = TRUE;
2246 if (got_int)
2247 {
2248 /* Interrupted, pretend a CTRL-C was typed. */
2249 inbuf[0] = 3;
2250 inbufcount = 1;
2251 }
2252 else
2253 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 /*
2255 * May perform conversion on the input characters.
2256 * Include the unconverted rest of the previous call.
2257 * If there is an incomplete char at the end it is kept for the next
2258 * time, reading more bytes should make conversion possible.
2259 * Don't do this in the unlikely event that the input buffer is too
2260 * small ("rest" still contains more bytes).
2261 */
2262 if (input_conv.vc_type != CONV_NONE)
2263 {
2264 inbufcount -= unconverted;
2265 len = convert_input_safe(inbuf + inbufcount,
2266 len + unconverted, INBUFLEN - inbufcount,
2267 rest == NULL ? &rest : NULL, &restlen);
2268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 while (len-- > 0)
2270 {
2271 /*
2272 * if a CTRL-C was typed, remove it from the buffer and set got_int
2273 */
2274 if (inbuf[inbufcount] == 3 && ctrl_c_interrupts)
2275 {
2276 /* remove everything typed before the CTRL-C */
2277 mch_memmove(inbuf, inbuf + inbufcount, (size_t)(len + 1));
2278 inbufcount = 0;
2279 got_int = TRUE;
2280 }
2281 ++inbufcount;
2282 }
2283 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002284#endif /* UNIX or VMS*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285}
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002286#endif /* defined(UNIX) || defined(FEAT_GUI) || defined(VMS) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287
2288/*
2289 * Exit because of an input read error.
2290 */
2291 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002292read_error_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293{
2294 if (silent_mode) /* Normal way to exit for "ex -s" */
2295 getout(0);
2296 STRCPY(IObuff, _("Vim: Error reading input, exiting...\n"));
2297 preserve_exit();
2298}
2299
2300#if defined(CURSOR_SHAPE) || defined(PROTO)
2301/*
2302 * May update the shape of the cursor.
2303 */
2304 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002305ui_cursor_shape_forced(int forced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306{
2307# ifdef FEAT_GUI
2308 if (gui.in_use)
2309 gui_update_cursor_later();
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002310 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002312 term_cursor_mode(forced);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002313
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314# ifdef MCH_CURSOR_SHAPE
2315 mch_update_cursor();
2316# endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002317
2318# ifdef FEAT_CONCEAL
Bram Moolenaarb9464822018-05-10 15:09:49 +02002319 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002320# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002322
2323 void
2324ui_cursor_shape(void)
2325{
2326 ui_cursor_shape_forced(FALSE);
2327}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328#endif
2329
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330/*
2331 * Check bounds for column number
2332 */
2333 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002334check_col(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335{
2336 if (col < 0)
2337 return 0;
2338 if (col >= (int)screen_Columns)
2339 return (int)screen_Columns - 1;
2340 return col;
2341}
2342
2343/*
2344 * Check bounds for row number
2345 */
2346 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002347check_row(int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348{
2349 if (row < 0)
2350 return 0;
2351 if (row >= (int)screen_Rows)
2352 return (int)screen_Rows - 1;
2353 return row;
2354}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355
2356/*
2357 * Stuff for the X clipboard. Shared between VMS and Unix.
2358 */
2359
2360#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) || defined(PROTO)
2361# include <X11/Xatom.h>
2362# include <X11/Intrinsic.h>
2363
2364/*
2365 * Open the application context (if it hasn't been opened yet).
2366 * Used for Motif and Athena GUI and the xterm clipboard.
2367 */
2368 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002369open_app_context(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370{
2371 if (app_context == NULL)
2372 {
2373 XtToolkitInitialize();
2374 app_context = XtCreateApplicationContext();
2375 }
2376}
2377
2378static Atom vim_atom; /* Vim's own special selection format */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379static Atom vimenc_atom; /* Vim's extended selection format */
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002380static Atom utf8_atom;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381static Atom compound_text_atom;
2382static Atom text_atom;
2383static Atom targets_atom;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002384static Atom timestamp_atom; /* Used to get a timestamp */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385
2386 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002387x11_setup_atoms(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388{
2389 vim_atom = XInternAtom(dpy, VIM_ATOM_NAME, False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 vimenc_atom = XInternAtom(dpy, VIMENC_ATOM_NAME,False);
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002391 utf8_atom = XInternAtom(dpy, "UTF8_STRING", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 compound_text_atom = XInternAtom(dpy, "COMPOUND_TEXT", False);
2393 text_atom = XInternAtom(dpy, "TEXT", False);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002394 targets_atom = XInternAtom(dpy, "TARGETS", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 clip_star.sel_atom = XA_PRIMARY;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002396 clip_plus.sel_atom = XInternAtom(dpy, "CLIPBOARD", False);
2397 timestamp_atom = XInternAtom(dpy, "TIMESTAMP", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398}
2399
2400/*
2401 * X Selection stuff, for cutting and pasting text to other windows.
2402 */
2403
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002404static Boolean clip_x11_convert_selection_cb(Widget w, Atom *sel_atom, Atom *target, Atom *type, XtPointer *value, long_u *length, int *format);
2405static void clip_x11_lose_ownership_cb(Widget w, Atom *sel_atom);
2406static void clip_x11_notify_cb(Widget w, Atom *sel_atom, Atom *target);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002407
2408/*
2409 * Property callback to get a timestamp for XtOwnSelection.
2410 */
2411 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002412clip_x11_timestamp_cb(
2413 Widget w,
2414 XtPointer n UNUSED,
2415 XEvent *event,
2416 Boolean *cont UNUSED)
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002417{
2418 Atom actual_type;
2419 int format;
2420 unsigned long nitems, bytes_after;
2421 unsigned char *prop=NULL;
2422 XPropertyEvent *xproperty=&event->xproperty;
2423
2424 /* Must be a property notify, state can't be Delete (True), has to be
2425 * one of the supported selection types. */
2426 if (event->type != PropertyNotify || xproperty->state
2427 || (xproperty->atom != clip_star.sel_atom
2428 && xproperty->atom != clip_plus.sel_atom))
2429 return;
2430
2431 if (XGetWindowProperty(xproperty->display, xproperty->window,
2432 xproperty->atom, 0, 0, False, timestamp_atom, &actual_type, &format,
2433 &nitems, &bytes_after, &prop))
2434 return;
2435
2436 if (prop)
2437 XFree(prop);
2438
2439 /* Make sure the property type is "TIMESTAMP" and it's 32 bits. */
2440 if (actual_type != timestamp_atom || format != 32)
2441 return;
2442
2443 /* Get the selection, using the event timestamp. */
Bram Moolenaar62b42182010-09-21 22:09:37 +02002444 if (XtOwnSelection(w, xproperty->atom, xproperty->time,
2445 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002446 clip_x11_notify_cb) == OK)
Bram Moolenaar62b42182010-09-21 22:09:37 +02002447 {
2448 /* Set the "owned" flag now, there may have been a call to
2449 * lose_ownership_cb in between. */
2450 if (xproperty->atom == clip_plus.sel_atom)
2451 clip_plus.owned = TRUE;
2452 else
2453 clip_star.owned = TRUE;
2454 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002455}
2456
2457 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002458x11_setup_selection(Widget w)
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002459{
2460 XtAddEventHandler(w, PropertyChangeMask, False,
2461 /*(XtEventHandler)*/clip_x11_timestamp_cb, (XtPointer)NULL);
2462}
2463
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002465clip_x11_request_selection_cb(
2466 Widget w UNUSED,
2467 XtPointer success,
2468 Atom *sel_atom,
2469 Atom *type,
2470 XtPointer value,
2471 long_u *length,
2472 int *format)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473{
Bram Moolenaard44347f2011-06-19 01:14:29 +02002474 int motion_type = MAUTO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 long_u len;
2476 char_u *p;
2477 char **text_list = NULL;
Bram Moolenaar0554fa42019-06-14 21:36:54 +02002478 Clipboard_T *cbd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
2481 if (*sel_atom == clip_plus.sel_atom)
2482 cbd = &clip_plus;
2483 else
2484 cbd = &clip_star;
2485
2486 if (value == NULL || *length == 0)
2487 {
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002488 clip_free_selection(cbd); /* nothing received, clear register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 *(int *)success = FALSE;
2490 return;
2491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 p = (char_u *)value;
2493 len = *length;
2494 if (*type == vim_atom)
2495 {
2496 motion_type = *p++;
2497 len--;
2498 }
2499
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 else if (*type == vimenc_atom)
2501 {
2502 char_u *enc;
2503 vimconv_T conv;
2504 int convlen;
2505
2506 motion_type = *p++;
2507 --len;
2508
2509 enc = p;
2510 p += STRLEN(p) + 1;
2511 len -= p - enc;
2512
2513 /* If the encoding of the text is different from 'encoding', attempt
2514 * converting it. */
2515 conv.vc_type = CONV_NONE;
2516 convert_setup(&conv, enc, p_enc);
2517 if (conv.vc_type != CONV_NONE)
2518 {
2519 convlen = len; /* Need to use an int here. */
2520 tmpbuf = string_convert(&conv, p, &convlen);
2521 len = convlen;
2522 if (tmpbuf != NULL)
2523 p = tmpbuf;
2524 convert_setup(&conv, NULL, NULL);
2525 }
2526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002528 else if (*type == compound_text_atom
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002529 || *type == utf8_atom
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002530 || (enc_dbcs != 0 && *type == text_atom))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 {
2532 XTextProperty text_prop;
2533 int n_text = 0;
2534 int status;
2535
2536 text_prop.value = (unsigned char *)value;
2537 text_prop.encoding = *type;
2538 text_prop.format = *format;
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002539 text_prop.nitems = len;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002540#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaare2e663f2013-03-07 18:02:30 +01002541 if (*type == utf8_atom)
2542 status = Xutf8TextPropertyToTextList(X_DISPLAY, &text_prop,
2543 &text_list, &n_text);
2544 else
2545#endif
2546 status = XmbTextPropertyToTextList(X_DISPLAY, &text_prop,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 &text_list, &n_text);
2548 if (status != Success || n_text < 1)
2549 {
2550 *(int *)success = FALSE;
2551 return;
2552 }
2553 p = (char_u *)text_list[0];
2554 len = STRLEN(p);
2555 }
2556 clip_yank_selection(motion_type, p, (long)len, cbd);
2557
2558 if (text_list != NULL)
2559 XFreeStringList(text_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 XtFree((char *)value);
2562 *(int *)success = TRUE;
2563}
2564
2565 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002566clip_x11_request_selection(
2567 Widget myShell,
2568 Display *dpy,
Bram Moolenaar0554fa42019-06-14 21:36:54 +02002569 Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570{
2571 XEvent event;
2572 Atom type;
2573 static int success;
2574 int i;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002575 time_t start_time;
2576 int timed_out = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002578 for (i = 0; i < 6; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 {
2580 switch (i)
2581 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 case 0: type = vimenc_atom; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 case 1: type = vim_atom; break;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002584 case 2: type = utf8_atom; break;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002585 case 3: type = compound_text_atom; break;
2586 case 4: type = text_atom; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 default: type = XA_STRING;
2588 }
Bram Moolenaar81851112013-04-12 12:27:30 +02002589 if (type == utf8_atom
2590# if defined(X_HAVE_UTF8_STRING)
2591 && !enc_utf8
2592# endif
2593 )
2594 /* Only request utf-8 when 'encoding' is utf8 and
2595 * Xutf8TextPropertyToTextList is available. */
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002596 continue;
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002597 success = MAYBE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 XtGetSelectionValue(myShell, cbd->sel_atom, type,
2599 clip_x11_request_selection_cb, (XtPointer)&success, CurrentTime);
2600
2601 /* Make sure the request for the selection goes out before waiting for
2602 * a response. */
2603 XFlush(dpy);
2604
2605 /*
2606 * Wait for result of selection request, otherwise if we type more
2607 * characters, then they will appear before the one that requested the
2608 * paste! Don't worry, we will catch up with any other events later.
2609 */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002610 start_time = time(NULL);
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002611 while (success == MAYBE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002613 if (XCheckTypedEvent(dpy, PropertyNotify, &event)
2614 || XCheckTypedEvent(dpy, SelectionNotify, &event)
2615 || XCheckTypedEvent(dpy, SelectionRequest, &event))
Bram Moolenaar89417b92008-09-07 19:48:53 +00002616 {
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002617 /* This is where clip_x11_request_selection_cb() should be
2618 * called. It may actually happen a bit later, so we loop
2619 * until "success" changes.
2620 * We may get a SelectionRequest here and if we don't handle
2621 * it we hang. KDE klipper does this, for example.
2622 * We need to handle a PropertyNotify for large selections. */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002623 XtDispatchEvent(&event);
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002624 continue;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626
Bram Moolenaar89417b92008-09-07 19:48:53 +00002627 /* Time out after 2 to 3 seconds to avoid that we hang when the
2628 * other process doesn't respond. Note that the SelectionNotify
2629 * event may still come later when the selection owner comes back
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002630 * to life and the text gets inserted unexpectedly. Don't know
2631 * why that happens or how to avoid that :-(. */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002632 if (time(NULL) > start_time + 2)
2633 {
2634 timed_out = TRUE;
2635 break;
2636 }
2637
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 /* Do we need this? Probably not. */
2639 XSync(dpy, False);
2640
Bram Moolenaar89417b92008-09-07 19:48:53 +00002641 /* Wait for 1 msec to avoid that we eat up all CPU time. */
2642 ui_delay(1L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 }
2644
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002645 if (success == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 return;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002647
2648 /* don't do a retry with another type after timing out, otherwise we
2649 * hang for 15 seconds. */
2650 if (timed_out)
2651 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 }
2653
2654 /* Final fallback position - use the X CUT_BUFFER0 store */
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002655 yank_cut_buffer0(dpy, cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656}
2657
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 static Boolean
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002659clip_x11_convert_selection_cb(
2660 Widget w UNUSED,
2661 Atom *sel_atom,
2662 Atom *target,
2663 Atom *type,
2664 XtPointer *value,
2665 long_u *length,
2666 int *format)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667{
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002668 static char_u *save_result = NULL;
2669 static long_u save_length = 0;
2670 char_u *string;
2671 int motion_type;
Bram Moolenaar0554fa42019-06-14 21:36:54 +02002672 Clipboard_T *cbd;
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002673 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674
2675 if (*sel_atom == clip_plus.sel_atom)
2676 cbd = &clip_plus;
2677 else
2678 cbd = &clip_star;
2679
2680 if (!cbd->owned)
2681 return False; /* Shouldn't ever happen */
2682
2683 /* requestor wants to know what target types we support */
2684 if (*target == targets_atom)
2685 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002686 static Atom array[7];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 *value = (XtPointer)array;
2689 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 array[i++] = targets_atom;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 array[i++] = vimenc_atom;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 array[i++] = vim_atom;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002693 if (enc_utf8)
2694 array[i++] = utf8_atom;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002695 array[i++] = XA_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 array[i++] = text_atom;
2697 array[i++] = compound_text_atom;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002698
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 *type = XA_ATOM;
2700 /* This used to be: *format = sizeof(Atom) * 8; but that caused
2701 * crashes on 64 bit machines. (Peter Derr) */
2702 *format = 32;
2703 *length = i;
2704 return True;
2705 }
2706
2707 if ( *target != XA_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 && *target != vimenc_atom
Bram Moolenaar980e58f2014-06-12 13:28:30 +02002709 && (*target != utf8_atom || !enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 && *target != vim_atom
2711 && *target != text_atom
2712 && *target != compound_text_atom)
2713 return False;
2714
2715 clip_get_selection(cbd);
2716 motion_type = clip_convert_selection(&string, length, cbd);
2717 if (motion_type < 0)
2718 return False;
2719
2720 /* For our own format, the first byte contains the motion type */
2721 if (*target == vim_atom)
2722 (*length)++;
2723
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 /* Our own format with encoding: motion 'encoding' NUL text */
2725 if (*target == vimenc_atom)
2726 *length += STRLEN(p_enc) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002728 if (save_length < *length || save_length / 2 >= *length)
2729 *value = XtRealloc((char *)save_result, (Cardinal)*length + 1);
2730 else
2731 *value = save_result;
2732 if (*value == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 {
2734 vim_free(string);
2735 return False;
2736 }
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002737 save_result = (char_u *)*value;
2738 save_length = *length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002740 if (*target == XA_STRING || (*target == utf8_atom && enc_utf8))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002742 mch_memmove(save_result, string, (size_t)(*length));
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002743 *type = *target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 }
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002745 else if (*target == compound_text_atom || *target == text_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 {
2747 XTextProperty text_prop;
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002748 char *string_nt = (char *)save_result;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002749 int conv_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750
2751 /* create NUL terminated string which XmbTextListToTextProperty wants */
2752 mch_memmove(string_nt, string, (size_t)*length);
2753 string_nt[*length] = NUL;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002754 conv_result = XmbTextListToTextProperty(X_DISPLAY, (char **)&string_nt,
2755 1, XCompoundTextStyle, &text_prop);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002756 if (conv_result != Success)
2757 {
2758 vim_free(string);
2759 return False;
2760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 *value = (XtPointer)(text_prop.value); /* from plain text */
2762 *length = text_prop.nitems;
2763 *type = compound_text_atom;
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002764 XtFree((char *)save_result);
2765 save_result = (char_u *)*value;
2766 save_length = *length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 else if (*target == vimenc_atom)
2769 {
2770 int l = STRLEN(p_enc);
2771
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002772 save_result[0] = motion_type;
2773 STRCPY(save_result + 1, p_enc);
2774 mch_memmove(save_result + l + 2, string, (size_t)(*length - l - 2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 *type = vimenc_atom;
2776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 else
2778 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002779 save_result[0] = motion_type;
2780 mch_memmove(save_result + 1, string, (size_t)(*length - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 *type = vim_atom;
2782 }
2783 *format = 8; /* 8 bits per char */
2784 vim_free(string);
2785 return True;
2786}
2787
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002789clip_x11_lose_ownership_cb(Widget w UNUSED, Atom *sel_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790{
2791 if (*sel_atom == clip_plus.sel_atom)
2792 clip_lose_selection(&clip_plus);
2793 else
2794 clip_lose_selection(&clip_star);
2795}
2796
2797 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02002798clip_x11_lose_selection(Widget myShell, Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799{
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002800 XtDisownSelection(myShell, cbd->sel_atom,
2801 XtLastTimestampProcessed(XtDisplay(myShell)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802}
2803
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002804 static void
2805clip_x11_notify_cb(Widget w UNUSED, Atom *sel_atom UNUSED, Atom *target UNUSED)
2806{
2807 /* To prevent automatically freeing the selection value. */
2808}
2809
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02002811clip_x11_own_selection(Widget myShell, Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812{
Bram Moolenaar62b42182010-09-21 22:09:37 +02002813 /* When using the GUI we have proper timestamps, use the one of the last
2814 * event. When in the console we don't get events (the terminal gets
2815 * them), Get the time by a zero-length append, clip_x11_timestamp_cb will
2816 * be called with the current timestamp. */
2817#ifdef FEAT_GUI
2818 if (gui.in_use)
2819 {
2820 if (XtOwnSelection(myShell, cbd->sel_atom,
2821 XtLastTimestampProcessed(XtDisplay(myShell)),
2822 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002823 clip_x11_notify_cb) == False)
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +01002824 return FAIL;
Bram Moolenaar62b42182010-09-21 22:09:37 +02002825 }
2826 else
2827#endif
2828 {
2829 if (!XChangeProperty(XtDisplay(myShell), XtWindow(myShell),
2830 cbd->sel_atom, timestamp_atom, 32, PropModeAppend, NULL, 0))
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +01002831 return FAIL;
Bram Moolenaar62b42182010-09-21 22:09:37 +02002832 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002833 /* Flush is required in a terminal as nothing else is doing it. */
2834 XFlush(XtDisplay(myShell));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 return OK;
2836}
2837
2838/*
2839 * Send the current selection to the clipboard. Do nothing for X because we
2840 * will fill in the selection only when requested by another app.
2841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02002843clip_x11_set_selection(Clipboard_T *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844{
2845}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002846
Bram Moolenaar113e1072019-01-20 15:30:40 +01002847#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD) && defined(USE_SYSTEM)) \
2848 || defined(PROTO)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002849 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02002850clip_x11_owner_exists(Clipboard_T *cbd)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002851{
2852 return XGetSelectionOwner(X_DISPLAY, cbd->sel_atom) != None;
2853}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854#endif
Bram Moolenaar113e1072019-01-20 15:30:40 +01002855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002857#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) \
2858 || defined(FEAT_GUI_GTK) || defined(PROTO)
2859/*
2860 * Get the contents of the X CUT_BUFFER0 and put it in "cbd".
2861 */
2862 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02002863yank_cut_buffer0(Display *dpy, Clipboard_T *cbd)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002864{
2865 int nbytes = 0;
2866 char_u *buffer = (char_u *)XFetchBuffer(dpy, &nbytes, 0);
2867
2868 if (nbytes > 0)
2869 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002870 int done = FALSE;
2871
2872 /* CUT_BUFFER0 is supposed to be always latin1. Convert to 'enc' when
2873 * using a multi-byte encoding. Conversion between two 8-bit
2874 * character sets usually fails and the text might actually be in
2875 * 'enc' anyway. */
2876 if (has_mbyte)
2877 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002878 char_u *conv_buf;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002879 vimconv_T vc;
2880
2881 vc.vc_type = CONV_NONE;
2882 if (convert_setup(&vc, (char_u *)"latin1", p_enc) == OK)
2883 {
2884 conv_buf = string_convert(&vc, buffer, &nbytes);
2885 if (conv_buf != NULL)
2886 {
2887 clip_yank_selection(MCHAR, conv_buf, (long)nbytes, cbd);
2888 vim_free(conv_buf);
2889 done = TRUE;
2890 }
2891 convert_setup(&vc, NULL, NULL);
2892 }
2893 }
2894 if (!done) /* use the text without conversion */
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002895 clip_yank_selection(MCHAR, buffer, (long)nbytes, cbd);
2896 XFree((void *)buffer);
2897 if (p_verbose > 0)
2898 {
2899 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002900 verb_msg(_("Used CUT_BUFFER0 instead of empty selection"));
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002901 verbose_leave();
2902 }
2903 }
2904}
2905#endif
2906
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907#if defined(FEAT_MOUSE) || defined(PROTO)
2908
2909/*
2910 * Move the cursor to the specified row and column on the screen.
Bram Moolenaar49325942007-05-10 19:19:59 +00002911 * Change current window if necessary. Returns an integer with the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 * CURSOR_MOVED bit set if the cursor has moved or unset otherwise.
2913 *
2914 * The MOUSE_FOLD_CLOSE bit is set when clicked on the '-' in a fold column.
2915 * The MOUSE_FOLD_OPEN bit is set when clicked on the '+' in a fold column.
2916 *
2917 * If flags has MOUSE_FOCUS, then the current window will not be changed, and
2918 * if the mouse is outside the window then the text will scroll, or if the
2919 * mouse was previously on a status line, then the status line may be dragged.
2920 *
2921 * If flags has MOUSE_MAY_VIS, then VIsual mode will be started before the
2922 * cursor is moved unless the cursor was on a status line.
2923 * This function returns one of IN_UNKNOWN, IN_BUFFER, IN_STATUS_LINE or
2924 * IN_SEP_LINE depending on where the cursor was clicked.
2925 *
2926 * If flags has MOUSE_MAY_STOP_VIS, then Visual mode will be stopped, unless
2927 * the mouse is on the status line of the same window.
2928 *
2929 * If flags has MOUSE_DID_MOVE, nothing is done if the mouse didn't move since
2930 * the last call.
2931 *
2932 * If flags has MOUSE_SETPOS, nothing is done, only the current position is
2933 * remembered.
2934 */
2935 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002936jump_to_mouse(
2937 int flags,
2938 int *inclusive, /* used for inclusive operator, can be NULL */
2939 int which_button) /* MOUSE_LEFT, MOUSE_RIGHT, MOUSE_MIDDLE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940{
2941 static int on_status_line = 0; /* #lines below bottom of window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 static int on_sep_line = 0; /* on separator right of window */
Bram Moolenaareb163d72017-09-23 15:08:17 +02002943#ifdef FEAT_MENU
2944 static int in_winbar = FALSE;
2945#endif
Bram Moolenaar451d4b52019-06-12 20:22:27 +02002946#ifdef FEAT_TEXT_PROP
Bram Moolenaarb53fb312019-06-13 23:59:52 +02002947 static int in_popup_win = FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002948 static win_T *click_in_popup_win = NULL;
Bram Moolenaar451d4b52019-06-12 20:22:27 +02002949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 static int prev_row = -1;
2951 static int prev_col = -1;
2952 static win_T *dragwin = NULL; /* window being dragged */
2953 static int did_drag = FALSE; /* drag was noticed */
2954
2955 win_T *wp, *old_curwin;
2956 pos_T old_cursor;
2957 int count;
2958 int first;
2959 int row = mouse_row;
2960 int col = mouse_col;
2961#ifdef FEAT_FOLDING
2962 int mouse_char;
2963#endif
2964
2965 mouse_past_bottom = FALSE;
2966 mouse_past_eol = FALSE;
2967
2968 if (flags & MOUSE_RELEASED)
2969 {
2970 /* On button release we may change window focus if positioned on a
2971 * status line and no dragging happened. */
2972 if (dragwin != NULL && !did_drag)
2973 flags &= ~(MOUSE_FOCUS | MOUSE_DID_MOVE);
2974 dragwin = NULL;
2975 did_drag = FALSE;
Bram Moolenaarb53fb312019-06-13 23:59:52 +02002976#ifdef FEAT_TEXT_PROP
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002977 if (click_in_popup_win != NULL && popup_dragwin == NULL)
2978 popup_close_for_mouse_click(click_in_popup_win);
2979
Bram Moolenaarb53fb312019-06-13 23:59:52 +02002980 popup_dragwin = NULL;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002981 click_in_popup_win = NULL;
Bram Moolenaarb53fb312019-06-13 23:59:52 +02002982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 }
2984
2985 if ((flags & MOUSE_DID_MOVE)
2986 && prev_row == mouse_row
2987 && prev_col == mouse_col)
2988 {
2989retnomove:
Bram Moolenaar49325942007-05-10 19:19:59 +00002990 /* before moving the cursor for a left click which is NOT in a status
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 * line, stop Visual mode */
2992 if (on_status_line)
2993 return IN_STATUS_LINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 if (on_sep_line)
2995 return IN_SEP_LINE;
Bram Moolenaard327b0c2017-11-12 16:56:12 +01002996#ifdef FEAT_MENU
2997 if (in_winbar)
2998 {
2999 /* A quick second click may arrive as a double-click, but we use it
3000 * as a second click in the WinBar. */
3001 if ((mod_mask & MOD_MASK_MULTI_CLICK) && !(flags & MOUSE_RELEASED))
3002 {
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003003 wp = mouse_find_win(&row, &col, FAIL_POPUP);
Bram Moolenaard327b0c2017-11-12 16:56:12 +01003004 if (wp == NULL)
3005 return IN_UNKNOWN;
3006 winbar_click(wp, col);
3007 }
3008 return IN_OTHER_WIN | MOUSE_WINBAR;
3009 }
3010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 if (flags & MOUSE_MAY_STOP_VIS)
3012 {
3013 end_visual_mode();
3014 redraw_curbuf_later(INVERTED); /* delete the inversion */
3015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003017 // Continue a modeless selection in another window.
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003018 if (cmdwin_type != 0 && row < curwin->w_winrow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 return IN_OTHER_WIN;
3020#endif
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003021#ifdef FEAT_TEXT_PROP
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02003022 // Continue a modeless selection in a popup window or dragging it.
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003023 if (in_popup_win)
Bram Moolenaarb53fb312019-06-13 23:59:52 +02003024 {
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003025 click_in_popup_win = NULL; // don't close it on release
Bram Moolenaarb53fb312019-06-13 23:59:52 +02003026 if (popup_dragwin != NULL)
3027 {
3028 // dragging a popup window
3029 popup_drag(popup_dragwin);
3030 return IN_UNKNOWN;
3031 }
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003032 return IN_OTHER_WIN;
Bram Moolenaarb53fb312019-06-13 23:59:52 +02003033 }
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 return IN_BUFFER;
3036 }
3037
3038 prev_row = mouse_row;
3039 prev_col = mouse_col;
3040
3041 if (flags & MOUSE_SETPOS)
3042 goto retnomove; /* ugly goto... */
3043
3044#ifdef FEAT_FOLDING
3045 /* Remember the character under the mouse, it might be a '-' or '+' in the
3046 * fold column. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003047 if (row >= 0 && row < Rows && col >= 0 && col <= Columns
3048 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 mouse_char = ScreenLines[LineOffset[row] + col];
3050 else
3051 mouse_char = ' ';
3052#endif
3053
3054 old_curwin = curwin;
3055 old_cursor = curwin->w_cursor;
3056
3057 if (!(flags & MOUSE_FOCUS))
3058 {
Bram Moolenaarb53fb312019-06-13 23:59:52 +02003059 if (row < 0 || col < 0) // check if it makes sense
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 return IN_UNKNOWN;
3061
Bram Moolenaarb53fb312019-06-13 23:59:52 +02003062 // find the window where the row is in
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003063 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003064 if (wp == NULL)
3065 return IN_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 dragwin = NULL;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003067
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003068#ifdef FEAT_TEXT_PROP
Bram Moolenaarb53fb312019-06-13 23:59:52 +02003069 // Click in a popup window may start dragging or modeless selection,
3070 // but not much else.
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02003071 if (WIN_IS_POPUP(wp))
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003072 {
3073 on_sep_line = 0;
3074 in_popup_win = TRUE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003075 if (wp->w_popup_close == POPCLOSE_BUTTON
3076 && which_button == MOUSE_LEFT
3077 && popup_on_X_button(wp, row, col))
3078 {
3079 popup_close_for_mouse_click(wp);
3080 return IN_UNKNOWN;
3081 }
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003082 else if ((wp->w_popup_flags & (POPF_DRAG | POPF_RESIZE))
3083 && popup_on_border(wp, row, col))
Bram Moolenaarb53fb312019-06-13 23:59:52 +02003084 {
3085 popup_dragwin = wp;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003086 popup_start_drag(wp, row, col);
Bram Moolenaarb53fb312019-06-13 23:59:52 +02003087 return IN_UNKNOWN;
3088 }
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003089 // Only close on release, otherwise it's not possible to drag or do
3090 // modeless selection.
3091 else if (wp->w_popup_close == POPCLOSE_CLICK
3092 && which_button == MOUSE_LEFT)
3093 {
3094 click_in_popup_win = wp;
3095 }
3096 else if (which_button == MOUSE_LEFT)
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02003097 // If the click is in the scrollbar, may scroll up/down.
3098 popup_handle_scrollbar_click(wp, row, col);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003099# ifdef FEAT_CLIPBOARD
3100 return IN_OTHER_WIN;
3101# else
3102 return IN_UNKNOWN;
3103# endif
3104 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +02003105 in_popup_win = FALSE;
3106 popup_dragwin = NULL;
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003107#endif
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003108#ifdef FEAT_MENU
3109 if (row == -1)
3110 {
3111 /* A click in the window toolbar does not enter another window or
3112 * change Visual highlighting. */
3113 winbar_click(wp, col);
Bram Moolenaareb163d72017-09-23 15:08:17 +02003114 in_winbar = TRUE;
3115 return IN_OTHER_WIN | MOUSE_WINBAR;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003116 }
Bram Moolenaareb163d72017-09-23 15:08:17 +02003117 in_winbar = FALSE;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003118#endif
3119
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 /*
3121 * winpos and height may change in win_enter()!
3122 */
3123 if (row >= wp->w_height) /* In (or below) status line */
3124 {
3125 on_status_line = row - wp->w_height + 1;
3126 dragwin = wp;
3127 }
3128 else
3129 on_status_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 if (col >= wp->w_width) /* In separator line */
3131 {
3132 on_sep_line = col - wp->w_width + 1;
3133 dragwin = wp;
3134 }
3135 else
3136 on_sep_line = 0;
3137
3138 /* The rightmost character of the status line might be a vertical
3139 * separator character if there is no connecting window to the right. */
3140 if (on_status_line && on_sep_line)
3141 {
3142 if (stl_connected(wp))
3143 on_sep_line = 0;
3144 else
3145 on_status_line = 0;
3146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 /* Before jumping to another buffer, or moving the cursor for a left
3149 * click, stop Visual mode. */
3150 if (VIsual_active
3151 && (wp->w_buffer != curwin->w_buffer
Bram Moolenaar4033c552017-09-16 20:54:51 +02003152 || (!on_status_line && !on_sep_line
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003153#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003155# ifdef FEAT_RIGHTLEFT
Bram Moolenaar02631462017-09-22 15:20:32 +02003156 wp->w_p_rl ? col < wp->w_width - wp->w_p_fdc :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157# endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003158 col >= wp->w_p_fdc
3159# ifdef FEAT_CMDWIN
3160 + (cmdwin_type == 0 && wp == curwin ? 0 : 1)
3161# endif
3162 )
3163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 && (flags & MOUSE_MAY_STOP_VIS))))
3165 {
3166 end_visual_mode();
3167 redraw_curbuf_later(INVERTED); /* delete the inversion */
3168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169#ifdef FEAT_CMDWIN
3170 if (cmdwin_type != 0 && wp != curwin)
3171 {
3172 /* A click outside the command-line window: Use modeless
Bram Moolenaarf679a432010-03-02 18:16:09 +01003173 * selection if possible. Allow dragging the status lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 on_sep_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175# ifdef FEAT_CLIPBOARD
3176 if (on_status_line)
3177 return IN_STATUS_LINE;
3178 return IN_OTHER_WIN;
3179# else
3180 row = 0;
3181 col += wp->w_wincol;
3182 wp = curwin;
3183# endif
3184 }
3185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 /* Only change window focus when not clicking on or dragging the
3187 * status line. Do change focus when releasing the mouse button
3188 * (MOUSE_FOCUS was set above if we dragged first). */
3189 if (dragwin == NULL || (flags & MOUSE_RELEASED))
3190 win_enter(wp, TRUE); /* can make wp invalid! */
Bram Moolenaarc48369c2018-03-11 19:30:45 +01003191
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 if (curwin != old_curwin)
Bram Moolenaarc48369c2018-03-11 19:30:45 +01003193 {
3194#ifdef CHECK_DOUBLE_CLICK
3195 /* set topline, to be able to check for double click ourselves */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 set_mouse_topline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197#endif
Bram Moolenaarc48369c2018-03-11 19:30:45 +01003198#ifdef FEAT_TERMINAL
3199 /* when entering a terminal window may change state */
3200 term_win_entered();
3201#endif
3202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 if (on_status_line) /* In (or below) status line */
3204 {
3205 /* Don't use start_arrow() if we're in the same window */
3206 if (curwin == old_curwin)
3207 return IN_STATUS_LINE;
3208 else
3209 return IN_STATUS_LINE | CURSOR_MOVED;
3210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 if (on_sep_line) /* In (or below) status line */
3212 {
3213 /* Don't use start_arrow() if we're in the same window */
3214 if (curwin == old_curwin)
3215 return IN_SEP_LINE;
3216 else
3217 return IN_SEP_LINE | CURSOR_MOVED;
3218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219
3220 curwin->w_cursor.lnum = curwin->w_topline;
3221#ifdef FEAT_GUI
3222 /* remember topline, needed for double click */
3223 gui_prev_topline = curwin->w_topline;
3224# ifdef FEAT_DIFF
3225 gui_prev_topfill = curwin->w_topfill;
3226# endif
3227#endif
3228 }
3229 else if (on_status_line && which_button == MOUSE_LEFT)
3230 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 if (dragwin != NULL)
3232 {
3233 /* Drag the status line */
3234 count = row - dragwin->w_winrow - dragwin->w_height + 1
3235 - on_status_line;
3236 win_drag_status_line(dragwin, count);
3237 did_drag |= count;
3238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 return IN_STATUS_LINE; /* Cursor didn't move */
3240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 else if (on_sep_line && which_button == MOUSE_LEFT)
3242 {
3243 if (dragwin != NULL)
3244 {
3245 /* Drag the separator column */
3246 count = col - dragwin->w_wincol - dragwin->w_width + 1
3247 - on_sep_line;
3248 win_drag_vsep_line(dragwin, count);
3249 did_drag |= count;
3250 }
3251 return IN_SEP_LINE; /* Cursor didn't move */
3252 }
Bram Moolenaareb163d72017-09-23 15:08:17 +02003253#ifdef FEAT_MENU
3254 else if (in_winbar)
3255 {
3256 /* After a click on the window toolbar don't start Visual mode. */
3257 return IN_OTHER_WIN | MOUSE_WINBAR;
3258 }
3259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 else /* keep_window_focus must be TRUE */
3261 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 /* before moving the cursor for a left click, stop Visual mode */
3263 if (flags & MOUSE_MAY_STOP_VIS)
3264 {
3265 end_visual_mode();
3266 redraw_curbuf_later(INVERTED); /* delete the inversion */
3267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268
3269#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
3270 /* Continue a modeless selection in another window. */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003271 if (cmdwin_type != 0 && row < curwin->w_winrow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 return IN_OTHER_WIN;
3273#endif
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003274#ifdef FEAT_TEXT_PROP
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003275 if (in_popup_win)
Bram Moolenaarb53fb312019-06-13 23:59:52 +02003276 {
3277 if (popup_dragwin != NULL)
3278 {
3279 // dragging a popup window
3280 popup_drag(popup_dragwin);
3281 return IN_UNKNOWN;
3282 }
3283 // continue a modeless selection in a popup window
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003284 click_in_popup_win = NULL;
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003285 return IN_OTHER_WIN;
Bram Moolenaarb53fb312019-06-13 23:59:52 +02003286 }
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288
3289 row -= W_WINROW(curwin);
Bram Moolenaar53f81742017-09-22 14:35:51 +02003290 col -= curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291
3292 /*
3293 * When clicking beyond the end of the window, scroll the screen.
3294 * Scroll by however many rows outside the window we are.
3295 */
3296 if (row < 0)
3297 {
3298 count = 0;
3299 for (first = TRUE; curwin->w_topline > 1; )
3300 {
3301#ifdef FEAT_DIFF
3302 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
3303 ++count;
3304 else
3305#endif
3306 count += plines(curwin->w_topline - 1);
3307 if (!first && count > -row)
3308 break;
3309 first = FALSE;
3310#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02003311 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312#endif
3313#ifdef FEAT_DIFF
3314 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
3315 ++curwin->w_topfill;
3316 else
3317#endif
3318 {
3319 --curwin->w_topline;
3320#ifdef FEAT_DIFF
3321 curwin->w_topfill = 0;
3322#endif
3323 }
3324 }
3325#ifdef FEAT_DIFF
3326 check_topfill(curwin, FALSE);
3327#endif
3328 curwin->w_valid &=
3329 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3330 redraw_later(VALID);
3331 row = 0;
3332 }
3333 else if (row >= curwin->w_height)
3334 {
3335 count = 0;
3336 for (first = TRUE; curwin->w_topline < curbuf->b_ml.ml_line_count; )
3337 {
3338#ifdef FEAT_DIFF
3339 if (curwin->w_topfill > 0)
3340 ++count;
3341 else
3342#endif
3343 count += plines(curwin->w_topline);
3344 if (!first && count > row - curwin->w_height + 1)
3345 break;
3346 first = FALSE;
3347#ifdef FEAT_FOLDING
3348 if (hasFolding(curwin->w_topline, NULL, &curwin->w_topline)
3349 && curwin->w_topline == curbuf->b_ml.ml_line_count)
3350 break;
3351#endif
3352#ifdef FEAT_DIFF
3353 if (curwin->w_topfill > 0)
3354 --curwin->w_topfill;
3355 else
3356#endif
3357 {
3358 ++curwin->w_topline;
3359#ifdef FEAT_DIFF
3360 curwin->w_topfill =
3361 diff_check_fill(curwin, curwin->w_topline);
3362#endif
3363 }
3364 }
3365#ifdef FEAT_DIFF
3366 check_topfill(curwin, FALSE);
3367#endif
3368 redraw_later(VALID);
3369 curwin->w_valid &=
3370 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3371 row = curwin->w_height - 1;
3372 }
3373 else if (row == 0)
3374 {
3375 /* When dragging the mouse, while the text has been scrolled up as
3376 * far as it goes, moving the mouse in the top line should scroll
3377 * the text down (done later when recomputing w_topline). */
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003378 if (mouse_dragging > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 && curwin->w_cursor.lnum
3380 == curwin->w_buffer->b_ml.ml_line_count
3381 && curwin->w_cursor.lnum == curwin->w_topline)
3382 curwin->w_valid &= ~(VALID_TOPLINE);
3383 }
3384 }
3385
3386#ifdef FEAT_FOLDING
3387 /* Check for position outside of the fold column. */
3388 if (
3389# ifdef FEAT_RIGHTLEFT
Bram Moolenaar02631462017-09-22 15:20:32 +02003390 curwin->w_p_rl ? col < curwin->w_width - curwin->w_p_fdc :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391# endif
3392 col >= curwin->w_p_fdc
3393# ifdef FEAT_CMDWIN
3394 + (cmdwin_type == 0 ? 0 : 1)
3395# endif
3396 )
3397 mouse_char = ' ';
3398#endif
3399
3400 /* compute the position in the buffer line from the posn on the screen */
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003401 if (mouse_comp_pos(curwin, &row, &col, &curwin->w_cursor.lnum, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 mouse_past_bottom = TRUE;
3403
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 /* Start Visual mode before coladvance(), for when 'sel' != "old" */
3405 if ((flags & MOUSE_MAY_VIS) && !VIsual_active)
3406 {
3407 check_visual_highlight();
3408 VIsual = old_cursor;
3409 VIsual_active = TRUE;
3410 VIsual_reselect = TRUE;
3411 /* if 'selectmode' contains "mouse", start Select mode */
3412 may_start_select('o');
3413 setmouse();
Bram Moolenaar7df351e2006-01-23 22:30:28 +00003414 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 redraw_cmdline = TRUE; /* show visual mode later */
3416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417
3418 curwin->w_curswant = col;
3419 curwin->w_set_curswant = FALSE; /* May still have been TRUE */
3420 if (coladvance(col) == FAIL) /* Mouse click beyond end of line */
3421 {
3422 if (inclusive != NULL)
3423 *inclusive = TRUE;
3424 mouse_past_eol = TRUE;
3425 }
3426 else if (inclusive != NULL)
3427 *inclusive = FALSE;
3428
3429 count = IN_BUFFER;
3430 if (curwin != old_curwin || curwin->w_cursor.lnum != old_cursor.lnum
3431 || curwin->w_cursor.col != old_cursor.col)
3432 count |= CURSOR_MOVED; /* Cursor has moved */
3433
Bram Moolenaar4c063a02019-06-10 21:24:12 +02003434# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 if (mouse_char == '+')
3436 count |= MOUSE_FOLD_OPEN;
3437 else if (mouse_char != ' ')
3438 count |= MOUSE_FOLD_CLOSE;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02003439# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440
3441 return count;
3442}
Bram Moolenaar4c063a02019-06-10 21:24:12 +02003443#endif
3444
3445// Functions also used for popup windows.
3446#if defined(FEAT_MOUSE) || defined(FEAT_TEXT_PROP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447
3448/*
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003449 * Compute the buffer line position from the screen position "rowp" / "colp" in
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 * window "win".
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003451 * "plines_cache" can be NULL (no cache) or an array with "win->w_height"
3452 * entries that caches the plines_win() result from a previous call. Entry is
3453 * zero if not computed yet. There must be no text or setting changes since
3454 * the entry is put in the cache.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 * Returns TRUE if the position is below the last line.
3456 */
3457 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003458mouse_comp_pos(
3459 win_T *win,
3460 int *rowp,
3461 int *colp,
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003462 linenr_T *lnump,
3463 int *plines_cache)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464{
3465 int col = *colp;
3466 int row = *rowp;
3467 linenr_T lnum;
3468 int retval = FALSE;
3469 int off;
3470 int count;
3471
3472#ifdef FEAT_RIGHTLEFT
3473 if (win->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02003474 col = win->w_width - 1 - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475#endif
3476
3477 lnum = win->w_topline;
3478
3479 while (row > 0)
3480 {
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003481 int cache_idx = lnum - win->w_topline;
3482
3483 if (plines_cache != NULL && plines_cache[cache_idx] > 0)
3484 count = plines_cache[cache_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 else
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003486 {
3487#ifdef FEAT_DIFF
3488 /* Don't include filler lines in "count" */
3489 if (win->w_p_diff
3490# ifdef FEAT_FOLDING
3491 && !hasFoldingWin(win, lnum, NULL, NULL, TRUE, NULL)
3492# endif
3493 )
3494 {
3495 if (lnum == win->w_topline)
3496 row -= win->w_topfill;
3497 else
3498 row -= diff_check_fill(win, lnum);
3499 count = plines_win_nofill(win, lnum, TRUE);
3500 }
3501 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502#endif
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003503 count = plines_win(win, lnum, TRUE);
3504 if (plines_cache != NULL)
3505 plines_cache[cache_idx] = count;
3506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 if (count > row)
3508 break; /* Position is in this buffer line. */
3509#ifdef FEAT_FOLDING
3510 (void)hasFoldingWin(win, lnum, NULL, &lnum, TRUE, NULL);
3511#endif
3512 if (lnum == win->w_buffer->b_ml.ml_line_count)
3513 {
3514 retval = TRUE;
3515 break; /* past end of file */
3516 }
3517 row -= count;
3518 ++lnum;
3519 }
3520
3521 if (!retval)
3522 {
3523 /* Compute the column without wrapping. */
3524 off = win_col_off(win) - win_col_off2(win);
3525 if (col < off)
3526 col = off;
Bram Moolenaar02631462017-09-22 15:20:32 +02003527 col += row * (win->w_width - off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 /* add skip column (for long wrapping line) */
3529 col += win->w_skipcol;
3530 }
3531
3532 if (!win->w_p_wrap)
3533 col += win->w_leftcol;
3534
3535 /* skip line number and fold column in front of the line */
3536 col -= win_col_off(win);
3537 if (col < 0)
3538 {
3539#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003540 netbeans_gutter_click(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541#endif
3542 col = 0;
3543 }
3544
3545 *colp = col;
3546 *rowp = row;
3547 *lnump = lnum;
3548 return retval;
3549}
3550
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551/*
3552 * Find the window at screen position "*rowp" and "*colp". The positions are
3553 * updated to become relative to the top-left of the window.
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003554 * When "popup" is FAIL_POPUP and the position is in a popup window then NULL
3555 * is returned. When "popup" is IGNORE_POPUP then do not even check popup
3556 * windows.
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003557 * Returns NULL when something is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 win_T *
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003560mouse_find_win(int *rowp, int *colp, mouse_find_T popup UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561{
3562 frame_T *fp;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003563 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003565#ifdef FEAT_TEXT_PROP
3566 win_T *pwp = NULL;
3567
3568 if (popup != IGNORE_POPUP)
3569 {
3570 popup_reset_handled();
3571 while ((wp = find_next_popup(TRUE)) != NULL)
3572 {
3573 if (*rowp >= wp->w_winrow && *rowp < wp->w_winrow + popup_height(wp)
3574 && *colp >= wp->w_wincol
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02003575 && *colp < wp->w_wincol + popup_width(wp))
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003576 pwp = wp;
3577 }
3578 if (pwp != NULL)
3579 {
3580 if (popup == FAIL_POPUP)
3581 return NULL;
3582 *rowp -= pwp->w_winrow;
3583 *colp -= pwp->w_wincol;
3584 return pwp;
3585 }
3586 }
3587#endif
3588
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 fp = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003590 *rowp -= firstwin->w_winrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 for (;;)
3592 {
3593 if (fp->fr_layout == FR_LEAF)
3594 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 if (fp->fr_layout == FR_ROW)
3596 {
3597 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
3598 {
3599 if (*colp < fp->fr_width)
3600 break;
3601 *colp -= fp->fr_width;
3602 }
3603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 else /* fr_layout == FR_COL */
3605 {
3606 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
3607 {
3608 if (*rowp < fp->fr_height)
3609 break;
3610 *rowp -= fp->fr_height;
3611 }
3612 }
3613 }
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003614 /* When using a timer that closes a window the window might not actually
3615 * exist. */
3616 FOR_ALL_WINDOWS(wp)
3617 if (wp == fp->fr_win)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003618 {
3619#ifdef FEAT_MENU
3620 *rowp -= wp->w_winbar_height;
3621#endif
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003622 return wp;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003623 }
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003624 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626
Bram Moolenaar860cae12010-06-05 23:22:07 +02003627#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar658a1542018-03-03 19:29:43 +01003629 || defined(FEAT_GUI_PHOTON) || defined(FEAT_TERM_POPUP_MENU) \
3630 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631/*
3632 * Translate window coordinates to buffer position without any side effects
3633 */
3634 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003635get_fpos_of_mouse(pos_T *mpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636{
3637 win_T *wp;
3638 int row = mouse_row;
3639 int col = mouse_col;
3640
3641 if (row < 0 || col < 0) /* check if it makes sense */
3642 return IN_UNKNOWN;
3643
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 /* find the window where the row is in */
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003645 wp = mouse_find_win(&row, &col, FAIL_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003646 if (wp == NULL)
3647 return IN_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 /*
3649 * winpos and height may change in win_enter()!
3650 */
3651 if (row >= wp->w_height) /* In (or below) status line */
3652 return IN_STATUS_LINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 if (col >= wp->w_width) /* In vertical separator line */
3654 return IN_SEP_LINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655
3656 if (wp != curwin)
3657 return IN_UNKNOWN;
3658
3659 /* compute the position in the buffer line from the posn on the screen */
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003660 if (mouse_comp_pos(curwin, &row, &col, &mpos->lnum, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 return IN_STATUS_LINE; /* past bottom */
3662
3663 mpos->col = vcol2col(wp, mpos->lnum, col);
3664
3665 if (mpos->col > 0)
3666 --mpos->col;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003667 mpos->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 return IN_BUFFER;
3669}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003672#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MAC) \
3673 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar3767b612018-03-03 19:51:58 +01003674 || defined(FEAT_GUI_PHOTON) || defined(FEAT_BEVAL) \
3675 || defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676/*
3677 * Convert a virtual (screen) column to a character column.
3678 * The first column is one.
3679 */
3680 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003681vcol2col(win_T *wp, linenr_T lnum, int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682{
3683 /* try to advance to the specified column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 int count = 0;
3685 char_u *ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003686 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687
Bram Moolenaar597a4222014-06-25 14:39:50 +02003688 line = ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaarb6101cf2012-10-21 00:58:39 +02003689 while (count < vcol && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003691 count += win_lbr_chartabsize(wp, line, ptr, count, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003692 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02003694 return (int)(ptr - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695}
3696#endif
3697
3698#endif /* FEAT_MOUSE */
3699
Bram Moolenaar4f974752019-02-17 17:44:42 +01003700#if defined(FEAT_GUI) || defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701/*
3702 * Called when focus changed. Used for the GUI or for systems where this can
3703 * be done in the console (Win32).
3704 */
3705 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003706ui_focus_change(
3707 int in_focus) /* TRUE if focus gained. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708{
3709 static time_t last_time = (time_t)0;
3710 int need_redraw = FALSE;
3711
3712 /* When activated: Check if any file was modified outside of Vim.
3713 * Only do this when not done within the last two seconds (could get
3714 * several events in a row). */
3715 if (in_focus && last_time + 2 < time(NULL))
3716 {
3717 need_redraw = check_timestamps(
3718# ifdef FEAT_GUI
3719 gui.in_use
3720# else
3721 FALSE
3722# endif
3723 );
3724 last_time = time(NULL);
3725 }
3726
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 /*
3728 * Fire the focus gained/lost autocommand.
3729 */
3730 need_redraw |= apply_autocmds(in_focus ? EVENT_FOCUSGAINED
3731 : EVENT_FOCUSLOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732
3733 if (need_redraw)
3734 {
3735 /* Something was executed, make sure the cursor is put back where it
3736 * belongs. */
3737 need_wait_return = FALSE;
3738
3739 if (State & CMDLINE)
3740 redrawcmdline();
3741 else if (State == HITRETURN || State == SETWSIZE || State == ASKMORE
3742 || State == EXTERNCMD || State == CONFIRM || exmode_active)
3743 repeat_message();
3744 else if ((State & NORMAL) || (State & INSERT))
3745 {
3746 if (must_redraw != 0)
3747 update_screen(0);
3748 setcursor();
3749 }
3750 cursor_on(); /* redrawing may have switched it off */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003751 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752# ifdef FEAT_GUI
3753 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 gui_update_scrollbars(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755# endif
3756 }
3757#ifdef FEAT_TITLE
3758 /* File may have been changed from 'readonly' to 'noreadonly' */
3759 if (need_maketitle)
3760 maketitle();
3761#endif
3762}
3763#endif
3764
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003765#if defined(HAVE_INPUT_METHOD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766/*
3767 * Save current Input Method status to specified place.
3768 */
3769 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003770im_save_status(long *psave)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771{
3772 /* Don't save when 'imdisable' is set or "xic" is NULL, IM is always
3773 * disabled then (but might start later).
3774 * Also don't save when inside a mapping, vgetc_im_active has not been set
3775 * then.
3776 * And don't save when the keys were stuffed (e.g., for a "." command).
3777 * And don't save when the GUI is running but our window doesn't have
3778 * input focus (e.g., when a find dialog is open). */
3779 if (!p_imdisable && KeyTyped && !KeyStuffed
3780# ifdef FEAT_XIM
3781 && xic != NULL
3782# endif
3783# ifdef FEAT_GUI
3784 && (!gui.in_use || gui.in_focus)
3785# endif
3786 )
3787 {
3788 /* Do save when IM is on, or IM is off and saved status is on. */
3789 if (vgetc_im_active)
3790 *psave = B_IMODE_IM;
3791 else if (*psave == B_IMODE_IM)
3792 *psave = B_IMODE_NONE;
3793 }
3794}
3795#endif