Prepare PSMTabBarControl for 64 bit
[MacVim.git] / src / ui.c
blob8e3c7aeae487d8832e66df811cc0abf6ab97e3c3
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 retval = gui_get_shellsize();
313 else
314 #endif
315 retval = mch_get_shellsize();
317 check_shellsize();
319 /* adjust the default for 'lines' and 'columns' */
320 if (retval == OK)
322 set_number_default("lines", Rows);
323 set_number_default("columns", Columns);
325 return retval;
329 * Set the size of the Vim shell according to Rows and Columns, if possible.
330 * The gui_set_shellsize() or mch_set_shellsize() function will try to set the
331 * new size. If this is not possible, it will adjust Rows and Columns.
333 void
334 ui_set_shellsize(mustset)
335 int mustset UNUSED; /* set by the user */
337 #ifdef FEAT_GUI
338 if (gui.in_use)
339 gui_set_shellsize(mustset,
340 # ifdef WIN3264
341 TRUE
342 # else
343 FALSE
344 # endif
345 , RESIZE_BOTH);
346 else
347 #endif
348 mch_set_shellsize();
352 * Called when Rows and/or Columns changed. Adjust scroll region and mouse
353 * region.
355 void
356 ui_new_shellsize()
358 if (full_screen && !exiting)
360 #ifdef FEAT_GUI
361 if (gui.in_use)
362 gui_new_shellsize();
363 else
364 #endif
365 mch_new_shellsize();
369 void
370 ui_breakcheck()
372 #ifdef FEAT_GUI
373 if (gui.in_use)
374 gui_mch_update();
375 else
376 #endif
377 mch_breakcheck();
380 /*****************************************************************************
381 * Functions for copying and pasting text between applications.
382 * This is always included in a GUI version, but may also be included when the
383 * clipboard and mouse is available to a terminal version such as xterm.
384 * Note: there are some more functions in ops.c that handle selection stuff.
386 * Also note that the majority of functions here deal with the X 'primary'
387 * (visible - for Visual mode use) selection, and only that. There are no
388 * versions of these for the 'clipboard' selection, as Visual mode has no use
389 * for them.
392 #if defined(FEAT_CLIPBOARD) || defined(PROTO)
395 * Selection stuff using Visual mode, for cutting and pasting text to other
396 * windows.
400 * Call this to initialise the clipboard. Pass it FALSE if the clipboard code
401 * is included, but the clipboard can not be used, or TRUE if the clipboard can
402 * be used. Eg unix may call this with FALSE, then call it again with TRUE if
403 * the GUI starts.
405 void
406 clip_init(can_use)
407 int can_use;
409 VimClipboard *cb;
411 cb = &clip_star;
412 for (;;)
414 cb->available = can_use;
415 cb->owned = FALSE;
416 cb->start.lnum = 0;
417 cb->start.col = 0;
418 cb->end.lnum = 0;
419 cb->end.col = 0;
420 cb->state = SELECT_CLEARED;
422 if (cb == &clip_plus)
423 break;
424 cb = &clip_plus;
429 * Check whether the VIsual area has changed, and if so try to become the owner
430 * of the selection, and free any old converted selection we may still have
431 * lying around. If the VIsual mode has ended, make a copy of what was
432 * selected so we can still give it to others. Will probably have to make sure
433 * this is called whenever VIsual mode is ended.
435 void
436 clip_update_selection()
438 pos_T start, end;
440 /* If visual mode is only due to a redo command ("."), then ignore it */
441 if (!redo_VIsual_busy && VIsual_active && (State & NORMAL))
443 if (lt(VIsual, curwin->w_cursor))
445 start = VIsual;
446 end = curwin->w_cursor;
447 #ifdef FEAT_MBYTE
448 if (has_mbyte)
449 end.col += (*mb_ptr2len)(ml_get_cursor()) - 1;
450 #endif
452 else
454 start = curwin->w_cursor;
455 end = VIsual;
457 if (!equalpos(clip_star.start, start)
458 || !equalpos(clip_star.end, end)
459 || clip_star.vmode != VIsual_mode)
461 clip_clear_selection();
462 clip_star.start = start;
463 clip_star.end = end;
464 clip_star.vmode = VIsual_mode;
465 clip_free_selection(&clip_star);
466 clip_own_selection(&clip_star);
467 clip_gen_set_selection(&clip_star);
472 void
473 clip_own_selection(cbd)
474 VimClipboard *cbd;
477 * Also want to check somehow that we are reading from the keyboard rather
478 * than a mapping etc.
480 if (!cbd->owned && cbd->available)
482 cbd->owned = (clip_gen_own_selection(cbd) == OK);
483 #ifdef FEAT_X11
484 if (cbd == &clip_star)
486 /* May have to show a different kind of highlighting for the
487 * selected area. There is no specific redraw command for this,
488 * just redraw all windows on the current buffer. */
489 if (cbd->owned
490 && (get_real_state() == VISUAL
491 || get_real_state() == SELECTMODE)
492 && clip_isautosel()
493 && hl_attr(HLF_V) != hl_attr(HLF_VNC))
494 redraw_curbuf_later(INVERTED_ALL);
496 #endif
500 void
501 clip_lose_selection(cbd)
502 VimClipboard *cbd;
504 #ifdef FEAT_X11
505 int was_owned = cbd->owned;
506 #endif
507 int visual_selection = (cbd == &clip_star);
509 clip_free_selection(cbd);
510 cbd->owned = FALSE;
511 if (visual_selection)
512 clip_clear_selection();
513 clip_gen_lose_selection(cbd);
514 #ifdef FEAT_X11
515 if (visual_selection)
517 /* May have to show a different kind of highlighting for the selected
518 * area. There is no specific redraw command for this, just redraw all
519 * windows on the current buffer. */
520 if (was_owned
521 && (get_real_state() == VISUAL
522 || get_real_state() == SELECTMODE)
523 && clip_isautosel()
524 && hl_attr(HLF_V) != hl_attr(HLF_VNC))
526 update_curbuf(INVERTED_ALL);
527 setcursor();
528 cursor_on();
529 out_flush();
530 # ifdef FEAT_GUI
531 if (gui.in_use)
532 gui_update_cursor(TRUE, FALSE);
533 # endif
536 #endif
539 void
540 clip_copy_selection()
542 if (VIsual_active && (State & NORMAL) && clip_star.available)
544 if (clip_isautosel())
545 clip_update_selection();
546 clip_free_selection(&clip_star);
547 clip_own_selection(&clip_star);
548 if (clip_star.owned)
549 clip_get_selection(&clip_star);
550 clip_gen_set_selection(&clip_star);
555 * Called when Visual mode is ended: update the selection.
557 void
558 clip_auto_select()
560 if (clip_isautosel())
561 clip_copy_selection();
565 * Return TRUE if automatic selection of Visual area is desired.
568 clip_isautosel()
570 return (
571 #ifdef FEAT_GUI
572 gui.in_use ? (vim_strchr(p_go, GO_ASEL) != NULL) :
573 #endif
574 clip_autoselect);
579 * Stuff for general mouse selection, without using Visual mode.
582 static int clip_compare_pos __ARGS((int row1, int col1, int row2, int col2));
583 static void clip_invert_area __ARGS((int, int, int, int, int how));
584 static void clip_invert_rectangle __ARGS((int row, int col, int height, int width, int invert));
585 static void clip_get_word_boundaries __ARGS((VimClipboard *, int, int));
586 static int clip_get_line_end __ARGS((int));
587 static void clip_update_modeless_selection __ARGS((VimClipboard *, int, int,
588 int, int));
590 /* flags for clip_invert_area() */
591 #define CLIP_CLEAR 1
592 #define CLIP_SET 2
593 #define CLIP_TOGGLE 3
596 * Start, continue or end a modeless selection. Used when editing the
597 * command-line and in the cmdline window.
599 void
600 clip_modeless(button, is_click, is_drag)
601 int button;
602 int is_click;
603 int is_drag;
605 int repeat;
607 repeat = ((clip_star.mode == SELECT_MODE_CHAR
608 || clip_star.mode == SELECT_MODE_LINE)
609 && (mod_mask & MOD_MASK_2CLICK))
610 || (clip_star.mode == SELECT_MODE_WORD
611 && (mod_mask & MOD_MASK_3CLICK));
612 if (is_click && button == MOUSE_RIGHT)
614 /* Right mouse button: If there was no selection, start one.
615 * Otherwise extend the existing selection. */
616 if (clip_star.state == SELECT_CLEARED)
617 clip_start_selection(mouse_col, mouse_row, FALSE);
618 clip_process_selection(button, mouse_col, mouse_row, repeat);
620 else if (is_click)
621 clip_start_selection(mouse_col, mouse_row, repeat);
622 else if (is_drag)
624 /* Don't try extending a selection if there isn't one. Happens when
625 * button-down is in the cmdline and them moving mouse upwards. */
626 if (clip_star.state != SELECT_CLEARED)
627 clip_process_selection(button, mouse_col, mouse_row, repeat);
629 else /* release */
630 clip_process_selection(MOUSE_RELEASE, mouse_col, mouse_row, FALSE);
634 * Compare two screen positions ala strcmp()
636 static int
637 clip_compare_pos(row1, col1, row2, col2)
638 int row1;
639 int col1;
640 int row2;
641 int col2;
643 if (row1 > row2) return(1);
644 if (row1 < row2) return(-1);
645 if (col1 > col2) return(1);
646 if (col1 < col2) return(-1);
647 return(0);
651 * Start the selection
653 void
654 clip_start_selection(col, row, repeated_click)
655 int col;
656 int row;
657 int repeated_click;
659 VimClipboard *cb = &clip_star;
661 if (cb->state == SELECT_DONE)
662 clip_clear_selection();
664 row = check_row(row);
665 col = check_col(col);
666 #ifdef FEAT_MBYTE
667 col = mb_fix_col(col, row);
668 #endif
670 cb->start.lnum = row;
671 cb->start.col = col;
672 cb->end = cb->start;
673 cb->origin_row = (short_u)cb->start.lnum;
674 cb->state = SELECT_IN_PROGRESS;
676 if (repeated_click)
678 if (++cb->mode > SELECT_MODE_LINE)
679 cb->mode = SELECT_MODE_CHAR;
681 else
682 cb->mode = SELECT_MODE_CHAR;
684 #ifdef FEAT_GUI
685 /* clear the cursor until the selection is made */
686 if (gui.in_use)
687 gui_undraw_cursor();
688 #endif
690 switch (cb->mode)
692 case SELECT_MODE_CHAR:
693 cb->origin_start_col = cb->start.col;
694 cb->word_end_col = clip_get_line_end((int)cb->start.lnum);
695 break;
697 case SELECT_MODE_WORD:
698 clip_get_word_boundaries(cb, (int)cb->start.lnum, cb->start.col);
699 cb->origin_start_col = cb->word_start_col;
700 cb->origin_end_col = cb->word_end_col;
702 clip_invert_area((int)cb->start.lnum, cb->word_start_col,
703 (int)cb->end.lnum, cb->word_end_col, CLIP_SET);
704 cb->start.col = cb->word_start_col;
705 cb->end.col = cb->word_end_col;
706 break;
708 case SELECT_MODE_LINE:
709 clip_invert_area((int)cb->start.lnum, 0, (int)cb->start.lnum,
710 (int)Columns, CLIP_SET);
711 cb->start.col = 0;
712 cb->end.col = Columns;
713 break;
716 cb->prev = cb->start;
718 #ifdef DEBUG_SELECTION
719 printf("Selection started at (%u,%u)\n", cb->start.lnum, cb->start.col);
720 #endif
724 * Continue processing the selection
726 void
727 clip_process_selection(button, col, row, repeated_click)
728 int button;
729 int col;
730 int row;
731 int_u repeated_click;
733 VimClipboard *cb = &clip_star;
734 int diff;
735 int slen = 1; /* cursor shape width */
737 if (button == MOUSE_RELEASE)
739 /* Check to make sure we have something selected */
740 if (cb->start.lnum == cb->end.lnum && cb->start.col == cb->end.col)
742 #ifdef FEAT_GUI
743 if (gui.in_use)
744 gui_update_cursor(FALSE, FALSE);
745 #endif
746 cb->state = SELECT_CLEARED;
747 return;
750 #ifdef DEBUG_SELECTION
751 printf("Selection ended: (%u,%u) to (%u,%u)\n", cb->start.lnum,
752 cb->start.col, cb->end.lnum, cb->end.col);
753 #endif
754 if (clip_isautosel()
755 || (
756 #ifdef FEAT_GUI
757 gui.in_use ? (vim_strchr(p_go, GO_ASELML) != NULL) :
758 #endif
759 clip_autoselectml))
760 clip_copy_modeless_selection(FALSE);
761 #ifdef FEAT_GUI
762 if (gui.in_use)
763 gui_update_cursor(FALSE, FALSE);
764 #endif
766 cb->state = SELECT_DONE;
767 return;
770 row = check_row(row);
771 col = check_col(col);
772 #ifdef FEAT_MBYTE
773 col = mb_fix_col(col, row);
774 #endif
776 if (col == (int)cb->prev.col && row == cb->prev.lnum && !repeated_click)
777 return;
780 * When extending the selection with the right mouse button, swap the
781 * start and end if the position is before half the selection
783 if (cb->state == SELECT_DONE && button == MOUSE_RIGHT)
786 * If the click is before the start, or the click is inside the
787 * selection and the start is the closest side, set the origin to the
788 * end of the selection.
790 if (clip_compare_pos(row, col, (int)cb->start.lnum, cb->start.col) < 0
791 || (clip_compare_pos(row, col,
792 (int)cb->end.lnum, cb->end.col) < 0
793 && (((cb->start.lnum == cb->end.lnum
794 && cb->end.col - col > col - cb->start.col))
795 || ((diff = (cb->end.lnum - row) -
796 (row - cb->start.lnum)) > 0
797 || (diff == 0 && col < (int)(cb->start.col +
798 cb->end.col) / 2)))))
800 cb->origin_row = (short_u)cb->end.lnum;
801 cb->origin_start_col = cb->end.col - 1;
802 cb->origin_end_col = cb->end.col;
804 else
806 cb->origin_row = (short_u)cb->start.lnum;
807 cb->origin_start_col = cb->start.col;
808 cb->origin_end_col = cb->start.col;
810 if (cb->mode == SELECT_MODE_WORD && !repeated_click)
811 cb->mode = SELECT_MODE_CHAR;
814 /* set state, for when using the right mouse button */
815 cb->state = SELECT_IN_PROGRESS;
817 #ifdef DEBUG_SELECTION
818 printf("Selection extending to (%d,%d)\n", row, col);
819 #endif
821 if (repeated_click && ++cb->mode > SELECT_MODE_LINE)
822 cb->mode = SELECT_MODE_CHAR;
824 switch (cb->mode)
826 case SELECT_MODE_CHAR:
827 /* If we're on a different line, find where the line ends */
828 if (row != cb->prev.lnum)
829 cb->word_end_col = clip_get_line_end(row);
831 /* See if we are before or after the origin of the selection */
832 if (clip_compare_pos(row, col, cb->origin_row,
833 cb->origin_start_col) >= 0)
835 if (col >= (int)cb->word_end_col)
836 clip_update_modeless_selection(cb, cb->origin_row,
837 cb->origin_start_col, row, (int)Columns);
838 else
840 #ifdef FEAT_MBYTE
841 if (has_mbyte && mb_lefthalve(row, col))
842 slen = 2;
843 #endif
844 clip_update_modeless_selection(cb, cb->origin_row,
845 cb->origin_start_col, row, col + slen);
848 else
850 #ifdef FEAT_MBYTE
851 if (has_mbyte
852 && mb_lefthalve(cb->origin_row, cb->origin_start_col))
853 slen = 2;
854 #endif
855 if (col >= (int)cb->word_end_col)
856 clip_update_modeless_selection(cb, row, cb->word_end_col,
857 cb->origin_row, cb->origin_start_col + slen);
858 else
859 clip_update_modeless_selection(cb, row, col,
860 cb->origin_row, cb->origin_start_col + slen);
862 break;
864 case SELECT_MODE_WORD:
865 /* If we are still within the same word, do nothing */
866 if (row == cb->prev.lnum && col >= (int)cb->word_start_col
867 && col < (int)cb->word_end_col && !repeated_click)
868 return;
870 /* Get new word boundaries */
871 clip_get_word_boundaries(cb, row, col);
873 /* Handle being after the origin point of selection */
874 if (clip_compare_pos(row, col, cb->origin_row,
875 cb->origin_start_col) >= 0)
876 clip_update_modeless_selection(cb, cb->origin_row,
877 cb->origin_start_col, row, cb->word_end_col);
878 else
879 clip_update_modeless_selection(cb, row, cb->word_start_col,
880 cb->origin_row, cb->origin_end_col);
881 break;
883 case SELECT_MODE_LINE:
884 if (row == cb->prev.lnum && !repeated_click)
885 return;
887 if (clip_compare_pos(row, col, cb->origin_row,
888 cb->origin_start_col) >= 0)
889 clip_update_modeless_selection(cb, cb->origin_row, 0, row,
890 (int)Columns);
891 else
892 clip_update_modeless_selection(cb, row, 0, cb->origin_row,
893 (int)Columns);
894 break;
897 cb->prev.lnum = row;
898 cb->prev.col = col;
900 #ifdef DEBUG_SELECTION
901 printf("Selection is: (%u,%u) to (%u,%u)\n", cb->start.lnum,
902 cb->start.col, cb->end.lnum, cb->end.col);
903 #endif
906 #if 0 /* not used */
908 * Called after an Expose event to redraw the selection
910 void
911 clip_redraw_selection(x, y, w, h)
912 int x;
913 int y;
914 int w;
915 int h;
917 VimClipboard *cb = &clip_star;
918 int row1, col1, row2, col2;
919 int row;
920 int start;
921 int end;
923 if (cb->state == SELECT_CLEARED)
924 return;
926 row1 = check_row(Y_2_ROW(y));
927 col1 = check_col(X_2_COL(x));
928 row2 = check_row(Y_2_ROW(y + h - 1));
929 col2 = check_col(X_2_COL(x + w - 1));
931 /* Limit the rows that need to be re-drawn */
932 if (cb->start.lnum > row1)
933 row1 = cb->start.lnum;
934 if (cb->end.lnum < row2)
935 row2 = cb->end.lnum;
937 /* Look at each row that might need to be re-drawn */
938 for (row = row1; row <= row2; row++)
940 /* For the first selection row, use the starting selection column */
941 if (row == cb->start.lnum)
942 start = cb->start.col;
943 else
944 start = 0;
946 /* For the last selection row, use the ending selection column */
947 if (row == cb->end.lnum)
948 end = cb->end.col;
949 else
950 end = Columns;
952 if (col1 > start)
953 start = col1;
955 if (col2 < end)
956 end = col2 + 1;
958 if (end > start)
959 gui_mch_invert_rectangle(row, start, 1, end - start);
962 #endif
964 # if defined(FEAT_GUI) || defined(PROTO)
966 * Redraw part of the selection if character at "row,col" is inside of it.
967 * Only used for the GUI.
969 void
970 clip_may_redraw_selection(row, col, len)
971 int row, col;
972 int len;
974 int start = col;
975 int end = col + len;
977 if (clip_star.state != SELECT_CLEARED
978 && row >= clip_star.start.lnum
979 && row <= clip_star.end.lnum)
981 if (row == clip_star.start.lnum && start < (int)clip_star.start.col)
982 start = clip_star.start.col;
983 if (row == clip_star.end.lnum && end > (int)clip_star.end.col)
984 end = clip_star.end.col;
985 if (end > start)
986 clip_invert_area(row, start, row, end, 0);
989 # endif
992 * Called from outside to clear selected region from the display
994 void
995 clip_clear_selection()
997 VimClipboard *cb = &clip_star;
999 if (cb->state == SELECT_CLEARED)
1000 return;
1002 clip_invert_area((int)cb->start.lnum, cb->start.col, (int)cb->end.lnum,
1003 cb->end.col, CLIP_CLEAR);
1004 cb->state = SELECT_CLEARED;
1008 * Clear the selection if any lines from "row1" to "row2" are inside of it.
1010 void
1011 clip_may_clear_selection(row1, row2)
1012 int row1, row2;
1014 if (clip_star.state == SELECT_DONE
1015 && row2 >= clip_star.start.lnum
1016 && row1 <= clip_star.end.lnum)
1017 clip_clear_selection();
1021 * Called before the screen is scrolled up or down. Adjusts the line numbers
1022 * of the selection. Call with big number when clearing the screen.
1024 void
1025 clip_scroll_selection(rows)
1026 int rows; /* negative for scroll down */
1028 int lnum;
1030 if (clip_star.state == SELECT_CLEARED)
1031 return;
1033 lnum = clip_star.start.lnum - rows;
1034 if (lnum <= 0)
1035 clip_star.start.lnum = 0;
1036 else if (lnum >= screen_Rows) /* scrolled off of the screen */
1037 clip_star.state = SELECT_CLEARED;
1038 else
1039 clip_star.start.lnum = lnum;
1041 lnum = clip_star.end.lnum - rows;
1042 if (lnum < 0) /* scrolled off of the screen */
1043 clip_star.state = SELECT_CLEARED;
1044 else if (lnum >= screen_Rows)
1045 clip_star.end.lnum = screen_Rows - 1;
1046 else
1047 clip_star.end.lnum = lnum;
1051 * Invert a region of the display between a starting and ending row and column
1052 * Values for "how":
1053 * CLIP_CLEAR: undo inversion
1054 * CLIP_SET: set inversion
1055 * CLIP_TOGGLE: set inversion if pos1 < pos2, undo inversion otherwise.
1056 * 0: invert (GUI only).
1058 static void
1059 clip_invert_area(row1, col1, row2, col2, how)
1060 int row1;
1061 int col1;
1062 int row2;
1063 int col2;
1064 int how;
1066 int invert = FALSE;
1068 if (how == CLIP_SET)
1069 invert = TRUE;
1071 /* Swap the from and to positions so the from is always before */
1072 if (clip_compare_pos(row1, col1, row2, col2) > 0)
1074 int tmp_row, tmp_col;
1076 tmp_row = row1;
1077 tmp_col = col1;
1078 row1 = row2;
1079 col1 = col2;
1080 row2 = tmp_row;
1081 col2 = tmp_col;
1083 else if (how == CLIP_TOGGLE)
1084 invert = TRUE;
1086 /* If all on the same line, do it the easy way */
1087 if (row1 == row2)
1089 clip_invert_rectangle(row1, col1, 1, col2 - col1, invert);
1091 else
1093 /* Handle a piece of the first line */
1094 if (col1 > 0)
1096 clip_invert_rectangle(row1, col1, 1, (int)Columns - col1, invert);
1097 row1++;
1100 /* Handle a piece of the last line */
1101 if (col2 < Columns - 1)
1103 clip_invert_rectangle(row2, 0, 1, col2, invert);
1104 row2--;
1107 /* Handle the rectangle thats left */
1108 if (row2 >= row1)
1109 clip_invert_rectangle(row1, 0, row2 - row1 + 1, (int)Columns,
1110 invert);
1115 * Invert or un-invert a rectangle of the screen.
1116 * "invert" is true if the result is inverted.
1118 static void
1119 clip_invert_rectangle(row, col, height, width, invert)
1120 int row;
1121 int col;
1122 int height;
1123 int width;
1124 int invert;
1126 #ifdef FEAT_GUI
1127 if (gui.in_use)
1128 # ifdef FEAT_GUI_MACVIM
1129 gui_mch_invert_rectangle(row, col, height, width, invert);
1130 # else
1131 gui_mch_invert_rectangle(row, col, height, width);
1132 #endif
1133 else
1134 #endif
1135 screen_draw_rectangle(row, col, height, width, invert);
1139 * Copy the currently selected area into the '*' register so it will be
1140 * available for pasting.
1141 * When "both" is TRUE also copy to the '+' register.
1143 void
1144 clip_copy_modeless_selection(both)
1145 int both UNUSED;
1147 char_u *buffer;
1148 char_u *bufp;
1149 int row;
1150 int start_col;
1151 int end_col;
1152 int line_end_col;
1153 int add_newline_flag = FALSE;
1154 int len;
1155 #ifdef FEAT_MBYTE
1156 char_u *p;
1157 #endif
1158 int row1 = clip_star.start.lnum;
1159 int col1 = clip_star.start.col;
1160 int row2 = clip_star.end.lnum;
1161 int col2 = clip_star.end.col;
1163 /* Can't use ScreenLines unless initialized */
1164 if (ScreenLines == NULL)
1165 return;
1168 * Make sure row1 <= row2, and if row1 == row2 that col1 <= col2.
1170 if (row1 > row2)
1172 row = row1; row1 = row2; row2 = row;
1173 row = col1; col1 = col2; col2 = row;
1175 else if (row1 == row2 && col1 > col2)
1177 row = col1; col1 = col2; col2 = row;
1179 #ifdef FEAT_MBYTE
1180 /* correct starting point for being on right halve of double-wide char */
1181 p = ScreenLines + LineOffset[row1];
1182 if (enc_dbcs != 0)
1183 col1 -= (*mb_head_off)(p, p + col1);
1184 else if (enc_utf8 && p[col1] == 0)
1185 --col1;
1186 #endif
1188 /* Create a temporary buffer for storing the text */
1189 len = (row2 - row1 + 1) * Columns + 1;
1190 #ifdef FEAT_MBYTE
1191 if (enc_dbcs != 0)
1192 len *= 2; /* max. 2 bytes per display cell */
1193 else if (enc_utf8)
1194 len *= MB_MAXBYTES;
1195 #endif
1196 buffer = lalloc((long_u)len, TRUE);
1197 if (buffer == NULL) /* out of memory */
1198 return;
1200 /* Process each row in the selection */
1201 for (bufp = buffer, row = row1; row <= row2; row++)
1203 if (row == row1)
1204 start_col = col1;
1205 else
1206 start_col = 0;
1208 if (row == row2)
1209 end_col = col2;
1210 else
1211 end_col = Columns;
1213 line_end_col = clip_get_line_end(row);
1215 /* See if we need to nuke some trailing whitespace */
1216 if (end_col >= Columns && (row < row2 || end_col > line_end_col))
1218 /* Get rid of trailing whitespace */
1219 end_col = line_end_col;
1220 if (end_col < start_col)
1221 end_col = start_col;
1223 /* If the last line extended to the end, add an extra newline */
1224 if (row == row2)
1225 add_newline_flag = TRUE;
1228 /* If after the first row, we need to always add a newline */
1229 if (row > row1 && !LineWraps[row - 1])
1230 *bufp++ = NL;
1232 if (row < screen_Rows && end_col <= screen_Columns)
1234 #ifdef FEAT_MBYTE
1235 if (enc_dbcs != 0)
1237 int i;
1239 p = ScreenLines + LineOffset[row];
1240 for (i = start_col; i < end_col; ++i)
1241 if (enc_dbcs == DBCS_JPNU && p[i] == 0x8e)
1243 /* single-width double-byte char */
1244 *bufp++ = 0x8e;
1245 *bufp++ = ScreenLines2[LineOffset[row] + i];
1247 else
1249 *bufp++ = p[i];
1250 if (MB_BYTE2LEN(p[i]) == 2)
1251 *bufp++ = p[++i];
1254 else if (enc_utf8)
1256 int off;
1257 int i;
1258 int ci;
1260 off = LineOffset[row];
1261 for (i = start_col; i < end_col; ++i)
1263 /* The base character is either in ScreenLinesUC[] or
1264 * ScreenLines[]. */
1265 if (ScreenLinesUC[off + i] == 0)
1266 *bufp++ = ScreenLines[off + i];
1267 else
1269 bufp += utf_char2bytes(ScreenLinesUC[off + i], bufp);
1270 for (ci = 0; ci < Screen_mco; ++ci)
1272 /* Add a composing character. */
1273 if (ScreenLinesC[ci][off + i] == 0)
1274 break;
1275 bufp += utf_char2bytes(ScreenLinesC[ci][off + i],
1276 bufp);
1279 /* Skip right halve of double-wide character. */
1280 if (ScreenLines[off + i + 1] == 0)
1281 ++i;
1284 else
1285 #endif
1287 STRNCPY(bufp, ScreenLines + LineOffset[row] + start_col,
1288 end_col - start_col);
1289 bufp += end_col - start_col;
1294 /* Add a newline at the end if the selection ended there */
1295 if (add_newline_flag)
1296 *bufp++ = NL;
1298 /* First cleanup any old selection and become the owner. */
1299 clip_free_selection(&clip_star);
1300 clip_own_selection(&clip_star);
1302 /* Yank the text into the '*' register. */
1303 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_star);
1305 /* Make the register contents available to the outside world. */
1306 clip_gen_set_selection(&clip_star);
1308 #ifdef FEAT_X11
1309 if (both)
1311 /* Do the same for the '+' register. */
1312 clip_free_selection(&clip_plus);
1313 clip_own_selection(&clip_plus);
1314 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_plus);
1315 clip_gen_set_selection(&clip_plus);
1317 #endif
1318 vim_free(buffer);
1322 * Find the starting and ending positions of the word at the given row and
1323 * column. Only white-separated words are recognized here.
1325 #define CHAR_CLASS(c) (c <= ' ' ? ' ' : vim_iswordc(c))
1327 static void
1328 clip_get_word_boundaries(cb, row, col)
1329 VimClipboard *cb;
1330 int row;
1331 int col;
1333 int start_class;
1334 int temp_col;
1335 char_u *p;
1336 #ifdef FEAT_MBYTE
1337 int mboff;
1338 #endif
1340 if (row >= screen_Rows || col >= screen_Columns || ScreenLines == NULL)
1341 return;
1343 p = ScreenLines + LineOffset[row];
1344 #ifdef FEAT_MBYTE
1345 /* Correct for starting in the right halve of a double-wide char */
1346 if (enc_dbcs != 0)
1347 col -= dbcs_screen_head_off(p, p + col);
1348 else if (enc_utf8 && p[col] == 0)
1349 --col;
1350 #endif
1351 start_class = CHAR_CLASS(p[col]);
1353 temp_col = col;
1354 for ( ; temp_col > 0; temp_col--)
1355 #ifdef FEAT_MBYTE
1356 if (enc_dbcs != 0
1357 && (mboff = dbcs_screen_head_off(p, p + temp_col - 1)) > 0)
1358 temp_col -= mboff;
1359 else
1360 #endif
1361 if (CHAR_CLASS(p[temp_col - 1]) != start_class
1362 #ifdef FEAT_MBYTE
1363 && !(enc_utf8 && p[temp_col - 1] == 0)
1364 #endif
1366 break;
1367 cb->word_start_col = temp_col;
1369 temp_col = col;
1370 for ( ; temp_col < screen_Columns; temp_col++)
1371 #ifdef FEAT_MBYTE
1372 if (enc_dbcs != 0 && dbcs_ptr2cells(p + temp_col) == 2)
1373 ++temp_col;
1374 else
1375 #endif
1376 if (CHAR_CLASS(p[temp_col]) != start_class
1377 #ifdef FEAT_MBYTE
1378 && !(enc_utf8 && p[temp_col] == 0)
1379 #endif
1381 break;
1382 cb->word_end_col = temp_col;
1386 * Find the column position for the last non-whitespace character on the given
1387 * line.
1389 static int
1390 clip_get_line_end(row)
1391 int row;
1393 int i;
1395 if (row >= screen_Rows || ScreenLines == NULL)
1396 return 0;
1397 for (i = screen_Columns; i > 0; i--)
1398 if (ScreenLines[LineOffset[row] + i - 1] != ' ')
1399 break;
1400 return i;
1404 * Update the currently selected region by adding and/or subtracting from the
1405 * beginning or end and inverting the changed area(s).
1407 static void
1408 clip_update_modeless_selection(cb, row1, col1, row2, col2)
1409 VimClipboard *cb;
1410 int row1;
1411 int col1;
1412 int row2;
1413 int col2;
1415 /* See if we changed at the beginning of the selection */
1416 if (row1 != cb->start.lnum || col1 != (int)cb->start.col)
1418 clip_invert_area(row1, col1, (int)cb->start.lnum, cb->start.col,
1419 CLIP_TOGGLE);
1420 cb->start.lnum = row1;
1421 cb->start.col = col1;
1424 /* See if we changed at the end of the selection */
1425 if (row2 != cb->end.lnum || col2 != (int)cb->end.col)
1427 clip_invert_area((int)cb->end.lnum, cb->end.col, row2, col2,
1428 CLIP_TOGGLE);
1429 cb->end.lnum = row2;
1430 cb->end.col = col2;
1435 clip_gen_own_selection(cbd)
1436 VimClipboard *cbd;
1438 #ifdef FEAT_XCLIPBOARD
1439 # ifdef FEAT_GUI
1440 if (gui.in_use)
1441 return clip_mch_own_selection(cbd);
1442 else
1443 # endif
1444 return clip_xterm_own_selection(cbd);
1445 #else
1446 return clip_mch_own_selection(cbd);
1447 #endif
1450 void
1451 clip_gen_lose_selection(cbd)
1452 VimClipboard *cbd;
1454 #ifdef FEAT_XCLIPBOARD
1455 # ifdef FEAT_GUI
1456 if (gui.in_use)
1457 clip_mch_lose_selection(cbd);
1458 else
1459 # endif
1460 clip_xterm_lose_selection(cbd);
1461 #else
1462 clip_mch_lose_selection(cbd);
1463 #endif
1466 void
1467 clip_gen_set_selection(cbd)
1468 VimClipboard *cbd;
1470 #ifdef FEAT_XCLIPBOARD
1471 # ifdef FEAT_GUI
1472 if (gui.in_use)
1473 clip_mch_set_selection(cbd);
1474 else
1475 # endif
1476 clip_xterm_set_selection(cbd);
1477 #else
1478 clip_mch_set_selection(cbd);
1479 #endif
1482 void
1483 clip_gen_request_selection(cbd)
1484 VimClipboard *cbd;
1486 #ifdef FEAT_XCLIPBOARD
1487 # ifdef FEAT_GUI
1488 if (gui.in_use)
1489 clip_mch_request_selection(cbd);
1490 else
1491 # endif
1492 clip_xterm_request_selection(cbd);
1493 #else
1494 clip_mch_request_selection(cbd);
1495 #endif
1498 #endif /* FEAT_CLIPBOARD */
1500 /*****************************************************************************
1501 * Functions that handle the input buffer.
1502 * This is used for any GUI version, and the unix terminal version.
1504 * For Unix, the input characters are buffered to be able to check for a
1505 * CTRL-C. This should be done with signals, but I don't know how to do that
1506 * in a portable way for a tty in RAW mode.
1508 * For the client-server code in the console the received keys are put in the
1509 * input buffer.
1512 #if defined(USE_INPUT_BUF) || defined(PROTO)
1515 * Internal typeahead buffer. Includes extra space for long key code
1516 * descriptions which would otherwise overflow. The buffer is considered full
1517 * when only this extra space (or part of it) remains.
1519 #if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
1520 || defined(FEAT_CLIENTSERVER)
1522 * Sun WorkShop and NetBeans stuff debugger commands into the input buffer.
1523 * This requires a larger buffer...
1524 * (Madsen) Go with this for remote input as well ...
1526 # define INBUFLEN 4096
1527 #else
1528 # define INBUFLEN 250
1529 #endif
1531 static char_u inbuf[INBUFLEN + MAX_KEY_CODE_LEN];
1532 static int inbufcount = 0; /* number of chars in inbuf[] */
1535 * vim_is_input_buf_full(), vim_is_input_buf_empty(), add_to_input_buf(), and
1536 * trash_input_buf() are functions for manipulating the input buffer. These
1537 * are used by the gui_* calls when a GUI is used to handle keyboard input.
1541 vim_is_input_buf_full()
1543 return (inbufcount >= INBUFLEN);
1547 vim_is_input_buf_empty()
1549 return (inbufcount == 0);
1552 #if defined(FEAT_OLE) || defined(PROTO)
1554 vim_free_in_input_buf()
1556 return (INBUFLEN - inbufcount);
1558 #endif
1560 #if defined(FEAT_GUI_GTK) || defined(PROTO)
1562 vim_used_in_input_buf()
1564 return inbufcount;
1566 #endif
1568 #if defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) || defined(PROTO)
1570 * Return the current contents of the input buffer and make it empty.
1571 * The returned pointer must be passed to set_input_buf() later.
1573 char_u *
1574 get_input_buf()
1576 garray_T *gap;
1578 /* We use a growarray to store the data pointer and the length. */
1579 gap = (garray_T *)alloc((unsigned)sizeof(garray_T));
1580 if (gap != NULL)
1582 /* Add one to avoid a zero size. */
1583 gap->ga_data = alloc((unsigned)inbufcount + 1);
1584 if (gap->ga_data != NULL)
1585 mch_memmove(gap->ga_data, inbuf, (size_t)inbufcount);
1586 gap->ga_len = inbufcount;
1588 trash_input_buf();
1589 return (char_u *)gap;
1593 * Restore the input buffer with a pointer returned from get_input_buf().
1594 * The allocated memory is freed, this only works once!
1596 void
1597 set_input_buf(p)
1598 char_u *p;
1600 garray_T *gap = (garray_T *)p;
1602 if (gap != NULL)
1604 if (gap->ga_data != NULL)
1606 mch_memmove(inbuf, gap->ga_data, gap->ga_len);
1607 inbufcount = gap->ga_len;
1608 vim_free(gap->ga_data);
1610 vim_free(gap);
1613 #endif
1615 #if defined(FEAT_GUI) \
1616 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE) \
1617 || defined(FEAT_XCLIPBOARD) || defined(VMS) \
1618 || defined(FEAT_SNIFF) || defined(FEAT_CLIENTSERVER) \
1619 || defined(PROTO)
1621 * Add the given bytes to the input buffer
1622 * Special keys start with CSI. A real CSI must have been translated to
1623 * CSI KS_EXTRA KE_CSI. K_SPECIAL doesn't require translation.
1625 void
1626 add_to_input_buf(s, len)
1627 char_u *s;
1628 int len;
1630 if (inbufcount + len > INBUFLEN + MAX_KEY_CODE_LEN)
1631 return; /* Shouldn't ever happen! */
1633 #ifdef FEAT_HANGULIN
1634 if ((State & (INSERT|CMDLINE)) && hangul_input_state_get())
1635 if ((len = hangul_input_process(s, len)) == 0)
1636 return;
1637 #endif
1639 while (len--)
1640 inbuf[inbufcount++] = *s++;
1642 #endif
1644 #if ((defined(FEAT_XIM) || defined(FEAT_DND)) && defined(FEAT_GUI_GTK)) \
1645 || defined(FEAT_GUI_MSWIN) \
1646 || defined(FEAT_GUI_MAC) \
1647 || (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) \
1648 || (defined(FEAT_GUI) && (!defined(USE_ON_FLY_SCROLL) \
1649 || defined(FEAT_MENU))) \
1650 || defined(PROTO)
1652 * Add "str[len]" to the input buffer while escaping CSI bytes.
1654 void
1655 add_to_input_buf_csi(char_u *str, int len)
1657 int i;
1658 char_u buf[2];
1660 for (i = 0; i < len; ++i)
1662 add_to_input_buf(str + i, 1);
1663 if (str[i] == CSI)
1665 /* Turn CSI into K_CSI. */
1666 buf[0] = KS_EXTRA;
1667 buf[1] = (int)KE_CSI;
1668 add_to_input_buf(buf, 2);
1672 #endif
1674 #if defined(FEAT_HANGULIN) || defined(PROTO)
1675 void
1676 push_raw_key (s, len)
1677 char_u *s;
1678 int len;
1680 while (len--)
1681 inbuf[inbufcount++] = *s++;
1683 #endif
1685 #if defined(FEAT_GUI) || defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) \
1686 || defined(PROTO)
1687 /* Remove everything from the input buffer. Called when ^C is found */
1688 void
1689 trash_input_buf()
1691 inbufcount = 0;
1693 #endif
1696 * Read as much data from the input buffer as possible up to maxlen, and store
1697 * it in buf.
1698 * Note: this function used to be Read() in unix.c
1701 read_from_input_buf(buf, maxlen)
1702 char_u *buf;
1703 long maxlen;
1705 if (inbufcount == 0) /* if the buffer is empty, fill it */
1706 fill_input_buf(TRUE);
1707 if (maxlen > inbufcount)
1708 maxlen = inbufcount;
1709 mch_memmove(buf, inbuf, (size_t)maxlen);
1710 inbufcount -= maxlen;
1711 if (inbufcount)
1712 mch_memmove(inbuf, inbuf + maxlen, (size_t)inbufcount);
1713 return (int)maxlen;
1716 void
1717 fill_input_buf(exit_on_error)
1718 int exit_on_error UNUSED;
1720 #if defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X_UNIX)
1721 int len;
1722 int try;
1723 static int did_read_something = FALSE;
1724 # ifdef FEAT_MBYTE
1725 static char_u *rest = NULL; /* unconverted rest of previous read */
1726 static int restlen = 0;
1727 int unconverted;
1728 # endif
1729 #endif
1731 #ifdef FEAT_GUI
1732 if (gui.in_use
1733 # ifdef NO_CONSOLE_INPUT
1734 /* Don't use the GUI input when the window hasn't been opened yet.
1735 * We get here from ui_inchar() when we should try reading from stdin. */
1736 && !no_console_input()
1737 # endif
1740 gui_mch_update();
1741 return;
1743 #endif
1744 #if defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X_UNIX)
1745 if (vim_is_input_buf_full())
1746 return;
1748 * Fill_input_buf() is only called when we really need a character.
1749 * If we can't get any, but there is some in the buffer, just return.
1750 * If we can't get any, and there isn't any in the buffer, we give up and
1751 * exit Vim.
1753 # ifdef __BEOS__
1755 * On the BeBox version (for now), all input is secretly performed within
1756 * beos_select() which is called from RealWaitForChar().
1758 while (!vim_is_input_buf_full() && RealWaitForChar(read_cmd_fd, 0, NULL))
1760 len = inbufcount;
1761 inbufcount = 0;
1762 # else
1764 # ifdef FEAT_SNIFF
1765 if (sniff_request_waiting)
1767 add_to_input_buf((char_u *)"\233sniff",6); /* results in K_SNIFF */
1768 sniff_request_waiting = 0;
1769 want_sniff_request = 0;
1770 return;
1772 # endif
1774 # ifdef FEAT_MBYTE
1775 if (rest != NULL)
1777 /* Use remainder of previous call, starts with an invalid character
1778 * that may become valid when reading more. */
1779 if (restlen > INBUFLEN - inbufcount)
1780 unconverted = INBUFLEN - inbufcount;
1781 else
1782 unconverted = restlen;
1783 mch_memmove(inbuf + inbufcount, rest, unconverted);
1784 if (unconverted == restlen)
1786 vim_free(rest);
1787 rest = NULL;
1789 else
1791 restlen -= unconverted;
1792 mch_memmove(rest, rest + unconverted, restlen);
1794 inbufcount += unconverted;
1796 else
1797 unconverted = 0;
1798 #endif
1800 len = 0; /* to avoid gcc warning */
1801 for (try = 0; try < 100; ++try)
1803 # ifdef VMS
1804 len = vms_read(
1805 # else
1806 len = read(read_cmd_fd,
1807 # endif
1808 (char *)inbuf + inbufcount, (size_t)((INBUFLEN - inbufcount)
1809 # ifdef FEAT_MBYTE
1810 / input_conv.vc_factor
1811 # endif
1813 # if 0
1814 ) /* avoid syntax highlight error */
1815 # endif
1817 if (len > 0 || got_int)
1818 break;
1820 * If reading stdin results in an error, continue reading stderr.
1821 * This helps when using "foo | xargs vim".
1823 if (!did_read_something && !isatty(read_cmd_fd) && read_cmd_fd == 0)
1825 int m = cur_tmode;
1827 /* We probably set the wrong file descriptor to raw mode. Switch
1828 * back to cooked mode, use another descriptor and set the mode to
1829 * what it was. */
1830 settmode(TMODE_COOK);
1831 #ifdef HAVE_DUP
1832 /* Use stderr for stdin, also works for shell commands. */
1833 close(0);
1834 ignored = dup(2);
1835 #else
1836 read_cmd_fd = 2; /* read from stderr instead of stdin */
1837 #endif
1838 settmode(m);
1840 if (!exit_on_error)
1841 return;
1843 # endif
1844 if (len <= 0 && !got_int)
1845 read_error_exit();
1846 if (len > 0)
1847 did_read_something = TRUE;
1848 if (got_int)
1850 /* Interrupted, pretend a CTRL-C was typed. */
1851 inbuf[0] = 3;
1852 inbufcount = 1;
1854 else
1856 # ifdef FEAT_MBYTE
1858 * May perform conversion on the input characters.
1859 * Include the unconverted rest of the previous call.
1860 * If there is an incomplete char at the end it is kept for the next
1861 * time, reading more bytes should make conversion possible.
1862 * Don't do this in the unlikely event that the input buffer is too
1863 * small ("rest" still contains more bytes).
1865 if (input_conv.vc_type != CONV_NONE)
1867 inbufcount -= unconverted;
1868 len = convert_input_safe(inbuf + inbufcount,
1869 len + unconverted, INBUFLEN - inbufcount,
1870 rest == NULL ? &rest : NULL, &restlen);
1872 # endif
1873 while (len-- > 0)
1876 * if a CTRL-C was typed, remove it from the buffer and set got_int
1878 if (inbuf[inbufcount] == 3 && ctrl_c_interrupts)
1880 /* remove everything typed before the CTRL-C */
1881 mch_memmove(inbuf, inbuf + inbufcount, (size_t)(len + 1));
1882 inbufcount = 0;
1883 got_int = TRUE;
1885 ++inbufcount;
1888 #endif /* UNIX or OS2 or VMS*/
1890 #endif /* defined(UNIX) || defined(FEAT_GUI) || defined(OS2) || defined(VMS) */
1893 * Exit because of an input read error.
1895 void
1896 read_error_exit()
1898 if (silent_mode) /* Normal way to exit for "ex -s" */
1899 getout(0);
1900 STRCPY(IObuff, _("Vim: Error reading input, exiting...\n"));
1901 preserve_exit();
1904 #if defined(CURSOR_SHAPE) || defined(PROTO)
1906 * May update the shape of the cursor.
1908 void
1909 ui_cursor_shape()
1911 # ifdef FEAT_GUI
1912 if (gui.in_use)
1913 gui_update_cursor_later();
1914 else
1915 # endif
1916 term_cursor_shape();
1918 # ifdef MCH_CURSOR_SHAPE
1919 mch_update_cursor();
1920 # endif
1922 #endif
1924 #if defined(FEAT_CLIPBOARD) || defined(FEAT_GUI) || defined(FEAT_RIGHTLEFT) \
1925 || defined(FEAT_MBYTE) || defined(PROTO)
1927 * Check bounds for column number
1930 check_col(col)
1931 int col;
1933 if (col < 0)
1934 return 0;
1935 if (col >= (int)screen_Columns)
1936 return (int)screen_Columns - 1;
1937 return col;
1941 * Check bounds for row number
1944 check_row(row)
1945 int row;
1947 if (row < 0)
1948 return 0;
1949 if (row >= (int)screen_Rows)
1950 return (int)screen_Rows - 1;
1951 return row;
1953 #endif
1956 * Stuff for the X clipboard. Shared between VMS and Unix.
1959 #if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) || defined(PROTO)
1960 # include <X11/Xatom.h>
1961 # include <X11/Intrinsic.h>
1964 * Open the application context (if it hasn't been opened yet).
1965 * Used for Motif and Athena GUI and the xterm clipboard.
1967 void
1968 open_app_context()
1970 if (app_context == NULL)
1972 XtToolkitInitialize();
1973 app_context = XtCreateApplicationContext();
1977 static Atom vim_atom; /* Vim's own special selection format */
1978 #ifdef FEAT_MBYTE
1979 static Atom vimenc_atom; /* Vim's extended selection format */
1980 #endif
1981 static Atom compound_text_atom;
1982 static Atom text_atom;
1983 static Atom targets_atom;
1985 void
1986 x11_setup_atoms(dpy)
1987 Display *dpy;
1989 vim_atom = XInternAtom(dpy, VIM_ATOM_NAME, False);
1990 #ifdef FEAT_MBYTE
1991 vimenc_atom = XInternAtom(dpy, VIMENC_ATOM_NAME,False);
1992 #endif
1993 compound_text_atom = XInternAtom(dpy, "COMPOUND_TEXT", False);
1994 text_atom = XInternAtom(dpy, "TEXT", False);
1995 targets_atom = XInternAtom(dpy, "TARGETS", False);
1996 clip_star.sel_atom = XA_PRIMARY;
1997 clip_plus.sel_atom = XInternAtom(dpy, "CLIPBOARD", False);
2001 * X Selection stuff, for cutting and pasting text to other windows.
2004 static void clip_x11_request_selection_cb __ARGS((Widget, XtPointer, Atom *, Atom *, XtPointer, long_u *, int *));
2006 static void
2007 clip_x11_request_selection_cb(w, success, sel_atom, type, value, length,
2008 format)
2009 Widget w UNUSED;
2010 XtPointer success;
2011 Atom *sel_atom;
2012 Atom *type;
2013 XtPointer value;
2014 long_u *length;
2015 int *format;
2017 int motion_type;
2018 long_u len;
2019 char_u *p;
2020 char **text_list = NULL;
2021 VimClipboard *cbd;
2022 #ifdef FEAT_MBYTE
2023 char_u *tmpbuf = NULL;
2024 #endif
2026 if (*sel_atom == clip_plus.sel_atom)
2027 cbd = &clip_plus;
2028 else
2029 cbd = &clip_star;
2031 if (value == NULL || *length == 0)
2033 clip_free_selection(cbd); /* nothing received, clear register */
2034 *(int *)success = FALSE;
2035 return;
2037 motion_type = MCHAR;
2038 p = (char_u *)value;
2039 len = *length;
2040 if (*type == vim_atom)
2042 motion_type = *p++;
2043 len--;
2046 #ifdef FEAT_MBYTE
2047 else if (*type == vimenc_atom)
2049 char_u *enc;
2050 vimconv_T conv;
2051 int convlen;
2053 motion_type = *p++;
2054 --len;
2056 enc = p;
2057 p += STRLEN(p) + 1;
2058 len -= p - enc;
2060 /* If the encoding of the text is different from 'encoding', attempt
2061 * converting it. */
2062 conv.vc_type = CONV_NONE;
2063 convert_setup(&conv, enc, p_enc);
2064 if (conv.vc_type != CONV_NONE)
2066 convlen = len; /* Need to use an int here. */
2067 tmpbuf = string_convert(&conv, p, &convlen);
2068 len = convlen;
2069 if (tmpbuf != NULL)
2070 p = tmpbuf;
2071 convert_setup(&conv, NULL, NULL);
2074 #endif
2076 else if (*type == compound_text_atom || (
2077 #ifdef FEAT_MBYTE
2078 enc_dbcs != 0 &&
2079 #endif
2080 *type == text_atom))
2082 XTextProperty text_prop;
2083 int n_text = 0;
2084 int status;
2086 text_prop.value = (unsigned char *)value;
2087 text_prop.encoding = *type;
2088 text_prop.format = *format;
2089 text_prop.nitems = len;
2090 status = XmbTextPropertyToTextList(X_DISPLAY, &text_prop,
2091 &text_list, &n_text);
2092 if (status != Success || n_text < 1)
2094 *(int *)success = FALSE;
2095 return;
2097 p = (char_u *)text_list[0];
2098 len = STRLEN(p);
2100 clip_yank_selection(motion_type, p, (long)len, cbd);
2102 if (text_list != NULL)
2103 XFreeStringList(text_list);
2104 #ifdef FEAT_MBYTE
2105 vim_free(tmpbuf);
2106 #endif
2107 XtFree((char *)value);
2108 *(int *)success = TRUE;
2111 void
2112 clip_x11_request_selection(myShell, dpy, cbd)
2113 Widget myShell;
2114 Display *dpy;
2115 VimClipboard *cbd;
2117 XEvent event;
2118 Atom type;
2119 static int success;
2120 int i;
2121 time_t start_time;
2122 int timed_out = FALSE;
2124 for (i =
2125 #ifdef FEAT_MBYTE
2127 #else
2129 #endif
2130 ; i < 5; i++)
2132 switch (i)
2134 #ifdef FEAT_MBYTE
2135 case 0: type = vimenc_atom; break;
2136 #endif
2137 case 1: type = vim_atom; break;
2138 case 2: type = compound_text_atom; break;
2139 case 3: type = text_atom; break;
2140 default: type = XA_STRING;
2142 success = MAYBE;
2143 XtGetSelectionValue(myShell, cbd->sel_atom, type,
2144 clip_x11_request_selection_cb, (XtPointer)&success, CurrentTime);
2146 /* Make sure the request for the selection goes out before waiting for
2147 * a response. */
2148 XFlush(dpy);
2151 * Wait for result of selection request, otherwise if we type more
2152 * characters, then they will appear before the one that requested the
2153 * paste! Don't worry, we will catch up with any other events later.
2155 start_time = time(NULL);
2156 while (success == MAYBE)
2158 if (XCheckTypedEvent(dpy, SelectionNotify, &event)
2159 || XCheckTypedEvent(dpy, SelectionRequest, &event)
2160 || XCheckTypedEvent(dpy, PropertyNotify, &event))
2162 /* This is where clip_x11_request_selection_cb() should be
2163 * called. It may actually happen a bit later, so we loop
2164 * until "success" changes.
2165 * We may get a SelectionRequest here and if we don't handle
2166 * it we hang. KDE klipper does this, for example.
2167 * We need to handle a PropertyNotify for large selections. */
2168 XtDispatchEvent(&event);
2169 continue;
2172 /* Time out after 2 to 3 seconds to avoid that we hang when the
2173 * other process doesn't respond. Note that the SelectionNotify
2174 * event may still come later when the selection owner comes back
2175 * to life and the text gets inserted unexpectedly. Don't know
2176 * why that happens or how to avoid that :-(. */
2177 if (time(NULL) > start_time + 2)
2179 timed_out = TRUE;
2180 break;
2183 /* Do we need this? Probably not. */
2184 XSync(dpy, False);
2186 /* Wait for 1 msec to avoid that we eat up all CPU time. */
2187 ui_delay(1L, TRUE);
2190 if (success == TRUE)
2191 return;
2193 /* don't do a retry with another type after timing out, otherwise we
2194 * hang for 15 seconds. */
2195 if (timed_out)
2196 break;
2199 /* Final fallback position - use the X CUT_BUFFER0 store */
2200 yank_cut_buffer0(dpy, cbd);
2203 static Boolean clip_x11_convert_selection_cb __ARGS((Widget, Atom *, Atom *, Atom *, XtPointer *, long_u *, int *));
2205 static Boolean
2206 clip_x11_convert_selection_cb(w, sel_atom, target, type, value, length, format)
2207 Widget w UNUSED;
2208 Atom *sel_atom;
2209 Atom *target;
2210 Atom *type;
2211 XtPointer *value;
2212 long_u *length;
2213 int *format;
2215 char_u *string;
2216 char_u *result;
2217 int motion_type;
2218 VimClipboard *cbd;
2219 int i;
2221 if (*sel_atom == clip_plus.sel_atom)
2222 cbd = &clip_plus;
2223 else
2224 cbd = &clip_star;
2226 if (!cbd->owned)
2227 return False; /* Shouldn't ever happen */
2229 /* requestor wants to know what target types we support */
2230 if (*target == targets_atom)
2232 Atom *array;
2234 if ((array = (Atom *)XtMalloc((unsigned)(sizeof(Atom) * 6))) == NULL)
2235 return False;
2236 *value = (XtPointer)array;
2237 i = 0;
2238 array[i++] = XA_STRING;
2239 array[i++] = targets_atom;
2240 #ifdef FEAT_MBYTE
2241 array[i++] = vimenc_atom;
2242 #endif
2243 array[i++] = vim_atom;
2244 array[i++] = text_atom;
2245 array[i++] = compound_text_atom;
2246 *type = XA_ATOM;
2247 /* This used to be: *format = sizeof(Atom) * 8; but that caused
2248 * crashes on 64 bit machines. (Peter Derr) */
2249 *format = 32;
2250 *length = i;
2251 return True;
2254 if ( *target != XA_STRING
2255 #ifdef FEAT_MBYTE
2256 && *target != vimenc_atom
2257 #endif
2258 && *target != vim_atom
2259 && *target != text_atom
2260 && *target != compound_text_atom)
2261 return False;
2263 clip_get_selection(cbd);
2264 motion_type = clip_convert_selection(&string, length, cbd);
2265 if (motion_type < 0)
2266 return False;
2268 /* For our own format, the first byte contains the motion type */
2269 if (*target == vim_atom)
2270 (*length)++;
2272 #ifdef FEAT_MBYTE
2273 /* Our own format with encoding: motion 'encoding' NUL text */
2274 if (*target == vimenc_atom)
2275 *length += STRLEN(p_enc) + 2;
2276 #endif
2278 *value = XtMalloc((Cardinal)*length);
2279 result = (char_u *)*value;
2280 if (result == NULL)
2282 vim_free(string);
2283 return False;
2286 if (*target == XA_STRING)
2288 mch_memmove(result, string, (size_t)(*length));
2289 *type = XA_STRING;
2291 else if (*target == compound_text_atom
2292 || *target == text_atom)
2294 XTextProperty text_prop;
2295 char *string_nt = (char *)alloc((unsigned)*length + 1);
2297 /* create NUL terminated string which XmbTextListToTextProperty wants */
2298 mch_memmove(string_nt, string, (size_t)*length);
2299 string_nt[*length] = NUL;
2300 XmbTextListToTextProperty(X_DISPLAY, (char **)&string_nt, 1,
2301 XCompoundTextStyle, &text_prop);
2302 vim_free(string_nt);
2303 XtFree(*value); /* replace with COMPOUND text */
2304 *value = (XtPointer)(text_prop.value); /* from plain text */
2305 *length = text_prop.nitems;
2306 *type = compound_text_atom;
2309 #ifdef FEAT_MBYTE
2310 else if (*target == vimenc_atom)
2312 int l = STRLEN(p_enc);
2314 result[0] = motion_type;
2315 STRCPY(result + 1, p_enc);
2316 mch_memmove(result + l + 2, string, (size_t)(*length - l - 2));
2317 *type = vimenc_atom;
2319 #endif
2321 else
2323 result[0] = motion_type;
2324 mch_memmove(result + 1, string, (size_t)(*length - 1));
2325 *type = vim_atom;
2327 *format = 8; /* 8 bits per char */
2328 vim_free(string);
2329 return True;
2332 static void clip_x11_lose_ownership_cb __ARGS((Widget, Atom *));
2334 static void
2335 clip_x11_lose_ownership_cb(w, sel_atom)
2336 Widget w UNUSED;
2337 Atom *sel_atom;
2339 if (*sel_atom == clip_plus.sel_atom)
2340 clip_lose_selection(&clip_plus);
2341 else
2342 clip_lose_selection(&clip_star);
2345 void
2346 clip_x11_lose_selection(myShell, cbd)
2347 Widget myShell;
2348 VimClipboard *cbd;
2350 XtDisownSelection(myShell, cbd->sel_atom, CurrentTime);
2354 clip_x11_own_selection(myShell, cbd)
2355 Widget myShell;
2356 VimClipboard *cbd;
2358 if (XtOwnSelection(myShell, cbd->sel_atom, CurrentTime,
2359 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
2360 NULL) == False)
2361 return FAIL;
2362 return OK;
2366 * Send the current selection to the clipboard. Do nothing for X because we
2367 * will fill in the selection only when requested by another app.
2369 void
2370 clip_x11_set_selection(cbd)
2371 VimClipboard *cbd UNUSED;
2374 #endif
2376 #if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) \
2377 || defined(FEAT_GUI_GTK) || defined(PROTO)
2379 * Get the contents of the X CUT_BUFFER0 and put it in "cbd".
2381 void
2382 yank_cut_buffer0(dpy, cbd)
2383 Display *dpy;
2384 VimClipboard *cbd;
2386 int nbytes = 0;
2387 char_u *buffer = (char_u *)XFetchBuffer(dpy, &nbytes, 0);
2389 if (nbytes > 0)
2391 #ifdef FEAT_MBYTE
2392 int done = FALSE;
2394 /* CUT_BUFFER0 is supposed to be always latin1. Convert to 'enc' when
2395 * using a multi-byte encoding. Conversion between two 8-bit
2396 * character sets usually fails and the text might actually be in
2397 * 'enc' anyway. */
2398 if (has_mbyte)
2400 char_u *conv_buf = buffer;
2401 vimconv_T vc;
2403 vc.vc_type = CONV_NONE;
2404 if (convert_setup(&vc, (char_u *)"latin1", p_enc) == OK)
2406 conv_buf = string_convert(&vc, buffer, &nbytes);
2407 if (conv_buf != NULL)
2409 clip_yank_selection(MCHAR, conv_buf, (long)nbytes, cbd);
2410 vim_free(conv_buf);
2411 done = TRUE;
2413 convert_setup(&vc, NULL, NULL);
2416 if (!done) /* use the text without conversion */
2417 #endif
2418 clip_yank_selection(MCHAR, buffer, (long)nbytes, cbd);
2419 XFree((void *)buffer);
2420 if (p_verbose > 0)
2422 verbose_enter();
2423 verb_msg((char_u *)_("Used CUT_BUFFER0 instead of empty selection"));
2424 verbose_leave();
2428 #endif
2430 #if defined(FEAT_MOUSE) || defined(PROTO)
2433 * Move the cursor to the specified row and column on the screen.
2434 * Change current window if necessary. Returns an integer with the
2435 * CURSOR_MOVED bit set if the cursor has moved or unset otherwise.
2437 * The MOUSE_FOLD_CLOSE bit is set when clicked on the '-' in a fold column.
2438 * The MOUSE_FOLD_OPEN bit is set when clicked on the '+' in a fold column.
2440 * If flags has MOUSE_FOCUS, then the current window will not be changed, and
2441 * if the mouse is outside the window then the text will scroll, or if the
2442 * mouse was previously on a status line, then the status line may be dragged.
2444 * If flags has MOUSE_MAY_VIS, then VIsual mode will be started before the
2445 * cursor is moved unless the cursor was on a status line.
2446 * This function returns one of IN_UNKNOWN, IN_BUFFER, IN_STATUS_LINE or
2447 * IN_SEP_LINE depending on where the cursor was clicked.
2449 * If flags has MOUSE_MAY_STOP_VIS, then Visual mode will be stopped, unless
2450 * the mouse is on the status line of the same window.
2452 * If flags has MOUSE_DID_MOVE, nothing is done if the mouse didn't move since
2453 * the last call.
2455 * If flags has MOUSE_SETPOS, nothing is done, only the current position is
2456 * remembered.
2459 jump_to_mouse(flags, inclusive, which_button)
2460 int flags;
2461 int *inclusive; /* used for inclusive operator, can be NULL */
2462 int which_button; /* MOUSE_LEFT, MOUSE_RIGHT, MOUSE_MIDDLE */
2464 static int on_status_line = 0; /* #lines below bottom of window */
2465 #ifdef FEAT_VERTSPLIT
2466 static int on_sep_line = 0; /* on separator right of window */
2467 #endif
2468 static int prev_row = -1;
2469 static int prev_col = -1;
2470 static win_T *dragwin = NULL; /* window being dragged */
2471 static int did_drag = FALSE; /* drag was noticed */
2473 win_T *wp, *old_curwin;
2474 pos_T old_cursor;
2475 int count;
2476 int first;
2477 int row = mouse_row;
2478 int col = mouse_col;
2479 #ifdef FEAT_FOLDING
2480 int mouse_char;
2481 #endif
2483 mouse_past_bottom = FALSE;
2484 mouse_past_eol = FALSE;
2486 if (flags & MOUSE_RELEASED)
2488 /* On button release we may change window focus if positioned on a
2489 * status line and no dragging happened. */
2490 if (dragwin != NULL && !did_drag)
2491 flags &= ~(MOUSE_FOCUS | MOUSE_DID_MOVE);
2492 dragwin = NULL;
2493 did_drag = FALSE;
2496 if ((flags & MOUSE_DID_MOVE)
2497 && prev_row == mouse_row
2498 && prev_col == mouse_col)
2500 retnomove:
2501 /* before moving the cursor for a left click which is NOT in a status
2502 * line, stop Visual mode */
2503 if (on_status_line)
2504 return IN_STATUS_LINE;
2505 #ifdef FEAT_VERTSPLIT
2506 if (on_sep_line)
2507 return IN_SEP_LINE;
2508 #endif
2509 #ifdef FEAT_VISUAL
2510 if (flags & MOUSE_MAY_STOP_VIS)
2512 end_visual_mode();
2513 redraw_curbuf_later(INVERTED); /* delete the inversion */
2515 #endif
2516 #if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2517 /* Continue a modeless selection in another window. */
2518 if (cmdwin_type != 0 && row < W_WINROW(curwin))
2519 return IN_OTHER_WIN;
2520 #endif
2521 return IN_BUFFER;
2524 prev_row = mouse_row;
2525 prev_col = mouse_col;
2527 if (flags & MOUSE_SETPOS)
2528 goto retnomove; /* ugly goto... */
2530 #ifdef FEAT_FOLDING
2531 /* Remember the character under the mouse, it might be a '-' or '+' in the
2532 * fold column. */
2533 if (row >= 0 && row < Rows && col >= 0 && col <= Columns
2534 && ScreenLines != NULL)
2535 mouse_char = ScreenLines[LineOffset[row] + col];
2536 else
2537 mouse_char = ' ';
2538 #endif
2540 old_curwin = curwin;
2541 old_cursor = curwin->w_cursor;
2543 if (!(flags & MOUSE_FOCUS))
2545 if (row < 0 || col < 0) /* check if it makes sense */
2546 return IN_UNKNOWN;
2548 #ifdef FEAT_WINDOWS
2549 /* find the window where the row is in */
2550 wp = mouse_find_win(&row, &col);
2551 #else
2552 wp = firstwin;
2553 #endif
2554 dragwin = NULL;
2556 * winpos and height may change in win_enter()!
2558 if (row >= wp->w_height) /* In (or below) status line */
2560 on_status_line = row - wp->w_height + 1;
2561 dragwin = wp;
2563 else
2564 on_status_line = 0;
2565 #ifdef FEAT_VERTSPLIT
2566 if (col >= wp->w_width) /* In separator line */
2568 on_sep_line = col - wp->w_width + 1;
2569 dragwin = wp;
2571 else
2572 on_sep_line = 0;
2574 /* The rightmost character of the status line might be a vertical
2575 * separator character if there is no connecting window to the right. */
2576 if (on_status_line && on_sep_line)
2578 if (stl_connected(wp))
2579 on_sep_line = 0;
2580 else
2581 on_status_line = 0;
2583 #endif
2585 #ifdef FEAT_VISUAL
2586 /* Before jumping to another buffer, or moving the cursor for a left
2587 * click, stop Visual mode. */
2588 if (VIsual_active
2589 && (wp->w_buffer != curwin->w_buffer
2590 || (!on_status_line
2591 # ifdef FEAT_VERTSPLIT
2592 && !on_sep_line
2593 # endif
2594 # ifdef FEAT_FOLDING
2595 && (
2596 # ifdef FEAT_RIGHTLEFT
2597 wp->w_p_rl ? col < W_WIDTH(wp) - wp->w_p_fdc :
2598 # endif
2599 col >= wp->w_p_fdc
2600 # ifdef FEAT_CMDWIN
2601 + (cmdwin_type == 0 && wp == curwin ? 0 : 1)
2602 # endif
2604 # endif
2605 && (flags & MOUSE_MAY_STOP_VIS))))
2607 end_visual_mode();
2608 redraw_curbuf_later(INVERTED); /* delete the inversion */
2610 #endif
2611 #ifdef FEAT_CMDWIN
2612 if (cmdwin_type != 0 && wp != curwin)
2614 /* A click outside the command-line window: Use modeless
2615 * selection if possible. Allow dragging the status line of
2616 * windows just above the command-line window. */
2617 if (wp->w_winrow + wp->w_height
2618 != curwin->w_prev->w_winrow + curwin->w_prev->w_height)
2620 on_status_line = 0;
2621 dragwin = NULL;
2623 # ifdef FEAT_VERTSPLIT
2624 on_sep_line = 0;
2625 # endif
2626 # ifdef FEAT_CLIPBOARD
2627 if (on_status_line)
2628 return IN_STATUS_LINE;
2629 return IN_OTHER_WIN;
2630 # else
2631 row = 0;
2632 col += wp->w_wincol;
2633 wp = curwin;
2634 # endif
2636 #endif
2637 #ifdef FEAT_WINDOWS
2638 /* Only change window focus when not clicking on or dragging the
2639 * status line. Do change focus when releasing the mouse button
2640 * (MOUSE_FOCUS was set above if we dragged first). */
2641 if (dragwin == NULL || (flags & MOUSE_RELEASED))
2642 win_enter(wp, TRUE); /* can make wp invalid! */
2643 # ifdef CHECK_DOUBLE_CLICK
2644 /* set topline, to be able to check for double click ourselves */
2645 if (curwin != old_curwin)
2646 set_mouse_topline(curwin);
2647 # endif
2648 #endif
2649 if (on_status_line) /* In (or below) status line */
2651 /* Don't use start_arrow() if we're in the same window */
2652 if (curwin == old_curwin)
2653 return IN_STATUS_LINE;
2654 else
2655 return IN_STATUS_LINE | CURSOR_MOVED;
2657 #ifdef FEAT_VERTSPLIT
2658 if (on_sep_line) /* In (or below) status line */
2660 /* Don't use start_arrow() if we're in the same window */
2661 if (curwin == old_curwin)
2662 return IN_SEP_LINE;
2663 else
2664 return IN_SEP_LINE | CURSOR_MOVED;
2666 #endif
2668 curwin->w_cursor.lnum = curwin->w_topline;
2669 #ifdef FEAT_GUI
2670 /* remember topline, needed for double click */
2671 gui_prev_topline = curwin->w_topline;
2672 # ifdef FEAT_DIFF
2673 gui_prev_topfill = curwin->w_topfill;
2674 # endif
2675 #endif
2677 else if (on_status_line && which_button == MOUSE_LEFT)
2679 #ifdef FEAT_WINDOWS
2680 if (dragwin != NULL)
2682 /* Drag the status line */
2683 count = row - dragwin->w_winrow - dragwin->w_height + 1
2684 - on_status_line;
2685 win_drag_status_line(dragwin, count);
2686 did_drag |= count;
2688 #endif
2689 return IN_STATUS_LINE; /* Cursor didn't move */
2691 #ifdef FEAT_VERTSPLIT
2692 else if (on_sep_line && which_button == MOUSE_LEFT)
2694 if (dragwin != NULL)
2696 /* Drag the separator column */
2697 count = col - dragwin->w_wincol - dragwin->w_width + 1
2698 - on_sep_line;
2699 win_drag_vsep_line(dragwin, count);
2700 did_drag |= count;
2702 return IN_SEP_LINE; /* Cursor didn't move */
2704 #endif
2705 else /* keep_window_focus must be TRUE */
2707 #ifdef FEAT_VISUAL
2708 /* before moving the cursor for a left click, stop Visual mode */
2709 if (flags & MOUSE_MAY_STOP_VIS)
2711 end_visual_mode();
2712 redraw_curbuf_later(INVERTED); /* delete the inversion */
2714 #endif
2716 #if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2717 /* Continue a modeless selection in another window. */
2718 if (cmdwin_type != 0 && row < W_WINROW(curwin))
2719 return IN_OTHER_WIN;
2720 #endif
2722 row -= W_WINROW(curwin);
2723 #ifdef FEAT_VERTSPLIT
2724 col -= W_WINCOL(curwin);
2725 #endif
2728 * When clicking beyond the end of the window, scroll the screen.
2729 * Scroll by however many rows outside the window we are.
2731 if (row < 0)
2733 count = 0;
2734 for (first = TRUE; curwin->w_topline > 1; )
2736 #ifdef FEAT_DIFF
2737 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
2738 ++count;
2739 else
2740 #endif
2741 count += plines(curwin->w_topline - 1);
2742 if (!first && count > -row)
2743 break;
2744 first = FALSE;
2745 #ifdef FEAT_FOLDING
2746 hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2747 #endif
2748 #ifdef FEAT_DIFF
2749 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
2750 ++curwin->w_topfill;
2751 else
2752 #endif
2754 --curwin->w_topline;
2755 #ifdef FEAT_DIFF
2756 curwin->w_topfill = 0;
2757 #endif
2760 #ifdef FEAT_DIFF
2761 check_topfill(curwin, FALSE);
2762 #endif
2763 curwin->w_valid &=
2764 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2765 redraw_later(VALID);
2766 row = 0;
2768 else if (row >= curwin->w_height)
2770 count = 0;
2771 for (first = TRUE; curwin->w_topline < curbuf->b_ml.ml_line_count; )
2773 #ifdef FEAT_DIFF
2774 if (curwin->w_topfill > 0)
2775 ++count;
2776 else
2777 #endif
2778 count += plines(curwin->w_topline);
2779 if (!first && count > row - curwin->w_height + 1)
2780 break;
2781 first = FALSE;
2782 #ifdef FEAT_FOLDING
2783 if (hasFolding(curwin->w_topline, NULL, &curwin->w_topline)
2784 && curwin->w_topline == curbuf->b_ml.ml_line_count)
2785 break;
2786 #endif
2787 #ifdef FEAT_DIFF
2788 if (curwin->w_topfill > 0)
2789 --curwin->w_topfill;
2790 else
2791 #endif
2793 ++curwin->w_topline;
2794 #ifdef FEAT_DIFF
2795 curwin->w_topfill =
2796 diff_check_fill(curwin, curwin->w_topline);
2797 #endif
2800 #ifdef FEAT_DIFF
2801 check_topfill(curwin, FALSE);
2802 #endif
2803 redraw_later(VALID);
2804 curwin->w_valid &=
2805 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2806 row = curwin->w_height - 1;
2808 else if (row == 0)
2810 /* When dragging the mouse, while the text has been scrolled up as
2811 * far as it goes, moving the mouse in the top line should scroll
2812 * the text down (done later when recomputing w_topline). */
2813 if (mouse_dragging > 0
2814 && curwin->w_cursor.lnum
2815 == curwin->w_buffer->b_ml.ml_line_count
2816 && curwin->w_cursor.lnum == curwin->w_topline)
2817 curwin->w_valid &= ~(VALID_TOPLINE);
2821 #ifdef FEAT_FOLDING
2822 /* Check for position outside of the fold column. */
2823 if (
2824 # ifdef FEAT_RIGHTLEFT
2825 curwin->w_p_rl ? col < W_WIDTH(curwin) - curwin->w_p_fdc :
2826 # endif
2827 col >= curwin->w_p_fdc
2828 # ifdef FEAT_CMDWIN
2829 + (cmdwin_type == 0 ? 0 : 1)
2830 # endif
2832 mouse_char = ' ';
2833 #endif
2835 /* compute the position in the buffer line from the posn on the screen */
2836 if (mouse_comp_pos(curwin, &row, &col, &curwin->w_cursor.lnum))
2837 mouse_past_bottom = TRUE;
2839 #ifdef FEAT_VISUAL
2840 /* Start Visual mode before coladvance(), for when 'sel' != "old" */
2841 if ((flags & MOUSE_MAY_VIS) && !VIsual_active)
2843 check_visual_highlight();
2844 VIsual = old_cursor;
2845 VIsual_active = TRUE;
2846 VIsual_reselect = TRUE;
2847 /* if 'selectmode' contains "mouse", start Select mode */
2848 may_start_select('o');
2849 setmouse();
2850 if (p_smd && msg_silent == 0)
2851 redraw_cmdline = TRUE; /* show visual mode later */
2853 #endif
2855 curwin->w_curswant = col;
2856 curwin->w_set_curswant = FALSE; /* May still have been TRUE */
2857 if (coladvance(col) == FAIL) /* Mouse click beyond end of line */
2859 if (inclusive != NULL)
2860 *inclusive = TRUE;
2861 mouse_past_eol = TRUE;
2863 else if (inclusive != NULL)
2864 *inclusive = FALSE;
2866 count = IN_BUFFER;
2867 if (curwin != old_curwin || curwin->w_cursor.lnum != old_cursor.lnum
2868 || curwin->w_cursor.col != old_cursor.col)
2869 count |= CURSOR_MOVED; /* Cursor has moved */
2871 #ifdef FEAT_FOLDING
2872 if (mouse_char == '+')
2873 count |= MOUSE_FOLD_OPEN;
2874 else if (mouse_char != ' ')
2875 count |= MOUSE_FOLD_CLOSE;
2876 #endif
2878 return count;
2882 * Compute the position in the buffer line from the posn on the screen in
2883 * window "win".
2884 * Returns TRUE if the position is below the last line.
2887 mouse_comp_pos(win, rowp, colp, lnump)
2888 win_T *win;
2889 int *rowp;
2890 int *colp;
2891 linenr_T *lnump;
2893 int col = *colp;
2894 int row = *rowp;
2895 linenr_T lnum;
2896 int retval = FALSE;
2897 int off;
2898 int count;
2900 #ifdef FEAT_RIGHTLEFT
2901 if (win->w_p_rl)
2902 col = W_WIDTH(win) - 1 - col;
2903 #endif
2905 lnum = win->w_topline;
2907 while (row > 0)
2909 #ifdef FEAT_DIFF
2910 /* Don't include filler lines in "count" */
2911 if (win->w_p_diff
2912 # ifdef FEAT_FOLDING
2913 && !hasFoldingWin(win, lnum, NULL, NULL, TRUE, NULL)
2914 # endif
2917 if (lnum == win->w_topline)
2918 row -= win->w_topfill;
2919 else
2920 row -= diff_check_fill(win, lnum);
2921 count = plines_win_nofill(win, lnum, TRUE);
2923 else
2924 #endif
2925 count = plines_win(win, lnum, TRUE);
2926 if (count > row)
2927 break; /* Position is in this buffer line. */
2928 #ifdef FEAT_FOLDING
2929 (void)hasFoldingWin(win, lnum, NULL, &lnum, TRUE, NULL);
2930 #endif
2931 if (lnum == win->w_buffer->b_ml.ml_line_count)
2933 retval = TRUE;
2934 break; /* past end of file */
2936 row -= count;
2937 ++lnum;
2940 if (!retval)
2942 /* Compute the column without wrapping. */
2943 off = win_col_off(win) - win_col_off2(win);
2944 if (col < off)
2945 col = off;
2946 col += row * (W_WIDTH(win) - off);
2947 /* add skip column (for long wrapping line) */
2948 col += win->w_skipcol;
2951 if (!win->w_p_wrap)
2952 col += win->w_leftcol;
2954 /* skip line number and fold column in front of the line */
2955 col -= win_col_off(win);
2956 if (col < 0)
2958 #ifdef FEAT_NETBEANS_INTG
2959 if (usingNetbeans)
2960 netbeans_gutter_click(lnum);
2961 #endif
2962 col = 0;
2965 *colp = col;
2966 *rowp = row;
2967 *lnump = lnum;
2968 return retval;
2971 #if defined(FEAT_WINDOWS) || defined(PROTO)
2973 * Find the window at screen position "*rowp" and "*colp". The positions are
2974 * updated to become relative to the top-left of the window.
2976 win_T *
2977 mouse_find_win(rowp, colp)
2978 int *rowp;
2979 int *colp UNUSED;
2981 frame_T *fp;
2983 fp = topframe;
2984 *rowp -= firstwin->w_winrow;
2985 for (;;)
2987 if (fp->fr_layout == FR_LEAF)
2988 break;
2989 #ifdef FEAT_VERTSPLIT
2990 if (fp->fr_layout == FR_ROW)
2992 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
2994 if (*colp < fp->fr_width)
2995 break;
2996 *colp -= fp->fr_width;
2999 #endif
3000 else /* fr_layout == FR_COL */
3002 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
3004 if (*rowp < fp->fr_height)
3005 break;
3006 *rowp -= fp->fr_height;
3010 return fp->fr_win;
3012 #endif
3014 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined (FEAT_GUI_MAC) \
3015 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
3016 || defined(FEAT_GUI_PHOTON) || defined(PROTO) \
3017 || defined(FEAT_GUI_MACVIM)
3019 * Translate window coordinates to buffer position without any side effects
3022 get_fpos_of_mouse(mpos)
3023 pos_T *mpos;
3025 win_T *wp;
3026 int row = mouse_row;
3027 int col = mouse_col;
3029 if (row < 0 || col < 0) /* check if it makes sense */
3030 return IN_UNKNOWN;
3032 #ifdef FEAT_WINDOWS
3033 /* find the window where the row is in */
3034 wp = mouse_find_win(&row, &col);
3035 #else
3036 wp = firstwin;
3037 #endif
3039 * winpos and height may change in win_enter()!
3041 if (row >= wp->w_height) /* In (or below) status line */
3042 return IN_STATUS_LINE;
3043 #ifdef FEAT_VERTSPLIT
3044 if (col >= wp->w_width) /* In vertical separator line */
3045 return IN_SEP_LINE;
3046 #endif
3048 if (wp != curwin)
3049 return IN_UNKNOWN;
3051 /* compute the position in the buffer line from the posn on the screen */
3052 if (mouse_comp_pos(curwin, &row, &col, &mpos->lnum))
3053 return IN_STATUS_LINE; /* past bottom */
3055 mpos->col = vcol2col(wp, mpos->lnum, col);
3057 if (mpos->col > 0)
3058 --mpos->col;
3059 return IN_BUFFER;
3063 * Convert a virtual (screen) column to a character column.
3064 * The first column is one.
3067 vcol2col(wp, lnum, vcol)
3068 win_T *wp;
3069 linenr_T lnum;
3070 int vcol;
3072 /* try to advance to the specified column */
3073 int col = 0;
3074 int count = 0;
3075 char_u *ptr;
3077 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3078 while (count <= vcol && *ptr != NUL)
3080 ++col;
3081 count += win_lbr_chartabsize(wp, ptr, count, NULL);
3082 mb_ptr_adv(ptr);
3084 return col;
3086 #endif
3088 #endif /* FEAT_MOUSE */
3090 #if defined(FEAT_GUI) || defined(WIN3264) || defined(PROTO)
3092 * Called when focus changed. Used for the GUI or for systems where this can
3093 * be done in the console (Win32).
3095 void
3096 ui_focus_change(in_focus)
3097 int in_focus; /* TRUE if focus gained. */
3099 static time_t last_time = (time_t)0;
3100 int need_redraw = FALSE;
3102 /* When activated: Check if any file was modified outside of Vim.
3103 * Only do this when not done within the last two seconds (could get
3104 * several events in a row). */
3105 if (in_focus && last_time + 2 < time(NULL))
3107 need_redraw = check_timestamps(
3108 # ifdef FEAT_GUI
3109 gui.in_use
3110 # else
3111 FALSE
3112 # endif
3114 last_time = time(NULL);
3117 #ifdef FEAT_AUTOCMD
3119 * Fire the focus gained/lost autocommand.
3121 need_redraw |= apply_autocmds(in_focus ? EVENT_FOCUSGAINED
3122 : EVENT_FOCUSLOST, NULL, NULL, FALSE, curbuf);
3123 #endif
3125 if (need_redraw)
3127 /* Something was executed, make sure the cursor is put back where it
3128 * belongs. */
3129 need_wait_return = FALSE;
3131 if (State & CMDLINE)
3132 redrawcmdline();
3133 else if (State == HITRETURN || State == SETWSIZE || State == ASKMORE
3134 || State == EXTERNCMD || State == CONFIRM || exmode_active)
3135 repeat_message();
3136 else if ((State & NORMAL) || (State & INSERT))
3138 if (must_redraw != 0)
3139 update_screen(0);
3140 setcursor();
3142 cursor_on(); /* redrawing may have switched it off */
3143 out_flush();
3144 # ifdef FEAT_GUI
3145 if (gui.in_use)
3147 gui_update_cursor(FALSE, TRUE);
3148 gui_update_scrollbars(FALSE);
3150 # endif
3152 #ifdef FEAT_TITLE
3153 /* File may have been changed from 'readonly' to 'noreadonly' */
3154 if (need_maketitle)
3155 maketitle();
3156 #endif
3158 #endif
3160 #if defined(USE_IM_CONTROL) || defined(PROTO)
3162 * Save current Input Method status to specified place.
3164 void
3165 im_save_status(psave)
3166 long *psave;
3168 /* Don't save when 'imdisable' is set or "xic" is NULL, IM is always
3169 * disabled then (but might start later).
3170 * Also don't save when inside a mapping, vgetc_im_active has not been set
3171 * then.
3172 * And don't save when the keys were stuffed (e.g., for a "." command).
3173 * And don't save when the GUI is running but our window doesn't have
3174 * input focus (e.g., when a find dialog is open). */
3175 if (!p_imdisable && KeyTyped && !KeyStuffed
3176 # if defined(FEAT_XIM) && !defined(FEAT_GUI_MACVIM)
3177 && xic != NULL
3178 # endif
3179 # ifdef FEAT_GUI
3180 && (!gui.in_use || gui.in_focus)
3181 # endif
3184 /* Do save when IM is on, or IM is off and saved status is on. */
3185 if (vgetc_im_active)
3186 *psave = B_IMODE_IM;
3187 else if (*psave == B_IMODE_IM)
3188 *psave = B_IMODE_NONE;
3191 #endif