Using vim to browse and edit Tonto's Foo source code

From Tonto Wiki
Jump to: navigation, search
Vim-ad.jpg

If you want to browse or edit Tonto's Foo language source code in more comfort, you will definitely be interested vim.

This wiki section shows you how to:

  • Set up vim so that it highlights the code in colour
  • Set up vim so it `folds' routines so you only see the routine name.
  • Set up vim to use ctags to `jump' to routine and class definitions.
  • Set up `omni completion' where vim completes routine names for you as you type (under development)

Contents

Syntax highlighting and routine folding

To set this up copy the .vimrc file, and the .vim directory from the tonto/ directory to your home directory.

  cd tonto
  cp .vimrc ~
  cp -r .vim ~

Then start up vim or gvim on one of the files in foofiles/ , e.g.

  cd foofiles
  vim crystal.foo

And enjoy! You should see something like this (click to see larger):

The grey bars are the `folded' routines.

To `unfold' a routine and see the actual code position the cursor on the fold and type "zo".

(o is for open).

To close the fold type "zc".

(c is for close).

(To remember the z, think of it as a `folded piece of paper'. :-)

Jumping to routine definitions (with ctags)

To set this up, make sure you have installed exuberant ctags and copy the .ctags file from tonto/ to your home directory.

  cd tonto
  cp .ctags ~

Then type:

  cd foofiles
  ctags --lang=foo macros *.foo

You will see the ctags program making a `tags' file containing a list of all the routines, types and macros, that can be `jumped' to.

Then in vim start editing a Foo language file e.g. crystal.foo which contains the CRYSTAL module.

(The name of the module always matches it file name).

A. Now type g Ctrl-] when your cursor on top of the routine you want to go to

e.g. when your cursor is on top of the unit_cell method on the first active line of code in the make_k_pts routine, as shown below :

The result will be either

  • You'll jump directly to the unit_cell routine or component, or
  • You'll be given a choice of unit_cell jump targets that you could jump to

The latter is the case in the example above.

B. Choosing option 2 and hitting return then `jumps' to the unit_cell component defined as part of the CRYSTAL type in the types.foo file).

C. Moving the cursor over the definition of the object UNIT_CELL leads to its type definition, again in the types.foo file.

By the way, notice how all the routines and type components are documented below the variable? Nice, huh. You can really learn from this code.

D. Finally, type Ctrl-t multiple times to go back to where you had previously jumped from.

Wonderful!

Take a look also what happens if you do a `jump' when you are on top of the macro symbol PI, or the MAT{REAL}(3,3) array object.

You will see that these are expanded to normal Fortran definitions, but the Foo macros are shorter and clearer:

Foo code completion - Omnicompletion

This feature may be very useful if you are just beginning to get used to the Tonto library.

To enable code completion when editing a .foo file with vim, set up ctags as above and when editing a .foo file type:

  :FooCompleteOn

Now try inserting a dot after a previously declared variable, and while still in vim insert mode, after the dot type

  Ctrl-X Ctrl-O

You will see a popup menu with the available object methods!

For example, in the make_k_pts routine in the crystal.foo file, as in the example above, try removing the reciprocal_matrix part of the code (not the dot!) at the end of the second active line of code.

Then type Ctrl-X Ctrl-O and see what happens as you follow the captions below:

The kind object in the selection menu is labelled with an informative key:

  • (a) for attribute or type component
  • (s) for subroutine
  • (f) for function
  • (t) for a type
  • (m) for a macro
  • (g) for a global variable

Use the down- and up-arrows to choose the routine that you want, then hit return to choose it.

You can remove characters and type Ctrl-X Ctrl-O again to get back other options:

For more details on omni completion, when you are in the vim editor type:

  :help completion