Skip to content

Commit

Permalink
Move away from using terminfo codes
Browse files Browse the repository at this point in the history
They are meant for full-screen applications, not the command line.
Additionally, when a full-screen app exits, it sets the terminal back to
normal mode, but not all such exits cause line-init to be called. Thus,
when returning from a full-screen-ap, the command line can then
unexpectedly be in normal mode instead of app mode, causing certain
keyboard shortcuts to fail.
  • Loading branch information
marlonrichert committed May 22, 2021
1 parent a172a33 commit 4650999
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 86 deletions.
41 changes: 21 additions & 20 deletions .zshrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/zsh

# The code below sets all of `zsh-autocomplete`'s settings to their default values. To change a
# setting, copy it into your `.zshrc` file.
# The code below sets all of `zsh-autocomplete`'s settings to their default
# values. To change a setting, copy it into your `.zshrc` file.


zstyle ':autocomplete:*' default-context ''
Expand All @@ -22,33 +22,34 @@ zstyle ':autocomplete:*' ignored-input '' # extended glob pattern

# When completions don't fit on screen, show up to this many lines:
zstyle ':autocomplete:*' list-lines 16 # (integer)
# NOTE: The actual amount shown can be less.
# 💡 NOTE: The actual amount shown can be less.

# If any of the following are shown at the same time, list them in the order given:
# If any of the following are shown at the same time, list them in this order:
zstyle ':completion:*:' group-order \
expansions history-words options \
aliases functions builtins reserved-words \
executables local-directories directories suffix-aliases
# NOTE: This is NOT the order in which they are generated.
expansions history-words options \
aliases functions builtins reserved-words \
executables local-directories directories suffix-aliases
# 💡 NOTE: This is NOT the order in which they are generated.

zstyle ':autocomplete:tab:*' insert-unambiguous no
# no: (Shift-)Tab inserts top (bottom) completion.
# yes: Tab first inserts substring common to all listed completions (if any).
zstyle ':autocomplete:*' insert-unambiguous no
# no: Tab inserts the top completion.
# yes: Tab first inserts substring common to all listed completions, if any.

zstyle ':autocomplete:tab:*' widget-style complete-word
# complete-word: (Shift-)Tab inserts top (bottom) completion.
zstyle ':autocomplete:*' widget-style complete-word
# complete-word: (Shift-)Tab inserts the top (bottom) completion.
# menu-complete: Press again to cycle to next (previous) completion.
# menu-select: Same as `menu-complete`, but updates selection in menu.
# NOTE: Can NOT be changed at runtime.
# ⚠️ NOTE: This can NOT be changed at runtime.

zstyle ':autocomplete:tab:*' fzf-completion no
zstyle ':autocomplete:*' fzf-completion no
# no: Tab uses Zsh's completion system only.
# yes: Tab first tries Fzf's completion, then falls back to Zsh's.
# NOTE 1: Can NOT be changed at runtime.
# NOTE 2: Requires that you have installed Fzf's shell extensions.
# ⚠️ NOTE: This can NOT be changed at runtime and requires that you have
# installed Fzf's shell extensions.

# Add a space after these completions:
zstyle ':autocomplete:*' add-space executables aliases functions builtins reserved-words commands
zstyle ':autocomplete:*' add-space \
executables aliases functions builtins reserved-words commands


source /path/to/zsh-autocomplete.plugin.zsh
Expand All @@ -57,11 +58,11 @@ source /path/to/zsh-autocomplete.plugin.zsh
#


bindkey $key[Up] up-line-or-search
bindkey $key[Up] up-line-or-search
# up-line-or-search: Open history menu.
# up-line-or-history: Cycle to previous history line.

bindkey $key[Down] down-line-or-select
bindkey $key[Down] down-line-or-select
# down-line-or-select: Open completion menu.
# down-line-or-history: Cycle to next history line.

Expand Down
77 changes: 24 additions & 53 deletions module/.autocomplete.key
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,33 @@
zmodload -Fa zsh/terminfo b:echoti p:terminfo
builtin autoload -Uz add-zle-hook-widget

if [[ -v terminfo[smkx] ]]; then
.autocomplete.key.set() {
[[ -n $key[$1] ]] &&
return
key[$1]="$terminfo[$3]"
[[ -z $key[$1] ]] &&
key[$1]="$2"
}
else
.autocomplete.key.set() {
[[ -n $key[$1] ]] &&
return
key[$1]="$2"
}
fi
.autocomplete.key.precmd() {
if [[ -v terminfo[rmkx] ]]; then
.autocomplete.key.normal-mode() {
echoti rmkx
return 0
}
add-zle-hook-widget line-init .autocomplete.key.normal-mode
fi
}

if [[ ${(t)key} != association ]]; then
unset key
typeset -gA key=()

# This file can be generated interactively with `autoload -Uz zkbd; zkbd`.
# See http://zsh.sourceforge.net/Doc/Release/User-Contributions.html#Keyboard-Definition
local zkbd=${ZDOTDIR:-$HOME}/.zkbd/$TERM-$VENDOR
[[ -r $zkbd ]] &&
. $zkbd
fi

.autocomplete.key.set Return $'\C-M' cr
.autocomplete.key.set Tab $'\C-I' ht
.autocomplete.key.set Backtab $'\C-[[Z' kcbt
.autocomplete.key.set Up $'\C-[[A' kcuu1
.autocomplete.key.set Down $'\C-[[B' kcud1
.autocomplete.key.set Right $'\C-[[C' kcuf1
.autocomplete.key.set Left $'\C-[[D' kcub1
.autocomplete.key.set End $'\C-[[F' kend
.autocomplete.key.set Home $'\C-[[H' khome
.autocomplete.key.set PageUp $'\C-[[5~' kpp
.autocomplete.key.set PageDown $'\C-[[6~' knp

