The simplest pre-editor and post-editor on earth

2025-12-28 Sunday 7h 04 pm

This kid from Western Canada did some great work on one of my apps. But when it came to write code to change text on an entry line, let's say that he wasn't very inspired. :-)
All he could come up with is a code of search and replace. I had to ask him for code every-time I made a change.

A guy from India came along and he commented all this garbage. He came up with the following system:

(texta)#(textb)

Change texta to textb. I can even change sentences!

He did this for pre-editing and post-editing!

Here is the code for my TCL-TK app! Any comments? Any help?

# Function to process the entire text in the widget
proc processText {} {
    # Get the full text from the text widget
    set inputText [get .inputText "1.0" "end-1c"]

    # Split the text by lines
    set lines [split $inputText "\n"]

    # Initialize an empty list to hold the processed lines
    set updatedLines {}

    # Iterate through each line and process it
    foreach line $lines {
        # Check if there is a '#' in the line
        if {[string first "#" $line] != -1} {
            # Split the line at the '#'
            set parts [split $line "#"]
            
            # Get TextA (before #) and TextB (after #)
            set textA [lindex $parts 0]
            set textB [lindex $parts 1]

            # Replace TextA with TextB and add it to the updatedLines list
            lappend updatedLines $textB
        } else {
            # If no '#' is found, just append the original line
            lappend updatedLines $line
        }
    }

    # Join the updated lines into a single string with newlines
    set updatedText [join $updatedLines "\n"]

    # Update the text widget with the modified text
    .inputText delete "1.0" "end"
    .inputText insert "1.0" $updatedText
}

AMB - 2025-12-30 16:08:36

Using the string map command, you can replace text in a string:

set original {foo bar texta hello world texta}
set new [string map {texta textb} $original]
puts $new   ;# foo bar textb hello world textb

gold 01/15/2026. Added categories, so can find page in Wiki.