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.
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.
16 * 3. Input buffer stuff.
27 if (gui
.in_use
&& !gui
.dying
&& !gui
.starting
)
31 gui_wait_for_chars(p_wd
);
36 /* Don't output anything in silent mode ("ex -s") unless 'verbose' set */
37 if (!(silent_mode
&& p_verbose
== 0))
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
);
54 if (output_conv
.vc_type
!= CONV_NONE
)
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*/
72 ui_inchar_undo(s
, len
)
81 newlen
+= ta_len
- ta_off
;
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
);
92 mch_memmove(new, s
, (size_t)len
);
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
114 ui_inchar(buf
, maxlen
, wtime
, tb_change_cnt
)
117 long wtime
; /* don't use "time", MIPS cannot handle it */
122 #if defined(FEAT_GUI) && (defined(UNIX) || defined(VMS))
124 * Use the typeahead if there is any.
128 if (maxlen
>= ta_len
- ta_off
)
130 mch_memmove(buf
, ta_str
+ ta_off
, (size_t)ta_len
);
135 mch_memmove(buf
, ta_str
+ ta_off
, (size_t)maxlen
);
142 if (do_profiling
== PROF_YES
&& wtime
!= 0)
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;
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)
161 if (wtime
== -1 && ++count
== 1000)
169 /* When doing a blocking wait there is no need for CTRL-C to interrupt
170 * something, don't let it set got_int when it was mapped. */
171 if (mapped_ctrl_c
&& (wtime
== -1 || wtime
> 100L))
172 ctrl_c_interrupts
= FALSE
;
177 if (gui_wait_for_chars(wtime
) && !typebuf_changed(tb_change_cnt
))
178 retval
= read_from_input_buf(buf
, (long)maxlen
);
186 if (wtime
== -1 || wtime
> 100L)
187 /* allow signals to kill us */
188 (void)vim_handle_signal(SIGNAL_UNBLOCK
);
189 retval
= mch_inchar(buf
, maxlen
, wtime
, tb_change_cnt
);
190 if (wtime
== -1 || wtime
> 100L)
191 /* block SIGHUP et al. */
192 (void)vim_handle_signal(SIGNAL_BLOCK
);
196 ctrl_c_interrupts
= TRUE
;
198 #ifdef NO_CONSOLE_INPUT
202 if (do_profiling
== PROF_YES
&& wtime
!= 0)
209 * return non-zero if a character is available
218 return input_available();
222 # ifdef NO_CONSOLE_INPUT
223 if (no_console_input())
226 return mch_char_avail();
233 * Delay for the given number of milliseconds. If ignoreinput is FALSE then we
234 * cancel the delay if a key is hit.
237 ui_delay(msec
, ignoreinput
)
242 if (gui
.in_use
&& !ignoreinput
)
243 gui_wait_for_chars(msec
);
246 mch_delay(msec
, ignoreinput
);
250 * If the machine has job control, use it to suspend the program,
251 * otherwise fake it by starting a new shell.
252 * When running the GUI iconify the window.
267 #if !defined(UNIX) || !defined(SIGTSTP) || defined(PROTO) || defined(__BEOS__)
269 * When the OS can't really suspend, call this function to start a shell.
270 * This is never called in the GUI.
276 EMSG(_(e_shellempty
));
279 MSG_PUTS(_("new shell started\n"));
286 * Try to get the current Vim shell size. Put the result in Rows and Columns.
287 * Use the new sizes as defaults for 'columns' and 'lines'.
288 * Return OK when size could be determined, FAIL otherwise.
297 retval
= gui_get_shellsize();
300 retval
= mch_get_shellsize();
304 /* adjust the default for 'lines' and 'columns' */
307 set_number_default("lines", Rows
);
308 set_number_default("columns", Columns
);
314 * Set the size of the Vim shell according to Rows and Columns, if possible.
315 * The gui_set_shellsize() or mch_set_shellsize() function will try to set the
316 * new size. If this is not possible, it will adjust Rows and Columns.
320 ui_set_shellsize(mustset
)
321 int mustset
; /* set by the user */
325 gui_set_shellsize(mustset
,
338 * Called when Rows and/or Columns changed. Adjust scroll region and mouse
344 if (full_screen
&& !exiting
)
366 /*****************************************************************************
367 * Functions for copying and pasting text between applications.
368 * This is always included in a GUI version, but may also be included when the
369 * clipboard and mouse is available to a terminal version such as xterm.
370 * Note: there are some more functions in ops.c that handle selection stuff.
372 * Also note that the majority of functions here deal with the X 'primary'
373 * (visible - for Visual mode use) selection, and only that. There are no
374 * versions of these for the 'clipboard' selection, as Visual mode has no use
378 #if defined(FEAT_CLIPBOARD) || defined(PROTO)
381 * Selection stuff using Visual mode, for cutting and pasting text to other
386 * Call this to initialise the clipboard. Pass it FALSE if the clipboard code
387 * is included, but the clipboard can not be used, or TRUE if the clipboard can
388 * be used. Eg unix may call this with FALSE, then call it again with TRUE if
400 cb
->available
= can_use
;
406 cb
->state
= SELECT_CLEARED
;
408 if (cb
== &clip_plus
)
415 * Check whether the VIsual area has changed, and if so try to become the owner
416 * of the selection, and free any old converted selection we may still have
417 * lying around. If the VIsual mode has ended, make a copy of what was
418 * selected so we can still give it to others. Will probably have to make sure
419 * this is called whenever VIsual mode is ended.
422 clip_update_selection()
426 /* If visual mode is only due to a redo command ("."), then ignore it */
427 if (!redo_VIsual_busy
&& VIsual_active
&& (State
& NORMAL
))
429 if (lt(VIsual
, curwin
->w_cursor
))
432 end
= curwin
->w_cursor
;
435 end
.col
+= (*mb_ptr2len
)(ml_get_cursor()) - 1;
440 start
= curwin
->w_cursor
;
443 if (!equalpos(clip_star
.start
, start
)
444 || !equalpos(clip_star
.end
, end
)
445 || clip_star
.vmode
!= VIsual_mode
)
447 clip_clear_selection();
448 clip_star
.start
= start
;
450 clip_star
.vmode
= VIsual_mode
;
451 clip_free_selection(&clip_star
);
452 clip_own_selection(&clip_star
);
453 clip_gen_set_selection(&clip_star
);
459 clip_own_selection(cbd
)
463 * Also want to check somehow that we are reading from the keyboard rather
464 * than a mapping etc.
466 if (!cbd
->owned
&& cbd
->available
)
468 cbd
->owned
= (clip_gen_own_selection(cbd
) == OK
);
470 if (cbd
== &clip_star
)
472 /* May have to show a different kind of highlighting for the
473 * selected area. There is no specific redraw command for this,
474 * just redraw all windows on the current buffer. */
476 && (get_real_state() == VISUAL
477 || get_real_state() == SELECTMODE
)
479 && hl_attr(HLF_V
) != hl_attr(HLF_VNC
))
480 redraw_curbuf_later(INVERTED_ALL
);
487 clip_lose_selection(cbd
)
491 int was_owned
= cbd
->owned
;
493 int visual_selection
= (cbd
== &clip_star
);
495 clip_free_selection(cbd
);
497 if (visual_selection
)
498 clip_clear_selection();
499 clip_gen_lose_selection(cbd
);
501 if (visual_selection
)
503 /* May have to show a different kind of highlighting for the selected
504 * area. There is no specific redraw command for this, just redraw all
505 * windows on the current buffer. */
507 && (get_real_state() == VISUAL
508 || get_real_state() == SELECTMODE
)
510 && hl_attr(HLF_V
) != hl_attr(HLF_VNC
))
512 update_curbuf(INVERTED_ALL
);
518 gui_update_cursor(TRUE
, FALSE
);
526 clip_copy_selection()
528 if (VIsual_active
&& (State
& NORMAL
) && clip_star
.available
)
530 if (clip_isautosel())
531 clip_update_selection();
532 clip_free_selection(&clip_star
);
533 clip_own_selection(&clip_star
);
535 clip_get_selection(&clip_star
);
536 clip_gen_set_selection(&clip_star
);
541 * Called when Visual mode is ended: update the selection.
546 if (clip_isautosel())
547 clip_copy_selection();
551 * Return TRUE if automatic selection of Visual area is desired.
558 gui
.in_use
? (vim_strchr(p_go
, GO_ASEL
) != NULL
) :
565 * Stuff for general mouse selection, without using Visual mode.
568 static int clip_compare_pos
__ARGS((int row1
, int col1
, int row2
, int col2
));
569 static void clip_invert_area
__ARGS((int, int, int, int, int how
));
570 static void clip_invert_rectangle
__ARGS((int row
, int col
, int height
, int width
, int invert
));
571 static void clip_get_word_boundaries
__ARGS((VimClipboard
*, int, int));
572 static int clip_get_line_end
__ARGS((int));
573 static void clip_update_modeless_selection
__ARGS((VimClipboard
*, int, int,
576 /* flags for clip_invert_area() */
579 #define CLIP_TOGGLE 3
582 * Start, continue or end a modeless selection. Used when editing the
583 * command-line and in the cmdline window.
586 clip_modeless(button
, is_click
, is_drag
)
593 repeat
= ((clip_star
.mode
== SELECT_MODE_CHAR
594 || clip_star
.mode
== SELECT_MODE_LINE
)
595 && (mod_mask
& MOD_MASK_2CLICK
))
596 || (clip_star
.mode
== SELECT_MODE_WORD
597 && (mod_mask
& MOD_MASK_3CLICK
));
598 if (is_click
&& button
== MOUSE_RIGHT
)
600 /* Right mouse button: If there was no selection, start one.
601 * Otherwise extend the existing selection. */
602 if (clip_star
.state
== SELECT_CLEARED
)
603 clip_start_selection(mouse_col
, mouse_row
, FALSE
);
604 clip_process_selection(button
, mouse_col
, mouse_row
, repeat
);
607 clip_start_selection(mouse_col
, mouse_row
, repeat
);
610 /* Don't try extending a selection if there isn't one. Happens when
611 * button-down is in the cmdline and them moving mouse upwards. */
612 if (clip_star
.state
!= SELECT_CLEARED
)
613 clip_process_selection(button
, mouse_col
, mouse_row
, repeat
);
616 clip_process_selection(MOUSE_RELEASE
, mouse_col
, mouse_row
, FALSE
);
620 * Compare two screen positions ala strcmp()
623 clip_compare_pos(row1
, col1
, row2
, col2
)
629 if (row1
> row2
) return(1);
630 if (row1
< row2
) return(-1);
631 if (col1
> col2
) return(1);
632 if (col1
< col2
) return(-1);
637 * Start the selection
640 clip_start_selection(col
, row
, repeated_click
)
645 VimClipboard
*cb
= &clip_star
;
647 if (cb
->state
== SELECT_DONE
)
648 clip_clear_selection();
650 row
= check_row(row
);
651 col
= check_col(col
);
653 col
= mb_fix_col(col
, row
);
656 cb
->start
.lnum
= row
;
659 cb
->origin_row
= (short_u
)cb
->start
.lnum
;
660 cb
->state
= SELECT_IN_PROGRESS
;
664 if (++cb
->mode
> SELECT_MODE_LINE
)
665 cb
->mode
= SELECT_MODE_CHAR
;
668 cb
->mode
= SELECT_MODE_CHAR
;
671 /* clear the cursor until the selection is made */
678 case SELECT_MODE_CHAR
:
679 cb
->origin_start_col
= cb
->start
.col
;
680 cb
->word_end_col
= clip_get_line_end((int)cb
->start
.lnum
);
683 case SELECT_MODE_WORD
:
684 clip_get_word_boundaries(cb
, (int)cb
->start
.lnum
, cb
->start
.col
);
685 cb
->origin_start_col
= cb
->word_start_col
;
686 cb
->origin_end_col
= cb
->word_end_col
;
688 clip_invert_area((int)cb
->start
.lnum
, cb
->word_start_col
,
689 (int)cb
->end
.lnum
, cb
->word_end_col
, CLIP_SET
);
690 cb
->start
.col
= cb
->word_start_col
;
691 cb
->end
.col
= cb
->word_end_col
;
694 case SELECT_MODE_LINE
:
695 clip_invert_area((int)cb
->start
.lnum
, 0, (int)cb
->start
.lnum
,
696 (int)Columns
, CLIP_SET
);
698 cb
->end
.col
= Columns
;
702 cb
->prev
= cb
->start
;
704 #ifdef DEBUG_SELECTION
705 printf("Selection started at (%u,%u)\n", cb
->start
.lnum
, cb
->start
.col
);
710 * Continue processing the selection
713 clip_process_selection(button
, col
, row
, repeated_click
)
717 int_u repeated_click
;
719 VimClipboard
*cb
= &clip_star
;
721 int slen
= 1; /* cursor shape width */
723 if (button
== MOUSE_RELEASE
)
725 /* Check to make sure we have something selected */
726 if (cb
->start
.lnum
== cb
->end
.lnum
&& cb
->start
.col
== cb
->end
.col
)
730 gui_update_cursor(FALSE
, FALSE
);
732 cb
->state
= SELECT_CLEARED
;
736 #ifdef DEBUG_SELECTION
737 printf("Selection ended: (%u,%u) to (%u,%u)\n", cb
->start
.lnum
,
738 cb
->start
.col
, cb
->end
.lnum
, cb
->end
.col
);
743 gui
.in_use
? (vim_strchr(p_go
, GO_ASELML
) != NULL
) :
746 clip_copy_modeless_selection(FALSE
);
749 gui_update_cursor(FALSE
, FALSE
);
752 cb
->state
= SELECT_DONE
;
756 row
= check_row(row
);
757 col
= check_col(col
);
759 col
= mb_fix_col(col
, row
);
762 if (col
== (int)cb
->prev
.col
&& row
== cb
->prev
.lnum
&& !repeated_click
)
766 * When extending the selection with the right mouse button, swap the
767 * start and end if the position is before half the selection
769 if (cb
->state
== SELECT_DONE
&& button
== MOUSE_RIGHT
)
772 * If the click is before the start, or the click is inside the
773 * selection and the start is the closest side, set the origin to the
774 * end of the selection.
776 if (clip_compare_pos(row
, col
, (int)cb
->start
.lnum
, cb
->start
.col
) < 0
777 || (clip_compare_pos(row
, col
,
778 (int)cb
->end
.lnum
, cb
->end
.col
) < 0
779 && (((cb
->start
.lnum
== cb
->end
.lnum
780 && cb
->end
.col
- col
> col
- cb
->start
.col
))
781 || ((diff
= (cb
->end
.lnum
- row
) -
782 (row
- cb
->start
.lnum
)) > 0
783 || (diff
== 0 && col
< (int)(cb
->start
.col
+
784 cb
->end
.col
) / 2)))))
786 cb
->origin_row
= (short_u
)cb
->end
.lnum
;
787 cb
->origin_start_col
= cb
->end
.col
- 1;
788 cb
->origin_end_col
= cb
->end
.col
;
792 cb
->origin_row
= (short_u
)cb
->start
.lnum
;
793 cb
->origin_start_col
= cb
->start
.col
;
794 cb
->origin_end_col
= cb
->start
.col
;
796 if (cb
->mode
== SELECT_MODE_WORD
&& !repeated_click
)
797 cb
->mode
= SELECT_MODE_CHAR
;
800 /* set state, for when using the right mouse button */
801 cb
->state
= SELECT_IN_PROGRESS
;
803 #ifdef DEBUG_SELECTION
804 printf("Selection extending to (%d,%d)\n", row
, col
);
807 if (repeated_click
&& ++cb
->mode
> SELECT_MODE_LINE
)
808 cb
->mode
= SELECT_MODE_CHAR
;
812 case SELECT_MODE_CHAR
:
813 /* If we're on a different line, find where the line ends */
814 if (row
!= cb
->prev
.lnum
)
815 cb
->word_end_col
= clip_get_line_end(row
);
817 /* See if we are before or after the origin of the selection */
818 if (clip_compare_pos(row
, col
, cb
->origin_row
,
819 cb
->origin_start_col
) >= 0)
821 if (col
>= (int)cb
->word_end_col
)
822 clip_update_modeless_selection(cb
, cb
->origin_row
,
823 cb
->origin_start_col
, row
, (int)Columns
);
827 if (has_mbyte
&& mb_lefthalve(row
, col
))
830 clip_update_modeless_selection(cb
, cb
->origin_row
,
831 cb
->origin_start_col
, row
, col
+ slen
);
838 && mb_lefthalve(cb
->origin_row
, cb
->origin_start_col
))
841 if (col
>= (int)cb
->word_end_col
)
842 clip_update_modeless_selection(cb
, row
, cb
->word_end_col
,
843 cb
->origin_row
, cb
->origin_start_col
+ slen
);
845 clip_update_modeless_selection(cb
, row
, col
,
846 cb
->origin_row
, cb
->origin_start_col
+ slen
);
850 case SELECT_MODE_WORD
:
851 /* If we are still within the same word, do nothing */
852 if (row
== cb
->prev
.lnum
&& col
>= (int)cb
->word_start_col
853 && col
< (int)cb
->word_end_col
&& !repeated_click
)
856 /* Get new word boundaries */
857 clip_get_word_boundaries(cb
, row
, col
);
859 /* Handle being after the origin point of selection */
860 if (clip_compare_pos(row
, col
, cb
->origin_row
,
861 cb
->origin_start_col
) >= 0)
862 clip_update_modeless_selection(cb
, cb
->origin_row
,
863 cb
->origin_start_col
, row
, cb
->word_end_col
);
865 clip_update_modeless_selection(cb
, row
, cb
->word_start_col
,
866 cb
->origin_row
, cb
->origin_end_col
);
869 case SELECT_MODE_LINE
:
870 if (row
== cb
->prev
.lnum
&& !repeated_click
)
873 if (clip_compare_pos(row
, col
, cb
->origin_row
,
874 cb
->origin_start_col
) >= 0)
875 clip_update_modeless_selection(cb
, cb
->origin_row
, 0, row
,
878 clip_update_modeless_selection(cb
, row
, 0, cb
->origin_row
,
886 #ifdef DEBUG_SELECTION
887 printf("Selection is: (%u,%u) to (%u,%u)\n", cb
->start
.lnum
,
888 cb
->start
.col
, cb
->end
.lnum
, cb
->end
.col
);
894 * Called after an Expose event to redraw the selection
897 clip_redraw_selection(x
, y
, w
, h
)
903 VimClipboard
*cb
= &clip_star
;
904 int row1
, col1
, row2
, col2
;
909 if (cb
->state
== SELECT_CLEARED
)
912 row1
= check_row(Y_2_ROW(y
));
913 col1
= check_col(X_2_COL(x
));
914 row2
= check_row(Y_2_ROW(y
+ h
- 1));
915 col2
= check_col(X_2_COL(x
+ w
- 1));
917 /* Limit the rows that need to be re-drawn */
918 if (cb
->start
.lnum
> row1
)
919 row1
= cb
->start
.lnum
;
920 if (cb
->end
.lnum
< row2
)
923 /* Look at each row that might need to be re-drawn */
924 for (row
= row1
; row
<= row2
; row
++)
926 /* For the first selection row, use the starting selection column */
927 if (row
== cb
->start
.lnum
)
928 start
= cb
->start
.col
;
932 /* For the last selection row, use the ending selection column */
933 if (row
== cb
->end
.lnum
)
945 gui_mch_invert_rectangle(row
, start
, 1, end
- start
);
950 # if defined(FEAT_GUI) || defined(PROTO)
952 * Redraw part of the selection if character at "row,col" is inside of it.
953 * Only used for the GUI.
956 clip_may_redraw_selection(row
, col
, len
)
963 if (clip_star
.state
!= SELECT_CLEARED
964 && row
>= clip_star
.start
.lnum
965 && row
<= clip_star
.end
.lnum
)
967 if (row
== clip_star
.start
.lnum
&& start
< (int)clip_star
.start
.col
)
968 start
= clip_star
.start
.col
;
969 if (row
== clip_star
.end
.lnum
&& end
> (int)clip_star
.end
.col
)
970 end
= clip_star
.end
.col
;
972 clip_invert_area(row
, start
, row
, end
, 0);
978 * Called from outside to clear selected region from the display
981 clip_clear_selection()
983 VimClipboard
*cb
= &clip_star
;
985 if (cb
->state
== SELECT_CLEARED
)
988 clip_invert_area((int)cb
->start
.lnum
, cb
->start
.col
, (int)cb
->end
.lnum
,
989 cb
->end
.col
, CLIP_CLEAR
);
990 cb
->state
= SELECT_CLEARED
;
994 * Clear the selection if any lines from "row1" to "row2" are inside of it.
997 clip_may_clear_selection(row1
, row2
)
1000 if (clip_star
.state
== SELECT_DONE
1001 && row2
>= clip_star
.start
.lnum
1002 && row1
<= clip_star
.end
.lnum
)
1003 clip_clear_selection();
1007 * Called before the screen is scrolled up or down. Adjusts the line numbers
1008 * of the selection. Call with big number when clearing the screen.
1011 clip_scroll_selection(rows
)
1012 int rows
; /* negative for scroll down */
1016 if (clip_star
.state
== SELECT_CLEARED
)
1019 lnum
= clip_star
.start
.lnum
- rows
;
1021 clip_star
.start
.lnum
= 0;
1022 else if (lnum
>= screen_Rows
) /* scrolled off of the screen */
1023 clip_star
.state
= SELECT_CLEARED
;
1025 clip_star
.start
.lnum
= lnum
;
1027 lnum
= clip_star
.end
.lnum
- rows
;
1028 if (lnum
< 0) /* scrolled off of the screen */
1029 clip_star
.state
= SELECT_CLEARED
;
1030 else if (lnum
>= screen_Rows
)
1031 clip_star
.end
.lnum
= screen_Rows
- 1;
1033 clip_star
.end
.lnum
= lnum
;
1037 * Invert a region of the display between a starting and ending row and column
1039 * CLIP_CLEAR: undo inversion
1040 * CLIP_SET: set inversion
1041 * CLIP_TOGGLE: set inversion if pos1 < pos2, undo inversion otherwise.
1042 * 0: invert (GUI only).
1045 clip_invert_area(row1
, col1
, row2
, col2
, how
)
1054 if (how
== CLIP_SET
)
1057 /* Swap the from and to positions so the from is always before */
1058 if (clip_compare_pos(row1
, col1
, row2
, col2
) > 0)
1060 int tmp_row
, tmp_col
;
1069 else if (how
== CLIP_TOGGLE
)
1072 /* If all on the same line, do it the easy way */
1075 clip_invert_rectangle(row1
, col1
, 1, col2
- col1
, invert
);
1079 /* Handle a piece of the first line */
1082 clip_invert_rectangle(row1
, col1
, 1, (int)Columns
- col1
, invert
);
1086 /* Handle a piece of the last line */
1087 if (col2
< Columns
- 1)
1089 clip_invert_rectangle(row2
, 0, 1, col2
, invert
);
1093 /* Handle the rectangle thats left */
1095 clip_invert_rectangle(row1
, 0, row2
- row1
+ 1, (int)Columns
,
1101 * Invert or un-invert a rectangle of the screen.
1102 * "invert" is true if the result is inverted.
1105 clip_invert_rectangle(row
, col
, height
, width
, invert
)
1114 gui_mch_invert_rectangle(row
, col
, height
, width
);
1117 screen_draw_rectangle(row
, col
, height
, width
, invert
);
1121 * Copy the currently selected area into the '*' register so it will be
1122 * available for pasting.
1123 * When "both" is TRUE also copy to the '+' register.
1127 clip_copy_modeless_selection(both
)
1136 int add_newline_flag
= FALSE
;
1141 int row1
= clip_star
.start
.lnum
;
1142 int col1
= clip_star
.start
.col
;
1143 int row2
= clip_star
.end
.lnum
;
1144 int col2
= clip_star
.end
.col
;
1146 /* Can't use ScreenLines unless initialized */
1147 if (ScreenLines
== NULL
)
1151 * Make sure row1 <= row2, and if row1 == row2 that col1 <= col2.
1155 row
= row1
; row1
= row2
; row2
= row
;
1156 row
= col1
; col1
= col2
; col2
= row
;
1158 else if (row1
== row2
&& col1
> col2
)
1160 row
= col1
; col1
= col2
; col2
= row
;
1163 /* correct starting point for being on right halve of double-wide char */
1164 p
= ScreenLines
+ LineOffset
[row1
];
1166 col1
-= (*mb_head_off
)(p
, p
+ col1
);
1167 else if (enc_utf8
&& p
[col1
] == 0)
1171 /* Create a temporary buffer for storing the text */
1172 len
= (row2
- row1
+ 1) * Columns
+ 1;
1175 len
*= 2; /* max. 2 bytes per display cell */
1179 buffer
= lalloc((long_u
)len
, TRUE
);
1180 if (buffer
== NULL
) /* out of memory */
1183 /* Process each row in the selection */
1184 for (bufp
= buffer
, row
= row1
; row
<= row2
; row
++)
1196 line_end_col
= clip_get_line_end(row
);
1198 /* See if we need to nuke some trailing whitespace */
1199 if (end_col
>= Columns
&& (row
< row2
|| end_col
> line_end_col
))
1201 /* Get rid of trailing whitespace */
1202 end_col
= line_end_col
;
1203 if (end_col
< start_col
)
1204 end_col
= start_col
;
1206 /* If the last line extended to the end, add an extra newline */
1208 add_newline_flag
= TRUE
;
1211 /* If after the first row, we need to always add a newline */
1212 if (row
> row1
&& !LineWraps
[row
- 1])
1215 if (row
< screen_Rows
&& end_col
<= screen_Columns
)
1222 p
= ScreenLines
+ LineOffset
[row
];
1223 for (i
= start_col
; i
< end_col
; ++i
)
1224 if (enc_dbcs
== DBCS_JPNU
&& p
[i
] == 0x8e)
1226 /* single-width double-byte char */
1228 *bufp
++ = ScreenLines2
[LineOffset
[row
] + i
];
1233 if (MB_BYTE2LEN(p
[i
]) == 2)
1243 off
= LineOffset
[row
];
1244 for (i
= start_col
; i
< end_col
; ++i
)
1246 /* The base character is either in ScreenLinesUC[] or
1248 if (ScreenLinesUC
[off
+ i
] == 0)
1249 *bufp
++ = ScreenLines
[off
+ i
];
1252 bufp
+= utf_char2bytes(ScreenLinesUC
[off
+ i
], bufp
);
1253 for (ci
= 0; ci
< Screen_mco
; ++ci
)
1255 /* Add a composing character. */
1256 if (ScreenLinesC
[ci
][off
+ i
] == 0)
1258 bufp
+= utf_char2bytes(ScreenLinesC
[ci
][off
+ i
],
1262 /* Skip right halve of double-wide character. */
1263 if (ScreenLines
[off
+ i
+ 1] == 0)
1270 STRNCPY(bufp
, ScreenLines
+ LineOffset
[row
] + start_col
,
1271 end_col
- start_col
);
1272 bufp
+= end_col
- start_col
;
1277 /* Add a newline at the end if the selection ended there */
1278 if (add_newline_flag
)
1281 /* First cleanup any old selection and become the owner. */
1282 clip_free_selection(&clip_star
);
1283 clip_own_selection(&clip_star
);
1285 /* Yank the text into the '*' register. */
1286 clip_yank_selection(MCHAR
, buffer
, (long)(bufp
- buffer
), &clip_star
);
1288 /* Make the register contents available to the outside world. */
1289 clip_gen_set_selection(&clip_star
);
1294 /* Do the same for the '+' register. */
1295 clip_free_selection(&clip_plus
);
1296 clip_own_selection(&clip_plus
);
1297 clip_yank_selection(MCHAR
, buffer
, (long)(bufp
- buffer
), &clip_plus
);
1298 clip_gen_set_selection(&clip_plus
);
1305 * Find the starting and ending positions of the word at the given row and
1306 * column. Only white-separated words are recognized here.
1308 #define CHAR_CLASS(c) (c <= ' ' ? ' ' : vim_iswordc(c))
1311 clip_get_word_boundaries(cb
, row
, col
)
1323 if (row
>= screen_Rows
|| col
>= screen_Columns
|| ScreenLines
== NULL
)
1326 p
= ScreenLines
+ LineOffset
[row
];
1328 /* Correct for starting in the right halve of a double-wide char */
1330 col
-= dbcs_screen_head_off(p
, p
+ col
);
1331 else if (enc_utf8
&& p
[col
] == 0)
1334 start_class
= CHAR_CLASS(p
[col
]);
1337 for ( ; temp_col
> 0; temp_col
--)
1340 && (mboff
= dbcs_screen_head_off(p
, p
+ temp_col
- 1)) > 0)
1344 if (CHAR_CLASS(p
[temp_col
- 1]) != start_class
1346 && !(enc_utf8
&& p
[temp_col
- 1] == 0)
1350 cb
->word_start_col
= temp_col
;
1353 for ( ; temp_col
< screen_Columns
; temp_col
++)
1355 if (enc_dbcs
!= 0 && dbcs_ptr2cells(p
+ temp_col
) == 2)
1359 if (CHAR_CLASS(p
[temp_col
]) != start_class
1361 && !(enc_utf8
&& p
[temp_col
] == 0)
1365 cb
->word_end_col
= temp_col
;
1369 * Find the column position for the last non-whitespace character on the given
1373 clip_get_line_end(row
)
1378 if (row
>= screen_Rows
|| ScreenLines
== NULL
)
1380 for (i
= screen_Columns
; i
> 0; i
--)
1381 if (ScreenLines
[LineOffset
[row
] + i
- 1] != ' ')
1387 * Update the currently selected region by adding and/or subtracting from the
1388 * beginning or end and inverting the changed area(s).
1391 clip_update_modeless_selection(cb
, row1
, col1
, row2
, col2
)
1398 /* See if we changed at the beginning of the selection */
1399 if (row1
!= cb
->start
.lnum
|| col1
!= (int)cb
->start
.col
)
1401 clip_invert_area(row1
, col1
, (int)cb
->start
.lnum
, cb
->start
.col
,
1403 cb
->start
.lnum
= row1
;
1404 cb
->start
.col
= col1
;
1407 /* See if we changed at the end of the selection */
1408 if (row2
!= cb
->end
.lnum
|| col2
!= (int)cb
->end
.col
)
1410 clip_invert_area((int)cb
->end
.lnum
, cb
->end
.col
, row2
, col2
,
1412 cb
->end
.lnum
= row2
;
1418 clip_gen_own_selection(cbd
)
1421 #ifdef FEAT_XCLIPBOARD
1424 return clip_mch_own_selection(cbd
);
1427 return clip_xterm_own_selection(cbd
);
1429 return clip_mch_own_selection(cbd
);
1434 clip_gen_lose_selection(cbd
)
1437 #ifdef FEAT_XCLIPBOARD
1440 clip_mch_lose_selection(cbd
);
1443 clip_xterm_lose_selection(cbd
);
1445 clip_mch_lose_selection(cbd
);
1450 clip_gen_set_selection(cbd
)
1453 #ifdef FEAT_XCLIPBOARD
1456 clip_mch_set_selection(cbd
);
1459 clip_xterm_set_selection(cbd
);
1461 clip_mch_set_selection(cbd
);
1466 clip_gen_request_selection(cbd
)
1469 #ifdef FEAT_XCLIPBOARD
1472 clip_mch_request_selection(cbd
);
1475 clip_xterm_request_selection(cbd
);
1477 clip_mch_request_selection(cbd
);
1481 #endif /* FEAT_CLIPBOARD */
1483 /*****************************************************************************
1484 * Functions that handle the input buffer.
1485 * This is used for any GUI version, and the unix terminal version.
1487 * For Unix, the input characters are buffered to be able to check for a
1488 * CTRL-C. This should be done with signals, but I don't know how to do that
1489 * in a portable way for a tty in RAW mode.
1491 * For the client-server code in the console the received keys are put in the
1495 #if defined(USE_INPUT_BUF) || defined(PROTO)
1498 * Internal typeahead buffer. Includes extra space for long key code
1499 * descriptions which would otherwise overflow. The buffer is considered full
1500 * when only this extra space (or part of it) remains.
1502 #if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \
1503 || defined(FEAT_CLIENTSERVER)
1505 * Sun WorkShop and NetBeans stuff debugger commands into the input buffer.
1506 * This requires a larger buffer...
1507 * (Madsen) Go with this for remote input as well ...
1509 # define INBUFLEN 4096
1511 # define INBUFLEN 250
1514 static char_u inbuf
[INBUFLEN
+ MAX_KEY_CODE_LEN
];
1515 static int inbufcount
= 0; /* number of chars in inbuf[] */
1518 * vim_is_input_buf_full(), vim_is_input_buf_empty(), add_to_input_buf(), and
1519 * trash_input_buf() are functions for manipulating the input buffer. These
1520 * are used by the gui_* calls when a GUI is used to handle keyboard input.
1524 vim_is_input_buf_full()
1526 return (inbufcount
>= INBUFLEN
);
1530 vim_is_input_buf_empty()
1532 return (inbufcount
== 0);
1535 #if defined(FEAT_OLE) || defined(PROTO)
1537 vim_free_in_input_buf()
1539 return (INBUFLEN
- inbufcount
);
1543 #if defined(FEAT_GUI_GTK) || defined(PROTO)
1545 vim_used_in_input_buf()
1551 #if defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) || defined(PROTO)
1553 * Return the current contents of the input buffer and make it empty.
1554 * The returned pointer must be passed to set_input_buf() later.
1561 /* We use a growarray to store the data pointer and the length. */
1562 gap
= (garray_T
*)alloc((unsigned)sizeof(garray_T
));
1565 /* Add one to avoid a zero size. */
1566 gap
->ga_data
= alloc((unsigned)inbufcount
+ 1);
1567 if (gap
->ga_data
!= NULL
)
1568 mch_memmove(gap
->ga_data
, inbuf
, (size_t)inbufcount
);
1569 gap
->ga_len
= inbufcount
;
1572 return (char_u
*)gap
;
1576 * Restore the input buffer with a pointer returned from get_input_buf().
1577 * The allocated memory is freed, this only works once!
1583 garray_T
*gap
= (garray_T
*)p
;
1587 if (gap
->ga_data
!= NULL
)
1589 mch_memmove(inbuf
, gap
->ga_data
, gap
->ga_len
);
1590 inbufcount
= gap
->ga_len
;
1591 vim_free(gap
->ga_data
);
1598 #if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM) \
1599 || defined(FEAT_XCLIPBOARD) || defined(VMS) \
1600 || defined(FEAT_SNIFF) || defined(FEAT_CLIENTSERVER) \
1601 || (defined(FEAT_GUI) && (!defined(USE_ON_FLY_SCROLL) \
1602 || defined(FEAT_MENU))) \
1605 * Add the given bytes to the input buffer
1606 * Special keys start with CSI. A real CSI must have been translated to
1607 * CSI KS_EXTRA KE_CSI. K_SPECIAL doesn't require translation.
1610 add_to_input_buf(s
, len
)
1614 if (inbufcount
+ len
> INBUFLEN
+ MAX_KEY_CODE_LEN
)
1615 return; /* Shouldn't ever happen! */
1617 #ifdef FEAT_HANGULIN
1618 if ((State
& (INSERT
|CMDLINE
)) && hangul_input_state_get())
1619 if ((len
= hangul_input_process(s
, len
)) == 0)
1624 inbuf
[inbufcount
++] = *s
++;
1628 #if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) \
1629 || (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) \
1630 || (defined(FEAT_GUI) && (!defined(USE_ON_FLY_SCROLL) \
1631 || defined(FEAT_MENU))) \
1634 * Add "str[len]" to the input buffer while escaping CSI bytes.
1637 add_to_input_buf_csi(char_u
*str
, int len
)
1642 for (i
= 0; i
< len
; ++i
)
1644 add_to_input_buf(str
+ i
, 1);
1647 /* Turn CSI into K_CSI. */
1649 buf
[1] = (int)KE_CSI
;
1650 add_to_input_buf(buf
, 2);
1656 #if defined(FEAT_HANGULIN) || defined(PROTO)
1658 push_raw_key (s
, len
)
1663 inbuf
[inbufcount
++] = *s
++;
1667 #if defined(FEAT_GUI) || defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) \
1669 /* Remove everything from the input buffer. Called when ^C is found */
1678 * Read as much data from the input buffer as possible up to maxlen, and store
1680 * Note: this function used to be Read() in unix.c
1683 read_from_input_buf(buf
, maxlen
)
1687 if (inbufcount
== 0) /* if the buffer is empty, fill it */
1688 fill_input_buf(TRUE
);
1689 if (maxlen
> inbufcount
)
1690 maxlen
= inbufcount
;
1691 mch_memmove(buf
, inbuf
, (size_t)maxlen
);
1692 inbufcount
-= maxlen
;
1694 mch_memmove(inbuf
, inbuf
+ maxlen
, (size_t)inbufcount
);
1700 fill_input_buf(exit_on_error
)
1703 #if defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X_UNIX)
1706 static int did_read_something
= FALSE
;
1708 static char_u
*rest
= NULL
; /* unconverted rest of previous read */
1709 static int restlen
= 0;
1716 # ifdef NO_CONSOLE_INPUT
1717 /* Don't use the GUI input when the window hasn't been opened yet.
1718 * We get here from ui_inchar() when we should try reading from stdin. */
1719 && !no_console_input()
1727 #if defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X_UNIX)
1728 if (vim_is_input_buf_full())
1731 * Fill_input_buf() is only called when we really need a character.
1732 * If we can't get any, but there is some in the buffer, just return.
1733 * If we can't get any, and there isn't any in the buffer, we give up and
1738 * On the BeBox version (for now), all input is secretly performed within
1739 * beos_select() which is called from RealWaitForChar().
1741 while (!vim_is_input_buf_full() && RealWaitForChar(read_cmd_fd
, 0, NULL
))
1748 if (sniff_request_waiting
)
1750 add_to_input_buf((char_u
*)"\233sniff",6); /* results in K_SNIFF */
1751 sniff_request_waiting
= 0;
1752 want_sniff_request
= 0;
1760 /* Use remainder of previous call, starts with an invalid character
1761 * that may become valid when reading more. */
1762 if (restlen
> INBUFLEN
- inbufcount
)
1763 unconverted
= INBUFLEN
- inbufcount
;
1765 unconverted
= restlen
;
1766 mch_memmove(inbuf
+ inbufcount
, rest
, unconverted
);
1767 if (unconverted
== restlen
)
1774 restlen
-= unconverted
;
1775 mch_memmove(rest
, rest
+ unconverted
, restlen
);
1777 inbufcount
+= unconverted
;
1783 len
= 0; /* to avoid gcc warning */
1784 for (try = 0; try < 100; ++try)
1789 len
= read(read_cmd_fd
,
1791 (char *)inbuf
+ inbufcount
, (size_t)((INBUFLEN
- inbufcount
)
1793 / input_conv
.vc_factor
1797 ) /* avoid syntax highlight error */
1800 if (len
> 0 || got_int
)
1803 * If reading stdin results in an error, continue reading stderr.
1804 * This helps when using "foo | xargs vim".
1806 if (!did_read_something
&& !isatty(read_cmd_fd
) && read_cmd_fd
== 0)
1810 /* We probably set the wrong file descriptor to raw mode. Switch
1811 * back to cooked mode, use another descriptor and set the mode to
1813 settmode(TMODE_COOK
);
1815 /* Use stderr for stdin, also works for shell commands. */
1819 read_cmd_fd
= 2; /* read from stderr instead of stdin */
1827 if (len
<= 0 && !got_int
)
1830 did_read_something
= TRUE
;
1833 /* Interrupted, pretend a CTRL-C was typed. */
1841 * May perform conversion on the input characters.
1842 * Include the unconverted rest of the previous call.
1843 * If there is an incomplete char at the end it is kept for the next
1844 * time, reading more bytes should make conversion possible.
1845 * Don't do this in the unlikely event that the input buffer is too
1846 * small ("rest" still contains more bytes).
1848 if (input_conv
.vc_type
!= CONV_NONE
)
1850 inbufcount
-= unconverted
;
1851 len
= convert_input_safe(inbuf
+ inbufcount
,
1852 len
+ unconverted
, INBUFLEN
- inbufcount
,
1853 rest
== NULL
? &rest
: NULL
, &restlen
);
1859 * if a CTRL-C was typed, remove it from the buffer and set got_int
1861 if (inbuf
[inbufcount
] == 3 && ctrl_c_interrupts
)
1863 /* remove everything typed before the CTRL-C */
1864 mch_memmove(inbuf
, inbuf
+ inbufcount
, (size_t)(len
+ 1));
1871 #endif /* UNIX or OS2 or VMS*/
1873 #endif /* defined(UNIX) || defined(FEAT_GUI) || defined(OS2) || defined(VMS) */
1876 * Exit because of an input read error.
1881 if (silent_mode
) /* Normal way to exit for "ex -s" */
1883 STRCPY(IObuff
, _("Vim: Error reading input, exiting...\n"));
1887 #if defined(CURSOR_SHAPE) || defined(PROTO)
1889 * May update the shape of the cursor.
1896 gui_update_cursor_later();
1899 term_cursor_shape();
1901 # ifdef MCH_CURSOR_SHAPE
1902 mch_update_cursor();
1907 #if defined(FEAT_CLIPBOARD) || defined(FEAT_GUI) || defined(FEAT_RIGHTLEFT) \
1910 * Check bounds for column number
1918 if (col
>= (int)screen_Columns
)
1919 return (int)screen_Columns
- 1;
1924 * Check bounds for row number
1932 if (row
>= (int)screen_Rows
)
1933 return (int)screen_Rows
- 1;
1939 * Stuff for the X clipboard. Shared between VMS and Unix.
1942 #if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) || defined(PROTO)
1943 # include <X11/Xatom.h>
1944 # include <X11/Intrinsic.h>
1947 * Open the application context (if it hasn't been opened yet).
1948 * Used for Motif and Athena GUI and the xterm clipboard.
1953 if (app_context
== NULL
)
1955 XtToolkitInitialize();
1956 app_context
= XtCreateApplicationContext();
1960 static Atom vim_atom
; /* Vim's own special selection format */
1962 static Atom vimenc_atom
; /* Vim's extended selection format */
1964 static Atom compound_text_atom
;
1965 static Atom text_atom
;
1966 static Atom targets_atom
;
1969 x11_setup_atoms(dpy
)
1972 vim_atom
= XInternAtom(dpy
, VIM_ATOM_NAME
, False
);
1974 vimenc_atom
= XInternAtom(dpy
, VIMENC_ATOM_NAME
,False
);
1976 compound_text_atom
= XInternAtom(dpy
, "COMPOUND_TEXT", False
);
1977 text_atom
= XInternAtom(dpy
, "TEXT", False
);
1978 targets_atom
= XInternAtom(dpy
, "TARGETS", False
);
1979 clip_star
.sel_atom
= XA_PRIMARY
;
1980 clip_plus
.sel_atom
= XInternAtom(dpy
, "CLIPBOARD", False
);
1984 * X Selection stuff, for cutting and pasting text to other windows.
1987 static void clip_x11_request_selection_cb
__ARGS((Widget
, XtPointer
, Atom
*, Atom
*, XtPointer
, long_u
*, int *));
1991 clip_x11_request_selection_cb(w
, success
, sel_atom
, type
, value
, length
,
2004 char **text_list
= NULL
;
2007 char_u
*tmpbuf
= NULL
;
2010 if (*sel_atom
== clip_plus
.sel_atom
)
2015 if (value
== NULL
|| *length
== 0)
2017 clip_free_selection(cbd
); /* ??? [what's the query?] */
2018 *(int *)success
= FALSE
;
2021 motion_type
= MCHAR
;
2022 p
= (char_u
*)value
;
2024 if (*type
== vim_atom
)
2031 else if (*type
== vimenc_atom
)
2044 /* If the encoding of the text is different from 'encoding', attempt
2046 conv
.vc_type
= CONV_NONE
;
2047 convert_setup(&conv
, enc
, p_enc
);
2048 if (conv
.vc_type
!= CONV_NONE
)
2050 convlen
= len
; /* Need to use an int here. */
2051 tmpbuf
= string_convert(&conv
, p
, &convlen
);
2055 convert_setup(&conv
, NULL
, NULL
);
2060 else if (*type
== compound_text_atom
|| (
2064 *type
== text_atom
))
2066 XTextProperty text_prop
;
2070 text_prop
.value
= (unsigned char *)value
;
2071 text_prop
.encoding
= *type
;
2072 text_prop
.format
= *format
;
2073 text_prop
.nitems
= STRLEN(value
);
2074 status
= XmbTextPropertyToTextList(X_DISPLAY
, &text_prop
,
2075 &text_list
, &n_text
);
2076 if (status
!= Success
|| n_text
< 1)
2078 *(int *)success
= FALSE
;
2081 p
= (char_u
*)text_list
[0];
2084 clip_yank_selection(motion_type
, p
, (long)len
, cbd
);
2086 if (text_list
!= NULL
)
2087 XFreeStringList(text_list
);
2091 XtFree((char *)value
);
2092 *(int *)success
= TRUE
;
2096 clip_x11_request_selection(myShell
, dpy
, cbd
)
2119 case 0: type
= vimenc_atom
; break;
2121 case 1: type
= vim_atom
; break;
2122 case 2: type
= compound_text_atom
; break;
2123 case 3: type
= text_atom
; break;
2124 default: type
= XA_STRING
;
2126 XtGetSelectionValue(myShell
, cbd
->sel_atom
, type
,
2127 clip_x11_request_selection_cb
, (XtPointer
)&success
, CurrentTime
);
2129 /* Make sure the request for the selection goes out before waiting for
2134 * Wait for result of selection request, otherwise if we type more
2135 * characters, then they will appear before the one that requested the
2136 * paste! Don't worry, we will catch up with any other events later.
2140 if (XCheckTypedEvent(dpy
, SelectionNotify
, &event
))
2142 if (XCheckTypedEvent(dpy
, SelectionRequest
, &event
))
2143 /* We may get a SelectionRequest here and if we don't handle
2144 * it we hang. KDE klipper does this, for example. */
2145 XtDispatchEvent(&event
);
2147 /* Do we need this? Probably not. */
2150 /* Bernhard Walle solved a slow paste response in an X terminal by
2151 * adding: usleep(10000); here. */
2154 /* this is where clip_x11_request_selection_cb() is actually called */
2155 XtDispatchEvent(&event
);
2161 /* Final fallback position - use the X CUT_BUFFER0 store */
2162 buffer
= (char_u
*)XFetchBuffer(dpy
, &nbytes
, 0);
2166 clip_yank_selection(MCHAR
, buffer
, (long)nbytes
, cbd
);
2167 XFree((void *)buffer
);
2169 verb_msg((char_u
*)_("Used CUT_BUFFER0 instead of empty selection"));
2173 static Boolean clip_x11_convert_selection_cb
__ARGS((Widget
, Atom
*, Atom
*, Atom
*, XtPointer
*, long_u
*, int *));
2177 clip_x11_convert_selection_cb(w
, sel_atom
, target
, type
, value
, length
, format
)
2192 if (*sel_atom
== clip_plus
.sel_atom
)
2198 return False
; /* Shouldn't ever happen */
2200 /* requestor wants to know what target types we support */
2201 if (*target
== targets_atom
)
2205 if ((array
= (Atom
*)XtMalloc((unsigned)(sizeof(Atom
) * 6))) == NULL
)
2207 *value
= (XtPointer
)array
;
2209 array
[i
++] = XA_STRING
;
2210 array
[i
++] = targets_atom
;
2212 array
[i
++] = vimenc_atom
;
2214 array
[i
++] = vim_atom
;
2215 array
[i
++] = text_atom
;
2216 array
[i
++] = compound_text_atom
;
2218 /* This used to be: *format = sizeof(Atom) * 8; but that caused
2219 * crashes on 64 bit machines. (Peter Derr) */
2225 if ( *target
!= XA_STRING
2227 && *target
!= vimenc_atom
2229 && *target
!= vim_atom
2230 && *target
!= text_atom
2231 && *target
!= compound_text_atom
)
2234 clip_get_selection(cbd
);
2235 motion_type
= clip_convert_selection(&string
, length
, cbd
);
2236 if (motion_type
< 0)
2239 /* For our own format, the first byte contains the motion type */
2240 if (*target
== vim_atom
)
2244 /* Our own format with encoding: motion 'encoding' NUL text */
2245 if (*target
== vimenc_atom
)
2246 *length
+= STRLEN(p_enc
) + 2;
2249 *value
= XtMalloc((Cardinal
)*length
);
2250 result
= (char_u
*)*value
;
2257 if (*target
== XA_STRING
)
2259 mch_memmove(result
, string
, (size_t)(*length
));
2262 else if (*target
== compound_text_atom
2263 || *target
== text_atom
)
2265 XTextProperty text_prop
;
2266 char *string_nt
= (char *)alloc((unsigned)*length
+ 1);
2268 /* create NUL terminated string which XmbTextListToTextProperty wants */
2269 mch_memmove(string_nt
, string
, (size_t)*length
);
2270 string_nt
[*length
] = NUL
;
2271 XmbTextListToTextProperty(X_DISPLAY
, (char **)&string_nt
, 1,
2272 XCompoundTextStyle
, &text_prop
);
2273 vim_free(string_nt
);
2274 XtFree(*value
); /* replace with COMPOUND text */
2275 *value
= (XtPointer
)(text_prop
.value
); /* from plain text */
2276 *length
= text_prop
.nitems
;
2277 *type
= compound_text_atom
;
2281 else if (*target
== vimenc_atom
)
2283 int l
= STRLEN(p_enc
);
2285 result
[0] = motion_type
;
2286 STRCPY(result
+ 1, p_enc
);
2287 mch_memmove(result
+ l
+ 2, string
, (size_t)(*length
- l
- 2));
2288 *type
= vimenc_atom
;
2294 result
[0] = motion_type
;
2295 mch_memmove(result
+ 1, string
, (size_t)(*length
- 1));
2298 *format
= 8; /* 8 bits per char */
2303 static void clip_x11_lose_ownership_cb
__ARGS((Widget
, Atom
*));
2307 clip_x11_lose_ownership_cb(w
, sel_atom
)
2311 if (*sel_atom
== clip_plus
.sel_atom
)
2312 clip_lose_selection(&clip_plus
);
2314 clip_lose_selection(&clip_star
);
2318 clip_x11_lose_selection(myShell
, cbd
)
2322 XtDisownSelection(myShell
, cbd
->sel_atom
, CurrentTime
);
2326 clip_x11_own_selection(myShell
, cbd
)
2330 if (XtOwnSelection(myShell
, cbd
->sel_atom
, CurrentTime
,
2331 clip_x11_convert_selection_cb
, clip_x11_lose_ownership_cb
,
2338 * Send the current selection to the clipboard. Do nothing for X because we
2339 * will fill in the selection only when requested by another app.
2343 clip_x11_set_selection(cbd
)
2349 #if defined(FEAT_MOUSE) || defined(PROTO)
2352 * Move the cursor to the specified row and column on the screen.
2353 * Change current window if neccesary. Returns an integer with the
2354 * CURSOR_MOVED bit set if the cursor has moved or unset otherwise.
2356 * The MOUSE_FOLD_CLOSE bit is set when clicked on the '-' in a fold column.
2357 * The MOUSE_FOLD_OPEN bit is set when clicked on the '+' in a fold column.
2359 * If flags has MOUSE_FOCUS, then the current window will not be changed, and
2360 * if the mouse is outside the window then the text will scroll, or if the
2361 * mouse was previously on a status line, then the status line may be dragged.
2363 * If flags has MOUSE_MAY_VIS, then VIsual mode will be started before the
2364 * cursor is moved unless the cursor was on a status line.
2365 * This function returns one of IN_UNKNOWN, IN_BUFFER, IN_STATUS_LINE or
2366 * IN_SEP_LINE depending on where the cursor was clicked.
2368 * If flags has MOUSE_MAY_STOP_VIS, then Visual mode will be stopped, unless
2369 * the mouse is on the status line of the same window.
2371 * If flags has MOUSE_DID_MOVE, nothing is done if the mouse didn't move since
2374 * If flags has MOUSE_SETPOS, nothing is done, only the current position is
2378 jump_to_mouse(flags
, inclusive
, which_button
)
2380 int *inclusive
; /* used for inclusive operator, can be NULL */
2381 int which_button
; /* MOUSE_LEFT, MOUSE_RIGHT, MOUSE_MIDDLE */
2383 static int on_status_line
= 0; /* #lines below bottom of window */
2384 #ifdef FEAT_VERTSPLIT
2385 static int on_sep_line
= 0; /* on separator right of window */
2387 static int prev_row
= -1;
2388 static int prev_col
= -1;
2389 static win_T
*dragwin
= NULL
; /* window being dragged */
2390 static int did_drag
= FALSE
; /* drag was noticed */
2392 win_T
*wp
, *old_curwin
;
2396 int row
= mouse_row
;
2397 int col
= mouse_col
;
2402 mouse_past_bottom
= FALSE
;
2403 mouse_past_eol
= FALSE
;
2405 if (flags
& MOUSE_RELEASED
)
2407 /* On button release we may change window focus if positioned on a
2408 * status line and no dragging happened. */
2409 if (dragwin
!= NULL
&& !did_drag
)
2410 flags
&= ~(MOUSE_FOCUS
| MOUSE_DID_MOVE
);
2415 if ((flags
& MOUSE_DID_MOVE
)
2416 && prev_row
== mouse_row
2417 && prev_col
== mouse_col
)
2420 /* before moving the cursor for a left click wich is NOT in a status
2421 * line, stop Visual mode */
2423 return IN_STATUS_LINE
;
2424 #ifdef FEAT_VERTSPLIT
2429 if (flags
& MOUSE_MAY_STOP_VIS
)
2432 redraw_curbuf_later(INVERTED
); /* delete the inversion */
2435 #if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2436 /* Continue a modeless selection in another window. */
2437 if (cmdwin_type
!= 0 && row
< W_WINROW(curwin
))
2438 return IN_OTHER_WIN
;
2443 prev_row
= mouse_row
;
2444 prev_col
= mouse_col
;
2446 if (flags
& MOUSE_SETPOS
)
2447 goto retnomove
; /* ugly goto... */
2450 /* Remember the character under the mouse, it might be a '-' or '+' in the
2452 if (row
>= 0 && row
< Rows
&& col
>= 0 && col
<= Columns
2453 && ScreenLines
!= NULL
)
2454 mouse_char
= ScreenLines
[LineOffset
[row
] + col
];
2459 old_curwin
= curwin
;
2460 old_cursor
= curwin
->w_cursor
;
2462 if (!(flags
& MOUSE_FOCUS
))
2464 if (row
< 0 || col
< 0) /* check if it makes sense */
2468 /* find the window where the row is in */
2469 wp
= mouse_find_win(&row
, &col
);
2475 * winpos and height may change in win_enter()!
2477 if (row
>= wp
->w_height
) /* In (or below) status line */
2479 on_status_line
= row
- wp
->w_height
+ 1;
2484 #ifdef FEAT_VERTSPLIT
2485 if (col
>= wp
->w_width
) /* In separator line */
2487 on_sep_line
= col
- wp
->w_width
+ 1;
2493 /* The rightmost character of the status line might be a vertical
2494 * separator character if there is no connecting window to the right. */
2495 if (on_status_line
&& on_sep_line
)
2497 if (stl_connected(wp
))
2505 /* Before jumping to another buffer, or moving the cursor for a left
2506 * click, stop Visual mode. */
2508 && (wp
->w_buffer
!= curwin
->w_buffer
2510 # ifdef FEAT_VERTSPLIT
2513 # ifdef FEAT_FOLDING
2515 # ifdef FEAT_RIGHTLEFT
2516 wp
->w_p_rl
? col
< W_WIDTH(wp
) - wp
->w_p_fdc
:
2520 + (cmdwin_type
== 0 && wp
== curwin
? 0 : 1)
2524 && (flags
& MOUSE_MAY_STOP_VIS
))))
2527 redraw_curbuf_later(INVERTED
); /* delete the inversion */
2531 if (cmdwin_type
!= 0 && wp
!= curwin
)
2533 /* A click outside the command-line window: Use modeless
2534 * selection if possible. Allow dragging the status line of
2535 * windows just above the command-line window. */
2536 if (wp
->w_winrow
+ wp
->w_height
2537 != curwin
->w_prev
->w_winrow
+ curwin
->w_prev
->w_height
)
2542 # ifdef FEAT_VERTSPLIT
2545 # ifdef FEAT_CLIPBOARD
2547 return IN_STATUS_LINE
;
2548 return IN_OTHER_WIN
;
2551 col
+= wp
->w_wincol
;
2557 /* Only change window focus when not clicking on or dragging the
2558 * status line. Do change focus when releasing the mouse button
2559 * (MOUSE_FOCUS was set above if we dragged first). */
2560 if (dragwin
== NULL
|| (flags
& MOUSE_RELEASED
))
2561 win_enter(wp
, TRUE
); /* can make wp invalid! */
2562 # ifdef CHECK_DOUBLE_CLICK
2563 /* set topline, to be able to check for double click ourselves */
2564 if (curwin
!= old_curwin
)
2565 set_mouse_topline(curwin
);
2568 if (on_status_line
) /* In (or below) status line */
2570 /* Don't use start_arrow() if we're in the same window */
2571 if (curwin
== old_curwin
)
2572 return IN_STATUS_LINE
;
2574 return IN_STATUS_LINE
| CURSOR_MOVED
;
2576 #ifdef FEAT_VERTSPLIT
2577 if (on_sep_line
) /* In (or below) status line */
2579 /* Don't use start_arrow() if we're in the same window */
2580 if (curwin
== old_curwin
)
2583 return IN_SEP_LINE
| CURSOR_MOVED
;
2587 curwin
->w_cursor
.lnum
= curwin
->w_topline
;
2589 /* remember topline, needed for double click */
2590 gui_prev_topline
= curwin
->w_topline
;
2592 gui_prev_topfill
= curwin
->w_topfill
;
2596 else if (on_status_line
&& which_button
== MOUSE_LEFT
)
2599 if (dragwin
!= NULL
)
2601 /* Drag the status line */
2602 count
= row
- dragwin
->w_winrow
- dragwin
->w_height
+ 1
2604 win_drag_status_line(dragwin
, count
);
2608 return IN_STATUS_LINE
; /* Cursor didn't move */
2610 #ifdef FEAT_VERTSPLIT
2611 else if (on_sep_line
&& which_button
== MOUSE_LEFT
)
2613 if (dragwin
!= NULL
)
2615 /* Drag the separator column */
2616 count
= col
- dragwin
->w_wincol
- dragwin
->w_width
+ 1
2618 win_drag_vsep_line(dragwin
, count
);
2621 return IN_SEP_LINE
; /* Cursor didn't move */
2624 else /* keep_window_focus must be TRUE */
2627 /* before moving the cursor for a left click, stop Visual mode */
2628 if (flags
& MOUSE_MAY_STOP_VIS
)
2631 redraw_curbuf_later(INVERTED
); /* delete the inversion */
2635 #if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
2636 /* Continue a modeless selection in another window. */
2637 if (cmdwin_type
!= 0 && row
< W_WINROW(curwin
))
2638 return IN_OTHER_WIN
;
2641 row
-= W_WINROW(curwin
);
2642 #ifdef FEAT_VERTSPLIT
2643 col
-= W_WINCOL(curwin
);
2647 * When clicking beyond the end of the window, scroll the screen.
2648 * Scroll by however many rows outside the window we are.
2653 for (first
= TRUE
; curwin
->w_topline
> 1; )
2656 if (curwin
->w_topfill
< diff_check(curwin
, curwin
->w_topline
))
2660 count
+= plines(curwin
->w_topline
- 1);
2661 if (!first
&& count
> -row
)
2665 hasFolding(curwin
->w_topline
, &curwin
->w_topline
, NULL
);
2668 if (curwin
->w_topfill
< diff_check(curwin
, curwin
->w_topline
))
2669 ++curwin
->w_topfill
;
2673 --curwin
->w_topline
;
2675 curwin
->w_topfill
= 0;
2680 check_topfill(curwin
, FALSE
);
2683 ~(VALID_WROW
|VALID_CROW
|VALID_BOTLINE
|VALID_BOTLINE_AP
);
2684 redraw_later(VALID
);
2687 else if (row
>= curwin
->w_height
)
2690 for (first
= TRUE
; curwin
->w_topline
< curbuf
->b_ml
.ml_line_count
; )
2693 if (curwin
->w_topfill
> 0)
2697 count
+= plines(curwin
->w_topline
);
2698 if (!first
&& count
> row
- curwin
->w_height
+ 1)
2702 if (hasFolding(curwin
->w_topline
, NULL
, &curwin
->w_topline
)
2703 && curwin
->w_topline
== curbuf
->b_ml
.ml_line_count
)
2707 if (curwin
->w_topfill
> 0)
2708 --curwin
->w_topfill
;
2712 ++curwin
->w_topline
;
2715 diff_check_fill(curwin
, curwin
->w_topline
);
2720 check_topfill(curwin
, FALSE
);
2722 redraw_later(VALID
);
2724 ~(VALID_WROW
|VALID_CROW
|VALID_BOTLINE
|VALID_BOTLINE_AP
);
2725 row
= curwin
->w_height
- 1;
2729 /* When dragging the mouse, while the text has been scrolled up as
2730 * far as it goes, moving the mouse in the top line should scroll
2731 * the text down (done later when recomputing w_topline). */
2733 && curwin
->w_cursor
.lnum
2734 == curwin
->w_buffer
->b_ml
.ml_line_count
2735 && curwin
->w_cursor
.lnum
== curwin
->w_topline
)
2736 curwin
->w_valid
&= ~(VALID_TOPLINE
);
2741 /* Check for position outside of the fold column. */
2743 # ifdef FEAT_RIGHTLEFT
2744 curwin
->w_p_rl
? col
< W_WIDTH(curwin
) - curwin
->w_p_fdc
:
2746 col
>= curwin
->w_p_fdc
2748 + (cmdwin_type
== 0 ? 0 : 1)
2754 /* compute the position in the buffer line from the posn on the screen */
2755 if (mouse_comp_pos(curwin
, &row
, &col
, &curwin
->w_cursor
.lnum
))
2756 mouse_past_bottom
= TRUE
;
2759 /* Start Visual mode before coladvance(), for when 'sel' != "old" */
2760 if ((flags
& MOUSE_MAY_VIS
) && !VIsual_active
)
2762 check_visual_highlight();
2763 VIsual
= old_cursor
;
2764 VIsual_active
= TRUE
;
2765 VIsual_reselect
= TRUE
;
2766 /* if 'selectmode' contains "mouse", start Select mode */
2767 may_start_select('o');
2769 if (p_smd
&& msg_silent
== 0)
2770 redraw_cmdline
= TRUE
; /* show visual mode later */
2774 curwin
->w_curswant
= col
;
2775 curwin
->w_set_curswant
= FALSE
; /* May still have been TRUE */
2776 if (coladvance(col
) == FAIL
) /* Mouse click beyond end of line */
2778 if (inclusive
!= NULL
)
2780 mouse_past_eol
= TRUE
;
2782 else if (inclusive
!= NULL
)
2786 if (curwin
!= old_curwin
|| curwin
->w_cursor
.lnum
!= old_cursor
.lnum
2787 || curwin
->w_cursor
.col
!= old_cursor
.col
)
2788 count
|= CURSOR_MOVED
; /* Cursor has moved */
2791 if (mouse_char
== '+')
2792 count
|= MOUSE_FOLD_OPEN
;
2793 else if (mouse_char
!= ' ')
2794 count
|= MOUSE_FOLD_CLOSE
;
2801 * Compute the position in the buffer line from the posn on the screen in
2803 * Returns TRUE if the position is below the last line.
2806 mouse_comp_pos(win
, rowp
, colp
, lnump
)
2819 #ifdef FEAT_RIGHTLEFT
2821 col
= W_WIDTH(win
) - 1 - col
;
2824 lnum
= win
->w_topline
;
2829 /* Don't include filler lines in "count" */
2831 # ifdef FEAT_FOLDING
2832 && !hasFoldingWin(win
, lnum
, NULL
, NULL
, TRUE
, NULL
)
2836 if (lnum
== win
->w_topline
)
2837 row
-= win
->w_topfill
;
2839 row
-= diff_check_fill(win
, lnum
);
2840 count
= plines_win_nofill(win
, lnum
, TRUE
);
2844 count
= plines_win(win
, lnum
, TRUE
);
2846 break; /* Position is in this buffer line. */
2848 (void)hasFoldingWin(win
, lnum
, NULL
, &lnum
, TRUE
, NULL
);
2850 if (lnum
== win
->w_buffer
->b_ml
.ml_line_count
)
2853 break; /* past end of file */
2861 /* Compute the column without wrapping. */
2862 off
= win_col_off(win
) - win_col_off2(win
);
2865 col
+= row
* (W_WIDTH(win
) - off
);
2866 /* add skip column (for long wrapping line) */
2867 col
+= win
->w_skipcol
;
2871 col
+= win
->w_leftcol
;
2873 /* skip line number and fold column in front of the line */
2874 col
-= win_col_off(win
);
2877 #ifdef FEAT_NETBEANS_INTG
2879 netbeans_gutter_click(lnum
);
2890 #if defined(FEAT_WINDOWS) || defined(PROTO)
2892 * Find the window at screen position "*rowp" and "*colp". The positions are
2893 * updated to become relative to the top-left of the window.
2897 mouse_find_win(rowp
, colp
)
2904 *rowp
-= firstwin
->w_winrow
;
2907 if (fp
->fr_layout
== FR_LEAF
)
2909 #ifdef FEAT_VERTSPLIT
2910 if (fp
->fr_layout
== FR_ROW
)
2912 for (fp
= fp
->fr_child
; fp
->fr_next
!= NULL
; fp
= fp
->fr_next
)
2914 if (*colp
< fp
->fr_width
)
2916 *colp
-= fp
->fr_width
;
2920 else /* fr_layout == FR_COL */
2922 for (fp
= fp
->fr_child
; fp
->fr_next
!= NULL
; fp
= fp
->fr_next
)
2924 if (*rowp
< fp
->fr_height
)
2926 *rowp
-= fp
->fr_height
;
2934 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined (FEAT_GUI_MAC) \
2935 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2936 || defined(FEAT_GUI_PHOTON) || defined(PROTO)
2938 * Translate window coordinates to buffer position without any side effects
2941 get_fpos_of_mouse(mpos
)
2945 int row
= mouse_row
;
2946 int col
= mouse_col
;
2948 if (row
< 0 || col
< 0) /* check if it makes sense */
2952 /* find the window where the row is in */
2953 wp
= mouse_find_win(&row
, &col
);
2958 * winpos and height may change in win_enter()!
2960 if (row
>= wp
->w_height
) /* In (or below) status line */
2961 return IN_STATUS_LINE
;
2962 #ifdef FEAT_VERTSPLIT
2963 if (col
>= wp
->w_width
) /* In vertical separator line */
2970 /* compute the position in the buffer line from the posn on the screen */
2971 if (mouse_comp_pos(curwin
, &row
, &col
, &mpos
->lnum
))
2972 return IN_STATUS_LINE
; /* past bottom */
2974 mpos
->col
= vcol2col(wp
, mpos
->lnum
, col
);
2982 * Convert a virtual (screen) column to a character column.
2983 * The first column is one.
2986 vcol2col(wp
, lnum
, vcol
)
2991 /* try to advance to the specified column */
2996 ptr
= ml_get_buf(wp
->w_buffer
, lnum
, FALSE
);
2997 while (count
<= vcol
&& *ptr
!= NUL
)
3000 count
+= win_lbr_chartabsize(wp
, ptr
, count
, NULL
);
3007 #endif /* FEAT_MOUSE */
3009 #if defined(FEAT_GUI) || defined(WIN3264) || defined(PROTO)
3011 * Called when focus changed. Used for the GUI or for systems where this can
3012 * be done in the console (Win32).
3015 ui_focus_change(in_focus
)
3016 int in_focus
; /* TRUE if focus gained. */
3018 static time_t last_time
= (time_t)0;
3019 int need_redraw
= FALSE
;
3021 /* When activated: Check if any file was modified outside of Vim.
3022 * Only do this when not done within the last two seconds (could get
3023 * several events in a row). */
3024 if (in_focus
&& last_time
+ 2 < time(NULL
))
3026 need_redraw
= check_timestamps(
3033 last_time
= time(NULL
);
3038 * Fire the focus gained/lost autocommand.
3040 need_redraw
|= apply_autocmds(in_focus
? EVENT_FOCUSGAINED
3041 : EVENT_FOCUSLOST
, NULL
, NULL
, FALSE
, curbuf
);
3046 /* Something was executed, make sure the cursor is put back where it
3048 need_wait_return
= FALSE
;
3050 if (State
& CMDLINE
)
3052 else if (State
== HITRETURN
|| State
== SETWSIZE
|| State
== ASKMORE
3053 || State
== EXTERNCMD
|| State
== CONFIRM
|| exmode_active
)
3055 else if ((State
& NORMAL
) || (State
& INSERT
))
3057 if (must_redraw
!= 0)
3061 cursor_on(); /* redrawing may have switched it off */
3066 gui_update_cursor(FALSE
, TRUE
);
3067 gui_update_scrollbars(FALSE
);
3072 /* File may have been changed from 'readonly' to 'noreadonly' */
3079 #if defined(USE_IM_CONTROL) || defined(PROTO)
3081 * Save current Input Method status to specified place.
3084 im_save_status(psave
)
3087 /* Don't save when 'imdisable' is set or "xic" is NULL, IM is always
3088 * disabled then (but might start later).
3089 * Also don't save when inside a mapping, vgetc_im_active has not been set
3091 * And don't save when the keys were stuffed (e.g., for a "." command).
3092 * And don't save when the GUI is running but our window doesn't have
3093 * input focus (e.g., when a find dialog is open). */
3094 if (!p_imdisable
&& KeyTyped
&& !KeyStuffed
3099 && (!gui
.in_use
|| gui
.in_focus
)
3103 /* Do save when IM is on, or IM is off and saved status is on. */
3104 if (vgetc_im_active
)
3105 *psave
= B_IMODE_IM
;
3106 else if (*psave
== B_IMODE_IM
)
3107 *psave
= B_IMODE_NONE
;