-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathEditFunctions.ahk
193 lines (152 loc) · 5.18 KB
/
EditFunctions.ahk
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
191
192
193
;************************
; Edit Control Functions
;************************
;
; Standard parameters:
; Control, WinTitle If WinTitle is not specified, 'Control' may be the
; unique ID (hwnd) of the control. If "A" is specified
; in Control, the control with input focus is used.
;
; Standard/default return value:
; true on success, otherwise false.
;http://www.autohotkey.com/board/topic/20981-edit-control-functions/
EditFunc_Standard_Params(ByRef Control, ByRef WinTitle) { ; Helper function.
if (Control="A" && WinTitle="") { ; Control is "A", use focused control.
ControlGetFocus, Control, A
WinTitle = A
} else if (Control+0!="" && WinTitle="") { ; Control is numeric, assume its a ahk_id.
WinTitle := "ahk_id " . Control
Control =
}
}
; Returns true if text is selected, otherwise false.
;
EditFunc_TextIsSelected(Control="", WinTitle="")
{
EditFunc_Standard_Params(Control, WinTitle)
return EditFunc_GetSelection(start, end, Control, WinTitle) and (start!=end)
}
; Gets the start and end offset of the current selection.
;
EditFunc_GetSelection(ByRef start, ByRef end, Control="", WinTitle="")
{
EditFunc_Standard_Params(Control, WinTitle)
VarSetCapacity(start, 4), VarSetCapacity(end, 4)
SendMessage, 0xB0, &start, &end, %Control%, %WinTitle% ; EM_GETSEL
if (ErrorLevel="FAIL")
return false
start := NumGet(start), end := NumGet(end)
return true
}
; Selects text in a text box, given absolute character positions (starting at 0.)
;
; start: Starting character offset, or -1 to deselect.
; end: Ending character offset, or -1 for "end of text."
;
EditFunc_Select(start=0, end=-1, Control="", WinTitle="")
{
EditFunc_Standard_Params(Control, WinTitle)
SendMessage, 0xB1, start, end, %Control%, %WinTitle% ; EM_SETSEL
EditFunc_SCROLLCARET(control,wintitle)
return (ErrorLevel != "FAIL")
}
; Selects a line of text.
;
; line: One-based line number, or 0 to select the current line.
; include_newline: Whether to also select the line terminator (`r`n).
;
EditFunc_SelectLine(line=0, include_newline=false, Control="", WinTitle="")
{
EditFunc_Standard_Params(Control, WinTitle)
ControlGet, hwnd, Hwnd,, %Control%, %WinTitle%
if (!WinExist("ahk_id " hwnd))
return false
if (line<1)
ControlGet, line, CurrentLine
SendMessage, 0xBB, line-1, 0 ; EM_LINEINDEX
offset := ErrorLevel
SendMessage, 0xC1, offset, 0 ; EM_LINELENGTH
lineLen := ErrorLevel
if (include_newline) {
WinGetClass, class
lineLen += (class="Edit") ? 2 : 1 ; `r`n : `n
}
; Select the line.
SendMessage, 0xB1, offset, offset+lineLen ; EM_SETSEL
EditFunc_SCROLLCARET(control,wintitle)
return (ErrorLevel != "FAIL")
}
; Deletes a line of text.
;
; line: One-based line number, or 0 to delete current line.
;
EditFunc_DeleteLine(line=0, Control="", WinTitle="")
{
EditFunc_Standard_Params(Control, WinTitle)
; Select the line.
if (EditFunc_SelectLine(line, true, Control, WinTitle))
{ ; Delete it.
ControlSend, %Control%, {Delete}, %WinTitle%
return true
}
return false
}
EditFunc_GetLine(line=0, Control="", WinTitle=""){
EditFunc_Standard_Params(Control, WinTitle)
if (line<1)
ControlGet, line, CurrentLine
controlget,thisLine,line,% line
return thisLine
}
EditFunc_GetFullLine(line=0, include_newline=false, control="",wintitle=""){
EditFunc_Standard_Params(Control, WinTitle)
line:=floor(line*1)
if (line<1)
ControlGet, line, CurrentLine,,% control,% wintitle
SendMessage, 0xBB, line-1, 0,% control,% wintitle ; EM_LINEINDEX
offset:= ErrorLevel
controlgettext,alltext,% control,% wintitle
count:=0
loop,parse,alltext,`n
maxCount:=a_index
loop,parse,allText,`n
{
count+=strlen(a_loopfield)+1
if (count>offset){
str:=a_loopfield "`n"
count:=a_index
break
}
}
if (count=maxCount || !include_newLine)
return RegExReplace(str,"[\r\n]","")
else
return str
controlget,lineCount,LineCount,,% control,% wintitle
controlget,thisLine,line,% line,% control,% wintitle
clipboard:=thisLine
if lineCount=1
return thisLine
while !regexmatch(thisLine,"(\n|\r)$") && (line+a_index<=lineCount) {
controlget,addLine,line,% line+a_index,% control,% wintitle
thisLine.=addLine
}
while (line-a_index>0) {
controlget,addLine,line,% line-a_index,% control,% wintitle
if !regexmatch(addLine,"(\n|\r)$")
thisLine:=addLine thisLine
else
break
}
return thisLine
}
EditFunc_InsertText(text,control="",wintitle=""){
EditFunc_Standard_Params(Control, WinTitle)
EditFunc_getSelection(selStart,selEnd,control,wintitle)
controlgettext,alltext,% control,% wintitle
controlsettext,% control,% substr(alltext,1,selStart) text substr(alltext,selEnd+1),% wintitle
EditFunc_select(selStart+strlen(text),selStart+strlen(text),control,wintitle)
}
EditFunc_SCROLLCARET(control="",wintitle=""){
SendMessage,0xB7,,,% control,% wintitle
}