PageRenderTime 21ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/source/toolbar/Extensions.ahk

http://github.com/fincs/SciTE4AutoHotkey
AutoHotKey | 409 lines | 352 code | 54 blank | 3 comment | 40 complexity | 80d74733a2d8b2eedde5e2fec1987447 MD5 | raw file
  1. goto _skip_ext_ahk
  2. extmon:
  3. if extMonShown
  4. {
  5. Gui ExtMon:Show
  6. return
  7. }
  8. extMonFirstLabel := false
  9. Gui ExtMon:+Owner%hwndgui% LabelExtMon Resize MinSize
  10. Gui ExtMon:Default
  11. Gui, Menu, ExtMonMenu
  12. Gui, Margin, 0, 0
  13. Gui, Add, ListView, w480 h320 Checked vExtMonLV hwndExtMonLVHWND -LV0x10, Name|Version|Author|Internal name
  14. Util_ExtMonLoadExt()
  15. Gui, Show,, Extension Manager
  16. extMonShown := true
  17. extMonRemoveList := {}
  18. return
  19. ExtMonClose:
  20. Gui +OwnDialogs
  21. MsgBox, 35, Extension Manager, Do you want to save changes? This requires a restart.
  22. IfMsgBox, Cancel
  23. return
  24. IfMsgBox, Yes
  25. {
  26. for extName,_ in extMonRemoveList
  27. FileRemoveDir, %ExtensionDir%\%extName%, 1
  28. Util_ExtMonSaveExt()
  29. }
  30. Gui, Destroy
  31. extMonShown := false
  32. IfMsgBox, Yes
  33. gosub reloadextsForce
  34. return
  35. ExtMonSize:
  36. GuiControl, Move, ExtMonLV, w%A_GuiWidth% h%A_GuiHeight%
  37. return
  38. ExtMonRemoveExt:
  39. Gui +OwnDialogs
  40. selRows := LV_GetCount("S")
  41. if !selRows
  42. return
  43. plural := selRows > 1 ? "s" : ""
  44. MsgBox, 52, Extension Manager, Are you sure you want to remove the selected extension%plural%?
  45. IfMsgBox, No
  46. return
  47. Loop %selRows%
  48. {
  49. x := LV_GetNext()
  50. LV_GetText(xt, x, 4)
  51. extMonRemoveList[xt] := true
  52. LV_Delete(x)
  53. }
  54. return
  55. ExtMonExportExt:
  56. Gui +OwnDialogs
  57. if LV_GetCount("S") != 1
  58. return
  59. extToExport := LV_GetNext()
  60. LV_GetText(extIntlName, extToExport, 4)
  61. FileSelectFile, expFile, S16, %extIntlName%.s4x, Export Extension, SciTE4AutoHotkey Extension (*.s4x)
  62. if ErrorLevel
  63. return
  64. if !RegExMatch(expFile, "\.[^\\/]+$")
  65. expFile .= ".exe"
  66. if ExportExtension(extIntlName, expFile)
  67. MsgBox, 64, SciTE4AutoHotkey, Extension exported successfully!
  68. else
  69. MsgBox, 16, SciTE4AutoHotkey, Failed to export the extension!
  70. return
  71. ExtMonCreateExt:
  72. Gui +Disabled
  73. Gui ExtCreate:+OwnerExtMon LabelExtCreate
  74. Gui ExtCreate:Default
  75. ;Gui, Add, Text, w320, Not! Implemented! Sorry!
  76. Gui, Add, Text, Section Right w90, Internal name:
  77. Gui, Add, Edit, ys w320 vExtCreate_IntlName, com.yourname.extname
  78. Gui, Add, Text, xs Section Right w90, Name:
  79. Gui, Add, Edit, ys w320 vExtCreate_Name, Extension name
  80. Gui, Add, Text, xs Section Right w90, Version:
  81. Gui, Add, Edit, ys w320 vExtCreate_Version, 1.0
  82. Gui, Add, Text, xs Section Right w90, Author:
  83. Gui, Add, Edit, ys w320 vExtCreate_Author, Your name
  84. Gui, Add, Text, xs Section Right w90, Behaviour:
  85. Gui, Add, CheckBox, ys vExtCreate_HasProps Checked, Has SciTE properties
  86. Gui, Add, CheckBox, vExtCreate_HasToolbar, Has Toolbar buttons
  87. Gui, Add, CheckBox, vExtCreate_HasLua, Has custom Lua script
  88. Gui, Add, Button, gExtCreateCreate w90, Create
  89. Gui, Show,, New Extension
  90. return
  91. ExtCreateClose:
  92. Gui Destroy
  93. Gui ExtMon:-Disabled
  94. Gui ExtMon:Show
  95. return
  96. ExtCreateCreate:
  97. Gui +OwnDialogs
  98. Gui, Submit, NoHide
  99. if RegExMatch(ExtCreate_IntlName, "[\s\\/:\*?""<>\|]")
  100. {
  101. MsgBox, 48, New Extension, The internal name should not contain whitespace or any of these characters:`n\ / : * ? `" < > |
  102. return
  103. }
  104. if !ExtCreate_Name || !ExtCreate_Version || !ExtCreate_Author
  105. {
  106. MsgBox, 48, New Extension, The extension name, version and author should be filled in.
  107. return
  108. }
  109. tmpExtDir := ExtensionDir "\" ExtCreate_IntlName
  110. IfExist, %tmpExtDir%\
  111. {
  112. MsgBox, 48, New Extension, The specified internal name is already in use.
  113. return
  114. }
  115. gosub ExtCreateClose
  116. Gui ExtMon:Default
  117. Gui +OwnDialogs
  118. FileCreateDir, %tmpExtDir%
  119. manifest := "[Extension]`nName=" ExtCreate_Name "`nVersion=" ExtCreate_Version "`nAuthor=" ExtCreate_Author "`n`n[Behaviour]`n"
  120. if ExtCreate_HasProps
  121. {
  122. manifest .= "Properties=extension`n"
  123. FileAppend,
  124. (LTrim
  125. # Write properties specific to this extension here
  126. ), %tmpExtDir%\extension.properties
  127. }
  128. if ExtCreate_HasToolbar
  129. {
  130. manifest .= "Toolbar=toolbar.properties`n"
  131. FileAppend,
  132. (LTrim
  133. ; Toolbar button definitions specific to this extension
  134. ), %tmpExtDir%\toolbar.properties
  135. }
  136. if ExtCreate_HasLua
  137. {
  138. manifest .= "LuaScript=extension.lua`n"
  139. FileEncoding, CP0
  140. FileAppend,
  141. (LTrim
  142. -- Lua script specific to this extension
  143. -- Declare event handler table
  144. local events = {}
  145. -- Event handler example
  146. --function events.OnChar(ch)
  147. --`tprint("OnChar: "..ch.."\n")
  148. --`t-- If you want to mark this event as being already handled thus stopping
  149. --`t-- propagation through all extensions and scripts, return true.
  150. --end
  151. -- Register events
  152. RegisterEvents(events)
  153. ), %tmpExtDir%\extension.lua
  154. FileEncoding, UTF-8
  155. }
  156. FileAppend, % manifest, %tmpExtDir%\manifest.ini
  157. LV_Add("Check", ExtCreate_Name, ExtCreate_Version, ExtCreate_Author, ExtCreate_IntlName)
  158. return
  159. ExtMonInstallExt:
  160. Gui +OwnDialogs
  161. FileSelectFile, extFile, 1,, Install Extension, SciTE4AutoHotkey Extension (*.s4x)
  162. if ErrorLevel
  163. return
  164. Loop
  165. tempName := "~temp" A_TickCount ".tmp", tempDir := ExtensionDir "\" tempName
  166. until !FileExist(tempDir)
  167. rc := ExtractExtension(tempDir, extFile, extIntlName)
  168. if rc != OK
  169. {
  170. FileRemoveDir, %tempDir%, 1
  171. MsgBox, 16, SciTE4AutoHotkey, Error while extracting extension! %rc%.
  172. return
  173. }
  174. if extMonRemoveList[extIntlName]
  175. {
  176. MsgBox, 16, SciTE4AutoHotkey, You must restart SciTE before reinstalling a removed extension.
  177. return
  178. }
  179. extUpgrade := !!FileExist(willDir := ExtensionDir "\" extIntlName)
  180. tempM := Util_GetExtManifest(tempName).Extension
  181. if extUpgrade
  182. {
  183. extPrevVer := Util_GetExtManifest(extIntlName).Extension.Version
  184. if (extPrevVer > tempM.Version)
  185. {
  186. FileRemoveDir, %tempDir%, 1
  187. MsgBox, 16, SciTE4AutoHotkey, It is not possible to downgrade an extension.
  188. return
  189. }
  190. }
  191. msgboxMsg := "Name: " tempM.Name "`nVersion: " tempM.Version
  192. if extUpgrade
  193. msgBoxMsg .= "`nPrevious version: " extPrevVer
  194. msgboxMsg .= "`nAuthor: " tempM.Author "`n`nDo you want to " (extUpgrade ? "upgrade" : "install") " this extension?"
  195. MsgBox, 36, SciTE4AutoHotkey, % msgboxMsg
  196. IfMsgBox, No
  197. {
  198. FileRemoveDir, %tempDir%, 1
  199. return
  200. }
  201. if extUpgrade
  202. {
  203. FileRemoveDir, %willDir%, 1
  204. if ErrorLevel
  205. {
  206. FileRemoveDir, %tempDir%, 1
  207. MsgBox, 16, SciTE4AutoHotkey, Could not remove old version.
  208. return
  209. }
  210. }
  211. FileMoveDir, %tempDir%, %willDir%
  212. if ErrorLevel
  213. {
  214. FileRemoveDir, %tempDir%, 1
  215. MsgBox, 16, SciTE4AutoHotkey, Could not install extension.
  216. return
  217. }
  218. if !extUpgrade
  219. LV_Add("Check", tempM.Name, tempM.Version, tempM.Author, extIntlName)
  220. else
  221. {
  222. Loop, % LV_GetCount()
  223. {
  224. LV_GetText(qq, A_Index, 4)
  225. if (qq = extIntlName)
  226. {
  227. LV_Modify(A_Index, "Check", tempM.Name, tempM.Version, tempM.Author, extIntlName)
  228. break
  229. }
  230. }
  231. }
  232. tempM := ""
  233. return
  234. Util_ExtMonLoadExt()
  235. {
  236. for extName, enabled in Util_GetExtensionList()
  237. {
  238. m := Util_GetExtManifest(extName).Extension
  239. LV_Add(enabled ? "Check" : "", m.Name, m.Version, m.Author, extName)
  240. }
  241. LV_ModifyCol(1, "AutoHdr Sort")
  242. LV_ModifyCol(2, "AutoHdr")
  243. LV_ModifyCol(3, "AutoHdr")
  244. LV_ModifyCol(4, "AutoHdr")
  245. }
  246. Util_ExtMonSaveExt()
  247. {
  248. global ExtMonLVHWND
  249. ini := "[Installed]`n"
  250. Loop, % LV_GetCount()
  251. {
  252. LV_GetText(extName, A_Index, 4)
  253. extEn := Util_LVIsChecked(ExtMonLVHWND, A_Index)
  254. ini .= extName "=" extEn "`n"
  255. }
  256. FileDelete, %ExtensionDir%\extensions.ini
  257. FileAppend, % ini, %ExtensionDir%\extensions.ini
  258. }
  259. Util_LVIsChecked(lvHwnd, rowN)
  260. {
  261. SendMessage, 4140, rowN-1, 0xF000,, ahk_id %lvHwnd%
  262. return (ErrorLevel >> 12) - 1
  263. }
  264. Util_CheckReload()
  265. {
  266. MsgBox, 36, SciTE4AutoHotkey, The selected operation involves a SciTE reload. Continue?
  267. IfMsgBox, No
  268. Exit
  269. }
  270. Util_ReloadSciTE()
  271. {
  272. global SciTEDir, scitehwnd
  273. Run, "%A_AhkPath%" "%SciTEDir%\tools\SciTEReload.ahk" %scitehwnd%
  274. WinClose, ahk_id %scitehwnd%
  275. }
  276. Util_GetExtensionList()
  277. {
  278. return Ini2Object(ExtensionDir "\extensions.ini").Installed
  279. }
  280. Util_GetExtManifest(name)
  281. {
  282. return Ini2Object(ExtensionDir "\" name "\manifest.ini")
  283. }
  284. Util_ReadExtToolbarDef()
  285. {
  286. global LocalSciTEPath
  287. data := ""
  288. for extName, enabled in Util_GetExtensionList()
  289. {
  290. if !enabled
  291. continue
  292. mBeh := Util_GetExtManifest(extName).Behaviour
  293. mPropFile := mBeh.Toolbar
  294. if !mPropFile
  295. continue
  296. FileRead, x, *t %LocalSciTEPath%\Extensions\%extName%\%mPropFile%
  297. StringReplace, x, x, `%EXTDIR`%, %LocalSciTEPath%\Extensions\%extName%, All
  298. data .= x "`n"
  299. }
  300. return data
  301. }
  302. Util_RebuildExtensions()
  303. {
  304. global LocalSciTEPath, scitehwnd
  305. extProps := "# THIS FILE IS SCRIPT-GENERATED - DON'T TOUCH`n"
  306. luaProps := "-- THIS FILE IS SCRIPT-GENERATED - DON'T TOUCH`n"
  307. langMenu := "", dlgFilter := ""
  308. for extName, enabled in Util_GetExtensionList()
  309. {
  310. if !enabled
  311. continue
  312. mBeh := Util_GetExtManifest(extName).Behaviour
  313. mLuaScript := mBeh.LuaScript
  314. mProperties := mBeh.Properties
  315. langMenu .= mBeh.LanguageMenu
  316. dlgFilter .= mBeh.FileFilter
  317. if mLuaScript
  318. {
  319. StringReplace, mLuaScript, mLuaScript, \, /, All
  320. luaProps .= "dofile(props['SciteUserHome']..'/Extensions/" extName "/" mLuaScript "')`n"
  321. }
  322. if mProperties
  323. {
  324. StringReplace, mProperties, mProperties, \, /, All
  325. Loop, Parse, mProperties, |
  326. extProps .= "import Extensions/" extName "/" Trim(A_LoopField) "`n"
  327. }
  328. }
  329. if langMenu
  330. extProps .= "ext.menu.language=" langMenu "`n"
  331. if dlgFilter
  332. extProps .= "filter.ext=" dlgFilter "`n"
  333. t := A_FileEncoding
  334. FileEncoding, CP0
  335. FileDelete, %LocalSciTEPath%\_extensions.lua
  336. FileAppend, % luaProps, %LocalSciTEPath%\_extensions.lua
  337. FileEncoding, %t%
  338. FileDelete, %LocalSciTEPath%\_extensions.properties
  339. FileAppend, % extProps, %LocalSciTEPath%\_extensions.properties
  340. ;SendMessage, 1024+1, 0, 0,, ahk_id %scitehwnd% ; Reload properties
  341. }
  342. Util_VersionTextToNumber(v)
  343. {
  344. r := 0, i := 0
  345. while i < 4 && RegExMatch(v, "O)^(\d+).?", o)
  346. {
  347. StringTrimLeft, v, v, % o.Len
  348. val := o[1] + 0
  349. r |= (val&0xFFFF) << ((3-i)*8)
  350. i ++
  351. }
  352. return r
  353. }
  354. _skip_ext_ahk:
  355. _=_