diff options
Diffstat (limited to 'modules/by-name/zs/zsh/config/custom_cursor.zsh')
-rw-r--r-- | modules/by-name/zs/zsh/config/custom_cursor.zsh | 81 |
1 files changed, 46 insertions, 35 deletions
diff --git a/modules/by-name/zs/zsh/config/custom_cursor.zsh b/modules/by-name/zs/zsh/config/custom_cursor.zsh index 37390c1c..071bb5fe 100644 --- a/modules/by-name/zs/zsh/config/custom_cursor.zsh +++ b/modules/by-name/zs/zsh/config/custom_cursor.zsh @@ -1,42 +1,53 @@ #!/usr/bin/env zsh -# Change cursor shape for different vi modes. -function zle-keymap-select { - if [[ ${KEYMAP} == vicmd ]] || - [[ $1 = 'block' ]]; then - echo -ne '\e[1 q' - elif [[ ${KEYMAP} == main ]] || - [[ ${KEYMAP} == viins ]] || - [[ ${KEYMAP} = '' ]] || - [[ $1 = 'beam' ]]; then - echo -ne '\e[5 q' - fi -} -zle -N zle-keymap-select +autoload -U add-zsh-hook +autoload -U add-zle-hook-widget + +_cursor_beam() { echo -ne "\\033[5 q" } +_cursor_block() { echo -ne "\\033[1 q" } -# ci", ci', ci`, di", etc -autoload -U select-quoted -zle -N select-quoted -for m in visual viopp; do - for c in {a,i}{\',\",\`}; do - bindkey -M "$m" "$c" select-quoted - done -done +# Change cursor shape for different vi modes. +# From `ZSHZLE (1)`: +# > Executed every time the keymap changes, i.e. the special parameter KEYMAP is set to a different value, +# > while the line editor is active. Initialising the keymap when the line editor starts does +# > not cause the widget to be called. +# > +# > The value $KEYMAP within the function reflects the new keymap. The old keymap is passed as the sole argument. +# > +# > This can be used for detecting switches between the vi command (vicmd) and insert (usually main) keymaps. +_cursor_zle-keymap-select() { + : keymap select -# ci{, ci(, ci<, di{, etc -autoload -U select-bracketed -zle -N select-bracketed -for m in visual viopp; do - for c in {a,i}${(s..)^:-'()[]{}<>bB'}; do - bindkey -M $m $c select-bracketed - done -done + case "$KEYMAP" in + "vicmd" | "block") + _cursor_block + ;; + "main" | "viins" | "" | "beam") + _cursor_beam + ;; + esac +} +add-zle-hook-widget keymap-select _cursor_zle-keymap-select -zle-line-init() { - zle -K viins # initiate `vi insert` as keymap (can be removed if `bindkey -V` has been set elsewhere) - echo -ne "\e[5 q" +# From `ZSHZLE(1)`: +# > Executed every time the line editor is started to read a new line of input. +# > The following example puts the line editor into vi command mode when it starts up. +# > +# > zle-line-init() { zle -K vicmd; } +# > zle -N zle-line-init +# > +# > (The command inside the function sets the keymap directly; it is equivalent to zle vi-cmd-mode.) +_cursor_zle-line-init() { + : zle line init + _cursor_beam +} +# > This is similar to zle-line-init but is executed every time the line editor has finished reading a line of input. +_cursor_zle-line-finish() { + : zle line finish + _cursor_block } -zle -N zle-line-init +add-zle-hook-widget line-init _cursor_zle-line-init +add-zle-hook-widget line-finish _cursor_zle-line-finish -echo -ne '\e[5 q' # Use beam shape cursor on startup. -precmd() { echo -ne '\e[5 q' ;} # Use beam shape cursor for each new prompt. +# Use beam shape cursor on startup. +_cursor_beam |