-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTabsToSpaces_mod.js
More file actions
140 lines (126 loc) · 3.86 KB
/
TabsToSpaces_mod.js
File metadata and controls
140 lines (126 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// https://akelpad.sourceforge.net/forum/viewtopic.php?p=7081#p7081
// https://infocatcher.ucoz.net/js/akelpad_scripts/TabsToSpaces_mod.js
// https://github.com/Infocatcher/AkelPad_scripts/blob/master/TabsToSpaces_mod.js
//// Convert tabulation to space for AkelPad 4.x.x
// Modified by Infocatcher
// Version: 2010-12-29
// Author: Shengalts Aleksander aka Instructor, Infocatcher
var hMainWnd=AkelPad.GetMainWnd();
var hWndEdit=AkelPad.GetEditWnd();
var oSys=AkelPad.SystemFunction();
var pScriptName=WScript.ScriptName;
var pSelText;
var pResult;
var pSpaces;
var nSelStart;
var nSelEnd;
var nTabStop;
var nColumn;
var nSpaceCount;
var a;
var b;
if (hMainWnd)
{
if (AkelPad.IsAkelEdit())
{
nSelStart=AkelPad.GetSelStart();
nSelEnd=AkelPad.GetSelEnd();
if (nSelStart == nSelEnd)
{
//AkelPad.SetSel(0, -1);
nSelStart = 0;
nSelEnd = -1;
pSelText = AkelPad.GetTextRange(0, -1);
}
else
{
//SelCompleteLine(hWndEdit, nSelStart, nSelEnd);
var sel = GetCompleteLine(hWndEdit, nSelStart, nSelEnd);
nSelStart = sel[0];
nSelEnd = sel[1];
pSelText = AkelPad.GetTextRange(nSelStart, nSelEnd);
}
//nSelStart=AkelPad.GetSelStart();
//nSelEnd=AkelPad.GetSelEnd();
//pSelText=AkelPad.GetSelText();
nTabStop=AkelPad.SendMessage(hWndEdit, 3239 /*AEM_GETTABSTOP*/, 0, 0);
pResult="";
if (pSelText.indexOf("\t") == -1)
{
pResult = pSelText.replace(
new RegExp("^( {" + nTabStop + "})+", "mg"),
function(s)
{
return new Array(s.length/nTabStop + 1).join("\t");
}
);
}
else
{
for (a=0, b=0, nColumn=0; b < pSelText.length; ++b)
{
if (pSelText.charAt(b) == '\t')
{
nSpaceCount=nTabStop - nColumn % nTabStop;
nColumn+=nSpaceCount;
for (pSpaces=""; nSpaceCount; --nSpaceCount)
pSpaces=pSpaces + " ";
pResult=pResult + pSelText.substr(a, b - a) + pSpaces;
a=b + 1;
}
else if (pSelText.charAt(b) == '\r')
nColumn=0;
else
++nColumn;
}
pResult=pResult + pSelText.substr(a, b - a);
}
//AkelPad.ReplaceSel(pResult);
insertNoScroll(pResult, nSelStart, nSelEnd);
}
}
//Functions
function GetCompleteLine(hWnd, nMinSel, nMaxSel)
{
var nMinLine;
var nMaxLine;
var nMinLineIndex;
var nMaxLineIndex;
var nMaxLineLength;
if (nMinSel < nMaxSel)
{
nMinLine=AkelPad.SendMessage(hWnd, 1078 /*EM_EXLINEFROMCHAR*/, 0, nMinSel);
nMaxLine=AkelPad.SendMessage(hWnd, 1078 /*EM_EXLINEFROMCHAR*/, 0, nMaxSel);
nMinLineIndex=AkelPad.SendMessage(hWnd, 187 /*EM_LINEINDEX*/, nMinLine, 0);
nMaxLineIndex=AkelPad.SendMessage(hWnd, 187 /*EM_LINEINDEX*/, nMaxLine, 0);
nMaxLineLength=AkelPad.SendMessage(hWnd, 193 /*EM_LINELENGTH*/, nMaxSel, 0);
if (nMaxLineIndex == nMaxSel) --nMaxLine;
else if (nMaxLineLength) nMaxSel=nMaxLineIndex + nMaxLineLength + 1;
nMinSel=nMinLineIndex;
//AkelPad.SetSel(nMinSel, nMaxSel);
//return nMaxLine - nMinLine + 1;
return [nMinSel, nMaxSel];
}
//return 0;
throw new Error("GetCompleteLine: nMinSel >= nMaxSel");
}
function insertNoScroll(str, nSelStart, nSelEnd) {
var lpPoint = AkelPad.MemAlloc(8 /*sizeof(POINT)*/);
if(!lpPoint)
return;
//var hWndEdit = AkelPad.GetEditWnd();
setRedraw(hWndEdit, false);
AkelPad.SendMessage(hWndEdit, 1245 /*EM_GETSCROLLPOS*/, 0, lpPoint);
AkelPad.SetSel(nSelStart, nSelEnd);
//var ss = AkelPad.GetSelStart();
AkelPad.ReplaceSel(str, true);
//if(ss != AkelPad.GetSelStart())
// AkelPad.SetSel(ss, ss + str.length);
AkelPad.SendMessage(hWndEdit, 1246 /*EM_SETSCROLLPOS*/, 0, lpPoint);
setRedraw(hWndEdit, true);
AkelPad.MemFree(lpPoint);
}
function setRedraw(hWnd, bRedraw) {
AkelPad.SendMessage(hWnd, 11 /*WM_SETREDRAW*/, bRedraw, 0);
bRedraw && oSys.Call("user32::InvalidateRect", hWnd, 0, true);
}