-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoInsertQuotes.js
120 lines (103 loc) · 3.45 KB
/
autoInsertQuotes.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
// === [autoInsertQuotes.js] ===
// http://akelpad.sourceforge.net/forum/viewtopic.php?p=26484#26484
// https://github.com/cuprum/AkelPad_scripts/blob/master/autoInsertQuotes.js
//
// (c) Cuprum 2014
//
// Автоматически добавляет кавычки после печати знака равенства внутри HTML-тега.
// Горячая клавиша для скрипта '='.
//
// Adding qoutes for attribute value on typing equals sign(=) inside HTML tag.
// Hotkey for script is '='
//
// Arguments:
// -singleQuotes - Set single quotes after typing =.
// -fileExts="htm, html, php" - Allows to set custom file extensions. Default is "html, xml".
//
// Usage:
// Call("Scripts::Main", 1, "autoInsertQuotes.js")
// Call("Scripts::Main", 1, "autoInsertQuotes.js",'-singleQuotes')
// Call("Scripts::Main", 1, "autoInsertQuotes.js",'-fileExts="htm, html, php" -singleQuotes')
//
// Versions:
// 1.2 (23.11.2014)
// Support xml notation in attribute names.
//
// 1.1 (5.11.2014)
// Typing '=' without quotes inside attribute value.
//
// 1.0 (1.11.2014)
// Initial release.
var hMainWnd = AkelPad.GetMainWnd(),
hWndEdit = AkelPad.GetEditWnd(),
hWndFocus = AkelPad.SendMessage(hMainWnd, 1317 /*AKD_GETFOCUS*/, 0, 0),
fileExts = AkelPad.GetArgValue("fileExts", "html, xml"),
quotes = "\"\"",
LastTagPattern = /<[^\s>\/]+(?:[^>"']|"[^"]*"|'[^']*')*$/i,
attrPattern = /\s[a-z_:][\w\-.:]*$/i,
currentExt,
args,
textStr;
// http://akelpad.sourceforge.net/forum/viewtopic.php?p=11382#11382
function GetOffset(hWndEdit, nType /*AEGI_*/, nOffset) {
var lpIndex;
if (lpIndex = AkelPad.MemAlloc(_X64?24:12 /*sizeof(AECHARINDEX)*/)) {
if (nOffset != -1) {
AkelPad.SendMessage(hWndEdit, 3137 /*AEM_RICHOFFSETTOINDEX*/, nOffset, lpIndex);
}
AkelPad.SendMessage(hWndEdit, 3130 /*AEM_GETINDEX*/, nType, lpIndex);
nOffset = AkelPad.SendMessage(hWndEdit, 3136 /*AEM_INDEXTORICHOFFSET*/, 0, lpIndex);
AkelPad.MemFree(lpIndex);
}
return nOffset;
}
function GetTextLineStartToCaret() {
var nCaretPos,
nCaretLineStartPos;
nCaretPos = GetOffset(hWndEdit, 5 /*AEGI_CARETCHAR*/, -1);
nCaretLineStartPos = GetOffset(hWndEdit, 18 /*AEGI_WRAPLINEBEGIN*/, nCaretPos);
return AkelPad.GetTextRange(nCaretLineStartPos, nCaretPos);
}
// Compiling based on http://akelpad.sourceforge.net/forum/viewtopic.php?p=19641#19641
function GetFileExt() {
var hWndEdit = AkelPad.GetEditWnd(),
hDocEdit = AkelPad.GetEditDoc(),
pAlias="",
lpAlias,
fileExt;
if (!hWndEdit || !hDocEdit) {
return "";
}
if (lpAlias = AkelPad.MemAlloc(256 * 2 /*sizeof(wchar_t)*/)) {
AkelPad.CallW("Coder::Settings", 18 /*DLLA_CODER_GETALIAS*/, hWndEdit, hDocEdit, lpAlias, 0);
pAlias = AkelPad.MemRead(lpAlias, 1 /*DT_UNICODE*/);
AkelPad.MemFree(lpAlias);
}
if (pAlias) {
fileExt = pAlias.split('.');
fileExt = fileExt[fileExt.length - 1];
}
return fileExt;
}
function insertQuotes(quotes) {
var nCaretPos;
AkelPad.ReplaceSel("=" + quotes);
nCaretPos = AkelPad.GetSelStart() - 1;
AkelPad.SetSel(nCaretPos, nCaretPos);
WScript.Quit();
}
currentExt = GetFileExt();
if (args = AkelPad.GetArgLine()) {
if (args.indexOf("singleQuotes") >= 0) {
quotes = "''";
}
}
if (fileExts.indexOf(currentExt) >= 0) {
textStr = GetTextLineStartToCaret();
if (LastTagPattern.test(textStr) && attrPattern.test(textStr)) {
insertQuotes(quotes);
}
}
if (hWndFocus) {
AkelPad.SendMessage(hWndFocus, 258 /*WM_CHAR*/, 61 /*=*/, 0);
}