-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathARB.PowerPro
168 lines (139 loc) · 5.99 KB
/
ARB.PowerPro
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
;
; ARB v4.0 Hybrid Edition
; Written by Alex Peters, 13/6/2005
;
; Allows execution of aliases with parameter passing. Also accepts PowerPro
; commands, programs, folders, documents and Internet resources for execution.
;
; Aliases may exist as labels within the ARB script and/or as external scripts
; matching a user-specified wildcard.
;
; Usage: .ARB()
;
; * In the box that appears, enter the location of a program, folder,
; document or Internet resource and it will be opened.
; * Enter a PowerPro command and it will be executed.
; * Enter an alias, optionally following it with parameters, and it will be
; executed.
; * Potential matches for aliases and past entries will appear in a popup
; underneath the text field. Pressing Tab will alternate through these
; choices.
;
; All feedback is encouraged on the PowerPro discussion list:
; * http://groups.yahoo.com/group/power-pro
; ____________________________________________________________________________
;
; The EXT_PREFIX constant holds the file path prefix that qualifies any script
; within {scriptFolder} as an external ARB alias. A name prefix (e.g. 'ARB_'),
; folder prefix (e.g. 'ARB_Aliases\') or both may be specified. Setting to '-'
; or something your scripts don't start with will disable external aliases.
;
; * The prefix does not form part of the typed alias.
; * Only scripts with the extension '.PowerPro' will be detected.
; * Please ensure that any directories specified already exist before
; executing ARB.
;
local EXT_PREFIX = ?"ARB_Aliases\"
;
; The CACHE_PATH constant holds the desired location to read/write internal
; alias cache data.
;
local CACHE_PATH = pProFolder ++ ?"Data\ARB_Cache.txt"
;
; The HISTORY_PATH constant holds the desired location to read/write command
; history entries. Setting to 'NUL' will disable command history.
;
local HISTORY_PATH = pProFolder ++ ?"Data\ARB_History.txt"
; ____________________________________________________________________________
local LF = esc(?"\n", ?"\")
static cacheVersion, intAliases, history, lastEntered
; The cache data is not yet loaded if the cacheVersion variable is empty.
; Attempt to load from disk if necessary.
if (cacheVersion == "") do
local cacheFile = File.open(CACHE_PATH, ?"r")
if (cacheFile > 0) do
cacheVersion = File.readString(cacheFile)
for (1)
local alias = File.readString(cacheFile)
if (File.eof(cacheFile))
break
intAliases = intAliases ++ alias ++ LF
endFor
File.close(cacheFile)
endIf
endIf
; Determine the path to the ARB script. This is used for checking the time-
; stamp and parsing for labels if necessary. Restricting the location and
; extension of the ARB script also assures error-free use of internal aliases.
local arbPath = scriptFolder ++ ?"\" ++ scriptName ++ ?".PowerPro"
if (not(validPath(arbPath))) do
messageBox(?"stop", ?"The ARB script is improperly named or located. It must reside in '" ++ scriptFolder ++ ?"' with the extension '.PowerPro'.", ?"ARB Load Error")
quit
endIf
; Compare the current timestamp of the ARB script to the cache version (which
; may still be undefined) and rebuild the cache if they don't match.
local arbTimestamp = File.lastModified(arbPath)
if (arbTimestamp != cacheVersion) do
intAliases = ""
local arbFile = File.open(arbPath, ?"r")
if (arbFile <= 0) do
messageBox(?"stop", ?"Cannot open the ARB script for parsing. Internal aliases are unavailable.", ?"ARB Alias Parse Error")
else
for (1)
local arbWord = word(File.readString(arbFile), 1)
if (File.eof(arbFile))
break
if (select(arbWord, 1) == ?"@")
intAliases = intAliases ++ remove(arbWord, 1) ++ LF
endFor
File.close(arbFile)
File.writeAll(CACHE_PATH, arbTimestamp ++ LF ++ intAliases)
cacheVersion = arbTimestamp
endIf
endIf
; Scan for external aliases.
local extAliases
local extAliasPrefix = scriptFolder ++ ?"\" ++ EXT_PREFIX
local scriptFiles = File.listFiles(extAliasPrefix ++ ?"*.PowerPro", 1)
for (local i = line(scriptFiles, 0); i > 0; i = i - 1)
extAliases = extAliases ++ File.name(remove(line(scriptFiles, i), length(extAliasPrefix))) ++ LF
endFor
; Load history from disk if not already in memory.
if (history == "" && validPath(HISTORY_PATH))
history = File.readAll(HISTORY_PATH)
; Present the ARB dialogue with the auto-completion vector.
local autoComplete = Vec.createFromLines(intAliases ++ extAliases ++ history, 1)
local cmd = inputDefault(lastEntered, ?"ARB v4.0", ?"file =autoComplete")
autoComplete = Vec.destroy(autoComplete)
if (not(pProFlag(0)))
quit
lastEntered = cmd
if (cmd == "")
quit
; Is the first word of the command line an alias?
if (index(LF ++ intAliases ++ extAliases, LF ++ word(cmd, 1) ++ LF)) do
local alias = word(cmd, 1)
; Escape the parameter string into a bracketed evaluatable expression.
local params = remove(cmd, index(cmd, alias) + length(alias))
params = replaceChars(params, ?"\", ?"\\")
params = replaceChars(params, ?'"', ?"\X22")
params = ?'(esc(?"' ++ params ++ ?'", ?"\"))'
; Is the alias external? (External aliases get priority over internal ones of
; the same name.)
cmd = ?"." ++ ScriptName ++ ?"@" ++ alias ++ params
if (validPath(scriptFolder ++ ?"\" ++ EXT_PREFIX ++ alias ++ ?".PowerPro"))
cmd = ?"." ++ EXT_PREFIX ++ alias ++ params
else
; It's not an alias if we enter this block. Add the entered information to the
; history (if not a file, which can be easily auto-completed) and run it.
if (not(index(LF ++ history, LF ++ cmd ++ LF)) && not(validPath(cmd))) do
history = history ++ cmd ++ LF
File.writeAll(HISTORY_PATH, cmd ++ LF, ?"a")
EndIf
endIf
; Queue the command to be executed under PowerPro's ownership (and not ARB's).
; This prevents bad commands from throwing an ARB script error.
Event.createMS(1, 1, cmd)
quit
; Internal aliases are defined past this point.
; ____________________________________________________________________________