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
    else
        cd $dn
    fi
}

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.

2 comments:

Slava Pestov said...

FWIW, the "find-vocab-root" word outputs the directory containing a vocabulary.

( scratchpad ) "io.files" find-vocab-root .
"resource:core"

If you want to get a path you can pass to an external OS process, normalize it:

( scratchpad ) "io.files" find-vocab-root normalize-path .
"/Users/slava/factor/core"

Adam said...

I await fash.