blob: 4a4506efc9ec7321f06ec2d3bb786d1f7d57bd80 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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
21 void
22ui_write(s, len)
23 char_u *s;
24 int len;
25{
26#ifdef FEAT_GUI
27 if (gui.in_use && !gui.dying && !gui.starting)
28 {
29 gui_write(s, len);
30 if (p_wd)
31 gui_wait_for_chars(p_wd);
32 return;
33 }
34#endif
35#ifndef NO_CONSOLE
36 /* Don't output anything in silent mode ("ex -s") unless 'verbose' set */
37 if (!(silent_mode && p_verbose == 0))
38 {
39#ifdef FEAT_MBYTE
40 char_u *tofree = NULL;
41
42 if (output_conv.vc_type != CONV_NONE)
43 {
44 /* Convert characters from 'encoding' to 'termencoding'. */
45 tofree = string_convert(&output_conv, s, &len);
46 if (tofree != NULL)
47 s = tofree;
48 }
49#endif
50
51 mch_write(s, len);
52
53#ifdef FEAT_MBYTE
54 if (output_conv.vc_type != CONV_NONE)
55 vim_free(tofree);
56#endif
57 }
58#endif
59}
60
Bram Moolenaar4b9669f2011-07-07 16:20:52 +020061#if defined(UNIX) || defined(VMS) || defined(PROTO) || defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +000062/*
63 * When executing an external program, there may be some typed characters that
64 * are not consumed by it. Give them back to ui_inchar() and they are stored
65 * here for the next call.
66 */
67static char_u *ta_str = NULL;
68static int ta_off; /* offset for next char to use when ta_str != NULL */
69static int ta_len; /* length of ta_str when it's not NULL*/
70
71 void
72ui_inchar_undo(s, len)
73 char_u *s;
74 int len;
75{
76 char_u *new;
77 int newlen;
78
79 newlen = len;
80 if (ta_str != NULL)
81 newlen += ta_len - ta_off;
82 new = alloc(newlen);
83 if (new != NULL)
84 {
85 if (ta_str != NULL)
86 {
87 mch_memmove(new, ta_str + ta_off, (size_t)(ta_len - ta_off));
88 mch_memmove(new + ta_len - ta_off, s, (size_t)len);
89 vim_free(ta_str);
90 }
91 else
92 mch_memmove(new, s, (size_t)len);
93 ta_str = new;
94 ta_len = newlen;
95 ta_off = 0;
96 }
97}
98#endif
99
100/*
Bram Moolenaarb6101cf2012-10-21 00:58:39 +0200101 * ui_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102 * Get characters from the keyboard.
103 * Return the number of characters that are available.
104 * If "wtime" == 0 do not wait for characters.
105 * If "wtime" == -1 wait forever for characters.
106 * If "wtime" > 0 wait "wtime" milliseconds for a character.
107 *
108 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
109 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
110 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is NULL
111 * otherwise.
112 */
113 int
114ui_inchar(buf, maxlen, wtime, tb_change_cnt)
115 char_u *buf;
116 int maxlen;
117 long wtime; /* don't use "time", MIPS cannot handle it */
118 int tb_change_cnt;
119{
120 int retval = 0;
121
122#if defined(FEAT_GUI) && (defined(UNIX) || defined(VMS))
123 /*
124 * Use the typeahead if there is any.
125 */
126 if (ta_str != NULL)
127 {
128 if (maxlen >= ta_len - ta_off)
129 {
130 mch_memmove(buf, ta_str + ta_off, (size_t)ta_len);
131 vim_free(ta_str);
132 ta_str = NULL;
133 return ta_len;
134 }
135 mch_memmove(buf, ta_str + ta_off, (size_t)maxlen);
136 ta_off += maxlen;
137 return maxlen;
138 }
139#endif
140
Bram Moolenaar05159a02005-02-26 23:04:13 +0000141#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000142 if (do_profiling == PROF_YES && wtime != 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000143 prof_inchar_enter();
144#endif
145
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146#ifdef NO_CONSOLE_INPUT
147 /* Don't wait for character input when the window hasn't been opened yet.
148 * Do try reading, this works when redirecting stdin from a file.
149 * Must return something, otherwise we'll loop forever. If we run into
150 * this very often we probably got stuck, exit Vim. */
151 if (no_console_input())
152 {
153 static int count = 0;
154
155# ifndef NO_CONSOLE
Bram Moolenaar43b604c2005-03-22 23:06:55 +0000156 retval = mch_inchar(buf, maxlen, (wtime >= 0 && wtime < 10)
157 ? 10L : wtime, tb_change_cnt);
158 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. */
177 if (mapped_ctrl_c)
178 ctrl_c_interrupts = FALSE;
179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180
181#ifdef FEAT_GUI
182 if (gui.in_use)
183 {
184 if (gui_wait_for_chars(wtime) && !typebuf_changed(tb_change_cnt))
185 retval = read_from_input_buf(buf, (long)maxlen);
186 }
187#endif
188#ifndef NO_CONSOLE
189# ifdef FEAT_GUI
190 else
191# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000192 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 retval = mch_inchar(buf, maxlen, wtime, tb_change_cnt);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195#endif
196
Bram Moolenaarc8da3112007-03-08 12:36:46 +0000197 if (wtime == -1 || wtime > 100L)
198 /* block SIGHUP et al. */
199 (void)vim_handle_signal(SIGNAL_BLOCK);
200
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 ctrl_c_interrupts = TRUE;
202
Bram Moolenaar05159a02005-02-26 23:04:13 +0000203#ifdef NO_CONSOLE_INPUT
204theend:
205#endif
206#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000207 if (do_profiling == PROF_YES && wtime != 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000208 prof_inchar_exit();
209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 return retval;
211}
212
213/*
214 * return non-zero if a character is available
215 */
216 int
217ui_char_avail()
218{
219#ifdef FEAT_GUI
220 if (gui.in_use)
221 {
222 gui_mch_update();
223 return input_available();
224 }
225#endif
226#ifndef NO_CONSOLE
227# ifdef NO_CONSOLE_INPUT
228 if (no_console_input())
229 return 0;
230# endif
231 return mch_char_avail();
232#else
233 return 0;
234#endif
235}
236
237/*
238 * Delay for the given number of milliseconds. If ignoreinput is FALSE then we
239 * cancel the delay if a key is hit.
240 */
241 void
242ui_delay(msec, ignoreinput)
243 long msec;
244 int ignoreinput;
245{
246#ifdef FEAT_GUI
247 if (gui.in_use && !ignoreinput)
248 gui_wait_for_chars(msec);
249 else
250#endif
251 mch_delay(msec, ignoreinput);
252}
253
254/*
255 * If the machine has job control, use it to suspend the program,
256 * otherwise fake it by starting a new shell.
257 * When running the GUI iconify the window.
258 */
259 void
260ui_suspend()
261{
262#ifdef FEAT_GUI
263 if (gui.in_use)
264 {
265 gui_mch_iconify();
266 return;
267 }
268#endif
269 mch_suspend();
270}
271
272#if !defined(UNIX) || !defined(SIGTSTP) || defined(PROTO) || defined(__BEOS__)
273/*
274 * When the OS can't really suspend, call this function to start a shell.
275 * This is never called in the GUI.
276 */
277 void
278suspend_shell()
279{
280 if (*p_sh == NUL)
281 EMSG(_(e_shellempty));
282 else
283 {
284 MSG_PUTS(_("new shell started\n"));
285 do_shell(NULL, 0);
286 }
287}
288#endif
289
290/*
291 * Try to get the current Vim shell size. Put the result in Rows and Columns.
292 * Use the new sizes as defaults for 'columns' and 'lines'.
293 * Return OK when size could be determined, FAIL otherwise.
294 */
295 int
296ui_get_shellsize()
297{
298 int retval;
299
300#ifdef FEAT_GUI
301 if (gui.in_use)
302 retval = gui_get_shellsize();
303 else
304#endif
305 retval = mch_get_shellsize();
306
307 check_shellsize();
308
309 /* adjust the default for 'lines' and 'columns' */
310 if (retval == OK)
311 {
312 set_number_default("lines", Rows);
313 set_number_default("columns", Columns);
314 }
315 return retval;
316}
317
318/*
319 * Set the size of the Vim shell according to Rows and Columns, if possible.
320 * The gui_set_shellsize() or mch_set_shellsize() function will try to set the
321 * new size. If this is not possible, it will adjust Rows and Columns.
322 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 void
324ui_set_shellsize(mustset)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000325 int mustset UNUSED; /* set by the user */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326{
327#ifdef FEAT_GUI
328 if (gui.in_use)
329 gui_set_shellsize(mustset,
330# ifdef WIN3264
331 TRUE
332# else
333 FALSE
334# endif
Bram Moolenaar04a9d452006-03-27 21:03:26 +0000335 , RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 else
337#endif
338 mch_set_shellsize();
339}
340
341/*
342 * Called when Rows and/or Columns changed. Adjust scroll region and mouse
343 * region.
344 */
345 void
346ui_new_shellsize()
347{
348 if (full_screen && !exiting)
349 {
350#ifdef FEAT_GUI
351 if (gui.in_use)
352 gui_new_shellsize();
353 else
354#endif
355 mch_new_shellsize();
356 }
357}
358
359 void
360ui_breakcheck()
361{
362#ifdef FEAT_GUI
363 if (gui.in_use)
364 gui_mch_update();
365 else
366#endif
367 mch_breakcheck();
368}
369
370/*****************************************************************************
371 * Functions for copying and pasting text between applications.
372 * This is always included in a GUI version, but may also be included when the
373 * clipboard and mouse is available to a terminal version such as xterm.
374 * Note: there are some more functions in ops.c that handle selection stuff.
375 *
376 * Also note that the majority of functions here deal with the X 'primary'
377 * (visible - for Visual mode use) selection, and only that. There are no
378 * versions of these for the 'clipboard' selection, as Visual mode has no use
379 * for them.
380 */
381
382#if defined(FEAT_CLIPBOARD) || defined(PROTO)
383
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200384static void clip_copy_selection __ARGS((VimClipboard *clip));
385
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386/*
387 * Selection stuff using Visual mode, for cutting and pasting text to other
388 * windows.
389 */
390
391/*
392 * Call this to initialise the clipboard. Pass it FALSE if the clipboard code
393 * is included, but the clipboard can not be used, or TRUE if the clipboard can
394 * be used. Eg unix may call this with FALSE, then call it again with TRUE if
395 * the GUI starts.
396 */
397 void
398clip_init(can_use)
399 int can_use;
400{
401 VimClipboard *cb;
402
403 cb = &clip_star;
404 for (;;)
405 {
406 cb->available = can_use;
407 cb->owned = FALSE;
408 cb->start.lnum = 0;
409 cb->start.col = 0;
410 cb->end.lnum = 0;
411 cb->end.col = 0;
412 cb->state = SELECT_CLEARED;
413
414 if (cb == &clip_plus)
415 break;
416 cb = &clip_plus;
417 }
418}
419
420/*
421 * Check whether the VIsual area has changed, and if so try to become the owner
422 * of the selection, and free any old converted selection we may still have
423 * lying around. If the VIsual mode has ended, make a copy of what was
424 * selected so we can still give it to others. Will probably have to make sure
425 * this is called whenever VIsual mode is ended.
426 */
427 void
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200428clip_update_selection(clip)
429 VimClipboard *clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430{
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200431 pos_T start, end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432
433 /* If visual mode is only due to a redo command ("."), then ignore it */
434 if (!redo_VIsual_busy && VIsual_active && (State & NORMAL))
435 {
436 if (lt(VIsual, curwin->w_cursor))
437 {
438 start = VIsual;
439 end = curwin->w_cursor;
440#ifdef FEAT_MBYTE
441 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000442 end.col += (*mb_ptr2len)(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443#endif
444 }
445 else
446 {
447 start = curwin->w_cursor;
448 end = VIsual;
449 }
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200450 if (!equalpos(clip->start, start)
451 || !equalpos(clip->end, end)
452 || clip->vmode != VIsual_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200454 clip_clear_selection(clip);
455 clip->start = start;
456 clip->end = end;
457 clip->vmode = VIsual_mode;
458 clip_free_selection(clip);
459 clip_own_selection(clip);
460 clip_gen_set_selection(clip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 }
462 }
463}
464
465 void
466clip_own_selection(cbd)
467 VimClipboard *cbd;
468{
469 /*
470 * Also want to check somehow that we are reading from the keyboard rather
471 * than a mapping etc.
472 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473#ifdef FEAT_X11
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200474 /* Always own the selection, we might have lost it without being
Bram Moolenaar62b42182010-09-21 22:09:37 +0200475 * notified, e.g. during a ":sh" command. */
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200476 if (cbd->available)
477 {
478 int was_owned = cbd->owned;
479
480 cbd->owned = (clip_gen_own_selection(cbd) == OK);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200481 if (!was_owned && (cbd == &clip_star || cbd == &clip_plus))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 {
Bram Moolenaarebefac62005-12-28 22:39:57 +0000483 /* May have to show a different kind of highlighting for the
484 * selected area. There is no specific redraw command for this,
485 * just redraw all windows on the current buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 if (cbd->owned
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000487 && (get_real_state() == VISUAL
488 || get_real_state() == SELECTMODE)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200489 && (cbd == &clip_star ? clip_isautosel_star()
490 : clip_isautosel_plus())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 && hl_attr(HLF_V) != hl_attr(HLF_VNC))
492 redraw_curbuf_later(INVERTED_ALL);
493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200495#else
Bram Moolenaarb6101cf2012-10-21 00:58:39 +0200496 /* Only own the clipboard when we didn't own it yet. */
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200497 if (!cbd->owned && cbd->available)
498 cbd->owned = (clip_gen_own_selection(cbd) == OK);
499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500}
501
502 void
503clip_lose_selection(cbd)
504 VimClipboard *cbd;
505{
506#ifdef FEAT_X11
507 int was_owned = cbd->owned;
508#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200509 int visual_selection = FALSE;
510
511 if (cbd == &clip_star || cbd == &clip_plus)
512 visual_selection = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513
514 clip_free_selection(cbd);
515 cbd->owned = FALSE;
516 if (visual_selection)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200517 clip_clear_selection(cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 clip_gen_lose_selection(cbd);
519#ifdef FEAT_X11
520 if (visual_selection)
521 {
522 /* May have to show a different kind of highlighting for the selected
523 * area. There is no specific redraw command for this, just redraw all
524 * windows on the current buffer. */
525 if (was_owned
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000526 && (get_real_state() == VISUAL
527 || get_real_state() == SELECTMODE)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200528 && (cbd == &clip_star ?
529 clip_isautosel_star() : clip_isautosel_plus())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 && hl_attr(HLF_V) != hl_attr(HLF_VNC))
531 {
532 update_curbuf(INVERTED_ALL);
533 setcursor();
534 cursor_on();
535 out_flush();
536# ifdef FEAT_GUI
537 if (gui.in_use)
538 gui_update_cursor(TRUE, FALSE);
539# endif
540 }
541 }
542#endif
543}
544
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200545 static void
546clip_copy_selection(clip)
547 VimClipboard *clip;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548{
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200549 if (VIsual_active && (State & NORMAL) && clip->available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200551 clip_update_selection(clip);
552 clip_free_selection(clip);
553 clip_own_selection(clip);
554 if (clip->owned)
555 clip_get_selection(clip);
556 clip_gen_set_selection(clip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 }
558}
559
560/*
561 * Called when Visual mode is ended: update the selection.
562 */
563 void
564clip_auto_select()
565{
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200566 if (clip_isautosel_star())
567 clip_copy_selection(&clip_star);
568 if (clip_isautosel_plus())
569 clip_copy_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570}
571
572/*
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200573 * Return TRUE if automatic selection of Visual area is desired for the *
574 * register.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 */
576 int
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200577clip_isautosel_star()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578{
579 return (
580#ifdef FEAT_GUI
581 gui.in_use ? (vim_strchr(p_go, GO_ASEL) != NULL) :
582#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200583 clip_autoselect_star);
584}
585
586/*
587 * Return TRUE if automatic selection of Visual area is desired for the +
588 * register.
589 */
590 int
591clip_isautosel_plus()
592{
593 return (
594#ifdef FEAT_GUI
595 gui.in_use ? (vim_strchr(p_go, GO_ASELPLUS) != NULL) :
596#endif
597 clip_autoselect_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598}
599
600
601/*
602 * Stuff for general mouse selection, without using Visual mode.
603 */
604
605static int clip_compare_pos __ARGS((int row1, int col1, int row2, int col2));
606static void clip_invert_area __ARGS((int, int, int, int, int how));
607static void clip_invert_rectangle __ARGS((int row, int col, int height, int width, int invert));
608static void clip_get_word_boundaries __ARGS((VimClipboard *, int, int));
609static int clip_get_line_end __ARGS((int));
610static void clip_update_modeless_selection __ARGS((VimClipboard *, int, int,
611 int, int));
612
613/* flags for clip_invert_area() */
614#define CLIP_CLEAR 1
615#define CLIP_SET 2
616#define CLIP_TOGGLE 3
617
618/*
619 * Start, continue or end a modeless selection. Used when editing the
620 * command-line and in the cmdline window.
621 */
622 void
623clip_modeless(button, is_click, is_drag)
624 int button;
625 int is_click;
626 int is_drag;
627{
628 int repeat;
629
630 repeat = ((clip_star.mode == SELECT_MODE_CHAR
631 || clip_star.mode == SELECT_MODE_LINE)
632 && (mod_mask & MOD_MASK_2CLICK))
633 || (clip_star.mode == SELECT_MODE_WORD
634 && (mod_mask & MOD_MASK_3CLICK));
635 if (is_click && button == MOUSE_RIGHT)
636 {
637 /* Right mouse button: If there was no selection, start one.
638 * Otherwise extend the existing selection. */
639 if (clip_star.state == SELECT_CLEARED)
640 clip_start_selection(mouse_col, mouse_row, FALSE);
641 clip_process_selection(button, mouse_col, mouse_row, repeat);
642 }
643 else if (is_click)
644 clip_start_selection(mouse_col, mouse_row, repeat);
645 else if (is_drag)
646 {
647 /* Don't try extending a selection if there isn't one. Happens when
648 * button-down is in the cmdline and them moving mouse upwards. */
649 if (clip_star.state != SELECT_CLEARED)
650 clip_process_selection(button, mouse_col, mouse_row, repeat);
651 }
652 else /* release */
653 clip_process_selection(MOUSE_RELEASE, mouse_col, mouse_row, FALSE);
654}
655
656/*
657 * Compare two screen positions ala strcmp()
658 */
659 static int
660clip_compare_pos(row1, col1, row2, col2)
661 int row1;
662 int col1;
663 int row2;
664 int col2;
665{
666 if (row1 > row2) return(1);
667 if (row1 < row2) return(-1);
668 if (col1 > col2) return(1);
669 if (col1 < col2) return(-1);
670 return(0);
671}
672
673/*
674 * Start the selection
675 */
676 void
677clip_start_selection(col, row, repeated_click)
678 int col;
679 int row;
680 int repeated_click;
681{
682 VimClipboard *cb = &clip_star;
683
684 if (cb->state == SELECT_DONE)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200685 clip_clear_selection(cb);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686
687 row = check_row(row);
688 col = check_col(col);
689#ifdef FEAT_MBYTE
690 col = mb_fix_col(col, row);
691#endif
692
693 cb->start.lnum = row;
694 cb->start.col = col;
695 cb->end = cb->start;
696 cb->origin_row = (short_u)cb->start.lnum;
697 cb->state = SELECT_IN_PROGRESS;
698
699 if (repeated_click)
700 {
701 if (++cb->mode > SELECT_MODE_LINE)
702 cb->mode = SELECT_MODE_CHAR;
703 }
704 else
705 cb->mode = SELECT_MODE_CHAR;
706
707#ifdef FEAT_GUI
708 /* clear the cursor until the selection is made */
709 if (gui.in_use)
710 gui_undraw_cursor();
711#endif
712
713 switch (cb->mode)
714 {
715 case SELECT_MODE_CHAR:
716 cb->origin_start_col = cb->start.col;
717 cb->word_end_col = clip_get_line_end((int)cb->start.lnum);
718 break;
719
720 case SELECT_MODE_WORD:
721 clip_get_word_boundaries(cb, (int)cb->start.lnum, cb->start.col);
722 cb->origin_start_col = cb->word_start_col;
723 cb->origin_end_col = cb->word_end_col;
724
725 clip_invert_area((int)cb->start.lnum, cb->word_start_col,
726 (int)cb->end.lnum, cb->word_end_col, CLIP_SET);
727 cb->start.col = cb->word_start_col;
728 cb->end.col = cb->word_end_col;
729 break;
730
731 case SELECT_MODE_LINE:
732 clip_invert_area((int)cb->start.lnum, 0, (int)cb->start.lnum,
733 (int)Columns, CLIP_SET);
734 cb->start.col = 0;
735 cb->end.col = Columns;
736 break;
737 }
738
739 cb->prev = cb->start;
740
741#ifdef DEBUG_SELECTION
742 printf("Selection started at (%u,%u)\n", cb->start.lnum, cb->start.col);
743#endif
744}
745
746/*
747 * Continue processing the selection
748 */
749 void
750clip_process_selection(button, col, row, repeated_click)
751 int button;
752 int col;
753 int row;
754 int_u repeated_click;
755{
756 VimClipboard *cb = &clip_star;
757 int diff;
758 int slen = 1; /* cursor shape width */
759
760 if (button == MOUSE_RELEASE)
761 {
762 /* Check to make sure we have something selected */
763 if (cb->start.lnum == cb->end.lnum && cb->start.col == cb->end.col)
764 {
765#ifdef FEAT_GUI
766 if (gui.in_use)
767 gui_update_cursor(FALSE, FALSE);
768#endif
769 cb->state = SELECT_CLEARED;
770 return;
771 }
772
773#ifdef DEBUG_SELECTION
774 printf("Selection ended: (%u,%u) to (%u,%u)\n", cb->start.lnum,
775 cb->start.col, cb->end.lnum, cb->end.col);
776#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200777 if (clip_isautosel_star()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 || (
779#ifdef FEAT_GUI
780 gui.in_use ? (vim_strchr(p_go, GO_ASELML) != NULL) :
781#endif
782 clip_autoselectml))
783 clip_copy_modeless_selection(FALSE);
784#ifdef FEAT_GUI
785 if (gui.in_use)
786 gui_update_cursor(FALSE, FALSE);
787#endif
788
789 cb->state = SELECT_DONE;
790 return;
791 }
792
793 row = check_row(row);
794 col = check_col(col);
795#ifdef FEAT_MBYTE
796 col = mb_fix_col(col, row);
797#endif
798
799 if (col == (int)cb->prev.col && row == cb->prev.lnum && !repeated_click)
800 return;
801
802 /*
803 * When extending the selection with the right mouse button, swap the
804 * start and end if the position is before half the selection
805 */
806 if (cb->state == SELECT_DONE && button == MOUSE_RIGHT)
807 {
808 /*
809 * If the click is before the start, or the click is inside the
810 * selection and the start is the closest side, set the origin to the
811 * end of the selection.
812 */
813 if (clip_compare_pos(row, col, (int)cb->start.lnum, cb->start.col) < 0
814 || (clip_compare_pos(row, col,
815 (int)cb->end.lnum, cb->end.col) < 0
816 && (((cb->start.lnum == cb->end.lnum
817 && cb->end.col - col > col - cb->start.col))
818 || ((diff = (cb->end.lnum - row) -
819 (row - cb->start.lnum)) > 0
820 || (diff == 0 && col < (int)(cb->start.col +
821 cb->end.col) / 2)))))
822 {
823 cb->origin_row = (short_u)cb->end.lnum;
824 cb->origin_start_col = cb->end.col - 1;
825 cb->origin_end_col = cb->end.col;
826 }
827 else
828 {
829 cb->origin_row = (short_u)cb->start.lnum;
830 cb->origin_start_col = cb->start.col;
831 cb->origin_end_col = cb->start.col;
832 }
833 if (cb->mode == SELECT_MODE_WORD && !repeated_click)
834 cb->mode = SELECT_MODE_CHAR;
835 }
836
837 /* set state, for when using the right mouse button */
838 cb->state = SELECT_IN_PROGRESS;
839
840#ifdef DEBUG_SELECTION
841 printf("Selection extending to (%d,%d)\n", row, col);
842#endif
843
844 if (repeated_click && ++cb->mode > SELECT_MODE_LINE)
845 cb->mode = SELECT_MODE_CHAR;
846
847 switch (cb->mode)
848 {
849 case SELECT_MODE_CHAR:
850 /* If we're on a different line, find where the line ends */
851 if (row != cb->prev.lnum)
852 cb->word_end_col = clip_get_line_end(row);
853
854 /* See if we are before or after the origin of the selection */
855 if (clip_compare_pos(row, col, cb->origin_row,
856 cb->origin_start_col) >= 0)
857 {
858 if (col >= (int)cb->word_end_col)
859 clip_update_modeless_selection(cb, cb->origin_row,
860 cb->origin_start_col, row, (int)Columns);
861 else
862 {
863#ifdef FEAT_MBYTE
864 if (has_mbyte && mb_lefthalve(row, col))
865 slen = 2;
866#endif
867 clip_update_modeless_selection(cb, cb->origin_row,
868 cb->origin_start_col, row, col + slen);
869 }
870 }
871 else
872 {
873#ifdef FEAT_MBYTE
874 if (has_mbyte
875 && mb_lefthalve(cb->origin_row, cb->origin_start_col))
876 slen = 2;
877#endif
878 if (col >= (int)cb->word_end_col)
879 clip_update_modeless_selection(cb, row, cb->word_end_col,
880 cb->origin_row, cb->origin_start_col + slen);
881 else
882 clip_update_modeless_selection(cb, row, col,
883 cb->origin_row, cb->origin_start_col + slen);
884 }
885 break;
886
887 case SELECT_MODE_WORD:
888 /* If we are still within the same word, do nothing */
889 if (row == cb->prev.lnum && col >= (int)cb->word_start_col
890 && col < (int)cb->word_end_col && !repeated_click)
891 return;
892
893 /* Get new word boundaries */
894 clip_get_word_boundaries(cb, row, col);
895
896 /* Handle being after the origin point of selection */
897 if (clip_compare_pos(row, col, cb->origin_row,
898 cb->origin_start_col) >= 0)
899 clip_update_modeless_selection(cb, cb->origin_row,
900 cb->origin_start_col, row, cb->word_end_col);
901 else
902 clip_update_modeless_selection(cb, row, cb->word_start_col,
903 cb->origin_row, cb->origin_end_col);
904 break;
905
906 case SELECT_MODE_LINE:
907 if (row == cb->prev.lnum && !repeated_click)
908 return;
909
910 if (clip_compare_pos(row, col, cb->origin_row,
911 cb->origin_start_col) >= 0)
912 clip_update_modeless_selection(cb, cb->origin_row, 0, row,
913 (int)Columns);
914 else
915 clip_update_modeless_selection(cb, row, 0, cb->origin_row,
916 (int)Columns);
917 break;
918 }
919
920 cb->prev.lnum = row;
921 cb->prev.col = col;
922
923#ifdef DEBUG_SELECTION
924 printf("Selection is: (%u,%u) to (%u,%u)\n", cb->start.lnum,
925 cb->start.col, cb->end.lnum, cb->end.col);
926#endif
927}
928
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929# if defined(FEAT_GUI) || defined(PROTO)
930/*
931 * Redraw part of the selection if character at "row,col" is inside of it.
932 * Only used for the GUI.
933 */
934 void
935clip_may_redraw_selection(row, col, len)
936 int row, col;
937 int len;
938{
939 int start = col;
940 int end = col + len;
941
942 if (clip_star.state != SELECT_CLEARED
943 && row >= clip_star.start.lnum
944 && row <= clip_star.end.lnum)
945 {
946 if (row == clip_star.start.lnum && start < (int)clip_star.start.col)
947 start = clip_star.start.col;
948 if (row == clip_star.end.lnum && end > (int)clip_star.end.col)
949 end = clip_star.end.col;
950 if (end > start)
951 clip_invert_area(row, start, row, end, 0);
952 }
953}
954# endif
955
956/*
957 * Called from outside to clear selected region from the display
958 */
959 void
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200960clip_clear_selection(cbd)
961 VimClipboard *cbd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200964 if (cbd->state == SELECT_CLEARED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 return;
966
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200967 clip_invert_area((int)cbd->start.lnum, cbd->start.col, (int)cbd->end.lnum,
968 cbd->end.col, CLIP_CLEAR);
969 cbd->state = SELECT_CLEARED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970}
971
972/*
973 * Clear the selection if any lines from "row1" to "row2" are inside of it.
974 */
975 void
976clip_may_clear_selection(row1, row2)
977 int row1, row2;
978{
979 if (clip_star.state == SELECT_DONE
980 && row2 >= clip_star.start.lnum
981 && row1 <= clip_star.end.lnum)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200982 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983}
984
985/*
986 * Called before the screen is scrolled up or down. Adjusts the line numbers
987 * of the selection. Call with big number when clearing the screen.
988 */
989 void
990clip_scroll_selection(rows)
991 int rows; /* negative for scroll down */
992{
993 int lnum;
994
995 if (clip_star.state == SELECT_CLEARED)
996 return;
997
998 lnum = clip_star.start.lnum - rows;
999 if (lnum <= 0)
1000 clip_star.start.lnum = 0;
1001 else if (lnum >= screen_Rows) /* scrolled off of the screen */
1002 clip_star.state = SELECT_CLEARED;
1003 else
1004 clip_star.start.lnum = lnum;
1005
1006 lnum = clip_star.end.lnum - rows;
1007 if (lnum < 0) /* scrolled off of the screen */
1008 clip_star.state = SELECT_CLEARED;
1009 else if (lnum >= screen_Rows)
1010 clip_star.end.lnum = screen_Rows - 1;
1011 else
1012 clip_star.end.lnum = lnum;
1013}
1014
1015/*
1016 * Invert a region of the display between a starting and ending row and column
1017 * Values for "how":
1018 * CLIP_CLEAR: undo inversion
1019 * CLIP_SET: set inversion
1020 * CLIP_TOGGLE: set inversion if pos1 < pos2, undo inversion otherwise.
1021 * 0: invert (GUI only).
1022 */
1023 static void
1024clip_invert_area(row1, col1, row2, col2, how)
1025 int row1;
1026 int col1;
1027 int row2;
1028 int col2;
1029 int how;
1030{
1031 int invert = FALSE;
1032
1033 if (how == CLIP_SET)
1034 invert = TRUE;
1035
1036 /* Swap the from and to positions so the from is always before */
1037 if (clip_compare_pos(row1, col1, row2, col2) > 0)
1038 {
1039 int tmp_row, tmp_col;
1040
1041 tmp_row = row1;
1042 tmp_col = col1;
1043 row1 = row2;
1044 col1 = col2;
1045 row2 = tmp_row;
1046 col2 = tmp_col;
1047 }
1048 else if (how == CLIP_TOGGLE)
1049 invert = TRUE;
1050
1051 /* If all on the same line, do it the easy way */
1052 if (row1 == row2)
1053 {
1054 clip_invert_rectangle(row1, col1, 1, col2 - col1, invert);
1055 }
1056 else
1057 {
1058 /* Handle a piece of the first line */
1059 if (col1 > 0)
1060 {
1061 clip_invert_rectangle(row1, col1, 1, (int)Columns - col1, invert);
1062 row1++;
1063 }
1064
1065 /* Handle a piece of the last line */
1066 if (col2 < Columns - 1)
1067 {
1068 clip_invert_rectangle(row2, 0, 1, col2, invert);
1069 row2--;
1070 }
1071
1072 /* Handle the rectangle thats left */
1073 if (row2 >= row1)
1074 clip_invert_rectangle(row1, 0, row2 - row1 + 1, (int)Columns,
1075 invert);
1076 }
1077}
1078
1079/*
1080 * Invert or un-invert a rectangle of the screen.
1081 * "invert" is true if the result is inverted.
1082 */
1083 static void
1084clip_invert_rectangle(row, col, height, width, invert)
1085 int row;
1086 int col;
1087 int height;
1088 int width;
1089 int invert;
1090{
1091#ifdef FEAT_GUI
1092 if (gui.in_use)
1093 gui_mch_invert_rectangle(row, col, height, width);
1094 else
1095#endif
1096 screen_draw_rectangle(row, col, height, width, invert);
1097}
1098
1099/*
1100 * Copy the currently selected area into the '*' register so it will be
1101 * available for pasting.
1102 * When "both" is TRUE also copy to the '+' register.
1103 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 void
1105clip_copy_modeless_selection(both)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001106 int both UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107{
1108 char_u *buffer;
1109 char_u *bufp;
1110 int row;
1111 int start_col;
1112 int end_col;
1113 int line_end_col;
1114 int add_newline_flag = FALSE;
1115 int len;
1116#ifdef FEAT_MBYTE
1117 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118#endif
1119 int row1 = clip_star.start.lnum;
1120 int col1 = clip_star.start.col;
1121 int row2 = clip_star.end.lnum;
1122 int col2 = clip_star.end.col;
1123
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001124 /* Can't use ScreenLines unless initialized */
1125 if (ScreenLines == NULL)
1126 return;
1127
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 /*
1129 * Make sure row1 <= row2, and if row1 == row2 that col1 <= col2.
1130 */
1131 if (row1 > row2)
1132 {
1133 row = row1; row1 = row2; row2 = row;
1134 row = col1; col1 = col2; col2 = row;
1135 }
1136 else if (row1 == row2 && col1 > col2)
1137 {
1138 row = col1; col1 = col2; col2 = row;
1139 }
1140#ifdef FEAT_MBYTE
1141 /* correct starting point for being on right halve of double-wide char */
1142 p = ScreenLines + LineOffset[row1];
1143 if (enc_dbcs != 0)
1144 col1 -= (*mb_head_off)(p, p + col1);
1145 else if (enc_utf8 && p[col1] == 0)
1146 --col1;
1147#endif
1148
1149 /* Create a temporary buffer for storing the text */
1150 len = (row2 - row1 + 1) * Columns + 1;
1151#ifdef FEAT_MBYTE
1152 if (enc_dbcs != 0)
1153 len *= 2; /* max. 2 bytes per display cell */
1154 else if (enc_utf8)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001155 len *= MB_MAXBYTES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156#endif
1157 buffer = lalloc((long_u)len, TRUE);
1158 if (buffer == NULL) /* out of memory */
1159 return;
1160
1161 /* Process each row in the selection */
1162 for (bufp = buffer, row = row1; row <= row2; row++)
1163 {
1164 if (row == row1)
1165 start_col = col1;
1166 else
1167 start_col = 0;
1168
1169 if (row == row2)
1170 end_col = col2;
1171 else
1172 end_col = Columns;
1173
1174 line_end_col = clip_get_line_end(row);
1175
1176 /* See if we need to nuke some trailing whitespace */
1177 if (end_col >= Columns && (row < row2 || end_col > line_end_col))
1178 {
1179 /* Get rid of trailing whitespace */
1180 end_col = line_end_col;
1181 if (end_col < start_col)
1182 end_col = start_col;
1183
1184 /* If the last line extended to the end, add an extra newline */
1185 if (row == row2)
1186 add_newline_flag = TRUE;
1187 }
1188
1189 /* If after the first row, we need to always add a newline */
1190 if (row > row1 && !LineWraps[row - 1])
1191 *bufp++ = NL;
1192
1193 if (row < screen_Rows && end_col <= screen_Columns)
1194 {
1195#ifdef FEAT_MBYTE
1196 if (enc_dbcs != 0)
1197 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001198 int i;
1199
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 p = ScreenLines + LineOffset[row];
1201 for (i = start_col; i < end_col; ++i)
1202 if (enc_dbcs == DBCS_JPNU && p[i] == 0x8e)
1203 {
1204 /* single-width double-byte char */
1205 *bufp++ = 0x8e;
1206 *bufp++ = ScreenLines2[LineOffset[row] + i];
1207 }
1208 else
1209 {
1210 *bufp++ = p[i];
1211 if (MB_BYTE2LEN(p[i]) == 2)
1212 *bufp++ = p[++i];
1213 }
1214 }
1215 else if (enc_utf8)
1216 {
1217 int off;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001218 int i;
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001219 int ci;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220
1221 off = LineOffset[row];
1222 for (i = start_col; i < end_col; ++i)
1223 {
1224 /* The base character is either in ScreenLinesUC[] or
1225 * ScreenLines[]. */
1226 if (ScreenLinesUC[off + i] == 0)
1227 *bufp++ = ScreenLines[off + i];
1228 else
1229 {
1230 bufp += utf_char2bytes(ScreenLinesUC[off + i], bufp);
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001231 for (ci = 0; ci < Screen_mco; ++ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001233 /* Add a composing character. */
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001234 if (ScreenLinesC[ci][off + i] == 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001235 break;
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001236 bufp += utf_char2bytes(ScreenLinesC[ci][off + i],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 }
1239 }
1240 /* Skip right halve of double-wide character. */
1241 if (ScreenLines[off + i + 1] == 0)
1242 ++i;
1243 }
1244 }
1245 else
1246#endif
1247 {
1248 STRNCPY(bufp, ScreenLines + LineOffset[row] + start_col,
1249 end_col - start_col);
1250 bufp += end_col - start_col;
1251 }
1252 }
1253 }
1254
1255 /* Add a newline at the end if the selection ended there */
1256 if (add_newline_flag)
1257 *bufp++ = NL;
1258
1259 /* First cleanup any old selection and become the owner. */
1260 clip_free_selection(&clip_star);
1261 clip_own_selection(&clip_star);
1262
1263 /* Yank the text into the '*' register. */
1264 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_star);
1265
1266 /* Make the register contents available to the outside world. */
1267 clip_gen_set_selection(&clip_star);
1268
1269#ifdef FEAT_X11
1270 if (both)
1271 {
1272 /* Do the same for the '+' register. */
1273 clip_free_selection(&clip_plus);
1274 clip_own_selection(&clip_plus);
1275 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_plus);
1276 clip_gen_set_selection(&clip_plus);
1277 }
1278#endif
1279 vim_free(buffer);
1280}
1281
1282/*
1283 * Find the starting and ending positions of the word at the given row and
1284 * column. Only white-separated words are recognized here.
1285 */
1286#define CHAR_CLASS(c) (c <= ' ' ? ' ' : vim_iswordc(c))
1287
1288 static void
1289clip_get_word_boundaries(cb, row, col)
1290 VimClipboard *cb;
1291 int row;
1292 int col;
1293{
1294 int start_class;
1295 int temp_col;
1296 char_u *p;
1297#ifdef FEAT_MBYTE
1298 int mboff;
1299#endif
1300
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001301 if (row >= screen_Rows || col >= screen_Columns || ScreenLines == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 return;
1303
1304 p = ScreenLines + LineOffset[row];
1305#ifdef FEAT_MBYTE
1306 /* Correct for starting in the right halve of a double-wide char */
1307 if (enc_dbcs != 0)
1308 col -= dbcs_screen_head_off(p, p + col);
1309 else if (enc_utf8 && p[col] == 0)
1310 --col;
1311#endif
1312 start_class = CHAR_CLASS(p[col]);
1313
1314 temp_col = col;
1315 for ( ; temp_col > 0; temp_col--)
1316#ifdef FEAT_MBYTE
1317 if (enc_dbcs != 0
1318 && (mboff = dbcs_screen_head_off(p, p + temp_col - 1)) > 0)
1319 temp_col -= mboff;
1320 else
1321#endif
1322 if (CHAR_CLASS(p[temp_col - 1]) != start_class
1323#ifdef FEAT_MBYTE
1324 && !(enc_utf8 && p[temp_col - 1] == 0)
1325#endif
1326 )
1327 break;
1328 cb->word_start_col = temp_col;
1329
1330 temp_col = col;
1331 for ( ; temp_col < screen_Columns; temp_col++)
1332#ifdef FEAT_MBYTE
1333 if (enc_dbcs != 0 && dbcs_ptr2cells(p + temp_col) == 2)
1334 ++temp_col;
1335 else
1336#endif
1337 if (CHAR_CLASS(p[temp_col]) != start_class
1338#ifdef FEAT_MBYTE
1339 && !(enc_utf8 && p[temp_col] == 0)
1340#endif
1341 )
1342 break;
1343 cb->word_end_col = temp_col;
1344}
1345
1346/*
1347 * Find the column position for the last non-whitespace character on the given
1348 * line.
1349 */
1350 static int
1351clip_get_line_end(row)
1352 int row;
1353{
1354 int i;
1355
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001356 if (row >= screen_Rows || ScreenLines == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 return 0;
1358 for (i = screen_Columns; i > 0; i--)
1359 if (ScreenLines[LineOffset[row] + i - 1] != ' ')
1360 break;
1361 return i;
1362}
1363
1364/*
1365 * Update the currently selected region by adding and/or subtracting from the
1366 * beginning or end and inverting the changed area(s).
1367 */
1368 static void
1369clip_update_modeless_selection(cb, row1, col1, row2, col2)
1370 VimClipboard *cb;
1371 int row1;
1372 int col1;
1373 int row2;
1374 int col2;
1375{
1376 /* See if we changed at the beginning of the selection */
1377 if (row1 != cb->start.lnum || col1 != (int)cb->start.col)
1378 {
1379 clip_invert_area(row1, col1, (int)cb->start.lnum, cb->start.col,
1380 CLIP_TOGGLE);
1381 cb->start.lnum = row1;
1382 cb->start.col = col1;
1383 }
1384
1385 /* See if we changed at the end of the selection */
1386 if (row2 != cb->end.lnum || col2 != (int)cb->end.col)
1387 {
1388 clip_invert_area((int)cb->end.lnum, cb->end.col, row2, col2,
1389 CLIP_TOGGLE);
1390 cb->end.lnum = row2;
1391 cb->end.col = col2;
1392 }
1393}
1394
1395 int
1396clip_gen_own_selection(cbd)
1397 VimClipboard *cbd;
1398{
1399#ifdef FEAT_XCLIPBOARD
1400# ifdef FEAT_GUI
1401 if (gui.in_use)
1402 return clip_mch_own_selection(cbd);
1403 else
1404# endif
1405 return clip_xterm_own_selection(cbd);
1406#else
1407 return clip_mch_own_selection(cbd);
1408#endif
1409}
1410
1411 void
1412clip_gen_lose_selection(cbd)
1413 VimClipboard *cbd;
1414{
1415#ifdef FEAT_XCLIPBOARD
1416# ifdef FEAT_GUI
1417 if (gui.in_use)
1418 clip_mch_lose_selection(cbd);
1419 else
1420# endif
1421 clip_xterm_lose_selection(cbd);
1422#else
1423 clip_mch_lose_selection(cbd);
1424#endif
1425}
1426
1427 void
1428clip_gen_set_selection(cbd)
1429 VimClipboard *cbd;
1430{
1431#ifdef FEAT_XCLIPBOARD
1432# ifdef FEAT_GUI
1433 if (gui.in_use)
1434 clip_mch_set_selection(cbd);
1435 else
1436# endif
1437 clip_xterm_set_selection(cbd);
1438#else
1439 clip_mch_set_selection(cbd);
1440#endif
1441}
1442
1443 void
1444clip_gen_request_selection(cbd)
1445 VimClipboard *cbd;
1446{
1447#ifdef FEAT_XCLIPBOARD
1448# ifdef FEAT_GUI
1449 if (gui.in_use)
1450 clip_mch_request_selection(cbd);
1451 else
1452# endif
1453 clip_xterm_request_selection(cbd);
1454#else
1455 clip_mch_request_selection(cbd);
1456#endif
1457}
1458
1459#endif /* FEAT_CLIPBOARD */
1460
1461/*****************************************************************************
1462 * Functions that handle the input buffer.
1463 * This is used for any GUI version, and the unix terminal version.
1464 *
1465 * For Unix, the input characters are buffered to be able to check for a
1466 * CTRL-C. This should be done with signals, but I don't know how to do that
1467 * in a portable way for a tty in RAW mode.
1468 *
1469 * For the client-server code in the console the received keys are put in the
1470 * input buffer.
1471 */
1472
1473#if defined(USE_INPUT_BUF) || defined(PROTO)
1474
1475/*
1476 * Internal typeahead buffer. Includes extra space for long key code
1477 * descriptions which would otherwise overflow. The buffer is considered full
1478 * when only this extra space (or part of it) remains.
1479 */
1480#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
1481 || defined(FEAT_CLIENTSERVER)
1482 /*
1483 * Sun WorkShop and NetBeans stuff debugger commands into the input buffer.
1484 * This requires a larger buffer...
1485 * (Madsen) Go with this for remote input as well ...
1486 */
1487# define INBUFLEN 4096
1488#else
1489# define INBUFLEN 250
1490#endif
1491
1492static char_u inbuf[INBUFLEN + MAX_KEY_CODE_LEN];
1493static int inbufcount = 0; /* number of chars in inbuf[] */
1494
1495/*
1496 * vim_is_input_buf_full(), vim_is_input_buf_empty(), add_to_input_buf(), and
1497 * trash_input_buf() are functions for manipulating the input buffer. These
1498 * are used by the gui_* calls when a GUI is used to handle keyboard input.
1499 */
1500
1501 int
1502vim_is_input_buf_full()
1503{
1504 return (inbufcount >= INBUFLEN);
1505}
1506
1507 int
1508vim_is_input_buf_empty()
1509{
1510 return (inbufcount == 0);
1511}
1512
1513#if defined(FEAT_OLE) || defined(PROTO)
1514 int
1515vim_free_in_input_buf()
1516{
1517 return (INBUFLEN - inbufcount);
1518}
1519#endif
1520
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001521#if defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 int
1523vim_used_in_input_buf()
1524{
1525 return inbufcount;
1526}
1527#endif
1528
1529#if defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) || defined(PROTO)
1530/*
1531 * Return the current contents of the input buffer and make it empty.
1532 * The returned pointer must be passed to set_input_buf() later.
1533 */
1534 char_u *
1535get_input_buf()
1536{
1537 garray_T *gap;
1538
1539 /* We use a growarray to store the data pointer and the length. */
1540 gap = (garray_T *)alloc((unsigned)sizeof(garray_T));
1541 if (gap != NULL)
1542 {
1543 /* Add one to avoid a zero size. */
1544 gap->ga_data = alloc((unsigned)inbufcount + 1);
1545 if (gap->ga_data != NULL)
1546 mch_memmove(gap->ga_data, inbuf, (size_t)inbufcount);
1547 gap->ga_len = inbufcount;
1548 }
1549 trash_input_buf();
1550 return (char_u *)gap;
1551}
1552
1553/*
1554 * Restore the input buffer with a pointer returned from get_input_buf().
1555 * The allocated memory is freed, this only works once!
1556 */
1557 void
1558set_input_buf(p)
1559 char_u *p;
1560{
1561 garray_T *gap = (garray_T *)p;
1562
1563 if (gap != NULL)
1564 {
1565 if (gap->ga_data != NULL)
1566 {
1567 mch_memmove(inbuf, gap->ga_data, gap->ga_len);
1568 inbufcount = gap->ga_len;
1569 vim_free(gap->ga_data);
1570 }
1571 vim_free(gap);
1572 }
1573}
1574#endif
1575
Bram Moolenaar446cb832008-06-24 21:56:24 +00001576#if defined(FEAT_GUI) \
1577 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 || defined(FEAT_XCLIPBOARD) || defined(VMS) \
Bram Moolenaarf52c7252006-02-10 23:23:57 +00001579 || defined(FEAT_SNIFF) || defined(FEAT_CLIENTSERVER) \
Bram Moolenaarf52c7252006-02-10 23:23:57 +00001580 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581/*
1582 * Add the given bytes to the input buffer
1583 * Special keys start with CSI. A real CSI must have been translated to
1584 * CSI KS_EXTRA KE_CSI. K_SPECIAL doesn't require translation.
1585 */
1586 void
1587add_to_input_buf(s, len)
1588 char_u *s;
1589 int len;
1590{
1591 if (inbufcount + len > INBUFLEN + MAX_KEY_CODE_LEN)
1592 return; /* Shouldn't ever happen! */
1593
1594#ifdef FEAT_HANGULIN
1595 if ((State & (INSERT|CMDLINE)) && hangul_input_state_get())
1596 if ((len = hangul_input_process(s, len)) == 0)
1597 return;
1598#endif
1599
1600 while (len--)
1601 inbuf[inbufcount++] = *s++;
1602}
1603#endif
1604
Bram Moolenaar70c2a632007-08-15 18:08:50 +00001605#if ((defined(FEAT_XIM) || defined(FEAT_DND)) && defined(FEAT_GUI_GTK)) \
1606 || defined(FEAT_GUI_MSWIN) \
1607 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 || (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) \
Bram Moolenaarf52c7252006-02-10 23:23:57 +00001609 || (defined(FEAT_GUI) && (!defined(USE_ON_FLY_SCROLL) \
1610 || defined(FEAT_MENU))) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 || defined(PROTO)
1612/*
1613 * Add "str[len]" to the input buffer while escaping CSI bytes.
1614 */
1615 void
1616add_to_input_buf_csi(char_u *str, int len)
1617{
1618 int i;
1619 char_u buf[2];
1620
1621 for (i = 0; i < len; ++i)
1622 {
1623 add_to_input_buf(str + i, 1);
1624 if (str[i] == CSI)
1625 {
1626 /* Turn CSI into K_CSI. */
1627 buf[0] = KS_EXTRA;
1628 buf[1] = (int)KE_CSI;
1629 add_to_input_buf(buf, 2);
1630 }
1631 }
1632}
1633#endif
1634
1635#if defined(FEAT_HANGULIN) || defined(PROTO)
1636 void
Bram Moolenaard44347f2011-06-19 01:14:29 +02001637push_raw_key(s, len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 char_u *s;
1639 int len;
1640{
1641 while (len--)
1642 inbuf[inbufcount++] = *s++;
1643}
1644#endif
1645
1646#if defined(FEAT_GUI) || defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) \
1647 || defined(PROTO)
1648/* Remove everything from the input buffer. Called when ^C is found */
1649 void
1650trash_input_buf()
1651{
1652 inbufcount = 0;
1653}
1654#endif
1655
1656/*
1657 * Read as much data from the input buffer as possible up to maxlen, and store
1658 * it in buf.
1659 * Note: this function used to be Read() in unix.c
1660 */
1661 int
1662read_from_input_buf(buf, maxlen)
1663 char_u *buf;
1664 long maxlen;
1665{
1666 if (inbufcount == 0) /* if the buffer is empty, fill it */
1667 fill_input_buf(TRUE);
1668 if (maxlen > inbufcount)
1669 maxlen = inbufcount;
1670 mch_memmove(buf, inbuf, (size_t)maxlen);
1671 inbufcount -= maxlen;
1672 if (inbufcount)
1673 mch_memmove(inbuf, inbuf + maxlen, (size_t)inbufcount);
1674 return (int)maxlen;
1675}
1676
1677 void
1678fill_input_buf(exit_on_error)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001679 int exit_on_error UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680{
1681#if defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X_UNIX)
1682 int len;
1683 int try;
1684 static int did_read_something = FALSE;
1685# ifdef FEAT_MBYTE
1686 static char_u *rest = NULL; /* unconverted rest of previous read */
1687 static int restlen = 0;
1688 int unconverted;
1689# endif
1690#endif
1691
1692#ifdef FEAT_GUI
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001693 if (gui.in_use
1694# ifdef NO_CONSOLE_INPUT
1695 /* Don't use the GUI input when the window hasn't been opened yet.
1696 * We get here from ui_inchar() when we should try reading from stdin. */
1697 && !no_console_input()
1698# endif
1699 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 {
1701 gui_mch_update();
1702 return;
1703 }
1704#endif
1705#if defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X_UNIX)
1706 if (vim_is_input_buf_full())
1707 return;
1708 /*
1709 * Fill_input_buf() is only called when we really need a character.
1710 * If we can't get any, but there is some in the buffer, just return.
1711 * If we can't get any, and there isn't any in the buffer, we give up and
1712 * exit Vim.
1713 */
1714# ifdef __BEOS__
1715 /*
1716 * On the BeBox version (for now), all input is secretly performed within
1717 * beos_select() which is called from RealWaitForChar().
1718 */
1719 while (!vim_is_input_buf_full() && RealWaitForChar(read_cmd_fd, 0, NULL))
1720 ;
1721 len = inbufcount;
1722 inbufcount = 0;
1723# else
1724
1725# ifdef FEAT_SNIFF
1726 if (sniff_request_waiting)
1727 {
1728 add_to_input_buf((char_u *)"\233sniff",6); /* results in K_SNIFF */
1729 sniff_request_waiting = 0;
1730 want_sniff_request = 0;
1731 return;
1732 }
1733# endif
1734
1735# ifdef FEAT_MBYTE
1736 if (rest != NULL)
1737 {
1738 /* Use remainder of previous call, starts with an invalid character
1739 * that may become valid when reading more. */
1740 if (restlen > INBUFLEN - inbufcount)
1741 unconverted = INBUFLEN - inbufcount;
1742 else
1743 unconverted = restlen;
1744 mch_memmove(inbuf + inbufcount, rest, unconverted);
1745 if (unconverted == restlen)
1746 {
1747 vim_free(rest);
1748 rest = NULL;
1749 }
1750 else
1751 {
1752 restlen -= unconverted;
1753 mch_memmove(rest, rest + unconverted, restlen);
1754 }
1755 inbufcount += unconverted;
1756 }
1757 else
1758 unconverted = 0;
1759#endif
1760
1761 len = 0; /* to avoid gcc warning */
1762 for (try = 0; try < 100; ++try)
1763 {
1764# ifdef VMS
1765 len = vms_read(
1766# else
1767 len = read(read_cmd_fd,
1768# endif
1769 (char *)inbuf + inbufcount, (size_t)((INBUFLEN - inbufcount)
1770# ifdef FEAT_MBYTE
1771 / input_conv.vc_factor
1772# endif
1773 ));
1774# if 0
1775 ) /* avoid syntax highlight error */
1776# endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001777
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 if (len > 0 || got_int)
1779 break;
1780 /*
1781 * If reading stdin results in an error, continue reading stderr.
1782 * This helps when using "foo | xargs vim".
1783 */
1784 if (!did_read_something && !isatty(read_cmd_fd) && read_cmd_fd == 0)
1785 {
1786 int m = cur_tmode;
1787
1788 /* We probably set the wrong file descriptor to raw mode. Switch
1789 * back to cooked mode, use another descriptor and set the mode to
1790 * what it was. */
1791 settmode(TMODE_COOK);
1792#ifdef HAVE_DUP
1793 /* Use stderr for stdin, also works for shell commands. */
1794 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00001795 ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796#else
1797 read_cmd_fd = 2; /* read from stderr instead of stdin */
1798#endif
1799 settmode(m);
1800 }
1801 if (!exit_on_error)
1802 return;
1803 }
1804# endif
1805 if (len <= 0 && !got_int)
1806 read_error_exit();
1807 if (len > 0)
1808 did_read_something = TRUE;
1809 if (got_int)
1810 {
1811 /* Interrupted, pretend a CTRL-C was typed. */
1812 inbuf[0] = 3;
1813 inbufcount = 1;
1814 }
1815 else
1816 {
1817# ifdef FEAT_MBYTE
1818 /*
1819 * May perform conversion on the input characters.
1820 * Include the unconverted rest of the previous call.
1821 * If there is an incomplete char at the end it is kept for the next
1822 * time, reading more bytes should make conversion possible.
1823 * Don't do this in the unlikely event that the input buffer is too
1824 * small ("rest" still contains more bytes).
1825 */
1826 if (input_conv.vc_type != CONV_NONE)
1827 {
1828 inbufcount -= unconverted;
1829 len = convert_input_safe(inbuf + inbufcount,
1830 len + unconverted, INBUFLEN - inbufcount,
1831 rest == NULL ? &rest : NULL, &restlen);
1832 }
1833# endif
1834 while (len-- > 0)
1835 {
1836 /*
1837 * if a CTRL-C was typed, remove it from the buffer and set got_int
1838 */
1839 if (inbuf[inbufcount] == 3 && ctrl_c_interrupts)
1840 {
1841 /* remove everything typed before the CTRL-C */
1842 mch_memmove(inbuf, inbuf + inbufcount, (size_t)(len + 1));
1843 inbufcount = 0;
1844 got_int = TRUE;
1845 }
1846 ++inbufcount;
1847 }
1848 }
1849#endif /* UNIX or OS2 or VMS*/
1850}
1851#endif /* defined(UNIX) || defined(FEAT_GUI) || defined(OS2) || defined(VMS) */
1852
1853/*
1854 * Exit because of an input read error.
1855 */
1856 void
1857read_error_exit()
1858{
1859 if (silent_mode) /* Normal way to exit for "ex -s" */
1860 getout(0);
1861 STRCPY(IObuff, _("Vim: Error reading input, exiting...\n"));
1862 preserve_exit();
1863}
1864
1865#if defined(CURSOR_SHAPE) || defined(PROTO)
1866/*
1867 * May update the shape of the cursor.
1868 */
1869 void
1870ui_cursor_shape()
1871{
1872# ifdef FEAT_GUI
1873 if (gui.in_use)
1874 gui_update_cursor_later();
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001875 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001877 term_cursor_shape();
1878
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879# ifdef MCH_CURSOR_SHAPE
1880 mch_update_cursor();
1881# endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001882
1883# ifdef FEAT_CONCEAL
Bram Moolenaar8e469272010-07-28 19:38:16 +02001884 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001885# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886}
1887#endif
1888
1889#if defined(FEAT_CLIPBOARD) || defined(FEAT_GUI) || defined(FEAT_RIGHTLEFT) \
Bram Moolenaaraf51e662008-07-14 19:48:05 +00001890 || defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891/*
1892 * Check bounds for column number
1893 */
1894 int
1895check_col(col)
1896 int col;
1897{
1898 if (col < 0)
1899 return 0;
1900 if (col >= (int)screen_Columns)
1901 return (int)screen_Columns - 1;
1902 return col;
1903}
1904
1905/*
1906 * Check bounds for row number
1907 */
1908 int
1909check_row(row)
1910 int row;
1911{
1912 if (row < 0)
1913 return 0;
1914 if (row >= (int)screen_Rows)
1915 return (int)screen_Rows - 1;
1916 return row;
1917}
1918#endif
1919
1920/*
1921 * Stuff for the X clipboard. Shared between VMS and Unix.
1922 */
1923
1924#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) || defined(PROTO)
1925# include <X11/Xatom.h>
1926# include <X11/Intrinsic.h>
1927
1928/*
1929 * Open the application context (if it hasn't been opened yet).
1930 * Used for Motif and Athena GUI and the xterm clipboard.
1931 */
1932 void
1933open_app_context()
1934{
1935 if (app_context == NULL)
1936 {
1937 XtToolkitInitialize();
1938 app_context = XtCreateApplicationContext();
1939 }
1940}
1941
1942static Atom vim_atom; /* Vim's own special selection format */
1943#ifdef FEAT_MBYTE
1944static Atom vimenc_atom; /* Vim's extended selection format */
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01001945static Atom utf8_atom;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946#endif
1947static Atom compound_text_atom;
1948static Atom text_atom;
1949static Atom targets_atom;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001950static Atom timestamp_atom; /* Used to get a timestamp */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951
1952 void
1953x11_setup_atoms(dpy)
1954 Display *dpy;
1955{
1956 vim_atom = XInternAtom(dpy, VIM_ATOM_NAME, False);
1957#ifdef FEAT_MBYTE
1958 vimenc_atom = XInternAtom(dpy, VIMENC_ATOM_NAME,False);
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01001959 utf8_atom = XInternAtom(dpy, "UTF8_STRING", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960#endif
1961 compound_text_atom = XInternAtom(dpy, "COMPOUND_TEXT", False);
1962 text_atom = XInternAtom(dpy, "TEXT", False);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001963 targets_atom = XInternAtom(dpy, "TARGETS", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 clip_star.sel_atom = XA_PRIMARY;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001965 clip_plus.sel_atom = XInternAtom(dpy, "CLIPBOARD", False);
1966 timestamp_atom = XInternAtom(dpy, "TIMESTAMP", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967}
1968
1969/*
1970 * X Selection stuff, for cutting and pasting text to other windows.
1971 */
1972
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001973static Boolean clip_x11_convert_selection_cb __ARGS((Widget, Atom *, Atom *, Atom *, XtPointer *, long_u *, int *));
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001974static void clip_x11_lose_ownership_cb __ARGS((Widget, Atom *));
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001975static void clip_x11_timestamp_cb __ARGS((Widget w, XtPointer n, XEvent *event, Boolean *cont));
Bram Moolenaar62b42182010-09-21 22:09:37 +02001976static void clip_x11_request_selection_cb __ARGS((Widget, XtPointer, Atom *, Atom *, XtPointer, long_u *, int *));
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001977
1978/*
1979 * Property callback to get a timestamp for XtOwnSelection.
1980 */
1981 static void
1982clip_x11_timestamp_cb(w, n, event, cont)
1983 Widget w;
1984 XtPointer n UNUSED;
1985 XEvent *event;
1986 Boolean *cont UNUSED;
1987{
1988 Atom actual_type;
1989 int format;
1990 unsigned long nitems, bytes_after;
1991 unsigned char *prop=NULL;
1992 XPropertyEvent *xproperty=&event->xproperty;
1993
1994 /* Must be a property notify, state can't be Delete (True), has to be
1995 * one of the supported selection types. */
1996 if (event->type != PropertyNotify || xproperty->state
1997 || (xproperty->atom != clip_star.sel_atom
1998 && xproperty->atom != clip_plus.sel_atom))
1999 return;
2000
2001 if (XGetWindowProperty(xproperty->display, xproperty->window,
2002 xproperty->atom, 0, 0, False, timestamp_atom, &actual_type, &format,
2003 &nitems, &bytes_after, &prop))
2004 return;
2005
2006 if (prop)
2007 XFree(prop);
2008
2009 /* Make sure the property type is "TIMESTAMP" and it's 32 bits. */
2010 if (actual_type != timestamp_atom || format != 32)
2011 return;
2012
2013 /* Get the selection, using the event timestamp. */
Bram Moolenaar62b42182010-09-21 22:09:37 +02002014 if (XtOwnSelection(w, xproperty->atom, xproperty->time,
2015 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
2016 NULL) == OK)
2017 {
2018 /* Set the "owned" flag now, there may have been a call to
2019 * lose_ownership_cb in between. */
2020 if (xproperty->atom == clip_plus.sel_atom)
2021 clip_plus.owned = TRUE;
2022 else
2023 clip_star.owned = TRUE;
2024 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002025}
2026
2027 void
2028x11_setup_selection(w)
2029 Widget w;
2030{
2031 XtAddEventHandler(w, PropertyChangeMask, False,
2032 /*(XtEventHandler)*/clip_x11_timestamp_cb, (XtPointer)NULL);
2033}
2034
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 static void
2036clip_x11_request_selection_cb(w, success, sel_atom, type, value, length,
2037 format)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002038 Widget w UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 XtPointer success;
2040 Atom *sel_atom;
2041 Atom *type;
2042 XtPointer value;
2043 long_u *length;
2044 int *format;
2045{
Bram Moolenaard44347f2011-06-19 01:14:29 +02002046 int motion_type = MAUTO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 long_u len;
2048 char_u *p;
2049 char **text_list = NULL;
2050 VimClipboard *cbd;
2051#ifdef FEAT_MBYTE
2052 char_u *tmpbuf = NULL;
2053#endif
2054
2055 if (*sel_atom == clip_plus.sel_atom)
2056 cbd = &clip_plus;
2057 else
2058 cbd = &clip_star;
2059
2060 if (value == NULL || *length == 0)
2061 {
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002062 clip_free_selection(cbd); /* nothing received, clear register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 *(int *)success = FALSE;
2064 return;
2065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 p = (char_u *)value;
2067 len = *length;
2068 if (*type == vim_atom)
2069 {
2070 motion_type = *p++;
2071 len--;
2072 }
2073
2074#ifdef FEAT_MBYTE
2075 else if (*type == vimenc_atom)
2076 {
2077 char_u *enc;
2078 vimconv_T conv;
2079 int convlen;
2080
2081 motion_type = *p++;
2082 --len;
2083
2084 enc = p;
2085 p += STRLEN(p) + 1;
2086 len -= p - enc;
2087
2088 /* If the encoding of the text is different from 'encoding', attempt
2089 * converting it. */
2090 conv.vc_type = CONV_NONE;
2091 convert_setup(&conv, enc, p_enc);
2092 if (conv.vc_type != CONV_NONE)
2093 {
2094 convlen = len; /* Need to use an int here. */
2095 tmpbuf = string_convert(&conv, p, &convlen);
2096 len = convlen;
2097 if (tmpbuf != NULL)
2098 p = tmpbuf;
2099 convert_setup(&conv, NULL, NULL);
2100 }
2101 }
2102#endif
2103
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002104 else if (*type == compound_text_atom
2105#ifdef FEAT_MBYTE
2106 || *type == utf8_atom
2107#endif
2108 || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109#ifdef FEAT_MBYTE
2110 enc_dbcs != 0 &&
2111#endif
2112 *type == text_atom))
2113 {
2114 XTextProperty text_prop;
2115 int n_text = 0;
2116 int status;
2117
2118 text_prop.value = (unsigned char *)value;
2119 text_prop.encoding = *type;
2120 text_prop.format = *format;
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002121 text_prop.nitems = len;
Bram Moolenaare2e663f2013-03-07 18:02:30 +01002122#ifdef FEAT_MBYTE
2123 if (*type == utf8_atom)
2124 status = Xutf8TextPropertyToTextList(X_DISPLAY, &text_prop,
2125 &text_list, &n_text);
2126 else
2127#endif
2128 status = XmbTextPropertyToTextList(X_DISPLAY, &text_prop,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 &text_list, &n_text);
2130 if (status != Success || n_text < 1)
2131 {
2132 *(int *)success = FALSE;
2133 return;
2134 }
2135 p = (char_u *)text_list[0];
2136 len = STRLEN(p);
2137 }
2138 clip_yank_selection(motion_type, p, (long)len, cbd);
2139
2140 if (text_list != NULL)
2141 XFreeStringList(text_list);
2142#ifdef FEAT_MBYTE
2143 vim_free(tmpbuf);
2144#endif
2145 XtFree((char *)value);
2146 *(int *)success = TRUE;
2147}
2148
2149 void
2150clip_x11_request_selection(myShell, dpy, cbd)
2151 Widget myShell;
2152 Display *dpy;
2153 VimClipboard *cbd;
2154{
2155 XEvent event;
2156 Atom type;
2157 static int success;
2158 int i;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002159 time_t start_time;
2160 int timed_out = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161
2162 for (i =
2163#ifdef FEAT_MBYTE
2164 0
2165#else
2166 1
2167#endif
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002168 ; i < 6; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 {
2170 switch (i)
2171 {
2172#ifdef FEAT_MBYTE
2173 case 0: type = vimenc_atom; break;
2174#endif
2175 case 1: type = vim_atom; break;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002176#ifdef FEAT_MBYTE
2177 case 2: type = utf8_atom; break;
2178#endif
2179 case 3: type = compound_text_atom; break;
2180 case 4: type = text_atom; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 default: type = XA_STRING;
2182 }
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002183#ifdef FEAT_MBYTE
2184 if (type == utf8_atom && !enc_utf8)
2185 /* Only request utf-8 when 'encoding' is utf8. */
2186 continue;
2187#endif
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002188 success = MAYBE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 XtGetSelectionValue(myShell, cbd->sel_atom, type,
2190 clip_x11_request_selection_cb, (XtPointer)&success, CurrentTime);
2191
2192 /* Make sure the request for the selection goes out before waiting for
2193 * a response. */
2194 XFlush(dpy);
2195
2196 /*
2197 * Wait for result of selection request, otherwise if we type more
2198 * characters, then they will appear before the one that requested the
2199 * paste! Don't worry, we will catch up with any other events later.
2200 */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002201 start_time = time(NULL);
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002202 while (success == MAYBE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 {
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002204 if (XCheckTypedEvent(dpy, SelectionNotify, &event)
2205 || XCheckTypedEvent(dpy, SelectionRequest, &event)
2206 || XCheckTypedEvent(dpy, PropertyNotify, &event))
Bram Moolenaar89417b92008-09-07 19:48:53 +00002207 {
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002208 /* This is where clip_x11_request_selection_cb() should be
2209 * called. It may actually happen a bit later, so we loop
2210 * until "success" changes.
2211 * We may get a SelectionRequest here and if we don't handle
2212 * it we hang. KDE klipper does this, for example.
2213 * We need to handle a PropertyNotify for large selections. */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002214 XtDispatchEvent(&event);
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002215 continue;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217
Bram Moolenaar89417b92008-09-07 19:48:53 +00002218 /* Time out after 2 to 3 seconds to avoid that we hang when the
2219 * other process doesn't respond. Note that the SelectionNotify
2220 * event may still come later when the selection owner comes back
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002221 * to life and the text gets inserted unexpectedly. Don't know
2222 * why that happens or how to avoid that :-(. */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002223 if (time(NULL) > start_time + 2)
2224 {
2225 timed_out = TRUE;
2226 break;
2227 }
2228
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 /* Do we need this? Probably not. */
2230 XSync(dpy, False);
2231
Bram Moolenaar89417b92008-09-07 19:48:53 +00002232 /* Wait for 1 msec to avoid that we eat up all CPU time. */
2233 ui_delay(1L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 }
2235
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002236 if (success == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 return;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002238
2239 /* don't do a retry with another type after timing out, otherwise we
2240 * hang for 15 seconds. */
2241 if (timed_out)
2242 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 }
2244
2245 /* Final fallback position - use the X CUT_BUFFER0 store */
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002246 yank_cut_buffer0(dpy, cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247}
2248
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 static Boolean
2250clip_x11_convert_selection_cb(w, sel_atom, target, type, value, length, format)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002251 Widget w UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 Atom *sel_atom;
2253 Atom *target;
2254 Atom *type;
2255 XtPointer *value;
2256 long_u *length;
2257 int *format;
2258{
2259 char_u *string;
2260 char_u *result;
2261 int motion_type;
2262 VimClipboard *cbd;
2263 int i;
2264
2265 if (*sel_atom == clip_plus.sel_atom)
2266 cbd = &clip_plus;
2267 else
2268 cbd = &clip_star;
2269
2270 if (!cbd->owned)
2271 return False; /* Shouldn't ever happen */
2272
2273 /* requestor wants to know what target types we support */
2274 if (*target == targets_atom)
2275 {
2276 Atom *array;
2277
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002278 if ((array = (Atom *)XtMalloc((unsigned)(sizeof(Atom) * 7))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 return False;
2280 *value = (XtPointer)array;
2281 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 array[i++] = targets_atom;
2283#ifdef FEAT_MBYTE
2284 array[i++] = vimenc_atom;
2285#endif
2286 array[i++] = vim_atom;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002287#ifdef FEAT_MBYTE
2288 if (enc_utf8)
2289 array[i++] = utf8_atom;
2290#endif
2291 array[i++] = XA_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 array[i++] = text_atom;
2293 array[i++] = compound_text_atom;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002294
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 *type = XA_ATOM;
2296 /* This used to be: *format = sizeof(Atom) * 8; but that caused
2297 * crashes on 64 bit machines. (Peter Derr) */
2298 *format = 32;
2299 *length = i;
2300 return True;
2301 }
2302
2303 if ( *target != XA_STRING
2304#ifdef FEAT_MBYTE
2305 && *target != vimenc_atom
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002306 && *target != utf8_atom
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307#endif
2308 && *target != vim_atom
2309 && *target != text_atom
2310 && *target != compound_text_atom)
2311 return False;
2312
2313 clip_get_selection(cbd);
2314 motion_type = clip_convert_selection(&string, length, cbd);
2315 if (motion_type < 0)
2316 return False;
2317
2318 /* For our own format, the first byte contains the motion type */
2319 if (*target == vim_atom)
2320 (*length)++;
2321
2322#ifdef FEAT_MBYTE
2323 /* Our own format with encoding: motion 'encoding' NUL text */
2324 if (*target == vimenc_atom)
2325 *length += STRLEN(p_enc) + 2;
2326#endif
2327
2328 *value = XtMalloc((Cardinal)*length);
2329 result = (char_u *)*value;
2330 if (result == NULL)
2331 {
2332 vim_free(string);
2333 return False;
2334 }
2335
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002336 if (*target == XA_STRING
2337#ifdef FEAT_MBYTE
2338 || (*target == utf8_atom && enc_utf8)
2339#endif
2340 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 {
2342 mch_memmove(result, string, (size_t)(*length));
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002343 *type = *target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 }
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002345 else if (*target == compound_text_atom || *target == text_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 {
2347 XTextProperty text_prop;
2348 char *string_nt = (char *)alloc((unsigned)*length + 1);
2349
2350 /* create NUL terminated string which XmbTextListToTextProperty wants */
2351 mch_memmove(string_nt, string, (size_t)*length);
2352 string_nt[*length] = NUL;
2353 XmbTextListToTextProperty(X_DISPLAY, (char **)&string_nt, 1,
2354 XCompoundTextStyle, &text_prop);
2355 vim_free(string_nt);
2356 XtFree(*value); /* replace with COMPOUND text */
2357 *value = (XtPointer)(text_prop.value); /* from plain text */
2358 *length = text_prop.nitems;
2359 *type = compound_text_atom;
2360 }
2361
2362#ifdef FEAT_MBYTE
2363 else if (*target == vimenc_atom)
2364 {
2365 int l = STRLEN(p_enc);
2366
2367 result[0] = motion_type;
2368 STRCPY(result + 1, p_enc);
2369 mch_memmove(result + l + 2, string, (size_t)(*length - l - 2));
2370 *type = vimenc_atom;
2371 }
2372#endif
2373
2374 else
2375 {
2376 result[0] = motion_type;
2377 mch_memmove(result + 1, string, (size_t)(*length - 1));
2378 *type = vim_atom;
2379 }
2380 *format = 8; /* 8 bits per char */
2381 vim_free(string);
2382 return True;
2383}
2384
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 static void
2386clip_x11_lose_ownership_cb(w, sel_atom)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002387 Widget w UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 Atom *sel_atom;
2389{
2390 if (*sel_atom == clip_plus.sel_atom)
2391 clip_lose_selection(&clip_plus);
2392 else
2393 clip_lose_selection(&clip_star);
2394}
2395
2396 void
2397clip_x11_lose_selection(myShell, cbd)
Bram Moolenaar62b42182010-09-21 22:09:37 +02002398 Widget myShell;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 VimClipboard *cbd;
2400{
2401 XtDisownSelection(myShell, cbd->sel_atom, CurrentTime);
2402}
2403
2404 int
2405clip_x11_own_selection(myShell, cbd)
Bram Moolenaar62b42182010-09-21 22:09:37 +02002406 Widget myShell;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 VimClipboard *cbd;
2408{
Bram Moolenaar62b42182010-09-21 22:09:37 +02002409 /* When using the GUI we have proper timestamps, use the one of the last
2410 * event. When in the console we don't get events (the terminal gets
2411 * them), Get the time by a zero-length append, clip_x11_timestamp_cb will
2412 * be called with the current timestamp. */
2413#ifdef FEAT_GUI
2414 if (gui.in_use)
2415 {
2416 if (XtOwnSelection(myShell, cbd->sel_atom,
2417 XtLastTimestampProcessed(XtDisplay(myShell)),
2418 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
2419 NULL) == False)
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +01002420 return FAIL;
Bram Moolenaar62b42182010-09-21 22:09:37 +02002421 }
2422 else
2423#endif
2424 {
2425 if (!XChangeProperty(XtDisplay(myShell), XtWindow(myShell),
2426 cbd->sel_atom, timestamp_atom, 32, PropModeAppend, NULL, 0))
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +01002427 return FAIL;
Bram Moolenaar62b42182010-09-21 22:09:37 +02002428 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002429 /* Flush is required in a terminal as nothing else is doing it. */
2430 XFlush(XtDisplay(myShell));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 return OK;
2432}
2433
2434/*
2435 * Send the current selection to the clipboard. Do nothing for X because we
2436 * will fill in the selection only when requested by another app.
2437 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 void
2439clip_x11_set_selection(cbd)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002440 VimClipboard *cbd UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441{
2442}
2443#endif
2444
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002445#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) \
2446 || defined(FEAT_GUI_GTK) || defined(PROTO)
2447/*
2448 * Get the contents of the X CUT_BUFFER0 and put it in "cbd".
2449 */
2450 void
2451yank_cut_buffer0(dpy, cbd)
2452 Display *dpy;
2453 VimClipboard *cbd;
2454{
2455 int nbytes = 0;
2456 char_u *buffer = (char_u *)XFetchBuffer(dpy, &nbytes, 0);
2457
2458 if (nbytes > 0)
2459 {
2460#ifdef FEAT_MBYTE
2461 int done = FALSE;
2462
2463 /* CUT_BUFFER0 is supposed to be always latin1. Convert to 'enc' when
2464 * using a multi-byte encoding. Conversion between two 8-bit
2465 * character sets usually fails and the text might actually be in
2466 * 'enc' anyway. */
2467 if (has_mbyte)
2468 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002469 char_u *conv_buf;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002470 vimconv_T vc;
2471
2472 vc.vc_type = CONV_NONE;
2473 if (convert_setup(&vc, (char_u *)"latin1", p_enc) == OK)
2474 {
2475 conv_buf = string_convert(&vc, buffer, &nbytes);
2476 if (conv_buf != NULL)
2477 {
2478 clip_yank_selection(MCHAR, conv_buf, (long)nbytes, cbd);
2479 vim_free(conv_buf);
2480 done = TRUE;
2481 }
2482 convert_setup(&vc, NULL, NULL);
2483 }
2484 }
2485 if (!done) /* use the text without conversion */
2486#endif
2487 clip_yank_selection(MCHAR, buffer, (long)nbytes, cbd);
2488 XFree((void *)buffer);
2489 if (p_verbose > 0)
2490 {
2491 verbose_enter();
2492 verb_msg((char_u *)_("Used CUT_BUFFER0 instead of empty selection"));
2493 verbose_leave();
2494 }
2495 }
2496}
2497#endif
2498
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499#if defined(FEAT_MOUSE) || defined(PROTO)
2500
2501/*
2502 * Move the cursor to the specified row and column on the screen.
Bram Moolenaar49325942007-05-10 19:19:59 +00002503 * Change current window if necessary. Returns an integer with the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 * CURSOR_MOVED bit set if the cursor has moved or unset otherwise.
2505 *
2506 * The MOUSE_FOLD_CLOSE bit is set when clicked on the '-' in a fold column.
2507 * The MOUSE_FOLD_OPEN bit is set when clicked on the '+' in a fold column.
2508 *
2509 * If flags has MOUSE_FOCUS, then the current window will not be changed, and
2510 * if the mouse is outside the window then the text will scroll, or if the
2511 * mouse was previously on a status line, then the status line may be dragged.
2512 *
2513 * If flags has MOUSE_MAY_VIS, then VIsual mode will be started before the
2514 * cursor is moved unless the cursor was on a status line.
2515 * This function returns one of IN_UNKNOWN, IN_BUFFER, IN_STATUS_LINE or
2516 * IN_SEP_LINE depending on where the cursor was clicked.
2517 *
2518 * If flags has MOUSE_MAY_STOP_VIS, then Visual mode will be stopped, unless
2519 * the mouse is on the status line of the same window.
2520 *
2521 * If flags has MOUSE_DID_MOVE, nothing is done if the mouse didn't move since
2522 * the last call.
2523 *
2524 * If flags has MOUSE_SETPOS, nothing is done, only the current position is
2525 * remembered.
2526 */
2527 int
2528jump_to_mouse(flags, inclusive, which_button)
2529 int flags;
2530 int *inclusive; /* used for inclusive operator, can be NULL */
2531 int which_button; /* MOUSE_LEFT, MOUSE_RIGHT, MOUSE_MIDDLE */
2532{
2533 static int on_status_line = 0; /* #lines below bottom of window */
2534#ifdef FEAT_VERTSPLIT
2535 static int on_sep_line = 0; /* on separator right of window */
2536#endif
2537 static int prev_row = -1;
2538 static int prev_col = -1;
2539 static win_T *dragwin = NULL; /* window being dragged */
2540 static int did_drag = FALSE; /* drag was noticed */
2541
2542 win_T *wp, *old_curwin;
2543 pos_T old_cursor;
2544 int count;
2545 int first;
2546 int row = mouse_row;
2547 int col = mouse_col;
2548#ifdef FEAT_FOLDING
2549 int mouse_char;
2550#endif
2551
2552 mouse_past_bottom = FALSE;
2553 mouse_past_eol = FALSE;
2554
2555 if (flags & MOUSE_RELEASED)
2556 {
2557 /* On button release we may change window focus if positioned on a
2558 * status line and no dragging happened. */
2559 if (dragwin != NULL && !did_drag)
2560 flags &= ~(MOUSE_FOCUS | MOUSE_DID_MOVE);
2561 dragwin = NULL;
2562 did_drag = FALSE;
2563 }
2564
2565 if ((flags & MOUSE_DID_MOVE)
2566 && prev_row == mouse_row
2567 && prev_col == mouse_col)
2568 {
2569retnomove:
Bram Moolenaar49325942007-05-10 19:19:59 +00002570 /* before moving the cursor for a left click which is NOT in a status
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 * line, stop Visual mode */
2572 if (on_status_line)
2573 return IN_STATUS_LINE;
2574#ifdef FEAT_VERTSPLIT
2575 if (on_sep_line)
2576 return IN_SEP_LINE;
2577#endif
2578#ifdef FEAT_VISUAL
2579 if (flags & MOUSE_MAY_STOP_VIS)
2580 {
2581 end_visual_mode();
2582 redraw_curbuf_later(INVERTED); /* delete the inversion */
2583 }
2584#endif
2585#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2586 /* Continue a modeless selection in another window. */
2587 if (cmdwin_type != 0 && row < W_WINROW(curwin))
2588 return IN_OTHER_WIN;
2589#endif
2590 return IN_BUFFER;
2591 }
2592
2593 prev_row = mouse_row;
2594 prev_col = mouse_col;
2595
2596 if (flags & MOUSE_SETPOS)
2597 goto retnomove; /* ugly goto... */
2598
2599#ifdef FEAT_FOLDING
2600 /* Remember the character under the mouse, it might be a '-' or '+' in the
2601 * fold column. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002602 if (row >= 0 && row < Rows && col >= 0 && col <= Columns
2603 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 mouse_char = ScreenLines[LineOffset[row] + col];
2605 else
2606 mouse_char = ' ';
2607#endif
2608
2609 old_curwin = curwin;
2610 old_cursor = curwin->w_cursor;
2611
2612 if (!(flags & MOUSE_FOCUS))
2613 {
2614 if (row < 0 || col < 0) /* check if it makes sense */
2615 return IN_UNKNOWN;
2616
2617#ifdef FEAT_WINDOWS
2618 /* find the window where the row is in */
2619 wp = mouse_find_win(&row, &col);
2620#else
2621 wp = firstwin;
2622#endif
2623 dragwin = NULL;
2624 /*
2625 * winpos and height may change in win_enter()!
2626 */
2627 if (row >= wp->w_height) /* In (or below) status line */
2628 {
2629 on_status_line = row - wp->w_height + 1;
2630 dragwin = wp;
2631 }
2632 else
2633 on_status_line = 0;
2634#ifdef FEAT_VERTSPLIT
2635 if (col >= wp->w_width) /* In separator line */
2636 {
2637 on_sep_line = col - wp->w_width + 1;
2638 dragwin = wp;
2639 }
2640 else
2641 on_sep_line = 0;
2642
2643 /* The rightmost character of the status line might be a vertical
2644 * separator character if there is no connecting window to the right. */
2645 if (on_status_line && on_sep_line)
2646 {
2647 if (stl_connected(wp))
2648 on_sep_line = 0;
2649 else
2650 on_status_line = 0;
2651 }
2652#endif
2653
2654#ifdef FEAT_VISUAL
2655 /* Before jumping to another buffer, or moving the cursor for a left
2656 * click, stop Visual mode. */
2657 if (VIsual_active
2658 && (wp->w_buffer != curwin->w_buffer
2659 || (!on_status_line
2660# ifdef FEAT_VERTSPLIT
2661 && !on_sep_line
2662# endif
2663# ifdef FEAT_FOLDING
2664 && (
2665# ifdef FEAT_RIGHTLEFT
2666 wp->w_p_rl ? col < W_WIDTH(wp) - wp->w_p_fdc :
2667# endif
2668 col >= wp->w_p_fdc
2669# ifdef FEAT_CMDWIN
2670 + (cmdwin_type == 0 && wp == curwin ? 0 : 1)
2671# endif
2672 )
2673# endif
2674 && (flags & MOUSE_MAY_STOP_VIS))))
2675 {
2676 end_visual_mode();
2677 redraw_curbuf_later(INVERTED); /* delete the inversion */
2678 }
2679#endif
2680#ifdef FEAT_CMDWIN
2681 if (cmdwin_type != 0 && wp != curwin)
2682 {
2683 /* A click outside the command-line window: Use modeless
Bram Moolenaarf679a432010-03-02 18:16:09 +01002684 * selection if possible. Allow dragging the status lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685# ifdef FEAT_VERTSPLIT
2686 on_sep_line = 0;
2687# endif
2688# ifdef FEAT_CLIPBOARD
2689 if (on_status_line)
2690 return IN_STATUS_LINE;
2691 return IN_OTHER_WIN;
2692# else
2693 row = 0;
2694 col += wp->w_wincol;
2695 wp = curwin;
2696# endif
2697 }
2698#endif
2699#ifdef FEAT_WINDOWS
2700 /* Only change window focus when not clicking on or dragging the
2701 * status line. Do change focus when releasing the mouse button
2702 * (MOUSE_FOCUS was set above if we dragged first). */
2703 if (dragwin == NULL || (flags & MOUSE_RELEASED))
2704 win_enter(wp, TRUE); /* can make wp invalid! */
2705# ifdef CHECK_DOUBLE_CLICK
2706 /* set topline, to be able to check for double click ourselves */
2707 if (curwin != old_curwin)
2708 set_mouse_topline(curwin);
2709# endif
2710#endif
2711 if (on_status_line) /* In (or below) status line */
2712 {
2713 /* Don't use start_arrow() if we're in the same window */
2714 if (curwin == old_curwin)
2715 return IN_STATUS_LINE;
2716 else
2717 return IN_STATUS_LINE | CURSOR_MOVED;
2718 }
2719#ifdef FEAT_VERTSPLIT
2720 if (on_sep_line) /* In (or below) status line */
2721 {
2722 /* Don't use start_arrow() if we're in the same window */
2723 if (curwin == old_curwin)
2724 return IN_SEP_LINE;
2725 else
2726 return IN_SEP_LINE | CURSOR_MOVED;
2727 }
2728#endif
2729
2730 curwin->w_cursor.lnum = curwin->w_topline;
2731#ifdef FEAT_GUI
2732 /* remember topline, needed for double click */
2733 gui_prev_topline = curwin->w_topline;
2734# ifdef FEAT_DIFF
2735 gui_prev_topfill = curwin->w_topfill;
2736# endif
2737#endif
2738 }
2739 else if (on_status_line && which_button == MOUSE_LEFT)
2740 {
2741#ifdef FEAT_WINDOWS
2742 if (dragwin != NULL)
2743 {
2744 /* Drag the status line */
2745 count = row - dragwin->w_winrow - dragwin->w_height + 1
2746 - on_status_line;
2747 win_drag_status_line(dragwin, count);
2748 did_drag |= count;
2749 }
2750#endif
2751 return IN_STATUS_LINE; /* Cursor didn't move */
2752 }
2753#ifdef FEAT_VERTSPLIT
2754 else if (on_sep_line && which_button == MOUSE_LEFT)
2755 {
2756 if (dragwin != NULL)
2757 {
2758 /* Drag the separator column */
2759 count = col - dragwin->w_wincol - dragwin->w_width + 1
2760 - on_sep_line;
2761 win_drag_vsep_line(dragwin, count);
2762 did_drag |= count;
2763 }
2764 return IN_SEP_LINE; /* Cursor didn't move */
2765 }
2766#endif
2767 else /* keep_window_focus must be TRUE */
2768 {
2769#ifdef FEAT_VISUAL
2770 /* before moving the cursor for a left click, stop Visual mode */
2771 if (flags & MOUSE_MAY_STOP_VIS)
2772 {
2773 end_visual_mode();
2774 redraw_curbuf_later(INVERTED); /* delete the inversion */
2775 }
2776#endif
2777
2778#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2779 /* Continue a modeless selection in another window. */
2780 if (cmdwin_type != 0 && row < W_WINROW(curwin))
2781 return IN_OTHER_WIN;
2782#endif
2783
2784 row -= W_WINROW(curwin);
2785#ifdef FEAT_VERTSPLIT
2786 col -= W_WINCOL(curwin);
2787#endif
2788
2789 /*
2790 * When clicking beyond the end of the window, scroll the screen.
2791 * Scroll by however many rows outside the window we are.
2792 */
2793 if (row < 0)
2794 {
2795 count = 0;
2796 for (first = TRUE; curwin->w_topline > 1; )
2797 {
2798#ifdef FEAT_DIFF
2799 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
2800 ++count;
2801 else
2802#endif
2803 count += plines(curwin->w_topline - 1);
2804 if (!first && count > -row)
2805 break;
2806 first = FALSE;
2807#ifdef FEAT_FOLDING
2808 hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2809#endif
2810#ifdef FEAT_DIFF
2811 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
2812 ++curwin->w_topfill;
2813 else
2814#endif
2815 {
2816 --curwin->w_topline;
2817#ifdef FEAT_DIFF
2818 curwin->w_topfill = 0;
2819#endif
2820 }
2821 }
2822#ifdef FEAT_DIFF
2823 check_topfill(curwin, FALSE);
2824#endif
2825 curwin->w_valid &=
2826 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2827 redraw_later(VALID);
2828 row = 0;
2829 }
2830 else if (row >= curwin->w_height)
2831 {
2832 count = 0;
2833 for (first = TRUE; curwin->w_topline < curbuf->b_ml.ml_line_count; )
2834 {
2835#ifdef FEAT_DIFF
2836 if (curwin->w_topfill > 0)
2837 ++count;
2838 else
2839#endif
2840 count += plines(curwin->w_topline);
2841 if (!first && count > row - curwin->w_height + 1)
2842 break;
2843 first = FALSE;
2844#ifdef FEAT_FOLDING
2845 if (hasFolding(curwin->w_topline, NULL, &curwin->w_topline)
2846 && curwin->w_topline == curbuf->b_ml.ml_line_count)
2847 break;
2848#endif
2849#ifdef FEAT_DIFF
2850 if (curwin->w_topfill > 0)
2851 --curwin->w_topfill;
2852 else
2853#endif
2854 {
2855 ++curwin->w_topline;
2856#ifdef FEAT_DIFF
2857 curwin->w_topfill =
2858 diff_check_fill(curwin, curwin->w_topline);
2859#endif
2860 }
2861 }
2862#ifdef FEAT_DIFF
2863 check_topfill(curwin, FALSE);
2864#endif
2865 redraw_later(VALID);
2866 curwin->w_valid &=
2867 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2868 row = curwin->w_height - 1;
2869 }
2870 else if (row == 0)
2871 {
2872 /* When dragging the mouse, while the text has been scrolled up as
2873 * far as it goes, moving the mouse in the top line should scroll
2874 * the text down (done later when recomputing w_topline). */
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00002875 if (mouse_dragging > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 && curwin->w_cursor.lnum
2877 == curwin->w_buffer->b_ml.ml_line_count
2878 && curwin->w_cursor.lnum == curwin->w_topline)
2879 curwin->w_valid &= ~(VALID_TOPLINE);
2880 }
2881 }
2882
2883#ifdef FEAT_FOLDING
2884 /* Check for position outside of the fold column. */
2885 if (
2886# ifdef FEAT_RIGHTLEFT
2887 curwin->w_p_rl ? col < W_WIDTH(curwin) - curwin->w_p_fdc :
2888# endif
2889 col >= curwin->w_p_fdc
2890# ifdef FEAT_CMDWIN
2891 + (cmdwin_type == 0 ? 0 : 1)
2892# endif
2893 )
2894 mouse_char = ' ';
2895#endif
2896
2897 /* compute the position in the buffer line from the posn on the screen */
2898 if (mouse_comp_pos(curwin, &row, &col, &curwin->w_cursor.lnum))
2899 mouse_past_bottom = TRUE;
2900
2901#ifdef FEAT_VISUAL
2902 /* Start Visual mode before coladvance(), for when 'sel' != "old" */
2903 if ((flags & MOUSE_MAY_VIS) && !VIsual_active)
2904 {
2905 check_visual_highlight();
2906 VIsual = old_cursor;
2907 VIsual_active = TRUE;
2908 VIsual_reselect = TRUE;
2909 /* if 'selectmode' contains "mouse", start Select mode */
2910 may_start_select('o');
2911 setmouse();
Bram Moolenaar7df351e2006-01-23 22:30:28 +00002912 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 redraw_cmdline = TRUE; /* show visual mode later */
2914 }
2915#endif
2916
2917 curwin->w_curswant = col;
2918 curwin->w_set_curswant = FALSE; /* May still have been TRUE */
2919 if (coladvance(col) == FAIL) /* Mouse click beyond end of line */
2920 {
2921 if (inclusive != NULL)
2922 *inclusive = TRUE;
2923 mouse_past_eol = TRUE;
2924 }
2925 else if (inclusive != NULL)
2926 *inclusive = FALSE;
2927
2928 count = IN_BUFFER;
2929 if (curwin != old_curwin || curwin->w_cursor.lnum != old_cursor.lnum
2930 || curwin->w_cursor.col != old_cursor.col)
2931 count |= CURSOR_MOVED; /* Cursor has moved */
2932
2933#ifdef FEAT_FOLDING
2934 if (mouse_char == '+')
2935 count |= MOUSE_FOLD_OPEN;
2936 else if (mouse_char != ' ')
2937 count |= MOUSE_FOLD_CLOSE;
2938#endif
2939
2940 return count;
2941}
2942
2943/*
2944 * Compute the position in the buffer line from the posn on the screen in
2945 * window "win".
2946 * Returns TRUE if the position is below the last line.
2947 */
2948 int
2949mouse_comp_pos(win, rowp, colp, lnump)
2950 win_T *win;
2951 int *rowp;
2952 int *colp;
2953 linenr_T *lnump;
2954{
2955 int col = *colp;
2956 int row = *rowp;
2957 linenr_T lnum;
2958 int retval = FALSE;
2959 int off;
2960 int count;
2961
2962#ifdef FEAT_RIGHTLEFT
2963 if (win->w_p_rl)
2964 col = W_WIDTH(win) - 1 - col;
2965#endif
2966
2967 lnum = win->w_topline;
2968
2969 while (row > 0)
2970 {
2971#ifdef FEAT_DIFF
2972 /* Don't include filler lines in "count" */
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002973 if (win->w_p_diff
2974# ifdef FEAT_FOLDING
2975 && !hasFoldingWin(win, lnum, NULL, NULL, TRUE, NULL)
2976# endif
2977 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 {
2979 if (lnum == win->w_topline)
2980 row -= win->w_topfill;
2981 else
2982 row -= diff_check_fill(win, lnum);
2983 count = plines_win_nofill(win, lnum, TRUE);
2984 }
2985 else
2986#endif
2987 count = plines_win(win, lnum, TRUE);
2988 if (count > row)
2989 break; /* Position is in this buffer line. */
2990#ifdef FEAT_FOLDING
2991 (void)hasFoldingWin(win, lnum, NULL, &lnum, TRUE, NULL);
2992#endif
2993 if (lnum == win->w_buffer->b_ml.ml_line_count)
2994 {
2995 retval = TRUE;
2996 break; /* past end of file */
2997 }
2998 row -= count;
2999 ++lnum;
3000 }
3001
3002 if (!retval)
3003 {
3004 /* Compute the column without wrapping. */
3005 off = win_col_off(win) - win_col_off2(win);
3006 if (col < off)
3007 col = off;
3008 col += row * (W_WIDTH(win) - off);
3009 /* add skip column (for long wrapping line) */
3010 col += win->w_skipcol;
3011 }
3012
3013 if (!win->w_p_wrap)
3014 col += win->w_leftcol;
3015
3016 /* skip line number and fold column in front of the line */
3017 col -= win_col_off(win);
3018 if (col < 0)
3019 {
3020#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003021 netbeans_gutter_click(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022#endif
3023 col = 0;
3024 }
3025
3026 *colp = col;
3027 *rowp = row;
3028 *lnump = lnum;
3029 return retval;
3030}
3031
3032#if defined(FEAT_WINDOWS) || defined(PROTO)
3033/*
3034 * Find the window at screen position "*rowp" and "*colp". The positions are
3035 * updated to become relative to the top-left of the window.
3036 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 win_T *
3038mouse_find_win(rowp, colp)
3039 int *rowp;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003040 int *colp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041{
3042 frame_T *fp;
3043
3044 fp = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003045 *rowp -= firstwin->w_winrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 for (;;)
3047 {
3048 if (fp->fr_layout == FR_LEAF)
3049 break;
3050#ifdef FEAT_VERTSPLIT
3051 if (fp->fr_layout == FR_ROW)
3052 {
3053 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
3054 {
3055 if (*colp < fp->fr_width)
3056 break;
3057 *colp -= fp->fr_width;
3058 }
3059 }
3060#endif
3061 else /* fr_layout == FR_COL */
3062 {
3063 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
3064 {
3065 if (*rowp < fp->fr_height)
3066 break;
3067 *rowp -= fp->fr_height;
3068 }
3069 }
3070 }
3071 return fp->fr_win;
3072}
3073#endif
3074
Bram Moolenaar860cae12010-06-05 23:22:07 +02003075#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
3077 || defined(FEAT_GUI_PHOTON) || defined(PROTO)
3078/*
3079 * Translate window coordinates to buffer position without any side effects
3080 */
3081 int
3082get_fpos_of_mouse(mpos)
3083 pos_T *mpos;
3084{
3085 win_T *wp;
3086 int row = mouse_row;
3087 int col = mouse_col;
3088
3089 if (row < 0 || col < 0) /* check if it makes sense */
3090 return IN_UNKNOWN;
3091
3092#ifdef FEAT_WINDOWS
3093 /* find the window where the row is in */
3094 wp = mouse_find_win(&row, &col);
3095#else
3096 wp = firstwin;
3097#endif
3098 /*
3099 * winpos and height may change in win_enter()!
3100 */
3101 if (row >= wp->w_height) /* In (or below) status line */
3102 return IN_STATUS_LINE;
3103#ifdef FEAT_VERTSPLIT
3104 if (col >= wp->w_width) /* In vertical separator line */
3105 return IN_SEP_LINE;
3106#endif
3107
3108 if (wp != curwin)
3109 return IN_UNKNOWN;
3110
3111 /* compute the position in the buffer line from the posn on the screen */
3112 if (mouse_comp_pos(curwin, &row, &col, &mpos->lnum))
3113 return IN_STATUS_LINE; /* past bottom */
3114
3115 mpos->col = vcol2col(wp, mpos->lnum, col);
3116
3117 if (mpos->col > 0)
3118 --mpos->col;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003119#ifdef FEAT_VIRTUALEDIT
3120 mpos->coladd = 0;
3121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 return IN_BUFFER;
3123}
3124
3125/*
3126 * Convert a virtual (screen) column to a character column.
3127 * The first column is one.
3128 */
3129 int
3130vcol2col(wp, lnum, vcol)
3131 win_T *wp;
3132 linenr_T lnum;
3133 int vcol;
3134{
3135 /* try to advance to the specified column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 int count = 0;
3137 char_u *ptr;
Bram Moolenaar86c800a2009-09-11 14:48:27 +00003138 char_u *start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139
Bram Moolenaar86c800a2009-09-11 14:48:27 +00003140 start = ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaarb6101cf2012-10-21 00:58:39 +02003141 while (count < vcol && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 count += win_lbr_chartabsize(wp, ptr, count, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003144 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 }
Bram Moolenaar86c800a2009-09-11 14:48:27 +00003146 return (int)(ptr - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147}
3148#endif
3149
3150#endif /* FEAT_MOUSE */
3151
3152#if defined(FEAT_GUI) || defined(WIN3264) || defined(PROTO)
3153/*
3154 * Called when focus changed. Used for the GUI or for systems where this can
3155 * be done in the console (Win32).
3156 */
3157 void
3158ui_focus_change(in_focus)
3159 int in_focus; /* TRUE if focus gained. */
3160{
3161 static time_t last_time = (time_t)0;
3162 int need_redraw = FALSE;
3163
3164 /* When activated: Check if any file was modified outside of Vim.
3165 * Only do this when not done within the last two seconds (could get
3166 * several events in a row). */
3167 if (in_focus && last_time + 2 < time(NULL))
3168 {
3169 need_redraw = check_timestamps(
3170# ifdef FEAT_GUI
3171 gui.in_use
3172# else
3173 FALSE
3174# endif
3175 );
3176 last_time = time(NULL);
3177 }
3178
3179#ifdef FEAT_AUTOCMD
3180 /*
3181 * Fire the focus gained/lost autocommand.
3182 */
3183 need_redraw |= apply_autocmds(in_focus ? EVENT_FOCUSGAINED
3184 : EVENT_FOCUSLOST, NULL, NULL, FALSE, curbuf);
3185#endif
3186
3187 if (need_redraw)
3188 {
3189 /* Something was executed, make sure the cursor is put back where it
3190 * belongs. */
3191 need_wait_return = FALSE;
3192
3193 if (State & CMDLINE)
3194 redrawcmdline();
3195 else if (State == HITRETURN || State == SETWSIZE || State == ASKMORE
3196 || State == EXTERNCMD || State == CONFIRM || exmode_active)
3197 repeat_message();
3198 else if ((State & NORMAL) || (State & INSERT))
3199 {
3200 if (must_redraw != 0)
3201 update_screen(0);
3202 setcursor();
3203 }
3204 cursor_on(); /* redrawing may have switched it off */
3205 out_flush();
3206# ifdef FEAT_GUI
3207 if (gui.in_use)
3208 {
3209 gui_update_cursor(FALSE, TRUE);
3210 gui_update_scrollbars(FALSE);
3211 }
3212# endif
3213 }
3214#ifdef FEAT_TITLE
3215 /* File may have been changed from 'readonly' to 'noreadonly' */
3216 if (need_maketitle)
3217 maketitle();
3218#endif
3219}
3220#endif
3221
3222#if defined(USE_IM_CONTROL) || defined(PROTO)
3223/*
3224 * Save current Input Method status to specified place.
3225 */
3226 void
3227im_save_status(psave)
3228 long *psave;
3229{
3230 /* Don't save when 'imdisable' is set or "xic" is NULL, IM is always
3231 * disabled then (but might start later).
3232 * Also don't save when inside a mapping, vgetc_im_active has not been set
3233 * then.
3234 * And don't save when the keys were stuffed (e.g., for a "." command).
3235 * And don't save when the GUI is running but our window doesn't have
3236 * input focus (e.g., when a find dialog is open). */
3237 if (!p_imdisable && KeyTyped && !KeyStuffed
3238# ifdef FEAT_XIM
3239 && xic != NULL
3240# endif
3241# ifdef FEAT_GUI
3242 && (!gui.in_use || gui.in_focus)
3243# endif
3244 )
3245 {
3246 /* Do save when IM is on, or IM is off and saved status is on. */
3247 if (vgetc_im_active)
3248 *psave = B_IMODE_IM;
3249 else if (*psave == B_IMODE_IM)
3250 *psave = B_IMODE_NONE;
3251 }
3252}
3253#endif