scripting:examples:text-highlight

No renderer 'pdf' found for mode 'pdf'

Using Text Highlight

HippoEDIT scripting API does not support ordinary highlighting of the text ranges with a color. But you can do the same using styles. The example below shows you usage of AddStyleRange and RemoveStyleRanges for adding and removing text highlights.

highlighting.hejs
var doc = ActiveDocument;
var sel = new Range(0, 0, doc.LineCount - 1, doc.GetLineLength(doc.LineCount - 1));
var output = Application.Output("Test Output");
var results = doc.FindAll("test", 0, sel);
var styleHigligh = new Style("my_highlighting", "Highlighting", "My Highlighting Style");
styleHigligh.BoxColor = "Error";
styleHigligh.BoxStyle = eBoxStyleRoundRect;
output.writeln("Find all \"test\" matches in current document (" + results.length + "):");
for (var i = 1, len = results.length; i <= len; i++) {
    output.writeln("Line : " + results[i].Top + ", Pos : " + results[i].Left);
    doc.AddStyleRange(results[i], styleHigligh);
}
doc.RemoveStyleRanges(null, styleHigligh);
highlighting2.hejs
// create the highlighting style, remember, it is alive whole editor session
var styleHighlight = new Style("my_highlighting", "Highlighting", "My Highlighting Style");
//styleHighlight.ForeColor = "#FF0000";
styleHighlight.BackColor = "#FFFF00";
 
function Highlight1()
{
	// get current cursor position
	var posAbs = ActiveDocument.TextToAbsolute(ActiveView.Position);
 
	// create range including two characters before and two after
	var posStart = ActiveDocument.AbsoluteToText(posAbs - 2);
	var posEnd = ActiveDocument.AbsoluteToText(posAbs + 2);
	var rangeHighlight = new Range(posStart.Line, posStart.Pos, posEnd.Line, posEnd.Pos);
 
	// highlight range with our style
	ActiveDocument.AddStyleRange(rangeHighlight, styleHighlight);	
}
 
function Highlight2()
{
	// get current cursor position
	var posStart = ActiveDocument.MovePosition(ActiveView.Position, -2);
	var posEnd   = ActiveDocument.MovePosition(ActiveView.Position, 2);
 
	// create range including two characters before and two after
	var rangeHighlight = new Range(posStart, posEnd);
 
	// highlight range with our style
	ActiveDocument.AddStyleRange(rangeHighlight, styleHighlight);	
}
 
Highlight2();
 
// remove our highlighting
sleep(3000);
ActiveDocument.RemoveStyleRanges(null, styleHighlight);