Skip to content
Myr

[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

6 files+101 added−96 removed

added:shell/common.sh+38 added
modified:shell/completion.bash+8 added-24 removed
29 unmodified lines
30# }3132###########################################################3334#----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.3839__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}4442# 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 fi6869 # 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 INCLUDE5960__fzf_comprun() {61 if [[ "$(type -t _fzf_comprun 2>&1)" = function ]]; then62 _fzf_comprun "$@"
623 unmodified lines
modified:shell/completion.zsh+8 added-24 removed
94 unmodified lines
95# }9697###########################################################9899#----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.103104__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}109107# 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 fi133134 # 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 INCLUDE124125__fzf_comprun() {126 if [[ "$(type _fzf_comprun 2>&1)" =~ function ]]; then127 _fzf_comprun "$@"
343 unmodified lines
modified:shell/key-bindings.bash+8 added-24 removed
modified:shell/key-bindings.zsh+8 added-24 removed
36 unmodified lines
3738{39if [[ -o interactive ]]; then4041#----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.4546__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}5149# 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 fi7576 # 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 INCLUDE6667# CTRL-T - Paste the selected file path(s) into the command line68__fzf_select() {69 setopt localoptions pipefail no_aliases 2> /dev/null
93 unmodified lines
added:shell/update-common.sh+31 added
1#!/bin/sh23# This script applies the contents of "common.sh" to the other files.45set -e67# Go to the directory that contains this script8dir=${0%"${0##*/}"}9if [ -n "$dir" ]; then10 cd "$dir"11fi1213update() {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.20EOF21 grep -v '^[[:blank:]]*#' common.sh # remove code comments in common.sh22 sed -n '/^#----END INCLUDE/,$p' "$1"23 } > "$1.part"2425 mv -f "$1.part" "$1"26}2728update completion.bash29update completion.zsh30update key-bindings.bash31update key-bindings.zsh