My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
; Copyright (c) 2009 Charl P. Botha <http://cpbotha.net/>
; All rights reserved.

; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions
; are met:
; 1. Redistributions of source code must retain the above copyright
; notice, this list of conditions and the following disclaimer.
; 2. Redistributions in binary form must reproduce the above copyright
; notice, this list of conditions and the following disclaimer in the
; documentation and/or other materials provided with the distribution.
; 3. The name of the author may not be used to endorse or promote products
; derived from this software without specific prior written permission.

; THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
; OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
; IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
; NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
; THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


; todo:
; * if you invoke the path editor with F3 and apply, the cursor position
; in the main gui is lost.

; start of an envedit clone
; why am I still doing this?

; notes:
; you need to do
; win32gui.SendMessageTimeout(
; win32con.HWND_BROADCAST, win32con.WM_SETTINGCHANGE, 0,
; 'Environment', win32con.SMTO_ABORTIFHUNG, 100)
; to get new windows to recognise the new settings
; nearest I can get in AHK:
; SendMessage, 0x1A, 0, Environment,, ahk_id 0xFFFF
; with 0x1A being WM_SETTINGCHANGE
; 0 being wparam (as above)
; Environment being lparam (as above)
; ahk_id 0xFFFF being the broadcast
; above: flags includes ABORTIFHUNG, timeout is 100


version := "9.11.1"
window_title = envedit %version% Environment Editor (c) 2009 by Charl P. Botha

; new instances can be started up
#SingleInstance OFF

; functions =========================================================

; you need to pass string literals to this function
EnvToText(RootKey, Key)
{
; need to loop through whole registry and gather all keys and values
; append to variable and show in edit. This thing will loop through both
; keys and values separately
; string literals have been passed, so get to contents with %%
Loop, %RootKey%, %Key%, 1, 0
{
if a_LoopRegType = key
value =
else
{
RegRead, value
if ErrorLevel
value = *error*
}
; %a_LoopRegType% is e.g. REG_SZ
regline = %a_LoopRegName% = %value%
if regstr
regstr = %regstr%`r`n%regline%
else
regstr = %regline%
}

Sort, regstr
StringReplace, regstr, regstr, `r`n, `r`n`r`n, All

return regstr
}

