vim-patch:8.0.1553: find digraph to insert a character #8190

Problem:    Cannot see what digraph is used to insert a character.
Solution:   Show the digraph with the "ga" command. (Christian Brabandt)
5f73ef8d20

close #8190
This commit is contained in:
lokesh1197
2018-02-28 16:36:14 +05:30
committed by Justin M. Keyes
parent 500345aea2
commit 3159cd4503
8 changed files with 131 additions and 58 deletions

View File

@@ -37,23 +37,34 @@ CTRL-L Clear and redraw the screen. The redraw may happen
< <
:as[cii] or *ga* *:as* *:ascii* :as[cii] or *ga* *:as* *:ascii*
ga Print the ascii value of the character under the ga Print the ascii value of the character under the
cursor in decimal, hexadecimal and octal. For cursor in decimal, hexadecimal and octal.
example, when the cursor is on a 'R': Mnemonic: Get Ascii value.
For example, when the cursor is on a 'R':
<R> 82, Hex 52, Octal 122 ~ <R> 82, Hex 52, Octal 122 ~
When the character is a non-standard ASCII character, When the character is a non-standard ASCII character,
but printable according to the 'isprint' option, the but printable according to the 'isprint' option, the
non-printable version is also given. When the non-printable version is also given.
character is larger than 127, the <M-x> form is also
printed. For example: When the character is larger than 127, the <M-x> form
is also printed. For example:
<~A> <M-^A> 129, Hex 81, Octal 201 ~ <~A> <M-^A> 129, Hex 81, Octal 201 ~
<p> <|~> <M-~> 254, Hex fe, Octal 376 ~ <p> <|~> <M-~> 254, Hex fe, Octal 376 ~
(where <p> is a special character) (where <p> is a special character)
The <Nul> character in a file is stored internally as The <Nul> character in a file is stored internally as
<NL>, but it will be shown as: <NL>, but it will be shown as:
<^@> 0, Hex 00, Octal 000 ~ <^@> 0, Hex 00, Octal 000 ~
If the character has composing characters these are If the character has composing characters these are
also shown. The value of 'maxcombine' doesn't matter. also shown. The value of 'maxcombine' doesn't matter.
Mnemonic: Get ASCII value.
If the character can be inserted as a digraph, also
output the two characters that can be used to create
the character:
<ö> 246, Hex 00f6, Oct 366, Digr o: ~
This shows you can type CTRL-K o : to insert ö.
*g8* *g8*
g8 Print the hex values of the bytes used in the g8 Print the hex values of the bytes used in the

View File

@@ -1448,6 +1448,33 @@ int do_digraph(int c)
return c; return c;
} }
/// Find a digraph for "val". If found return the string to display it.
/// If not found return NULL.
char_u *get_digraph_for_char(int val)
{
digr_T *dp;
static char_u r[3];
for (int use_defaults = 0; use_defaults <= 1; use_defaults++) {
if (use_defaults == 0) {
dp = (digr_T *)user_digraphs.ga_data;
} else {
dp = digraphdefault;
}
for (int i = 0;
use_defaults ? dp->char1 != NUL : i < user_digraphs.ga_len; i++) {
if (dp->result == val) {
r[0] = dp->char1;
r[1] = dp->char2;
r[2] = NUL;
return r;
}
dp++;
}
}
return NULL;
}
/// Get a digraph. Used after typing CTRL-K on the command line or in normal /// Get a digraph. Used after typing CTRL-K on the command line or in normal
/// mode. /// mode.
/// ///

View File

