diff --git a/shell/common.sh b/shell/common.sh new file mode 100644 index 00000000..6866f0be --- /dev/null +++ b/shell/common.sh @@ -0,0 +1,38 @@ + +__fzf_defaults() { + # $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS + # $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS + printf '%s\n' "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1" + command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null + printf '%s\n' "${FZF_DEFAULT_OPTS-} $2" +} + +__fzf_exec_awk() { + # This function performs `exec awk "$@"` safely by working around awk + # compatibility issues. + # + # To reduce an extra fork, this function performs "exec" so is expected to be + # run as the last command in a subshell. + if [[ -z ${__fzf_awk-} ]]; then + __fzf_awk=awk + if [[ $OSTYPE == solaris* && -x /usr/xpg4/bin/awk ]]; then + # Note: Solaris awk at /usr/bin/awk is meant for backward compatibility + # with an ancient implementation of 1977 awk in the original UNIX. It + # lacks many features of POSIX awk, so it is essentially useless in the + # modern point of view. To use a standard-conforming version in Solaris, + # one needs to explicitly use /usr/xpg4/bin/awk. + __fzf_awk=/usr/xpg4/bin/awk + else + # choose the faster mawk if: it's installed && build date >= 20230322 && + # version >= 1.3.4 + local n x y z d + 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 + fi + # Note: macOS awk has a quirk that it stops processing at all when it sees + # any data not following UTF-8 in the input stream when the current LC_CTYPE + # specifies the UTF-8 encoding. To work around this quirk, one needs to + # specify LC_ALL=C to change the current encoding to the plain one. + LC_ALL=C exec "$__fzf_awk" "$@" +} diff --git a/shell/completion.bash b/shell/completion.bash index 2132ffd1..aef79060 100644 --- a/shell/completion.bash +++ b/shell/completion.bash @@ -31,47 +31,31 @@ if [[ $- =~ i ]]; then ########################################################### +#----BEGIN INCLUDE common.sh +# NOTE: Do not directly edit this section, which is copied from "common.sh". +# To modify it, one can edit "common.sh" and run "./update-common.sh" to apply +# the changes. See code comments in "common.sh" for the implementation details. + __fzf_defaults() { - # $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS - # $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS - echo "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1" + printf '%s\n' "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1" command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null - echo "${FZF_DEFAULT_OPTS-} $2" + printf '%s\n' "${FZF_DEFAULT_OPTS-} $2" } -# This function performs `exec awk "$@"` safely by working around awk -# compatibility issues. -# -# Note: To reduce an extra fork, this function performs "exec" so is expected -# to be run as the last command in a subshell. -# -# Note: This function is included with {completion,key-bindings}.{bash,zsh} and -# synchronized. __fzf_exec_awk() { if [[ -z ${__fzf_awk-} ]]; then __fzf_awk=awk if [[ $OSTYPE == solaris* && -x /usr/xpg4/bin/awk ]]; then - # Note: Solaris awk at /usr/bin/awk is meant for backward compatibility - # with an ancient implementation of 1977 awk in the original UNIX. It - # lacks many features of POSIX awk, so it is essentially useless in the - # modern point of view. To use a standard-conforming version in Solaris, - # one needs to explicitly use /usr/xpg4/bin/awk. __fzf_awk=/usr/xpg4/bin/awk else - # choose the faster mawk if: it's installed && build date >= 20230322 && - # version >= 1.3.4 local n x y z d 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 fi - - # Note: macOS awk has a quirk that it stops processing at all when it sees - # any data not following UTF-8 in the input stream when the current LC_CTYPE - # specifies the UTF-8 encoding. To work around this quirk, one needs to - # specify LC_ALL=C to change the current encoding to the plain one. LC_ALL=C exec "$__fzf_awk" "$@" } +#----END INCLUDE __fzf_comprun() { if [[ "$(type -t _fzf_comprun 2>&1)" = function ]]; then diff --git a/shell/completion.zsh b/shell/completion.zsh index d5406880..45d0e955 100644 --- a/shell/completion.zsh +++ b/shell/completion.zsh @@ -96,47 +96,31 @@ if [[ -o interactive ]]; then ########################################################### +#----BEGIN INCLUDE common.sh +# NOTE: Do not directly edit this section, which is copied from "common.sh". +# To modify it, one can edit "common.sh" and run "./update-common.sh" to apply +# the changes. See code comments in "common.sh" for the implementation details. + __fzf_defaults() { - # $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS - # $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS - echo -E "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1" + printf '%s\n' "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1" command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null - echo -E "${FZF_DEFAULT_OPTS-} $2" + printf '%s\n' "${FZF_DEFAULT_OPTS-} $2" } -# This function performs `exec awk "$@"` safely by working around awk -# compatibility issues. -# -# Note: To reduce an extra fork, this function performs "exec" so is expected -# to be run as the last command in a subshell. -# -# Note: This function is included with {completion,key-bindings}.{bash,zsh} and -# synchronized. __fzf_exec_awk() { if [[ -z ${__fzf_awk-} ]]; then __fzf_awk=awk if [[ $OSTYPE == solaris* && -x /usr/xpg4/bin/awk ]]; then - # Note: Solaris awk at /usr/bin/awk is meant for backward compatibility - # with an ancient implementation of 1977 awk in the original UNIX. It - # lacks many features of POSIX awk, so it is essentially useless in the - # modern point of view. To use a standard-conforming version in Solaris, - # one needs to explicitly use /usr/xpg4/bin/awk. __fzf_awk=/usr/xpg4/bin/awk else - # choose the faster mawk if: it's installed && build date >= 20230322 && - # version >= 1.3.4 local n x y z d 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 fi - - # Note: macOS awk has a quirk that it stops processing at all when it sees - # any data not following UTF-8 in the input stream when the current LC_CTYPE - # specifies the UTF-8 encoding. To work around this quirk, one needs to - # specify LC_ALL=C to change the current encoding to the plain one. LC_ALL=C exec "$__fzf_awk" "$@" } +#----END INCLUDE __fzf_comprun() { if [[ "$(type _fzf_comprun 2>&1)" =~ function ]]; then diff --git a/shell/key-bindings.bash b/shell/key-bindings.bash index 9cfae021..a1d4fa23 100644 --- a/shell/key-bindings.bash +++ b/shell/key-bindings.bash @@ -17,47 +17,31 @@ if [[ $- =~ i ]]; then # Key bindings # ------------ +#----BEGIN INCLUDE common.sh +# NOTE: Do not directly edit this section, which is copied from "common.sh". +# To modify it, one can edit "common.sh" and run "./update-common.sh" to apply +# the changes. See code comments in "common.sh" for the implementation details. + __fzf_defaults() { - # $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS - # $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS - echo "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1" + printf '%s\n' "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1" command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null - echo "${FZF_DEFAULT_OPTS-} $2" + printf '%s\n' "${FZF_DEFAULT_OPTS-} $2" } -# This function performs `exec awk "$@"` safely by working around awk -# compatibility issues. -# -# Note: To reduce an extra fork, this function performs "exec" so is expected -# to be run as the last command in a subshell. -# -# Note: This function is included with {completion,key-bindings}.{bash,zsh} and -# synchronized. __fzf_exec_awk() { if [[ -z ${__fzf_awk-} ]]; then __fzf_awk=awk if [[ $OSTYPE == solaris* && -x /usr/xpg4/bin/awk ]]; then - # Note: Solaris awk at /usr/bin/awk is meant for backward compatibility - # with an ancient implementation of 1977 awk in the original UNIX. It - # lacks many features of POSIX awk, so it is essentially useless in the - # modern point of view. To use a standard-conforming version in Solaris, - # one needs to explicitly use /usr/xpg4/bin/awk. __fzf_awk=/usr/xpg4/bin/awk else - # choose the faster mawk if: it's installed && build date >= 20230322 && - # version >= 1.3.4 local n x y z d 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 fi - - # Note: macOS awk has a quirk that it stops processing at all when it sees - # any data not following UTF-8 in the input stream when the current LC_CTYPE - # specifies the UTF-8 encoding. To work around this quirk, one needs to - # specify LC_ALL=C to change the current encoding to the plain one. LC_ALL=C exec "$__fzf_awk" "$@" } +#----END INCLUDE __fzf_select__() { FZF_DEFAULT_COMMAND=${FZF_CTRL_T_COMMAND:-} \ diff --git a/shell/key-bindings.zsh b/shell/key-bindings.zsh index f283d157..b2fc198c 100644 --- a/shell/key-bindings.zsh +++ b/shell/key-bindings.zsh @@ -38,47 +38,31 @@ fi { if [[ -o interactive ]]; then +#----BEGIN INCLUDE common.sh +# NOTE: Do not directly edit this section, which is copied from "common.sh". +# To modify it, one can edit "common.sh" and run "./update-common.sh" to apply +# the changes. See code comments in "common.sh" for the implementation details. + __fzf_defaults() { - # $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS - # $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS - echo -E "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1" + printf '%s\n' "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1" command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null - echo -E "${FZF_DEFAULT_OPTS-} $2" + printf '%s\n' "${FZF_DEFAULT_OPTS-} $2" } -# This function performs `exec awk "$@"` safely by working around awk -# compatibility issues. -# -# Note: To reduce an extra fork, this function performs "exec" so is expected -# to be run as the last command in a subshell. -# -# Note: This function is included with {completion,key-bindings}.{bash,zsh} and -# synchronized. __fzf_exec_awk() { if [[ -z ${__fzf_awk-} ]]; then __fzf_awk=awk if [[ $OSTYPE == solaris* && -x /usr/xpg4/bin/awk ]]; then - # Note: Solaris awk at /usr/bin/awk is meant for backward compatibility - # with an ancient implementation of 1977 awk in the original UNIX. It - # lacks many features of POSIX awk, so it is essentially useless in the - # modern point of view. To use a standard-conforming version in Solaris, - # one needs to explicitly use /usr/xpg4/bin/awk. __fzf_awk=/usr/xpg4/bin/awk else - # choose the faster mawk if: it's installed && build date >= 20230322 && - # version >= 1.3.4 local n x y z d 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 fi - - # Note: macOS awk has a quirk that it stops processing at all when it sees - # any data not following UTF-8 in the input stream when the current LC_CTYPE - # specifies the UTF-8 encoding. To work around this quirk, one needs to - # specify LC_ALL=C to change the current encoding to the plain one. LC_ALL=C exec "$__fzf_awk" "$@" } +#----END INCLUDE # CTRL-T - Paste the selected file path(s) into the command line __fzf_select() { diff --git a/shell/update-common.sh b/shell/update-common.sh new file mode 100755 index 00000000..55905043 --- /dev/null +++ b/shell/update-common.sh @@ -0,0 +1,31 @@ +#!/bin/sh + +# This script applies the contents of "common.sh" to the other files. + +set -e + +# Go to the directory that contains this script +dir=${0%"${0##*/}"} +if [ -n "$dir" ]; then + cd "$dir" +fi + +update() { + { + sed -n '1,/^#----BEGIN INCLUDE common\.sh/p' "$1" + cat < "$1.part" + + mv -f "$1.part" "$1" +} + +update completion.bash +update completion.zsh +update key-bindings.bash +update key-bindings.zsh