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
 
 
 
 
 
 
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
# find.py - Find and Replace
import afxres
import win32api
import win32con
import win32ui
from pywin.framework import scriptutils
from pywin.mfc import dialog

FOUND_NOTHING = 0
FOUND_NORMAL = 1
FOUND_LOOPED_BACK = 2
FOUND_NEXT_FILE = 3


class SearchParams:
    def __init__(self, other=None):
        if other is None:
            self.__dict__["findText"] = ""
            self.__dict__["replaceText"] = ""
            self.__dict__["matchCase"] = 0
            self.__dict__["matchWords"] = 0
            self.__dict__["acrossFiles"] = 0
            self.__dict__["remember"] = 1
            self.__dict__["sel"] = (-1, -1)
            self.__dict__["keepDialogOpen"] = 0
        else:
            self.__dict__.update(other.__dict__)

    # Helper so we cant misspell attributes :-)
    def __setattr__(self, attr, val):
        if not hasattr(self, attr):
            raise AttributeError(attr)
        self.__dict__[attr] = val


curDialog = None
lastSearch = defaultSearch = SearchParams()
searchHistory = []


def ShowFindDialog():
    _ShowDialog(FindDialog)


def ShowReplaceDialog():
    _ShowDialog(ReplaceDialog)


def _ShowDialog(dlgClass):
    global curDialog
    if curDialog is not None:
        if curDialog.__class__ != dlgClass:
            curDialog.DestroyWindow()
            curDialog = None
        else:
            curDialog.SetFocus()
    if curDialog is None:
        curDialog = dlgClass()
        curDialog.CreateWindow()


def FindNext():
    params = SearchParams(lastSearch)
    params.sel = (-1, -1)
    if not params.findText:
        ShowFindDialog()
    else:
        return _FindIt(None, params)


def _GetControl(control=None):
    if control is None:
        control = scriptutils.GetActiveEditControl()
    return control


def _FindIt(control, searchParams):
    global lastSearch, defaultSearch
    control = _GetControl(control)
    if control is None:
        return FOUND_NOTHING

    # Move to the next char, so we find the next one.
    flags = 0
    if searchParams.matchWords:
        flags = flags | win32con.FR_WHOLEWORD
    if searchParams.matchCase:
        flags = flags | win32con.FR_MATCHCASE
    if searchParams.sel == (-1, -1):
        sel = control.GetSel()
        # If the position is the same as we found last time,
        # then we assume it is a "FindNext"
        if sel == lastSearch.sel:
            sel = sel[0] + 1, sel[0] + 1
    else:
        sel = searchParams.sel

    if sel[0] == sel[1]:
        sel = sel[0], control.GetTextLength()

    rc = FOUND_NOTHING
    # (Old edit control will fail here!)
    posFind, foundSel = control.FindText(flags, sel, searchParams.findText)
    lastSearch = SearchParams(searchParams)
    if posFind >= 0:
        rc = FOUND_NORMAL
        lineno = control.LineFromChar(posFind)
        control.SCIEnsureVisible(lineno)
        control.SetSel(foundSel)
        control.SetFocus()
        win32ui.SetStatusText(win32ui.LoadString(afxres.AFX_IDS_IDLEMESSAGE))
    if rc == FOUND_NOTHING and lastSearch.acrossFiles:
        # Loop around all documents.  First find this document.
        try:
            try:
                doc = control.GetDocument()
            except AttributeError:
                try:
                    doc = control.GetParent().GetDocument()
                except AttributeError:
                    print("Cant find a document for the control!")
                    doc = None
            if doc is not None:
                template = doc.GetDocTemplate()
                alldocs = template.GetDocumentList()
                mypos = lookpos = alldocs.index(doc)
                while 1:
                    lookpos = (lookpos + 1) % len(alldocs)
                    if lookpos == mypos:
                        break
                    view = alldocs[lookpos].GetFirstView()
                    posFind, foundSel = view.FindText(
                        flags, (0, view.GetTextLength()), searchParams.findText
                    )
                    if posFind >= 0:
                        nChars = foundSel[1] - foundSel[0]
                        lineNo = view.LineFromChar(posFind)  # zero based.
                        lineStart = view.LineIndex(lineNo)
                        colNo = posFind - lineStart  # zero based.
                        scriptutils.JumpToDocument(
                            alldocs[lookpos].GetPathName(),
                            lineNo + 1,
                            colNo + 1,
                            nChars,
                        )
                        rc = FOUND_NEXT_FILE
                        break
        except win32ui.error:
            pass
    if rc == FOUND_NOTHING:
        # Loop around this control - attempt to find from the start of the control.
        posFind, foundSel = control.FindText(
            flags, (0, sel[0] - 1), searchParams.findText
        )
        if posFind >= 0:
            control.SCIEnsureVisible(control.LineFromChar(foundSel[0]))
            control.SetSel(foundSel)
            control.SetFocus()
            win32ui.SetStatusText("Not found! Searching from the top of the file.")
            rc = FOUND_LOOPED_BACK
        else:
            lastSearch.sel = -1, -1
            win32ui.SetStatusText("Can not find '%s'" % searchParams.findText)

    if rc != FOUND_NOTHING:
        lastSearch.sel = foundSel

    if lastSearch.remember:
        defaultSearch = lastSearch

        # track search history
        try:
            ix = searchHistory.index(searchParams.findText)
        except ValueError:
            if len(searchHistory) > 50:
                searchHistory[50:] = []
        else:
            del searchHistory[ix]
        searchHistory.insert(0, searchParams.findText)

    return rc


