Starting with Lua
iNMR embeds Lua 5.1.
Detailed documentation about Lua can be found on the official website.
Lua is a compact and elegant language, but you do not need to learn much of it to automate everyday tasks in iNMR.
As a rule, call iNMR functions with parentheses, even when they take no arguments. For example, to show the whole spectrum, write:
full()
You can add a comment, preceded by two dashes:
full() -- what you write after the dashes is not interpreted by the program
When there is a single argument and it is already delimited, like a literal string or a table, the parentheses are optional. Ex:
print ("hello world!")
print "hello world!" -- identical
color { r=0.5, g=0, b=0 } -- changes the color to dark red
The color() function, like many others, changes an internal parameter but does not immediately redraw the spectrum. You can draw programmatically with the function amp() (no arguments) or, interactively, pressing the “Esc” key. You can separate assignments with a comma, as shown above, or a semicolon. Now let's write a complete program, including input and output...
-- prints the square of the given number
x = ask "enter a number" -- waits for user input
print("the square of ", x, "is ", x * x) -- compulsory parentheses
To make the program a little more professional, we can declare x as a local variable, so it ceases occupying memory after execution, and we can put the calculation inside a function:
-- a more professional version
function square( num ) -- starts the definition of a new function
return num * num
end -- closes the definition
local x = ask("enter a number") -- x is declared as a local variable
print("the square of ", x, "is ", square( x )) -- calls the function
You can define your own functions for daily tasks. A perfect example is integration, which would otherwise require a command like:
region( 4, 3 ) press "i" -- creates an integral region between 4 and 3 ppm
You can declare:
function integral( from, to )
region( from, to )
press "i"
end
function zoom( from, to ) -- another function
region( from, to )
press "z"
end
To keep your functions permanently available, save them in a file.
You should keep all your Lua scripts in a folder and its subfolders.
Then tell iNMR where the folder is (you can do it with the Preferences dialog).
iNMR requires that all filenames end with “.lua” If you follow this rule,
iNMR will list the rest of the names in the Scripts menu (a special, top-level, menu).
If you create subfolders, iNMR will also create submenus. When you select a file from the Scripts menu,
iNMR will execute the Lua code contained therein.
You'll want to create a file called “Lua.init”.
Unlike normal scripts, it is loaded automatically whenever the console is opened.
Lua.init is the ideal place to store your “integral” function defined above and similar initializations.
The following example uses the function to create 10 integral regions, going from 0 to 1 ppm, from 1 to 2, etc.
for i = 1,10 do
integral( i, i-1 )
end
It is always a good idea to learn from examples. The iNMR website hosts a library of scripts and functions, all tested and commented.
Related Topics
Official Lua 5.1 reference manual
The Console: Command Line and Script Editor
Setting Up iNMR to Run the Scripts