blob: 3fe99c5e47da89e81fe00922a11b2d0675ccd868 [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
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001683/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 void
1685fill_input_buf(exit_on_error)
1686 int exit_on_error;
1687{
1688#if defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X_UNIX)
1689 int len;
1690 int try;
1691 static int did_read_something = FALSE;
1692# ifdef FEAT_MBYTE
1693 static char_u *rest = NULL; /* unconverted rest of previous read */
1694 static int restlen = 0;
1695 int unconverted;
1696# endif
1697#endif
1698
1699#ifdef FEAT_GUI
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001700 if (gui.in_use
1701# ifdef NO_CONSOLE_INPUT
1702 /* Don't use the GUI input when the window hasn't been opened yet.
1703 * We get here from ui_inchar() when we should try reading from stdin. */
1704 && !no_console_input()
1705# endif
1706 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 {
1708 gui_mch_update();
1709 return;
1710 }
1711#endif
1712#if defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X_UNIX)
1713 if (vim_is_input_buf_full())
1714 return;
1715 /*
1716 * Fill_input_buf() is only called when we really need a character.
1717 * If we can't get any, but there is some in the buffer, just return.
1718 * If we can't get any, and there isn't any in the buffer, we give up and
1719 * exit Vim.
1720 */
1721# ifdef __BEOS__
1722 /*
1723 * On the BeBox version (for now), all input is secretly performed within
1724 * beos_select() which is called from RealWaitForChar().
1725 */
1726 while (!vim_is_input_buf_full() && RealWaitForChar(read_cmd_fd, 0, NULL))
1727 ;
1728 len = inbufcount;
1729 inbufcount = 0;
1730# else
1731
1732# ifdef FEAT_SNIFF
1733 if (sniff_request_waiting)
1734 {
1735 add_to_input_buf((char_u *)"\233sniff",6); /* results in K_SNIFF */
1736 sniff_request_waiting = 0;
1737 want_sniff_request = 0;
1738 return;
1739 }
1740# endif
1741
1742# ifdef FEAT_MBYTE
1743 if (rest != NULL)
1744 {
1745 /* Use remainder of previous call, starts with an invalid character
1746 * that may become valid when reading more. */
1747 if (restlen > INBUFLEN - inbufcount)
1748 unconverted = INBUFLEN - inbufcount;
1749 else
1750 unconverted = restlen;
1751 mch_memmove(inbuf + inbufcount, rest, unconverted);
1752 if (unconverted == restlen)
1753 {
1754 vim_free(rest);
1755 rest = NULL;
1756 }
1757 else
1758 {
1759 restlen -= unconverted;
1760 mch_memmove(rest, rest + unconverted, restlen);
1761 }
1762 inbufcount += unconverted;
1763 }
1764 else
1765 unconverted = 0;
1766#endif
1767
1768 len = 0; /* to avoid gcc warning */
1769 for (try = 0; try < 100; ++try)
1770 {
1771# ifdef VMS
1772 len = vms_read(
1773# else
1774 len = read(read_cmd_fd,
1775# endif
1776 (char *)inbuf + inbufcount, (size_t)((INBUFLEN - inbufcount)
1777# ifdef FEAT_MBYTE
1778 / input_conv.vc_factor
1779# endif
1780 ));
1781# if 0
1782 ) /* avoid syntax highlight error */
1783# endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001784
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 if (len > 0 || got_int)
1786 break;
1787 /*
1788 * If reading stdin results in an error, continue reading stderr.
1789 * This helps when using "foo | xargs vim".
1790 */
1791 if (!did_read_something && !isatty(read_cmd_fd) && read_cmd_fd == 0)
1792 {
1793 int m = cur_tmode;
1794
1795 /* We probably set the wrong file descriptor to raw mode. Switch
1796 * back to cooked mode, use another descriptor and set the mode to
1797 * what it was. */
1798 settmode(TMODE_COOK);
1799#ifdef HAVE_DUP
1800 /* Use stderr for stdin, also works for shell commands. */
1801 close(0);
1802 dup(2);
1803#else
1804 read_cmd_fd = 2; /* read from stderr instead of stdin */
1805#endif
1806 settmode(m);
1807 }
1808 if (!exit_on_error)
1809 return;
1810 }
1811# endif
1812 if (len <= 0 && !got_int)
1813 read_error_exit();
1814 if (len > 0)
1815 did_read_something = TRUE;
1816 if (got_int)
1817 {
1818 /* Interrupted, pretend a CTRL-C was typed. */
1819 inbuf[0] = 3;
1820 inbufcount = 1;
1821 }
1822 else
1823 {
1824# ifdef FEAT_MBYTE
1825 /*
1826 * May perform conversion on the input characters.
1827 * Include the unconverted rest of the previous call.
1828 * If there is an incomplete char at the end it is kept for the next
1829 * time, reading more bytes should make conversion possible.
1830 * Don't do this in the unlikely event that the input buffer is too
1831 * small ("rest" still contains more bytes).
1832 */
1833 if (input_conv.vc_type != CONV_NONE)
1834 {
1835 inbufcount -= unconverted;
1836 len = convert_input_safe(inbuf + inbufcount,
1837 len + unconverted, INBUFLEN - inbufcount,
1838 rest == NULL ? &rest : NULL, &restlen);
1839 }
1840# endif
1841 while (len-- > 0)
1842 {
1843 /*
1844 * if a CTRL-C was typed, remove it from the buffer and set got_int
1845 */
1846 if (inbuf[inbufcount] == 3 && ctrl_c_interrupts)
1847 {
1848 /* remove everything typed before the CTRL-C */
1849 mch_memmove(inbuf, inbuf + inbufcount, (size_t)(len + 1));
1850 inbufcount = 0;
1851 got_int = TRUE;
1852 }
1853 ++inbufcount;
1854 }
1855 }
1856#endif /* UNIX or OS2 or VMS*/
1857}
1858#endif /* defined(UNIX) || defined(FEAT_GUI) || defined(OS2) || defined(VMS) */
1859
1860/*
1861 * Exit because of an input read error.
1862 */
1863 void
1864read_error_exit()
1865{
1866 if (silent_mode) /* Normal way to exit for "ex -s" */
1867 getout(0);
1868 STRCPY(IObuff, _("Vim: Error reading input, exiting...\n"));
1869 preserve_exit();
1870}
1871
1872#if defined(CURSOR_SHAPE) || defined(PROTO)
1873/*
1874 * May update the shape of the cursor.
1875 */
1876 void
1877ui_cursor_shape()
1878{
1879# ifdef FEAT_GUI
1880 if (gui.in_use)
1881 gui_update_cursor_later();
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001882 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001884 term_cursor_shape();
1885
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886# ifdef MCH_CURSOR_SHAPE
1887 mch_update_cursor();
1888# endif
1889}
1890#endif
1891
1892#if defined(FEAT_CLIPBOARD) || defined(FEAT_GUI) || defined(FEAT_RIGHTLEFT) \
1893 || defined(PROTO)
1894/*
1895 * Check bounds for column number
1896 */
1897 int
1898check_col(col)
1899 int col;
1900{
1901 if (col < 0)
1902 return 0;
1903 if (col >= (int)screen_Columns)
1904 return (int)screen_Columns - 1;
1905 return col;
1906}
1907
1908/*
1909 * Check bounds for row number
1910 */
1911 int
1912check_row(row)
1913 int row;
1914{
1915 if (row < 0)
1916 return 0;
1917 if (row >= (int)screen_Rows)
1918 return (int)screen_Rows - 1;
1919 return row;
1920}
1921#endif
1922
1923/*
1924 * Stuff for the X clipboard. Shared between VMS and Unix.
1925 */
1926
1927#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) || defined(PROTO)
1928# include <X11/Xatom.h>
1929# include <X11/Intrinsic.h>
1930
1931/*
1932 * Open the application context (if it hasn't been opened yet).
1933 * Used for Motif and Athena GUI and the xterm clipboard.
1934 */
1935 void
1936open_app_context()
1937{
1938 if (app_context == NULL)
1939 {
1940 XtToolkitInitialize();
1941 app_context = XtCreateApplicationContext();
1942 }
1943}
1944
1945static Atom vim_atom; /* Vim's own special selection format */
1946#ifdef FEAT_MBYTE
1947static Atom vimenc_atom; /* Vim's extended selection format */
1948#endif
1949static Atom compound_text_atom;
1950static Atom text_atom;
1951static Atom targets_atom;
1952
1953 void
1954x11_setup_atoms(dpy)
1955 Display *dpy;
1956{
1957 vim_atom = XInternAtom(dpy, VIM_ATOM_NAME, False);
1958#ifdef FEAT_MBYTE
1959 vimenc_atom = XInternAtom(dpy, VIMENC_ATOM_NAME,False);
1960#endif
1961 compound_text_atom = XInternAtom(dpy, "COMPOUND_TEXT", False);
1962 text_atom = XInternAtom(dpy, "TEXT", False);
1963 targets_atom = XInternAtom(dpy, "TARGETS", False);
1964 clip_star.sel_atom = XA_PRIMARY;
1965 clip_plus.sel_atom = XInternAtom(dpy, "CLIPBOARD", False);
1966}
1967
1968/*
1969 * X Selection stuff, for cutting and pasting text to other windows.
1970 */
1971
1972static void clip_x11_request_selection_cb __ARGS((Widget, XtPointer, Atom *, Atom *, XtPointer, long_u *, int *));
1973
1974/* ARGSUSED */
1975 static void
1976clip_x11_request_selection_cb(w, success, sel_atom, type, value, length,
1977 format)
1978 Widget w;
1979 XtPointer success;
1980 Atom *sel_atom;
1981 Atom *type;
1982 XtPointer value;
1983 long_u *length;
1984 int *format;
1985{
1986 int motion_type;
1987 long_u len;
1988 char_u *p;
1989 char **text_list = NULL;
1990 VimClipboard *cbd;
1991#ifdef FEAT_MBYTE
1992 char_u *tmpbuf = NULL;
1993#endif
1994
1995 if (*sel_atom == clip_plus.sel_atom)
1996 cbd = &clip_plus;
1997 else
1998 cbd = &clip_star;
1999
2000 if (value == NULL || *length == 0)
2001 {
2002 clip_free_selection(cbd); /* ??? [what's the query?] */
2003 *(int *)success = FALSE;
2004 return;
2005 }
2006 motion_type = MCHAR;
2007 p = (char_u *)value;
2008 len = *length;
2009 if (*type == vim_atom)
2010 {
2011 motion_type = *p++;
2012 len--;
2013 }
2014
2015#ifdef FEAT_MBYTE
2016 else if (*type == vimenc_atom)
2017 {
2018 char_u *enc;
2019 vimconv_T conv;
2020 int convlen;
2021
2022 motion_type = *p++;
2023 --len;
2024
2025 enc = p;
2026 p += STRLEN(p) + 1;
2027 len -= p - enc;
2028
2029 /* If the encoding of the text is different from 'encoding', attempt
2030 * converting it. */
2031 conv.vc_type = CONV_NONE;
2032 convert_setup(&conv, enc, p_enc);
2033 if (conv.vc_type != CONV_NONE)
2034 {
2035 convlen = len; /* Need to use an int here. */
2036 tmpbuf = string_convert(&conv, p, &convlen);
2037 len = convlen;
2038 if (tmpbuf != NULL)
2039 p = tmpbuf;
2040 convert_setup(&conv, NULL, NULL);
2041 }
2042 }
2043#endif
2044
2045 else if (*type == compound_text_atom || (
2046#ifdef FEAT_MBYTE
2047 enc_dbcs != 0 &&
2048#endif
2049 *type == text_atom))
2050 {
2051 XTextProperty text_prop;
2052 int n_text = 0;
2053 int status;
2054
2055 text_prop.value = (unsigned char *)value;
2056 text_prop.encoding = *type;
2057 text_prop.format = *format;
2058 text_prop.nitems = STRLEN(value);
2059 status = XmbTextPropertyToTextList(X_DISPLAY, &text_prop,
2060 &text_list, &n_text);
2061 if (status != Success || n_text < 1)
2062 {
2063 *(int *)success = FALSE;
2064 return;
2065 }
2066 p = (char_u *)text_list[0];
2067 len = STRLEN(p);
2068 }
2069 clip_yank_selection(motion_type, p, (long)len, cbd);
2070
2071 if (text_list != NULL)
2072 XFreeStringList(text_list);
2073#ifdef FEAT_MBYTE
2074 vim_free(tmpbuf);
2075#endif
2076 XtFree((char *)value);
2077 *(int *)success = TRUE;
2078}
2079
2080 void
2081clip_x11_request_selection(myShell, dpy, cbd)
2082 Widget myShell;
2083 Display *dpy;
2084 VimClipboard *cbd;
2085{
2086 XEvent event;
2087 Atom type;
2088 static int success;
2089 int i;
2090 int nbytes = 0;
2091 char_u *buffer;
2092
2093 for (i =
2094#ifdef FEAT_MBYTE
2095 0
2096#else
2097 1
2098#endif
2099 ; i < 5; i++)
2100 {
2101 switch (i)
2102 {
2103#ifdef FEAT_MBYTE
2104 case 0: type = vimenc_atom; break;
2105#endif
2106 case 1: type = vim_atom; break;
2107 case 2: type = compound_text_atom; break;
2108 case 3: type = text_atom; break;
2109 default: type = XA_STRING;
2110 }
2111 XtGetSelectionValue(myShell, cbd->sel_atom, type,
2112 clip_x11_request_selection_cb, (XtPointer)&success, CurrentTime);
2113
2114 /* Make sure the request for the selection goes out before waiting for
2115 * a response. */
2116 XFlush(dpy);
2117
2118 /*
2119 * Wait for result of selection request, otherwise if we type more
2120 * characters, then they will appear before the one that requested the
2121 * paste! Don't worry, we will catch up with any other events later.
2122 */
2123 for (;;)
2124 {
2125 if (XCheckTypedEvent(dpy, SelectionNotify, &event))
2126 break;
2127
2128 /* Do we need this? Probably not. */
2129 XSync(dpy, False);
2130
2131 /* Bernhard Walle solved a slow paste response in an X terminal by
2132 * adding: usleep(10000); here. */
2133 }
2134
2135 /* this is where clip_x11_request_selection_cb() is actually called */
2136 XtDispatchEvent(&event);
2137
2138 if (success)
2139 return;
2140 }
2141
2142 /* Final fallback position - use the X CUT_BUFFER0 store */
2143 buffer = (char_u *)XFetchBuffer(dpy, &nbytes, 0);
2144 if (nbytes > 0)
2145 {
2146 /* Got something */
2147 clip_yank_selection(MCHAR, buffer, (long)nbytes, cbd);
2148 XFree((void *)buffer);
2149 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002150 verb_msg((char_u *)_("Used CUT_BUFFER0 instead of empty selection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 }
2152}
2153
2154static Boolean clip_x11_convert_selection_cb __ARGS((Widget, Atom *, Atom *, Atom *, XtPointer *, long_u *, int *));
2155
2156/* ARGSUSED */
2157 static Boolean
2158clip_x11_convert_selection_cb(w, sel_atom, target, type, value, length, format)
2159 Widget w;
2160 Atom *sel_atom;
2161 Atom *target;
2162 Atom *type;
2163 XtPointer *value;
2164 long_u *length;
2165 int *format;
2166{
2167 char_u *string;
2168 char_u *result;
2169 int motion_type;
2170 VimClipboard *cbd;
2171 int i;
2172
2173 if (*sel_atom == clip_plus.sel_atom)
2174 cbd = &clip_plus;
2175 else
2176 cbd = &clip_star;
2177
2178 if (!cbd->owned)
2179 return False; /* Shouldn't ever happen */
2180
2181 /* requestor wants to know what target types we support */
2182 if (*target == targets_atom)
2183 {
2184 Atom *array;
2185
2186 if ((array = (Atom *)XtMalloc((unsigned)(sizeof(Atom) * 6))) == NULL)
2187 return False;
2188 *value = (XtPointer)array;
2189 i = 0;
2190 array[i++] = XA_STRING;
2191 array[i++] = targets_atom;
2192#ifdef FEAT_MBYTE
2193 array[i++] = vimenc_atom;
2194#endif
2195 array[i++] = vim_atom;
2196 array[i++] = text_atom;
2197 array[i++] = compound_text_atom;
2198 *type = XA_ATOM;
2199 /* This used to be: *format = sizeof(Atom) * 8; but that caused
2200 * crashes on 64 bit machines. (Peter Derr) */
2201 *format = 32;
2202 *length = i;
2203 return True;
2204 }
2205
2206 if ( *target != XA_STRING
2207#ifdef FEAT_MBYTE
2208 && *target != vimenc_atom
2209#endif
2210 && *target != vim_atom
2211 && *target != text_atom
2212 && *target != compound_text_atom)
2213 return False;
2214
2215 clip_get_selection(cbd);
2216 motion_type = clip_convert_selection(&string, length, cbd);
2217 if (motion_type < 0)
2218 return False;
2219
2220 /* For our own format, the first byte contains the motion type */
2221 if (*target == vim_atom)
2222 (*length)++;
2223
2224#ifdef FEAT_MBYTE
2225 /* Our own format with encoding: motion 'encoding' NUL text */
2226 if (*target == vimenc_atom)
2227 *length += STRLEN(p_enc) + 2;
2228#endif
2229
2230 *value = XtMalloc((Cardinal)*length);
2231 result = (char_u *)*value;
2232 if (result == NULL)
2233 {
2234 vim_free(string);
2235 return False;
2236 }
2237
2238 if (*target == XA_STRING)
2239 {
2240 mch_memmove(result, string, (size_t)(*length));
2241 *type = XA_STRING;
2242 }
2243 else if (*target == compound_text_atom
2244 || *target == text_atom)
2245 {
2246 XTextProperty text_prop;
2247 char *string_nt = (char *)alloc((unsigned)*length + 1);
2248
2249 /* create NUL terminated string which XmbTextListToTextProperty wants */
2250 mch_memmove(string_nt, string, (size_t)*length);
2251 string_nt[*length] = NUL;
2252 XmbTextListToTextProperty(X_DISPLAY, (char **)&string_nt, 1,
2253 XCompoundTextStyle, &text_prop);
2254 vim_free(string_nt);
2255 XtFree(*value); /* replace with COMPOUND text */
2256 *value = (XtPointer)(text_prop.value); /* from plain text */
2257 *length = text_prop.nitems;
2258 *type = compound_text_atom;
2259 }
2260
2261#ifdef FEAT_MBYTE
2262 else if (*target == vimenc_atom)
2263 {
2264 int l = STRLEN(p_enc);
2265
2266 result[0] = motion_type;
2267 STRCPY(result + 1, p_enc);
2268 mch_memmove(result + l + 2, string, (size_t)(*length - l - 2));
2269 *type = vimenc_atom;
2270 }
2271#endif
2272
2273 else
2274 {
2275 result[0] = motion_type;
2276 mch_memmove(result + 1, string, (size_t)(*length - 1));
2277 *type = vim_atom;
2278 }
2279 *format = 8; /* 8 bits per char */
2280 vim_free(string);
2281 return True;
2282}
2283
2284static void clip_x11_lose_ownership_cb __ARGS((Widget, Atom *));
2285
2286/* ARGSUSED */
2287 static void
2288clip_x11_lose_ownership_cb(w, sel_atom)
2289 Widget w;
2290 Atom *sel_atom;
2291{
2292 if (*sel_atom == clip_plus.sel_atom)
2293 clip_lose_selection(&clip_plus);
2294 else
2295 clip_lose_selection(&clip_star);
2296}
2297
2298 void
2299clip_x11_lose_selection(myShell, cbd)
2300 Widget myShell;
2301 VimClipboard *cbd;
2302{
2303 XtDisownSelection(myShell, cbd->sel_atom, CurrentTime);
2304}
2305
2306 int
2307clip_x11_own_selection(myShell, cbd)
2308 Widget myShell;
2309 VimClipboard *cbd;
2310{
2311 if (XtOwnSelection(myShell, cbd->sel_atom, CurrentTime,
2312 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
2313 NULL) == False)
2314 return FAIL;
2315 return OK;
2316}
2317
2318/*
2319 * Send the current selection to the clipboard. Do nothing for X because we
2320 * will fill in the selection only when requested by another app.
2321 */
2322/*ARGSUSED*/
2323 void
2324clip_x11_set_selection(cbd)
2325 VimClipboard *cbd;
2326{
2327}
2328#endif
2329
2330#if defined(FEAT_MOUSE) || defined(PROTO)
2331
2332/*
2333 * Move the cursor to the specified row and column on the screen.
2334 * Change current window if neccesary. Returns an integer with the
2335 * CURSOR_MOVED bit set if the cursor has moved or unset otherwise.
2336 *
2337 * The MOUSE_FOLD_CLOSE bit is set when clicked on the '-' in a fold column.
2338 * The MOUSE_FOLD_OPEN bit is set when clicked on the '+' in a fold column.
2339 *
2340 * If flags has MOUSE_FOCUS, then the current window will not be changed, and
2341 * if the mouse is outside the window then the text will scroll, or if the
2342 * mouse was previously on a status line, then the status line may be dragged.
2343 *
2344 * If flags has MOUSE_MAY_VIS, then VIsual mode will be started before the
2345 * cursor is moved unless the cursor was on a status line.
2346 * This function returns one of IN_UNKNOWN, IN_BUFFER, IN_STATUS_LINE or
2347 * IN_SEP_LINE depending on where the cursor was clicked.
2348 *
2349 * If flags has MOUSE_MAY_STOP_VIS, then Visual mode will be stopped, unless
2350 * the mouse is on the status line of the same window.
2351 *
2352 * If flags has MOUSE_DID_MOVE, nothing is done if the mouse didn't move since
2353 * the last call.
2354 *
2355 * If flags has MOUSE_SETPOS, nothing is done, only the current position is
2356 * remembered.
2357 */
2358 int
2359jump_to_mouse(flags, inclusive, which_button)
2360 int flags;
2361 int *inclusive; /* used for inclusive operator, can be NULL */
2362 int which_button; /* MOUSE_LEFT, MOUSE_RIGHT, MOUSE_MIDDLE */
2363{
2364 static int on_status_line = 0; /* #lines below bottom of window */
2365#ifdef FEAT_VERTSPLIT
2366 static int on_sep_line = 0; /* on separator right of window */
2367#endif
2368 static int prev_row = -1;
2369 static int prev_col = -1;
2370 static win_T *dragwin = NULL; /* window being dragged */
2371 static int did_drag = FALSE; /* drag was noticed */
2372
2373 win_T *wp, *old_curwin;
2374 pos_T old_cursor;
2375 int count;
2376 int first;
2377 int row = mouse_row;
2378 int col = mouse_col;
2379#ifdef FEAT_FOLDING
2380 int mouse_char;
2381#endif
2382
2383 mouse_past_bottom = FALSE;
2384 mouse_past_eol = FALSE;
2385
2386 if (flags & MOUSE_RELEASED)
2387 {
2388 /* On button release we may change window focus if positioned on a
2389 * status line and no dragging happened. */
2390 if (dragwin != NULL && !did_drag)
2391 flags &= ~(MOUSE_FOCUS | MOUSE_DID_MOVE);
2392 dragwin = NULL;
2393 did_drag = FALSE;
2394 }
2395
2396 if ((flags & MOUSE_DID_MOVE)
2397 && prev_row == mouse_row
2398 && prev_col == mouse_col)
2399 {
2400retnomove:
2401 /* before moving the cursor for a left click wich is NOT in a status
2402 * line, stop Visual mode */
2403 if (on_status_line)
2404 return IN_STATUS_LINE;
2405#ifdef FEAT_VERTSPLIT
2406 if (on_sep_line)
2407 return IN_SEP_LINE;
2408#endif
2409#ifdef FEAT_VISUAL
2410 if (flags & MOUSE_MAY_STOP_VIS)
2411 {
2412 end_visual_mode();
2413 redraw_curbuf_later(INVERTED); /* delete the inversion */
2414 }
2415#endif
2416#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2417 /* Continue a modeless selection in another window. */
2418 if (cmdwin_type != 0 && row < W_WINROW(curwin))
2419 return IN_OTHER_WIN;
2420#endif
2421 return IN_BUFFER;
2422 }
2423
2424 prev_row = mouse_row;
2425 prev_col = mouse_col;
2426
2427 if (flags & MOUSE_SETPOS)
2428 goto retnomove; /* ugly goto... */
2429
2430#ifdef FEAT_FOLDING
2431 /* Remember the character under the mouse, it might be a '-' or '+' in the
2432 * fold column. */
2433 if (row >= 0 && row < Rows && col >= 0 && col <= Columns)
2434 mouse_char = ScreenLines[LineOffset[row] + col];
2435 else
2436 mouse_char = ' ';
2437#endif
2438
2439 old_curwin = curwin;
2440 old_cursor = curwin->w_cursor;
2441
2442 if (!(flags & MOUSE_FOCUS))
2443 {
2444 if (row < 0 || col < 0) /* check if it makes sense */
2445 return IN_UNKNOWN;
2446
2447#ifdef FEAT_WINDOWS
2448 /* find the window where the row is in */
2449 wp = mouse_find_win(&row, &col);
2450#else
2451 wp = firstwin;
2452#endif
2453 dragwin = NULL;
2454 /*
2455 * winpos and height may change in win_enter()!
2456 */
2457 if (row >= wp->w_height) /* In (or below) status line */
2458 {
2459 on_status_line = row - wp->w_height + 1;
2460 dragwin = wp;
2461 }
2462 else
2463 on_status_line = 0;
2464#ifdef FEAT_VERTSPLIT
2465 if (col >= wp->w_width) /* In separator line */
2466 {
2467 on_sep_line = col - wp->w_width + 1;
2468 dragwin = wp;
2469 }
2470 else
2471 on_sep_line = 0;
2472
2473 /* The rightmost character of the status line might be a vertical
2474 * separator character if there is no connecting window to the right. */
2475 if (on_status_line && on_sep_line)
2476 {
2477 if (stl_connected(wp))
2478 on_sep_line = 0;
2479 else
2480 on_status_line = 0;
2481 }
2482#endif
2483
2484#ifdef FEAT_VISUAL
2485 /* Before jumping to another buffer, or moving the cursor for a left
2486 * click, stop Visual mode. */
2487 if (VIsual_active
2488 && (wp->w_buffer != curwin->w_buffer
2489 || (!on_status_line
2490# ifdef FEAT_VERTSPLIT
2491 && !on_sep_line
2492# endif
2493# ifdef FEAT_FOLDING
2494 && (
2495# ifdef FEAT_RIGHTLEFT
2496 wp->w_p_rl ? col < W_WIDTH(wp) - wp->w_p_fdc :
2497# endif
2498 col >= wp->w_p_fdc
2499# ifdef FEAT_CMDWIN
2500 + (cmdwin_type == 0 && wp == curwin ? 0 : 1)
2501# endif
2502 )
2503# endif
2504 && (flags & MOUSE_MAY_STOP_VIS))))
2505 {
2506 end_visual_mode();
2507 redraw_curbuf_later(INVERTED); /* delete the inversion */
2508 }
2509#endif
2510#ifdef FEAT_CMDWIN
2511 if (cmdwin_type != 0 && wp != curwin)
2512 {
2513 /* A click outside the command-line window: Use modeless
2514 * selection if possible. Allow dragging the status line of
2515 * windows just above the command-line window. */
2516 if (wp->w_winrow + wp->w_height
2517 != curwin->w_prev->w_winrow + curwin->w_prev->w_height)
2518 {
2519 on_status_line = 0;
2520 dragwin = NULL;
2521 }
2522# ifdef FEAT_VERTSPLIT
2523 on_sep_line = 0;
2524# endif
2525# ifdef FEAT_CLIPBOARD
2526 if (on_status_line)
2527 return IN_STATUS_LINE;
2528 return IN_OTHER_WIN;
2529# else
2530 row = 0;
2531 col += wp->w_wincol;
2532 wp = curwin;
2533# endif
2534 }
2535#endif
2536#ifdef FEAT_WINDOWS
2537 /* Only change window focus when not clicking on or dragging the
2538 * status line. Do change focus when releasing the mouse button
2539 * (MOUSE_FOCUS was set above if we dragged first). */
2540 if (dragwin == NULL || (flags & MOUSE_RELEASED))
2541 win_enter(wp, TRUE); /* can make wp invalid! */
2542# ifdef CHECK_DOUBLE_CLICK
2543 /* set topline, to be able to check for double click ourselves */
2544 if (curwin != old_curwin)
2545 set_mouse_topline(curwin);
2546# endif
2547#endif
2548 if (on_status_line) /* In (or below) status line */
2549 {
2550 /* Don't use start_arrow() if we're in the same window */
2551 if (curwin == old_curwin)
2552 return IN_STATUS_LINE;
2553 else
2554 return IN_STATUS_LINE | CURSOR_MOVED;
2555 }
2556#ifdef FEAT_VERTSPLIT
2557 if (on_sep_line) /* In (or below) status line */
2558 {
2559 /* Don't use start_arrow() if we're in the same window */
2560 if (curwin == old_curwin)
2561 return IN_SEP_LINE;
2562 else
2563 return IN_SEP_LINE | CURSOR_MOVED;
2564 }
2565#endif
2566
2567 curwin->w_cursor.lnum = curwin->w_topline;
2568#ifdef FEAT_GUI
2569 /* remember topline, needed for double click */
2570 gui_prev_topline = curwin->w_topline;
2571# ifdef FEAT_DIFF
2572 gui_prev_topfill = curwin->w_topfill;
2573# endif
2574#endif
2575 }
2576 else if (on_status_line && which_button == MOUSE_LEFT)
2577 {
2578#ifdef FEAT_WINDOWS
2579 if (dragwin != NULL)
2580 {
2581 /* Drag the status line */
2582 count = row - dragwin->w_winrow - dragwin->w_height + 1
2583 - on_status_line;
2584 win_drag_status_line(dragwin, count);
2585 did_drag |= count;
2586 }
2587#endif
2588 return IN_STATUS_LINE; /* Cursor didn't move */
2589 }
2590#ifdef FEAT_VERTSPLIT
2591 else if (on_sep_line && which_button == MOUSE_LEFT)
2592 {
2593 if (dragwin != NULL)
2594 {
2595 /* Drag the separator column */
2596 count = col - dragwin->w_wincol - dragwin->w_width + 1
2597 - on_sep_line;
2598 win_drag_vsep_line(dragwin, count);
2599 did_drag |= count;
2600 }
2601 return IN_SEP_LINE; /* Cursor didn't move */
2602 }
2603#endif
2604 else /* keep_window_focus must be TRUE */
2605 {
2606#ifdef FEAT_VISUAL
2607 /* before moving the cursor for a left click, stop Visual mode */
2608 if (flags & MOUSE_MAY_STOP_VIS)
2609 {
2610 end_visual_mode();
2611 redraw_curbuf_later(INVERTED); /* delete the inversion */
2612 }
2613#endif
2614
2615#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2616 /* Continue a modeless selection in another window. */
2617 if (cmdwin_type != 0 && row < W_WINROW(curwin))
2618 return IN_OTHER_WIN;
2619#endif
2620
2621 row -= W_WINROW(curwin);
2622#ifdef FEAT_VERTSPLIT
2623 col -= W_WINCOL(curwin);
2624#endif
2625
2626 /*
2627 * When clicking beyond the end of the window, scroll the screen.
2628 * Scroll by however many rows outside the window we are.
2629 */
2630 if (row < 0)
2631 {
2632 count = 0;
2633 for (first = TRUE; curwin->w_topline > 1; )
2634 {
2635#ifdef FEAT_DIFF
2636 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
2637 ++count;
2638 else
2639#endif
2640 count += plines(curwin->w_topline - 1);
2641 if (!first && count > -row)
2642 break;
2643 first = FALSE;
2644#ifdef FEAT_FOLDING
2645 hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2646#endif
2647#ifdef FEAT_DIFF
2648 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
2649 ++curwin->w_topfill;
2650 else
2651#endif
2652 {
2653 --curwin->w_topline;
2654#ifdef FEAT_DIFF
2655 curwin->w_topfill = 0;
2656#endif
2657 }
2658 }
2659#ifdef FEAT_DIFF
2660 check_topfill(curwin, FALSE);
2661#endif
2662 curwin->w_valid &=
2663 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2664 redraw_later(VALID);
2665 row = 0;
2666 }
2667 else if (row >= curwin->w_height)
2668 {
2669 count = 0;
2670 for (first = TRUE; curwin->w_topline < curbuf->b_ml.ml_line_count; )
2671 {
2672#ifdef FEAT_DIFF
2673 if (curwin->w_topfill > 0)
2674 ++count;
2675 else
2676#endif
2677 count += plines(curwin->w_topline);
2678 if (!first && count > row - curwin->w_height + 1)
2679 break;
2680 first = FALSE;
2681#ifdef FEAT_FOLDING
2682 if (hasFolding(curwin->w_topline, NULL, &curwin->w_topline)
2683 && curwin->w_topline == curbuf->b_ml.ml_line_count)
2684 break;
2685#endif
2686#ifdef FEAT_DIFF
2687 if (curwin->w_topfill > 0)
2688 --curwin->w_topfill;
2689 else
2690#endif
2691 {
2692 ++curwin->w_topline;
2693#ifdef FEAT_DIFF
2694 curwin->w_topfill =
2695 diff_check_fill(curwin, curwin->w_topline);
2696#endif
2697 }
2698 }
2699#ifdef FEAT_DIFF
2700 check_topfill(curwin, FALSE);
2701#endif
2702 redraw_later(VALID);
2703 curwin->w_valid &=
2704 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2705 row = curwin->w_height - 1;
2706 }
2707 else if (row == 0)
2708 {
2709 /* When dragging the mouse, while the text has been scrolled up as
2710 * far as it goes, moving the mouse in the top line should scroll
2711 * the text down (done later when recomputing w_topline). */
2712 if (mouse_dragging
2713 && curwin->w_cursor.lnum
2714 == curwin->w_buffer->b_ml.ml_line_count
2715 && curwin->w_cursor.lnum == curwin->w_topline)
2716 curwin->w_valid &= ~(VALID_TOPLINE);
2717 }
2718 }
2719
2720#ifdef FEAT_FOLDING
2721 /* Check for position outside of the fold column. */
2722 if (
2723# ifdef FEAT_RIGHTLEFT
2724 curwin->w_p_rl ? col < W_WIDTH(curwin) - curwin->w_p_fdc :
2725# endif
2726 col >= curwin->w_p_fdc
2727# ifdef FEAT_CMDWIN
2728 + (cmdwin_type == 0 ? 0 : 1)
2729# endif
2730 )
2731 mouse_char = ' ';
2732#endif
2733
2734 /* compute the position in the buffer line from the posn on the screen */
2735 if (mouse_comp_pos(curwin, &row, &col, &curwin->w_cursor.lnum))
2736 mouse_past_bottom = TRUE;
2737
2738#ifdef FEAT_VISUAL
2739 /* Start Visual mode before coladvance(), for when 'sel' != "old" */
2740 if ((flags & MOUSE_MAY_VIS) && !VIsual_active)
2741 {
2742 check_visual_highlight();
2743 VIsual = old_cursor;
2744 VIsual_active = TRUE;
2745 VIsual_reselect = TRUE;
2746 /* if 'selectmode' contains "mouse", start Select mode */
2747 may_start_select('o');
2748 setmouse();
2749 if (p_smd)
2750 redraw_cmdline = TRUE; /* show visual mode later */
2751 }
2752#endif
2753
2754 curwin->w_curswant = col;
2755 curwin->w_set_curswant = FALSE; /* May still have been TRUE */
2756 if (coladvance(col) == FAIL) /* Mouse click beyond end of line */
2757 {
2758 if (inclusive != NULL)
2759 *inclusive = TRUE;
2760 mouse_past_eol = TRUE;
2761 }
2762 else if (inclusive != NULL)
2763 *inclusive = FALSE;
2764
2765 count = IN_BUFFER;
2766 if (curwin != old_curwin || curwin->w_cursor.lnum != old_cursor.lnum
2767 || curwin->w_cursor.col != old_cursor.col)
2768 count |= CURSOR_MOVED; /* Cursor has moved */
2769
2770#ifdef FEAT_FOLDING
2771 if (mouse_char == '+')
2772 count |= MOUSE_FOLD_OPEN;
2773 else if (mouse_char != ' ')
2774 count |= MOUSE_FOLD_CLOSE;
2775#endif
2776
2777 return count;
2778}
2779
2780/*
2781 * Compute the position in the buffer line from the posn on the screen in
2782 * window "win".
2783 * Returns TRUE if the position is below the last line.
2784 */
2785 int
2786mouse_comp_pos(win, rowp, colp, lnump)
2787 win_T *win;
2788 int *rowp;
2789 int *colp;
2790 linenr_T *lnump;
2791{
2792 int col = *colp;
2793 int row = *rowp;
2794 linenr_T lnum;
2795 int retval = FALSE;
2796 int off;
2797 int count;
2798
2799#ifdef FEAT_RIGHTLEFT
2800 if (win->w_p_rl)
2801 col = W_WIDTH(win) - 1 - col;
2802#endif
2803
2804 lnum = win->w_topline;
2805
2806 while (row > 0)
2807 {
2808#ifdef FEAT_DIFF
2809 /* Don't include filler lines in "count" */
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002810 if (win->w_p_diff
2811# ifdef FEAT_FOLDING
2812 && !hasFoldingWin(win, lnum, NULL, NULL, TRUE, NULL)
2813# endif
2814 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 {
2816 if (lnum == win->w_topline)
2817 row -= win->w_topfill;
2818 else
2819 row -= diff_check_fill(win, lnum);
2820 count = plines_win_nofill(win, lnum, TRUE);
2821 }
2822 else
2823#endif
2824 count = plines_win(win, lnum, TRUE);
2825 if (count > row)
2826 break; /* Position is in this buffer line. */
2827#ifdef FEAT_FOLDING
2828 (void)hasFoldingWin(win, lnum, NULL, &lnum, TRUE, NULL);
2829#endif
2830 if (lnum == win->w_buffer->b_ml.ml_line_count)
2831 {
2832 retval = TRUE;
2833 break; /* past end of file */
2834 }
2835 row -= count;
2836 ++lnum;
2837 }
2838
2839 if (!retval)
2840 {
2841 /* Compute the column without wrapping. */
2842 off = win_col_off(win) - win_col_off2(win);
2843 if (col < off)
2844 col = off;
2845 col += row * (W_WIDTH(win) - off);
2846 /* add skip column (for long wrapping line) */
2847 col += win->w_skipcol;
2848 }
2849
2850 if (!win->w_p_wrap)
2851 col += win->w_leftcol;
2852
2853 /* skip line number and fold column in front of the line */
2854 col -= win_col_off(win);
2855 if (col < 0)
2856 {
2857#ifdef FEAT_NETBEANS_INTG
2858 if (usingNetbeans)
2859 netbeans_gutter_click(lnum);
2860#endif
2861 col = 0;
2862 }
2863
2864 *colp = col;
2865 *rowp = row;
2866 *lnump = lnum;
2867 return retval;
2868}
2869
2870#if defined(FEAT_WINDOWS) || defined(PROTO)
2871/*
2872 * Find the window at screen position "*rowp" and "*colp". The positions are
2873 * updated to become relative to the top-left of the window.
2874 */
2875/*ARGSUSED*/
2876 win_T *
2877mouse_find_win(rowp, colp)
2878 int *rowp;
2879 int *colp;
2880{
2881 frame_T *fp;
2882
2883 fp = topframe;
2884 for (;;)
2885 {
2886 if (fp->fr_layout == FR_LEAF)
2887 break;
2888#ifdef FEAT_VERTSPLIT
2889 if (fp->fr_layout == FR_ROW)
2890 {
2891 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
2892 {
2893 if (*colp < fp->fr_width)
2894 break;
2895 *colp -= fp->fr_width;
2896 }
2897 }
2898#endif
2899 else /* fr_layout == FR_COL */
2900 {
2901 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
2902 {
2903 if (*rowp < fp->fr_height)
2904 break;
2905 *rowp -= fp->fr_height;
2906 }
2907 }
2908 }
2909 return fp->fr_win;
2910}
2911#endif
2912
Bram Moolenaar843ee412004-06-30 16:16:41 +00002913#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined (FEAT_GUI_MAC) || defined (FEAT_GUI_KDE) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2915 || defined(FEAT_GUI_PHOTON) || defined(PROTO)
2916/*
2917 * Translate window coordinates to buffer position without any side effects
2918 */
2919 int
2920get_fpos_of_mouse(mpos)
2921 pos_T *mpos;
2922{
2923 win_T *wp;
2924 int row = mouse_row;
2925 int col = mouse_col;
2926
2927 if (row < 0 || col < 0) /* check if it makes sense */
2928 return IN_UNKNOWN;
2929
2930#ifdef FEAT_WINDOWS
2931 /* find the window where the row is in */
2932 wp = mouse_find_win(&row, &col);
2933#else
2934 wp = firstwin;
2935#endif
2936 /*
2937 * winpos and height may change in win_enter()!
2938 */
2939 if (row >= wp->w_height) /* In (or below) status line */
2940 return IN_STATUS_LINE;
2941#ifdef FEAT_VERTSPLIT
2942 if (col >= wp->w_width) /* In vertical separator line */
2943 return IN_SEP_LINE;
2944#endif
2945
2946 if (wp != curwin)
2947 return IN_UNKNOWN;
2948
2949 /* compute the position in the buffer line from the posn on the screen */
2950 if (mouse_comp_pos(curwin, &row, &col, &mpos->lnum))
2951 return IN_STATUS_LINE; /* past bottom */
2952
2953 mpos->col = vcol2col(wp, mpos->lnum, col);
2954
2955 if (mpos->col > 0)
2956 --mpos->col;
2957 return IN_BUFFER;
2958}
2959
2960/*
2961 * Convert a virtual (screen) column to a character column.
2962 * The first column is one.
2963 */
2964 int
2965vcol2col(wp, lnum, vcol)
2966 win_T *wp;
2967 linenr_T lnum;
2968 int vcol;
2969{
2970 /* try to advance to the specified column */
2971 int col = 0;
2972 int count = 0;
2973 char_u *ptr;
2974
2975 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
2976 while (count <= vcol && *ptr != NUL)
2977 {
2978 ++col;
2979 count += win_lbr_chartabsize(wp, ptr, count, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002980 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 }
2982 return col;
2983}
2984#endif
2985
2986#endif /* FEAT_MOUSE */
2987
2988#if defined(FEAT_GUI) || defined(WIN3264) || defined(PROTO)
2989/*
2990 * Called when focus changed. Used for the GUI or for systems where this can
2991 * be done in the console (Win32).
2992 */
2993 void
2994ui_focus_change(in_focus)
2995 int in_focus; /* TRUE if focus gained. */
2996{
2997 static time_t last_time = (time_t)0;
2998 int need_redraw = FALSE;
2999
3000 /* When activated: Check if any file was modified outside of Vim.
3001 * Only do this when not done within the last two seconds (could get
3002 * several events in a row). */
3003 if (in_focus && last_time + 2 < time(NULL))
3004 {
3005 need_redraw = check_timestamps(
3006# ifdef FEAT_GUI
3007 gui.in_use
3008# else
3009 FALSE
3010# endif
3011 );
3012 last_time = time(NULL);
3013 }
3014
3015#ifdef FEAT_AUTOCMD
3016 /*
3017 * Fire the focus gained/lost autocommand.
3018 */
3019 need_redraw |= apply_autocmds(in_focus ? EVENT_FOCUSGAINED
3020 : EVENT_FOCUSLOST, NULL, NULL, FALSE, curbuf);
3021#endif
3022
3023 if (need_redraw)
3024 {
3025 /* Something was executed, make sure the cursor is put back where it
3026 * belongs. */
3027 need_wait_return = FALSE;
3028
3029 if (State & CMDLINE)
3030 redrawcmdline();
3031 else if (State == HITRETURN || State == SETWSIZE || State == ASKMORE
3032 || State == EXTERNCMD || State == CONFIRM || exmode_active)
3033 repeat_message();
3034 else if ((State & NORMAL) || (State & INSERT))
3035 {
3036 if (must_redraw != 0)
3037 update_screen(0);
3038 setcursor();
3039 }
3040 cursor_on(); /* redrawing may have switched it off */
3041 out_flush();
3042# ifdef FEAT_GUI
3043 if (gui.in_use)
3044 {
3045 gui_update_cursor(FALSE, TRUE);
3046 gui_update_scrollbars(FALSE);
3047 }
3048# endif
3049 }
3050#ifdef FEAT_TITLE
3051 /* File may have been changed from 'readonly' to 'noreadonly' */
3052 if (need_maketitle)
3053 maketitle();
3054#endif
3055}
3056#endif
3057
3058#if defined(USE_IM_CONTROL) || defined(PROTO)
3059/*
3060 * Save current Input Method status to specified place.
3061 */
3062 void
3063im_save_status(psave)
3064 long *psave;
3065{
3066 /* Don't save when 'imdisable' is set or "xic" is NULL, IM is always
3067 * disabled then (but might start later).
3068 * Also don't save when inside a mapping, vgetc_im_active has not been set
3069 * then.
3070 * And don't save when the keys were stuffed (e.g., for a "." command).
3071 * And don't save when the GUI is running but our window doesn't have
3072 * input focus (e.g., when a find dialog is open). */
3073 if (!p_imdisable && KeyTyped && !KeyStuffed
3074# ifdef FEAT_XIM
3075 && xic != NULL
3076# endif
3077# ifdef FEAT_GUI
3078 && (!gui.in_use || gui.in_focus)
3079# endif
3080 )
3081 {
3082 /* Do save when IM is on, or IM is off and saved status is on. */
3083 if (vgetc_im_active)
3084 *psave = B_IMODE_IM;
3085 else if (*psave == B_IMODE_IM)
3086 *psave = B_IMODE_NONE;
3087 }
3088}
3089#endif