nomad-software / tkd

GUI toolkit for the D programming language based on Tcl/Tk
MIT License
117 stars 16 forks source link

text widget appendText escaping #26

Closed callumenator closed 9 years ago

callumenator commented 9 years ago
auto editor = new Text();
editor.appendText("{");

outputs In text widget:

\{

Possible solution: Use the following for the escaper:

string escape(string arg)
{
  string replacer(Captures!(string) m)
  {
    final switch(m.hit) 
    {
      case `\`: return `\\`;
      case `"`: return `\"`;
      case `$`: return `\$`;
      case `[`: return `\[`;
      case `]`: return `\]`;                
    }
    assert(false);
  }     
  return arg.replaceAll!(replacer)(regex(`\\|"|\$|\[|\]`));       
}

And insert the text using:

this._tk.eval(`%s insert end "%s"`, this.id, text);
nomad-software commented 9 years ago

I knew this would bite me eventually. This potentially might be a large piece of work to test all the text widgets to make sure all follow the same quoting rule. For example just about everything uses {...} to quote strings for the interpreter so the escape function should deal with that.

I've found this which might help: http://stackoverflow.com/questions/5302120/general-string-quoting-for-tcl

I'll need to set some time aside to look into this better.

callumenator commented 9 years ago

It might be ok to just use this escaping function when inserting text into text widgets, since as you say usually {} quoting is what you want. A method to evaluate raw strings would also be handy, leaving it up to the to do any necessary escaping.

(Somewhat related: https://github.com/nomad-software/tkd/blob/master/source/tkd/interpreter/tcl.d#L123 should statically check for zero args and not call format in that case, since any literal format specifiers in the string get picked up as 'lone format specifiers').

nomad-software commented 9 years ago

The above issue is now solved so i might take another look at this.

nomad-software commented 9 years ago

Right i think i've tackled this bad boy! See https://github.com/nomad-software/tkd/commit/40423f08f91c4647f2d9e8cdab76ea2e7d532c03 for a run down of changes. Test your stuff and see if this fixes the escaping issue. Many thanks for the escaper function. :+1:

If everything tests as good, i'll close this issue.

callumenator commented 9 years ago

Yep everything tests fine for me, thanks a lot. Just a note, the escaper doesn't do anything about non-printing characters (mentioned in the SO link you found), but I don't know if this will be an issue.

nomad-software commented 9 years ago

Hmmm... i don't think they will be a problem and i don't really know what he means by \nnn escapes for non-printing characters. so i'll close this and create another release.