@@ -114,6 +114,7 @@ typedef struct {
/// ":ascii" and "ga" implementation /// ":ascii" and "ga" implementation
void do_ascii(const exarg_T *const eap) void do_ascii(const exarg_T *const eap)
{ {
char_u *dig;
int cc[MAX_MCO]; int cc[MAX_MCO];
int c = utfc_ptr2char(get_cursor_pos_ptr(), cc); int c = utfc_ptr2char(get_cursor_pos_ptr(), cc);
if (c == NUL) { if (c == NUL) {
@@ -141,10 +142,22 @@ void do_ascii(const exarg_T *const eap)
} }
char buf2[20]; char buf2[20];
buf2[0] = NUL; buf2[0] = NUL;
dig = get_digraph_for_char(cval);
if (dig != NULL) {
iobuff_len += ( iobuff_len += (
vim_snprintf((char *)IObuff + iobuff_len, sizeof(IObuff) - iobuff_len, vim_snprintf((char *)IObuff + iobuff_len,
sizeof(IObuff) - iobuff_len,
_("<%s>%s%s %d, Hex %02x, Oct %03o, Digr %s"),
transchar(c), buf1, buf2, cval, cval, cval, dig));
} else {
iobuff_len += (
vim_snprintf((char *)IObuff + iobuff_len,
sizeof(IObuff) - iobuff_len,
_("<%s>%s%s %d, Hex %02x, Octal %03o"), _("<%s>%s%s %d, Hex %02x, Octal %03o"),
transchar(c), buf1, buf2, cval, cval, cval)); transchar(c), buf1, buf2, cval, cval, cval));
}
c = cc[ci++]; c = cc[ci++];
} }
@@ -179,11 +192,25 @@ void do_ascii(const exarg_T *const eap)
IObuff[iobuff_len++] = ' '; // Draw composing char on top of a space. IObuff[iobuff_len++] = ' '; // Draw composing char on top of a space.
} }
iobuff_len += utf_char2bytes(c, IObuff + iobuff_len); iobuff_len += utf_char2bytes(c, IObuff + iobuff_len);
dig = get_digraph_for_char(c);
if (dig != NULL) {
iobuff_len += ( iobuff_len += (
vim_snprintf((char *)IObuff + iobuff_len, sizeof(IObuff) - iobuff_len, vim_snprintf((char *)IObuff + iobuff_len,
sizeof(IObuff) - iobuff_len,
(c < 0x10000
? _("> %d, Hex %04x, Oct %o, Digr %s")
: _("> %d, Hex %08x, Oct %o, Digr %s")),
c, c, c, dig));
} else {
iobuff_len += (
vim_snprintf((char *)IObuff + iobuff_len,
sizeof(IObuff) - iobuff_len,
(c < 0x10000 (c < 0x10000
? _("> %d, Hex %04x, Octal %o") ? _("> %d, Hex %04x, Octal %o")
: _("> %d, Hex %08x, Octal %o")), c, c, c)); : _("> %d, Hex %08x, Octal %o")),
c, c, c));
}
if (ci == MAX_MCO) { if (ci == MAX_MCO) {
break; break;
} }

View File

@@ -237,3 +237,14 @@ func RunVimPiped(before, after, arguments, pipecmd)
endif endif
return 1 return 1
endfunc endfunc
" Get line "lnum" as displayed on the screen.
" Trailing white space is trimmed.
func! Screenline(lnum)
let chars = []
for c in range(1, winwidth(0))
call add(chars, nr2char(screenchar(a:lnum, c)))
endfor
let line = join(chars, '')
return matchstr(line, '^.\{-}\ze\s*$')
endfunc

View File

@@ -16,9 +16,9 @@ func s:get_chars(lnum)
let numchars = strchars(getline('.'), 1) let numchars = strchars(getline('.'), 1)
for i in range(1, numchars) for i in range(1, numchars)
exe 'norm ' i . '|' exe 'norm ' i . '|'
let c=execute('ascii') let c = execute('ascii')
let c=substitute(c, '\n\?<.\{-}Hex\s*', 'U+', 'g') let c = substitute(c, '\n\?<.\{-}Hex\s*', 'U+', 'g')
let c=substitute(c, ',\s*Octal\s*\d*', '', 'g') let c = substitute(c, ',\s*Oct\(al\)\=\s\d*\(, Digr ..\)\=', '', 'g')
call add(chars, c) call add(chars, c)
endfor endfor
return chars return chars

View File

