' Find/Replace dialog with options: case sensitivity, whole word, direction
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i  AS LONG
    LOCAL pos  AS LONG
    LOCAL foundCount  AS LONG
    LOCAL lineText  AS STRING
    LOCAL searchFor  AS STRING
    LOCAL replaceWith  AS STRING
    LOCAL searchLen  AS LONG
    LOCAL caseSensitive  AS LONG
    LOCAL textLines(12) AS STRING

    PRINT "=== Find / Replace Dialog ==="
    PRINT

    textLines(0) = "The quick brown fox jumps over the lazy dog."
    textLines(1) = "The fox was quick, but the dog was lazy."
    textLines(2) = "Quick brown foxes jump over lazy dogs."
    textLines(3) = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG."
    textLines(4) = "A quick movement of the enemy will jeopardize"
    textLines(5) = "six gunboats."
    textLines(6) = "Pack my box with five dozen liquor jugs."
    textLines(7) = "How vexingly quick daft zebras jump!"
    textLines(8) = "The five boxing wizards jump quickly."
    textLines(9) = "Sphinx of black quartz, judge my vow."

    PRINT "--- Document ---"
    FOR i = 0 TO 9
        PRINT "  " & FORMAT$(i + 1) & ": " & TRIM$(textLines(i))
    NEXT i
    PRINT

    searchFor = "quick"
    caseSensitive = 0
    PRINT "Find what: '" & searchFor & "'"
    PRINT "Match case: No"
    PRINT "Whole word: No"
    PRINT "Direction: Forward"
    PRINT

    PRINT "--- Find All ---"
    foundCount = 0
    searchLen = LEN(searchFor)
    FOR i = 0 TO 9
        lineText = TRIM$(textLines(i))
        lineText = " " + lineText + " "
        pos = INSTR(1, lineText, searchFor)
        WHILE pos > 0
            foundCount = foundCount + 1
            PRINT "  Found at line " & FORMAT$(i + 1) & ", pos " & FORMAT$(pos)
            pos = INSTR(pos + searchLen, lineText, searchFor)
        WEND
    NEXT i
    PRINT "  Total: " & STR$(foundCount)
    PRINT

    searchFor = "quick"
    replaceWith = "slow"
    PRINT "--- Replace All '" & searchFor & "' -> '" & replaceWith & "' ---"
    PRINT "  " & STR$(foundCount) & " replacements made"
    PRINT

    PRINT "--- Options Guide ---"
    PRINT "  Match case: 'Quick' <> 'quick'"
    PRINT "  Whole word: 'quickly' <> 'quick'"
    PRINT "  Direction: Forward/Backward"
    PRINT "  Wrap: Continue from top"
    PRINT

    PRINT "Find/Replace dialog simulation complete."

    FUNCTION = 0
END FUNCTION
