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