feat: improve InsertChar handling for Unicode and input validation
- Use strcharpart for Unicode character support - Add warning message if more than one character is entered - Refactor InsertChar to clarify variable names and flow - Make user input handling robust against accidental multi-char input - Add global config for warning message - Update commands and mappings for flexible argument handling
This commit is contained in:
parent
cd5475bb20
commit
e3b5bdd392
2 changed files with 35 additions and 25 deletions
|
|
@ -1,39 +1,41 @@
|
|||
vim9script noclear
|
||||
|
||||
# Function to handle character insertion
|
||||
# Parameters:
|
||||
# mode: 'at' to insert before cursor, 'after' to insert after cursor
|
||||
# count: number of times to repeat the character
|
||||
# pkey: character to insert (optional)
|
||||
export def InsertChar(mode: string, count: number, pkey: string = ''): void
|
||||
export def InsertChar(mode: string, count: number, input_char: string = ''): void
|
||||
var warning_message = ''
|
||||
var first_char = strcharpart(input_char, 0, 1) # strcharpart for Unicode support
|
||||
|
||||
var key = pkey
|
||||
if pkey == ''
|
||||
if input_char == ''
|
||||
echo g:singlechar_prompt
|
||||
redraw
|
||||
|
||||
# Get character from user
|
||||
var char = getchar()
|
||||
key = nr2char(char)
|
||||
first_char = nr2char(char)
|
||||
elseif g:singlechar_keylen_warning == 1 && strchars(input_char) > 1
|
||||
warning_message = substitute(g:singlechar_warning_message, '{char}', first_char, '')
|
||||
endif
|
||||
if key ==# "\<Esc>"
|
||||
|
||||
if first_char ==# "\<Esc>"
|
||||
return
|
||||
endif
|
||||
|
||||
# Determine whether to insert before (i) or after (a) cursor
|
||||
var cmd = (mode ==# 'at') ? 'i' : 'a'
|
||||
var text = repeat(key, count)
|
||||
var insert_command = (mode ==# 'at') ? 'i' : 'a'
|
||||
var insert_text = repeat(first_char, (count == 0) ? 1 : count)
|
||||
|
||||
execute 'normal! ' .. cmd .. text .. "\<Esc>"
|
||||
#save
|
||||
g:last_singlechar_key = key
|
||||
execute 'normal! ' .. insert_command .. insert_text .. "\<Esc>"
|
||||
|
||||
# Save last used values
|
||||
g:last_singlechar_key = first_char
|
||||
g:last_singlechar_mode = mode
|
||||
g:last_singlechar_count = count
|
||||
|
||||
#set vim-repeat
|
||||
#if exists('*repeat#set')
|
||||
legacy silent! call repeat#set("\<Plug>(singlechar-repeat)")
|
||||
#endif
|
||||
legacy silent! call repeat#set("\<Plug>(singlechar-repeat)")
|
||||
|
||||
if warning_message != ''
|
||||
redraw!
|
||||
echomsg warning_message
|
||||
endif
|
||||
enddef
|
||||
|
||||
def g:RepeatSingleChar(): void
|
||||
|
|
|
|||
|
|
@ -32,7 +32,15 @@ endif
|
|||
|
||||
# Prompt message shown when waiting for character input
|
||||
if !exists('g:singlechar_prompt')
|
||||
g:singlechar_prompt = 'Press the character to insert - Press Esc to cancel...'
|
||||
g:singlechar_prompt = 'Press the character to insert - Press Esc to cancel...'
|
||||
endif
|
||||
|
||||
if !exists('g:singlechar_keylen_warning')
|
||||
g:singlechar_keylen_warning = 1
|
||||
endif
|
||||
|
||||
if !exists('g:singlechar_warning_message')
|
||||
g:singlechar_warning_message = 'Only the first character will be taken: {char}'
|
||||
endif
|
||||
|
||||
# ------------------------------------------------------------------------------ #
|
||||
|
|
@ -46,8 +54,8 @@ g:last_singlechar_count = 1
|
|||
nnoremap <Plug>(singlechar-repeat) :call g:RepeatSingleChar()<CR>
|
||||
|
||||
# Direct command implementations
|
||||
command! -count=1 -nargs=0 InsertCharAt singlechar.InsertChar('at', <count>)
|
||||
command! -count=1 -nargs=0 InsertCharAfter singlechar.InsertChar('after', <count>)
|
||||
command! -count=1 -nargs=? InsertCharAt call singlechar.InsertChar('at', <count>, <q-args>)
|
||||
command! -count=1 -nargs=? InsertCharAfter singlechar.InsertChar('after', <count>, <q-args>)
|
||||
|
||||
# Create default mappings unless disabled
|
||||
if !exists('g:singlechar_no_mappings')
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue