Author Topic: Transparent clipboard  (Read 3515 times)

suevian

  • Community Member
  • Posts: 10
  • Hero Points: 0
Transparent clipboard
« on: October 10, 2006, 12:16:44 pm »
Hello forumers,

I have a recorded macro that swaps two lines. Works fine, except for one flaw. When I run the macro, and then CTRL+V, the pasted text is the line that was copied by the macro, not the one I had copied before using the macro. I don't want this macro to mess with my clipboard. Any ideas what should I do?

This is my recorded macro:

_command swap_lines_up() name_info(','VSARG2_MARK|VSARG2_REQUIRES_EDITORCTL)
{
   _macro('R',1);
   begin_line_text_toggle();
   deselect();
   _select_char('','E');
   end_line();
   select_it('CHAR','','E');
   cut();
   linewrap_delete_char();
   cursor_up();
   c_enter();
   cursor_up();
   paste();
}

I want to be able to do the following:

1: I have file of three lines:
   
   Line 1: aaaaaaaaaaaaa
   Line 2: bbbbbbbbbbbbb
   Line 3: ccccccccccccc

2: Copy Line 1
3: Go to Line 3 and perform the recorded macro, it should swap the places of Line 3 and Line 2
4: Go to end of file and perform CTRL+V, it should paste Line 1, not Line 3, as it does now.

Any help will be greatly appreciated.
 


hs2

  • Senior Community Member
  • Posts: 2736
  • Hero Points: 285
Re: Transparent clipboard
« Reply #1 on: October 10, 2006, 01:14:45 pm »
Maybe it's not even possible with macro recording. It's sometimes better to write your own.
Have a look at the sample below. It takes only a minute once you're a bit more experienced (maintaining your personal macro-module filled w/ such useful things).
Code: [Select]
_command void swap_lines_up() name_info(','VSARG2_MARK|VSARG2_REQUIRES_EDITORCTL)
{
   _str line1, line2;

   _save_pos2( p );
   
   _deselect( );

   begin_line();
   get_line( line1 );
   up();
   get_line( line2 );
   replace_line( line1 );
   down();
   replace_line( line2 );

   _restore_pos2( p );
}

Writing macros yourself allows you to use all the (macro-)functions Slick provides and not only 'commands' (bound to a keys). This makes a difference !

Have fun,

HS2
« Last Edit: October 10, 2006, 01:21:24 pm by hs2 »

suevian

  • Community Member
  • Posts: 10
  • Hero Points: 0
Re: Transparent clipboard
« Reply #2 on: October 10, 2006, 02:03:33 pm »
That was almost what I needed, thanks.

Just to share with you, I modified your code just a little bit, so that original position is not restored. In that way I can bind the command to CTRL+SHIFT+UParrow and "grab" a line and move it let's say 5 lines up. I have the same command for moving a line down. I've been using it today for some time, it's something I can't live without anymore!

_command void swap_lines_up() name_info(','VSARG2_MARK|VSARG2_REQUIRES_EDITORCTL)
{
   _str line1, line2;

   _deselect( );

   begin_line();
   get_line( line1 );
   up();
   get_line( line2 );
   replace_line( line1 );
   down();
   replace_line( line2 );
   up ();
}

_command void swap_lines_down() name_info(','VSARG2_MARK|VSARG2_REQUIRES_EDITORCTL)
{
   _str line1, line2;
   
   _deselect( );

   begin_line();
   get_line( line1 );
   down();
   get_line( line2 );
   replace_line( line1 );
   up();
   replace_line( line2 );
   down ();
}

Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 4273
  • Hero Points: 314
Re: Transparent clipboard
« Reply #3 on: October 10, 2006, 02:16:52 pm »
cut() and paste() take a name argument.  Named clipboards won't mess up Ctrl+V.  Using get_line() works well especially for short lines.  You wouldn't want to use it for a 1 meg line though.  The most efficient way to move large amounts of text is to allocate a selection, use _select_char() at start and end, then use _move_to_cursor().  This way there is no termporary storage used at all (no clipboard, no temp variable).  This is over kill for short lines and probably slower.

hs2

  • Senior Community Member
  • Posts: 2736
  • Hero Points: 285
Re: Transparent clipboard
« Reply #4 on: October 10, 2006, 03:08:29 pm »
Thanks for this little chapter from the 'Enhanced Slick-C macro programming' book ;)
Good to know !

HS2