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