Using an External Text Editor With QB45

Compiling and Running QB Programs from Within Programmer's Notepad
(This portion first appeared HERE)

I previously wrote how I had been inspired to use the methods found in Issue #2 of QB Express to liberate QB from DOS. Since that time I have stopped thinking of liberating QB and started thinking of ways to enhance the QB experience. In this article I describe how I write, compile, and run QB programs within a WindowsXP environment.

The first thing you need to decide on is what text editor you want to use as a programming environment. For the last few years I have used Crimson Editor almost exclusively, but a few days ago I switched to Programmer's Notepad. Both text editors can be configured to do everything I describe in this article. Each editor has features not found in the other. It is up to you to choose your programming environment, but for the sake of this article I will be using Programmer's Notepad (PN).

In PN, create a text file and type the following line of code:
PRINT "Hello, Windows!"
Save this as "hello.bas" anywhere on your computer. Now we have created QuickBASIC source code in our editor, but what can we do with it?

Press Ctrl+N to create a new document. Type (or copy and paste) the following text into the new file:
@ECHO OFF
:ABOUT
::      O---------------------------------------------O
::      |compileMaker.bat v1.0 generates compile%1.bat|
::      |which can then be used to compile your       |
::      |QuickBasic project.                          |
::      |                                             |
::      |This batch file was created by Jason         |
::      |Bales (gamerplusplus@gmail.com)              |
::      |http://gamerpp.blogspot.com/                 |
::      O---------------------------------------------O

SETLOCAL
PROMPT
TITLE COMPILE MAKER IS NOW RUNNING
COLOR 1F

:VARIABLES
REM %1 IS YOUR BASIC FILE NAME
REM %optiones% SHOULD BE YOUR ADDED COMMAND-LINE COMPILE OPTIONS LIKE /X/E
REM Make sure to change all paths so they point to
REM the right place on your computer
SET BC=C:\QBasic\BC.EXE
SET LINK=C:\QBasic\LINK.EXE
SET BCOM=C:\QBasic\BCOM45.LIB

:: get command-line options
CLS
SET /p optiones="Enter any special command-line options: "

:makeIt
> COMPILE%1.BAT ECHO @ECHO OFF
>>COMPILE%1.BAT ECHO REM Run this batch file to compile your .bas file
>>COMPILE%1.BAT ECHO.
>>COMPILE%1.BAT ECHO :: Compile and Link
>>COMPILE%1.BAT ECHO CALL %BC% %1.bas %optiones%/o;
>>COMPILE%1.BAT ECHO CALL %LINK% %1.OBJ, %1.EXE, , %BCOM%
>>COMPILE%1.BAT ECHO.
>>COMPILE%1.BAT ECHO :: Do a little cleanup
>>COMPILE%1.BAT ECHO DEL %1.OBJ
>>COMPILE%1.BAT ECHO DEL %1.MAP

ENDLOCAL
Save this file as "compileMaker.bat". I recommend saving it to your QB install folder. Make sure you change the paths in this batch file so they are correct on your system.

This batch file creates another batch file in your .BAS file's directory. This new batch file will be used to compile your code.

Before we can use this batch file we will need to create a tool to pass our file name and path to compileMaker.bat. In PN, click Tools>Options. A new window should pop open. In this, the Options window, select Tools and then click the Add button you see on the right.
Yet another window will open.
Make the window look like this:
Make sure that the Command section of this tool contains the full path, file name, and extension to compileMaker.bat. You can click the button to the right of the field to browse to the file. You can choose whatever shortcut keys you like.

In this window click the Console I/O tab and make sure the Capture output checkbox is un-checked.
Now click on OK in the tool properties window and then the OK in the Options window.

If you click on Tools you should now see your new tool. Every time you create a tool for .BAS files, it will appear here.
With our hello.bas file selected in PN, run the Create compile%1.BAT tool. A console window should pop open:
If you needed to send command-line options to BC, this is where you would type them. Since hello.bas only has a print statement, just press return.

Running this tool creates a batch file in the same directory as our code. If we run that new batch file we can compile our code into an executable. One way of running batch files is to navigate in Windows explorer to the file and double click it. Another way of running the file is to set up a user tool.

In PN, click Tools>Options. Click OK once you've made the properties window look something like this:
As with the previous tool, select the Console I/O tab and make sure the capture output option is un-selected.

Those of you actually following along will know what's coming next...

In PN, press Ctrl+N and type the following text:
@ECHO OFF

REM Pass the file title into this batch to compile.

