[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: my-calculate-indent function
From: |
Tim X |
Subject: |
Re: my-calculate-indent function |
Date: |
Fri, 20 Apr 2007 12:18:05 +1000 |
User-agent: |
Gnus/5.11 (Gnus v5.11) Emacs/22.0.95 (gnu/linux) |
Pietro Giorgianni <giorgian@gmail.com> writes:
> hi,
>
> i'm working on a major mode for a simple script language.
>
> blocks are enclosed in {}, and i wrote this function:
>
> (defun tintin-calculate-indent ()
> (let ((opened (count-matches "{" 0 (point)))
> (closed (count-matches "}" 0 (point))))
> (max 0
> (if (equal (char-after) 125)
> (* tintin-indentation-step (- (- opened closed) 1))
> (* tintin-indentation-step (- opened closed))))))
>
>
> which works, but is ugly.
>
> questions:
>
> 1) is there already a function that does this?
>
> 2) if not, how can i do it better?
>
I had a similar experience and decided to use some of the built-in syntax
parsing functions. For these to work, you will probably have to tweak the
syntax table entries, but this isn't hard and will probably help with
font-locking as well. In particular, see the sections on syntax tables and
parsing in the emacs lisp reference. the function syntax-ppss was particularly
useful for me.
,----[ C-h f syntax-ppss RET ]
| syntax-ppss is a compiled Lisp function in `syntax.el'.
| (syntax-ppss &optional POS)
|
| Parse-Partial-Sexp State at POS.
| The returned value is the same as `parse-partial-sexp' except that
| the 2nd and 6th values of the returned state cannot be relied upon.
| Point is at POS when this function returns.
`----
--
tcross (at) rapttech dot com dot au