-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathezMenu.ahk
217 lines (177 loc) · 6.02 KB
/
ezMenu.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
[script info]
version = 1.2.1
description = easy menu creation using indentation and markdown-like syntax
ahk version = 1.1.26.01
author = davebrny
source = https://github.com/davebrny/ezMenu
*/
ezMenu_init:
global menu_created, default_label, default_item, disabled_item, tab_width, s_tab
default_label := ""
tab_width = 4
loop, % tab_width
s_tab .= a_space
return
ezMenu(menu_name, string_or_file, modify_func="") {
goSub, ezMenu_init
if fileExist(string_or_file)
menu_text := ezMenu_get(string_or_file)
else menu_text := trim(string_or_file, "`n")
if (menu_text = "")
error_return("""" menu_name """ menu is empty")
if !inStr(menu_text, "`n") and inStr(menu_text, "|")
stringReplace, menu_text, menu_text, |, `n, all
if isFunc(modify_func)
menu_text := %modify_func%(menu_text)
loop, parse, menu_text, `n, `r
{
if a_loopfield is space
continue ; if whitespace or empty
if (inStr(LTrim(a_loopfield), ";") = 1)
continue ; if commented
line_text := a_loopfield
menu_level(line_text, level) ; (byRef: line_text, level)
error_check(line_text, level, a_index)
menu_add(menu_name, line_text, level)
}
menu_created := true
menu, % menu_name, show
menu, % menu_name, delete
}
ezMenu_get(filepath) {
fileRead, contents, % filepath
stringReplace, contents, contents, `r`n, `n, all
if (subStr(filepath, -8, 9) = ".menu.ahk")
{
if !inStr(contents, "[ezMenu]")
error_return("[ezMenu] not found")
else if !inStr(contents, "[ezMenu_end]")
error_return("[ezMenu_end] not found")
stringGetPos, pos, contents, [ezMenu], L1
stringMid, right_text, contents, pos + 9
stringGetPos, pos, right_text, [ezMenu_end], L1
stringMid, menu_text, right_text, pos, , L
}
else menu_text := contents
return trim(menu_text, "`n")
}
error_return(msg) {
msgBox, % msg
exit
}
menu_level(byRef line_text, byRef level) {
line_text := rTrim(line_text)
line_text := strReplace(line_text, a_tab, s_tab) ; replace tabs with spaces
replaced := line_text
line_text := LTrim(line_text)
white_space := strLen(replaced) - strLen(line_text) ; (whitespace count before text)
level := (white_space // tab_width) + 1 ; use floor divide to move incorrectly indented item up or down 1 level
}
error_check(line_text, level, line_number) {
static last_level
if (line_number = 1)
last_level := "0"
if (level > 21)
error_return("Error on menu line " line_number " `n" line_text " `n`n"
. "Maximum levels allowed: 21")
if (level > last_level) and (level > last_level + 1)
error_return("Error on menu line " line_number " `n" line_text " `n`n"
. "Item has no parent menu")
last_level := level
}
menu_add(menu_name, item_name, menu_level) {
if (menu_level = "")
menu_level := "1"
if (default_label = "")
default_label := menu_name
item_label := default_label
;# default menu item
if (inStr(LTrim(item_name), "*") = 1)
{
stringTrimLeft, item_name, item_name, 1
default_item := true
}
;# disable menu item
if (inStr(LTrim(LTrim(item_name, "*")), ".") = 1)
{
stringTrimLeft, item_name, item_name, 1
disabled_item := true
}
;# custom label action
item_name := LTrim(item_name)
if instr(item_name, "!")
{
loop, parse, % item_name, !, % a_space
{
if (a_index = 1)
continue
first_word := strSplit(a_loopField, a_space)
if isLabel(first_word[1])
found_label := first_word[1]
}
until (found_label)
if (found_label)
{
item_label := found_label
if inStr(item_name, "!" found_label "!") ; if setting a new global default
{
default_label := found_label
if (inStr(item_name, "!" found_label "!") = 1) ; if at start, then skip menu add
return
item_name := strReplace(item_name, "!" found_label "!", "")
}
else if inStr(item_name, "!" found_label) ; if setting a custom label for this line
item_name := strReplace(item_name, "!" found_label, "")
}
item_name := trim(item_name)
}
;# save value for later use
save_value(item_name, menu_level)
;# add item to menu
if (menu_level = 1)
{
if (trim(item_name) = "---")
menu(menu_name, separator)
last_menu := item_label
}
else if (menu_level > 1)
{
loop, ; loop down through levels
{
sub_item := stored_value("menu" menu_level)
parent_item := stored_value("menu" menu_level - 1)
item_action := (a_index = 1) ? (item_label) : (last_menu)
if (trim(item_name) = "---") and (menu_created != true)
menu(parent_item, separator)
else if (trim(item_name) != "---")
menu(parent_item, sub_item, item_action)
last_menu := ":" parent_item
--menu_level
}
until (menu_level = 1)
}
;# add final root level (level 1)
final_menu := stored_value("menu" menu_level)
if (trim(item_name) != "---")
menu(menu_name, final_menu, last_menu)
disabled_item := ""
item_label := ""
} ; end of menu_add()
save_value(item_name, menu_level) {
global
menu%menu_level% := item_name
}
stored_value(name) {
stored_value := %name%
return stored_value
}
menu(menu, item="", action="") {
menu, % menu, add, % item, % action
if (disabled_item = true)
menu, % menu, disable, % item
if (default_item = true)
menu, % menu, default, % item
disabled_item := ""
default_item := ""
}