[erlang-questions] Coloring variable bindings to lessen ambiguity.

user <>
Fri Nov 17 17:48:57 CET 2006


Vlad Dumitrescu <vladdu55 <at> gmail.com> writes:

> 
> Hi,
> 
> On 11/16/06, Henri Tuhola <henri.tuhola <at> gmail.com> wrote:
> > There are few ways to solve this problem, I think that coloring the
> > first occurrence of variable in scope by color editor would solve it all
> > without any changes into the language!
> ...
> > It'd require the text editor to understand the language scope, how hard
> > this can be?
> 
> I guess it depends what editor we are talking about  VIM (and the
> like) can't handle this. Emacs and Erlide could.

With quick hack it can be done with Emacs using only the standard erlang mode.

Here's a naive implementation. It can be fooled by funs within the function if 
the fun binds variables which shadow other variables bound in the outer 
function, but it's bad programming practice anyway.

Copy this code into your emacs *before* erlang stuff is loaded (so that the 
erlang-load-hook is run) and off you go:

(require 'cl)
 
(defface my-bound-var-face '((t (:background "yellow"))) "")
 
(defun my-erlang-highlight-bound-vars (limit)
 (when (re-search-forward (concat "[^?]\\(\\<" erlang-variable-regexp
"\\>\\)") limit t)
   (if (or (eq (get-text-property (point) 'face) 'font-lock-comment-face)
           (eq (get-text-property (point) 'face) 'font-lock-string-face)
           (save-match-data
             (let ((var (match-string 1))
                   (pos (point)))
               (save-excursion
                 (erlang-beginning-of-clause)
                 (re-search-forward (concat "[^?]\\(\\<" var "\\>\\)") limit t)
                 (> pos (point))))))
       (set-match-data (list 0 0)))
   t))
 
(defun my-erlang-highlight-setup
 (font-lock-add-keywords 'erlang-mode '((my-erlang-highlight-bound-vars
                                         . (1 'my-bound-var-face t t))) t))
 
(add-hook 'erlang-load-hook 'my-erlang-highlight-setup)





More information about the erlang-questions mailing list