Skip to content

Commit 724bdc6

Browse files
committed
fix shellcheck warnings and fix version check for 3.0a
1 parent 5b09bd9 commit 724bdc6

File tree

3 files changed

+51
-40
lines changed

3 files changed

+51
-40
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ Put `set -g @open 'x'` in `tmux.conf`.
6666
6767
Put `set -g @open-editor 'C-x'` in `tmux.conf`.
6868

69+
> How can I change the default editor without setting `$EDITOR`?
70+
71+
Put `set -g @open-editor-command 'my-editor'` in `tmux.conf`.
72+
73+
> How can I change the default command for opening the selection?
74+
75+
Put `set -g @open-opener-command 'my-opener'` in `tmux.conf`.
76+
6977
> How can I change the default search engine to "duckduckgo" or any other one?
7078
7179
Put `set -g @open-S 'https://www.duckduckgo.com/'` in `tmux.conf`

open.tmux

+32-27
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
44

5+
# shellcheck source=./scripts/helpers.sh
56
source "$CURRENT_DIR/scripts/helpers.sh"
67

78
default_open_key="o"
@@ -11,29 +12,21 @@ default_open_editor_key="C-o"
1112
open_editor_option="@open-editor"
1213
open_editor_override="@open-editor-command"
1314

15+
open_opener_override="@open-opener-command"
16+
1417
command_exists() {
1518
local command="$1"
1619
type "$command" >/dev/null 2>&1
1720
}
1821

1922
is_osx() {
20-
local platform=$(uname)
21-
[ "$platform" == "Darwin" ]
23+
[ "$(uname)" == "Darwin" ]
2224
}
2325

2426
is_cygwin() {
2527
[[ "$(uname)" =~ CYGWIN ]]
2628
}
2729