# These are not defined in `terminfo`.
.autocomplete.key.set Alt $'\C-['
.autocomplete.key.set Control-Space $'\C-@'

.autocomplete.key.precmd() {
if [[ -v terminfo[smkx] ]]; then
.autocomplete.key.line-init() {
echoti smkx
return 0
}
add-zle-hook-widget line-init .autocomplete.key.line-init
if [[ -v terminfo[rmkx] ]]; then
.autocomplete.key.line-finish() {
echoti rmkx
return 0
}
add-zle-hook-widget line-finish .autocomplete.key.line-finish
fi
fi
}
key+=(
Control-Space $'\0'
Tab $'\t'
Return $'\r'
Escape $'\e'
Up $'\e[A'
Down $'\e[B'
Right $'\e[C'
Left $'\e[D'
End $'\e[F'
Home $'\e[H'
Shift-Tab $'\e[Z'
PageUp $'\e[5~'
PageDown $'\e[6~'
)
18 changes: 9 additions & 9 deletions module/.autocomplete.key-binding
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ zmodload -Fa zsh/parameter p:funcstack p:functions

.autocomplete.key-binding.precmd() {
# Work around plugin managers making `key` local.
[[ -z $key[Tab] || -z $key[Backtab] ]] &&
[[ -z $key[Tab] || -z $key[Shift-Tab] ]] &&
.autocomplete.key

local tab_style; zstyle -s :autocomplete:tab: widget-style tab_style ||
Expand All @@ -29,15 +29,15 @@ zmodload -Fa zsh/parameter p:funcstack p:functions
bindkey -M viins $key[Tab] $tab_style
fi
local backtab_style=${tab_style:/menu-complete/reverse-menu-complete}
bindkey -M emacs $key[Backtab] $backtab_style
bindkey -M viins $key[Backtab] $backtab_style
bindkey -M emacs $key[Shift-Tab] $backtab_style
bindkey -M viins $key[Shift-Tab] $backtab_style

if [[ $tab_style == menu-* ]]; then
bindkey -M menuselect $key[Tab] menu-complete
bindkey -M menuselect $key[Backtab] reverse-menu-complete
bindkey -M menuselect $key[Shift-Tab] reverse-menu-complete
else
bindkey -M menuselect $key[Tab] accept-line
bindkey -M menuselect $key[Backtab] send-break
bindkey -M menuselect $key[Shift-Tab] send-break
fi
}

Expand All @@ -51,11 +51,11 @@ bindkey -M viins $key[Up] up-line-or-search
bindkey -M emacs $key[Down] down-line-or-select
bindkey -M viins $key[Down] down-line-or-select

bindkey -M emacs $key[Alt]$key[Up] up-line
bindkey -M vicmd $key[Up] up-line
bindkey -M emacs $key[Escape]$key[Up] up-line
bindkey -M vicmd $key[Up] up-line

bindkey -M emacs $key[Alt]$key[Down] down-line
bindkey -M vicmd $key[Down] down-line
bindkey -M emacs $key[Escape]$key[Down] down-line
bindkey -M vicmd $key[Down] down-line

bindkey -M emacs $key[PageUp] history-search
bindkey -M viins $key[PageUp] history-search
Expand Down
4 changes: 2 additions & 2 deletions widget/.autocomplete.complete-word.completion-widget
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ builtin autoload -Uz .autocomplete.complete-word.post
local +h curcontext=${curcontext:-${WIDGET}:::}
local +h -a comppostfuncs=( .autocomplete.complete-word.post "$comppostfuncs[@]" )
if [[ $WIDGET == *-select ]] ||
( ( [[ "$KEYS" == "$key[Backtab]" ]] ||
( ( [[ "$KEYS" == "$key[Shift-Tab]" ]] ||
zstyle -t ":autocomplete:${(kL)key[(Re)$KEYS]}:" insert-unambiguous
) && [[
$_lastcomp[iprefix]$_lastcomp[prefix]$_lastcomp[suffix]$_lastcomp[isuffix] !=
Expand All @@ -14,7 +14,7 @@ if [[ $WIDGET == *-select ]] ||
_main_complete
elif [[ -n $compstate[old_list] ]]; then
compstate[old_list]=keep
[[ "$KEYS" == "$key[Backtab]" && $_lastcomp[tags] == *all-matches ]] &&
[[ "$KEYS" == "$key[Shift-Tab]" && $_lastcomp[tags] == *all-matches ]] &&
compstate[insert]=all
_main_complete -
fi
Expand Down
4 changes: 2 additions & 2 deletions widget/.autocomplete.complete-word.post
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
unset curcontext
compstate[list]=

if ( [[ $KEYS == $key[Backtab] ]] ||
if ( [[ $KEYS == $key[Shift-Tab] ]] ||
zstyle -t ":autocomplete:${(kL)key[(Re)$KEYS]}:" insert-unambiguous
) && [[ $IPREFIX$PREFIX$SUFFIX$ISUFFIX != *${compstate[unambiguous]:#?}* ]]; then
compstate[insert]=${${(M)WIDGET:#*menu-*}:+automenu-}unambiguous
Expand All @@ -18,7 +18,7 @@ if [[ $WIDGET == *menu-select ]]; then
MENUSELECT=0
fi

if [[ $KEYS == $key[Backtab] ]]; then
if [[ $KEYS == $key[Shift-Tab] ]]; then
compstate[insert]+=0
else
compstate[insert]+=1
Expand Down

0 comments on commit 4650999

Please sign in to comment.