-
Notifications
You must be signed in to change notification settings - Fork 5
Extra Functionality
Note: The documentation has moved to the LuaPreprocess website. Information here may be out of date!
In addition to the exclamation mark (!) there is another symbol with special meaning in LuaPreprocess: the at sign (@). It's used as prefix for a few special preprocessor keywords that do different things.
[v1.11] Inserts the path to the current file as a string literal.
print("Current file: "..@file)
This keyword has two variants.
[v1.10]
This variant inserts a resource as-is in-place before the metaprogram runs (the resource being a Lua code string from somewhere).
By default, name
is a path to a file to be inserted but this behavior can be changed by defining params.onInsert()
when calling processFile() or processString()
(or by handling the "insert" message in the message handler in the command line program).
-- script.lua
local one = 1
@insert "partialScript.lua"
print(one + two)
-- partialScript.lua
local two = !( 1+1 )
The keyword can also appear in the metaprogram anywhere.
-- script.lua
!(
@insert "yellFunction.lua"
yell("aaargh")
)
local versionText = !( "Version: " .. @insert "appVersion.txt" )
print(versionText) -- Version: 1.2.3
-- yellFunction.lua
local function yell(text)
print(text:upper().."!!!")
end
-- appVersion.txt
"1.2.3"
[v1.13]
@insert
can also be written more compactly as @@
:
@@"yellFunction.lua"
[v1.13]
This variant outputs the result returned from a function func()
defined in the metaprogram (like !!(func())
)
except all arguments are converted to individual strings containing the apparent Lua code before the call.
(This is similar to how macros work in C/C++.)
-- Define a better assert function.
!(
local DEBUG = true
local function ASSERT(conditionCode, messageCode)
if not DEBUG then
-- Make ASSERT() calls do nothing if we're not in debug mode.
return ""
end
if not messageCode then
messageCode = string.format("%q", "Assertion failed: "..conditionCode)
end
return "if not ("..conditionCode..") then error("..messageCode..") end"
end
)
local text = "Herzlich willkommen!"
@insert ASSERT(#text < 15, "Text is too long: "..text)
-- Output:
local text = "Herzlich willkommen!"
if not (#text < 15) then error("Text is too long: "..text) end
@insert
can also be written more compactly as @@
:
@@ASSERT(#text < 15, "Text is too long: "..text)
[v1.11] Inserts the current line number as an integer numeral.
print("Current line: "..@line)
The backtickStrings parameter (or --backtickstrings option) enables the backtick (`) to be used as string literal delimiters. Backtick strings don't interpret any escape sequences and can't contain other backticks.
This feature can be nice for strings that contain Lua code and you want your text editor to apply normal syntax highlighting to the string contents. Example:
!local DOUBLE_X = `
x = x*2
`
local x = 1
!!(DOUBLE_X)
!!(DOUBLE_X)
print(x) -- 4