iNMR icon

  Starting with Lua

The official and accurate documentation about Lua is already available on the web. It is a sophisticated language, with clear definitions, but you don't need to learn it all. For all your basic tasks, and more, you only need to learn a few simple rules. All the commands, even when invoked without arguments, must be followed by a pair of parentheses. 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, doesn't redraw the spectrum. You can draw programmatically with the function amp() (no arguments) or, interactively, pressing the “Esc” key. You can separate assignements with a comma, as shown above, or a semi-colon. 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 function for daily tasks. A fitting example is integration, which is otherwise performed with a line 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 always in existence, save them into a file. You should keep all your lua scripts into a folder. Then tell iNMR where the folder is (you do it inside the Preferences dialog). iNMR requires that all filenames end with “.lua” If you respect this rule, iNMR will list the rest of the name into the Scripts menu (a special, top-level, menu). If you create subfolders, iNMR will also create submenus. When you select a file from the script menu, iNMR will execute the Lua code therein contained.
You'll want to create a file called “Lua.init”. It will not appear into the menu (because it hasn't the proper suffix), but it will be loaded and executed every time you open the console. Lua.init is the ideal place where you can 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 2 to 3, etc..

for i = 1,10 do
integral( i, i-1 )
end

See also

Console Commands

Lua built-in functions on the web

Console

Keyboard: what you can do with it