2022-11-07 14:40:49 +01:00
|
|
|
#!/usr/bin/env bash
|
2020-05-28 07:07:51 +02:00
|
|
|
|
2025-06-10 15:53:17 +02:00
|
|
|
DEFAULT_BINARY=$(which tmux-copyrat)
|
2021-03-14 12:15:36 +01:00
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Top-level options
|
|
|
|
|
#
|
|
|
|
|
|
2022-11-07 14:40:49 +01:00
|
|
|
setup_option () {
|
|
|
|
|
opt_name=$1
|
|
|
|
|
default_value=$2
|
|
|
|
|
current_value=$(tmux show-option -gqv @copyrat-${opt_name})
|
2021-10-24 20:49:18 +02:00
|
|
|
value=${current_value:-${default_value}}
|
|
|
|
|
tmux set-option -g @copyrat-${opt_name} ${value}
|
2020-06-03 23:55:09 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-10 15:53:17 +02:00
|
|
|
# Allows manual configuration of path to tmux-copyrat binary
|
|
|
|
|
setup_option "binary" "$DEFAULT_BINARY"
|
2020-06-03 23:55:09 +02:00
|
|
|
|
2021-03-14 12:15:36 +01:00
|
|
|
# Sets the window name which copyrat should use when running, providing a
|
|
|
|
|
# default value in case @copyrat-window-name was not defined.
|
2020-06-03 23:55:09 +02:00
|
|
|
setup_option "window-name" "[copyrat]"
|
|
|
|
|
|
2021-03-14 12:15:36 +01:00
|
|
|
# Get that window name as a local variable for use in pattern bindings below.
|
|
|
|
|
window_name=$(tmux show-option -gqv @copyrat-window-name)
|
|
|
|
|
|
2021-10-24 09:33:55 +02:00
|
|
|
# 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 + <your-shortcut>
|
2020-06-03 23:55:09 +02:00
|
|
|
setup_option "keytable" "cpyrt"
|
|
|
|
|
|
|
|
|
|
# Sets the key to access the keytable: prefix + <key> + <your-shortcut>
|
|
|
|
|
# providing a default if @copyrat-keyswitch is not defined.
|
2020-06-04 08:03:59 +02:00
|
|
|
setup_option "keyswitch" "t"
|
2020-06-03 23:55:09 +02:00
|
|
|
|
2021-03-14 12:15:36 +01:00
|
|
|
keyswitch=$(tmux show-option -gv @copyrat-keyswitch)
|
|
|
|
|
keytable=$(tmux show-option -gv @copyrat-keytable)
|
|
|
|
|
tmux bind-key ${keyswitch} switch-client -T ${keytable}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Pattern bindings
|
|
|
|
|
#
|
2020-06-03 23:55:09 +02:00
|
|
|
|
2023-09-02 00:54:17 +02:00
|
|
|
if [[ "$OSTYPE" == darwin* ]]; then
|
|
|
|
|
setup_option "clipboard-exe" "pbcopy"
|
|
|
|
|
else
|
|
|
|
|
if [[ "$XDG_SESSION_TYPE" == wayland ]]; then
|
|
|
|
|
setup_option "clipboard-exe" "wl-copy"
|
|
|
|
|
else
|
|
|
|
|
setup_option "clipboard-exe" "xclip -selection clipboard"
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
clipboard_exe=$(tmux show-option -gv @copyrat-clipboard-exe)
|
2025-06-10 15:53:17 +02:00
|
|
|
binary=$(tmux show-option -gv @copyrat-binary)
|
2023-09-02 00:54:17 +02:00
|
|
|
|
2022-11-07 14:40:49 +01:00
|
|
|
setup_pattern_binding () {
|
|
|
|
|
key=$1
|
|
|
|
|
pattern_arg="$2"
|
2025-06-10 15:53:17 +02:00
|
|
|
tmux bind-key -T ${keytable} ${key} new-window -d -n "${window_name}" "${binary} run --window-name \"${window_name}\" --clipboard-exe \"${clipboard_exe}\" --reverse --unique-hint ${pattern_arg}"
|
2020-06-03 23:55:09 +02:00
|
|
|
}
|
2020-05-28 07:07:51 +02:00
|
|
|
|
2024-09-01 02:17:21 +02:00
|
|
|
# prefix + t + a searches for command-line arguments
|
|
|
|
|
setup_pattern_binding "a" "--pattern-name command-line-args"
|
2021-04-09 09:18:28 +02:00
|
|
|
# prefix + t + c searches for hex colors #aa00f5
|
|
|
|
|
setup_pattern_binding "c" "--pattern-name hexcolor"
|
2021-03-23 07:56:49 +01:00
|
|
|
# prefix + t + d searches for dates or datetimes
|
|
|
|
|
setup_pattern_binding "d" "--pattern-name datetime"
|
2021-04-09 09:18:28 +02:00
|
|
|
# prefix + t + D searches for docker shas
|
|
|
|
|
setup_pattern_binding "D" "--pattern-name docker"
|
2020-06-04 08:03:59 +02:00
|
|
|
# prefix + t + e searches for email addresses (see https://www.regular-expressions.info/email.html)
|
|
|
|
|
setup_pattern_binding "e" "--pattern-name email"
|
2021-04-09 09:18:28 +02:00
|
|
|
# prefix + t + G searches for any string of 4+ digits
|
|
|
|
|
setup_pattern_binding "G" "--pattern-name digits"
|
|
|
|
|
# prefix + t + h searches for SHA1/2 short or long hashes
|
|
|
|
|
setup_pattern_binding "h" "--pattern-name sha"
|
|
|
|
|
# prefix + t + m searches for Markdown URLs [...](matched.url)
|
|
|
|
|
setup_pattern_binding "m" "--pattern-name markdown-url"
|
|
|
|
|
# prefix + t + p searches for absolute & relative paths
|
|
|
|
|
setup_pattern_binding "p" "--pattern-name path"
|
|
|
|
|
# prefix + t + P searches for hex numbers: 0xbedead
|
|
|
|
|
setup_pattern_binding "P" "--pattern-name pointer-address"
|
2021-03-27 16:23:27 +01:00
|
|
|
# prefix + t + q searches for strings inside single|double|backticks
|
2021-10-26 09:25:29 +02:00
|
|
|
setup_pattern_binding "q" "-x quoted-single -x quoted-double -x quoted-backtick"
|
2025-06-10 15:56:25 +02:00
|
|
|
# prefix + t + s searches for nix shas
|
|
|
|
|
setup_pattern_binding "s" "--pattern-name nix-shas"
|
2021-04-09 09:18:28 +02:00
|
|
|
# prefix + t + u searches for URLs
|
|
|
|
|
setup_pattern_binding "u" "--pattern-name url"
|
2020-06-04 08:03:59 +02:00
|
|
|
# prefix + t + U searches for UUIDs
|
|
|
|
|
setup_pattern_binding "U" "--pattern-name uuid"
|
2021-03-17 10:12:57 +01:00
|
|
|
# prefix + t + v searches for version numbers
|
|
|
|
|
setup_pattern_binding "v" "--pattern-name version"
|
2020-06-04 08:03:59 +02:00
|
|
|
# prefix + t + 4 searches for IPV4
|
|
|
|
|
setup_pattern_binding "4" "--pattern-name ipv4"
|
|
|
|
|
# prefix + t + 6 searches for IPV6
|
|
|
|
|
setup_pattern_binding "6" "--pattern-name ipv6"
|
2020-06-04 09:45:58 +02:00
|
|
|
# prefix + t + Space searches for all known patterns (noisy and potentially slower)
|
|
|
|
|
setup_pattern_binding "space" "--all-patterns"
|
|
|
|
|
|
|
|
|
|
# prefix + t + / prompts for a pattern and search for it
|
2025-06-10 15:53:17 +02:00
|
|
|
tmux bind-key -T ${keytable} "/" command-prompt -p "search:" "new-window -d -n '${window_name}' \"${binary} run --window-name \\\"${window_name}\\\" --reverse --unique-hint --clipboard-exe \\\"${clipboard_exe}\\\" --custom-patterns \\\"%%\\\"\""
|