Author Topic: Invoking Functions with parameters in Aliases  (Read 7958 times)

PlanetPratt

  • Senior Community Member
  • Posts: 145
  • Hero Points: 6
Invoking Functions with parameters in Aliases
« on: September 22, 2007, 07:46:29 am »
I have written a couple of aliases that reference functions and they work great, but I'm trying to write one that has to pass an alias parameter as an argument to the function and I can't figure out the correct syntax.  The function I'm trying to call is:

Code: [Select]
_command _str capitalize (_str string = "") {
  return upcase(substr(string,0,1)) :+ substr(string,1);
}

I defined a couple of alias parameters attr = "size" & type = "String"

And the the alias:

Code: [Select]
public void set%\m capitalize %(attr) % (%(type) %(attr)) {
  this.%(attr) = %(attr);
}

which seems to follow the syntax outlined in the help file, it throws a Slick-C error with a Stack trace that says there's an invalid argument.  If I modify it slightly, using parens, which seems to look right I tried:

Code: [Select]
public void set%\m capitalize(%(attr))% (%(type) %(attr)) {
  this.%(attr) = %(attr);
}

There's no error, but the output isn't what was expected.

Code: [Select]
  /**
   * Set the value of the size attribute.
   *
   * @param size The new size value as a String
   */
  public void set(attr))(type) size) {
    this.size = size;
  }

  (*Chris*)
« Last Edit: September 22, 2007, 07:49:32 am by PlanetPratt »

hs2

  • Senior Community Member
  • Posts: 2759
  • Hero Points: 292
Re: Invoking Functions with parameters in Aliases
« Reply #1 on: September 22, 2007, 11:05:49 am »
Nice idea - code generation powered by 'alias' :)
But AFAIK 'alias' is more like a (sophisticated) single-pass text replacement.
The given parameters are just queried and replaced in-line, macro args need to be constant values (i.e. nesting other '%' items is not supported) and their return values are inserted in the resulting text at the position where the macro is invoked.
Therefore you'd need some tailored macros for in-place (alias-)text manipulation used in aliases.

Applied that to your use case this could be a solution:

alias definition:
Code: [Select]
public void %(attr)%\madd_prefix_and_capitalize_word set% (%(type) %(attr)) {
%\ithis.%(attr) = %(attr);
}

Code: [Select]
_command void add_prefix_and_capitalize_word (_str prefix = '') {
   prev_word(); begin_word();
   _insert_text( prefix );
   select_word();
   cap_selection();
   deselect();
}

The '_command' is optional and can be removed e.g. after debugging the alias-macro...

Have fun,
HS2
« Last Edit: September 22, 2007, 11:33:18 am by hs2 »

PlanetPratt

  • Senior Community Member
  • Posts: 145
  • Hero Points: 6
Re: Invoking Functions with parameters in Aliases
« Reply #2 on: September 22, 2007, 08:16:50 pm »
That works perfectly, that's definitely worth a hero point!!  Thanks.
  (*Chris*)

hs2

  • Senior Community Member
  • Posts: 2759
  • Hero Points: 292
Re: Invoking Functions with parameters in Aliases
« Reply #3 on: September 24, 2007, 02:12:41 am »
@Chris: An older maybe also interesting posting conc. aliases came into mind.
http://community.slickedit.com/index.php?topic=242.msg1521#msg1521

Another thing I found today:
alias expansion is not CaSe sensitive by default. But there is a 'def_alias_case' config var which controls that !
It defaults to 'i' and if it's set to 'e' you get CaSe sensitive aliases.
Unfortunately it's curr. not referenced in the alias related help, but the variable itself is documented.

@Lisa: I think it's worth adding a link to 'def_alias_case' in the 'alias' topic.

HS2

Lisa

  • Senior Community Member
  • Posts: 238
  • Hero Points: 24
  • User-friendly geek-speak translator extraordinaire
Re: Invoking Functions with parameters in Aliases
« Reply #4 on: September 24, 2007, 01:47:12 pm »
@Lisa: I think it's worth adding a link to 'def_alias_case' in the 'alias' topic.

Agreed and noted!