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