@@ -4,15 +4,15 @@ if !has("digraphs") || !has("multi_byte")
finish finish
endif endif
func! Put_Dig(chars) func Put_Dig(chars)
exe "norm! o\<c-k>".a:chars exe "norm! o\<c-k>".a:chars
endfu endfu
func! Put_Dig_BS(char1, char2) func Put_Dig_BS(char1, char2)
exe "norm! o".a:char1."\<bs>".a:char2 exe "norm! o".a:char1."\<bs>".a:char2
endfu endfu
func! Test_digraphs() func Test_digraphs()
new new
call Put_Dig("00") call Put_Dig("00")
call assert_equal("∞", getline('.')) call assert_equal("∞", getline('.'))
@@ -214,7 +214,7 @@ func! Test_digraphs()
bw! bw!
endfunc endfunc
func! Test_digraphs_option() func Test_digraphs_option()
" reset whichwrap option, so that testing <esc><bs>A works, " reset whichwrap option, so that testing <esc><bs>A works,
" without moving up a line " without moving up a line
set digraph ww= set digraph ww=
@@ -420,7 +420,7 @@ func! Test_digraphs_option()
bw! bw!
endfunc endfunc
func! Test_digraphs_output() func Test_digraphs_output()
new new
let out = execute(':digraph') let out = execute(':digraph')
call assert_equal('Eu € 8364', matchstr(out, '\C\<Eu\D*8364\>')) call assert_equal('Eu € 8364', matchstr(out, '\C\<Eu\D*8364\>'))
@@ -436,7 +436,7 @@ func! Test_digraphs_output()
bw! bw!
endfunc endfunc
func! Test_loadkeymap() func Test_loadkeymap()
if !has('keymap') if !has('keymap')
return return
endif endif
@@ -450,7 +450,7 @@ func! Test_loadkeymap()
bw! bw!
endfunc endfunc
func! Test_digraph_cmndline() func Test_digraph_cmndline()
" Create digraph on commandline " Create digraph on commandline
" This is a hack, to let Vim create the digraph in commandline mode " This is a hack, to let Vim create the digraph in commandline mode
let s = '' let s = ''
@@ -458,4 +458,11 @@ func! Test_digraph_cmndline()
call assert_equal("€", s) call assert_equal("€", s)
endfunc endfunc
func Test_show_digraph()
new
call Put_Dig("e=")
call assert_equal("\n<е> 1077, Hex 0435, Oct 2065, Digr e=", execute('ascii'))
bwipe!
endfunc
" vim: shiftwidth=2 sts=2 expandtab " vim: shiftwidth=2 sts=2 expandtab

View File

@@ -11,13 +11,13 @@ func Test_ga_command()
new new
set display=uhex set display=uhex
call assert_equal("\nNUL", Do_ga('')) call assert_equal("\nNUL", Do_ga(''))
call assert_equal("\n<<01>> 1, Hex 01, Octal 001", Do_ga("\x01")) call assert_equal("\n<<01>> 1, Hex 01, Oct 001, Digr SH", Do_ga("\x01"))
call assert_equal("\n<<09>> 9, Hex 09, Octal 011", Do_ga("\t")) call assert_equal("\n<<09>> 9, Hex 09, Oct 011, Digr HT", Do_ga("\t"))
set display= set display=
call assert_equal("\nNUL", Do_ga('')) call assert_equal("\nNUL", Do_ga(''))
call assert_equal("\n<^A> 1, Hex 01, Octal 001", Do_ga("\x01")) call assert_equal("\n<^A> 1, Hex 01, Oct 001, Digr SH", Do_ga("\x01"))
call assert_equal("\n<^I> 9, Hex 09, Octal 011", Do_ga("\t")) call assert_equal("\n<^I> 9, Hex 09, Oct 011, Digr HT", Do_ga("\t"))
call assert_equal("\n<e> 101, Hex 65, Octal 145", Do_ga('e')) call assert_equal("\n<e> 101, Hex 65, Octal 145", Do_ga('e'))
@@ -26,8 +26,8 @@ func Test_ga_command()
endif endif
" Test a few multi-bytes characters. " Test a few multi-bytes characters.
call assert_equal("\n<é> 233, Hex 00e9, Octal 351", Do_ga('é')) call assert_equal("\n<é> 233, Hex 00e9, Oct 351, Digr e'", Do_ga('é'))
call assert_equal("\n<ẻ> 7867, Hex 1ebb, Octal 17273", Do_ga('ẻ')) call assert_equal("\n<ẻ> 7867, Hex 1ebb, Oct 17273, Digr e2", Do_ga('ẻ'))
" Test with combining characters. " Test with combining characters.
call assert_equal("\n<e> 101, Hex 65, Octal 145 < ́> 769, Hex 0301, Octal 1401", Do_ga("e\u0301")) call assert_equal("\n<e> 101, Hex 65, Octal 145 < ́> 769, Hex 0301, Octal 1401", Do_ga("e\u0301"))

