From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Tim X Newsgroups: gmane.emacs.help Subject: Re: my-calculate-indent function Date: Fri, 20 Apr 2007 12:18:05 +1000 Organization: Posted via Supernews, http://www.supernews.com Message-ID: <878xcnzt42.fsf@lion.rapttech.com.au> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1177036533 13388 80.91.229.12 (20 Apr 2007 02:35:33 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Fri, 20 Apr 2007 02:35:33 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Apr 20 04:35:26 2007 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1Heiy6-0002B5-Av for geh-help-gnu-emacs@m.gmane.org; Fri, 20 Apr 2007 04:35:26 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Hej36-0001vz-Of for geh-help-gnu-emacs@m.gmane.org; Thu, 19 Apr 2007 22:40:36 -0400 Original-Path: shelby.stanford.edu!newshub.stanford.edu!postnews.google.com!news1.google.com!news.glorb.com!sn-xt-sjc-05!sn-xt-sjc-07!sn-post-sjc-01!supernews.com!corp.supernews.com!not-for-mail Original-Newsgroups: gnu.emacs.help User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.95 (gnu/linux) Cancel-Lock: sha1:NwY2iGS+slxosC9s/ubFv/OXBRw= Original-X-Complaints-To: abuse@supernews.com Original-Lines: 46 Original-Xref: shelby.stanford.edu gnu.emacs.help:147240 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:42844 Archived-At: Pietro Giorgianni 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