6.27.1 Locating source code definitions

Many programming systems are organized as an integrated development environment (IDE) where the editor is the hub of the system, and allows building and running programs. If you want that, Gforth has it, too (see Emacs and Gforth).

However, several Forth systems have a different kind of IDE: The Forth command line is the hub of the environment; you can view the source from there in various ways, and call an editor if needed.

Gforth also implements such an IDE. It mostly follows the conventions of SwiftForth where they exist, but implements features beyond them.

An advantage of this approach is that it allows you to use your favourite editor: set the environment variable EDITOR to your favourite editor, and the editing commands will call that editor; Gforth invokes some GUI editors in the background (so you do not need to finish editing to continue with your Forth session), terminal editors in the foreground (default for editors not known to Gforth is foreground). If you have not set EDITOR, the default editor is vi.

locate ( "name" –  ) gforth-1.0 “locate”

Show the source code of the word name and set the current location there.

xt-locate ( nt/xt –  ) gforth-1.0 “xt-locate”

Show the source code of the word xt and set the current location there.

The current location is set by a number of other words in addition to locate. Also, when an error happens while loading a file, the location of the error becomes the current location.

A number of words work with the current location:

l ( ) gforth-1.0 “l”

Display source code lines at the current location.

n ( ) gforth-1.0 “n”

Display lines behind the current location, or behind the last n or b output (whichever was later).

b ( ) gforth-1.0 “b”

Display lines before the current location, or before the last n or b output (whichever was later).

g ( ) gforth-0.7 “g”

Enter the editor at the current location, or at the start of the last n or b output (whichever was later).

You can control how many lines l, n and b show by changing the values:

before-locate ( – u  ) gforth-1.0 “before-locate”

number of lines shown before current location (default 3).

after-locate ( – u  ) gforth-1.0 “after-locate”

number of lines shown after current location (default 12).

Finally, you can directly go to the source code of a word in the editor with

edit ( "name" –  ) gforth-1.0 “edit”

Enter the editor at the location of "name"

You can see the definitions of similarly-named words with

browse ( "subname" –  ) gforth-1.0 “browse”

Show all places where a word with a name that contains subname is defined (mwords-like, see Word Lists). You can then use ww, nw or bw (see Locating uses of a word) to inspect specific occurences more closely.