]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/vi-mode/vi-mode.plugin.zsh
initial
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / vi-mode / vi-mode.plugin.zsh
1 # Control whether to force a redraw on each mode change.
2 #
3 # Resetting the prompt on every mode change can cause lag when switching modes.
4 # This is especially true if the prompt does things like checking git status.
5 #
6 # Set to "true" to force the prompt to reset on each mode change.
7 # Unset or set to any other value to do the opposite.
8 #
9 # The default is not to reset, unless we're showing the mode in RPS1.
10 typeset -g VI_MODE_RESET_PROMPT_ON_MODE_CHANGE
11 # Control whether to change the cursor style on mode change.
12 #
13 # Set to "true" to change the cursor on each mode change.
14 # Unset or set to any other value to do the opposite.
15 typeset -g VI_MODE_SET_CURSOR
16
17 typeset -g VI_KEYMAP=main
18
19 function _vi-mode-set-cursor-shape-for-keymap() {
20   [[ "$VI_MODE_SET_CURSOR" = true ]] || return
21
22   # https://vt100.net/docs/vt510-rm/DECSCUSR
23   local _shape=0
24   case "${1:-${VI_KEYMAP:-main}}" in
25     main)    _shape=6 ;; # vi insert: line
26     viins)   _shape=6 ;; # vi insert: line
27     isearch) _shape=6 ;; # inc search: line
28     command) _shape=6 ;; # read a command name
29     vicmd)   _shape=2 ;; # vi cmd: block
30     visual)  _shape=2 ;; # vi visual mode: block
31     viopp)   _shape=0 ;; # vi operation pending: blinking block
32     *)       _shape=0 ;;
33   esac
34   printf $'\e[%d q' "${_shape}"
35 }
36
37 # Updates editor information when the keymap changes.
38 function zle-keymap-select() {
39   # update keymap variable for the prompt
40   typeset -g VI_KEYMAP=$KEYMAP
41
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 zle -N zle-keymap-select
49
50 # These "echoti" statements were originally set in lib/key-bindings.zsh
51 # Not sure the best way to extend without overriding.
52 function zle-line-init() {
53   local prev_vi_keymap
54   prev_vi_keymap="${VI_KEYMAP:-}"
55   typeset -g VI_KEYMAP=main
56   [[ "$prev_vi_keymap" != 'main' ]] && [[ "${VI_MODE_RESET_PROMPT_ON_MODE_CHANGE:-}" = true ]] && zle reset-prompt
57   (( ! ${+terminfo[smkx]} )) || echoti smkx
58   _vi-mode-set-cursor-shape-for-keymap "${VI_KEYMAP}"
59 }
60 zle -N zle-line-init
61
62 function zle-line-finish() {
63   typeset -g VI_KEYMAP=main
64   (( ! ${+terminfo[rmkx]} )) || echoti rmkx
65   _vi-mode-set-cursor-shape-for-keymap default
66 }
67 zle -N zle-line-finish
68
69 bindkey -v
70
71 # allow vv to edit the command line (standard behaviour)
72 autoload -Uz edit-command-line
73 zle -N edit-command-line
74 bindkey -M vicmd 'vv' edit-command-line
75
76 # allow ctrl-p, ctrl-n for navigate history (standard behaviour)
77 bindkey '^P' up-history
78 bindkey '^N' down-history
79
80 # allow ctrl-h, ctrl-w, ctrl-? for char and word deletion (standard behaviour)
81 bindkey '^?' backward-delete-char
82 bindkey '^h' backward-delete-char
83 bindkey '^w' backward-kill-word
84
85 # allow ctrl-r and ctrl-s to search the history
86 bindkey '^r' history-incremental-search-backward
87 bindkey '^s' history-incremental-search-forward
88
89 # allow ctrl-a and ctrl-e to move to beginning/end of line
90 bindkey '^a' beginning-of-line
91 bindkey '^e' end-of-line
92
93 function wrap_clipboard_widgets() {
94   # NB: Assume we are the first wrapper and that we only wrap native widgets
95   # See zsh-autosuggestions.zsh for a more generic and more robust wrapper
96   local verb="$1"
97   shift
98
99   local widget
100   local wrapped_name
101   for widget in "$@"; do
102     wrapped_name="_zsh-vi-${verb}-${widget}"
103     if [ "${verb}" = copy ]; then
104       eval "
105         function ${wrapped_name}() {
106           zle .${widget}
107           printf %s \"\${CUTBUFFER}\" | clipcopy 2>/dev/null || true
108         }
109       "
110     else
111       eval "
112         function ${wrapped_name}() {
113           CUTBUFFER=\"\$(clippaste 2>/dev/null || echo \$CUTBUFFER)\"
114           zle .${widget}
115         }
116       "
117     fi
118     zle -N "${widget}" "${wrapped_name}"
119   done
120 }
121
122 wrap_clipboard_widgets copy vi-yank vi-yank-eol vi-backward-kill-word vi-change-whole-line vi-delete vi-delete-char
123 wrap_clipboard_widgets paste vi-put-{before,after}
124 unfunction wrap_clipboard_widgets
125
126 # if mode indicator wasn't setup by theme, define default, we'll leave INSERT_MODE_INDICATOR empty by default
127 if [[ -z "$MODE_INDICATOR" ]]; then
128   MODE_INDICATOR='%B%F{red}<%b<<%f'
129 fi
130
131 function vi_mode_prompt_info() {
132   # If we're using the prompt to display mode info, and we haven't explicitly
133   # disabled "reset prompt on mode change", then set it here.
134   #
135   # We do that here instead of the `if` statement below because the user may
136   # set RPS1/RPROMPT to something else in their custom config.
137   : "${VI_MODE_RESET_PROMPT_ON_MODE_CHANGE:=true}"
138
139   echo "${${VI_KEYMAP/vicmd/$MODE_INDICATOR}/(main|viins)/$INSERT_MODE_INDICATOR}"
140 }
141
142 # define right prompt, if it wasn't defined by a theme
143 if [[ -z "$RPS1" && -z "$RPROMPT" ]]; then
144   RPS1='$(vi_mode_prompt_info)'
145 fi