Author Topic: Change join_lines to add a space only when needed.  (Read 5339 times)

flethuseo

  • Senior Community Member
  • Posts: 177
  • Hero Points: 2
Change join_lines to add a space only when needed.
« on: February 10, 2015, 07:08:48 PM »
I want to change the command join_lines to add a space between lines if there isn't one already. Anyone know what I can change that insert_text(' ') to.
        /**
         * Joins the next line to the current line at the cursor
         * position.  Works on selections.
         *
         * @appliesTo Edit_Window, Editor_Control
         *
         * @categories Edit_Window_Methods, Editor_Control_Methods
         *
         */
        _command void join_lines() name_info(','VSARG2_MULTI_CURSOR|VSARG2_REQUIRES_EDITORCTL|VSARG2_MARK)
        {
           if (select_active()) {
              int i;
              _str num_lines = count_lines_in_selection();
              if (!isinteger(num_lines)) {
                 // ?
                 message('Bad selection.');
                 return;
              }
              int nl = (int)num_lines;
              begin_select();
              typeless p;
              _save_pos2(p);
              for (i = 0; i < nl - 1; i++) {
                 end_line();
                 _insert_text(' ');
                 join_line();
              }
              _restore_pos2(p);
           } else {
              join_line();
           }
        }


Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: Change join_lines to add a space only when needed.
« Reply #1 on: February 11, 2015, 09:59:14 AM »
Hi Ted
I don't follow what you want.  If you join the following 3 lines
   abc
   def
   ghi
you get
abc def ghi
with a single space between each of the original lines.  Can you give an example of what you want?

flethuseo

  • Senior Community Member
  • Posts: 177
  • Hero Points: 2
Re: Change join_lines to add a space only when needed.
« Reply #2 on: February 12, 2015, 09:37:05 PM »
I have:
Code: [Select]
hello,
slick-c,   
world 
spaces after each of these words are: 1, 2, 2  respectively
join-lines gives me:
Code: [Select]
hello,  slick-c,    world 
I want only one space between words:
Code: [Select]
hello, slick-c, world

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: Change join_lines to add a space only when needed.
« Reply #3 on: February 13, 2015, 09:50:50 AM »
The behaviour of join_line varies according to whereabouts the cursor is in the line being joined.  If the cursor is within trailing spaces at the end of the line then spaces that come after the cursor are removed and spaces just before the cursor are retained.  So to get one space between lines after joining you need to remove trailing spaces, then insert one space and leave the cursor just after that space  - so you just need to insert a call to strip_trailing_spaces in the for loop.  It's possible the behaviour will vary according to configuration options  - tools -> options -> editing -> cursor movement.

Code: [Select]
      for (i = 0; i < nl - 1; i++) {
         strip_trailing_spaces();
         end_line();
         _insert_text(' ');
         join_line();
      }

flethuseo

  • Senior Community Member
  • Posts: 177
  • Hero Points: 2
Re: Change join_lines to add a space only when needed.
« Reply #4 on: February 13, 2015, 05:05:19 PM »
Interesting, there is one test case where I did not get what I expected after the change you suggested Graeme.

Code: [Select]
        hello,         
        slick-c,       
        world           

All the words here have spaces before and after. When I call join-lines with the added strip_trailing_spaces(); I am getting one space between each line, except for the very first line which retains the trailing spaces at the beginning. It's not big deal, but I am curious why it removes trailing spaces at the beginning for all lines except for the first.

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: Change join_lines to add a space only when needed.
« Reply #5 on: February 14, 2015, 12:44:05 AM »
I can't reproduce this, I get one space between first and second lines.  Are the lines "comments"?  Can you post the full context?  What kind of source file?  I'm using 19.0.1 with latest hotfixes.

flethuseo

  • Senior Community Member
  • Posts: 177
  • Hero Points: 2
Re: Change join_lines to add a space only when needed.
« Reply #6 on: February 19, 2015, 04:25:14 PM »
Never mind about this. I like the current behavior. The first line retains the spaces.. or perhaps I should say original indentation.

I am attaching a vid of the behavior I meant, since it's hard to explain.

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: Change join_lines to add a space only when needed.
« Reply #7 on: February 21, 2015, 02:31:08 AM »
Never mind about this. I like the current behavior. The first line retains the spaces.. or perhaps I should say original indentation.

Oh, well I failed to notice that when you said the following you were actually contradicting yourself and contradicting your video.  Your video shows you are getting exactly one space between joined lines including the first line and when you said it "removes trailing spaces at the beginning" you actually meant it removes *leading* spaces at the beginning.

Quote
I am getting one space between each line, except for the very first line which retains the trailing spaces at the beginning. It's not big deal, but I am curious why it removes trailing spaces at the beginning for all lines except for the first.
« Last Edit: February 21, 2015, 11:51:40 AM by Graeme »