Skip to content

Commit 47ef19f

Browse files
committed
[refactor]: Update codebase
1 parent 912a96e commit 47ef19f

File tree

7 files changed

+507
-9
lines changed

7 files changed

+507
-9
lines changed

functions/zew

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
2+
# vim: ft=zsh sw=2 ts=2 et
3+
#
4+
# Displays help on Zew provided actions. Lines are 72 characters long
5+
6+
autoload colors; colors
7+
8+
local h1="$fg_bold[magenta]"
9+
local h2="$fg_bold[green]"
10+
local h3="$fg_bold[blue]"
11+
local h4="$fg_bold[yellow]"
12+
local rst="$reset_color"
13+
14+
local LESS_bkp="$LESS"
15+
export LESS="-iRX"
16+
17+
less <<<"
18+
${h1}Key Bindings${rst}
19+
20+
Zew provides organized key bindings for various command line editing
21+
features, and also provides some of the features (not existing in all
22+
Zsh versions, or having limitations or bugs). Original features are used
23+
to make user not depart from mainstream Zsh.
24+
25+
The key bindings are below. Original features are marked by
26+
blue color set on the key shortcut:
27+
28+
1. ${h2}Alt-w${rst} - delete a ${h4}shell word${rst} (what's a shell word is explained in
29+
following section)
30+
2. ${h2}Alt-t${rst} - transpose (swap) ${h4}shell words${rst}
31+
3. ${h3}Alt-m${rst} - copy previous ${h4}shell word${rst}, or word before that, etc. when
32+
used multiple times
33+
4. ${h3}Alt-M${rst} - just copy previous ${h4}shell word${rst} without iterating to
34+
previous ones
35+
5. ${h3}Alt-.${rst} - copy last ${h4}shell word${rst} from previous line, or line before
36+
that, etc. when used multiple times; can be combined with
37+
${h3}Alt-m${rst}
38+
6. ${h3}Ctrl-W${rst} - delete word according to configured ${h4}word style${rst} (what's the
39+
style is explained in following section)
40+
7. ${h3}Alt-r${rst} - transpose (swap) words according to configured ${h4}word style${rst}
41+
(cursor needs to be placed on beginning of word to swap)
42+
8. ${h3}Alt-/${rst} - complete ${h4}some word${rst} from history (explained in next section)
43+
9. ${h2}Alt-h/H${rst} - complete ${h4}shell word${rst} from history (custom version)
44+
10. ${h3}Ctrl-J${rst} - break line
45+
11. ${h3}Ctrl-_${rst} - undo
46+
47+
48+
${h1}Definitions${rst}
49+
50+
A ${h4}shell word${rst} is a text that Zsh would see as single segment. For example
51+
\$(( i + 1 )) is a single shell word.
52+
53+
A ${h4}word style${rst} defines a way Zsh recognizes segments (words) of text in
54+
commands that want to use the style information. The style can be
55+
configured in zew.conf to be one of:
56+
57+
- bash words are built up of alphanumeric characters only
58+
- normal as in normal shell operation: word characters are
59+
alphanumeric characters plus any characters present in the
60+
string given by the parameter \$WORDCHARS
61+
- shell words are complete shell command arguments, possibly
62+
including complete quoted strings, or any tokens special to
63+
the shell
64+
- whitespace words are any set of characters delimited by whitespace
65+
- default restore the default settings; this is the same as 'normal'
66+
with default \$WORDCHARS
67+
68+
${h4}Some word${rst} is in general a sophisticated word, but not a shell word,
69+
because of limitations in Zsh history word completion. ${h4}Some word${rst} is
70+
rather not build from special characters, it works well for normal
71+
characters.
72+
"
73+
74+
export LESS="$LESS_bkp"
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
2+
# vim: ft=zsh sw=2 ts=2 et
3+
#
4+
# Copyright (c) 2016 Daniel Shahaf
5+
#
6+
# MIT License
7+
# -----------
8+
#
9+
# Permission is hereby granted, free of charge, to any person obtaining a copy
10+
# of this software and associated documentation files (the "Software"), to deal
11+
# in the Software without restriction, including without limitation the rights
12+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
# copies of the Software, and to permit persons to whom the Software is
14+
# furnished to do so, subject to the following conditions:
15+
#
16+
# The above copyright notice and this permission notice shall be included in
17+
# all copies or substantial portions of the Software.
18+
#
19+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
# THE SOFTWARE.
26+
#
27+
# Modifications Copyright (c) 2016 Sebastian Gniazdowski
28+
# Modifications Copyright (c) 2021 Salvydas Lukosius
29+
30+
local MATCH; integer MBEGIN MEND
31+
local -a match mbegin mend
32+
integer start end_of_word end_of_cut=$CURSOR
33+
34+
# Walk backwards to an end-of-word
35+
[[ $LBUFFER =~ '[[:space:]]*$' ]] || : # sets $MATCH
36+
(( end_of_word = CURSOR - $#MATCH ))
37+
38+
# Find the start of the shell word ending at $BUFFER[end_of_word]
39+
() {
40+
local l="$PREBUFFER$LBUFFER[1,end_of_word]"
41+
local -a a
42+
if [[ -o interactive_comments ]]; then
43+
a=( ${(zZ+c+)l} )
44+
else
45+
a=( ${(z)l} )
46+
fi
47+
(( start = end_of_word - ${#a[-1]} + 1 ))
48+
}
49+
50+
# Standard kill-widget behavior
51+
autoload -Uz is-at-least
52+
if is-at-least 5.2; then
53+
zle -f 'kill'
54+
fi
55+
56+
if [[ $LASTWIDGET == *'kill'* ]]; then
57+
CUTBUFFER=${BUFFER[start,end_of_cut]}$CUTBUFFER
58+
else
59+
zle copy-region-as-kill -- "${BUFFER[start,end_of_cut]}"
60+
fi
61+
62+
# Delete the last shell word from $LBUFFER
63+
LBUFFER[start,end_of_cut]=""
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
2+
# vim: ft=zsh sw=2 ts=2 et
3+
4+
builtin emulate -L zsh ${=${options[xtrace]:#off}:+-o xtrace}
5+
builtin setopt extended_glob warn_create_global typeset_silent no_short_loops rc_quotes no_auto_pushd
6+
7+
# When an error, then no cursor keys bindings
8+
zmodload zsh/terminfo 2>/dev/null
9+
zmodload zsh/termcap 2>/dev/null
10+
11+
# Prepare output variables for zew-process-buffer
12+
local ZEW_PB_WORDS ZEW_PB_WORDS_BEGINNINGS ZEW_PB_SPACES
13+
local ZEW_PB_SELECTED_WORD ZEW_PB_LEFT ZEW_PB_RIGHT
14+
15+
local MATCH; integer MBEGIN MEND
16+
local -a match mbegin mend
17+
18+
typeset -g __zew_hcw_index __zew_hcw_left __zew_hcw_right __zew_hcw_call_count
19+
typeset -g __zew_hcw_widget_name __zew_hcw_restart __zew_hcw_finished
20+
typeset -gaU __zew_hcw_found
21+
22+
(( __zew_hcw_call_count ++ ))
23+
24+
_zhcw_main() {
25+
26+
autoload zew-process-buffer
27+
zew-process-buffer "$BUFFER" "$CURSOR"
28+
29+
# First call or restart?
30+
if [[ "$__zew_hcw_call_count" -le 1 || "$__zew_hcw_restart" = "1" ]]; then
31+
# '0' will get changed into $to_display limit
32+
[[ "$WIDGET" != *-backwards ]] && __zew_hcw_index="1" || __zew_hcw_index="0"
33+
__zew_hcw_widget_name="${WIDGET%-backwards}"
34+
__zew_hcw_left="$ZEW_PB_LEFT"
35+
__zew_hcw_right="$ZEW_PB_RIGHT"
36+
__zew_hcw_found=( )
37+
__zew_hcw_finished="0"
38+
__zew_hcw_restart="0"
39+
else
40+
# Consecutive call
41+
[[ "$WIDGET" != *-backwards ]] && (( __zew_hcw_index ++ )) || (( __zew_hcw_index -- ))
42+
fi
43+
44+
# Find history words matching $left ... $right
45+
if [ "$#__zew_hcw_found" -eq "0" ]; then
46+
repeat 1; do
47+
__zew_hcw_found=( "${(@M)historywords:#(#i)$__zew_hcw_left*$__zew_hcw_right}" )
48+
done
49+
# The first result should be always $__zew_hcw_left$__zew_hcw_right
50+
if [ "$__zew_hcw_found[1]" != "$__zew_hcw_left$__zew_hcw_right" ]; then
51+
__zew_hcw_found=( "$__zew_hcw_left$__zew_hcw_right" "$__zew_hcw_found[@]" )
52+
fi
53+
fi
54+
55+
if [ "$#__zew_hcw_found" -le "0" ]; then
56+
zle -M "No matches found"
57+
return 0
58+
fi
59+
60+
# Pagination, index value guards
61+
integer page_size=$(( LINES / 2 ))
62+
integer max_index="$#__zew_hcw_found"
63+
[ "$page_size" -gt "$max_index" ] && page_size="$max_index"
64+
[ "$__zew_hcw_index" -le 0 ] && __zew_hcw_index="$max_index"
65+
[ "$__zew_hcw_index" -gt "$max_index" ] && __zew_hcw_index=1
66+
integer page_start_idx=$(( ((__zew_hcw_index-1)/page_size)*page_size+1 ))
67+
integer on_page_idx=$(( (__zew_hcw_index-1) % page_size + 1 ))
68+
69+
# Display matches
70+
typeset -a disp_list
71+
disp_list=( "${(@)__zew_hcw_found[page_start_idx,page_start_idx+page_size-1]}" )
72+
73+
# Add two spaces before every element
74+
disp_list=( "${(@)disp_list/(#m)*/ ${MATCH}}" )
75+
76+
# Add > before active element
77+
local entry="${disp_list[on_page_idx]}"
78+
entry[1]='>'
79+
disp_list[on_page_idx]="$entry"
80+
81+
zle -M -- \
82+
"Searching for '${__zew_hcw_left}_${__zew_hcw_right}'. "\
83+
"Element #$__zew_hcw_index of $max_index"$'\n'"${(F)disp_list}"
84+
85+
# Regenerate command line
86+
local buf=""
87+
integer nwords="${#ZEW_PB_WORDS}"
88+
for (( i=1; i<=nwords; i++ )); do
89+
if [ "$i" = "$ZEW_PB_SELECTED_WORD" ]; then
90+
buf+="${ZEW_PB_SPACES[i]}${__zew_hcw_found[__zew_hcw_index]}"
91+
else
92+
buf+="${ZEW_PB_SPACES[i]}${ZEW_PB_WORDS[i]}"
93+
fi
94+
done
95+
96+
if [[ "$nwords" = "0" && "$ZEW_PB_SELECTED_WORD" = "0" ]]; then
97+
buf+="${__zew_hcw_found[__zew_hcw_index]}"
98+
fi
99+
100+
# Add trailing spaces
101+
buf+="$ZEW_PB_SPACES[i]"
102+
103+
# Set command line
104+
BUFFER="$buf"
105+
106+
}
107+
108+
_zhcw_self_insert() {
109+
LBUFFER+="${KEYS[-1]}"
110+
__zew_hcw_restart="1"
111+
_zhcw_main
112+
}
113+
114+
_zhcw_backward_delete_char() {
115+
LBUFFER="${LBUFFER%?}"
116+
__zew_hcw_restart="1"
117+
_zhcw_main
118+
}
119+
120+
_zhcw_delete_char() {
121+
RBUFFER="${RBUFFER#?}"
122+
__zew_hcw_restart="1"
123+
_zhcw_main
124+
}
125+
126+
_zhcw_main
127+
128+
if [ "$__zew_hcw_call_count" -eq "1" ]; then
129+
# Make the zhcw keymap a copy of the current main
130+
bindkey -N zhcw emacs
131+
local down_widget="${WIDGET%-backwards}"
132+
local up_widget="${down_widget}-backwards"
133+
# Manual, termcap, terminfo
134+
bindkey -M zhcw '^[OA' "$up_widget"
135+
bindkey -M zhcw '^[OB' "$down_widget"
136+
bindkey -M zhcw '^[[A' "$up_widget"
137+
bindkey -M zhcw '^[[B' "$down_widget"
138+
[ -n "$termcap[ku]" ] && bindkey -M zhcw "$termcap[ku]" "$up_widget"
139+
[ -n "$termcap[kd]" ] && bindkey -M zhcw "$termcap[kd]" "$down_widget"
140+
[ -n "$termcap[kD]" ] && bindkey -M zhcw "$termcap[kD]" delete-char
141+
[ -n "$terminfo[kcuu1]" ] && bindkey -M zhcw "$terminfo[kcuu1]" "$up_widget"
142+
[ -n "$terminfo[kcud1]" ] && bindkey -M zhcw "$terminfo[kcud1]" "$down_widget"
143+
[ -n "$terminfo[kdch1]" ] && bindkey -M zhcw "$terminfo[kdch1]" delete-char
144+
# Needed for Fedora 23, zsh-5.1.1
145+
bindkey -M zhcw ' ' self-insert
146+
# Substitute self-insert, backward-delete-char, delete-char
147+
zle -A self-insert saved-self-insert
148+
zle -A backward-delete-char saved-backward-delete-char
149+
zle -A delete-char saved-delete-char
150+
zle -N self-insert _zhcw_self_insert
151+
zle -N backward-delete-char _zhcw_backward_delete_char
152+
zle -N delete-char _zhcw_delete_char
153+
zle recursive-edit -K zhcw
154+
zle -M ""
155+
zle -A saved-self-insert self-insert
156+
zle -A saved-backward-delete-char backward-delete-char
157+
zle -A saved-delete-char delete-char
158+
zle -D saved-self-insert saved-backward-delete-char saved-delete-char
159+
# Full initialization at next call
160+
__zew_hcw_call_count="0"
161+
fi

0 commit comments

Comments
 (0)