forked from gphilippot/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GrepWindow.pb
615 lines (459 loc) · 19.5 KB
/
GrepWindow.pb
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
;--------------------------------------------------------------------------------------------
; Copyright (c) Fantaisie Software. All rights reserved.
; Dual licensed under the GPL and Fantaisie Software licenses.
; See LICENSE and LICENSE-FANTAISIE in the project root for license information.
;--------------------------------------------------------------------------------------------
#WINDOW_Grep_Width = 480
#WINDOW_Grep_Height = 235
Global GrepAbort
; Procedure CheckGrepAbort()
;
; Repeat
; Event = WindowEvent()
;
; CompilerIf #CompileWindows
; CompilerEvents(Event)
; CompilerEndIf
;
; If EventWindow() = #WINDOW_Grep
; If Event = #PB_EventGadget And EventGadget() = #GADGET_Grep_Stop
; Result = 1
; EndIf
; EndIf
;
; Until Event = 0
;
; ProcedureReturn Result
;
; EndProcedure
Procedure UpdateFindComboBox(GadgetID) ; Works the same for find & replace combo
Text$ = GetGadgetText(GadgetID)
If Text$
For k=1 To CountGadgetItems(GadgetID)
If GetGadgetItemText(GadgetID, k-1, 0) = Text$
RemoveGadgetItem(GadgetID, k-1)
Break
EndIf
Next
AddGadgetItem(GadgetID, 0, Text$) ; Insert at the top of the list
EndIf
While CountGadgetItems(GadgetID) > FindHistorySize
RemoveGadgetItem(GadgetID, CountGadgetItems(GadgetID)-1)
Wend
SetGadgetText(GadgetID, Text$)
EndProcedure
Procedure DisableGrepWindow(State)
DisableGadget(#GADGET_Grep_FindWord, State)
DisableGadget(#GADGET_Grep_Directory, State)
DisableGadget(#GADGET_Grep_SelectDirectory, State)
DisableGadget(#GADGET_Grep_Pattern, State)
DisableGadget(#GADGET_Grep_MatchCase, State)
DisableGadget(#GADGET_Grep_Recurse, State)
DisableGadget(#GADGET_Grep_WholeWord, State)
DisableGadget(#GADGET_Grep_NoComments, State)
DisableGadget(#GADGET_Grep_NoStrings, State)
DisableGadget(#GADGET_Grep_Cancel, State)
DisableGadget(#GADGET_Grep_Find, State)
If State
DisableGadget(#GADGET_Grep_Stop, 0)
Else
DisableGadget(#GADGET_Grep_Stop, 1)
EndIf
EndProcedure
Procedure SearchStringInFile(FileID, Filename$, String$, InitialPathLength)
Quit = 0
If ReadFile(FileID, Filename$)
StringMode = ReadStringFormat(FileID) ; try to detect UTF8 bom
FileLength = Lof(FileID) - Loc(FileID) ; subtract bom size
StringLength = Len(String$)
BinaryCount = 0 ; with more than 10 nontext chars, we consider it binary
If FileLength And StringLength
*BufferStart = AllocateMemory(FileLength+10)
If *BufferStart
ReadData(FileID, *BufferStart, FileLength)
Line = 1
*Buffer.Ascii = *BufferStart
*BufferEnd = *Buffer+FileLength
*LineStart = *BufferStart
*NextAbortChecked = *Buffer + 50000
If StringMode = #PB_UTF8
*String = StringToUTF8(String$)
StringLength = StringByteLength(String$, #PB_UTF8)
Else
*String = StringToAscii(String$)
EndIf
While *Buffer < *BufferEnd
If *Buffer >= *NextAbortCheck
FlushEvents()
If GrepAbort
Quit = 1
Break
EndIf
*NextAbortChecked = *Buffer + 50000
EndIf
If CompareMemoryString(*Buffer, *String, 1-GrepCaseSensitive, StringLength, StringMode) = 0 ; we always need the flag now for UTF8 files support
If GrepWholeWord = 0 Or ((*Buffer = *LineStart Or ValidCharacters(PeekA(*Buffer-1)) = 0) And (*Buffer+StringLength = *BufferEnd Or ValidCharacters(PeekB(*Buffer+StringLength) & $FF) = 0) )
If BinaryCount > 10 ; its a binary file
LogLine$ = Filename$+": " + Language("Find","BinaryFile") + " (" + Str(*Buffer-*BufferStart) + ")"
*Buffer+1 ; move on!
Else
LineLength = 0
; Now, go to the end of line to isolate it
;
*Buffer = *LineStart
While *Buffer < *BufferEnd And *Buffer\a <> 13 And *Buffer\a <> 10
LineLength+1
*Buffer+1
Wend
; Note: we don't include the Chr(13) or Chr(10), it will be processed again in the next loop
;
*Buffer = *LineStart ; Reset the buffer to the start of the line, and add the line length
*Buffer + LineLength
; LogLine$ = Right(Filename$, Len(Filename$)-InitialPathLength-1)+": "+Str(Line)+" - "+PeekS(*LineStart, LineLength)
LogLine$ = Filename$+": "+Str(Line)+" - "+ReplaceString(PeekS(*LineStart, LineLength, StringMode), Chr(9), " ") ; Replace all Tab with 2 spaces
EndIf
AddGadgetItem(#GADGET_GrepOutput_List, -1, LogLine$)
NbGrepFiles+1
Else
*Buffer+1 ; if WholeWord case is not met, move on!
EndIf
ElseIf GrepNoComments And *Buffer\a = ';' And BinaryCount < 10
While *Buffer\a And *Buffer\a <> 13 And *Buffer\a <> 10 ; just skip the line
*Buffer+1
Wend
ElseIf GrepNoStrings And *Buffer\a = '"' And BinaryCount < 10
*Buffer + 1
While *Buffer\a And *Buffer\a <> 13 And *Buffer\a <> 10 And *Buffer\a <> '"' ; just skip to the next " or line end
*Buffer+1
Wend
If *Buffer\a = '"' ; otherwise there is an endless loop
*Buffer+1
EndIf
ElseIf GrepNoStrings And *Buffer\a = '~' And PeekA(*Buffer + 1) = '"'
*Buffer + 2
While *Buffer\a And *Buffer\a <> 13 And *Buffer\a <> 10 And *Buffer\a <> '"'
If *Buffer\a = '\'
*Buffer + 1 ; skip \
If *Buffer\a And *Buffer\a <> 13 And *Buffer\a <> 10
*Buffer + 1 ; skip escaped char if this is not the end of the line
EndIf
Else
*Buffer+1
EndIf
Wend
If *Buffer\a = '"' ; otherwise there is an endless loop
*Buffer+1
EndIf
ElseIf *Buffer\a = 10 ; Unix files..
Line+1
*Buffer+1
*LineStart = *Buffer
ElseIf *Buffer\a = 13 ; DOS compatible file line ending
Line+1
*Buffer+1
If *Buffer\a = 10 ; Test is wrong, not a CRLF file
*Buffer+1
EndIf
*LineStart = *Buffer
Else
; check for binary bytes
If *Buffer\a < 32 And *Buffer\a <> 9 ; newline chars already checked
BinaryCount + 1
EndIf
*Buffer+1 ; Default case, just go on the next byte
EndIf
Wend
FreeMemory(*String)
FreeMemory(*BufferStart)
EndIf
EndIf
CloseFile(FileID)
EndIf
ProcedureReturn Quit
EndProcedure
Procedure Grep(DirectoryID, Directory$, String$, PatternList$, InitialPathLength)
Shared Grep_BaseDirectory$
; First, check only directories if recursing is on
;
If GrepRecurse
If ExamineDirectory(DirectoryID, Directory$, "")
While NextDirectoryEntry(DirectoryID) And GrepAbort = 0
FlushEvents() ; handle window and stop events
If DirectoryEntryType(DirectoryID) = 2 And DirectoryEntryName(DirectoryID) <> ".." And DirectoryEntryName(DirectoryID) <> "."
Grep(DirectoryID+1, Directory$+DirectoryEntryName(DirectoryID)+#Separator, String$, PatternList$, InitialPathLength)
EndIf
Wend
FinishDirectory(DirectoryID)
EndIf
EndIf
If GrepAbort
ProcedureReturn
EndIf
; Now check all the Patterns (if any)
; We need to keep track of already seen filenames, as one might match multiple patterns!
;
NewList SearchedFiles.s()
If Trim(PatternList$) = ""
PatternList$ = "*.*"
EndIf
Index = 1
Repeat
Pattern$ = Trim(StringField(PatternList$, Index, ","))
If Pattern$
If ExamineDirectory(DirectoryID, Directory$, Pattern$)
While NextDirectoryEntry(DirectoryID) And GrepAbort = 0
FlushEvents() ; handle window and stop events
If DirectoryEntryType(DirectoryID) = 1
Filename$ = DirectoryEntryName(DirectoryID)
Found = 0
ForEach SearchedFiles()
Searched$ = SearchedFiles() ; NOTE: @SearchedFiles() no longer returns the string pointer !?
If IsEqualFile(Searched$, FileName$)
Found = 1
Break
EndIf
Next SearchedFiles()
If Found = 0
AddElement(SearchedFiles())
SearchedFiles() = FileName$
SetGadgetText(#GADGET_GrepOutput_Current, CreateRelativePath(Grep_BaseDirectory$, Directory$+FileName$))
SearchStringInFile(#FILE_Grep, Directory$+Filename$, String$, InitialPathLength)
EndIf
EndIf
Wend
FinishDirectory(DirectoryID)
EndIf
EndIf
Index + 1
Until GrepAbort Or Pattern$ = "" Or Pattern$ = "*" Or Pattern$ = "*.*" ; we also abort on these patterns, as then all files are scanned
ProcedureReturn GrepAbort
EndProcedure
Procedure OpenGrepOutputWindow()
If IsWindow(#WINDOW_GrepOutput) = 0
GrepOutputDialog = OpenDialog(?Dialog_GrepOutput, WindowID(#WINDOW_Main), @GrepOutputPosition)
EnsureWindowOnDesktop(#WINDOW_GrepOutput)
Else
SetWindowForeground(#WINDOW_GrepOutput)
EndIf
EndProcedure
Procedure GrepOutputWindowEvents(EventID)
If EventID = #PB_Event_Menu ; Little wrapper to map the shortcut events (identified as menu)
EventID = #PB_Event_Gadget ; to normal gadget events...
GadgetID = EventMenu()
Else
GadgetID = EventGadget()
EndIf
Select EventID
Case #PB_Event_CloseWindow
Quit = 1
Case #PB_Event_SizeWindow
GrepOutputDialog\SizeUpdate()
Case #PB_Event_Gadget
Select GadgetID
Case #GADGET_GrepOutput_List
If EventType() = #PB_EventType_LeftDoubleClick
Line$ = GetGadgetItemText(#GADGET_GrepOutput_List, GetGadgetState(#GADGET_GrepOutput_List), 0)
Filename$ = StringField(Line$, 1, ":")
If Len(Filename$) = 1 ; Probably only a letter of drive like 'C:'
Filename$ = Filename$+":"+StringField(Line$, 2, ":")
EndIf
If Filename$ And Asc(Filename$) <> ';' ; Not '; Search starting' or finished stuff
If LoadSourceFile(FileName$)
LineNumber = Val(StringField(Right(Line$, Len(Line$)-Len(Filename$)-2), 1, " "))
ChangeActiveLine(LineNumber, -5)
EndIf
EndIf
EndIf
Case #GADGET_GrepOutput_Clear
ClearGadgetItems(#GADGET_GrepOutput_List)
Case #GADGET_GrepOutput_Close
Quit = 1
EndSelect
EndSelect
If Quit
; abort any ongoing search
GrepAbort = 1
If MemorizeWindow
GrepOutputDialog\Close(@GrepOutputPosition)
Else
GrepOutputDialog\Close()
EndIf
GrepOutputDialog = 0
EndIf
EndProcedure
Procedure OpenGrepWindow()
If IsWindow(#WINDOW_Grep) = 0
GrepWindowDialog = OpenDialog(?Dialog_Grep, WindowID(#WINDOW_Main), @GrepWindowPosition)
If GrepWindowDialog
EnsureWindowOnDesktop(#WINDOW_Grep)
AddKeyboardShortcut(#WINDOW_Grep, #PB_Shortcut_Return, #GADGET_Grep_Find)
AddKeyboardShortcut(#WINDOW_Grep, #PB_Shortcut_Escape, #GADGET_Grep_Cancel)
EnableGadgetDrop(#GADGET_Grep_Directory, #PB_Drop_Files, #PB_Drag_Copy)
For i = 1 To FindHistorySize
If GrepFindHistory(i) <> ""
AddGadgetItem(#GADGET_Grep_FindWord, -1, GrepFindHistory(i))
EndIf
If GrepDirectoryHistory(i) <> ""
AddGadgetItem(#GADGET_Grep_Directory, -1, GrepDirectoryHistory(i))
EndIf
If GrepExtensionHistory(i) <> ""
AddGadgetItem(#GADGET_Grep_Pattern, -1, GrepExtensionHistory(i))
EndIf
Next i
If CountGadgetItems(#GADGET_Grep_Pattern) = 0
AddGadgetItem(#GADGET_Grep_Pattern, -1, "*" + #SourceFileExtension) ; Default to *.pb files, else it will find nothing
EndIf
SetGadgetState(#GADGET_Grep_Pattern, 0)
SetGadgetState(#GADGET_Grep_Directory, 0)
SetGadgetState(#GADGET_Grep_MatchCase, GrepCaseSensitive)
SetGadgetState(#GADGET_Grep_WholeWord, GrepWholeWord)
SetGadgetState(#GADGET_Grep_Recurse, GrepRecurse)
SetGadgetState(#GADGET_Grep_NoComments, GrepNoComments)
SetGadgetState(#GADGET_Grep_NoStrings, GrepNoStrings)
SetGadgetState(#GADGET_Grep_FindWord, 0)
GetSelection(@LineStart, @RowStart, @LineEnd, @RowEnd)
If LineStart = LineEnd And RowStart <> RowEnd
; display the default selection in the box
Line$ = Mid(GetLine(LineStart-1), RowStart, RowEnd-RowStart)
SetGadgetText(#GADGET_Grep_FindWord, Line$)
EndIf
DisableGrepWindow(0)
HideWindow(#WINDOW_Grep, 0)
EndIf
Else
SetWindowForeground(#WINDOW_Grep)
EndIf
SelectComboBoxText(#GADGET_Grep_FindWord)
SetActiveGadget(#GADGET_Grep_FindWord)
EndProcedure
Procedure UpdateGrepWindow()
GrepWindowDialog\LanguageUpdate()
While FindHistorySize < CountGadgetItems(#GADGET_Grep_FindWord)
RemoveGadgetItem(#GADGET_Grep_FindWord, CountGadgetItems(#GADGET_Grep_FindWord)-1)
Wend
While FindHistorySize < CountGadgetItems(#GADGET_Grep_Directory)
RemoveGadgetItem(#GADGET_Grep_Directory, CountGadgetItems(#GADGET_Grep_Directory)-1)
Wend
While FindHistorySize < CountGadgetItems(#GADGET_Grep_Pattern)
RemoveGadgetItem(#GADGET_Grep_Pattern, CountGadgetItems(#GADGET_Grep_Pattern)-1)
Wend
GrepWindowDialog\GuiUpdate()
EndProcedure
Procedure GrepWindowEvents(EventID)
Shared Grep_BaseDirectory$
If EventID = #PB_Event_Menu ; Little wrapper to map the shortcut events (identified as menu)
EventID = #PB_Event_Gadget ; to normal gadget events...
GadgetID = EventMenu()
Else
GadgetID = EventGadget()
EndIf
Select EventID
Case #PB_Event_CloseWindow
Quit = 1
Case #PB_Event_GadgetDrop
If GadgetID = #GADGET_Grep_Directory
Path$ = StringField(EventDropFiles(), 1, Chr(10))
If FileSize(Path$) <> -2 ; probably a file then
Path$ = GetPathPart(Path$)
EndIf
SetGadgetText(#GADGET_Grep_Directory, Path$)
EndIf
Case #PB_Event_Gadget
Select GadgetID
Case #GADGET_Grep_SelectDirectory
Directory$ = PathRequester("", GetGadgetText(#GADGET_Grep_Directory))
If Directory$
SetGadgetText(#GADGET_Grep_Directory, Directory$)
EndIf
Case #GADGET_Grep_UseCurrentDirectory
If *ActiveSource = *ProjectInfo
Directory$ = GetPathPart(ProjectFile$)
ElseIf *ActiveSource And *ActiveSource\FileName$ <> ""
Directory$ = GetPathPart(*ActiveSource\FileName$)
Else
Directory$ = GetCurrentDirectory()
EndIf
SetGadgetText(#GADGET_Grep_Directory, Directory$)
Case #GADGET_Grep_Find
Directory$ = GetGadgetText(#GADGET_Grep_Directory)
If Directory$
If Right(Directory$, 1) <> #Separator
Directory$ + #Separator
EndIf
String$ = GetGadgetText(#GADGET_Grep_FindWord)
If String$
DisableGrepWindow(#True)
OpenGrepOutputWindow()
NbGrepFiles = 0
; get checkbox choices
GrepCaseSensitive = GetGadgetState(#GADGET_Grep_MatchCase)
GrepWholeWord = GetGadgetState(#GADGET_Grep_WholeWord)
GrepNoComments = GetGadgetState(#GADGET_Grep_NoComments)
GrepNoStrings = GetGadgetState(#GADGET_Grep_NoStrings)
GrepRecurse = GetGadgetState(#GADGET_Grep_Recurse)
AddGadgetItem(#GADGET_GrepOutput_List, -1, "; "+Language("Find","Started")+". '"+String$+"'...")
Grep_BaseDirectory$ = Directory$
GrepAbort = 0
UpdateFindComboBox(#Gadget_Grep_FindWord)
UpdateFindComboBox(#Gadget_Grep_Directory)
UpdateFindComboBox(#Gadget_Grep_Pattern)
If Grep(0, Directory$, String$, GetGadgetText(#GADGET_Grep_Pattern)+",", Len(Directory$))
If IsWindow(#WINDOW_GrepOutput) ; the window could have been closed!
AddGadgetItem(#GADGET_GrepOutput_List, -1, "; "+Language("Find","Aborted")+". "+Str(NbGrepFiles)+" "+Language("Find","LinesFound")+".")
EndIf
Else
AddGadgetItem(#GADGET_GrepOutput_List, -1, "; "+Language("Find","Finished")+". "+Str(NbGrepFiles)+" "+Language("Find","LinesFound")+".")
EndIf
If IsGadget(#GADGET_GrepOutput_Current) ; the window could have been closed!
SetGadgetText(#GADGET_GrepOutput_Current, "")
EndIf
DisableGrepWindow(#False)
Else
MessageRequester(Language("Find","Info"), Language("Find","NeedString")+".", #FLAG_WARNING)
SetActiveGadget(#GADGET_Grep_FindWord)
EndIf
Else
MessageRequester(Language("Find","Info"), Language("Find","NeedPath")+".", #FLAG_WARNING)
SetActiveGadget(#GADGET_Grep_Directory)
EndIf
Case #GADGET_Grep_Cancel
Quit = 1
; the stop event now ends up here too, as the events are dispatched from the Grep function.
Case #GADGET_Grep_Stop
GrepAbort = 1
EndSelect
EndSelect
If Quit
; abort any ongoing search
GrepAbort = 1
; get checkbox choices
GrepCaseSensitive = GetGadgetState(#GADGET_Grep_MatchCase)
GrepWholeWord = GetGadgetState(#GADGET_Grep_WholeWord)
GrepNoComments = GetGadgetState(#GADGET_Grep_NoComments)
GrepNoStrings = GetGadgetState(#GADGET_Grep_NoStrings)
GrepRecurse = GetGadgetState(#GADGET_Grep_Recurse)
; save patterns
For i = 1 To FindHistorySize
If CountGadgetItems(#GADGET_Grep_FindWord) >= i
GrepFindHistory(i) = GetGadgetItemText(#GADGET_Grep_FindWord, i-1, 0)
Else
GrepFindHistory(i) = ""
EndIf
If CountGadgetItems(#GADGET_Grep_Directory) >= i
GrepDirectoryHistory(i) = GetGadgetItemText(#GADGET_Grep_Directory, i-1, 0)
Else
GrepDirectoryHistory(i) = ""
EndIf
If CountGadgetItems(#GADGET_Grep_Pattern) >= i
GrepExtensionHistory(i) = GetGadgetItemText(#GADGET_Grep_Pattern, i-1, 0)
Else
GrepExtensionHistory(i) = ""
EndIf
Next i
If MemorizeWindow
GrepWindowDialog\Close(@GrepWindowPosition)
Else
GrepWindowDialog\Close()
EndIf
EndIf
EndProcedure