' Simple Line Editor — cursor movement, insert/delete via INKEY$
#COMPILE EXE
#DIM ALL

SUB ShowBuffer(buf AS STRING, BYVAL cursorPos AS LONG)
    PRINT "> " & LEFT$(buf, cursorPos) & "|" & MID$(buf, cursorPos + 1)
    PRINT "  " & STRING$(LEN(LEFT$(buf, cursorPos)) + 2, 32) & "^"
    PRINT "  Cursor: " & STR$(cursorPos) & "  Len: " & STR$(LEN(buf))
END SUB

SUB ClearLine()
    LOCAL i AS LONG
    FOR i = 1 TO 80
        PRINT CHR$(8);
    NEXT i
    FOR i = 1 TO 80
        PRINT " ";
    NEXT i
    FOR i = 1 TO 80
        PRINT CHR$(8);
    NEXT i
END SUB

SUB TestEditBuffer()
    LOCAL buf AS STRING
    LOCAL cursorPos AS LONG
    LOCAL k AS STRING
    LOCAL done AS LONG
    LOCAL newBuf AS STRING
    LOCAL keyCode AS LONG
    buf = "Hello, World!"
    cursorPos = LEN(buf)
    done = 0
    PRINT "=== LINE EDITOR DEMO ==="
    PRINT "Editor commands:"
    PRINT "  Left/Right arrows = move cursor"
    PRINT "  Home = start, End = end"
    PRINT "  Delete = delete char, Backspace = backspace"
    PRINT "  Esc = exit editor"
    PRINT
    CALL ShowBuffer(buf, cursorPos)
    DO
        k = INKEY$
        IF k <> "" THEN
            IF k = CHR$(27) THEN
                done = 1
            ELSEIF LEN(k) = 2 AND ASC(LEFT$(k, 1)) = 0 THEN
                keyCode = ASC(MID$(k, 2, 1))
                IF keyCode = 75 THEN
                    IF cursorPos > 0 THEN cursorPos = cursorPos - 1
                ELSEIF keyCode = 77 THEN
                    IF cursorPos < LEN(buf) THEN cursorPos = cursorPos + 1
                ELSEIF keyCode = 71 THEN
                    cursorPos = 0
                ELSEIF keyCode = 79 THEN
                    cursorPos = LEN(buf)
                ELSEIF keyCode = 83 THEN
                    IF LEN(buf) > 0 AND cursorPos < LEN(buf) THEN
                        buf = LEFT$(buf, cursorPos) & MID$(buf, cursorPos + 2)
                    END IF
                END IF
            ELSEIF k = CHR$(8) THEN
                IF cursorPos > 0 THEN
                    buf = LEFT$(buf, cursorPos - 1) & MID$(buf, cursorPos + 1)
                    cursorPos = cursorPos - 1
                END IF
            ELSEIF ASC(k) >= 32 AND ASC(k) <= 126 THEN
                buf = LEFT$(buf, cursorPos) & k & MID$(buf, cursorPos + 1)
                cursorPos = cursorPos + 1
            END IF
            CALL ClearLine()
            CALL ShowBuffer(buf, cursorPos)
        END IF
        SLEEP 30
    LOOP UNTIL done
    PRINT
    PRINT "Final text: " & buf
    PRINT "Editor demo complete."
    PRINT
END SUB

SUB CountWords(BYVAL text AS STRING)
    LOCAL i AS LONG
    LOCAL wc AS LONG
    LOCAL inWord AS LONG
    LOCAL ch AS STRING
    wc = 0
    inWord = 0
    FOR i = 1 TO LEN(text)
        ch = MID$(text, i, 1)
        IF ch <> " " THEN
            IF inWord = 0 THEN
                wc = wc + 1
                inWord = 1
            END IF
        ELSE
            inWord = 0
        END IF
    NEXT i
    PRINT "Word count: " & STR$(wc)
END SUB

SUB CharAnalysis(BYVAL text AS STRING)
    LOCAL i AS LONG
    LOCAL ch AS STRING
    LOCAL letters AS LONG
    LOCAL digits AS LONG
    LOCAL spaces AS LONG
    LOCAL other AS LONG
    letters = 0
    digits = 0
    spaces = 0
    other = 0
    FOR i = 1 TO LEN(text)
        ch = MID$(text, i, 1)
        IF (ch >= "A" AND ch <= "Z") OR (ch >= "a" AND ch <= "z") THEN
            letters = letters + 1
        ELSEIF ch >= "0" AND ch <= "9" THEN
            digits = digits + 1
        ELSEIF ch = " " THEN
            spaces = spaces + 1
        ELSE
            other = other + 1
        END IF
    NEXT i
    PRINT "=== CHARACTER ANALYSIS ==="
    PRINT "Text: " & text
    PRINT "Letters: " & STR$(letters)
    PRINT "Digits: " & STR$(digits)
    PRINT "Spaces: " & STR$(spaces)
    PRINT "Other: " & STR$(other)
    PRINT "Total: " & STR$(LEN(text))
    PRINT
END SUB

SUB ReverseString(BYVAL s AS STRING, BYREF result AS STRING)
    LOCAL i AS LONG
    LOCAL n AS LONG
    LOCAL rev AS STRING
    n = LEN(s)
    rev = ""
    FOR i = n TO 1 STEP -1
        rev = rev & MID$(s, i, 1)
    NEXT i
    result = rev
END SUB

SUB IsPalindrome(BYVAL s AS STRING)
    LOCAL i AS LONG
    LOCAL n AS LONG
    LOCAL isPal AS LONG
    n = LEN(s)
    isPal = 1
    FOR i = 1 TO n \ 2
        IF MID$(s, i, 1) <> MID$(s, n - i + 1, 1) THEN
            isPal = 0
            EXIT FOR
        END IF
    NEXT i
    IF isPal THEN
        PRINT "The string is a palindrome."
    ELSE
        PRINT "The string is NOT a palindrome."
    END IF
END SUB

FUNCTION PBMAIN() AS LONG
    LOCAL rev AS STRING
    PRINT "=========================="
    PRINT " Console Line Editor "
    PRINT "=========================="
    PRINT
    CALL TestEditBuffer()
    PRINT "=========== TEXT ANALYSIS ==========="
    CALL CountWords("The quick brown fox jumps over the lazy dog")
    CALL CharAnalysis("Hello World! 123 ABC def. x=5")
    CALL ReverseString("Hello World", rev)
    PRINT "Reversed: " & rev
    CALL IsPalindrome("racecar")
    CALL IsPalindrome("hello")
    PRINT
    PRINT "Press Enter to exit..."
    SLEEP 3000
    FUNCTION = 0
END FUNCTION
