-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: Add vi-mode plugin #272
Draft
alxbl
wants to merge
9
commits into
geometry-zsh:mnml
Choose a base branch
from
alxbl:vi-mode
base: mnml
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f5b9d3e
feat: Added vi-mode indicator. Resolves #165
alxbl 82f3153
Merge remote-tracking branch 'upstream/mnml' into vi-mode
alxbl 7e39b36
feat: Added ZLE widget handling for vi-mode indicator.
alxbl 08d81dc
fix: geometry_vi.zsh -> geometry_vi
alxbl ad2bdb5
doc: Added geometry_vi documentation.
alxbl 6224a36
doc: add geometry_vi to README.
alxbl 26bd850
Merge branch 'mnml' of https://github.com/geometry-zsh/geometry into …
alxbl 8979280
fix(vi): Added gate logic for widget initialization.
alxbl 2f649c9
fix: bad variable name `cur_widget`.
alxbl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# geometry_vi - A geometry plugin to display the ZLE vi-mode state. | ||
|
||
geometry_vi() { | ||
# Required for RPROMPT to render properly after submitting a command. | ||
geometry::vi-get-mode | ||
} | ||
|
||
# Re-render the prompt. | ||
function geometry::vi-update-prompt() { | ||
imode=$(geometry::vi-insert-mode) | ||
nmode=$(geometry::vi-normal-mode) | ||
if [[ $KEYMAP == vicmd ]]; then | ||
PROMPT=${PROMPT//$imode/$nmode} | ||
RPROMPT=${RPROMPT//$imode/$nmode} | ||
else | ||
PROMPT=${PROMPT//$nmode/$imode} | ||
RPROMPT=${RPROMPT//$nmode/$imode} | ||
fi | ||
} | ||
|
||
# Render normal mode. | ||
function geometry::vi-normal-mode() { | ||
ansi ${GEOMETRY_VI_NORMAL_COLOR:-white} ${GEOMETRY_VI_NORMAL_MODE:-"<NORMAL>"} | ||
} | ||
|
||
# Render insert mode. | ||
function geometry::vi-insert-mode() { | ||
ansi ${GEOMETRY_VI_INSERT_COLOR:-yellow} ${GEOMETRY_VI_INSERT_MODE:-"<INSERT>"} | ||
} | ||
|
||
# Return the currently active mode. | ||
function geometry::vi-get-mode() { | ||
imode=$(geometry::vi-insert-mode) | ||
nmode=$(geometry::vi-normal-mode) | ||
[[ $KEYMAP == vicmd ]] && echo $nmode || echo $imode | ||
} | ||
|
||
# ZLE widget to trigger prompt updates. | ||
function geometry::vi-draw-mode { | ||
keymap=$(geometry::vi-get-mode) | ||
geometry::vi-update-prompt | ||
zle reset-prompt | ||
} | ||
|
||
|
||
# Skip initialization code if geometry_vi is not configured for rendering. | ||
[[ ${GEOMETRY_RPROMPT[(ie)geometry_vi]} -gt ${#GEOMETRY_RPROMPT} ]] && [[ ${GEOMETRY_PROMPT[(ie)geometry_vi]} -gt ${#GEOMETRY_PROMPT} ]] && return | ||
|
||
# Bind the widgets required to perform proper prompt updates. | ||
# Adapted from https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/zsh-syntax-highlighting.zsh#L295-L359 | ||
local prefix="geometry_vi" | ||
for w (zle-keymap-select zle-line-pre-redraw zle-line-init zle-line-finish); do | ||
case ${widgets[$w]:-""} in | ||
# Already Bound to geometry: Do nothing. | ||
user:geometry-vi-draw) ;; | ||
|
||
# Existing user widget: Chain a call to it. | ||
user:*) zle -N $prefix-$w ${widgets[$w]#*:} | ||
# Dynamically create a function to call the previous widget. | ||
eval "geometry::${(q)prefix}-${(q)w}() { geometry::vi-draw-mode; builtin zle ${(q)prefix}-${(q)w} -- \"\$@\" }" | ||
zle -N $w geometry::$prefix-$w;; | ||
|
||
# Completion widgets: Ignore. | ||
completion:*) ;; | ||
|
||
# Builtin widget: Chain a call to ".widget". | ||
# Eval is required to get a closure ( | ||
builtin) eval "geometry::${(q)prefix}-${(q)w}() { geometry::vi-draw-mode; builtin zle .${(q)w} -- \"\$@\" }" | ||
zle -N $w _zsh_highlight_widget_$prefix-$w;; | ||
|
||
# Unbound widget: Bind directly. | ||
*) | ||
if [[ $w == zle-* ]] && (( ! ${+widgets[$w]} )); then | ||
zle -N $w geometry::vi-draw-mode | ||
else | ||
# Default: unhandled case. | ||
print -r -- >&2 "geometry::geometry_vi: unhandled ZLE widget ${(qq)w}" | ||
fi | ||
esac | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was trying to find a clever way to pass the prompt as an indirect variable through
${(P)varname}
but couldn't find a way to assign back, so for now this is the cleanest way to do it.