diff --git a/autoload/singlechar.vim b/autoload/singlechar.vim new file mode 100644 index 0000000..f14370e --- /dev/null +++ b/autoload/singlechar.vim @@ -0,0 +1,43 @@ +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 + + var key = pkey + if pkey == '' + echo g:singlechar_prompt + redraw + + # Get character from user + var char = getchar() + key = nr2char(char) + endif + if key ==# "\" + return + endif + + # Determine whether to insert before (i) or after (a) cursor + var cmd = (mode ==# 'at') ? 'i' : 'a' + var text = repeat(key, count) + + execute 'normal! ' .. cmd .. text .. "\" + #save + g:last_singlechar_key = key + g:last_singlechar_mode = mode + g:last_singlechar_count = count + + #set vim-repeat + #if exists('*repeat#set') + legacy silent! call repeat#set("\(singlechar-repeat)") + #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 diff --git a/plugin/singlechar.vim b/plugin/singlechar.vim index 76d8b2f..465158c 100644 --- a/plugin/singlechar.vim +++ b/plugin/singlechar.vim @@ -1,4 +1,5 @@ vim9script noclear +import autoload 'singlechar.vim' # ------------------------------------------------------------------------------ # # singlechar.vim - Insert single characters without entering insert mode # Author: lohhiiccc @@ -17,8 +18,8 @@ if exists('g:loaded_singlechar') || &cp || v:version < 900 endif g:loaded_singlechar = 1 # ------------------------------------------------------------------------------ # - # Configuration options + # Mapping to insert a character at cursor position (before cursor) if !exists('g:singlechar_map_insert_at') g:singlechar_map_insert_at = 'i' @@ -35,61 +36,18 @@ if !exists('g:singlechar_prompt') endif # ------------------------------------------------------------------------------ # -# Core functionality - +# g:last_singlechar_key = '' g:last_singlechar_mode = '' g:last_singlechar_count = 1 - -# 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) -def InsertChar(mode: string, count: number, pkey: string = ''): void - - var key = pkey - if pkey == '' - echo g:singlechar_prompt - redraw - - # Get character from user - var char = getchar() - key = nr2char(char) - endif - if key ==# "\" - return - endif - - # Determine whether to insert before (i) or after (a) cursor - var cmd = (mode ==# 'at') ? 'i' : 'a' - var text = repeat(key, count) - - execute 'normal! ' .. cmd .. text .. "\" - #save - g:last_singlechar_key = key - g:last_singlechar_mode = mode - g:last_singlechar_count = count - - #set vim-repeat - if exists('*repeat#set') - legacy call repeat#set("\(singlechar-repeat)") - 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 - +# # ------------------------------------------------------------------------------ # # Mappings nnoremap (singlechar-repeat) :call g:RepeatSingleChar() # Direct command implementations -command! -count=1 -nargs=0 InsertCharAt InsertChar('at', ) -command! -count=1 -nargs=0 InsertCharAfter InsertChar('after', ) +command! -count=1 -nargs=0 InsertCharAt singlechar.InsertChar('at', ) +command! -count=1 -nargs=0 InsertCharAfter singlechar.InsertChar('after', ) # Create default mappings unless disabled if !exists('g:singlechar_no_mappings') @@ -97,7 +55,6 @@ if !exists('g:singlechar_no_mappings') execute 'nnoremap ' .. g:singlechar_map_insert_after .. ' ":InsertCharAfter " .. v:count1 .. ""' endif - # Usage: # i - Insert character at cursor position # a - Insert character after cursor position