Skip to content

Commit 766f4ea

Browse files
committed
[bash,zsh] Separate common functions into "shell/common.sh"
1 parent 5d761aa commit 766f4ea

File tree

6 files changed

+101
-96
lines changed

6 files changed

+101
-96
lines changed

shell/common.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
__fzf_defaults() {
3+
# $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
4+
# $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
5+
printf '%s\n' "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1"
6+
command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null
7+
printf '%s\n' "${FZF_DEFAULT_OPTS-} $2"
8+
}
9+
10+
__fzf_exec_awk() {
11+
# This function performs `exec awk "$@"` safely by working around awk
12+
# compatibility issues.
13+
#
14+
# To reduce an extra fork, this function performs "exec" so is expected to be
15+
# run as the last command in a subshell.
16+
if [[ -z ${__fzf_awk-} ]]; then
17+
__fzf_awk=awk
18+
if [[ $OSTYPE == solaris* && -x /usr/xpg4/bin/awk ]]; then
19+
# Note: Solaris awk at /usr/bin/awk is meant for backward compatibility
20+
# with an ancient implementation of 1977 awk in the original UNIX. It
21+
# lacks many features of POSIX awk, so it is essentially useless in the
22+
# modern point of view. To use a standard-conforming version in Solaris,
23+
# one needs to explicitly use /usr/xpg4/bin/awk.
24+
__fzf_awk=/usr/xpg4/bin/awk
25+
else
26+
# choose the faster mawk if: it's installed && build date >= 20230322 &&
27+
# version >= 1.3.4
28+
local n x y z d
29+
IFS=' .' read n x y z d <<< $(command mawk -W version 2> /dev/null)
30+
[[ $n == mawk ]] && (( d >= 20230302 && (x * 1000 + y) * 1000 + z >= 1003004 )) && __fzf_awk=mawk
31+
fi
32+
fi
33+
# Note: macOS awk has a quirk that it stops processing at all when it sees
34+
# any data not following UTF-8 in the input stream when the current LC_CTYPE
35+
# specifies the UTF-8 encoding. To work around this quirk, one needs to
36+
# specify LC_ALL=C to change the current encoding to the plain one.
37+
LC_ALL=C exec "$__fzf_awk" "$@"
38+
}

shell/completion.bash

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,47 +31,31 @@ if [[ $- =~ i ]]; then
3131

3232
###########################################################
3333

34+
#----BEGIN INCLUDE common.sh
35+
# 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 apply
37+
# the changes. see code comments in "common.sh" For the implementation details.
38+
3439
__fzf_defaults() {
35-
# $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
36-
# $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
37-
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"
3841
command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null
39-
echo "${FZF_DEFAULT_OPTS-} $2"
42+
printf '%s\n' "${FZF_DEFAULT_OPTS-} $2"
4043
}
4144

42-
# This function performs `exec awk "$@"` safely by working around awk
43-
# compatibility issues.
44-
#
45-
# Note: To reduce an extra fork, this function performs "exec" so is expected
46-
# to be run as the last command in a subshell.
47-
#
48-
# Note: This function is included with {completion,key-bindings}.{bash,zsh} and
49-
# synchronized.
5045
__fzf_exec_awk() {
5146
if [[ -z ${__fzf_awk-} ]]; then
5247
__fzf_awk=awk
5348
if [[ $OSTYPE == solaris* && -x /usr/xpg4/bin/awk ]]; then
54-
# Note: Solaris awk at /usr/bin/awk is meant for backward compatibility
55-
# with an ancient implementation of 1977 awk in the original UNIX. It
56-
# lacks many features of POSIX awk, so it is essentially useless in the
57-
# modern point of view. To use a standard-conforming version in Solaris,
58-
# one needs to explicitly use /usr/xpg4/bin/awk.
5949
__fzf_awk=/usr/xpg4/bin/awk
6050
else
61-
# choose the faster mawk if: it's installed && build date >= 20230322 &&
62-
# version >= 1.3.4
6351
local n x y z d
6452
IFS=' .' read n x y z d <<< $(command mawk -W version 2> /dev/null)
6553
[[ $n == mawk ]] && (( d >= 20230302 && (x * 1000 + y) * 1000 + z >= 1003004 )) && __fzf_awk=mawk
6654
fi
6755
fi
68-
69-
# Note: macOS awk has a quirk that it stops processing at all when it sees
70-
# any data not following UTF-8 in the input stream when the current LC_CTYPE
71-
# specifies the UTF-8 encoding. To work around this quirk, one needs to
72-
# specify LC_ALL=C to change the current encoding to the plain one.
7356
LC_ALL=C exec "$__fzf_awk" "$@"
7457
}
58+
#----END INCLUDE
7559

