From de7d77d1b48f24beeb782f700b2c59ca0f8b9463 Mon Sep 17 00:00:00 2001 From: lohhiiccc <96543753+lohhiiccc@users.noreply.github.com> Date: Thu, 21 Aug 2025 16:13:10 +0200 Subject: [PATCH] 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 (I and 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. --- autoload/singlechar.vim | 30 +++++++++++++++++++++++++++--- plugin/singlechar.vim | 21 +++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/autoload/singlechar.vim b/autoload/singlechar.vim index bd8cefc..53878e2 100644 --- a/autoload/singlechar.vim +++ b/autoload/singlechar.vim @@ -19,11 +19,10 @@ export def InsertChar(mode: string, count: number, input_char: string = ''): voi return endif - # Determine whether to insert before (i) or after (a) cursor - var insert_command = (mode ==# 'at') ? 'i' : 'a' + var insert_command = GetInsertCommand(mode) var insert_text = repeat(first_char, (count == 0) ? 1 : count) - execute 'normal! ' .. insert_command .. insert_text .. "\" + DoSingleChar(insert_command, insert_text, g:singlechar_static_cursor) # Save last used values g:last_singlechar_key = first_char @@ -38,6 +37,31 @@ export def InsertChar(mode: string, count: number, input_char: string = ''): voi 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 .. "\" + 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) diff --git a/plugin/singlechar.vim b/plugin/singlechar.vim index e4ddf80..b0d62dc 100644 --- a/plugin/singlechar.vim +++ b/plugin/singlechar.vim @@ -30,15 +30,32 @@ if !exists('g:singlechar_map_insert_after') g:singlechar_map_insert_after = 'a' endif +# Mapping to insert a character at the endline +if !exists('g:singlechar_map_insert_end') + g:singlechar_map_insert_end = 'A' +endif + +# Mapping to insert a character at the startline +if !exists('g:singlechar_map_insert_begin') + g:singlechar_map_insert_begin = 'I' +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...' endif +#Toggle static cursor +if !exists('g:singlechar_static_cursor') + g:singlechar_static_cursor = 0 +endif + +# toggle warnign if !exists('g:singlechar_keylen_warning') g:singlechar_keylen_warning = 1 endif +# warning message if !exists('g:singlechar_warning_message') g:singlechar_warning_message = 'Only the first character will be taken: {char}' endif @@ -54,6 +71,8 @@ g:last_singlechar_count = 1 nnoremap (singlechar-repeat) :call g:RepeatSingleChar() # Direct command implementations +command! -count=1 -nargs=? InsertCharBegin singlechar.InsertChar('begin', , ) +command! -count=1 -nargs=? InsertCharEnd singlechar.InsertChar('end', , ) command! -count=1 -nargs=? InsertCharAt singlechar.InsertChar('at', , ) command! -count=1 -nargs=? InsertCharAfter singlechar.InsertChar('after', , ) @@ -61,6 +80,8 @@ command! -count=1 -nargs=? InsertCharAfter singlechar.InsertChar('after', ' .. g:singlechar_map_insert_at .. ' ":InsertCharAt " .. v:count1 .. ""' execute 'nnoremap ' .. g:singlechar_map_insert_after .. ' ":InsertCharAfter " .. v:count1 .. ""' + execute 'nnoremap ' .. g:singlechar_map_insert_begin .. ' ":InsertCharBegin " .. v:count1 .. ""' + execute 'nnoremap ' .. g:singlechar_map_insert_end .. ' ":InsertCharEnd " .. v:count1 .. ""' endif # Usage: