all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How to get syntax information in batch mode?
@ 2006-05-19  5:58 Davin Pearson
  2006-05-19 11:19 ` andlind
  0 siblings, 1 reply; 4+ messages in thread
From: Davin Pearson @ 2006-05-19  5:58 UTC (permalink / raw)



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?

If there is no such function, then could Emacs be modified so that
text properties are online in batch mode?

Here is an example of a function that always prints nil when invoked
noninteractively.

(defun test ()
  (find-file "~/a.java")
  (font-lock-mode)
  (goto-char (point-min))
  (while (< (point) (point-max))
    (message "%s\n" (get-char-property (point) 'face))
    (forward-char 1)))

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: How to get syntax information in batch mode?
  2006-05-19  5:58 How to get syntax information in batch mode? Davin Pearson
@ 2006-05-19 11:19 ` andlind
  0 siblings, 0 replies; 4+ messages in thread
From: andlind @ 2006-05-19 11:19 UTC (permalink / raw)


Many years ago (at least ten) I wrote a stand-alone "print" utility
that invoked Emacs, loaded a file, started font-lock and executed
ps-print-buffer-with-faces, so I know it's possible to do what you want
to do.

After looking at the font-lock source, I think that all you have to do
is setting "noninteractive" to nil, e.g.:

    (let ((noninteractive nil))
      (font-lock-mode t))

However, this is just a speculation, I leave all the fun
trial-end-error stuff to you. ;)

    -- Anders

Davin Pearson skrev:

> 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?
>
> If there is no such function, then could Emacs be modified so that
> text properties are online in batch mode?
>
> Here is an example of a function that always prints nil when invoked
> noninteractively.
>
> (defun test ()
>   (find-file "~/a.java")
>   (font-lock-mode)
>   (goto-char (point-min))
>   (while (< (point) (point-max))
>     (message "%s\n" (get-char-property (point) 'face))
>     (forward-char 1)))

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: How to get syntax information in batch mode?
@ 2006-05-20 13:25 martin rudalics
  2006-05-21  6:55 ` Davin Pearson
  0 siblings, 1 reply; 4+ messages in thread
From: martin rudalics @ 2006-05-20 13:25 UTC (permalink / raw)
  Cc: help-gnu-emacs

 > 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.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: How to get syntax information in batch mode?
  2006-05-20 13:25 martin rudalics
@ 2006-05-21  6:55 ` Davin Pearson
  0 siblings, 0 replies; 4+ messages in thread
From: Davin Pearson @ 2006-05-21  6:55 UTC (permalink / raw)
  Cc: help-gnu-emacs

On 21/05/06, martin rudalics <rudalics@gmx.at> wrote:
>  > 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.

That works for me!

Thank you very much for your helpful advice!


-- 
Davin Pearson    http://www.davinpearson.com

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2006-05-21  6:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-05-19  5:58 How to get syntax information in batch mode? Davin Pearson
2006-05-19 11:19 ` andlind
  -- strict thread matches above, loose matches on Subject: below --
2006-05-20 13:25 martin rudalics
2006-05-21  6:55 ` Davin Pearson

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.