/
newFileTemplate.js
190 lines (179 loc) · 5.35 KB
/
newFileTemplate.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// https://akelpad.sourceforge.net/forum/viewtopic.php?p=9926#p9926
// https://infocatcher.ucoz.net/js/akelpad_scripts/newFileTemplate.js
// https://github.com/Infocatcher/AkelPad_scripts/blob/master/newFileTemplate.js
// (c) Infocatcher 2009
// Version: 0.2.5 - 2010-06-21
// Author: Infocatcher
//// Create new file from template
// Usage:
// Call("Scripts::Main", 1, "newFileTemplate.js", "html", 0)
// - use template for "html"
// Call("Scripts::Main", 1, "newFileTemplate.js", "", 0)
// - use template for extension of current file
// Call("Scripts::Main", 1, "newFileTemplate.js", "?", 0)
// - ask user about extension
// Call("Scripts::Main", 1, "newFileTemplate.js", `"%f"`, 0)
// - use active file (e.g. in ContextMenu plugin)
// - or `"c:\\path\to\my\file\myFile.ext"` instead of `"%f"`
// Settings begin
var templatesDir = "%a\\AkelFiles\\Plugs\\Scripts\\Params\\" + getScriptDirName();
// For "script.js" getScriptDirName() return "script".
var templateFileName = "template";
var selFlag = "%|";
var selTextFlag = "%s";
// Settings end
//var AkelPad = new ActiveXObject("AkelPad.document");
var hMainWnd = AkelPad.GetMainWnd();
var oSys = AkelPad.SystemFunction();
var locale = {
lng: null,
strings: {
extRequired: {
ru: "Расширение нового файла: ",
en: "Extension of new file: "
}
},
getStr: function(name) {
if(!this.lng) {
var nLangID = oSys.Call("kernel32::GetUserDefaultLangID");
nLangID = nLangID & 0x3ff; //PRIMARYLANGID
switch(nLangID) {
case 0x19: this.lng = "ru"; break;
default: this.lng = "en";
}
}
return this.strings[name][this.lng];
}
};
function TemplateMaker(tDir, tFileName, selFlag, selTextFlag) {
this.tDir = tDir;
this.tFileName = tFileName;
this.selFlag = selFlag;
this.selTextFlag = selTextFlag;
}
TemplateMaker.prototype = {
initTemplate: function() {
this.ext = this.getExt();
return this.getTemplate();
},
getExt: function() {
var ext;
if(WScript.Arguments.length) {
ext = WScript.Arguments(0);
if(ext == "?")
return this.ackUserExt();
else if(ext && /\\.*\.([^.]+)$/.test(ext)) { // ...\filename.ext
this.tPath = ext;
ext = RegExp.$1;
this.precessTemplate = false;
}
}
ext = ext || this.getCurrentExt() || this.ackUserExt();
return ext && ext.toLowerCase();
},
ackUserExt: function() {
var ext = AkelPad.InputBox(
hMainWnd, WScript.ScriptName,
locale.getStr("extRequired"),
this.getCurrentExt() || regVal("lastExt") || ""
);
return ext && ext.toLowerCase();
},
getCurrentExt: function() {
if(!this._getCurrentExt) {
var cFile = AkelPad.GetEditFile(0);
this._getCurrentExt = cFile && /\.([^.]+)$/i.test(cFile) && RegExp.$1;
}
return this._getCurrentExt;
},
tPath: null,
tmpl: "",
precessTemplate: true,
selParams: null,
getTemplate: function() {
if(!this.ext)
return false;
var pFile = this.tPath
|| this.tDir
.replace("%a", AkelPad.GetAkelDir())
.replace(/\\?$/, "\\")
+ this.tFileName + "." + this.ext;
if(!this.fileExist(pFile))
return true;
this.tmpl = AkelPad.ReadFile(pFile)
.replace(/\r\n/g, "\n"); // For AkelPad 4.4.4
if(!this.precessTemplate)
return true;
if(this.tmpl.indexOf(this.selTextFlag) != -1)
this.tmpl = this.tmpl.replace(this.selTextFlag, AkelPad.GetSelText());
var selStart = this.tmpl.indexOf(this.selFlag);
this.delFlag(selStart);
var selEnd = this.tmpl.lastIndexOf(this.selFlag);
this.delFlag(selEnd);
if(selStart != -1 && selEnd == -1)
selEnd = selStart;
this.selParams = selStart != -1
? [selStart, selEnd]
: null;
return true;
},
fileExist: function(fName) {
return new ActiveXObject("Scripting.FileSystemObject")
.FileExists(fName);
},
delFlag: function(pos) {
if(pos != -1)
this.tmpl = this.tmpl.substring(0, pos)
+ this.tmpl.substring(pos + this.selFlag.length, this.tmpl.length);
},
insTemplate: function() {
AkelPad.ReplaceSel(this.tmpl);
this.formatTemplate();
regVal("lastExt", this.ext);
},
formatTemplate: function() {
var ext = this.ext;
if(ext) {
//AkelPad.Call("HighLight::Main", 1, ext);
//AkelPad.Call("AutoComplete::Main", 1, ext);
AkelPad.Call("Coder::Settings", 1, ext);
}
var sp = this.selParams;
if(sp)
AkelPad.SetSel(sp[0], sp[1]);
else
AkelPad.SetSel(0, 0);
}
};
if(hMainWnd) {
var lpPoint = AkelPad.MemAlloc(8 /*sizeof(POINT)*/);
if(lpPoint) {
var tm = new TemplateMaker(templatesDir, templateFileName, selFlag, selTextFlag);
if(tm.initTemplate()) {
AkelPad.SendMessage(hMainWnd, 273 /*WM_COMMAND*/, 4101 /*IDM_FILE_NEW*/, 0);
var hWndEditNew = AkelPad.GetEditWnd();
setRedraw(hWndEditNew, false);
AkelPad.SendMessage(hWndEditNew, 1245 /*EM_GETSCROLLPOS*/, 0, lpPoint);
tm.insTemplate();
AkelPad.SendMessage(hWndEditNew, 1246 /*EM_SETSCROLLPOS*/, 0, lpPoint);
setRedraw(hWndEditNew, true);
AkelPad.MemFree(lpPoint);
}
}
}
function setRedraw(hWnd, bRedraw) {
AkelPad.SendMessage(hWnd, 11 /*WM_SETREDRAW*/, bRedraw, 0);
if(bRedraw)
oSys.Call("user32::InvalidateRect", hWnd, 0, true);
}
function getScriptDirName() {
return WScript.ScriptName.replace(/\.js$/i, "");
}
function regVal(name, val) {
name = "HKCU\\Software\\Akelsoft\\AkelPad\\Plugs\\Scripts\\newFileTemplate\\" + name;
var ws = new ActiveXObject("WScript.shell");
if(typeof val != "undefined")
return ws.RegWrite(name, val, "REG_SZ");
try { return ws.RegRead(name); }
catch(e) { return ""; }
}