Integration prefs pane update
[MacVim.git] / src / ui.c
blob5e4f69f10cfeaa53305dc6d8db6e8b43fe861a5e
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 /*ARGSUSED*/
334 void
335 ui_set_shellsize(mustset)
336 int mustset; /* set by the user */
338 #ifdef FEAT_GUI
339 if (gui.in_use)
340 gui_set_shellsize(mustset,
341 # ifdef WIN3264
342 TRUE
343 # else
344 FALSE
345 # endif
346 , RESIZE_BOTH);
347 else
348 #endif
349 mch_set_shellsize();
353 * Called when Rows and/or Columns changed. Adjust scroll region and mouse
354 * region.
356 void
357 ui_new_shellsize()
359 if (full_screen && !exiting)
361 #ifdef FEAT_GUI
362 if (gui.in_use)
363 gui_new_shellsize();
364 else
365 #endif
366 mch_new_shellsize();
370 void
371 ui_breakcheck()
373 #ifdef FEAT_GUI
374 if (gui.in_use)
375 gui_mch_update();
376 else
377 #endif
378 mch_breakcheck();
381 /*****************************************************************************
382 * Functions for copying and pasting text between applications.
383 * This is always included in a GUI version, but may also be included when the
384 * clipboard and mouse is available to a terminal version such as xterm.
385 * Note: there are some more functions in ops.c that handle selection stuff.
387 * Also note that the majority of functions here deal with the X 'primary'
388 * (visible - for Visual mode use) selection, and only that. There are no
389 * versions of these for the 'clipboard' selection, as Visual mode has no use
390 * for them.
393 #if defined(FEAT_CLIPBOARD) || defined(PROTO)
396 * Selection stuff using Visual mode, for cutting and pasting text to other
397 * windows.
401 * Call this to initialise the clipboard. Pass it FALSE if the clipboard code
402 * is included, but the clipboard can not be used, or TRUE if the clipboard can
403 * be used. Eg unix may call this with FALSE, then call it again with TRUE if
404 * the GUI starts.
406 void
407 clip_init(can_use)
408 int can_use;
410 VimClipboard *cb;
412 cb = &clip_star;
413 for (;;)
415 cb->available = can_use;
416 cb->owned = FALSE;
417 cb->start.lnum = 0;
418 cb->start.col = 0;
419 cb->end.lnum = 0;
420 cb->end.col = 0;
421 cb->state = SELECT_CLEARED;
423 if (cb == &clip_plus)
424 break;
425 cb = &clip_plus;
430 * Check whether the VIsual area has changed, and if so try to become the owner
431 * of the selection, and free any old converted selection we may still have
432 * lying around. If the VIsual mode has ended, make a copy of what was
433 * selected so we can still give it to others. Will probably have to make sure
434 * this is called whenever VIsual mode is ended.
436 void
437 clip_update_selection()
439 pos_T start, end;
441 /* If visual mode is only due to a redo command ("."), then ignore it */
442 if (!redo_VIsual_busy && VIsual_active && (State & NORMAL))
444 if (lt(VIsual, curwin->w_cursor))
446 start = VIsual;
447 end = curwin->w_cursor;
448 #ifdef FEAT_MBYTE
449 if (has_mbyte)
450 end.col += (*mb_ptr2len)(ml_get_cursor()) - 1;
451 #endif
453 else
455 start = curwin->w_cursor;
456 end = VIsual;
458 if (!equalpos(clip_star.start, start)
459 || !equalpos(clip_star.end, end)
460 || clip_star.vmode != VIsual_mode)
462 clip_clear_selection();
463 clip_star.start = start;
464 clip_star.end = end;
465 clip_star.vmode = VIsual_mode;
466 clip_free_selection(&clip_star);
467 clip_own_selection(&clip_star);
468 clip_gen_set_selection(&clip_star);
473 void
474 clip_own_selection(cbd)
475 VimClipboard *cbd;
478 * Also want to check somehow that we are reading from the keyboard rather
479 * than a mapping etc.
481 if (!cbd->owned && cbd->available)
483 cbd->owned = (clip_gen_own_selection(cbd) == OK);
484 #ifdef FEAT_X11
485 if (cbd == &clip_star)
487 /* May have to show a different kind of highlighting for the
488 * selected area. There is no specific redraw command for this,
489 * just redraw all windows on the current buffer. */
490 if (cbd->owned
491 && (get_real_state() == VISUAL
492 || get_real_state() == SELECTMODE)
493 && clip_isautosel()
494 && hl_attr(HLF_V) != hl_attr(HLF_VNC))
495 redraw_curbuf_later(INVERTED_ALL);
497 #endif
501 void
502 clip_lose_selection(cbd)
503 VimClipboard *cbd;
505 #ifdef FEAT_X11
506 int was_owned = cbd->owned;
507 #endif
508 int visual_selection = (cbd == &clip_star);
510 clip_free_selection(cbd);
511 cbd->owned = FALSE;
512 if (visual_selection)
513 clip_clear_selection();
514 clip_gen_lose_selection(cbd);
515 #ifdef FEAT_X11
516 if (visual_selection)
518 /* May have to show a different kind of highlighting for the selected
519 * area. There is no specific redraw command for this, just redraw all
520 * windows on the current buffer. */
521 if (was_owned
522 && (get_real_state() == VISUAL
523 || get_real_state() == SELECTMODE)
524 && clip_isautosel()
525 && hl_attr(HLF_V) != hl_attr(HLF_VNC))
527 update_curbuf(INVERTED_ALL);
528 setcursor();
529 cursor_on();
530 out_flush();
531 # ifdef FEAT_GUI
532 if (gui.in_use)
533 gui_update_cursor(TRUE, FALSE);
534 # endif
537 #endif
540 void
541 clip_copy_selection()
543 if (VIsual_active && (State & NORMAL) && clip_star.available)
545 if (clip_isautosel())
546 clip_update_selection();
547 clip_free_selection(&clip_star);
548 clip_own_selection(&clip_star);
549 if (clip_star.owned)
550 clip_get_selection(&clip_star);
551 clip_gen_set_selection(&clip_star);
556 * Called when Visual mode is ended: update the selection.
558 void
559 clip_auto_select()
561 if (clip_isautosel())
562 clip_copy_selection();
566 * Return TRUE if automatic selection of Visual area is desired.
569 clip_isautosel()
571 return (
572 #ifdef FEAT_GUI
573 gui.in_use ? (vim_strchr(p_go, GO_ASEL) != NULL) :
574 #endif
575 clip_autoselect);
580 * Stuff for general mouse selection, without using Visual mode.
583 static int clip_compare_pos __ARGS((int row1, int col1, int row2, int col2));
584 static void clip_invert_area __ARGS((int, int, int, int, int how));
585 static void clip_invert_rectangle __ARGS((int row, int col, int height, int width, int invert));
586 static void clip_get_word_boundaries __ARGS((VimClipboard *, int, int));
587 static int clip_get_line_end __ARGS((int));
588 static void clip_update_modeless_selection __ARGS((VimClipboard *, int, int,
589 int, int));
591 /* flags for clip_invert_area() */
592 #define CLIP_CLEAR 1
593 #define CLIP_SET 2
594 #define CLIP_TOGGLE 3
597 * Start, continue or end a modeless selection. Used when editing the
598 * command-line and in the cmdline window.
600 void
601 clip_modeless(button, is_click, is_drag)
602 int button;
603 int is_click;
604 int is_drag;
606 int repeat;
608 repeat = ((clip_star.mode == SELECT_MODE_CHAR
609 || clip_star.mode == SELECT_MODE_LINE)
610 && (mod_mask & MOD_MASK_2CLICK))
611 || (clip_star.mode == SELECT_MODE_WORD
612 && (mod_mask & MOD_MASK_3CLICK));
613 if (is_click && button == MOUSE_RIGHT)
615 /* Right mouse button: If there was no selection, start one.
616 * Otherwise extend the existing selection. */
617 if (clip_star.state == SELECT_CLEARED)
618 clip_start_selection(mouse_col, mouse_row, FALSE);
619 clip_process_selection(button, mouse_col, mouse_row, repeat);
621 else if (is_click)
622 clip_start_selection(mouse_col, mouse_row, repeat);
623 else if (is_drag)
625 /* Don't try extending a selection if there isn't one. Happens when
626 * button-down is in the cmdline and them moving mouse upwards. */
627 if (clip_star.state != SELECT_CLEARED)
628 clip_process_selection(button, mouse_col, mouse_row, repeat);
630 else /* release */
631 clip_process_selection(MOUSE_RELEASE, mouse_col, mouse_row, FALSE);
635 * Compare two screen positions ala strcmp()
637 static int
638 clip_compare_pos(row1, col1, row2, col2)
639 int row1;
640 int col1;
641 int row2;
642 int col2;
644 if (row1 > row2) return(1);
645 if (row1 < row2) return(-1);
646 if (col1 > col2) return(1);
647 if (col1 < col2) return(-1);
648 return(0);
652 * Start the selection
654 void
655 clip_start_selection(col, row, repeated_click)
656 int col;
657 int row;
658 int repeated_click;
660 VimClipboard *cb = &clip_star;
662 if (cb->state == SELECT_DONE)
663 clip_clear_selection();
665 row = check_row(row);
666 col = check_col(col);
667 #ifdef FEAT_MBYTE
668 col = mb_fix_col(col, row);
669 #endif
671 cb->start.lnum = row;
672 cb->start.col = col;
673 cb->end = cb->start;
674 cb->origin_row = (short_u)cb->start.lnum;
675 cb->state = SELECT_IN_PROGRESS;
677 if (repeated_click)
679 if (++cb->mode > SELECT_MODE_LINE)
680 cb->mode = SELECT_MODE_CHAR;
682 else
683 cb->mode = SELECT_MODE_CHAR;
685 #ifdef FEAT_GUI
686 /* clear the cursor until the selection is made */
687 if (gui.in_use)
688 gui_undraw_cursor();
689 #endif
691 switch (cb->mode)
693 case SELECT_MODE_CHAR:
694 cb->origin_start_col = cb->start.col;
695 cb->word_end_col = clip_get_line_end((int)cb->start.lnum);
696 break;
698 case SELECT_MODE_WORD:
699 clip_get_word_boundaries(cb, (int)cb->start.lnum, cb->start.col);
700 cb->origin_start_col = cb->word_start_col;
701 cb->origin_end_col = cb->word_end_col;
703 clip_invert_area((int)cb->start.lnum, cb->word_start_col,
704 (int)cb->end.lnum, cb->word_end_col, CLIP_SET);
705 cb->start.col = cb->word_start_col;
706 cb->end.col = cb->word_end_col;
707 break;
709 case SELECT_MODE_LINE:
710 clip_invert_area((int)cb->start.lnum, 0, (int)cb->start.lnum,
711 (int)Columns, CLIP_SET);
712 cb->start.col = 0;
713 cb->end.col = Columns;
714 break;
717 cb->prev = cb->start;
719 #ifdef DEBUG_SELECTION
720 printf("Selection started at (%u,%u)\n", cb->start.lnum, cb->start.col);
721 #endif
725 * Continue processing the selection
727 void
728 clip_process_selection(button, col, row, repeated_click)
729 int button;
730 int col;
731 int row;
732 int_u repeated_click;
734 VimClipboard *cb = &clip_star;
735 int diff;
736 int slen = 1; /* cursor shape width */
738 if (button == MOUSE_RELEASE)
740 /* Check to make sure we have something selected */
741 if (cb->start.lnum == cb->end.lnum && cb->start.col == cb->end.col)
743 #ifdef FEAT_GUI
744 if (gui.in_use)
745 gui_update_cursor(FALSE, FALSE);
746 #endif
747 cb->state = SELECT_CLEARED;
748 return;
751 #ifdef DEBUG_SELECTION
752 printf("Selection ended: (%u,%u) to (%u,%u)\n", cb->start.lnum,
753 cb->start.col, cb->end.lnum, cb->end.col);
754 #endif
755 if (clip_isautosel()
756 || (
757 #ifdef FEAT_GUI
758 gui.in_use ? (vim_strchr(p_go, GO_ASELML) != NULL) :
759 #endif
760 clip_autoselectml))
761 clip_copy_modeless_selection(FALSE);
762 #ifdef FEAT_GUI
763 if (gui.in_use)
764 gui_update_cursor(FALSE, FALSE);
765 #endif
767 cb->state = SELECT_DONE;
768 return;
771 row = check_row(row);
772 col = check_col(col);
773 #ifdef FEAT_MBYTE
774 col = mb_fix_col(col, row);
775 #endif
777 if (col == (int)cb->prev.col && row == cb->prev.lnum && !repeated_click)
778 return;
781 * When extending the selection with the right mouse button, swap the
782 * start and end if the position is before half the selection
784 if (cb->state == SELECT_DONE && button == MOUSE_RIGHT)
787 * If the click is before the start, or the click is inside the
788 * selection and the start is the closest side, set the origin to the
789 * end of the selection.
791 if (clip_compare_pos(row, col, (int)cb->start.lnum, cb->start.col) < 0
792 || (clip_compare_pos(row, col,
793 (int)cb->end.lnum, cb->end.col) < 0
794 && (((cb->start.lnum == cb->end.lnum
795 && cb->end.col - col > col - cb->start.col))
796 || ((diff = (cb->end.lnum - row) -
797 (row - cb->start.lnum)) > 0
798 || (diff == 0 && col < (int)(cb->start.col +
799 cb->end.col) / 2)))))
801 cb->origin_row = (short_u)cb->end.lnum;
802 cb->origin_start_col = cb->end.col - 1;
803 cb->origin_end_col = cb->end.col;
805 else
807 cb->origin_row = (short_u)cb->start.lnum;
808 cb->origin_start_col = cb->start.col;
809 cb->origin_end_col = cb->start.col;
811 if (cb->mode == SELECT_MODE_WORD && !repeated_click)
812 cb->mode = SELECT_MODE_CHAR;
815 /* set state, for when using the right mouse button */
816 cb->state = SELECT_IN_PROGRESS;
818 #ifdef DEBUG_SELECTION
819 printf("Selection extending to (%d,%d)\n", row, col);
820 #endif
822 if (repeated_click && ++cb->mode > SELECT_MODE_LINE)
823 cb->mode = SELECT_MODE_CHAR;
825 switch (cb->mode)
827 case SELECT_MODE_CHAR:
828 /* If we're on a different line, find where the line ends */
829 if (row != cb->prev.lnum)
830 cb->word_end_col = clip_get_line_end(row);
832 /* See if we are before or after the origin of the selection */
833 if (clip_compare_pos(row, col, cb->origin_row,
834 cb->origin_start_col) >= 0)
836 if (col >= (int)cb->word_end_col)
837 clip_update_modeless_selection(cb, cb->origin_row,
838 cb->origin_start_col, row, (int)Columns);
839 else
841 #ifdef FEAT_MBYTE
842 if (has_mbyte && mb_lefthalve(row, col))
843 slen = 2;
844 #endif
845 clip_update_modeless_selection(cb, cb->origin_row,
846 cb->origin_start_col, row, col + slen);
849 else
851 #ifdef FEAT_MBYTE
852 if (has_mbyte
853 && mb_lefthalve(cb->origin_row, cb->origin_start_col))
854 slen = 2;
855 #endif
856 if (col >= (int)cb->word_end_col)
857 clip_update_modeless_selection(cb, row, cb->word_end_col,
858 cb->origin_row, cb->origin_start_col + slen);
859 else
860 clip_update_modeless_selection(cb, row, col,
861 cb->origin_row, cb->origin_start_col + slen);
863 break;
865 case SELECT_MODE_WORD:
866 /* If we are still within the same word, do nothing */
867 if (row == cb->prev.lnum && col >= (int)cb->word_start_col
868 && col < (int)cb->word_end_col && !repeated_click)
869 return;
871 /* Get new word boundaries */
872 clip_get_word_boundaries(cb, row, col);
874 /* Handle being after the origin point of selection */
875 if (clip_compare_pos(row, col, cb->origin_row,
876 cb->origin_start_col) >= 0)
877 clip_update_modeless_selection(cb, cb->origin_row,
878 cb->origin_start_col, row, cb->word_end_col);
879 else
880 clip_update_modeless_selection(cb, row, cb->word_start_col,
881 cb->origin_row, cb->origin_end_col);
882 break;
884 case SELECT_MODE_LINE:
885 if (row == cb->prev.lnum && !repeated_click)
886 return;
888 if (clip_compare_pos(row, col, cb->origin_row,
889 cb->origin_start_col) >= 0)
890 clip_update_modeless_selection(cb, cb->origin_row, 0, row,
891 (int)Columns);
892 else
893 clip_update_modeless_selection(cb, row, 0, cb->origin_row,
894 (int)Columns);
895 break;
898 cb->prev.lnum = row;
899 cb->prev.col = col;
901 #ifdef DEBUG_SELECTION
902 printf("Selection is: (%u,%u) to (%u,%u)\n", cb->start.lnum,
903 cb->start.col, cb->end.lnum, cb->end.col);
904 #endif
907 #if 0 /* not used */
909 * Called after an Expose event to redraw the selection
911 void
912 clip_redraw_selection(x, y, w, h)
913 int x;
914 int y;
915 int w;
916 int h;
918 VimClipboard *cb = &clip_star;
919 int row1, col1, row2, col2;
920 int row;
921 int start;
922 int end;
924 if (cb->state == SELECT_CLEARED)
925 return;
927 row1 = check_row(Y_2_ROW(y));
928 col1 = check_col(X_2_COL(x));
929 row2 = check_row(Y_2_ROW(y + h - 1));
930 col2 = check_col(X_2_COL(x + w - 1));
932 /* Limit the rows that need to be re-drawn */
933 if (cb->start.lnum > row1)
934 row1 = cb->start.lnum;
935 if (cb->end.lnum < row2)
936 row2 = cb->end.lnum;
938 /* Look at each row that might need to be re-drawn */
939 for (row = row1; row <= row2; row++)
941 /* For the first selection row, use the starting selection column */
942 if (row == cb->start.lnum)
943 start = cb->start.col;
944 else
945 start = 0;
947 /* For the last selection row, use the ending selection column */
948 if (row == cb->end.lnum)
949 end = cb->end.col;
950 else
951 end = Columns;
953 if (col1 > start)
954 start = col1;
956 if (col2 < end)
957 end = col2 + 1;
959 if (end > start)
960 gui_mch_invert_rectangle(row, start, 1, end - start);
963 #endif
965 # if defined(FEAT_GUI) || defined(PROTO)
967 * Redraw part of the selection if character at "row,col" is inside of it.
968 * Only used for the GUI.
970 void
971 clip_may_redraw_selection(row, col, len)
972 int row, col;
973 int len;
975 int start = col;
976 int end = col + len;
978 if (clip_star.state != SELECT_CLEARED
979 && row >= clip_star.start.lnum
980 && row <= clip_star.end.lnum)
982 if (row == clip_star.start.lnum && start < (int)clip_star.start.col)
983 start = clip_star.start.col;
984 if (row == clip_star.end.lnum && end > (int)clip_star.end.col)
985 end = clip_star.end.col;
986 if (end > start)
987 clip_invert_area(row, start, row, end, 0);
990 # endif
993 * Called from outside to clear selected region from the display
995 void
996 clip_clear_selection()
998 VimClipboard *cb = &clip_star;
1000 if (cb->state == SELECT_CLEARED)
1001 return;
1003 clip_invert_area((int)cb->start.lnum, cb->start.col, (int)cb->end.lnum,
1004 cb->end.col, CLIP_CLEAR);
1005 cb->state = SELECT_CLEARED;
1009 * Clear the selection if any lines from "row1" to "row2" are inside of it.
1011 void
1012 clip_may_clear_selection(row1, row2)
1013 int row1, row2;
1015 if (clip_star.state == SELECT_DONE
1016 && row2 >= clip_star.start.lnum
1017 && row1 <= clip_star.end.lnum)
1018 clip_clear_selection();
1022 * Called before the screen is scrolled up or down. Adjusts the line numbers
1023 * of the selection. Call with big number when clearing the screen.
1025 void
1026 clip_scroll_selection(rows)
1027 int rows; /* negative for scroll down */
1029 int lnum;
1031 if (clip_star.state == SELECT_CLEARED)
1032 return;
1034 lnum = clip_star.start.lnum - rows;
1035 if (lnum <= 0)
1036 clip_star.start.lnum = 0;
1037 else if (lnum >= screen_Rows) /* scrolled off of the screen */
1038 clip_star.state = SELECT_CLEARED;
1039 else
1040 clip_star.start.lnum = lnum;
1042 lnum = clip_star.end.lnum - rows;
1043 if (lnum < 0) /* scrolled off of the screen */
1044 clip_star.state = SELECT_CLEARED;
1045 else if (lnum >= screen_Rows)
1046 clip_star.end.lnum = screen_Rows - 1;
1047 else
1048 clip_star.end.lnum = lnum;
1052 * Invert a region of the display between a starting and ending row and column
1053 * Values for "how":
1054 * CLIP_CLEAR: undo inversion
1055 * CLIP_SET: set inversion
1056 * CLIP_TOGGLE: set inversion if pos1 < pos2, undo inversion otherwise.
1057 * 0: invert (GUI only).
1059 static void
1060 clip_invert_area(row1, col1, row2, col2, how)
1061 int row1;
1062 int col1;
1063 int row2;
1064 int col2;
1065 int how;
1067 int invert = FALSE;
1069 if (how == CLIP_SET)
1070 invert = TRUE;
1072 /* Swap the from and to positions so the from is always before */
1073 if (clip_compare_pos(row1, col1, row2, col2) > 0)
1075 int tmp_row, tmp_col;
1077 tmp_row = row1;
1078 tmp_col = col1;
1079 row1 = row2;
1080 col1 = col2;
1081 row2 = tmp_row;
1082 col2 = tmp_col;
1084 else if (how == CLIP_TOGGLE)
1085 invert = TRUE;
1087 /* If all on the same line, do it the easy way */
1088 if (row1 == row2)
1090 clip_invert_rectangle(row1, col1, 1, col2 - col1, invert);
1092 else
1094 /* Handle a piece of the first line */
1095 if (col1 > 0)
1097 clip_invert_rectangle(row1, col1, 1, (int)Columns - col1, invert);
1098 row1++;
1101 /* Handle a piece of the last line */
1102 if (col2 < Columns - 1)
1104 clip_invert_rectangle(row2, 0, 1, col2, invert);
1105 row2--;
1108 /* Handle the rectangle thats left */
1109 if (row2 >= row1)
1110 clip_invert_rectangle(row1, 0, row2 - row1 + 1, (int)Columns,
1111 invert);
1116 * Invert or un-invert a rectangle of the screen.
1117 * "invert" is true if the result is inverted.
1119 static void
1120 clip_invert_rectangle(row, col, height, width, invert)
1121 int row;
1122 int col;
1123 int height;
1124 int width;
1125 int invert;
1127 #ifdef FEAT_GUI
1128 if (gui.in_use)
1129 gui_mch_invert_rectangle(row, col, height, width);
1130 else
1131 #endif
1132 screen_draw_rectangle(row, col, height, width, invert);
1136 * Copy the currently selected area into the '*' register so it will be
1137 * available for pasting.
1138 * When "both" is TRUE also copy to the '+' register.
1140 /*ARGSUSED*/
1141 void
1142 clip_copy_modeless_selection(both)
1143 int both;
1145 char_u *buffer;
1146 char_u *bufp;
1147 int row;
1148 int start_col;
1149 int end_col;
1150 int line_end_col;
1151 int add_newline_flag = FALSE;
1152 int len;
1153 #ifdef FEAT_MBYTE
1154 char_u *p;
1155 #endif
1156 int row1 = clip_star.start.lnum;
1157 int col1 = clip_star.start.col;
1158 int row2 = clip_star.end.lnum;
1159 int col2 = clip_star.end.col;
1161 /* Can't use ScreenLines unless initialized */
1162 if (ScreenLines == NULL)
1163 return;
1166 * Make sure row1 <= row2, and if row1 == row2 that col1 <= col2.
1168 if (row1 > row2)
1170 row = row1; row1 = row2; row2 = row;
1171 row = col1; col1 = col2; col2 = row;
1173 else if (row1 == row2 && col1 > col2)
1175 row = col1; col1 = col2; col2 = row;
1177 #ifdef FEAT_MBYTE
1178 /* correct starting point for being on right halve of double-wide char */
1179 p = ScreenLines + LineOffset[row1];
1180 if (enc_dbcs != 0)
1181 col1 -= (*mb_head_off)(p, p + col1);
1182 else if (enc_utf8 && p[col1] == 0)
1183 --col1;
1184 #endif
1186 /* Create a temporary buffer for storing the text */
1187 len = (row2 - row1 + 1) * Columns + 1;
1188 #ifdef FEAT_MBYTE
1189 if (enc_dbcs != 0)
1190 len *= 2; /* max. 2 bytes per display cell */
1191 else if (enc_utf8)
1192 len *= MB_MAXBYTES;
1193 #endif
1194 buffer = lalloc((long_u)len, TRUE);
1195 if (buffer == NULL) /* out of memory */
1196 return;
1198 /* Process each row in the selection */
1199 for (bufp = buffer, row = row1; row <= row2; row++)
1201 if (row == row1)
1202 start_col = col1;
1203 else
1204 start_col = 0;
1206 if (row == row2)
1207 end_col = col2;
1208 else
1209 end_col = Columns;
1211 line_end_col = clip_get_line_end(row);
1213 /* See if we need to nuke some trailing whitespace */
1214 if (end_col >= Columns && (row < row2 || end_col > line_end_col))
1216 /* Get rid of trailing whitespace */
1217 end_col = line_end_col;
1218 if (end_col < start_col)
1219 end_col = start_col;
1221 /* If the last line extended to the end, add an extra newline */
1222 if (row == row2)
1223 add_newline_flag = TRUE;
1226 /* If after the first row, we need to always add a newline */
1227 if (row > row1 && !LineWraps[row - 1])
1228 *bufp++ = NL;
1230 if (row < screen_Rows && end_col <= screen_Columns)
1232 #ifdef FEAT_MBYTE
1233 if (enc_dbcs != 0)
1235 int i;
1237 p = ScreenLines + LineOffset[row];
1238 for (i = start_col; i < end_col; ++i)
1239 if (enc_dbcs == DBCS_JPNU && p[i] == 0x8e)
1241 /* single-width double-byte char */
1242 *bufp++ = 0x8e;
1243 *bufp++ = ScreenLines2[LineOffset[row] + i];
1245 else
1247 *bufp++ = p[i];
1248 if (MB_BYTE2LEN(p[i]) == 2)
1249 *bufp++ = p[++i];
1252 else if (enc_utf8)
1254 int off;
1255 int i;
1256 int ci;
1258 off = LineOffset[row];
1259 for (i = start_col; i < end_col; ++i)
1261 /* The base character is either in ScreenLinesUC[] or
1262 * ScreenLines[]. */
1263 if (ScreenLinesUC[off + i] == 0)
1264 *bufp++ = ScreenLines[off + i];
1265 else
1267 bufp += utf_char2bytes(ScreenLinesUC[off + i], bufp);
1268 for (ci = 0; ci < Screen_mco; ++ci)
1270 /* Add a composing character. */
1271 if (ScreenLinesC[ci][off + i] == 0)
1272 break;
1273 bufp += utf_char2bytes(ScreenLinesC[ci][off + i],
1274 bufp);
1277 /* Skip right halve of double-wide character. */
1278 if (ScreenLines[off + i + 1] == 0)
1279 ++i;
1282 else
1283 #endif
1285 STRNCPY(bufp, ScreenLines + LineOffset[row] + start_col,
1286 end_col - start_col);
1287 bufp += end_col - start_col;
1292 /* Add a newline at the end if the selection ended there */
1293 if (add_newline_flag)
1294 *bufp++ = NL;
1296 /* First cleanup any old selection and become the owner. */
1297 clip_free_selection(&clip_star);
1298 clip_own_selection(&clip_star);
1300 /* Yank the text into the '*' register. */
1301 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_star);
1303 /* Make the register contents available to the outside world. */
1304 clip_gen_set_selection(&clip_star);
1306 #ifdef FEAT_X11
1307 if (both)
1309 /* Do the same for the '+' register. */
1310 clip_free_selection(&clip_plus);
1311 clip_own_selection(&clip_plus);
1312 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_plus);
1313 clip_gen_set_selection(&clip_plus);
1315 #endif
1316 vim_free(buffer);
1320 * Find the starting and ending positions of the word at the given row and
1321 * column. Only white-separated words are recognized here.
1323 #define CHAR_CLASS(c) (c <= ' ' ? ' ' : vim_iswordc(c))
1325 static void
1326 clip_get_word_boundaries(cb, row, col)
1327 VimClipboard *cb;
1328 int row;
1329 int col;
1331 int start_class;
1332 int temp_col;
1333 char_u *p;
1334 #ifdef FEAT_MBYTE
1335 int mboff;
1336 #endif
1338 if (row >= screen_Rows || col >= screen_Columns || ScreenLines == NULL)
1339 return;
1341 p = ScreenLines + LineOffset[row];
1342 #ifdef FEAT_MBYTE
1343 /* Correct for starting in the right halve of a double-wide char */
1344 if (enc_dbcs != 0)
1345 col -= dbcs_screen_head_off(p, p + col);
1346 else if (enc_utf8 && p[col] == 0)
1347 --col;
1348 #endif
1349 start_class = CHAR_CLASS(p[col]);
1351 temp_col = col;
1352 for ( ; temp_col > 0; temp_col--)
1353 #ifdef FEAT_MBYTE
1354 if (enc_dbcs != 0
1355 && (mboff = dbcs_screen_head_off(p, p + temp_col - 1)) > 0)
1356 temp_col -= mboff;
1357 else
1358 #endif
1359 if (CHAR_CLASS(p[temp_col - 1]) != start_class
1360 #ifdef FEAT_MBYTE
1361 && !(enc_utf8 && p[temp_col - 1] == 0)
1362 #endif
1364 break;
1365 cb->word_start_col = temp_col;
1367 temp_col = col;
1368 for ( ; temp_col < screen_Columns; temp_col++)
1369 #ifdef FEAT_MBYTE
1370 if (enc_dbcs != 0 && dbcs_ptr2cells(p + temp_col) == 2)
1371 ++temp_col;
1372 else
1373 #endif
1374 if (CHAR_CLASS(p[temp_col]) != start_class
1375 #ifdef FEAT_MBYTE
1376 && !(enc_utf8 && p[temp_col] == 0)
1377 #endif
1379 break;
1380 cb->word_end_col = temp_col;
1384 * Find the column position for the last non-whitespace character on the given
1385 * line.
1387 static int
1388 clip_get_line_end(row)
1389 int row;
1391 int i;
1393 if (row >= screen_Rows || ScreenLines == NULL)
1394 return 0;
1395 for (i = screen_Columns; i > 0; i--)
1396 if (ScreenLines[LineOffset[row] + i - 1] != ' ')
1397 break;
1398 return i;
1402 * Update the currently selected region by adding and/or subtracting from the
1403 * beginning or end and inverting the changed area(s).
1405 static void
1406 clip_update_modeless_selection(cb, row1, col1, row2, col2)
1407 VimClipboard *cb;
1408 int row1;
1409 int col1;
1410 int row2;
1411 int col2;
1413 /* See if we changed at the beginning of the selection */
1414 if (row1 != cb->start.lnum || col1 != (int)cb->start.col)
1416 clip_invert_area(row1, col1, (int)cb->start.lnum, cb->start.col,
1417 CLIP_TOGGLE);
1418 cb->start.lnum = row1;
1419 cb->start.col = col1;
1422 /* See if we changed at the end of the selection */
1423 if (row2 != cb->end.lnum || col2 != (int)cb->end.col)
1425 clip_invert_area((int)cb->end.lnum, cb->end.col, row2, col2,
1426 CLIP_TOGGLE);
1427 cb->end.lnum = row2;
1428 cb->end.col = col2;
1433 clip_gen_own_selection(cbd)
1434 VimClipboard *cbd;
1436 #ifdef FEAT_XCLIPBOARD
1437 # ifdef FEAT_GUI
1438 if (gui.in_use)
1439 return clip_mch_own_selection(cbd);
1440 else
1441 # endif
1442 return clip_xterm_own_selection(cbd);
1443 #else
1444 return clip_mch_own_selection(cbd);
1445 #endif
1448 void
1449 clip_gen_lose_selection(cbd)
1450 VimClipboard *cbd;
1452 #ifdef FEAT_XCLIPBOARD
1453 # ifdef FEAT_GUI
1454 if (gui.in_use)
1455 clip_mch_lose_selection(cbd);
1456 else
1457 # endif
1458 clip_xterm_lose_selection(cbd);
1459 #else
1460 clip_mch_lose_selection(cbd);
1461 #endif
1464 void
1465 clip_gen_set_selection(cbd)
1466 VimClipboard *cbd;
1468 #ifdef FEAT_XCLIPBOARD
1469 # ifdef FEAT_GUI
1470 if (gui.in_use)
1471 clip_mch_set_selection(cbd);
1472 else
1473 # endif
1474 clip_xterm_set_selection(cbd);
1475 #else
1476 clip_mch_set_selection(cbd);
1477 #endif
1480 void
1481 clip_gen_request_selection(cbd)
1482 VimClipboard *cbd;
1484 #ifdef FEAT_XCLIPBOARD
1485 # ifdef FEAT_GUI
1486 if (gui.in_use)
1487 clip_mch_request_selection(cbd);
1488 else
1489 # endif
1490 clip_xterm_request_selection(cbd);
1491 #else
1492 clip_mch_request_selection(cbd);
1493 #endif
1496 #endif /* FEAT_CLIPBOARD */
1498 /*****************************************************************************
1499 * Functions that handle the input buffer.
1500 * This is used for any GUI version, and the unix terminal version.
1502 * For Unix, the input characters are buffered to be able to check for a
1503 * CTRL-C. This should be done with signals, but I don't know how to do that
1504 * in a portable way for a tty in RAW mode.
1506 * For the client-server code in the console the received keys are put in the
1507 * input buffer.
1510 #if defined(USE_INPUT_BUF) || defined(PROTO)
1513 * Internal typeahead buffer. Includes extra space for long key code
1514 * descriptions which would otherwise overflow. The buffer is considered full
1515 * when only this extra space (or part of it) remains.
1517 #if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
1518 || defined(FEAT_CLIENTSERVER)
1520 * Sun WorkShop and NetBeans stuff debugger commands into the input buffer.
1521 * This requires a larger buffer...
1522 * (Madsen) Go with this for remote input as well ...
1524 # define INBUFLEN 4096
1525 #else
1526 # define INBUFLEN 250
1527 #endif
1529 static char_u inbuf[INBUFLEN + MAX_KEY_CODE_LEN];
1530 static int inbufcount = 0; /* number of chars in inbuf[] */
1533 * vim_is_input_buf_full(), vim_is_input_buf_empty(), add_to_input_buf(), and
1534 * trash_input_buf() are functions for manipulating the input buffer. These
1535 * are used by the gui_* calls when a GUI is used to handle keyboard input.
1539 vim_is_input_buf_full()
1541 return (inbufcount >= INBUFLEN);
1545 vim_is_input_buf_empty()
1547 return (inbufcount == 0);
1550 #if defined(FEAT_OLE) || defined(PROTO)
1552 vim_free_in_input_buf()
1554 return (INBUFLEN - inbufcount);
1556 #endif
1558 #if defined(FEAT_GUI_GTK) || defined(PROTO)
1560 vim_used_in_input_buf()
1562 return inbufcount;
1564 #endif
1566 #if defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) || defined(PROTO)
1568 * Return the current contents of the input buffer and make it empty.
1569 * The returned pointer must be passed to set_input_buf() later.
1571 char_u *
1572 get_input_buf()
1574 garray_T *gap;
1576 /* We use a growarray to store the data pointer and the length. */
1577 gap = (garray_T *)alloc((unsigned)sizeof(garray_T));
1578 if (gap != NULL)
1580 /* Add one to avoid a zero size. */
1581 gap->ga_data = alloc((unsigned)inbufcount + 1);
1582 if (gap->ga_data != NULL)
1583 mch_memmove(gap->ga_data, inbuf, (size_t)inbufcount);
1584 gap->ga_len = inbufcount;
1586 trash_input_buf();
1587 return (char_u *)gap;
1591 * Restore the input buffer with a pointer returned from get_input_buf().
1592 * The allocated memory is freed, this only works once!
1594 void
1595 set_input_buf(p)
1596 char_u *p;
1598 garray_T *gap = (garray_T *)p;
1600 if (gap != NULL)
1602 if (gap->ga_data != NULL)
1604 mch_memmove(inbuf, gap->ga_data, gap->ga_len);
1605 inbufcount = gap->ga_len;
1606 vim_free(gap->ga_data);
1608 vim_free(gap);
1611 #endif
1613 #if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM) \
1614 || defined(FEAT_XCLIPBOARD) || defined(VMS) \
1615 || defined(FEAT_SNIFF) || defined(FEAT_CLIENTSERVER) \
1616 || defined(PROTO)
1618 * Add the given bytes to the input buffer
1619 * Special keys start with CSI. A real CSI must have been translated to
1620 * CSI KS_EXTRA KE_CSI. K_SPECIAL doesn't require translation.
1622 void
1623 add_to_input_buf(s, len)
1624 char_u *s;
1625 int len;
1627 if (inbufcount + len > INBUFLEN + MAX_KEY_CODE_LEN)
1628 return; /* Shouldn't ever happen! */
1630 #ifdef FEAT_HANGULIN
1631 if ((State & (INSERT|CMDLINE)) && hangul_input_state_get())
1632 if ((len = hangul_input_process(s, len)) == 0)
1633 return;
1634 #endif
1636 while (len--)
1637 inbuf[inbufcount++] = *s++;
1639 #endif
1641 #if ((defined(FEAT_XIM) || defined(FEAT_DND)) && defined(FEAT_GUI_GTK)) \
1642 || defined(FEAT_GUI_MSWIN) \
1643 || defined(FEAT_GUI_MAC) \
1644 || (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) \
1645 || (defined(FEAT_GUI) && (!defined(USE_ON_FLY_SCROLL) \
1646 || defined(FEAT_MENU))) \
1647 || defined(PROTO)
1649 * Add "str[len]" to the input buffer while escaping CSI bytes.
1651 void
1652 add_to_input_buf_csi(char_u *str, int len)
1654 int i;
1655 char_u buf[2];
1657 for (i = 0; i < len; ++i)
1659 add_to_input_buf(str + i, 1);
1660 if (str[i] == CSI)
1662 /* Turn CSI into K_CSI. */
1663 buf[0] = KS_EXTRA;
1664 buf[1] = (int)KE_CSI;
1665 add_to_input_buf(buf, 2);
1669 #endif
1671 #if defined(FEAT_HANGULIN) || defined(PROTO)
1672 void
1673 push_raw_key (s, len)
1674 char_u *s;
1675 int len;
1677 while (len--)
1678 inbuf[inbufcount++] = *s++;
1680 #endif
1682 #if defined(FEAT_GUI) || defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) \
1683 || defined(PROTO)
1684 /* Remove everything from the input buffer. Called when ^C is found */
1685 void
1686 trash_input_buf()
1688 inbufcount = 0;
1690 #endif
1693 * Read as much data from the input buffer as possible up to maxlen, and store
1694 * it in buf.
1695 * Note: this function used to be Read() in unix.c
1698 read_from_input_buf(buf, maxlen)
1699 char_u *buf;
1700 long maxlen;
1702 if (inbufcount == 0) /* if the buffer is empty, fill it */
1703 fill_input_buf(TRUE);
1704 if (maxlen > inbufcount)
1705 maxlen = inbufcount;
1706 mch_memmove(buf, inbuf, (size_t)maxlen);
1707 inbufcount -= maxlen;
1708 if (inbufcount)
1709 mch_memmove(inbuf, inbuf + maxlen, (size_t)inbufcount);
1710 return (int)maxlen;
1713 /*ARGSUSED*/
1714 void
1715 fill_input_buf(exit_on_error)
1716 int exit_on_error;
1718 #if defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X_UNIX)
1719 int len;
1720 int try;
1721 static int did_read_something = FALSE;
1722 # ifdef FEAT_MBYTE
1723 static char_u *rest = NULL; /* unconverted rest of previous read */
1724 static int restlen = 0;
1725 int unconverted;
1726 # endif
1727 #endif
1729 #ifdef FEAT_GUI
1730 if (gui.in_use
1731 # ifdef NO_CONSOLE_INPUT
1732 /* Don't use the GUI input when the window hasn't been opened yet.
1733 * We get here from ui_inchar() when we should try reading from stdin. */
1734 && !no_console_input()
1735 # endif
1738 gui_mch_update();
1739 return;
1741 #endif
1742 #if defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X_UNIX)
1743 if (vim_is_input_buf_full())
1744 return;
1746 * Fill_input_buf() is only called when we really need a character.
1747 * If we can't get any, but there is some in the buffer, just return.
1748 * If we can't get any, and there isn't any in the buffer, we give up and
1749 * exit Vim.
1751 # ifdef __BEOS__
1753 * On the BeBox version (for now), all input is secretly performed within
1754 * beos_select() which is called from RealWaitForChar().
1756 while (!vim_is_input_buf_full() && RealWaitForChar(read_cmd_fd, 0, NULL))
1758 len = inbufcount;
1759 inbufcount = 0;
1760 # else
1762 # ifdef FEAT_SNIFF
1763 if (sniff_request_waiting)
1765 add_to_input_buf((char_u *)"\233sniff",6); /* results in K_SNIFF */
1766 sniff_request_waiting = 0;
1767 want_sniff_request = 0;
1768 return;
1770 # endif
1772 # ifdef FEAT_MBYTE
1773 if (rest != NULL)
1775 /* Use remainder of previous call, starts with an invalid character
1776 * that may become valid when reading more. */
1777 if (restlen > INBUFLEN - inbufcount)
1778 unconverted = INBUFLEN - inbufcount;
1779 else
1780 unconverted = restlen;
1781 mch_memmove(inbuf + inbufcount, rest, unconverted);
1782 if (unconverted == restlen)
1784 vim_free(rest);
1785 rest = NULL;
1787 else
1789 restlen -= unconverted;
1790 mch_memmove(rest, rest + unconverted, restlen);
1792 inbufcount += unconverted;
1794 else
1795 unconverted = 0;
1796 #endif
1798 len = 0; /* to avoid gcc warning */
1799 for (try = 0; try < 100; ++try)
1801 # ifdef VMS
1802 len = vms_read(
1803 # else
1804 len = read(read_cmd_fd,
1805 # endif
1806 (char *)inbuf + inbufcount, (size_t)((INBUFLEN - inbufcount)
1807 # ifdef FEAT_MBYTE
1808 / input_conv.vc_factor
1809 # endif
1811 # if 0
1812 ) /* avoid syntax highlight error */
1813 # endif
1815 if (len > 0 || got_int)
1816 break;
1818 * If reading stdin results in an error, continue reading stderr.
1819 * This helps when using "foo | xargs vim".
1821 if (!did_read_something && !isatty(read_cmd_fd) && read_cmd_fd == 0)
1823 int m = cur_tmode;
1825 /* We probably set the wrong file descriptor to raw mode. Switch
1826 * back to cooked mode, use another descriptor and set the mode to
1827 * what it was. */
1828 settmode(TMODE_COOK);
1829 #ifdef HAVE_DUP
1830 /* Use stderr for stdin, also works for shell commands. */
1831 close(0);
1832 dup(2);
1833 #else
1834 read_cmd_fd = 2; /* read from stderr instead of stdin */
1835 #endif
1836 settmode(m);
1838 if (!exit_on_error)
1839 return;
1841 # endif
1842 if (len <= 0 && !got_int)
1843 read_error_exit();
1844 if (len > 0)
1845 did_read_something = TRUE;
1846 if (got_int)
1848 /* Interrupted, pretend a CTRL-C was typed. */
1849 inbuf[0] = 3;
1850 inbufcount = 1;
1852 else
1854 # ifdef FEAT_MBYTE
1856 * May perform conversion on the input characters.
1857 * Include the unconverted rest of the previous call.
1858 * If there is an incomplete char at the end it is kept for the next
1859 * time, reading more bytes should make conversion possible.
1860 * Don't do this in the unlikely event that the input buffer is too
1861 * small ("rest" still contains more bytes).
1863 if (input_conv.vc_type != CONV_NONE)
1865 inbufcount -= unconverted;
1866 len = convert_input_safe(inbuf + inbufcount,
1867 len + unconverted, INBUFLEN - inbufcount,
1868 rest == NULL ? &rest : NULL, &restlen);
1870 # endif
1871 while (len-- > 0)
1874 * if a CTRL-C was typed, remove it from the buffer and set got_int
1876 if (inbuf[inbufcount] == 3 && ctrl_c_interrupts)
1878 /* remove everything typed before the CTRL-C */
1879 mch_memmove(inbuf, inbuf + inbufcount, (size_t)(len + 1));
1880 inbufcount = 0;
1881 got_int = TRUE;
1883 ++inbufcount;
1886 #endif /* UNIX or OS2 or VMS*/
1888 #endif /* defined(UNIX) || defined(FEAT_GUI) || defined(OS2) || defined(VMS) */
1891 * Exit because of an input read error.
1893 void
1894 read_error_exit()
1896 if (silent_mode) /* Normal way to exit for "ex -s" */
1897 getout(0);
1898 STRCPY(IObuff, _("Vim: Error reading input, exiting...\n"));
1899 preserve_exit();
1902 #if defined(CURSOR_SHAPE) || defined(PROTO)
1904 * May update the shape of the cursor.
1906 void
1907 ui_cursor_shape()
1909 # ifdef FEAT_GUI
1910 if (gui.in_use)
1911 gui_update_cursor_later();
1912 else
1913 # endif
1914 term_cursor_shape();
1916 # ifdef MCH_CURSOR_SHAPE
1917 mch_update_cursor();
1918 # endif
1920 #endif
1922 #if defined(FEAT_CLIPBOARD) || defined(FEAT_GUI) || defined(FEAT_RIGHTLEFT) \
1923 || defined(PROTO)
1925 * Check bounds for column number
1928 check_col(col)
1929 int col;
1931 if (col < 0)
1932 return 0;
1933 if (col >= (int)screen_Columns)
1934 return (int)screen_Columns - 1;
1935 return col;
1939 * Check bounds for row number
1942 check_row(row)
1943 int row;
1945 if (row < 0)
1946 return 0;
1947 if (row >= (int)screen_Rows)
1948 return (int)screen_Rows - 1;
1949 return row;
1951 #endif
1954 * Stuff for the X clipboard. Shared between VMS and Unix.
1957 #if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) || defined(PROTO)
1958 # include <X11/Xatom.h>
1959 # include <X11/Intrinsic.h>
1962 * Open the application context (if it hasn't been opened yet).
1963 * Used for Motif and Athena GUI and the xterm clipboard.
1965 void
1966 open_app_context()
1968 if (app_context == NULL)
1970 XtToolkitInitialize();
1971 app_context = XtCreateApplicationContext();
1975 static Atom vim_atom; /* Vim's own special selection format */
1976 #ifdef FEAT_MBYTE
1977 static Atom vimenc_atom; /* Vim's extended selection format */
1978 #endif
1979 static Atom compound_text_atom;
1980 static Atom text_atom;
1981 static Atom targets_atom;
1983 void
1984 x11_setup_atoms(dpy)
1985 Display *dpy;
1987 vim_atom = XInternAtom(dpy, VIM_ATOM_NAME, False);
1988 #ifdef FEAT_MBYTE
1989 vimenc_atom = XInternAtom(dpy, VIMENC_ATOM_NAME,False);
1990 #endif
1991 compound_text_atom = XInternAtom(dpy, "COMPOUND_TEXT", False);
1992 text_atom = XInternAtom(dpy, "TEXT", False);
1993 targets_atom = XInternAtom(dpy, "TARGETS", False);
1994 clip_star.sel_atom = XA_PRIMARY;
1995 clip_plus.sel_atom = XInternAtom(dpy, "CLIPBOARD", False);
1999 * X Selection stuff, for cutting and pasting text to other windows.
2002 static void clip_x11_request_selection_cb __ARGS((Widget, XtPointer, Atom *, Atom *, XtPointer, long_u *, int *));
2004 /* ARGSUSED */
2005 static void
2006 clip_x11_request_selection_cb(w, success, sel_atom, type, value, length,
2007 format)
2008 Widget w;
2009 XtPointer success;
2010 Atom *sel_atom;
2011 Atom *type;
2012 XtPointer value;
2013 long_u *length;
2014 int *format;
2016 int motion_type;
2017 long_u len;
2018 char_u *p;
2019 char **text_list = NULL;
2020 VimClipboard *cbd;
2021 #ifdef FEAT_MBYTE
2022 char_u *tmpbuf = NULL;
2023 #endif
2025 if (*sel_atom == clip_plus.sel_atom)
2026 cbd = &clip_plus;
2027 else
2028 cbd = &clip_star;
2030 if (value == NULL || *length == 0)
2032 clip_free_selection(cbd); /* ??? [what's the query?] */
2033 *(int *)success = FALSE;
2034 return;
2036 motion_type = MCHAR;
2037 p = (char_u *)value;
2038 len = *length;
2039 if (*type == vim_atom)
2041 motion_type = *p++;
2042 len--;
2045 #ifdef FEAT_MBYTE
2046 else if (*type == vimenc_atom)
2048 char_u *enc;
2049 vimconv_T conv;
2050 int convlen;
2052 motion_type = *p++;
2053 --len;
2055 enc = p;
2056 p += STRLEN(p) + 1;
2057 len -= p - enc;
2059 /* If the encoding of the text is different from 'encoding', attempt
2060 * converting it. */
2061 conv.vc_type = CONV_NONE;
2062 convert_setup(&conv, enc, p_enc);
2063 if (conv.vc_type != CONV_NONE)
2065 convlen = len; /* Need to use an int here. */
2066 tmpbuf = string_convert(&conv, p, &convlen);
2067 len = convlen;
2068 if (tmpbuf != NULL)
2069 p = tmpbuf;
2070 convert_setup(&conv, NULL, NULL);
2073 #endif
2075 else if (*type == compound_text_atom || (
2076 #ifdef FEAT_MBYTE
2077 enc_dbcs != 0 &&
2078 #endif
2079 *type == text_atom))
2081 XTextProperty text_prop;
2082 int n_text = 0;
2083 int status;
2085 text_prop.value = (unsigned char *)value;
2086 text_prop.encoding = *type;
2087 text_prop.format = *format;
2088 text_prop.nitems = STRLEN(value);
2089 status = XmbTextPropertyToTextList(X_DISPLAY, &text_prop,
2090 &text_list, &n_text);
2091 if (status != Success || n_text < 1)
2093 *(int *)success = FALSE;
2094 return;
2096 p = (char_u *)text_list[0];
2097 len = STRLEN(p);
2099 clip_yank_selection(motion_type, p, (long)len, cbd);
2101 if (text_list != NULL)
2102 XFreeStringList(text_list);
2103 #ifdef FEAT_MBYTE
2104 vim_free(tmpbuf);
2105 #endif
2106 XtFree((char *)value);
2107 *(int *)success = TRUE;
2110 void
2111 clip_x11_request_selection(myShell, dpy, cbd)
2112 Widget myShell;
2113 Display *dpy;
2114 VimClipboard *cbd;
2116 XEvent event;
2117 Atom type;
2118 static int success;
2119 int i;
2120 int nbytes = 0;
2121 char_u *buffer;
2123 for (i =
2124 #ifdef FEAT_MBYTE
2126 #else
2128 #endif
2129 ; i < 5; i++)
2131 switch (i)
2133 #ifdef FEAT_MBYTE
2134 case 0: type = vimenc_atom; break;
2135 #endif
2136 case 1: type = vim_atom; break;
2137 case 2: type = compound_text_atom; break;
2138 case 3: type = text_atom; break;
2139 default: type = XA_STRING;
2141 XtGetSelectionValue(myShell, cbd->sel_atom, type,
2142 clip_x11_request_selection_cb, (XtPointer)&success, CurrentTime);
2144 /* Make sure the request for the selection goes out before waiting for
2145 * a response. */
2146 XFlush(dpy);
2149 * Wait for result of selection request, otherwise if we type more
2150 * characters, then they will appear before the one that requested the
2151 * paste! Don't worry, we will catch up with any other events later.
2153 for (;;)
2155 if (XCheckTypedEvent(dpy, SelectionNotify, &event))
2156 break;
2157 if (XCheckTypedEvent(dpy, SelectionRequest, &event))
2158 /* We may get a SelectionRequest here and if we don't handle
2159 * it we hang. KDE klipper does this, for example. */
2160 XtDispatchEvent(&event);
2162 /* Do we need this? Probably not. */
2163 XSync(dpy, False);
2165 /* Bernhard Walle solved a slow paste response in an X terminal by
2166 * adding: usleep(10000); here. */
2169 /* this is where clip_x11_request_selection_cb() is actually called */
2170 XtDispatchEvent(&event);
2172 if (success)
2173 return;
2176 /* Final fallback position - use the X CUT_BUFFER0 store */
2177 buffer = (char_u *)XFetchBuffer(dpy, &nbytes, 0);
2178 if (nbytes > 0)
2180 /* Got something */
2181 clip_yank_selection(MCHAR, buffer, (long)nbytes, cbd);
2182 XFree((void *)buffer);
2183 if (p_verbose > 0)
2184 verb_msg((char_u *)_("Used CUT_BUFFER0 instead of empty selection"));
2188 static Boolean clip_x11_convert_selection_cb __ARGS((Widget, Atom *, Atom *, Atom *, XtPointer *, long_u *, int *));
2190 /* ARGSUSED */
2191 static Boolean
2192 clip_x11_convert_selection_cb(w, sel_atom, target, type, value, length, format)
2193 Widget w;
2194 Atom *sel_atom;
2195 Atom *target;
2196 Atom *type;
2197 XtPointer *value;
2198 long_u *length;
2199 int *format;
2201 char_u *string;
2202 char_u *result;
2203 int motion_type;
2204 VimClipboard *cbd;
2205 int i;
2207 if (*sel_atom == clip_plus.sel_atom)
2208 cbd = &clip_plus;
2209 else
2210 cbd = &clip_star;
2212 if (!cbd->owned)
2213 return False; /* Shouldn't ever happen */
2215 /* requestor wants to know what target types we support */
2216 if (*target == targets_atom)
2218 Atom *array;
2220 if ((array = (Atom *)XtMalloc((unsigned)(sizeof(Atom) * 6))) == NULL)
2221 return False;
2222 *value = (XtPointer)array;
2223 i = 0;
2224 array[i++] = XA_STRING;
2225 array[i++] = targets_atom;
2226 #ifdef FEAT_MBYTE
2227 array[i++] = vimenc_atom;
2228 #endif
2229 array[i++] = vim_atom;
2230 array[i++] = text_atom;
2231 array[i++] = compound_text_atom;
2232 *type = XA_ATOM;
2233 /* This used to be: *format = sizeof(Atom) * 8; but that caused
2234 * crashes on 64 bit machines. (Peter Derr) */
2235 *format = 32;
2236 *length = i;
2237 return True;
2240 if ( *target != XA_STRING
2241 #ifdef FEAT_MBYTE
2242 && *target != vimenc_atom
2243 #endif
2244 && *target != vim_atom
2245 && *target != text_atom
2246 && *target != compound_text_atom)
2247 return False;
2249 clip_get_selection(cbd);
2250 motion_type = clip_convert_selection(&string, length, cbd);
2251 if (motion_type < 0)
2252 return False;
2254 /* For our own format, the first byte contains the motion type */
2255 if (*target == vim_atom)
2256 (*length)++;
2258 #ifdef FEAT_MBYTE
2259 /* Our own format with encoding: motion 'encoding' NUL text */
2260 if (*target == vimenc_atom)
2261 *length += STRLEN(p_enc) + 2;
2262 #endif
2264 *value = XtMalloc((Cardinal)*length);
2265 result = (char_u *)*value;
2266 if (result == NULL)
2268 vim_free(string);
2269 return False;
2272 if (*target == XA_STRING)
2274 mch_memmove(result, string, (size_t)(*length));
2275 *type = XA_STRING;
2277 else if (*target == compound_text_atom
2278 || *target == text_atom)
2280 XTextProperty text_prop;
2281 char *string_nt = (char *)alloc((unsigned)*length + 1);
2283 /* create NUL terminated string which XmbTextListToTextProperty wants */
2284 mch_memmove(string_nt, string, (size_t)*length);
2285 string_nt[*length] = NUL;
2286 XmbTextListToTextProperty(X_DISPLAY, (char **)&string_nt, 1,
2287 XCompoundTextStyle, &text_prop);
2288 vim_free(string_nt);
2289 XtFree(*value); /* replace with COMPOUND text */
2290 *value = (XtPointer)(text_prop.value); /* from plain text */
2291 *length = text_prop.nitems;
2292 *type = compound_text_atom;
2295 #ifdef FEAT_MBYTE
2296 else if (*target == vimenc_atom)
2298 int l = STRLEN(p_enc);
2300 result[0] = motion_type;
2301 STRCPY(result + 1, p_enc);
2302 mch_memmove(result + l + 2, string, (size_t)(*length - l - 2));
2303 *type = vimenc_atom;
2305 #endif
2307 else
2309 result[0] = motion_type;
2310 mch_memmove(result + 1, string, (size_t)(*length - 1));
2311 *type = vim_atom;
2313 *format = 8; /* 8 bits per char */
2314 vim_free(string);
2315 return True;
2318 static void clip_x11_lose_ownership_cb __ARGS((Widget, Atom *));
2320 /* ARGSUSED */
2321 static void
2322 clip_x11_lose_ownership_cb(w, sel_atom)
2323 Widget w;
2324 Atom *sel_atom;
2326 if (*sel_atom == clip_plus.sel_atom)
2327 clip_lose_selection(&clip_plus);
2328 else
2329 clip_lose_selection(&clip_star);
2332 void
2333 clip_x11_lose_selection(myShell, cbd)
2334 Widget myShell;
2335 VimClipboard *cbd;
2337 XtDisownSelection(myShell, cbd->sel_atom, CurrentTime);
2341 clip_x11_own_selection(myShell, cbd)
2342 Widget myShell;
2343 VimClipboard *cbd;
2345 if (XtOwnSelection(myShell, cbd->sel_atom, CurrentTime,
2346 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
2347 NULL) == False)
2348 return FAIL;
2349 return OK;
2353 * Send the current selection to the clipboard. Do nothing for X because we
2354 * will fill in the selection only when requested by another app.
2356 /*ARGSUSED*/
2357 void
2358 clip_x11_set_selection(cbd)
2359 VimClipboard *cbd;
2362 #endif
2364 #if defined(FEAT_MOUSE) || defined(PROTO)
2367 * Move the cursor to the specified row and column on the screen.
2368 * Change current window if necessary. Returns an integer with the
2369 * CURSOR_MOVED bit set if the cursor has moved or unset otherwise.
2371 * The MOUSE_FOLD_CLOSE bit is set when clicked on the '-' in a fold column.
2372 * The MOUSE_FOLD_OPEN bit is set when clicked on the '+' in a fold column.
2374 * If flags has MOUSE_FOCUS, then the current window will not be changed, and
2375 * if the mouse is outside the window then the text will scroll, or if the
2376 * mouse was previously on a status line, then the status line may be dragged.
2378 * If flags has MOUSE_MAY_VIS, then VIsual mode will be started before the
2379 * cursor is moved unless the cursor was on a status line.
2380 * This function returns one of IN_UNKNOWN, IN_BUFFER, IN_STATUS_LINE or
2381 * IN_SEP_LINE depending on where the cursor was clicked.
2383 * If flags has MOUSE_MAY_STOP_VIS, then Visual mode will be stopped, unless
2384 * the mouse is on the status line of the same window.
2386 * If flags has MOUSE_DID_MOVE, nothing is done if the mouse didn't move since
2387 * the last call.
2389 * If flags has MOUSE_SETPOS, nothing is done, only the current position is
2390 * remembered.
2393 jump_to_mouse(flags, inclusive, which_button)
2394 int flags;
2395 int *inclusive; /* used for inclusive operator, can be NULL */
2396 int which_button; /* MOUSE_LEFT, MOUSE_RIGHT, MOUSE_MIDDLE */
2398 static int on_status_line = 0; /* #lines below bottom of window */
2399 #ifdef FEAT_VERTSPLIT
2400 static int on_sep_line = 0; /* on separator right of window */
2401 #endif
2402 static int prev_row = -1;
2403 static int prev_col = -1;
2404 static win_T *dragwin = NULL; /* window being dragged */
2405 static int did_drag = FALSE; /* drag was noticed */
2407 win_T *wp, *old_curwin;
2408 pos_T old_cursor;
2409 int count;
2410 int first;
2411 int row = mouse_row;
2412 int col = mouse_col;
2413 #ifdef FEAT_FOLDING
2414 int mouse_char;
2415 #endif
2417 mouse_past_bottom = FALSE;
2418 mouse_past_eol = FALSE;
2420 if (flags & MOUSE_RELEASED)
2422 /* On button release we may change window focus if positioned on a
2423 * status line and no dragging happened. */
2424 if (dragwin != NULL && !did_drag)
2425 flags &= ~(MOUSE_FOCUS | MOUSE_DID_MOVE);
2426 dragwin = NULL;
2427 did_drag = FALSE;
2430 if ((flags & MOUSE_DID_MOVE)
2431 && prev_row == mouse_row
2432 && prev_col == mouse_col)
2434 retnomove:
2435 /* before moving the cursor for a left click which is NOT in a status
2436 * line, stop Visual mode */
2437 if (on_status_line)
2438 return IN_STATUS_LINE;
2439 #ifdef FEAT_VERTSPLIT
2440 if (on_sep_line)
2441 return IN_SEP_LINE;
2442 #endif
2443 #ifdef FEAT_VISUAL
2444 if (flags & MOUSE_MAY_STOP_VIS)
2446 end_visual_mode();
2447 redraw_curbuf_later(INVERTED); /* delete the inversion */
2449 #endif
2450 #if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2451 /* Continue a modeless selection in another window. */
2452 if (cmdwin_type != 0 && row < W_WINROW(curwin))
2453 return IN_OTHER_WIN;
2454 #endif
2455 return IN_BUFFER;
2458 prev_row = mouse_row;
2459 prev_col = mouse_col;
2461 if (flags & MOUSE_SETPOS)
2462 goto retnomove; /* ugly goto... */
2464 #ifdef FEAT_FOLDING
2465 /* Remember the character under the mouse, it might be a '-' or '+' in the
2466 * fold column. */
2467 if (row >= 0 && row < Rows && col >= 0 && col <= Columns
2468 && ScreenLines != NULL)
2469 mouse_char = ScreenLines[LineOffset[row] + col];
2470 else
2471 mouse_char = ' ';
2472 #endif
2474 old_curwin = curwin;
2475 old_cursor = curwin->w_cursor;
2477 if (!(flags & MOUSE_FOCUS))
2479 if (row < 0 || col < 0) /* check if it makes sense */
2480 return IN_UNKNOWN;
2482 #ifdef FEAT_WINDOWS
2483 /* find the window where the row is in */
2484 wp = mouse_find_win(&row, &col);
2485 #else
2486 wp = firstwin;
2487 #endif
2488 dragwin = NULL;
2490 * winpos and height may change in win_enter()!
2492 if (row >= wp->w_height) /* In (or below) status line */
2494 on_status_line = row - wp->w_height + 1;
2495 dragwin = wp;
2497 else
2498 on_status_line = 0;
2499 #ifdef FEAT_VERTSPLIT
2500 if (col >= wp->w_width) /* In separator line */
2502 on_sep_line = col - wp->w_width + 1;
2503 dragwin = wp;
2505 else
2506 on_sep_line = 0;
2508 /* The rightmost character of the status line might be a vertical
2509 * separator character if there is no connecting window to the right. */
2510 if (on_status_line && on_sep_line)
2512 if (stl_connected(wp))
2513 on_sep_line = 0;
2514 else
2515 on_status_line = 0;
2517 #endif
2519 #ifdef FEAT_VISUAL
2520 /* Before jumping to another buffer, or moving the cursor for a left
2521 * click, stop Visual mode. */
2522 if (VIsual_active
2523 && (wp->w_buffer != curwin->w_buffer
2524 || (!on_status_line
2525 # ifdef FEAT_VERTSPLIT
2526 && !on_sep_line
2527 # endif
2528 # ifdef FEAT_FOLDING
2529 && (
2530 # ifdef FEAT_RIGHTLEFT
2531 wp->w_p_rl ? col < W_WIDTH(wp) - wp->w_p_fdc :
2532 # endif
2533 col >= wp->w_p_fdc
2534 # ifdef FEAT_CMDWIN
2535 + (cmdwin_type == 0 && wp == curwin ? 0 : 1)
2536 # endif
2538 # endif
2539 && (flags & MOUSE_MAY_STOP_VIS))))
2541 end_visual_mode();
2542 redraw_curbuf_later(INVERTED); /* delete the inversion */
2544 #endif
2545 #ifdef FEAT_CMDWIN
2546 if (cmdwin_type != 0 && wp != curwin)
2548 /* A click outside the command-line window: Use modeless
2549 * selection if possible. Allow dragging the status line of
2550 * windows just above the command-line window. */
2551 if (wp->w_winrow + wp->w_height
2552 != curwin->w_prev->w_winrow + curwin->w_prev->w_height)
2554 on_status_line = 0;
2555 dragwin = NULL;
2557 # ifdef FEAT_VERTSPLIT
2558 on_sep_line = 0;
2559 # endif
2560 # ifdef FEAT_CLIPBOARD
2561 if (on_status_line)
2562 return IN_STATUS_LINE;
2563 return IN_OTHER_WIN;
2564 # else
2565 row = 0;
2566 col += wp->w_wincol;
2567 wp = curwin;
2568 # endif
2570 #endif
2571 #ifdef FEAT_WINDOWS
2572 /* Only change window focus when not clicking on or dragging the
2573 * status line. Do change focus when releasing the mouse button
2574 * (MOUSE_FOCUS was set above if we dragged first). */
2575 if (dragwin == NULL || (flags & MOUSE_RELEASED))
2576 win_enter(wp, TRUE); /* can make wp invalid! */
2577 # ifdef CHECK_DOUBLE_CLICK
2578 /* set topline, to be able to check for double click ourselves */
2579 if (curwin != old_curwin)
2580 set_mouse_topline(curwin);
2581 # endif
2582 #endif
2583 if (on_status_line) /* In (or below) status line */
2585 /* Don't use start_arrow() if we're in the same window */
2586 if (curwin == old_curwin)
2587 return IN_STATUS_LINE;
2588 else
2589 return IN_STATUS_LINE | CURSOR_MOVED;
2591 #ifdef FEAT_VERTSPLIT
2592 if (on_sep_line) /* In (or below) status line */
2594 /* Don't use start_arrow() if we're in the same window */
2595 if (curwin == old_curwin)
2596 return IN_SEP_LINE;
2597 else
2598 return IN_SEP_LINE | CURSOR_MOVED;
2600 #endif
2602 curwin->w_cursor.lnum = curwin->w_topline;
2603 #ifdef FEAT_GUI
2604 /* remember topline, needed for double click */
2605 gui_prev_topline = curwin->w_topline;
2606 # ifdef FEAT_DIFF
2607 gui_prev_topfill = curwin->w_topfill;
2608 # endif
2609 #endif
2611 else if (on_status_line && which_button == MOUSE_LEFT)
2613 #ifdef FEAT_WINDOWS
2614 if (dragwin != NULL)
2616 /* Drag the status line */
2617 count = row - dragwin->w_winrow - dragwin->w_height + 1
2618 - on_status_line;
2619 win_drag_status_line(dragwin, count);
2620 did_drag |= count;
2622 #endif
2623 return IN_STATUS_LINE; /* Cursor didn't move */
2625 #ifdef FEAT_VERTSPLIT
2626 else if (on_sep_line && which_button == MOUSE_LEFT)
2628 if (dragwin != NULL)
2630 /* Drag the separator column */
2631 count = col - dragwin->w_wincol - dragwin->w_width + 1
2632 - on_sep_line;
2633 win_drag_vsep_line(dragwin, count);
2634 did_drag |= count;
2636 return IN_SEP_LINE; /* Cursor didn't move */
2638 #endif
2639 else /* keep_window_focus must be TRUE */
2641 #ifdef FEAT_VISUAL
2642 /* before moving the cursor for a left click, stop Visual mode */
2643 if (flags & MOUSE_MAY_STOP_VIS)
2645 end_visual_mode();
2646 redraw_curbuf_later(INVERTED); /* delete the inversion */
2648 #endif
2650 #if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2651 /* Continue a modeless selection in another window. */
2652 if (cmdwin_type != 0 && row < W_WINROW(curwin))
2653 return IN_OTHER_WIN;
2654 #endif
2656 row -= W_WINROW(curwin);
2657 #ifdef FEAT_VERTSPLIT
2658 col -= W_WINCOL(curwin);
2659 #endif
2662 * When clicking beyond the end of the window, scroll the screen.
2663 * Scroll by however many rows outside the window we are.
2665 if (row < 0)
2667 count = 0;
2668 for (first = TRUE; curwin->w_topline > 1; )
2670 #ifdef FEAT_DIFF
2671 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
2672 ++count;
2673 else
2674 #endif
2675 count += plines(curwin->w_topline - 1);
2676 if (!first && count > -row)
2677 break;
2678 first = FALSE;
2679 #ifdef FEAT_FOLDING
2680 hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
2681 #endif
2682 #ifdef FEAT_DIFF
2683 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
2684 ++curwin->w_topfill;
2685 else
2686 #endif
2688 --curwin->w_topline;
2689 #ifdef FEAT_DIFF
2690 curwin->w_topfill = 0;
2691 #endif
2694 #ifdef FEAT_DIFF
2695 check_topfill(curwin, FALSE);
2696 #endif
2697 curwin->w_valid &=
2698 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2699 redraw_later(VALID);
2700 row = 0;
2702 else if (row >= curwin->w_height)
2704 count = 0;
2705 for (first = TRUE; curwin->w_topline < curbuf->b_ml.ml_line_count; )
2707 #ifdef FEAT_DIFF
2708 if (curwin->w_topfill > 0)
2709 ++count;
2710 else
2711 #endif
2712 count += plines(curwin->w_topline);
2713 if (!first && count > row - curwin->w_height + 1)
2714 break;
2715 first = FALSE;
2716 #ifdef FEAT_FOLDING
2717 if (hasFolding(curwin->w_topline, NULL, &curwin->w_topline)
2718 && curwin->w_topline == curbuf->b_ml.ml_line_count)
2719 break;
2720 #endif
2721 #ifdef FEAT_DIFF
2722 if (curwin->w_topfill > 0)
2723 --curwin->w_topfill;
2724 else
2725 #endif
2727 ++curwin->w_topline;
2728 #ifdef FEAT_DIFF
2729 curwin->w_topfill =
2730 diff_check_fill(curwin, curwin->w_topline);
2731 #endif
2734 #ifdef FEAT_DIFF
2735 check_topfill(curwin, FALSE);
2736 #endif
2737 redraw_later(VALID);
2738 curwin->w_valid &=
2739 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
2740 row = curwin->w_height - 1;
2742 else if (row == 0)
2744 /* When dragging the mouse, while the text has been scrolled up as
2745 * far as it goes, moving the mouse in the top line should scroll
2746 * the text down (done later when recomputing w_topline). */
2747 if (mouse_dragging > 0
2748 && curwin->w_cursor.lnum
2749 == curwin->w_buffer->b_ml.ml_line_count
2750 && curwin->w_cursor.lnum == curwin->w_topline)
2751 curwin->w_valid &= ~(VALID_TOPLINE);
2755 #ifdef FEAT_FOLDING
2756 /* Check for position outside of the fold column. */
2757 if (
2758 # ifdef FEAT_RIGHTLEFT
2759 curwin->w_p_rl ? col < W_WIDTH(curwin) - curwin->w_p_fdc :
2760 # endif
2761 col >= curwin->w_p_fdc
2762 # ifdef FEAT_CMDWIN
2763 + (cmdwin_type == 0 ? 0 : 1)
2764 # endif
2766 mouse_char = ' ';
2767 #endif
2769 /* compute the position in the buffer line from the posn on the screen */
2770 if (mouse_comp_pos(curwin, &row, &col, &curwin->w_cursor.lnum))
2771 mouse_past_bottom = TRUE;
2773 #ifdef FEAT_VISUAL
2774 /* Start Visual mode before coladvance(), for when 'sel' != "old" */
2775 if ((flags & MOUSE_MAY_VIS) && !VIsual_active)
2777 check_visual_highlight();
2778 VIsual = old_cursor;
2779 VIsual_active = TRUE;
2780 VIsual_reselect = TRUE;
2781 /* if 'selectmode' contains "mouse", start Select mode */
2782 may_start_select('o');
2783 setmouse();
2784 if (p_smd && msg_silent == 0)
2785 redraw_cmdline = TRUE; /* show visual mode later */
2787 #endif
2789 curwin->w_curswant = col;
2790 curwin->w_set_curswant = FALSE; /* May still have been TRUE */
2791 if (coladvance(col) == FAIL) /* Mouse click beyond end of line */
2793 if (inclusive != NULL)
2794 *inclusive = TRUE;
2795 mouse_past_eol = TRUE;
2797 else if (inclusive != NULL)
2798 *inclusive = FALSE;
2800 count = IN_BUFFER;
2801 if (curwin != old_curwin || curwin->w_cursor.lnum != old_cursor.lnum
2802 || curwin->w_cursor.col != old_cursor.col)
2803 count |= CURSOR_MOVED; /* Cursor has moved */
2805 #ifdef FEAT_FOLDING
2806 if (mouse_char == '+')
2807 count |= MOUSE_FOLD_OPEN;
2808 else if (mouse_char != ' ')
2809 count |= MOUSE_FOLD_CLOSE;
2810 #endif
2812 return count;
2816 * Compute the position in the buffer line from the posn on the screen in
2817 * window "win".
2818 * Returns TRUE if the position is below the last line.
2821 mouse_comp_pos(win, rowp, colp, lnump)
2822 win_T *win;
2823 int *rowp;
2824 int *colp;
2825 linenr_T *lnump;
2827 int col = *colp;
2828 int row = *rowp;
2829 linenr_T lnum;
2830 int retval = FALSE;
2831 int off;
2832 int count;
2834 #ifdef FEAT_RIGHTLEFT
2835 if (win->w_p_rl)
2836 col = W_WIDTH(win) - 1 - col;
2837 #endif
2839 lnum = win->w_topline;
2841 while (row > 0)
2843 #ifdef FEAT_DIFF
2844 /* Don't include filler lines in "count" */
2845 if (win->w_p_diff
2846 # ifdef FEAT_FOLDING
2847 && !hasFoldingWin(win, lnum, NULL, NULL, TRUE, NULL)
2848 # endif
2851 if (lnum == win->w_topline)
2852 row -= win->w_topfill;
2853 else
2854 row -= diff_check_fill(win, lnum);
2855 count = plines_win_nofill(win, lnum, TRUE);
2857 else
2858 #endif
2859 count = plines_win(win, lnum, TRUE);
2860 if (count > row)
2861 break; /* Position is in this buffer line. */
2862 #ifdef FEAT_FOLDING
2863 (void)hasFoldingWin(win, lnum, NULL, &lnum, TRUE, NULL);
2864 #endif
2865 if (lnum == win->w_buffer->b_ml.ml_line_count)
2867 retval = TRUE;
2868 break; /* past end of file */
2870 row -= count;
2871 ++lnum;
2874 if (!retval)
2876 /* Compute the column without wrapping. */
2877 off = win_col_off(win) - win_col_off2(win);
2878 if (col < off)
2879 col = off;
2880 col += row * (W_WIDTH(win) - off);
2881 /* add skip column (for long wrapping line) */
2882 col += win->w_skipcol;
2885 if (!win->w_p_wrap)
2886 col += win->w_leftcol;
2888 /* skip line number and fold column in front of the line */
2889 col -= win_col_off(win);
2890 if (col < 0)
2892 #ifdef FEAT_NETBEANS_INTG
2893 if (usingNetbeans)
2894 netbeans_gutter_click(lnum);
2895 #endif
2896 col = 0;
2899 *colp = col;
2900 *rowp = row;
2901 *lnump = lnum;
2902 return retval;
2905 #if defined(FEAT_WINDOWS) || defined(PROTO)
2907 * Find the window at screen position "*rowp" and "*colp". The positions are
2908 * updated to become relative to the top-left of the window.
2910 /*ARGSUSED*/
2911 win_T *
2912 mouse_find_win(rowp, colp)
2913 int *rowp;
2914 int *colp;
2916 frame_T *fp;
2918 fp = topframe;
2919 *rowp -= firstwin->w_winrow;
2920 for (;;)
2922 if (fp->fr_layout == FR_LEAF)
2923 break;
2924 #ifdef FEAT_VERTSPLIT
2925 if (fp->fr_layout == FR_ROW)
2927 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
2929 if (*colp < fp->fr_width)
2930 break;
2931 *colp -= fp->fr_width;
2934 #endif
2935 else /* fr_layout == FR_COL */
2937 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
2939 if (*rowp < fp->fr_height)
2940 break;
2941 *rowp -= fp->fr_height;
2945 return fp->fr_win;
2947 #endif
2949 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined (FEAT_GUI_MAC) \
2950 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2951 || defined(FEAT_GUI_PHOTON) || defined(PROTO) \
2952 || defined(FEAT_GUI_MACVIM)
2954 * Translate window coordinates to buffer position without any side effects
2957 get_fpos_of_mouse(mpos)
2958 pos_T *mpos;
2960 win_T *wp;
2961 int row = mouse_row;
2962 int col = mouse_col;
2964 if (row < 0 || col < 0) /* check if it makes sense */
2965 return IN_UNKNOWN;
2967 #ifdef FEAT_WINDOWS
2968 /* find the window where the row is in */
2969 wp = mouse_find_win(&row, &col);
2970 #else
2971 wp = firstwin;
2972 #endif
2974 * winpos and height may change in win_enter()!
2976 if (row >= wp->w_height) /* In (or below) status line */
2977 return IN_STATUS_LINE;
2978 #ifdef FEAT_VERTSPLIT
2979 if (col >= wp->w_width) /* In vertical separator line */
2980 return IN_SEP_LINE;
2981 #endif
2983 if (wp != curwin)
2984 return IN_UNKNOWN;
2986 /* compute the position in the buffer line from the posn on the screen */
2987 if (mouse_comp_pos(curwin, &row, &col, &mpos->lnum))
2988 return IN_STATUS_LINE; /* past bottom */
2990 mpos->col = vcol2col(wp, mpos->lnum, col);
2992 if (mpos->col > 0)
2993 --mpos->col;
2994 return IN_BUFFER;
2998 * Convert a virtual (screen) column to a character column.
2999 * The first column is one.
3002 vcol2col(wp, lnum, vcol)
3003 win_T *wp;
3004 linenr_T lnum;
3005 int vcol;
3007 /* try to advance to the specified column */
3008 int col = 0;
3009 int count = 0;
3010 char_u *ptr;
3012 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3013 while (count <= vcol && *ptr != NUL)
3015 ++col;
3016 count += win_lbr_chartabsize(wp, ptr, count, NULL);
3017 mb_ptr_adv(ptr);
3019 return col;
3021 #endif
3023 #endif /* FEAT_MOUSE */
3025 #if defined(FEAT_GUI) || defined(WIN3264) || defined(PROTO)
3027 * Called when focus changed. Used for the GUI or for systems where this can
3028 * be done in the console (Win32).
3030 void
3031 ui_focus_change(in_focus)
3032 int in_focus; /* TRUE if focus gained. */
3034 static time_t last_time = (time_t)0;
3035 int need_redraw = FALSE;
3037 /* When activated: Check if any file was modified outside of Vim.
3038 * Only do this when not done within the last two seconds (could get
3039 * several events in a row). */
3040 if (in_focus && last_time + 2 < time(NULL))
3042 need_redraw = check_timestamps(
3043 # ifdef FEAT_GUI
3044 gui.in_use
3045 # else
3046 FALSE
3047 # endif
3049 last_time = time(NULL);
3052 #ifdef FEAT_AUTOCMD
3054 * Fire the focus gained/lost autocommand.
3056 need_redraw |= apply_autocmds(in_focus ? EVENT_FOCUSGAINED
3057 : EVENT_FOCUSLOST, NULL, NULL, FALSE, curbuf);
3058 #endif
3060 if (need_redraw)
3062 /* Something was executed, make sure the cursor is put back where it
3063 * belongs. */
3064 need_wait_return = FALSE;
3066 if (State & CMDLINE)
3067 redrawcmdline();
3068 else if (State == HITRETURN || State == SETWSIZE || State == ASKMORE
3069 || State == EXTERNCMD || State == CONFIRM || exmode_active)
3070 repeat_message();
3071 else if ((State & NORMAL) || (State & INSERT))
3073 if (must_redraw != 0)
3074 update_screen(0);
3075 setcursor();
3077 cursor_on(); /* redrawing may have switched it off */
3078 out_flush();
3079 # ifdef FEAT_GUI
3080 if (gui.in_use)
3082 gui_update_cursor(FALSE, TRUE);
3083 gui_update_scrollbars(FALSE);
3085 # endif
3087 #ifdef FEAT_TITLE
3088 /* File may have been changed from 'readonly' to 'noreadonly' */
3089 if (need_maketitle)
3090 maketitle();
3091 #endif
3093 #endif
3095 #if defined(USE_IM_CONTROL) || defined(PROTO)
3097 * Save current Input Method status to specified place.
3099 void
3100 im_save_status(psave)
3101 long *psave;
3103 /* Don't save when 'imdisable' is set or "xic" is NULL, IM is always
3104 * disabled then (but might start later).
3105 * Also don't save when inside a mapping, vgetc_im_active has not been set
3106 * then.
3107 * And don't save when the keys were stuffed (e.g., for a "." command).
3108 * And don't save when the GUI is running but our window doesn't have
3109 * input focus (e.g., when a find dialog is open). */
3110 if (!p_imdisable && KeyTyped && !KeyStuffed
3111 # ifdef FEAT_XIM
3112 && xic != NULL
3113 # endif
3114 # ifdef FEAT_GUI
3115 && (!gui.in_use || gui.in_focus)
3116 # endif
3119 /* Do save when IM is on, or IM is off and saved status is on. */
3120 if (vgetc_im_active)
3121 *psave = B_IMODE_IM;
3122 else if (*psave == B_IMODE_IM)
3123 *psave = B_IMODE_NONE;
3126 #endif