EmEditor (text editor) Forum Index
   Macro Samples
     Select whole HTML / XML tag
Register To Post

Threaded | Newest First Previous Topic | Next Topic | Bottom
Poster Thread
Stefan
Posted on: 5/15/2012 8:58 am
Home away from home
Joined: 7/14/2008
From: Germany, EU
Posts: 285
Select whole HTML / XML tag

Select whole HTML / XML tag

<tag> ... </tag>



How this looks:





Some tips:

- This is version 0.01 but works good for me.

- The purpose is to select an whole <tag>...</tag>

- Therefore the script takes the word under the cursor (the TAG, i hope)
- then it looks at each line till EOF for the closing tag with same name.
- On the way it counts other opening and closing tag's.
- only if (opening - closing == 0) i assume the right closing tag is found.
That way i can find nested tag's also.

- it works only for proper written tags without spaces due to:

//Set the search pattern:
OpenTag = "<" + TagName;
CloseTag = "</" + TagName + ">";

Examples:
<tag yxz> = OK
<tag> = OK
</tag> = OK
/> = OK
< tag > = wrong
</ tag> = wrong

- put your cursor ON the word of the opening tag or at one side:

Examples:
<|tag yxz> = OK
<t|ag yxz> = OK
<ta|g yxz> = OK
<tag| yxz> = OK
<tag |yxz> = wrong

(but i think that can be improved,
as the whole macro could be more clever)



The macro
- save this script as "SelectWholeTag.jsee"
- assign an shortcut to it like "Ctrl+Alt+T"
- or put it as macro into the snippet plugin.



//Select Whole HTML / XML tag, v 0.01, 05/2012 Stefan
/////////////////////////////////////////////////////

//for less typing:
o = document.selection;

// MUCH quicker. But less informative.
Redraw = false; 

//get the start position:
PosStartLine   = o.GetActivePointY(eePosLogical);
PosOrigColumn  = o.GetActivePointX(eePosLogical);

//get the current word under cursor:
o.SelectWord();
TagName  = o.Text;

//This did not work if tag name length==1 AND cursor at the right. So:
if (TagName.length == 1){
	ValidSigns = "abcdefghijklmnopqrstuvwxyz"; //means: no '>' or blank.
	if (ValidSigns.indexOf(TagName) == -1){
		o.SetActivePoint( eePosLogical, PosOrigColumn -1, PosStartLine, false );
		o.SelectWord();
		TagName  = o.Text;
	}
}

//Set the search pattern:
OpenTag  = "<"  + TagName;
CloseTag = "</" + TagName + ">";

//Get the position of the first opening tag:
TextThisLine   = document.GetLine(PosStartLine);
PosStartColumn = TextThisLine.indexOf(OpenTag);

//some initializing:
EndTagFound = false;


//start first round:
DoIt(CloseTag);

//If no closing tag found, try an another syntax:
	if (EndTagFound == false){
		o.SetActivePoint( eePosLogical, PosOrigColumn, PosStartLine, false );
		//alert("EOF reached but have not found the matching tag: " + CloseTag 
			  //+ "\n\nFound opening " + 	CountOpeningTAGS + " but " 
		      //+ CountClosingTAGS + " closing tags.\n\nWill try again...");

		CloseTag = "/>"; 
		DoIt(CloseTag);
		if (EndTagFound == false){
			o.SetActivePoint( eePosLogical, PosOrigColumn, PosStartLine, false );
			//alert("EOF reached but have not found the matching tag: " + CloseTag 
				  //+ "\n\nFound opening " + 	CountOpeningTAGS + " but "
			      //+ CountClosingTAGS + " closing tags.\n\nSorry, i giving up.");
		}
	}

//The main function to search the right closing tag:
function DoIt(CloseTag){
	CountOpeningTAGS=0;
	CountClosingTAGS=0;
  
	//For Each Line till end of document:
	while (true){
	//For debugging, iterate a few lines only:
	//for( L=1; L < 60; L++){
    
		//Get current line:
		PosThisLine = o.GetActivePointY(eePosLogical);
		TextThisLine = document.GetLine(PosThisLine);
		
		//Found an opening tag?
		if (TextThisLine.indexOf(OpenTag) > -1){
			CountOpeningTAGS++; 
			LastCol = TextThisLine.indexOf(OpenTag) + 1;
		}
		//check for an second at the same line:
		if (TextThisLine.indexOf(OpenTag, LastCol +1) > -1){
			CountOpeningTAGS++; 
		}

		
		//Found an closing tag?
		if (TextThisLine.indexOf(CloseTag) > -1){
			CountClosingTAGS++;
			LastCol = TextThisLine.indexOf(CloseTag);
		}
		//check for an second at the same line:
		if (TextThisLine.indexOf(CloseTag, LastCol +1) > -1){
			CountClosingTAGS++;
		}
			
		//Found the right closing tag?
		//alert(CountOpeningTAGS +'-'+ CountClosingTAGS);
		if (CountOpeningTAGS - CountClosingTAGS == 0){
			PosEndColumn = TextThisLine.indexOf(CloseTag) + 1;
			PosEndColumn = TextThisLine.indexOf(">", PosEndColumn) + 2;

			o.SetActivePoint( eePosLogical, PosEndColumn, PosThisLine, false );
			o.SetAnchorPoint( eePosLogical, PosStartColumn +1, PosStartLine );
			EndTagFound = true;
			break;
		}
		
		//If EOF? Then stop:
      if (PosThisLine >= document.GetLines()){break;}
		//Else move one line down:
      o.StartOfLine( false , eeLineLogical );
      o.LineDown();
		//alert( ' Now on line: ' +PosThisLine);

	} //Loop
} // function DoIt();



Threaded | Newest First Previous Topic | Next Topic | Top


Register To Post
 
English čeština Deutsch español français italiano 日本語 한국어 Русский 简体中文 繁體中文