-
Notifications
You must be signed in to change notification settings - Fork 1
/
cd.red
341 lines (304 loc) · 11.2 KB
/
cd.red
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
; / ------------------- header file ------------------
Red [
Title: "cd.red"
TODO: [
1 {cd github should not open browser}
2 {put in config list of libraries}
]
Version: [0.0.1 {searching subfolder automatically}]
Builds: [
0.0.0.5.1.14 {request-dir if no arg}
0.0.0.5.1.12 {alpha release - support for partial subfolder, example cd redlang/github}
0.0.0.5.1.7 {support for partial subfolder, example cd redlang/github}
0.0.0.5.6 {fixed tree duplicates}
0.0.0.5.3 {revert to 1}
0.0.0.4.13 {refactoring and unless only [.dir/only]}
0.0.0.4 {/only for preventing dir list}
0.0.0.2.8 {searching up if not found TBC}
]
]
; ------------------- header file ------------------ /
; / ------------------- load libraries ------------------
if not value? '.redlang [
do https://redlang.red
]
.redlang [search-dir dir-tree dir block-to-string
;do-trace
]
; ------------------- load libraries ------------------ /
; / ------------------- cd function ------------------
if not value? '.syscd [
.syscd: :cd
.cd: func [
"Change directory (shell shortcut function)."
[catch]
'path [file! word! path! unset! string! paren! url!] "Accepts %file, :variables and just words (as dirs)"
/only {do not list folders}
/search
/up
/_build
/no-autoexec {don't autoexecute %.red and %autoload.red}
/silent
/_debug
/local _counter _first_path
][
search: true
; // ------------------- build number ------------------
if _build [
>builds: [
0.0.0.5.2.6 {exclude function type}
0.0.0.5.2.3 {splitting file}
0.0.0.5.1.14 {request-dir if no arg}
0.0.0.5.1.12 {alpha release - support for partial subfolder, example cd redlang/github}
0.0.0.5.1.7 {support for partial subfolder, example cd redlang/github}
0.0.0.5.6 {fixed tree duplicates}
0.0.0.5.3 {revert to 1}
0.0.0.4.13 {refactoring and unless only [.dir/only]}
0.0.0.4 {/only for preventing dir list}
0.0.0.2.8 {searching up if not found TBC}
]
unless silent [
?? >builds
]
return >builds
]
; ------------------- build number ------------------ //
; // ------------------- param path ------------------
switch type?/word get/any 'path [
unset! [
>path: request-dir/dir (what-dir)
if none? >path [
return none
]
]
file! word! path! unset! string! paren! url! [
>path: :path
]
]
; ------------------- param path ------------------ //
; // ------------------- debug counter ------------------
if _debug [
_counter: []
_first_path: []
either empty? _counter [
append _counter 1
append _first_path >path
][
_counter/1: _counter/1 + 1
]
]
; ------------------- debug counter ------------------ //
; // ------------------- up refinement ------------------
if up [
thepath: form >path
if found: search-dir/up (thepath) [
if _debug [
do-trace 67 [
?? thepath
] %cd.5.red
]
.cd (found)
unless only [.dir/only] ; 0.0.0.4.01.2 0.0.0.4.1.11
return what-dir
]
exit
]
; ------------------- up refinement ------------------ //
; // ------------------- dir-not-found function ------------------
dir-not-found: function [
path searchString
/_debug
][
either found: search-dir/folder (searchString) (path) [
if _debug [
do-trace 91 [
?? searchString
?? path
?? found
] %cd.5.red
]
either exists? (found) [
change-dir (found)
unless only [.dir/only] ; 0.0.0.4.01.2
][
.cd (found)
]
][
if _debug [
do-trace 104 [
?? searchString
?? path
?? found
] %cd.5.red
]
if find searchString "/" [
searchString: pick (splitted-path: split searchString "/") 1
if _debug [
do-trace 118 [
?? path
?? searchString
] %cd.7.red
;dir-not-found/_debug %./ (searchString)
]
found: search-dir/folder (searchString) %./
if found [
change-dir to-file (found)
remove splitted-path
searchString: block-to-string splitted-path "/"
.cd (searchString)
return what-dir
]
]
]
]
; ------------------- dir-not-found function ------------------ //
; // ------------------- switch path type ------------------
if paren? get/any 'path [set/any 'path do path]
switch/default type?/word get/any 'path [
unset! [
unless only [.dir/only]
path: request-dir
.cd (path)
unless only [.dir/only] ; 0.0.0.4.01.2
]
string! file! url! [
searchString: form path
path: to-file searchString
if _debug [
do-trace 123 [
?? path
] %cd.5.red
]
if error? try [
change-dir to-file path
print [{cd} to-file path]
unless only [.dir/only] ; 0.0.0.4.01.2
][
either _debug [
do-trace 180 [
?? searchString
] %cd.7.red
dir-not-found/_debug %. (searchString)
][
dir-not-found %. (searchString)
]
]
]
word! path! [
if error? try [
; if value? to-word >path [
; the-path: (get in system/words >path)
; if not logic? the-path [
; if _debug [
; do-trace 155 [
; ?? the-path
; ] %cd.5.red
; ]
; .cd (the-path)
; unless only [.dir/only] ; 0.0.0.4.01.2
; exit
; ]
; ]
if value? to-word >path [
; start 0.0.0.5.02.4
local>keyword: to-word >path
local>type: type? get :local>keyword
either (function! = local>type) [
>path: form >path
.cd (>path)
][
the-path: (get in system/words >path)
if not logic? the-path [
if _debug [
do-trace 155 [
?? the-path
] %cd.5.red
]
.cd (the-path)
unless only [.dir/only] ; 0.0.0.4.01.2
exit
]
]
; finish 0.0.0.5.02.4
]
][
the-path: to-red-file form >path
either exists? (the-path) [
if _debug [
do-trace 170 [
?? the-path
] %cd.5.red
]
change-dir (the-path)
unless only [.dir/only] ; 0.0.0.4.01.2
][
if _debug [
do-trace 179 [
?? the-path
] %cd.5.red
]
either _debug [
.cd/_debug (the-path)
][
.cd (the-path)
]
]
exit
]
if error? try [
change-dir to-file path
print [{cd} to-file path]
unless only [.dir/only] ; 0.0.0.4.01.2
][
searchString: form path
dir-not-found %. searchString
]
; unless no-autoexec [
; if exists? %autoload.red [
; do %autoload.red
; ]
; if exists? %.red [
; do %.red
; ]
; ]
]
] [
throw error 'script 'expect-arg reduce ['cd 'path type? get/any 'path]
]
what-dir
; ------------------- switch path type ------------------ //
]
system/words/..: function [][
.cd ".."
return what-dir
]
system/words/...: function [][
.cd ".."
.cd ".."
return what-dir
]
system/words/....: function [][
.cd ".."
.cd ".."
.cd ".."
return what-dir
]
if not value? '.c [
system/words/.c: function [][
.cd %/c/
return what-dir
]
]
if not value? 'c [c: :.c]
if not value? '.d [
system/words/.d: function [][
.cd %/d/
return what-dir
]
]
if not value? 'd [d: :.d]
]
; ------------------- cd function ------------------ /
; / ------------------- cd alias ------------------
cd: :.cd
; ------------------- cd alias ------------------ /