blob: b268cd78d4b1c5513963f8465618493b6aa165d5 [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
Bram Moolenaar2c6f3dc2013-07-05 20:09:16 +020021#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
22# define WIN32_LEAN_AND_MEAN
23# include <windows.h>
24# include "winclip.pro"
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027 void
28ui_write(s, len)
29 char_u *s;
30 int len;
31{
32#ifdef FEAT_GUI
33 if (gui.in_use && !gui.dying && !gui.starting)
34 {
35 gui_write(s, len);
36 if (p_wd)
37 gui_wait_for_chars(p_wd);
38 return;
39 }
40#endif
41#ifndef NO_CONSOLE
42 /* Don't output anything in silent mode ("ex -s") unless 'verbose' set */
43 if (!(silent_mode && p_verbose == 0))
44 {
45#ifdef FEAT_MBYTE
46 char_u *tofree = NULL;
47
48 if (output_conv.vc_type != CONV_NONE)
49 {
50 /* Convert characters from 'encoding' to 'termencoding'. */
51 tofree = string_convert(&output_conv, s, &len);
52 if (tofree != NULL)
53 s = tofree;
54 }
55#endif
56
57 mch_write(s, len);
58
59#ifdef FEAT_MBYTE
60 if (output_conv.vc_type != CONV_NONE)
61 vim_free(tofree);
62#endif
63 }
64#endif
65}
66
Bram Moolenaar4b9669f2011-07-07 16:20:52 +020067#if defined(UNIX) || defined(VMS) || defined(PROTO) || defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +000068/*
69 * When executing an external program, there may be some typed characters that
70 * are not consumed by it. Give them back to ui_inchar() and they are stored
71 * here for the next call.
72 */
73static char_u *ta_str = NULL;
74static int ta_off; /* offset for next char to use when ta_str != NULL */
75static int ta_len; /* length of ta_str when it's not NULL*/
76
77 void
78ui_inchar_undo(s, len)
79 char_u *s;
80 int len;
81{
82 char_u *new;
83 int newlen;
84
85 newlen = len;
86 if (ta_str != NULL)
87 newlen += ta_len - ta_off;
88 new = alloc(newlen);
89 if (new != NULL)
90 {
91 if (ta_str != NULL)
92 {
93 mch_memmove(new, ta_str + ta_off, (size_t)(ta_len - ta_off));
94 mch_memmove(new + ta_len - ta_off, s, (size_t)len);
95 vim_free(ta_str);
96 }
97 else
98 mch_memmove(new, s, (size_t)len);
99 ta_str = new;
100 ta_len = newlen;
101 ta_off = 0;
102 }
103}
104#endif
105
106/*
Bram Moolenaarb6101cf2012-10-21 00:58:39 +0200107 * ui_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108 * Get characters from the keyboard.
109 * Return the number of characters that are available.
110 * If "wtime" == 0 do not wait for characters.
111 * If "wtime" == -1 wait forever for characters.
112 * If "wtime" > 0 wait "wtime" milliseconds for a character.
113 *
114 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
115 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
116 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is NULL
117 * otherwise.
118 */
119 int
120ui_inchar(buf, maxlen, wtime, tb_change_cnt)
121 char_u *buf;
122 int maxlen;
123 long wtime; /* don't use "time", MIPS cannot handle it */
124 int tb_change_cnt;
125{
126 int retval = 0;
127
128#if defined(FEAT_GUI) && (defined(UNIX) || defined(VMS))
129 /*
130 * Use the typeahead if there is any.
131 */
132 if (ta_str != NULL)
133 {
134 if (maxlen >= ta_len - ta_off)
135 {
136 mch_memmove(buf, ta_str + ta_off, (size_t)ta_len);
137 vim_free(ta_str);
138 ta_str = NULL;
139 return ta_len;
140 }
141 mch_memmove(buf, ta_str + ta_off, (size_t)maxlen);
142 ta_off += maxlen;
143 return maxlen;
144 }
145#endif
146
Bram Moolenaar05159a02005-02-26 23:04:13 +0000147#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000148 if (do_profiling == PROF_YES && wtime != 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000149 prof_inchar_enter();
150#endif
151
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152#ifdef NO_CONSOLE_INPUT
153 /* Don't wait for character input when the window hasn't been opened yet.
154 * Do try reading, this works when redirecting stdin from a file.
155 * Must return something, otherwise we'll loop forever. If we run into
156 * this very often we probably got stuck, exit Vim. */
157 if (no_console_input())
158 {
159 static int count = 0;
160
161# ifndef NO_CONSOLE
Bram Moolenaar43b604c2005-03-22 23:06:55 +0000162 retval = mch_inchar(buf, maxlen, (wtime >= 0 && wtime < 10)
163 ? 10L : wtime, tb_change_cnt);
164 if (retval > 0 || typebuf_changed(tb_change_cnt) || wtime >= 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000165 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166# endif
167 if (wtime == -1 && ++count == 1000)
168 read_error_exit();
169 buf[0] = CAR;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000170 retval = 1;
171 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 }
173#endif
174
Bram Moolenaarc8da3112007-03-08 12:36:46 +0000175 /* If we are going to wait for some time or block... */
176 if (wtime == -1 || wtime > 100L)
177 {
178 /* ... allow signals to kill us. */
179 (void)vim_handle_signal(SIGNAL_UNBLOCK);
180
181 /* ... there is no need for CTRL-C to interrupt something, don't let
182 * it set got_int when it was mapped. */
183 if (mapped_ctrl_c)
184 ctrl_c_interrupts = FALSE;
185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186
187#ifdef FEAT_GUI
188 if (gui.in_use)
189 {
190 if (gui_wait_for_chars(wtime) && !typebuf_changed(tb_change_cnt))
191 retval = read_from_input_buf(buf, (long)maxlen);
192 }
193#endif
194#ifndef NO_CONSOLE
195# ifdef FEAT_GUI
196 else
197# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000198 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 retval = mch_inchar(buf, maxlen, wtime, tb_change_cnt);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#endif
202
Bram Moolenaarc8da3112007-03-08 12:36:46 +0000203 if (wtime == -1 || wtime > 100L)
204 /* block SIGHUP et al. */
205 (void)vim_handle_signal(SIGNAL_BLOCK);
206
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 ctrl_c_interrupts = TRUE;
208
Bram Moolenaar05159a02005-02-26 23:04:13 +0000209#ifdef NO_CONSOLE_INPUT
210theend:
211#endif
212#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000213 if (do_profiling == PROF_YES && wtime != 0)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000214 prof_inchar_exit();
215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 return retval;
217}
218
219/*
220 * return non-zero if a character is available
221 */
222 int
223ui_char_avail()
224{
225#ifdef FEAT_GUI
226 if (gui.in_use)
227 {
228 gui_mch_update();
229 return input_available();
230 }
231#endif
232#ifndef NO_CONSOLE
233# ifdef NO_CONSOLE_INPUT
234 if (no_console_input())
235 return 0;
236# endif
237 return mch_char_avail();
238#else
239 return 0;
240#endif
241}
242
243/*
244 * Delay for the given number of milliseconds. If ignoreinput is FALSE then we
245 * cancel the delay if a key is hit.
246 */
247 void
248ui_delay(msec, ignoreinput)
249 long msec;
250 int ignoreinput;
251{
252#ifdef FEAT_GUI
253 if (gui.in_use && !ignoreinput)
254 gui_wait_for_chars(msec);
255 else
256#endif
257 mch_delay(msec, ignoreinput);
258}
259
260/*
261 * If the machine has job control, use it to suspend the program,
262 * otherwise fake it by starting a new shell.
263 * When running the GUI iconify the window.
264 */
265 void
266ui_suspend()
267{
268#ifdef FEAT_GUI
269 if (gui.in_use)
270 {
271 gui_mch_iconify();
272 return;
273 }
274#endif
275 mch_suspend();
276}
277
278#if !defined(UNIX) || !defined(SIGTSTP) || defined(PROTO) || defined(__BEOS__)
279/*
280 * When the OS can't really suspend, call this function to start a shell.
281 * This is never called in the GUI.
282 */
283 void
284suspend_shell()
285{
286 if (*p_sh == NUL)
287 EMSG(_(e_shellempty));
288 else
289 {
290 MSG_PUTS(_("new shell started\n"));
291 do_shell(NULL, 0);
292 }
293}
294#endif
295
296/*
297 * Try to get the current Vim shell size. Put the result in Rows and Columns.
298 * Use the new sizes as defaults for 'columns' and 'lines'.
299 * Return OK when size could be determined, FAIL otherwise.
300 */
301 int
302ui_get_shellsize()
303{
304 int retval;
305
306#ifdef FEAT_GUI
307 if (gui.in_use)
308 retval = gui_get_shellsize();
309 else
310#endif
311 retval = mch_get_shellsize();
312
313 check_shellsize();
314
315 /* adjust the default for 'lines' and 'columns' */
316 if (retval == OK)
317 {
318 set_number_default("lines", Rows);
319 set_number_default("columns", Columns);
320 }
321 return retval;
322}
323
324/*
325 * Set the size of the Vim shell according to Rows and Columns, if possible.
326 * The gui_set_shellsize() or mch_set_shellsize() function will try to set the
327 * new size. If this is not possible, it will adjust Rows and Columns.
328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 void
330ui_set_shellsize(mustset)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000331 int mustset UNUSED; /* set by the user */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332{
333#ifdef FEAT_GUI
334 if (gui.in_use)
Bram Moolenaar8968a312013-07-03 16:58:44 +0200335 gui_set_shellsize(mustset, TRUE, 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/*
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200561 * Save and restore clip_unnamed before doing possibly many changes. This
562 * prevents accessing the clipboard very often which might slow down Vim
563 * considerably.
564 */
565
566/*
567 * Save clip_unnamed and reset it.
568 */
569 void
570start_global_changes()
571{
572 clip_unnamed_saved = clip_unnamed;
573
Bram Moolenaar714db3b2014-09-19 13:46:52 +0200574 if (clip_did_set_selection > 0)
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200575 {
576 clip_unnamed = FALSE;
577 clip_did_set_selection = FALSE;
578 }
579}
580
581/*
582 * Restore clip_unnamed and set the selection when needed.
583 */
584 void
585end_global_changes()
586{
Bram Moolenaar714db3b2014-09-19 13:46:52 +0200587 if (clip_did_set_selection == FALSE) /* not when -1 */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +0200588 {
589 clip_did_set_selection = TRUE;
590 clip_unnamed = clip_unnamed_saved;
591 if (clip_unnamed & CLIP_UNNAMED)
592 {
593 clip_own_selection(&clip_star);
594 clip_gen_set_selection(&clip_star);
595 }
596 if (clip_unnamed & CLIP_UNNAMED_PLUS)
597 {
598 clip_own_selection(&clip_plus);
599 clip_gen_set_selection(&clip_plus);
600 }
601 }
602 clip_unnamed_saved = FALSE;
603}
604
605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 * Called when Visual mode is ended: update the selection.
607 */
608 void
609clip_auto_select()
610{
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200611 if (clip_isautosel_star())
612 clip_copy_selection(&clip_star);
613 if (clip_isautosel_plus())
614 clip_copy_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615}
616
617/*
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200618 * Return TRUE if automatic selection of Visual area is desired for the *
619 * register.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 */
621 int
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200622clip_isautosel_star()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623{
624 return (
625#ifdef FEAT_GUI
626 gui.in_use ? (vim_strchr(p_go, GO_ASEL) != NULL) :
627#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200628 clip_autoselect_star);
629}
630
631/*
632 * Return TRUE if automatic selection of Visual area is desired for the +
633 * register.
634 */
635 int
636clip_isautosel_plus()
637{
638 return (
639#ifdef FEAT_GUI
640 gui.in_use ? (vim_strchr(p_go, GO_ASELPLUS) != NULL) :
641#endif
642 clip_autoselect_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643}
644
645
646/*
647 * Stuff for general mouse selection, without using Visual mode.
648 */
649
650static int clip_compare_pos __ARGS((int row1, int col1, int row2, int col2));
651static void clip_invert_area __ARGS((int, int, int, int, int how));
652static void clip_invert_rectangle __ARGS((int row, int col, int height, int width, int invert));
653static void clip_get_word_boundaries __ARGS((VimClipboard *, int, int));
654static int clip_get_line_end __ARGS((int));
655static void clip_update_modeless_selection __ARGS((VimClipboard *, int, int,
656 int, int));
657
658/* flags for clip_invert_area() */
659#define CLIP_CLEAR 1
660#define CLIP_SET 2
661#define CLIP_TOGGLE 3
662
663/*
664 * Start, continue or end a modeless selection. Used when editing the
665 * command-line and in the cmdline window.
666 */
667 void
668clip_modeless(button, is_click, is_drag)
669 int button;
670 int is_click;
671 int is_drag;
672{
673 int repeat;
674
675 repeat = ((clip_star.mode == SELECT_MODE_CHAR
676 || clip_star.mode == SELECT_MODE_LINE)
677 && (mod_mask & MOD_MASK_2CLICK))
678 || (clip_star.mode == SELECT_MODE_WORD
679 && (mod_mask & MOD_MASK_3CLICK));
680 if (is_click && button == MOUSE_RIGHT)
681 {
682 /* Right mouse button: If there was no selection, start one.
683 * Otherwise extend the existing selection. */
684 if (clip_star.state == SELECT_CLEARED)
685 clip_start_selection(mouse_col, mouse_row, FALSE);
686 clip_process_selection(button, mouse_col, mouse_row, repeat);
687 }
688 else if (is_click)
689 clip_start_selection(mouse_col, mouse_row, repeat);
690 else if (is_drag)
691 {
692 /* Don't try extending a selection if there isn't one. Happens when
693 * button-down is in the cmdline and them moving mouse upwards. */
694 if (clip_star.state != SELECT_CLEARED)
695 clip_process_selection(button, mouse_col, mouse_row, repeat);
696 }
697 else /* release */
698 clip_process_selection(MOUSE_RELEASE, mouse_col, mouse_row, FALSE);
699}
700
701/*
702 * Compare two screen positions ala strcmp()
703 */
704 static int
705clip_compare_pos(row1, col1, row2, col2)
706 int row1;
707 int col1;
708 int row2;
709 int col2;
710{
711 if (row1 > row2) return(1);
712 if (row1 < row2) return(-1);
713 if (col1 > col2) return(1);
714 if (col1 < col2) return(-1);
715 return(0);
716}
717
718/*
719 * Start the selection
720 */
721 void
722clip_start_selection(col, row, repeated_click)
723 int col;
724 int row;
725 int repeated_click;
726{
727 VimClipboard *cb = &clip_star;
728
729 if (cb->state == SELECT_DONE)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200730 clip_clear_selection(cb);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731
732 row = check_row(row);
733 col = check_col(col);
734#ifdef FEAT_MBYTE
735 col = mb_fix_col(col, row);
736#endif
737
738 cb->start.lnum = row;
739 cb->start.col = col;
740 cb->end = cb->start;
741 cb->origin_row = (short_u)cb->start.lnum;
742 cb->state = SELECT_IN_PROGRESS;
743
744 if (repeated_click)
745 {
746 if (++cb->mode > SELECT_MODE_LINE)
747 cb->mode = SELECT_MODE_CHAR;
748 }
749 else
750 cb->mode = SELECT_MODE_CHAR;
751
752#ifdef FEAT_GUI
753 /* clear the cursor until the selection is made */
754 if (gui.in_use)
755 gui_undraw_cursor();
756#endif
757
758 switch (cb->mode)
759 {
760 case SELECT_MODE_CHAR:
761 cb->origin_start_col = cb->start.col;
762 cb->word_end_col = clip_get_line_end((int)cb->start.lnum);
763 break;
764
765 case SELECT_MODE_WORD:
766 clip_get_word_boundaries(cb, (int)cb->start.lnum, cb->start.col);
767 cb->origin_start_col = cb->word_start_col;
768 cb->origin_end_col = cb->word_end_col;
769
770 clip_invert_area((int)cb->start.lnum, cb->word_start_col,
771 (int)cb->end.lnum, cb->word_end_col, CLIP_SET);
772 cb->start.col = cb->word_start_col;
773 cb->end.col = cb->word_end_col;
774 break;
775
776 case SELECT_MODE_LINE:
777 clip_invert_area((int)cb->start.lnum, 0, (int)cb->start.lnum,
778 (int)Columns, CLIP_SET);
779 cb->start.col = 0;
780 cb->end.col = Columns;
781 break;
782 }
783
784 cb->prev = cb->start;
785
786#ifdef DEBUG_SELECTION
787 printf("Selection started at (%u,%u)\n", cb->start.lnum, cb->start.col);
788#endif
789}
790
791/*
792 * Continue processing the selection
793 */
794 void
795clip_process_selection(button, col, row, repeated_click)
796 int button;
797 int col;
798 int row;
799 int_u repeated_click;
800{
801 VimClipboard *cb = &clip_star;
802 int diff;
803 int slen = 1; /* cursor shape width */
804
805 if (button == MOUSE_RELEASE)
806 {
807 /* Check to make sure we have something selected */
808 if (cb->start.lnum == cb->end.lnum && cb->start.col == cb->end.col)
809 {
810#ifdef FEAT_GUI
811 if (gui.in_use)
812 gui_update_cursor(FALSE, FALSE);
813#endif
814 cb->state = SELECT_CLEARED;
815 return;
816 }
817
818#ifdef DEBUG_SELECTION
819 printf("Selection ended: (%u,%u) to (%u,%u)\n", cb->start.lnum,
820 cb->start.col, cb->end.lnum, cb->end.col);
821#endif
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200822 if (clip_isautosel_star()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 || (
824#ifdef FEAT_GUI
825 gui.in_use ? (vim_strchr(p_go, GO_ASELML) != NULL) :
826#endif
827 clip_autoselectml))
828 clip_copy_modeless_selection(FALSE);
829#ifdef FEAT_GUI
830 if (gui.in_use)
831 gui_update_cursor(FALSE, FALSE);
832#endif
833
834 cb->state = SELECT_DONE;
835 return;
836 }
837
838 row = check_row(row);
839 col = check_col(col);
840#ifdef FEAT_MBYTE
841 col = mb_fix_col(col, row);
842#endif
843
844 if (col == (int)cb->prev.col && row == cb->prev.lnum && !repeated_click)
845 return;
846
847 /*
848 * When extending the selection with the right mouse button, swap the
849 * start and end if the position is before half the selection
850 */
851 if (cb->state == SELECT_DONE && button == MOUSE_RIGHT)
852 {
853 /*
854 * If the click is before the start, or the click is inside the
855 * selection and the start is the closest side, set the origin to the
856 * end of the selection.
857 */
858 if (clip_compare_pos(row, col, (int)cb->start.lnum, cb->start.col) < 0
859 || (clip_compare_pos(row, col,
860 (int)cb->end.lnum, cb->end.col) < 0
861 && (((cb->start.lnum == cb->end.lnum
862 && cb->end.col - col > col - cb->start.col))
863 || ((diff = (cb->end.lnum - row) -
864 (row - cb->start.lnum)) > 0
865 || (diff == 0 && col < (int)(cb->start.col +
866 cb->end.col) / 2)))))
867 {
868 cb->origin_row = (short_u)cb->end.lnum;
869 cb->origin_start_col = cb->end.col - 1;
870 cb->origin_end_col = cb->end.col;
871 }
872 else
873 {
874 cb->origin_row = (short_u)cb->start.lnum;
875 cb->origin_start_col = cb->start.col;
876 cb->origin_end_col = cb->start.col;
877 }
878 if (cb->mode == SELECT_MODE_WORD && !repeated_click)
879 cb->mode = SELECT_MODE_CHAR;
880 }
881
882 /* set state, for when using the right mouse button */
883 cb->state = SELECT_IN_PROGRESS;
884
885#ifdef DEBUG_SELECTION
886 printf("Selection extending to (%d,%d)\n", row, col);
887#endif
888
889 if (repeated_click && ++cb->mode > SELECT_MODE_LINE)
890 cb->mode = SELECT_MODE_CHAR;
891
892 switch (cb->mode)
893 {
894 case SELECT_MODE_CHAR:
895 /* If we're on a different line, find where the line ends */
896 if (row != cb->prev.lnum)
897 cb->word_end_col = clip_get_line_end(row);
898
899 /* See if we are before or after the origin of the selection */
900 if (clip_compare_pos(row, col, cb->origin_row,
901 cb->origin_start_col) >= 0)
902 {
903 if (col >= (int)cb->word_end_col)
904 clip_update_modeless_selection(cb, cb->origin_row,
905 cb->origin_start_col, row, (int)Columns);
906 else
907 {
908#ifdef FEAT_MBYTE
909 if (has_mbyte && mb_lefthalve(row, col))
910 slen = 2;
911#endif
912 clip_update_modeless_selection(cb, cb->origin_row,
913 cb->origin_start_col, row, col + slen);
914 }
915 }
916 else
917 {
918#ifdef FEAT_MBYTE
919 if (has_mbyte
920 && mb_lefthalve(cb->origin_row, cb->origin_start_col))
921 slen = 2;
922#endif
923 if (col >= (int)cb->word_end_col)
924 clip_update_modeless_selection(cb, row, cb->word_end_col,
925 cb->origin_row, cb->origin_start_col + slen);
926 else
927 clip_update_modeless_selection(cb, row, col,
928 cb->origin_row, cb->origin_start_col + slen);
929 }
930 break;
931
932 case SELECT_MODE_WORD:
933 /* If we are still within the same word, do nothing */
934 if (row == cb->prev.lnum && col >= (int)cb->word_start_col
935 && col < (int)cb->word_end_col && !repeated_click)
936 return;
937
938 /* Get new word boundaries */
939 clip_get_word_boundaries(cb, row, col);
940
941 /* Handle being after the origin point of selection */
942 if (clip_compare_pos(row, col, cb->origin_row,
943 cb->origin_start_col) >= 0)
944 clip_update_modeless_selection(cb, cb->origin_row,
945 cb->origin_start_col, row, cb->word_end_col);
946 else
947 clip_update_modeless_selection(cb, row, cb->word_start_col,
948 cb->origin_row, cb->origin_end_col);
949 break;
950
951 case SELECT_MODE_LINE:
952 if (row == cb->prev.lnum && !repeated_click)
953 return;
954
955 if (clip_compare_pos(row, col, cb->origin_row,
956 cb->origin_start_col) >= 0)
957 clip_update_modeless_selection(cb, cb->origin_row, 0, row,
958 (int)Columns);
959 else
960 clip_update_modeless_selection(cb, row, 0, cb->origin_row,
961 (int)Columns);
962 break;
963 }
964
965 cb->prev.lnum = row;
966 cb->prev.col = col;
967
968#ifdef DEBUG_SELECTION
969 printf("Selection is: (%u,%u) to (%u,%u)\n", cb->start.lnum,
970 cb->start.col, cb->end.lnum, cb->end.col);
971#endif
972}
973
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974# if defined(FEAT_GUI) || defined(PROTO)
975/*
976 * Redraw part of the selection if character at "row,col" is inside of it.
977 * Only used for the GUI.
978 */
979 void
980clip_may_redraw_selection(row, col, len)
981 int row, col;
982 int len;
983{
984 int start = col;
985 int end = col + len;
986
987 if (clip_star.state != SELECT_CLEARED
988 && row >= clip_star.start.lnum
989 && row <= clip_star.end.lnum)
990 {
991 if (row == clip_star.start.lnum && start < (int)clip_star.start.col)
992 start = clip_star.start.col;
993 if (row == clip_star.end.lnum && end > (int)clip_star.end.col)
994 end = clip_star.end.col;
995 if (end > start)
996 clip_invert_area(row, start, row, end, 0);
997 }
998}
999# endif
1000
1001/*
1002 * Called from outside to clear selected region from the display
1003 */
1004 void
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001005clip_clear_selection(cbd)
1006 VimClipboard *cbd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001009 if (cbd->state == SELECT_CLEARED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 return;
1011
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001012 clip_invert_area((int)cbd->start.lnum, cbd->start.col, (int)cbd->end.lnum,
1013 cbd->end.col, CLIP_CLEAR);
1014 cbd->state = SELECT_CLEARED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015}
1016
1017/*
1018 * Clear the selection if any lines from "row1" to "row2" are inside of it.
1019 */
1020 void
1021clip_may_clear_selection(row1, row2)
1022 int row1, row2;
1023{
1024 if (clip_star.state == SELECT_DONE
1025 && row2 >= clip_star.start.lnum
1026 && row1 <= clip_star.end.lnum)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001027 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028}
1029
1030/*
1031 * Called before the screen is scrolled up or down. Adjusts the line numbers
1032 * of the selection. Call with big number when clearing the screen.
1033 */
1034 void
1035clip_scroll_selection(rows)
1036 int rows; /* negative for scroll down */
1037{
1038 int lnum;
1039
1040 if (clip_star.state == SELECT_CLEARED)
1041 return;
1042
1043 lnum = clip_star.start.lnum - rows;
1044 if (lnum <= 0)
1045 clip_star.start.lnum = 0;
1046 else if (lnum >= screen_Rows) /* scrolled off of the screen */
1047 clip_star.state = SELECT_CLEARED;
1048 else
1049 clip_star.start.lnum = lnum;
1050
1051 lnum = clip_star.end.lnum - rows;
1052 if (lnum < 0) /* scrolled off of the screen */
1053 clip_star.state = SELECT_CLEARED;
1054 else if (lnum >= screen_Rows)
1055 clip_star.end.lnum = screen_Rows - 1;
1056 else
1057 clip_star.end.lnum = lnum;
1058}
1059
1060/*
1061 * Invert a region of the display between a starting and ending row and column
1062 * Values for "how":
1063 * CLIP_CLEAR: undo inversion
1064 * CLIP_SET: set inversion
1065 * CLIP_TOGGLE: set inversion if pos1 < pos2, undo inversion otherwise.
1066 * 0: invert (GUI only).
1067 */
1068 static void
1069clip_invert_area(row1, col1, row2, col2, how)
1070 int row1;
1071 int col1;
1072 int row2;
1073 int col2;
1074 int how;
1075{
1076 int invert = FALSE;
1077
1078 if (how == CLIP_SET)
1079 invert = TRUE;
1080
1081 /* Swap the from and to positions so the from is always before */
1082 if (clip_compare_pos(row1, col1, row2, col2) > 0)
1083 {
1084 int tmp_row, tmp_col;
1085
1086 tmp_row = row1;
1087 tmp_col = col1;
1088 row1 = row2;
1089 col1 = col2;
1090 row2 = tmp_row;
1091 col2 = tmp_col;
1092 }
1093 else if (how == CLIP_TOGGLE)
1094 invert = TRUE;
1095
1096 /* If all on the same line, do it the easy way */
1097 if (row1 == row2)
1098 {
1099 clip_invert_rectangle(row1, col1, 1, col2 - col1, invert);
1100 }
1101 else
1102 {
1103 /* Handle a piece of the first line */
1104 if (col1 > 0)
1105 {
1106 clip_invert_rectangle(row1, col1, 1, (int)Columns - col1, invert);
1107 row1++;
1108 }
1109
1110 /* Handle a piece of the last line */
1111 if (col2 < Columns - 1)
1112 {
1113 clip_invert_rectangle(row2, 0, 1, col2, invert);
1114 row2--;
1115 }
1116
1117 /* Handle the rectangle thats left */
1118 if (row2 >= row1)
1119 clip_invert_rectangle(row1, 0, row2 - row1 + 1, (int)Columns,
1120 invert);
1121 }
1122}
1123
1124/*
1125 * Invert or un-invert a rectangle of the screen.
1126 * "invert" is true if the result is inverted.
1127 */
1128 static void
1129clip_invert_rectangle(row, col, height, width, invert)
1130 int row;
1131 int col;
1132 int height;
1133 int width;
1134 int invert;
1135{
1136#ifdef FEAT_GUI
1137 if (gui.in_use)
1138 gui_mch_invert_rectangle(row, col, height, width);
1139 else
1140#endif
1141 screen_draw_rectangle(row, col, height, width, invert);
1142}
1143
1144/*
1145 * Copy the currently selected area into the '*' register so it will be
1146 * available for pasting.
1147 * When "both" is TRUE also copy to the '+' register.
1148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 void
1150clip_copy_modeless_selection(both)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001151 int both UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152{
1153 char_u *buffer;
1154 char_u *bufp;
1155 int row;
1156 int start_col;
1157 int end_col;
1158 int line_end_col;
1159 int add_newline_flag = FALSE;
1160 int len;
1161#ifdef FEAT_MBYTE
1162 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163#endif
1164 int row1 = clip_star.start.lnum;
1165 int col1 = clip_star.start.col;
1166 int row2 = clip_star.end.lnum;
1167 int col2 = clip_star.end.col;
1168
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001169 /* Can't use ScreenLines unless initialized */
1170 if (ScreenLines == NULL)
1171 return;
1172
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 /*
1174 * Make sure row1 <= row2, and if row1 == row2 that col1 <= col2.
1175 */
1176 if (row1 > row2)
1177 {
1178 row = row1; row1 = row2; row2 = row;
1179 row = col1; col1 = col2; col2 = row;
1180 }
1181 else if (row1 == row2 && col1 > col2)
1182 {
1183 row = col1; col1 = col2; col2 = row;
1184 }
1185#ifdef FEAT_MBYTE
1186 /* correct starting point for being on right halve of double-wide char */
1187 p = ScreenLines + LineOffset[row1];
1188 if (enc_dbcs != 0)
1189 col1 -= (*mb_head_off)(p, p + col1);
1190 else if (enc_utf8 && p[col1] == 0)
1191 --col1;
1192#endif
1193
1194 /* Create a temporary buffer for storing the text */
1195 len = (row2 - row1 + 1) * Columns + 1;
1196#ifdef FEAT_MBYTE
1197 if (enc_dbcs != 0)
1198 len *= 2; /* max. 2 bytes per display cell */
1199 else if (enc_utf8)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001200 len *= MB_MAXBYTES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201#endif
1202 buffer = lalloc((long_u)len, TRUE);
1203 if (buffer == NULL) /* out of memory */
1204 return;
1205
1206 /* Process each row in the selection */
1207 for (bufp = buffer, row = row1; row <= row2; row++)
1208 {
1209 if (row == row1)
1210 start_col = col1;
1211 else
1212 start_col = 0;
1213
1214 if (row == row2)
1215 end_col = col2;
1216 else
1217 end_col = Columns;
1218
1219 line_end_col = clip_get_line_end(row);
1220
1221 /* See if we need to nuke some trailing whitespace */
1222 if (end_col >= Columns && (row < row2 || end_col > line_end_col))
1223 {
1224 /* Get rid of trailing whitespace */
1225 end_col = line_end_col;
1226 if (end_col < start_col)
1227 end_col = start_col;
1228
1229 /* If the last line extended to the end, add an extra newline */
1230 if (row == row2)
1231 add_newline_flag = TRUE;
1232 }
1233
1234 /* If after the first row, we need to always add a newline */
1235 if (row > row1 && !LineWraps[row - 1])
1236 *bufp++ = NL;
1237
1238 if (row < screen_Rows && end_col <= screen_Columns)
1239 {
1240#ifdef FEAT_MBYTE
1241 if (enc_dbcs != 0)
1242 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001243 int i;
1244
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 p = ScreenLines + LineOffset[row];
1246 for (i = start_col; i < end_col; ++i)
1247 if (enc_dbcs == DBCS_JPNU && p[i] == 0x8e)
1248 {
1249 /* single-width double-byte char */
1250 *bufp++ = 0x8e;
1251 *bufp++ = ScreenLines2[LineOffset[row] + i];
1252 }
1253 else
1254 {
1255 *bufp++ = p[i];
1256 if (MB_BYTE2LEN(p[i]) == 2)
1257 *bufp++ = p[++i];
1258 }
1259 }
1260 else if (enc_utf8)
1261 {
1262 int off;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001263 int i;
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001264 int ci;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
1266 off = LineOffset[row];
1267 for (i = start_col; i < end_col; ++i)
1268 {
1269 /* The base character is either in ScreenLinesUC[] or
1270 * ScreenLines[]. */
1271 if (ScreenLinesUC[off + i] == 0)
1272 *bufp++ = ScreenLines[off + i];
1273 else
1274 {
1275 bufp += utf_char2bytes(ScreenLinesUC[off + i], bufp);
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001276 for (ci = 0; ci < Screen_mco; ++ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001278 /* Add a composing character. */
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001279 if (ScreenLinesC[ci][off + i] == 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001280 break;
Bram Moolenaar34e9e2f2006-03-14 23:07:19 +00001281 bufp += utf_char2bytes(ScreenLinesC[ci][off + i],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 }
1284 }
1285 /* Skip right halve of double-wide character. */
1286 if (ScreenLines[off + i + 1] == 0)
1287 ++i;
1288 }
1289 }
1290 else
1291#endif
1292 {
1293 STRNCPY(bufp, ScreenLines + LineOffset[row] + start_col,
1294 end_col - start_col);
1295 bufp += end_col - start_col;
1296 }
1297 }
1298 }
1299
1300 /* Add a newline at the end if the selection ended there */
1301 if (add_newline_flag)
1302 *bufp++ = NL;
1303
1304 /* First cleanup any old selection and become the owner. */
1305 clip_free_selection(&clip_star);
1306 clip_own_selection(&clip_star);
1307
1308 /* Yank the text into the '*' register. */
1309 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_star);
1310
1311 /* Make the register contents available to the outside world. */
1312 clip_gen_set_selection(&clip_star);
1313
1314#ifdef FEAT_X11
1315 if (both)
1316 {
1317 /* Do the same for the '+' register. */
1318 clip_free_selection(&clip_plus);
1319 clip_own_selection(&clip_plus);
1320 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_plus);
1321 clip_gen_set_selection(&clip_plus);
1322 }
1323#endif
1324 vim_free(buffer);
1325}
1326
1327/*
1328 * Find the starting and ending positions of the word at the given row and
1329 * column. Only white-separated words are recognized here.
1330 */
1331#define CHAR_CLASS(c) (c <= ' ' ? ' ' : vim_iswordc(c))
1332
1333 static void
1334clip_get_word_boundaries(cb, row, col)
1335 VimClipboard *cb;
1336 int row;
1337 int col;
1338{
1339 int start_class;
1340 int temp_col;
1341 char_u *p;
1342#ifdef FEAT_MBYTE
1343 int mboff;
1344#endif
1345
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001346 if (row >= screen_Rows || col >= screen_Columns || ScreenLines == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 return;
1348
1349 p = ScreenLines + LineOffset[row];
1350#ifdef FEAT_MBYTE
1351 /* Correct for starting in the right halve of a double-wide char */
1352 if (enc_dbcs != 0)
1353 col -= dbcs_screen_head_off(p, p + col);
1354 else if (enc_utf8 && p[col] == 0)
1355 --col;
1356#endif
1357 start_class = CHAR_CLASS(p[col]);
1358
1359 temp_col = col;
1360 for ( ; temp_col > 0; temp_col--)
1361#ifdef FEAT_MBYTE
1362 if (enc_dbcs != 0
1363 && (mboff = dbcs_screen_head_off(p, p + temp_col - 1)) > 0)
1364 temp_col -= mboff;
1365 else
1366#endif
1367 if (CHAR_CLASS(p[temp_col - 1]) != start_class
1368#ifdef FEAT_MBYTE
1369 && !(enc_utf8 && p[temp_col - 1] == 0)
1370#endif
1371 )
1372 break;
1373 cb->word_start_col = temp_col;
1374
1375 temp_col = col;
1376 for ( ; temp_col < screen_Columns; temp_col++)
1377#ifdef FEAT_MBYTE
1378 if (enc_dbcs != 0 && dbcs_ptr2cells(p + temp_col) == 2)
1379 ++temp_col;
1380 else
1381#endif
1382 if (CHAR_CLASS(p[temp_col]) != start_class
1383#ifdef FEAT_MBYTE
1384 && !(enc_utf8 && p[temp_col] == 0)
1385#endif
1386 )
1387 break;
1388 cb->word_end_col = temp_col;
1389}
1390
1391/*
1392 * Find the column position for the last non-whitespace character on the given
1393 * line.
1394 */
1395 static int
1396clip_get_line_end(row)
1397 int row;
1398{
1399 int i;
1400
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001401 if (row >= screen_Rows || ScreenLines == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 return 0;
1403 for (i = screen_Columns; i > 0; i--)
1404 if (ScreenLines[LineOffset[row] + i - 1] != ' ')
1405 break;
1406 return i;
1407}
1408
1409/*
1410 * Update the currently selected region by adding and/or subtracting from the
1411 * beginning or end and inverting the changed area(s).
1412 */
1413 static void
1414clip_update_modeless_selection(cb, row1, col1, row2, col2)
1415 VimClipboard *cb;
1416 int row1;
1417 int col1;
1418 int row2;
1419 int col2;
1420{
1421 /* See if we changed at the beginning of the selection */
1422 if (row1 != cb->start.lnum || col1 != (int)cb->start.col)
1423 {
1424 clip_invert_area(row1, col1, (int)cb->start.lnum, cb->start.col,
1425 CLIP_TOGGLE);
1426 cb->start.lnum = row1;
1427 cb->start.col = col1;
1428 }
1429
1430 /* See if we changed at the end of the selection */
1431 if (row2 != cb->end.lnum || col2 != (int)cb->end.col)
1432 {
1433 clip_invert_area((int)cb->end.lnum, cb->end.col, row2, col2,
1434 CLIP_TOGGLE);
1435 cb->end.lnum = row2;
1436 cb->end.col = col2;
1437 }
1438}
1439
1440 int
1441clip_gen_own_selection(cbd)
1442 VimClipboard *cbd;
1443{
1444#ifdef FEAT_XCLIPBOARD
1445# ifdef FEAT_GUI
1446 if (gui.in_use)
1447 return clip_mch_own_selection(cbd);
1448 else
1449# endif
1450 return clip_xterm_own_selection(cbd);
1451#else
1452 return clip_mch_own_selection(cbd);
1453#endif
1454}
1455
1456 void
1457clip_gen_lose_selection(cbd)
1458 VimClipboard *cbd;
1459{
1460#ifdef FEAT_XCLIPBOARD
1461# ifdef FEAT_GUI
1462 if (gui.in_use)
1463 clip_mch_lose_selection(cbd);
1464 else
1465# endif
1466 clip_xterm_lose_selection(cbd);
1467#else
1468 clip_mch_lose_selection(cbd);
1469#endif
1470}
1471
1472 void
1473clip_gen_set_selection(cbd)
1474 VimClipboard *cbd;
1475{
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001476 if (!clip_did_set_selection)
1477 {
1478 /* Updating postponed, so that accessing the system clipboard won't
1479 * hang Vim when accessing it many times (e.g. on a :g comand). */
1480 if (cbd == &clip_plus && (clip_unnamed_saved & CLIP_UNNAMED_PLUS))
1481 return;
1482 else if (cbd == &clip_star && (clip_unnamed_saved & CLIP_UNNAMED))
1483 return;
1484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485#ifdef FEAT_XCLIPBOARD
1486# ifdef FEAT_GUI
1487 if (gui.in_use)
1488 clip_mch_set_selection(cbd);
1489 else
1490# endif
1491 clip_xterm_set_selection(cbd);
1492#else
1493 clip_mch_set_selection(cbd);
1494#endif
1495}
1496
1497 void
1498clip_gen_request_selection(cbd)
1499 VimClipboard *cbd;
1500{
1501#ifdef FEAT_XCLIPBOARD
1502# ifdef FEAT_GUI
1503 if (gui.in_use)
1504 clip_mch_request_selection(cbd);
1505 else
1506# endif
1507 clip_xterm_request_selection(cbd);
1508#else
1509 clip_mch_request_selection(cbd);
1510#endif
1511}
1512
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001513 int
1514clip_gen_owner_exists(cbd)
Bram Moolenaar81851112013-04-12 12:27:30 +02001515 VimClipboard *cbd UNUSED;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001516{
1517#ifdef FEAT_XCLIPBOARD
1518# ifdef FEAT_GUI_GTK
1519 if (gui.in_use)
1520 return clip_gtk_owner_exists(cbd);
1521 else
1522# endif
1523 return clip_x11_owner_exists(cbd);
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02001524#else
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001525 return TRUE;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02001526#endif
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001527}
1528
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529#endif /* FEAT_CLIPBOARD */
1530
1531/*****************************************************************************
1532 * Functions that handle the input buffer.
1533 * This is used for any GUI version, and the unix terminal version.
1534 *
1535 * For Unix, the input characters are buffered to be able to check for a
1536 * CTRL-C. This should be done with signals, but I don't know how to do that
1537 * in a portable way for a tty in RAW mode.
1538 *
1539 * For the client-server code in the console the received keys are put in the
1540 * input buffer.
1541 */
1542
1543#if defined(USE_INPUT_BUF) || defined(PROTO)
1544
1545/*
1546 * Internal typeahead buffer. Includes extra space for long key code
1547 * descriptions which would otherwise overflow. The buffer is considered full
1548 * when only this extra space (or part of it) remains.
1549 */
1550#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
1551 || defined(FEAT_CLIENTSERVER)
1552 /*
1553 * Sun WorkShop and NetBeans stuff debugger commands into the input buffer.
1554 * This requires a larger buffer...
1555 * (Madsen) Go with this for remote input as well ...
1556 */
1557# define INBUFLEN 4096
1558#else
1559# define INBUFLEN 250
1560#endif
1561
1562static char_u inbuf[INBUFLEN + MAX_KEY_CODE_LEN];
1563static int inbufcount = 0; /* number of chars in inbuf[] */
1564
1565/*
1566 * vim_is_input_buf_full(), vim_is_input_buf_empty(), add_to_input_buf(), and
1567 * trash_input_buf() are functions for manipulating the input buffer. These
1568 * are used by the gui_* calls when a GUI is used to handle keyboard input.
1569 */
1570
1571 int
1572vim_is_input_buf_full()
1573{
1574 return (inbufcount >= INBUFLEN);
1575}
1576
1577 int
1578vim_is_input_buf_empty()
1579{
1580 return (inbufcount == 0);
1581}
1582
1583#if defined(FEAT_OLE) || defined(PROTO)
1584 int
1585vim_free_in_input_buf()
1586{
1587 return (INBUFLEN - inbufcount);
1588}
1589#endif
1590
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001591#if defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 int
1593vim_used_in_input_buf()
1594{
1595 return inbufcount;
1596}
1597#endif
1598
1599#if defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) || defined(PROTO)
1600/*
1601 * Return the current contents of the input buffer and make it empty.
1602 * The returned pointer must be passed to set_input_buf() later.
1603 */
1604 char_u *
1605get_input_buf()
1606{
1607 garray_T *gap;
1608
1609 /* We use a growarray to store the data pointer and the length. */
1610 gap = (garray_T *)alloc((unsigned)sizeof(garray_T));
1611 if (gap != NULL)
1612 {
1613 /* Add one to avoid a zero size. */
1614 gap->ga_data = alloc((unsigned)inbufcount + 1);
1615 if (gap->ga_data != NULL)
1616 mch_memmove(gap->ga_data, inbuf, (size_t)inbufcount);
1617 gap->ga_len = inbufcount;
1618 }
1619 trash_input_buf();
1620 return (char_u *)gap;
1621}
1622
1623/*
1624 * Restore the input buffer with a pointer returned from get_input_buf().
1625 * The allocated memory is freed, this only works once!
1626 */
1627 void
1628set_input_buf(p)
1629 char_u *p;
1630{
1631 garray_T *gap = (garray_T *)p;
1632
1633 if (gap != NULL)
1634 {
1635 if (gap->ga_data != NULL)
1636 {
1637 mch_memmove(inbuf, gap->ga_data, gap->ga_len);
1638 inbufcount = gap->ga_len;
1639 vim_free(gap->ga_data);
1640 }
1641 vim_free(gap);
1642 }
1643}
1644#endif
1645
Bram Moolenaar446cb832008-06-24 21:56:24 +00001646#if defined(FEAT_GUI) \
1647 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 || defined(FEAT_XCLIPBOARD) || defined(VMS) \
Bram Moolenaarf52c7252006-02-10 23:23:57 +00001649 || defined(FEAT_SNIFF) || defined(FEAT_CLIENTSERVER) \
Bram Moolenaarf52c7252006-02-10 23:23:57 +00001650 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651/*
1652 * Add the given bytes to the input buffer
1653 * Special keys start with CSI. A real CSI must have been translated to
1654 * CSI KS_EXTRA KE_CSI. K_SPECIAL doesn't require translation.
1655 */
1656 void
1657add_to_input_buf(s, len)
1658 char_u *s;
1659 int len;
1660{
1661 if (inbufcount + len > INBUFLEN + MAX_KEY_CODE_LEN)
1662 return; /* Shouldn't ever happen! */
1663
1664#ifdef FEAT_HANGULIN
1665 if ((State & (INSERT|CMDLINE)) && hangul_input_state_get())
1666 if ((len = hangul_input_process(s, len)) == 0)
1667 return;
1668#endif
1669
1670 while (len--)
1671 inbuf[inbufcount++] = *s++;
1672}
1673#endif
1674
Bram Moolenaar70c2a632007-08-15 18:08:50 +00001675#if ((defined(FEAT_XIM) || defined(FEAT_DND)) && defined(FEAT_GUI_GTK)) \
1676 || defined(FEAT_GUI_MSWIN) \
1677 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 || (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) \
Bram Moolenaarf52c7252006-02-10 23:23:57 +00001679 || (defined(FEAT_GUI) && (!defined(USE_ON_FLY_SCROLL) \
1680 || defined(FEAT_MENU))) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 || defined(PROTO)
1682/*
1683 * Add "str[len]" to the input buffer while escaping CSI bytes.
1684 */
1685 void
1686add_to_input_buf_csi(char_u *str, int len)
1687{
1688 int i;
1689 char_u buf[2];
1690
1691 for (i = 0; i < len; ++i)
1692 {
1693 add_to_input_buf(str + i, 1);
1694 if (str[i] == CSI)
1695 {
1696 /* Turn CSI into K_CSI. */
1697 buf[0] = KS_EXTRA;
1698 buf[1] = (int)KE_CSI;
1699 add_to_input_buf(buf, 2);
1700 }
1701 }
1702}
1703#endif
1704
1705#if defined(FEAT_HANGULIN) || defined(PROTO)
1706 void
Bram Moolenaard44347f2011-06-19 01:14:29 +02001707push_raw_key(s, len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 char_u *s;
1709 int len;
1710{
1711 while (len--)
1712 inbuf[inbufcount++] = *s++;
1713}
1714#endif
1715
1716#if defined(FEAT_GUI) || defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) \
1717 || defined(PROTO)
1718/* Remove everything from the input buffer. Called when ^C is found */
1719 void
1720trash_input_buf()
1721{
1722 inbufcount = 0;
1723}
1724#endif
1725
1726/*
1727 * Read as much data from the input buffer as possible up to maxlen, and store
1728 * it in buf.
1729 * Note: this function used to be Read() in unix.c
1730 */
1731 int
1732read_from_input_buf(buf, maxlen)
1733 char_u *buf;
1734 long maxlen;
1735{
1736 if (inbufcount == 0) /* if the buffer is empty, fill it */
1737 fill_input_buf(TRUE);
1738 if (maxlen > inbufcount)
1739 maxlen = inbufcount;
1740 mch_memmove(buf, inbuf, (size_t)maxlen);
1741 inbufcount -= maxlen;
1742 if (inbufcount)
1743 mch_memmove(inbuf, inbuf + maxlen, (size_t)inbufcount);
1744 return (int)maxlen;
1745}
1746
1747 void
1748fill_input_buf(exit_on_error)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001749 int exit_on_error UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750{
1751#if defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X_UNIX)
1752 int len;
1753 int try;
1754 static int did_read_something = FALSE;
1755# ifdef FEAT_MBYTE
1756 static char_u *rest = NULL; /* unconverted rest of previous read */
1757 static int restlen = 0;
1758 int unconverted;
1759# endif
1760#endif
1761
1762#ifdef FEAT_GUI
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001763 if (gui.in_use
1764# ifdef NO_CONSOLE_INPUT
1765 /* Don't use the GUI input when the window hasn't been opened yet.
1766 * We get here from ui_inchar() when we should try reading from stdin. */
1767 && !no_console_input()
1768# endif
1769 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 {
1771 gui_mch_update();
1772 return;
1773 }
1774#endif
1775#if defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X_UNIX)
1776 if (vim_is_input_buf_full())
1777 return;
1778 /*
1779 * Fill_input_buf() is only called when we really need a character.
1780 * If we can't get any, but there is some in the buffer, just return.
1781 * If we can't get any, and there isn't any in the buffer, we give up and
1782 * exit Vim.
1783 */
1784# ifdef __BEOS__
1785 /*
1786 * On the BeBox version (for now), all input is secretly performed within
1787 * beos_select() which is called from RealWaitForChar().
1788 */
1789 while (!vim_is_input_buf_full() && RealWaitForChar(read_cmd_fd, 0, NULL))
1790 ;
1791 len = inbufcount;
1792 inbufcount = 0;
1793# else
1794
1795# ifdef FEAT_SNIFF
1796 if (sniff_request_waiting)
1797 {
1798 add_to_input_buf((char_u *)"\233sniff",6); /* results in K_SNIFF */
1799 sniff_request_waiting = 0;
1800 want_sniff_request = 0;
1801 return;
1802 }
1803# endif
1804
1805# ifdef FEAT_MBYTE
1806 if (rest != NULL)
1807 {
1808 /* Use remainder of previous call, starts with an invalid character
1809 * that may become valid when reading more. */
1810 if (restlen > INBUFLEN - inbufcount)
1811 unconverted = INBUFLEN - inbufcount;
1812 else
1813 unconverted = restlen;
1814 mch_memmove(inbuf + inbufcount, rest, unconverted);
1815 if (unconverted == restlen)
1816 {
1817 vim_free(rest);
1818 rest = NULL;
1819 }
1820 else
1821 {
1822 restlen -= unconverted;
1823 mch_memmove(rest, rest + unconverted, restlen);
1824 }
1825 inbufcount += unconverted;
1826 }
1827 else
1828 unconverted = 0;
1829#endif
1830
1831 len = 0; /* to avoid gcc warning */
1832 for (try = 0; try < 100; ++try)
1833 {
1834# ifdef VMS
1835 len = vms_read(
1836# else
1837 len = read(read_cmd_fd,
1838# endif
1839 (char *)inbuf + inbufcount, (size_t)((INBUFLEN - inbufcount)
1840# ifdef FEAT_MBYTE
1841 / input_conv.vc_factor
1842# endif
1843 ));
1844# if 0
1845 ) /* avoid syntax highlight error */
1846# endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001847
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 if (len > 0 || got_int)
1849 break;
1850 /*
1851 * If reading stdin results in an error, continue reading stderr.
1852 * This helps when using "foo | xargs vim".
1853 */
1854 if (!did_read_something && !isatty(read_cmd_fd) && read_cmd_fd == 0)
1855 {
1856 int m = cur_tmode;
1857
1858 /* We probably set the wrong file descriptor to raw mode. Switch
1859 * back to cooked mode, use another descriptor and set the mode to
1860 * what it was. */
1861 settmode(TMODE_COOK);
1862#ifdef HAVE_DUP
1863 /* Use stderr for stdin, also works for shell commands. */
1864 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00001865 ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866#else
1867 read_cmd_fd = 2; /* read from stderr instead of stdin */
1868#endif
1869 settmode(m);
1870 }
1871 if (!exit_on_error)
1872 return;
1873 }
1874# endif
1875 if (len <= 0 && !got_int)
1876 read_error_exit();
1877 if (len > 0)
1878 did_read_something = TRUE;
1879 if (got_int)
1880 {
1881 /* Interrupted, pretend a CTRL-C was typed. */
1882 inbuf[0] = 3;
1883 inbufcount = 1;
1884 }
1885 else
1886 {
1887# ifdef FEAT_MBYTE
1888 /*
1889 * May perform conversion on the input characters.
1890 * Include the unconverted rest of the previous call.
1891 * If there is an incomplete char at the end it is kept for the next
1892 * time, reading more bytes should make conversion possible.
1893 * Don't do this in the unlikely event that the input buffer is too
1894 * small ("rest" still contains more bytes).
1895 */
1896 if (input_conv.vc_type != CONV_NONE)
1897 {
1898 inbufcount -= unconverted;
1899 len = convert_input_safe(inbuf + inbufcount,
1900 len + unconverted, INBUFLEN - inbufcount,
1901 rest == NULL ? &rest : NULL, &restlen);
1902 }
1903# endif
1904 while (len-- > 0)
1905 {
1906 /*
1907 * if a CTRL-C was typed, remove it from the buffer and set got_int
1908 */
1909 if (inbuf[inbufcount] == 3 && ctrl_c_interrupts)
1910 {
1911 /* remove everything typed before the CTRL-C */
1912 mch_memmove(inbuf, inbuf + inbufcount, (size_t)(len + 1));
1913 inbufcount = 0;
1914 got_int = TRUE;
1915 }
1916 ++inbufcount;
1917 }
1918 }
1919#endif /* UNIX or OS2 or VMS*/
1920}
1921#endif /* defined(UNIX) || defined(FEAT_GUI) || defined(OS2) || defined(VMS) */
1922
1923/*
1924 * Exit because of an input read error.
1925 */
1926 void
1927read_error_exit()
1928{
1929 if (silent_mode) /* Normal way to exit for "ex -s" */
1930 getout(0);
1931 STRCPY(IObuff, _("Vim: Error reading input, exiting...\n"));
1932 preserve_exit();
1933}
1934
1935#if defined(CURSOR_SHAPE) || defined(PROTO)
1936/*
1937 * May update the shape of the cursor.
1938 */
1939 void
1940ui_cursor_shape()
1941{
1942# ifdef FEAT_GUI
1943 if (gui.in_use)
1944 gui_update_cursor_later();
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001945 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001947 term_cursor_shape();
1948
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949# ifdef MCH_CURSOR_SHAPE
1950 mch_update_cursor();
1951# endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001952
1953# ifdef FEAT_CONCEAL
Bram Moolenaar8e469272010-07-28 19:38:16 +02001954 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001955# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956}
1957#endif
1958
1959#if defined(FEAT_CLIPBOARD) || defined(FEAT_GUI) || defined(FEAT_RIGHTLEFT) \
Bram Moolenaaraf51e662008-07-14 19:48:05 +00001960 || defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961/*
1962 * Check bounds for column number
1963 */
1964 int
1965check_col(col)
1966 int col;
1967{
1968 if (col < 0)
1969 return 0;
1970 if (col >= (int)screen_Columns)
1971 return (int)screen_Columns - 1;
1972 return col;
1973}
1974
1975/*
1976 * Check bounds for row number
1977 */
1978 int
1979check_row(row)
1980 int row;
1981{
1982 if (row < 0)
1983 return 0;
1984 if (row >= (int)screen_Rows)
1985 return (int)screen_Rows - 1;
1986 return row;
1987}
1988#endif
1989
1990/*
1991 * Stuff for the X clipboard. Shared between VMS and Unix.
1992 */
1993
1994#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) || defined(PROTO)
1995# include <X11/Xatom.h>
1996# include <X11/Intrinsic.h>
1997
1998/*
1999 * Open the application context (if it hasn't been opened yet).
2000 * Used for Motif and Athena GUI and the xterm clipboard.
2001 */
2002 void
2003open_app_context()
2004{
2005 if (app_context == NULL)
2006 {
2007 XtToolkitInitialize();
2008 app_context = XtCreateApplicationContext();
2009 }
2010}
2011
2012static Atom vim_atom; /* Vim's own special selection format */
2013#ifdef FEAT_MBYTE
2014static Atom vimenc_atom; /* Vim's extended selection format */
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002015static Atom utf8_atom;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016#endif
2017static Atom compound_text_atom;
2018static Atom text_atom;
2019static Atom targets_atom;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002020static Atom timestamp_atom; /* Used to get a timestamp */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021
2022 void
2023x11_setup_atoms(dpy)
2024 Display *dpy;
2025{
2026 vim_atom = XInternAtom(dpy, VIM_ATOM_NAME, False);
2027#ifdef FEAT_MBYTE
2028 vimenc_atom = XInternAtom(dpy, VIMENC_ATOM_NAME,False);
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002029 utf8_atom = XInternAtom(dpy, "UTF8_STRING", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030#endif
2031 compound_text_atom = XInternAtom(dpy, "COMPOUND_TEXT", False);
2032 text_atom = XInternAtom(dpy, "TEXT", False);
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002033 targets_atom = XInternAtom(dpy, "TARGETS", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 clip_star.sel_atom = XA_PRIMARY;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002035 clip_plus.sel_atom = XInternAtom(dpy, "CLIPBOARD", False);
2036 timestamp_atom = XInternAtom(dpy, "TIMESTAMP", False);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037}
2038
2039/*
2040 * X Selection stuff, for cutting and pasting text to other windows.
2041 */
2042
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002043static Boolean clip_x11_convert_selection_cb __ARGS((Widget, Atom *, Atom *, Atom *, XtPointer *, long_u *, int *));
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002044static void clip_x11_lose_ownership_cb __ARGS((Widget, Atom *));
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002045static void clip_x11_timestamp_cb __ARGS((Widget w, XtPointer n, XEvent *event, Boolean *cont));
Bram Moolenaar62b42182010-09-21 22:09:37 +02002046static void clip_x11_request_selection_cb __ARGS((Widget, XtPointer, Atom *, Atom *, XtPointer, long_u *, int *));
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002047
2048/*
2049 * Property callback to get a timestamp for XtOwnSelection.
2050 */
2051 static void
2052clip_x11_timestamp_cb(w, n, event, cont)
2053 Widget w;
2054 XtPointer n UNUSED;
2055 XEvent *event;
2056 Boolean *cont UNUSED;
2057{
2058 Atom actual_type;
2059 int format;
2060 unsigned long nitems, bytes_after;
2061 unsigned char *prop=NULL;
2062 XPropertyEvent *xproperty=&event->xproperty;
2063
2064 /* Must be a property notify, state can't be Delete (True), has to be
2065 * one of the supported selection types. */
2066 if (event->type != PropertyNotify || xproperty->state
2067 || (xproperty->atom != clip_star.sel_atom
2068 && xproperty->atom != clip_plus.sel_atom))
2069 return;
2070
2071 if (XGetWindowProperty(xproperty->display, xproperty->window,
2072 xproperty->atom, 0, 0, False, timestamp_atom, &actual_type, &format,
2073 &nitems, &bytes_after, &prop))
2074 return;
2075
2076 if (prop)
2077 XFree(prop);
2078
2079 /* Make sure the property type is "TIMESTAMP" and it's 32 bits. */
2080 if (actual_type != timestamp_atom || format != 32)
2081 return;
2082
2083 /* Get the selection, using the event timestamp. */
Bram Moolenaar62b42182010-09-21 22:09:37 +02002084 if (XtOwnSelection(w, xproperty->atom, xproperty->time,
2085 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
2086 NULL) == OK)
2087 {
2088 /* Set the "owned" flag now, there may have been a call to
2089 * lose_ownership_cb in between. */
2090 if (xproperty->atom == clip_plus.sel_atom)
2091 clip_plus.owned = TRUE;
2092 else
2093 clip_star.owned = TRUE;
2094 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002095}
2096
2097 void
2098x11_setup_selection(w)
2099 Widget w;
2100{
2101 XtAddEventHandler(w, PropertyChangeMask, False,
2102 /*(XtEventHandler)*/clip_x11_timestamp_cb, (XtPointer)NULL);
2103}
2104
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 static void
2106clip_x11_request_selection_cb(w, success, sel_atom, type, value, length,
2107 format)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002108 Widget w UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 XtPointer success;
2110 Atom *sel_atom;
2111 Atom *type;
2112 XtPointer value;
2113 long_u *length;
2114 int *format;
2115{
Bram Moolenaard44347f2011-06-19 01:14:29 +02002116 int motion_type = MAUTO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 long_u len;
2118 char_u *p;
2119 char **text_list = NULL;
2120 VimClipboard *cbd;
2121#ifdef FEAT_MBYTE
2122 char_u *tmpbuf = NULL;
2123#endif
2124
2125 if (*sel_atom == clip_plus.sel_atom)
2126 cbd = &clip_plus;
2127 else
2128 cbd = &clip_star;
2129
2130 if (value == NULL || *length == 0)
2131 {
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002132 clip_free_selection(cbd); /* nothing received, clear register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 *(int *)success = FALSE;
2134 return;
2135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 p = (char_u *)value;
2137 len = *length;
2138 if (*type == vim_atom)
2139 {
2140 motion_type = *p++;
2141 len--;
2142 }
2143
2144#ifdef FEAT_MBYTE
2145 else if (*type == vimenc_atom)
2146 {
2147 char_u *enc;
2148 vimconv_T conv;
2149 int convlen;
2150
2151 motion_type = *p++;
2152 --len;
2153
2154 enc = p;
2155 p += STRLEN(p) + 1;
2156 len -= p - enc;
2157
2158 /* If the encoding of the text is different from 'encoding', attempt
2159 * converting it. */
2160 conv.vc_type = CONV_NONE;
2161 convert_setup(&conv, enc, p_enc);
2162 if (conv.vc_type != CONV_NONE)
2163 {
2164 convlen = len; /* Need to use an int here. */
2165 tmpbuf = string_convert(&conv, p, &convlen);
2166 len = convlen;
2167 if (tmpbuf != NULL)
2168 p = tmpbuf;
2169 convert_setup(&conv, NULL, NULL);
2170 }
2171 }
2172#endif
2173
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002174 else if (*type == compound_text_atom
2175#ifdef FEAT_MBYTE
2176 || *type == utf8_atom
2177#endif
2178 || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179#ifdef FEAT_MBYTE
2180 enc_dbcs != 0 &&
2181#endif
2182 *type == text_atom))
2183 {
2184 XTextProperty text_prop;
2185 int n_text = 0;
2186 int status;
2187
2188 text_prop.value = (unsigned char *)value;
2189 text_prop.encoding = *type;
2190 text_prop.format = *format;
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002191 text_prop.nitems = len;
Bram Moolenaar81851112013-04-12 12:27:30 +02002192#if defined(FEAT_MBYTE) && defined(X_HAVE_UTF8_STRING)
Bram Moolenaare2e663f2013-03-07 18:02:30 +01002193 if (*type == utf8_atom)
2194 status = Xutf8TextPropertyToTextList(X_DISPLAY, &text_prop,
2195 &text_list, &n_text);
2196 else
2197#endif
2198 status = XmbTextPropertyToTextList(X_DISPLAY, &text_prop,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 &text_list, &n_text);
2200 if (status != Success || n_text < 1)
2201 {
2202 *(int *)success = FALSE;
2203 return;
2204 }
2205 p = (char_u *)text_list[0];
2206 len = STRLEN(p);
2207 }
2208 clip_yank_selection(motion_type, p, (long)len, cbd);
2209
2210 if (text_list != NULL)
2211 XFreeStringList(text_list);
2212#ifdef FEAT_MBYTE
2213 vim_free(tmpbuf);
2214#endif
2215 XtFree((char *)value);
2216 *(int *)success = TRUE;
2217}
2218
2219 void
2220clip_x11_request_selection(myShell, dpy, cbd)
2221 Widget myShell;
2222 Display *dpy;
2223 VimClipboard *cbd;
2224{
2225 XEvent event;
2226 Atom type;
2227 static int success;
2228 int i;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002229 time_t start_time;
2230 int timed_out = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231
2232 for (i =
2233#ifdef FEAT_MBYTE
2234 0
2235#else
2236 1
2237#endif
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002238 ; i < 6; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 {
2240 switch (i)
2241 {
2242#ifdef FEAT_MBYTE
2243 case 0: type = vimenc_atom; break;
2244#endif
2245 case 1: type = vim_atom; break;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002246#ifdef FEAT_MBYTE
2247 case 2: type = utf8_atom; break;
2248#endif
2249 case 3: type = compound_text_atom; break;
2250 case 4: type = text_atom; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 default: type = XA_STRING;
2252 }
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002253#ifdef FEAT_MBYTE
Bram Moolenaar81851112013-04-12 12:27:30 +02002254 if (type == utf8_atom
2255# if defined(X_HAVE_UTF8_STRING)
2256 && !enc_utf8
2257# endif
2258 )
2259 /* Only request utf-8 when 'encoding' is utf8 and
2260 * Xutf8TextPropertyToTextList is available. */
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002261 continue;
2262#endif
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002263 success = MAYBE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 XtGetSelectionValue(myShell, cbd->sel_atom, type,
2265 clip_x11_request_selection_cb, (XtPointer)&success, CurrentTime);
2266
2267 /* Make sure the request for the selection goes out before waiting for
2268 * a response. */
2269 XFlush(dpy);
2270
2271 /*
2272 * Wait for result of selection request, otherwise if we type more
2273 * characters, then they will appear before the one that requested the
2274 * paste! Don't worry, we will catch up with any other events later.
2275 */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002276 start_time = time(NULL);
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002277 while (success == MAYBE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 {
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002279 if (XCheckTypedEvent(dpy, SelectionNotify, &event)
2280 || XCheckTypedEvent(dpy, SelectionRequest, &event)
2281 || XCheckTypedEvent(dpy, PropertyNotify, &event))
Bram Moolenaar89417b92008-09-07 19:48:53 +00002282 {
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002283 /* This is where clip_x11_request_selection_cb() should be
2284 * called. It may actually happen a bit later, so we loop
2285 * until "success" changes.
2286 * We may get a SelectionRequest here and if we don't handle
2287 * it we hang. KDE klipper does this, for example.
2288 * We need to handle a PropertyNotify for large selections. */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002289 XtDispatchEvent(&event);
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002290 continue;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292
Bram Moolenaar89417b92008-09-07 19:48:53 +00002293 /* Time out after 2 to 3 seconds to avoid that we hang when the
2294 * other process doesn't respond. Note that the SelectionNotify
2295 * event may still come later when the selection owner comes back
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002296 * to life and the text gets inserted unexpectedly. Don't know
2297 * why that happens or how to avoid that :-(. */
Bram Moolenaar89417b92008-09-07 19:48:53 +00002298 if (time(NULL) > start_time + 2)
2299 {
2300 timed_out = TRUE;
2301 break;
2302 }
2303
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 /* Do we need this? Probably not. */
2305 XSync(dpy, False);
2306
Bram Moolenaar89417b92008-09-07 19:48:53 +00002307 /* Wait for 1 msec to avoid that we eat up all CPU time. */
2308 ui_delay(1L, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 }
2310
Bram Moolenaar24d92ce2008-09-14 13:58:34 +00002311 if (success == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 return;
Bram Moolenaar89417b92008-09-07 19:48:53 +00002313
2314 /* don't do a retry with another type after timing out, otherwise we
2315 * hang for 15 seconds. */
2316 if (timed_out)
2317 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 }
2319
2320 /* Final fallback position - use the X CUT_BUFFER0 store */
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002321 yank_cut_buffer0(dpy, cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322}
2323
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 static Boolean
2325clip_x11_convert_selection_cb(w, sel_atom, target, type, value, length, format)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002326 Widget w UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 Atom *sel_atom;
2328 Atom *target;
2329 Atom *type;
2330 XtPointer *value;
2331 long_u *length;
2332 int *format;
2333{
2334 char_u *string;
2335 char_u *result;
2336 int motion_type;
2337 VimClipboard *cbd;
2338 int i;
2339
2340 if (*sel_atom == clip_plus.sel_atom)
2341 cbd = &clip_plus;
2342 else
2343 cbd = &clip_star;
2344
2345 if (!cbd->owned)
2346 return False; /* Shouldn't ever happen */
2347
2348 /* requestor wants to know what target types we support */
2349 if (*target == targets_atom)
2350 {
2351 Atom *array;
2352
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002353 if ((array = (Atom *)XtMalloc((unsigned)(sizeof(Atom) * 7))) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 return False;
2355 *value = (XtPointer)array;
2356 i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 array[i++] = targets_atom;
2358#ifdef FEAT_MBYTE
2359 array[i++] = vimenc_atom;
2360#endif
2361 array[i++] = vim_atom;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002362#ifdef FEAT_MBYTE
2363 if (enc_utf8)
2364 array[i++] = utf8_atom;
2365#endif
2366 array[i++] = XA_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 array[i++] = text_atom;
2368 array[i++] = compound_text_atom;
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002369
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 *type = XA_ATOM;
2371 /* This used to be: *format = sizeof(Atom) * 8; but that caused
2372 * crashes on 64 bit machines. (Peter Derr) */
2373 *format = 32;
2374 *length = i;
2375 return True;
2376 }
2377
2378 if ( *target != XA_STRING
2379#ifdef FEAT_MBYTE
2380 && *target != vimenc_atom
Bram Moolenaar980e58f2014-06-12 13:28:30 +02002381 && (*target != utf8_atom || !enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382#endif
2383 && *target != vim_atom
2384 && *target != text_atom
2385 && *target != compound_text_atom)
2386 return False;
2387
2388 clip_get_selection(cbd);
2389 motion_type = clip_convert_selection(&string, length, cbd);
2390 if (motion_type < 0)
2391 return False;
2392
2393 /* For our own format, the first byte contains the motion type */
2394 if (*target == vim_atom)
2395 (*length)++;
2396
2397#ifdef FEAT_MBYTE
2398 /* Our own format with encoding: motion 'encoding' NUL text */
2399 if (*target == vimenc_atom)
2400 *length += STRLEN(p_enc) + 2;
2401#endif
2402
2403 *value = XtMalloc((Cardinal)*length);
2404 result = (char_u *)*value;
2405 if (result == NULL)
2406 {
2407 vim_free(string);
2408 return False;
2409 }
2410
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002411 if (*target == XA_STRING
2412#ifdef FEAT_MBYTE
2413 || (*target == utf8_atom && enc_utf8)
2414#endif
2415 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 {
2417 mch_memmove(result, string, (size_t)(*length));
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002418 *type = *target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 }
Bram Moolenaarefcb54b2012-02-12 01:35:10 +01002420 else if (*target == compound_text_atom || *target == text_atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
2422 XTextProperty text_prop;
2423 char *string_nt = (char *)alloc((unsigned)*length + 1);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002424 int conv_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425
2426 /* create NUL terminated string which XmbTextListToTextProperty wants */
2427 mch_memmove(string_nt, string, (size_t)*length);
2428 string_nt[*length] = NUL;
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002429 conv_result = XmbTextListToTextProperty(X_DISPLAY, (char **)&string_nt,
2430 1, XCompoundTextStyle, &text_prop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 vim_free(string_nt);
2432 XtFree(*value); /* replace with COMPOUND text */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002433 if (conv_result != Success)
2434 {
2435 vim_free(string);
2436 return False;
2437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 *value = (XtPointer)(text_prop.value); /* from plain text */
2439 *length = text_prop.nitems;
2440 *type = compound_text_atom;
2441 }
2442
2443#ifdef FEAT_MBYTE
2444 else if (*target == vimenc_atom)
2445 {
2446 int l = STRLEN(p_enc);
2447
2448 result[0] = motion_type;
2449 STRCPY(result + 1, p_enc);
2450 mch_memmove(result + l + 2, string, (size_t)(*length - l - 2));
2451 *type = vimenc_atom;
2452 }
2453#endif
2454
2455 else
2456 {
2457 result[0] = motion_type;
2458 mch_memmove(result + 1, string, (size_t)(*length - 1));
2459 *type = vim_atom;
2460 }
2461 *format = 8; /* 8 bits per char */
2462 vim_free(string);
2463 return True;
2464}
2465
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 static void
2467clip_x11_lose_ownership_cb(w, sel_atom)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002468 Widget w UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 Atom *sel_atom;
2470{
2471 if (*sel_atom == clip_plus.sel_atom)
2472 clip_lose_selection(&clip_plus);
2473 else
2474 clip_lose_selection(&clip_star);
2475}
2476
2477 void
2478clip_x11_lose_selection(myShell, cbd)
Bram Moolenaar62b42182010-09-21 22:09:37 +02002479 Widget myShell;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 VimClipboard *cbd;
2481{
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002482 XtDisownSelection(myShell, cbd->sel_atom,
2483 XtLastTimestampProcessed(XtDisplay(myShell)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484}
2485
2486 int
2487clip_x11_own_selection(myShell, cbd)
Bram Moolenaar62b42182010-09-21 22:09:37 +02002488 Widget myShell;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 VimClipboard *cbd;
2490{
Bram Moolenaar62b42182010-09-21 22:09:37 +02002491 /* When using the GUI we have proper timestamps, use the one of the last
2492 * event. When in the console we don't get events (the terminal gets
2493 * them), Get the time by a zero-length append, clip_x11_timestamp_cb will
2494 * be called with the current timestamp. */
2495#ifdef FEAT_GUI
2496 if (gui.in_use)
2497 {
2498 if (XtOwnSelection(myShell, cbd->sel_atom,
2499 XtLastTimestampProcessed(XtDisplay(myShell)),
2500 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
2501 NULL) == False)
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +01002502 return FAIL;
Bram Moolenaar62b42182010-09-21 22:09:37 +02002503 }
2504 else
2505#endif
2506 {
2507 if (!XChangeProperty(XtDisplay(myShell), XtWindow(myShell),
2508 cbd->sel_atom, timestamp_atom, 32, PropModeAppend, NULL, 0))
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +01002509 return FAIL;
Bram Moolenaar62b42182010-09-21 22:09:37 +02002510 }
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002511 /* Flush is required in a terminal as nothing else is doing it. */
2512 XFlush(XtDisplay(myShell));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 return OK;
2514}
2515
2516/*
2517 * Send the current selection to the clipboard. Do nothing for X because we
2518 * will fill in the selection only when requested by another app.
2519 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 void
2521clip_x11_set_selection(cbd)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002522 VimClipboard *cbd UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523{
2524}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01002525
2526 int
2527clip_x11_owner_exists(cbd)
2528 VimClipboard *cbd;
2529{
2530 return XGetSelectionOwner(X_DISPLAY, cbd->sel_atom) != None;
2531}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532#endif
2533
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002534#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) \
2535 || defined(FEAT_GUI_GTK) || defined(PROTO)
2536/*
2537 * Get the contents of the X CUT_BUFFER0 and put it in "cbd".
2538 */
2539 void
2540yank_cut_buffer0(dpy, cbd)
2541 Display *dpy;
2542 VimClipboard *cbd;
2543{
2544 int nbytes = 0;
2545 char_u *buffer = (char_u *)XFetchBuffer(dpy, &nbytes, 0);
2546
2547 if (nbytes > 0)
2548 {
2549#ifdef FEAT_MBYTE
2550 int done = FALSE;
2551
2552 /* CUT_BUFFER0 is supposed to be always latin1. Convert to 'enc' when
2553 * using a multi-byte encoding. Conversion between two 8-bit
2554 * character sets usually fails and the text might actually be in
2555 * 'enc' anyway. */
2556 if (has_mbyte)
2557 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002558 char_u *conv_buf;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00002559 vimconv_T vc;
2560
2561 vc.vc_type = CONV_NONE;
2562 if (convert_setup(&vc, (char_u *)"latin1", p_enc) == OK)
2563 {
2564 conv_buf = string_convert(&vc, buffer, &nbytes);
2565 if (conv_buf != NULL)
2566 {
2567 clip_yank_selection(MCHAR, conv_buf, (long)nbytes, cbd);
2568 vim_free(conv_buf);
2569 done = TRUE;
2570 }
2571 convert_setup(&vc, NULL, NULL);
2572 }
2573 }
2574 if (!done) /* use the text without conversion */
2575#endif
2576 clip_yank_selection(MCHAR, buffer, (long)nbytes, cbd);
2577 XFree((void *)buffer);
2578 if (p_verbose > 0)
2579 {
2580 verbose_enter();
2581 verb_msg((char_u *)_("Used CUT_BUFFER0 instead of empty selection"));
2582 verbose_leave();
2583 }
2584 }
2585}
2586#endif
2587
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588#if defined(FEAT_MOUSE) || defined(PROTO)
2589
2590/*
2591 * Move the cursor to the specified row and column on the screen.
Bram Moolenaar49325942007-05-10 19:19:59 +00002592 * Change current window if necessary. Returns an integer with the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 * CURSOR_MOVED bit set if the cursor has moved or unset otherwise.
2594 *
2595 * The MOUSE_FOLD_CLOSE bit is set when clicked on the '-' in a fold column.
2596 * The MOUSE_FOLD_OPEN bit is set when clicked on the '+' in a fold column.
2597 *
2598 * If flags has MOUSE_FOCUS, then the current window will not be changed, and
2599 * if the mouse is outside the window then the text will scroll, or if the
2600 * mouse was previously on a status line, then the status line may be dragged.
2601 *
2602 * If flags has MOUSE_MAY_VIS, then VIsual mode will be started before the
2603 * cursor is moved unless the cursor was on a status line.
2604 * This function returns one of IN_UNKNOWN, IN_BUFFER, IN_STATUS_LINE or
2605 * IN_SEP_LINE depending on where the cursor was clicked.
2606 *
2607 * If flags has MOUSE_MAY_STOP_VIS, then Visual mode will be stopped, unless
2608 * the mouse is on the status line of the same window.
2609 *
2610 * If flags has MOUSE_DID_MOVE, nothing is done if the mouse didn't move since
2611 * the last call.
2612 *
2613 * If flags has MOUSE_SETPOS, nothing is done, only the current position is
2614 * remembered.
2615 */
2616 int
2617jump_to_mouse(flags, inclusive, which_button)
2618 int flags;
2619 int *inclusive; /* used for inclusive operator, can be NULL */
2620 int which_button; /* MOUSE_LEFT, MOUSE_RIGHT, MOUSE_MIDDLE */
2621{
2622 static int on_status_line = 0; /* #lines below bottom of window */
2623#ifdef FEAT_VERTSPLIT
2624 static int on_sep_line = 0; /* on separator right of window */
2625#endif
2626 static int prev_row = -1;
2627 static int prev_col = -1;
2628 static win_T *dragwin = NULL; /* window being dragged */
2629 static int did_drag = FALSE; /* drag was noticed */
2630
2631 win_T *wp, *old_curwin;
2632 pos_T old_cursor;
2633 int count;
2634 int first;
2635 int row = mouse_row;
2636 int col = mouse_col;
2637#ifdef FEAT_FOLDING
2638 int mouse_char;
2639#endif
2640
2641 mouse_past_bottom = FALSE;
2642 mouse_past_eol = FALSE;
2643
2644 if (flags & MOUSE_RELEASED)
2645 {
2646 /* On button release we may change window focus if positioned on a
2647 * status line and no dragging happened. */
2648 if (dragwin != NULL && !did_drag)
2649 flags &= ~(MOUSE_FOCUS | MOUSE_DID_MOVE);
2650 dragwin = NULL;
2651 did_drag = FALSE;
2652 }
2653
2654 if ((flags & MOUSE_DID_MOVE)
2655 && prev_row == mouse_row
2656 && prev_col == mouse_col)
2657 {
2658retnomove:
Bram Moolenaar49325942007-05-10 19:19:59 +00002659 /* before moving the cursor for a left click which is NOT in a status
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 * line, stop Visual mode */
2661 if (on_status_line)
2662 return IN_STATUS_LINE;
2663#ifdef FEAT_VERTSPLIT
2664 if (on_sep_line)
2665 return IN_SEP_LINE;
2666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 if (flags & MOUSE_MAY_STOP_VIS)
2668 {
2669 end_visual_mode();
2670 redraw_curbuf_later(INVERTED); /* delete the inversion */
2671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2673 /* Continue a modeless selection in another window. */
2674 if (cmdwin_type != 0 && row < W_WINROW(curwin))
2675 return IN_OTHER_WIN;
2676#endif
2677 return IN_BUFFER;
2678 }
2679
2680 prev_row = mouse_row;
2681 prev_col = mouse_col;
2682
2683 if (flags & MOUSE_SETPOS)
2684 goto retnomove; /* ugly goto... */
2685
2686#ifdef FEAT_FOLDING
2687 /* Remember the character under the mouse, it might be a '-' or '+' in the
2688 * fold column. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002689 if (row >= 0 && row < Rows && col >= 0 && col <= Columns
2690 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 mouse_char = ScreenLines[LineOffset[row] + col];
2692 else
2693 mouse_char = ' ';
2694#endif
2695
2696 old_curwin = curwin;
2697 old_cursor = curwin->w_cursor;
2698
2699 if (!(flags & MOUSE_FOCUS))
2700 {
2701 if (row < 0 || col < 0) /* check if it makes sense */
2702 return IN_UNKNOWN;
2703
2704#ifdef FEAT_WINDOWS
2705 /* find the window where the row is in */
2706 wp = mouse_find_win(&row, &col);
2707#else
2708 wp = firstwin;
2709#endif
2710 dragwin = NULL;
2711 /*
2712 * winpos and height may change in win_enter()!
2713 */
2714 if (row >= wp->w_height) /* In (or below) status line */
2715 {
2716 on_status_line = row - wp->w_height + 1;
2717 dragwin = wp;
2718 }
2719 else
2720 on_status_line = 0;
2721#ifdef FEAT_VERTSPLIT
2722 if (col >= wp->w_width) /* In separator line */
2723 {
2724 on_sep_line = col - wp->w_width + 1;
2725 dragwin = wp;
2726 }
2727 else
2728 on_sep_line = 0;
2729
2730 /* The rightmost character of the status line might be a vertical
2731 * separator character if there is no connecting window to the right. */
2732 if (on_status_line && on_sep_line)
2733 {
2734 if (stl_connected(wp))
2735 on_sep_line = 0;
2736 else
2737 on_status_line = 0;
2738 }
2739#endif
2740
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 /* Before jumping to another buffer, or moving the cursor for a left
2742 * click, stop Visual mode. */
2743 if (VIsual_active
2744 && (wp->w_buffer != curwin->w_buffer
2745 || (!on_status_line
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002746#ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 && !on_sep_line
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002748#endif
2749#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002751# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 wp->w_p_rl ? col < W_WIDTH(wp) - wp->w_p_fdc :
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753# endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002754 col >= wp->w_p_fdc
2755# ifdef FEAT_CMDWIN
2756 + (cmdwin_type == 0 && wp == curwin ? 0 : 1)
2757# endif
2758 )
2759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 && (flags & MOUSE_MAY_STOP_VIS))))
2761 {
2762 end_visual_mode();
2763 redraw_curbuf_later(INVERTED); /* delete the inversion */
2764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765#ifdef FEAT_CMDWIN
2766 if (cmdwin_type != 0 && wp != curwin)
2767 {
2768 /* A click outside the command-line window: Use modeless
Bram Moolenaarf679a432010-03-02 18:16:09 +01002769 * selection if possible. Allow dragging the status lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770# ifdef FEAT_VERTSPLIT
2771 on_sep_line = 0;
2772# endif
2773# ifdef FEAT_CLIPBOARD
2774 if (on_status_line)
2775 return IN_STATUS_LINE;
2776 return IN_OTHER_WIN;
2777# else
2778 row = 0;
2779 col += wp->w_wincol;
2780 wp = curwin;
2781# endif
2782 }
2783#endif
2784#ifdef FEAT_WINDOWS
2785 /* Only change window focus when not clicking on or dragging the
2786 * status line. Do change focus when releasing the mouse button
2787 * (MOUSE_FOCUS was set above if we dragged first). */
2788 if (dragwin == NULL || (flags & MOUSE_RELEASED))
2789 win_enter(wp, TRUE); /* can make wp invalid! */
2790# ifdef CHECK_DOUBLE_CLICK
2791 /* set topline, to be able to check for double click ourselves */
2792 if (curwin != old_curwin)
2793 set_mouse_topline(curwin);
2794# endif
2795#endif
2796 if (on_status_line) /* In (or below) status line */
2797 {
2798 /* Don't use start_arrow() if we're in the same window */
2799 if (curwin == old_curwin)
2800 return IN_STATUS_LINE;
2801 else
2802 return IN_STATUS_LINE | CURSOR_MOVED;
2803 }
2804#ifdef FEAT_VERTSPLIT
2805 if (on_sep_line) /* In (or below) status line */
2806 {
2807 /* Don't use start_arrow() if we're in the same window */
2808 if (curwin == old_curwin)
2809 return IN_SEP_LINE;
2810 else
2811 return IN_SEP_LINE | CURSOR_MOVED;
2812 }
2813#endif
2814
2815 curwin->w_cursor.lnum = curwin->w_topline;
2816#ifdef FEAT_GUI
2817 /* remember topline, needed for double click */
2818 gui_prev_topline = curwin->w_topline;
2819# ifdef FEAT_DIFF
2820 gui_prev_topfill = curwin->w_topfill;
2821# endif
2822#endif
2823 }
2824 else if (on_status_line && which_button == MOUSE_LEFT)
2825 {
2826#ifdef FEAT_WINDOWS
2827 if (dragwin != NULL)
2828 {
2829 /* Drag the status line */
2830 count = row - dragwin->w_winrow - dragwin->w_height + 1
2831 - on_status_line;
2832 win_drag_status_line(dragwin, count);
2833 did_drag |= count;
2834 }
2835#endif
2836 return IN_STATUS_LINE; /* Cursor didn't move */
2837 }
2838#ifdef FEAT_VERTSPLIT
2839 else if (on_sep_line && which_button == MOUSE_LEFT)
2840 {
2841 if (dragwin != NULL)
2842 {
2843 /* Drag the separator column */
2844 count = col - dragwin->w_wincol - dragwin->w_width + 1
2845 - on_sep_line;
2846 win_drag_vsep_line(dragwin, count);
2847 did_drag |= count;
2848 }
2849 return IN_SEP_LINE; /* Cursor didn't move */
2850 }
2851#endif
2852 else /* keep_window_focus must be TRUE */
2853 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 /* before moving the cursor for a left click, stop Visual mode */
2855 if (flags & MOUSE_MAY_STOP_VIS)
2856 {
2857 end_visual_mode();
2858 redraw_curbuf_later(INVERTED); /* delete the inversion */
2859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860
2861#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2862 /* Continue a modeless selection in another window. */
2863 if (cmdwin_type != 0 && row < W_WINROW(curwin))
2864 return IN_OTHER_WIN;
2865#endif
2866
2867 row -= W_WINROW(curwin);
2868#ifdef FEAT_VERTSPLIT
2869 col -= W_WINCOL(curwin);
2870#endif
2871
2872 /*
2873 * When clicking beyond the end of the window, scroll the screen.
2874 * Scroll by however many rows outside the window we are.
2875 */
2876 if (row < 0)
2877 {
2878 count = 0;
2879 for (first = TRUE; curwin->w_topline > 1; )
2880 {
2881#ifdef FEAT_DIFF
2882 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
2883 ++count;
2884 else
2885#endif
2886 count += plines(curwin->w_topline - 1);
2887 if (!first && count > -row)
2888 break;
2889 first = FALSE;
2890#ifdef FEAT_FOLDING
2891 hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2892#endif
2893#ifdef FEAT_DIFF
2894 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
2895 ++curwin->w_topfill;
2896 else
2897#endif
2898 {
2899 --curwin->w_topline;
2900#ifdef FEAT_DIFF
2901 curwin->w_topfill = 0;
2902#endif
2903 }
2904 }
2905#ifdef FEAT_DIFF
2906 check_topfill(curwin, FALSE);
2907#endif
2908 curwin->w_valid &=
2909 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2910 redraw_later(VALID);
2911 row = 0;
2912 }
2913 else if (row >= curwin->w_height)
2914 {
2915 count = 0;
2916 for (first = TRUE; curwin->w_topline < curbuf->b_ml.ml_line_count; )
2917 {
2918#ifdef FEAT_DIFF
2919 if (curwin->w_topfill > 0)
2920 ++count;
2921 else
2922#endif
2923 count += plines(curwin->w_topline);
2924 if (!first && count > row - curwin->w_height + 1)
2925 break;
2926 first = FALSE;
2927#ifdef FEAT_FOLDING
2928 if (hasFolding(curwin->w_topline, NULL, &curwin->w_topline)
2929 && curwin->w_topline == curbuf->b_ml.ml_line_count)
2930 break;
2931#endif
2932#ifdef FEAT_DIFF
2933 if (curwin->w_topfill > 0)
2934 --curwin->w_topfill;
2935 else
2936#endif
2937 {
2938 ++curwin->w_topline;
2939#ifdef FEAT_DIFF
2940 curwin->w_topfill =
2941 diff_check_fill(curwin, curwin->w_topline);
2942#endif
2943 }
2944 }
2945#ifdef FEAT_DIFF
2946 check_topfill(curwin, FALSE);
2947#endif
2948 redraw_later(VALID);
2949 curwin->w_valid &=
2950 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2951 row = curwin->w_height - 1;
2952 }
2953 else if (row == 0)
2954 {
2955 /* When dragging the mouse, while the text has been scrolled up as
2956 * far as it goes, moving the mouse in the top line should scroll
2957 * the text down (done later when recomputing w_topline). */
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00002958 if (mouse_dragging > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 && curwin->w_cursor.lnum
2960 == curwin->w_buffer->b_ml.ml_line_count
2961 && curwin->w_cursor.lnum == curwin->w_topline)
2962 curwin->w_valid &= ~(VALID_TOPLINE);
2963 }
2964 }
2965
2966#ifdef FEAT_FOLDING
2967 /* Check for position outside of the fold column. */
2968 if (
2969# ifdef FEAT_RIGHTLEFT
2970 curwin->w_p_rl ? col < W_WIDTH(curwin) - curwin->w_p_fdc :
2971# endif
2972 col >= curwin->w_p_fdc
2973# ifdef FEAT_CMDWIN
2974 + (cmdwin_type == 0 ? 0 : 1)
2975# endif
2976 )
2977 mouse_char = ' ';
2978#endif
2979
2980 /* compute the position in the buffer line from the posn on the screen */
2981 if (mouse_comp_pos(curwin, &row, &col, &curwin->w_cursor.lnum))
2982 mouse_past_bottom = TRUE;
2983
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 /* Start Visual mode before coladvance(), for when 'sel' != "old" */
2985 if ((flags & MOUSE_MAY_VIS) && !VIsual_active)
2986 {
2987 check_visual_highlight();
2988 VIsual = old_cursor;
2989 VIsual_active = TRUE;
2990 VIsual_reselect = TRUE;
2991 /* if 'selectmode' contains "mouse", start Select mode */
2992 may_start_select('o');
2993 setmouse();
Bram Moolenaar7df351e2006-01-23 22:30:28 +00002994 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 redraw_cmdline = TRUE; /* show visual mode later */
2996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997
2998 curwin->w_curswant = col;
2999 curwin->w_set_curswant = FALSE; /* May still have been TRUE */
3000 if (coladvance(col) == FAIL) /* Mouse click beyond end of line */
3001 {
3002 if (inclusive != NULL)
3003 *inclusive = TRUE;
3004 mouse_past_eol = TRUE;
3005 }
3006 else if (inclusive != NULL)
3007 *inclusive = FALSE;
3008
3009 count = IN_BUFFER;
3010 if (curwin != old_curwin || curwin->w_cursor.lnum != old_cursor.lnum
3011 || curwin->w_cursor.col != old_cursor.col)
3012 count |= CURSOR_MOVED; /* Cursor has moved */
3013
3014#ifdef FEAT_FOLDING
3015 if (mouse_char == '+')
3016 count |= MOUSE_FOLD_OPEN;
3017 else if (mouse_char != ' ')
3018 count |= MOUSE_FOLD_CLOSE;
3019#endif
3020
3021 return count;
3022}
3023
3024/*
3025 * Compute the position in the buffer line from the posn on the screen in
3026 * window "win".
3027 * Returns TRUE if the position is below the last line.
3028 */
3029 int
3030mouse_comp_pos(win, rowp, colp, lnump)
3031 win_T *win;
3032 int *rowp;
3033 int *colp;
3034 linenr_T *lnump;
3035{
3036 int col = *colp;
3037 int row = *rowp;
3038 linenr_T lnum;
3039 int retval = FALSE;
3040 int off;
3041 int count;
3042
3043#ifdef FEAT_RIGHTLEFT
3044 if (win->w_p_rl)
3045 col = W_WIDTH(win) - 1 - col;
3046#endif
3047
3048 lnum = win->w_topline;
3049
3050 while (row > 0)
3051 {
3052#ifdef FEAT_DIFF
3053 /* Don't include filler lines in "count" */
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003054 if (win->w_p_diff
3055# ifdef FEAT_FOLDING
3056 && !hasFoldingWin(win, lnum, NULL, NULL, TRUE, NULL)
3057# endif
3058 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 {
3060 if (lnum == win->w_topline)
3061 row -= win->w_topfill;
3062 else
3063 row -= diff_check_fill(win, lnum);
3064 count = plines_win_nofill(win, lnum, TRUE);
3065 }
3066 else
3067#endif
3068 count = plines_win(win, lnum, TRUE);
3069 if (count > row)
3070 break; /* Position is in this buffer line. */
3071#ifdef FEAT_FOLDING
3072 (void)hasFoldingWin(win, lnum, NULL, &lnum, TRUE, NULL);
3073#endif
3074 if (lnum == win->w_buffer->b_ml.ml_line_count)
3075 {
3076 retval = TRUE;
3077 break; /* past end of file */
3078 }
3079 row -= count;
3080 ++lnum;
3081 }
3082
3083 if (!retval)
3084 {
3085 /* Compute the column without wrapping. */
3086 off = win_col_off(win) - win_col_off2(win);
3087 if (col < off)
3088 col = off;
3089 col += row * (W_WIDTH(win) - off);
3090 /* add skip column (for long wrapping line) */
3091 col += win->w_skipcol;
3092 }
3093
3094 if (!win->w_p_wrap)
3095 col += win->w_leftcol;
3096
3097 /* skip line number and fold column in front of the line */
3098 col -= win_col_off(win);
3099 if (col < 0)
3100 {
3101#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003102 netbeans_gutter_click(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103#endif
3104 col = 0;
3105 }
3106
3107 *colp = col;
3108 *rowp = row;
3109 *lnump = lnum;
3110 return retval;
3111}
3112
3113#if defined(FEAT_WINDOWS) || defined(PROTO)
3114/*
3115 * Find the window at screen position "*rowp" and "*colp". The positions are
3116 * updated to become relative to the top-left of the window.
3117 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 win_T *
3119mouse_find_win(rowp, colp)
3120 int *rowp;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003121 int *colp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122{
3123 frame_T *fp;
3124
3125 fp = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003126 *rowp -= firstwin->w_winrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 for (;;)
3128 {
3129 if (fp->fr_layout == FR_LEAF)
3130 break;
3131#ifdef FEAT_VERTSPLIT
3132 if (fp->fr_layout == FR_ROW)
3133 {
3134 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
3135 {
3136 if (*colp < fp->fr_width)
3137 break;
3138 *colp -= fp->fr_width;
3139 }
3140 }
3141#endif
3142 else /* fr_layout == FR_COL */
3143 {
3144 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
3145 {
3146 if (*rowp < fp->fr_height)
3147 break;
3148 *rowp -= fp->fr_height;
3149 }
3150 }
3151 }
3152 return fp->fr_win;
3153}
3154#endif
3155
Bram Moolenaar860cae12010-06-05 23:22:07 +02003156#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
3158 || defined(FEAT_GUI_PHOTON) || defined(PROTO)
3159/*
3160 * Translate window coordinates to buffer position without any side effects
3161 */
3162 int
3163get_fpos_of_mouse(mpos)
3164 pos_T *mpos;
3165{
3166 win_T *wp;
3167 int row = mouse_row;
3168 int col = mouse_col;
3169
3170 if (row < 0 || col < 0) /* check if it makes sense */
3171 return IN_UNKNOWN;
3172
3173#ifdef FEAT_WINDOWS
3174 /* find the window where the row is in */
3175 wp = mouse_find_win(&row, &col);
3176#else
3177 wp = firstwin;
3178#endif
3179 /*
3180 * winpos and height may change in win_enter()!
3181 */
3182 if (row >= wp->w_height) /* In (or below) status line */
3183 return IN_STATUS_LINE;
3184#ifdef FEAT_VERTSPLIT
3185 if (col >= wp->w_width) /* In vertical separator line */
3186 return IN_SEP_LINE;
3187#endif
3188
3189 if (wp != curwin)
3190 return IN_UNKNOWN;
3191
3192 /* compute the position in the buffer line from the posn on the screen */
3193 if (mouse_comp_pos(curwin, &row, &col, &mpos->lnum))
3194 return IN_STATUS_LINE; /* past bottom */
3195
3196 mpos->col = vcol2col(wp, mpos->lnum, col);
3197
3198 if (mpos->col > 0)
3199 --mpos->col;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003200#ifdef FEAT_VIRTUALEDIT
3201 mpos->coladd = 0;
3202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 return IN_BUFFER;
3204}
3205
3206/*
3207 * Convert a virtual (screen) column to a character column.
3208 * The first column is one.
3209 */
3210 int
3211vcol2col(wp, lnum, vcol)
3212 win_T *wp;
3213 linenr_T lnum;
3214 int vcol;
3215{
3216 /* try to advance to the specified column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 int count = 0;
3218 char_u *ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003219 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220
Bram Moolenaar597a4222014-06-25 14:39:50 +02003221 line = ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaarb6101cf2012-10-21 00:58:39 +02003222 while (count < vcol && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003224 count += win_lbr_chartabsize(wp, line, ptr, count, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003225 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02003227 return (int)(ptr - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228}
3229#endif
3230
3231#endif /* FEAT_MOUSE */
3232
3233#if defined(FEAT_GUI) || defined(WIN3264) || defined(PROTO)
3234/*
3235 * Called when focus changed. Used for the GUI or for systems where this can
3236 * be done in the console (Win32).
3237 */
3238 void
3239ui_focus_change(in_focus)
3240 int in_focus; /* TRUE if focus gained. */
3241{
3242 static time_t last_time = (time_t)0;
3243 int need_redraw = FALSE;
3244
3245 /* When activated: Check if any file was modified outside of Vim.
3246 * Only do this when not done within the last two seconds (could get
3247 * several events in a row). */
3248 if (in_focus && last_time + 2 < time(NULL))
3249 {
3250 need_redraw = check_timestamps(
3251# ifdef FEAT_GUI
3252 gui.in_use
3253# else
3254 FALSE
3255# endif
3256 );
3257 last_time = time(NULL);
3258 }
3259
3260#ifdef FEAT_AUTOCMD
3261 /*
3262 * Fire the focus gained/lost autocommand.
3263 */
3264 need_redraw |= apply_autocmds(in_focus ? EVENT_FOCUSGAINED
3265 : EVENT_FOCUSLOST, NULL, NULL, FALSE, curbuf);
3266#endif
3267
3268 if (need_redraw)
3269 {
3270 /* Something was executed, make sure the cursor is put back where it
3271 * belongs. */
3272 need_wait_return = FALSE;
3273
3274 if (State & CMDLINE)
3275 redrawcmdline();
3276 else if (State == HITRETURN || State == SETWSIZE || State == ASKMORE
3277 || State == EXTERNCMD || State == CONFIRM || exmode_active)
3278 repeat_message();
3279 else if ((State & NORMAL) || (State & INSERT))
3280 {
3281 if (must_redraw != 0)
3282 update_screen(0);
3283 setcursor();
3284 }
3285 cursor_on(); /* redrawing may have switched it off */
3286 out_flush();
3287# ifdef FEAT_GUI
3288 if (gui.in_use)
3289 {
3290 gui_update_cursor(FALSE, TRUE);
3291 gui_update_scrollbars(FALSE);
3292 }
3293# endif
3294 }
3295#ifdef FEAT_TITLE
3296 /* File may have been changed from 'readonly' to 'noreadonly' */
3297 if (need_maketitle)
3298 maketitle();
3299#endif
3300}
3301#endif
3302
3303#if defined(USE_IM_CONTROL) || defined(PROTO)
3304/*
3305 * Save current Input Method status to specified place.
3306 */
3307 void
3308im_save_status(psave)
3309 long *psave;
3310{
3311 /* Don't save when 'imdisable' is set or "xic" is NULL, IM is always
3312 * disabled then (but might start later).
3313 * Also don't save when inside a mapping, vgetc_im_active has not been set
3314 * then.
3315 * And don't save when the keys were stuffed (e.g., for a "." command).
3316 * And don't save when the GUI is running but our window doesn't have
3317 * input focus (e.g., when a find dialog is open). */
3318 if (!p_imdisable && KeyTyped && !KeyStuffed
3319# ifdef FEAT_XIM
3320 && xic != NULL
3321# endif
3322# ifdef FEAT_GUI
3323 && (!gui.in_use || gui.in_focus)
3324# endif
3325 )
3326 {
3327 /* Do save when IM is on, or IM is off and saved status is on. */
3328 if (vgetc_im_active)
3329 *psave = B_IMODE_IM;
3330 else if (*psave == B_IMODE_IM)
3331 *psave = B_IMODE_NONE;
3332 }
3333}
3334#endif