[comp.emacs] check lisp file for unbalanced parentheses

rmb@sage.cs.reading.ac.uk (Rob Bodington) (07/02/87)

Does anyone know of an emacs macro to check a lisp file for unbalanced 
parentheses?

Thanks.

jr@LF-SERVER-2.BBN.COM (John Robinson) (07/10/87)

One trivial way is to insert a close paren at the end to see if it
matches anything (asuming you are in lisp mode).  Slight harder is to
put an extra open at the top, and then the extra close at the end
should match it.  But, at least it required no macros (beyond lisp
mode).

NB - you didn't say which emacs.  I forget whether gosmacs has
paren-balancing in its lisp mode (or, for that matter, whether it has
a lisp mode).

/jr

larus@paris.Berkeley.EDU.berkeley.edu (James Larus) (07/11/87)

Here is a function that I use.  It is not prefect, but it is usually acurate
and is fast.

/Jim

(defun find-unbalanced-parenthesis ()
  "Verifies that parentheses in the current Lisp buffer are balanced."
  (interactive)
  (let ((started-here (point)))
    (goto-char (point-min))
    (while (re-search-forward "^(" nil t)
      (backward-char 1)
      (forward-sexp 1)
      (if (looking-at ")") (error "Extra `)'")))
    (goto-char started-here)
    (message "All parentheses appear to be balanced.")))

seindal@diku.UUCP (Rene Seindal) (07/13/87)

In article <385@sage.cs.reading.ac.uk> rmb@sage.cs.reading.ac.uk (Rob Bodington) writes:
>Does anyone know of an emacs macro to check a lisp file for unbalanced 
>parentheses?

If you are using GNU Emacs, you can use the functions forward-sexp (C-M-f)
and backward-sexp (C-M-b) to across whole S-expressions.  These functions
check for unbalanced parantheses as well.  These functions are actually a
part of lisp mode, but are globally bound.

I suppose most other emacs-like editors have similar functions.

Rene' Seindal (seindal@diku.uucp)
The Computer Department, Institute of Datalogy, U. of Copenhagen.

kimcm@olamb.UUCP (Kim Chr. Madsen) (07/14/87)

In article <8707101837.AA20157@ucbvax.Berkeley.EDU>, jr@LF-SERVER-2.BBN.COM (John Robinson) writes:
> One trivial way is to insert a close paren at the end to see if it
> matches anything (asuming you are in lisp mode).  Slight harder is to
> put an extra open at the top, and then the extra close at the end
> should match it.  But, at least it required no macros (beyond lisp
> mode).

Another simple way of doing it is to load vip-mode - then the % key will
assuming the cursor is on a parentesis move it to the correspondong open or
close parentesis (if it exists) it also works on braces and brackets.

PS:
Once vip-mode is loaded you can easily switch mode between vip-mode and
emacs-mode with ctrl-z.

					Kim Chr. Madsen