Page 6 of 10

Re: Scripting: User-Defined Functions

Posted: 20 Apr 2015 10:55
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

Re: Scripting: User-Defined Functions

Posted: 20 Apr 2015 11:51
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

Re: Scripting: User-Defined Functions

Posted: 21 Apr 2015 09:33
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. :|

Re: Scripting: User-Defined Functions

Posted: 21 Apr 2015 09:59
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.

Re: Scripting: User-Defined Functions

Posted: 21 Apr 2015 10:32
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.

Re: Scripting: User-Defined Functions

Posted: 21 Apr 2015 10:36
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!");

Re: Scripting: User-Defined Functions

Posted: 21 Apr 2015 11:05
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.

Re: Scripting: User-Defined Functions

Posted: 21 Apr 2015 12:45
by bdeshi
keep pressing: append blindly?

keeping a new line at the end of included files can solve it.

Re: Scripting: User-Defined Functions

Posted: 21 Apr 2015 16:56
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

Re: Scripting: User-Defined Functions

Posted: 21 Apr 2015 20:04
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.

Re: Scripting: User-Defined Functions

Posted: 26 Apr 2015 21:51
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".

Re: Scripting: User-Defined Functions

Posted: 26 Apr 2015 22:50
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>;

Re: Scripting: User-Defined Functions

Posted: 26 Apr 2015 23:28
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:

Re: Scripting: User-Defined Functions

Posted: 26 Apr 2015 23:55
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;

Re: Scripting: User-Defined Functions

Posted: 27 Apr 2015 00:29
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...)