diff --git a/copyrat.tmux b/copyrat.tmux index eb2bb4c..c3ee0bd 100755 --- a/copyrat.tmux +++ b/copyrat.tmux @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/usr/bin/env zsh # This scripts provides a default configuration for tmux-copyrat options and key bindings. # It is run only once at tmux launch. @@ -26,9 +26,14 @@ # You can also entirely ignore this file (not even source it) and define all # options and bindings in your `tmux.conf`. -CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +CURRENT_DIR="$( cd "$( dirname "$0" )" && pwd )" BINARY="${CURRENT_DIR}/tmux-copyrat" + +# +# Top-level options +# + setup_option() { local opt_name=$1 local default_value=$2 @@ -38,10 +43,13 @@ setup_option() { } -# Sets the window name when copyrat is run, providing a default in case -# @copyrat-window-name was not defined. +# Sets the window name which copyrat should use when running, providing a +# default value in case @copyrat-window-name was not defined. setup_option "window-name" "[copyrat]" +# Get that window name as a local variable for use in pattern bindings below. +window_name=$(tmux show-option -gqv @copyrat-window-name) + # Sets the keytable for all bindings, providing a default if @copyrat-keytable was not defined. # Keytables open a new shortcut space: if 't' is the switcher (see below), prefix + t + setup_option "keytable" "cpyrt" @@ -50,15 +58,21 @@ setup_option "keytable" "cpyrt" # providing a default if @copyrat-keyswitch is not defined. setup_option "keyswitch" "t" -local keyswitch=$(tmux show-option @copyrat-keyswitch) -local keytable=$(tmux show-option @copyrat-keytable) -tmux bind-key ${keyswitch} -T ${keytable} +keyswitch=$(tmux show-option -gv @copyrat-keyswitch) +keytable=$(tmux show-option -gv @copyrat-keytable) +tmux bind-key ${keyswitch} switch-client -T ${keytable} -setup_binding() { + +# +# Pattern bindings +# + +setup_pattern_binding() { local key=$1 - local pattern_name="$2" - local window_name=$(tmux show-option -gqv @copyrat-window-name) - tmux bind-key -T ${keytable} $key new-window -d -n ${window_name} "${BINARY} --window-name ${window_name} --reverse --unique ${pattern_name}" + local pattern_arg="$2" + # The default window name `[copyrat]` has to be single quoted because it is + # interpreted by the shell when launched by tmux. + tmux bind-key -T ${keytable} ${key} new-window -d -n ${window_name} "${BINARY} --window-name '"${window_name}"' --reverse --unique-hint ${pattern_arg}" } # prefix + t + p searches for absolute & relative paths @@ -89,7 +103,7 @@ setup_pattern_binding "6" "--pattern-name ipv6" setup_pattern_binding "space" "--all-patterns" # prefix + t + / prompts for a pattern and search for it -tmux bind-key -T ${keytable} "/" command-prompt -p "search:" 'new-window -d -n ${COPYRAT_WINDOW_NAME} "${BINARY} --window-name ${COPYRAT_WINDOW_NAME} --reverse --unique --custom-pattern %%"' +tmux bind-key -T ${keytable} "/" command-prompt -p "search:" "new-window -d -n '${window_name}' \"${BINARY}\" --window-name '${window_name}' --reverse --unique-hint --custom-pattern %%" # Auto-install is currently disabled as it requires the user to have cargo installed.