28-
get_editor_from_the_env_var() {
29-
if [ -z $EDITOR ]; then
30-
# $EDITOR not set, fallback
31-
echo "vi"
32-
else
33-
echo "$EDITOR"
34-
fi
35-
}
36-
3730
command_generator() {
3831
local command_string="$1"
3932
echo "xargs -I {} tmux run-shell -b 'cd #{pane_current_path}; $command_string \"{}\" > /dev/null'"
@@ -47,12 +40,15 @@ search_command_generator() {
4740
}
4841

4942
generate_open_command() {
50-
if is_osx; then
51-
echo "$(command_generator "open")"
43+
local opener
44+
if opener="$(get_tmux_option "$open_opener_override" '')" && [ -n "${opener-}" ]; then
45+
command_generator "${opener}"
46+
elif is_osx; then
47+
command_generator "open"
5248
elif is_cygwin; then
53-
echo "$(command_generator "cygstart")"
49+
command_generator "cygstart"
5450
elif command_exists "xdg-open"; then
55-
echo "$(command_generator "xdg-open")"
51+
command_generator "xdg-open"
5652
else
5753
# error command for Linux machines when 'xdg-open' not installed
5854
"$CURRENT_DIR/scripts/tmux_open_error_message.sh" "xdg-open"
@@ -61,12 +57,15 @@ generate_open_command() {
6157

6258
generate_open_search_command() {
6359
local engine="$1"
64-
if is_osx; then
65-
echo "$(search_command_generator "open" "$engine")"
60+
local opener
61+
if opener="$(get_tmux_option "$open_opener_override" '')" && [ -n "${opener-}" ]; then
62+
search_command_generator "$opener" "$engine"
63+
elif is_osx; then
64+
search_command_generator "open" "$engine"
6665
elif is_cygwin; then
67-
echo "$(command_generator "cygstart")"
66+
command_generator "cygstart"
6867
elif command_exists "xdg-open"; then
69-
echo "$(search_command_generator "xdg-open" "$engine")"
68+
search_command_generator "xdg-open" "$engine"
7069
else
7170
# error command for Linux machines when 'xdg-open' not installed
7271
"$CURRENT_DIR/scripts/tmux_open_error_message.sh" "xdg-open"
@@ -76,16 +75,19 @@ generate_open_search_command() {
7675
# 1. write a command to the terminal, example: 'vim -- some_file.txt'
7776
# 2. invoke the command by pressing enter/C-m
7877
generate_editor_command() {
79-
local environment_editor=$(get_editor_from_the_env_var)
80-
local editor=$(get_tmux_option "$open_editor_override" "$environment_editor")
78+
local environment_editor="${EDITOR:-vi}"
79+
local editor
80+
editor=$(get_tmux_option "$open_editor_override" "$environment_editor")
8181
# vim freezes terminal unless there's the '--' argument. Other editors seem
8282
# to be fine with it (textmate [mate], light table [table]).
8383
echo "xargs -I {} tmux send-keys '$editor -- \"{}\"'; tmux send-keys 'C-m'"
8484
}
8585

8686
set_copy_mode_open_bindings() {
87-
local open_command="$(generate_open_command)"
88-
local key_bindings=$(get_tmux_option "$open_option" "$default_open_key")
87+
local open_command
88+
open_command="$(generate_open_command)"
89+
local key_bindings
90+
key_bindings=$(get_tmux_option "$open_option" "$default_open_key")
8991
local key
9092
for key in $key_bindings; do
9193
if tmux-is-at-least 2.4; then
@@ -99,8 +101,10 @@ set_copy_mode_open_bindings() {
99101
}
100102

101103
set_copy_mode_open_editor_bindings() {
102-
local editor_command="$(generate_editor_command)"
103-
local key_bindings=$(get_tmux_option "$open_editor_option" "$default_open_editor_key")
104+
local editor_command
105+
editor_command="$(generate_editor_command)"
106+
local key_bindings
107+
key_bindings="$(get_tmux_option "$open_editor_option" "$default_open_editor_key")"
104108
local key
105109
for key in $key_bindings; do
106110
if tmux-is-at-least 2.4; then
@@ -114,13 +118,14 @@ set_copy_mode_open_editor_bindings() {
114118
}
115119

116120
set_copy_mode_open_search_bindings() {
117-
local stored_engine_vars="$(stored_engine_vars)"
121+
local stored_engine_vars
122+
stored_engine_vars="$(stored_engine_vars)"
118123
local engine_var
119124
local engine
120125
local key
121126

122127
for engine_var in $stored_engine_vars; do
123-
engine="$(get_engine "$engine_var")"
128+
engine="$(get_engine "$engine_var")" || continue
124129

125130
if tmux-is-at-least 2.4; then
126131
tmux bind-key -T copy-mode-vi "$engine_var" send-keys -X copy-pipe-and-cancel "$(generate_open_search_command "$engine")"

scripts/helpers.sh

+11-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
get_tmux_option() {
22
local option="$1"
33
local default_value="$2"
4-
local option_value=$(tmux show-option -gqv "$option")
5-
if [ -z "$option_value" ]; then
6-
echo "$default_value"
7-
else
8-
echo "$option_value"
9-
fi
4+
local option_value
5+
option_value="$(tmux show-option -gqv "$option")"
6+
echo "${option_value:-$default_value}"
107
}
118

129
# Ensures a message is displayed for 5 seconds in tmux prompt.
@@ -22,7 +19,8 @@ display_message() {
2219
fi
2320

2421
# saves user-set 'display-time' option
25-
local saved_display_time=$(get_tmux_option "display-time" "750")
22+
local saved_display_time
23+
saved_display_time="$(get_tmux_option "display-time" "750")"
2624

2725
# sets message display time to 5 seconds
2826
tmux set-option -gq display-time "$display_duration"
@@ -37,7 +35,7 @@ display_message() {
3735
stored_engine_vars() {
3836
tmux show-options -g |
3937
grep -i "^@open-" |
40-
grep -vi "^@open-editor" |
38+
grep -Evi "^@open-(editor|opener)" |
4139
cut -d '-' -f2 |
4240
cut -d ' ' -f1 |
4341
xargs
@@ -48,15 +46,15 @@ get_engine() {
4846
tmux show-options -g | grep -i "^@open-$engine_var" | cut -d ' ' -f2 | xargs
4947
}
5048

51-
tmux_version="$(tmux -V | cut -d ' ' -f 2 | sed 's/next-//'))"
49+
# The last grep is required to remove non-digits from version such as "3.0a".
50+
tmux_version="$(tmux -V | cut -d ' ' -f 2 | grep -Eo '[0-9\.]+')"
5251
tmux-is-at-least() {
53-
if [[ $tmux_version == $1 ]]
54-
then
52+
if [[ $tmux_version == "$1" ]]; then
5553
return 0
5654
fi
5755

58-
local IFS=.
59-
local i tver=($tmux_version) wver=($1)
56+
IFS='.' read -r -a tver <<< "$tmux_version"
57+
IFS='.' read -r -a wver <<< "$1"
6058

6159
# fill empty fields in tver with zeros
6260
for ((i=${#tver[@]}; i<${#wver[@]}; i++)); do

0 commit comments

Comments
 (0)