Author Topic: Current context/function  (Read 234 times)

joecar

  • Senior Community Member
  • Posts: 308
  • Hero Points: 9
  • engineer/gearhead
Current context/function
« on: August 25, 2021, 09:05:11 pm »
Hi,

Is there a way to paste in a debug print that includes the current context/function name (i.e. similar to doing rightclick->Generate Debug Statement For xxxxx (where xxxxx is under the cursor))...?

I'm using a C compiler that does not define __func__ or __FUNCTION__ .

Running SE 25.0.2.0.

Thanks
Joe

Graeme

  • Senior Community Member
  • Posts: 2631
  • Hero Points: 330
Re: Current context/function
« Reply #1 on: August 25, 2021, 10:23:35 pm »
maybe this

      _str cur_proc = '', cur_class = '';

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


Graeme

  • Senior Community Member
  • Posts: 2631
  • Hero Points: 330
Re: Current context/function
« Reply #2 on: August 25, 2021, 10:36:15 pm »
You need to call insert_text(current_proc()) from a macro.

Below is some code I wrote a long time ago (don't know if it still works) to add trace statements to all open buffers of "C" family type.  I think it requires the defs toolbar to be showing functions only.


Code: [Select]
int trace_id;

_str trace_cross_ref[];

_command add_trace() name_info(','VSARG2_MACRO|VSARG2_MARK|VSARG2_REQUIRES_MDI_EDITORCTL)
{
   top();
   int line1;
   _str filen = strip_filename(p_buf_name,'DP');
   while (next_tag('Y') == 0)
   {
      find('{','I?');
      _deselect();
      end_line();
      c_enter();
      begin_line();
      _insert_text("XTRACE(" :+ (_str)++trace_id :+ ');');
      line1 = p_line;
      find('{','I-?');
      _deselect();
      find_matching_paren(true);
      keyin_enter();
      cursor_up();
      _insert_text("XTRACE_EXIT(" :+ (_str)trace_id :+ ');');

      _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;

      trace_cross_ref[trace_cross_ref._length()] =
         (_str)trace_id :+ ' lines: ' :+ field(line1,5) :+ ' ' :+ field(p_line,5) :+
             ' ' :+ field(cur_proc,35) :+ '  ' :+ field('"' :+ filen :+ '"', 16) :+  '  "' :+ p_buf_name :+ '"';
   }
}

_str trace_files[];


int add_to_trace_list()
{
   if (p_extension == 'c') {
      trace_files[trace_files._length()] = p_buf_name;
   }
   return 0;
}

_command void add_trace_all() name_info(','VSARG2_MACRO|VSARG2_MARK|VSARG2_REQUIRES_MDI_EDITORCTL)
{
   trace_id = 10000;
   trace_files._makeempty();
   trace_cross_ref._makeempty();
   for_each_buffer('add_to_trace_list');
   trace_files._sort('F');
   int k;
   for (k = 0; k < trace_files._length(); ++k) {
      _str ext = get_extension(trace_files[k]);
      if ( ext == 'c' || ext == 'cpp') {
         message('Trace ' :+ field(trace_id,5) :+ ' ' :+ strip_filename(p_buf_name,'DP'));
         edit(maybe_quote_filename(trace_files[k]));
         add_trace();
      }
   }
   edit('trace_cross_ref.txt');
   bottom();
   for (k = 0; k < trace_cross_ref._length(); ++k) {
      keyin_enter();
      _insert_text(trace_cross_ref[k]);
   }
   keyin_enter();
   keyin_enter();
   _insert_text('End cross reference');
   keyin_enter();
}


Dennis

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 3386
  • Hero Points: 465
Re: Current context/function
« Reply #3 on: August 25, 2021, 11:16:30 pm »
I create an language-specific alias for this.  That way you only need to remember the alias name.

1) Document > [lang] Options... > Aliases

2) |+| Create a new alias, like "pf" (for printf)

3) Define it like this:
   
Code: [Select]
xprintf("%\n+(%%d): %\c", __LINE__);
4) This uses %\n for the current function name, but you can also use %\j for the current class, %\j+ for the current class, qualified, %\n- for function with class name, %\n+ for function with qualified class name...

5) If you pull down the menu in the upper-right-hand corner of the Alias editor, you can see what sequences are available.

I also have a "pi" alias like this to quickly generate a print for an integer variable.  It creates multiple cursors, so you just expand it and type the variable name once, then hit Escape to cancel the multi-cursor mode.
Code: [Select]
printf("%\n(%%d): %\|=%%d\n", __LINE__, %\|);

joecar

  • Senior Community Member
  • Posts: 308
  • Hero Points: 9
  • engineer/gearhead
Re: Current context/function
« Reply #4 on: August 26, 2021, 08:23:50 pm »
Thanks, this is exactly what I was looking for, I appreciate your help and suggestions  :)

