jed-users mailing list

[2003 Date Index] [2003 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

Re: optional arguments


On Wed, Jun 11, 2003 at 06:03:39PM +0000, Joerg Sommer wrote:
> G|nter Milde <milde@xxxxxxxxxxxxxxxxxxxx> wrote:
 
> > Unfortunately, menu_append_item doesnot set _NARGS right, when calling a
> > function with the 4th argument as argument. So use with caution.
> 
> Must be _NARGS set by hand or is this done by slang?

_NARGS can only be set by slang and is available/sensible only within a
function.

It was not present in early versions of SLang, so there are some historic
functions that still need the number of arguments to be given as last
argument.

Some functions calling functions might confuse _NARGS, I know of
menu_append_item() and menu_insert_item(). Consider this as equivalent to
slang code doing
  3; foo()
where within foo, _NARGS is 0. The only workaround I know is not to use
funktions with other than the default argument in a menu entry, i.e. define
an auxiliary function with a fixed number of args (maybe zero).

If you are using _NARGS a lot (as I do), you might find the function
push_defaults usefull, which is in sl_utils.sl from jedmodes.sf.net.

I use it especially for more than one optional argument, as e.g.

% Rebind all keys that are bound to old_fun to new_fun
define rebind() % (old_fun, new_fun, keymap=what_keymap(), key_prefix="")
{
   variable old_fun, new_fun, keymap, key_prefix;
   (old_fun, new_fun, keymap, key_prefix) =
     push_defaults( , , what_keymap(),"", _NARGS);
...

(Which also uses the  "( , , ..." to "(NULL, NULL, ..." expansion for lazy
typists).

Drawback of this approach is, that the default has to be evaluated also if
the optional argument is given (which can be avoided with the if(_NARGS > 2)
construct). Thus I do not use it in time-critical situations.

G|nter


-- 
Milde at ife.et.tu-dresden.de

--------------------------
To unsubscribe send email to <jed-users-request@xxxxxxxxxxx> with
the word "unsubscribe" in the message body.
Need help? Email <jed-users-owner@xxxxxxxxxxx>.


[2003 date index] [2003 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]