A variable that is declared with the var statement, or created by assignment is considered local to the function that contains the statement. You cannot access the local variable outside of the function that defines it. A local variable ceases to exist after the function returns. However, a global variable has a global scope instead of local. That means you can access it from multiple functions, and its value exists after a function returns, or the whole macro stops running.
Once a global variable is defined, its value will be retained as long as the Source Insight session lasts. That is, they can contain information between invocations of macros or event functions. They are lost when you exit Source Insight.
The global statement is used to declare global variables. For example:
You can declare more than one global variable by separating the variables with a comma. For example:
global a, b, c // declares three global variables
The global statement should appear inside a function, and must be in the execution path of your code. For example:
When the global statement is encountered, the variable is entered into the global-scope variable table. The variable is initialized to nil. This example shows how you might declare and use a global counter variable:
// this initializes it the first time through
if last_count == nil last_count = 0
last_count++ // increment our global counter
Global variables hold their values for the whole Source Insight session.
Global variables are useful for adding counters, and other persistent state. They cannot hold any kind of handle, because all handles are destroyed when a macro finishes. So, for example, this will not work:
hbuf = OpenBuf("abc.txt")
In the above example, the hbuf variable will contain a bogus handle as soon as this macro finishes because handles only exist while a macro is running.