:: If there's no COMPILE%1.BAT...
:: Make the COMPILE%1.BAT
IF NOT EXIST COMPILE%1.BAT CALL C:\QBasic\compileMaker.bat %1

:: Compile our .BAS file
CALL COMPILE%1.bat

PAUSE

Save This file in your QB install folder as "batchCompile.bat". Make sure the path to compileMaker.bat is correct for your computer.

Now run your new tool (find it in the Tools menu or use your hotkeys). If all went well you should see a prompt like this:
What this means is you just compiled a QuickBASIC program!

To run the program, we will make yet another tool. In PN, click Tools>Options and make the options window look like this:
Make sure the command portion of the tool points to your QB install folder. Again, on the Console I/O tab, de-select the capture output option. Click OK then OK again.

Back in PN, press Ctrl+N and enter the following text:
@ECHO OFF
CLS
PROMPT
COLOR 1F
TITLE "%~N1" IS NOW RUNNING...

REM Use this simple batch to run command-line programs
REM from explorer and programming editors.
REM Pass into it the full path with file name and extension.

CALL %1

ECHO.
ECHO.
PAUSE
Save this as CommandLineExecutor.bat in your QB install folder.

In PN, select hello.bas again and use your newest tool to run your compiled program:


Two IDE's Are Better Than One
(this part of the page first published HERE)

In my last article I showed how to use Programmer's Notepad with BC.exe (the QuickBASIC compiler). Some people might have gotten the impression I was trying to do away with the QuickBASIC IDE. That's just not true! I have chosen to see QuickBASIC as a programming language that happens to have an IDE. The QB IDE has a built-in help system and advanced debugging tools (tools that do not work in WindowsXP). It also has a cumbersome text editor that is difficult or impossible to use on many current computers. This is especially true for my laptop.

Luckily, the QuickBASIC IDE can be set to save and open QB projects as plain text. These plain text files can be opened, modified, and saved by modern text editors. Using a combination of QB.EXE and modern text editors, working in tandem, you can significantly increase productivity on newer computers. This is especially true for large, multi-file QB projects. Then there are advantages in terms of memory and performance, but I'll get into that some other day.

The previous article was an introduction to using Programmer's Notepad(PN) along with BC.EXE. In this article I will introduce the idea of using PN with QB.EXE.

With a BASIC file open in QB.EXE, select File>Save As
A new window will open
In the Format section, select the option marked Text - Readable by Other Programs and click OK. Save your BASIC file.

In PN, you can now open the very same BASIC file for editing.

There are many differences between the QB IDE and PN. PN can make organizing, editing, and compiling multifile QB projects easier. You can also have each file of a multiple module program open in its own tab. There are also some advanced features, including user tools and macros written in Python, which allow you to do things in PN that you cannot do in QB.EXE.