def _ReplaceIt(control):
    control = _GetControl(control)
    statusText = "Can not find '%s'." % lastSearch.findText
    rc = FOUND_NOTHING
    if control is not None and lastSearch.sel != (-1, -1):
        control.ReplaceSel(lastSearch.replaceText)
        rc = FindNext()
        if rc != FOUND_NOTHING:
            statusText = win32ui.LoadString(afxres.AFX_IDS_IDLEMESSAGE)
    win32ui.SetStatusText(statusText)
    return rc


class FindReplaceDialog(dialog.Dialog):
    def __init__(self):
        dialog.Dialog.__init__(self, self._GetDialogTemplate())
        self.HookCommand(self.OnFindNext, 109)

    def OnInitDialog(self):
        self.editFindText = self.GetDlgItem(102)
        self.butMatchWords = self.GetDlgItem(105)
        self.butMatchCase = self.GetDlgItem(107)
        self.butKeepDialogOpen = self.GetDlgItem(115)
        self.butAcrossFiles = self.GetDlgItem(116)
        self.butRemember = self.GetDlgItem(117)

        self.editFindText.SetWindowText(defaultSearch.findText)
        control = _GetControl()
        # _GetControl only gets normal MDI windows; if the interactive
        # window is docked and no document open, we get None.
        if control:
            # If we have a selection, default to that.
            sel = control.GetSelText()
            if len(sel) != 0:
                self.editFindText.SetWindowText(sel)
                if defaultSearch.remember:
                    defaultSearch.findText = sel
        for hist in searchHistory:
            self.editFindText.AddString(hist)

        if hasattr(self.editFindText, "SetEditSel"):
            self.editFindText.SetEditSel(0, -1)
        else:
            self.editFindText.SetSel(0, -1)
        self.butMatchWords.SetCheck(defaultSearch.matchWords)
        self.butMatchCase.SetCheck(defaultSearch.matchCase)
        self.butKeepDialogOpen.SetCheck(defaultSearch.keepDialogOpen)
        self.butAcrossFiles.SetCheck(defaultSearch.acrossFiles)
        self.butRemember.SetCheck(defaultSearch.remember)
        return dialog.Dialog.OnInitDialog(self)

    def OnDestroy(self, msg):
        global curDialog
        curDialog = None
        return dialog.Dialog.OnDestroy(self, msg)

    def DoFindNext(self):
        params = SearchParams()
        params.findText = self.editFindText.GetWindowText()
        params.matchCase = self.butMatchCase.GetCheck()
        params.matchWords = self.butMatchWords.GetCheck()
        params.acrossFiles = self.butAcrossFiles.GetCheck()
        params.remember = self.butRemember.GetCheck()
        return _FindIt(None, params)

    def OnFindNext(self, id, code):
        if code != 0:  # BN_CLICKED
            # 3d controls (python.exe + start_pythonwin.pyw) send
            # other notification codes
            return 1  #
        if not self.editFindText.GetWindowText():
            win32api.MessageBeep()
            return 1
        if self.DoFindNext() != FOUND_NOTHING:
            if not self.butKeepDialogOpen.GetCheck():
                self.DestroyWindow()


