unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* my-calculate-indent function
@ 2007-04-17  0:02 Pietro Giorgianni
  0 siblings, 0 replies; 4+ messages in thread
From: Pietro Giorgianni @ 2007-04-17  0:02 UTC (permalink / raw)
  To: help-gnu-emacs

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?

thanks

giorgian

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

* Re: my-calculate-indent function
       [not found] <mailman.2156.1176768900.7795.help-gnu-emacs@gnu.org>
@ 2007-04-17  3:06 ` Stefan Monnier
  2007-04-20  2:21   ` Tim X
  2007-04-20  2:18 ` Tim X
  1 sibling, 1 reply; 4+ messages in thread
From: Stefan Monnier @ 2007-04-17  3:06 UTC (permalink / raw)
  To: help-gnu-emacs

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

(nth 0 (syntax-ppss <pos>)) returns more or less (- opened closed).


        Stefan


PS: In Emacs<22, parse-partial-sexp can give you that info instead of
    syntax-ppss.

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

* Re: my-calculate-indent function
       [not found] <mailman.2156.1176768900.7795.help-gnu-emacs@gnu.org>
  2007-04-17  3:06 ` Stefan Monnier
@ 2007-04-20  2:18 ` Tim X
  1 sibling, 0 replies; 4+ messages in thread
From: Tim X @ 2007-04-20  2:18 UTC (permalink / raw)
  To: help-gnu-emacs

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

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

* Re: my-calculate-indent function
  2007-04-17  3:06 ` Stefan Monnier
@ 2007-04-20  2:21   ` Tim X
  0 siblings, 0 replies; 4+ messages in thread
From: Tim X @ 2007-04-20  2:21 UTC (permalink / raw)
  To: help-gnu-emacs

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>
> PS: In Emacs<22, parse-partial-sexp can give you that info instead of
>     syntax-ppss.

As I understand it syntax-ppss parses the whole file (but uses a caching system
to improve performance). Am I right in assuming parse-partial-sexp under emacs
22 would achieve a similar result, but more efficiently and without the caching
stuff as it only parses a part of the file?

Just interested as a new mode I've been working on is using syntax-ppss, which
seems find given my hardware and size of the files I'm typically working on,
but at the time of writing, I did have a small concern regarding how well the
approach would work on large input files.

Tim

-- 
tcross (at) rapttech dot com dot au

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

end of thread, other threads:[~2007-04-20  2:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-04-17  0:02 my-calculate-indent function Pietro Giorgianni
     [not found] <mailman.2156.1176768900.7795.help-gnu-emacs@gnu.org>
2007-04-17  3:06 ` Stefan Monnier
2007-04-20  2:21   ` Tim X
2007-04-20  2:18 ` Tim X

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).