From b5d385271581c5259622778dbd94c05cf4465fd5 Mon Sep 17 00:00:00 2001 From: step- Date: Fri, 6 Oct 2023 12:47:10 +0200 Subject: [PATCH 1/6] [shell] improve mawk detection * Use the all-compatible mawk `-W version` option. https://github.com/junegunn/fzf/pull/3313#issuecomment-1747934690. * Do not remap the history key if no dependent commands is installed (perl, awk or mawk in this order). * Run the command and not a function consistently with #3462. The version check bash code relies on the following mawk source code, extracted from mawk 1.3.4 20230322. ``` version.c: 18- #include "init.h" 19- #include "patchlev.h" 20- 21: #define VERSION_STRING \ 22- "mawk %d.%d%s %s\n\ 23- Copyright 2008-2022,2023, Thomas E. Dickey\n\ 24- Copyright 1991-1996,2014, Michael D. Brennan\n\n" .... 30- void 31- print_version(FILE *fp) 32- { 33: fprintf(fp, VERSION_STRING, PATCH_BASE, PATCH_LEVEL, PATCH_STRING, DATE_STRING); 34- fflush(fp); 35- 36- #define SHOW_RANDOM "random-funcs:" patchlev.h: 13- /* 14- * $MawkId: patchlev.h,v 1.128 2023/03/23 00:23:57 tom Exp $ 15- */ 16: #define PATCH_BASE 1 17- #define PATCH_LEVEL 3 18- #define PATCH_STRING ".4" 19- #define DATE_STRING "20230322" ``` --- shell/key-bindings.bash | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/shell/key-bindings.bash b/shell/key-bindings.bash index 8654a6f2c05..d48f303fa44 100644 --- a/shell/key-bindings.bash +++ b/shell/key-bindings.bash @@ -48,7 +48,7 @@ __fzf_cd__() { dir=$(set +o pipefail; eval "$cmd" | FZF_DEFAULT_OPTS="$opts" $(__fzfcmd)) && printf 'builtin cd -- %q' "$dir" } -if command -v perl > /dev/null; then +if command -v perl >&- ; then __fzf_history__() { local output opts script opts="--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore ${FZF_DEFAULT_OPTS-} -n2..,.. --scheme=history --bind=ctrl-r:toggle-sort ${FZF_CTRL_R_OPTS-} +m --read0" @@ -56,7 +56,7 @@ if command -v perl > /dev/null; then output=$( set +o pipefail builtin fc -lnr -2147483648 | - last_hist=$(HISTTIMEFORMAT='' builtin history 1) perl -n -l0 -e "$script" | + last_hist=$(HISTTIMEFORMAT='' builtin history 1) command perl -n -l0 -e "$script" | FZF_DEFAULT_OPTS="$opts" $(__fzfcmd) --query "$READLINE_LINE" ) || return READLINE_LINE=${output#*$'\t'} @@ -66,16 +66,14 @@ if command -v perl > /dev/null; then READLINE_POINT=0x7fffffff fi } -else # awk +elif command -v awk >&- || command -v mawk >&- ; then # awk - fallback for POSIX systems __fzf_history__() { - local output opts script + local output opts script n x y z d if [[ -z $__fzf_awk ]]; then __fzf_awk=awk - # if installed, mawk is faster - command -v mawk > /dev/null && - mawk --version | # at least 1.3.4 - awk 'NR == 1 { split($2, a, "."); v=(a[1]*1000000+ a[2]*1000+ a[3]*1); exit !(v >= 1003004) }' && - __fzf_awk=mawk + # choose the faster mawk if: it's installed && build date >= 20230322 && version >= 1.3.4 + IFS=' .' read n x y z d <<< $(command mawk -W version 2>&-) + [[ $n == mawk ]] && (( d >= 20230302 && (x *1000 +y) *1000 +z >= 1003004 )) && __fzf_awk=mawk fi opts="--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore ${FZF_DEFAULT_OPTS-} -n2..,.. --scheme=history --bind=ctrl-r:toggle-sort ${FZF_CTRL_R_OPTS-} +m --read0" [[ $(HISTTIMEFORMAT='' builtin history 1) =~ [[:digit:]]+ ]] # how many history entries @@ -86,8 +84,8 @@ else # awk END { if (NR) P(b) }' output=$( set +o pipefail - builtin fc -lnr -2147483648 2> /dev/null | # ( $'\t '$'\n' )* ; ::= [^\n]* ( $'\n' )* - $__fzf_awk "$script" | # ( $'\t'$'\000' )* + builtin fc -lnr -2147483648 2>&- | # ( $'\t '$'\n' )* ; ::= [^\n]* ( $'\n' )* + command $__fzf_awk "$script" | # ( $'\t'$'\000' )* FZF_DEFAULT_OPTS="$opts" $(__fzfcmd) --query "$READLINE_LINE" ) || return READLINE_LINE=${output#*$'\t'} @@ -112,20 +110,24 @@ if (( BASH_VERSINFO[0] < 4 )); then bind -m vi-command '"\C-t": "\C-z\C-t\C-z"' bind -m vi-insert '"\C-t": "\C-z\C-t\C-z"' - # CTRL-R - Paste the selected command from history into the command line - bind -m emacs-standard '"\C-r": "\C-e \C-u\C-y\ey\C-u`__fzf_history__`\e\C-e\er"' - bind -m vi-command '"\C-r": "\C-z\C-r\C-z"' - bind -m vi-insert '"\C-r": "\C-z\C-r\C-z"' + if [[ $(type -t __fzf_history__) == function ]]; then + # CTRL-R - Paste the selected command from history into the command line + bind -m emacs-standard '"\C-r": "\C-e \C-u\C-y\ey\C-u`__fzf_history__`\e\C-e\er"' + bind -m vi-command '"\C-r": "\C-z\C-r\C-z"' + bind -m vi-insert '"\C-r": "\C-z\C-r\C-z"' + fi else # CTRL-T - Paste the selected file path into the command line bind -m emacs-standard -x '"\C-t": fzf-file-widget' bind -m vi-command -x '"\C-t": fzf-file-widget' bind -m vi-insert -x '"\C-t": fzf-file-widget' - # CTRL-R - Paste the selected command from history into the command line - bind -m emacs-standard -x '"\C-r": __fzf_history__' - bind -m vi-command -x '"\C-r": __fzf_history__' - bind -m vi-insert -x '"\C-r": __fzf_history__' + if [[ $(type -t __fzf_history__) == function ]]; then + # CTRL-R - Paste the selected command from history into the command line + bind -m emacs-standard -x '"\C-r": __fzf_history__' + bind -m vi-command -x '"\C-r": __fzf_history__' + bind -m vi-insert -x '"\C-r": __fzf_history__' + fi fi # ALT-C - cd into the selected directory From 4a797e81b56f30595e2190eed21bbcf55fd010ca Mon Sep 17 00:00:00 2001 From: step- Date: Sat, 7 Oct 2023 08:33:18 +0200 Subject: [PATCH 2/6] replace x with redirection to /dev/null --- shell/key-bindings.bash | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/shell/key-bindings.bash b/shell/key-bindings.bash index d48f303fa44..eb8d9cae651 100644 --- a/shell/key-bindings.bash +++ b/shell/key-bindings.bash @@ -48,7 +48,7 @@ __fzf_cd__() { dir=$(set +o pipefail; eval "$cmd" | FZF_DEFAULT_OPTS="$opts" $(__fzfcmd)) && printf 'builtin cd -- %q' "$dir" } -if command -v perl >&- ; then +if command -v perl > /dev/null ; then __fzf_history__() { local output opts script opts="--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore ${FZF_DEFAULT_OPTS-} -n2..,.. --scheme=history --bind=ctrl-r:toggle-sort ${FZF_CTRL_R_OPTS-} +m --read0" @@ -66,13 +66,13 @@ if command -v perl >&- ; then READLINE_POINT=0x7fffffff fi } -elif command -v awk >&- || command -v mawk >&- ; then # awk - fallback for POSIX systems +elif command -v awk > /dev/null || command -v mawk > /dev/null ; then # awk - fallback for POSIX systems __fzf_history__() { local output opts script n x y z d if [[ -z $__fzf_awk ]]; then __fzf_awk=awk # choose the faster mawk if: it's installed && build date >= 20230322 && version >= 1.3.4 - IFS=' .' read n x y z d <<< $(command mawk -W version 2>&-) + IFS=' .' read n x y z d <<< $(command mawk -W version 2> /dev/null) [[ $n == mawk ]] && (( d >= 20230302 && (x *1000 +y) *1000 +z >= 1003004 )) && __fzf_awk=mawk fi opts="--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore ${FZF_DEFAULT_OPTS-} -n2..,.. --scheme=history --bind=ctrl-r:toggle-sort ${FZF_CTRL_R_OPTS-} +m --read0" @@ -84,8 +84,8 @@ elif command -v awk >&- || command -v mawk >&- ; then # awk - fallback for POSIX END { if (NR) P(b) }' output=$( set +o pipefail - builtin fc -lnr -2147483648 2>&- | # ( $'\t '$'\n' )* ; ::= [^\n]* ( $'\n' )* - command $__fzf_awk "$script" | # ( $'\t'$'\000' )* + builtin fc -lnr -2147483648 2> /dev/null | # ( $'\t '$'\n' )* ; ::= [^\n]* ( $'\n' )* + command $__fzf_awk "$script" | # ( $'\t'$'\000' )* FZF_DEFAULT_OPTS="$opts" $(__fzfcmd) --query "$READLINE_LINE" ) || return READLINE_LINE=${output#*$'\t'} From 614af5231fead67133b0e5285f2866d89499d435 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sat, 7 Oct 2023 17:15:20 +0900 Subject: [PATCH 3/6] Update shell/key-bindings.bash --- shell/key-bindings.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/key-bindings.bash b/shell/key-bindings.bash index eb8d9cae651..f944631341b 100644 --- a/shell/key-bindings.bash +++ b/shell/key-bindings.bash @@ -48,7 +48,7 @@ __fzf_cd__() { dir=$(set +o pipefail; eval "$cmd" | FZF_DEFAULT_OPTS="$opts" $(__fzfcmd)) && printf 'builtin cd -- %q' "$dir" } -if command -v perl > /dev/null ; then +if command -v perl > /dev/null; then __fzf_history__() { local output opts script opts="--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore ${FZF_DEFAULT_OPTS-} -n2..,.. --scheme=history --bind=ctrl-r:toggle-sort ${FZF_CTRL_R_OPTS-} +m --read0" From c7e1f0bd27861f6e3c7c35d7dfedc09a103b735e Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sat, 7 Oct 2023 17:15:27 +0900 Subject: [PATCH 4/6] Update shell/key-bindings.bash --- shell/key-bindings.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/key-bindings.bash b/shell/key-bindings.bash index f944631341b..e0e972be351 100644 --- a/shell/key-bindings.bash +++ b/shell/key-bindings.bash @@ -66,7 +66,7 @@ if command -v perl > /dev/null; then READLINE_POINT=0x7fffffff fi } -elif command -v awk > /dev/null || command -v mawk > /dev/null ; then # awk - fallback for POSIX systems +elif command -v awk > /dev/null || command -v mawk > /dev/null; then # awk - fallback for POSIX systems __fzf_history__() { local output opts script n x y z d if [[ -z $__fzf_awk ]]; then From 09dd8af2cbbeb17aac06fa389763df2f099860d5 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sat, 7 Oct 2023 17:15:32 +0900 Subject: [PATCH 5/6] Update shell/key-bindings.bash --- shell/key-bindings.bash | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shell/key-bindings.bash b/shell/key-bindings.bash index e0e972be351..bde64682b88 100644 --- a/shell/key-bindings.bash +++ b/shell/key-bindings.bash @@ -84,8 +84,8 @@ elif command -v awk > /dev/null || command -v mawk > /dev/null; then # awk - fal END { if (NR) P(b) }' output=$( set +o pipefail - builtin fc -lnr -2147483648 2> /dev/null | # ( $'\t '$'\n' )* ; ::= [^\n]* ( $'\n' )* - command $__fzf_awk "$script" | # ( $'\t'$'\000' )* + builtin fc -lnr -2147483648 2> /dev/null | # ( $'\t '$'\n' )* ; ::= [^\n]* ( $'\n' )* + command $__fzf_awk "$script" | # ( $'\t'$'\000' )* FZF_DEFAULT_OPTS="$opts" $(__fzfcmd) --query "$READLINE_LINE" ) || return READLINE_LINE=${output#*$'\t'} From 3462876d24f3b83441c53ae389457386f671df1a Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sat, 7 Oct 2023 17:22:44 +0900 Subject: [PATCH 6/6] Apply suggestions from code review --- shell/key-bindings.bash | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/shell/key-bindings.bash b/shell/key-bindings.bash index bde64682b88..398f66e49c4 100644 --- a/shell/key-bindings.bash +++ b/shell/key-bindings.bash @@ -66,7 +66,7 @@ if command -v perl > /dev/null; then READLINE_POINT=0x7fffffff fi } -elif command -v awk > /dev/null || command -v mawk > /dev/null; then # awk - fallback for POSIX systems +else # awk - fallback for POSIX systems __fzf_history__() { local output opts script n x y z d if [[ -z $__fzf_awk ]]; then @@ -110,24 +110,20 @@ if (( BASH_VERSINFO[0] < 4 )); then bind -m vi-command '"\C-t": "\C-z\C-t\C-z"' bind -m vi-insert '"\C-t": "\C-z\C-t\C-z"' - if [[ $(type -t __fzf_history__) == function ]]; then - # CTRL-R - Paste the selected command from history into the command line - bind -m emacs-standard '"\C-r": "\C-e \C-u\C-y\ey\C-u`__fzf_history__`\e\C-e\er"' - bind -m vi-command '"\C-r": "\C-z\C-r\C-z"' - bind -m vi-insert '"\C-r": "\C-z\C-r\C-z"' - fi + # CTRL-R - Paste the selected command from history into the command line + bind -m emacs-standard '"\C-r": "\C-e \C-u\C-y\ey\C-u`__fzf_history__`\e\C-e\er"' + bind -m vi-command '"\C-r": "\C-z\C-r\C-z"' + bind -m vi-insert '"\C-r": "\C-z\C-r\C-z"' else # CTRL-T - Paste the selected file path into the command line bind -m emacs-standard -x '"\C-t": fzf-file-widget' bind -m vi-command -x '"\C-t": fzf-file-widget' bind -m vi-insert -x '"\C-t": fzf-file-widget' - if [[ $(type -t __fzf_history__) == function ]]; then - # CTRL-R - Paste the selected command from history into the command line - bind -m emacs-standard -x '"\C-r": __fzf_history__' - bind -m vi-command -x '"\C-r": __fzf_history__' - bind -m vi-insert -x '"\C-r": __fzf_history__' - fi + # CTRL-R - Paste the selected command from history into the command line + bind -m emacs-standard -x '"\C-r": __fzf_history__' + bind -m vi-command -x '"\C-r": __fzf_history__' + bind -m vi-insert -x '"\C-r": __fzf_history__' fi # ALT-C - cd into the selected directory