diff --git a/README.md b/README.md index d2d17bc..e51340c 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,14 @@ Put `set -g @open 'x'` in `tmux.conf`. Put `set -g @open-editor 'C-x'` in `tmux.conf`. +> How can I change the default editor without setting `$EDITOR`? + +Put `set -g @open-editor-command 'my-editor'` in `tmux.conf`. + +> How can I change the default command for opening the selection? + +Put `set -g @open-opener-command 'my-opener'` in `tmux.conf`. + > How can I change the default search engine to "duckduckgo" or any other one? Put `set -g @open-S 'https://www.duckduckgo.com/'` in `tmux.conf` diff --git a/open.tmux b/open.tmux index 824ea24..517cdd1 100755 --- a/open.tmux +++ b/open.tmux @@ -2,6 +2,7 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +# shellcheck source=./scripts/helpers.sh source "$CURRENT_DIR/scripts/helpers.sh" default_open_key="o" @@ -11,29 +12,21 @@ default_open_editor_key="C-o" open_editor_option="@open-editor" open_editor_override="@open-editor-command" +open_opener_override="@open-opener-command" + command_exists() { local command="$1" type "$command" >/dev/null 2>&1 } is_osx() { - local platform=$(uname) - [ "$platform" == "Darwin" ] + [ "$(uname)" == "Darwin" ] } is_cygwin() { [[ "$(uname)" =~ CYGWIN ]] } -get_editor_from_the_env_var() { - if [ -z $EDITOR ]; then - # $EDITOR not set, fallback - echo "vi" - else - echo "$EDITOR" - fi -} - command_generator() { local command_string="$1" echo "xargs -I {} tmux run-shell -b 'cd #{pane_current_path}; $command_string \"{}\" > /dev/null'" @@ -47,12 +40,15 @@ search_command_generator() { } generate_open_command() { - if is_osx; then - echo "$(command_generator "open")" + local opener + if opener="$(get_tmux_option "$open_opener_override" '')" && [ -n "${opener-}" ]; then + command_generator "${opener}" + elif is_osx; then + command_generator "open" elif is_cygwin; then - echo "$(command_generator "cygstart")" + command_generator "cygstart" elif command_exists "xdg-open"; then - echo "$(command_generator "xdg-open")" + command_generator "xdg-open" else # error command for Linux machines when 'xdg-open' not installed "$CURRENT_DIR/scripts/tmux_open_error_message.sh" "xdg-open" @@ -61,12 +57,15 @@ generate_open_command() { generate_open_search_command() { local engine="$1" - if is_osx; then - echo "$(search_command_generator "open" "$engine")" + local opener + if opener="$(get_tmux_option "$open_opener_override" '')" && [ -n "${opener-}" ]; then + search_command_generator "$opener" "$engine" + elif is_osx; then + search_command_generator "open" "$engine" elif is_cygwin; then - echo "$(command_generator "cygstart")" + command_generator "cygstart" elif command_exists "xdg-open"; then - echo "$(search_command_generator "xdg-open" "$engine")" + search_command_generator "xdg-open" "$engine" else # error command for Linux machines when 'xdg-open' not installed "$CURRENT_DIR/scripts/tmux_open_error_message.sh" "xdg-open" @@ -76,16 +75,19 @@ generate_open_search_command() { # 1. write a command to the terminal, example: 'vim -- some_file.txt' # 2. invoke the command by pressing enter/C-m generate_editor_command() { - local environment_editor=$(get_editor_from_the_env_var) - local editor=$(get_tmux_option "$open_editor_override" "$environment_editor") + local environment_editor="${EDITOR:-vi}" + local editor + editor=$(get_tmux_option "$open_editor_override" "$environment_editor") # vim freezes terminal unless there's the '--' argument. Other editors seem # to be fine with it (textmate [mate], light table [table]). echo "xargs -I {} tmux send-keys '$editor -- \"{}\"'; tmux send-keys 'C-m'" } set_copy_mode_open_bindings() { - local open_command="$(generate_open_command)" - local key_bindings=$(get_tmux_option "$open_option" "$default_open_key") + local open_command + open_command="$(generate_open_command)" + local key_bindings + key_bindings=$(get_tmux_option "$open_option" "$default_open_key") local key for key in $key_bindings; do if tmux-is-at-least 2.4; then @@ -99,8 +101,10 @@ set_copy_mode_open_bindings() { } set_copy_mode_open_editor_bindings() { - local editor_command="$(generate_editor_command)" - local key_bindings=$(get_tmux_option "$open_editor_option" "$default_open_editor_key") + local editor_command + editor_command="$(generate_editor_command)" + local key_bindings + key_bindings="$(get_tmux_option "$open_editor_option" "$default_open_editor_key")" local key for key in $key_bindings; do if tmux-is-at-least 2.4; then @@ -114,13 +118,14 @@ set_copy_mode_open_editor_bindings() { } set_copy_mode_open_search_bindings() { - local stored_engine_vars="$(stored_engine_vars)" + local stored_engine_vars + stored_engine_vars="$(stored_engine_vars)" local engine_var local engine local key for engine_var in $stored_engine_vars; do - engine="$(get_engine "$engine_var")" + engine="$(get_engine "$engine_var")" || continue if tmux-is-at-least 2.4; then tmux bind-key -T copy-mode-vi "$engine_var" send-keys -X copy-pipe-and-cancel "$(generate_open_search_command "$engine")" diff --git a/scripts/helpers.sh b/scripts/helpers.sh index 9dc984c..ae06e05 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -1,12 +1,9 @@ get_tmux_option() { local option="$1" local default_value="$2" - local option_value=$(tmux show-option -gqv "$option") - if [ -z "$option_value" ]; then - echo "$default_value" - else - echo "$option_value" - fi + local option_value + option_value="$(tmux show-option -gqv "$option")" + echo "${option_value:-$default_value}" } # Ensures a message is displayed for 5 seconds in tmux prompt. @@ -22,7 +19,8 @@ display_message() { fi # saves user-set 'display-time' option - local saved_display_time=$(get_tmux_option "display-time" "750") + local saved_display_time + saved_display_time="$(get_tmux_option "display-time" "750")" # sets message display time to 5 seconds tmux set-option -gq display-time "$display_duration" @@ -37,7 +35,7 @@ display_message() { stored_engine_vars() { tmux show-options -g | grep -i "^@open-" | - grep -vi "^@open-editor" | + grep -Evi "^@open-(editor|opener)" | cut -d '-' -f2 | cut -d ' ' -f1 | xargs @@ -48,15 +46,15 @@ get_engine() { tmux show-options -g | grep -i "^@open-$engine_var" | cut -d ' ' -f2 | xargs } -tmux_version="$(tmux -V | cut -d ' ' -f 2 | sed 's/next-//'))" +# The last grep is required to remove non-digits from version such as "3.0a". +tmux_version="$(tmux -V | cut -d ' ' -f 2 | grep -Eo '[0-9\.]+')" tmux-is-at-least() { - if [[ $tmux_version == $1 ]] - then + if [[ $tmux_version == "$1" ]]; then return 0 fi - local IFS=. - local i tver=($tmux_version) wver=($1) + IFS='.' read -r -a tver <<< "$tmux_version" + IFS='.' read -r -a wver <<< "$1" # fill empty fields in tver with zeros for ((i=${#tver[@]}; i<${#wver[@]}; i++)); do