Scripting: User-Defined Functions

Features wanted...
Post Reply
PeterH
Posts: 2776
Joined: 21 Nov 2005 20:39
Location: Germany

Re: Scripting: User-Defined Functions

Post by PeterH »

...talking about include...

It would be fine to expand the sysntax!

I was glad that I can include a bit of code by Include - but it is inconvenient to create an include file for just 1 or a few lines. Often in parallel to an existing file defining some functions.

So it would be fine to be able to:
- define some lines in a script file as an "Include Group", with a label naming it
- add 2nd operand to Include to address such a group (Analog to Load command.)

This way each file could define multiple "Include Groups"), and even a usual script could also supply includes.

Today we have multiple-script and -function files. This way we would add multiple-include to this.
(And yes: I would prefer to define all this in one file, be it .xys or .inc 8) )

By the way: since some days I'm converting some old sub-scripts, including their use of perm and global variables, to functions!
Very fine - can't say it often enough :D
W7(x64) SP1 German
( +WXP SP3 )

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: Scripting: User-Defined Functions

Post by bdeshi »

PeterH wrote:By the way: since some days I'm converting some old sub-scripts, including their use of perm and global variables, to functions!
Very fine - can't say it often enough :D
Yep, same here. These new features stand to make both xyscripts and <xyscripts> much cleaner! :D
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

admin
Site Admin
Posts: 60357
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Scripting: User-Defined Functions

Post by admin »

SammaySarkar wrote:I really need include cmd to be available for one-liners. I've saved some frequent functions as an inc file, but none can be used in addressbar quickscripts because include wants to eat the whole line.

It's possible to script an alternative but nothing as simple as include "inc\xys.inc"
Don't understand. :|

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: Scripting: User-Defined Functions

Post by bdeshi »

Stop include from demanding the whole line for itself.

When we use include in a single-line script, nothing else is possible because include requires the whole line.

I suggest include also works like this:

Code: Select all

::Include "fun.inc"| text 'hello';
so the command can be used in singleline scripts.

Why the | ? I suppose you search the script resource using a pattern to find-and-replace Include statements at runtime. The pattern-matching doesn't stop until encountering a newline.
Let it stop when it finds the | character. It can't be used in filenames.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

admin
Site Admin
Posts: 60357
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Scripting: User-Defined Functions

Post by admin »

I see your intention. But doing this could be a can of worms. I rather don't include a file into the middle of a line.

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: Scripting: User-Defined Functions

Post by bdeshi »

Can you make an exception when Include is the very first statement and at the beginning of line ?

Code: Select all

::include "Fun.inc"| text customecho("I promise I won't (can't) use any more Includes!");
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

admin
Site Admin
Posts: 60357
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Scripting: User-Defined Functions

Post by admin »

Not possible. The trailing part cannot be meaningfully appended to the contents of all possible included files. XY cannot know what "Fun.inc" is and it does not want to know.

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: Scripting: User-Defined Functions

Post by bdeshi »

keep pressing: append blindly?

keeping a new line at the end of included files can solve it.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Scripting: User-Defined Functions

Post by TheQwerty »

admin wrote:
TheQwerty wrote::bug: XY can crash when you start using a dozen or more arguments.
Yes. Will be handled smoother in next beta.
Erm.. Sorry I forgot to check on this sooner but with v15.00.0501 the script doesn't even run when a function with more than 11 arguments is declared.

I suppose it's smoother than a crash but I think I preferred the crash simply because any feedback is better than none. :P

admin
Site Admin
Posts: 60357
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Scripting: User-Defined Functions

Post by admin »

Ah! The error message was generated but not shown. Pfff. Well, the feature is still inofficial and in progress... 15.10 will be the day, uh, version.

PS: Fixed in next beta.

Stef123

Re: Scripting: User-Defined Functions

Post by Stef123 »

Cannot get the include statement to work in 15.00.0507
The file "<xyscripts>\MyFunctions.xyi" does exist
When I enter the following line at the top of a multi-script file.xys with multi-line sub-scripts:

Code: Select all

include MyFunctions.xyi
I get an error that the hosting file.xys "does not contain any visible scripts."

Depending on various syntax variations I tried - with and w/o trailing semicolon, quotes, path hardwired - I sometimes also get the error "include file not found".

highend
Posts: 13274
Joined: 06 Feb 2011 00:33

Re: Scripting: User-Defined Functions

Post by highend »

Do you use a non indented line with a script command after that include?

E.g. this doesn't work:

Code: Select all

include "_functions.inc"

    text "hello";
But this works:

Code: Select all

include "_functions.inc"

text "hello";
    <more commands>;
One of my scripts helped you out? Please donate via Paypal

Stef123

Re: Scripting: User-Defined Functions

Post by Stef123 »

Ha, yes indeed. Upon closer examination you are right. When I make myself ignore all my comments on top, the original file starts like this:

Code: Select all

/*** image file menu ***/
  $image = <<<IMAGE
I did not dare to change the indentation, because this is part of a larger script file, and I thought with Heredoc and all those syntax rules there is probably a good reason why this script template came with indented lines. Turns out, I can safely flush it to the left, and then "include" works.

However, if I also flush other, similar lines to the left, I get weird results, no longer seeing my menu entries, but rather the script code itself is showing on my context menu. :veryconfused:

highend
Posts: 13274
Joined: 06 Feb 2011 00:33

Re: Scripting: User-Defined Functions

Post by highend »

No... The first non "include ..." line must be non indented (this is only necessary if you include something, if not you can still begin with an indented line), not any other (apart from other include commands or subscript sections "<subscript 1>", etc.).

Btw, heredocs don't need a non indented opening line but a non intended closing (if they are the first line after an include this changes).

So again:
Works:

Code: Select all

include "_functions.inc"

$a = <<<>>>
hello world
>>>;

    text $a;
Doesn't:

Code: Select all

include "_functions.inc"

    $a = <<<>>>
hello world
>>>;

    text $a;
One of my scripts helped you out? Please donate via Paypal

PeterH
Posts: 2776
Joined: 21 Nov 2005 20:39
Location: Germany

Re: Scripting: User-Defined Functions

Post by PeterH »

In the conversations we had over time, I came to the conclusion that every script should have a Caption / Label, so that I think you are on the safe side with:

Code: Select all

include "_functions.inc"

"Main"
    $a = <<<>>>
hello world
>>>;

    text $a;
(Don't forget to indent the $a= in this case...)
W7(x64) SP1 German
( +WXP SP3 )

Post Reply