[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to get syntax information in batch mode?
From: |
martin rudalics |
Subject: |
Re: How to get syntax information in batch mode? |
Date: |
Sat, 20 May 2006 15:25:52 +0200 |
User-agent: |
Mozilla Thunderbird 1.0 (Windows/20041206) |
> I have written an automatic code indentation function in ELisp and I
> want to be able to invoke this function from Emacs' batch mode so that
> it can be invoked from a Makefile, without needing to start Emacs
> interactively.
>
> However the function get-char-property always seems to return nil when
> noninteractive is set to t. Am I correct about this deduction?
>
> I use get-char-property to tell the automatic indentation function
> whether or not we are currently inside a string or a comment. Is
> there a different function for telling whether or not we are currently
> inside a string or a comment?
By default, Emacs doesn't fontify text that is not displayed. Hence, in
general, your function might not work interactively either.
You have two basic ways to achieve what you want:
Use font-lock and text-properties: A brute force approach is to call
`font-lock-fontify-buffer' before doing the indentation. More subtle is
`font-lock-fontify-syntactically-region (point-min) (point-max)' but you
might have to wrap this in something equivalent to font-lock's
`save-buffer-state' and you shouldn't want to set syntax-table text
properties in this case. Also be careful when indenting to avoid that
text properties get inherited in some unpredictable way.
Use `parse-partial-sexp': To program this efficiently you should
intermittently save the parsing state to avoid rescanning from
point-min. Emacs 22 has `syntax-ppss' which does this automatically.
`syntax-ppss' also guarantees that indenting won't mess up the parse
state.
Always bear in mind that font-lock uses the syntax parsing routines
anyway to find comments and strings.