/
service.c
6287 lines (5458 loc) · 167 KB
/
service.c
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//---------------------------------------------------------------------------
#include <windows.h>
#include <ctype.h>
#include <math.h>
#include "version.h"
#include "luafar.h"
#include "util.h"
#include "ustring.h"
#include "compat52.h"
#ifndef LUADLL
# if LUA_VERSION_NUM == 501
# define LUADLL "lua51.dll"
# elif LUA_VERSION_NUM == 502
# define LUADLL "lua52.dll"
# endif
#endif
typedef struct PluginStartupInfo PSInfo;
extern int bit64_push(lua_State *L, INT64 v);
extern int bit64_pushuserdata(lua_State *L, INT64 v);
extern int bit64_getvalue(lua_State *L, int pos, INT64 *target);
extern int luaopen_bit64(lua_State *L);
extern int luaopen_regex(lua_State*);
extern int luaopen_usercontrol(lua_State*);
extern int luaopen_uio(lua_State *L);
extern int luaopen_unicode(lua_State *L);
extern int luaopen_upackage(lua_State *L);
extern int luaopen_win(lua_State *L);
extern int luaB_dofileW(lua_State *L);
extern int luaB_loadfileW(lua_State *L);
extern int pcall_msg(lua_State* L, int narg, int nret);
extern void push_flags_table(lua_State *L);
extern void SetFarColors(lua_State *L);
extern void FillPluginPanelItem(lua_State *L, struct PluginPanelItem *pi, int CollectorPos);
extern void WINAPI FarPanelItemFreeCallback(void* UserData, const struct FarPanelItemFreeInfo* Info);
extern int far_MacroCallFar(lua_State *L);
extern int far_FarMacroCallToLua(lua_State *L);
extern void PackMacroValues(lua_State* L, size_t Count, const struct FarMacroValue* Values);
extern void PushFarMacroValue(lua_State* L, const struct FarMacroValue* val);
extern int GetExportFunction(lua_State* L, const char* FuncName);
const char FarFileFilterType[] = "FarFileFilter";
const char FarTimerType[] = "FarTimer";
const char FarDialogType[] = "FarDialog";
const char SettingsType[] = "FarSettings";
const char SettingsHandles[] = "FarSettingsHandles";
const char PluginHandleType[] = "FarPluginHandle";
const char AddMacroDataType[] = "FarAddMacroData";
const char SavedScreenType[] = "FarSavedScreen";
const char FAR_VIRTUALKEYS[] = "far.virtualkeys";
const char FAR_FLAGSTABLE[] = "far.Flags";
const char FAR_DN_STORAGE[] = "FAR_DN_STORAGE";
const char* VirtualKeyStrings[256] =
{
// 0x00
NULL, "LBUTTON", "RBUTTON", "CANCEL",
"MBUTTON", "XBUTTON1", "XBUTTON2", NULL,
"BACK", "TAB", NULL, NULL,
"CLEAR", "RETURN", NULL, NULL,
// 0x10
"SHIFT", "CONTROL", "MENU", "PAUSE",
"CAPITAL", "KANA", NULL, "JUNJA",
"FINAL", "HANJA", NULL, "ESCAPE",
NULL, "NONCONVERT", "ACCEPT", "MODECHANGE",
// 0x20
"SPACE", "PRIOR", "NEXT", "END",
"HOME", "LEFT", "UP", "RIGHT",
"DOWN", "SELECT", "PRINT", "EXECUTE",
"SNAPSHOT", "INSERT", "DELETE", "HELP",
// 0x30
"0", "1", "2", "3",
"4", "5", "6", "7",
"8", "9", NULL, NULL,
NULL, NULL, NULL, NULL,
// 0x40
NULL, "A", "B", "C",
"D", "E", "F", "G",
"H", "I", "J", "K",
"L", "M", "N", "O",
// 0x50
"P", "Q", "R", "S",
"T", "U", "V", "W",
"X", "Y", "Z", "LWIN",
"RWIN", "APPS", NULL, "SLEEP",
// 0x60
"NUMPAD0", "NUMPAD1", "NUMPAD2", "NUMPAD3",
"NUMPAD4", "NUMPAD5", "NUMPAD6", "NUMPAD7",
"NUMPAD8", "NUMPAD9", "MULTIPLY", "ADD",
"SEPARATOR", "SUBTRACT", "DECIMAL", "DIVIDE",
// 0x70
"F1", "F2", "F3", "F4",
"F5", "F6", "F7", "F8",
"F9", "F10", "F11", "F12",
"F13", "F14", "F15", "F16",
// 0x80
"F17", "F18", "F19", "F20",
"F21", "F22", "F23", "F24",
NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL,
// 0x90
"NUMLOCK", "SCROLL", "OEM_NEC_EQUAL", "OEM_FJ_MASSHOU",
"OEM_FJ_TOUROKU", "OEM_FJ_LOYA", "OEM_FJ_ROYA", NULL,
NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL,
// 0xA0
"LSHIFT", "RSHIFT", "LCONTROL", "RCONTROL",
"LMENU", "RMENU", "BROWSER_BACK", "BROWSER_FORWARD",
"BROWSER_REFRESH", "BROWSER_STOP", "BROWSER_SEARCH", "BROWSER_FAVORITES",
"BROWSER_HOME", "VOLUME_MUTE", "VOLUME_DOWN", "VOLUME_UP",
// 0xB0
"MEDIA_NEXT_TRACK", "MEDIA_PREV_TRACK", "MEDIA_STOP", "MEDIA_PLAY_PAUSE",
"LAUNCH_MAIL", "LAUNCH_MEDIA_SELECT", "LAUNCH_APP1", "LAUNCH_APP2",
NULL, NULL, "OEM_1", "OEM_PLUS",
"OEM_COMMA", "OEM_MINUS", "OEM_PERIOD", "OEM_2",
// 0xC0
"OEM_3", NULL, NULL, NULL,
NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL,
// 0xD0
NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL,
NULL, NULL, NULL, "OEM_4",
"OEM_5", "OEM_6", "OEM_7", "OEM_8",
// 0xE0
NULL, NULL, "OEM_102", NULL,
NULL, "PROCESSKEY", NULL, "PACKET",
NULL, "OEM_RESET", "OEM_JUMP", "OEM_PA1",
"OEM_PA2", "OEM_PA3", "OEM_WSCTRL", NULL,
// 0xF0
NULL, NULL, NULL, NULL,
NULL, NULL, "ATTN", "CRSEL",
"EXSEL", "EREOF", "PLAY", "ZOOM",
"NONAME", "PA1", "OEM_CLEAR", NULL,
};
static lua_CFunction luaopen_bit = NULL;
static lua_CFunction luaopen_ffi = NULL;
static lua_CFunction luaopen_jit = NULL;
int IsLuaJIT() { return luaopen_jit != NULL; }
BOOL WINAPI DllMain(HANDLE hDll, DWORD dwReason, LPVOID lpReserved)
{
(void) lpReserved;
if(DLL_PROCESS_ATTACH == dwReason && hDll)
{
// Try to load LuaJIT 2.0 libraries. This is done dynamically to ensure that
// LuaFAR works with either Lua 5.1 or LuaJIT 2.0
HMODULE hLib = GetModuleHandleA(LUADLL);
if (hLib)
{
luaopen_bit = (lua_CFunction) GetProcAddress(hLib, "luaopen_bit");
luaopen_ffi = (lua_CFunction) GetProcAddress(hLib, "luaopen_ffi");
luaopen_jit = (lua_CFunction) GetProcAddress(hLib, "luaopen_jit");
}
}
return TRUE;
}
static TSynchroData* CreateSynchroData(TTimerData *td, int action, int data)
{
TSynchroData* SD = (TSynchroData*) malloc(sizeof(TSynchroData));
SD->timerData = td;
SD->regAction = action;
SD->data = data;
return SD;
}
static int IsFarSpring(lua_State *L)
{
TPluginData *pd = GetPluginData(L);
return pd->Info->Private != NULL; // FIXME
}
HANDLE OptHandle(lua_State *L)
{
switch(lua_type(L,1))
{
case LUA_TNONE:
case LUA_TNIL:
break;
case LUA_TNUMBER:
return (HANDLE)lua_tointeger(L,1);
case LUA_TUSERDATA:
return *(HANDLE*)luaL_checkudata(L, 1, PluginHandleType);
case LUA_TLIGHTUSERDATA:
return lua_touserdata(L,1);
default:
luaL_typerror(L, 1, "integer or userdata");
}
return NULL;
}
HANDLE OptHandle2(lua_State *L)
{
return lua_isnoneornil(L,1) ? (luaL_checkinteger(L,2) % 2 ? PANEL_ACTIVE:PANEL_PASSIVE) : OptHandle(L);
}
static UINT64 get_env_flag(lua_State *L, int pos, int *success)
{
int dummy;
const char *str;
INT64 ret = 0;
if (success)
*success = TRUE;
else
success = &dummy;
switch(lua_type(L, pos))
{
case LUA_TNONE:
case LUA_TNIL:
break;
case LUA_TNUMBER:
ret = (__int64)lua_tonumber(L, pos); // IMPORTANT: cast to signed integer.
break;
case LUA_TSTRING:
str = lua_tostring(L, pos);
lua_getfield(L, LUA_REGISTRYINDEX, FAR_FLAGSTABLE);
lua_getfield(L, -1, str);
if (lua_type(L, -1) == LUA_TNUMBER)
ret = (__int64)lua_tonumber(L, -1); // IMPORTANT: cast to signed integer.
else if (!bit64_getvalue(L, -1, &ret))
*success = FALSE;
lua_pop(L, 2);
break;
default:
if (!bit64_getvalue(L, pos, &ret))
*success = FALSE;
break;
}
return ret;
}
static UINT64 check_env_flag(lua_State *L, int pos)
{
int success = FALSE;
UINT64 ret = get_env_flag(L, pos, &success);
if (!success)
{
if (lua_isstring(L, pos))
{
lua_pushfstring(L, "invalid flag: \"%s\"", lua_tostring(L, pos));
luaL_argerror(L, pos, lua_tostring(L, -1));
}
else
luaL_argerror(L, pos, "invalid flag");
}
return ret;
}
UINT64 GetFlagCombination(lua_State *L, int pos, int *success)
{
UINT64 ret = 0;
if(lua_type(L, pos) == LUA_TTABLE)
{
if(success)
*success = TRUE;
pos = abs_index(L, pos);
lua_pushnil(L);
while(lua_next(L, pos))
{
if(lua_type(L,-2)==LUA_TSTRING && lua_toboolean(L,-1))
{
UINT64 flag = get_env_flag(L, -2, success);
if(success == NULL || *success)
ret |= flag;
else
{ lua_pop(L,2); return ret; }
}
lua_pop(L, 1);
}
}
else
ret = get_env_flag(L, pos, success);
return ret;
}
static UINT64 CheckFlags(lua_State* L, int pos)
{
int success = FALSE;
UINT64 Flags = GetFlagCombination(L, pos, &success);
if(!success)
luaL_error(L, "invalid flag combination");
return Flags;
}
UINT64 OptFlags(lua_State* L, int pos, UINT64 dflt)
{
return lua_isnoneornil(L, pos) ? dflt : CheckFlags(L, pos);
}
static UINT64 CheckFlagsFromTable(lua_State *L, int pos, const char* key)
{
UINT64 f;
lua_getfield(L, pos, key);
f = CheckFlags(L, -1);
lua_pop(L, 1);
return f;
}
UINT64 GetFlagsFromTable(lua_State *L, int pos, const char* key)
{
UINT64 f;
lua_getfield(L, pos, key);
f = GetFlagCombination(L, -1, NULL);
lua_pop(L, 1);
return f;
}
void PutFlagsToTable(lua_State *L, const char* key, UINT64 flags)
{
bit64_push(L, flags);
lua_setfield(L, -2, key);
}
void PutFlagsToArray(lua_State *L, int index, UINT64 flags)
{
bit64_push(L, flags);
lua_rawseti(L, -2, index);
}
TPluginData* GetPluginData(lua_State* L)
{
TPluginData *pd;
(void) lua_getallocf(L, (void**)&pd);
return pd;
}
static void PushPluginHandle(lua_State *L, HANDLE Handle)
{
if (Handle)
{
HANDLE *p = (HANDLE*)lua_newuserdata(L, sizeof(HANDLE));
*p = Handle;
luaL_getmetatable(L, PluginHandleType);
lua_setmetatable(L, -2);
}
else
lua_pushnil(L);
}
void ConvertLuaValue (lua_State *L, int pos, struct FarMacroValue *target)
{
INT64 val64;
int type = lua_type(L, pos);
pos = abs_index(L, pos);
target->Type = FMVT_UNKNOWN;
if(type == LUA_TNUMBER)
{
target->Type = FMVT_DOUBLE;
target->Value.Double = lua_tonumber(L, pos);
}
else if(type == LUA_TSTRING)
{
target->Type = FMVT_STRING;
target->Value.String = check_utf8_string(L, pos, NULL);
}
else if(type == LUA_TTABLE)
{
lua_rawgeti(L,pos,1);
if (lua_type(L,-1) == LUA_TSTRING)
{
target->Type = FMVT_BINARY;
target->Value.Binary.Data = (void*)lua_tolstring(L, -1, &target->Value.Binary.Size);
}
lua_pop(L,1);
}
else if(type == LUA_TBOOLEAN)
{
target->Type = FMVT_BOOLEAN;
target->Value.Boolean = lua_toboolean(L, pos);
}
else if(type == LUA_TNIL)
{
target->Type = FMVT_NIL;
}
else if(type == LUA_TLIGHTUSERDATA)
{
target->Type = FMVT_POINTER;
target->Value.Pointer = lua_touserdata(L, pos);
}
else if(bit64_getvalue(L, pos, &val64))
{
target->Type = FMVT_INTEGER;
target->Value.Integer = val64;
}
}
static int far_GetFileOwner(lua_State *L)
{
wchar_t Owner[512];
const wchar_t *Computer = opt_utf8_string(L, 1, NULL);
const wchar_t *Name = check_utf8_string(L, 2, NULL);
if(GetPluginData(L)->FSF->GetFileOwner(Computer, Name, Owner, ARRSIZE(Owner)))
push_utf8_string(L, Owner, -1);
else
lua_pushnil(L);
return 1;
}
static int far_GetNumberOfLinks(lua_State *L)
{
const wchar_t *Name = check_utf8_string(L, 1, NULL);
int num = (int)GetPluginData(L)->FSF->GetNumberOfLinks(Name);
return lua_pushinteger(L, num), 1;
}
static void GetMouseEvent(lua_State *L, MOUSE_EVENT_RECORD* rec)
{
rec->dwMousePosition.X = GetOptIntFromTable(L, "MousePositionX", 0);
rec->dwMousePosition.Y = GetOptIntFromTable(L, "MousePositionY", 0);
rec->dwButtonState = GetOptIntFromTable(L, "ButtonState", 0);
rec->dwControlKeyState = GetOptIntFromTable(L, "ControlKeyState", 0);
rec->dwEventFlags = GetOptIntFromTable(L, "EventFlags", 0);
}
void PutMouseEvent(lua_State *L, const MOUSE_EVENT_RECORD* rec, BOOL table_exist)
{
if(!table_exist)
lua_createtable(L, 0, 5);
PutNumToTable(L, "MousePositionX", rec->dwMousePosition.X);
PutNumToTable(L, "MousePositionY", rec->dwMousePosition.Y);
PutNumToTable(L, "ButtonState", rec->dwButtonState);
PutNumToTable(L, "ControlKeyState", rec->dwControlKeyState);
PutNumToTable(L, "EventFlags", rec->dwEventFlags);
}
// convert a string from utf-8 to wide char and put it into a table,
// to prevent stack overflow and garbage collection
const wchar_t* StoreTempString(lua_State *L, int store_stack_pos, int* index)
{
const wchar_t *s = check_utf8_string(L,-1,NULL);
lua_rawseti(L, store_stack_pos, ++(*index));
return s;
}
static void PushEditorSetPosition(lua_State *L, const struct EditorSetPosition *esp)
{
lua_createtable(L, 0, 6);
PutIntToTable(L, "CurLine", esp->CurLine + 1);
PutIntToTable(L, "CurPos", esp->CurPos + 1);
PutIntToTable(L, "CurTabPos", esp->CurTabPos + 1);
PutIntToTable(L, "TopScreenLine", esp->TopScreenLine + 1);
PutIntToTable(L, "LeftPos", esp->LeftPos + 1);
PutIntToTable(L, "Overtype", esp->Overtype);
}
static void FillEditorSetPosition(lua_State *L, struct EditorSetPosition *esp)
{
esp->CurLine = GetOptIntFromTable(L, "CurLine", 0) - 1;
esp->CurPos = GetOptIntFromTable(L, "CurPos", 0) - 1;
esp->CurTabPos = GetOptIntFromTable(L, "CurTabPos", 0) - 1;
esp->TopScreenLine = GetOptIntFromTable(L, "TopScreenLine", 0) - 1;
esp->LeftPos = GetOptIntFromTable(L, "LeftPos", 0) - 1;
esp->Overtype = GetOptIntFromTable(L, "Overtype", -1);
}
void PushPanelItem(lua_State *L, const struct PluginPanelItem *PanelItem)
{
lua_createtable(L, 0, 16); // "PanelItem"
//-----------------------------------------------------------------------
PutFileTimeToTable (L, "CreationTime", PanelItem->CreationTime);
PutFileTimeToTable (L, "LastAccessTime", PanelItem->LastAccessTime);
PutFileTimeToTable (L, "LastWriteTime", PanelItem->LastWriteTime);
PutFileTimeToTable (L, "ChangeTime", PanelItem->ChangeTime);
PutNumToTable (L, "FileSize", (double)PanelItem->FileSize);
PutNumToTable (L, "AllocationSize", (double)PanelItem->AllocationSize);
PutWStrToTable (L, "FileName", PanelItem->FileName, -1);
PutWStrToTable (L, "AlternateFileName", PanelItem->AlternateFileName, -1);
PutFlagsToTable (L, "Flags", PanelItem->Flags);
PutNumToTable (L, "NumberOfLinks", (double)PanelItem->NumberOfLinks);
PutNumToTable (L, "CRC32", (double)PanelItem->CRC32);
PutAttrToTable(L, (int)PanelItem->FileAttributes);
if(PanelItem->Description)
PutWStrToTable(L, "Description", PanelItem->Description, -1);
if(PanelItem->Owner)
PutWStrToTable(L, "Owner", PanelItem->Owner, -1);
/* not clear why custom columns are defined on per-file basis */
if(PanelItem->CustomColumnNumber > 0)
{
int j;
lua_createtable(L, (int)PanelItem->CustomColumnNumber, 0);
for(j=0; j < (int)PanelItem->CustomColumnNumber; j++)
PutWStrToArray(L, j+1, PanelItem->CustomColumnData[j], -1);
lua_setfield(L, -2, "CustomColumnData");
}
if(PanelItem->UserData.Data && PanelItem->UserData.FreeData==FarPanelItemFreeCallback)
{
// This is a panel of a LuaFAR plugin
FarPanelItemUserData* ud = (FarPanelItemUserData*)PanelItem->UserData.Data;
if(ud->L == L)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, ud->ref);
lua_setfield(L, -2, "UserData");
}
}
}
//---------------------------------------------------------------------------
void PushPanelItems(lua_State *L, const struct PluginPanelItem *PanelItems, size_t ItemsNumber)
{
int i;
lua_createtable(L, (int)ItemsNumber, 0); // "PanelItems"
for(i=0; i < (int)ItemsNumber; i++)
{
PushPanelItem(L, PanelItems + i);
lua_rawseti(L, -2, i+1);
}
}
//---------------------------------------------------------------------------
static int far_PluginStartupInfo(lua_State *L)
{
const wchar_t *p;
intptr_t len=0;
TPluginData *pd = GetPluginData(L);
lua_createtable(L, 0, 4);
PutWStrToTable(L, "ModuleName", pd->Info->ModuleName, -1);
for(p=pd->Info->ModuleName; *p; p++)
{
if(*p==L'\\') len = p - pd->Info->ModuleName;
}
PutWStrToTable(L, "ModuleDir", pd->Info->ModuleName, len+1);
PutLStrToTable(L, "PluginGuid", pd->PluginId, sizeof(GUID));
return 1;
}
static int far_GetCurrentDirectory(lua_State *L)
{
struct FarStandardFunctions *FSF = GetPluginData(L)->FSF;
size_t size = FSF->GetCurrentDirectory(0, NULL);
wchar_t* buf = (wchar_t*)lua_newuserdata(L, size * sizeof(wchar_t));
FSF->GetCurrentDirectory(size, buf);
push_utf8_string(L, buf, -1);
return 1;
}
static int push_ev_filename(lua_State *L, int isEditor, intptr_t Id)
{
wchar_t* fname;
size_t size;
PSInfo *Info = GetPluginData(L)->Info;
size = isEditor ?
Info->EditorControl(Id, ECTL_GETFILENAME, 0, 0) :
Info->ViewerControl(Id, VCTL_GETFILENAME, 0, 0);
if(!size) return 0;
fname = (wchar_t*)lua_newuserdata(L, size * sizeof(wchar_t));
size = isEditor ?
Info->EditorControl(Id, ECTL_GETFILENAME, size, fname) :
Info->ViewerControl(Id, VCTL_GETFILENAME, size, fname);
if(size)
{
push_utf8_string(L, fname, -1);
lua_remove(L, -2);
return 1;
}
lua_pop(L,1);
return 0;
}
static int editor_GetFileName(lua_State *L)
{
intptr_t EditorId = luaL_optinteger(L, 1, CURRENT_EDITOR);
if(!push_ev_filename(L, 1, EditorId)) lua_pushnil(L);
return 1;
}
static int editor_GetInfo(lua_State *L)
{
intptr_t EditorId = luaL_optinteger(L, 1, CURRENT_EDITOR);
PSInfo *Info = GetPluginData(L)->Info;
struct EditorInfo ei;
ei.StructSize = sizeof(ei);
if(!Info->EditorControl(EditorId, ECTL_GETINFO, 0, &ei))
return lua_pushnil(L), 1;
lua_createtable(L, 0, 18);
PutNumToTable(L, "EditorID", (double)ei.EditorID);
if(push_ev_filename(L, 1, EditorId))
lua_setfield(L, -2, "FileName");
PutNumToTable(L, "WindowSizeX", (double) ei.WindowSizeX);
PutNumToTable(L, "WindowSizeY", (double) ei.WindowSizeY);
PutNumToTable(L, "TotalLines", (double) ei.TotalLines);
PutNumToTable(L, "CurLine", (double) ei.CurLine + 1);
PutNumToTable(L, "CurPos", (double) ei.CurPos + 1);
PutNumToTable(L, "CurTabPos", (double) ei.CurTabPos + 1);
PutNumToTable(L, "TopScreenLine", (double) ei.TopScreenLine + 1);
PutNumToTable(L, "LeftPos", (double) ei.LeftPos + 1);
PutNumToTable(L, "Overtype", (double) ei.Overtype);
PutNumToTable(L, "BlockType", (double) ei.BlockType);
PutNumToTable(L, "BlockStartLine", (double) ei.BlockStartLine + 1);
PutNumToTable(L, "Options", (double) ei.Options);
PutNumToTable(L, "TabSize", (double) ei.TabSize);
PutNumToTable(L, "BookmarkCount", (double) ei.BookmarkCount);
PutNumToTable(L, "SessionBookmarkCount", (double) ei.SessionBookmarkCount);
PutNumToTable(L, "CurState", (double) ei.CurState);
PutNumToTable(L, "CodePage", (double) ei.CodePage);
return 1;
}
/* t-rex:
* Äëÿ òåõ êîìó ïëîõî äîõîäèò îïèñûâàþ:
* Ðåäàêòîð â ôàðå ýòî äâóõ ñâÿçíûé ñïèñîê, óêàçàòåëü íà òåêóùþþ ñòðîêó
* èçìåíÿåòñÿ òîëüêî ïðè ECTL_SETPOSITION, ïðè èñïîëüçîâàíèè ëþáîé äðóãîé
* ECTL_* äëÿ êîòîðîé íóæíî çàäàâàòü íîìåð ñòðîêè åñëè ýòîò íîìåð íå -1
* (ò.å. òåêóùààÿ ñòðîêà) òî ôàð äîëæåí íàéòè ýòó ñòðîêó â ñïèñêå (à ýòî
* çàíèìàåò äîôèãà âðåìåíè), ïîýòîìó åñëè íàäî äåëàòü íåñêîëüêî ECTL_*
* (òåì áîëåå êîãäà îíè äåëàþòñÿ íà ïîñëåäîâàòåëüíîñòü ñòðîê
* i,i+1,i+2,...) òî ïåðåä êàæäûì ECTL_* íàäî äåëàòü ECTL_SETPOSITION à
* ñàìè ECTL_* âûçûâàòü ñ -1.
*/
static BOOL FastGetString(intptr_t EditorId, intptr_t string_num,
struct EditorGetString *egs, PSInfo *Info)
{
struct EditorSetPosition esp;
esp.StructSize = sizeof(esp);
esp.CurLine = string_num;
esp.CurPos = -1;
esp.CurTabPos = -1;
esp.TopScreenLine = -1;
esp.LeftPos = -1;
esp.Overtype = -1;
if(!Info->EditorControl(EditorId, ECTL_SETPOSITION, 0, &esp))
return FALSE;
egs->StringNumber = string_num;
return Info->EditorControl(EditorId, ECTL_GETSTRING, 0, egs) != 0;
}
// EditorGetString (EditorId, line_num, [mode])
//
// line_num: number of line in the Editor, a 1-based integer.
//
// mode: 0 = returns: table LineInfo; changes current position: no
// 1 = returns: table LineInfo; changes current position: yes
// 2 = returns: StringText,StringEOL; changes current position: yes
// 3 = returns: StringText,StringEOL; changes current position: no
//
// return: either table LineInfo or StringText,StringEOL - depending on `mode` argument.
//
static int _EditorGetString(lua_State *L, int is_wide)
{
intptr_t EditorId = luaL_optinteger(L, 1, CURRENT_EDITOR);
PSInfo *Info = GetPluginData(L)->Info;
intptr_t line_num = luaL_optinteger(L, 2, 0) - 1;
intptr_t mode = luaL_optinteger(L, 3, 0);
BOOL res = 0;
struct EditorGetString egs;
egs.StructSize = sizeof(egs);
if(mode == 0 || mode == 3)
{
egs.StringNumber = line_num;
res = Info->EditorControl(EditorId, ECTL_GETSTRING, 0, &egs) != 0;
}
else if(mode == 1 || mode == 2)
res = FastGetString(EditorId, line_num, &egs, Info);
if(res)
{
if(mode == 2 || mode == 3)
{
if(is_wide)
{
push_utf16_string(L, egs.StringText, egs.StringLength);
push_utf16_string(L, egs.StringEOL, -1);
}
else
{
push_utf8_string(L, egs.StringText, egs.StringLength);
push_utf8_string(L, egs.StringEOL, -1);
}
return 2;
}
else
{
lua_createtable(L, 0, 6);
PutNumToTable(L, "StringNumber", (double)egs.StringNumber+1);
PutNumToTable(L, "StringLength", (double)egs.StringLength);
PutNumToTable(L, "SelStart", (double)egs.SelStart+1);
PutNumToTable(L, "SelEnd", (double)egs.SelEnd);
if(is_wide)
{
push_utf16_string(L, egs.StringText, egs.StringLength);
lua_setfield(L, -2, "StringText");
push_utf16_string(L, egs.StringEOL, -1);
lua_setfield(L, -2, "StringEOL");
}
else
{
PutWStrToTable(L, "StringText", egs.StringText, egs.StringLength);
PutWStrToTable(L, "StringEOL", egs.StringEOL, -1);
}
}
return 1;
}
return lua_pushnil(L), 1;
}
static int editor_GetString(lua_State *L) { return _EditorGetString(L, 0); }
static int editor_GetStringW(lua_State *L) { return _EditorGetString(L, 1); }
static int _EditorSetString(lua_State *L, int is_wide)
{
PSInfo *Info = GetPluginData(L)->Info;
struct EditorSetString ess;
intptr_t EditorId = luaL_optinteger(L, 1, CURRENT_EDITOR);
size_t len;
ess.StructSize = sizeof(ess);
ess.StringNumber = luaL_optinteger(L, 2, 0) - 1;
if(is_wide)
{
ess.StringText = check_utf16_string(L, 3, &len);
ess.StringEOL = opt_utf16_string(L, 4, NULL);
if(ess.StringEOL)
{
lua_pushvalue(L, 4);
lua_pushliteral(L, "\0\0");
lua_concat(L, 2);
ess.StringEOL = (wchar_t*) lua_tostring(L, -1);
}
}
else
{
ess.StringText = check_utf8_string(L, 3, &len);
ess.StringEOL = opt_utf8_string(L, 4, NULL);
}
ess.StringLength = len;
lua_pushboolean(L, Info->EditorControl(EditorId, ECTL_SETSTRING, 0, &ess) != 0);
return 1;
}
static int editor_SetString(lua_State *L) { return _EditorSetString(L, 0); }
static int editor_SetStringW(lua_State *L) { return _EditorSetString(L, 1); }
static int editor_InsertString(lua_State *L)
{
PSInfo *Info = GetPluginData(L)->Info;
intptr_t EditorId = luaL_optinteger(L, 1, CURRENT_EDITOR);
int indent = lua_toboolean(L, 2);
lua_pushboolean(L, Info->EditorControl(EditorId, ECTL_INSERTSTRING, 0, &indent) != 0);
return 1;
}
static int editor_DeleteString(lua_State *L)
{
PSInfo *Info = GetPluginData(L)->Info;
intptr_t EditorId = luaL_optinteger(L, 1, CURRENT_EDITOR);
lua_pushboolean(L, Info->EditorControl(EditorId, ECTL_DELETESTRING, 0, 0) != 0);
return 1;
}
static int _EditorInsertText(lua_State *L, int is_wide)
{
intptr_t EditorId = luaL_optinteger(L, 1, CURRENT_EDITOR);
PSInfo *Info = GetPluginData(L)->Info;
const wchar_t* text;
if (is_wide)
{
size_t len;
const char *s = luaL_checklstring(L,2,&len);
int needZero = 0;
if (len % sizeof(wchar_t))
{
if (s[len-1] && --len)
needZero = 1;
}
else
needZero = len && (s[len-2] || s[len-1]);
if (needZero)
{
lua_pushlstring(L, s, len);
lua_pushlstring(L, "\0", 1);
lua_concat(L, 2);
text = (const wchar_t*)lua_tostring(L, -1);
}
else
text = len ? (const wchar_t*)s : L"";
}
else
{
text = check_utf8_string(L,2,NULL);
}
lua_pushboolean(L, Info->EditorControl(EditorId, ECTL_INSERTTEXT, 0, (void*)text) != 0);
return 1;
}
static int editor_InsertText(lua_State *L) { return _EditorInsertText(L, 0); }
static int editor_InsertTextW(lua_State *L) { return _EditorInsertText(L, 1); }
static int editor_DeleteChar(lua_State *L)
{
intptr_t EditorId = luaL_optinteger(L, 1, CURRENT_EDITOR);
PSInfo *Info = GetPluginData(L)->Info;
lua_pushboolean(L, Info->EditorControl(EditorId, ECTL_DELETECHAR, 0, 0) != 0);
return 1;
}
static int editor_DeleteBlock(lua_State *L)
{
intptr_t EditorId = luaL_optinteger(L, 1, CURRENT_EDITOR);
PSInfo *Info = GetPluginData(L)->Info;
lua_pushboolean(L, Info->EditorControl(EditorId, ECTL_DELETEBLOCK, 0, 0) != 0);
return 1;
}
static int editor_UndoRedo(lua_State *L)
{
intptr_t EditorId = luaL_optinteger(L, 1, CURRENT_EDITOR);
struct EditorUndoRedo eur;
memset(&eur, 0, sizeof(eur));
eur.StructSize = sizeof(eur);
eur.Command = check_env_flag(L, 2);
lua_pushboolean(L, GetPluginData(L)->Info->EditorControl(EditorId, ECTL_UNDOREDO, 0, &eur) != 0);
return 1;
}
static void FillKeyBarTitles(lua_State *L, int src_pos, struct KeyBarTitles *kbt)
{
int store=0, store_pos, i;
size_t size;
lua_newtable(L);
store_pos = lua_gettop(L);
//-------------------------------------------------------------------------
memset(kbt, 0, sizeof(*kbt));
kbt->CountLabels = lua_objlen(L, src_pos);
size = kbt->CountLabels * sizeof(struct KeyBarLabel);
kbt->Labels = (struct KeyBarLabel*)lua_newuserdata(L, size);
memset(kbt->Labels, 0, size);
for(i=0; i < (int)kbt->CountLabels; i++)
{
lua_rawgeti(L, src_pos, i+1);
if(!lua_istable(L, -1))
{
kbt->CountLabels = i;
lua_pop(L, 1);
break;
}
kbt->Labels[i].Key.VirtualKeyCode = GetOptIntFromTable(L, "VirtualKeyCode", 0);
kbt->Labels[i].Key.ControlKeyState = CAST(DWORD,CheckFlagsFromTable(L, -1, "ControlKeyState"));
//-----------------------------------------------------------------------
lua_getfield(L, -1, "Text");
kbt->Labels[i].Text = StoreTempString(L, store_pos, &store);
//-----------------------------------------------------------------------
lua_getfield(L, -1, "LongText");
kbt->Labels[i].LongText = StoreTempString(L, store_pos, &store);
//-----------------------------------------------------------------------
lua_pop(L, 1);
}
}
static int SetKeyBar(lua_State *L, BOOL editor)
{
intptr_t result;
void* param;
struct KeyBarTitles kbt;
struct FarSetKeyBarTitles skbt;
intptr_t Id = luaL_optinteger(L, 1, -1);
PSInfo *Info = GetPluginData(L)->Info;
enum { REDRAW=-1, RESTORE=0 }; // corresponds to FAR API
BOOL argfail = FALSE;
if(lua_isstring(L,2))
{
const char* p = lua_tostring(L,2);
if(0 == strcmp("redraw", p)) param = (void*)REDRAW;
else if(0 == strcmp("restore", p)) param = (void*)RESTORE;
else argfail = TRUE;
}
else if(lua_istable(L,2))
{
param = &skbt;
FillKeyBarTitles(L, 2, &kbt);
skbt.StructSize = sizeof(skbt);
skbt.Titles = &kbt;
}
else
argfail = TRUE;
if(argfail)
return luaL_argerror(L, 2, "must be 'redraw', 'restore', or table");
result = editor ? Info->EditorControl(Id, ECTL_SETKEYBAR, 0, param) :
Info->ViewerControl(Id, VCTL_SETKEYBAR, 0, param);
lua_pushboolean(L, result != 0);
return 1;
}
static int editor_SetKeyBar(lua_State *L)
{
return SetKeyBar(L, TRUE);
}
static int viewer_SetKeyBar(lua_State *L)
{
return SetKeyBar(L, FALSE);
}
static int editor_SetParam(lua_State *L)
{
wchar_t buf[256];
int tp;
intptr_t result;
intptr_t EditorId = luaL_optinteger(L, 1, CURRENT_EDITOR);
PSInfo *Info = GetPluginData(L)->Info;
struct EditorSetParameter esp;
memset(&esp, 0, sizeof(esp));
esp.StructSize = sizeof(esp);
esp.Type = check_env_flag(L,2);
//-----------------------------------------------------
tp = lua_type(L,3);
if(tp == LUA_TNUMBER)
esp.Param.iParam = lua_tointeger(L,3);
else if(tp == LUA_TBOOLEAN)
esp.Param.iParam = lua_toboolean(L,3);
else if(tp == LUA_TSTRING)
esp.Param.wszParam = (wchar_t*)check_utf8_string(L,3,NULL);
//-----------------------------------------------------
if(esp.Type == ESPT_GETWORDDIV)
{
esp.Param.wszParam = buf;
esp.Size = ARRSIZE(buf);
}
//-----------------------------------------------------
esp.Flags = GetFlagCombination(L, 4, NULL);
//-----------------------------------------------------
result = Info->EditorControl(EditorId, ECTL_SETPARAM, 0, &esp);
lua_pushboolean(L, result != 0);
if(result && esp.Type == ESPT_GETWORDDIV)
{
push_utf8_string(L,buf,-1); return 2;
}
return 1;
}
static int editor_SetPosition(lua_State *L)
{