in reply to Re: Tags in TK Text widgets
in thread Tags in TK Text widgets

The problem in this case is that the newline character is invalid input and needs to be highlighted so the user can see their error and remove it just like any other invalid character.

Some background, this is part of a Morse code teaching system and for beginners the character set is purposefully kept small. Morse code does not have the concept of 'newline' and there is no code for such a character, so newline characters are invalid and need to be clearly marked as such.

So, I need a way to make it obvious what the character is and having the Text widget highlight the rest of an apparently empty line is not optimal.

So I need a way to replace the newline characters with something that will show up clearly.

Replies are listed 'Best First'.
Re^3: Tags in TK Text widgets
by tybalt89 (Monsignor) on Aug 18, 2024 at 15:39 UTC

    One thing to do is to replace newlines with spaces in the validate sub.

    sub validate_text { my $wstring = $ts_box->get("1.0", "end") =~ s/\s+/ /gr; $ts_box->delete('1.0', 'end'); $ts_box->insert('end', $wstring); $ts_box->tagAdd( 'badc', "1.$-[0]", "1.$+[0]" ) while $wstring =~ /[^A-Za-z0-9 \/.,=?*+]+/g; }

    Some of the existing code has been slightly tightened up, just a little. :)

      Ah, that is what I was trying to do using

      $ts_box->FindAndReplaceAll(-regexp, -nocase, "[\n]", " ");
      I tried several variations but although they matched a normal character I could never get it to match the \n

      Your tightening doesn't unfortunately apply to my actual code because there are other tests made in the bad check loop and $cnt is used for other stuff too. But thanks, I hadn't thought of pulling the string out of the widget to work on it and then putting it back.

Re^3: Tags in TK Text widgets
by Marshall (Canon) on Aug 18, 2024 at 09:54 UTC
    You say: "So I need a way to replace the newline characters with something that will show up clearly."

    The obvious thing to do would be to show what the Morse code guy would send in place of "\n", which would be the prosign {BT}. I used enclosing curly brackets to designate a prosign. When writing a prosign by hand, there would be a line atop the characters "BT". Instead of BT, move the underline to on top of the characters. If you send the characters: "BT", there will be 3 dots worth of time spacing between the B and the T. When sent as a prosign, there is no spacing and the two characters are run together. When someone runs characters together unintentionally, they are known as a "swing fist" and it is very hard to understand them!

    Samuel Morse's historic message would have been sent: "WHAT HATH GOD WROUGHT {SK}", the prosign SK means the end of the message. There are a bunch of common prosigns: {BT} (change of thought/paragraph), {AS} (standby). {AA} (All After). {BK} ("over to you") among them.

    You say you are teaching beginners. I would not teach the weirdo math characters +-*= as a first pass. I suggest you come up with a list of prosigns to teach - these will be heard far more often than these "math" characters. Another issue is how to represent the Error symbol. That is 8 dits in a row. I've never written a character for that because this is the "oops" character when you have botched the spelling of a word or other error. But I suppose if this is some test lesson script, you would want to include that so the students will know how to deal with it when they hear it.

    For other UI possibilities, I think it would be possible to have the input refuse to enter any illegal character. Perhaps it looks to the user like nothing happens when he types "&". Some prototyping and extra thought are required to see if that idea pans out.

    I did run your code. I had to install the TFrame module. For this demo, Frame would have sufficed. I see weird spaces in a number of places on my display. Like extra space before the highlighted "&" or when a space character precedes a "bad" character. This extra space is not composed of space characters since a "copy-n-paste" does not copy any extra spaces. I am puzzled by this behavior and I don't know if it is because of the ancient version of Perl that I run (5.24). I am still fiddling with a few ideas.

    I suppose you could be teaching Morse as part of an HST (High Speed Telegraphy) competition? If so, I would read the rules on the universe of symbols that could be sent.

    Update: Oh not all prosigns are 2 letters, {AAA} is a "full stop". Some old guy on the Titanic would have been familiar with that, but I don't hear that. Update- that's not right - just forget it.