Wednesday, February 25, 2009

cd factor

When working with Factor source code, you might find yourself moving between several directories on the filesystem, navigating and editing several files.

Due to source code being kept inside core, basis, extra, work, and other locations, it is not always a simple matter of moving up and down directories to find the files and vocabularies that you are looking for.

Today, I wrote a bit of bash script to simplify, and improve, this part of the development process.

# change directories to a factor module
cdfactor() {
    code=$(printf "USING: io io.pathnames vocabs vocabs.loader ; "
           printf "\"%s\" <vocab> vocab-source-path (normalize-path) print" $1)
    echo $code > $HOME/.cdfactor
    fn=$(factor $HOME/.cdfactor)
    dn=$(dirname $fn)
    echo $dn
    if [ -z "$dn" ]; then
        echo "Warning: directory '$1' not found" 1>&2
        cd $dn

This function takes a vocabulary as argument. When run, it generates a temporary Factor script that is executed to find the pathname to the source file. It then changes directories into the directory containing the source file.

For example, if you want to switch to the io.files vocab, just run:

$ cdfactor io.files
Note: this requires factor to be on the $PATH.

Wednesday, February 18, 2009

Readline support for Factor

When using Factor from the command line, you might notice that it lacks many of the features of typical REPL's offered by other languages. For example, using arrow keys, home, end, delete, and backspace to navigate and edit text, and having proper history support for using previous lines of text.

These features are often implemented by using the GNU Readline library.

But, how do you use readline with Factor? The easiest way is to use the rlwrap command, which wraps an arbitrary command in readline.

To install it on Linux, run sudo yum install rlwrap (for RPM-based distros) or sudo apt-get install rlwrap (for Debian-based distros).

On the Mac, you can install it with the MacPorts system by running sudo port install rlwrap.

Once installed, add rlwrap in front of your factor command. For instance:

rlwrap ./factor

You should find interacting with Factor to be much easier.