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