class FindDialog(FindReplaceDialog):
    def _GetDialogTemplate(self):
        style = (
            win32con.DS_MODALFRAME
            | win32con.WS_POPUP
            | win32con.WS_VISIBLE
            | win32con.WS_CAPTION
            | win32con.WS_SYSMENU
            | win32con.DS_SETFONT
        )
        visible = win32con.WS_CHILD | win32con.WS_VISIBLE
        dt = [
            ["Find", (0, 2, 240, 75), style, None, (8, "MS Sans Serif")],
            ["Static", "Fi&nd What:", 101, (5, 8, 40, 10), visible],
            [
                "ComboBox",
                "",
                102,
                (50, 7, 120, 120),
                visible
                | win32con.WS_BORDER
                | win32con.WS_TABSTOP
                | win32con.WS_VSCROLL
                | win32con.CBS_DROPDOWN
                | win32con.CBS_AUTOHSCROLL,
            ],
            [
                "Button",
                "Match &whole word only",
                105,
                (5, 23, 100, 10),
                visible | win32con.BS_AUTOCHECKBOX | win32con.WS_TABSTOP,
            ],
            [
                "Button",
                "Match &case",
                107,
                (5, 33, 100, 10),
                visible | win32con.BS_AUTOCHECKBOX | win32con.WS_TABSTOP,
            ],
            [
                "Button",
                "Keep &dialog open",
                115,
                (5, 43, 100, 10),
                visible | win32con.BS_AUTOCHECKBOX | win32con.WS_TABSTOP,
            ],
            [
                "Button",
                "Across &open files",
                116,
                (5, 52, 100, 10),
                visible | win32con.BS_AUTOCHECKBOX | win32con.WS_TABSTOP,
            ],
            [
                "Button",
                "&Remember as default search",
                117,
                (5, 61, 150, 10),
                visible | win32con.BS_AUTOCHECKBOX | win32con.WS_TABSTOP,
            ],
            [
                "Button",
                "&Find Next",
                109,
                (185, 5, 50, 14),
                visible | win32con.BS_DEFPUSHBUTTON | win32con.WS_TABSTOP,
            ],
            [
                "Button",
                "Cancel",
                win32con.IDCANCEL,
                (185, 23, 50, 14),
                visible | win32con.WS_TABSTOP,
            ],
        ]
        return dt


