-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdialog.lua.1
507 lines (496 loc) · 17.5 KB
/
dialog.lua.1
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
local Shared = ... -- name, outputFilename, utils, askAI, cfgpath
local State = Shared.State
local utils = Shared.utils -- HelpTopic, clearSession, HK
local menu = Shared.menu
local O = Shared.O
local F = far.Flags
local DIF_HOMEITEM = F.DIF_HOMEITEM or 0 -- far2m
local isFar3 = F.ACTL_GETFARMANAGERVERSION
local newDlgInfo do --> begin of Dlg helper <-- v0.1
local function norm (arg, base, align)
if not arg or arg==0 then
return base
elseif type(arg)=="string" then
return base + tonumber(arg)
end
return base + arg + (arg<0 and align or 0)
end
local function add (self,item)
item[6] = item[5] or item.Selected or item.ListItems or 0
item[7] = item.History
item[8] = item.Mask
item[9] = item[4] or item.Flags
item[10] = item[2] or item.Data
item[11] = item.MaxLength
item[12] = item.UserData
local c = item[3] or item.coords or {}
local coords = {}
coords[1] = norm(c[1], self.x0, self.width) -- X1
coords[2] = norm(c[2], self.y0+self.y) -- Y1
coords[3] = norm(c[3], self.x0, self.width) -- X2
coords[4] = not c[4] and coords[2] -- Y2==Y1
or norm(self, c[4], self.y0+self.y)
item[1] = item[1] or item.Type
for i=2,5 do item[i] = coords[i-1] end
local i = #self+1; item.idx = i; self[i] = item;
if i==1 and (item[1]==F.DI_SINGLEBOX or item[1]==F.DI_DOUBLEBOX) then
self.x0, self.y0, self.width = self.x0+2, self.y0+1, self.width-2*2 -- accomodate "working area" params
end
if item.name then
if self.names[item.name] then
far.Message("Duplicated item name: "..item.name, "Error in config", nil ,"lw")
else
self.names[item.name] = item
end
end
return i
end
function newDlgInfo (data)
data.Flags = data.Flags or 0
assert(type(data.Flags)=="number")
local function params (self) -- method; returns all arguments required for far.Dialog[Init]
self.dlgHeight = self.y0*2 + self.y+1
local item = self[1] -- 1st item
if item[1]==F.DI_SINGLEBOX or item[1]==F.DI_DOUBLEBOX then
item[5] = self.dlgHeight-self.y0 -- set Y2
end
return data.Guid,-1,-1, self.dlgWidth, self.dlgHeight, data.HelpTopic, self, data.Flags, data.DlgProc, data.Param
end
local function ln (self) -- method; "current" line++
self.y = self.y+1
end
local self = {add=add, ln=ln, names={}, params=params }
self.y = 0 -- offset - relative to y0
local small = bit64.band(data.Flags, F.FDLG_SMALLDIALOG)~=0
self.x0 = small and 0 or 3 -- working area start
self.y0 = small and 0 or 1 --
self.small = small
self.width = assert(data.width, "width is required")
self.dlgWidth = self.width + self.x0*2 -- width + margins
return self
end
end--> end of Dlg helper <--
local X2, SELECTED, HISTORY, FLAGS, DATA = 3,6,7,9,10
local function populateHistory (self, hDlg, name, values, source)
if type(values)~="table" then values = {values} end
local param = self.names[name]
if not param then return end
local success, history = pcall(utils.readHistory, param.History)
if not success then
far.Message(history, "Error", nil, "wl")
return
end
local inHistory = {}
for _,v in ipairs(history) do inHistory[v] = true end
local added
for i=#values,1,-1 do
if not inHistory[values[i]] then
hDlg:send(F.DM_ADDHISTORY, param.idx, values[i])
added = true
end
end
if added and history[1] then -- preserve lasthistory
hDlg:send(F.DM_ADDHISTORY, param.idx, history[#history])
end
if source=="init" and #history==0 then
hDlg:send(F.DM_SETTEXT, param.idx, values[1] or "")
end
end
local function getEnvValues (list)
local t = {}
for var in pairs(list) do
local v = win.GetEnv(var)
if v then table.insert(t,v) end
end
return t
end
local function isOpened (filename)
for i=1,far.AdvControl"ACTL_GETWINDOWCOUNT" do
if far.AdvControl("ACTL_GETWINDOWINFO", i).Name==filename then
return true
end
end
end
local idInput = win.Uuid"58DD9ECD-CFFA-472E-BFD7-042295C86CAE"
local SESSION = "default"
local _prompt = "Ask any &question (editor selection will be included with request)"
local function main (cfg, prompt, context)
if State.isDlgOpened then return end
cfg.params = {}
for i=1,debug.getinfo(cfg.applyParamsFn, "u").nparams do
local name = debug.getlocal(cfg.applyParamsFn, i)
cfg.params[i] = name
cfg.params[name] = i
end
local canSession = cfg.info.clearSession or cfg.info.sessionFile or cfg.info.historyName
local width = 86
local flags = O.smallDlg and F.FDLG_SMALLDIALOG or 0
local maxW = Far.Width
if width+(O.smallDlg and 0 or 3*2)>maxW then
flags = F.FDLG_SMALLDIALOG
width = math.min(maxW, width)
end
local data = {
width=width,
Guid=idInput,
Flags=flags,
HelpTopic=utils.HelpTopic"",
}
local II = newDlgInfo(data)
II:add {F.DI_DOUBLEBOX, Shared.name, {[X2]=-1}}
II:add {F.DI_TEXT, _prompt}
II:ln() -- -- -- --
II:add {F.DI_EDIT, prompt, {[X2]=-1},
DIF_HOMEITEM +F.DIF_FOCUS
+F.DIF_HISTORY +F.DIF_USELASTHISTORY, History="AskAI prompt",
name="prompt"
}
II:ln() -- -- -- --
II:add {F.DI_TEXT, cfg.info.name, {}, F.DIF_SEPARATOR +F.DIF_CENTERGROUP}
II:ln() -- -- -- --
II:add {F.DI_CHECKBOX, "&Session", nil, canSession and F.DIF_3STATE or F.DIF_DISABLE,
canSession and State.useSession or 0,
name="chkUseSession"
}
local sessionExists = cfg.info.clearSession
if cfg.info.sessionFile then
sessionExists = win.GetFileAttr(cfg.info.sessionFile:format(SESSION))
elseif cfg.info.historyName then
sessionExists = State[cfg.info.historyName]
end
II:add {F.DI_BUTTON, "‹&-›", {12}, F.DIF_BTNNOCLOSE +F.DIF_NOBRACKETS +(sessionExists and 0 or F.DIF_DISABLE),
onclick=function(hDlg,idx)
utils.clearSession(cfg.info, SESSION)
local item = hDlg:send(F.DM_GETDLGITEM, idx)
item[FLAGS] = bit64.bor(item[FLAGS], F.DIF_DISABLE)
hDlg:send(F.DM_SETDLGITEM, idx, item)
end, name="btnClrSession"
}
local x2 = 18
II:add {F.DI_CHECKBOX, "st&ream", {x2}, cfg.params.stream and 0 or F.DIF_DISABLE, State.useStream,
name="chkStream"
}
II:add {F.DI_CHECKBOX, "&Wrap", {-12}, F.DIF_3STATE, State.useWrap,
onclick=function (hDlg,idx)
hDlg:send(F.DM_ENABLE, idx+1, hDlg:send(F.DM_GETCHECK, idx)==0 and 0 or 1)
end, name="chkWrap"
}
II:add {F.DI_FIXEDIT, State.wrapAt, {-3, nil, -1},
(State.useWrap~=0 and 0 or F.DIF_DISABLE) +F.DIF_MASKEDIT, Mask="999",
name="edtWrap"
}
II:ln() -- -- -- --
local function makeHistoryName (param, name)
name = name or cfg.info.histories and cfg.info.histories[param]
local history = "AskAI param "..(name or param)
if not (name or O.sharedParams[param]) then
local baseName = cfg.info.name:match"[^[]+":match"^(.-)%s*$"
history = history.." - "..baseName
end
return history
end
local function processSetsHistories (hDlg, text, dependants)
local env = cfg.info.env
for _,subparam in ipairs(dependants) do
local items = {II.names[subparam]}
items[#items+1] = env and env[subparam] and II.names[env[subparam]]
for _,item in ipairs(items) do
local pos = item.idx
hDlg:send(F.DM_SETTEXT, pos, "")
local History = makeHistoryName(nil, subparam..":"..text)
II.names[item.name].History = History
hDlg:send(F.DM_SETHISTORY, pos, History)
hDlg:send(F.DM_EDITUNCHANGEDFLAG, pos, 1)
end
end
end
local hk = utils.HK.new("")
for _,param in ipairs(cfg.params) do
if not ({context=1, prompt=1, session=1, stream=1})[param] then
if hk.i==0 then
II:add {F.DI_TEXT, "Parameters", {[X2]=-1}, F.DIF_SEPARATOR +F.DIF_CENTERTEXT}
II:ln() -- -- -- --
end
local col2 = math.max(x2, param:len()+1)
local visibility = cfg.info.hidden and cfg.info.hidden[param] and F.DIF_HIDDEN or F.DIF_NONE
II:add {F.DI_TEXT, hk:iter(visibility==F.DIF_HIDDEN)..param, {"-1", [X2]=col2-1}, visibility}
II:add {F.DI_EDIT, nil, {col2, nil, -1}, F.DIF_HISTORY +F.DIF_USELASTHISTORY + visibility,
History=makeHistoryName(param),
name=param
}
if visibility~=F.DIF_HIDDEN then II:ln() end -- -- -- --
end
end
if cfg.info.env then
II:add {F.DI_TEXT, "Environment &variables", {[X2]=-1}, F.DIF_SEPARATOR +F.DIF_CENTERTEXT}
II:ln() -- -- -- --
for k,var in pairs(cfg.info.env) do
local value = win.GetEnv(var)
local col2 = math.max(x2, var:len()+1)
II:add {F.DI_TEXT, hk:iter()..var, {"-1", [X2]=col2-1}}
II:add {F.DI_EDIT, value or "", {col2, nil, -1}, F.DIF_HISTORY +F.DIF_MANUALADDHISTORY,
History=makeHistoryName(k),
name=var
}
II:ln() -- -- -- --
end
end
II:add {F.DI_TEXT, nil, nil, F.DIF_SEPARATOR}
II:ln() -- -- -- --
if cfg.info.exe~=true then
local vFlags = cfg.info.config and 0 or F.DIF_DISABLE
II:add {F.DI_BUTTON, "&Utility cfg", nil, F.DIF_BTNNOCLOSE +F.DIF_CENTERGROUP +vFlags,
onclick=function()
if cfg.info.config then
editor.Editor(cfg.info.config)
end
end,
}
end
II:add {F.DI_BUTTON, "Def&inition", nil, F.DIF_BTNNOCLOSE +F.DIF_CENTERGROUP,
onclick=function(hDlg,idx)
if F.EEC_MODIFIED==editor.Editor(cfg.pathname) then
hDlg:send(F.DM_CLOSE, idx)
utils.synchro(Shared.askAI, prompt)
end
end,
}
local function _pcall (fn, ...)
local success, res = pcall(fn, ...)
if success then return res end
far.Message(res,"Error in preset",nil,"wl")
end
II:add {F.DI_BUTTON, "&Presets - F5", nil, F.DIF_BTNNOCLOSE +F.DIF_CENTERGROUP,
onclick=function(hDlg)
local preset = menu.choosePreset(II.names, cfg.info)
if preset then
hDlg:send(F.DM_ENABLEREDRAW,0)
local secondary = {}
for k,v in pairs(preset) do
local item = II.names[k]
local dependants = cfg.info.sets and cfg.info.sets[k]
if dependants then
if type(v)=="function" then
v = _pcall(v)
if not v then goto exitPresets end
end
if v~=hDlg:send(F.DM_GETTEXT, item.idx) then
hDlg:send(F.DM_SETTEXT, item.idx, v)
hDlg:send(F.DM_ADDHISTORY, item.idx, v)
processSetsHistories(hDlg, v, dependants)
end
else
secondary[item] = v
end
end
for item,v in pairs(secondary) do
if type(v)=="table" then
populateHistory(II, hDlg, item.name, v)
v = v[1]
end
local isHidden = cfg.info.hidden and cfg.info.hidden[item.name]
local exV = not isHidden and hDlg:send(F.DM_GETTEXT, item.idx)
if isHidden or exV=="" or exV:match" env variable!$" then
if type(v)=="function" then v = _pcall(v) end
if v then
hDlg:send(F.DM_SETTEXT, item.idx, v)
hDlg:send(F.DM_ADDHISTORY, item.idx, v)
end
end
end
::exitPresets::
hDlg:send(F.DM_ENABLEREDRAW,1)
end
end,
name="btnPreset"
}
II:add {F.DI_BUTTON, "&Models - F6", nil, F.DIF_BTNNOCLOSE +F.DIF_CENTERGROUP +(cfg.getModels and 0 or F.DIF_DISABLE),
onclick=function(hDlg)
local args = {}
for i=1,#cfg.params do
local param = cfg.params[i]
local ii = II.names[param] and II.names[param].idx
if ii then
local value = hDlg:send(F.DM_GETTEXT,ii)
args[param] = value~="" and value or nil
end
end
local getModels, fromHistory = cfg.getModels
local SHIFT = 16 -- invert action (also for F6/ShiftF6)
local invert = isFar3 and win.GetKeyState(SHIFT)
if not cfg.getModels or (args.modelsMeta=="none" ~= invert) then
if not II.names.model then return end
if isFar3 then
utils.synchro(function()
local hLst = actl.GetWindowInfo().Id
hLst:send(F.DM_ENABLEREDRAW, 0)
hLst:send(F.DM_LISTSORT, 1)
local pos = hLst:send(F.DM_LISTFINDSTRING, 1, {Pattern=args.model or "", Flags=F.LIFIND_EXACTMATCH})
hLst:send(F.DM_LISTSETCURPOS, 1, {SelectPos=pos})
hLst:send(F.DM_ENABLEREDRAW, 1)
end)
hDlg:send(F.DM_SETDROPDOWNOPENED, II.names.model.idx, 1)
return
end
fromHistory = true
function getModels ()
return utils.readHistory(II.names.model.History)
end
end
local success, model, argName = pcall(menu.chooseModel, getModels, args, fromHistory)
if not success then
far.Message(model, "Error fetching models", nil, "wl")
elseif model then
local param = II.names[argName or "model"]
if not param then return end
hDlg:send(F.DM_SETTEXT, param.idx, model)
hDlg:send(F.DM_SETFOCUS, param.idx)
end
end,
name="btnModels"
}
II:add {F.DI_BUTTON, "Swi&tch", nil, F.DIF_CENTERGROUP,
name="btnSwitch"
}
II:add {F.DI_BUTTON, "Go&!", nil, F.DIF_CENTERGROUP +F.DIF_DEFAULTBUTTON,
name="btnGo"
}
data.DlgProc=function (hDlg,msg,idx,Param2)
local item = II[idx]
if msg==F.DN_INITDIALOG then
for param,list in pairs(cfg.info.predefined or {}) do
populateHistory(II, hDlg, param, list, "init")
end
for param,list in pairs(O.stdEnvs) do
populateHistory(II, hDlg, param, getEnvValues(list))
end
local sets = cfg.info.sets
if sets then
for param, dependants in pairs(sets) do
if II.names[param] then
local text = hDlg:send(F.DM_GETTEXT, II.names[param].idx)
processSetsHistories(hDlg, text, dependants)
end
end
end
elseif msg==F.DN_EDITCHANGE then
local sets = cfg.info.sets
if sets then
for param, dependants in pairs(sets) do
if II[idx].name==param then
local text = Param2[DATA]
hDlg:send(F.DM_ENABLEREDRAW,0)
processSetsHistories(hDlg, text, dependants)
hDlg:send(F.DM_ENABLEREDRAW,1)
return
end
end
end
elseif msg==F.DN_BTNCLICK then
if item.onclick then
item.onclick(hDlg,idx)
end
elseif msg==(F.DN_CONTROLINPUT or F.DN_KEY) then
local key = far.KeyToName and far.KeyToName(Param2) --luacheck: globals far.KeyToName -- far2m
or far.InputRecordToName(Param2)
if key=="F3" and item.name=="chkUseSession" then
if cfg.info.showSession then
cfg.info.showSession()
elseif cfg.info.sessionFile then
local pathname = cfg.info.sessionFile:format(SESSION)
if win.GetFileAttr(pathname) then
viewer.Viewer(pathname)
end
end
return
end
if key==O.key then
hDlg:send(F.DM_CLOSE, II.names.btnSwitch.idx)
elseif key=="AltSubtract" then
hDlg:send(F.DN_BTNCLICK, II.names.btnClrSession.idx)
elseif key=="ShiftF4" then
if II.names.headers then
local d_item = hDlg:send(F.DM_GETDLGITEM, II.names.headers.idx)
local id = win.Uuid"6EDB0A6C-2D65-4F1B-80B9-A8C88B08701B"
local headers = far.InputBox(id, cfg.info.name, "Enter headers (moonscript- or lua-table):",
d_item[HISTORY], d_item[DATA])
if headers then
hDlg:send(F.DM_SETTEXT, II.names.headers.idx, headers)
end
end
elseif key=="F5" then
hDlg:send(F.DN_BTNCLICK, II.names.btnPreset.idx)
elseif key=="ShiftF5" then
local apibase = II.names.apibase or II.names.api_base or II.names.api or II.names.provider
if apibase then
hDlg:send(F.DM_SETDROPDOWNOPENED, apibase.idx, 1)
end
elseif key=="F6" then
hDlg:send(F.DN_BTNCLICK, II.names.btnModels.idx)
elseif key=="ShiftF6" then
if isFar3 then
hDlg:send(F.DN_BTNCLICK, II.names.btnModels.idx)
elseif II.names.model then
hDlg:send(F.DM_SETDROPDOWNOPENED, II.names.model.idx, 1)
end
elseif key=="PgUp" then
hDlg:send(F.DM_SETFOCUS, II.names.prompt.idx)
end
end
end
State.isDlgOpened = true
local res = far.Dialog(II:params())
State.isDlgOpened = false
if res==-1 then return end
if II[res].name=="btnGo" then
if cfg.info.env then
for _,var in pairs(cfg.info.env) do
local v = II.names[var][DATA]
win.SetEnv(var, v~="" and v or nil)
end
end
if canSession then
State.useSession = II.names.chkUseSession[SELECTED]
end
State.useStream = II.names.chkStream[SELECTED]
State.useWrap = II.names.chkWrap[SELECTED]
State.wrapAt = II.names.edtWrap[DATA]
if State.useSession==2 and not isOpened(Shared.outputFilename) then
utils.clearSession(cfg.info, SESSION)
end
if not cfg.params.prompt then
local p = II.names.prompt[DATA]
if context~="" then
local n
p,n = p:gsub("{{%input%}}", context)
if n==0 then
p = p.."\n"..context
end
end
context = p
end
local base = {
context=context,
session=State.useSession==0 and "" or SESSION,
stream=State.useStream==1,
}
local args = {}
for i=1,#cfg.params do
local param = cfg.params[i]
local value = base[param]==nil and II.names[param][DATA] or base[param]
args[i] = value~="" and value or nil
end
if context~="" and not cfg.params.context then
cfg.info._context = context
end
local linewrap = ({[0]=nil, tonumber(II.names.edtWrap[DATA]), "dynamic"})[State.useWrap]
return cfg.applyParamsFn(unpack(args,1,#cfg.params)),
II.names.prompt[DATA],
linewrap,
cfg.params.stream and State.useStream==1
elseif II[res].name=="btnSwitch" then
menu.chooseCfg(prompt)
return
end
end
return main