-
Notifications
You must be signed in to change notification settings - Fork 3
/
ackvim.sh
242 lines (196 loc) · 4.64 KB
/
ackvim.sh
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
#! /bin/cat
# This script is intended to be sourced, not run
if [[ $0 == $BASH_SOURCE ]]
then
echo "This file should be run as"
echo " source $0"
echo "and should not be run as"
echo " sh $0"
fi
# File organisation: Functions are sorted by length of name first, then alphabetically
# x
unalias a 2>/dev/null || true
a () {
ack_find "$@"
}
# xx
unalias aa 2>/dev/null || true
aa () {
run_ack_vim "$@"
}
ac () {
find_class_or_function ack_find class "$@"
}
ae () {
ack_find --erl "$@"
}
af () {
find_class_or_function ack_find def "$@"
}
ah () {
ack --html "$@"
}
ahv () {
ack --help | vim -
}
ai () {
local _options=--python
[[ $1 == "-t" ]] && _options=--test && shift
[[ $1 == "-T" ]] && _options=--pyt && shift
[[ $1 == "-l" ]] && _options="$_options --files-with-matches" && shift
local sought=$1; shift
ack $_options '(import.*'"$sought|$sought"'.*import)' "$@"
}
al () {
ack_find --html "$@"
}
ap () {
local _ignores=( /test /tests /lib )
ack_find ${_ignores[@]/#\// --ignore-dir } --python "$@"
}
at () {
ack_find --test "$@"
}
ay () {
ack_find --yaml "$@"
}
av () {
run_ack_vim "$@"
}
# xxx
unalias aaa 2>/dev/null || true
aaa () {
run_ack_vim --nojunk "$@"
}
aac () {
find_class_or_function run_ack_vim class "$@"
}
aae () {
run_ack_vim --erl "$@"
}
aaf () {
find_class_or_function run_ack_vim def "$@"
}
aal () {
run_ack_vim --html "$@"
}
aai () {
local _regexp=$(convert_regexp "$@")
local _files=$(ai "$@" -l| tr '\n' ' ')
vim -p $_files +/"$_regexp"
}
aap () {
local _ignores=( /test /tests /lib )
run_ack_vim ${_ignores[@]/#\// --ignore-dir } --python "$@"
}
aat () {
run_ack_vim --test "$@"
}
aay () {
run_ack_vim --yaml "$@"
}
aaw () {
run_ack_vim -w "$@"
}
aco () {
run_ack_vim --code "$@"
}
aiw () {
local sought=$1
ack --python "(import.*\b$sought\b|\b$sought\b.import)"
}
app () {
local _ignores=( /lib )
ack_find ${_ignores[@]/#\// --ignore-dir } --pyt "$@"
}
ash () {
ack_find --shell "$@"
}
# xxxx
unalias aaaa 2>/dev/null || true
aaaa () {
run_ack_vim --all "$@"
}
aash () {
run_ack_vim --shell "$@"
}
aapp () {
local _ignores=( /lib )
run_ack_vim ${_ignores[@]/#\// --ignore-dir } --pyt "$@"
}
lack () {
ack_find -l "$@"
}
ack_python () {
local ack_dir_=$(dirname $(readlink -f $BASH_SOURCE))
local python_="${ack_dir_}/.venv/bin/python3"
if [[ -x "$python_" ]]; then
echo $python_
return 0
else
echo python3
return 1
fi
}
convert_regexp () {
local ack_dir_=$(dirname $(readlink -f $BASH_SOURCE))
local python_dir_=${ack_dir_}/ackvim
local python_="$(ack_python)"
"$python_" "$python_dir_/convert_regexps.py" "$@"
}
# xxxxx
clack () {
clear
ack_find "$@"
}
# xxxxxx+
run_ack_with () {
local __doc__="Interpret args, search with ack"
[[ $* =~ -l ]] || python3 -c "print('\n\033[0;36m%s\033[0m\n' % ('#' * "$(tput cols 2>/dev/null || echo 0)"))"
local _script="$(readlink -f $BASH_SOURCE)"
local sh_dir_="$(dirname $_script)"
local py_dir_="$sh_dir_/ackvim"
local py_script_="$py_dir_/run_ack_with.py"
if [[ ! -f $py_script_ ]]; then
[[ -f $_script ]] || echo "$_script is not a file" >&2
[[ -d $py_dir_ ]] || echo "$py_dir_ is not a directory" >&2
[[ -f $py_script_ ]] || echo "$py_script_ is not a file" >&2
command ack "$@"
return 1
fi
local _option=-j
[[ $* =~ -j ]] && _option=
local python_="$(ack_python)"
eval $("$python_" "$py_script_" $_option "$@")
}
ack_find () {
local __doc__="Choose which ack-function to run"
if [[ $# -gt 0 && ${!#} =~ -v ]]; then
run_ack_vim "${@/-v/}"
else
run_ack_with "$@"
fi
}
run_ack_vim () {
local __doc__="Search for args with ack, edit results with vim"
local _regexp=$(convert_regexp "$@")
local _files=$(run_ack_with -l "$@" | tr '\n' ' ')
[[ $_files ]] && vim -p $_files +/"$_regexp"
}
find_class_or_function () {
local __doc__="""Search for a class/function definition in python files"""
local _find_command=$1; shift
local _class_or_function=$1; shift
local _option=
local _sought="$@"
if has_option i $_sought; then
_option=-i
_sought=$(remove_option i $_sought)
fi
local _regexp=\\s*${_class_or_function}.'[^(]*'"$_sought"
$_find_command $_option --python $_regexp --ignore-dir=tests && return 0
_regexp=$_sought
[[ $_class_or_function == "class" ]] && return 1
[[ $_class_or_function == "def" ]] && _regexp="^$_sought ()"
$_find_command $_option --shell $_regexp
}