; pass string literals as params
TextToEnv(Text, RootKey, Key)
{
; loop through text, building up list of keys that we can use to
; determine which keys from the registry should be deleted
alivekeys =
Loop, PARSE, Text, `n, `r
{
; if line is empty, skip on to next one
if A_LoopField =
continue

; also skip lines that have no equals
if not InStr(A_LoopField, "=")
continue

; else try to split it
StringSplit, keyvalue, A_LoopField, `=
if keyvalue1
{
; autotrim will remove leading / trailing spaces
akey = %keyvalue1%
StringUpper, akey, akey
avalue = %keyvalue2%

if alivekeys =
alivekeys = %akey%
else
alivekeys = %alivekeys%,%akey%
}
}
;MsgBox %alivekeys%

; now go through registry, deleting keys that are NOT in our list
; the registry loop goes in reverse order, neat huh?
Loop, %RootKey%, %Key%, 1, 0
{
; we don't want subkeys
if a_LoopRegType = key
continue

; if the current reg key is not in alivekeys, we kill it
StringUpper, A_LoopRegNameU, A_LoopRegName
if A_LoopRegNameU not in %alivekeys%
RegDelete
}

; loop through the text one last time, this time applying all values
Loop, PARSE, Text, `n, `r
{
; if line is empty, skip on to next one
if A_LoopField =
continue

; also skip lines that have no equals
if not InStr(A_LoopField, "=")
continue

; else try to split it
StringSplit, keyvalue, A_LoopField, `=
if keyvalue1
{
; autotrim will remove leading / trailing spaces
akey = %keyvalue1%
StringUpper, akey, akey
avalue = %keyvalue2%

IfInString, avalue, `%
rtype = REG_EXPAND_SZ
else
rtype = REG_SZ

RegWrite, %rtype%, %RootKey%, %Key%, %akey%, %avalue%
}
}

; notify everyone that we've changed the environment
SendMessage, 0x1A, 0, "Environment",, ahk_id 0xFFFF

; we should probably read back the environment to see what
; we've in fact done...
regstr := EnvToText(RootKey, Key)
GuiControl,,MainEdit,%regstr%
}


; get selection start char in c1 and end char in c2 for
; control Hwnd
GetSel(Hwnd, byref c1, byref c2)
{
; EM_GETSEL returns start and end char of selection. Even
; if there's no selection, both will be the caret.
; http://msdn.microsoft.com/en-us/library/bb775458(VS.85).aspx
VarSetCapacity(A1,4)
VarSetCapacity(A2,4)
SendMessage, 0xB0, &A1, &A2,, ahk_id %Hwnd%
c1 := *(&A1)+(*(&A1+1)<<8)+(*(&A1+2)<<16)+(*(&A1+3)<<24) ; Selection start
c2 := *(&A2)+(*(&A2+1)<<8)+(*(&A2+2)<<16)+(*(&A2+3)<<24)
}

SetSel(Hwnd, c1, c2)
{
SendMessage, 0xB1, %c1%, %c2%, , ahk_id %Hwnd%
}

; auto-execute section ==============================================
; until the first return. we use this to build the gui.


hkcustr := EnvToText("HKCU", "Environment")
; vMainEdit assigns edit control to var "MainEdit"
Gui, Add, Edit, w550 h300 vMainEdit -Wrap +HScroll
; also store Hwnd of edit control, we need this for some calls
GuiControlGet, Hwnd_MainEdit, Hwnd, MainEdit

Menu, FileMenu, Add, &Save to TXT File Ctrl+S, MenuFileSave
Menu, FileMenu, Add, &Load from TXT File Ctrl+O, MenuFileLoad
Menu, FileMenu, Add
Menu, FileMenu, Add, E&xit Ctrl+Q, GuiClose

Menu, EnvMenu, Add, S&ync with Environment (UNDO) F5, MenuEnvSync
Menu, EnvMenu, Add, &Apply to Environment F8, MenuEnvApply

Menu, ToolsMenu, Add, &Path edit current variable F3, MenuToolsPathEdit
Menu, ToolsMenu, Add, Insert &directory at cursor, MenuToolsInsDir
Menu, ToolsMenu, Add, Insert &file path at cursor, MenuToolsInsFilePath

Menu, HelpMenu, Add, &About, MenuHelpAbout

Menu, MyMenuBar, Add, &File, :FileMenu
Menu, MyMenuBar, Add, E&nvironment, :EnvMenu
Menu, MyMenuBar, Add, &Tools, :ToolsMenu
Menu, MyMenuBar, Add, &Help, :HelpMenu
Gui, Menu, MyMenuBar

Gui, Show,, %window_title%

; only set text here AFTER window has been shown
; else all text gets selected by default
GuiControl,,MainEdit, %hkcustr%

; going to use this later for setting up shortcut keys
GroupAdd, eeGroup, %window_title%

; already create path edit dialog, don't use yet:
Gui, 2: +owner
Gui, 2: Add, Edit, w400 h300 vPathEdit -Wrap +HScroll
GuiControlGet, Hwnd_PathEdit, 2: Hwnd, PathEdit
; and set the default selection
SetSel(Hwnd_PathEdit, 0,0)
Gui, 2: Add, Button,,Apply
Gui, 2: Add, Button,x+10,Cancel

Menu, peMenuBar, Add, &Alpha Sort, peMenuAlphaSort
Menu, peMenuBar, Add, Insert &Directory, peMenuInsDir
Menu, peMenuBar, Add, Insert &File Path, peMenuInsFilePath
Gui, 2: Menu, peMenuBar

Return

; event handlers ====================================================
#IfWinActive ahk_group eeGroup
^s::
MenuFileSave: ;------------------------------------------------------
Gui +OwnDialogs
FileSelectFile, SelectedFileName, S16,,Save File, Text Documents (*.txt)
if SelectedFileName =
return

IfExist %SelectedFileName%
{
FileDelete %SelectedFileName%
if ErrorLevel
{
MsgBox The attempt to overwrite "%SelectedFileName%" failed.
return
}
}
; this will by default get stuff from the control called MainEdit into a
; local variable MainEdit (confusing, I know)
GuiControlGet, MainEdit
FileAppend, %MainEdit%, %SelectedFileName% ; Save the contents to the file.

return

#IfWinActive ahk_group eeGroup
^o::
^l::
MenuFileLoad: ;------------------------------------------------------
Gui +OwnDialogs
FileSelectFile, SelectedFileName, 1,, Load File, Text Documents (*.txt)
if SelectedFileName =
return
FileRead, Contents, %SelectedFileName%
GuiControl,, MainEdit, %Contents%

return

#IfWinActive ahk_group eeGroup
F5::
MenuEnvSync: ;-------------------------------------------------------
hkcustr := EnvToText("HKCU", "Environment")
GuiControl,, MainEdit, %hkcustr%

return

#IfWinActive ahk_group eeGroup
F8::
MenuEnvApply: ;------------------------------------------------------
GuiControlGet, MainEdit
TextToEnv(MainEdit, "HKCU", "Environment")

return

#IfWinActive ahk_group eeGroup
F3::
MenuToolsPathEdit: ;-------------------------------------------------
; get current line from MainEdit control
ControlGet, CurLineN, CurrentLine, , , ahk_id %Hwnd_MainEdit%
ControlGet, CurLine, Line, %CurLineN%, , ahk_id %Hwnd_MainEdit%

; only process line if it has an "='
if not InStr(CurLine, "=")
return

; break the line into key-value array
StringSplit, keyvalue, CurLine, `=
if keyvalue1 =
return

; autotrim will remove leading / trailing spaces
CurKey = %keyvalue1%
value = %keyvalue2%

; variable is global, so have to zero
complist =
; the omitchars at the end make sure everything is stripped!
Loop, PARSE, value, `;, %A_Space%%A_Tab%
{
if complist =
complist = %A_LoopField%
else
complist = %complist%`n%A_LoopField%
}

Gui +disabled ; disable parent gui, we want modal
Gui, 2: Show,, Edit: %CurKey%

; default window is the main, we have to specify
; sub-window explicitly here
; we set the text AFTER the window has been shown, else
; all gets selected by default
GuiControl,2:,PathEdit, %complist%

return

MenuToolsInsDir: ;---------------------------------------------------
Gui +OwnDialogs
FileSelectFolder, TheFolder
if TheFolder =
return
; then use that to paste our text at the current cursor
Control, EditPaste, %TheFolder%, , ahk_id %Hwnd_MainEdit%
return

MenuToolsInsFilePath: ;----------------------------------------------
Gui +OwnDialogs

; have to restore this after the fileselect dialog, it nukes the selection
GetSel(Hwnd_MainEdit, c1, c2)

FileSelectFile, SelectedFileName,,, Load File, All (*.*)

; restore selection (somehow, fileselect dialog selects all text)
SetSel(Hwnd_MainEdit, c1, c2)

if SelectedFileName =
return

Control, EditPaste, %SelectedFileName%, , ahk_id %Hwnd_MainEdit%
return


MenuHelpAbout: ;-----------------------------------------------------
; owndialogs: dialogs are modal.
Gui, +OwnDialogs
MsgBox, 64, Help | About,
(
envedit %version% is copyright 2009 by Charl P. Botha <cpbotha.net>

Because the Windows environment editor will ruin your day, every time.

YOU ARE USING ENVEDIT ENTIRELY **AT YOUR OWN RISK**.
)
return

peMenuAlphaSort: ;---------------------------------------------------
; GUI 2 is already default here (they trigged us)
; get text from edit
GuiControlGet, PathEdit
; sort it
Sort, PathEdit
; put back in edit
GuiControl, , PathEdit, %PathEdit%
return

peMenuInsDir: ;------------------------------------------------------
; needs to be modal
Gui +OwnDialogs
FileSelectFolder, TheFolder
if TheFolder =
return
; then use that to paste our text at the current cursor
Control, EditPaste, %TheFolder%, , ahk_id %Hwnd_PathEdit%
return

peMenuInsFilePath: ;-------------------------------------------------
; Gui 2 should be default in this thread, make sure the file
; selection dialog is modal
Gui +OwnDialogs

; have to restore this after the fileselect dialog, it nukes the selection
GetSel(Hwnd_PathEdit, c1, c2)

FileSelectFile, SelectedFileName

; restore selection (somehow, fileselect dialog selects all text)
SetSel(Hwnd_PathEdit, c1, c2)

if SelectedFileName =
return
; then use that to paste our text at the current cursor
; hmmm, when this comes back from the fileselection dialog, all text
; is selected, which then gets overwrited by the new path.
; apparently default behaviour for edit controls: selection is inverted
; when they get focus. Doh.
Control, EditPaste, %SelectedFileName%, , ahk_id %Hwnd_PathEdit%
return

; automatic label: button "Apply" in 2nd GUI. just wow.
2ButtonApply: ;------------------------------------------------------
; concatenate line with ; instead of `n (use Loop)
; then do %CurKey% = concatenated_string
; then loop through current MainEdit text putting into new textvar
; when you reach %CurLineN%, do the new string
; copy the whole text into the MainEdit
; should we reset caret?
GuiControlGet, PathEdit
; loop through text, splitting on line ends
; `r in OmitChars means we work on all line-endings
; other two OmitChars strips leading and trailing space
NewValue =
Loop, Parse, PathEdit, `n, `r%A_Space%%A_Tab%
{
; only do stuff if there is stuff
if A_LoopField =
continue

if NewValue =
NewValue = %A_LoopField%
else
NewValue = %NewValue%`;%A_LoopField%
}
NewKeyValueString = %CurKey% = %NewValue%

; put this string back in the MainEdit control
; first get the current text
GuiControlGet, MainEdit, 1:
; now loop throuh it, replacing JUST the right string
NewText =
Loop, Parse, MainEdit, `n, `r
{
; HAVE to use these parentheses for the expression to get evaluated
if (A_Index = CurLineN)
TheLine = %NewKeyValueString%
else
TheLine = %A_LoopField%

if NewText =
NewText = %TheLine%
else
NewText = %NewText%`n%TheLine%
}

GuiControl,1:,MainEdit, %NewText%

GoSub, 2GuiClose
return

2ButtonCancel: ;-----------------------------------------------------
GoSub, 2GuiClose
return

; same here, automatic label
2GuiClose: ;---------------------------------------------------------
; in this thread, Gui 2 is the default, so we have to specify Gui 1 to
; get to the main window and re-enable it.
Gui 1: -Disabled
; put it away for later. hehe.
Gui 2: Hide
return

#IfWinActive ahk_group eeGroup
^q::
GuiClose: ;----------------------------------------------------------
ExitApp

Change log

r78 by cpbotha on Nov 17, 2009   Diff
added bug to the todo list
Go to: 
Project members, sign in to write a code review

Older revisions

r77 by cpbotha on Nov 16, 2009   Diff
small fixes
r76 by cpbotha on Nov 16, 2009   Diff
environment setting now works.  yay!
r75 by cpbotha on Nov 15, 2009   Diff
added notes on broadcast message to
notify all windows of change in env.
All revisions of this file

File info

Size: 14610 bytes, 493 lines
Powered by Google Project Hosting