98762faa0762e1f0ede05149c1f6b3a9db93e074

Author
Robert Estelle <robertestelle@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

fix(vi-mode): control cursor, restore and use visual mode and speed up mode changes (#8004)

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/vi-mode/vi-mode.plugin.zsh b/plugins/vi-mode/vi-mode.plugin.zsh
  2index c91ba05ba0eb745bf64acb83c07480cdbc51a675..7ff826ea314af8943401b6f70f747199304f7eef 100644
  3--- a/plugins/vi-mode/vi-mode.plugin.zsh
  4+++ b/plugins/vi-mode/vi-mode.plugin.zsh
  5@@ -1,33 +1,61 @@
  6+# Control whether to force a redraw on each mode change.
  7+#
  8+# Resetting the prompt on every mode change can cause lag when switching modes.
  9+# This is especially true if the prompt does things like checking git status.
 10+#
 11+# Set to "true" to force the prompt to reset on each mode change.
 12+# Set to "false" to force the prompt *not* to reset on each mode change.
 13+#
 14+# (The default is not to reset, unless we're showing the mode in RPS1).
 15+typeset -g VI_MODE_RESET_PROMPT_ON_MODE_CHANGE
 16+typeset -g VI_KEYMAP=main
 17+
 18+function _vi-mode-set-cursor-shape-for-keymap() {
 19+  # https://vt100.net/docs/vt510-rm/DECSCUSR
 20+  local _shape=0
 21+  case "${1:-${VI_KEYMAP:-main}}" in
 22+    main)    _shape=6 ;; # vi insert: line
 23+    viins)   _shape=6 ;; # vi insert: line
 24+    isearch) _shape=6 ;; # inc search: line
 25+    command) _shape=6 ;; # read a command name
 26+    vicmd)   _shape=2 ;; # vi cmd: block
 27+    visual)  _shape=2 ;; # vi visual mode: block
 28+    viopp)   _shape=0 ;; # vi operation pending: blinking block
 29+    *)       _shape=0 ;;
 30+  esac
 31+  printf $'\e[%d q' "${_shape}"
 32+}
 33+
 34 # Updates editor information when the keymap changes.
 35 function zle-keymap-select() {
 36   # update keymap variable for the prompt
 37-  VI_KEYMAP=$KEYMAP
 38+  typeset -g VI_KEYMAP=$KEYMAP
 39 
 40-  zle reset-prompt
 41-  zle -R
 42+  if [ "${VI_MODE_RESET_PROMPT_ON_MODE_CHANGE:-}" = true ]; then
 43+    zle reset-prompt
 44+    zle -R
 45+  fi
 46+  _vi-mode-set-cursor-shape-for-keymap "${VI_KEYMAP}"
 47 }
 48-
 49 zle -N zle-keymap-select
 50 
 51-function vi-accept-line() {
 52-  VI_KEYMAP=main
 53-  zle accept-line
 54+# These "echoti" statements were originally set in lib/key-bindings.zsh
 55+# Not sure the best way to extend without overriding.
 56+function zle-line-init() {
 57+  typeset -g VI_KEYMAP=main
 58+  (( ! ${+terminfo[smkx]} )) || echoti smkx
 59+  _vi-mode-set-cursor-shape-for-keymap "${VI_KEYMAP}"
 60 }
 61+zle -N zle-line-init
 62 
 63-zle -N vi-accept-line
 64-
 65+function zle-line-finish() {
 66+  (( ! ${+terminfo[rmkx]} )) || echoti rmkx
 67+  _vi-mode-set-cursor-shape-for-keymap default
 68+}
 69+zle -N zle-line-finish
 70 
 71 bindkey -v
 72 
 73-# use custom accept-line widget to update $VI_KEYMAP
 74-bindkey -M vicmd '^J' vi-accept-line
 75-bindkey -M vicmd '^M' vi-accept-line
 76-
 77-# allow v to edit the command line (standard behaviour)
 78-autoload -Uz edit-command-line
 79-zle -N edit-command-line
 80-bindkey -M vicmd 'v' edit-command-line
 81-
 82 # allow ctrl-p, ctrl-n for navigate history (standard behaviour)
 83 bindkey '^P' up-history
 84 bindkey '^N' down-history
 85@@ -45,12 +73,95 @@ bindkey '^s' history-incremental-search-forward
 86 bindkey '^a' beginning-of-line
 87 bindkey '^e' end-of-line
 88 
 89+if [[ "${terminfo[kpp]}" != "" ]]; then
 90+  bindkey "${terminfo[kpp]}" up-line-or-history       # [PageUp] - Up a line of history
 91+fi
 92+if [[ "${terminfo[knp]}" != "" ]]; then
 93+  bindkey "${terminfo[knp]}" down-line-or-history     # [PageDown] - Down a line of history
 94+fi
 95+
 96+# start typing + [Up-Arrow] - fuzzy find history forward
 97+if [[ "${terminfo[kcuu1]}" != "" ]]; then
 98+  autoload -U up-line-or-beginning-search
 99+  zle -N up-line-or-beginning-search
100+  bindkey "${terminfo[kcuu1]}" up-line-or-beginning-search
101+fi
102+# start typing + [Down-Arrow] - fuzzy find history backward
103+if [[ "${terminfo[kcud1]}" != "" ]]; then
104+  autoload -U down-line-or-beginning-search