7660
__fzf_comprun() {
7761
if [[ "$(type -t _fzf_comprun 2>&1)" = function ]]; then

shell/completion.zsh

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -96,47 +96,31 @@ if [[ -o interactive ]]; then
9696

9797
###########################################################
9898

99+
#----BEGIN INCLUDE common.sh
100+
# 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 apply
102+
# the changes. see code comments in "common.sh" For the implementation details.
103+
99104
__fzf_defaults() {
100-
# $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
101-
# $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
102-
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"
103106
command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null
104-
echo -E "${FZF_DEFAULT_OPTS-} $2"
107+
printf '%s\n' "${FZF_DEFAULT_OPTS-} $2"
105108
}
106109

107-
# This function performs `exec awk "$@"` safely by working around awk
108-
# compatibility issues.
109-
#
110-
# Note: To reduce an extra fork, this function performs "exec" so is expected
111-
# to be run as the last command in a subshell.
112-
#
113-
# Note: This function is included with {completion,key-bindings}.{bash,zsh} and
114-
# synchronized.
115110
__fzf_exec_awk() {
116111
if [[ -z ${__fzf_awk-} ]]; then
117112
__fzf_awk=awk
118113
if [[ $OSTYPE == solaris* && -x /usr/xpg4/bin/awk ]]; then
119-
# Note: Solaris awk at /usr/bin/awk is meant for backward compatibility
120-
# with an ancient implementation of 1977 awk in the original UNIX. It
121-
# lacks many features of POSIX awk, so it is essentially useless in the
122-
# modern point of view. To use a standard-conforming version in Solaris,
123-
# one needs to explicitly use /usr/xpg4/bin/awk.
124114
__fzf_awk=/usr/xpg4/bin/awk
125115
else
126-
# choose the faster mawk if: it's installed && build date >= 20230322 &&
127-
# version >= 1.3.4
128116
local n x y z d
129117
IFS=' .' read n x y z d <<< $(command mawk -W version 2> /dev/null)
130118
[[ $n == mawk ]] && (( d >= 20230302 && (x * 1000 + y) * 1000 + z >= 1003004 )) && __fzf_awk=mawk
131119
fi
132120
fi
133-
134-
# Note: macOS awk has a quirk that it stops processing at all when it sees
135-
# any data not following UTF-8 in the input stream when the current LC_CTYPE
136-
# specifies the UTF-8 encoding. To work around this quirk, one needs to
137-
# specify LC_ALL=C to change the current encoding to the plain one.
138121
LC_ALL=C exec "$__fzf_awk" "$@"
139122
}
123+
#----END INCLUDE
140124

141125
__fzf_comprun() {
142126
if [[ "$(type _fzf_comprun 2>&1)" =~ function ]]; then

shell/key-bindings.bash

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,47 +17,31 @@ if [[ $- =~ i ]]; then
1717
# Key bindings
1818
# ------------
1919

20+
#----BEGIN INCLUDE common.sh
21+
# NOTE: Do not directly edit this section, which is copied from "common.sh".
22+
# To modify it, one can edit "common.sh" and run "./update-common.sh" to apply
23+
# the changes. see code comments in "common.sh" For the implementation details.
24+
2025
__fzf_defaults() {
21-
# $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
22-
# $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
23-
echo "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1"
26+
printf '%s\n' "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1"
2427
command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null
25-
echo "${FZF_DEFAULT_OPTS-} $2"
28+
printf '%s\n' "${FZF_DEFAULT_OPTS-} $2"
2629
}
2730

28-
# This function performs `exec awk "$@"` safely by working around awk
29-
# compatibility issues.
30-
#
31-
# Note: To reduce an extra fork, this function performs "exec" so is expected
32-
# to be run as the last command in a subshell.
33-
#
34-
# Note: This function is included with {completion,key-bindings}.{bash,zsh} and
35-
# synchronized.
3631
__fzf_exec_awk() {
3732
if [[ -z ${__fzf_awk-} ]]; then
3833
__fzf_awk=awk
3934
if [[ $OSTYPE == solaris* && -x /usr/xpg4/bin/awk ]]; then
40-
# Note: Solaris awk at /usr/bin/awk is meant for backward compatibility
41-
# with an ancient implementation of 1977 awk in the original UNIX. It
42-
# lacks many features of POSIX awk, so it is essentially useless in the
43-
# modern point of view. To use a standard-conforming version in Solaris,
44-
# one needs to explicitly use /usr/xpg4/bin/awk.
4535
__fzf_awk=/usr/xpg4/bin/awk
4636
else
47-
# choose the faster mawk if: it's installed && build date >= 20230322 &&
48-
# version >= 1.3.4
4937
local n x y z d
5038
IFS=' .' read n x y z d <<< $(command mawk -W version 2> /dev/null)
5139
[[ $n == mawk ]] && (( d >= 20230302 && (x * 1000 + y) * 1000 + z >= 1003004 )) && __fzf_awk=mawk
5240
fi
5341
fi
54-
55-
# Note: macOS awk has a quirk that it stops processing at all when it sees
56-
# any data not following UTF-8 in the input stream when the current LC_CTYPE
57-
# specifies the UTF-8 encoding. To work around this quirk, one needs to
58-
# specify LC_ALL=C to change the current encoding to the plain one.
5942
LC_ALL=C exec "$__fzf_awk" "$@"
6043
}
44+
#----END INCLUDE
6145

6246
__fzf_select__() {
6347
FZF_DEFAULT_COMMAND=${FZF_CTRL_T_COMMAND:-} \

shell/key-bindings.zsh

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,47 +38,31 @@ fi
3838
{
3939
if [[ -o interactive ]]; then
4040

41+
#----BEGIN INCLUDE common.sh
42+
# 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 apply
44+
# the changes. see code comments in "common.sh" For the implementation details.
45+
4146
__fzf_defaults() {
42-
# $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
43-
# $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
44-
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"
4548
command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null
46-
echo -E "${FZF_DEFAULT_OPTS-} $2"
49+
printf '%s\n' "${FZF_DEFAULT_OPTS-} $2"
4750
}
4851

49-
# This function performs `exec awk "$@"` safely by working around awk
50-
# compatibility issues.
51-
#
52-
# Note: To reduce an extra fork, this function performs "exec" so is expected
53-
# to be run as the last command in a subshell.
54-
#
55-
# Note: This function is included with {completion,key-bindings}.{bash,zsh} and
56-
# synchronized.
5752
__fzf_exec_awk() {
5853
if [[ -z ${__fzf_awk-} ]]; then
5954
__fzf_awk=awk
6055
if [[ $OSTYPE == solaris* && -x /usr/xpg4/bin/awk ]]; then
61-
# Note: Solaris awk at /usr/bin/awk is meant for backward compatibility
62-
# with an ancient implementation of 1977 awk in the original UNIX. It
63-
# lacks many features of POSIX awk, so it is essentially useless in the
64-
# modern point of view. To use a standard-conforming version in Solaris,
65-
# one needs to explicitly use /usr/xpg4/bin/awk.
6656
__fzf_awk=/usr/xpg4/bin/awk
6757
else
68-
# choose the faster mawk if: it's installed && build date >= 20230322 &&
69-
# version >= 1.3.4
7058
local n x y z d
7159
IFS=' .' read n x y z d <<< $(command mawk -W version 2> /dev/null)
7260
[[ $n == mawk ]] && (( d >= 20230302 && (x * 1000 + y) * 1000 + z >= 1003004 )) && __fzf_awk=mawk
7361
fi
7462
fi
75-
76-
# Note: macOS awk has a quirk that it stops processing at all when it sees
77-
# any data not following UTF-8 in the input stream when the current LC_CTYPE
78-
# specifies the UTF-8 encoding. To work around this quirk, one needs to
79-
# specify LC_ALL=C to change the current encoding to the plain one.
8063
LC_ALL=C exec "$__fzf_awk" "$@"
8164
}
65+
#----END INCLUDE
8266

8367
# CTRL-T - Paste the selected file path(s) into the command line
8468
__fzf_select() {

shell/update-common.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/sh
2+
3+
# This script applies the contents of "common.sh" to the other files.
4+
5+
set -e
6+
7+
# Go to the directory that contains this script
8+
dir=${0%"${0##*/}"}
9+
if [ -n "$dir" ]; then
10+
cd "$dir"
11+
fi
12+
13+
update() {
14+
{
15+
sed -n '1,/^#----BEGIN INCLUDE common\.sh/p' "$1"
16+
cat <<EOF
17+
# 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 apply
19+
# the changes. see code comments in "common.sh" For the implementation details.
20+
EOF
21+
grep -v '^[[:blank:]]*#' common.sh # remove code comments in common.sh
22+
sed -n '/^#----END INCLUDE/,$p' "$1"
23+
} > "$1.part"
24+
25+
mv -f "$1.part" "$1"
26+
}
27+
28+
update completion.bash
29+
update completion.zsh
30+
update key-bindings.bash
31+
update key-bindings.zsh

0 commit comments

Comments
 (0)