Add termpastefilter option

Change to specify a character to be filtered as an option when pasting on
the terminal.
This commit is contained in:
erw7 2020-04-04 16:10:09 +09:00
parent 14158e8d47
commit 1e3fadb419
5 changed files with 91 additions and 8 deletions

View File

@ -6289,6 +6289,29 @@ A jump table for the options with a short description can be found at |Q_op|.
attributes instead of "cterm" attributes. |highlight-guifg| attributes instead of "cterm" attributes. |highlight-guifg|
Requires an ISO-8613-3 compatible terminal. Requires an ISO-8613-3 compatible terminal.
*'termpastefilter'* *'tpf'*
'termpastefilter' 'tpf' string (default: "")
global
A comma separated list of options for specifying control characters
to be removed from the text pasted into the terminal window. The
supported values are:
BS Backspace
HT TAB
FF Form feed
ESC Escape
DEL DEL
C0 Other control characters, excluding Line feed and
Carriage return < ' '
C1 Control characters 0x80...0x9F
*'terse'* *'noterse'* *'terse'* *'noterse'*
'terse' boolean (default off) 'terse' boolean (default off)
global global

View File

@ -1942,6 +1942,7 @@ static void didset_options(void)
(void)opt_strings_flags(p_dy, p_dy_values, &dy_flags, true); (void)opt_strings_flags(p_dy, p_dy_values, &dy_flags, true);
(void)opt_strings_flags(p_rdb, p_rdb_values, &rdb_flags, true); (void)opt_strings_flags(p_rdb, p_rdb_values, &rdb_flags, true);
(void)opt_strings_flags(p_tc, p_tc_values, &tc_flags, false); (void)opt_strings_flags(p_tc, p_tc_values, &tc_flags, false);
(void)opt_strings_flags(p_tpf, p_tpf_values, &tpf_flags, true);
(void)opt_strings_flags(p_ve, p_ve_values, &ve_flags, true); (void)opt_strings_flags(p_ve, p_ve_values, &ve_flags, true);
(void)opt_strings_flags(p_wop, p_wop_values, &wop_flags, true); (void)opt_strings_flags(p_wop, p_wop_values, &wop_flags, true);
(void)opt_strings_flags(p_jop, p_jop_values, &jop_flags, true); (void)opt_strings_flags(p_jop, p_jop_values, &jop_flags, true);
@ -3077,6 +3078,10 @@ ambw_end:
if (!parse_winhl_opt(curwin)) { if (!parse_winhl_opt(curwin)) {
errmsg = e_invarg; errmsg = e_invarg;
} }
} else if (varp == &p_tpf) {
if (opt_strings_flags(p_tpf, p_tpf_values, &tpf_flags, true) != OK) {
errmsg = e_invarg;
}
} else { } else {
// Options that are a list of flags. // Options that are a list of flags.
p = NULL; p = NULL;

View File

@ -621,6 +621,19 @@ EXTERN int p_sta; // 'smarttab'
EXTERN int p_sb; // 'splitbelow' EXTERN int p_sb; // 'splitbelow'
EXTERN long p_tpm; // 'tabpagemax' EXTERN long p_tpm; // 'tabpagemax'
EXTERN char_u *p_tal; // 'tabline' EXTERN char_u *p_tal; // 'tabline'
EXTERN char_u *p_tpf; // 'termpastefilter'
EXTERN unsigned int tpf_flags; ///< flags from 'termpastefilter'
#ifdef IN_OPTION_C
static char *(p_tpf_values[]) =
{ "BS", "HT", "FF", "ESC", "DEL", "C0", "C1", NULL };
#endif
# define TPF_BS 0x001
# define TPF_HT 0x002
# define TPF_FF 0x004
# define TPF_ESC 0x008
# define TPF_DEL 0x010
# define TPF_C0 0x020
# define TPF_C1 0x040
EXTERN char_u *p_sps; // 'spellsuggest' EXTERN char_u *p_sps; // 'spellsuggest'
EXTERN int p_spr; // 'splitright' EXTERN int p_spr; // 'splitright'
EXTERN int p_sol; // 'startofline' EXTERN int p_sol; // 'startofline'

View File

@ -2820,6 +2820,14 @@ return {
varname='p_tgc', varname='p_tgc',
defaults={if_true={vi=false}} defaults={if_true={vi=false}}
}, },
{
full_name='termpastefilter', abbreviation='tpf',
type='string', list='onecomma', scope={'global'},
deny_duplicates=true,
vim=true,
varname='p_tpf',
defaults={if_true={vi="", vim=""}}
},
{ {
full_name='terse', full_name='terse',
short_desc=N_("hides notification of search wrap"), short_desc=N_("hides notification of search wrap"),

View File

@ -535,6 +535,38 @@ void terminal_send(Terminal *term, char *data, size_t size)
term->opts.write_cb(data, size, term->opts.data); term->opts.write_cb(data, size, term->opts.data);
} }
static bool is_filter_char(int c)
{
unsigned int flag = 0;
switch (c) {
case 0x08:
flag = TPF_BS;
break;
case 0x09:
flag = TPF_HT;
break;
case 0x0A:
case 0x0D:
break;
case 0x0C:
flag = TPF_FF;
break;
case 0x1b:
flag = TPF_ESC;
break;
case 0x7F:
flag = TPF_DEL;
break;
default:
if (c < ' ') {
flag = TPF_C0;
} else if (c >= 0x80 && c <= 0x9F) {
flag = TPF_C1;
}
}
return !!(tpf_flags & flag);
}
void terminal_paste(long count, char_u **y_array, size_t y_size) void terminal_paste(long count, char_u **y_array, size_t y_size)
{ {
vterm_keyboard_start_paste(curbuf->terminal->vt); vterm_keyboard_start_paste(curbuf->terminal->vt);
@ -553,16 +585,18 @@ void terminal_paste(long count, char_u **y_array, size_t y_size)
buff = xrealloc(buff, len); buff = xrealloc(buff, len);
buff_len = len; buff_len = len;
} }
char_u *p = buff; char_u *dst = buff;
char_u *q = y_array[j]; char_u *src = y_array[j];
while (*q != '\0') { while (*src != '\0') {
if ((*q > '\x07' && *q < '\x0e' && *q != '\x0b' && *q != '\x0c') len = (size_t)utf_ptr2len(src);
|| *q > '\x1f') { int c = utf_ptr2char(src);
*(p++) = *q; if (!is_filter_char(c)) {
memcpy(dst, src, len);
dst += len;
} }
q++; src += len;
} }
terminal_send(curbuf->terminal, (char *)buff, (size_t)(p - buff)); terminal_send(curbuf->terminal, (char *)buff, (size_t)(dst - buff));
} }
} }
xfree(buff); xfree(buff);