by cf1 » Wed Aug 30, 2023 12:06 pm
if you need to do this with selected text, add the following functions to convertleading.js:
/////////////////////////////////////////////
function SpacesToTabsSelected(Sender) {
var tabSize = Script.Settings.TabSize;
var userSelection = Editor.Selection;
if ((userSelection.SelStartLine >= 0) && (userSelection.SelEndLine >= 0)) {
Editor.BeginEditing;
var startLine = userSelection.SelStartLine;
var endLine = userSelection.SelEndLine;
if ((endLine > startLine) && (userSelection.SelEndCol == 0)) {
endLine--;
}
var line;
var s = "";
for (var i = startLine; i <= endLine; i++) {
line = Editor.Lines[i];
if(RegexMatchAll(line, "^(\\s*)(.*)$", true, match, p) == true) {
var len = Length(_v(match, [0, 1]));
var remainingSpaces = len - Round(len/tabSize)*tabSize;
line=
RepeatChar(chr(9), Round(len/tabSize)) + RepeatChar(" ", remainingSpaces) + _v(match, [0, 2]);
}
s += line;
if(i< endLine) s += "\n";
}
Editor.SelText = s;
Editor.EndEditing;
}
}
function TabsToSpacesSelected(Sender) {
var userSelection = Editor.Selection;
if ((userSelection.SelStartLine >= 0) && (userSelection.SelEndLine >= 0)) {
Editor.BeginEditing;
var startLine = userSelection.SelStartLine;
var endLine = userSelection.SelEndLine;
if ((endLine > startLine) && (userSelection.SelEndCol == 0)) {
endLine--;
}
var SL = new TStringList;
for (var i = startLine; i <= endLine; i++) {
SL.Add(Editor.LinesAsDisplayed[i]);
}
Editor.SelText = SL.Text;
delete SL;
Editor.EndEditing;
}
}
/////////////////////////////////////////////
and lines to the end of the file:
var act3 = Script.RegisterDocumentAction("Convert Leading", "Spaces to Tabs in selected", "", "SpacesToTabsSelected");
var act4 = Script.RegisterDocumentAction("Convert Leading", "Tabs to Spaces in selected", "", "TabsToSpacesSelected");