Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Align selected text lines at a given pattern or substring
- var alignAt = UltraEdit.getString("Align at pattern",1);
- var alignAtPattern = new RegExp( alignAt );
- // Cut selectedLines into clipboard
- // include cutSelection.js
- // First Pass: Compute match positions and their maximum
- var pos = selectedLines.map( function(line) {
- return line.search( alignAtPattern );
- });
- var maxPos = Math.max.apply(null,pos);
- // Second pass: Perform the alignment
- if (maxPos >= 0) {
- spaces = new Array(maxPos).join(' '); // = ' ' x maxPos
- var result = selectedLines.map( function(line, i ) {
- if (pos[i]<0) {
- // Pattern doesn't match? Leave line unchanged
- return line;
- }
- else {
- // Pattern matches: Insert correct number of spaces
- return line.substring( 0, pos[i] ) +
- spaces.substring(0,maxPos-pos[i]) +
- line.substring(pos[i])
- }
- }).join(lineEnd);
- UltraEdit.activeDocument.write( result );
- }
- else {
- UltraEdit.activeDocument.paste();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement