|
Macro Examples |
|
|
The following example macros show the syntax of Boxer's macro language, while also suggesting useful methods of attack for common programming tasks:
Move cursor to bottom of paragraph
// move the cursor to the bottom line of the current paragraph
macro BottomOfParagraph() { while (LineNumber < LineCount && !LineIsEmpty(LineNumber+1)) Down; }
Move cursor to top of previous paragraph
// move the cursor to the top line of the previous paragraph
macro TopOfPreviousParagraph() { Up;
while (LineNumber > 1 && !LineIsEmpty(LineNumber-1)) Up;
StartOfLine; }
Move cursor to top of current paragraph
// move the cursor to the top line of the current paragraph
macro TopOfCurrentParagraph() { while (LineNumber > 1 && !LineIsEmpty(LineNumber-1)) Up; }
Move cursor to top of next paragraph
// move the cursor to the first line of the next paragraph
macro TopOfNextParagraph() { while (LineNumber < LineCount && !LineIsEmpty(LineNumber)) Down;
Down; StartOfLine; }
Add a newline after every closing angle bracket
// add a newline after each closing angle (>) character // unless the angle already appears at end of line
macro AddNewlineAfterCloseAngle() { int line, i, j; string str;
// loop on all lines in the file for (line = 1; line <= LineCount(); line++) { // get the text of line 'line' into string 'str' GetLineText(line, str);
// get the index of the closing angle j = strchr(str, '>');
// if the character was found and was not at end-of-line... if (j != -1 && str[j+1] != '\0') { GotoLine(line); GotoColumn(1);
// advance the cursor to the character while (ValueAtCursor() != '>') Right;
// and past the character Right;
// insert a newline Enter;
// process this line again in case other tags exist line--; } } }
Apply HTML markup to a simple text file
// apply HTML markup to a simple text file // also converts double quote, ampersand, and // angle brackets to HTML equivalents
macro ApplyHTMLMarkup() { int prevlen, len, i; string str; int numchanges;
// loop on all lines in the file for (i = 1; i <= LineCount; i++) { // get the text of line 'i' into 'str' GetLineText(i, str);
// reset the change counter numchanges = 0;
// convert sensitive characters to HTML codes numchanges += ChangeString(str, "&", "&"); numchanges += ChangeString(str, "<", "<"); numchanges += ChangeString(str, ">", ">"); numchanges += ChangeString(str, "\"", """);
// if changes were made, replace the line's text if (numchanges > 0) PutLineText(i, str); }
// move to top of file StartOfFile; Down;
// loop on all lines in the file, starting on line 2 for (i = 2; i <= LineCount; i++) { // get the length of the previous line prevlen = LineLength(i-1);
// get the length of this line len = LineLength(i);
// if this line is empty, and the previous line isn't... // apply <br> markers to the end of the line if (len == 0 && prevlen != 0) { Up; EndOfLine; PutString("<br><br>"); StartofLine; Down; }
Down; // move down to the next line }
StartOfFile;
PutString("<html>\n"); PutString("<head>\n"); PutString("<title></title>\n"); PutString("</head>\n\n"); PutString("<body>\n");
EndOfFile; EndOfLine; PutString("\n"); PutString("</body>\n"); PutString("</html>\n");
// place cursor between title and /title GotoLine(3); GotoColumn(8); }
Display an ASCII chart in a new file
// ASCII chart example
macro ASCIIchart(void) { char i;
// open a new file New;
// loop from space to 255 to show all chars for (i = ' '; i <= 255; i++) printf("The ASCII value of '%c' is %d\n", i, i); }
Convert comma-separated-value (CSV) data
// convert comma-separated-value (CSV) data on the current // line so that each field is placed on its own line
macro ConvertCSV() { string str; int numquotes, numcommas;
// get the count of quotes/commas on this line numquotes = LineContains(linenumber, "\"");
numcommas = LineContains(linenumber, ",");
// if this appears to be CSV data... if (numcommas+1 == numquotes / 2) { // get the text of the current line GetLineText(linenumber, str);
// remove any empty data fields ChangeString(str, "\"\",", "");
// convert "," to a newline ChangeString(str, "\",\"", "\n");
// remove the first and last quotes ChangeString(str, "\"", "");
// select the line GoToColumn(1); SelectToEndOfLine;
// replace the selection PutString(str); }
// position for next line Down; StartOfLine; }
Cut lines containing a user-defined string
// cut lines containing a user-defined string to the Windows clipboard
macro CutLinesContaining(); { int line; int len; string str; int numcut = 0;
// get the string from the user len = GetString("Cut lines containing this string:", str);
if (len == 0) return;
// make the Windows clipboard the active clipboard SetClipboard(0);
// clear the Windows clipboard ClearClipboard(0);
// move cursor to start of file StartOfFile();
// loop on all lines in the file for (line = 1; line <= LineCount(); line++) { // does this line contain the string? if (LineContains(line, str)) { GotoLine(line); CutAppend(); numcut++; // tally the cut line--; // stay here for next line } }
// report the results if (numcut == 1) message("Results", "1 line was cut to the Windows clipboard"); else message("Results", numcut, " lines were cut to the Windows clipboard"); }
Delete blank lines
// delete blank lines in the current file
macro DeleteBlankLines(void) { int i, len;
// start at the top of the file StartOfFile;
// loop on all lines in the file for (i = 1; i <= LineCount; i++) { // get the length of this line len = LineLength(i);
// is this line empty? if (len == 0) { DeleteLine; // delete this line i--; // stay at this line # } else { Down; // move down to the next line } } }
Delete lines containing a user-defined string
// deletes lines containing a user-defined string
macro DeleteLinesContaining() { int line; int len; string str; int deleted = 0;
// get the string from the user len = GetString("Delete lines containing this string:", str);
if (len == 0) return;
// loop on all lines in the file for (line = 1; line <= LineCount(); line++) { // does this line contain the string? if (LineContains(line, str)) { DeleteLine(line); // delete it deleted++; // tally the deletion line--; // stay here for next line } }
// report the results if (deleted == 1) message("Results", "1 line was deleted"); else message("Results", deleted, " lines were deleted"); }
Delete lines NOT containing a user-defined string
// deletes lines NOT containing a user-defined string
macro DeleteLinesNotContaining() { int line; int len; string str; int deleted = 0;
// get the string from the user len = GetString("Delete lines that do NOT contain this string:", str);
if (len == 0) return;
// loop on all lines in the file for (line = 1; line <= LineCount(); line++) { // does this line contain the string? if (!LineContains(line, str)) { DeleteLine(line); // delete it deleted++; // tally the deletion line--; // stay here for next line } }
// report the results if (deleted == 1) message("Results", "1 line was deleted"); else message("Results", deleted, " lines were deleted"); }
Compute return on a deposited amount
// Compute result of amount left on deposit with continuous // compounding. Uses the formula: P = pe^rt
macro ComputeDeposit() { float amt, newamt, rate, years; string str;
GetFloat("Enter the amount on deposit:", amt);
GetFloat("Enter the interest rate:", rate);
// if user entered 5, make it .05, for example if (rate > 1.0) rate /= 100.0;
GetFloat("Enter the number of years on deposit:", years);
newamt = amt * pow(e, rate * years);
sprintf(str, "The amount with interest applied is: %.2f", newamt); Message("Result", str); }
Add blank lines after lines ending with !.?
// add a blank line after any line that ends with !.?
macro AddBlankLines(void) { char ch; int i;
// loop on all lines in the file for (i = 1; i <= LineCount; i++) { // make sure this line is not empty if (LineLength(i) > 1) { // move the cursor to this line GotoLine(i);
// move to the end of the line EndOfLine;
// backup off newline and onto last char Left;
// get the value of char at the cursor ch = ValueAtCursor();
// if it's a line ender, add Enter if (ch == '.' || ch == '?' || ch == '!') { EndOfLine; Enter; } } } }
Double space and reformat
// double space and reformat the text on the clipboard // prepares a web document for printing
macro DoubleSpaceAndReformat(void) { int i; int numlines;
// save various editor settings SaveSettings;
// open a new file and paste from clipboard New; Paste;
// set Text Width to 96 TextWidth(96);
// delete all blank lines DeleteBlankLines;
// record the number of lines BEFORE we start adding lines numlines = LineCount - 1;
// go to the top StartOfFile;
// double space the file for (i = 1; i <= numlines; i++) { Down; PutString("\n"); }
// reformat the whole file |