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