The Slick-CŪ language provides two module initialization functions called definit and defload. If the two are present, the procedures definit and defload are called when a module is loaded. The definit module is called before defload. When the module is saved by the write_state command, the definit procedure is invoked each time the editor is invoked. This gives your module an opportunity to perform initializations such as creating a temporary file, or allocating a selection, or bookmark. The following syntax is used for defining the special functions definit and defload:
definit()
{
statements
}
defload()
{
statements
}
The return value of these functions is always void. You cannot specify an argument to the return statement. To enhance the performance of SlickEditŪ, use the defload primitive instead of the definit primitive. The definit primitive forces a module to be loaded when the editor is invoked. When definit is called, the expression arg(1) indicates whether the module was loaded with the load command or when the editor initialized. When a module is loaded, arg(1) returns L. Otherwise arg(1) returns a null string ("").
Example:
int gmarkid= -1;
definit()
{
// If this is an editor invocation,
if (arg(1)!="L") {
gmarkid=-1; // indicate no mark is allocated.
}
}There are two subtle points to this example when assuming that the gmarkid variable is used to contain an allocated mark id (also called selection handle). First, the variable gmarkid is scoped as global and not static. This is because the mark needs to remain allocated when this module is reloaded. When the module is reloaded, an unload of the module occurs first and the _free_selection built-in is not called to free a mark already allocated (there is no defunload primitive). Modules with static variables (module scope) lose their value when reloaded. Second, the value of arg(1) is used to make sure that the variable gmarkid is initialized only when the editor is invoked and not when the module is loaded. Use this as a template for creating a temporary buffer in the hidden window.
Example:
#include "slick.sh"
definit()
{
get_view_id(view_id);
activate_view(HIDDEN_VIEW_ID);
status=find_view(".bookmark");
if ( status ) {
/* Create a buffer and view in hidden window. */
status=load_files("+c +t");
if (status) {
// The nls function may be used for national language support
// in the future.
_message_box(nls('Could not create bookmark buffer'))
return;
}
p_buf_name=".bookmark";_delete_line();
p_buf_flags= THROW_AWAY_CHANGES|HIDE_BUFFER|KEEP_ON_QUIT;
}
// Note: ELSE case cannot empty bookmark buffer unless mark ids
// are freed. Might as well leave them.
get_view_id(bookmark_view_id);
activate_view(view_id);
}