TCL Syntax Checker for VIM

1. Overview

This project introduced a TCL syntax checker for Vim users.

The core of the checker is the Nagelfar engine created by Peter Spjuth since 1999.

A VIM plugin is then developed to integrate Nagelfar into VIM. Whenever we save a TCL file, the Nagelfar engine gets invoked automatically and the syntax results are displayed in the quickfix window of VIM. The VIM plugin is disabled by default if the TCL file is large (e.g., more than 3000 lines) to prevent VIM from hanging for too long. It also allows users to enter their own rule exceptions.

2. How to Update Nagelfar Database?

In order to run Nagelfar in Terminal, call this command

tclsh path/to/nagelfar/nagelfar.tcl -H [path-to-TCL-file]

Although Nagelfar is very powerful, it has a major drawbacks. If we use Nagelfar in our specific TCL code base, we would probably receive many warning messages of "W Unknown command". The reason is that the Nagelfar could not recognize our functions or variables as the valid ones.

To solve this problem, we need to update the Nagelfar database with our function names, following these steps below

  • The database is stored in <nagelfar-directory>/syntaxdb.tcl

  • List all unknown functions using this command

    find "path-to-your-tcl-code-base" -type f -iname "*.tcl" –print0 | 
    xargs -0 tclsh "path-to-nagelfar132/nagelfar.tcl" -H |
    grep "W Unknown command" | cut -d """ -f 2 |
    sort | uniq > all_new_funcs.log

    • 1st line: Look up all TCL files inside our code base

    • 2nd line: Run nagelfar.tcl on every single TCL file

    • 3rd line: Select only "W Unknown command" messages and extract the command names

    • 4th line: Remove the duplicates and save the results to the all_new_funcs.log file

  • Copy all new function names to <nagelfar-directory>/syntaxdb.tcl

3. How to Integrate Nagelfar into VIM?

  • Develop a VIM plugin named tcl_syntax_checker.vim. It is written based on Vimscript language.

  • The plugin consists of five main parts, as shown below

  • The flow chart is shown below

  • To install the plugin, follow these steps

    • Open $HOME/.vimrc, add the lines

      source path/to/tcl_syntax_checker/tcl_syntax_checker.vim
      let g:tclsc_engine = 'path/to/tcl_syntax_checker/nagelfar132/nagelfar.tcl'
      let g:tclsc_nlines = 3000
      " Example of exceptions. Note: change " to \" let g:tclsc_exceptions = [ \ "E Bad expression: can't read \"darth_vader_mind\": no such variable", \ "E Unknown variable \"order_66\""
      ]

    • Restart VIM

    • If the file is large, we need to call the plugin manually :XTclCheckSyntax 1