-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathum_adapter.lua
More file actions
591 lines (396 loc) · 13.2 KB
/
um_adapter.lua
File metadata and controls
591 lines (396 loc) · 13.2 KB
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
--[[ UM Adapter ]]--
----------------------------------------
--[[ description:
-- _usermenu Adapter for packs to LuaFAR for Editor.
-- Адаптер _usermenu для пакетов к LuaFAR for Editor.
--]]
----------------------------------------
--[[ uses:
far2.
-- group: Macros/Plugins.
--]]
----------------------------------------
--[[ based on:
luafar4editor plugin
(LuaFAR for Editor.)
(c) 2007+, Shmuel Zeigerman.
--]]
--------------------------------------------------------------------------------
----------------------------------------
local package = package
local far2Utils
--local far2History
----------------------------------------
local far = far
local F = far.Flags
--local export
----------------------------------------
--[[
local debugs = require "context.utils.useDebugs"
local logShow = debugs.Show
--]]
--------------------------------------------------------------------------------
local unit = {}
unit.Guid = win.Uuid("ffbbf294-a780-4452-8ef0-82df38507b42")
unit.global = {} -- Глобальные переменные пакетов
---------------------------------------- Pathes
do
local slen = string.len
function unit.ExtendPackagePath (Path)
local PackPath = package.path
if not (slen(Path) > 0) then return end
if Path:sub(-1, 1) ~= ";" then Path = Path..";" end
--far.Show(Path:gsub(";", "\n"))
if PackPath:find(Path, 1, true) then return end
package.path = Path..PackPath
--far.Show(package.path:gsub(";", "\n"))
end ---- ExtendPackagePath
end
---------------------------------------- LuaFAR Plugin API
-- based on lf4ed.lua
function unit:ReloadUserFile ()
if not self.FirstRun then
self:RunExitScriptHandlers()
--self:ResetPackageLoaded()
end
--package.path = self.PackagePath -- restore to original value
self.UserItems, self.CommandTable, self.HotKeyTable, self.Handlers =
far2Utils.LoadUserMenu(self.Plugin, "_usermenu.lua")
end ---- ReloadUserFile
function unit:RunExitScriptHandlers ()
for _, f in ipairs(self.Handlers.ExitScript) do f() end
end
---------------------------------------- ---- Menu
do
local traceback = debug.traceback
local function traceback3 (msg)
return traceback(msg, 3)
end
function unit:RunMenuItem (aItem, aArg)
local argCopy = {} -- prevent parasite connection between utilities
for k,v in pairs(aArg) do argCopy[k]=v end
--local restoreConfig = aRestoreConfig and lf4ed.config()
local function wrapfunc()
if aItem.action then return aItem.action(argCopy) end
return far2Utils.RunUserItem(aItem, argCopy)
end
local ok, result = xpcall(wrapfunc, traceback3)
local result2 = false --CurrentConfig.ReturnToMainMenu
--if restoreConfig then lf4ed.config(restoreConfig) end
if not ok then export.OnError(result) end
return ok, result, result2
end ---- RunMenuItem
end
local farMenuFlags = { FMENU_WRAPMODE = 1, FMENU_AUTOHIGHLIGHT = 1, }
function unit:MakeMainMenu (aFrom)
local properties = {
Flags = farMenuFlags,
Title = "UM Adapter", --M.MPluginName,
--HelpTopic = "Contents",
Bottom = "alt+sh+f9",
}
--------
local items = {}
--if aFrom == "editor" then far2Utils.AddMenuItems(items, EditorMenuItems, M) end
--far.Show("aFrom", aFrom)
local mItems = self.UserItems[aFrom]
if mItems then
far2Utils.AddMenuItems(items, self.UserItems[aFrom])
--far2Utils.AddMenuItems(items, self.UserItems[aFrom], M)
end
--------
--local keys = {}
local function mConfigure ()
return unit:Configure({ From = aFrom })
end --
local keys = {
{ BreakKey = "AS+F9", action = mConfigure, },
} ---
return properties, items, keys
end ---- MakeMainMenu
---------------------------------------- ---- Run
do
local dNumFromToArea = {
[F.OPEN_PLUGINSMENU] = "panels",
[F.OPEN_EDITOR] = "editor",
[F.OPEN_VIEWER] = "viewer",
[F.OPEN_DIALOG] = "dialog",
} --- dNumFromToArea
local dStrFromToArea = {
Shell = "panels",
Editor = "editor",
Viewer = "viewer",
Dialog = "dialog",
} --- dStrFromToArea
-- = ProcessOpen
function unit:Run (aFrom, aItem)
if aFrom == nil then
if aItem == nil then
return self:Configure({ From = "config" })
end
return
end
local sFrom = aFrom
--[[
if sFrom == "frommacro" then
return far2Utils.OpenMacro(aItem, self.CommandTable)
--return far2Utils.OpenMacro(aItem, self.CommandTable, lf4ed.config)
elseif sFrom == "commandline" then
return far2Utils.OpenCommandLine(aItem, self.CommandTable)
--return far2Utils.OpenCommandLine(aItem, self.CommandTable, lf4ed.config)
end
--]]
if type(sFrom) == 'number' then
sFrom = dNumFromToArea[aFrom]
else
sFrom = dStrFromToArea[aFrom]
end
if sFrom == nil then return end
local mItem = aItem or {}
--local history = _History:field("menu." .. sFrom)
local properties, items, keys = self:MakeMainMenu(sFrom)
--properties.SelectIndex = history.position
while true do
local item, pos = far.Menu(properties, items, keys)
if not item then break end
--far.Show(item, pos)
--history.position = pos
local arg = { From = sFrom }
if sFrom == "dialog" then arg.hDlg = mItem.hDlg end
--local ok, result, bRetToMainMenu = self:RunMenuItem(item, arg)
local ok, result, bRetToMainMenu =
self:RunMenuItem(item, arg, item.action ~= unit.Configure)
if not ok then break end
--_History:save()
--if not bRetToMainMenu then break end
if not (bRetToMainMenu or item.action == unit.Configure) then break end
--if result=="reloaded" then
if item.action == unit.Configure and result == "reloaded" then
properties, items, keys = self:MakeMainMenu(sFrom)
else
properties.SelectIndex = pos
end
end -- while
return true
end ---- Run
end
function unit:Configure (aArg)
local properties = {
Flags = farMenuFlags,
Title = "UM Adapter Configure", --M.MPluginName,
--Title = M.MPluginNameCfg,
--HelpTopic = "Contents",
}
local items = {
--{ text = M.MPluginSettings, action = unit.PluginConfig },
--{ text = M.MReloadUserFile, action = unit.ReloadUserFile },
}
for _,v in ipairs(self.UserItems.config) do items[#items+1]=v end
if not (#items > 0) then return end
while true do
local item, pos = far.Menu(properties, items)
if not item then return end
local ok = self:RunMenuItem(item, aArg, false)
--local ok, result = self:RunMenuItem(item, aArg, false)
if not ok then return end
--if result then _History:save() end
if item.action == unit.ReloadUserFile then return "reloaded" end
properties.SelectIndex = pos
end -- while
end ---- Configure
---------------------------------------- ---- Events
function unit:ExitScripts ()
self:RunExitScriptHandlers()
end
function unit:ProcessEditorEvent (EditorId, Event, Param)
for _,f in ipairs(self.Handlers.EditorEvent) do
f(EditorId, Event, Param)
end
end ---- ProcessEditorEvent
function unit:ProcessViewerEvent (ViewerId, Event, Param)
for _,f in ipairs(self.Handlers.ViewerEvent) do
f(ViewerId, Event, Param)
end
end ---- ProcessViewerEvent
do
local VK = win.GetVirtualKeys()
local band, bor = bit64.band, bit64.bor
--local bnot, bxor = bit64.bnot, bit64.bxor
--function unit.KeyComb (Rec)
local function KeyComb (Rec)
local f = 0
local state = Rec.ControlKeyState
local ALT = bor(F.LEFT_ALT_PRESSED, F.RIGHT_ALT_PRESSED)
local CTRL = bor(F.LEFT_CTRL_PRESSED, F.RIGHT_CTRL_PRESSED)
local SHIFT = F.SHIFT_PRESSED
if 0 ~= band(state, ALT) then f = bor(f, 0x01) end
if 0 ~= band(state, CTRL) then f = bor(f, 0x02) end
if 0 ~= band(state, SHIFT) then f = bor(f, 0x04) end
f = f .. "+" .. VK[Rec.VirtualKeyCode%256]
return f
end ---- KeyComb
--end
--do
-- local KeyComb = unit.KeyComb
function unit:ProcessEditorInput (Rec)
local EventType = Rec.EventType
if EventType == F.KEY_EVENT then
local item = self.HotKeyTable[KeyComb(Rec)]
if item then
if Rec.KeyDown then
--if type(item)=="number" then item = EditorMenuItems[item] end
--if item then self:RunMenuItem(item, {From="editor"}) end
--if item then
if type(item) == 'table' then
self:RunMenuItem(item, {From="editor"}, item.action ~= unit.Configure)
end
end
return true
end
end
for _, f in ipairs(self.Handlers.EditorInput) do
if f(Rec) then return true end
end
end ---- ProcessEditorInput
end
---------------------------------------- Register
function unit:RegisterMenuItem (text, description)
self.Env.MenuItem {
text = text or "UM Adapter",
description = description or "_usermenu Adapter",
guid = win.Uuid(unit.Guid),
menu = "Plugins Config",
area = "Shell Editor Viewer Dialog",
action = function (aFrom, aItem)
return self:Run(aFrom, aItem)
end,
} ---
end ---- RegisterMenuItem
function unit:RegisterEvents (Priority)
Priority = Priority --or 1--0--0
local Event = self.Env.Event
Event {
group = "EditorInput",
description = "um_adapter ProcessEditorInput",
priority = Priority,
action = function (rec)
return self:ProcessEditorInput(rec)
end,
} ---
Event {
group = "EditorEvent",
description = "um_adapter ProcessEditorEvent",
priority = Priority,
action = function (id, event, param)
return self:ProcessEditorEvent(id, event, param)
end,
} ---
Event {
group = "ViewerEvent",
description = "um_adapter ProcessViewerEvent",
priority = Priority,
action = function (id, event, param)
return self:ProcessViewerEvent(id, event, param)
end,
} ---
Event {
group = "ExitFAR",
description = "um_adapter ExitScripts",
priority = Priority,
action = function ()
return self:ExitScripts()
end,
} ---
end ---- RegisterEvents
function unit:RegisterCmdLine ()
local CmdLine = self.Env.CommandLine
local SplitCmdLine = far2Utils.SplitCommandLine
local RunUserItem = far2Utils.RunUserItem
local FromPanels = { From = "panels" }
for k, v in pairs(self.CommandTable) do
CmdLine {
prefixes = k,
action = function (prefix, text)
local args = SplitCmdLine(text)
RunUserItem(v, FromPanels, unpack(args))
end,
} ---
end -- for
end ---- RegisterCmdLine
function unit:RegisterUserMenu (Priority)
self:RegisterMenuItem()
self:RegisterEvents(Priority)
self:RegisterCmdLine()
end ---- RegisterUserMenu
---------------------------------------- main
do
local GetSysEnv = win.GetEnv
--[[
local function ExpandEnv (s)
return (s or ""):gsub("%%(.-)%%", GetSysEnv)
end
--]]
function unit.Execute (Env) --> (table)
unit.Env = Env
--export = unit.Env.export
--far.Show(export)
unit.FirstRun = true
local ProfilePath = GetSysEnv("FARPROFILE")
if not ProfilePath then
ProfilePath = GetSysEnv("FARHOME").."\\Profile"
end
unit.ProfilePath = ProfilePath
local ModuleDir = ProfilePath.."\\work\\"
unit.ModuleDir = ModuleDir
unit.ExtendPackagePath(ModuleDir.."?.lua;")
do -- Путь к LuaFAR пакетам
local Path = ModuleDir.."scripts\\?.lua;"
unit.ScriptsPath = Path
unit.ExtendPackagePath(Path)
end
--[[
local Path = ExpandEnv(GetSysEnv("LUAFAR_PATH"))
if Path then -- Путь к LuaFAR context
unit.LuaFAR_Path = Path
unit.ExtendPackagePath(Path)
end
--]]
far2Utils = require "far2.utils"
--far2History = require "far2.history"
local Plugin = far2Utils.InitPlugin()
unit.Plugin = Plugin
Plugin.ModuleDir = ModuleDir
--Plugin.OriginalRequire = require
--Plugin.History = far2History.newsettings(nil, "alldata")
unit.PackagePath = package.path
--far.Show(package.path:gsub(";", "\n"))
unit:ReloadUserFile()
unit:RegisterUserMenu()
--local logShow = context.ShowInfo
--logShow({ unit.PackagePath:gsub(";", "\n") }, "PackagePath", "w d2")
--logShow(unit, "um_adapter", "w d3")
unit.FirstRun = false
return unit
end ---- Execute
end
---------------------------------------- um_adapter
do
local _H = getfenv()
local Unit = unit.Execute(_H)
--far.Show(um_adapter)
local mG = _G
local uG = Unit.global
for k, v in pairs(uG) do mG[k] = v end
--far.Show(mG.context)
local acall = mf.acall
local aItem = { from = "macro" }
function mG.UMAdapterMenu ()
local sArea = Area.Current
--far.Show("Area", sArea)
--return um_adapter:Run(sArea)
return acall(Unit.Run, Unit, sArea, aItem)
end ---- UMAdapterMenu
end
--------------------------------------------------------------------------------
--return { Execute = unit.Execute }
--------------------------------------------------------------------------------