Author Topic: simple text-to-clipboard macro  (Read 9017 times)

hs2

  • Senior Community Member
  • Posts: 2762
  • Hero Points: 292
simple text-to-clipboard macro
« on: October 21, 2006, 09:02:24 AM »
Hello Slick-C Code Monsters ;)

time ago I needed a macro to put some arbitrary text to the clipboard to be pasted in another app.
It's not big deal as you can see, but I think it could be useful for daily work.

I use the ccs example macro quite often when communicating w/ other developers (IM,eMail) referring to specific code locations.
Code: [Select]
// clipboard_type == 'CHAR' , 'LINE' or 'BLOCK'
_command int text_to_clipboard (_str text = '', _str clipboard_type = 'CHAR', _str clipboard_name = '') name_info(','VSARG2_REQUIRES_EDITORCTL)
{
   // 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);
}

// example 1: C-opy C-urrent S-ource location to clipboard (C / C++ tested)
_command void ccs () name_info (','VSARG2_MARK|VSARG2_TEXT_BOX|VSARG2_REQUIRES_EDITORCTL|VSARG2_READ_ONLY)
{
   _str cur_proc = '', cur_class = '';

   cur_proc = current_proc ( false );
   cur_class = current_class ( false );

   if ( length ( cur_class ) )
      cur_proc = cur_class :+ '::' :+ cur_proc;

   _str cur_src = strip_filename (p_buf_name,'DP') " - " cur_proc "() [line " p_RLine "]:";
   text_to_clipboard ( cur_src );
   message ( "'" cur_src "' copied to clipboard")
}

// sometimes the single filename is not unique (in larger projects) -> filename relative to the project maybe useful
// example 2: C-opy C-urrent B-uffer relative to P-roject to clipboard
_command void ccbp () name_info (','VSARG2_MARK|VSARG2_TEXT_BOX|VSARG2_REQUIRES_EDITORCTL|VSARG2_READ_ONLY)
{
   _str prjbuf = absolute ( strip ( _ProjectGet_WorkingDir (_ProjectHandle (_project_name)) ) );
   prjbuf = relative( p_buf_name, prjbuf );
   text_to_clipboard ( prjbuf );
   message ( "'" prjbuf "' copied to clipboard")
}

BTW: You can use 'text-to-clipboard' even on cmdline.

HS2
« Last Edit: October 21, 2006, 09:32:12 AM by hs2 »

hs2

  • Senior Community Member
  • Posts: 2762
  • Hero Points: 292
Re: simple text-to-clipboard macro
« Reply #1 on: January 05, 2012, 12:30:29 AM »
While ago I've added some minor features. So here we go:
Code: [Select]
/**
 * helper for using the clipboard in user macro functions/commands
 *
 * @param text             text to copy/append to clipboard
 * @param doAppend         append given text to the current clipboard
 * @param clipboard_type   'CHAR' , 'LINE' or 'BLOCK'
 * @param clipboard_name   usually ''
 * @param quiet            print status message or not
 *
 * @return  0: OK
 *          TEXT_NOT_SELECTED_RC: empty text - nothing to copy/append
 */
_command int text_to_clipboard( _str text = '', boolean doAppend = false, _str clipboard_type = 'CHAR', _str clipboard_name = '', boolean quiet = false ) name_info(','VSARG2_REQUIRES_EDITORCTL|VSARG2_READ_ONLY)
{
   // s.th. to copy ?
   if ( length( text ) )
   {
      if ( !doAppend )  push_clipboard_itype( clipboard_type,clipboard_name,1,true );
      append_clipboard_text( text );
      if ( !quiet ) message( "'" text "' " (doAppend ? "appended" : "copied") " to clipboard [" clipboard_type "]" );
      return 0;
   }
   else return TEXT_NOT_SELECTED_RC;
}

Have fun, HS2