Merge branch 'vim'
[MacVim.git] / src / ui.c
blob903645fc44af8ada1a6d116dc24c101c19becac3
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
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 */
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.
19 #include "vim.h"
21 void
22 ui_write(s, len)
23 char_u *s;
24 int len;
26 #ifdef FEAT_GUI
27 if (gui.in_use && !gui.dying && !gui.starting)
29 gui_write(s, len);
30 if (p_wd)
31 gui_wait_for_chars(p_wd);
32 return;
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))
39 #ifdef FEAT_MBYTE
40 char_u *tofree = NULL;
42 if (output_conv.vc_type != CONV_NONE)
44 /* Convert characters from 'encoding' to 'termencoding'. */
45 tofree = string_convert(&output_conv, s, &len);
46 if (tofree != NULL)
47 s = tofree;
49 #endif
51 mch_write(s, len);
53 #ifdef FEAT_MBYTE
54 if (output_conv.vc_type != CONV_NONE)
55 vim_free(tofree);
56 #endif
58 #endif
61 #if defined(UNIX) || defined(VMS) || defined(PROTO)
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.
67 static char_u *ta_str = NULL;
68 static int ta_off; /* offset for next char to use when ta_str != NULL */
69 static int ta_len; /* length of ta_str when it's not NULL*/
71 void
72 ui_inchar_undo(s, len)
73 char_u *s;
74 int len;
76 char_u *new;
77 int newlen;
79 newlen = len;
80 if (ta_str != NULL)
81 newlen += ta_len - ta_off;
82 new = alloc(newlen);
83 if (new != NULL)
85 if (ta_str != NULL)
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);
91 else
92 mch_memmove(new, s, (size_t)len);
93 ta_str = new;
94 ta_len = newlen;
95 ta_off = 0;
98 #endif
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.
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.
114 ui_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;
120 int retval = 0;
122 #if defined(FEAT_GUI) && (defined(UNIX) || defined(VMS))
124 * Use the typeahead if there is any.
126 if (ta_str != NULL)
128 if (maxlen >= ta_len - ta_off)
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;
135 mch_memmove(buf, ta_str + ta_off, (size_t)maxlen);
136 ta_off += maxlen;
137 return maxlen;
139 #endif
141 #ifdef FEAT_PROFILE
142 if (do_profiling == PROF_YES && wtime != 0)
143 prof_inchar_enter();
144 #endif
146 #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())
153 static int count = 0;
155 # ifndef NO_CONSOLE
156 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)
159 goto theend;
160 # endif
161 if (wtime == -1 && ++count == 1000)
162 read_error_exit();
163 buf[0] = CAR;
164 retval = 1;
165 goto theend;
167 #endif
169 /* If we are going to wait for some time or block... */
170 if (wtime == -1 || wtime > 100L)
172 /* ... allow signals to kill us. */
173 (void)vim_handle_signal(SIGNAL_UNBLOCK);
175 /* ... there is no need for CTRL-C to interrupt something, don't let
176 * it set got_int when it was mapped. */
177 if (mapped_ctrl_c)
178 ctrl_c_interrupts = FALSE;
181 #ifdef FEAT_GUI
182 if (gui.in_use)
184 if (gui_wait_for_chars(wtime) && !typebuf_changed(tb_change_cnt))
185 retval = read_from_input_buf(buf, (long)maxlen);
187 #endif
188 #ifndef NO_CONSOLE
189 # ifdef FEAT_GUI
190 else
191 # endif
193 retval = mch_inchar(buf, maxlen, wtime, tb_change_cnt);
195 #endif
197 if (wtime == -1 || wtime > 100L)
198 /* block SIGHUP et al. */
199 (void)vim_handle_signal(SIGNAL_BLOCK);
201 ctrl_c_interrupts = TRUE;
203 #ifdef NO_CONSOLE_INPUT
204 theend:
205 #endif
206 #ifdef FEAT_PROFILE
207 if (do_profiling == PROF_YES && wtime != 0)
208 prof_inchar_exit();
209 #endif
210 return retval;
214 * return non-zero if a character is available
217 ui_char_avail()
219 #ifdef FEAT_GUI
220 if (gui.in_use)
222 gui_mch_update();
223 return input_available();
225 #endif
226 #ifndef NO_CONSOLE
227 # ifdef NO_CONSOLE_INPUT
228 if (no_console_input())
229 return 0;
230 # endif
231 return mch_char_avail();
232 #else
233 return 0;
234 #endif
238 * Delay for the given number of milliseconds. If ignoreinput is FALSE then we
239 * cancel the delay if a key is hit.
241 void
242 ui_delay(msec, ignoreinput)
243 long msec;
244 int ignoreinput;
246 #ifdef FEAT_GUI
247 if (gui.in_use && !ignoreinput)
248 gui_wait_for_chars(msec);
249 else
250 #endif
252 #if defined(FEAT_GUI_MACVIM)
253 /* MacVim tries to be conservative with flushing, but when Vim takes a
254 * nap it really must flush (else timed error messages might not appear
255 * etc.). Note that gui_mch_wait_for_chars() already does force a
256 * flush, so only do it here. */
257 if (gui.in_use)
258 gui_macvim_force_flush();
259 #endif
260 mch_delay(msec, ignoreinput);
265 * If the machine has job control, use it to suspend the program,
266 * otherwise fake it by starting a new shell.
267 * When running the GUI iconify the window.
269 void
270 ui_suspend()
272 #ifdef FEAT_GUI
273 if (gui.in_use)
275 gui_mch_iconify();
276 return;
278 #endif
279 mch_suspend();
282 #if !defined(UNIX) || !defined(SIGTSTP) || defined(PROTO) || defined(__BEOS__)
284 * When the OS can't really suspend, call this function to start a shell.
285 * This is never called in the GUI.
287 void
288 suspend_shell()
290 if (*p_sh == NUL)
291 EMSG(_(e_shellempty));
292 else
294 MSG_PUTS(_("new shell started\n"));
295 do_shell(NULL, 0);
298 #endif
301 * Try to get the current Vim shell size. Put the result in Rows and Columns.
302 * Use the new sizes as defaults for 'columns' and 'lines'.
303 * Return OK when size could be determined, FAIL otherwise.
306 ui_get_shellsize()
308 int retval;
310 #ifdef FEAT_GUI
311 if (gui.in_use
312 # ifdef FEAT_GUI_MACVIM
313 /* Avoid using terminal dimensions for GUI window. MacVim
314 * autosaves the dimensions of the first window. */
315 || gui.starting
316 # endif
318 retval = gui_get_shellsize();
319 else
320 #endif
321 retval = mch_get_shellsize();
323 check_shellsize();
325 /* adjust the default for 'lines' and 'columns' */
326 if (retval == OK)
328 set_number_default("lines", Rows);
329 set_number_default("columns", Columns);
331 return retval;
335 * Set the size of the Vim shell according to Rows and Columns, if possible.
336 * The gui_set_shellsize() or mch_set_shellsize() function will try to set the
337 * new size. If this is not possible, it will adjust Rows and Columns.
339 void
340 ui_set_shellsize(mustset)
341 int mustset UNUSED; /* set by the user */
343 #ifdef FEAT_GUI
344 if (gui.in_use)
345 gui_set_shellsize(mustset,
346 # ifdef WIN3264
347 TRUE
348 # else
349 FALSE
350 # endif
351 , RESIZE_BOTH);
352 else
353 #endif
354 mch_set_shellsize();
358 * Called when Rows and/or Columns changed. Adjust scroll region and mouse
359 * region.
361 void
362 ui_new_shellsize()
364 if (full_screen && !exiting)
366 #ifdef FEAT_GUI
367 if (gui.in_use)
368 gui_new_shellsize();
369 else
370 #endif
371 mch_new_shellsize();
375 void
376 ui_breakcheck()
378 #ifdef FEAT_GUI
379 if (gui.in_use)
380 gui_mch_update();
381 else
382 #endif
383 mch_breakcheck();
386 /*****************************************************************************
387 * Functions for copying and pasting text between applications.
388 * This is always included in a GUI version, but may also be included when the
389 * clipboard and mouse is available to a terminal version such as xterm.
390 * Note: there are some more functions in ops.c that handle selection stuff.
392 * Also note that the majority of functions here deal with the X 'primary'
393 * (visible - for Visual mode use) selection, and only that. There are no
394 * versions of these for the 'clipboard' selection, as Visual mode has no use
395 * for them.
398 #if defined(FEAT_CLIPBOARD) || defined(PROTO)
401 * Selection stuff using Visual mode, for cutting and pasting text to other
402 * windows.
406 * Call this to initialise the clipboard. Pass it FALSE if the clipboard code
407 * is included, but the clipboard can not be used, or TRUE if the clipboard can
408 * be used. Eg unix may call this with FALSE, then call it again with TRUE if
409 * the GUI starts.
411 void
412 clip_init(can_use)
413 int can_use;
415 VimClipboard *cb;
417 cb = &clip_star;
418 for (;;)
420 cb->available = can_use;
421 cb->owned = FALSE;
422 cb->start.lnum = 0;
423 cb->start.col = 0;
424 cb->end.lnum = 0;
425 cb->end.col = 0;
426 cb->state = SELECT_CLEARED;
428 if (cb == &clip_plus)
429 break;
430 cb = &clip_plus;
435 * Check whether the VIsual area has changed, and if so try to become the owner
436 * of the selection, and free any old converted selection we may still have
437 * lying around. If the VIsual mode has ended, make a copy of what was
438 * selected so we can still give it to others. Will probably have to make sure
439 * this is called whenever VIsual mode is ended.
441 void
442 clip_update_selection()
444 pos_T start, end;
446 /* If visual mode is only due to a redo command ("."), then ignore it */
447 if (!redo_VIsual_busy && VIsual_active && (State & NORMAL))
449 if (lt(VIsual, curwin->w_cursor))
451 start = VIsual;
452 end = curwin->w_cursor;
453 #ifdef FEAT_MBYTE
454 if (has_mbyte)
455 end.col += (*mb_ptr2len)(ml_get_cursor()) - 1;
456 #endif
458 else
460 start = curwin->w_cursor;
461 end = VIsual;
463 if (!equalpos(clip_star.start, start)
464 || !equalpos(clip_star.end, end)
465 || clip_star.vmode != VIsual_mode)
467 clip_clear_selection();
468 clip_star.start = start;
469 clip_star.end = end;
470 clip_star.vmode = VIsual_mode;
471 clip_free_selection(&clip_star);
472 clip_own_selection(&clip_star);
473 clip_gen_set_selection(&clip_star);
478 void
479 clip_own_selection(cbd)
480 VimClipboard *cbd;
483 * Also want to check somehow that we are reading from the keyboard rather
484 * than a mapping etc.
486 if (!cbd->owned && cbd->available)
488 cbd->owned = (clip_gen_own_selection(cbd) == OK);
489 #ifdef FEAT_X11
490 if (cbd == &clip_star)
492 /* May have to show a different kind of highlighting for the
493 * selected area. There is no specific redraw command for this,
494 * just redraw all windows on the current buffer. */
495 if (cbd->owned
496 && (get_real_state() == VISUAL
497 || get_real_state() == SELECTMODE)
498 && clip_isautosel()
499 && hl_attr(HLF_V) != hl_attr(HLF_VNC))
500 redraw_curbuf_later(INVERTED_ALL);
502 #endif
506 void
507 clip_lose_selection(cbd)
508 VimClipboard *cbd;
510 #ifdef FEAT_X11
511 int was_owned = cbd->owned;
512 #endif
513 int visual_selection = (cbd == &clip_star);
515 clip_free_selection(cbd);
516 cbd->owned = FALSE;
517 if (visual_selection)
518 clip_clear_selection();
519 clip_gen_lose_selection(cbd);
520 #ifdef FEAT_X11
521 if (visual_selection)
523 /* May have to show a different kind of highlighting for the selected
524 * area. There is no specific redraw command for this, just redraw all
525 * windows on the current buffer. */
526 if (was_owned
527 && (get_real_state() == VISUAL
528 || get_real_state() == SELECTMODE)
529 && clip_isautosel()
530 && hl_attr(HLF_V) != hl_attr(HLF_VNC))
532 update_curbuf(INVERTED_ALL);
533 setcursor();
534 cursor_on();
535 out_flush();
536 # ifdef FEAT_GUI
537 if (gui.in_use)
538 gui_update_cursor(TRUE, FALSE);
539 # endif
542 #endif
545 void
546 clip_copy_selection()
548 if (VIsual_active && (State & NORMAL) && clip_star.available)
550 if (clip_isautosel())
551 clip_update_selection();
552 clip_free_selection(&clip_star);
553 clip_own_selection(&clip_star);
554 if (clip_star.owned)
555 clip_get_selection(&clip_star);
556 clip_gen_set_selection(&clip_star);
561 * Called when Visual mode is ended: update the selection.
563 void
564 clip_auto_select()
566 if (clip_isautosel())
567 clip_copy_selection();
571 * Return TRUE if automatic selection of Visual area is desired.
574 clip_isautosel()
576 return (
577 #ifdef FEAT_GUI
578 gui.in_use ? (vim_strchr(p_go, GO_ASEL) != NULL) :
579 #endif
580 clip_autoselect);
585 * Stuff for general mouse selection, without using Visual mode.
588 static int clip_compare_pos __ARGS((int row1, int col1, int row2, int col2));
589 static void clip_invert_area __ARGS((int, int, int, int, int how));
590 static void clip_invert_rectangle __ARGS((int row, int col, int height, int width, int invert));
591 static void clip_get_word_boundaries __ARGS((VimClipboard *, int, int));
592 static int clip_get_line_end __ARGS((int));
593 static void clip_update_modeless_selection __ARGS((VimClipboard *, int, int,
594 int, int));
596 /* flags for clip_invert_area() */
597 #define CLIP_CLEAR 1
598 #define CLIP_SET 2
599 #define CLIP_TOGGLE 3
602 * Start, continue or end a modeless selection. Used when editing the
603 * command-line and in the cmdline window.
605 void
606 clip_modeless(button, is_click, is_drag)
607 int button;
608 int is_click;
609 int is_drag;
611 int repeat;
613 repeat = ((clip_star.mode == SELECT_MODE_CHAR
614 || clip_star.mode == SELECT_MODE_LINE)
615 && (mod_mask & MOD_MASK_2CLICK))
616 || (clip_star.mode == SELECT_MODE_WORD
617 && (mod_mask & MOD_MASK_3CLICK));
618 if (is_click && button == MOUSE_RIGHT)
620 /* Right mouse button: If there was no selection, start one.
621 * Otherwise extend the existing selection. */
622 if (clip_star.state == SELECT_CLEARED)
623 clip_start_selection(mouse_col, mouse_row, FALSE);
624 clip_process_selection(button, mouse_col, mouse_row, repeat);
626 else if (is_click)
627 clip_start_selection(mouse_col, mouse_row, repeat);
628 else if (is_drag)
630 /* Don't try extending a selection if there isn't one. Happens when
631 * button-down is in the cmdline and them moving mouse upwards. */
632 if (clip_star.state != SELECT_CLEARED)
633 clip_process_selection(button, mouse_col, mouse_row, repeat);
635 else /* release */
636 clip_process_selection(MOUSE_RELEASE, mouse_col, mouse_row, FALSE);
640 * Compare two screen positions ala strcmp()
642 static int
643 clip_compare_pos(row1, col1, row2, col2)
644 int row1;
645 int col1;
646 int row2;
647 int col2;
649 if (row1 > row2) return(1);
650 if (row1 < row2) return(-1);
651 if (col1 > col2) return(1);
652 if (col1 < col2) return(-1);
653 return(0);
657 * Start the selection
659 void
660 clip_start_selection(col, row, repeated_click)
661 int col;
662 int row;
663 int repeated_click;
665 VimClipboard *cb = &clip_star;
667 if (cb->state == SELECT_DONE)
668 clip_clear_selection();
670 row = check_row(row);
671 col = check_col(col);
672 #ifdef FEAT_MBYTE
673 col = mb_fix_col(col, row);
674 #endif
676 cb->start.lnum = row;
677 cb->start.col = col;
678 cb->end = cb->start;
679 cb->origin_row = (short_u)cb->start.lnum;
680 cb->state = SELECT_IN_PROGRESS;
682 if (repeated_click)
684 if (++cb->mode > SELECT_MODE_LINE)
685 cb->mode = SELECT_MODE_CHAR;
687 else
688 cb->mode = SELECT_MODE_CHAR;
690 #ifdef FEAT_GUI
691 /* clear the cursor until the selection is made */
692 if (gui.in_use)
693 gui_undraw_cursor();
694 #endif
696 switch (cb->mode)
698 case SELECT_MODE_CHAR:
699 cb->origin_start_col = cb->start.col;
700 cb->word_end_col = clip_get_line_end((int)cb->start.lnum);
701 break;
703 case SELECT_MODE_WORD:
704 clip_get_word_boundaries(cb, (int)cb->start.lnum, cb->start.col);
705 cb->origin_start_col = cb->word_start_col;
706 cb->origin_end_col = cb->word_end_col;
708 clip_invert_area((int)cb->start.lnum, cb->word_start_col,
709 (int)cb->end.lnum, cb->word_end_col, CLIP_SET);
710 cb->start.col = cb->word_start_col;
711 cb->end.col = cb->word_end_col;
712 break;
714 case SELECT_MODE_LINE:
715 clip_invert_area((int)cb->start.lnum, 0, (int)cb->start.lnum,
716 (int)Columns, CLIP_SET);
717 cb->start.col = 0;
718 cb->end.col = Columns;
719 break;
722 cb->prev = cb->start;
724 #ifdef DEBUG_SELECTION
725 printf("Selection started at (%u,%u)\n", cb->start.lnum, cb->start.col);
726 #endif
730 * Continue processing the selection
732 void
733 clip_process_selection(button, col, row, repeated_click)
734 int button;
735 int col;
736 int row;
737 int_u repeated_click;
739 VimClipboard *cb = &clip_star;
740 int diff;
741 int slen = 1; /* cursor shape width */
743 if (button == MOUSE_RELEASE)
745 /* Check to make sure we have something selected */
746 if (cb->start.lnum == cb->end.lnum && cb->start.col == cb->end.col)
748 #ifdef FEAT_GUI
749 if (gui.in_use)
750 gui_update_cursor(FALSE, FALSE);
751 #endif
752 cb->state = SELECT_CLEARED;
753 return;
756 #ifdef DEBUG_SELECTION
757 printf("Selection ended: (%u,%u) to (%u,%u)\n", cb->start.lnum,
758 cb->start.col, cb->end.lnum, cb->end.col);
759 #endif
760 if (clip_isautosel()
761 || (
762 #ifdef FEAT_GUI
763 gui.in_use ? (vim_strchr(p_go, GO_ASELML) != NULL) :
764 #endif
765 clip_autoselectml))
766 clip_copy_modeless_selection(FALSE);
767 #ifdef FEAT_GUI
768 if (gui.in_use)
769 gui_update_cursor(FALSE, FALSE);
770 #endif
772 cb->state = SELECT_DONE;
773 return;
776 row = check_row(row);
777 col = check_col(col);
778 #ifdef FEAT_MBYTE
779 col = mb_fix_col(col, row);
780 #endif
782 if (col == (int)cb->prev.col && row == cb->prev.lnum && !repeated_click)
783 return;
786 * When extending the selection with the right mouse button, swap the
787 * start and end if the position is before half the selection
789 if (cb->state == SELECT_DONE && button == MOUSE_RIGHT)
792 * If the click is before the start, or the click is inside the
793 * selection and the start is the closest side, set the origin to the
794 * end of the selection.
796 if (clip_compare_pos(row, col, (int)cb->start.lnum, cb->start.col) < 0
797 || (clip_compare_pos(row, col,
798 (int)cb->end.lnum, cb->end.col) < 0
799 && (((cb->start.lnum == cb->end.lnum
800 && cb->end.col - col > col - cb->start.col))
801 || ((diff = (cb->end.lnum - row) -
802 (row - cb->start.lnum)) > 0
803 || (diff == 0 && col < (int)(cb->start.col +
804 cb->end.col) / 2)))))
806 cb->origin_row = (short_u)cb->end.lnum;
807 cb->origin_start_col = cb->end.col - 1;
808 cb->origin_end_col = cb->end.col;
810 else
812 cb->origin_row = (short_u)cb->start.lnum;
813 cb->origin_start_col = cb->start.col;
814 cb->origin_end_col = cb->start.col;
816 if (cb->mode == SELECT_MODE_WORD && !repeated_click)
817 cb->mode = SELECT_MODE_CHAR;
820 /* set state, for when using the right mouse button */
821 cb->state = SELECT_IN_PROGRESS;
823 #ifdef DEBUG_SELECTION
824 printf("Selection extending to (%d,%d)\n", row, col);
825 #endif
827 if (repeated_click && ++cb->mode > SELECT_MODE_LINE)
828 cb->mode = SELECT_MODE_CHAR;
830 switch (cb->mode)
832 case SELECT_MODE_CHAR:
833 /* If we're on a different line, find where the line ends */
834 if (row != cb->prev.lnum)
835 cb->word_end_col = clip_get_line_end(row);
837 /* See if we are before or after the origin of the selection */
838 if (clip_compare_pos(row, col, cb->origin_row,
839 cb->origin_start_col) >= 0)
841 if (col >= (int)cb->word_end_col)
842 clip_update_modeless_selection(cb, cb->origin_row,
843 cb->origin_start_col, row, (int)Columns);
844 else
846 #ifdef FEAT_MBYTE
847 if (has_mbyte && mb_lefthalve(row, col))
848 slen = 2;
849 #endif
850 clip_update_modeless_selection(cb, cb->origin_row,
851 cb->origin_start_col, row, col + slen);
854 else
856 #ifdef FEAT_MBYTE
857 if (has_mbyte
858 && mb_lefthalve(cb->origin_row, cb->origin_start_col))
859 slen = 2;
860 #endif
861 if (col >= (int)cb->word_end_col)
862 clip_update_modeless_selection(cb, row, cb->word_end_col,
863 cb->origin_row, cb->origin_start_col + slen);
864 else
865 clip_update_modeless_selection(cb, row, col,
866 cb->origin_row, cb->origin_start_col + slen);
868 break;
870 case SELECT_MODE_WORD:
871 /* If we are still within the same word, do nothing */
872 if (row == cb->prev.lnum && col >= (int)cb->word_start_col
873 && col < (int)cb->word_end_col && !repeated_click)
874 return;
876 /* Get new word boundaries */
877 clip_get_word_boundaries(cb, row, col);
879 /* Handle being after the origin point of selection */
880 if (clip_compare_pos(row, col, cb->origin_row,
881 cb->origin_start_col) >= 0)
882 clip_update_modeless_selection(cb, cb->origin_row,
883 cb->origin_start_col, row, cb->word_end_col);
884 else
885 clip_update_modeless_selection(cb, row, cb->word_start_col,
886 cb->origin_row, cb->origin_end_col);
887 break;
889 case SELECT_MODE_LINE:
890 if (row == cb->prev.lnum && !repeated_click)
891 return;
893 if (clip_compare_pos(row, col, cb->origin_row,
894 cb->origin_start_col) >= 0)
895 clip_update_modeless_selection(cb, cb->origin_row, 0, row,
896 (int)Columns);
897 else
898 clip_update_modeless_selection(cb, row, 0, cb->origin_row,
899 (int)Columns);
900 break;
903 cb->prev.lnum = row;
904 cb->prev.col = col;
906 #ifdef DEBUG_SELECTION
907 printf("Selection is: (%u,%u) to (%u,%u)\n", cb->start.lnum,
908 cb->start.col, cb->end.lnum, cb->end.col);
909 #endif
912 #if 0 /* not used */
914 * Called after an Expose event to redraw the selection
916 void
917 clip_redraw_selection(x, y, w, h)
918 int x;
919 int y;
920 int w;
921 int h;
923 VimClipboard *cb = &clip_star;
924 int row1, col1, row2, col2;
925 int row;
926 int start;
927 int end;
929 if (cb->state == SELECT_CLEARED)
930 return;
932 row1 = check_row(Y_2_ROW(y));
933 col1 = check_col(X_2_COL(x));
934 row2 = check_row(Y_2_ROW(y + h - 1));
935 col2 = check_col(X_2_COL(x + w - 1));
937 /* Limit the rows that need to be re-drawn */
938 if (cb->start.lnum > row1)
939 row1 = cb->start.lnum;
940 if (cb->end.lnum < row2)
941 row2 = cb->end.lnum;
943 /* Look at each row that might need to be re-drawn */
944 for (row = row1; row <= row2; row++)
946 /* For the first selection row, use the starting selection column */
947 if (row == cb->start.lnum)
948 start = cb->start.col;
949 else
950 start = 0;
952 /* For the last selection row, use the ending selection column */
953 if (row == cb->end.lnum)
954 end = cb->end.col;
955 else
956 end = Columns;
958 if (col1 > start)
959 start = col1;
961 if (col2 < end)
962 end = col2 + 1;
964 if (end > start)
965 gui_mch_invert_rectangle(row, start, 1, end - start);
968 #endif
970 # if defined(FEAT_GUI) || defined(PROTO)
972 * Redraw part of the selection if character at "row,col" is inside of it.
973 * Only used for the GUI.
975 void
976 clip_may_redraw_selection(row, col, len)
977 int row, col;
978 int len;
980 int start = col;
981 int end = col + len;
983 if (clip_star.state != SELECT_CLEARED
984 && row >= clip_star.start.lnum
985 && row <= clip_star.end.lnum)
987 if (row == clip_star.start.lnum && start < (int)clip_star.start.col)
988 start = clip_star.start.col;
989 if (row == clip_star.end.lnum && end > (int)clip_star.end.col)
990 end = clip_star.end.col;
991 if (end > start)
992 clip_invert_area(row, start, row, end, 0);
995 # endif
998 * Called from outside to clear selected region from the display
1000 void
1001 clip_clear_selection()
1003 VimClipboard *cb = &clip_star;
1005 if (cb->state == SELECT_CLEARED)
1006 return;
1008 clip_invert_area((int)cb->start.lnum, cb->start.col, (int)cb->end.lnum,
1009 cb->end.col, CLIP_CLEAR);
1010 cb->state = SELECT_CLEARED;
1014 * Clear the selection if any lines from "row1" to "row2" are inside of it.
1016 void
1017 clip_may_clear_selection(row1, row2)
1018 int row1, row2;
1020 if (clip_star.state == SELECT_DONE
1021 && row2 >= clip_star.start.lnum
1022 && row1 <= clip_star.end.lnum)
1023 clip_clear_selection();
1027 * Called before the screen is scrolled up or down. Adjusts the line numbers
1028 * of the selection. Call with big number when clearing the screen.
1030 void
1031 clip_scroll_selection(rows)
1032 int rows; /* negative for scroll down */
1034 int lnum;
1036 if (clip_star.state == SELECT_CLEARED)
1037 return;
1039 lnum = clip_star.start.lnum - rows;
1040 if (lnum <= 0)
1041 clip_star.start.lnum = 0;
1042 else if (lnum >= screen_Rows) /* scrolled off of the screen */
1043 clip_star.state = SELECT_CLEARED;
1044 else
1045 clip_star.start.lnum = lnum;
1047 lnum = clip_star.end.lnum - rows;
1048 if (lnum < 0) /* scrolled off of the screen */
1049 clip_star.state = SELECT_CLEARED;
1050 else if (lnum >= screen_Rows)
1051 clip_star.end.lnum = screen_Rows - 1;
1052 else
1053 clip_star.end.lnum = lnum;
1057 * Invert a region of the display between a starting and ending row and column
1058 * Values for "how":
1059 * CLIP_CLEAR: undo inversion
1060 * CLIP_SET: set inversion
1061 * CLIP_TOGGLE: set inversion if pos1 < pos2, undo inversion otherwise.
1062 * 0: invert (GUI only).
1064 static void
1065 clip_invert_area(row1, col1, row2, col2, how)
1066 int row1;
1067 int col1;
1068 int row2;
1069 int col2;
1070 int how;
1072 int invert = FALSE;
1074 if (how == CLIP_SET)
1075 invert = TRUE;
1077 /* Swap the from and to positions so the from is always before */
1078 if (clip_compare_pos(row1, col1, row2, col2) > 0)
1080 int tmp_row, tmp_col;
1082 tmp_row = row1;
1083 tmp_col = col1;
1084 row1 = row2;
1085 col1 = col2;
1086 row2 = tmp_row;
1087 col2 = tmp_col;
1089 else if (how == CLIP_TOGGLE)
1090 invert = TRUE;
1092 /* If all on the same line, do it the easy way */
1093 if (row1 == row2)
1095 clip_invert_rectangle(row1, col1, 1, col2 - col1, invert);
1097 else
1099 /* Handle a piece of the first line */
1100 if (col1 > 0)
1102 clip_invert_rectangle(row1, col1, 1, (int)Columns - col1, invert);
1103 row1++;
1106 /* Handle a piece of the last line */
1107 if (col2 < Columns - 1)
1109 clip_invert_rectangle(row2, 0, 1, col2, invert);
1110 row2--;
1113 /* Handle the rectangle thats left */
1114 if (row2 >= row1)
1115 clip_invert_rectangle(row1, 0, row2 - row1 + 1, (int)Columns,
1116 invert);
1121 * Invert or un-invert a rectangle of the screen.
1122 * "invert" is true if the result is inverted.
1124 static void
1125 clip_invert_rectangle(row, col, height, width, invert)
1126 int row;
1127 int col;
1128 int height;
1129 int width;
1130 int invert;
1132 #ifdef FEAT_GUI
1133 if (gui.in_use)
1134 # ifdef FEAT_GUI_MACVIM
1135 gui_mch_invert_rectangle(row, col, height, width, invert);
1136 # else
1137 gui_mch_invert_rectangle(row, col, height, width);
1138 #endif
1139 else
1140 #endif
1141 screen_draw_rectangle(row, col, height, width, invert);
1145 * Copy the currently selected area into the '*' register so it will be
1146 * available for pasting.
1147 * When "both" is TRUE also copy to the '+' register.
1149 void
1150 clip_copy_modeless_selection(both)
1151 int both UNUSED;
1153 char_u *buffer;
1154 char_u *bufp;
1155 int row;
1156 int start_col;
1157 int end_col;
1158 int line_end_col;
1159 int add_newline_flag = FALSE;
1160 int len;
1161 #ifdef FEAT_MBYTE
1162 char_u *p;
1163 #endif
1164 int row1 = clip_star.start.lnum;
1165 int col1 = clip_star.start.col;
1166 int row2 = clip_star.end.lnum;
1167 int col2 = clip_star.end.col;
1169 /* Can't use ScreenLines unless initialized */
1170 if (ScreenLines == NULL)
1171 return;
1174 * Make sure row1 <= row2, and if row1 == row2 that col1 <= col2.
1176 if (row1 > row2)
1178 row = row1; row1 = row2; row2 = row;
1179 row = col1; col1 = col2; col2 = row;
1181 else if (row1 == row2 && col1 > col2)
1183 row = col1; col1 = col2; col2 = row;
1185 #ifdef FEAT_MBYTE
1186 /* correct starting point for being on right halve of double-wide char */
1187 p = ScreenLines + LineOffset[row1];
1188 if (enc_dbcs != 0)
1189 col1 -= (*mb_head_off)(p, p + col1);
1190 else if (enc_utf8 && p[col1] == 0)
1191 --col1;
1192 #endif
1194 /* Create a temporary buffer for storing the text */
1195 len = (row2 - row1 + 1) * Columns + 1;
1196 #ifdef FEAT_MBYTE
1197 if (enc_dbcs != 0)
1198 len *= 2; /* max. 2 bytes per display cell */
1199 else if (enc_utf8)
1200 len *= MB_MAXBYTES;
1201 #endif
1202 buffer = lalloc((long_u)len, TRUE);
1203 if (buffer == NULL) /* out of memory */
1204 return;
1206 /* Process each row in the selection */
1207 for (bufp = buffer, row = row1; row <= row2; row++)
1209 if (row == row1)
1210 start_col = col1;
1211 else
1212 start_col = 0;
1214 if (row == row2)
1215 end_col = col2;
1216 else
1217 end_col = Columns;
1219 line_end_col = clip_get_line_end(row);
1221 /* See if we need to nuke some trailing whitespace */
1222 if (end_col >= Columns && (row < row2 || end_col > line_end_col))
1224 /* Get rid of trailing whitespace */
1225 end_col = line_end_col;
1226 if (end_col < start_col)
1227 end_col = start_col;
1229 /* If the last line extended to the end, add an extra newline */
1230 if (row == row2)
1231 add_newline_flag = TRUE;
1234 /* If after the first row, we need to always add a newline */
1235 if (row > row1 && !LineWraps[row - 1])
1236 *bufp++ = NL;
1238 if (row < screen_Rows && end_col <= screen_Columns)
1240 #ifdef FEAT_MBYTE
1241 if (enc_dbcs != 0)
1243 int i;
1245 p = ScreenLines + LineOffset[row];
1246 for (i = start_col; i < end_col; ++i)
1247 if (enc_dbcs == DBCS_JPNU && p[i] == 0x8e)
1249 /* single-width double-byte char */
1250 *bufp++ = 0x8e;
1251 *bufp++ = ScreenLines2[LineOffset[row] + i];
1253 else
1255 *bufp++ = p[i];
1256 if (MB_BYTE2LEN(p[i]) == 2)
1257 *bufp++ = p[++i];
1260 else if (enc_utf8)
1262 int off;
1263 int i;
1264 int ci;
1266 off = LineOffset[row];
1267 for (i = start_col; i < end_col; ++i)
1269 /* The base character is either in ScreenLinesUC[] or
1270 * ScreenLines[]. */
1271 if (ScreenLinesUC[off + i] == 0)
1272 *bufp++ = ScreenLines[off + i];
1273 else
1275 bufp += utf_char2bytes(ScreenLinesUC[off + i], bufp);
1276 for (ci = 0; ci < Screen_mco; ++ci)
1278 /* Add a composing character. */
1279 if (ScreenLinesC[ci][off + i] == 0)
1280 break;
1281 bufp += utf_char2bytes(ScreenLinesC[ci][off + i],
1282 bufp);
1285 /* Skip right halve of double-wide character. */
1286 if (ScreenLines[off + i + 1] == 0)
1287 ++i;
1290 else
1291 #endif
1293 STRNCPY(bufp, ScreenLines + LineOffset[row] + start_col,
1294 end_col - start_col);
1295 bufp += end_col - start_col;
1300 /* Add a newline at the end if the selection ended there */
1301 if (add_newline_flag)
1302 *bufp++ = NL;
1304 /* First cleanup any old selection and become the owner. */
1305 clip_free_selection(&clip_star);
1306 clip_own_selection(&clip_star);
1308 /* Yank the text into the '*' register. */
1309 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_star);
1311 /* Make the register contents available to the outside world. */
1312 clip_gen_set_selection(&clip_star);
1314 #ifdef FEAT_X11
1315 if (both)
1317 /* Do the same for the '+' register. */
1318 clip_free_selection(&clip_plus);
1319 clip_own_selection(&clip_plus);
1320 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_plus);
1321 clip_gen_set_selection(&clip_plus);
1323 #endif
1324 vim_free(buffer);
1328 * Find the starting and ending positions of the word at the given row and
1329 * column. Only white-separated words are recognized here.
1331 #define CHAR_CLASS(c) (c <= ' ' ? ' ' : vim_iswordc(c))
1333 static void
1334 clip_get_word_boundaries(cb, row, col)
1335 VimClipboard *cb;
1336 int row;
1337 int col;
1339 int start_class;
1340 int temp_col;
1341 char_u *p;
1342 #ifdef FEAT_MBYTE
1343 int mboff;
1344 #endif
1346 if (row >= screen_Rows || col >= screen_Columns || ScreenLines == NULL)
1347 return;
1349 p = ScreenLines + LineOffset[row];
1350 #ifdef FEAT_MBYTE
1351 /* Correct for starting in the right halve of a double-wide char */
1352 if (enc_dbcs != 0)
1353 col -= dbcs_screen_head_off(p, p + col);
1354 else if (enc_utf8 && p[col] == 0)
1355 --col;
1356 #endif
1357 start_class = CHAR_CLASS(p[col]);
1359 temp_col = col;
1360 for ( ; temp_col > 0; temp_col--)
1361 #ifdef FEAT_MBYTE
1362 if (enc_dbcs != 0
1363 && (mboff = dbcs_screen_head_off(p, p + temp_col - 1)) > 0)
1364 temp_col -= mboff;
1365 else
1366 #endif
1367 if (CHAR_CLASS(p[temp_col - 1]) != start_class
1368 #ifdef FEAT_MBYTE
1369 && !(enc_utf8 && p[temp_col - 1] == 0)
1370 #endif
1372 break;
1373 cb->word_start_col = temp_col;
1375 temp_col = col;
1376 for ( ; temp_col < screen_Columns; temp_col++)
1377 #ifdef FEAT_MBYTE
1378 if (enc_dbcs != 0 && dbcs_ptr2cells(p + temp_col) == 2)
1379 ++temp_col;
1380 else
1381 #endif
1382 if (CHAR_CLASS(p[temp_col]) != start_class
1383 #ifdef FEAT_MBYTE
1384 && !(enc_utf8 && p[temp_col] == 0)
1385 #endif
1387 break;
1388 cb->word_end_col = temp_col;
1392 * Find the column position for the last non-whitespace character on the given
1393 * line.
1395 static int
1396 clip_get_line_end(row)
1397 int row;
1399 int i;
1401 if (row >= screen_Rows || ScreenLines == NULL)
1402 return 0;
1403 for (i = screen_Columns; i > 0; i--)
1404 if (ScreenLines[LineOffset[row] + i - 1] != ' ')
1405 break;
1406 return i;
1410 * Update the currently selected region by adding and/or subtracting from the
1411 * beginning or end and inverting the changed area(s).
1413 static void
1414 clip_update_modeless_selection(cb, row1, col1, row2, col2)
1415 VimClipboard *cb;
1416 int row1;
1417 int col1;
1418 int row2;
1419 int col2;
1421 /* See if we changed at the beginning of the selection */
1422 if (row1 != cb->start.lnum || col1 != (int)cb->start.col)
1424 clip_invert_area(row1, col1, (int)cb->start.lnum, cb->start.col,
1425 CLIP_TOGGLE);
1426 cb->start.lnum = row1;
1427 cb->start.col = col1;
1430 /* See if we changed at the end of the selection */
1431 if (row2 != cb->end.lnum || col2 != (int)cb->end.col)
1433 clip_invert_area((int)cb->end.lnum, cb->end.col, row2, col2,
1434 CLIP_TOGGLE);
1435 cb->end.lnum = row2;
1436 cb->end.col = col2;
1441 clip_gen_own_selection(cbd)
1442 VimClipboard *cbd;
1444 #ifdef FEAT_XCLIPBOARD
1445 # ifdef FEAT_GUI
1446 if (gui.in_use)
1447 return clip_mch_own_selection(cbd);
1448 else
1449 # endif
1450 return clip_xterm_own_selection(cbd);
1451 #else
1452 return clip_mch_own_selection(cbd);
1453 #endif
1456 void
1457 clip_gen_lose_selection(cbd)
1458 VimClipboard *cbd;
1460 #ifdef FEAT_XCLIPBOARD
1461 # ifdef FEAT_GUI
1462 if (gui.in_use)
1463 clip_mch_lose_selection(cbd);
1464 else
1465 # endif
1466 clip_xterm_lose_selection(cbd);
1467 #else
1468 clip_mch_lose_selection(cbd);
1469 #endif
1472 void
1473 clip_gen_set_selection(cbd)
1474 VimClipboard *cbd;
1476 #ifdef FEAT_XCLIPBOARD
1477 # ifdef FEAT_GUI
1478 if (gui.in_use)
1479 clip_mch_set_selection(cbd);
1480 else
1481 # endif
1482 clip_xterm_set_selection(cbd);
1483 #else
1484 clip_mch_set_selection(cbd);
1485 #endif
1488 void
1489 clip_gen_request_selection(cbd)
1490 VimClipboard *cbd;
1492 #ifdef FEAT_XCLIPBOARD
1493 # ifdef FEAT_GUI
1494 if (gui.in_use)
1495 clip_mch_request_selection(cbd);
1496 else
1497 # endif
1498 clip_xterm_request_selection(cbd);
1499 #else
1500 clip_mch_request_selection(cbd);
1501 #endif
1504 #endif /* FEAT_CLIPBOARD */
1506 /*****************************************************************************
1507 * Functions that handle the input buffer.
1508 * This is used for any GUI version, and the unix terminal version.
1510 * For Unix, the input characters are buffered to be able to check for a
1511 * CTRL-C. This should be done with signals, but I don't know how to do that
1512 * in a portable way for a tty in RAW mode.
1514 * For the client-server code in the console the received keys are put in the
1515 * input buffer.
1518 #if defined(USE_INPUT_BUF) || defined(PROTO)
1521 * Internal typeahead buffer. Includes extra space for long key code
1522 * descriptions which would otherwise overflow. The buffer is considered full
1523 * when only this extra space (or part of it) remains.
1525 #if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
1526 || defined(FEAT_CLIENTSERVER)
1528 * Sun WorkShop and NetBeans stuff debugger commands into the input buffer.
1529 * This requires a larger buffer...
1530 * (Madsen) Go with this for remote input as well ...
1532 # define INBUFLEN 4096
1533 #else
1534 # define INBUFLEN 250
1535 #endif
1537 static char_u inbuf[INBUFLEN + MAX_KEY_CODE_LEN];
1538 static int inbufcount = 0; /* number of chars in inbuf[] */
1541 * vim_is_input_buf_full(), vim_is_input_buf_empty(), add_to_input_buf(), and
1542 * trash_input_buf() are functions for manipulating the input buffer. These
1543 * are used by the gui_* calls when a GUI is used to handle keyboard input.
1547 vim_is_input_buf_full()
1549 return (inbufcount >= INBUFLEN);
1553 vim_is_input_buf_empty()
1555 return (inbufcount == 0);
1558 #if defined(FEAT_OLE) || defined(PROTO)
1560 vim_free_in_input_buf()
1562 return (INBUFLEN - inbufcount);
1564 #endif
1566 #if defined(FEAT_GUI_GTK) || defined(PROTO)
1568 vim_used_in_input_buf()
1570 return inbufcount;
1572 #endif
1574 #if defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) || defined(PROTO)
1576 * Return the current contents of the input buffer and make it empty.
1577 * The returned pointer must be passed to set_input_buf() later.
1579 char_u *
1580 get_input_buf()
1582 garray_T *gap;
1584 /* We use a growarray to store the data pointer and the length. */
1585 gap = (garray_T *)alloc((unsigned)sizeof(garray_T));
1586 if (gap != NULL)
1588 /* Add one to avoid a zero size. */
1589 gap->ga_data = alloc((unsigned)inbufcount + 1);
1590 if (gap->ga_data != NULL)
1591 mch_memmove(gap->ga_data, inbuf, (size_t)inbufcount);
1592 gap->ga_len = inbufcount;
1594 trash_input_buf();
1595 return (char_u *)gap;
1599 * Restore the input buffer with a pointer returned from get_input_buf().
1600 * The allocated memory is freed, this only works once!
1602 void
1603 set_input_buf(p)
1604 char_u *p;
1606 garray_T *gap = (garray_T *)p;
1608 if (gap != NULL)
1610 if (gap->ga_data != NULL)
1612 mch_memmove(inbuf, gap->ga_data, gap->ga_len);
1613 inbufcount = gap->ga_len;
1614 vim_free(gap->ga_data);
1616 vim_free(gap);
1619 #endif
1621 #if defined(FEAT_GUI) \
1622 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE) \
1623 || defined(FEAT_XCLIPBOARD) || defined(VMS) \
1624 || defined(FEAT_SNIFF) || defined(FEAT_CLIENTSERVER) \
1625 || defined(PROTO)
1627 * Add the given bytes to the input buffer
1628 * Special keys start with CSI. A real CSI must have been translated to
1629 * CSI KS_EXTRA KE_CSI. K_SPECIAL doesn't require translation.
1631 void
1632 add_to_input_buf(s, len)
1633 char_u *s;
1634 int len;
1636 if (inbufcount + len > INBUFLEN + MAX_KEY_CODE_LEN)
1637 return; /* Shouldn't ever happen! */
1639 #ifdef FEAT_HANGULIN
1640 if ((State & (INSERT|CMDLINE)) && hangul_input_state_get())
1641 if ((len = hangul_input_process(s, len)) == 0)
1642 return;
1643 #endif
1645 while (len--)
1646 inbuf[inbufcount++] = *s++;
1648 #endif
1650 #if ((defined(FEAT_XIM) || defined(FEAT_DND)) && defined(FEAT_GUI_GTK)) \
1651 || defined(FEAT_GUI_MSWIN) \
1652 || defined(FEAT_GUI_MAC) \
1653 || (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) \
1654 || (defined(FEAT_GUI) && (!defined(USE_ON_FLY_SCROLL) \
1655 || defined(FEAT_MENU))) \
1656 || defined(PROTO)
1658 * Add "str[len]" to the input buffer while escaping CSI bytes.
1660 void
1661 add_to_input_buf_csi(char_u *str, int len)
1663 int i;
1664 char_u buf[2];
1666 for (i = 0; i < len; ++i)
1668 add_to_input_buf(str + i, 1);
1669 if (str[i] == CSI)
1671 /* Turn CSI into K_CSI. */
1672 buf[0] = KS_EXTRA;
1673 buf[1] = (int)KE_CSI;
1674 add_to_input_buf(buf, 2);
1678 #endif
1680 #if defined(FEAT_HANGULIN) || defined(PROTO)
1681 void
1682 push_raw_key (s, len)
1683 char_u *s;
1684 int len;
1686 while (len--)
1687 inbuf[inbufcount++] = *s++;
1689 #endif
1691 #if defined(FEAT_GUI) || defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) \
1692 || defined(PROTO)
1693 /* Remove everything from the input buffer. Called when ^C is found */
1694 void
1695 trash_input_buf()
1697 inbufcount = 0;
1699 #endif
1702 * Read as much data from the input buffer as possible up to maxlen, and store
1703 * it in buf.
1704 * Note: this function used to be Read() in unix.c
1707 read_from_input_buf(buf, maxlen)
1708 char_u *buf;
1709 long maxlen;
1711 if (inbufcount == 0) /* if the buffer is empty, fill it */
1712 fill_input_buf(TRUE);
1713 if (maxlen > inbufcount)
1714 maxlen = inbufcount;
1715 mch_memmove(buf, inbuf, (size_t)maxlen);
1716 inbufcount -= maxlen;
1717 if (inbufcount)
1718 mch_memmove(inbuf, inbuf + maxlen, (size_t)inbufcount);
1719 return (int)maxlen;
1722 void
1723 fill_input_buf(exit_on_error)
1724 int exit_on_error UNUSED;
1726 #if defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X_UNIX)
1727 int len;
1728 int try;
1729 static int did_read_something = FALSE;
1730 # ifdef FEAT_MBYTE
1731 static char_u *rest = NULL; /* unconverted rest of previous read */
1732 static int restlen = 0;
1733 int unconverted;
1734 # endif
1735 #endif
1737 #ifdef FEAT_GUI
1738 if (gui.in_use
1739 # ifdef NO_CONSOLE_INPUT
1740 /* Don't use the GUI input when the window hasn't been opened yet.
1741 * We get here from ui_inchar() when we should try reading from stdin. */
1742 && !no_console_input()
1743 # endif
1746 gui_mch_update();
1747 return;
1749 #endif
1750 #if defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X_UNIX)
1751 if (vim_is_input_buf_full())
1752 return;
1754 * Fill_input_buf() is only called when we really need a character.
1755 * If we can't get any, but there is some in the buffer, just return.
1756 * If we can't get any, and there isn't any in the buffer, we give up and
1757 * exit Vim.
1759 # ifdef __BEOS__
1761 * On the BeBox version (for now), all input is secretly performed within
1762 * beos_select() which is called from RealWaitForChar().
1764 while (!vim_is_input_buf_full() && RealWaitForChar(read_cmd_fd, 0, NULL))
1766 len = inbufcount;
1767 inbufcount = 0;
1768 # else
1770 # ifdef FEAT_SNIFF
1771 if (sniff_request_waiting)
1773 add_to_input_buf((char_u *)"\233sniff",6); /* results in K_SNIFF */
1774 sniff_request_waiting = 0;
1775 want_sniff_request = 0;
1776 return;
1778 # endif
1780 # ifdef FEAT_MBYTE
1781 if (rest != NULL)
1783 /* Use remainder of previous call, starts with an invalid character
1784 * that may become valid when reading more. */
1785 if (restlen > INBUFLEN - inbufcount)
1786 unconverted = INBUFLEN - inbufcount;
1787 else
1788 unconverted = restlen;
1789 mch_memmove(inbuf + inbufcount, rest, unconverted);
1790 if (unconverted == restlen)
1792 vim_free(rest);
1793 rest = NULL;
1795 else
1797 restlen -= unconverted;
1798 mch_memmove(rest, rest + unconverted, restlen);
1800 inbufcount += unconverted;
1802 else
1803 unconverted = 0;
1804 #endif
1806 len = 0; /* to avoid gcc warning */
1807 for (try = 0; try < 100; ++try)
1809 # ifdef VMS
1810 len = vms_read(
1811 # else
1812 len = read(read_cmd_fd,
1813 # endif
1814 (char *)inbuf + inbufcount, (size_t)((INBUFLEN - inbufcount)
1815 # ifdef FEAT_MBYTE
1816 / input_conv.vc_factor
1817 # endif
1819 # if 0
1820 ) /* avoid syntax highlight error */
1821 # endif
1823 if (len > 0 || got_int)
1824 break;
1826 * If reading stdin results in an error, continue reading stderr.
1827 * This helps when using "foo | xargs vim".
1829 if (!did_read_something && !isatty(read_cmd_fd) && read_cmd_fd == 0)
1831 int m = cur_tmode;
1833 /* We probably set the wrong file descriptor to raw mode. Switch
1834 * back to cooked mode, use another descriptor and set the mode to
1835 * what it was. */
1836 settmode(TMODE_COOK);
1837 #ifdef HAVE_DUP
1838 /* Use stderr for stdin, also works for shell commands. */
1839 close(0);
1840 ignored = dup(2);
1841 #else
1842 read_cmd_fd = 2; /* read from stderr instead of stdin */
1843 #endif
1844 settmode(m);
1846 if (!exit_on_error)
1847 return;
1849 # endif
1850 if (len <= 0 && !got_int)
1851 read_error_exit();
1852 if (len > 0)
1853 did_read_something = TRUE;
1854 if (got_int)
1856 /* Interrupted, pretend a CTRL-C was typed. */
1857 inbuf[0] = 3;
1858 inbufcount = 1;
1860 else
1862 # ifdef FEAT_MBYTE
1864 * May perform conversion on the input characters.
1865 * Include the unconverted rest of the previous call.
1866 * If there is an incomplete char at the end it is kept for the next
1867 * time, reading more bytes should make conversion possible.
1868 * Don't do this in the unlikely event that the input buffer is too
1869 * small ("rest" still contains more bytes).
1871 if (input_conv.vc_type != CONV_NONE)
1873 inbufcount -= unconverted;
1874 len = convert_input_safe(inbuf + inbufcount,
1875 len + unconverted, INBUFLEN - inbufcount,
1876 rest == NULL ? &rest : NULL, &restlen);
1878 # endif
1879 while (len-- > 0)
1882 * if a CTRL-C was typed, remove it from the buffer and set got_int
1884 if (inbuf[inbufcount] == 3 && ctrl_c_interrupts)
1886 /* remove everything typed before the CTRL-C */
1887 mch_memmove(inbuf, inbuf + inbufcount, (size_t)(len + 1));
1888 inbufcount = 0;
1889 got_int = TRUE;
1891 ++inbufcount;
1894 #endif /* UNIX or OS2 or VMS*/
1896 #endif /* defined(UNIX) || defined(FEAT_GUI) || defined(OS2) || defined(VMS) */
1899 * Exit because of an input read error.
1901 void
1902 read_error_exit()
1904 if (silent_mode) /* Normal way to exit for "ex -s" */
1905 getout(0);
1906 STRCPY(IObuff, _("Vim: Error reading input, exiting...\n"));
1907 preserve_exit();
1910 #if defined(CURSOR_SHAPE) || defined(PROTO)
1912 * May update the shape of the cursor.
1914 void
1915 ui_cursor_shape()
1917 # ifdef FEAT_GUI
1918 if (gui.in_use)
1919 gui_update_cursor_later();
1920 else
1921 # endif
1922 term_cursor_shape();
1924 # ifdef MCH_CURSOR_SHAPE
1925 mch_update_cursor();
1926 # endif
1928 #endif
1930 #if defined(FEAT_CLIPBOARD) || defined(FEAT_GUI) || defined(FEAT_RIGHTLEFT) \
1931 || defined(FEAT_MBYTE) || defined(PROTO)
1933 * Check bounds for column number
1936 check_col(col)
1937 int col;
1939 if (col < 0)
1940 return 0;
1941 if (col >= (int)screen_Columns)
1942 return (int)screen_Columns - 1;
1943 return col;
1947 * Check bounds for row number
1950 check_row(row)
1951 int row;
1953 if (row < 0)
1954 return 0;
1955 if (row >= (int)screen_Rows)
1956 return (int)screen_Rows - 1;
1957 return row;
1959 #endif
1962 * Stuff for the X clipboard. Shared between VMS and Unix.
1965 #if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) || defined(PROTO)
1966 # include <X11/Xatom.h>
1967 # include <X11/Intrinsic.h>
1970 * Open the application context (if it hasn't been opened yet).
1971 * Used for Motif and Athena GUI and the xterm clipboard.
1973 void
1974 open_app_context()
1976 if (app_context == NULL)
1978 XtToolkitInitialize();
1979 app_context = XtCreateApplicationContext();
1983 static Atom vim_atom; /* Vim's own special selection format */
1984 #ifdef FEAT_MBYTE
1985 static Atom vimenc_atom; /* Vim's extended selection format */
1986 #endif
1987 static Atom compound_text_atom;
1988 static Atom text_atom;
1989 static Atom targets_atom;
1991 void
1992 x11_setup_atoms(dpy)
1993 Display *dpy;
1995 vim_atom = XInternAtom(dpy, VIM_ATOM_NAME, False);
1996 #ifdef FEAT_MBYTE
1997 vimenc_atom = XInternAtom(dpy, VIMENC_ATOM_NAME,False);
1998 #endif
1999 compound_text_atom = XInternAtom(dpy, "COMPOUND_TEXT", False);
2000 text_atom = XInternAtom(dpy, "TEXT", False);
2001 targets_atom = XInternAtom(dpy, "TARGETS", False);
2002 clip_star.sel_atom = XA_PRIMARY;
2003 clip_plus.sel_atom = XInternAtom(dpy, "CLIPBOARD", False);
2007 * X Selection stuff, for cutting and pasting text to other windows.
2010 static void clip_x11_request_selection_cb __ARGS((Widget, XtPointer, Atom *, Atom *, XtPointer, long_u *, int *));
2012 static void
2013 clip_x11_request_selection_cb(w, success, sel_atom, type, value, length,
2014 format)
2015 Widget w UNUSED;
2016 XtPointer success;
2017 Atom *sel_atom;
2018 Atom *type;
2019 XtPointer value;
2020 long_u *length;
2021 int *format;
2023 int motion_type;
2024 long_u len;
2025 char_u *p;
2026 char **text_list = NULL;
2027 VimClipboard *cbd;
2028 #ifdef FEAT_MBYTE
2029 char_u *tmpbuf = NULL;
2030 #endif
2032 if (*sel_atom == clip_plus.sel_atom)
2033 cbd = &clip_plus;
2034 else
2035 cbd = &clip_star;
2037 if (value == NULL || *length == 0)
2039 clip_free_selection(cbd); /* nothing received, clear register */
2040 *(int *)success = FALSE;
2041 return;
2043 motion_type = MCHAR;
2044 p = (char_u *)value;
2045 len = *length;
2046 if (*type == vim_atom)
2048 motion_type = *p++;
2049 len--;
2052 #ifdef FEAT_MBYTE
2053 else if (*type == vimenc_atom)
2055 char_u *enc;
2056 vimconv_T conv;
2057 int convlen;
2059 motion_type = *p++;
2060 --len;
2062 enc = p;
2063 p += STRLEN(p) + 1;
2064 len -= p - enc;
2066 /* If the encoding of the text is different from 'encoding', attempt
2067 * converting it. */
2068 conv.vc_type = CONV_NONE;
2069 convert_setup(&conv, enc, p_enc);
2070 if (conv.vc_type != CONV_NONE)
2072 convlen = len; /* Need to use an int here. */
2073 tmpbuf = string_convert(&conv, p, &convlen);
2074 len = convlen;
2075 if (tmpbuf != NULL)
2076 p = tmpbuf;
2077 convert_setup(&conv, NULL, NULL);
2080 #endif
2082 else if (*type == compound_text_atom || (
2083 #ifdef FEAT_MBYTE
2084 enc_dbcs != 0 &&
2085 #endif
2086 *type == text_atom))
2088 XTextProperty text_prop;
2089 int n_text = 0;
2090 int status;
2092 text_prop.value = (unsigned char *)value;
2093 text_prop.encoding = *type;
2094 text_prop.format = *format;
2095 text_prop.nitems = len;
2096 status = XmbTextPropertyToTextList(X_DISPLAY, &text_prop,
2097 &text_list, &n_text);
2098 if (status != Success || n_text < 1)
2100 *(int *)success = FALSE;
2101 return;
2103 p = (char_u *)text_list[0];
2104 len = STRLEN(p);
2106 clip_yank_selection(motion_type, p, (long)len, cbd);
2108 if (text_list != NULL)
2109 XFreeStringList(text_list);
2110 #ifdef FEAT_MBYTE
2111 vim_free(tmpbuf);
2112 #endif
2113 XtFree((char *)value);
2114 *(int *)success = TRUE;
2117 void
2118 clip_x11_request_selection(myShell, dpy, cbd)
2119 Widget myShell;
2120 Display *dpy;
2121 VimClipboard *cbd;
2123 XEvent event;
2124 Atom type;
2125 static int success;
2126 int i;
2127 time_t start_time;
2128 int timed_out = FALSE;
2130 for (i =
2131 #ifdef FEAT_MBYTE
2133 #else
2135 #endif
2136 ; i < 5; i++)
2138 switch (i)
2140 #ifdef FEAT_MBYTE
2141 case 0: type = vimenc_atom; break;
2142 #endif
2143 case 1: type = vim_atom; break;
2144 case 2: type = compound_text_atom; break;
2145 case 3: type = text_atom; break;
2146 default: type = XA_STRING;
2148 success = MAYBE;
2149 XtGetSelectionValue(myShell, cbd->sel_atom, type,
2150 clip_x11_request_selection_cb, (XtPointer)&success, CurrentTime);
2152 /* Make sure the request for the selection goes out before waiting for
2153 * a response. */
2154 XFlush(dpy);
2157 * Wait for result of selection request, otherwise if we type more
2158 * characters, then they will appear before the one that requested the
2159 * paste! Don't worry, we will catch up with any other events later.
2161 start_time = time(NULL);
2162 while (success == MAYBE)
2164 if (XCheckTypedEvent(dpy, SelectionNotify, &event)
2165 || XCheckTypedEvent(dpy, SelectionRequest, &event)
2166 || XCheckTypedEvent(dpy, PropertyNotify, &event))
2168 /* This is where clip_x11_request_selection_cb() should be
2169 * called. It may actually happen a bit later, so we loop
2170 * until "success" changes.
2171 * We may get a SelectionRequest here and if we don't handle
2172 * it we hang. KDE klipper does this, for example.
2173 * We need to handle a PropertyNotify for large selections. */
2174 XtDispatchEvent(&event);
2175 continue;
2178 /* Time out after 2 to 3 seconds to avoid that we hang when the
2179 * other process doesn't respond. Note that the SelectionNotify
2180 * event may still come later when the selection owner comes back
2181 * to life and the text gets inserted unexpectedly. Don't know
2182 * why that happens or how to avoid that :-(. */
2183 if (time(NULL) > start_time + 2)
2185 timed_out = TRUE;
2186 break;
2189 /* Do we need this? Probably not. */
2190 XSync(dpy, False);
2192 /* Wait for 1 msec to avoid that we eat up all CPU time. */
2193 ui_delay(1L, TRUE);
2196 if (success == TRUE)
2197 return;
2199 /* don't do a retry with another type after timing out, otherwise we
2200 * hang for 15 seconds. */
2201 if (timed_out)
2202 break;
2205 /* Final fallback position - use the X CUT_BUFFER0 store */
2206 yank_cut_buffer0(dpy, cbd);
2209 static Boolean clip_x11_convert_selection_cb __ARGS((Widget, Atom *, Atom *, Atom *, XtPointer *, long_u *, int *));
2211 static Boolean
2212 clip_x11_convert_selection_cb(w, sel_atom, target, type, value, length, format)
2213 Widget w UNUSED;
2214 Atom *sel_atom;
2215 Atom *target;
2216 Atom *type;
2217 XtPointer *value;
2218 long_u *length;
2219 int *format;
2221 char_u *string;
2222 char_u *result;
2223 int motion_type;
2224 VimClipboard *cbd;
2225 int i;
2227 if (*sel_atom == clip_plus.sel_atom)
2228 cbd = &clip_plus;
2229 else
2230 cbd = &clip_star;
2232 if (!cbd->owned)
2233 return False; /* Shouldn't ever happen */
2235 /* requestor wants to know what target types we support */
2236 if (*target == targets_atom)
2238 Atom *array;
2240 if ((array = (Atom *)XtMalloc((unsigned)(sizeof(Atom) * 6))) == NULL)
2241 return False;
2242 *value = (XtPointer)array;
2243 i = 0;
2244 array[i++] = XA_STRING;
2245 array[i++] = targets_atom;
2246 #ifdef FEAT_MBYTE
2247 array[i++] = vimenc_atom;
2248 #endif
2249 array[i++] = vim_atom;
2250 array[i++] = text_atom;
2251 array[i++] = compound_text_atom;
2252 *type = XA_ATOM;
2253 /* This used to be: *format = sizeof(Atom) * 8; but that caused
2254 * crashes on 64 bit machines. (Peter Derr) */
2255 *format = 32;
2256 *length = i;
2257 return True;
2260 if ( *target != XA_STRING
2261 #ifdef FEAT_MBYTE
2262 && *target != vimenc_atom
2263 #endif
2264 && *target != vim_atom
2265 && *target != text_atom
2266 && *target != compound_text_atom)
2267 return False;
2269 clip_get_selection(cbd);
2270 motion_type = clip_convert_selection(&string, length, cbd);
2271 if (motion_type < 0)
2272 return False;
2274 /* For our own format, the first byte contains the motion type */
2275 if (*target == vim_atom)
2276 (*length)++;
2278 #ifdef FEAT_MBYTE
2279 /* Our own format with encoding: motion 'encoding' NUL text */
2280 if (*target == vimenc_atom)
2281 *length += STRLEN(p_enc) + 2;
2282 #endif
2284 *value = XtMalloc((Cardinal)*length);
2285 result = (char_u *)*value;
2286 if (result == NULL)
2288 vim_free(string);
2289 return False;
2292 if (*target == XA_STRING)
2294 mch_memmove(result, string, (size_t)(*length));
2295 *type = XA_STRING;
2297 else if (*target == compound_text_atom
2298 || *target == text_atom)
2300 XTextProperty text_prop;
2301 char *string_nt = (char *)alloc((unsigned)*length + 1);
2303 /* create NUL terminated string which XmbTextListToTextProperty wants */
2304 mch_memmove(string_nt, string, (size_t)*length);
2305 string_nt[*length] = NUL;
2306 XmbTextListToTextProperty(X_DISPLAY, (char **)&string_nt, 1,
2307 XCompoundTextStyle, &text_prop);
2308 vim_free(string_nt);
2309 XtFree(*value); /* replace with COMPOUND text */
2310 *value = (XtPointer)(text_prop.value); /* from plain text */
2311 *length = text_prop.nitems;
2312 *type = compound_text_atom;
2315 #ifdef FEAT_MBYTE
2316 else if (*target == vimenc_atom)
2318 int l = STRLEN(p_enc);
2320 result[0] = motion_type;
2321 STRCPY(result + 1, p_enc);
2322 mch_memmove(result + l + 2, string, (size_t)(*length - l - 2));
2323 *type = vimenc_atom;
2325 #endif
2327 else
2329 result[0] = motion_type;
2330 mch_memmove(result + 1, string, (size_t)(*length - 1));
2331 *type = vim_atom;
2333 *format = 8; /* 8 bits per char */
2334 vim_free(string);
2335 return True;
2338 static void clip_x11_lose_ownership_cb __ARGS((Widget, Atom *));
2340 static void
2341 clip_x11_lose_ownership_cb(w, sel_atom)
2342 Widget w UNUSED;
2343 Atom *sel_atom;
2345 if (*sel_atom == clip_plus.sel_atom)
2346 clip_lose_selection(&clip_plus);
2347 else
2348 clip_lose_selection(&clip_star);
2351 void
2352 clip_x11_lose_selection(myShell, cbd)
2353 Widget myShell;
2354 VimClipboard *cbd;
2356 XtDisownSelection(myShell, cbd->sel_atom, CurrentTime);
2360 clip_x11_own_selection(myShell, cbd)
2361 Widget myShell;
2362 VimClipboard *cbd;
2364 if (XtOwnSelection(myShell, cbd->sel_atom, CurrentTime,
2365 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
2366 NULL) == False)
2367 return FAIL;
2368 return OK;
2372 * Send the current selection to the clipboard. Do nothing for X because we
2373 * will fill in the selection only when requested by another app.
2375 void
2376 clip_x11_set_selection(cbd)
2377 VimClipboard *cbd UNUSED;
2380 #endif
2382 #if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) \
2383 || defined(FEAT_GUI_GTK) || defined(PROTO)
2385 * Get the contents of the X CUT_BUFFER0 and put it in "cbd".
2387 void
2388 yank_cut_buffer0(dpy, cbd)
2389 Display *dpy;
2390 VimClipboard *cbd;
2392 int nbytes = 0;
2393 char_u *buffer = (char_u *)XFetchBuffer(dpy, &nbytes, 0);
2395 if (nbytes > 0)
2397 #ifdef FEAT_MBYTE
2398 int done = FALSE;
2400 /* CUT_BUFFER0 is supposed to be always latin1. Convert to 'enc' when
2401 * using a multi-byte encoding. Conversion between two 8-bit
2402 * character sets usually fails and the text might actually be in
2403 * 'enc' anyway. */
2404 if (has_mbyte)
2406 char_u *conv_buf;
2407 vimconv_T vc;
2409 vc.vc_type = CONV_NONE;
2410 if (convert_setup(&vc, (char_u *)"latin1", p_enc) == OK)
2412 conv_buf = string_convert(&vc, buffer, &nbytes);
2413 if (conv_buf != NULL)
2415 clip_yank_selection(MCHAR, conv_buf, (long)nbytes, cbd);
2416 vim_free(conv_buf);
2417 done = TRUE;
2419 convert_setup(&vc, NULL, NULL);
2422 if (!done) /* use the text without conversion */
2423 #endif
2424 clip_yank_selection(MCHAR, buffer, (long)nbytes, cbd);
2425 XFree((void *)buffer);
2426 if (p_verbose > 0)
2428 verbose_enter();
2429 verb_msg((char_u *)_("Used CUT_BUFFER0 instead of empty selection"));
2430 verbose_leave();
2434 #endif
2436 #if defined(FEAT_MOUSE) || defined(PROTO)
2439 * Move the cursor to the specified row and column on the screen.
2440 * Change current window if necessary. Returns an integer with the
2441 * CURSOR_MOVED bit set if the cursor has moved or unset otherwise.
2443 * The MOUSE_FOLD_CLOSE bit is set when clicked on the '-' in a fold column.
2444 * The MOUSE_FOLD_OPEN bit is set when clicked on the '+' in a fold column.
2446 * If flags has MOUSE_FOCUS, then the current window will not be changed, and
2447 * if the mouse is outside the window then the text will scroll, or if the
2448 * mouse was previously on a status line, then the status line may be dragged.
2450 * If flags has MOUSE_MAY_VIS, then VIsual mode will be started before the
2451 * cursor is moved unless the cursor was on a status line.
2452 * This function returns one of IN_UNKNOWN, IN_BUFFER, IN_STATUS_LINE or
2453 * IN_SEP_LINE depending on where the cursor was clicked.
2455 * If flags has MOUSE_MAY_STOP_VIS, then Visual mode will be stopped, unless
2456 * the mouse is on the status line of the same window.
2458 * If flags has MOUSE_DID_MOVE, nothing is done if the mouse didn't move since
2459 * the last call.
2461 * If flags has MOUSE_SETPOS, nothing is done, only the current position is
2462 * remembered.
2465 jump_to_mouse(flags, inclusive, which_button)
2466 int flags;
2467 int *inclusive; /* used for inclusive operator, can be NULL */
2468 int which_button; /* MOUSE_LEFT, MOUSE_RIGHT, MOUSE_MIDDLE */
2470 static int on_status_line = 0; /* #lines below bottom of window */
2471 #ifdef FEAT_VERTSPLIT
2472 static int on_sep_line = 0; /* on separator right of window */
2473 #endif
2474 static int prev_row = -1;
2475 static int prev_col = -1;
2476 static win_T *dragwin = NULL; /* window being dragged */
2477 static int did_drag = FALSE; /* drag was noticed */
2479 win_T *wp, *old_curwin;
2480 pos_T old_cursor;
2481 int count;
2482 int first;
2483 int row = mouse_row;
2484 int col = mouse_col;
2485 #ifdef FEAT_FOLDING
2486 int mouse_char;
2487 #endif
2489 mouse_past_bottom = FALSE;
2490 mouse_past_eol = FALSE;
2492 if (flags & MOUSE_RELEASED)
2494 /* On button release we may change window focus if positioned on a
2495 * status line and no dragging happened. */
2496 if (dragwin != NULL && !did_drag)
2497 flags &= ~(MOUSE_FOCUS | MOUSE_DID_MOVE);
2498 dragwin = NULL;
2499 did_drag = FALSE;
2502 if ((flags & MOUSE_DID_MOVE)
2503 && prev_row == mouse_row
2504 && prev_col == mouse_col)
2506 retnomove:
2507 /* before moving the cursor for a left click which is NOT in a status
2508 * line, stop Visual mode */
2509 if (on_status_line)
2510 return IN_STATUS_LINE;
2511 #ifdef FEAT_VERTSPLIT
2512 if (on_sep_line)
2513 return IN_SEP_LINE;
2514 #endif
2515 #ifdef FEAT_VISUAL
2516 if (flags & MOUSE_MAY_STOP_VIS)
2518 end_visual_mode();
2519 redraw_curbuf_later(INVERTED); /* delete the inversion */
2521 #endif
2522 #if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2523 /* Continue a modeless selection in another window. */
2524 if (cmdwin_type != 0 && row < W_WINROW(curwin))
2525 return IN_OTHER_WIN;
2526 #endif
2527 return IN_BUFFER;
2530 prev_row = mouse_row;
2531 prev_col = mouse_col;
2533 if (flags & MOUSE_SETPOS)
2534 goto retnomove; /* ugly goto... */
2536 #ifdef FEAT_FOLDING
2537 /* Remember the character under the mouse, it might be a '-' or '+' in the
2538 * fold column. */
2539 if (row >= 0 && row < Rows && col >= 0 && col <= Columns
2540 && ScreenLines != NULL)
2541 mouse_char = ScreenLines[LineOffset[row] + col];
2542 else
2543 mouse_char = ' ';
2544 #endif
2546 old_curwin = curwin;
2547 old_cursor = curwin->w_cursor;
2549 if (!(flags & MOUSE_FOCUS))
2551 if (row < 0 || col < 0) /* check if it makes sense */
2552 return IN_UNKNOWN;
2554 #ifdef FEAT_WINDOWS
2555 /* find the window where the row is in */
2556 wp = mouse_find_win(&row, &col);
2557 #else
2558 wp = firstwin;
2559 #endif
2560 dragwin = NULL;
2562 * winpos and height may change in win_enter()!
2564 if (row >= wp->w_height) /* In (or below) status line */
2566 on_status_line = row - wp->w_height + 1;
2567 dragwin = wp;
2569 else
2570 on_status_line = 0;
2571 #ifdef FEAT_VERTSPLIT
2572 if (col >= wp->w_width) /* In separator line */
2574 on_sep_line = col - wp->w_width + 1;
2575 dragwin = wp;
2577 else
2578 on_sep_line = 0;
2580 /* The rightmost character of the status line might be a vertical
2581 * separator character if there is no connecting window to the right. */
2582 if (on_status_line && on_sep_line)
2584 if (stl_connected(wp))
2585 on_sep_line = 0;
2586 else
2587 on_status_line = 0;
2589 #endif
2591 #ifdef FEAT_VISUAL
2592 /* Before jumping to another buffer, or moving the cursor for a left
2593 * click, stop Visual mode. */
2594 if (VIsual_active
2595 && (wp->w_buffer != curwin->w_buffer
2596 || (!on_status_line
2597 # ifdef FEAT_VERTSPLIT
2598 && !on_sep_line
2599 # endif
2600 # ifdef FEAT_FOLDING
2601 && (
2602 # ifdef FEAT_RIGHTLEFT
2603 wp->w_p_rl ? col < W_WIDTH(wp) - wp->w_p_fdc :
2604 # endif
2605 col >= wp->w_p_fdc
2606 # ifdef FEAT_CMDWIN
2607 + (cmdwin_type == 0 && wp == curwin ? 0 : 1)
2608 # endif
2610 # endif
2611 && (flags & MOUSE_MAY_STOP_VIS))))
2613 end_visual_mode();
2614 redraw_curbuf_later(INVERTED); /* delete the inversion */
2616 #endif
2617 #ifdef FEAT_CMDWIN
2618 if (cmdwin_type != 0 && wp != curwin)
2620 /* A click outside the command-line window: Use modeless
2621 * selection if possible. Allow dragging the status lines. */
2622 # ifdef FEAT_VERTSPLIT
2623 on_sep_line = 0;
2624 # endif
2625 # ifdef FEAT_CLIPBOARD
2626 if (on_status_line)
2627 return IN_STATUS_LINE;
2628 return IN_OTHER_WIN;
2629 # else
2630 row = 0;
2631 col += wp->w_wincol;
2632 wp = curwin;
2633 # endif
2635 #endif
2636 #ifdef FEAT_WINDOWS
2637 /* Only change window focus when not clicking on or dragging the
2638 * status line. Do change focus when releasing the mouse button
2639 * (MOUSE_FOCUS was set above if we dragged first). */
2640 if (dragwin == NULL || (flags & MOUSE_RELEASED))
2641 win_enter(wp, TRUE); /* can make wp invalid! */
2642 # ifdef CHECK_DOUBLE_CLICK
2643 /* set topline, to be able to check for double click ourselves */
2644 if (curwin != old_curwin)
2645 set_mouse_topline(curwin);
2646 # endif
2647 #endif
2648 if (on_status_line) /* In (or below) status line */
2650 /* Don't use start_arrow() if we're in the same window */
2651 if (curwin == old_curwin)
2652 return IN_STATUS_LINE;
2653 else
2654 return IN_STATUS_LINE | CURSOR_MOVED;
2656 #ifdef FEAT_VERTSPLIT
2657 if (on_sep_line) /* In (or below) status line */
2659 /* Don't use start_arrow() if we're in the same window */
2660 if (curwin == old_curwin)
2661 return IN_SEP_LINE;
2662 else
2663 return IN_SEP_LINE | CURSOR_MOVED;
2665 #endif
2667 curwin->w_cursor.lnum = curwin->w_topline;
2668 #ifdef FEAT_GUI
2669 /* remember topline, needed for double click */
2670 gui_prev_topline = curwin->w_topline;
2671 # ifdef FEAT_DIFF
2672 gui_prev_topfill = curwin->w_topfill;
2673 # endif
2674 #endif
2676 else if (on_status_line && which_button == MOUSE_LEFT)
2678 #ifdef FEAT_WINDOWS
2679 if (dragwin != NULL)
2681 /* Drag the status line */
2682 count = row - dragwin->w_winrow - dragwin->w_height + 1
2683 - on_status_line;
2684 win_drag_status_line(dragwin, count);
2685 did_drag |= count;
2687 #endif
2688 return IN_STATUS_LINE; /* Cursor didn't move */
2690 #ifdef FEAT_VERTSPLIT
2691 else if (on_sep_line && which_button == MOUSE_LEFT)
2693 if (dragwin != NULL)
2695 /* Drag the separator column */
2696 count = col - dragwin->w_wincol - dragwin->w_width + 1
2697 - on_sep_line;
2698 win_drag_vsep_line(dragwin, count);
2699 did_drag |= count;
2701 return IN_SEP_LINE; /* Cursor didn't move */
2703 #endif
2704 else /* keep_window_focus must be TRUE */
2706 #ifdef FEAT_VISUAL
2707 /* before moving the cursor for a left click, stop Visual mode */
2708 if (flags & MOUSE_MAY_STOP_VIS)
2710 end_visual_mode();
2711 redraw_curbuf_later(INVERTED); /* delete the inversion */
2713 #endif
2715 #if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2716 /* Continue a modeless selection in another window. */
2717 if (cmdwin_type != 0 && row < W_WINROW(curwin))
2718 return IN_OTHER_WIN;
2719 #endif
2721 row -= W_WINROW(curwin);
2722 #ifdef FEAT_VERTSPLIT
2723 col -= W_WINCOL(curwin);
2724 #endif
2727 * When clicking beyond the end of the window, scroll the screen.
2728 * Scroll by however many rows outside the window we are.
2730 if (row < 0)
2732 count = 0;
2733 for (first = TRUE; curwin->w_topline > 1; )
2735 #ifdef FEAT_DIFF
2736 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
2737 ++count;
2738 else
2739 #endif
2740 count += plines(curwin->w_topline - 1);
2741 if (!first && count > -row)
2742 break;
2743 first = FALSE;
2744 #ifdef FEAT_FOLDING
2745 hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2746 #endif
2747 #ifdef FEAT_DIFF
2748 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
2749 ++curwin->w_topfill;
2750 else
2751 #endif
2753 --curwin->w_topline;
2754 #ifdef FEAT_DIFF
2755 curwin->w_topfill = 0;
2756 #endif
2759 #ifdef FEAT_DIFF
2760 check_topfill(curwin, FALSE);
2761 #endif
2762 curwin->w_valid &=
2763 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2764 redraw_later(VALID);
2765 row = 0;
2767 else if (row >= curwin->w_height)
2769 count = 0;
2770 for (first = TRUE; curwin->w_topline < curbuf->b_ml.ml_line_count; )
2772 #ifdef FEAT_DIFF
2773 if (curwin->w_topfill > 0)
2774 ++count;
2775 else
2776 #endif
2777 count += plines(curwin->w_topline);
2778 if (!first && count > row - curwin->w_height + 1)
2779 break;
2780 first = FALSE;
2781 #ifdef FEAT_FOLDING
2782 if (hasFolding(curwin->w_topline, NULL, &curwin->w_topline)
2783 && curwin->w_topline == curbuf->b_ml.ml_line_count)
2784 break;
2785 #endif
2786 #ifdef FEAT_DIFF
2787 if (curwin->w_topfill > 0)
2788 --curwin->w_topfill;
2789 else
2790 #endif
2792 ++curwin->w_topline;
2793 #ifdef FEAT_DIFF
2794 curwin->w_topfill =
2795 diff_check_fill(curwin, curwin->w_topline);
2796 #endif
2799 #ifdef FEAT_DIFF
2800 check_topfill(curwin, FALSE);
2801 #endif
2802 redraw_later(VALID);
2803 curwin->w_valid &=
2804 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2805 row = curwin->w_height - 1;
2807 else if (row == 0)
2809 /* When dragging the mouse, while the text has been scrolled up as
2810 * far as it goes, moving the mouse in the top line should scroll
2811 * the text down (done later when recomputing w_topline). */
2812 if (mouse_dragging > 0
2813 && curwin->w_cursor.lnum
2814 == curwin->w_buffer->b_ml.ml_line_count
2815 && curwin->w_cursor.lnum == curwin->w_topline)
2816 curwin->w_valid &= ~(VALID_TOPLINE);
2820 #ifdef FEAT_FOLDING
2821 /* Check for position outside of the fold column. */
2822 if (
2823 # ifdef FEAT_RIGHTLEFT
2824 curwin->w_p_rl ? col < W_WIDTH(curwin) - curwin->w_p_fdc :
2825 # endif
2826 col >= curwin->w_p_fdc
2827 # ifdef FEAT_CMDWIN
2828 + (cmdwin_type == 0 ? 0 : 1)
2829 # endif
2831 mouse_char = ' ';
2832 #endif
2834 /* compute the position in the buffer line from the posn on the screen */
2835 if (mouse_comp_pos(curwin, &row, &col, &curwin->w_cursor.lnum))
2836 mouse_past_bottom = TRUE;
2838 #ifdef FEAT_VISUAL
2839 /* Start Visual mode before coladvance(), for when 'sel' != "old" */
2840 if ((flags & MOUSE_MAY_VIS) && !VIsual_active)
2842 check_visual_highlight();
2843 VIsual = old_cursor;
2844 VIsual_active = TRUE;
2845 VIsual_reselect = TRUE;
2846 /* if 'selectmode' contains "mouse", start Select mode */
2847 may_start_select('o');
2848 setmouse();
2849 if (p_smd && msg_silent == 0)
2850 redraw_cmdline = TRUE; /* show visual mode later */
2852 #endif
2854 curwin->w_curswant = col;
2855 curwin->w_set_curswant = FALSE; /* May still have been TRUE */
2856 if (coladvance(col) == FAIL) /* Mouse click beyond end of line */
2858 if (inclusive != NULL)
2859 *inclusive = TRUE;
2860 mouse_past_eol = TRUE;
2862 else if (inclusive != NULL)
2863 *inclusive = FALSE;
2865 count = IN_BUFFER;
2866 if (curwin != old_curwin || curwin->w_cursor.lnum != old_cursor.lnum
2867 || curwin->w_cursor.col != old_cursor.col)
2868 count |= CURSOR_MOVED; /* Cursor has moved */
2870 #ifdef FEAT_FOLDING
2871 if (mouse_char == '+')
2872 count |= MOUSE_FOLD_OPEN;
2873 else if (mouse_char != ' ')
2874 count |= MOUSE_FOLD_CLOSE;
2875 #endif
2877 return count;
2881 * Compute the position in the buffer line from the posn on the screen in
2882 * window "win".
2883 * Returns TRUE if the position is below the last line.
2886 mouse_comp_pos(win, rowp, colp, lnump)
2887 win_T *win;
2888 int *rowp;
2889 int *colp;
2890 linenr_T *lnump;
2892 int col = *colp;
2893 int row = *rowp;
2894 linenr_T lnum;
2895 int retval = FALSE;
2896 int off;
2897 int count;
2899 #ifdef FEAT_RIGHTLEFT
2900 if (win->w_p_rl)
2901 col = W_WIDTH(win) - 1 - col;
2902 #endif
2904 lnum = win->w_topline;
2906 while (row > 0)
2908 #ifdef FEAT_DIFF
2909 /* Don't include filler lines in "count" */
2910 if (win->w_p_diff
2911 # ifdef FEAT_FOLDING
2912 && !hasFoldingWin(win, lnum, NULL, NULL, TRUE, NULL)
2913 # endif
2916 if (lnum == win->w_topline)
2917 row -= win->w_topfill;
2918 else
2919 row -= diff_check_fill(win, lnum);
2920 count = plines_win_nofill(win, lnum, TRUE);
2922 else
2923 #endif
2924 count = plines_win(win, lnum, TRUE);
2925 if (count > row)
2926 break; /* Position is in this buffer line. */
2927 #ifdef FEAT_FOLDING
2928 (void)hasFoldingWin(win, lnum, NULL, &lnum, TRUE, NULL);
2929 #endif
2930 if (lnum == win->w_buffer->b_ml.ml_line_count)
2932 retval = TRUE;
2933 break; /* past end of file */
2935 row -= count;
2936 ++lnum;
2939 if (!retval)
2941 /* Compute the column without wrapping. */
2942 off = win_col_off(win) - win_col_off2(win);
2943 if (col < off)
2944 col = off;
2945 col += row * (W_WIDTH(win) - off);
2946 /* add skip column (for long wrapping line) */
2947 col += win->w_skipcol;
2950 if (!win->w_p_wrap)
2951 col += win->w_leftcol;
2953 /* skip line number and fold column in front of the line */
2954 col -= win_col_off(win);
2955 if (col < 0)
2957 #ifdef FEAT_NETBEANS_INTG
2958 if (usingNetbeans)
2959 netbeans_gutter_click(lnum);
2960 #endif
2961 col = 0;
2964 *colp = col;
2965 *rowp = row;
2966 *lnump = lnum;
2967 return retval;
2970 #if defined(FEAT_WINDOWS) || defined(PROTO)
2972 * Find the window at screen position "*rowp" and "*colp". The positions are
2973 * updated to become relative to the top-left of the window.
2975 win_T *
2976 mouse_find_win(rowp, colp)
2977 int *rowp;
2978 int *colp UNUSED;
2980 frame_T *fp;
2982 fp = topframe;
2983 *rowp -= firstwin->w_winrow;
2984 for (;;)
2986 if (fp->fr_layout == FR_LEAF)
2987 break;
2988 #ifdef FEAT_VERTSPLIT
2989 if (fp->fr_layout == FR_ROW)
2991 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
2993 if (*colp < fp->fr_width)
2994 break;
2995 *colp -= fp->fr_width;
2998 #endif
2999 else /* fr_layout == FR_COL */
3001 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
3003 if (*rowp < fp->fr_height)
3004 break;
3005 *rowp -= fp->fr_height;
3009 return fp->fr_win;
3011 #endif
3013 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined (FEAT_GUI_MAC) \
3014 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
3015 || defined(FEAT_GUI_PHOTON) || defined(PROTO) \
3016 || defined(FEAT_GUI_MACVIM)
3018 * Translate window coordinates to buffer position without any side effects
3021 get_fpos_of_mouse(mpos)
3022 pos_T *mpos;
3024 win_T *wp;
3025 int row = mouse_row;
3026 int col = mouse_col;
3028 if (row < 0 || col < 0) /* check if it makes sense */
3029 return IN_UNKNOWN;
3031 #ifdef FEAT_WINDOWS
3032 /* find the window where the row is in */
3033 wp = mouse_find_win(&row, &col);
3034 #else
3035 wp = firstwin;
3036 #endif
3038 * winpos and height may change in win_enter()!
3040 if (row >= wp->w_height) /* In (or below) status line */
3041 return IN_STATUS_LINE;
3042 #ifdef FEAT_VERTSPLIT
3043 if (col >= wp->w_width) /* In vertical separator line */
3044 return IN_SEP_LINE;
3045 #endif
3047 if (wp != curwin)
3048 return IN_UNKNOWN;
3050 /* compute the position in the buffer line from the posn on the screen */
3051 if (mouse_comp_pos(curwin, &row, &col, &mpos->lnum))
3052 return IN_STATUS_LINE; /* past bottom */
3054 mpos->col = vcol2col(wp, mpos->lnum, col);
3056 if (mpos->col > 0)
3057 --mpos->col;
3058 return IN_BUFFER;
3062 * Convert a virtual (screen) column to a character column.
3063 * The first column is one.
3066 vcol2col(wp, lnum, vcol)
3067 win_T *wp;
3068 linenr_T lnum;
3069 int vcol;
3071 /* try to advance to the specified column */
3072 int count = 0;
3073 char_u *ptr;
3074 char_u *start;
3076 start = ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3077 while (count <= vcol && *ptr != NUL)
3079 count += win_lbr_chartabsize(wp, ptr, count, NULL);
3080 mb_ptr_adv(ptr);
3082 return (int)(ptr - start);
3084 #endif
3086 #endif /* FEAT_MOUSE */
3088 #if defined(FEAT_GUI) || defined(WIN3264) || defined(PROTO)
3090 * Called when focus changed. Used for the GUI or for systems where this can
3091 * be done in the console (Win32).
3093 void
3094 ui_focus_change(in_focus)
3095 int in_focus; /* TRUE if focus gained. */
3097 static time_t last_time = (time_t)0;
3098 int need_redraw = FALSE;
3100 /* When activated: Check if any file was modified outside of Vim.
3101 * Only do this when not done within the last two seconds (could get
3102 * several events in a row). */
3103 if (in_focus && last_time + 2 < time(NULL))
3105 need_redraw = check_timestamps(
3106 # ifdef FEAT_GUI
3107 gui.in_use
3108 # else
3109 FALSE
3110 # endif
3112 last_time = time(NULL);
3115 #ifdef FEAT_AUTOCMD
3117 * Fire the focus gained/lost autocommand.
3119 need_redraw |= apply_autocmds(in_focus ? EVENT_FOCUSGAINED
3120 : EVENT_FOCUSLOST, NULL, NULL, FALSE, curbuf);
3121 #endif
3123 if (need_redraw)
3125 /* Something was executed, make sure the cursor is put back where it
3126 * belongs. */
3127 need_wait_return = FALSE;
3129 if (State & CMDLINE)
3130 redrawcmdline();
3131 else if (State == HITRETURN || State == SETWSIZE || State == ASKMORE
3132 || State == EXTERNCMD || State == CONFIRM || exmode_active)
3133 repeat_message();
3134 else if ((State & NORMAL) || (State & INSERT))
3136 if (must_redraw != 0)
3137 update_screen(0);
3138 setcursor();
3140 cursor_on(); /* redrawing may have switched it off */
3141 out_flush();
3142 # ifdef FEAT_GUI
3143 if (gui.in_use)
3145 gui_update_cursor(FALSE, TRUE);
3146 gui_update_scrollbars(FALSE);
3148 # endif
3150 #ifdef FEAT_TITLE
3151 /* File may have been changed from 'readonly' to 'noreadonly' */
3152 if (need_maketitle)
3153 maketitle();
3154 #endif
3156 #endif
3158 #if defined(USE_IM_CONTROL) || defined(PROTO)
3160 * Save current Input Method status to specified place.
3162 void
3163 im_save_status(psave)
3164 long *psave;
3166 /* Don't save when 'imdisable' is set or "xic" is NULL, IM is always
3167 * disabled then (but might start later).
3168 * Also don't save when inside a mapping, vgetc_im_active has not been set
3169 * then.
3170 * And don't save when the keys were stuffed (e.g., for a "." command).
3171 * And don't save when the GUI is running but our window doesn't have
3172 * input focus (e.g., when a find dialog is open). */
3173 if (!p_imdisable && KeyTyped && !KeyStuffed
3174 # if defined(FEAT_XIM) && !defined(FEAT_GUI_MACVIM)
3175 && xic != NULL
3176 # endif
3177 # ifdef FEAT_GUI
3178 && (!gui.in_use || gui.in_focus)
3179 # endif
3182 /* Do save when IM is on, or IM is off and saved status is on. */
3183 if (vgetc_im_active)
3184 *psave = B_IMODE_IM;
3185 else if (*psave == B_IMODE_IM)
3186 *psave = B_IMODE_NONE;
3189 #endif