[bash,zsh] Separate common functions into "shell/common.sh"
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Koichi Murase · committed by Junegunn Choi · parent 243e52f · Browse files · 6 files·+101 added−96 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+101 added−96 removed
- other:+101 added−96 removed
Provenance
- Commit
bfa287b- Tree
7c15e74- Author
Koichi Murase <myoga.murase@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
243e52f
6 files changed+101 added−96 removed
added:shell/common.sh+38 added
1234567891011121314151617181920212223242526272829303132333435363738
__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" "$@"}modified:shell/completion.bash+8 added-24 removed
29 unmodified lines30 # }31 32 ###########################################################33 34+#----BEGIN INCLUDE common.sh35+# NOTE: Do not directly edit this section, which is copied from "common.sh".36+# To modify it, one can edit "common.sh" and run "./update-common.sh" to apply37+# the changes. See code comments in "common.sh" for the implementation details.38+39 __fzf_defaults() {35- # $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS36- # $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS37- echo "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1"40+ printf '%s\n' "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1"41 command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null39- echo "${FZF_DEFAULT_OPTS-} $2"42+ printf '%s\n' "${FZF_DEFAULT_OPTS-} $2"43 }44 42-# This function performs `exec awk "$@"` safely by working around awk43-# compatibility issues.44-#45-# Note: To reduce an extra fork, this function performs "exec" so is expected46-# to be run as the last command in a subshell.47-#48-# Note: This function is included with {completion,key-bindings}.{bash,zsh} and49-# synchronized.45 __fzf_exec_awk() {46 if [[ -z ${__fzf_awk-} ]]; then47 __fzf_awk=awk48 if [[ $OSTYPE == solaris* && -x /usr/xpg4/bin/awk ]]; then54- # Note: Solaris awk at /usr/bin/awk is meant for backward compatibility55- # with an ancient implementation of 1977 awk in the original UNIX. It56- # lacks many features of POSIX awk, so it is essentially useless in the57- # modern point of view. To use a standard-conforming version in Solaris,58- # one needs to explicitly use /usr/xpg4/bin/awk.49 __fzf_awk=/usr/xpg4/bin/awk50 else61- # choose the faster mawk if: it's installed && build date >= 20230322 &&62- # version >= 1.3.451 local n x y z d52 IFS=' .' read n x y z d <<< $(command mawk -W version 2> /dev/null)53 [[ $n == mawk ]] && (( d >= 20230302 && (x * 1000 + y) * 1000 + z >= 1003004 )) && __fzf_awk=mawk54 fi55 fi68-69- # Note: macOS awk has a quirk that it stops processing at all when it sees70- # any data not following UTF-8 in the input stream when the current LC_CTYPE71- # specifies the UTF-8 encoding. To work around this quirk, one needs to72- # specify LC_ALL=C to change the current encoding to the plain one.56 LC_ALL=C exec "$__fzf_awk" "$@"57 }58+#----END INCLUDE59 60 __fzf_comprun() {61 if [[ "$(type -t _fzf_comprun 2>&1)" = function ]]; then62 _fzf_comprun "$@"623 unmodified linesmodified:shell/completion.zsh+8 added-24 removed
94 unmodified lines95 # }96 97 ###########################################################98 99+#----BEGIN INCLUDE common.sh100+# NOTE: Do not directly edit this section, which is copied from "common.sh".101+# To modify it, one can edit "common.sh" and run "./update-common.sh" to apply102+# the changes. See code comments in "common.sh" for the implementation details.103+104 __fzf_defaults() {100- # $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS101- # $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS102- echo -E "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1"105+ printf '%s\n' "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1"106 command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null104- echo -E "${FZF_DEFAULT_OPTS-} $2"107+ printf '%s\n' "${FZF_DEFAULT_OPTS-} $2"108 }109 107-# This function performs `exec awk "$@"` safely by working around awk108-# compatibility issues.109-#110-# Note: To reduce an extra fork, this function performs "exec" so is expected111-# to be run as the last command in a subshell.112-#113-# Note: This function is included with {completion,key-bindings}.{bash,zsh} and114-# synchronized.110 __fzf_exec_awk() {111 if [[ -z ${__fzf_awk-} ]]; then112 __fzf_awk=awk113 if [[ $OSTYPE == solaris* && -x /usr/xpg4/bin/awk ]]; then119- # Note: Solaris awk at /usr/bin/awk is meant for backward compatibility120- # with an ancient implementation of 1977 awk in the original UNIX. It121- # lacks many features of POSIX awk, so it is essentially useless in the122- # modern point of view. To use a standard-conforming version in Solaris,123- # one needs to explicitly use /usr/xpg4/bin/awk.114 __fzf_awk=/usr/xpg4/bin/awk115 else126- # choose the faster mawk if: it's installed && build date >= 20230322 &&127- # version >= 1.3.4116 local n x y z d117 IFS=' .' read n x y z d <<< $(command mawk -W version 2> /dev/null)118 [[ $n == mawk ]] && (( d >= 20230302 && (x * 1000 + y) * 1000 + z >= 1003004 )) && __fzf_awk=mawk119 fi120 fi133-134- # Note: macOS awk has a quirk that it stops processing at all when it sees135- # any data not following UTF-8 in the input stream when the current LC_CTYPE136- # specifies the UTF-8 encoding. To work around this quirk, one needs to137- # specify LC_ALL=C to change the current encoding to the plain one.121 LC_ALL=C exec "$__fzf_awk" "$@"122 }123+#----END INCLUDE124 125 __fzf_comprun() {126 if [[ "$(type _fzf_comprun 2>&1)" =~ function ]]; then127 _fzf_comprun "$@"343 unmodified linesmodified:shell/key-bindings.bash+8 added-24 removed
15 unmodified lines161718192021222324252122232627252829302829303132333435313233344041424344353647483738394041545556575842434445464748110 unmodified linesExpand all15 unmodified lines
# 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:-} \ FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse --walker=file,dir,follow,hidden --scheme=path" "${FZF_CTRL_T_OPTS-} -m") \110 unmodified linesExpand allmodified:shell/key-bindings.zsh+8 added-24 removed
36 unmodified lines37 38 {39 if [[ -o interactive ]]; then40 41+#----BEGIN INCLUDE common.sh42+# NOTE: Do not directly edit this section, which is copied from "common.sh".43+# To modify it, one can edit "common.sh" and run "./update-common.sh" to apply44+# the changes. See code comments in "common.sh" for the implementation details.45+46 __fzf_defaults() {42- # $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS43- # $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS44- echo -E "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1"47+ printf '%s\n' "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1"48 command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null46- echo -E "${FZF_DEFAULT_OPTS-} $2"49+ printf '%s\n' "${FZF_DEFAULT_OPTS-} $2"50 }51 49-# This function performs `exec awk "$@"` safely by working around awk50-# compatibility issues.51-#52-# Note: To reduce an extra fork, this function performs "exec" so is expected53-# to be run as the last command in a subshell.54-#55-# Note: This function is included with {completion,key-bindings}.{bash,zsh} and56-# synchronized.52 __fzf_exec_awk() {53 if [[ -z ${__fzf_awk-} ]]; then54 __fzf_awk=awk55 if [[ $OSTYPE == solaris* && -x /usr/xpg4/bin/awk ]]; then61- # Note: Solaris awk at /usr/bin/awk is meant for backward compatibility62- # with an ancient implementation of 1977 awk in the original UNIX. It63- # lacks many features of POSIX awk, so it is essentially useless in the64- # modern point of view. To use a standard-conforming version in Solaris,65- # one needs to explicitly use /usr/xpg4/bin/awk.56 __fzf_awk=/usr/xpg4/bin/awk57 else68- # choose the faster mawk if: it's installed && build date >= 20230322 &&69- # version >= 1.3.458 local n x y z d59 IFS=' .' read n x y z d <<< $(command mawk -W version 2> /dev/null)60 [[ $n == mawk ]] && (( d >= 20230302 && (x * 1000 + y) * 1000 + z >= 1003004 )) && __fzf_awk=mawk61 fi62 fi75-76- # Note: macOS awk has a quirk that it stops processing at all when it sees77- # any data not following UTF-8 in the input stream when the current LC_CTYPE78- # specifies the UTF-8 encoding. To work around this quirk, one needs to79- # specify LC_ALL=C to change the current encoding to the plain one.63 LC_ALL=C exec "$__fzf_awk" "$@"64 }65+#----END INCLUDE66 67 # CTRL-T - Paste the selected file path(s) into the command line68 __fzf_select() {69 setopt localoptions pipefail no_aliases 2> /dev/null93 unmodified linesadded:shell/update-common.sh+31 added
1+#!/bin/sh2+3+# This script applies the contents of "common.sh" to the other files.4+5+set -e6+7+# Go to the directory that contains this script8+dir=${0%"${0##*/}"}9+if [ -n "$dir" ]; then10+ cd "$dir"11+fi12+13+update() {14+ {15+ sed -n '1,/^#----BEGIN INCLUDE common\.sh/p' "$1"16+ cat <<EOF17+# NOTE: Do not directly edit this section, which is copied from "common.sh".18+# To modify it, one can edit "common.sh" and run "./update-common.sh" to apply19+# the changes. See code comments in "common.sh" for the implementation details.20+EOF21+ grep -v '^[[:blank:]]*#' common.sh # remove code comments in common.sh22+ sed -n '/^#----END INCLUDE/,$p' "$1"23+ } > "$1.part"24+25+ mv -f "$1.part" "$1"26+}27+28+update completion.bash29+update completion.zsh30+update key-bindings.bash31+update key-bindings.zsh