/* Welcome to the macro examples file! */ // The first function only serves as an introduction. // It is bound to a keyboard shortcut, to make it easier, // the rest of the macros can be invoked via Macro item in the menu. macro "Start here" Ctrl+Alt+S { // Open this file EditFile("Macro", "Macro.usc"); // Set cursor to the start SetCursor(0); // Now you can continue reading and exploring the rest of the examples... } // Macros can call functions defined via "fn" keyword anywhere (even other files, see Func.usc). // This is usefull e.g. when you want to share some funcionality between several macros. macro "Using functions" { // This function opens the example file, moves cursor at the end and appends some strings // generated by functions. EditFile("Macro","example.txt"); MoveTextEnd(); Insert("\n " + FuncA("C") + FuncB("D") + " \n"); } fn FuncA(arg) { return FuncB(arg + "A"); } /* Chapter 1: Methods controlling the editor */ macro "1 Editor":"1.1 File name" { ClearConsole(); Echo("Currently edited file is '" + FileName() + "'"); } macro "1 Editor":"1.2 Opening file" { // EditFile can be used with package and file names, as seen here, or it can ba called // with single parameter containing full path to a file. EditFile("Macro","example.txt"); } macro "1 Editor":"1.3 Closing file" { // Save the file first, to be sure SaveCurrentFile(); // Now we can close it CloseFile(); } /* Chapter 2: Methods for automatic text editing */ macro "2 Editing":"2.1 Select all" { // Again, make sure we use the testing file EditFile("Macro","example.txt"); // This moves the cursor at the begining of file (the same can be achieved with SetCursor(0);) MoveTextBegin(); // Now we move the cursor at the end of file. The parameter says that we want to select all // the text in between the previous and new position. MoveTextEnd(1); } macro "2 Editing":"2.2 Cursor position" { EditFile("Macro","example.txt"); // We can get curent cursor position (in characters from start)... pos = GetCursor(); // ... which can be used to calculate line number ... line = GetLine(pos); // ... and also position within the line col = GetColumn(pos); // Now we move at the end of file and insert a string summarizing what we found out MoveTextEnd(); Insert("Line:"+to_string(line)+", Col:"+to_string(col)+", Pos"+to_string(pos)+"\n"); // And finally, we put the cursor back where it originally was, just for users convenience SetCursor(pos); } macro "2 Editing":"2.3 File info" { EditFile("Macro","example.txt"); pos = GetCursor(); // Another set of informations we can get from the editor methods is how many text is there length = GetLength(); // And also how many lines lines = GetLineCount(); // Again, we append the info at the end and return to original position MoveTextEnd(); Insert("Length:"+to_string(length)+", Lines:"+to_string(lines)+"\n"); SetCursor(pos); } macro "2 Editing":"2.4 Complex example" { /* This function analyzes a document (example.txt) and prints a histogram of character * usage at the bottom. It could probably be implemented better, faster or both, * but it is intentionaly left as is, to demonstrate that even simple macro language * can be used to achieve quite complex tasks. */ // how many lines should we use to print the histogram height = 20; // set active file to the example text EditFile("Macro", "example.txt"); // remember cursor position, so we can return it at the end pos = GetCursor(); // this is a little trick to make sure Esc knows that hist is a map, not an array hist[""] = 0; // iterate over entire document and count letters length = GetLength(); for(i=0; i