PN will not check your code as you write it the way QB does (I'll work on that later), but PN has autocomplete functionality. Just start typing and you will see a list of words that you might use.

Another cool feature is code folding. To get an idea of what code folding is, look at this code:
And now look at the same code after it's folded:
You see here a program with 141 lines of code can fit in a small space if it's all folded up. Each section of code can be folded or unfolded or all code can be folded or unfolded.
You may also notice that as you type, your text changes color. This is called syntax highlighting. It makes reading code easier. The words recognized by autocomplete and syntax highlighting can be adapted to suit your needs.

If you look around PN, you will find many more useful features. There are windows for projects, tags, and scripts. PN also allows users to create macros and user tools. Of all these features, we will just be looking at user tools in this article.

With your BASIC file open in PN, select Tools>Options and then make sure Tools is selected on the left. Press the Add button. Make the next window that opens look like this:
Make sure the path to QB.EXE is correct. Press OK and then OK again. Press your hotkeys to use the tool. You should now see your code opened in the QB IDE. You can use all the tools of the QB IDE (if you are on an older system) and save the file. When you go back to PN you will see this popup:
If you want to keep the changes you made to your code in the QB IDE, choose Yes. Your updated code will appear in PN.

If you would like to have QB.EXE interpret the code you are working on in PN, you can do that as well.

With your BASIC file open in PN, select Tools>Options and then make sure Tools is selected on the left. Press the Add button. Make the next window that opens look like this:

Again, make sure the path to QB.EXE is correct on your computer. Press OK and then OK again. Press your hotkeys to use the tool. You should now see your code interpreted by the QB IDE. If there are errors in your code, the IDE will let you know with a message like this:
If there are no errors your code will execute. When finished, your code will be opened in the IDE.

NOTE: If you do not want your interpreted program to exit to the QB IDE, make your code end with the keyword SYSTEM.

Adding QB SUBs and FUNCTIONs in PN
(this portion of the page was first published HERE)

One nice thing about using PN for a programming environment is how easy it is to create and use simple tools as you need them. In this article I will walk you through the creation of a programming tool that will allow you to insert a SUB or FUNCTION while working with .BAS files in PN.

Open a new document in PN and type (or copy and paste) the following text into the file:
@ECHO OFF
:About
::      O--------------------------------------------------------------O
::      |subShop.bat helps you create QB SUBs and FUNCTIONs in Crimson |
::      |           Editor. Pass in your source file as %1.            |
::      |                                                              |
::      |            This file was created by Jason Bales              |
::      |                   gamerplusplus@gmail.com                    |
::      |                 http://gamerpp.blogspot.com/                 |
::      O--------------------------------------------------------------O

CLS
SETLOCAL
PROMPT
COLOR 47
TITLE WELCOME TO THE SUB SHOP!!

:: Are we to make a Sub or a Function?
SET /p subOrFunction="Are we to make a Sub or a Function?: "
CLS

:: What is the name of the Sub or Function?
SET /p name="What will we call it?: "
CLS

:: What are we passing in?
SET /p parameters="What are we passing in?: "

:: Simple choice
IF %subOrFunction% == SUB GOTO SUB
IF %subOrFunction% == sub GOTO SUB
IF %subOrFunction% == Sub GOTO SUB
IF %subOrFunction% == SUb GOTO SUB
IF %subOrFunction% == suB GOTO SUB
IF %subOrFunction% == sUB GOTO SUB
IF %subOrFunction% == sUb GOTO SUB

REM Notice you only need to type SUB if you want a SUB.
REM Any other input creates a FUNCTION.

:: Write a FUNCTION
> SCRATCH.TXT ECHO DECLARE FUNCTION %name% (%parameters%)
>>SCRATCH.TXT TYPE %1
>>SCRATCH.TXT ECHO.
>>SCRATCH.TXT ECHO ' ____________________
>>SCRATCH.TXT ECHO FUNCTION %name% (%parameters%)
>>SCRATCH.TXT ECHO    ' Write your code here
>>SCRATCH.TXT ECHO %name% = ' Make sure the function returns something
>>SCRATCH.TXT ECHO END FUNCTION

> %1 TYPE SCRATCH.TXT
GOTO FIN

:: Write a SUB
:SUB
> SCRATCH.TXT ECHO DECLARE SUB %name% (%parameters%)
>>SCRATCH.TXT TYPE %1
>>SCRATCH.TXT ECHO.
>>SCRATCH.TXT ECHO ' ____________________
>>SCRATCH.TXT ECHO SUB %name% (%parameters%)
>>SCRATCH.TXT ECHO    ' Write your code here
>>SCRATCH.TXT ECHO END SUB

> %1 TYPE SCRATCH.TXT

:FIN
DEL SCRATCH.TXT
ENDLOCAL




Save this as subShop.bat in your QB install folder. This file inserts a SUB or FUNCTION into whatever BASIC code you are working on.

To use it we will have to associate it with a tool in PN.

Press Ctrl+N to open a new document. Save this document anywhere on your computer as BLT.BAS.

Click Tools>Options and then select Tools and click the Add button. Make your new tool look like this:
On the Console I/O tab, make sure the first two checkboxes are NOT checked. Click OK and then OK again.

In your empty document write the line:
PRINT "I love ";

And then select Tools>Add SUB or FUNCTION.

Into the command prompt that appears type SUB
and press Enter.

For the next prompt enter BLT
and press Enter.

At the next prompt type BACON$, LETTUCE$, TOMATO$
and press Enter.

Inside the SUB, in place of the comment line, type this code:
PRINT BACON$; LETTUCE$; TOMATO$;


Your code should now look like this:
After your first PRINT command type the following lines:
CALL BLT("BACON, ", "LETTUCE, ", " and TOMATO ")
PRINT "sandwiches!"


Your complete code should now look like this:
DECLARE SUB BLT (BACON$, LETTUCE$, TOMATO$)

PRINT "I love ";

CALL BLT("BACON, ", "LETTUCE, ", " and TOMATO ")

PRINT "sandwiches!"

' ____________________
SUB BLT (BACON$, LETTUCE$, TOMATO$)
  PRINT BACON$; LETTUCE$; TOMATO$;
END SUB

Now execute/interpret your code (F5 if you have been following the tutorials). You should see a prompt like this:
When you press a key to continue, you should see your code in the QB IDE.

Hopefully this tutorial has shown how easy it can be to create custom tools for PN.