blob: d1013efb44041f6f61d37861ff48ab59431f8068 [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 {
Bram Moolenaarebefac62005-12-28 22:39:57 +0000472 /* May have to show a different kind of highlighting for the
473 * selected area. There is no specific redraw command for this,
474 * just redraw all windows on the current buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 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
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001145 /* Can't use ScreenLines unless initialized */
1146 if (ScreenLines == NULL)
1147 return;
1148
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 /*
1150 * Make sure row1 <= row2, and if row1 == row2 that col1 <= col2.
1151 */
1152 if (row1 > row2)
1153 {
1154 row = row1; row1 = row2; row2 = row;
1155 row = col1; col1 = col2; col2 = row;
1156 }
1157 else if (row1 == row2 && col1 > col2)
1158 {
1159 row = col1; col1 = col2; col2 = row;
1160 }
1161#ifdef FEAT_MBYTE
1162 /* correct starting point for being on right halve of double-wide char */
1163 p = ScreenLines + LineOffset[row1];
1164 if (enc_dbcs != 0)
1165 col1 -= (*mb_head_off)(p, p + col1);
1166 else if (enc_utf8 && p[col1] == 0)
1167 --col1;
1168#endif
1169
1170 /* Create a temporary buffer for storing the text */
1171 len = (row2 - row1 + 1) * Columns + 1;
1172#ifdef FEAT_MBYTE
1173 if (enc_dbcs != 0)
1174 len *= 2; /* max. 2 bytes per display cell */
1175 else if (enc_utf8)
1176 len *= 9; /* max. 3 bytes per display cell + 2 composing chars */
1177#endif
1178 buffer = lalloc((long_u)len, TRUE);
1179 if (buffer == NULL) /* out of memory */
1180 return;
1181
1182 /* Process each row in the selection */
1183 for (bufp = buffer, row = row1; row <= row2; row++)
1184 {
1185 if (row == row1)
1186 start_col = col1;
1187 else
1188 start_col = 0;
1189
1190 if (row == row2)
1191 end_col = col2;
1192 else
1193 end_col = Columns;
1194
1195 line_end_col = clip_get_line_end(row);
1196
1197 /* See if we need to nuke some trailing whitespace */
1198 if (end_col >= Columns && (row < row2 || end_col > line_end_col))
1199 {
1200 /* Get rid of trailing whitespace */
1201 end_col = line_end_col;
1202 if (end_col < start_col)
1203 end_col = start_col;
1204
1205 /* If the last line extended to the end, add an extra newline */
1206 if (row == row2)
1207 add_newline_flag = TRUE;
1208 }
1209
1210 /* If after the first row, we need to always add a newline */
1211 if (row > row1 && !LineWraps[row - 1])
1212 *bufp++ = NL;
1213
1214 if (row < screen_Rows && end_col <= screen_Columns)
1215 {
1216#ifdef FEAT_MBYTE
1217 if (enc_dbcs != 0)
1218 {
1219 p = ScreenLines + LineOffset[row];
1220 for (i = start_col; i < end_col; ++i)
1221 if (enc_dbcs == DBCS_JPNU && p[i] == 0x8e)
1222 {
1223 /* single-width double-byte char */
1224 *bufp++ = 0x8e;
1225 *bufp++ = ScreenLines2[LineOffset[row] + i];
1226 }
1227 else
1228 {
1229 *bufp++ = p[i];
1230 if (MB_BYTE2LEN(p[i]) == 2)
1231 *bufp++ = p[++i];
1232 }
1233 }
1234 else if (enc_utf8)
1235 {
1236 int off;
1237
1238 off = LineOffset[row];
1239 for (i = start_col; i < end_col; ++i)
1240 {
1241 /* The base character is either in ScreenLinesUC[] or
1242 * ScreenLines[]. */
1243 if (ScreenLinesUC[off + i] == 0)
1244 *bufp++ = ScreenLines[off + i];
1245 else
1246 {
1247 bufp += utf_char2bytes(ScreenLinesUC[off + i], bufp);
1248 if (ScreenLinesC1[off + i] != 0)
1249 {
1250 /* Add one or two composing characters. */
1251 bufp += utf_char2bytes(ScreenLinesC1[off + i],
1252 bufp);
1253 if (ScreenLinesC2[off + i] != 0)
1254 bufp += utf_char2bytes(ScreenLinesC2[off + i],
1255 bufp);
1256 }
1257 }
1258 /* Skip right halve of double-wide character. */
1259 if (ScreenLines[off + i + 1] == 0)
1260 ++i;
1261 }
1262 }
1263 else
1264#endif
1265 {
1266 STRNCPY(bufp, ScreenLines + LineOffset[row] + start_col,
1267 end_col - start_col);
1268 bufp += end_col - start_col;
1269 }
1270 }
1271 }
1272
1273 /* Add a newline at the end if the selection ended there */
1274 if (add_newline_flag)
1275 *bufp++ = NL;
1276
1277 /* First cleanup any old selection and become the owner. */
1278 clip_free_selection(&clip_star);
1279 clip_own_selection(&clip_star);
1280
1281 /* Yank the text into the '*' register. */
1282 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_star);
1283
1284 /* Make the register contents available to the outside world. */
1285 clip_gen_set_selection(&clip_star);
1286
1287#ifdef FEAT_X11
1288 if (both)
1289 {
1290 /* Do the same for the '+' register. */
1291 clip_free_selection(&clip_plus);
1292 clip_own_selection(&clip_plus);
1293 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_plus);
1294 clip_gen_set_selection(&clip_plus);
1295 }
1296#endif
1297 vim_free(buffer);
1298}
1299
1300/*
1301 * Find the starting and ending positions of the word at the given row and
1302 * column. Only white-separated words are recognized here.
1303 */
1304#define CHAR_CLASS(c) (c <= ' ' ? ' ' : vim_iswordc(c))
1305
1306 static void
1307clip_get_word_boundaries(cb, row, col)
1308 VimClipboard *cb;
1309 int row;
1310 int col;
1311{
1312 int start_class;
1313 int temp_col;
1314 char_u *p;
1315#ifdef FEAT_MBYTE
1316 int mboff;
1317#endif
1318
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001319 if (row >= screen_Rows || col >= screen_Columns || ScreenLines == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 return;
1321
1322 p = ScreenLines + LineOffset[row];
1323#ifdef FEAT_MBYTE
1324 /* Correct for starting in the right halve of a double-wide char */
1325 if (enc_dbcs != 0)
1326 col -= dbcs_screen_head_off(p, p + col);
1327 else if (enc_utf8 && p[col] == 0)
1328 --col;
1329#endif
1330 start_class = CHAR_CLASS(p[col]);
1331
1332 temp_col = col;
1333 for ( ; temp_col > 0; temp_col--)
1334#ifdef FEAT_MBYTE
1335 if (enc_dbcs != 0
1336 && (mboff = dbcs_screen_head_off(p, p + temp_col - 1)) > 0)
1337 temp_col -= mboff;
1338 else
1339#endif
1340 if (CHAR_CLASS(p[temp_col - 1]) != start_class
1341#ifdef FEAT_MBYTE
1342 && !(enc_utf8 && p[temp_col - 1] == 0)
1343#endif
1344 )
1345 break;
1346 cb->word_start_col = temp_col;
1347
1348 temp_col = col;
1349 for ( ; temp_col < screen_Columns; temp_col++)
1350#ifdef FEAT_MBYTE
1351 if (enc_dbcs != 0 && dbcs_ptr2cells(p + temp_col) == 2)
1352 ++temp_col;
1353 else
1354#endif
1355 if (CHAR_CLASS(p[temp_col]) != start_class
1356#ifdef FEAT_MBYTE
1357 && !(enc_utf8 && p[temp_col] == 0)
1358#endif
1359 )
1360 break;
1361 cb->word_end_col = temp_col;
1362}
1363
1364/*
1365 * Find the column position for the last non-whitespace character on the given
1366 * line.
1367 */
1368 static int
1369clip_get_line_end(row)
1370 int row;
1371{
1372 int i;
1373
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001374 if (row >= screen_Rows || ScreenLines == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 return 0;
1376 for (i = screen_Columns; i > 0; i--)
1377 if (ScreenLines[LineOffset[row] + i - 1] != ' ')
1378 break;
1379 return i;
1380}
1381
1382/*
1383 * Update the currently selected region by adding and/or subtracting from the
1384 * beginning or end and inverting the changed area(s).
1385 */
1386 static void
1387clip_update_modeless_selection(cb, row1, col1, row2, col2)
1388 VimClipboard *cb;
1389 int row1;
1390 int col1;
1391 int row2;
1392 int col2;
1393{
1394 /* See if we changed at the beginning of the selection */
1395 if (row1 != cb->start.lnum || col1 != (int)cb->start.col)
1396 {
1397 clip_invert_area(row1, col1, (int)cb->start.lnum, cb->start.col,
1398 CLIP_TOGGLE);
1399 cb->start.lnum = row1;
1400 cb->start.col = col1;
1401 }
1402
1403 /* See if we changed at the end of the selection */
1404 if (row2 != cb->end.lnum || col2 != (int)cb->end.col)
1405 {
1406 clip_invert_area((int)cb->end.lnum, cb->end.col, row2, col2,
1407 CLIP_TOGGLE);
1408 cb->end.lnum = row2;
1409 cb->end.col = col2;
1410 }
1411}
1412
1413 int
1414clip_gen_own_selection(cbd)
1415 VimClipboard *cbd;
1416{
1417#ifdef FEAT_XCLIPBOARD
1418# ifdef FEAT_GUI
1419 if (gui.in_use)
1420 return clip_mch_own_selection(cbd);
1421 else
1422# endif
1423 return clip_xterm_own_selection(cbd);
1424#else
1425 return clip_mch_own_selection(cbd);
1426#endif
1427}
1428
1429 void
1430clip_gen_lose_selection(cbd)
1431 VimClipboard *cbd;
1432{
1433#ifdef FEAT_XCLIPBOARD
1434# ifdef FEAT_GUI
1435 if (gui.in_use)
1436 clip_mch_lose_selection(cbd);
1437 else
1438# endif
1439 clip_xterm_lose_selection(cbd);
1440#else
1441 clip_mch_lose_selection(cbd);
1442#endif
1443}
1444
1445 void
1446clip_gen_set_selection(cbd)
1447 VimClipboard *cbd;
1448{
1449#ifdef FEAT_XCLIPBOARD
1450# ifdef FEAT_GUI
1451 if (gui.in_use)
1452 clip_mch_set_selection(cbd);
1453 else
1454# endif
1455 clip_xterm_set_selection(cbd);
1456#else
1457 clip_mch_set_selection(cbd);
1458#endif
1459}
1460
1461 void
1462clip_gen_request_selection(cbd)
1463 VimClipboard *cbd;
1464{
1465#ifdef FEAT_XCLIPBOARD
1466# ifdef FEAT_GUI
1467 if (gui.in_use)
1468 clip_mch_request_selection(cbd);
1469 else
1470# endif
1471 clip_xterm_request_selection(cbd);
1472#else
1473 clip_mch_request_selection(cbd);
1474#endif
1475}
1476
1477#endif /* FEAT_CLIPBOARD */
1478
1479/*****************************************************************************
1480 * Functions that handle the input buffer.
1481 * This is used for any GUI version, and the unix terminal version.
1482 *
1483 * For Unix, the input characters are buffered to be able to check for a
1484 * CTRL-C. This should be done with signals, but I don't know how to do that
1485 * in a portable way for a tty in RAW mode.
1486 *
1487 * For the client-server code in the console the received keys are put in the
1488 * input buffer.
1489 */
1490
1491#if defined(USE_INPUT_BUF) || defined(PROTO)
1492
1493/*
1494 * Internal typeahead buffer. Includes extra space for long key code
1495 * descriptions which would otherwise overflow. The buffer is considered full
1496 * when only this extra space (or part of it) remains.
1497 */
1498#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
1499 || defined(FEAT_CLIENTSERVER)
1500 /*
1501 * Sun WorkShop and NetBeans stuff debugger commands into the input buffer.
1502 * This requires a larger buffer...
1503 * (Madsen) Go with this for remote input as well ...
1504 */
1505# define INBUFLEN 4096
1506#else
1507# define INBUFLEN 250
1508#endif
1509
1510static char_u inbuf[INBUFLEN + MAX_KEY_CODE_LEN];
1511static int inbufcount = 0; /* number of chars in inbuf[] */
1512
1513/*
1514 * vim_is_input_buf_full(), vim_is_input_buf_empty(), add_to_input_buf(), and
1515 * trash_input_buf() are functions for manipulating the input buffer. These
1516 * are used by the gui_* calls when a GUI is used to handle keyboard input.
1517 */
1518
1519 int
1520vim_is_input_buf_full()
1521{
1522 return (inbufcount >= INBUFLEN);
1523}
1524
1525 int
1526vim_is_input_buf_empty()
1527{
1528 return (inbufcount == 0);
1529}
1530
1531#if defined(FEAT_OLE) || defined(PROTO)
1532 int
1533vim_free_in_input_buf()
1534{
1535 return (INBUFLEN - inbufcount);
1536}
1537#endif
1538
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001539#if defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 int
1541vim_used_in_input_buf()
1542{
1543 return inbufcount;
1544}
1545#endif
1546
1547#if defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) || defined(PROTO)
1548/*
1549 * Return the current contents of the input buffer and make it empty.
1550 * The returned pointer must be passed to set_input_buf() later.
1551 */
1552 char_u *
1553get_input_buf()
1554{
1555 garray_T *gap;
1556
1557 /* We use a growarray to store the data pointer and the length. */
1558 gap = (garray_T *)alloc((unsigned)sizeof(garray_T));
1559 if (gap != NULL)
1560 {
1561 /* Add one to avoid a zero size. */
1562 gap->ga_data = alloc((unsigned)inbufcount + 1);
1563 if (gap->ga_data != NULL)
1564 mch_memmove(gap->ga_data, inbuf, (size_t)inbufcount);
1565 gap->ga_len = inbufcount;
1566 }
1567 trash_input_buf();
1568 return (char_u *)gap;
1569}
1570
1571/*
1572 * Restore the input buffer with a pointer returned from get_input_buf().
1573 * The allocated memory is freed, this only works once!
1574 */
1575 void
1576set_input_buf(p)
1577 char_u *p;
1578{
1579 garray_T *gap = (garray_T *)p;
1580
1581 if (gap != NULL)
1582 {
1583 if (gap->ga_data != NULL)
1584 {
1585 mch_memmove(inbuf, gap->ga_data, gap->ga_len);
1586 inbufcount = gap->ga_len;
1587 vim_free(gap->ga_data);
1588 }
1589 vim_free(gap);
1590 }
1591}
1592#endif
1593
1594#if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM) \
1595 || defined(FEAT_XCLIPBOARD) || defined(VMS) \
Bram Moolenaarf52c7252006-02-10 23:23:57 +00001596 || defined(FEAT_SNIFF) || defined(FEAT_CLIENTSERVER) \
1597 || (defined(FEAT_GUI) && (!defined(USE_ON_FLY_SCROLL) \
1598 || defined(FEAT_MENU))) \
1599 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600/*
1601 * Add the given bytes to the input buffer
1602 * Special keys start with CSI. A real CSI must have been translated to
1603 * CSI KS_EXTRA KE_CSI. K_SPECIAL doesn't require translation.
1604 */
1605 void
1606add_to_input_buf(s, len)
1607 char_u *s;
1608 int len;
1609{
1610 if (inbufcount + len > INBUFLEN + MAX_KEY_CODE_LEN)
1611 return; /* Shouldn't ever happen! */
1612
1613#ifdef FEAT_HANGULIN
1614 if ((State & (INSERT|CMDLINE)) && hangul_input_state_get())
1615 if ((len = hangul_input_process(s, len)) == 0)
1616 return;
1617#endif
1618
1619 while (len--)
1620 inbuf[inbufcount++] = *s++;
1621}
1622#endif
1623
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001624#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 || (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) \
Bram Moolenaarf52c7252006-02-10 23:23:57 +00001626 || (defined(FEAT_GUI) && (!defined(USE_ON_FLY_SCROLL) \
1627 || defined(FEAT_MENU))) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 || defined(PROTO)
1629/*
1630 * Add "str[len]" to the input buffer while escaping CSI bytes.
1631 */
1632 void
1633add_to_input_buf_csi(char_u *str, int len)
1634{
1635 int i;
1636 char_u buf[2];
1637
1638 for (i = 0; i < len; ++i)
1639 {
1640 add_to_input_buf(str + i, 1);
1641 if (str[i] == CSI)
1642 {
1643 /* Turn CSI into K_CSI. */
1644 buf[0] = KS_EXTRA;
1645 buf[1] = (int)KE_CSI;
1646 add_to_input_buf(buf, 2);
1647 }
1648 }
1649}
1650#endif
1651
1652#if defined(FEAT_HANGULIN) || defined(PROTO)
1653 void
1654push_raw_key (s, len)
1655 char_u *s;
1656 int len;
1657{
1658 while (len--)
1659 inbuf[inbufcount++] = *s++;
1660}
1661#endif
1662
1663#if defined(FEAT_GUI) || defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) \
1664 || defined(PROTO)
1665/* Remove everything from the input buffer. Called when ^C is found */
1666 void
1667trash_input_buf()
1668{
1669 inbufcount = 0;
1670}
1671#endif
1672
1673/*
1674 * Read as much data from the input buffer as possible up to maxlen, and store
1675 * it in buf.
1676 * Note: this function used to be Read() in unix.c
1677 */
1678 int
1679read_from_input_buf(buf, maxlen)
1680 char_u *buf;
1681 long maxlen;
1682{
1683 if (inbufcount == 0) /* if the buffer is empty, fill it */
1684 fill_input_buf(TRUE);
1685 if (maxlen > inbufcount)
1686 maxlen = inbufcount;
1687 mch_memmove(buf, inbuf, (size_t)maxlen);
1688 inbufcount -= maxlen;
1689 if (inbufcount)
1690 mch_memmove(inbuf, inbuf + maxlen, (size_t)inbufcount);
1691 return (int)maxlen;
1692}
1693
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001694/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 void
1696fill_input_buf(exit_on_error)
1697 int exit_on_error;
1698{
1699#if defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X_UNIX)
1700 int len;
1701 int try;
1702 static int did_read_something = FALSE;
1703# ifdef FEAT_MBYTE
1704 static char_u *rest = NULL; /* unconverted rest of previous read */
1705 static int restlen = 0;
1706 int unconverted;
1707# endif
1708#endif
1709
1710#ifdef FEAT_GUI
Bram Moolenaar54ee7752005-05-31 22:22:17 +00001711 if (gui.in_use
1712# ifdef NO_CONSOLE_INPUT
1713 /* Don't use the GUI input when the window hasn't been opened yet.
1714 * We get here from ui_inchar() when we should try reading from stdin. */
1715 && !no_console_input()
1716# endif
1717 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 {
1719 gui_mch_update();
1720 return;
1721 }
1722#endif
1723#if defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X_UNIX)
1724 if (vim_is_input_buf_full())
1725 return;
1726 /*
1727 * Fill_input_buf() is only called when we really need a character.
1728 * If we can't get any, but there is some in the buffer, just return.
1729 * If we can't get any, and there isn't any in the buffer, we give up and
1730 * exit Vim.
1731 */
1732# ifdef __BEOS__
1733 /*
1734 * On the BeBox version (for now), all input is secretly performed within
1735 * beos_select() which is called from RealWaitForChar().
1736 */
1737 while (!vim_is_input_buf_full() && RealWaitForChar(read_cmd_fd, 0, NULL))
1738 ;
1739 len = inbufcount;
1740 inbufcount = 0;
1741# else
1742
1743# ifdef FEAT_SNIFF
1744 if (sniff_request_waiting)
1745 {
1746 add_to_input_buf((char_u *)"\233sniff",6); /* results in K_SNIFF */
1747 sniff_request_waiting = 0;
1748 want_sniff_request = 0;
1749 return;
1750 }
1751# endif
1752
1753# ifdef FEAT_MBYTE
1754 if (rest != NULL)
1755 {
1756 /* Use remainder of previous call, starts with an invalid character
1757 * that may become valid when reading more. */
1758 if (restlen > INBUFLEN - inbufcount)
1759 unconverted = INBUFLEN - inbufcount;
1760 else
1761 unconverted = restlen;
1762 mch_memmove(inbuf + inbufcount, rest, unconverted);
1763 if (unconverted == restlen)
1764 {
1765 vim_free(rest);
1766 rest = NULL;
1767 }
1768 else
1769 {
1770 restlen -= unconverted;
1771 mch_memmove(rest, rest + unconverted, restlen);
1772 }
1773 inbufcount += unconverted;
1774 }
1775 else
1776 unconverted = 0;
1777#endif
1778
1779 len = 0; /* to avoid gcc warning */
1780 for (try = 0; try < 100; ++try)
1781 {
1782# ifdef VMS
1783 len = vms_read(
1784# else
1785 len = read(read_cmd_fd,
1786# endif
1787 (char *)inbuf + inbufcount, (size_t)((INBUFLEN - inbufcount)
1788# ifdef FEAT_MBYTE
1789 / input_conv.vc_factor
1790# endif
1791 ));
1792# if 0
1793 ) /* avoid syntax highlight error */
1794# endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001795
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 if (len > 0 || got_int)
1797 break;
1798 /*
1799 * If reading stdin results in an error, continue reading stderr.
1800 * This helps when using "foo | xargs vim".
1801 */
1802 if (!did_read_something && !isatty(read_cmd_fd) && read_cmd_fd == 0)
1803 {
1804 int m = cur_tmode;
1805
1806 /* We probably set the wrong file descriptor to raw mode. Switch
1807 * back to cooked mode, use another descriptor and set the mode to
1808 * what it was. */
1809 settmode(TMODE_COOK);
1810#ifdef HAVE_DUP
1811 /* Use stderr for stdin, also works for shell commands. */
1812 close(0);
1813 dup(2);
1814#else
1815 read_cmd_fd = 2; /* read from stderr instead of stdin */
1816#endif
1817 settmode(m);
1818 }
1819 if (!exit_on_error)
1820 return;
1821 }
1822# endif
1823 if (len <= 0 && !got_int)
1824 read_error_exit();
1825 if (len > 0)
1826 did_read_something = TRUE;
1827 if (got_int)
1828 {
1829 /* Interrupted, pretend a CTRL-C was typed. */
1830 inbuf[0] = 3;
1831 inbufcount = 1;
1832 }
1833 else
1834 {
1835# ifdef FEAT_MBYTE
1836 /*
1837 * May perform conversion on the input characters.
1838 * Include the unconverted rest of the previous call.
1839 * If there is an incomplete char at the end it is kept for the next
1840 * time, reading more bytes should make conversion possible.
1841 * Don't do this in the unlikely event that the input buffer is too
1842 * small ("rest" still contains more bytes).
1843 */
1844 if (input_conv.vc_type != CONV_NONE)
1845 {
1846 inbufcount -= unconverted;
1847 len = convert_input_safe(inbuf + inbufcount,
1848 len + unconverted, INBUFLEN - inbufcount,
1849 rest == NULL ? &rest : NULL, &restlen);
1850 }
1851# endif
1852 while (len-- > 0)
1853 {
1854 /*
1855 * if a CTRL-C was typed, remove it from the buffer and set got_int
1856 */
1857 if (inbuf[inbufcount] == 3 && ctrl_c_interrupts)
1858 {
1859 /* remove everything typed before the CTRL-C */
1860 mch_memmove(inbuf, inbuf + inbufcount, (size_t)(len + 1));
1861 inbufcount = 0;
1862 got_int = TRUE;
1863 }
1864 ++inbufcount;
1865 }
1866 }
1867#endif /* UNIX or OS2 or VMS*/
1868}
1869#endif /* defined(UNIX) || defined(FEAT_GUI) || defined(OS2) || defined(VMS) */
1870
1871/*
1872 * Exit because of an input read error.
1873 */
1874 void
1875read_error_exit()
1876{
1877 if (silent_mode) /* Normal way to exit for "ex -s" */
1878 getout(0);
1879 STRCPY(IObuff, _("Vim: Error reading input, exiting...\n"));
1880 preserve_exit();
1881}
1882
1883#if defined(CURSOR_SHAPE) || defined(PROTO)
1884/*
1885 * May update the shape of the cursor.
1886 */
1887 void
1888ui_cursor_shape()
1889{
1890# ifdef FEAT_GUI
1891 if (gui.in_use)
1892 gui_update_cursor_later();
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001893 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001895 term_cursor_shape();
1896
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897# ifdef MCH_CURSOR_SHAPE
1898 mch_update_cursor();
1899# endif
1900}
1901#endif
1902
1903#if defined(FEAT_CLIPBOARD) || defined(FEAT_GUI) || defined(FEAT_RIGHTLEFT) \
1904 || defined(PROTO)
1905/*
1906 * Check bounds for column number
1907 */
1908 int
1909check_col(col)
1910 int col;
1911{
1912 if (col < 0)
1913 return 0;
1914 if (col >= (int)screen_Columns)
1915 return (int)screen_Columns - 1;
1916 return col;
1917}
1918
1919/*
1920 * Check bounds for row number
1921 */
1922 int
1923check_row(row)
1924 int row;
1925{
1926 if (row < 0)
1927 return 0;
1928 if (row >= (int)screen_Rows)
1929 return (int)screen_Rows - 1;
1930 return row;
1931}
1932#endif
1933
1934/*
1935 * Stuff for the X clipboard. Shared between VMS and Unix.
1936 */
1937
1938#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) || defined(PROTO)
1939# include <X11/Xatom.h>
1940# include <X11/Intrinsic.h>
1941
1942/*
1943 * Open the application context (if it hasn't been opened yet).
1944 * Used for Motif and Athena GUI and the xterm clipboard.
1945 */
1946 void
1947open_app_context()
1948{
1949 if (app_context == NULL)
1950 {
1951 XtToolkitInitialize();
1952 app_context = XtCreateApplicationContext();
1953 }
1954}
1955
1956static Atom vim_atom; /* Vim's own special selection format */
1957#ifdef FEAT_MBYTE
1958static Atom vimenc_atom; /* Vim's extended selection format */
1959#endif
1960static Atom compound_text_atom;
1961static Atom text_atom;
1962static Atom targets_atom;
1963
1964 void
1965x11_setup_atoms(dpy)
1966 Display *dpy;
1967{
1968 vim_atom = XInternAtom(dpy, VIM_ATOM_NAME, False);
1969#ifdef FEAT_MBYTE
1970 vimenc_atom = XInternAtom(dpy, VIMENC_ATOM_NAME,False);
1971#endif
1972 compound_text_atom = XInternAtom(dpy, "COMPOUND_TEXT", False);
1973 text_atom = XInternAtom(dpy, "TEXT", False);
1974 targets_atom = XInternAtom(dpy, "TARGETS", False);
1975 clip_star.sel_atom = XA_PRIMARY;
1976 clip_plus.sel_atom = XInternAtom(dpy, "CLIPBOARD", False);
1977}
1978
1979/*
1980 * X Selection stuff, for cutting and pasting text to other windows.
1981 */
1982
1983static void clip_x11_request_selection_cb __ARGS((Widget, XtPointer, Atom *, Atom *, XtPointer, long_u *, int *));
1984
1985/* ARGSUSED */
1986 static void
1987clip_x11_request_selection_cb(w, success, sel_atom, type, value, length,
1988 format)
1989 Widget w;
1990 XtPointer success;
1991 Atom *sel_atom;
1992 Atom *type;
1993 XtPointer value;
1994 long_u *length;
1995 int *format;
1996{
1997 int motion_type;
1998 long_u len;
1999 char_u *p;
2000 char **text_list = NULL;
2001 VimClipboard *cbd;
2002#ifdef FEAT_MBYTE
2003 char_u *tmpbuf = NULL;
2004#endif
2005
2006 if (*sel_atom == clip_plus.sel_atom)
2007 cbd = &clip_plus;
2008 else
2009 cbd = &clip_star;
2010
2011 if (value == NULL || *length == 0)
2012 {
2013 clip_free_selection(cbd); /* ??? [what's the query?] */
2014 *(int *)success = FALSE;
2015 return;
2016 }
2017 motion_type = MCHAR;
2018 p = (char_u *)value;
2019 len = *length;
2020 if (*type == vim_atom)
2021 {
2022 motion_type = *p++;
2023 len--;
2024 }
2025
2026#ifdef FEAT_MBYTE
2027 else if (*type == vimenc_atom)
2028 {
2029 char_u *enc;
2030 vimconv_T conv;
2031 int convlen;
2032
2033 motion_type = *p++;
2034 --len;
2035
2036 enc = p;
2037 p += STRLEN(p) + 1;
2038 len -= p - enc;
2039
2040 /* If the encoding of the text is different from 'encoding', attempt
2041 * converting it. */
2042 conv.vc_type = CONV_NONE;
2043 convert_setup(&conv, enc, p_enc);
2044 if (conv.vc_type != CONV_NONE)
2045 {
2046 convlen = len; /* Need to use an int here. */
2047 tmpbuf = string_convert(&conv, p, &convlen);
2048 len = convlen;
2049 if (tmpbuf != NULL)
2050 p = tmpbuf;
2051 convert_setup(&conv, NULL, NULL);
2052 }
2053 }
2054#endif
2055
2056 else if (*type == compound_text_atom || (
2057#ifdef FEAT_MBYTE
2058 enc_dbcs != 0 &&
2059#endif
2060 *type == text_atom))
2061 {
2062 XTextProperty text_prop;
2063 int n_text = 0;
2064 int status;
2065
2066 text_prop.value = (unsigned char *)value;
2067 text_prop.encoding = *type;
2068 text_prop.format = *format;
2069 text_prop.nitems = STRLEN(value);
2070 status = XmbTextPropertyToTextList(X_DISPLAY, &text_prop,
2071 &text_list, &n_text);
2072 if (status != Success || n_text < 1)
2073 {
2074 *(int *)success = FALSE;
2075 return;
2076 }
2077 p = (char_u *)text_list[0];
2078 len = STRLEN(p);
2079 }
2080 clip_yank_selection(motion_type, p, (long)len, cbd);
2081
2082 if (text_list != NULL)
2083 XFreeStringList(text_list);
2084#ifdef FEAT_MBYTE
2085 vim_free(tmpbuf);
2086#endif
2087 XtFree((char *)value);
2088 *(int *)success = TRUE;
2089}
2090
2091 void
2092clip_x11_request_selection(myShell, dpy, cbd)
2093 Widget myShell;
2094 Display *dpy;
2095 VimClipboard *cbd;
2096{
2097 XEvent event;
2098 Atom type;
2099 static int success;
2100 int i;
2101 int nbytes = 0;
2102 char_u *buffer;
2103
2104 for (i =
2105#ifdef FEAT_MBYTE
2106 0
2107#else
2108 1
2109#endif
2110 ; i < 5; i++)
2111 {
2112 switch (i)
2113 {
2114#ifdef FEAT_MBYTE
2115 case 0: type = vimenc_atom; break;
2116#endif
2117 case 1: type = vim_atom; break;
2118 case 2: type = compound_text_atom; break;
2119 case 3: type = text_atom; break;
2120 default: type = XA_STRING;
2121 }
2122 XtGetSelectionValue(myShell, cbd->sel_atom, type,
2123 clip_x11_request_selection_cb, (XtPointer)&success, CurrentTime);
2124
2125 /* Make sure the request for the selection goes out before waiting for
2126 * a response. */
2127 XFlush(dpy);
2128
2129 /*
2130 * Wait for result of selection request, otherwise if we type more
2131 * characters, then they will appear before the one that requested the
2132 * paste! Don't worry, we will catch up with any other events later.
2133 */
2134 for (;;)
2135 {
2136 if (XCheckTypedEvent(dpy, SelectionNotify, &event))
2137 break;
Bram Moolenaarebefac62005-12-28 22:39:57 +00002138 if (XCheckTypedEvent(dpy, SelectionRequest, &event))
2139 /* We may get a SelectionRequest here and if we don't handle
2140 * it we hang. KDE klipper does this, for example. */
2141 XtDispatchEvent(&event);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142
2143 /* Do we need this? Probably not. */
2144 XSync(dpy, False);
2145
2146 /* Bernhard Walle solved a slow paste response in an X terminal by
2147 * adding: usleep(10000); here. */
2148 }
2149
2150 /* this is where clip_x11_request_selection_cb() is actually called */
2151 XtDispatchEvent(&event);
2152
2153 if (success)
2154 return;
2155 }
2156
2157 /* Final fallback position - use the X CUT_BUFFER0 store */
2158 buffer = (char_u *)XFetchBuffer(dpy, &nbytes, 0);
2159 if (nbytes > 0)
2160 {
2161 /* Got something */
2162 clip_yank_selection(MCHAR, buffer, (long)nbytes, cbd);
2163 XFree((void *)buffer);
2164 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002165 verb_msg((char_u *)_("Used CUT_BUFFER0 instead of empty selection"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 }
2167}
2168
2169static Boolean clip_x11_convert_selection_cb __ARGS((Widget, Atom *, Atom *, Atom *, XtPointer *, long_u *, int *));
2170
2171/* ARGSUSED */
2172 static Boolean
2173clip_x11_convert_selection_cb(w, sel_atom, target, type, value, length, format)
2174 Widget w;
2175 Atom *sel_atom;
2176 Atom *target;
2177 Atom *type;
2178 XtPointer *value;
2179 long_u *length;
2180 int *format;
2181{
2182 char_u *string;
2183 char_u *result;
2184 int motion_type;
2185 VimClipboard *cbd;
2186 int i;
2187
2188 if (*sel_atom == clip_plus.sel_atom)
2189 cbd = &clip_plus;
2190 else
2191 cbd = &clip_star;
2192
2193 if (!cbd->owned)
2194 return False; /* Shouldn't ever happen */
2195
2196 /* requestor wants to know what target types we support */
2197 if (*target == targets_atom)
2198 {
2199 Atom *array;
2200
2201 if ((array = (Atom *)XtMalloc((unsigned)(sizeof(Atom) * 6))) == NULL)
2202 return False;
2203 *value = (XtPointer)array;
2204 i = 0;
2205 array[i++] = XA_STRING;
2206 array[i++] = targets_atom;
2207#ifdef FEAT_MBYTE
2208 array[i++] = vimenc_atom;
2209#endif
2210 array[i++] = vim_atom;
2211 array[i++] = text_atom;
2212 array[i++] = compound_text_atom;
2213 *type = XA_ATOM;
2214 /* This used to be: *format = sizeof(Atom) * 8; but that caused
2215 * crashes on 64 bit machines. (Peter Derr) */
2216 *format = 32;
2217 *length = i;
2218 return True;
2219 }
2220
2221 if ( *target != XA_STRING
2222#ifdef FEAT_MBYTE
2223 && *target != vimenc_atom
2224#endif
2225 && *target != vim_atom
2226 && *target != text_atom
2227 && *target != compound_text_atom)
2228 return False;
2229
2230 clip_get_selection(cbd);
2231 motion_type = clip_convert_selection(&string, length, cbd);
2232 if (motion_type < 0)
2233 return False;
2234
2235 /* For our own format, the first byte contains the motion type */
2236 if (*target == vim_atom)
2237 (*length)++;
2238
2239#ifdef FEAT_MBYTE
2240 /* Our own format with encoding: motion 'encoding' NUL text */
2241 if (*target == vimenc_atom)
2242 *length += STRLEN(p_enc) + 2;
2243#endif
2244
2245 *value = XtMalloc((Cardinal)*length);
2246 result = (char_u *)*value;
2247 if (result == NULL)
2248 {
2249 vim_free(string);
2250 return False;
2251 }
2252
2253 if (*target == XA_STRING)
2254 {
2255 mch_memmove(result, string, (size_t)(*length));
2256 *type = XA_STRING;
2257 }
2258 else if (*target == compound_text_atom
2259 || *target == text_atom)
2260 {
2261 XTextProperty text_prop;
2262 char *string_nt = (char *)alloc((unsigned)*length + 1);
2263
2264 /* create NUL terminated string which XmbTextListToTextProperty wants */
2265 mch_memmove(string_nt, string, (size_t)*length);
2266 string_nt[*length] = NUL;
2267 XmbTextListToTextProperty(X_DISPLAY, (char **)&string_nt, 1,
2268 XCompoundTextStyle, &text_prop);
2269 vim_free(string_nt);
2270 XtFree(*value); /* replace with COMPOUND text */
2271 *value = (XtPointer)(text_prop.value); /* from plain text */
2272 *length = text_prop.nitems;
2273 *type = compound_text_atom;
2274 }
2275
2276#ifdef FEAT_MBYTE
2277 else if (*target == vimenc_atom)
2278 {
2279 int l = STRLEN(p_enc);
2280
2281 result[0] = motion_type;
2282 STRCPY(result + 1, p_enc);
2283 mch_memmove(result + l + 2, string, (size_t)(*length - l - 2));
2284 *type = vimenc_atom;
2285 }
2286#endif
2287
2288 else
2289 {
2290 result[0] = motion_type;
2291 mch_memmove(result + 1, string, (size_t)(*length - 1));
2292 *type = vim_atom;
2293 }
2294 *format = 8; /* 8 bits per char */
2295 vim_free(string);
2296 return True;
2297}
2298
2299static void clip_x11_lose_ownership_cb __ARGS((Widget, Atom *));
2300
2301/* ARGSUSED */
2302 static void
2303clip_x11_lose_ownership_cb(w, sel_atom)
2304 Widget w;
2305 Atom *sel_atom;
2306{
2307 if (*sel_atom == clip_plus.sel_atom)
2308 clip_lose_selection(&clip_plus);
2309 else
2310 clip_lose_selection(&clip_star);
2311}
2312
2313 void
2314clip_x11_lose_selection(myShell, cbd)
2315 Widget myShell;
2316 VimClipboard *cbd;
2317{
2318 XtDisownSelection(myShell, cbd->sel_atom, CurrentTime);
2319}
2320
2321 int
2322clip_x11_own_selection(myShell, cbd)
2323 Widget myShell;
2324 VimClipboard *cbd;
2325{
2326 if (XtOwnSelection(myShell, cbd->sel_atom, CurrentTime,
2327 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
2328 NULL) == False)
2329 return FAIL;
2330 return OK;
2331}
2332
2333/*
2334 * Send the current selection to the clipboard. Do nothing for X because we
2335 * will fill in the selection only when requested by another app.
2336 */
2337/*ARGSUSED*/
2338 void
2339clip_x11_set_selection(cbd)
2340 VimClipboard *cbd;
2341{
2342}
2343#endif
2344
2345#if defined(FEAT_MOUSE) || defined(PROTO)
2346
2347/*
2348 * Move the cursor to the specified row and column on the screen.
2349 * Change current window if neccesary. Returns an integer with the
2350 * CURSOR_MOVED bit set if the cursor has moved or unset otherwise.
2351 *
2352 * The MOUSE_FOLD_CLOSE bit is set when clicked on the '-' in a fold column.
2353 * The MOUSE_FOLD_OPEN bit is set when clicked on the '+' in a fold column.
2354 *
2355 * If flags has MOUSE_FOCUS, then the current window will not be changed, and
2356 * if the mouse is outside the window then the text will scroll, or if the
2357 * mouse was previously on a status line, then the status line may be dragged.
2358 *
2359 * If flags has MOUSE_MAY_VIS, then VIsual mode will be started before the
2360 * cursor is moved unless the cursor was on a status line.
2361 * This function returns one of IN_UNKNOWN, IN_BUFFER, IN_STATUS_LINE or
2362 * IN_SEP_LINE depending on where the cursor was clicked.
2363 *
2364 * If flags has MOUSE_MAY_STOP_VIS, then Visual mode will be stopped, unless
2365 * the mouse is on the status line of the same window.
2366 *
2367 * If flags has MOUSE_DID_MOVE, nothing is done if the mouse didn't move since
2368 * the last call.
2369 *
2370 * If flags has MOUSE_SETPOS, nothing is done, only the current position is
2371 * remembered.
2372 */
2373 int
2374jump_to_mouse(flags, inclusive, which_button)
2375 int flags;
2376 int *inclusive; /* used for inclusive operator, can be NULL */
2377 int which_button; /* MOUSE_LEFT, MOUSE_RIGHT, MOUSE_MIDDLE */
2378{
2379 static int on_status_line = 0; /* #lines below bottom of window */
2380#ifdef FEAT_VERTSPLIT
2381 static int on_sep_line = 0; /* on separator right of window */
2382#endif
2383 static int prev_row = -1;
2384 static int prev_col = -1;
2385 static win_T *dragwin = NULL; /* window being dragged */
2386 static int did_drag = FALSE; /* drag was noticed */
2387
2388 win_T *wp, *old_curwin;
2389 pos_T old_cursor;
2390 int count;
2391 int first;
2392 int row = mouse_row;
2393 int col = mouse_col;
2394#ifdef FEAT_FOLDING
2395 int mouse_char;
2396#endif
2397
2398 mouse_past_bottom = FALSE;
2399 mouse_past_eol = FALSE;
2400
2401 if (flags & MOUSE_RELEASED)
2402 {
2403 /* On button release we may change window focus if positioned on a
2404 * status line and no dragging happened. */
2405 if (dragwin != NULL && !did_drag)
2406 flags &= ~(MOUSE_FOCUS | MOUSE_DID_MOVE);
2407 dragwin = NULL;
2408 did_drag = FALSE;
2409 }
2410
2411 if ((flags & MOUSE_DID_MOVE)
2412 && prev_row == mouse_row
2413 && prev_col == mouse_col)
2414 {
2415retnomove:
2416 /* before moving the cursor for a left click wich is NOT in a status
2417 * line, stop Visual mode */
2418 if (on_status_line)
2419 return IN_STATUS_LINE;
2420#ifdef FEAT_VERTSPLIT
2421 if (on_sep_line)
2422 return IN_SEP_LINE;
2423#endif
2424#ifdef FEAT_VISUAL
2425 if (flags & MOUSE_MAY_STOP_VIS)
2426 {
2427 end_visual_mode();
2428 redraw_curbuf_later(INVERTED); /* delete the inversion */
2429 }
2430#endif
2431#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2432 /* Continue a modeless selection in another window. */
2433 if (cmdwin_type != 0 && row < W_WINROW(curwin))
2434 return IN_OTHER_WIN;
2435#endif
2436 return IN_BUFFER;
2437 }
2438
2439 prev_row = mouse_row;
2440 prev_col = mouse_col;
2441
2442 if (flags & MOUSE_SETPOS)
2443 goto retnomove; /* ugly goto... */
2444
2445#ifdef FEAT_FOLDING
2446 /* Remember the character under the mouse, it might be a '-' or '+' in the
2447 * fold column. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002448 if (row >= 0 && row < Rows && col >= 0 && col <= Columns
2449 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 mouse_char = ScreenLines[LineOffset[row] + col];
2451 else
2452 mouse_char = ' ';
2453#endif
2454
2455 old_curwin = curwin;
2456 old_cursor = curwin->w_cursor;
2457
2458 if (!(flags & MOUSE_FOCUS))
2459 {
2460 if (row < 0 || col < 0) /* check if it makes sense */
2461 return IN_UNKNOWN;
2462
2463#ifdef FEAT_WINDOWS
2464 /* find the window where the row is in */
2465 wp = mouse_find_win(&row, &col);
2466#else
2467 wp = firstwin;
2468#endif
2469 dragwin = NULL;
2470 /*
2471 * winpos and height may change in win_enter()!
2472 */
2473 if (row >= wp->w_height) /* In (or below) status line */
2474 {
2475 on_status_line = row - wp->w_height + 1;
2476 dragwin = wp;
2477 }
2478 else
2479 on_status_line = 0;
2480#ifdef FEAT_VERTSPLIT
2481 if (col >= wp->w_width) /* In separator line */
2482 {
2483 on_sep_line = col - wp->w_width + 1;
2484 dragwin = wp;
2485 }
2486 else
2487 on_sep_line = 0;
2488
2489 /* The rightmost character of the status line might be a vertical
2490 * separator character if there is no connecting window to the right. */
2491 if (on_status_line && on_sep_line)
2492 {
2493 if (stl_connected(wp))
2494 on_sep_line = 0;
2495 else
2496 on_status_line = 0;
2497 }
2498#endif
2499
2500#ifdef FEAT_VISUAL
2501 /* Before jumping to another buffer, or moving the cursor for a left
2502 * click, stop Visual mode. */
2503 if (VIsual_active
2504 && (wp->w_buffer != curwin->w_buffer
2505 || (!on_status_line
2506# ifdef FEAT_VERTSPLIT
2507 && !on_sep_line
2508# endif
2509# ifdef FEAT_FOLDING
2510 && (
2511# ifdef FEAT_RIGHTLEFT
2512 wp->w_p_rl ? col < W_WIDTH(wp) - wp->w_p_fdc :
2513# endif
2514 col >= wp->w_p_fdc
2515# ifdef FEAT_CMDWIN
2516 + (cmdwin_type == 0 && wp == curwin ? 0 : 1)
2517# endif
2518 )
2519# endif
2520 && (flags & MOUSE_MAY_STOP_VIS))))
2521 {
2522 end_visual_mode();
2523 redraw_curbuf_later(INVERTED); /* delete the inversion */
2524 }
2525#endif
2526#ifdef FEAT_CMDWIN
2527 if (cmdwin_type != 0 && wp != curwin)
2528 {
2529 /* A click outside the command-line window: Use modeless
2530 * selection if possible. Allow dragging the status line of
2531 * windows just above the command-line window. */
2532 if (wp->w_winrow + wp->w_height
2533 != curwin->w_prev->w_winrow + curwin->w_prev->w_height)
2534 {
2535 on_status_line = 0;
2536 dragwin = NULL;
2537 }
2538# ifdef FEAT_VERTSPLIT
2539 on_sep_line = 0;
2540# endif
2541# ifdef FEAT_CLIPBOARD
2542 if (on_status_line)
2543 return IN_STATUS_LINE;
2544 return IN_OTHER_WIN;
2545# else
2546 row = 0;
2547 col += wp->w_wincol;
2548 wp = curwin;
2549# endif
2550 }
2551#endif
2552#ifdef FEAT_WINDOWS
2553 /* Only change window focus when not clicking on or dragging the
2554 * status line. Do change focus when releasing the mouse button
2555 * (MOUSE_FOCUS was set above if we dragged first). */
2556 if (dragwin == NULL || (flags & MOUSE_RELEASED))
2557 win_enter(wp, TRUE); /* can make wp invalid! */
2558# ifdef CHECK_DOUBLE_CLICK
2559 /* set topline, to be able to check for double click ourselves */
2560 if (curwin != old_curwin)
2561 set_mouse_topline(curwin);
2562# endif
2563#endif
2564 if (on_status_line) /* In (or below) status line */
2565 {
2566 /* Don't use start_arrow() if we're in the same window */
2567 if (curwin == old_curwin)
2568 return IN_STATUS_LINE;
2569 else
2570 return IN_STATUS_LINE | CURSOR_MOVED;
2571 }
2572#ifdef FEAT_VERTSPLIT
2573 if (on_sep_line) /* In (or below) status line */
2574 {
2575 /* Don't use start_arrow() if we're in the same window */
2576 if (curwin == old_curwin)
2577 return IN_SEP_LINE;
2578 else
2579 return IN_SEP_LINE | CURSOR_MOVED;
2580 }
2581#endif
2582
2583 curwin->w_cursor.lnum = curwin->w_topline;
2584#ifdef FEAT_GUI
2585 /* remember topline, needed for double click */
2586 gui_prev_topline = curwin->w_topline;
2587# ifdef FEAT_DIFF
2588 gui_prev_topfill = curwin->w_topfill;
2589# endif
2590#endif
2591 }
2592 else if (on_status_line && which_button == MOUSE_LEFT)
2593 {
2594#ifdef FEAT_WINDOWS
2595 if (dragwin != NULL)
2596 {
2597 /* Drag the status line */
2598 count = row - dragwin->w_winrow - dragwin->w_height + 1
2599 - on_status_line;
2600 win_drag_status_line(dragwin, count);
2601 did_drag |= count;
2602 }
2603#endif
2604 return IN_STATUS_LINE; /* Cursor didn't move */
2605 }
2606#ifdef FEAT_VERTSPLIT
2607 else if (on_sep_line && which_button == MOUSE_LEFT)
2608 {
2609 if (dragwin != NULL)
2610 {
2611 /* Drag the separator column */
2612 count = col - dragwin->w_wincol - dragwin->w_width + 1
2613 - on_sep_line;
2614 win_drag_vsep_line(dragwin, count);
2615 did_drag |= count;
2616 }
2617 return IN_SEP_LINE; /* Cursor didn't move */
2618 }
2619#endif
2620 else /* keep_window_focus must be TRUE */
2621 {
2622#ifdef FEAT_VISUAL
2623 /* before moving the cursor for a left click, stop Visual mode */
2624 if (flags & MOUSE_MAY_STOP_VIS)
2625 {
2626 end_visual_mode();
2627 redraw_curbuf_later(INVERTED); /* delete the inversion */
2628 }
2629#endif
2630
2631#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2632 /* Continue a modeless selection in another window. */
2633 if (cmdwin_type != 0 && row < W_WINROW(curwin))
2634 return IN_OTHER_WIN;
2635#endif
2636
2637 row -= W_WINROW(curwin);
2638#ifdef FEAT_VERTSPLIT
2639 col -= W_WINCOL(curwin);
2640#endif
2641
2642 /*
2643 * When clicking beyond the end of the window, scroll the screen.
2644 * Scroll by however many rows outside the window we are.
2645 */
2646 if (row < 0)
2647 {
2648 count = 0;
2649 for (first = TRUE; curwin->w_topline > 1; )
2650 {
2651#ifdef FEAT_DIFF
2652 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
2653 ++count;
2654 else
2655#endif
2656 count += plines(curwin->w_topline - 1);
2657 if (!first && count > -row)
2658 break;
2659 first = FALSE;
2660#ifdef FEAT_FOLDING
2661 hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2662#endif
2663#ifdef FEAT_DIFF
2664 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
2665 ++curwin->w_topfill;
2666 else
2667#endif
2668 {
2669 --curwin->w_topline;
2670#ifdef FEAT_DIFF
2671 curwin->w_topfill = 0;
2672#endif
2673 }
2674 }
2675#ifdef FEAT_DIFF
2676 check_topfill(curwin, FALSE);
2677#endif
2678 curwin->w_valid &=
2679 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2680 redraw_later(VALID);
2681 row = 0;
2682 }
2683 else if (row >= curwin->w_height)
2684 {
2685 count = 0;
2686 for (first = TRUE; curwin->w_topline < curbuf->b_ml.ml_line_count; )
2687 {
2688#ifdef FEAT_DIFF
2689 if (curwin->w_topfill > 0)
2690 ++count;
2691 else
2692#endif
2693 count += plines(curwin->w_topline);
2694 if (!first && count > row - curwin->w_height + 1)
2695 break;
2696 first = FALSE;
2697#ifdef FEAT_FOLDING
2698 if (hasFolding(curwin->w_topline, NULL, &curwin->w_topline)
2699 && curwin->w_topline == curbuf->b_ml.ml_line_count)
2700 break;
2701#endif
2702#ifdef FEAT_DIFF
2703 if (curwin->w_topfill > 0)
2704 --curwin->w_topfill;
2705 else
2706#endif
2707 {
2708 ++curwin->w_topline;
2709#ifdef FEAT_DIFF
2710 curwin->w_topfill =
2711 diff_check_fill(curwin, curwin->w_topline);
2712#endif
2713 }
2714 }
2715#ifdef FEAT_DIFF
2716 check_topfill(curwin, FALSE);
2717#endif
2718 redraw_later(VALID);
2719 curwin->w_valid &=
2720 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2721 row = curwin->w_height - 1;
2722 }
2723 else if (row == 0)
2724 {
2725 /* When dragging the mouse, while the text has been scrolled up as
2726 * far as it goes, moving the mouse in the top line should scroll
2727 * the text down (done later when recomputing w_topline). */
2728 if (mouse_dragging
2729 && curwin->w_cursor.lnum
2730 == curwin->w_buffer->b_ml.ml_line_count
2731 && curwin->w_cursor.lnum == curwin->w_topline)
2732 curwin->w_valid &= ~(VALID_TOPLINE);
2733 }
2734 }
2735
2736#ifdef FEAT_FOLDING
2737 /* Check for position outside of the fold column. */
2738 if (
2739# ifdef FEAT_RIGHTLEFT
2740 curwin->w_p_rl ? col < W_WIDTH(curwin) - curwin->w_p_fdc :
2741# endif
2742 col >= curwin->w_p_fdc
2743# ifdef FEAT_CMDWIN
2744 + (cmdwin_type == 0 ? 0 : 1)
2745# endif
2746 )
2747 mouse_char = ' ';
2748#endif
2749
2750 /* compute the position in the buffer line from the posn on the screen */
2751 if (mouse_comp_pos(curwin, &row, &col, &curwin->w_cursor.lnum))
2752 mouse_past_bottom = TRUE;
2753
2754#ifdef FEAT_VISUAL
2755 /* Start Visual mode before coladvance(), for when 'sel' != "old" */
2756 if ((flags & MOUSE_MAY_VIS) && !VIsual_active)
2757 {
2758 check_visual_highlight();
2759 VIsual = old_cursor;
2760 VIsual_active = TRUE;
2761 VIsual_reselect = TRUE;
2762 /* if 'selectmode' contains "mouse", start Select mode */
2763 may_start_select('o');
2764 setmouse();
Bram Moolenaar7df351e2006-01-23 22:30:28 +00002765 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 redraw_cmdline = TRUE; /* show visual mode later */
2767 }
2768#endif
2769
2770 curwin->w_curswant = col;
2771 curwin->w_set_curswant = FALSE; /* May still have been TRUE */
2772 if (coladvance(col) == FAIL) /* Mouse click beyond end of line */
2773 {
2774 if (inclusive != NULL)
2775 *inclusive = TRUE;
2776 mouse_past_eol = TRUE;
2777 }
2778 else if (inclusive != NULL)
2779 *inclusive = FALSE;
2780
2781 count = IN_BUFFER;
2782 if (curwin != old_curwin || curwin->w_cursor.lnum != old_cursor.lnum
2783 || curwin->w_cursor.col != old_cursor.col)
2784 count |= CURSOR_MOVED; /* Cursor has moved */
2785
2786#ifdef FEAT_FOLDING
2787 if (mouse_char == '+')
2788 count |= MOUSE_FOLD_OPEN;
2789 else if (mouse_char != ' ')
2790 count |= MOUSE_FOLD_CLOSE;
2791#endif
2792
2793 return count;
2794}
2795
2796/*
2797 * Compute the position in the buffer line from the posn on the screen in
2798 * window "win".
2799 * Returns TRUE if the position is below the last line.
2800 */
2801 int
2802mouse_comp_pos(win, rowp, colp, lnump)
2803 win_T *win;
2804 int *rowp;
2805 int *colp;
2806 linenr_T *lnump;
2807{
2808 int col = *colp;
2809 int row = *rowp;
2810 linenr_T lnum;
2811 int retval = FALSE;
2812 int off;
2813 int count;
2814
2815#ifdef FEAT_RIGHTLEFT
2816 if (win->w_p_rl)
2817 col = W_WIDTH(win) - 1 - col;
2818#endif
2819
2820 lnum = win->w_topline;
2821
2822 while (row > 0)
2823 {
2824#ifdef FEAT_DIFF
2825 /* Don't include filler lines in "count" */
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002826 if (win->w_p_diff
2827# ifdef FEAT_FOLDING
2828 && !hasFoldingWin(win, lnum, NULL, NULL, TRUE, NULL)
2829# endif
2830 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 {
2832 if (lnum == win->w_topline)
2833 row -= win->w_topfill;
2834 else
2835 row -= diff_check_fill(win, lnum);
2836 count = plines_win_nofill(win, lnum, TRUE);
2837 }
2838 else
2839#endif
2840 count = plines_win(win, lnum, TRUE);
2841 if (count > row)
2842 break; /* Position is in this buffer line. */
2843#ifdef FEAT_FOLDING
2844 (void)hasFoldingWin(win, lnum, NULL, &lnum, TRUE, NULL);
2845#endif
2846 if (lnum == win->w_buffer->b_ml.ml_line_count)
2847 {
2848 retval = TRUE;
2849 break; /* past end of file */
2850 }
2851 row -= count;
2852 ++lnum;
2853 }
2854
2855 if (!retval)
2856 {
2857 /* Compute the column without wrapping. */
2858 off = win_col_off(win) - win_col_off2(win);
2859 if (col < off)
2860 col = off;
2861 col += row * (W_WIDTH(win) - off);
2862 /* add skip column (for long wrapping line) */
2863 col += win->w_skipcol;
2864 }
2865
2866 if (!win->w_p_wrap)
2867 col += win->w_leftcol;
2868
2869 /* skip line number and fold column in front of the line */
2870 col -= win_col_off(win);
2871 if (col < 0)
2872 {
2873#ifdef FEAT_NETBEANS_INTG
2874 if (usingNetbeans)
2875 netbeans_gutter_click(lnum);
2876#endif
2877 col = 0;
2878 }
2879
2880 *colp = col;
2881 *rowp = row;
2882 *lnump = lnum;
2883 return retval;
2884}
2885
2886#if defined(FEAT_WINDOWS) || defined(PROTO)
2887/*
2888 * Find the window at screen position "*rowp" and "*colp". The positions are
2889 * updated to become relative to the top-left of the window.
2890 */
2891/*ARGSUSED*/
2892 win_T *
2893mouse_find_win(rowp, colp)
2894 int *rowp;
2895 int *colp;
2896{
2897 frame_T *fp;
2898
2899 fp = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002900 *rowp -= firstwin->w_winrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 for (;;)
2902 {
2903 if (fp->fr_layout == FR_LEAF)
2904 break;
2905#ifdef FEAT_VERTSPLIT
2906 if (fp->fr_layout == FR_ROW)
2907 {
2908 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
2909 {
2910 if (*colp < fp->fr_width)
2911 break;
2912 *colp -= fp->fr_width;
2913 }
2914 }
2915#endif
2916 else /* fr_layout == FR_COL */
2917 {
2918 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
2919 {
2920 if (*rowp < fp->fr_height)
2921 break;
2922 *rowp -= fp->fr_height;
2923 }
2924 }
2925 }
2926 return fp->fr_win;
2927}
2928#endif
2929
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00002930#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined (FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2932 || defined(FEAT_GUI_PHOTON) || defined(PROTO)
2933/*
2934 * Translate window coordinates to buffer position without any side effects
2935 */
2936 int
2937get_fpos_of_mouse(mpos)
2938 pos_T *mpos;
2939{
2940 win_T *wp;
2941 int row = mouse_row;
2942 int col = mouse_col;
2943
2944 if (row < 0 || col < 0) /* check if it makes sense */
2945 return IN_UNKNOWN;
2946
2947#ifdef FEAT_WINDOWS
2948 /* find the window where the row is in */
2949 wp = mouse_find_win(&row, &col);
2950#else
2951 wp = firstwin;
2952#endif
2953 /*
2954 * winpos and height may change in win_enter()!
2955 */
2956 if (row >= wp->w_height) /* In (or below) status line */
2957 return IN_STATUS_LINE;
2958#ifdef FEAT_VERTSPLIT
2959 if (col >= wp->w_width) /* In vertical separator line */
2960 return IN_SEP_LINE;
2961#endif
2962
2963 if (wp != curwin)
2964 return IN_UNKNOWN;
2965
2966 /* compute the position in the buffer line from the posn on the screen */
2967 if (mouse_comp_pos(curwin, &row, &col, &mpos->lnum))
2968 return IN_STATUS_LINE; /* past bottom */
2969
2970 mpos->col = vcol2col(wp, mpos->lnum, col);
2971
2972 if (mpos->col > 0)
2973 --mpos->col;
2974 return IN_BUFFER;
2975}
2976
2977/*
2978 * Convert a virtual (screen) column to a character column.
2979 * The first column is one.
2980 */
2981 int
2982vcol2col(wp, lnum, vcol)
2983 win_T *wp;
2984 linenr_T lnum;
2985 int vcol;
2986{
2987 /* try to advance to the specified column */
2988 int col = 0;
2989 int count = 0;
2990 char_u *ptr;
2991
2992 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
2993 while (count <= vcol && *ptr != NUL)
2994 {
2995 ++col;
2996 count += win_lbr_chartabsize(wp, ptr, count, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002997 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 }
2999 return col;
3000}
3001#endif
3002
3003#endif /* FEAT_MOUSE */
3004
3005#if defined(FEAT_GUI) || defined(WIN3264) || defined(PROTO)
3006/*
3007 * Called when focus changed. Used for the GUI or for systems where this can
3008 * be done in the console (Win32).
3009 */
3010 void
3011ui_focus_change(in_focus)
3012 int in_focus; /* TRUE if focus gained. */
3013{
3014 static time_t last_time = (time_t)0;
3015 int need_redraw = FALSE;
3016
3017 /* When activated: Check if any file was modified outside of Vim.
3018 * Only do this when not done within the last two seconds (could get
3019 * several events in a row). */
3020 if (in_focus && last_time + 2 < time(NULL))
3021 {
3022 need_redraw = check_timestamps(
3023# ifdef FEAT_GUI
3024 gui.in_use
3025# else
3026 FALSE
3027# endif
3028 );
3029 last_time = time(NULL);
3030 }
3031
3032#ifdef FEAT_AUTOCMD
3033 /*
3034 * Fire the focus gained/lost autocommand.
3035 */
3036 need_redraw |= apply_autocmds(in_focus ? EVENT_FOCUSGAINED
3037 : EVENT_FOCUSLOST, NULL, NULL, FALSE, curbuf);
3038#endif
3039
3040 if (need_redraw)
3041 {
3042 /* Something was executed, make sure the cursor is put back where it
3043 * belongs. */
3044 need_wait_return = FALSE;
3045
3046 if (State & CMDLINE)
3047 redrawcmdline();
3048 else if (State == HITRETURN || State == SETWSIZE || State == ASKMORE
3049 || State == EXTERNCMD || State == CONFIRM || exmode_active)
3050 repeat_message();
3051 else if ((State & NORMAL) || (State & INSERT))
3052 {
3053 if (must_redraw != 0)
3054 update_screen(0);
3055 setcursor();
3056 }
3057 cursor_on(); /* redrawing may have switched it off */
3058 out_flush();
3059# ifdef FEAT_GUI
3060 if (gui.in_use)
3061 {
3062 gui_update_cursor(FALSE, TRUE);
3063 gui_update_scrollbars(FALSE);
3064 }
3065# endif
3066 }
3067#ifdef FEAT_TITLE
3068 /* File may have been changed from 'readonly' to 'noreadonly' */
3069 if (need_maketitle)
3070 maketitle();
3071#endif
3072}
3073#endif
3074
3075#if defined(USE_IM_CONTROL) || defined(PROTO)
3076/*
3077 * Save current Input Method status to specified place.
3078 */
3079 void
3080im_save_status(psave)
3081 long *psave;
3082{
3083 /* Don't save when 'imdisable' is set or "xic" is NULL, IM is always
3084 * disabled then (but might start later).
3085 * Also don't save when inside a mapping, vgetc_im_active has not been set
3086 * then.
3087 * And don't save when the keys were stuffed (e.g., for a "." command).
3088 * And don't save when the GUI is running but our window doesn't have
3089 * input focus (e.g., when a find dialog is open). */
3090 if (!p_imdisable && KeyTyped && !KeyStuffed
3091# ifdef FEAT_XIM
3092 && xic != NULL
3093# endif
3094# ifdef FEAT_GUI
3095 && (!gui.in_use || gui.in_focus)
3096# endif
3097 )
3098 {
3099 /* Do save when IM is on, or IM is off and saved status is on. */
3100 if (vgetc_im_active)
3101 *psave = B_IMODE_IM;
3102 else if (*psave == B_IMODE_IM)
3103 *psave = B_IMODE_NONE;
3104 }
3105}
3106#endif