#include "vim.h"
#ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# include "winclip.pro"
#endif
void
ui_write(char_u *s, int len)
{
#ifdef FEAT_GUI
if (gui.in_use && !gui.dying && !gui.starting)
{
gui_write(s, len);
if (p_wd)
gui_wait_for_chars(p_wd);
return;
}
#endif
#ifndef NO_CONSOLE
if (!(silent_mode && p_verbose == 0))
{
#if defined(FEAT_MBYTE) && !defined(WIN3264)
char_u *tofree = NULL;
if (output_conv.vc_type != CONV_NONE)
{
tofree = string_convert(&output_conv, s, &len);
if (tofree != NULL)
s = tofree;
}
#endif
mch_write(s, len);
#if defined(FEAT_MBYTE) && !defined(WIN3264)
if (output_conv.vc_type != CONV_NONE)
vim_free(tofree);
#endif
}
#endif
}
#if defined(UNIX) || defined(VMS) || defined(PROTO) || defined(WIN3264)
static char_u *ta_str = NULL;
static int ta_off;
static int ta_len;
void
ui_inchar_undo(char_u *s, int len)
{
char_u *new;
int newlen;
newlen = len;
if (ta_str != NULL)
newlen += ta_len - ta_off;
new = alloc(newlen);
if (new != NULL)
{
if (ta_str != NULL)
{
mch_memmove(new, ta_str + ta_off, (size_t)(ta_len - ta_off));
mch_memmove(new + ta_len - ta_off, s, (size_t)len);
vim_free(ta_str);
}
else
mch_memmove(new, s, (size_t)len);
ta_str = new;
ta_len = newlen;
ta_off = 0;
}
}
#endif
int
ui_inchar(
char_u *buf,
int maxlen,
long wtime,
int tb_change_cnt)
{
int retval = 0;
#if defined(FEAT_GUI) && (defined(UNIX) || defined(VMS))
if (ta_str != NULL)
{
if (maxlen >= ta_len - ta_off)
{
mch_memmove(buf, ta_str + ta_off, (size_t)ta_len);
vim_free(ta_str);
ta_str = NULL;
return ta_len;
}
mch_memmove(buf, ta_str + ta_off, (size_t)maxlen);
ta_off += maxlen;
return maxlen;
}
#endif
#ifdef FEAT_PROFILE
if (do_profiling == PROF_YES && wtime != 0)
prof_inchar_enter();
#endif
#ifdef NO_CONSOLE_INPUT
if (no_console_input())
{
static int count = 0;
# ifndef NO_CONSOLE
retval = mch_inchar(buf, maxlen, (wtime >= 0 && wtime < 10)
? 10L : wtime, tb_change_cnt);
if (retval > 0 || typebuf_changed(tb_change_cnt) || wtime >= 0)
goto theend;
# endif
if (wtime == -1 && ++count == 1000)
read_error_exit();
buf[0] = CAR;
retval = 1;
goto theend;
}
#endif
if (wtime == -1 || wtime > 100L)
{
(void)vim_handle_signal(SIGNAL_UNBLOCK);
if ((mapped_ctrl_c | curbuf->b_mapped_ctrl_c) & get_real_state())
ctrl_c_interrupts = FALSE;
}
#ifdef FEAT_GUI
if (gui.in_use)
{
if (gui_wait_for_chars(wtime) && !typebuf_changed(tb_change_cnt))
retval = read_from_input_buf(buf, (long)maxlen);
}
#endif
#ifndef NO_CONSOLE
# ifdef FEAT_GUI
else
# endif
{
retval = mch_inchar(buf, maxlen, wtime, tb_change_cnt);
}
#endif
if (wtime == -1 || wtime > 100L)
(void)vim_handle_signal(SIGNAL_BLOCK);
ctrl_c_interrupts = TRUE;
#ifdef NO_CONSOLE_INPUT
theend:
#endif
#ifdef FEAT_PROFILE
if (do_profiling == PROF_YES && wtime != 0)
prof_inchar_exit();
#endif
return retval;
}
int
ui_char_avail(void)
{
#ifdef FEAT_GUI
if (gui.in_use)
{
gui_mch_update();
return input_available();
}
#endif
#ifndef NO_CONSOLE
# ifdef NO_CONSOLE_INPUT
if (no_console_input())
return 0;
# endif
return mch_char_avail();
#else
return 0;
#endif
}
void
ui_delay(long msec, int ignoreinput)
{
#ifdef FEAT_GUI
if (gui.in_use && !ignoreinput)
gui_wait_for_chars(msec);
else
#endif
mch_delay(msec, ignoreinput);
}
void
ui_suspend(void)
{
#ifdef FEAT_GUI
if (gui.in_use)
{
gui_mch_iconify();
return;
}
#endif
mch_suspend();
}
#if !defined(UNIX) || !defined(SIGTSTP) || defined(PROTO) || defined(__BEOS__)
void
suspend_shell(void)
{
if (*p_sh == NUL)
EMSG(_(e_shellempty));
else
{
MSG_PUTS(_("new shell started\n"));
do_shell(NULL, 0);
}
}
#endif
int
ui_get_shellsize(void)
{
int retval;
#ifdef FEAT_GUI
if (gui.in_use)
retval = gui_get_shellsize();
else
#endif
retval = mch_get_shellsize();
check_shellsize();
if (retval == OK)
{
set_number_default("lines", Rows);
set_number_default("columns", Columns);
}
return retval;
}
void
ui_set_shellsize(
int mustset UNUSED)
{
#ifdef FEAT_GUI
if (gui.in_use)
gui_set_shellsize(mustset, TRUE, RESIZE_BOTH);
else
#endif
mch_set_shellsize();
}
void
ui_new_shellsize(void)
{
if (full_screen && !exiting)
{
#ifdef FEAT_GUI
if (gui.in_use)
gui_new_shellsize();
else
#endif
mch_new_shellsize();
}
}
void
ui_breakcheck(void)
{
ui_breakcheck_force(FALSE);
}
void
ui_breakcheck_force(int force)
{
int save_us = updating_screen;
++updating_screen;
#ifdef FEAT_GUI
if (gui.in_use)
gui_mch_update();
else
#endif
mch_breakcheck(force);
updating_screen = save_us;
}
#if defined(FEAT_CLIPBOARD) || defined(PROTO)
void
clip_init(int can_use)
{
VimClipboard *cb;
cb = &clip_star;
for (;;)
{
cb->available = can_use;
cb->owned = FALSE;
cb->start.lnum = 0;
cb->start.col = 0;
cb->end.lnum = 0;
cb->end.col = 0;
cb->state = SELECT_CLEARED;
if (cb == &clip_plus)
break;
cb = &clip_plus;
}
}
void
clip_update_selection(VimClipboard *clip)
{
pos_T start, end;
if (!redo_VIsual_busy && VIsual_active && (State & NORMAL))
{
if (LT_POS(VIsual, curwin->w_cursor))
{
start = VIsual;
end = curwin->w_cursor;
#ifdef FEAT_MBYTE
if (has_mbyte)
end.col += (*mb_ptr2len)(ml_get_cursor()) - 1;
#endif
}
else
{
start = curwin->w_cursor;
end = VIsual;
}
if (!EQUAL_POS(clip->start, start)
|| !EQUAL_POS(clip->end, end)
|| clip->vmode != VIsual_mode)
{
clip_clear_selection(clip);
clip->start = start;
clip->end = end;
clip->vmode = VIsual_mode;
clip_free_selection(clip);
clip_own_selection(clip);
clip_gen_set_selection(clip);
}
}
}
void
clip_own_selection(VimClipboard *cbd)
{
#ifdef FEAT_X11
if (cbd->available)
{
int was_owned = cbd->owned;
cbd->owned = (clip_gen_own_selection(cbd) == OK);
if (!was_owned && (cbd == &clip_star || cbd == &clip_plus))
{
if (cbd->owned
&& (get_real_state() == VISUAL
|| get_real_state() == SELECTMODE)
&& (cbd == &clip_star ? clip_isautosel_star()
: clip_isautosel_plus())
&& HL_ATTR(HLF_V) != HL_ATTR(HLF_VNC))
redraw_curbuf_later(INVERTED_ALL);
}
}
#else
if (!cbd->owned && cbd->available)
cbd->owned = (clip_gen_own_selection(cbd) == OK);
#endif
}
void
clip_lose_selection(VimClipboard *cbd)
{
#ifdef FEAT_X11
int was_owned = cbd->owned;
#endif
int visual_selection = FALSE;
if (cbd == &clip_star || cbd == &clip_plus)
visual_selection = TRUE;
clip_free_selection(cbd);
cbd->owned = FALSE;
if (visual_selection)
clip_clear_selection(cbd);
clip_gen_lose_selection(cbd);
#ifdef FEAT_X11
if (visual_selection)
{
if (was_owned
&& (get_real_state() == VISUAL
|| get_real_state() == SELECTMODE)
&& (cbd == &clip_star ?
clip_isautosel_star() : clip_isautosel_plus())
&& HL_ATTR(HLF_V) != HL_ATTR(HLF_VNC))
{
update_curbuf(INVERTED_ALL);
setcursor();
cursor_on();
out_flush();
# ifdef FEAT_GUI
if (gui.in_use)
gui_update_cursor(TRUE, FALSE);
# endif
}
}
#endif
}
static void
clip_copy_selection(VimClipboard *clip)
{
if (VIsual_active && (State & NORMAL) && clip->available)
{
clip_update_selection(clip);
clip_free_selection(clip);
clip_own_selection(clip);
if (clip->owned)
clip_get_selection(clip);
clip_gen_set_selection(clip);
}
}
static int global_change_count = 0;
static int clipboard_needs_update = FALSE;
static int clip_did_set_selection = TRUE;
void
start_global_changes(void)
{
if (++global_change_count > 1)
return;
clip_unnamed_saved = clip_unnamed;
clipboard_needs_update = FALSE;
if (clip_did_set_selection)
{
clip_unnamed = FALSE;
clip_did_set_selection = FALSE;
}
}
int
is_clipboard_needs_update()
{
return clipboard_needs_update;
}
void
end_global_changes(void)
{
if (--global_change_count > 0)
return;
if (!clip_did_set_selection)
{
clip_did_set_selection = TRUE;
clip_unnamed = clip_unnamed_saved;
clip_unnamed_saved = FALSE;
if (clipboard_needs_update)
{
if (clip_unnamed & CLIP_UNNAMED)
{
clip_own_selection(&clip_star);
clip_gen_set_selection(&clip_star);
}
if (clip_unnamed & CLIP_UNNAMED_PLUS)
{
clip_own_selection(&clip_plus);
clip_gen_set_selection(&clip_plus);
}
}
}
clipboard_needs_update = FALSE;
}
void
clip_auto_select(void)
{
if (clip_isautosel_star())
clip_copy_selection(&clip_star);
if (clip_isautosel_plus())
clip_copy_selection(&clip_plus);
}
int
clip_isautosel_star(void)
{
return (
#ifdef FEAT_GUI
gui.in_use ? (vim_strchr(p_go, GO_ASEL) != NULL) :
#endif
clip_autoselect_star);
}
int
clip_isautosel_plus(void)
{
return (
#ifdef FEAT_GUI
gui.in_use ? (vim_strchr(p_go, GO_ASELPLUS) != NULL) :
#endif
clip_autoselect_plus);
}
static int clip_compare_pos(int row1, int col1, int row2, int col2);
static void clip_invert_area(int, int, int, int, int how);
static void clip_invert_rectangle(int row, int col, int height, int width, int invert);
static void clip_get_word_boundaries(VimClipboard *, int, int);
static int clip_get_line_end(int);
static void clip_update_modeless_selection(VimClipboard *, int, int,
int, int);
#define CLIP_CLEAR 1
#define CLIP_SET 2
#define CLIP_TOGGLE 3
void
clip_modeless(int button, int is_click, int is_drag)
{
int repeat;
repeat = ((clip_star.mode == SELECT_MODE_CHAR
|| clip_star.mode == SELECT_MODE_LINE)
&& (mod_mask & MOD_MASK_2CLICK))
|| (clip_star.mode == SELECT_MODE_WORD
&& (mod_mask & MOD_MASK_3CLICK));
if (is_click && button == MOUSE_RIGHT)
{
if (clip_star.state == SELECT_CLEARED)
clip_start_selection(mouse_col, mouse_row, FALSE);
clip_process_selection(button, mouse_col, mouse_row, repeat);
}
else if (is_click)
clip_start_selection(mouse_col, mouse_row, repeat);
else if (is_drag)
{
if (clip_star.state != SELECT_CLEARED)
clip_process_selection(button, mouse_col, mouse_row, repeat);
}
else
clip_process_selection(MOUSE_RELEASE, mouse_col, mouse_row, FALSE);
}
static int
clip_compare_pos(
int row1,
int col1,
int row2,
int col2)
{
if (row1 > row2) return(1);
if (row1 < row2) return(-1);
if (col1 > col2) return(1);
if (col1 < col2) return(-1);
return(0);
}
void
clip_start_selection(int col, int row, int repeated_click)
{
VimClipboard *cb = &clip_star;
if (cb->state == SELECT_DONE)
clip_clear_selection(cb);
row = check_row(row);
col = check_col(col);
#ifdef FEAT_MBYTE
col = mb_fix_col(col, row);
#endif
cb->start.lnum = row;
cb->start.col = col;
cb->end = cb->start;
cb->origin_row = (short_u)cb->start.lnum;
cb->state = SELECT_IN_PROGRESS;
if (repeated_click)
{
if (++cb->mode > SELECT_MODE_LINE)
cb->mode = SELECT_MODE_CHAR;
}
else
cb->mode = SELECT_MODE_CHAR;
#ifdef FEAT_GUI
if (gui.in_use)
gui_undraw_cursor();
#endif
switch (cb->mode)
{
case SELECT_MODE_CHAR:
cb->origin_start_col = cb->start.col;
cb->word_end_col = clip_get_line_end((int)cb->start.lnum);
break;
case SELECT_MODE_WORD:
clip_get_word_boundaries(cb, (int)cb->start.lnum, cb->start.col);
cb->origin_start_col = cb->word_start_col;
cb->origin_end_col = cb->word_end_col;
clip_invert_area((int)cb->start.lnum, cb->word_start_col,
(int)cb->end.lnum, cb->word_end_col, CLIP_SET);
cb->start.col = cb->word_start_col;
cb->end.col = cb->word_end_col;
break;
case SELECT_MODE_LINE:
clip_invert_area((int)cb->start.lnum, 0, (int)cb->start.lnum,
(int)Columns, CLIP_SET);
cb->start.col = 0;
cb->end.col = Columns;
break;
}
cb->prev = cb->start;
#ifdef DEBUG_SELECTION
printf("Selection started at (%u,%u)\n", cb->start.lnum, cb->start.col);
#endif
}
void
clip_process_selection(
int button,
int col,
int row,
int_u repeated_click)
{
VimClipboard *cb = &clip_star;
int diff;
int slen = 1;
if (button == MOUSE_RELEASE)
{
if (cb->start.lnum == cb->end.lnum && cb->start.col == cb->end.col)
{
#ifdef FEAT_GUI
if (gui.in_use)
gui_update_cursor(FALSE, FALSE);
#endif
cb->state = SELECT_CLEARED;
return;
}
#ifdef DEBUG_SELECTION
printf("Selection ended: (%u,%u) to (%u,%u)\n", cb->start.lnum,
cb->start.col, cb->end.lnum, cb->end.col);
#endif
if (clip_isautosel_star()
|| (
#ifdef FEAT_GUI
gui.in_use ? (vim_strchr(p_go, GO_ASELML) != NULL) :
#endif
clip_autoselectml))
clip_copy_modeless_selection(FALSE);
#ifdef FEAT_GUI
if (gui.in_use)
gui_update_cursor(FALSE, FALSE);
#endif
cb->state = SELECT_DONE;
return;
}
row = check_row(row);
col = check_col(col);
#ifdef FEAT_MBYTE
col = mb_fix_col(col, row);
#endif
if (col == (int)cb->prev.col && row == cb->prev.lnum && !repeated_click)
return;
if (cb->state == SELECT_DONE && button == MOUSE_RIGHT)
{
if (clip_compare_pos(row, col, (int)cb->start.lnum, cb->start.col) < 0
|| (clip_compare_pos(row, col,
(int)cb->end.lnum, cb->end.col) < 0
&& (((cb->start.lnum == cb->end.lnum
&& cb->end.col - col > col - cb->start.col))
|| ((diff = (cb->end.lnum - row) -
(row - cb->start.lnum)) > 0
|| (diff == 0 && col < (int)(cb->start.col +
cb->end.col) / 2)))))
{
cb->origin_row = (short_u)cb->end.lnum;
cb->origin_start_col = cb->end.col - 1;
cb->origin_end_col = cb->end.col;
}
else
{
cb->origin_row = (short_u)cb->start.lnum;
cb->origin_start_col = cb->start.col;
cb->origin_end_col = cb->start.col;
}
if (cb->mode == SELECT_MODE_WORD && !repeated_click)
cb->mode = SELECT_MODE_CHAR;
}
cb->state = SELECT_IN_PROGRESS;
#ifdef DEBUG_SELECTION
printf("Selection extending to (%d,%d)\n", row, col);
#endif
if (repeated_click && ++cb->mode > SELECT_MODE_LINE)
cb->mode = SELECT_MODE_CHAR;
switch (cb->mode)
{
case SELECT_MODE_CHAR:
if (row != cb->prev.lnum)
cb->word_end_col = clip_get_line_end(row);
if (clip_compare_pos(row, col, cb->origin_row,
cb->origin_start_col) >= 0)
{
if (col >= (int)cb->word_end_col)
clip_update_modeless_selection(cb, cb->origin_row,
cb->origin_start_col, row, (int)Columns);
else
{
#ifdef FEAT_MBYTE
if (has_mbyte && mb_lefthalve(row, col))
slen = 2;
#endif
clip_update_modeless_selection(cb, cb->origin_row,
cb->origin_start_col, row, col + slen);
}
}
else
{
#ifdef FEAT_MBYTE
if (has_mbyte
&& mb_lefthalve(cb->origin_row, cb->origin_start_col))
slen = 2;
#endif
if (col >= (int)cb->word_end_col)
clip_update_modeless_selection(cb, row, cb->word_end_col,
cb->origin_row, cb->origin_start_col + slen);
else
clip_update_modeless_selection(cb, row, col,
cb->origin_row, cb->origin_start_col + slen);
}
break;
case SELECT_MODE_WORD:
if (row == cb->prev.lnum && col >= (int)cb->word_start_col
&& col < (int)cb->word_end_col && !repeated_click)
return;
clip_get_word_boundaries(cb, row, col);
if (clip_compare_pos(row, col, cb->origin_row,
cb->origin_start_col) >= 0)
clip_update_modeless_selection(cb, cb->origin_row,
cb->origin_start_col, row, cb->word_end_col);
else
clip_update_modeless_selection(cb, row, cb->word_start_col,
cb->origin_row, cb->origin_end_col);
break;
case SELECT_MODE_LINE:
if (row == cb->prev.lnum && !repeated_click)
return;
if (clip_compare_pos(row, col, cb->origin_row,
cb->origin_start_col) >= 0)
clip_update_modeless_selection(cb, cb->origin_row, 0, row,
(int)Columns);
else
clip_update_modeless_selection(cb, row, 0, cb->origin_row,
(int)Columns);
break;
}
cb->prev.lnum = row;
cb->prev.col = col;
#ifdef DEBUG_SELECTION
printf("Selection is: (%u,%u) to (%u,%u)\n", cb->start.lnum,
cb->start.col, cb->end.lnum, cb->end.col);
#endif
}
# if defined(FEAT_GUI) || defined(PROTO)
void
clip_may_redraw_selection(int row, int col, int len)
{
int start = col;
int end = col + len;
if (clip_star.state != SELECT_CLEARED
&& row >= clip_star.start.lnum
&& row <= clip_star.end.lnum)
{
if (row == clip_star.start.lnum && start < (int)clip_star.start.col)
start = clip_star.start.col;
if (row == clip_star.end.lnum && end > (int)clip_star.end.col)
end = clip_star.end.col;
if (end > start)
clip_invert_area(row, start, row, end, 0);
}
}
# endif
void
clip_clear_selection(VimClipboard *cbd)
{
if (cbd->state == SELECT_CLEARED)
return;
clip_invert_area((int)cbd->start.lnum, cbd->start.col, (int)cbd->end.lnum,
cbd->end.col, CLIP_CLEAR);
cbd->state = SELECT_CLEARED;
}
void
clip_may_clear_selection(int row1, int row2)
{
if (clip_star.state == SELECT_DONE
&& row2 >= clip_star.start.lnum
&& row1 <= clip_star.end.lnum)
clip_clear_selection(&clip_star);
}
void
clip_scroll_selection(
int rows)
{
int lnum;
if (clip_star.state == SELECT_CLEARED)
return;
lnum = clip_star.start.lnum - rows;
if (lnum <= 0)
clip_star.start.lnum = 0;
else if (lnum >= screen_Rows)
clip_star.state = SELECT_CLEARED;
else
clip_star.start.lnum = lnum;
lnum = clip_star.end.lnum - rows;
if (lnum < 0)
clip_star.state = SELECT_CLEARED;
else if (lnum >= screen_Rows)
clip_star.end.lnum = screen_Rows - 1;
else
clip_star.end.lnum = lnum;
}
static void
clip_invert_area(
int row1,
int col1,
int row2,
int col2,
int how)
{
int invert = FALSE;
if (how == CLIP_SET)
invert = TRUE;
if (clip_compare_pos(row1, col1, row2, col2) > 0)
{
int tmp_row, tmp_col;
tmp_row = row1;
tmp_col = col1;
row1 = row2;
col1 = col2;
row2 = tmp_row;
col2 = tmp_col;
}
else if (how == CLIP_TOGGLE)
invert = TRUE;
if (row1 == row2)
{
clip_invert_rectangle(row1, col1, 1, col2 - col1, invert);
}
else
{
if (col1 > 0)
{
clip_invert_rectangle(row1, col1, 1, (int)Columns - col1, invert);
row1++;
}
if (col2 < Columns - 1)
{
clip_invert_rectangle(row2, 0, 1, col2, invert);
row2--;
}
if (row2 >= row1)
clip_invert_rectangle(row1, 0, row2 - row1 + 1, (int)Columns,
invert);
}
}
static void
clip_invert_rectangle(
int row,
int col,
int height,
int width,
int invert)
{
#ifdef FEAT_GUI
if (gui.in_use)
gui_mch_invert_rectangle(row, col, height, width);
else
#endif
screen_draw_rectangle(row, col, height, width, invert);
}
void
clip_copy_modeless_selection(int both UNUSED)
{
char_u *buffer;
char_u *bufp;
int row;
int start_col;
int end_col;
int line_end_col;
int add_newline_flag = FALSE;
int len;
#ifdef FEAT_MBYTE
char_u *p;
#endif
int row1 = clip_star.start.lnum;
int col1 = clip_star.start.col;
int row2 = clip_star.end.lnum;
int col2 = clip_star.end.col;
if (ScreenLines == NULL)
return;
if (row1 > row2)
{
row = row1; row1 = row2; row2 = row;
row = col1; col1 = col2; col2 = row;
}
else if (row1 == row2 && col1 > col2)
{
row = col1; col1 = col2; col2 = row;
}
#ifdef FEAT_MBYTE
p = ScreenLines + LineOffset[row1];
if (enc_dbcs != 0)
col1 -= (*mb_head_off)(p, p + col1);
else if (enc_utf8 && p[col1] == 0)
--col1;
#endif
len = (row2 - row1 + 1) * Columns + 1;
#ifdef FEAT_MBYTE
if (enc_dbcs != 0)
len *= 2;
else if (enc_utf8)
len *= MB_MAXBYTES;
#endif
buffer = lalloc((long_u)len, TRUE);
if (buffer == NULL)
return;
for (bufp = buffer, row = row1; row <= row2; row++)
{
if (row == row1)
start_col = col1;
else
start_col = 0;
if (row == row2)
end_col = col2;
else
end_col = Columns;
line_end_col = clip_get_line_end(row);
if (end_col >= Columns && (row < row2 || end_col > line_end_col))
{
end_col = line_end_col;
if (end_col < start_col)
end_col = start_col;
if (row == row2)
add_newline_flag = TRUE;
}
if (row > row1 && !LineWraps[row - 1])
*bufp++ = NL;
if (row < screen_Rows && end_col <= screen_Columns)
{
#ifdef FEAT_MBYTE
if (enc_dbcs != 0)
{
int i;
p = ScreenLines + LineOffset[row];
for (i = start_col; i < end_col; ++i)
if (enc_dbcs == DBCS_JPNU && p[i] == 0x8e)
{
*bufp++ = 0x8e;
*bufp++ = ScreenLines2[LineOffset[row] + i];
}
else
{
*bufp++ = p[i];
if (MB_BYTE2LEN(p[i]) == 2)
*bufp++ = p[++i];
}
}
else if (enc_utf8)
{
int off;
int i;
int ci;
off = LineOffset[row];
for (i = start_col; i < end_col; ++i)
{
if (ScreenLinesUC[off + i] == 0)
*bufp++ = ScreenLines[off + i];
else
{
bufp += utf_char2bytes(ScreenLinesUC[off + i], bufp);
for (ci = 0; ci < Screen_mco; ++ci)
{
if (ScreenLinesC[ci][off + i] == 0)
break;
bufp += utf_char2bytes(ScreenLinesC[ci][off + i],
bufp);
}
}
if (ScreenLines[off + i + 1] == 0)
++i;
}
}
else
#endif
{
STRNCPY(bufp, ScreenLines + LineOffset[row] + start_col,
end_col - start_col);
bufp += end_col - start_col;
}
}
}
if (add_newline_flag)
*bufp++ = NL;
clip_free_selection(&clip_star);
clip_own_selection(&clip_star);
clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_star);
clip_gen_set_selection(&clip_star);
#ifdef FEAT_X11
if (both)
{
clip_free_selection(&clip_plus);
clip_own_selection(&clip_plus);
clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_plus);
clip_gen_set_selection(&clip_plus);
}
#endif
vim_free(buffer);
}
#define CHAR_CLASS(c) (c <= ' ' ? ' ' : vim_iswordc(c))
static void
clip_get_word_boundaries(VimClipboard *cb, int row, int col)
{
int start_class;
int temp_col;
char_u *p;
#ifdef FEAT_MBYTE
int mboff;
#endif
if (row >= screen_Rows || col >= screen_Columns || ScreenLines == NULL)
return;
p = ScreenLines + LineOffset[row];
#ifdef FEAT_MBYTE
if (enc_dbcs != 0)
col -= dbcs_screen_head_off(p, p + col);
else if (enc_utf8 && p[col] == 0)
--col;
#endif
start_class = CHAR_CLASS(p[col]);
temp_col = col;
for ( ; temp_col > 0; temp_col--)
#ifdef FEAT_MBYTE
if (enc_dbcs != 0
&& (mboff = dbcs_screen_head_off(p, p + temp_col - 1)) > 0)
temp_col -= mboff;
else
#endif
if (CHAR_CLASS(p[temp_col - 1]) != start_class
#ifdef FEAT_MBYTE
&& !(enc_utf8 && p[temp_col - 1] == 0)
#endif
)
break;
cb->word_start_col = temp_col;
temp_col = col;
for ( ; temp_col < screen_Columns; temp_col++)
#ifdef FEAT_MBYTE
if (enc_dbcs != 0 && dbcs_ptr2cells(p + temp_col) == 2)
++temp_col;
else
#endif
if (CHAR_CLASS(p[temp_col]) != start_class
#ifdef FEAT_MBYTE
&& !(enc_utf8 && p[temp_col] == 0)
#endif
)
break;
cb->word_end_col = temp_col;
}
static int
clip_get_line_end(int row)
{
int i;
if (row >= screen_Rows || ScreenLines == NULL)
return 0;
for (i = screen_Columns; i > 0; i--)
if (ScreenLines[LineOffset[row] + i - 1] != ' ')
break;
return i;
}
static void
clip_update_modeless_selection(
VimClipboard *cb,
int row1,
int col1,
int row2,
int col2)
{
if (row1 != cb->start.lnum || col1 != (int)cb->start.col)
{
clip_invert_area(row1, col1, (int)cb->start.lnum, cb->start.col,
CLIP_TOGGLE);
cb->start.lnum = row1;
cb->start.col = col1;
}
if (row2 != cb->end.lnum || col2 != (int)cb->end.col)
{
clip_invert_area((int)cb->end.lnum, cb->end.col, row2, col2,
CLIP_TOGGLE);
cb->end.lnum = row2;
cb->end.col = col2;
}
}
int
clip_gen_own_selection(VimClipboard *cbd)
{
#ifdef FEAT_XCLIPBOARD
# ifdef FEAT_GUI
if (gui.in_use)
return clip_mch_own_selection(cbd);
else
# endif
return clip_xterm_own_selection(cbd);
#else
return clip_mch_own_selection(cbd);
#endif
}
void
clip_gen_lose_selection(VimClipboard *cbd)
{
#ifdef FEAT_XCLIPBOARD
# ifdef FEAT_GUI
if (gui.in_use)
clip_mch_lose_selection(cbd);
else
# endif
clip_xterm_lose_selection(cbd);
#else
clip_mch_lose_selection(cbd);
#endif
}
void
clip_gen_set_selection(VimClipboard *cbd)
{
if (!clip_did_set_selection)
{
if ((cbd == &clip_plus && (clip_unnamed_saved & CLIP_UNNAMED_PLUS))
|| (cbd == &clip_star && (clip_unnamed_saved & CLIP_UNNAMED)))
{
clipboard_needs_update = TRUE;
return;
}
}
#ifdef FEAT_XCLIPBOARD
# ifdef FEAT_GUI
if (gui.in_use)
clip_mch_set_selection(cbd);
else
# endif
clip_xterm_set_selection(cbd);
#else
clip_mch_set_selection(cbd);
#endif
}
void
clip_gen_request_selection(VimClipboard *cbd)
{
#ifdef FEAT_XCLIPBOARD
# ifdef FEAT_GUI
if (gui.in_use)
clip_mch_request_selection(cbd);
else
# endif
clip_xterm_request_selection(cbd);
#else
clip_mch_request_selection(cbd);
#endif
}
int
clip_gen_owner_exists(VimClipboard *cbd UNUSED)
{
#ifdef FEAT_XCLIPBOARD
# ifdef FEAT_GUI_GTK
if (gui.in_use)
return clip_gtk_owner_exists(cbd);
else
# endif
return clip_x11_owner_exists(cbd);
#else
return TRUE;
#endif
}
#endif
#if defined(USE_INPUT_BUF) || defined(PROTO)
#if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_JOB_CHANNEL) \
|| defined(FEAT_CLIENTSERVER)
# define INBUFLEN 4096
#else
# define INBUFLEN 250
#endif
static char_u inbuf[INBUFLEN + MAX_KEY_CODE_LEN];
static int inbufcount = 0;
int
vim_is_input_buf_full(void)
{
return (inbufcount >= INBUFLEN);
}
int
vim_is_input_buf_empty(void)
{
return (inbufcount == 0);
}
#if defined(FEAT_OLE) || defined(PROTO)
int
vim_free_in_input_buf(void)
{
return (INBUFLEN - inbufcount);
}
#endif
#if defined(FEAT_GUI_GTK) || defined(PROTO)
int
vim_used_in_input_buf(void)
{
return inbufcount;
}
#endif
char_u *
get_input_buf(void)
{
garray_T *gap;
gap = (garray_T *)alloc((unsigned)sizeof(garray_T));
if (gap != NULL)
{
gap->ga_data = alloc((unsigned)inbufcount + 1);
if (gap->ga_data != NULL)
mch_memmove(gap->ga_data, inbuf, (size_t)inbufcount);
gap->ga_len = inbufcount;
}
trash_input_buf();
return (char_u *)gap;
}
void
set_input_buf(char_u *p)
{
garray_T *gap = (garray_T *)p;
if (gap != NULL)
{
if (gap->ga_data != NULL)
{
mch_memmove(inbuf, gap->ga_data, gap->ga_len);
inbufcount = gap->ga_len;
vim_free(gap->ga_data);
}
vim_free(gap);
}
}
void
add_to_input_buf(char_u *s, int len)
{
if (inbufcount + len > INBUFLEN + MAX_KEY_CODE_LEN)
return;
#ifdef FEAT_HANGULIN
if ((State & (INSERT|CMDLINE)) && hangul_input_state_get())
if ((len = hangul_input_process(s, len)) == 0)
return;
#endif
while (len--)
inbuf[inbufcount++] = *s++;
}
void
add_to_input_buf_csi(char_u *str, int len)
{
int i;
char_u buf[2];
for (i = 0; i < len; ++i)
{
add_to_input_buf(str + i, 1);
if (str[i] == CSI)
{
buf[0] = KS_EXTRA;
buf[1] = (int)KE_CSI;
add_to_input_buf(buf, 2);
}
}
}
#if defined(FEAT_HANGULIN) || defined(PROTO)
void
push_raw_key(char_u *s, int len)
{
char_u *tmpbuf;
char_u *inp = s;
tmpbuf = hangul_string_convert(s, &len);
if (tmpbuf != NULL)
inp = tmpbuf;
for (; len--; inp++)
{
inbuf[inbufcount++] = *inp;
if (*inp == CSI)
{
inbuf[inbufcount++] = KS_EXTRA;
inbuf[inbufcount++] = (int)KE_CSI;
}
}
vim_free(tmpbuf);
}
#endif
void
trash_input_buf(void)
{
inbufcount = 0;
}
int
read_from_input_buf(char_u *buf, long maxlen)
{
if (inbufcount == 0)
fill_input_buf(TRUE);
if (maxlen > inbufcount)
maxlen = inbufcount;
mch_memmove(buf, inbuf, (size_t)maxlen);
inbufcount -= maxlen;
if (inbufcount)
mch_memmove(inbuf, inbuf + maxlen, (size_t)inbufcount);
return (int)maxlen;
}
void
fill_input_buf(int exit_on_error UNUSED)
{
#if defined(UNIX) || defined(VMS) || defined(MACOS_X)
int len;
int try;
static int did_read_something = FALSE;
# ifdef FEAT_MBYTE
static char_u *rest = NULL;
static int restlen = 0;
int unconverted;
# endif
#endif
#ifdef FEAT_GUI
if (gui.in_use
# ifdef NO_CONSOLE_INPUT
&& !no_console_input()
# endif
)
{
gui_mch_update();
return;
}
#endif
#if defined(UNIX) || defined(VMS) || defined(MACOS_X)
if (vim_is_input_buf_full())
return;
# ifdef __BEOS__
while (!vim_is_input_buf_full() && RealWaitForChar(read_cmd_fd, 0, NULL))
;
len = inbufcount;
inbufcount = 0;
# else
# ifdef FEAT_MBYTE
if (rest != NULL)
{
if (restlen > INBUFLEN - inbufcount)
unconverted = INBUFLEN - inbufcount;
else
unconverted = restlen;
mch_memmove(inbuf + inbufcount, rest, unconverted);
if (unconverted == restlen)
{
vim_free(rest);
rest = NULL;
}
else
{
restlen -= unconverted;
mch_memmove(rest, rest + unconverted, restlen);
}
inbufcount += unconverted;
}
else
unconverted = 0;
# endif
len = 0;
for (try = 0; try < 100; ++try)
{
# ifdef VMS
len = vms_read(
# else
len = read(read_cmd_fd,
# endif
(char *)inbuf + inbufcount, (size_t)((INBUFLEN - inbufcount)
# ifdef FEAT_MBYTE
/ input_conv.vc_factor
# endif
));
# if 0
)
# endif
if (len > 0 || got_int)
break;
if (!did_read_something && !isatty(read_cmd_fd) && read_cmd_fd == 0)
{
int m = cur_tmode;
settmode(TMODE_COOK);
#ifdef HAVE_DUP
close(0);
ignored = dup(2);
#else
read_cmd_fd = 2;
#endif
settmode(m);
}
if (!exit_on_error)
return;
}
# endif
if (len <= 0 && !got_int)
read_error_exit();
if (len > 0)
did_read_something = TRUE;
if (got_int)
{
inbuf[0] = 3;
inbufcount = 1;
}
else
{
# ifdef FEAT_MBYTE
if (input_conv.vc_type != CONV_NONE)
{
inbufcount -= unconverted;
len = convert_input_safe(inbuf + inbufcount,
len + unconverted, INBUFLEN - inbufcount,
rest == NULL ? &rest : NULL, &restlen);
}
# endif
while (len-- > 0)
{
if (inbuf[inbufcount] == 3 && ctrl_c_interrupts)
{
mch_memmove(inbuf, inbuf + inbufcount, (size_t)(len + 1));
inbufcount = 0;
got_int = TRUE;
}
++inbufcount;
}
}
#endif
}
#endif
void
read_error_exit(void)
{
if (silent_mode)
getout(0);
STRCPY(IObuff, _("Vim: Error reading input, exiting...\n"));
preserve_exit();
}
#if defined(CURSOR_SHAPE) || defined(PROTO)
void
ui_cursor_shape_forced(int forced)
{
# ifdef FEAT_GUI
if (gui.in_use)
gui_update_cursor_later();
else
# endif
term_cursor_mode(forced);
# ifdef MCH_CURSOR_SHAPE
mch_update_cursor();
# endif
# ifdef FEAT_CONCEAL
conceal_check_cursur_line();
# endif
}
void
ui_cursor_shape(void)
{
ui_cursor_shape_forced(FALSE);
}
#endif
#if defined(FEAT_CLIPBOARD) || defined(FEAT_GUI) || defined(FEAT_RIGHTLEFT) \
|| defined(FEAT_MBYTE) || defined(PROTO)
int
check_col(int col)
{
if (col < 0)
return 0;
if (col >= (int)screen_Columns)
return (int)screen_Columns - 1;
return col;
}
int
check_row(int row)
{
if (row < 0)
return 0;
if (row >= (int)screen_Rows)
return (int)screen_Rows - 1;
return row;
}
#endif
#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) || defined(PROTO)
# include <X11/Xatom.h>
# include <X11/Intrinsic.h>
void
open_app_context(void)
{
if (app_context == NULL)
{
XtToolkitInitialize();
app_context = XtCreateApplicationContext();
}
}
static Atom vim_atom;
#ifdef FEAT_MBYTE
static Atom vimenc_atom;
static Atom utf8_atom;
#endif
static Atom compound_text_atom;
static Atom text_atom;
static Atom targets_atom;
static Atom timestamp_atom;
void
x11_setup_atoms(Display *dpy)
{
vim_atom = XInternAtom(dpy, VIM_ATOM_NAME, False);
#ifdef FEAT_MBYTE
vimenc_atom = XInternAtom(dpy, VIMENC_ATOM_NAME,False);
utf8_atom = XInternAtom(dpy, "UTF8_STRING", False);
#endif
compound_text_atom = XInternAtom(dpy, "COMPOUND_TEXT", False);
text_atom = XInternAtom(dpy, "TEXT", False);
targets_atom = XInternAtom(dpy, "TARGETS", False);
clip_star.sel_atom = XA_PRIMARY;
clip_plus.sel_atom = XInternAtom(dpy, "CLIPBOARD", False);
timestamp_atom = XInternAtom(dpy, "TIMESTAMP", False);
}
static Boolean clip_x11_convert_selection_cb(Widget w, Atom *sel_atom, Atom *target, Atom *type, XtPointer *value, long_u *length, int *format);
static void clip_x11_lose_ownership_cb(Widget w, Atom *sel_atom);
static void clip_x11_notify_cb(Widget w, Atom *sel_atom, Atom *target);
static void clip_x11_timestamp_cb(Widget w, XtPointer n, XEvent *event, Boolean *cont);
static void clip_x11_request_selection_cb(Widget w, XtPointer success, Atom *sel_atom, Atom *type, XtPointer value, long_u *length, int *format);
static void
clip_x11_timestamp_cb(
Widget w,
XtPointer n UNUSED,
XEvent *event,
Boolean *cont UNUSED)
{
Atom actual_type;
int format;
unsigned long nitems, bytes_after;
unsigned char *prop=NULL;
XPropertyEvent *xproperty=&event->xproperty;
if (event->type != PropertyNotify || xproperty->state
|| (xproperty->atom != clip_star.sel_atom
&& xproperty->atom != clip_plus.sel_atom))
return;
if (XGetWindowProperty(xproperty->display, xproperty->window,
xproperty->atom, 0, 0, False, timestamp_atom, &actual_type, &format,
&nitems, &bytes_after, &prop))
return;
if (prop)
XFree(prop);
if (actual_type != timestamp_atom || format != 32)
return;
if (XtOwnSelection(w, xproperty->atom, xproperty->time,
clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
clip_x11_notify_cb) == OK)
{
if (xproperty->atom == clip_plus.sel_atom)
clip_plus.owned = TRUE;
else
clip_star.owned = TRUE;
}
}
void
x11_setup_selection(Widget w)
{
XtAddEventHandler(w, PropertyChangeMask, False,
clip_x11_timestamp_cb, (XtPointer)NULL);
}
static void
clip_x11_request_selection_cb(
Widget w UNUSED,
XtPointer success,
Atom *sel_atom,
Atom *type,
XtPointer value,
long_u *length,
int *format)
{
int motion_type = MAUTO;
long_u len;
char_u *p;
char **text_list = NULL;
VimClipboard *cbd;
#ifdef FEAT_MBYTE
char_u *tmpbuf = NULL;
#endif
if (*sel_atom == clip_plus.sel_atom)
cbd = &clip_plus;
else
cbd = &clip_star;
if (value == NULL || *length == 0)
{
clip_free_selection(cbd);
*(int *)success = FALSE;
return;
}
p = (char_u *)value;
len = *length;
if (*type == vim_atom)
{
motion_type = *p++;
len--;
}
#ifdef FEAT_MBYTE
else if (*type == vimenc_atom)
{
char_u *enc;
vimconv_T conv;
int convlen;
motion_type = *p++;
--len;
enc = p;
p += STRLEN(p) + 1;
len -= p - enc;
conv.vc_type = CONV_NONE;
convert_setup(&conv, enc, p_enc);
if (conv.vc_type != CONV_NONE)
{
convlen = len;
tmpbuf = string_convert(&conv, p, &convlen);
len = convlen;
if (tmpbuf != NULL)
p = tmpbuf;
convert_setup(&conv, NULL, NULL);
}
}
#endif
else if (*type == compound_text_atom
#ifdef FEAT_MBYTE
|| *type == utf8_atom
#endif
|| (
#ifdef FEAT_MBYTE
enc_dbcs != 0 &&
#endif
*type == text_atom))
{
XTextProperty text_prop;
int n_text = 0;
int status;
text_prop.value = (unsigned char *)value;
text_prop.encoding = *type;
text_prop.format = *format;
text_prop.nitems = len;
#if defined(FEAT_MBYTE) && defined(X_HAVE_UTF8_STRING)
if (*type == utf8_atom)
status = Xutf8TextPropertyToTextList(X_DISPLAY, &text_prop,
&text_list, &n_text);
else
#endif
status = XmbTextPropertyToTextList(X_DISPLAY, &text_prop,
&text_list, &n_text);
if (status != Success || n_text < 1)
{
*(int *)success = FALSE;
return;
}
p = (char_u *)text_list[0];
len = STRLEN(p);
}
clip_yank_selection(motion_type, p, (long)len, cbd);
if (text_list != NULL)
XFreeStringList(text_list);
#ifdef FEAT_MBYTE
vim_free(tmpbuf);
#endif
XtFree((char *)value);
*(int *)success = TRUE;
}
void
clip_x11_request_selection(
Widget myShell,
Display *dpy,
VimClipboard *cbd)
{
XEvent event;
Atom type;
static int success;
int i;
time_t start_time;
int timed_out = FALSE;
for (i =
#ifdef FEAT_MBYTE
0
#else
1
#endif
; i < 6; i++)
{
switch (i)
{
#ifdef FEAT_MBYTE
case 0: type = vimenc_atom; break;
#endif
case 1: type = vim_atom; break;
#ifdef FEAT_MBYTE
case 2: type = utf8_atom; break;
#endif
case 3: type = compound_text_atom; break;
case 4: type = text_atom; break;
default: type = XA_STRING;
}
#ifdef FEAT_MBYTE
if (type == utf8_atom
# if defined(X_HAVE_UTF8_STRING)
&& !enc_utf8
# endif
)
continue;
#endif
success = MAYBE;
XtGetSelectionValue(myShell, cbd->sel_atom, type,
clip_x11_request_selection_cb, (XtPointer)&success, CurrentTime);
XFlush(dpy);
start_time = time(NULL);
while (success == MAYBE)
{
if (XCheckTypedEvent(dpy, PropertyNotify, &event)
|| XCheckTypedEvent(dpy, SelectionNotify, &event)
|| XCheckTypedEvent(dpy, SelectionRequest, &event))
{
XtDispatchEvent(&event);
continue;
}
if (time(NULL) > start_time + 2)
{
timed_out = TRUE;
break;
}
XSync(dpy, False);
ui_delay(1L, TRUE);
}
if (success == TRUE)
return;
if (timed_out)
break;
}
yank_cut_buffer0(dpy, cbd);
}
static Boolean
clip_x11_convert_selection_cb(
Widget w UNUSED,
Atom *sel_atom,
Atom *target,
Atom *type,
XtPointer *value,
long_u *length,
int *format)
{
static char_u *save_result = NULL;
static long_u save_length = 0;
char_u *string;
int motion_type;
VimClipboard *cbd;
int i;
if (*sel_atom == clip_plus.sel_atom)
cbd = &clip_plus;
else
cbd = &clip_star;
if (!cbd->owned)
return False;
if (*target == targets_atom)
{
static Atom array[7];
*value = (XtPointer)array;
i = 0;
array[i++] = targets_atom;
#ifdef FEAT_MBYTE
array[i++] = vimenc_atom;
#endif
array[i++] = vim_atom;
#ifdef FEAT_MBYTE
if (enc_utf8)
array[i++] = utf8_atom;
#endif
array[i++] = XA_STRING;
array[i++] = text_atom;
array[i++] = compound_text_atom;
*type = XA_ATOM;
*format = 32;
*length = i;
return True;
}
if ( *target != XA_STRING
#ifdef FEAT_MBYTE
&& *target != vimenc_atom
&& (*target != utf8_atom || !enc_utf8)
#endif
&& *target != vim_atom
&& *target != text_atom
&& *target != compound_text_atom)
return False;
clip_get_selection(cbd);
motion_type = clip_convert_selection(&string, length, cbd);
if (motion_type < 0)
return False;
if (*target == vim_atom)
(*length)++;
#ifdef FEAT_MBYTE
if (*target == vimenc_atom)
*length += STRLEN(p_enc) + 2;
#endif
if (save_length < *length || save_length / 2 >= *length)
*value = XtRealloc((char *)save_result, (Cardinal)*length + 1);
else
*value = save_result;
if (*value == NULL)
{
vim_free(string);
return False;
}
save_result = (char_u *)*value;
save_length = *length;
if (*target == XA_STRING
#ifdef FEAT_MBYTE
|| (*target == utf8_atom && enc_utf8)
#endif
)
{
mch_memmove(save_result, string, (size_t)(*length));
*type = *target;
}
else if (*target == compound_text_atom || *target == text_atom)
{
XTextProperty text_prop;
char *string_nt = (char *)save_result;
int conv_result;
mch_memmove(string_nt, string, (size_t)*length);
string_nt[*length] = NUL;
conv_result = XmbTextListToTextProperty(X_DISPLAY, (char **)&string_nt,
1, XCompoundTextStyle, &text_prop);
if (conv_result != Success)
{
vim_free(string);
return False;
}
*value = (XtPointer)(text_prop.value);
*length = text_prop.nitems;
*type = compound_text_atom;
XtFree((char *)save_result);
save_result = (char_u *)*value;
save_length = *length;
}
#ifdef FEAT_MBYTE
else if (*target == vimenc_atom)
{
int l = STRLEN(p_enc);
save_result[0] = motion_type;
STRCPY(save_result + 1, p_enc);
mch_memmove(save_result + l + 2, string, (size_t)(*length - l - 2));
*type = vimenc_atom;
}
#endif
else
{
save_result[0] = motion_type;
mch_memmove(save_result + 1, string, (size_t)(*length - 1));
*type = vim_atom;
}
*format = 8;
vim_free(string);
return True;
}
static void
clip_x11_lose_ownership_cb(Widget w UNUSED, Atom *sel_atom)
{
if (*sel_atom == clip_plus.sel_atom)
clip_lose_selection(&clip_plus);
else
clip_lose_selection(&clip_star);
}
void
clip_x11_lose_selection(Widget myShell, VimClipboard *cbd)
{
XtDisownSelection(myShell, cbd->sel_atom,
XtLastTimestampProcessed(XtDisplay(myShell)));
}
static void
clip_x11_notify_cb(Widget w UNUSED, Atom *sel_atom UNUSED, Atom *target UNUSED)
{
}
int
clip_x11_own_selection(Widget myShell, VimClipboard *cbd)
{
#ifdef FEAT_GUI
if (gui.in_use)
{
if (XtOwnSelection(myShell, cbd->sel_atom,
XtLastTimestampProcessed(XtDisplay(myShell)),
clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb,
clip_x11_notify_cb) == False)
return FAIL;
}
else
#endif
{
if (!XChangeProperty(XtDisplay(myShell), XtWindow(myShell),
cbd->sel_atom, timestamp_atom, 32, PropModeAppend, NULL, 0))
return FAIL;
}
XFlush(XtDisplay(myShell));
return OK;
}
void
clip_x11_set_selection(VimClipboard *cbd UNUSED)
{
}
int
clip_x11_owner_exists(VimClipboard *cbd)
{
return XGetSelectionOwner(X_DISPLAY, cbd->sel_atom) != None;
}
#endif
#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) \
|| defined(FEAT_GUI_GTK) || defined(PROTO)
void
yank_cut_buffer0(Display *dpy, VimClipboard *cbd)
{
int nbytes = 0;
char_u *buffer = (char_u *)XFetchBuffer(dpy, &nbytes, 0);
if (nbytes > 0)
{
#ifdef FEAT_MBYTE
int done = FALSE;
if (has_mbyte)
{
char_u *conv_buf;
vimconv_T vc;
vc.vc_type = CONV_NONE;
if (convert_setup(&vc, (char_u *)"latin1", p_enc) == OK)
{
conv_buf = string_convert(&vc, buffer, &nbytes);
if (conv_buf != NULL)
{
clip_yank_selection(MCHAR, conv_buf, (long)nbytes, cbd);
vim_free(conv_buf);
done = TRUE;
}
convert_setup(&vc, NULL, NULL);
}
}
if (!done)
#endif
clip_yank_selection(MCHAR, buffer, (long)nbytes, cbd);
XFree((void *)buffer);
if (p_verbose > 0)
{
verbose_enter();
verb_msg((char_u *)_("Used CUT_BUFFER0 instead of empty selection"));
verbose_leave();
}
}
}
#endif
#if defined(FEAT_MOUSE) || defined(PROTO)
int
jump_to_mouse(
int flags,
int *inclusive,
int which_button)
{
static int on_status_line = 0;
static int on_sep_line = 0;
#ifdef FEAT_MENU
static int in_winbar = FALSE;
#endif
static int prev_row = -1;
static int prev_col = -1;
static win_T *dragwin = NULL;
static int did_drag = FALSE;
win_T *wp, *old_curwin;
pos_T old_cursor;
int count;
int first;
int row = mouse_row;
int col = mouse_col;
#ifdef FEAT_FOLDING
int mouse_char;
#endif
mouse_past_bottom = FALSE;
mouse_past_eol = FALSE;
if (flags & MOUSE_RELEASED)
{
if (dragwin != NULL && !did_drag)
flags &= ~(MOUSE_FOCUS | MOUSE_DID_MOVE);
dragwin = NULL;
did_drag = FALSE;
}
if ((flags & MOUSE_DID_MOVE)
&& prev_row == mouse_row
&& prev_col == mouse_col)
{
retnomove:
if (on_status_line)
return IN_STATUS_LINE;
if (on_sep_line)
return IN_SEP_LINE;
if (flags & MOUSE_MAY_STOP_VIS)
{
end_visual_mode();
redraw_curbuf_later(INVERTED);
}
#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
if (cmdwin_type != 0 && row < curwin->w_winrow)
return IN_OTHER_WIN;
#endif
return IN_BUFFER;
}
prev_row = mouse_row;
prev_col = mouse_col;
if (flags & MOUSE_SETPOS)
goto retnomove;
#ifdef FEAT_FOLDING
if (row >= 0 && row < Rows && col >= 0 && col <= Columns
&& ScreenLines != NULL)
mouse_char = ScreenLines[LineOffset[row] + col];
else
mouse_char = ' ';
#endif
old_curwin = curwin;
old_cursor = curwin->w_cursor;
if (!(flags & MOUSE_FOCUS))
{
if (row < 0 || col < 0)
return IN_UNKNOWN;
wp = mouse_find_win(&row, &col);
if (wp == NULL)
return IN_UNKNOWN;
dragwin = NULL;
#ifdef FEAT_MENU
if (row == -1)
{
winbar_click(wp, col);
in_winbar = TRUE;
return IN_OTHER_WIN | MOUSE_WINBAR;
}
in_winbar = FALSE;
#endif
if (row >= wp->w_height)
{
on_status_line = row - wp->w_height + 1;
dragwin = wp;
}
else
on_status_line = 0;
if (col >= wp->w_width)
{
on_sep_line = col - wp->w_width + 1;
dragwin = wp;
}
else
on_sep_line = 0;
if (on_status_line && on_sep_line)
{
if (stl_connected(wp))
on_sep_line = 0;
else
on_status_line = 0;
}
if (VIsual_active
&& (wp->w_buffer != curwin->w_buffer
|| (!on_status_line && !on_sep_line
#ifdef FEAT_FOLDING
&& (
# ifdef FEAT_RIGHTLEFT
wp->w_p_rl ? col < wp->w_width - wp->w_p_fdc :
# endif
col >= wp->w_p_fdc
# ifdef FEAT_CMDWIN
+ (cmdwin_type == 0 && wp == curwin ? 0 : 1)
# endif
)
#endif
&& (flags & MOUSE_MAY_STOP_VIS))))
{
end_visual_mode();
redraw_curbuf_later(INVERTED);
}
#ifdef FEAT_CMDWIN
if (cmdwin_type != 0 && wp != curwin)
{
on_sep_line = 0;
# ifdef FEAT_CLIPBOARD
if (on_status_line)
return IN_STATUS_LINE;
return IN_OTHER_WIN;
# else
row = 0;
col += wp->w_wincol;
wp = curwin;
# endif
}
#endif
if (dragwin == NULL || (flags & MOUSE_RELEASED))
win_enter(wp, TRUE);
#ifdef CHECK_DOUBLE_CLICK
if (curwin != old_curwin)
set_mouse_topline(curwin);
#endif
if (on_status_line)
{
if (curwin == old_curwin)
return IN_STATUS_LINE;
else
return IN_STATUS_LINE | CURSOR_MOVED;
}
if (on_sep_line)
{
if (curwin == old_curwin)
return IN_SEP_LINE;
else
return IN_SEP_LINE | CURSOR_MOVED;
}
curwin->w_cursor.lnum = curwin->w_topline;
#ifdef FEAT_GUI
gui_prev_topline = curwin->w_topline;
# ifdef FEAT_DIFF
gui_prev_topfill = curwin->w_topfill;
# endif
#endif
}
else if (on_status_line && which_button == MOUSE_LEFT)
{
if (dragwin != NULL)
{
count = row - dragwin->w_winrow - dragwin->w_height + 1
- on_status_line;
win_drag_status_line(dragwin, count);
did_drag |= count;
}
return IN_STATUS_LINE;
}
else if (on_sep_line && which_button == MOUSE_LEFT)
{
if (dragwin != NULL)
{
count = col - dragwin->w_wincol - dragwin->w_width + 1
- on_sep_line;
win_drag_vsep_line(dragwin, count);
did_drag |= count;
}
return IN_SEP_LINE;
}
#ifdef FEAT_MENU
else if (in_winbar)
{
return IN_OTHER_WIN | MOUSE_WINBAR;
}
#endif
else
{
if (flags & MOUSE_MAY_STOP_VIS)
{
end_visual_mode();
redraw_curbuf_later(INVERTED);
}
#if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD)
if (cmdwin_type != 0 && row < curwin->w_winrow)
return IN_OTHER_WIN;
#endif
row -= W_WINROW(curwin);
col -= curwin->w_wincol;
if (row < 0)
{
count = 0;
for (first = TRUE; curwin->w_topline > 1; )
{
#ifdef FEAT_DIFF
if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
++count;
else
#endif
count += plines(curwin->w_topline - 1);
if (!first && count > -row)
break;
first = FALSE;
#ifdef FEAT_FOLDING
(void)hasFolding(curwin->w_topline, &curwin->w_topline, NULL);
#endif
#ifdef FEAT_DIFF
if (curwin->w_topfill < diff_check(curwin, curwin->w_topline))
++curwin->w_topfill;
else
#endif
{
--curwin->w_topline;
#ifdef FEAT_DIFF
curwin->w_topfill = 0;
#endif
}
}
#ifdef FEAT_DIFF
check_topfill(curwin, FALSE);
#endif
curwin->w_valid &=
~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
redraw_later(VALID);
row = 0;
}
else if (row >= curwin->w_height)
{
count = 0;
for (first = TRUE; curwin->w_topline < curbuf->b_ml.ml_line_count; )
{
#ifdef FEAT_DIFF
if (curwin->w_topfill > 0)
++count;
else
#endif
count += plines(curwin->w_topline);
if (!first && count > row - curwin->w_height + 1)
break;
first = FALSE;
#ifdef FEAT_FOLDING
if (hasFolding(curwin->w_topline, NULL, &curwin->w_topline)
&& curwin->w_topline == curbuf->b_ml.ml_line_count)
break;
#endif
#ifdef FEAT_DIFF
if (curwin->w_topfill > 0)
--curwin->w_topfill;
else
#endif
{
++curwin->w_topline;
#ifdef FEAT_DIFF
curwin->w_topfill =
diff_check_fill(curwin, curwin->w_topline);
#endif
}
}
#ifdef FEAT_DIFF
check_topfill(curwin, FALSE);
#endif
redraw_later(VALID);
curwin->w_valid &=
~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP);
row = curwin->w_height - 1;
}
else if (row == 0)
{
if (mouse_dragging > 0
&& curwin->w_cursor.lnum
== curwin->w_buffer->b_ml.ml_line_count
&& curwin->w_cursor.lnum == curwin->w_topline)
curwin->w_valid &= ~(VALID_TOPLINE);
}
}
#ifdef FEAT_FOLDING
if (
# ifdef FEAT_RIGHTLEFT
curwin->w_p_rl ? col < curwin->w_width - curwin->w_p_fdc :
# endif
col >= curwin->w_p_fdc
# ifdef FEAT_CMDWIN
+ (cmdwin_type == 0 ? 0 : 1)
# endif
)
mouse_char = ' ';
#endif
if (mouse_comp_pos(curwin, &row, &col, &curwin->w_cursor.lnum))
mouse_past_bottom = TRUE;
if ((flags & MOUSE_MAY_VIS) && !VIsual_active)
{
check_visual_highlight();
VIsual = old_cursor;
VIsual_active = TRUE;
VIsual_reselect = TRUE;
may_start_select('o');
setmouse();
if (p_smd && msg_silent == 0)
redraw_cmdline = TRUE;
}
curwin->w_curswant = col;
curwin->w_set_curswant = FALSE;
if (coladvance(col) == FAIL)
{
if (inclusive != NULL)
*inclusive = TRUE;
mouse_past_eol = TRUE;
}
else if (inclusive != NULL)
*inclusive = FALSE;
count = IN_BUFFER;
if (curwin != old_curwin || curwin->w_cursor.lnum != old_cursor.lnum
|| curwin->w_cursor.col != old_cursor.col)
count |= CURSOR_MOVED;
#ifdef FEAT_FOLDING
if (mouse_char == '+')
count |= MOUSE_FOLD_OPEN;
else if (mouse_char != ' ')
count |= MOUSE_FOLD_CLOSE;
#endif
return count;
}
int
mouse_comp_pos(
win_T *win,
int *rowp,
int *colp,
linenr_T *lnump)
{
int col = *colp;
int row = *rowp;
linenr_T lnum;
int retval = FALSE;
int off;
int count;
#ifdef FEAT_RIGHTLEFT
if (win->w_p_rl)
col = win->w_width - 1 - col;
#endif
lnum = win->w_topline;
while (row > 0)
{
#ifdef FEAT_DIFF
if (win->w_p_diff
# ifdef FEAT_FOLDING
&& !hasFoldingWin(win, lnum, NULL, NULL, TRUE, NULL)
# endif
)
{
if (lnum == win->w_topline)
row -= win->w_topfill;
else
row -= diff_check_fill(win, lnum);
count = plines_win_nofill(win, lnum, TRUE);
}
else
#endif
count = plines_win(win, lnum, TRUE);
if (count > row)
break;
#ifdef FEAT_FOLDING
(void)hasFoldingWin(win, lnum, NULL, &lnum, TRUE, NULL);
#endif
if (lnum == win->w_buffer->b_ml.ml_line_count)
{
retval = TRUE;
break;
}
row -= count;
++lnum;
}
if (!retval)
{
off = win_col_off(win) - win_col_off2(win);
if (col < off)
col = off;
col += row * (win->w_width - off);
col += win->w_skipcol;
}
if (!win->w_p_wrap)
col += win->w_leftcol;
col -= win_col_off(win);
if (col < 0)
{
#ifdef FEAT_NETBEANS_INTG
netbeans_gutter_click(lnum);
#endif
col = 0;
}
*colp = col;
*rowp = row;
*lnump = lnum;
return retval;
}
win_T *
mouse_find_win(int *rowp, int *colp UNUSED)
{
frame_T *fp;
win_T *wp;
fp = topframe;
*rowp -= firstwin->w_winrow;
for (;;)
{
if (fp->fr_layout == FR_LEAF)
break;
if (fp->fr_layout == FR_ROW)
{
for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
{
if (*colp < fp->fr_width)
break;
*colp -= fp->fr_width;
}
}
else
{
for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
{
if (*rowp < fp->fr_height)
break;
*rowp -= fp->fr_height;
}
}
}
FOR_ALL_WINDOWS(wp)
if (wp == fp->fr_win)
{
#ifdef FEAT_MENU
*rowp -= wp->w_winbar_height;
#endif
return wp;
}
return NULL;
}
#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MAC) \
|| defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
|| defined(FEAT_GUI_PHOTON) || defined(PROTO)
int
get_fpos_of_mouse(pos_T *mpos)
{
win_T *wp;
int row = mouse_row;
int col = mouse_col;
if (row < 0 || col < 0)
return IN_UNKNOWN;
wp = mouse_find_win(&row, &col);
if (wp == NULL)
return IN_UNKNOWN;
if (row >= wp->w_height)
return IN_STATUS_LINE;
if (col >= wp->w_width)
return IN_SEP_LINE;
if (wp != curwin)
return IN_UNKNOWN;
if (mouse_comp_pos(curwin, &row, &col, &mpos->lnum))
return IN_STATUS_LINE;
mpos->col = vcol2col(wp, mpos->lnum, col);
if (mpos->col > 0)
--mpos->col;
#ifdef FEAT_VIRTUALEDIT
mpos->coladd = 0;
#endif
return IN_BUFFER;
}
int
vcol2col(win_T *wp, linenr_T lnum, int vcol)
{
int count = 0;
char_u *ptr;
char_u *line;
line = ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
while (count < vcol && *ptr != NUL)
{
count += win_lbr_chartabsize(wp, line, ptr, count, NULL);
MB_PTR_ADV(ptr);
}
return (int)(ptr - line);
}
#endif
#endif
#if defined(FEAT_GUI) || defined(WIN3264) || defined(PROTO)
void
ui_focus_change(
int in_focus)
{
static time_t last_time = (time_t)0;
int need_redraw = FALSE;
if (in_focus && last_time + 2 < time(NULL))
{
need_redraw = check_timestamps(
# ifdef FEAT_GUI
gui.in_use
# else
FALSE
# endif
);
last_time = time(NULL);
}
#ifdef FEAT_AUTOCMD
need_redraw |= apply_autocmds(in_focus ? EVENT_FOCUSGAINED
: EVENT_FOCUSLOST, NULL, NULL, FALSE, curbuf);
#endif
if (need_redraw)
{
need_wait_return = FALSE;
if (State & CMDLINE)
redrawcmdline();
else if (State == HITRETURN || State == SETWSIZE || State == ASKMORE
|| State == EXTERNCMD || State == CONFIRM || exmode_active)
repeat_message();
else if ((State & NORMAL) || (State & INSERT))
{
if (must_redraw != 0)
update_screen(0);
setcursor();
}
cursor_on();
out_flush();
# ifdef FEAT_GUI
if (gui.in_use)
{
gui_update_cursor(FALSE, TRUE);
gui_update_scrollbars(FALSE);
}
# endif
}
#ifdef FEAT_TITLE
if (need_maketitle)
maketitle();
#endif
}
#endif
#if defined(USE_IM_CONTROL) || defined(PROTO)
void
im_save_status(long *psave)
{
if (!p_imdisable && KeyTyped && !KeyStuffed
# ifdef FEAT_XIM
&& xic != NULL
# endif
# ifdef FEAT_GUI
&& (!gui.in_use || gui.in_focus)
# endif
)
{
if (vgetc_im_active)
*psave = B_IMODE_IM;
else if (*psave == B_IMODE_IM)
*psave = B_IMODE_NONE;
}
}
#endif