vim-singlechar/autoload/singlechar.vim
lohhiiccc de7d77d1b4 feature: Add begin/end line singlechar insertion and static cursor option
- Introduce InsertCharBegin and InsertCharEnd commands
to insert a character at the beginning or end of the line (<Leader>I and <Leader>A).
- Add configuration variables for customizable mappings:
 - g:singlechar_map_insert_begin
 - g:singlechar_map_insert_end
- Implement GetInsertCommand() and DoSingleChar() in autoload/singlechar.vim
to support new modes and static cursor feature.
- Add g:singlechar_static_cursor option:
    when enabled, cursor returns to its original position after insertion.
2025-08-21 16:13:10 +02:00

69 lines
1.8 KiB
VimL

vim9script noclear
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
if input_char == ''
echo g:singlechar_prompt
redraw
# Get character from user
var char = getchar()
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 first_char ==# "\<Esc>"
return
endif
var insert_command = GetInsertCommand(mode)
var insert_text = repeat(first_char, (count == 0) ? 1 : count)
DoSingleChar(insert_command, insert_text, g:singlechar_static_cursor)
# Save last used values
g:last_singlechar_key = first_char
g:last_singlechar_mode = mode
g:last_singlechar_count = count
legacy silent! call repeat#set("\<Plug>(singlechar-repeat)")
if warning_message != ''
redraw!
echomsg warning_message
endif
enddef
def GetInsertCommand(mode: string): string
if mode ==# 'at'
return 'i'
elseif mode ==# 'after'
return 'a'
elseif mode ==# 'begin'
return 'I'
elseif mode ==# 'end'
return 'A'
else
return 'i'
endif
enddef
def DoSingleChar(command: string, text: string, resetCursor: number): void
var pos = getpos('.')
execute 'normal! ' .. command .. text .. "\<Esc>"
if resetCursor == 1
if (command ==# 'i')
pos[2] = pos[2] + 1
endif
setpos('.', pos)
endif
enddef
def g:RepeatSingleChar(): void
if g:last_singlechar_key != ''
call InsertChar(g:last_singlechar_mode, g:last_singlechar_count, g:last_singlechar_key)
endif
enddef