Author Topic: Duplicating selected lines of text  (Read 7499 times)

TxDot

  • Community Member
  • Posts: 38
  • Hero Points: 0
Duplicating selected lines of text
« on: November 12, 2007, 04:02:24 PM »
Does anyone have a macro that will allow me to select 2 or more lines and then duplicate the lines? I found multiple references to _duplicate_selection and  have done some tests but am only able to get the line that the cursor is on to be duplicated.

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: Duplicating selected lines of text
« Reply #1 on: November 12, 2007, 04:20:01 PM »
I'm using this one:
Code: [Select]
_command void dupl () name_info(','VSARG2_MARK|VSARG2_TEXT_BOX|VSARG2_REQUIRES_EDITORCTL)
{
   if ( !select_active () )
   {
      duplicate_line();
   }
   else
   {
      _select_line();
      end_select ();
      copy_to_clipboard();
      cursor_down();
      paste();
   }
}

HS2
« Last Edit: November 12, 2007, 04:22:46 PM by hs2 »

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: Duplicating selected lines of text
« Reply #2 on: November 12, 2007, 04:34:49 PM »
And this version avoids creating extra clipboard entries:
Code: [Select]
_command void dupl () name_info(','VSARG2_MARK|VSARG2_TEXT_BOX|VSARG2_REQUIRES_EDITORCTL)
{
   select_line();
   end_select();
   _copy_to_cursor()
   deselect();
}
HS2

Edit: removed useless 'lock_selection();'
« Last Edit: November 12, 2007, 04:52:35 PM by hs2 »

TxDot

  • Community Member
  • Posts: 38
  • Hero Points: 0
Re: Duplicating selected lines of text
« Reply #3 on: November 12, 2007, 06:55:09 PM »
Thanks HS2. Works great; appreciate the quick response.

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: Duplicating selected lines of text
« Reply #4 on: November 12, 2007, 07:28:05 PM »
Thank you for asking b/c I wanted to rewrite the old version since a longer time (w/o utilizing the clipboard for multi-line dups).
This was a good occasion :) - HS2

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: Duplicating selected lines of text
« Reply #5 on: November 12, 2007, 11:14:10 PM »
Small but useful (final) change.
Using brief_select_line doesn't require LINE selections anymore.
Any selection 'marking' some lines (e.g. a BLOCK selection) will do. (The current line is duplicated if nothing is selected as before.)
Code: [Select]
_command void dupl () name_info(','VSARG2_MARK|VSARG2_TEXT_BOX|VSARG2_REQUIRES_EDITORCTL)
{
   if ( !select_active () )   select_line();
   else _select_type("",'T','LINE');

   end_select();
   _copy_to_cursor()
   deselect();

   // optionally go to the beginning of the dup'd lines
   // down(); first_non_blank();
}
HS2

Edit: Sorry - prev. version was broken (for line selections !) - fixed.
« Last Edit: November 13, 2007, 11:35:29 PM by hs2 »