Enhancing the Dr. Dobb's C Screen Editor Alan D. Howard Rt. 3, Box 680 Crozet, VA 22932 July 24, 1982 At the beginning of Edward K. Ream's article introducing his Small-C Screen Editor in Dr. Dobbs Journal (#63, January, 1982) he asked whether our present editor lacked flexibility, transporability, and extensibility. Although my mainstay editor has been PIE from Software Toolworks, I have often been annoyed at its inability to edit large files or to make major structural changes in text. On the other hand, Ream's editor as presented did not go much beyond PIE and seemed clumsy in its Edit-mode, but it does encourage adapting it to fit individual needs since it is presented in source code. Thus, after several nights of typing and several more nights of debugging, I had the C editor up and running. I have been modifying the editor during the last few months and it has evolved to the point that I feel some of Dr. Dobb's readers may be interested. The changes are of several types: 1) Extensions to the file-handling and buffer management to permit editing large files, extracting parts of the text to a different file, and moving and copying portions of the text within the file. 2) Changing the edit mode operation to recognize special keys on the H19 Terminal or H89 Computer instead of single-letter commands. As a bonus, the editor now assumes eXchange mode for any other typed key. 3) Addition of a few new commands to the edit-mode and some changes in rules of operation. 4) Slight modifications to allow modular compilation using the Software Toolworks C/80 compiler. These enhancements are discussed below. A documentation file is included which summarizes the expanded features and all commands. Changes to File Handling and Command-Mode Operation Most of the changes to the command-mode are concerned with adding flexibility to file handling and major buffer alterations. Separate read and write files are now maintained, with load specifying the read file name, clearing the buffer, reading the file, and closing the read file if it all fits in the buffer. Otherwise as much as fits is read in, and the read file remains open. The open command opens the readfile without clearing the buffer or reading anything in. More from the read file can be added to the buffer with either the rest or add commands. Rest reads in as much more as possible and gives the option of clearing the buffer, but add reads only more lines without clearing the buffer. The write file is specified by name for a new file, or delname for writing over an existing file (This replaces the resave command). Write writes n lines from the buffer to the write file, deleting those lines. To balance the existing append command, an extract command has been added to write the indicated line range to a new file (the name is requested after the command is entered) without deleting those lines from the buffer. Finally, to allow flexibility, the read and write files can be closed by closeread and closewrite. Major structural changes in the file can now be made using the copy and move commands, which take n lines from and copy them to before line . In addition, move deletes the lines at . For speed, these routines open up the new space, and then do the copying or moving. No move or copy is allowed if there is not room in the buffer. If buffer space is tight, or if the move or copy is to a part of the file not currently in memory, then use the extract and append commands, which use the disk as a buffer. The following command-mode commands are unaltered from the original: append, change, clear, delete, dos, find, g, list, search, and tabs. Changes to Edit and Insert Modes The most important change to the edit mode is the replacement of the single-letter commands by escape-character sequences generated by the special keys on the H19/H89 keyboard. Similar keys, but with different escape sequences, are found on many other terminals and computers. This allows default use of the eXchange mode (replacing contents of cursor position with the typed keystroke) for normal keys, including the space bar. Most special characters and edit mode commands retain their original functions (see the documentation portion of the listing for details), but two new commands have been created for the edit mode: the HOME key moves alternately to the top and bottom line on the screen, and the ERASE key erases from the current cursor position to the end of the line. More subtle changes have also been made. The RETURN key moves to the beginning of the next line in Edit mode, but acts as the insert down key in the Insert mode. Both the DC and DELETE keys delete the character at the cursor, but BACKSPACE deletes the character to the left of the cursor, the same as the original delete character special key. The only commands remaining as control codes are split and join. There is some room for expansion: three keys are currently assigned to force command mode, but could be reassigned. Note that the ESC key must be pressed twice to be recognized, because it is also issued by the special character codes of the keyboard. My choice of key assignments and features has admittedly been influenced by the PIE editor. Implementation Details I'd like to start out with an unsolicited endorsement of the Software Toolworks C/80 Compiler, now in version 2.0 and incorporating almost all of the C language. Its a decendent of Small-C and is cheap, fast, and powerful. Its salient feature in the context of the editor is support for modular compilation using Microsoft M80 and L80. Since the editor is broken into 9 fairly independent modules, changes can be made in one without having to recompile everything. Also, SID can be used for debugging using global variable and function names. The disadvantage is that variable and function names must be distinct at six characters length. This has necessitated renaming several functions in the editor, as listed in Figure 1. Also, to force the main buffer to be at the end of the program, the short program MBUFFER.MAC must be assembled by M80 and be the last module linked (after the C/80 CLIBRARY module). Most details of the changes are explained in the listing. Concluding Remarks This editor is a very useful addition to my stable of inexpensive editors, and is particularly suited to editing large files, interleaving text from other files, and breaking up a file or portions of a file into smaller pieces. It doesn't do everything, and some of the things it does do go excruciatingly slowly. In particular, the default eXchange operation in the Edit mode is somewhat slower than typing speed for my 2 Mz H89, as are the insert and delete line commands. I suggest those with H89's go through the bothersome task of reassembling their BIOS with the type-ahead buffer option. The search, find, and change commands would benefit from some assembly language optimization. When I move on to my next system, I will feel secure that I have its first editor waiting in the wings, and it won't cost me twice as much as my previous editor for the same features. The modified editor is in the public domain. Altered Function Names Original New search1 suurch outgetx outxget outgety outyget outhasdn outdnhas outhasup outuphas pmtmode1 pmt1mode pmtfile1 pmt1file pmtline1 pmt1line pmtcol1 pmt1col sysmovdn sysdnmov sysmovup sysupmov bufdeln bufndel bufoutln buflnout bufmovup bufupmov bufmovdn bufdnmov buffer mbuffer Figure 1