Author Topic: convert_spaces2tabs could 'break' strings  (Read 4526 times)

hs2

  • Senior Community Member
  • Posts: 2737
  • Hero Points: 288
convert_spaces2tabs could 'break' strings
« on: March 20, 2007, 12:25:36 am »
Hello SlickTeam,

it's nice to see that convert_spaces2tabs converts some more SPACEs ;), but applying it to sth. like:
(C - code)
Code: [Select]
printf ("Hello                    world\n");may lead to surprising results b/c the TABs now contained in the (compiled) string maybe interpreted differently @ runtime...

I think it's almost impossible to get this right. I also tried it once w/o much success, but at least I tried to skip all string-SPACEs (C strings only, but also in comments).
I'd propose a hint in the docs.

HS2
« Last Edit: March 20, 2007, 01:10:44 am by hs2 »

Lee

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 1299
  • Hero Points: 130
Re: convert_spaces2tabs could 'break' strings
« Reply #1 on: March 20, 2007, 01:20:37 pm »
That is very good warning indeed.  Yes, convert_spaces2tabs (Edit > Other > Spaces to Tabs) now converts all whitespace in buffer/selection to tabs, rather than just leading whitespace as previously done.

hs2

  • Senior Community Member
  • Posts: 2737
  • Hero Points: 288
Re: convert_spaces2tabs could 'break' strings
« Reply #2 on: March 20, 2007, 01:32:41 pm »
Yep - stumbled into that long time ago. Even worse I used an editor (dark pre-Slick era :() where I could't visualize whitespaces...
However, I double-checked my old v11 hack and as I've seen I used an add. 'boolean all=false' argument to enhance the command in a compatible way. Maybe a similar thing could be added to the new convert_spaces2tabs too. Just an idea.

HS2

lambertia

  • Senior Community Member
  • Posts: 382
  • Hero Points: 14
  • I have nothing sufficiently witty to say.
Re: convert_spaces2tabs could 'break' strings
« Reply #3 on: March 22, 2007, 01:10:43 am »
Hi.

Another (and the correct solution IMHO) is to write the code as:

Code: [Select]
printf("Hello\t\t\t\tworld\n");
There'll be no problems then. Not helpful for legacy code but is by far the safest.

Cheers,

Wanderer

  • Senior Community Member
  • Posts: 557
  • Hero Points: 23
Re: convert_spaces2tabs could 'break' strings
« Reply #4 on: March 22, 2007, 01:17:19 am »
That is very good warning indeed.  Yes, convert_spaces2tabs (Edit > Other > Spaces to Tabs) now converts all whitespace in buffer/selection to tabs, rather than just leading whitespace as previously done.

How do we get back to the previous behaviour? 

hs2

  • Senior Community Member
  • Posts: 2737
  • Hero Points: 288
Re: convert_spaces2tabs could 'break' strings
« Reply #5 on: March 22, 2007, 01:48:24 am »
Right, this is the correct way.
However, white space alignment is anyway inherently wrong, at least if you do GUI work.
Nevertheless I've seen a lot of SPACE alignment (legacy) code ...

HS2

@Wanderer:
You could replace the org. command in util.e w/ this v11-v12 merge providing the choice ...

Edit: Of course you could add this macro to your personal collection instead of patching the product.

Code: [Select]
_command void convert_spaces2tabs( boolean all = false ) name_info(','VSARG2_MARK|VSARG2_REQUIRES_EDITORCTL)
{
   clear_message();
   int old_mark_id = _duplicate_selection('');
   int mark_id = -1;
   int ln=0;
   int col=0;
   int left_edge=0;
   int cursor_y=0;
   // HS2-ADD: better message text #1
   area := "Selection";

   if (!select_active()) {
      // HS2-ADD: better message text #2
      area = "Whole buffer";
      ln=p_line;col=p_col;left_edge=p_left_edge;cursor_y=p_cursor_y;
      mark_id = _alloc_selection();
      _show_selection(mark_id);
      top(); _select_line(mark_id);
      bottom(); _select_line(mark_id);
   }

   if ( all )
   {
      filter_selection('_tabify_filter','',true);
   }
   else
   {
      // HS2-ADD: old but v11 behaviour: leading TABs only
      _end_select();
      esline := p_line;
      _begin_select();
      bsline := p_line;

      // For every line in the selection, convert indent spaces to tabs:
      old_value := p_indent_with_tabs;
      p_indent_with_tabs = true;
      _str line;
      int i;
      for ( i = bsline; i <= esline; i++ )
      {
         p_line = i;
         get_line( line );
         typeless non_blank=verify( line," \t");
         if ( non_blank )
         {
            replace_line( indent_string(text_col(line,non_blank,'I')-1) :+ substr(line,non_blank) );
         }
      }
      p_indent_with_tabs = old_value;
   }

   if (mark_id > 0) {
      _show_selection(old_mark_id);
      _free_selection(mark_id);
      p_line=ln;p_col=col;set_scroll_pos(left_edge,cursor_y);
   }
   // HS2-ADD: better message text #3
   message (area " de-spaced (" (all ? "all" : "leading only") ")");
}
« Last Edit: March 22, 2007, 01:50:21 am by hs2 »