I made an alias zzprint defined as
Code: [Select]
printf("> %%s():\n", "%\n");%\c
Then when I used it (I press tab and then enter or space), it inserts the following plus one space (pressed enter) or two spaces (pressed space):

Code: [Select]
    printf("> %s():\n", "function_name");
How do I prevent the 1 or 2 spaces from inserting?

Dennis

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 3386
  • Hero Points: 465
Re: Current context/function
« Reply #5 on: August 26, 2021, 11:34:08 pm »
I tried it and did not see any extra spaces, but I was not clear where you meant there was extra spaces inserted, the indentation, or after the semicolon?  Also, if you have Document > C/C++ Options > Editing > Beautify > Beautify alias expansions, that step could be inserting spaces, depending on beautifier settings.

patrick

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 1580
  • Hero Points: 140
Re: Current context/function
« Reply #6 on: August 27, 2021, 02:40:11 pm »
Expanding on what Dennis said about the beautifier, there's a setting for putting spaces statement terminating semicolons which defaults to being on.  So that will put a space after the semicolon, before your cursor.   If you don't want to disable beautifying alias expansions, you can go to Tools -> Beautify -> Edit Current Profile, and then to Spacing -> Statements -> General Statement, and turn off the "Space after semicolon" setting.

Normally a trailing space like that wouldn't be put in.  But we've run afoul of some special casing for alias expansions that tries to preserve spacing around the cursor locations that's needed in other situations.  That semicolon spacing setting only makes sense for when you have "Leave multiple statements on one line", or the "Leave multiple declarations on one line", so I'm starting to think it should be smart enough not to fire that rule for a line with a single statement on it.  Which would take care of this case.  I'll look into changing that.

joecar

  • Senior Community Member
  • Posts: 308
  • Hero Points: 9
  • engineer/gearhead
Re: Current context/function
« Reply #7 on: August 27, 2021, 07:21:26 pm »
I tried it and did not see any extra spaces, but I was not clear where you meant there was extra spaces inserted, the indentation, or after the semicolon?  Also, if you have Document > C/C++ Options > Editing > Beautify > Beautify alias expansions, that step could be inserting spaces, depending on beautifier settings.
After the semicolon.

joecar

  • Senior Community Member
  • Posts: 308
  • Hero Points: 9
  • engineer/gearhead
Re: Current context/function
« Reply #8 on: August 27, 2021, 07:32:17 pm »
Expanding on what Dennis said about the beautifier, there's a setting for putting spaces statement terminating semicolons which defaults to being on.  So that will put a space after the semicolon, before your cursor.   If you don't want to disable beautifying alias expansions, you can go to Tools -> Beautify -> Edit Current Profile, and then to Spacing -> Statements -> General Statement, and turn off the "Space after semicolon" setting.

Normally a trailing space like that wouldn't be put in.  But we've run afoul of some special casing for alias expansions that tries to preserve spacing around the cursor locations that's needed in other situations.  That semicolon spacing setting only makes sense for when you have "Leave multiple statements on one line", or the "Leave multiple declarations on one line", so I'm starting to think it should be smart enough not to fire that rule for a line with a single statement on it.  Which would take care of this case.  I'll look into changing that.
Space after semicolon was on.

Thanks for all your assistance, I really appreciate it!!  :)

joecar

  • Senior Community Member
  • Posts: 308
  • Hero Points: 9
  • engineer/gearhead
Re: Current context/function
« Reply #9 on: August 27, 2021, 07:40:05 pm »
. . .
I also have a "pi" alias like this to quickly generate a print for an integer variable.  It creates multiple cursors, so you just expand it and type the variable name once, then hit Escape to cancel the multi-cursor mode.
Code: [Select]
printf("%\n(%%d): %\|=%%d\n", __LINE__, %\|);
Your post just made me realize how to use %\| (i.e. multiple times), thanks for the insight.
« Last Edit: August 28, 2021, 08:06:40 pm by joecar »

joecar

  • Senior Community Member
  • Posts: 308
  • Hero Points: 9
  • engineer/gearhead
Re: Current context/function
« Reply #10 on: August 27, 2021, 07:43:21 pm »
Graeme,

Thanks for the SlickC code fragments  :)

I currently don't know how to program SlickC and I don't want to break SlickEdit, I rely on it for my job.

Graeme

  • Senior Community Member
  • Posts: 2631
  • Hero Points: 330
Re: Current context/function
« Reply #11 on: August 28, 2021, 11:10:27 am »
Sure, much better to use an alias.  I should pay more attention to aliases.  You can call a macro from an alias if you need :)  e.g.
%\M_workspace_path%
or from the help file
%\m sur_text%

sur_text is a global function that returns void.  I don't know what the difference between upper and lowercase M is but _workspace_path is a global function that returns a string that will be added to your source file.