This repository has been archived by the owner on Nov 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCodeSearch.ahk
148 lines (141 loc) · 3.59 KB
/
CodeSearch.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
/*
TODO:
- Add ability to double-click open a file to that line number
- Add progress bar
- Add right-click context menu
- Add option to open file location
- Add Anchor()
- Account for additional extensions
- Possibly add an extension manager?
- Find an icon
- Add pre-search checks (extension selection, directory)
- Add finished notification (statusbar?)
- Add auto saving of selected options and filters
*/
#Include Classes/Config.ahk
config := new Config()
Gui, Color, White
Gui, Font, s11, Segoe UI Light
Gui, Add, Text,, % "Initial Directory:"
Gui, Add, Edit, Section w300 h27 -Wrap vtxtInitialDirectory, % config.getValue("LastDir") ? config.getValue("LastDir") : ""
Gui, Add, Button, hp+1 w40 ys-1 gbtnDirectoryBrowse_Click vbtnDirectoryBrowse, % "..."
Gui, Add, Text, xm, % "String to search for:"
Gui, Add, Edit, Section w300 vtxtSearchString
Gui, Add, Button, ys-1 hp+1 vbtnSearch gbtnSearch_Click, % "Search"
Gui, Add, Checkbox, Section xm w30 h30 0x1000 vcbxRecurse, % "R"
Gui, Add, Checkbox, ys wp hp 0x1000 vcbxWholeWord, % "W"
Gui, Add, Checkbox, ys wp hp 0x1000 vcbxCase, % "C"
Gui, Add, GroupBox, ym w500 h120, % "File Types"
Gui, Add, Checkbox, yp+30 xp+15 Section vcbxAhk, % ".ahk"
Gui, Add, Checkbox, ys vcbxHtml, % ".html"
Gui, Add, Checkbox, ys vcbxCss, % ".css"
Gui, Add, Checkbox, ys vcbxJs, % ".js"
Gui, Add, Checkbox, ys vcbxIni, % ".ini"
Gui, Add, Checkbox, ys vcbxTxt, % ".txt"
Gui, Add, Text, xs, % "Additional extension (ex. xml,cs,aspx)"
Gui, Add, Edit, w300 vtxtAdditionalExtensions
Gui, Font, s11, Consolas
Gui, Add, ListView, xm w1000 r20 glvResults_Click vlvResults, % "File|Line Text|Line #|Position"
Gui, Show, AutoSize Center, Code Search
return
btnDirectoryBrowse_Click:
{
Gui, Submit, NoHide
FileSelectFolder, targetDir, *C:\, 3, % "Select a starting directory."
if (ErrorLevel) {
return
}
if (targetDir != "") {
GuiControl,, txtInitialDirectory, %targetDir%
config.setValue(targetDir, "LastDir")
}
return
}
btnSearch_Click:
{
Gui, Submit, NoHide
LV_Delete()
keyword := txtSearchString
extensions := getExtensions()
recurse := 0
if (cbxRecurse)
recurse := 1
SetWorkingDir, %txtInitialDirectory%
Loop, *.*,, %recurse%
{
if A_LoopFileAttrib contains H,S,R
continue
if A_LoopFileExt not in %extensions%
continue
file := A_LoopFileFullPath
Loop, Read, %file%
{
line := A_LoopReadLine
RegExMatch(line, getRegExOptions(cbxCase) getExpression(keyword, cbxWholeWord), obj)
if (obj.Len() > 0) {
LV_Add("", file, truncate(line), A_Index, obj.Pos())
;~ msgbox, % "Found a match on line: " A_Index "`nIn file: " file "`n`n" line
adjHdrs("lvResults")
}
}
}
return
}
lvResults_Click:
{
Gui, Submit, NoHide
dir := txtInitialDirectory
LV_GetText(fileName, A_EventInfo)
Run Edit %dir%\%fileName%
return
}
GuiClose:
{
ExitApp
}
adjHdrs(listView="") {
Gui, ListView, %listView%
Loop, % LV_GetCount("Col")
LV_ModifyCol(A_Index, "autoHdr")
LV_ModifyCol(1,"Integer Left")
return
}
truncate(s, c="50") {
if (StrLen(s) > c) {
return SubStr(s, 1, c) " (...)"
}
return s
}
getExtensions() {
global
e := ""
if (cbxAhk)
e := "ahk,"
if (cbxTxt)
e .= "txt,"
if (cbxIni)
e .= "ini,"
if (cbxHtml)
e .= "html,"
if (cbxCss)
e .= "css,"
if (cbxJs)
e .= "js,"
StringTrimRight, e, e, 1
return e
}
getExpression(keyword, wholeWord) {
if (wholeWord) {
expression := "[\s|\W]?" keyword "[\s|\W]"
} else {
expression := keyword
}
return expression
}
getRegExOptions(caseSense) {
options := "O" ; return regex match result as an object
if (!caseSense) {
options := options "i" ; case sensitive searching
}
return options ")"
}