Debian Codegraph @

shell/key-bindings.bash

raw
1 # ____ ____
2 # / __/___ / __/
3 # / /_/_ / / /_
4 # / __/ / /_/ __/
5 # /_/ /___/_/ key-bindings.bash
6 #
7 # - $FZF_CTRL_T_COMMAND
8 # - $FZF_CTRL_T_OPTS
9 # - $FZF_CTRL_R_COMMAND
10 # - $FZF_CTRL_R_OPTS
11 # - $FZF_ALT_C_COMMAND
12 # - $FZF_ALT_C_OPTS
13
14 if [[ $- =~ i ]]; then
15
16
17 # Key bindings
18 # ------------
19
20 #----BEGIN shfmt
21 #----BEGIN INCLUDE common.sh
22 # NOTE: Do not directly edit this section, which is copied from "common.sh".
23 # To modify it, one can edit "common.sh" and run "./update.sh" to apply
24 # the changes. See code comments in "common.sh" for the implementation details.
25
26 __fzf_defaults() {
27 builtin printf '%s\n' "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1"
28 command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null
29 builtin printf '%s\n' "${FZF_DEFAULT_OPTS-} $2"
30 }
31
32 __fzf_exec_awk() {
33 if [[ -z ${__fzf_awk-} ]]; then
34 __fzf_awk=awk
35 if [[ $OSTYPE == solaris* && -x /usr/xpg4/bin/awk ]]; then
36 __fzf_awk=/usr/xpg4/bin/awk
37 elif command -v mawk > /dev/null 2>&1; then
38 local n x y z d
39 IFS=' .' read -r n x y z d <<< $(command mawk -W version 2> /dev/null)
40 [[ $n == mawk ]] &&
41 (((x * 1000 + y) * 1000 + z >= 1003004)) 2> /dev/null &&
42 ((d >= 20230302)) 2> /dev/null &&
43 __fzf_awk=mawk
44 fi
45 fi
46 LC_ALL=C exec "$__fzf_awk" "$@"
47 }
48 #----END INCLUDE
49
50 __fzf_select__() {
51 FZF_DEFAULT_COMMAND=${FZF_CTRL_T_COMMAND:-} \
52 FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse --walker=file,dir,follow,hidden --scheme=path" "${FZF_CTRL_T_OPTS-} -m") \
53 FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd) "$@" |
54 while read -r item; do
55 printf '%q ' "$item" # escape special chars
56 done
57 }
58
59 __fzfcmd() {
60 [[ -n ${TMUX_PANE-} ]] && { [[ ${FZF_TMUX:-0} != 0 ]] || [[ -n ${FZF_TMUX_OPTS-} ]]; } &&
61 echo "fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- " || echo "fzf"
62 }
63
64 fzf-file-widget() {
65 local selected="$(__fzf_select__ "$@")"
66 READLINE_LINE="${READLINE_LINE:0:READLINE_POINT}$selected${READLINE_LINE:READLINE_POINT}"
67 READLINE_POINT=$((READLINE_POINT + ${#selected}))
68 }
69
70 __fzf_cd__() {
71 local dir
72 dir=$(
73 FZF_DEFAULT_COMMAND=${FZF_ALT_C_COMMAND:-} \
74 FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse --walker=dir,follow,hidden --scheme=path" "${FZF_ALT_C_OPTS-} +m") \
75 FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd)
76 ) && printf 'builtin cd -- %q' "$(builtin unset CDPATH && builtin cd -- "$dir" && builtin pwd)"
77 }
78
79 __fzf_history_delete() {
80 [[ -s $1 ]] || return
81
82 local offsets
83 offsets=($(sort -rnu "$1"))
84 for offset in "${offsets[@]}"; do
85 builtin history -d "$offset"
86 done
87
88 if [[ ${#offsets[@]} -gt 0 ]] && shopt -q histappend; then
89 builtin history -w
90 fi
91 }
92
93 if command -v perl > /dev/null; then
94 __fzf_history__() {
95 local output script deletefile
96 deletefile=$(mktemp)
97 script='BEGIN { getc; $/ = "\n\t"; $HISTCOUNT = $ENV{last_hist} + 1 } s/^[ *]//; s/\n/\n\t/gm; print $HISTCOUNT - $. . "\t$_" if !$seen{$_}++'
98 output=$(
99 set +o pipefail
100 builtin fc -lnr -2147483648 |
101 last_hist=$(HISTTIMEFORMAT='' builtin history 1) command perl -n -l0 -e "$script" |
102 FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort,alt-r:toggle-raw --wrap-sign '"$'\t'"↳ ' --highlight-line --bind 'shift-delete:execute-silent(cat {+f1} >> \"$deletefile\")+exclude-multi' --multi ${FZF_CTRL_R_OPTS-} --read0") \
103 FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd) --query "$READLINE_LINE"
104 )
105 __fzf_history_delete "$deletefile"
106 command rm -f "$deletefile"
107 [[ -n $output ]] || return
108 READLINE_LINE=$(command perl -pe 's/^\d*\t//' <<< "$output")
109 if [[ -z $READLINE_POINT ]]; then
110 echo "$READLINE_LINE"
111 else
112 READLINE_POINT=0x7fffffff
113 fi
114 }
115 else # awk - fallback for POSIX systems
116 __fzf_history__() {
117 local output script deletefile
118 deletefile=$(mktemp)
119 [[ $(HISTTIMEFORMAT='' builtin history 1) =~ [[:digit:]]+ ]] # how many history entries
120 script='function P(b) { ++n; sub(/^[ *]/, "", b); if (!seen[b]++) { printf "%d\t%s%c", '$((BASH_REMATCH + 1))' - n, b, 0 } }
121 NR==1 { b = substr($0, 2); next }
122 /^\t/ { P(b); b = substr($0, 2); next }
123 { b = b RS $0 }
124 END { if (NR) P(b) }'
125 output=$(
126 set +o pipefail
127 builtin fc -lnr -2147483648 2> /dev/null | # ( $'\t '<lines>$'\n' )* ; <lines> ::= [^\n]* ( $'\n'<lines> )*
128 __fzf_exec_awk "$script" | # ( <counter>$'\t'<lines>$'\000' )*
129 FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort,alt-r:toggle-raw --wrap-sign '"$'\t'"↳ ' --highlight-line --bind 'shift-delete:execute-silent(cat {+f1} >> \"$deletefile\")+exclude-multi' --multi ${FZF_CTRL_R_OPTS-} --read0") \
130 FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd) --query "$READLINE_LINE"
131 )
132 __fzf_history_delete "$deletefile"
133 command rm -f "$deletefile"
134 [[ -n $output ]] || return
135 READLINE_LINE=${output#*$'\t'}
136 if [[ -z $READLINE_POINT ]]; then
137 echo "$READLINE_LINE"
138 else
139 READLINE_POINT=0x7fffffff
140 fi
141 }
142 fi
143
144 # Required to refresh the prompt after fzf
145 bind -m emacs-standard '"\C-\e(": redraw-current-line'
146
147 bind -m vi-command '"\C-z": emacs-editing-mode'
148 bind -m vi-insert '"\C-z": emacs-editing-mode'
149 bind -m emacs-standard '"\C-z": vi-editing-mode'
150
151 if ((BASH_VERSINFO[0] < 4)); then
152 # CTRL-T - Paste the selected file path into the command line
153 if [[ ${FZF_CTRL_T_COMMAND-x} != "" ]]; then
154 bind -m emacs-standard '"\C-t": " \C-b\C-k \C-u`__fzf_select__`\e\C-e\C-\e(\C-a\C-y\C-h\C-e\e \C-y\ey\C-x\C-x\C-f\C-y\ey\C-_"'
155 bind -m vi-command '"\C-t": "\C-z\C-t\C-z"'
156 bind -m vi-insert '"\C-t": "\C-z\C-t\C-z"'
157 fi
158
159 # CTRL-R - Paste the selected command from history into the command line
160 if [[ ${FZF_CTRL_R_COMMAND-x} != "" ]]; then
161 if [[ -n ${FZF_CTRL_R_COMMAND-} ]]; then
162 echo "warning: FZF_CTRL_R_COMMAND is set to a custom command, but custom commands are not yet supported for CTRL-R" >&2
163 fi
164 bind -m emacs-standard '"\C-r": "\C-e \C-u\C-y\ey\C-u`__fzf_history__`\e\C-e\C-\e("'
165 bind -m vi-command '"\C-r": "\C-z\C-r\C-z"'
166 bind -m vi-insert '"\C-r": "\C-z\C-r\C-z"'
167 fi
168 else
169 # CTRL-T - Paste the selected file path into the command line
170 if [[ ${FZF_CTRL_T_COMMAND-x} != "" ]]; then
171 bind -m emacs-standard -x '"\C-t": fzf-file-widget'
172 bind -m vi-command -x '"\C-t": fzf-file-widget'
173 bind -m vi-insert -x '"\C-t": fzf-file-widget'
174 fi
175
176 # CTRL-R - Paste the selected command from history into the command line
177 if [[ ${FZF_CTRL_R_COMMAND-x} != "" ]]; then
178 if [[ -n ${FZF_CTRL_R_COMMAND-} ]]; then
179 echo "warning: FZF_CTRL_R_COMMAND is set to a custom command, but custom commands are not yet supported for CTRL-R" >&2
180 fi
181 bind -m emacs-standard -x '"\C-r": __fzf_history__'
182 bind -m vi-command -x '"\C-r": __fzf_history__'
183 bind -m vi-insert -x '"\C-r": __fzf_history__'
184 fi
185 fi
186
187 # ALT-C - cd into the selected directory
188 if [[ ${FZF_ALT_C_COMMAND-x} != "" ]]; then
189 bind -m emacs-standard '"\ec": " \C-b\C-k \C-u`__fzf_cd__`\e\C-e\C-\e(\C-m\C-y\C-h\e \C-y\ey\C-x\C-x\C-d\C-y\ey\C-_"'
190 bind -m vi-command '"\ec": "\C-z\ec\C-z"'
191 bind -m vi-insert '"\ec": "\C-z\ec\C-z"'
192 fi
193 #----END shfmt
194
195 fi