class ReplaceDialog(FindReplaceDialog):
    def _GetDialogTemplate(self):
        style = (
            win32con.DS_MODALFRAME
            | win32con.WS_POPUP
            | win32con.WS_VISIBLE
            | win32con.WS_CAPTION
            | win32con.WS_SYSMENU
            | win32con.DS_SETFONT
        )
        visible = win32con.WS_CHILD | win32con.WS_VISIBLE
        dt = [
            ["Replace", (0, 2, 240, 95), style, 0, (8, "MS Sans Serif")],
            ["Static", "Fi&nd What:", 101, (5, 8, 40, 10), visible],
            [
                "ComboBox",
                "",
                102,
                (60, 7, 110, 120),
                visible
                | win32con.WS_BORDER
                | win32con.WS_TABSTOP
                | win32con.WS_VSCROLL
                | win32con.CBS_DROPDOWN
                | win32con.CBS_AUTOHSCROLL,
            ],
            ["Static", "Re&place with:", 103, (5, 25, 50, 10), visible],
            [
                "ComboBox",
                "",
                104,
                (60, 24, 110, 120),
                visible
                | win32con.WS_BORDER
                | win32con.WS_TABSTOP
                | win32con.WS_VSCROLL
                | win32con.CBS_DROPDOWN
                | win32con.CBS_AUTOHSCROLL,
            ],
            [
                "Button",
                "Match &whole word only",
                105,
                (5, 42, 100, 10),
                visible | win32con.BS_AUTOCHECKBOX | win32con.WS_TABSTOP,
            ],
            [
                "Button",
                "Match &case",
                107,
                (5, 52, 100, 10),
                visible | win32con.BS_AUTOCHECKBOX | win32con.WS_TABSTOP,
            ],
            [
                "Button",
                "Keep &dialog open",
                115,
                (5, 62, 100, 10),
                visible | win32con.BS_AUTOCHECKBOX | win32con.WS_TABSTOP,
            ],
            [
                "Button",
                "Across &open files",
                116,
                (5, 72, 100, 10),
                visible | win32con.BS_AUTOCHECKBOX | win32con.WS_TABSTOP,
            ],
            [
                "Button",
                "&Remember as default search",
                117,
                (5, 81, 150, 10),
                visible | win32con.BS_AUTOCHECKBOX | win32con.WS_TABSTOP,
            ],
            [
                "Button",
                "&Find Next",
                109,
                (185, 5, 50, 14),
                visible | win32con.BS_DEFPUSHBUTTON | win32con.WS_TABSTOP,
            ],
            [
                "Button",
                "&Replace",
                110,
                (185, 23, 50, 14),
                visible | win32con.WS_TABSTOP,
            ],
            [
                "Button",
                "Replace &All",
                111,
                (185, 41, 50, 14),
                visible | win32con.WS_TABSTOP,
            ],
            [
                "Button",
                "Cancel",
                win32con.IDCANCEL,
                (185, 59, 50, 14),
                visible | win32con.WS_TABSTOP,
            ],
        ]
        return dt

    def OnInitDialog(self):
        rc = FindReplaceDialog.OnInitDialog(self)
        self.HookCommand(self.OnReplace, 110)
        self.HookCommand(self.OnReplaceAll, 111)
        self.HookMessage(self.OnActivate, win32con.WM_ACTIVATE)
        self.editReplaceText = self.GetDlgItem(104)
        self.editReplaceText.SetWindowText(lastSearch.replaceText)
        if hasattr(self.editReplaceText, "SetEditSel"):
            self.editReplaceText.SetEditSel(0, -1)
        else:
            self.editReplaceText.SetSel(0, -1)
        self.butReplace = self.GetDlgItem(110)
        self.butReplaceAll = self.GetDlgItem(111)
        self.CheckButtonStates()
        return rc  # 0 when focus set

    def CheckButtonStates(self):
        # We can do a "Replace" or "Replace All" if the current selection
        # is the same as the search text.
        ft = self.editFindText.GetWindowText()
        control = _GetControl()
        # 		bCanReplace = len(ft)>0 and control.GetSelText() == ft
        bCanReplace = control is not None and lastSearch.sel == control.GetSel()
        self.butReplace.EnableWindow(bCanReplace)

    # 		self.butReplaceAll.EnableWindow(bCanReplace)

    def OnActivate(self, msg):
        wparam = msg[2]
        fActive = win32api.LOWORD(wparam)
        if fActive != win32con.WA_INACTIVE:
            self.CheckButtonStates()

    def OnFindNext(self, id, code):
        if code != 0:
            return 1
        self.DoFindNext()
        self.CheckButtonStates()

    def OnReplace(self, id, code):
        if code != 0:
            return 1
        lastSearch.replaceText = self.editReplaceText.GetWindowText()
        _ReplaceIt(None)

    def OnReplaceAll(self, id, code):
        if code != 0:
            return 1
        control = _GetControl(None)
        if control is not None:
            control.SetSel(0)
            num = 0
            if self.DoFindNext() == FOUND_NORMAL:
                num = 1
                lastSearch.replaceText = self.editReplaceText.GetWindowText()
                while _ReplaceIt(control) == FOUND_NORMAL:
                    num = num + 1

            win32ui.SetStatusText("Replaced %d occurrences" % num)
            if num > 0 and not self.butKeepDialogOpen.GetCheck():
                self.DestroyWindow()


if __name__ == "__main__":
    ShowFindDialog()