FORTH ESCAPES
ST control characters from Forth
by TIMOTHY HUANGWhile working with my new ST development system, I came upon a series of built-in escape sequences designed to control the video display. With these sequences, you can easily clear the screen, home the text cursor, manipulate the cursor, turn on reverse video, and more...
I quickly decided this was an excellent opportunity to create a few Forth definitions using the Dragon Group's 4xFORTH.
By generating an ASCII escape character (27 decimal, $lB hexadecimal) followed by the correct ASCII letter or number, you can accomplish quite a lot. For example, an escape followed by a lower-case p will turn on reverse video text. An escape followed by a lower-case q will return to normal text. (While the following techniques will work from 4xFORTH, we have not been able to get them to work from GEM-controlled LOGO. ANTIC ED)
From Forth, you can easily try out a given escape sequence by typing something like: 27 EMIT ASCII p EMIT.
Or, should you want to add something more permanent to your working system, you can write some simple definitions, and compile them:
: REVERSE.ON ( --- ) 27 EMIT ASCII p EMIT ; : REVERSE.OFF ( --- ) 27 EMIT ASCII q EMIT ; : HOME ( --- ) 27 EMIT ASCII h EMIT ;
However, the above approach does not really take full advantage of Forth. Let's approach this in a logical manner.
FORTH LOGIC
Since the phrase 27 EMIT exists in every definition, we can follow good Forth practices and factor it out as a separate word. The name of this word might logically be ESC.
Secondly, since the phrases following the ESC are identical except for the ASCII character, we may create a defining word to take care of the redundancies.
A defining word consists of three parts: the word itself, the compiling time behavior, and the execution time behavior. If you look at the first block of Listing 1, you can see our defining word in lines 3 through 7.
The word I've chosen is 1TERM because it will take 1 ASCII character from the terminal.
The compiling time behavior is included between CREATE and DOES>. CREATE will create a new name from what you provide. ASCII is an immediate word, but since we don't want it to be executed during the compiling of our defining word, we use the phrase: [COMPILE] ASCII C. This will defer taking the ASCII value until we actually use the defining word, and then C will compile the ASCII value into the dictionary.
The third part or execution time behavior consists of ESC C @ EMIT. ESC will emit the escape character (as defined above, in line 2). And then we fetch the stored character with C @ and send it out with EMIT.
Blocks 2 and 3 list the ST escape controls. The names chosen are self-explanatory, but using good Forth programming practice I have commented every one of them.
The cursor positioning word AT, in block 4, is not defined by 1TERM because it takes two additional values. The arguments of the cursor row and column must be provided on the stack prior to execution of this word, at which time we add 32 the ASCII value of the space to move the cursor.
FOREGROUND and BACKGROUND take, as an additional argument, the color index. Since only the least four significant bits are used, 16 colors are available in low resolution, and four in medium.
4xFORTH The Dragon Group 148 Poca Fork Road Elkview, WV 25071 (304) 965-5517 $99.95
Antic met Timothy Huang at the first Digital Research GEM seminar. He is an ST program developer concentrating on software conversions for the Far East market.