- Offizieller Beitrag
Es können 2 Arten von Bibliotheken verwendet werden: Dll- und Lua-Bibliotheken.
Der Aufruf im Skript erfolgt mit:
Die Funktionen lassen sich dann aufrufen mit:
• Registrieren von Bibliotheken
Damit die Bibliotheken von "require" gefunden werden, müssen die Speicherpfade in der "SciTEStartup.lua" registriert werden.
Dll-Dateien werden registriert in "package.cpath", Lua-Skripts in "package.path".
Damit die eigenen Skripte und Bibliotheken auch nach einem Update von SciTE noch vorhanden sind, darf man diese keinesfalls im Ordner "..\SciTE\LUA" speichern. Dieser wird bei einem Update ungefragt überschrieben. Es empfiehlt sich einen eigenen Ordner dafür anzulegen und diesen Pfad in den "SciTEUser.properties" zu speichern.
Ich habe dort z.B. eingetragen: "Lua.User.Scripts.Path=C:\Code_AutoIt\LUA". Wichtig: Ohne abschließenden Backslash! Nun kann man diesen Pfad überall innerhalb SciTE abfragen mit: "props['Lua.User.Scripts.Path']".
Es können leider nicht alle Dll auch in SciTE genutzt werden. progandy hatte das schon näher beschrieben.
Hier mal als Bsp., wie man sowohl Dll- als auch Lua-Dateien im eigenen Skriptpfad registriert. Die Art der Registrierung ermöglicht, dass die Dateien direkt im benannten Pfad gespeichert sind oder in einem Unterordner, der denselben Namen wie die Datei trägt.
local sUserLua = props["Lua.User.Scripts.Path"]
LUA_USER_PATH = sUserLua .. "\\?.dll;" .. sUserLua .. "\\?\\?.dll;"
package.cpath = LUA_USER_PATH .. package.cpath
LUA_USER_LUA = sUserLua .. "\\?.lua;" .. sUserLua .. "\\?\\?.lua;"
package.path = LUA_USER_LUA .. package.path
• Eigene Bibliotheken erstellen
Das Erstellen eigener Bibliotheken (Lua-Skript) ist eine einfache Sache. Wobei sich die Möglichkeit bietet innerhalb der Bibliothek eigene Funktionsnamen zu verwenden und für den Anwender andere Namen zur Verfügung zu stellen.
Muster (gleiche Namen)
Muster (mit Namen-Routing)
Man kann natürlich, wie bei den Includes in AutoIt, auch Bibliotheken in Bibliotheken einbinden.
Bsp. meine WinAPI
do
----------------------------------------------------------------------------------------------------
local alien = require "alien"
------------------------------------------------------------------------- STRUCTURES ---------------
local POINT = alien.defstruct{{ 'X', 'int'}, { 'Y', 'int'}}
local RECT = alien.defstruct{{'left', 'long'}, {'top', 'long'}, {'right', 'long'}, {'bottom', 'long'}}
----------------------------------------------------------------------------------------------------
------------------------------------------------------------------------- CONSTANTS ----------------
local __MAXPATH = 260
-- DesiredAccess
local __GENERIC_ALL = 0x10000000
local __GENERIC_READ = 0x80000000
local __GENERIC_WRITE = 0x40000000
local __GENERIC_EXECUTE = 0x20000000
-- CreationDisposition
local __CREATE_NEW = 1
local __CREATE_ALWAYS = 2
local __OPEN_ALWAYS = 4
local __OPEN_EXISTING = 3
local __TRUNCATE_EXISTING = 5
-- FlagsAndAttributes
local __FILE_ATTRIBUTE_ARCHIVE = 0x20
local __FILE_ATTRIBUTE_ENCRYPTED = 0x4000
local __FILE_ATTRIBUTE_HIDDEN = 0x2
local __FILE_ATTRIBUTE_NORMAL = 0x80
local __FILE_ATTRIBUTE_OFFLINE = 0x1000
local __FILE_ATTRIBUTE_READONLY = 0x1
local __FILE_ATTRIBUTE_SYSTEM = 0x4
local __FILE_ATTRIBUTE_TEMPORARY = 0x100
local __FILE_FLAG_BACKUP_SEMANTICS = 0x02000000
local __FILE_FLAG_DELETE_ON_CLOSE = 0x04000000
local __FILE_FLAG_NO_BUFFERING = 0x20000000
local __FILE_FLAG_OPEN_NO_RECALL = 0x00100000
local __FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000
local __FILE_FLAG_OVERLAPPED = 0x40000000
local __FILE_FLAG_POSIX_SEMANTICS = 0x0100000
local __FILE_FLAG_RANDOM_ACCESS = 0x10000000
local __FILE_FLAG_SESSION_AWARE = 0x00800000
local __FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000
local __FILE_FLAG_WRITE_THROUGH = 0x80000000
-- ShareMode
local __0 = 0x00000000
local __FILE_SHARE_DELETE = 0x00000004
local __FILE_SHARE_READ = 0x00000001
local __FILE_SHARE_WRITE = 0x00000002
----------------------------------------------------------------------------------------------------
------------------------------------------------------------------------- FUNCTIONS ----------------
WinAPI = {}
--[=[
GetLastError
RECTANGLE:
CopyRect
CreateRect
EqualRect
InflateRect
IntersectRect
IsRectEmpty
OffsetRect
PtInRect
SetRect
SetRectEmpty
SetRectLogic
SubtractRect
UnionRect
CURSOR:
GetCursorPos
DIRECTORY
CreateDirectory
GetCurrentDirectory
RemoveDirectory
SetCurrentDirectory
--]=]
----------------------------------------------------------------------------------------------------
--[[ Gets the last Windows System Error ]]
----------------------------------------------------------------------------------------------------
function WinAPI.GetLastError()
local GetLastError = assert(alien.kernel32.GetLastError)
GetLastError:types{ret = 'long', abi = 'stdcall'}
return GetLastError()
end
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
--[[ The CopyRect function copies the coordinates of one rectangle to another.
in...: pointer to the rectangle struct, that should copied
out..: rectangle struct with coordinates from source
]]
----------------------------------------------------------------------------------------------------
function WinAPI.CopyRect(_ptRect)
local rectCopy = RECT:new()
local CopyRect = assert(alien.user32.CopyRect)
CopyRect:types{ret = 'int', abi = 'stdcall', 'pointer', 'pointer'}
CopyRect(rectCopy(), _ptRect)
return rectCopy
end
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
--[[ Creates an empty Rectangle structure or fill them with values when they are given.
in...: (optional) coordinates of the rectangle
out..: rectangle struct, empty or with given coordinates
]]
----------------------------------------------------------------------------------------------------
function WinAPI.CreateRect(_iLeft, _iTop, _iRight, _iBottom)
local rect = RECT:new()
if _iLeft == nil then WinAPI.SetRectEmpty(rect())
else WinAPI.SetRect(rect(), _iLeft, _iTop, _iRight, _iBottom) end
return rect
end
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
--[[ The EqualRect function determines whether the two specified rectangles are equal by comparing
the coordinates of their upper-left and lower-right corners.
in...: pointer to the rectangle structs
out..: non zero if rectangles are identical, otherwise zero
]]
----------------------------------------------------------------------------------------------------
function WinAPI.EqualRect(_ptRect1, _ptRect2)
local EqualRect = assert(alien.user32.EqualRect)
EqualRect:types{ret = 'int', abi = 'stdcall', 'pointer', 'pointer'}
return EqualRect(_ptRect1, _ptRect2)
end
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
--[[ The InflateRect function increases or decreases the width and height of the specified rectangle.
The InflateRect function adds dx units to the left and right ends of the rectangle and dy units to
the top and bottom. The dx and dy parameters are signed values; positive values increase the width
and height, and negative values decrease them.
in...: pointer to the rectangle struct, x- and y- value for in-/ decreasing
out..: non zero if success, otherwise zero
]]
----------------------------------------------------------------------------------------------------
function WinAPI.InflateRect(_ptRect, _idX, _idY)
local InflateRect = assert(alien.user32.InflateRect)
InflateRect:types{ret = 'long', abi = 'stdcall', 'pointer', 'long', 'long'}
if _idY == nil then _idY = 0 end
return InflateRect(_ptRect, _idX, _idY)
end
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
--[[ The IntersectRect function calculates the intersection of two source rectangles and places the
coordinates of the intersection rectangle into the destination rectangle. If the source rectangles
do not intersect, an empty rectangle (in which all coordinates are set to zero) is placed into the
destination rectangle.
in...: pointer to the rectangle structs
out..: non zero if intersect, otherwise zero; 2nd: destination rectangle struct
]]
----------------------------------------------------------------------------------------------------
function WinAPI.IntersectRect(_ptRect1, _ptRect2)
local rInter = RECT:new()
local IntersectRect = assert(alien.user32.IntersectRect)
IntersectRect:types{ret = 'int', abi = 'stdcall', 'pointer', 'pointer', 'pointer'}
return IntersectRect(rInter(), _ptRect1, _ptRect2), rInter
end
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
--[[ The IsRectEmpty function determines whether the specified rectangle is empty. An empty rectangle
is one that has no area; that is, the coordinate of the right side is less than or equal to the
coordinate of the left side, or the coordinate of the bottom side is less than or equal to the
coordinate of the top side.
in...: pointer to the rectangle struct
out..: non zero if empty, otherwise zero
]]
----------------------------------------------------------------------------------------------------
function WinAPI.IsRectEmpty(_ptRect)
local IsRectEmpty = assert(alien.user32.IsRectEmpty)
IsRectEmpty:types{ret = 'int', abi = 'stdcall', 'pointer'}
return IsRectEmpty(_ptRect)
end
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
--[[ The OffsetRect function moves the specified rectangle by the specified offsets.
in...: pointer to the rectangle struct, offset values for x and y
out..: non zero if success, otherwise zero
]]
----------------------------------------------------------------------------------------------------
function WinAPI.OffsetRect(_ptRect, _iX, _iY)
local OffsetRect = assert(alien.user32.OffsetRect)
OffsetRect:types{ret = 'long', abi = 'stdcall', 'pointer', 'long', 'long'}
if _iY == nil then _iY = 0 end
return OffsetRect(_ptRect, _iX, _iY)
end
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
--[[ The PtInRect function determines whether the specified point lies within the specified rectangle.
A point is within a rectangle if it lies on the left or top side or is within all four sides.
A point on the right or bottom side is considered outside the rectangle.
in...: x and y coordinates from point, pointer to the rectangle struct
out..: non zero if point inside the rectangle, otherwise zero
]]
----------------------------------------------------------------------------------------------------
function WinAPI.PtInRect(_iX, _iY, _ptRect)
local PtInRect = assert(alien.user32.PtInRect)
PtInRect:types{ret = 'int', abi = 'stdcall', 'pointer', 'pointer'}
local point = POINT:new()
point.X = _iX
point.Y = _iY
return PtInRect(point, _ptRect)
end
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
--[[ The SetRect function sets the coordinates of the specified rectangle. This is equivalent to
assigning the left, top, right, and bottom arguments to the appropriate members of the RECT structure.
in...: pointer to the rectangle struct, values to set (left, top, right, bottom)
out..: non zero if success, otherwise zero
]]
----------------------------------------------------------------------------------------------------
function WinAPI.SetRect(_ptRect, _iLeft, _iTop, _iRight, _iBottom)
local SetRect = assert(alien.user32.SetRect)
SetRect:types{ret = 'long', abi = 'stdcall', 'pointer', 'long', 'long', 'long', 'long'}
return SetRect(_ptRect, _iLeft, _iTop, _iRight, _iBottom)
end
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
--[[ The SetRectEmpty function sets all coordinates are of the given rectangle to zero.
in...: pointer to the rectangle struct
out..: non zero if success, otherwise zero
]]
----------------------------------------------------------------------------------------------------
function WinAPI.SetRectEmpty(_ptRect)
local SetRectEmpty = assert(alien.user32.SetRectEmpty)
SetRectEmpty:types{ret = 'long', abi = 'stdcall', 'pointer'}
return SetRectEmpty(_ptRect)
end
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
--[[ Swaps the values, if they were placed logically false.
in...: rectangle struct
out..: rectangle struct with swapped values, if required
]]
----------------------------------------------------------------------------------------------------
function WinAPI.SetRectLogic(_Rect)
if _Rect.left > _Rect.right then _Rect.left, _Rect.right = _Rect.right, _Rect.left end
if _Rect.top > _Rect.bottom then _Rect.top, _Rect.bottom = _Rect.bottom, _Rect.top end
return _Rect
end
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
--[[ The SubtractRect function determines the coordinates of a rectangle formed by subtracting
one rectangle from another.
in...: pointer to the rectangle structs
out..: non zero if destination rectangle has values, otherwise zero; 2nd: destination rectangle struct
]]
----------------------------------------------------------------------------------------------------
function WinAPI.SubtractRect(_ptRect1, _ptRect2)
local SubtractRect = assert(alien.user32.SubtractRect)
SubtractRect:types{ret = 'int', abi = 'stdcall', 'pointer', 'pointer', 'pointer'}
local rectSub = RECT:new()
return SubtractRect(rectSub(), _ptRect1, _ptRect2), rectSub
end
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
--[[ The UnionRect function creates the union of two rectangles. The union is the smallest rectangle
that contains both source rectangles.
in...: pointer to the rectangle structs
out..: non zero if destination rectangle has values, otherwise zero; 2nd: destination rectangle struct
]]
----------------------------------------------------------------------------------------------------
function WinAPI.UnionRect(_ptRect1, _ptRect2)
local UnionRect = assert(alien.user32.UnionRect)
UnionRect:types{ret = 'int', abi = 'stdcall', 'pointer', 'pointer', 'pointer'}
local rectUnion = RECT:new()
return UnionRect(rectUnion(), _ptRect1, _ptRect2), rectUnion
end
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
--[[ Retrieves the position of the mouse cursor, in screen coordinates.
in...: ---
out..: point structure of position; 2nd: x-coordinate; 3rd: y-coordinate
]]
----------------------------------------------------------------------------------------------------
function WinAPI.GetCursorPos()
local CursorPos = assert(alien.user32.GetCursorPos)
CursorPos:types{ret = 'int', abi = 'stdcall', "pointer"}
local pos = POINT:new()
CursorPos(pos())
return pos, pos.X, pos.Y
end
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
--[[ Creates a new directory.
in...: full path to the directory, limited length 260 -12 to allow append an 8.3 file name
out..: non zero if success, otherwise zero
]]
----------------------------------------------------------------------------------------------------
function WinAPI.CreateDirectory(_sPath)
if _sPath:len() > __MAXPATH -12 then return nil end
local CreateDirectory = assert(alien.kernel32.CreateDirectoryA)
CreateDirectory:types{ret = 'int', abi = 'stdcall', 'string', 'pointer'}
return CreateDirectory(_sPath..string.char(0), 0)
end
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
--[[ Retrieves the current directory for the current process.
in...: ---
out..: directory full path; if path length > 260 returns nil
]]
----------------------------------------------------------------------------------------------------
function WinAPI.GetCurrentDirectory()
local GetCurrentDirectory = assert(alien.kernel32.GetCurrentDirectoryA)
GetCurrentDirectory:types{ret = 'long', abi = 'stdcall', 'long', 'string'}
local buff = alien.buffer()
local iRet = GetCurrentDirectory(__MAXPATH, buff)
if iRet > __MAXPATH then return nil end
return tostring(buff)
end
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
--[[ Deletes an existing empty directory.
in...: full path to the directory want to delete
out..: non zero if success, otherwise zero; if path length > 260 returns nil
]]
----------------------------------------------------------------------------------------------------
function WinAPI.RemoveDirectory(_sPath)
if _sPath:len() > __MAXPATH then return nil end
local RemoveDirectory = assert(alien.kernel32.RemoveDirectoryA)
RemoveDirectory:types{ret = 'int', abi = 'stdcall', 'string'}
return RemoveDirectory(_sPath)
end
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
--[[ Changes the current directory for the current process.
in...: full path to the new directory
out..: non zero if success, otherwise zero; if path length > 260 -2 returns nil (path:len + '\' + char(0))
]]
----------------------------------------------------------------------------------------------------
function WinAPI.SetCurrentDirectory(_sPath)
if _sPath:len() > __MAXPATH -2 then return nil end
local SetCurrentDirectory = assert(alien.kernel32.SetCurrentDirectoryA)
SetCurrentDirectory:types{ret = 'int', abi = 'stdcall', 'string'}
return SetCurrentDirectory(_sPath..string.char(0))
end
----------------------------------------------------------------------------------------------------
return WinAPI
end
Alles anzeigen
NB: Da ich gerade erst die aktuelle Version von SciTE installiert habe, ist mir folgendes aufgefallen:
Um Lua Skripte in SciTE auszuführen war es bei der vorigen Version ausreichend, in der SciTEUser.properties einzutragen:
command.go.$(file.patterns.lua)=dofile $(FilePath).
In der aktuellen Version muss zusätzlich auch das Subsystem angegeben werden:
command.go.subsystem.$(file.patterns.lua)=3