Feature/unicode input #2
4 changed files with 51 additions and 28 deletions
11
README.md
11
README.md
|
|
@ -1,6 +1,6 @@
|
||||||
# vim-singlechar
|
# vim-singlechar
|
||||||
|
|
||||||
A simple Vim plugin that lets you insert single characters without leaving normal mode.
|
A simple Vim plugin that lets you insert single characters (including Unicode) without leaving normal mode.
|
||||||
|
|
||||||
## Overview
|
## Overview
|
||||||
|
|
||||||
|
|
@ -8,7 +8,7 @@ A simple Vim plugin that lets you insert single characters without leaving norma
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- Insert characters at or after the cursor position
|
- Insert characters (supports Unicode) at or after the cursor position
|
||||||
- Support for counts to insert a character multiple times
|
- Support for counts to insert a character multiple times
|
||||||
- Stay in normal mode for efficient editing
|
- Stay in normal mode for efficient editing
|
||||||
- Minimal workflow interruption
|
- Minimal workflow interruption
|
||||||
|
|
@ -71,6 +71,13 @@ let g:singlechar_prompt = 'Press the character to insert - Press Esc to cancel..
|
||||||
|
|
||||||
" Disable default mappings (if you want to create your own)
|
" Disable default mappings (if you want to create your own)
|
||||||
let g:singlechar_no_mappings = 1
|
let g:singlechar_no_mappings = 1
|
||||||
|
|
||||||
|
" Show warning if more than one character is entered (default: enabled)
|
||||||
|
let g:singlechar_keylen_warning = 1
|
||||||
|
|
||||||
|
" Customize the warning message (default: 'Only the first character will be taken: {char}')
|
||||||
|
let g:singlechar_warning_message = 'Only the first character will be taken: {char}'
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Commands
|
## Commands
|
||||||
|
|
|
||||||
|
|
@ -1,39 +1,41 @@
|
||||||
vim9script noclear
|
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
|
export def InsertChar(mode: string, count: number, input_char: string = ''): void
|
||||||
if pkey == ''
|
var warning_message = ''
|
||||||
|
var first_char = strcharpart(input_char, 0, 1) # strcharpart for Unicode support
|
||||||
|
|
||||||
|
if input_char == ''
|
||||||
echo g:singlechar_prompt
|
echo g:singlechar_prompt
|
||||||
redraw
|
redraw
|
||||||
|
|
||||||
# Get character from user
|
# Get character from user
|
||||||
var char = getchar()
|
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
|
endif
|
||||||
if key ==# "\<Esc>"
|
|
||||||
|
if first_char ==# "\<Esc>"
|
||||||
return
|
return
|
||||||
endif
|
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 .. "\<Esc>"
|
# Determine whether to insert before (i) or after (a) cursor
|
||||||
#save
|
var insert_command = (mode ==# 'at') ? 'i' : 'a'
|
||||||
g:last_singlechar_key = key
|
var insert_text = repeat(first_char, (count == 0) ? 1 : count)
|
||||||
|
|
||||||
|
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_mode = mode
|
||||||
g:last_singlechar_count = count
|
g:last_singlechar_count = count
|
||||||
|
|
||||||
#set vim-repeat
|
legacy silent! call repeat#set("\<Plug>(singlechar-repeat)")
|
||||||
#if exists('*repeat#set')
|
|
||||||
legacy silent! call repeat#set("\<Plug>(singlechar-repeat)")
|
if warning_message != ''
|
||||||
#endif
|
redraw!
|
||||||
|
echomsg warning_message
|
||||||
|
endif
|
||||||
enddef
|
enddef
|
||||||
|
|
||||||
def g:RepeatSingleChar(): void
|
def g:RepeatSingleChar(): void
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ INTRODUCTION *singlechar-introducti
|
||||||
vim-singlechar is a minimalist Vim plugin designed to optimize the workflow of
|
vim-singlechar is a minimalist Vim plugin designed to optimize the workflow of
|
||||||
inserting single characters during editing. It eliminates the need to enter insert
|
inserting single characters during editing. It eliminates the need to enter insert
|
||||||
mode for simple edits like adding punctuation, which helps maintain the flow of
|
mode for simple edits like adding punctuation, which helps maintain the flow of
|
||||||
normal mode operations.
|
normal mode operations. Now supports Unicode.
|
||||||
|
|
||||||
===============================================================================
|
===============================================================================
|
||||||
REQUIREMENTS *singlechar-requirements*
|
REQUIREMENTS *singlechar-requirements*
|
||||||
|
|
@ -111,6 +111,12 @@ Disabling default mappings: >
|
||||||
Changing the prompt message: >
|
Changing the prompt message: >
|
||||||
let g:singlechar_prompt = 'Character to insert (Esc to cancel): '
|
let g:singlechar_prompt = 'Character to insert (Esc to cancel): '
|
||||||
<
|
<
|
||||||
|
Show warning if more than one character is entered (default: enabled): >
|
||||||
|
let g:singlechar_keylen_warning = 1
|
||||||
|
<
|
||||||
|
Customize the warning message (default: 'Only the first character will be taken: {char}'): >
|
||||||
|
let g:singlechar_warning_message = 'Only the first character will be taken: {char}'
|
||||||
|
<
|
||||||
===============================================================================
|
===============================================================================
|
||||||
EXAMPLES *singlechar-examples*
|
EXAMPLES *singlechar-examples*
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,15 @@ endif
|
||||||
|
|
||||||
# Prompt message shown when waiting for character input
|
# Prompt message shown when waiting for character input
|
||||||
if !exists('g:singlechar_prompt')
|
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
|
endif
|
||||||
|
|
||||||
# ------------------------------------------------------------------------------ #
|
# ------------------------------------------------------------------------------ #
|
||||||
|
|
@ -46,8 +54,8 @@ g:last_singlechar_count = 1
|
||||||
nnoremap <Plug>(singlechar-repeat) :call g:RepeatSingleChar()<CR>
|
nnoremap <Plug>(singlechar-repeat) :call g:RepeatSingleChar()<CR>
|
||||||
|
|
||||||
# Direct command implementations
|
# Direct command implementations
|
||||||
command! -count=1 -nargs=0 InsertCharAt singlechar.InsertChar('at', <count>)
|
command! -count=1 -nargs=? InsertCharAt singlechar.InsertChar('at', <count>, <q-args>)
|
||||||
command! -count=1 -nargs=0 InsertCharAfter singlechar.InsertChar('after', <count>)
|
command! -count=1 -nargs=? InsertCharAfter singlechar.InsertChar('after', <count>, <q-args>)
|
||||||
|
|
||||||
# Create default mappings unless disabled
|
# Create default mappings unless disabled
|
||||||
if !exists('g:singlechar_no_mappings')
|
if !exists('g:singlechar_no_mappings')
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue