Author Topic: [Question] don't know how to write a surrounded comment macro  (Read 6922 times)

Gilvin

  • Community Member
  • Posts: 86
  • Hero Points: 1
hi all. this confused me for 5 days..

assume that there is a line of code i modified (added, removed):

Code: [Select]
printf ("hello world\n");
and i want a macro that when i select the line and press a fn key (F8, F9...etc), it will be like this:

(x x x is a constant number, never be changed, but yyy will be changed whenever i have to)
Code: [Select]
//[-start-yymmdd-AA0xxx0yyy-modify]//
printf ("hello world\n");
//[-end-yymmdd-AA0xxx0yyy-modify]//

or

Code: [Select]
//[-start-yymmdd-AA0xxx0yyy-add]//
printf ("hello world\n");
//[-end-yymmdd-AA0xxx0yyy-add]//

after 5 days after work trying, the result is a failure... can someone teach me, thanks

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Re: [Question] don't know how to write a surrounded comment macro
« Reply #1 on: August 02, 2010, 06:48:56 PM »
Which parts of the task are you having trouble with?

Here is something that may help you out.  This is a helper function I use to surround the current line block selection with a "before" and "after" line passed as arguments:
Code: [Select]
/**
 * Surrounds the current LINE type selection with the <i>sBefore</i> and
 * <i>sAfter</i> lines.  For example 'SurroundLineBlock("#if 0","#endif")'.
 *
 * @param sBefore    Line to insert before the current selection.
 * @param sAfter     Line to insert after the current selection.
 */
static void SurroundLineBlock( _str sBefore, _str sAfter )
{
   if ( _select_type() == 'LINE' )
   {
      typeless p;
      _save_pos2( p );

      _begin_select();
      up();
      insert_line( sBefore );

      _end_select();
      insert_line( sAfter );

      _deselect();

      _restore_pos2( p );
   }
   else
   {
      message( 'No line block.' );
   }
}

Next you can construct before and after strings to match your template, and pass them to this routine to surround the line block selection.

Gilvin

  • Community Member
  • Posts: 86
  • Hero Points: 1
Re: [Question] don't know how to write a surrounded comment macro
« Reply #2 on: August 03, 2010, 02:10:10 AM »
i've tried using alias to do the job, but when i turned on the "surround with" checkbox, slickedit wouldn't expand my alias, so i tried again using macro

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Re: [Question] don't know how to write a surrounded comment macro
« Reply #3 on: August 03, 2010, 02:31:18 AM »
Which parts of the task (of writing a macro to do this) are you having trouble with?
Does the helper function I posted enable you to finish writing a working macro?
If not, are there specific things that are problematic?

Gilvin

  • Community Member
  • Posts: 86
  • Hero Points: 1
Re: [Question] don't know how to write a surrounded comment macro
« Reply #4 on: August 03, 2010, 04:58:03 AM »
it helps. the surround with in your macro is great. but i don't know how to add date in YYMMDD format, in slickedit there's a default MMDDYY, and i don't know how to modify it

hs2

  • Senior Community Member
  • Posts: 2762
  • Hero Points: 292
Re: [Question] don't know how to write a surrounded comment macro
« Reply #5 on: August 03, 2010, 09:21:39 AM »
@Gilvin: Try this add. helper macro I'm using:
Code: [Select]
/**
 * get compact date
 *  
 * @param delim   opt. delimiter of date tokens
 * @param yyyy    true: 4 digit year - false: 2 digits
 * @param lzero   leading zero of each date token/number
 *
 * @return  compact date string
 */
static _str getcdate ( _str delim = '', boolean yyyy = false, boolean lzero = true )
{
   _str month, day, year;
   parse _date() with month'/'day'/'year;
   if (lzero && length(month)<2) month='0'month;
   if (lzero && length(day)<2)   day='0'day;
   if (length(year)>2 && !yyyy)  year=substr(year,3);
   return ( year delim month delim day );
}

Your desired format is returned when using the defaults e.g. _str mydate = getcdate();
Good luck, HS2
« Last Edit: August 03, 2010, 09:24:33 AM by hs2 »

Gilvin

  • Community Member
  • Posts: 86
  • Hero Points: 1
Re: [Question] don't know how to write a surrounded comment macro
« Reply #6 on: August 03, 2010, 04:32:57 PM »
wow, thanks a lot ;D