'Find-and-replace that doesn't use APIs (SetFocus is optional)
'By Achilles B. Mina
'January 3, 2002  

DECLARE FUNCTION SetFocus LIB "USER32" ALIAS "SetFocus" (HWnd AS LONG) AS LONG
DECLARE SUB Search
DECLARE SUB Find(Sender AS QBUTTON)
DECLARE SUB Radio

DEFBYTE back
DEFSTR oldtext, newtext

CREATE Arial12 AS QFONT
    Name = "Arial"
    Size = 12
END CREATE


CREATE Form AS QFORM
    Width = Screen.Width
    Height = Screen.Height - 28
    CREATE Main AS QMAINMENU        
        CREATE MFind AS QMENUITEM
            Caption = "&Search"
            OnClick = Search
        END CREATE
    END CREATE    
    CREATE Rich AS QRICHEDIT
        Align = 5
        ScrollBars = 2
        HideSelection = 0
        Font = Arial12
        WantTabs = 1
    END CREATE
    CREATE FindDlg AS QFORM
        Height = 150
        Caption = "Find Dialog"      
        BorderStyle = 3
        FormStyle = 3
        Center 
        CREATE FindText AS QEDIT
            Top = 10
            Left = 15
            Width = 170
            Height = 45
            Tag = 1
        END CREATE
        CREATE FindLabel AS QLABEL
            Top = 35
            Left = 18
            Width = 50
            Caption = "Find what word/s?"
        END CREATE
        CREATE FindButt AS QBUTTON
            Top = 10
            Left = 187
            Height = 22
            Width = 111            
            Caption = "Find"
            Default = 1
            OnClick = Find
        END CREATE
        CREATE Panel AS QPANEL
            Top = 60
            Left = 15
            Width = 283
            Height = 3
        END CREATE            
        CREATE ReplaceText AS QEDIT
            Top = 75
            Left = 15
            Width = 170
            Height = 45
        END CREATE
        CREATE ReplaceLabel AS QLABEL
            Top = 100
            Left = 18
            Width = 50
            Caption = "Replace with what?"
        END CREATE
        CREATE ReplaceButt AS QBUTTON
            Top = 75
            Left = 187
            Height = 22
            Width = 55
            Caption = "This"
            Default = 1
            OnClick = Find
        END CREATE
        CREATE ReplaceAButt AS QBUTTON
            Top = 75
            Left = 243
            Height = 22
            Width = 55
            Caption = "All"
            Default = 1
            OnClick = Find
        END CREATE
        CREATE RadioW AS QRADIOBUTTON
            Caption = "Whole word"
            Left = 140
            Top = 35
            Checked = 1
            OnClick = Radio
        END CREATE
        CREATE RadioP AS QRADIOBUTTON
            Caption = "Part of word"
            Left = 222
            Top = 35
            OnClick = Radio            
        END CREATE                                                       
    END CREATE        
END CREATE

Form.ShowModal

SUB Search
    FindDlg.Visible = 1
END SUB

SUB Radio
    SetFocus(FindText.Handle)
END SUB

SUB Find 
    DEFLNG start, where
    DEFBYTE back
    SELECT CASE Sender.Handle
      CASE FindButt.Handle
      FindButt.Caption = "Next"
      back = 0    
      DO       
          where = INSTR(start,Rich.Text,FindText.Text)
          wherelast = where+LEN(FindText.Text)
          IF wherelast > LEN(Rich.Text) THEN DEC wherelast   
          IF where > 1 THEN
              IF RadioW.Checked = 1 THEN
                  SELECT CASE MID$(Rich.Text,where-1,1)
                      CASE " ",CHR$(13),CHR$(10)
                          SELECT CASE MID$(Rich.Text,wherelast,1)
                              CASE " ",CHR$(13),CHR$(10),RIGHT$(FindText.Text,1)                      
                                  Rich.SelStart = where - 1
                                  back = 0
                              CASE ELSE
                                  start = where + LEN(FindText.Text)
                                  back = 1                        
                          END SELECT
                      CASE ELSE
                          start = where + LEN(FindText.Text)
                          back = 1
                  END SELECT        
              ELSE
                Rich.SelStart = where - 1     
              END IF    
              Rich.SelLength = LEN(FindText.Text)
              start = where + LEN(FindText.Text)
          ELSEIF where = 1 THEN
              IF RadioW.Checked = 1 THEN
                  SELECT CASE MID$(Rich.Text,wherelast,1)
                      CASE " ",CHR$(13),CHR$(10)
                          Rich.SelStart = where - 1
                          back = 0
                  END SELECT        
              ELSE
                  Rich.SelStart = where - 1      
              END IF    
              Rich.SelLength = LEN(FindText.Text)
              start = where + LEN(FindText.Text)         
          ELSE
              SHOWMESSAGE "No more instances of " + FindText.Text
              start = 0
              back = 0
          END IF
      LOOP UNTIL back = 0
      CASE ReplaceButt.Handle
          oldtext = Rich.Text
          diff = LEN(ReplaceText.Text) - LEN(FindText.Text)
          IF diff > 0 THEN
              Rich.Text = INSERT$(SPACE$(diff),Rich.Text,where)
              Rich.Text = REPLACE$(Rich.Text,ReplaceText.Text,where)
          ELSEIF diff < 0 THEN
              Rich.Text = REPLACE$(Rich.Text,ReplaceText.Text,where)
              Rich.Text = DELETE$(Rich.Text,where+LEN(ReplaceText.Text),ABS(SPACE$(diff)))
          ELSE
              Rich.Text = REPLACE$(Rich.Text,ReplaceText.Text,where)
          END IF
          Rich.SelStart = where + LEN(ReplaceText.Text) - 1
      CASE ReplaceAButt.Handle
          oldtext = Rich.Text
          Rich.Text = REPLACESUBSTR$(Rich.Text,FindText.Text,ReplaceText.Text)
          Rich.SelStart = RINSTR(Rich.Text,ReplaceText.Text) + LEN(ReplaceText.Text) -1
  END SELECT
END SUB