View File

@@ -3,17 +3,7 @@ if !has('conceal')
finish finish
endif endif
function! s:screenline(lnum) abort source shared.vim
let line = []
for c in range(1, winwidth(0))
call add(line, nr2char(screenchar(a:lnum, c)))
endfor
return s:trim(join(line, ''))
endfunction
function! s:trim(str) abort
return matchstr(a:str,'^\s*\zs.\{-}\ze\s*$')
endfunction
function! Test_simple_matchadd() function! Test_simple_matchadd()
new new
@@ -26,7 +16,7 @@ function! Test_simple_matchadd()
call matchadd('Conceal', '\%2l ') call matchadd('Conceal', '\%2l ')
redraw! redraw!
let lnum = 2 let lnum = 2
call assert_equal(expect, s:screenline(lnum)) call assert_equal(expect, Screenline(lnum))
call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2)) call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2)) call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7)) call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
@@ -49,7 +39,7 @@ function! Test_simple_matchadd_and_conceal()
call matchadd('Conceal', '\%2l ', 10, -1, {'conceal': 'X'}) call matchadd('Conceal', '\%2l ', 10, -1, {'conceal': 'X'})
redraw! redraw!
let lnum = 2 let lnum = 2
call assert_equal(expect, s:screenline(lnum)) call assert_equal(expect, Screenline(lnum))
call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2)) call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7)) call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10)) call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
@@ -75,7 +65,7 @@ function! Test_matchadd_and_conceallevel_3()
call matchadd('Conceal', '\%2l ', 10, -1, {'conceal': 'X'}) call matchadd('Conceal', '\%2l ', 10, -1, {'conceal': 'X'})
redraw! redraw!
let lnum = 2 let lnum = 2
call assert_equal(expect, s:screenline(lnum)) call assert_equal(expect, Screenline(lnum))
call assert_equal(screenattr(lnum, 1), screenattr(lnum, 2)) call assert_equal(screenattr(lnum, 1), screenattr(lnum, 2))
call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7)) call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10)) call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
@@ -88,7 +78,7 @@ function! Test_matchadd_and_conceallevel_3()
call matchadd('ErrorMsg', '\%2l Test', 20, -1, {'conceal': 'X'}) call matchadd('ErrorMsg', '\%2l Test', 20, -1, {'conceal': 'X'})
redraw! redraw!
call assert_equal(expect, s:screenline(lnum)) call assert_equal(expect, Screenline(lnum))
call assert_equal(screenattr(lnum, 1) , screenattr(lnum, 2)) call assert_equal(screenattr(lnum, 1) , screenattr(lnum, 2))
call assert_equal(screenattr(lnum, 2) , screenattr(lnum, 7)) call assert_equal(screenattr(lnum, 2) , screenattr(lnum, 7))
call assert_notequal(screenattr(lnum, 1) , screenattr(lnum, 10)) call assert_notequal(screenattr(lnum, 1) , screenattr(lnum, 10))
@@ -112,7 +102,7 @@ function! Test_default_conceal_char()
call matchadd('Conceal', '\%2l ', 10, -1, {}) call matchadd('Conceal', '\%2l ', 10, -1, {})
redraw! redraw!
let lnum = 2 let lnum = 2
call assert_equal(expect, s:screenline(lnum)) call assert_equal(expect, Screenline(lnum))
call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2)) call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7)) call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10)) call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
@@ -125,7 +115,7 @@ function! Test_default_conceal_char()
set listchars=conceal:+ set listchars=conceal:+
redraw! redraw!
call assert_equal(expect, s:screenline(lnum)) call assert_equal(expect, Screenline(lnum))
call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2)) call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7)) call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10)) call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
@@ -149,7 +139,7 @@ function! Test_syn_and_match_conceal()
syntax match MyConceal /\%2l / conceal containedin=ALL cchar=* syntax match MyConceal /\%2l / conceal containedin=ALL cchar=*
redraw! redraw!
let lnum = 2 let lnum = 2
call assert_equal(expect, s:screenline(lnum)) call assert_equal(expect, Screenline(lnum))
call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2)) call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7)) call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10)) call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
@@ -161,7 +151,7 @@ function! Test_syn_and_match_conceal()
call clearmatches() call clearmatches()
redraw! redraw!
call assert_equal(expect, s:screenline(lnum)) call assert_equal(expect, Screenline(lnum))
call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2)) call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7)) call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10)) call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
@@ -187,7 +177,7 @@ function! Test_clearmatches()
redraw! redraw!
let lnum = 2 let lnum = 2
call assert_equal(expect, s:screenline(lnum)) call assert_equal(expect, Screenline(lnum))
call assert_equal(screenattr(lnum, 1), screenattr(lnum, 2)) call assert_equal(screenattr(lnum, 1), screenattr(lnum, 2))
call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7)) call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10)) call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
@@ -200,7 +190,7 @@ function! Test_clearmatches()
call setmatches(a) call setmatches(a)
redraw! redraw!
call assert_equal(expect, s:screenline(lnum)) call assert_equal(expect, Screenline(lnum))
call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2)) call assert_notequal(screenattr(lnum, 1), screenattr(lnum, 2))
call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7)) call assert_equal(screenattr(lnum, 2), screenattr(lnum, 7))
call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10)) call assert_equal(screenattr(lnum, 2), screenattr(lnum, 10))
@@ -228,7 +218,7 @@ function! Test_using_matchaddpos()
redraw! redraw!
let lnum = 2 let lnum = 2
call assert_equal(expect, s:screenline(lnum)) call assert_equal(expect, Screenline(lnum))
call assert_notequal(screenattr(lnum, 1) , screenattr(lnum, 2)) call assert_notequal(screenattr(lnum, 1) , screenattr(lnum, 2))
call assert_notequal(screenattr(lnum, 2) , screenattr(lnum, 7)) call assert_notequal(screenattr(lnum, 2) , screenattr(lnum, 7))
call assert_equal(screenattr(lnum, 1) , screenattr(lnum, 7)) call assert_equal(screenattr(lnum, 1) , screenattr(lnum, 7))
@@ -250,13 +240,13 @@ function! Test_matchadd_repeat_conceal_with_syntax_off()
1put ='TARGET_TARGETTARGET' 1put ='TARGET_TARGETTARGET'
call cursor(1, 1) call cursor(1, 1)
redraw redraw
call assert_equal('TARGET_TARGETTARGET', s:screenline(2)) call assert_equal('TARGET_TARGETTARGET', Screenline(2))
setlocal conceallevel=2 setlocal conceallevel=2
call matchadd('Conceal', 'TARGET', 10, -1, {'conceal': 't'}) call matchadd('Conceal', 'TARGET', 10, -1, {'conceal': 't'})
redraw redraw
call assert_equal('t_tt', s:screenline(2)) call assert_equal('t_tt', Screenline(2))
quit! quit!
endfunction endfunction
@@ -272,13 +262,13 @@ function! Test_matchadd_and_syn_conceal()
syntax on syntax on
syntax keyword coqKwd bool conceal cchar=- syntax keyword coqKwd bool conceal cchar=-
redraw! redraw!
call assert_equal(expect, s:screenline(1)) call assert_equal(expect, Screenline(1))
call assert_notequal(screenattr(1, 10) , screenattr(1, 11)) call assert_notequal(screenattr(1, 10) , screenattr(1, 11))
call assert_notequal(screenattr(1, 11) , screenattr(1, 12)) call assert_notequal(screenattr(1, 11) , screenattr(1, 12))
call assert_equal(screenattr(1, 11) , screenattr(1, 32)) call assert_equal(screenattr(1, 11) , screenattr(1, 32))
call matchadd('CheckedByCoq', '\%<2l\%>9c\%<16c') call matchadd('CheckedByCoq', '\%<2l\%>9c\%<16c')
redraw! redraw!
call assert_equal(expect, s:screenline(1)) call assert_equal(expect, Screenline(1))
call assert_notequal(screenattr(1, 10) , screenattr(1, 11)) call assert_notequal(screenattr(1, 10) , screenattr(1, 11))
call assert_notequal(screenattr(1, 11) , screenattr(1, 12)) call assert_notequal(screenattr(1, 11) , screenattr(1, 12))
call assert_equal(screenattr(1, 11) , screenattr(1, 32)) call assert_equal(screenattr(1, 11) , screenattr(1, 32))