scripting:examples:text-highlight

This is an old revision of the document!


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
// 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);