blob: 153d0203c27602ef4665d4d13ce29997ee5bdf4c [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 Moolenaarff9f27c2019-08-17 16:15:53 +02001101 cb->max_col = wp->w_wincol + popup_width(wp) - wp->w_popup_border[1];
1102 if (cb->max_col > screen_Columns)
1103 cb->max_col = screen_Columns;
Bram Moolenaar13b11ed2019-08-01 15:52:45 +02001104 cb->min_row = wp->w_winrow + wp->w_popup_border[0];
1105 cb->max_row = wp->w_winrow + popup_height(wp) - 1
1106 - wp->w_popup_border[2];
1107 }
1108 else
1109 {
1110 cb->min_col = 0;
1111 cb->max_col = screen_Columns;
1112 cb->min_row = 0;
1113 cb->max_row = screen_Rows;
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001114 }
1115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116
1117 if (repeated_click)
1118 {
1119 if (++cb->mode > SELECT_MODE_LINE)
1120 cb->mode = SELECT_MODE_CHAR;
1121 }
1122 else
1123 cb->mode = SELECT_MODE_CHAR;
1124
1125#ifdef FEAT_GUI
1126 /* clear the cursor until the selection is made */
1127 if (gui.in_use)
1128 gui_undraw_cursor();
1129#endif
1130
1131 switch (cb->mode)
1132 {
1133 case SELECT_MODE_CHAR:
1134 cb->origin_start_col = cb->start.col;
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001135 cb->word_end_col = clip_get_line_end(cb, (int)cb->start.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 break;
1137
1138 case SELECT_MODE_WORD:
1139 clip_get_word_boundaries(cb, (int)cb->start.lnum, cb->start.col);
1140 cb->origin_start_col = cb->word_start_col;
1141 cb->origin_end_col = cb->word_end_col;
1142
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001143 clip_invert_area(cb, (int)cb->start.lnum, cb->word_start_col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 (int)cb->end.lnum, cb->word_end_col, CLIP_SET);
1145 cb->start.col = cb->word_start_col;
1146 cb->end.col = cb->word_end_col;
1147 break;
1148
1149 case SELECT_MODE_LINE:
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001150 clip_invert_area(cb, (int)cb->start.lnum, 0, (int)cb->start.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 (int)Columns, CLIP_SET);
1152 cb->start.col = 0;
1153 cb->end.col = Columns;
1154 break;
1155 }
1156
1157 cb->prev = cb->start;
1158
1159#ifdef DEBUG_SELECTION
1160 printf("Selection started at (%u,%u)\n", cb->start.lnum, cb->start.col);
1161#endif
1162}
1163
1164/*
1165 * Continue processing the selection
1166 */
1167 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001168clip_process_selection(
1169 int button,
1170 int col,
1171 int row,
1172 int_u repeated_click)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173{
Bram Moolenaar0554fa42019-06-14 21:36:54 +02001174 Clipboard_T *cb = &clip_star;
1175 int diff;
1176 int slen = 1; // cursor shape width
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177
1178 if (button == MOUSE_RELEASE)
1179 {
1180 /* Check to make sure we have something selected */
1181 if (cb->start.lnum == cb->end.lnum && cb->start.col == cb->end.col)
1182 {
1183#ifdef FEAT_GUI
1184 if (gui.in_use)
1185 gui_update_cursor(FALSE, FALSE);
1186#endif
1187 cb->state = SELECT_CLEARED;
1188 return;
1189 }
1190
1191#ifdef DEBUG_SELECTION
1192 printf("Selection ended: (%u,%u) to (%u,%u)\n", cb->start.lnum,
1193 cb->start.col, cb->end.lnum, cb->end.col);
1194#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001195 if (clip_isautosel_star()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 || (
1197#ifdef FEAT_GUI
1198 gui.in_use ? (vim_strchr(p_go, GO_ASELML) != NULL) :
1199#endif
1200 clip_autoselectml))
1201 clip_copy_modeless_selection(FALSE);
1202#ifdef FEAT_GUI
1203 if (gui.in_use)
1204 gui_update_cursor(FALSE, FALSE);
1205#endif
1206
1207 cb->state = SELECT_DONE;
1208 return;
1209 }
1210
1211 row = check_row(row);
1212 col = check_col(col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 col = mb_fix_col(col, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214
1215 if (col == (int)cb->prev.col && row == cb->prev.lnum && !repeated_click)
1216 return;
1217
1218 /*
1219 * When extending the selection with the right mouse button, swap the
1220 * start and end if the position is before half the selection
1221 */
1222 if (cb->state == SELECT_DONE && button == MOUSE_RIGHT)
1223 {
1224 /*
1225 * If the click is before the start, or the click is inside the
1226 * selection and the start is the closest side, set the origin to the
1227 * end of the selection.
1228 */
1229 if (clip_compare_pos(row, col, (int)cb->start.lnum, cb->start.col) < 0
1230 || (clip_compare_pos(row, col,
1231 (int)cb->end.lnum, cb->end.col) < 0
1232 && (((cb->start.lnum == cb->end.lnum
1233 && cb->end.col - col > col - cb->start.col))
1234 || ((diff = (cb->end.lnum - row) -
1235 (row - cb->start.lnum)) > 0
1236 || (diff == 0 && col < (int)(cb->start.col +
1237 cb->end.col) / 2)))))
1238 {
1239 cb->origin_row = (short_u)cb->end.lnum;
1240 cb->origin_start_col = cb->end.col - 1;
1241 cb->origin_end_col = cb->end.col;
1242 }
1243 else
1244 {
1245 cb->origin_row = (short_u)cb->start.lnum;
1246 cb->origin_start_col = cb->start.col;
1247 cb->origin_end_col = cb->start.col;
1248 }
1249 if (cb->mode == SELECT_MODE_WORD && !repeated_click)
1250 cb->mode = SELECT_MODE_CHAR;
1251 }
1252
1253 /* set state, for when using the right mouse button */
1254 cb->state = SELECT_IN_PROGRESS;
1255
1256#ifdef DEBUG_SELECTION
1257 printf("Selection extending to (%d,%d)\n", row, col);
1258#endif
1259
1260 if (repeated_click && ++cb->mode > SELECT_MODE_LINE)
1261 cb->mode = SELECT_MODE_CHAR;
1262
1263 switch (cb->mode)
1264 {
1265 case SELECT_MODE_CHAR:
1266 /* If we're on a different line, find where the line ends */
1267 if (row != cb->prev.lnum)
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001268 cb->word_end_col = clip_get_line_end(cb, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269
1270 /* See if we are before or after the origin of the selection */
1271 if (clip_compare_pos(row, col, cb->origin_row,
1272 cb->origin_start_col) >= 0)
1273 {
1274 if (col >= (int)cb->word_end_col)
1275 clip_update_modeless_selection(cb, cb->origin_row,
1276 cb->origin_start_col, row, (int)Columns);
1277 else
1278 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 if (has_mbyte && mb_lefthalve(row, col))
1280 slen = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 clip_update_modeless_selection(cb, cb->origin_row,
1282 cb->origin_start_col, row, col + slen);
1283 }
1284 }
1285 else
1286 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 if (has_mbyte
1288 && mb_lefthalve(cb->origin_row, cb->origin_start_col))
1289 slen = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 if (col >= (int)cb->word_end_col)
1291 clip_update_modeless_selection(cb, row, cb->word_end_col,
1292 cb->origin_row, cb->origin_start_col + slen);
1293 else
1294 clip_update_modeless_selection(cb, row, col,
1295 cb->origin_row, cb->origin_start_col + slen);
1296 }
1297 break;
1298
1299 case SELECT_MODE_WORD:
1300 /* If we are still within the same word, do nothing */
1301 if (row == cb->prev.lnum && col >= (int)cb->word_start_col
1302 && col < (int)cb->word_end_col && !repeated_click)
1303 return;
1304
1305 /* Get new word boundaries */
1306 clip_get_word_boundaries(cb, row, col);
1307
1308 /* Handle being after the origin point of selection */
1309 if (clip_compare_pos(row, col, cb->origin_row,
1310 cb->origin_start_col) >= 0)
1311 clip_update_modeless_selection(cb, cb->origin_row,
1312 cb->origin_start_col, row, cb->word_end_col);
1313 else
1314 clip_update_modeless_selection(cb, row, cb->word_start_col,
1315 cb->origin_row, cb->origin_end_col);
1316 break;
1317
1318 case SELECT_MODE_LINE:
1319 if (row == cb->prev.lnum && !repeated_click)
1320 return;
1321
1322 if (clip_compare_pos(row, col, cb->origin_row,
1323 cb->origin_start_col) >= 0)
1324 clip_update_modeless_selection(cb, cb->origin_row, 0, row,
1325 (int)Columns);
1326 else
1327 clip_update_modeless_selection(cb, row, 0, cb->origin_row,
1328 (int)Columns);
1329 break;
1330 }
1331
1332 cb->prev.lnum = row;
1333 cb->prev.col = col;
1334
1335#ifdef DEBUG_SELECTION
1336 printf("Selection is: (%u,%u) to (%u,%u)\n", cb->start.lnum,
1337 cb->start.col, cb->end.lnum, cb->end.col);
1338#endif
1339}
1340
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341# if defined(FEAT_GUI) || defined(PROTO)
1342/*
1343 * Redraw part of the selection if character at "row,col" is inside of it.
1344 * Only used for the GUI.
1345 */
1346 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001347clip_may_redraw_selection(int row, int col, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
1349 int start = col;
1350 int end = col + len;
1351
1352 if (clip_star.state != SELECT_CLEARED
1353 && row >= clip_star.start.lnum
1354 && row <= clip_star.end.lnum)
1355 {
1356 if (row == clip_star.start.lnum && start < (int)clip_star.start.col)
1357 start = clip_star.start.col;
1358 if (row == clip_star.end.lnum && end > (int)clip_star.end.col)
1359 end = clip_star.end.col;
1360 if (end > start)
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001361 clip_invert_area(&clip_star, row, start, row, end, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 }
1363}
1364# endif
1365
1366/*
1367 * Called from outside to clear selected region from the display
1368 */
1369 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02001370clip_clear_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001373 if (cbd->state == SELECT_CLEARED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 return;
1375
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001376 clip_invert_area(cbd, (int)cbd->start.lnum, cbd->start.col,
1377 (int)cbd->end.lnum, cbd->end.col, CLIP_CLEAR);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001378 cbd->state = SELECT_CLEARED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379}
1380
1381/*
1382 * Clear the selection if any lines from "row1" to "row2" are inside of it.
1383 */
1384 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001385clip_may_clear_selection(int row1, int row2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386{
1387 if (clip_star.state == SELECT_DONE
1388 && row2 >= clip_star.start.lnum
1389 && row1 <= clip_star.end.lnum)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001390 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391}
1392
1393/*
1394 * Called before the screen is scrolled up or down. Adjusts the line numbers
1395 * of the selection. Call with big number when clearing the screen.
1396 */
1397 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001398clip_scroll_selection(
1399 int rows) /* negative for scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400{
1401 int lnum;
1402
1403 if (clip_star.state == SELECT_CLEARED)
1404 return;
1405
1406 lnum = clip_star.start.lnum - rows;
1407 if (lnum <= 0)
1408 clip_star.start.lnum = 0;
1409 else if (lnum >= screen_Rows) /* scrolled off of the screen */
1410 clip_star.state = SELECT_CLEARED;
1411 else
1412 clip_star.start.lnum = lnum;
1413
1414 lnum = clip_star.end.lnum - rows;
1415 if (lnum < 0) /* scrolled off of the screen */
1416 clip_star.state = SELECT_CLEARED;
1417 else if (lnum >= screen_Rows)
1418 clip_star.end.lnum = screen_Rows - 1;
1419 else
1420 clip_star.end.lnum = lnum;
1421}
1422
1423/*
1424 * Invert a region of the display between a starting and ending row and column
1425 * Values for "how":
1426 * CLIP_CLEAR: undo inversion
1427 * CLIP_SET: set inversion
1428 * CLIP_TOGGLE: set inversion if pos1 < pos2, undo inversion otherwise.
1429 * 0: invert (GUI only).
1430 */
1431 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001432clip_invert_area(
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001433 Clipboard_T *cbd,
1434 int row1,
1435 int col1,
1436 int row2,
1437 int col2,
1438 int how)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439{
1440 int invert = FALSE;
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001441 int max_col;
1442
1443#ifdef FEAT_TEXT_PROP
Bram Moolenaarff9f27c2019-08-17 16:15:53 +02001444 max_col = cbd->max_col - 1;
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001445#else
1446 max_col = Columns - 1;
1447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448
1449 if (how == CLIP_SET)
1450 invert = TRUE;
1451
1452 /* Swap the from and to positions so the from is always before */
1453 if (clip_compare_pos(row1, col1, row2, col2) > 0)
1454 {
1455 int tmp_row, tmp_col;
1456
1457 tmp_row = row1;
1458 tmp_col = col1;
1459 row1 = row2;
1460 col1 = col2;
1461 row2 = tmp_row;
1462 col2 = tmp_col;
1463 }
1464 else if (how == CLIP_TOGGLE)
1465 invert = TRUE;
1466
1467 /* If all on the same line, do it the easy way */
1468 if (row1 == row2)
1469 {
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001470 clip_invert_rectangle(cbd, row1, col1, 1, col2 - col1, invert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 }
1472 else
1473 {
1474 /* Handle a piece of the first line */
1475 if (col1 > 0)
1476 {
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001477 clip_invert_rectangle(cbd, row1, col1, 1,
1478 (int)Columns - col1, invert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 row1++;
1480 }
1481
1482 /* Handle a piece of the last line */
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001483 if (col2 < max_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 {
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001485 clip_invert_rectangle(cbd, row2, 0, 1, col2, invert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 row2--;
1487 }
1488
1489 /* Handle the rectangle thats left */
1490 if (row2 >= row1)
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001491 clip_invert_rectangle(cbd, row1, 0, row2 - row1 + 1,
1492 (int)Columns, invert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 }
1494}
1495
1496/*
1497 * Invert or un-invert a rectangle of the screen.
1498 * "invert" is true if the result is inverted.
1499 */
1500 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001501clip_invert_rectangle(
Bram Moolenaar405bb422019-06-21 00:12:29 +02001502 Clipboard_T *cbd UNUSED,
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001503 int row_arg,
1504 int col_arg,
1505 int height_arg,
1506 int width_arg,
1507 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508{
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001509 int row = row_arg;
1510 int col = col_arg;
1511 int height = height_arg;
1512 int width = width_arg;
1513
Bram Moolenaar451d4b52019-06-12 20:22:27 +02001514#ifdef FEAT_TEXT_PROP
1515 // this goes on top of all popup windows
Bram Moolenaarc662ec92019-06-23 00:15:57 +02001516 screen_zindex = CLIP_ZINDEX;
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001517
1518 if (col < cbd->min_col)
1519 {
1520 width -= cbd->min_col - col;
1521 col = cbd->min_col;
1522 }
Bram Moolenaarff9f27c2019-08-17 16:15:53 +02001523 if (width > cbd->max_col - col)
1524 width = cbd->max_col - col;
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001525 if (row < cbd->min_row)
1526 {
1527 height -= cbd->min_row - row;
1528 row = cbd->min_row;
1529 }
1530 if (height > cbd->max_row - row + 1)
1531 height = cbd->max_row - row + 1;
Bram Moolenaar451d4b52019-06-12 20:22:27 +02001532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533#ifdef FEAT_GUI
1534 if (gui.in_use)
1535 gui_mch_invert_rectangle(row, col, height, width);
1536 else
1537#endif
1538 screen_draw_rectangle(row, col, height, width, invert);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02001539#ifdef FEAT_TEXT_PROP
1540 screen_zindex = 0;
1541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542}
1543
1544/*
1545 * Copy the currently selected area into the '*' register so it will be
1546 * available for pasting.
1547 * When "both" is TRUE also copy to the '+' register.
1548 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001550clip_copy_modeless_selection(int both UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551{
1552 char_u *buffer;
1553 char_u *bufp;
1554 int row;
1555 int start_col;
1556 int end_col;
1557 int line_end_col;
1558 int add_newline_flag = FALSE;
1559 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 int row1 = clip_star.start.lnum;
1562 int col1 = clip_star.start.col;
1563 int row2 = clip_star.end.lnum;
1564 int col2 = clip_star.end.col;
1565
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001566 /* Can't use ScreenLines unless initialized */
1567 if (ScreenLines == NULL)
1568 return;
1569
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 /*
1571 * Make sure row1 <= row2, and if row1 == row2 that col1 <= col2.
1572 */
1573 if (row1 > row2)
1574 {
1575 row = row1; row1 = row2; row2 = row;
1576 row = col1; col1 = col2; col2 = row;
1577 }
1578 else if (row1 == row2 && col1 > col2)
1579 {
1580 row = col1; col1 = col2; col2 = row;
1581 }
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001582#ifdef FEAT_TEXT_PROP
1583 if (col1 < clip_star.min_col)
1584 col1 = clip_star.min_col;
Bram Moolenaarff9f27c2019-08-17 16:15:53 +02001585 if (col2 > clip_star.max_col)
1586 col2 = clip_star.max_col;
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001587 if (row1 < clip_star.min_row)
1588 row1 = clip_star.min_row;
1589 if (row2 > clip_star.max_row)
1590 row2 = clip_star.max_row;
1591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 /* correct starting point for being on right halve of double-wide char */
1593 p = ScreenLines + LineOffset[row1];
1594 if (enc_dbcs != 0)
1595 col1 -= (*mb_head_off)(p, p + col1);
1596 else if (enc_utf8 && p[col1] == 0)
1597 --col1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598
1599 /* Create a temporary buffer for storing the text */
1600 len = (row2 - row1 + 1) * Columns + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 if (enc_dbcs != 0)
1602 len *= 2; /* max. 2 bytes per display cell */
1603 else if (enc_utf8)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001604 len *= MB_MAXBYTES;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001605 buffer = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 if (buffer == NULL) /* out of memory */
1607 return;
1608
1609 /* Process each row in the selection */
1610 for (bufp = buffer, row = row1; row <= row2; row++)
1611 {
1612 if (row == row1)
1613 start_col = col1;
1614 else
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001615#ifdef FEAT_TEXT_PROP
1616 start_col = clip_star.min_col;
1617#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 start_col = 0;
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620
1621 if (row == row2)
1622 end_col = col2;
Bram Moolenaard5cf8982019-08-16 23:09:11 +02001623 else
Bram Moolenaarff9f27c2019-08-17 16:15:53 +02001624#ifdef FEAT_TEXT_PROP
1625 end_col = clip_star.max_col;
1626#else
Bram Moolenaard5cf8982019-08-16 23:09:11 +02001627 end_col = Columns;
Bram Moolenaarff9f27c2019-08-17 16:15:53 +02001628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001630 line_end_col = clip_get_line_end(&clip_star, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
1632 /* See if we need to nuke some trailing whitespace */
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001633 if (end_col >=
1634#ifdef FEAT_TEXT_PROP
Bram Moolenaarff9f27c2019-08-17 16:15:53 +02001635 clip_star.max_col
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001636#else
1637 Columns
1638#endif
1639 && (row < row2 || end_col > line_end_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 {
1641 /* Get rid of trailing whitespace */
1642 end_col = line_end_col;
1643 if (end_col < start_col)
1644 end_col = start_col;
1645
1646 /* If the last line extended to the end, add an extra newline */
1647 if (row == row2)
1648 add_newline_flag = TRUE;
1649 }
1650
1651 /* If after the first row, we need to always add a newline */
1652 if (row > row1 && !LineWraps[row - 1])
1653 *bufp++ = NL;
1654
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001655 // Safetey check for in case resizing went wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 if (row < screen_Rows && end_col <= screen_Columns)
1657 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 if (enc_dbcs != 0)
1659 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001660 int i;
1661
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 p = ScreenLines + LineOffset[row];
1663 for (i = start_col; i < end_col; ++i)
1664 if (enc_dbcs == DBCS_JPNU && p[i] == 0x8e)
1665 {
1666 /* single-width double-byte char */
1667 *bufp++ = 0x8e;
1668 *bufp++ = ScreenLines2[LineOffset[row] + i];
1669 }
1670 else
1671 {
1672 *bufp++ = p[i];
1673 if (MB_BYTE2LEN(p[i]) == 2)
1674 *bufp++ = p[++i];
1675 }
1676 }
1677 else if (enc_utf8)
1678 {
1679 int off;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001680 int i;
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001681 int ci;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
1683 off = LineOffset[row];
1684 for (i = start_col; i < end_col; ++i)
1685 {
1686 /* The base character is either in ScreenLinesUC[] or
1687 * ScreenLines[]. */
1688 if (ScreenLinesUC[off + i] == 0)
1689 *bufp++ = ScreenLines[off + i];
1690 else
1691 {
1692 bufp += utf_char2bytes(ScreenLinesUC[off + i], bufp);
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001693 for (ci = 0; ci < Screen_mco; ++ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001695 /* Add a composing character. */
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001696 if (ScreenLinesC[ci][off + i] == 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001697 break;
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001698 bufp += utf_char2bytes(ScreenLinesC[ci][off + i],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 }
1701 }
1702 /* Skip right halve of double-wide character. */
1703 if (ScreenLines[off + i + 1] == 0)
1704 ++i;
1705 }
1706 }
1707 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 {
1709 STRNCPY(bufp, ScreenLines + LineOffset[row] + start_col,
1710 end_col - start_col);
1711 bufp += end_col - start_col;
1712 }
1713 }
1714 }
1715
1716 /* Add a newline at the end if the selection ended there */
1717 if (add_newline_flag)
1718 *bufp++ = NL;
1719
1720 /* First cleanup any old selection and become the owner. */
1721 clip_free_selection(&clip_star);
1722 clip_own_selection(&clip_star);
1723
1724 /* Yank the text into the '*' register. */
1725 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_star);
1726
1727 /* Make the register contents available to the outside world. */
1728 clip_gen_set_selection(&clip_star);
1729
1730#ifdef FEAT_X11
1731 if (both)
1732 {
1733 /* Do the same for the '+' register. */
1734 clip_free_selection(&clip_plus);
1735 clip_own_selection(&clip_plus);
1736 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_plus);
1737 clip_gen_set_selection(&clip_plus);
1738 }
1739#endif
1740 vim_free(buffer);
1741}
1742
1743/*
1744 * Find the starting and ending positions of the word at the given row and
1745 * column. Only white-separated words are recognized here.
1746 */
1747#define CHAR_CLASS(c) (c <= ' ' ? ' ' : vim_iswordc(c))
1748
1749 static void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02001750clip_get_word_boundaries(Clipboard_T *cb, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751{
1752 int start_class;
1753 int temp_col;
1754 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 int mboff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001757 if (row >= screen_Rows || col >= screen_Columns || ScreenLines == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 return;
1759
1760 p = ScreenLines + LineOffset[row];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 /* Correct for starting in the right halve of a double-wide char */
1762 if (enc_dbcs != 0)
1763 col -= dbcs_screen_head_off(p, p + col);
1764 else if (enc_utf8 && p[col] == 0)
1765 --col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 start_class = CHAR_CLASS(p[col]);
1767
1768 temp_col = col;
1769 for ( ; temp_col > 0; temp_col--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 if (enc_dbcs != 0
1771 && (mboff = dbcs_screen_head_off(p, p + temp_col - 1)) > 0)
1772 temp_col -= mboff;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01001773 else if (CHAR_CLASS(p[temp_col - 1]) != start_class
1774 && !(enc_utf8 && p[temp_col - 1] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 break;
1776 cb->word_start_col = temp_col;
1777
1778 temp_col = col;
1779 for ( ; temp_col < screen_Columns; temp_col++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 if (enc_dbcs != 0 && dbcs_ptr2cells(p + temp_col) == 2)
1781 ++temp_col;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01001782 else if (CHAR_CLASS(p[temp_col]) != start_class
1783 && !(enc_utf8 && p[temp_col] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 break;
1785 cb->word_end_col = temp_col;
1786}
1787
1788/*
1789 * Find the column position for the last non-whitespace character on the given
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001790 * line at or before start_col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 */
1792 static int
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001793clip_get_line_end(Clipboard_T *cbd UNUSED, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794{
1795 int i;
1796
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001797 if (row >= screen_Rows || ScreenLines == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 return 0;
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001799 for (i =
1800#ifdef FEAT_TEXT_PROP
Bram Moolenaarff9f27c2019-08-17 16:15:53 +02001801 cbd->max_col;
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001802#else
1803 screen_Columns;
1804#endif
1805 i > 0; i--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 if (ScreenLines[LineOffset[row] + i - 1] != ' ')
1807 break;
1808 return i;
1809}
1810
1811/*
1812 * Update the currently selected region by adding and/or subtracting from the
1813 * beginning or end and inverting the changed area(s).
1814 */
1815 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001816clip_update_modeless_selection(
Bram Moolenaar0554fa42019-06-14 21:36:54 +02001817 Clipboard_T *cb,
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001818 int row1,
1819 int col1,
1820 int row2,
1821 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822{
1823 /* See if we changed at the beginning of the selection */
1824 if (row1 != cb->start.lnum || col1 != (int)cb->start.col)
1825 {
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001826 clip_invert_area(cb, row1, col1, (int)cb->start.lnum, cb->start.col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 CLIP_TOGGLE);
1828 cb->start.lnum = row1;
1829 cb->start.col = col1;
1830 }
1831
1832 /* See if we changed at the end of the selection */
1833 if (row2 != cb->end.lnum || col2 != (int)cb->end.col)
1834 {
Bram Moolenaarbd75b532019-06-14 23:41:55 +02001835 clip_invert_area(cb, (int)cb->end.lnum, cb->end.col, row2, col2,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 CLIP_TOGGLE);
1837 cb->end.lnum = row2;
1838 cb->end.col = col2;
1839 }
1840}
1841
1842 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02001843clip_gen_own_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844{
1845#ifdef FEAT_XCLIPBOARD
1846# ifdef FEAT_GUI
1847 if (gui.in_use)
1848 return clip_mch_own_selection(cbd);
1849 else
1850# endif
1851 return clip_xterm_own_selection(cbd);
1852#else
1853 return clip_mch_own_selection(cbd);
1854#endif
1855}
1856
1857 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02001858clip_gen_lose_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859{
1860#ifdef FEAT_XCLIPBOARD
1861# ifdef FEAT_GUI
1862 if (gui.in_use)
1863 clip_mch_lose_selection(cbd);
1864 else
1865# endif
1866 clip_xterm_lose_selection(cbd);
1867#else
1868 clip_mch_lose_selection(cbd);
1869#endif
1870}
1871
1872 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02001873clip_gen_set_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874{
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001875 if (!clip_did_set_selection)
1876 {
1877 /* Updating postponed, so that accessing the system clipboard won't
Bram Moolenaarbdace832019-03-02 10:13:42 +01001878 * hang Vim when accessing it many times (e.g. on a :g command). */
Bram Moolenaar5c27fd12015-01-27 14:09:37 +01001879 if ((cbd == &clip_plus && (clip_unnamed_saved & CLIP_UNNAMED_PLUS))
1880 || (cbd == &clip_star && (clip_unnamed_saved & CLIP_UNNAMED)))
1881 {
1882 clipboard_needs_update = TRUE;
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001883 return;
Bram Moolenaar5c27fd12015-01-27 14:09:37 +01001884 }
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886#ifdef FEAT_XCLIPBOARD
1887# ifdef FEAT_GUI
1888 if (gui.in_use)
1889 clip_mch_set_selection(cbd);
1890 else
1891# endif
1892 clip_xterm_set_selection(cbd);
1893#else
1894 clip_mch_set_selection(cbd);
1895#endif
1896}
1897
1898 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02001899clip_gen_request_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900{
1901#ifdef FEAT_XCLIPBOARD
1902# ifdef FEAT_GUI
1903 if (gui.in_use)
1904 clip_mch_request_selection(cbd);
1905 else
1906# endif
1907 clip_xterm_request_selection(cbd);
1908#else
1909 clip_mch_request_selection(cbd);
1910#endif
1911}
1912
Bram Moolenaar113e1072019-01-20 15:30:40 +01001913#if (defined(FEAT_X11) && defined(USE_SYSTEM)) || defined(PROTO)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001914 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02001915clip_gen_owner_exists(Clipboard_T *cbd UNUSED)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001916{
1917#ifdef FEAT_XCLIPBOARD
1918# ifdef FEAT_GUI_GTK
1919 if (gui.in_use)
1920 return clip_gtk_owner_exists(cbd);
1921 else
1922# endif
1923 return clip_x11_owner_exists(cbd);
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02001924#else
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001925 return TRUE;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02001926#endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001927}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001928#endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001929
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930#endif /* FEAT_CLIPBOARD */
1931
1932/*****************************************************************************
1933 * Functions that handle the input buffer.
1934 * This is used for any GUI version, and the unix terminal version.
1935 *
1936 * For Unix, the input characters are buffered to be able to check for a
1937 * CTRL-C. This should be done with signals, but I don't know how to do that
1938 * in a portable way for a tty in RAW mode.
1939 *
1940 * For the client-server code in the console the received keys are put in the
1941 * input buffer.
1942 */
1943
1944#if defined(USE_INPUT_BUF) || defined(PROTO)
1945
1946/*
1947 * Internal typeahead buffer. Includes extra space for long key code
1948 * descriptions which would otherwise overflow. The buffer is considered full
1949 * when only this extra space (or part of it) remains.
1950 */
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001951#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_CLIENTSERVER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 /*
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001953 * NetBeans stuffs debugger commands into the input buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 * This requires a larger buffer...
1955 * (Madsen) Go with this for remote input as well ...
1956 */
1957# define INBUFLEN 4096
1958#else
1959# define INBUFLEN 250
1960#endif
1961
1962static char_u inbuf[INBUFLEN + MAX_KEY_CODE_LEN];
1963static int inbufcount = 0; /* number of chars in inbuf[] */
1964
1965/*
1966 * vim_is_input_buf_full(), vim_is_input_buf_empty(), add_to_input_buf(), and
1967 * trash_input_buf() are functions for manipulating the input buffer. These
1968 * are used by the gui_* calls when a GUI is used to handle keyboard input.
1969 */
1970
1971 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001972vim_is_input_buf_full(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973{
1974 return (inbufcount >= INBUFLEN);
1975}
1976
1977 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001978vim_is_input_buf_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979{
1980 return (inbufcount == 0);
1981}
1982
1983#if defined(FEAT_OLE) || defined(PROTO)
1984 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001985vim_free_in_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986{
1987 return (INBUFLEN - inbufcount);
1988}
1989#endif
1990
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001991#if defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001993vim_used_in_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994{
1995 return inbufcount;
1996}
1997#endif
1998
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999/*
2000 * Return the current contents of the input buffer and make it empty.
2001 * The returned pointer must be passed to set_input_buf() later.
2002 */
2003 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002004get_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005{
2006 garray_T *gap;
2007
2008 /* We use a growarray to store the data pointer and the length. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002009 gap = ALLOC_ONE(garray_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 if (gap != NULL)
2011 {
2012 /* Add one to avoid a zero size. */
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02002013 gap->ga_data = alloc(inbufcount + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 if (gap->ga_data != NULL)
2015 mch_memmove(gap->ga_data, inbuf, (size_t)inbufcount);
2016 gap->ga_len = inbufcount;
2017 }
2018 trash_input_buf();
2019 return (char_u *)gap;
2020}
2021
2022/*
2023 * Restore the input buffer with a pointer returned from get_input_buf().
2024 * The allocated memory is freed, this only works once!
2025 */
2026 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002027set_input_buf(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028{
2029 garray_T *gap = (garray_T *)p;
2030
2031 if (gap != NULL)
2032 {
2033 if (gap->ga_data != NULL)
2034 {
2035 mch_memmove(inbuf, gap->ga_data, gap->ga_len);
2036 inbufcount = gap->ga_len;
2037 vim_free(gap->ga_data);
2038 }
2039 vim_free(gap);
2040 }
2041}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043/*
2044 * Add the given bytes to the input buffer
2045 * Special keys start with CSI. A real CSI must have been translated to
2046 * CSI KS_EXTRA KE_CSI. K_SPECIAL doesn't require translation.
2047 */
2048 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002049add_to_input_buf(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050{
2051 if (inbufcount + len > INBUFLEN + MAX_KEY_CODE_LEN)
2052 return; /* Shouldn't ever happen! */
2053
2054#ifdef FEAT_HANGULIN
2055 if ((State & (INSERT|CMDLINE)) && hangul_input_state_get())
2056 if ((len = hangul_input_process(s, len)) == 0)
2057 return;
2058#endif
2059
2060 while (len--)
2061 inbuf[inbufcount++] = *s++;
2062}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064/*
2065 * Add "str[len]" to the input buffer while escaping CSI bytes.
2066 */
2067 void
2068add_to_input_buf_csi(char_u *str, int len)
2069{
2070 int i;
2071 char_u buf[2];
2072
2073 for (i = 0; i < len; ++i)
2074 {
2075 add_to_input_buf(str + i, 1);
2076 if (str[i] == CSI)
2077 {
2078 /* Turn CSI into K_CSI. */
2079 buf[0] = KS_EXTRA;
2080 buf[1] = (int)KE_CSI;
2081 add_to_input_buf(buf, 2);
2082 }
2083 }
2084}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085
2086#if defined(FEAT_HANGULIN) || defined(PROTO)
2087 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002088push_raw_key(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089{
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01002090 char_u *tmpbuf;
Bram Moolenaar179f1b92016-03-04 22:52:34 +01002091 char_u *inp = s;
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01002092
Bram Moolenaar179f1b92016-03-04 22:52:34 +01002093 /* use the conversion result if possible */
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01002094 tmpbuf = hangul_string_convert(s, &len);
2095 if (tmpbuf != NULL)
Bram Moolenaar179f1b92016-03-04 22:52:34 +01002096 inp = tmpbuf;
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01002097
Bram Moolenaar179f1b92016-03-04 22:52:34 +01002098 for (; len--; inp++)
2099 {
2100 inbuf[inbufcount++] = *inp;
2101 if (*inp == CSI)
Bram Moolenaar00ded432016-03-03 11:45:15 +01002102 {
Bram Moolenaar179f1b92016-03-04 22:52:34 +01002103 /* Turn CSI into K_CSI. */
2104 inbuf[inbufcount++] = KS_EXTRA;
2105 inbuf[inbufcount++] = (int)KE_CSI;
Bram Moolenaar00ded432016-03-03 11:45:15 +01002106 }
Bram Moolenaar00ded432016-03-03 11:45:15 +01002107 }
Bram Moolenaar179f1b92016-03-04 22:52:34 +01002108 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109}
2110#endif
2111
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112/* Remove everything from the input buffer. Called when ^C is found */
2113 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002114trash_input_buf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115{
2116 inbufcount = 0;
2117}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118
2119/*
2120 * Read as much data from the input buffer as possible up to maxlen, and store
2121 * it in buf.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 */
2123 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002124read_from_input_buf(char_u *buf, long maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125{
2126 if (inbufcount == 0) /* if the buffer is empty, fill it */
2127 fill_input_buf(TRUE);
2128 if (maxlen > inbufcount)
2129 maxlen = inbufcount;
2130 mch_memmove(buf, inbuf, (size_t)maxlen);
2131 inbufcount -= maxlen;
2132 if (inbufcount)
2133 mch_memmove(inbuf, inbuf + maxlen, (size_t)inbufcount);
2134 return (int)maxlen;
2135}
2136
2137 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002138fill_input_buf(int exit_on_error UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139{
Bram Moolenaard0573012017-10-28 21:11:06 +02002140#if defined(UNIX) || defined(VMS) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 int len;
2142 int try;
2143 static int did_read_something = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 static char_u *rest = NULL; /* unconverted rest of previous read */
2145 static int restlen = 0;
2146 int unconverted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147#endif
2148
2149#ifdef FEAT_GUI
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002150 if (gui.in_use
2151# ifdef NO_CONSOLE_INPUT
2152 /* Don't use the GUI input when the window hasn't been opened yet.
2153 * We get here from ui_inchar() when we should try reading from stdin. */
2154 && !no_console_input()
2155# endif
2156 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 {
2158 gui_mch_update();
2159 return;
2160 }
2161#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02002162#if defined(UNIX) || defined(VMS) || defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 if (vim_is_input_buf_full())
2164 return;
2165 /*
2166 * Fill_input_buf() is only called when we really need a character.
2167 * If we can't get any, but there is some in the buffer, just return.
2168 * If we can't get any, and there isn't any in the buffer, we give up and
2169 * exit Vim.
2170 */
2171# ifdef __BEOS__
2172 /*
2173 * On the BeBox version (for now), all input is secretly performed within
2174 * beos_select() which is called from RealWaitForChar().
2175 */
2176 while (!vim_is_input_buf_full() && RealWaitForChar(read_cmd_fd, 0, NULL))
2177 ;
2178 len = inbufcount;
2179 inbufcount = 0;
2180# else
2181
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 if (rest != NULL)
2183 {
2184 /* Use remainder of previous call, starts with an invalid character
2185 * that may become valid when reading more. */
2186 if (restlen > INBUFLEN - inbufcount)
2187 unconverted = INBUFLEN - inbufcount;
2188 else
2189 unconverted = restlen;
2190 mch_memmove(inbuf + inbufcount, rest, unconverted);
2191 if (unconverted == restlen)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002192 VIM_CLEAR(rest);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 else
2194 {
2195 restlen -= unconverted;
2196 mch_memmove(rest, rest + unconverted, restlen);
2197 }
2198 inbufcount += unconverted;
2199 }
2200 else
2201 unconverted = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202
2203 len = 0; /* to avoid gcc warning */
2204 for (try = 0; try < 100; ++try)
2205 {
Bram Moolenaar4e601e32018-04-24 13:29:51 +02002206 size_t readlen = (size_t)((INBUFLEN - inbufcount)
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002207 / input_conv.vc_factor);
Bram Moolenaar4e601e32018-04-24 13:29:51 +02002208# ifdef VMS
Bram Moolenaar49943732018-04-24 20:27:26 +02002209 len = vms_read((char *)inbuf + inbufcount, readlen);
Bram Moolenaar4e601e32018-04-24 13:29:51 +02002210# else
2211 len = read(read_cmd_fd, (char *)inbuf + inbufcount, readlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212# endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002213
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 if (len > 0 || got_int)
2215 break;
2216 /*
2217 * If reading stdin results in an error, continue reading stderr.
2218 * This helps when using "foo | xargs vim".
2219 */
2220 if (!did_read_something && !isatty(read_cmd_fd) && read_cmd_fd == 0)
2221 {
2222 int m = cur_tmode;
2223
2224 /* We probably set the wrong file descriptor to raw mode. Switch
2225 * back to cooked mode, use another descriptor and set the mode to
2226 * what it was. */
2227 settmode(TMODE_COOK);
2228#ifdef HAVE_DUP
2229 /* Use stderr for stdin, also works for shell commands. */
2230 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002231 vim_ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232#else
2233 read_cmd_fd = 2; /* read from stderr instead of stdin */
2234#endif
2235 settmode(m);
2236 }
2237 if (!exit_on_error)
2238 return;
2239 }
2240# endif
2241 if (len <= 0 && !got_int)
2242 read_error_exit();
2243 if (len > 0)
2244 did_read_something = TRUE;
2245 if (got_int)
2246 {
2247 /* Interrupted, pretend a CTRL-C was typed. */
2248 inbuf[0] = 3;
2249 inbufcount = 1;
2250 }
2251 else
2252 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 /*
2254 * May perform conversion on the input characters.
2255 * Include the unconverted rest of the previous call.
2256 * If there is an incomplete char at the end it is kept for the next
2257 * time, reading more bytes should make conversion possible.
2258 * Don't do this in the unlikely event that the input buffer is too
2259 * small ("rest" still contains more bytes).
2260 */
2261 if (input_conv.vc_type != CONV_NONE)
2262 {
2263 inbufcount -= unconverted;
2264 len = convert_input_safe(inbuf + inbufcount,
2265 len + unconverted, INBUFLEN - inbufcount,
2266 rest == NULL ? &rest : NULL, &restlen);
2267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 while (len-- > 0)
2269 {
2270 /*
2271 * if a CTRL-C was typed, remove it from the buffer and set got_int
2272 */
2273 if (inbuf[inbufcount] == 3 && ctrl_c_interrupts)
2274 {
2275 /* remove everything typed before the CTRL-C */
2276 mch_memmove(inbuf, inbuf + inbufcount, (size_t)(len + 1));
2277 inbufcount = 0;
2278 got_int = TRUE;
2279 }
2280 ++inbufcount;
2281 }
2282 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002283#endif /* UNIX or VMS*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284}
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002285#endif /* defined(UNIX) || defined(FEAT_GUI) || defined(VMS) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286
2287/*
2288 * Exit because of an input read error.
2289 */
2290 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002291read_error_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292{
2293 if (silent_mode) /* Normal way to exit for "ex -s" */
2294 getout(0);
2295 STRCPY(IObuff, _("Vim: Error reading input, exiting...\n"));
2296 preserve_exit();
2297}
2298
2299#if defined(CURSOR_SHAPE) || defined(PROTO)
2300/*
2301 * May update the shape of the cursor.
2302 */
2303 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002304ui_cursor_shape_forced(int forced)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305{
2306# ifdef FEAT_GUI
2307 if (gui.in_use)
2308 gui_update_cursor_later();
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002309 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002311 term_cursor_mode(forced);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002312
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313# ifdef MCH_CURSOR_SHAPE
2314 mch_update_cursor();
2315# endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002316
2317# ifdef FEAT_CONCEAL
Bram Moolenaarb9464822018-05-10 15:09:49 +02002318 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002319# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002321
2322 void
2323ui_cursor_shape(void)
2324{
2325 ui_cursor_shape_forced(FALSE);
2326}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327#endif
2328
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329/*
2330 * Check bounds for column number
2331 */
2332 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002333check_col(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334{
2335 if (col < 0)
2336 return 0;
2337 if (col >= (int)screen_Columns)
2338 return (int)screen_Columns - 1;
2339 return col;
2340}
2341
2342/*
2343 * Check bounds for row number
2344 */
2345 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002346check_row(int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347{
2348 if (row < 0)
2349 return 0;
2350 if (row >= (int)screen_Rows)
2351 return (int)screen_Rows - 1;
2352 return row;
2353}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
2355/*
2356 * Stuff for the X clipboard. Shared between VMS and Unix.
2357 */
2358
2359#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) || defined(PROTO)
2360# include <X11/Xatom.h>
2361# include <X11/Intrinsic.h>
2362
2363/*
2364 * Open the application context (if it hasn't been opened yet).
2365 * Used for Motif and Athena GUI and the xterm clipboard.
2366 */
2367 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002368open_app_context(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369{
2370 if (app_context == NULL)
2371 {
2372 XtToolkitInitialize();
2373 app_context = XtCreateApplicationContext();
2374 }
2375}
2376
2377static Atom vim_atom; /* Vim's own special selection format */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378static Atom vimenc_atom; /* Vim's extended selection format */
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002379static Atom utf8_atom;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380static Atom compound_text_atom;
2381static Atom text_atom;
2382static Atom targets_atom;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002383static Atom timestamp_atom; /* Used to get a timestamp */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384
2385 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002386x11_setup_atoms(Display *dpy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387{
2388 vim_atom = XInternAtom(dpy, VIM_ATOM_NAME, False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 vimenc_atom = XInternAtom(dpy, VIMENC_ATOM_NAME,False);
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002390 utf8_atom = XInternAtom(dpy, "UTF8_STRING", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 compound_text_atom = XInternAtom(dpy, "COMPOUND_TEXT", False);
2392 text_atom = XInternAtom(dpy, "TEXT", False);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002393 targets_atom = XInternAtom(dpy, "TARGETS", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 clip_star.sel_atom = XA_PRIMARY;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002395 clip_plus.sel_atom = XInternAtom(dpy, "CLIPBOARD", False);
2396 timestamp_atom = XInternAtom(dpy, "TIMESTAMP", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397}
2398
2399/*
2400 * X Selection stuff, for cutting and pasting text to other windows.
2401 */
2402
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002403static Boolean clip_x11_convert_selection_cb(Widget w, Atom *sel_atom, Atom *target, Atom *type, XtPointer *value, long_u *length, int *format);
2404static void clip_x11_lose_ownership_cb(Widget w, Atom *sel_atom);
2405static void clip_x11_notify_cb(Widget w, Atom *sel_atom, Atom *target);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002406
2407/*
2408 * Property callback to get a timestamp for XtOwnSelection.
2409 */
2410 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002411clip_x11_timestamp_cb(
2412 Widget w,
2413 XtPointer n UNUSED,
2414 XEvent *event,
2415 Boolean *cont UNUSED)
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002416{
2417 Atom actual_type;
2418 int format;
2419 unsigned long nitems, bytes_after;
2420 unsigned char *prop=NULL;
2421 XPropertyEvent *xproperty=&event->xproperty;
2422
2423 /* Must be a property notify, state can't be Delete (True), has to be
2424 * one of the supported selection types. */
2425 if (event->type != PropertyNotify || xproperty->state
2426 || (xproperty->atom != clip_star.sel_atom
2427 && xproperty->atom != clip_plus.sel_atom))
2428 return;
2429
2430 if (XGetWindowProperty(xproperty->display, xproperty->window,
2431 xproperty->atom, 0, 0, False, timestamp_atom, &actual_type, &format,
2432 &nitems, &bytes_after, &prop))
2433 return;
2434
2435 if (prop)
2436 XFree(prop);
2437
2438 /* Make sure the property type is "TIMESTAMP" and it's 32 bits. */
2439 if (actual_type != timestamp_atom || format != 32)
2440 return;
2441
2442 /* Get the selection, using the event timestamp. */
Bram Moolenaar62b42182010-09-21 22:09:37 +02002443 if (XtOwnSelection(w, xproperty->atom, xproperty->time,
2444 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002445 clip_x11_notify_cb) == OK)
Bram Moolenaar62b42182010-09-21 22:09:37 +02002446 {
2447 /* Set the "owned" flag now, there may have been a call to
2448 * lose_ownership_cb in between. */
2449 if (xproperty->atom == clip_plus.sel_atom)
2450 clip_plus.owned = TRUE;
2451 else
2452 clip_star.owned = TRUE;
2453 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002454}
2455
2456 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002457x11_setup_selection(Widget w)
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002458{
2459 XtAddEventHandler(w, PropertyChangeMask, False,
2460 /*(XtEventHandler)*/clip_x11_timestamp_cb, (XtPointer)NULL);
2461}
2462
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002464clip_x11_request_selection_cb(
2465 Widget w UNUSED,
2466 XtPointer success,
2467 Atom *sel_atom,
2468 Atom *type,
2469 XtPointer value,
2470 long_u *length,
2471 int *format)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472{
Bram Moolenaard44347f2011-06-19 01:14:29 +02002473 int motion_type = MAUTO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 long_u len;
2475 char_u *p;
2476 char **text_list = NULL;
Bram Moolenaar0554fa42019-06-14 21:36:54 +02002477 Clipboard_T *cbd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
2480 if (*sel_atom == clip_plus.sel_atom)
2481 cbd = &clip_plus;
2482 else
2483 cbd = &clip_star;
2484
2485 if (value == NULL || *length == 0)
2486 {
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002487 clip_free_selection(cbd); /* nothing received, clear register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 *(int *)success = FALSE;
2489 return;
2490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 p = (char_u *)value;
2492 len = *length;
2493 if (*type == vim_atom)
2494 {
2495 motion_type = *p++;
2496 len--;
2497 }
2498
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 else if (*type == vimenc_atom)
2500 {
2501 char_u *enc;
2502 vimconv_T conv;
2503 int convlen;
2504
2505 motion_type = *p++;
2506 --len;
2507
2508 enc = p;
2509 p += STRLEN(p) + 1;
2510 len -= p - enc;
2511
2512 /* If the encoding of the text is different from 'encoding', attempt
2513 * converting it. */
2514 conv.vc_type = CONV_NONE;
2515 convert_setup(&conv, enc, p_enc);
2516 if (conv.vc_type != CONV_NONE)
2517 {
2518 convlen = len; /* Need to use an int here. */
2519 tmpbuf = string_convert(&conv, p, &convlen);
2520 len = convlen;
2521 if (tmpbuf != NULL)
2522 p = tmpbuf;
2523 convert_setup(&conv, NULL, NULL);
2524 }
2525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002527 else if (*type == compound_text_atom
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002528 || *type == utf8_atom
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002529 || (enc_dbcs != 0 && *type == text_atom))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 {
2531 XTextProperty text_prop;
2532 int n_text = 0;
2533 int status;
2534
2535 text_prop.value = (unsigned char *)value;
2536 text_prop.encoding = *type;
2537 text_prop.format = *format;
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002538 text_prop.nitems = len;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002539#if defined(X_HAVE_UTF8_STRING)
Bram Moolenaare2e663f2013-03-07 18:02:30 +01002540 if (*type == utf8_atom)
2541 status = Xutf8TextPropertyToTextList(X_DISPLAY, &text_prop,
2542 &text_list, &n_text);
2543 else
2544#endif
2545 status = XmbTextPropertyToTextList(X_DISPLAY, &text_prop,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 &text_list, &n_text);
2547 if (status != Success || n_text < 1)
2548 {
2549 *(int *)success = FALSE;
2550 return;
2551 }
2552 p = (char_u *)text_list[0];
2553 len = STRLEN(p);
2554 }
2555 clip_yank_selection(motion_type, p, (long)len, cbd);
2556
2557 if (text_list != NULL)
2558 XFreeStringList(text_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 XtFree((char *)value);
2561 *(int *)success = TRUE;
2562}
2563
2564 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002565clip_x11_request_selection(
2566 Widget myShell,
2567 Display *dpy,
Bram Moolenaar0554fa42019-06-14 21:36:54 +02002568 Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569{
2570 XEvent event;
2571 Atom type;
2572 static int success;
2573 int i;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002574 time_t start_time;
2575 int timed_out = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002577 for (i = 0; i < 6; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 {
2579 switch (i)
2580 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 case 0: type = vimenc_atom; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 case 1: type = vim_atom; break;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002583 case 2: type = utf8_atom; break;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002584 case 3: type = compound_text_atom; break;
2585 case 4: type = text_atom; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 default: type = XA_STRING;
2587 }
Bram Moolenaar81851112013-04-12 12:27:30 +02002588 if (type == utf8_atom
2589# if defined(X_HAVE_UTF8_STRING)
2590 && !enc_utf8
2591# endif
2592 )
2593 /* Only request utf-8 when 'encoding' is utf8 and
2594 * Xutf8TextPropertyToTextList is available. */
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002595 continue;
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002596 success = MAYBE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 XtGetSelectionValue(myShell, cbd->sel_atom, type,
2598 clip_x11_request_selection_cb, (XtPointer)&success, CurrentTime);
2599
2600 /* Make sure the request for the selection goes out before waiting for
2601 * a response. */
2602 XFlush(dpy);
2603
2604 /*
2605 * Wait for result of selection request, otherwise if we type more
2606 * characters, then they will appear before the one that requested the
2607 * paste! Don't worry, we will catch up with any other events later.
2608 */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002609 start_time = time(NULL);
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002610 while (success == MAYBE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002612 if (XCheckTypedEvent(dpy, PropertyNotify, &event)
2613 || XCheckTypedEvent(dpy, SelectionNotify, &event)
2614 || XCheckTypedEvent(dpy, SelectionRequest, &event))
Bram Moolenaar89417b92008-09-07 19:48:53 +00002615 {
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002616 /* This is where clip_x11_request_selection_cb() should be
2617 * called. It may actually happen a bit later, so we loop
2618 * until "success" changes.
2619 * We may get a SelectionRequest here and if we don't handle
2620 * it we hang. KDE klipper does this, for example.
2621 * We need to handle a PropertyNotify for large selections. */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002622 XtDispatchEvent(&event);
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002623 continue;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625
Bram Moolenaar89417b92008-09-07 19:48:53 +00002626 /* Time out after 2 to 3 seconds to avoid that we hang when the
2627 * other process doesn't respond. Note that the SelectionNotify
2628 * event may still come later when the selection owner comes back
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002629 * to life and the text gets inserted unexpectedly. Don't know
2630 * why that happens or how to avoid that :-(. */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002631 if (time(NULL) > start_time + 2)
2632 {
2633 timed_out = TRUE;
2634 break;
2635 }
2636
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 /* Do we need this? Probably not. */
2638 XSync(dpy, False);
2639
Bram Moolenaar89417b92008-09-07 19:48:53 +00002640 /* Wait for 1 msec to avoid that we eat up all CPU time. */
2641 ui_delay(1L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 }
2643
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002644 if (success == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 return;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002646
2647 /* don't do a retry with another type after timing out, otherwise we
2648 * hang for 15 seconds. */
2649 if (timed_out)
2650 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 }
2652
2653 /* Final fallback position - use the X CUT_BUFFER0 store */
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002654 yank_cut_buffer0(dpy, cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655}
2656
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 static Boolean
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002658clip_x11_convert_selection_cb(
2659 Widget w UNUSED,
2660 Atom *sel_atom,
2661 Atom *target,
2662 Atom *type,
2663 XtPointer *value,
2664 long_u *length,
2665 int *format)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666{
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002667 static char_u *save_result = NULL;
2668 static long_u save_length = 0;
2669 char_u *string;
2670 int motion_type;
Bram Moolenaar0554fa42019-06-14 21:36:54 +02002671 Clipboard_T *cbd;
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002672 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673
2674 if (*sel_atom == clip_plus.sel_atom)
2675 cbd = &clip_plus;
2676 else
2677 cbd = &clip_star;
2678
2679 if (!cbd->owned)
2680 return False; /* Shouldn't ever happen */
2681
2682 /* requestor wants to know what target types we support */
2683 if (*target == targets_atom)
2684 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002685 static Atom array[7];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 *value = (XtPointer)array;
2688 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 array[i++] = targets_atom;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 array[i++] = vimenc_atom;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 array[i++] = vim_atom;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002692 if (enc_utf8)
2693 array[i++] = utf8_atom;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002694 array[i++] = XA_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 array[i++] = text_atom;
2696 array[i++] = compound_text_atom;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002697
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 *type = XA_ATOM;
2699 /* This used to be: *format = sizeof(Atom) * 8; but that caused
2700 * crashes on 64 bit machines. (Peter Derr) */
2701 *format = 32;
2702 *length = i;
2703 return True;
2704 }
2705
2706 if ( *target != XA_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 && *target != vimenc_atom
Bram Moolenaar980e58f2014-06-12 13:28:30 +02002708 && (*target != utf8_atom || !enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 && *target != vim_atom
2710 && *target != text_atom
2711 && *target != compound_text_atom)
2712 return False;
2713
2714 clip_get_selection(cbd);
2715 motion_type = clip_convert_selection(&string, length, cbd);
2716 if (motion_type < 0)
2717 return False;
2718
2719 /* For our own format, the first byte contains the motion type */
2720 if (*target == vim_atom)
2721 (*length)++;
2722
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 /* Our own format with encoding: motion 'encoding' NUL text */
2724 if (*target == vimenc_atom)
2725 *length += STRLEN(p_enc) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002727 if (save_length < *length || save_length / 2 >= *length)
2728 *value = XtRealloc((char *)save_result, (Cardinal)*length + 1);
2729 else
2730 *value = save_result;
2731 if (*value == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 {
2733 vim_free(string);
2734 return False;
2735 }
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002736 save_result = (char_u *)*value;
2737 save_length = *length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002739 if (*target == XA_STRING || (*target == utf8_atom && enc_utf8))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002741 mch_memmove(save_result, string, (size_t)(*length));
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002742 *type = *target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 }
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002744 else if (*target == compound_text_atom || *target == text_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 {
2746 XTextProperty text_prop;
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002747 char *string_nt = (char *)save_result;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002748 int conv_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749
2750 /* create NUL terminated string which XmbTextListToTextProperty wants */
2751 mch_memmove(string_nt, string, (size_t)*length);
2752 string_nt[*length] = NUL;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002753 conv_result = XmbTextListToTextProperty(X_DISPLAY, (char **)&string_nt,
2754 1, XCompoundTextStyle, &text_prop);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002755 if (conv_result != Success)
2756 {
2757 vim_free(string);
2758 return False;
2759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 *value = (XtPointer)(text_prop.value); /* from plain text */
2761 *length = text_prop.nitems;
2762 *type = compound_text_atom;
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002763 XtFree((char *)save_result);
2764 save_result = (char_u *)*value;
2765 save_length = *length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 else if (*target == vimenc_atom)
2768 {
2769 int l = STRLEN(p_enc);
2770
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002771 save_result[0] = motion_type;
2772 STRCPY(save_result + 1, p_enc);
2773 mch_memmove(save_result + l + 2, string, (size_t)(*length - l - 2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 *type = vimenc_atom;
2775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 else
2777 {
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002778 save_result[0] = motion_type;
2779 mch_memmove(save_result + 1, string, (size_t)(*length - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 *type = vim_atom;
2781 }
2782 *format = 8; /* 8 bits per char */
2783 vim_free(string);
2784 return True;
2785}
2786
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002788clip_x11_lose_ownership_cb(Widget w UNUSED, Atom *sel_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789{
2790 if (*sel_atom == clip_plus.sel_atom)
2791 clip_lose_selection(&clip_plus);
2792 else
2793 clip_lose_selection(&clip_star);
2794}
2795
2796 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02002797clip_x11_lose_selection(Widget myShell, Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798{
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002799 XtDisownSelection(myShell, cbd->sel_atom,
2800 XtLastTimestampProcessed(XtDisplay(myShell)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801}
2802
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002803 static void
2804clip_x11_notify_cb(Widget w UNUSED, Atom *sel_atom UNUSED, Atom *target UNUSED)
2805{
2806 /* To prevent automatically freeing the selection value. */
2807}
2808
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02002810clip_x11_own_selection(Widget myShell, Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811{
Bram Moolenaar62b42182010-09-21 22:09:37 +02002812 /* When using the GUI we have proper timestamps, use the one of the last
2813 * event. When in the console we don't get events (the terminal gets
2814 * them), Get the time by a zero-length append, clip_x11_timestamp_cb will
2815 * be called with the current timestamp. */
2816#ifdef FEAT_GUI
2817 if (gui.in_use)
2818 {
2819 if (XtOwnSelection(myShell, cbd->sel_atom,
2820 XtLastTimestampProcessed(XtDisplay(myShell)),
2821 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
Bram Moolenaarcdb7e1b2017-07-19 19:55:58 +02002822 clip_x11_notify_cb) == False)
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +01002823 return FAIL;
Bram Moolenaar62b42182010-09-21 22:09:37 +02002824 }
2825 else
2826#endif
2827 {
2828 if (!XChangeProperty(XtDisplay(myShell), XtWindow(myShell),
2829 cbd->sel_atom, timestamp_atom, 32, PropModeAppend, NULL, 0))
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +01002830 return FAIL;
Bram Moolenaar62b42182010-09-21 22:09:37 +02002831 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002832 /* Flush is required in a terminal as nothing else is doing it. */
2833 XFlush(XtDisplay(myShell));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 return OK;
2835}
2836
2837/*
2838 * Send the current selection to the clipboard. Do nothing for X because we
2839 * will fill in the selection only when requested by another app.
2840 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02002842clip_x11_set_selection(Clipboard_T *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843{
2844}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002845
Bram Moolenaar113e1072019-01-20 15:30:40 +01002846#if (defined(FEAT_X11) && defined(FEAT_XCLIPBOARD) && defined(USE_SYSTEM)) \
2847 || defined(PROTO)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002848 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02002849clip_x11_owner_exists(Clipboard_T *cbd)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002850{
2851 return XGetSelectionOwner(X_DISPLAY, cbd->sel_atom) != None;
2852}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853#endif
Bram Moolenaar113e1072019-01-20 15:30:40 +01002854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002856#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) \
2857 || defined(FEAT_GUI_GTK) || defined(PROTO)
2858/*
2859 * Get the contents of the X CUT_BUFFER0 and put it in "cbd".
2860 */
2861 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02002862yank_cut_buffer0(Display *dpy, Clipboard_T *cbd)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002863{
2864 int nbytes = 0;
2865 char_u *buffer = (char_u *)XFetchBuffer(dpy, &nbytes, 0);
2866
2867 if (nbytes > 0)
2868 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002869 int done = FALSE;
2870
2871 /* CUT_BUFFER0 is supposed to be always latin1. Convert to 'enc' when
2872 * using a multi-byte encoding. Conversion between two 8-bit
2873 * character sets usually fails and the text might actually be in
2874 * 'enc' anyway. */
2875 if (has_mbyte)
2876 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002877 char_u *conv_buf;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002878 vimconv_T vc;
2879
2880 vc.vc_type = CONV_NONE;
2881 if (convert_setup(&vc, (char_u *)"latin1", p_enc) == OK)
2882 {
2883 conv_buf = string_convert(&vc, buffer, &nbytes);
2884 if (conv_buf != NULL)
2885 {
2886 clip_yank_selection(MCHAR, conv_buf, (long)nbytes, cbd);
2887 vim_free(conv_buf);
2888 done = TRUE;
2889 }
2890 convert_setup(&vc, NULL, NULL);
2891 }
2892 }
2893 if (!done) /* use the text without conversion */
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002894 clip_yank_selection(MCHAR, buffer, (long)nbytes, cbd);
2895 XFree((void *)buffer);
2896 if (p_verbose > 0)
2897 {
2898 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01002899 verb_msg(_("Used CUT_BUFFER0 instead of empty selection"));
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002900 verbose_leave();
2901 }
2902 }
2903}
2904#endif
2905
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906#if defined(FEAT_MOUSE) || defined(PROTO)
2907
2908/*
2909 * Move the cursor to the specified row and column on the screen.
Bram Moolenaar49325942007-05-10 19:19:59 +00002910 * Change current window if necessary. Returns an integer with the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 * CURSOR_MOVED bit set if the cursor has moved or unset otherwise.
2912 *
2913 * The MOUSE_FOLD_CLOSE bit is set when clicked on the '-' in a fold column.
2914 * The MOUSE_FOLD_OPEN bit is set when clicked on the '+' in a fold column.
2915 *
2916 * If flags has MOUSE_FOCUS, then the current window will not be changed, and
2917 * if the mouse is outside the window then the text will scroll, or if the
2918 * mouse was previously on a status line, then the status line may be dragged.
2919 *
2920 * If flags has MOUSE_MAY_VIS, then VIsual mode will be started before the
2921 * cursor is moved unless the cursor was on a status line.
2922 * This function returns one of IN_UNKNOWN, IN_BUFFER, IN_STATUS_LINE or
2923 * IN_SEP_LINE depending on where the cursor was clicked.
2924 *
2925 * If flags has MOUSE_MAY_STOP_VIS, then Visual mode will be stopped, unless
2926 * the mouse is on the status line of the same window.
2927 *
2928 * If flags has MOUSE_DID_MOVE, nothing is done if the mouse didn't move since
2929 * the last call.
2930 *
2931 * If flags has MOUSE_SETPOS, nothing is done, only the current position is
2932 * remembered.
2933 */
2934 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002935jump_to_mouse(
2936 int flags,
2937 int *inclusive, /* used for inclusive operator, can be NULL */
2938 int which_button) /* MOUSE_LEFT, MOUSE_RIGHT, MOUSE_MIDDLE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939{
2940 static int on_status_line = 0; /* #lines below bottom of window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 static int on_sep_line = 0; /* on separator right of window */
Bram Moolenaareb163d72017-09-23 15:08:17 +02002942#ifdef FEAT_MENU
2943 static int in_winbar = FALSE;
2944#endif
Bram Moolenaar451d4b52019-06-12 20:22:27 +02002945#ifdef FEAT_TEXT_PROP
Bram Moolenaarb53fb312019-06-13 23:59:52 +02002946 static int in_popup_win = FALSE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002947 static win_T *click_in_popup_win = NULL;
Bram Moolenaar451d4b52019-06-12 20:22:27 +02002948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 static int prev_row = -1;
2950 static int prev_col = -1;
2951 static win_T *dragwin = NULL; /* window being dragged */
2952 static int did_drag = FALSE; /* drag was noticed */
2953
2954 win_T *wp, *old_curwin;
2955 pos_T old_cursor;
2956 int count;
2957 int first;
2958 int row = mouse_row;
2959 int col = mouse_col;
2960#ifdef FEAT_FOLDING
2961 int mouse_char;
2962#endif
2963
2964 mouse_past_bottom = FALSE;
2965 mouse_past_eol = FALSE;
2966
2967 if (flags & MOUSE_RELEASED)
2968 {
2969 /* On button release we may change window focus if positioned on a
2970 * status line and no dragging happened. */
2971 if (dragwin != NULL && !did_drag)
2972 flags &= ~(MOUSE_FOCUS | MOUSE_DID_MOVE);
2973 dragwin = NULL;
2974 did_drag = FALSE;
Bram Moolenaarb53fb312019-06-13 23:59:52 +02002975#ifdef FEAT_TEXT_PROP
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002976 if (click_in_popup_win != NULL && popup_dragwin == NULL)
2977 popup_close_for_mouse_click(click_in_popup_win);
2978
Bram Moolenaarb53fb312019-06-13 23:59:52 +02002979 popup_dragwin = NULL;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02002980 click_in_popup_win = NULL;
Bram Moolenaarb53fb312019-06-13 23:59:52 +02002981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 }
2983
2984 if ((flags & MOUSE_DID_MOVE)
2985 && prev_row == mouse_row
2986 && prev_col == mouse_col)
2987 {
2988retnomove:
Bram Moolenaar49325942007-05-10 19:19:59 +00002989 /* before moving the cursor for a left click which is NOT in a status
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 * line, stop Visual mode */
2991 if (on_status_line)
2992 return IN_STATUS_LINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 if (on_sep_line)
2994 return IN_SEP_LINE;
Bram Moolenaard327b0c2017-11-12 16:56:12 +01002995#ifdef FEAT_MENU
2996 if (in_winbar)
2997 {
2998 /* A quick second click may arrive as a double-click, but we use it
2999 * as a second click in the WinBar. */
3000 if ((mod_mask & MOD_MASK_MULTI_CLICK) && !(flags & MOUSE_RELEASED))
3001 {
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003002 wp = mouse_find_win(&row, &col, FAIL_POPUP);
Bram Moolenaard327b0c2017-11-12 16:56:12 +01003003 if (wp == NULL)
3004 return IN_UNKNOWN;
3005 winbar_click(wp, col);
3006 }
3007 return IN_OTHER_WIN | MOUSE_WINBAR;
3008 }
3009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 if (flags & MOUSE_MAY_STOP_VIS)
3011 {
3012 end_visual_mode();
3013 redraw_curbuf_later(INVERTED); /* delete the inversion */
3014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003016 // Continue a modeless selection in another window.
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003017 if (cmdwin_type != 0 && row < curwin->w_winrow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 return IN_OTHER_WIN;
3019#endif
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003020#ifdef FEAT_TEXT_PROP
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02003021 // Continue a modeless selection in a popup window or dragging it.
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003022 if (in_popup_win)
Bram Moolenaarb53fb312019-06-13 23:59:52 +02003023 {
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003024 click_in_popup_win = NULL; // don't close it on release
Bram Moolenaarb53fb312019-06-13 23:59:52 +02003025 if (popup_dragwin != NULL)
3026 {
3027 // dragging a popup window
3028 popup_drag(popup_dragwin);
3029 return IN_UNKNOWN;
3030 }
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003031 return IN_OTHER_WIN;
Bram Moolenaarb53fb312019-06-13 23:59:52 +02003032 }
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 return IN_BUFFER;
3035 }
3036
3037 prev_row = mouse_row;
3038 prev_col = mouse_col;
3039
3040 if (flags & MOUSE_SETPOS)
3041 goto retnomove; /* ugly goto... */
3042
3043#ifdef FEAT_FOLDING
3044 /* Remember the character under the mouse, it might be a '-' or '+' in the
3045 * fold column. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003046 if (row >= 0 && row < Rows && col >= 0 && col <= Columns
3047 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 mouse_char = ScreenLines[LineOffset[row] + col];
3049 else
3050 mouse_char = ' ';
3051#endif
3052
3053 old_curwin = curwin;
3054 old_cursor = curwin->w_cursor;
3055
3056 if (!(flags & MOUSE_FOCUS))
3057 {
Bram Moolenaarb53fb312019-06-13 23:59:52 +02003058 if (row < 0 || col < 0) // check if it makes sense
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 return IN_UNKNOWN;
3060
Bram Moolenaarb53fb312019-06-13 23:59:52 +02003061 // find the window where the row is in
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003062 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003063 if (wp == NULL)
3064 return IN_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 dragwin = NULL;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003066
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003067#ifdef FEAT_TEXT_PROP
Bram Moolenaarb53fb312019-06-13 23:59:52 +02003068 // Click in a popup window may start dragging or modeless selection,
3069 // but not much else.
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02003070 if (WIN_IS_POPUP(wp))
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003071 {
3072 on_sep_line = 0;
3073 in_popup_win = TRUE;
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003074 if (wp->w_popup_close == POPCLOSE_BUTTON
3075 && which_button == MOUSE_LEFT
3076 && popup_on_X_button(wp, row, col))
3077 {
3078 popup_close_for_mouse_click(wp);
3079 return IN_UNKNOWN;
3080 }
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003081 else if ((wp->w_popup_flags & (POPF_DRAG | POPF_RESIZE))
3082 && popup_on_border(wp, row, col))
Bram Moolenaarb53fb312019-06-13 23:59:52 +02003083 {
3084 popup_dragwin = wp;
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +02003085 popup_start_drag(wp, row, col);
Bram Moolenaarb53fb312019-06-13 23:59:52 +02003086 return IN_UNKNOWN;
3087 }
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003088 // Only close on release, otherwise it's not possible to drag or do
3089 // modeless selection.
3090 else if (wp->w_popup_close == POPCLOSE_CLICK
3091 && which_button == MOUSE_LEFT)
3092 {
3093 click_in_popup_win = wp;
3094 }
3095 else if (which_button == MOUSE_LEFT)
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02003096 // If the click is in the scrollbar, may scroll up/down.
3097 popup_handle_scrollbar_click(wp, row, col);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003098# ifdef FEAT_CLIPBOARD
3099 return IN_OTHER_WIN;
3100# else
3101 return IN_UNKNOWN;
3102# endif
3103 }
Bram Moolenaarb53fb312019-06-13 23:59:52 +02003104 in_popup_win = FALSE;
3105 popup_dragwin = NULL;
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003106#endif
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003107#ifdef FEAT_MENU
3108 if (row == -1)
3109 {
3110 /* A click in the window toolbar does not enter another window or
3111 * change Visual highlighting. */
3112 winbar_click(wp, col);
Bram Moolenaareb163d72017-09-23 15:08:17 +02003113 in_winbar = TRUE;
3114 return IN_OTHER_WIN | MOUSE_WINBAR;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003115 }
Bram Moolenaareb163d72017-09-23 15:08:17 +02003116 in_winbar = FALSE;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003117#endif
3118
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 /*
3120 * winpos and height may change in win_enter()!
3121 */
3122 if (row >= wp->w_height) /* In (or below) status line */
3123 {
3124 on_status_line = row - wp->w_height + 1;
3125 dragwin = wp;
3126 }
3127 else
3128 on_status_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 if (col >= wp->w_width) /* In separator line */
3130 {
3131 on_sep_line = col - wp->w_width + 1;
3132 dragwin = wp;
3133 }
3134 else
3135 on_sep_line = 0;
3136
3137 /* The rightmost character of the status line might be a vertical
3138 * separator character if there is no connecting window to the right. */
3139 if (on_status_line && on_sep_line)
3140 {
3141 if (stl_connected(wp))
3142 on_sep_line = 0;
3143 else
3144 on_status_line = 0;
3145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 /* Before jumping to another buffer, or moving the cursor for a left
3148 * click, stop Visual mode. */
3149 if (VIsual_active
3150 && (wp->w_buffer != curwin->w_buffer
Bram Moolenaar4033c552017-09-16 20:54:51 +02003151 || (!on_status_line && !on_sep_line
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003152#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003154# ifdef FEAT_RIGHTLEFT
Bram Moolenaar02631462017-09-22 15:20:32 +02003155 wp->w_p_rl ? col < wp->w_width - wp->w_p_fdc :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156# endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003157 col >= wp->w_p_fdc
3158# ifdef FEAT_CMDWIN
3159 + (cmdwin_type == 0 && wp == curwin ? 0 : 1)
3160# endif
3161 )
3162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 && (flags & MOUSE_MAY_STOP_VIS))))
3164 {
3165 end_visual_mode();
3166 redraw_curbuf_later(INVERTED); /* delete the inversion */
3167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168#ifdef FEAT_CMDWIN
3169 if (cmdwin_type != 0 && wp != curwin)
3170 {
3171 /* A click outside the command-line window: Use modeless
Bram Moolenaarf679a432010-03-02 18:16:09 +01003172 * selection if possible. Allow dragging the status lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 on_sep_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174# ifdef FEAT_CLIPBOARD
3175 if (on_status_line)
3176 return IN_STATUS_LINE;
3177 return IN_OTHER_WIN;
3178# else
3179 row = 0;
3180 col += wp->w_wincol;
3181 wp = curwin;
3182# endif
3183 }
3184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 /* Only change window focus when not clicking on or dragging the
3186 * status line. Do change focus when releasing the mouse button
3187 * (MOUSE_FOCUS was set above if we dragged first). */
3188 if (dragwin == NULL || (flags & MOUSE_RELEASED))
3189 win_enter(wp, TRUE); /* can make wp invalid! */
Bram Moolenaarc48369c2018-03-11 19:30:45 +01003190
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 if (curwin != old_curwin)
Bram Moolenaarc48369c2018-03-11 19:30:45 +01003192 {
3193#ifdef CHECK_DOUBLE_CLICK
3194 /* set topline, to be able to check for double click ourselves */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 set_mouse_topline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196#endif
Bram Moolenaarc48369c2018-03-11 19:30:45 +01003197#ifdef FEAT_TERMINAL
3198 /* when entering a terminal window may change state */
3199 term_win_entered();
3200#endif
3201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 if (on_status_line) /* In (or below) status line */
3203 {
3204 /* Don't use start_arrow() if we're in the same window */
3205 if (curwin == old_curwin)
3206 return IN_STATUS_LINE;
3207 else
3208 return IN_STATUS_LINE | CURSOR_MOVED;
3209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 if (on_sep_line) /* In (or below) status line */
3211 {
3212 /* Don't use start_arrow() if we're in the same window */
3213 if (curwin == old_curwin)
3214 return IN_SEP_LINE;
3215 else
3216 return IN_SEP_LINE | CURSOR_MOVED;
3217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218
3219 curwin->w_cursor.lnum = curwin->w_topline;
3220#ifdef FEAT_GUI
3221 /* remember topline, needed for double click */
3222 gui_prev_topline = curwin->w_topline;
3223# ifdef FEAT_DIFF
3224 gui_prev_topfill = curwin->w_topfill;
3225# endif
3226#endif
3227 }
3228 else if (on_status_line && which_button == MOUSE_LEFT)
3229 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 if (dragwin != NULL)
3231 {
3232 /* Drag the status line */
3233 count = row - dragwin->w_winrow - dragwin->w_height + 1
3234 - on_status_line;
3235 win_drag_status_line(dragwin, count);
3236 did_drag |= count;
3237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 return IN_STATUS_LINE; /* Cursor didn't move */
3239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 else if (on_sep_line && which_button == MOUSE_LEFT)
3241 {
3242 if (dragwin != NULL)
3243 {
3244 /* Drag the separator column */
3245 count = col - dragwin->w_wincol - dragwin->w_width + 1
3246 - on_sep_line;
3247 win_drag_vsep_line(dragwin, count);
3248 did_drag |= count;
3249 }
3250 return IN_SEP_LINE; /* Cursor didn't move */
3251 }
Bram Moolenaareb163d72017-09-23 15:08:17 +02003252#ifdef FEAT_MENU
3253 else if (in_winbar)
3254 {
3255 /* After a click on the window toolbar don't start Visual mode. */
3256 return IN_OTHER_WIN | MOUSE_WINBAR;
3257 }
3258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 else /* keep_window_focus must be TRUE */
3260 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 /* before moving the cursor for a left click, stop Visual mode */
3262 if (flags & MOUSE_MAY_STOP_VIS)
3263 {
3264 end_visual_mode();
3265 redraw_curbuf_later(INVERTED); /* delete the inversion */
3266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267
3268#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
3269 /* Continue a modeless selection in another window. */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003270 if (cmdwin_type != 0 && row < curwin->w_winrow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 return IN_OTHER_WIN;
3272#endif
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003273#ifdef FEAT_TEXT_PROP
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003274 if (in_popup_win)
Bram Moolenaarb53fb312019-06-13 23:59:52 +02003275 {
3276 if (popup_dragwin != NULL)
3277 {
3278 // dragging a popup window
3279 popup_drag(popup_dragwin);
3280 return IN_UNKNOWN;
3281 }
3282 // continue a modeless selection in a popup window
Bram Moolenaar2e62b562019-06-30 18:07:00 +02003283 click_in_popup_win = NULL;
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003284 return IN_OTHER_WIN;
Bram Moolenaarb53fb312019-06-13 23:59:52 +02003285 }
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287
3288 row -= W_WINROW(curwin);
Bram Moolenaar53f81742017-09-22 14:35:51 +02003289 col -= curwin->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290
3291 /*
3292 * When clicking beyond the end of the window, scroll the screen.
3293 * Scroll by however many rows outside the window we are.
3294 */
3295 if (row < 0)
3296 {
3297 count = 0;
3298 for (first = TRUE; curwin->w_topline > 1; )
3299 {
3300#ifdef FEAT_DIFF
3301 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
3302 ++count;
3303 else
3304#endif
3305 count += plines(curwin->w_topline - 1);
3306 if (!first && count > -row)
3307 break;
3308 first = FALSE;
3309#ifdef FEAT_FOLDING
Bram Moolenaarcde88542015-08-11 19:14:00 +02003310 (void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311#endif
3312#ifdef FEAT_DIFF
3313 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
3314 ++curwin->w_topfill;
3315 else
3316#endif
3317 {
3318 --curwin->w_topline;
3319#ifdef FEAT_DIFF
3320 curwin->w_topfill = 0;
3321#endif
3322 }
3323 }
3324#ifdef FEAT_DIFF
3325 check_topfill(curwin, FALSE);
3326#endif
3327 curwin->w_valid &=
3328 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3329 redraw_later(VALID);
3330 row = 0;
3331 }
3332 else if (row >= curwin->w_height)
3333 {
3334 count = 0;
3335 for (first = TRUE; curwin->w_topline < curbuf->b_ml.ml_line_count; )
3336 {
3337#ifdef FEAT_DIFF
3338 if (curwin->w_topfill > 0)
3339 ++count;
3340 else
3341#endif
3342 count += plines(curwin->w_topline);
3343 if (!first && count > row - curwin->w_height + 1)
3344 break;
3345 first = FALSE;
3346#ifdef FEAT_FOLDING
3347 if (hasFolding(curwin->w_topline, NULL, &curwin->w_topline)
3348 && curwin->w_topline == curbuf->b_ml.ml_line_count)
3349 break;
3350#endif
3351#ifdef FEAT_DIFF
3352 if (curwin->w_topfill > 0)
3353 --curwin->w_topfill;
3354 else
3355#endif
3356 {
3357 ++curwin->w_topline;
3358#ifdef FEAT_DIFF
3359 curwin->w_topfill =
3360 diff_check_fill(curwin, curwin->w_topline);
3361#endif
3362 }
3363 }
3364#ifdef FEAT_DIFF
3365 check_topfill(curwin, FALSE);
3366#endif
3367 redraw_later(VALID);
3368 curwin->w_valid &=
3369 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
3370 row = curwin->w_height - 1;
3371 }
3372 else if (row == 0)
3373 {
3374 /* When dragging the mouse, while the text has been scrolled up as
3375 * far as it goes, moving the mouse in the top line should scroll
3376 * the text down (done later when recomputing w_topline). */
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003377 if (mouse_dragging > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 && curwin->w_cursor.lnum
3379 == curwin->w_buffer->b_ml.ml_line_count
3380 && curwin->w_cursor.lnum == curwin->w_topline)
3381 curwin->w_valid &= ~(VALID_TOPLINE);
3382 }
3383 }
3384
3385#ifdef FEAT_FOLDING
3386 /* Check for position outside of the fold column. */
3387 if (
3388# ifdef FEAT_RIGHTLEFT
Bram Moolenaar02631462017-09-22 15:20:32 +02003389 curwin->w_p_rl ? col < curwin->w_width - curwin->w_p_fdc :
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390# endif
3391 col >= curwin->w_p_fdc
3392# ifdef FEAT_CMDWIN
3393 + (cmdwin_type == 0 ? 0 : 1)
3394# endif
3395 )
3396 mouse_char = ' ';
3397#endif
3398
3399 /* compute the position in the buffer line from the posn on the screen */
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003400 if (mouse_comp_pos(curwin, &row, &col, &curwin->w_cursor.lnum, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 mouse_past_bottom = TRUE;
3402
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 /* Start Visual mode before coladvance(), for when 'sel' != "old" */
3404 if ((flags & MOUSE_MAY_VIS) && !VIsual_active)
3405 {
3406 check_visual_highlight();
3407 VIsual = old_cursor;
3408 VIsual_active = TRUE;
3409 VIsual_reselect = TRUE;
3410 /* if 'selectmode' contains "mouse", start Select mode */
3411 may_start_select('o');
3412 setmouse();
Bram Moolenaar7df351e2006-01-23 22:30:28 +00003413 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 redraw_cmdline = TRUE; /* show visual mode later */
3415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416
3417 curwin->w_curswant = col;
3418 curwin->w_set_curswant = FALSE; /* May still have been TRUE */
3419 if (coladvance(col) == FAIL) /* Mouse click beyond end of line */
3420 {
3421 if (inclusive != NULL)
3422 *inclusive = TRUE;
3423 mouse_past_eol = TRUE;
3424 }
3425 else if (inclusive != NULL)
3426 *inclusive = FALSE;
3427
3428 count = IN_BUFFER;
3429 if (curwin != old_curwin || curwin->w_cursor.lnum != old_cursor.lnum
3430 || curwin->w_cursor.col != old_cursor.col)
3431 count |= CURSOR_MOVED; /* Cursor has moved */
3432
Bram Moolenaar4c063a02019-06-10 21:24:12 +02003433# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 if (mouse_char == '+')
3435 count |= MOUSE_FOLD_OPEN;
3436 else if (mouse_char != ' ')
3437 count |= MOUSE_FOLD_CLOSE;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02003438# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
3440 return count;
3441}
Bram Moolenaar4c063a02019-06-10 21:24:12 +02003442#endif
3443
3444// Functions also used for popup windows.
3445#if defined(FEAT_MOUSE) || defined(FEAT_TEXT_PROP) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
3447/*
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003448 * Compute the buffer line position from the screen position "rowp" / "colp" in
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 * window "win".
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003450 * "plines_cache" can be NULL (no cache) or an array with "win->w_height"
3451 * entries that caches the plines_win() result from a previous call. Entry is
3452 * zero if not computed yet. There must be no text or setting changes since
3453 * the entry is put in the cache.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 * Returns TRUE if the position is below the last line.
3455 */
3456 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003457mouse_comp_pos(
3458 win_T *win,
3459 int *rowp,
3460 int *colp,
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003461 linenr_T *lnump,
3462 int *plines_cache)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463{
3464 int col = *colp;
3465 int row = *rowp;
3466 linenr_T lnum;
3467 int retval = FALSE;
3468 int off;
3469 int count;
3470
3471#ifdef FEAT_RIGHTLEFT
3472 if (win->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02003473 col = win->w_width - 1 - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474#endif
3475
3476 lnum = win->w_topline;
3477
3478 while (row > 0)
3479 {
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003480 int cache_idx = lnum - win->w_topline;
3481
3482 if (plines_cache != NULL && plines_cache[cache_idx] > 0)
3483 count = plines_cache[cache_idx];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 else
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003485 {
3486#ifdef FEAT_DIFF
3487 /* Don't include filler lines in "count" */
3488 if (win->w_p_diff
3489# ifdef FEAT_FOLDING
3490 && !hasFoldingWin(win, lnum, NULL, NULL, TRUE, NULL)
3491# endif
3492 )
3493 {
3494 if (lnum == win->w_topline)
3495 row -= win->w_topfill;
3496 else
3497 row -= diff_check_fill(win, lnum);
3498 count = plines_win_nofill(win, lnum, TRUE);
3499 }
3500 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501#endif
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003502 count = plines_win(win, lnum, TRUE);
3503 if (plines_cache != NULL)
3504 plines_cache[cache_idx] = count;
3505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 if (count > row)
3507 break; /* Position is in this buffer line. */
3508#ifdef FEAT_FOLDING
3509 (void)hasFoldingWin(win, lnum, NULL, &lnum, TRUE, NULL);
3510#endif
3511 if (lnum == win->w_buffer->b_ml.ml_line_count)
3512 {
3513 retval = TRUE;
3514 break; /* past end of file */
3515 }
3516 row -= count;
3517 ++lnum;
3518 }
3519
3520 if (!retval)
3521 {
3522 /* Compute the column without wrapping. */
3523 off = win_col_off(win) - win_col_off2(win);
3524 if (col < off)
3525 col = off;
Bram Moolenaar02631462017-09-22 15:20:32 +02003526 col += row * (win->w_width - off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 /* add skip column (for long wrapping line) */
3528 col += win->w_skipcol;
3529 }
3530
3531 if (!win->w_p_wrap)
3532 col += win->w_leftcol;
3533
3534 /* skip line number and fold column in front of the line */
3535 col -= win_col_off(win);
3536 if (col < 0)
3537 {
3538#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003539 netbeans_gutter_click(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540#endif
3541 col = 0;
3542 }
3543
3544 *colp = col;
3545 *rowp = row;
3546 *lnump = lnum;
3547 return retval;
3548}
3549
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550/*
3551 * Find the window at screen position "*rowp" and "*colp". The positions are
3552 * updated to become relative to the top-left of the window.
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003553 * When "popup" is FAIL_POPUP and the position is in a popup window then NULL
3554 * is returned. When "popup" is IGNORE_POPUP then do not even check popup
3555 * windows.
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003556 * Returns NULL when something is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 win_T *
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003559mouse_find_win(int *rowp, int *colp, mouse_find_T popup UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560{
3561 frame_T *fp;
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003562 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003564#ifdef FEAT_TEXT_PROP
3565 win_T *pwp = NULL;
3566
3567 if (popup != IGNORE_POPUP)
3568 {
3569 popup_reset_handled();
3570 while ((wp = find_next_popup(TRUE)) != NULL)
3571 {
3572 if (*rowp >= wp->w_winrow && *rowp < wp->w_winrow + popup_height(wp)
3573 && *colp >= wp->w_wincol
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02003574 && *colp < wp->w_wincol + popup_width(wp))
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003575 pwp = wp;
3576 }
3577 if (pwp != NULL)
3578 {
3579 if (popup == FAIL_POPUP)
3580 return NULL;
3581 *rowp -= pwp->w_winrow;
3582 *colp -= pwp->w_wincol;
3583 return pwp;
3584 }
3585 }
3586#endif
3587
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 fp = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003589 *rowp -= firstwin->w_winrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 for (;;)
3591 {
3592 if (fp->fr_layout == FR_LEAF)
3593 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 if (fp->fr_layout == FR_ROW)
3595 {
3596 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
3597 {
3598 if (*colp < fp->fr_width)
3599 break;
3600 *colp -= fp->fr_width;
3601 }
3602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 else /* fr_layout == FR_COL */
3604 {
3605 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
3606 {
3607 if (*rowp < fp->fr_height)
3608 break;
3609 *rowp -= fp->fr_height;
3610 }
3611 }
3612 }
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003613 /* When using a timer that closes a window the window might not actually
3614 * exist. */
3615 FOR_ALL_WINDOWS(wp)
3616 if (wp == fp->fr_win)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003617 {
3618#ifdef FEAT_MENU
3619 *rowp -= wp->w_winbar_height;
3620#endif
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003621 return wp;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003622 }
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003623 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625
Bram Moolenaar860cae12010-06-05 23:22:07 +02003626#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar658a1542018-03-03 19:29:43 +01003628 || defined(FEAT_GUI_PHOTON) || defined(FEAT_TERM_POPUP_MENU) \
3629 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630/*
3631 * Translate window coordinates to buffer position without any side effects
3632 */
3633 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003634get_fpos_of_mouse(pos_T *mpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635{
3636 win_T *wp;
3637 int row = mouse_row;
3638 int col = mouse_col;
3639
3640 if (row < 0 || col < 0) /* check if it makes sense */
3641 return IN_UNKNOWN;
3642
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 /* find the window where the row is in */
Bram Moolenaar451d4b52019-06-12 20:22:27 +02003644 wp = mouse_find_win(&row, &col, FAIL_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02003645 if (wp == NULL)
3646 return IN_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 /*
3648 * winpos and height may change in win_enter()!
3649 */
3650 if (row >= wp->w_height) /* In (or below) status line */
3651 return IN_STATUS_LINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 if (col >= wp->w_width) /* In vertical separator line */
3653 return IN_SEP_LINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654
3655 if (wp != curwin)
3656 return IN_UNKNOWN;
3657
3658 /* compute the position in the buffer line from the posn on the screen */
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02003659 if (mouse_comp_pos(curwin, &row, &col, &mpos->lnum, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 return IN_STATUS_LINE; /* past bottom */
3661
3662 mpos->col = vcol2col(wp, mpos->lnum, col);
3663
3664 if (mpos->col > 0)
3665 --mpos->col;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003666 mpos->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 return IN_BUFFER;
3668}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003671#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MAC) \
3672 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar3767b612018-03-03 19:51:58 +01003673 || defined(FEAT_GUI_PHOTON) || defined(FEAT_BEVAL) \
3674 || defined(FEAT_TERM_POPUP_MENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675/*
3676 * Convert a virtual (screen) column to a character column.
3677 * The first column is one.
3678 */
3679 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003680vcol2col(win_T *wp, linenr_T lnum, int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681{
3682 /* try to advance to the specified column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 int count = 0;
3684 char_u *ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003685 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686
Bram Moolenaar597a4222014-06-25 14:39:50 +02003687 line = ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaarb6101cf2012-10-21 00:58:39 +02003688 while (count < vcol && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003690 count += win_lbr_chartabsize(wp, line, ptr, count, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003691 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02003693 return (int)(ptr - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694}
3695#endif
3696
3697#endif /* FEAT_MOUSE */
3698
Bram Moolenaar4f974752019-02-17 17:44:42 +01003699#if defined(FEAT_GUI) || defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700/*
3701 * Called when focus changed. Used for the GUI or for systems where this can
3702 * be done in the console (Win32).
3703 */
3704 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003705ui_focus_change(
3706 int in_focus) /* TRUE if focus gained. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707{
3708 static time_t last_time = (time_t)0;
3709 int need_redraw = FALSE;
3710
3711 /* When activated: Check if any file was modified outside of Vim.
3712 * Only do this when not done within the last two seconds (could get
3713 * several events in a row). */
3714 if (in_focus && last_time + 2 < time(NULL))
3715 {
3716 need_redraw = check_timestamps(
3717# ifdef FEAT_GUI
3718 gui.in_use
3719# else
3720 FALSE
3721# endif
3722 );
3723 last_time = time(NULL);
3724 }
3725
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 /*
3727 * Fire the focus gained/lost autocommand.
3728 */
3729 need_redraw |= apply_autocmds(in_focus ? EVENT_FOCUSGAINED
3730 : EVENT_FOCUSLOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731
3732 if (need_redraw)
3733 {
3734 /* Something was executed, make sure the cursor is put back where it
3735 * belongs. */
3736 need_wait_return = FALSE;
3737
3738 if (State & CMDLINE)
3739 redrawcmdline();
3740 else if (State == HITRETURN || State == SETWSIZE || State == ASKMORE
3741 || State == EXTERNCMD || State == CONFIRM || exmode_active)
3742 repeat_message();
3743 else if ((State & NORMAL) || (State & INSERT))
3744 {
3745 if (must_redraw != 0)
3746 update_screen(0);
3747 setcursor();
3748 }
3749 cursor_on(); /* redrawing may have switched it off */
Bram Moolenaara338adc2018-01-31 20:51:47 +01003750 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751# ifdef FEAT_GUI
3752 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 gui_update_scrollbars(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754# endif
3755 }
3756#ifdef FEAT_TITLE
3757 /* File may have been changed from 'readonly' to 'noreadonly' */
3758 if (need_maketitle)
3759 maketitle();
3760#endif
3761}
3762#endif
3763
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003764#if defined(HAVE_INPUT_METHOD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765/*
3766 * Save current Input Method status to specified place.
3767 */
3768 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003769im_save_status(long *psave)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770{
3771 /* Don't save when 'imdisable' is set or "xic" is NULL, IM is always
3772 * disabled then (but might start later).
3773 * Also don't save when inside a mapping, vgetc_im_active has not been set
3774 * then.
3775 * And don't save when the keys were stuffed (e.g., for a "." command).
3776 * And don't save when the GUI is running but our window doesn't have
3777 * input focus (e.g., when a find dialog is open). */
3778 if (!p_imdisable && KeyTyped && !KeyStuffed
3779# ifdef FEAT_XIM
3780 && xic != NULL
3781# endif
3782# ifdef FEAT_GUI
3783 && (!gui.in_use || gui.in_focus)
3784# endif
3785 )
3786 {
3787 /* Do save when IM is on, or IM is off and saved status is on. */
3788 if (vgetc_im_active)
3789 *psave = B_IMODE_IM;
3790 else if (*psave == B_IMODE_IM)
3791 *psave = B_IMODE_NONE;
3792 }
3793}
3794#endif