LEMacros.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // IMPORTANT: View the complexity here since there is no concrete (visual)
  3. // quick and reliable way to see what's in and out of the functions,
  4. // please comment properly using the current function's comment
  5. // syntax.
  6. ////////////////////////////////////////////////////////////////////////////////
  7. unit LEMacros;
  8. interface
  9. uses lua, lualib, lauxlib, Forms, SysUtils, LuaUtils, Misc, SynEdit,
  10. LuaEditMessages, Classes, Registry, Windows;
  11. procedure LERegisterToLua(L: Plua_State; NeedLuaLibs: Boolean = True);
  12. implementation
  13. uses Main;
  14. ////////////////////////////////////////////////////////////////////////////////
  15. // LuaEdit related functions
  16. ////////////////////////////////////////////////////////////////////////////////
  17. ////////////////////////////////////////////////////////////////////////////////
  18. // Desc: This function open a file using LuaEdit's open file dialog
  19. // In: None
  20. // Out: (1) True if the operation ended successfully. False if canceled,
  21. // unavailable (disabled) or failed.
  22. //
  23. // 14/05/2006 - Jean-Francois Goulet
  24. ////////////////////////////////////////////////////////////////////////////////
  25. function LuaLEOpenFile(L: Plua_State): Integer; cdecl;
  26. var
  27. FilesName: TStringList;
  28. x: Integer;
  29. begin
  30. // Initialize variables
  31. FilesName := TStringList.Create;
  32. // Retrieve all files' name (if any...)
  33. for x := 1 to lua_gettop(L) do
  34. if lua_type(L, x) = LUA_TSTRING then
  35. FilesName.Add(StrPas(lua_tostring(L, x)));
  36. // Return the luaedit version as a string
  37. lua_pushboolean(L, Integer(frmLuaEditMain.DoOpenFileExecute(FilesName)));
  38. // Free variables
  39. FilesName.Free;
  40. // Return in Delphi the number of argument pushed on the stack
  41. Result := 1;
  42. end;
  43. ////////////////////////////////////////////////////////////////////////////////
  44. // Desc: This function open a project using LuaEdit's open project dialog
  45. // In: None
  46. // Out: (1) True if the operation ended successfully. False if canceled,
  47. // unavailable (disabled) or failed.
  48. //
  49. // 14/05/2006 - Jean-Francois Goulet
  50. ////////////////////////////////////////////////////////////////////////////////
  51. function LuaLEOpenProject(L: Plua_State): Integer; cdecl;
  52. begin
  53. // Return the luaedit version as a string
  54. lua_pushboolean(L, Integer(frmLuaEditMain.DoOpenProjectExecute()));
  55. // Return in Delphi the number of argument pushed on the stack
  56. Result := 1;
  57. end;
  58. ////////////////////////////////////////////////////////////////////////////////
  59. // Desc: This function save all opened files
  60. // In: None
  61. // Out: (1) True if the operation ended successfully. False if canceled,
  62. // unavailable (disabled) or failed.
  63. //
  64. // 14/05/2006 - Jean-Francois Goulet
  65. ////////////////////////////////////////////////////////////////////////////////
  66. function LuaLESaveAll(L: Plua_State): Integer; cdecl;
  67. begin
  68. // Return the luaedit version as a string
  69. lua_pushboolean(L, Integer(frmLuaEditMain.DoSaveAllExecute()));
  70. // Return in Delphi the number of argument pushed on the stack
  71. Result := 1;
  72. end;
  73. ////////////////////////////////////////////////////////////////////////////////
  74. // Desc: This function save the current active project as...
  75. // In: None
  76. // Out: (1) True if the operation ended successfully. False if canceled,
  77. // unavailable (disabled) or failed.
  78. //
  79. // 14/05/2006 - Jean-Francois Goulet
  80. ////////////////////////////////////////////////////////////////////////////////
  81. function LuaLESavePrjAs(L: Plua_State): Integer; cdecl;
  82. begin
  83. // Return the luaedit version as a string
  84. lua_pushboolean(L, Integer(frmLuaEditMain.DoSaveProjectAsExecute()));
  85. // Return in Delphi the number of argument pushed on the stack
  86. Result := 1;
  87. end;
  88. ////////////////////////////////////////////////////////////////////////////////
  89. // Desc: This function save the currently opened unit as...
  90. // In: (1) Path of unit to save as
  91. // Out: (1) True if the operation ended successfully. False if canceled,
  92. // unavailable (disabled) or failed.
  93. //
  94. // 14/05/2006 - Jean-Francois Goulet
  95. ////////////////////////////////////////////////////////////////////////////////
  96. function LuaLESaveUnitAs(L: Plua_State): Integer; cdecl;
  97. var
  98. bRes: Boolean;
  99. sPath: String;
  100. pFile: TLuaEditBasicTextFile;
  101. begin
  102. // Initialize result
  103. bRes := False;
  104. // Retrieve SAFELY the first parameter
  105. if lua_type(L, -1) = LUA_TSTRING then
  106. sPath := lua_tostring(L, -1);
  107. pFile := frmLuaEditMain.GetOpenedFile(sPath);
  108. // Save the specified file
  109. if Assigned(pFile) then
  110. bRes := frmLuaEditMain.DoSaveAsExecute(pFile);
  111. lua_pushboolean(L, Integer(bRes));
  112. // Return in Delphi the number of argument pushed on the stack
  113. Result := 1;
  114. end;
  115. ////////////////////////////////////////////////////////////////////////////////
  116. // Desc: This function save the currently opened unit
  117. // In: (1) Path of unit to save
  118. // Out: (1) True if the operation ended successfully. False if canceled,
  119. // unavailable (disabled) or failed.
  120. //
  121. // 14/05/2006 - Jean-Francois Goulet
  122. ////////////////////////////////////////////////////////////////////////////////
  123. function LuaLESaveUnit(L: Plua_State): Integer; cdecl;
  124. var
  125. bRes: Boolean;
  126. sPath: String;
  127. pFile: TLuaEditBasicTextFile;
  128. begin
  129. // Initialize result
  130. bRes := False;
  131. // Retrieve SAFELY the first parameter
  132. if lua_type(L, -1) = LUA_TSTRING then
  133. sPath := lua_tostring(L, -1);
  134. pFile := frmLuaEditMain.GetOpenedFile(sPath);
  135. // Save the specified file
  136. if Assigned(pFile) then
  137. bRes := frmLuaEditMain.DoSaveExecute(pFile);
  138. lua_pushboolean(L, Integer(bRes));
  139. // Return in Delphi the number of argument pushed on the stack
  140. Result := 1;
  141. end;
  142. ////////////////////////////////////////////////////////////////////////////////
  143. // Desc: This function return informations about the specified unit
  144. // In: (1) [OPTIONAL] The index of the unit from which to retrieve informations
  145. // Out: (1) A lua table formatted this way:
  146. // t = {}
  147. // t["Name"] = "Unit1.lua"
  148. // t["Path"] = "C:\\Program Files\\LuaEdit\\Unit1.lua"
  149. // t["IsReadOnly"] = False
  150. // t["IsNew"] = False
  151. // t["HasChanged"] = True
  152. // t["Text"] = {}
  153. // t["Text"]["Count"] = 3
  154. // t["Text"][-1] = "print(123)??print(456)??print(789)"
  155. // t["Text"][0] = "print(123)"
  156. // t["Text"][1] = "print(456)"
  157. // t["Text"][2] = "print(789)"
  158. //
  159. // 14/05/2006 - Jean-Francois Goulet
  160. ////////////////////////////////////////////////////////////////////////////////
  161. function LuaLEGetFile(L: Plua_State): Integer; cdecl;
  162. var
  163. Index, x: Integer;
  164. pLuaUnit: TLuaEditUnit;
  165. begin
  166. // Retrieve SAFELY the first parameter
  167. if lua_type(L, -1) = LUA_TNUMBER then
  168. Index := Trunc(lua_tonumber(L, -1))
  169. else if lua_gettop(L) = 0 then
  170. Index := -1;
  171. // Retrieve currently selected tab index
  172. if Index = -1 then
  173. if Assigned(frmLuaEditMain.jvUnitBar.SelectedTab) then
  174. Index := frmLuaEditMain.jvUnitBar.SelectedTab.Index;
  175. if ((Index >= 0) and (Index < frmLuaEditMain.jvUnitBar.Tabs.Count)) then
  176. begin
  177. // Verify availability of the data at the specified index
  178. if Assigned(frmLuaEditMain.jvUnitBar.Tabs[Index]) then
  179. begin
  180. if Assigned(frmLuaEditMain.jvUnitBar.Tabs[Index].Data) then
  181. begin
  182. // Retrieve unit data
  183. pLuaUnit := TLuaEditUnit(frmLuaEditMain.jvUnitBar.Tabs[Index].Data);
  184. // Create table on the lua stack which will be returned to lua
  185. lua_newtable(L);
  186. // Push "Name" data
  187. lua_pushstring(L, 'Name');
  188. lua_pushstring(L, PChar(pLuaUnit.Name));
  189. lua_settable(L, -3);
  190. // Push "Path" data
  191. lua_pushstring(L, 'Path');
  192. lua_pushstring(L, PChar(pLuaUnit.DisplayPath));
  193. lua_settable(L, -3);
  194. // Push "IsLoaded" data
  195. lua_pushstring(L, 'IsLoaded');
  196. lua_pushboolean(L, Integer(pLuaUnit.IsLoaded));
  197. lua_settable(L, -3);
  198. // Push "IsReadOnly" data
  199. lua_pushstring(L, 'IsReadOnly');
  200. lua_pushboolean(L, Integer(pLuaUnit.IsReadOnly));
  201. lua_settable(L, -3);
  202. // Push "IsNew" data
  203. lua_pushstring(L, 'IsNew');
  204. lua_pushboolean(L, Integer(pLuaUnit.IsNew));
  205. lua_settable(L, -3);
  206. // Push "HasChanged" data
  207. lua_pushstring(L, 'HasChanged');
  208. lua_pushboolean(L, Integer(pLuaUnit.HasChanged));
  209. lua_settable(L, -3);
  210. // Push "Text" data
  211. lua_pushstring(L, 'Text');
  212. lua_newtable(L);
  213. // Push "Text"."Count" data
  214. lua_pushstring(L, 'Count');
  215. lua_pushnumber(L, pLuaUnit.synUnit.Lines.Count);
  216. lua_settable(L, -3);
  217. // Push "Text".-1 data
  218. lua_pushnumber(L, -1);
  219. lua_pushstring(L, PChar(pLuaUnit.synUnit.Text));
  220. lua_settable(L, -3);
  221. // Push "Text".x data
  222. for x := 0 to pLuaUnit.synUnit.Lines.Count - 1 do
  223. begin
  224. lua_pushnumber(L, x);
  225. lua_pushstring(L, PChar(pLuaUnit.synUnit.Lines[x]));
  226. lua_settable(L, -3);
  227. end;
  228. // Push the "Text" table
  229. lua_settable(L, -3);
  230. end;
  231. end;
  232. end;
  233. // Return nil if no unit associated to the given index was found
  234. if not Assigned(pLuaUnit) then
  235. lua_pushnil(L);
  236. // Return in Delphi the number of argument pushed on the stack
  237. Result := 1;
  238. end;
  239. ////////////////////////////////////////////////////////////////////////////////
  240. // Desc: This function return informations about the current active project
  241. // In: None
  242. // Out: (1) A lua table formatted this way:
  243. // t = {}
  244. // t["Name"] = "Project1"
  245. // t["Path"] = "C:\\Program Files\\LuaEdit\\Project1.lpr"
  246. // t["Initializer"] = "C:\\Initializer.dll"
  247. // t["RemoteIP"] = "192.168.0.1"
  248. // t["RemoteDirectory"] = "//RemoteDir//Bin//"
  249. // t["RuntimeDirectory"] = "C:\\Program Files\\LuaEdit\\"
  250. // t["TargetLuaUnit"] = "C:\\Program Files\\LuaEdit\\Unit1.lua"
  251. // t["CompileDirectory"] = "C:\\Bin\\"
  252. // t["CompileExtension"] = ".luac"
  253. // t["AutoIncRevNumber"] = False
  254. // t["IsReadOnly"] = False
  255. // t["IsNew"] = False
  256. // t["HasChanged"] = True
  257. // t["VersionMajor"] = 1
  258. // t["VersionMinor"] = 0
  259. // t["VersionRelease"] = 1
  260. // t["VersionRevision"] = 0
  261. // t["RemotePort"] = 6666
  262. // t["ConnectTimeOut"] = 10
  263. // t["Files"] = {}
  264. // t["Files"]["Count"] = 2
  265. // t["Files"][0] = "C:\\Program Files\\LuaEdit\\Unit1.lua"
  266. // t["Files"][1] = "C:\\Program Files\\LuaEdit\\Unit2.lua"
  267. //
  268. // 14/05/2006 - Jean-Francois Goulet
  269. ////////////////////////////////////////////////////////////////////////////////
  270. function LuaLEGetActivePrj(L: Plua_State): Integer; cdecl;
  271. var
  272. x: Integer;
  273. pLuaUnit: TLuaEditUnit;
  274. begin
  275. // Verify availability of the data
  276. if Assigned(ActiveProject) then
  277. begin
  278. // Create table on the lua stack which will be returned to lua
  279. lua_newtable(L);
  280. // Push "Name" data
  281. lua_pushstring(L, 'Name');
  282. lua_pushstring(L, PChar(ActiveProject.Name));
  283. lua_settable(L, -3);
  284. // Push "Path" data
  285. lua_pushstring(L, 'Path');
  286. lua_pushstring(L, PChar(ActiveProject.DisplayPath));
  287. lua_settable(L, -3);
  288. // Push "Initializer" data
  289. lua_pushstring(L, 'Initializer');
  290. lua_pushstring(L, PChar(ActiveProject.sInitializer));
  291. lua_settable(L, -3);
  292. // Push "RemoteIP" data
  293. lua_pushstring(L, 'RemoteIP');
  294. lua_pushstring(L, PChar(ActiveProject.sRemoteIP));
  295. lua_settable(L, -3);
  296. // Push "RemoteDirectory" data
  297. lua_pushstring(L, 'RemoteDirectory');
  298. lua_pushstring(L, PChar(ActiveProject.sRemoteDirectory));
  299. lua_settable(L, -3);
  300. // Push "RuntimeDirectory" data
  301. lua_pushstring(L, 'RuntimeDirectory');
  302. lua_pushstring(L, PChar(ActiveProject.sRuntimeDirectory));
  303. lua_settable(L, -3);
  304. // Push "TargetLuaUnit" data
  305. lua_pushstring(L, 'TargetLuaUnit');
  306. lua_pushstring(L, PChar(ActiveProject.sTargetLuaUnit));
  307. lua_settable(L, -3);
  308. // Push "CompileDirectory" data
  309. lua_pushstring(L, 'CompileDirectory');
  310. lua_pushstring(L, PChar(ActiveProject.sCompileDirectory));
  311. lua_settable(L, -3);
  312. // Push "CompileExtension" data
  313. lua_pushstring(L, 'CompileExtension');
  314. lua_pushstring(L, PChar(ActiveProject.sCompileExtension));
  315. lua_settable(L, -3);
  316. // Push "AutoIncRevNumber" data
  317. lua_pushstring(L, 'AutoIncRevNumber');
  318. lua_pushboolean(L, Integer(ActiveProject.AutoIncRevNumber));
  319. lua_settable(L, -3);
  320. // Push "IsReadOnly" data
  321. lua_pushstring(L, 'IsReadOnly');
  322. lua_pushboolean(L, Integer(ActiveProject.IsReadOnly));
  323. lua_settable(L, -3);
  324. // Push "IsNew" data
  325. lua_pushstring(L, 'IsNew');
  326. lua_pushboolean(L, Integer(ActiveProject.IsNew));
  327. lua_settable(L, -3);
  328. // Push "HasChanged" data
  329. lua_pushstring(L, 'HasChanged');
  330. lua_pushboolean(L, Integer(ActiveProject.HasChanged));
  331. lua_settable(L, -3);
  332. // Push "VersionMajor" data
  333. lua_pushstring(L, 'VersionMajor');
  334. lua_pushnumber(L, ActiveProject.iVersionMajor);
  335. lua_settable(L, -3);
  336. // Push "VersionMinor" data
  337. lua_pushstring(L, 'VersionMinor');
  338. lua_pushnumber(L, ActiveProject.iVersionMinor);
  339. lua_settable(L, -3);
  340. // Push "VersionRelease" data
  341. lua_pushstring(L, 'VersionRelease');
  342. lua_pushnumber(L, ActiveProject.iVersionRelease);
  343. lua_settable(L, -3);
  344. // Push "VersionRevision" data
  345. lua_pushstring(L, 'VersionRevision');
  346. lua_pushnumber(L, ActiveProject.iVersionRevision);
  347. lua_settable(L, -3);
  348. // Push "RemotePort" data
  349. lua_pushstring(L, 'RemotePort');
  350. lua_pushnumber(L, ActiveProject.iRemotePort);
  351. lua_settable(L, -3);
  352. // Push "ConnectTimeOut" data
  353. lua_pushstring(L, 'ConnectTimeOut');
  354. lua_pushnumber(L, ActiveProject.iConnectTimeOut);
  355. lua_settable(L, -3);
  356. // Push "Files" data
  357. lua_pushstring(L, 'Files');
  358. lua_newtable(L);
  359. // Push "Files"."Count" data
  360. lua_pushstring(L, 'Count');
  361. lua_pushnumber(L, ActiveProject.lstUnits.Count);
  362. lua_settable(L, -3);
  363. // Push "Files".x data
  364. for x := 0 to ActiveProject.lstUnits.Count - 1 do
  365. begin
  366. lua_pushnumber(L, x);
  367. lua_pushstring(L, PChar(TLuaEditUnit(ActiveProject.lstUnits[x]).Path));
  368. lua_settable(L, -3);
  369. end;
  370. // Push the "Files" table
  371. lua_settable(L, -3);
  372. end;
  373. // Return nil if no unit associated to the given index was found
  374. if not Assigned(pLuaUnit) then
  375. lua_pushnil(L);
  376. // Return in Delphi the number of argument pushed on the stack
  377. Result := 1;
  378. end;
  379. ////////////////////////////////////////////////////////////////////////////////
  380. // Desc: This function print the given message in LuaEdit's message window
  381. // In: (1) The message to print
  382. // (2) The type of message to print (EG: 1 = 'hint', 2 = 'warning', 3 = 'error')
  383. // Out: (1) The complete string as displayed in LuaEdit's message window or nil
  384. //
  385. // 14/05/2006 - Jean-Francois Goulet
  386. ////////////////////////////////////////////////////////////////////////////////
  387. function LuaLEPrint(L: PLua_State): Integer; cdecl;
  388. var
  389. Msg, FullMsg: String;
  390. MsgType: Integer;
  391. begin
  392. // Retrieve SAFELY the second parameter (of the first if there is only one argument)
  393. if lua_type(L, -1) = LUA_TNUMBER then
  394. begin
  395. MsgType := Trunc(lua_tonumber(L, -1));
  396. // Retrieve SAFELY the first parameter (or assign default message type if only one argument was supplied)
  397. if lua_type(L, -2) <> LUA_TSTRING then
  398. begin
  399. Msg := lua_tostring(L, -1);
  400. MsgType := LUAEDIT_HINT_MSG;
  401. end
  402. else
  403. Msg := lua_tostring(L, -2);
  404. // Show message in LuaEdit's message window
  405. FullMsg := Msg + ' - ' + DateTimeToStr(Now);
  406. lua_pushstring(L, PChar(FullMsg));
  407. frmLuaEditMessages.Put(FullMsg, MsgType);
  408. end
  409. else
  410. lua_pushnil(L);
  411. // Return in Delphi the number of argument pushed on the stack
  412. Result := 1;
  413. end;
  414. ////////////////////////////////////////////////////////////////////////////////
  415. // Desc: This function return LuaEdit's version on the stack
  416. // In: None
  417. // Out: (1) A string containing LuaEdit's version
  418. //
  419. // 14/05/2006 - Jean-Francois Goulet
  420. ////////////////////////////////////////////////////////////////////////////////
  421. function LuaLEGetVersion(L: Plua_State): Integer; cdecl;
  422. begin
  423. // Return the luaedit version as a string
  424. lua_pushstring(L, GetFileVersion(PChar(Application.ExeName)));
  425. // Return in Delphi the number of argument pushed on the stack
  426. Result := 1;
  427. end;
  428. ////////////////////////////////////////////////////////////////////////////////
  429. // Desc: This function return LuaEdit's product name on the stack
  430. // In: None
  431. // Out: (1) A string containing LuaEdit's product name
  432. //
  433. // 14/05/2006 - Jean-Francois Goulet
  434. ////////////////////////////////////////////////////////////////////////////////
  435. function LuaLEGetProductName(L: Plua_State): Integer; cdecl;
  436. begin
  437. // Return the luaedit product name
  438. lua_pushstring(L, 'LuaEdit');
  439. // Return in Delphi the number of argument pushed on the stack
  440. Result := 1;
  441. end;
  442. ////////////////////////////////////////////////////////////////////////////////
  443. // Desc: This function exit LuaEdit
  444. // In: None
  445. // Out: None
  446. //
  447. // 14/05/2006 - Jean-Francois Goulet
  448. ////////////////////////////////////////////////////////////////////////////////
  449. function LuaLEExit(L: PLua_State): Integer; cdecl;
  450. begin
  451. // Safely exit from LuaEdit
  452. frmLuaEditMain.DoExitExecute;
  453. // Return in Delphi the number of argument pushed on the stack
  454. Result := 0;
  455. end;
  456. ////////////////////////////////////////////////////////////////////////////////
  457. // Win32 related functions
  458. ////////////////////////////////////////////////////////////////////////////////
  459. ////////////////////////////////////////////////////////////////////////////////
  460. // Desc: This reads the registry given its key and value(s)
  461. // In: (1) The root key to open
  462. // (2) The registry key to open
  463. // (3) The value #1 to read from the specified key
  464. // (4) The value #2 to read from the specified key
  465. // (...) The value #X to read from the specified key
  466. // Out: (1) The registry value's value
  467. //
  468. // 30/09/2006 - Jean-Francois Goulet
  469. ////////////////////////////////////////////////////////////////////////////////
  470. function LuaLERegRead(L: PLua_State): Integer; cdecl;
  471. var
  472. pReg: TRegistry;
  473. x: Integer;
  474. RootKeyTemp, Key, ValueName: String;
  475. RootKey: HKEY;
  476. begin
  477. Result := 0;
  478. // Retrieve SAFELY the first parameter
  479. if lua_type(L, 1) = LUA_TSTRING then
  480. begin
  481. RootKeyTemp := UpperCase(lua_tostring(L, 1));
  482. // Determine the root key
  483. if RootKeyTemp = 'HKLM' then
  484. RootKey := HKEY_LOCAL_MACHINE
  485. else if RootKeyTemp = 'HKCR' then
  486. RootKey := HKEY_CLASSES_ROOT
  487. else if RootKeyTemp = 'HKCU' then
  488. RootKey := HKEY_CURRENT_USER
  489. else if RootKeyTemp = 'HKU' then
  490. RootKey := HKEY_USERS
  491. else if RootKeyTemp = 'HKCC' then
  492. RootKey := HKEY_CURRENT_CONFIG;
  493. // Retrieve SAFELY the second parameter
  494. if lua_type(L, 2) = LUA_TSTRING then
  495. begin
  496. Key := lua_tostring(L, 2);
  497. pReg := TRegistry.Create;
  498. pReg.RootKey := RootKey;
  499. if pReg.OpenKey(Key, False) then
  500. begin
  501. for x := 3 to lua_gettop(L) do
  502. begin
  503. // Increment number of argument pushed on the stack
  504. Inc(Result);
  505. if lua_type(L, x) = LUA_TSTRING then
  506. begin
  507. ValueName := lua_tostring(L, x);
  508. if pReg.ValueExists(ValueName) then
  509. begin
  510. case pReg.GetDataType(ValueName) of
  511. rdString, rdExpandString: lua_pushstring(L, PChar(pReg.ReadString(ValueName)));
  512. rdInteger: lua_pushnumber(L, pReg.ReadFloat(ValueName));
  513. else
  514. lua_pushnil(L);
  515. end;
  516. end
  517. else
  518. lua_pushnil(L);
  519. end
  520. else
  521. lua_pushnil(L);
  522. end;
  523. end;
  524. pReg.Free;
  525. end;
  526. end;
  527. end;
  528. ////////////////////////////////////////////////////////////////////////////////
  529. // Desc: This write the registry given its key and value
  530. // In: (1) The root key to open
  531. // (2) The registry key to open
  532. // (3) The value name to write
  533. // (4) The value's value
  534. // Out: (1) Return true if successful. Otherwise return nil.
  535. //
  536. // 30/09/2006 - Jean-Francois Goulet
  537. ////////////////////////////////////////////////////////////////////////////////
  538. function LuaLERegWrite(L: PLua_State): Integer; cdecl;
  539. var
  540. pReg: TRegistry;
  541. x: Integer;
  542. RootKeyTemp, Key, ValueName: String;
  543. RootKey: HKEY;
  544. begin
  545. // Retrieve SAFELY the first parameter
  546. if lua_type(L, 1) = LUA_TSTRING then
  547. begin
  548. RootKeyTemp := UpperCase(lua_tostring(L, 1));
  549. // Determine the root key
  550. if RootKeyTemp = 'HKLM' then
  551. RootKey := HKEY_LOCAL_MACHINE
  552. else if RootKeyTemp = 'HKCR' then
  553. RootKey := HKEY_CLASSES_ROOT
  554. else if RootKeyTemp = 'HKCU' then
  555. RootKey := HKEY_CURRENT_USER
  556. else if RootKeyTemp = 'HKU' then
  557. RootKey := HKEY_USERS
  558. else if RootKeyTemp = 'HKCC' then
  559. RootKey := HKEY_CURRENT_CONFIG;
  560. // Retrieve SAFELY the second parameter
  561. if lua_type(L, 2) = LUA_TSTRING then
  562. begin
  563. Key := lua_tostring(L, 2);
  564. pReg := TRegistry.Create;
  565. pReg.RootKey := RootKey;
  566. pReg.OpenKey(Key, True);
  567. if lua_gettop(L) >= 4 then
  568. begin
  569. if lua_type(L, 4) = LUA_TSTRING then
  570. begin
  571. ValueName := lua_tostring(L, 4);
  572. case lua_type(L, 5) of
  573. LUA_TSTRING: pReg.WriteString(ValueName, lua_tostring(L, 5));
  574. LUA_TNUMBER: pReg.WriteFloat(ValueName, lua_tonumber(L, 5));
  575. end;
  576. end;
  577. end;
  578. pReg.Free;
  579. end;
  580. end;
  581. // Return in Delphi the number of argument pushed on the stack
  582. Result := 1;
  583. end;
  584. ////////////////////////////////////////////////////////////////////////////////
  585. // Lua state registration related functions
  586. ////////////////////////////////////////////////////////////////////////////////
  587. // This function register in the given lua state all luaedit related function
  588. // to allow to the user some interface customization. Kind of like macros but
  589. // using lua.
  590. procedure LERegisterToLua(L: Plua_State; NeedLuaLibs: Boolean);
  591. const
  592. LETableName = 'luaedit';
  593. LuaEditFuncPackage: array [0..14] of LuaL_Reg = (// LuaEdit's core system functions
  594. (name: 'getver'; func: LuaLEGetVersion),
  595. (name: 'getproductname'; func: LuaLEGetProductName),
  596. (name: 'exit'; func: LuaLEExit),
  597. // LuaEdit's interfaces functions
  598. (name: 'print'; func: LuaLEPrint),
  599. // LuaEdit's file manipulation functions
  600. (name: 'openfile'; func: LuaLEOpenFile),
  601. (name: 'openprj'; func: LuaLEOpenProject),
  602. (name: 'saveall'; func: LuaLESaveAll),
  603. (name: 'saveprjas'; func: LuaLESavePrjAs),
  604. (name: 'saveunit'; func: LuaLESaveUnit),
  605. (name: 'saveunitas'; func: LuaLESaveUnitAs),
  606. (name: 'getfile'; func: LuaLEGetFile),
  607. (name: 'getactiveprj'; func: LuaLEGetActivePrj),
  608. // Win32 system functions
  609. (name: 'regread'; func: LuaLERegRead),
  610. (name: 'regwrite'; func: LuaLERegWrite),
  611. (name: nil; func: nil)
  612. );
  613. procedure LuaRegisterVarStrInPackage(L: PLua_State; const Package: PChar; const VarName: PChar; const VarValue: PChar);
  614. begin
  615. // Get package table from globals
  616. lua_getglobal(L, Package);
  617. // Pushes variable name on the stack
  618. lua_pushstring(L, VarName);
  619. lua_pushstring(L, VarValue);
  620. lua_settable(L, -3);
  621. lua_pop(L, 1);
  622. end;
  623. begin
  624. // Open basic lua libraries (if required)
  625. if NeedLuaLibs then
  626. begin
  627. lua_baselibopen(L);
  628. lua_packlibopen(L);
  629. lua_tablibopen(L);
  630. lua_strlibopen(L);
  631. lua_iolibopen(L);
  632. lua_mathlibopen(L);
  633. lua_dblibopen(L);
  634. end;
  635. // Register functions
  636. luaL_Register(L, LETableName, @LuaEditFuncPackage);
  637. // Register variables
  638. LuaRegisterVarStrInPackage(L, LETableName, '_VERSION', PChar(_LuaEditVersion));
  639. end;
  640. end.