-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsubl
executable file
·252 lines (216 loc) · 5.92 KB
/
subl
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/bin/bash
# subl - Launch Sublime Text from Cygwin
# usage: subl [options] -- [files]
# To the extent possible under law, the author(s) have dedicated all copyright
# and related and neighboring rights to this software to the public domain
# worldwide. This software is distributed without any warranty. See
# <http://creativecommons.org/publicdomain/zero/1.0/> for a copy of the CC0
# Public Domain Dedication, which applies to this software.
verbatim=no
touch=no
cygstart=no
fileonly=no
launch=no
subl_args=()
subl_args_win=
current_path=
current_pos=
# has_cmd <command> - Checks for command on system
has_cmd() {
local command="$1"
command &>/dev/null -v -- "$command"
return $?
}
# win_start <exe> <cmd_line> - Start a native Windows application
win_start() {
local exe="$1"
local cmd_line="$2"
if has_cmd cygstart; then
cygstart -- "$(cygpath -u -- "$exe")" "$cmd_line"
else
"$COMSPEC" /C "start \"\" \"$exe\" $cmd_line"
fi
}
# split_path <path> - Split 'path' into current_path and current_pos
split_path() {
local path="$1"
local pos=
local drivespec=
# Remove the drive spec (C:\) if the path starts with one
if [[ $path =~ ^[a-zA-Z]: ]]; then
drivespec="${path:0:2}"
path="${path:2}"
fi
# Check for :line:column and :line syntax
if [[ $path =~ :[^\\/:]+:[^\\/:]+$ ]]; then
local split="${path%:*:*}"
pos="${path:${#split}}"
path="$split"
elif [[ $path =~ :[^\\/:]+$ ]]; then
local split="${path%:*}"
pos="${path:${#split}}"
path="$split"
fi
current_path="$drivespec$path"
current_pos="$pos"
}
# add_path <path> <pos> - Add a path and file position to subl_args
add_path() {
local path="$1"
local pos="$2"
local winpath=$(cygpath 2>/dev/null -aw -- "$path")
# Touch files before creating them to ensure they have properly set Cygwin
# permissions
if [[ $touch == yes ]] && [[ -n $winpath ]] && [[ ! -e $path ]]; then
touch 2>/dev/null -- "$path"
winpath=$(cygpath 2>/dev/null -aw -- "$path")
fi
if [[ -n $winpath ]]; then
subl_args+=("$winpath$pos")
else
subl_args+=("$path$pos")
fi
}
# rep_str <str> <count> <out> - Repeat 'str' 'count' times
rep_str() {
local rep_str_str="$1"
local rep_str_count="$2"
local rep_str_out="$3"
if [[ $rep_str_count -lt 1 ]]; then
eval $rep_str_out=
return
fi
local rep_str_tmp=
printf -v rep_str_tmp "%${rep_str_count}s"
rep_str_tmp=${rep_str_tmp// /$rep_str_str}
eval $rep_str_out=\$rep_str_tmp
}
# write_args_win - Convert subl_args to a Windows format command line
write_args_win() {
subl_args_win=
local arg
for arg in "${subl_args[@]}"; do
local last_non_slash=-1
local i
for (( i=0; i<${#arg}; i++ )); do
local chr=${arg:$i:1}
# When a quote is encountered, double all preceding slashes and
# escape the quote
if [[ $chr == '"' ]]; then
local num_slashes=$(( $i - 1 - $last_non_slash ))
local slashes=
rep_str '\' $num_slashes slashes
arg="${arg:0:$i}${slashes}\\${arg:$i}"
i=$(( $i + $num_slashes + 1 ))
fi
if [[ $chr != '\' ]]; then
last_non_slash=$i
fi
done
# Double all trailing slashes
local num_slashes=$(( $i - 1 - $last_non_slash ))
local slashes=
rep_str '\' $num_slashes slashes
# Add the escaped argument to the list
subl_args_win="$subl_args_win \"$arg$slashes\""
done
}
# is_sublime_running - Check the existence of Sublime's mutex
is_sublime_running() {
exists() {
[[ -e "$1" ]] && return 0 || return 1
}
# Yes this is crazy
exists /proc/sys/BaseNamedObjects/Session/*/*-Sublime\ Text || \
exists /proc/sys/BaseNamedObjects/*-Sublime\ Text
return $?
}
for arg; do
if [[ $fileonly == yes ]]; then
# Interpret all arguments as files (whether they exist or not) and
# don't parse :line:column syntax. Using absolute Windows filenames
# makes this unambiguous, since they can't start with dashes or contain
# colons in the file name.
add_path "$arg"
elif [[ $verbatim == yes ]]; then
# The last option was --command, so don't parse the argument
subl_args+=("$arg")
verbatim=no
else
case $arg in
--cygstart)
# Launch Sublime Text with `cygstart`
cygstart=yes
;;
--no-cygstart)
cygstart=no
;;
--touch)
# Touch files in Cygwin before creating them
touch=yes
;;
--no-touch)
touch=no
;;
--launch)
# Ensure a copy of Sublime Text is running before executing the
# command. This is good for use with --wait.
launch=yes
;;
--no-launch)
launch=no
;;
--command)
# Interpret the following argument as a command
subl_args+=("$arg")
verbatim=yes
;;
--)
# Interpret all following arguments as files
fileonly=yes
;;
-*)
# Pass other options verbatim
subl_args+=("$arg")
;;
*)
# Split :line:column suffix from end of path
split_path "$arg"
# If the Cygwin path exists, convert it to a Windows path
if [[ -d $(dirname "$current_path") ]]; then
add_path "$current_path" "$current_pos"
else
subl_args+=("$arg")
fi
;;
esac
fi
done
# Get the location of the sublime_text binary from the registry
reg='/proc/registry'
[[ -d '/proc/registry64' ]] && reg='/proc/registry64'
key="$reg/HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/Uninstall"
if [[ -e "$key/Sublime Text 3_is1/InstallLocation" ]]; then
IFS= read <"$key/Sublime Text 3_is1/InstallLocation" -r install_location
elif [[ -e "$key/Sublime Text 2_is1/InstallLocation" ]]; then
IFS= read <"$key/Sublime Text 2_is1/InstallLocation" -r install_location
else
printf 2>&1 '%s: Could not find sublime_text binary\n' "$0"
exit 127
fi
subl_bin_win="${install_location}sublime_text.exe"
if [[ $launch == yes ]] && ! is_sublime_running; then
win_start "$subl_bin_win"
# Wait two seconds or till the Sublime Text mutex appears
for (( i=0; i<19; i++ )); do
is_sublime_running && break
sleep 0.1
done
sleep 0.1
fi
if [[ $cygstart == yes ]]; then
write_args_win
win_start "$subl_bin_win" "$subl_args_win"
else
exec "$(cygpath -u -- "$subl_bin_win")" "${subl_args[@]}"
fi