Lua Semantic Highlighting for Emacs

One potential source of errors in Lua programs is the unintended use of global variables. The lua2-mode Emacs mode aims to show how improved editors can help programmers to avoid these errors, without changing the Lua language itself.

When writing Lua code, it is all too easy to forget to declare a certain variable as local. In all likelihood, the program will still work as expected, but problems will occur if the affected code is called recursively or from co-routines. Another code fragment might mistakenly use the same global variable, too.

Traditional approaches for detecting unwanted global variables use separate tools external to the editor, typically based on bytecode scanning. Obvious advantage is editor independence and integration with build processes. The downside is a lack of immediate feedback to programmers. lua2-mode is an experiment to see if such immediate feedback is helpful when writing Lua programs.

When lua2-mode is loaded, global variable references are shown in bold. Assignments and bindings are underlined. Assignments to global variables are also shown in italics.

You can change the appearance of variable references by customizing the lua2-* faces. If you drop the references to font-lock-variable-name-face , globals stand out even more clearly, but this might be considered excessive.

If the point is at a reference to a local variable, pressing C-c C-d jumps to its definition. You can rename a local variable (and all its occurrences within its scope) by pressing C-c C-r .

I wrote the parser by hand, and it is still not quite right. In particular, some invalid programs are accepted.

The current implementation is too slow for actually editing medium-sized files with it. It might be possible to improve its speed by switching to syntax tables instead of regular expressions for lexing.

Better editor support is possible if we execute Lua code in a sandbox and extract information from that. Microsoft has demonstrated the feasibility of this approach for Javascript (Scott Guthrie, JavaScript Intellisense Improvements with VS 2010 , April 2010). We would have to embed a Lua interpreter into Emacs, but Emacs lacks a plug-in interface, so we would have to create a private fork for this purpose. For this reason, the current approach based on Emacs and a parser written in Emacs Lisp is a dead end.

lua2-mode is free software, available on the GNU General Public License, version 2 or later.

lua2-mode has only been tested with GNU Emacs 23.1. It depends on lua-mode , which must also be installed.

I recommend to byte-compile lua2-mode , in order to get somewhat better performance.

Please send problem reports to fw@deneb.enyo.de and include the string lua2-mode in the subject line. Problem reports should include Lua source which exhibits the problem, and a short description what is going wrong. Please also indicate if I may put your source code into the test suite.

Revisions

2010-07-25: Version 0.1 published.

2011-10-09: Released version 0.2, using a bold, black face for global variable references, and fixing a bug in for scope handling.

Florian Weimer

Home Blog (DE) Blog (EN) RSS Feeds Impressum