Author Topic: Copying words to named clipboards  (Read 6900 times)

jqphantom

  • Junior Community Member
  • Posts: 3
  • Hero Points: 0
Copying words to named clipboards
« on: June 27, 2007, 06:39:37 PM »
I want to program keys like this: Ctrl-Alt-1 copies a word (variable name) to named clipboard c1, Ctrl-Alt-2 to named clipboard c2, and so forth up to c9. Then, Alt-1 would paste the contents of clipboard c1 into the document, Alt-2 would paste clipboard c2, etc., up to Alt-9.

The aim is a facility where I can separately copy a bunch of variables, say, from var or int declarations, over to statements that use the variable names. When I need the first variable, I just hit Alt-1, and so on. I have found this technique to bump up my productivity a bunch with SAS code (using ED for Windows), but it would work with other languages, as well. It is much faster than using the single default clipboard for one variable at a time; I can pepper my code with one of the chosen set of variables very quickly, at will.

Can someone tell me exactly how I could do this? I am sure it can be done with macros, I just don't know the details.

Thanks!

Michael M./jqphantom

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: Copying words to named clipboards
« Reply #1 on: June 27, 2007, 07:58:03 PM »
Hi jqphantom,
I ripped some code from my macro-toolbox. I hope it's sufficient to get an idea.

Code: [Select]
_command void copy_word_to_autonamed_clipboard () name_info (','VSARG2_LASTKEY|VSARG2_READ_ONLY|VSARG2_REQUIRES_EDITORCTL)
{
   // could be parsed much better/more robust -> tailored to C-A-<number>
   _str num;
   parse event2name (last_event ()) with '-' '-' num;

   _str text=cur_word (junk,'',1);
   text_to_clipboard ( text, 'c'num);
}
_command void insert_autonamed_clipboard () name_info (','VSARG2_LASTKEY|VSARG2_READ_ONLY|VSARG2_REQUIRES_EDITORCTL)
{
   // could be parsed much better/more robust -> tailored to A-<number>
   _str num;
   parse event2name (last_event ()) with '-' num;
   paste( 'c'num );
}

// clipboard_type == 'CHAR' , 'LINE' or 'BLOCK'
_command int text_to_clipboard (_str text = '', _str clipboard_name = '', _str clipboard_type = 'CHAR') name_info(','VSARG2_REQUIRES_EDITORCTL|VSARG2_READ_ONLY)
{
   // s.th. to copy ?
   if ( length ( text ) )
   {
      push_clipboard_itype (clipboard_type,clipboard_name,1,true);
      append_clipboard_text (text);
      return(0);
   }
   else return(TEXT_NOT_SELECTED_RC);  // TEXT_NOT_SELECTED_RC is repurposed here
}

Have fun,

HS2

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: Copying words to named clipboards
« Reply #2 on: June 27, 2007, 08:22:26 PM »
Hello again jqphantom,

you can also specify shortcuts which consist of a 'lead-in' key combination/shortcut and one or more add keys.
As an example in the keybinding dialog you just have to press and release the desired lead-in combination (e.g. SHIFT-CTRL-w) and press '1' afterwards.
In this case the last_event parsing would be just:
Code: [Select]
parse event2name (last_event ()) with num;
HS2

jqphantom

  • Junior Community Member
  • Posts: 3
  • Hero Points: 0
Re: Copying words to named clipboards
« Reply #3 on: June 29, 2007, 07:30:43 PM »
HS2 --

Thanks for the code, but I am sorry, I cannot make heads or tails of it. How does it work? How do you tie it to specific keystrokes?

Thanks!

Michael M/jqphantom

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: Copying words to named clipboards
« Reply #4 on: June 29, 2007, 08:12:54 PM »
Hi jqphantom,
see attached a vusrmacs.e module to make your life a bit easier :)
Copy it to your VSLICKCONFIG dir or merge it with the exisiting one.
Use 'Macro->Load Module' to load it. After that you can bind e.g. CTRL-ALT-1, CTRL-ALT-2, etc to the copy-word-to-autonamed-clipboard command and e..g CTRL-1, CTRL-2, etc to insert-autonamed-clipboard in the key bindings dialog.
See the comments in the code for the 2nd keybinding method using an 'lead-in' key combination.

Hope it's ok for you. But don't hesitate to ask if it's still confusing. I just don't know your experience level ;)

HS2

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: Copying words to named clipboards
« Reply #5 on: June 29, 2007, 08:36:25 PM »
Sorry - I've forgotten to tell sth. about it...
As an example: 'copy-word-to-autonamed-clipboard' command is bound to 'CTRL-ALT-1'
The 'event2name (last_event ())' part returns the last event as string. In this example this is 'C-A-1' b/c this is the shortcut which invoked the command. Using the incredible 'parse' function we extract the trailing '1' needed for the named clipboard. (If 'CTRl-ALT-2' is  also bound to this command we'd get the '2' here.)
'cur_word' returns the current word also as string and now we got all the things we need.
'text_to_clipboard' is a little helper function (here also a command), which creates clipboard with the given name (the notation 'c'num means that the string 'num' is concatenated to the string 'c'), adds the given text to it. The clipboard type can be CHAR, BLOCK or LINE and defaults to CHAR. This should be ok for word copy/paste operations. BLOCK can also be used if you want. The behaviour on paste might be slightly different and also depends on your emulation (e.g. Brief emulation handles BLOCK type clipboards a bit different).
'insert-autonamed-clipboard' is almost the same except the paste call instead of adding text to a clipboard.
That's it :)

Edit:
I've added a few lines to 'vusrmacs.e' to do all the keybindings by loading the module (but it's disabled by default).
Set the #define DO_KEY_BINDINGS_HERE to '1' and load it if you don't want to add the keybindings manually.

HS2
« Last Edit: June 30, 2007, 08:03:25 AM by hs2 »