-
Notifications
You must be signed in to change notification settings - Fork 62
/
open.tmux
executable file
·146 lines (123 loc) · 4.24 KB
/
open.tmux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/scripts/helpers.sh"
default_open_key="o"
open_option="@open"
default_open_editor_key="C-o"
open_editor_option="@open-editor"
open_editor_override="@open-editor-command"
command_exists() {
local command="$1"
type "$command" >/dev/null 2>&1
}
is_osx() {
local platform=$(uname)
[ "$platform" == "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
}
preserve_url_hash() {
echo "sed s/##/####/g"
}
command_generator() {
local command_string="$1"
echo "$(preserve_url_hash) | xargs -I {} tmux run-shell -b 'cd #{pane_current_path}; $command_string \"{}\" > /dev/null'"
}
search_command_generator() {
local command_string="$1"
local engine="$2"
echo "$(preserve_url_hash) | sed 's/\ /+/g' | xargs -I {} tmux run-shell -b 'cd #{pane_current_path}; $command_string $engine\"{}\" > /dev/null'"
}
generate_open_command() {
if is_osx; then
echo "$(command_generator "open")"
elif is_cygwin; then
echo "$(command_generator "cygstart")"
elif command_exists "xdg-open"; then
echo "$(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"
fi
}
generate_open_search_command() {
local engine="$1"
if is_osx; then
echo "$(search_command_generator "open" "$engine")"
elif is_cygwin; then
echo "$(command_generator "cygstart")"
elif command_exists "xdg-open"; then
echo "$(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"
fi
}
# 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")
# vim freezes terminal unless there's the '--' argument. Other editors seem
# to be fine with it (textmate [mate], light table [table]).
echo "$(preserve_url_hash) | 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 key
for key in $key_bindings; do
if tmux-is-at-least 2.4; then
tmux bind-key -T copy-mode-vi "$key" send-keys -X copy-pipe-and-cancel "$open_command"
tmux bind-key -T copy-mode "$key" send-keys -X copy-pipe-and-cancel "$open_command"
else
tmux bind-key -t vi-copy "$key" copy-pipe "$open_command"
tmux bind-key -t emacs-copy "$key" copy-pipe "$open_command"
fi
done
}
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 key
for key in $key_bindings; do
if tmux-is-at-least 2.4; then
tmux bind-key -T copy-mode-vi "$key" send-keys -X copy-pipe-and-cancel "$editor_command"
tmux bind-key -T copy-mode "$key" send-keys -X copy-pipe-and-cancel "$editor_command"
else
tmux bind-key -t vi-copy "$key" copy-pipe "$editor_command"
tmux bind-key -t emacs-copy "$key" copy-pipe "$editor_command"
fi
done
}
set_copy_mode_open_search_bindings() {
local 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")"
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")"
tmux bind-key -T copy-mode "$engine_var" send-keys -X copy-pipe-and-cancel "$(generate_open_search_command "$engine")"
else
tmux bind-key -t vi-copy "$engine_var" copy-pipe "$(generate_open_search_command "$engine")"
tmux bind-key -t emacs-copy "$engine_var" copy-pipe "$(generate_open_search_command "$engine")"
fi
done
}
main() {
set_copy_mode_open_bindings
set_copy_mode_open_editor_bindings
set_copy_mode_open_search_bindings
}
main