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