Advertisement
rplantiko

UltraEdit - align at pattern

Jun 1st, 2014
605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Align selected text lines at a given pattern or substring
  2.  
  3. var alignAt   = UltraEdit.getString("Align at pattern",1);
  4. var alignAtPattern = new RegExp( alignAt );
  5.  
  6. // Cut selectedLines into clipboard
  7. // include cutSelection.js
  8.  
  9. // First Pass: Compute match positions and their maximum
  10.  
  11. var pos = selectedLines.map( function(line) {
  12.   return line.search( alignAtPattern );
  13.   });
  14. var maxPos = Math.max.apply(null,pos);
  15.  
  16. // Second pass: Perform the alignment
  17. if (maxPos >= 0) {
  18.   spaces = new Array(maxPos).join(' ');  // = ' ' x maxPos
  19.   var result = selectedLines.map( function(line, i ) {
  20.     if (pos[i]<0) {
  21. // Pattern doesn't match? Leave line unchanged
  22.       return line;
  23.       }
  24.     else {
  25. // Pattern matches: Insert correct number of spaces
  26.       return line.substring( 0, pos[i] ) +
  27.              spaces.substring(0,maxPos-pos[i]) +
  28.              line.substring(pos[i])
  29.       }
  30.     }).join(lineEnd);
  31.   UltraEdit.activeDocument.write( result );
  32.   }
  33. else {
  34.   UltraEdit.activeDocument.paste();
  35.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement