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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
if not modules then modules = { } end modules ['mtx-fcd'] = {
version = 1.002,
comment = "companion to mtxrun.lua",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files",
comment = "based on the ruby version from 2005",
}
-- This is a kind of variant of the good old ncd (norton change directory) program. This
-- script uses the same indirect cmd trick as Erwin Waterlander's wcd program.
--
-- The program is called via the stubs fcd.cmd or fcd.sh. On unix one should probably source
-- the file: ". fcd args" in order to make the chdir persistent.
--
-- You need to create a stub with:
--
-- mtxrun --script fcd --stub > fcd.cmd
-- mtxrun --script fcd --stub > fcd.sh
--
-- The stub starts this script and afterwards runs the created directory change script as
-- part if the same run, so that indeed we change.
local helpinfo = [[
<?xml version="1.0"?>
<application>
<metadata>
<entry name="name">mtx-fcd</entry>
<entry name="detail">Fast Directory Change</entry>
<entry name="version">1.00</entry>
</metadata>
<flags>
<category name="basic">
<subcategory>
<flag name="clear"><short>clear the cache</short></flag>
<flag name="clear"><short><ref name="history"/> [entry] clear the history</short></flag>
<flag name="scan"><short>clear the cache and add given path(s)</short></flag>
<flag name="add"><short>add given path(s)</short></flag>
<flag name="find"><short>find given path (can be substring)</short></flag>
<flag name="find"><short><ref name="nohistory"/> find given path (can be substring) but don't use history</short></flag>
<flag name="stub"><short>print platform stub file</short></flag>
<flag name="list"><short>show roots of cached dirs</short></flag>
<flag name="list"><short><ref name="history"/> show history of chosen dirs</short></flag>
<flag name="help"><short>show this help</short></flag>
</subcategory>
</category>
</flags>
<examples>
<category>
<title>Example</title>
<subcategory>
<example><command>fcd --scan t:\</command></example>
<example><command>fcd --add f:\project</command></example>
<example><command>fcd [--find] whatever</command></example>
<example><command>fcd --list</command></example>
</subcategory>
</category>
</examples>
</application>
]]
local application = logs.application {
name = "mtx-fcd",
banner = "Fast Directory Change 1.00",
helpinfo = helpinfo,
}
local report = application.report
local writeln = print -- texio.write_nl
local find, char, byte, lower, gsub, format = string.find, string.char, string.byte, string.lower, string.gsub, string.format
local mswinstub = [[@echo off
rem this is: fcd.cmd
@echo off
if not exist "%HOME%" goto homepath
:home
mtxrun --script mtx-fcd.lua %1 %2 %3 %4 %5 %6 %7 %8 %9
if exist "%HOME%\mtx-fcd-goto.cmd" call "%HOME%\mtx-fcd-goto.cmd"
goto end
:homepath
if not exist "%HOMEDRIVE%\%HOMEPATH%" goto end
mtxrun --script mtx-fcd.lua %1 %2 %3 %4 %5 %6 %7 %8 %9
if exist "%HOMEDRIVE%\%HOMEPATH%\mtx-fcd-goto.cmd" call "%HOMEDRIVE%\%HOMEPATH%\mtx-fcd-goto.cmd"
goto end
:end
]]
local unixstub = [[#!/usr/bin/env sh
# this is: fcd.sh
# mv fcd.sh fcd
# chmod fcd 755
# . fcd [args]
ruby -S fcd_start.rb $1 $2 $3 $4 $5 $6 $7 $8 $9
if test -f "$HOME/fcd_stage.sh" ; then
. $HOME/fcd_stage.sh ;
fi;
]]
local gotofile
local datafile
local stubfile
local stubdata
local stubdummy
local stubchdir
if os.type == 'windows' then
gotofile = 'mtx-fcd-goto.cmd'
datafile = 'mtx-fcd-data.lua'
stubfile = 'fcd.cmd'
stubdata = mswinstub
stubdummy = 'rem no dir to change to'
stubchdir = 'cd /d "%s"'
else
gotofile = 'mtx-fcd-goto.sh'
datafile = 'mtx-fcd-data.lua'
stubfile = 'fcd.sh'
stubdata = unixstub
stubdummy = '# no dir to change to'
stubchdir = '# cd "%s"'
end
local homedir = os.env["HOME"] or "" -- no longer TMP etc
if homedir == "" then
homedir = format("%s/%s",os.env["HOMEDRIVE"] or "",os.env["HOMEPATH"] or "")
end
if homedir == "/" or not lfs.isdir(homedir) then
os.exit()
end
local datafile = file.join(homedir,datafile)
local gotofile = file.join(homedir,gotofile)
local hash = nil
local found = { }
local pattern = ""
local version = modules['mtx-fcd'].version
io.savedata(gotofile,stubdummy)
if not lfs.isfile(gotofile) then
-- write error
os.exit()
end
local function fcd_clear(onlyhistory,what)
if onlyhistory and hash and hash.history then
if what and what ~= "" then
hash.history[what] = nil
else
hash.history = { }
end
else
hash = {
name = "fcd cache",
comment = "generated by mtx-fcd.lua",
created = os.date(),
version = version,
paths = { },
history = { },
}
end
end
local function fcd_changeto(dir)
if dir and dir ~= "" then
io.savedata(gotofile,format(stubchdir,dir))
end
end
local function fcd_load(forcecreate)
if lfs.isfile(datafile) then
hash = dofile(datafile)
end
if not hash or hash.version ~= version then
if forcecache then
fcd_clear()
else
writeln("empty dir cache")
fcd_clear()
os.exit()
end
end
end
local function fcd_save()
if hash then
io.savedata(datafile,table.serialize(hash,true))
end
end
local function fcd_list(onlyhistory)
if hash then
writeln("")
if onlyhistory then
if next(hash.history) then
for k, v in table.sortedhash(hash.history) do
writeln(format("%s => %s",k,v))
end
else
writeln("no history")
end
else
local paths = hash.paths
if #paths > 0 then
for i=1,#paths do
local path = paths[i]
writeln(format("%4i %s",#path[2],path[1]))
end
else
writeln("empty cache")
end
end
end
end
local function fcd_find()
found = { }
pattern = environment.files[1] or ""
if pattern ~= "" then
pattern = string.escapedpattern(pattern)
local paths = hash.paths
for i=1,#paths do
local paths = paths[i][2]
for i=1,#paths do
local path = paths[i]
if find(path,pattern) then
found[#found+1] = path
end
end
end
end
end
local function fcd_choose(new)
if pattern == "" then
writeln(format("staying in dir %q",(gsub(lfs.currentdir(),"\\","/"))))
return
end
if #found == 0 then
writeln(format("dir %q not found",pattern))
return
end
local okay = #found == 1 and found[1] or (not new and hash.history[pattern])
if okay then
writeln(format("changing to %q",okay))
fcd_changeto(okay)
return
end
local offset = 0
while true do
if not found[offset] then
offset = 0
end
io.write("\n")
for i=1,26 do
local v = found[i+offset]
if v then
writeln(format("%s %3i %s",char(i+96),offset+i,v))
else
break
end
end
offset = offset + 26
if found[offset+1] then
io.write("\n[press enter for more or select letter]\n\n>> ")
else
io.write("\n[select letter]\n\n>> ")
end
local answer = lower(io.read() or "")
if not answer or answer == 'quit' then
break
elseif #answer > 0 then
local choice = tonumber(answer)
if not choice then
if answer >= "a" and answer <= "z" then
choice = byte(answer) - 96 + offset - 26
end
end
local newdir = found[choice]
if newdir then
hash.history[pattern] = newdir
writeln(format("changing to %q",newdir))
fcd_changeto(newdir)
fcd_save()
return
end
else
-- try again
end
end
end
local function globdirs(path,dirs)
local dirs = dirs or { }
for name in lfs.dir(path) do
if not find(name,"%.$") then
local fullname = path .. "/" .. name
if lfs.isdir(fullname) and not find(fullname,"/%.") then
dirs[#dirs+1] = fullname
globdirs(fullname,dirs)
end
end
end
return dirs
end
local function fcd_scan()
if hash then
local paths = hash.paths
for i=1,#environment.files do
local name = environment.files[i]
local name = gsub(name,"\\","/")
local name = gsub(name,"/$","")
local list = globdirs(name)
local done = false
for i=1,#paths do
if paths[i][1] == name then
paths[i][2] = list
done = true
break
end
end
if not done then
paths[#paths+1] = { name, list }
end
end
end
end
local argument = environment.argument
if argument("clear") then
if argument("history") then
fcd_load()
fcd_clear(true)
else
fcd_clear()
end
fcd_save()
elseif argument("scan") then
fcd_clear()
fcd_scan()
fcd_save()
elseif argument("add") then
fcd_load(true)
fcd_scan()
fcd_save()
elseif argument("stub") then
writeln(stubdata)
elseif argument("list") then
fcd_load()
if argument("history") then
fcd_list(true)
else
fcd_list()
end
elseif argument("help") then
application.help()
elseif argument("exporthelp") then
application.export(argument("exporthelp"),environment.files[1])
else -- also argument("find")
fcd_load()
fcd_find()
fcd_choose(argument("nohistory"))
end
|