Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. file info. rss. print.

Have FUN with PRO scripts
In my previous article I described the basic functionality of the Intellisense manager. However, it is possible to do a lot more with it. When you browse through the foxcode table you could see that there are several kind of recordtypes. One of them is an “S” record. This stands for Script. Basically they do the following. All the records get a parameter oFoxCode. This is a reference to an object that has some basic information. There is some info about the place where you type in some code. This is the oFoxCode.Location. See my previous article for more info on that. There is some info you can place in the object about the valuetype you want to return. Then there is some info that comes from the calling record. A script contains code that is running and returns a value.

Looking at those records we see that one of the fields is a “user” field of the memo type. Those “user” fields are to be used by you, the user. Other VFP tools, like the databasecontainer have such a field as well. You can use that field to extend the functionality of any table. Thinking about the possibilities I guess it would be nice to have one piece of code to create, as an example, a function or a procedure.

So, if I type “FUN” this should expand to a function with a header parameters and everything that goes with it. Once I type “PRO” I would like to have a complete block of code to create my procedure.

Yeah, I’m lazy, always have been, that’s why I am such a good programmer, you know the saying: ”Good programmers rather are lazy then tired!”. That’s why I like OOP so much, write once use a gazillion times... OK, enough talking. Let’s write some code for that script.

Open the Intellisense Manager and go to the Custom tab. Under the Replace type in the name of your script, in this case I choose “Document”. Before pressing the ADD button however we have to let VFP knbow that this is a script. On the bottom there is a combo that standard has “Command” as an option, choose “Script” here.

Next, press ADD. Now we have a record for a script. Press the Script button and type in the following code.

LOCAL lcParam, lcParamDescript, lcType, lnI, lcName, lcDescription, lnParameters, lcRetVal

lcParam = ""
lcParamDescript = ""

* Run everywhere but not in the command window
IF oFoxCode.Location = 0
    return "FUN" 

* we want to return a value
oFoxcode.valuetype = "V"

* read the user field in the record that called this script.
* what do you want to create, a FUN or a PRO.
IF oFoxCode.user = "F" && point 1
    lcType = "FUNCTION"
    lcType = "PROCEDURE"

* get the name
lcName = inputbox(lcType+" Name:", _screen.caption)

* and some descriptive description
lcDescription = inputbox(lcType+" Description:",_screen.caption)

* read the number of parameters
lnParameters = val( inputbox(lcType+" Number of parameters:",_screen.caption,"1") )

* some code to execute when there are 1 or more parameters
IF lnParameters > 0
    FOR lnI = 1 to lnParameters
        * fill the lcParam variable
        lcParam = lcParam+inputbox("Parameter "+alltrim(str(lnI))+" Name",_screen.caption)+", "
        * some descriptive text for the parameter would be nice for documenting purposes.
        lcparamDescript = lcparamDescript+"Parameter "+alltrim(str(lnI))+": "+;
            inputbox("Parameter "+alltrim(str(lnI))+" Description: ", _screen.caption) +chr(13) +chr(10) +"* "
    * strip off the last ", " from the parameter list
    lcParam = substr(lcParam,1, len(lcParam)-2)

* make a block of text to return
* <<lcType>> <<lcName>>( <<lcparam>> )
* Date : <<DATE()>>, <<time()>>
* author : <<GETENV("USERNAME")>>
* description: <<lcDescription>>

****** PARAMETER BLOCK **************
* Parameters : <<alltrim( str(lnParameters))>>
* <<lcParamDescript>>
<<lcType>> <<lcName>>( <<lcParam>> )
LOCAL lcRetVal
&&~code goes here~



What happens here is that in the record calling this script I filled the user-field with a value, either P for Procedure or F for function. That is done at point 1. I'll get back on that one later on in this article.

In the textmerge block I use all the parameters I created before to place in the outputblock. Also, I read some environmental info with the getenv() function. The variable I use, "USERNAME" is a standard in NT and Win2K. For those who use Win9x or ME you could create this variable yourself by placing a line in the autoexec.bat USERNAME=My Name. Another option of course is the sys(0) function.

Calling the Script

A script is of no use if we can not call it. Therefor we create two records, one with the command PRO and the other with the command FUN. Once added we will edit them. What we see is a browse, in the cmd field we filll in {document}, in the user-field we fill in “F” (without the quotes) when we are in the FUN record and “P” when we are editing the PRO record. Close this browse window, you should be back now in the Manager form. Once you click the Edit button once more you should see two records. The first record is the FUN or PRO record, the second is the DOCUMENT record, containing the script. Easy maintenance that is.

About the Data Field
When you look at the record for the FUN or the PRO there is nothing in the datafield. However, it is possible of course to place some executable code there as well. This is not fired automatically though. Here again come the oFoxCode object.

In the script you could place some code like:
IF not empty( oFoxCode.Data)

In that data field could be a call to an external procedure or even to creating an external object...

As always, your fantasy is the limit with VFP, with a smart tool like the IntelliSense Manager you can probably do more then you can imagine now.


Boudewijn Lutgerink Programming is one of the many hobbies of Boudewijn. He has worked with computers since 1985 and is the author of two books from Sybex. He has a weblog at http://weblogs.foxite.com/boudewijnlutgerink.


Your Name: 
Your Feedback: 

Spam Protection:
Enter the code shown: