From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Michael George Lerner Newsgroups: gmane.emacs.help Subject: trouble making an indent-line function that doesn't move the point Date: Tue, 11 Mar 2008 12:16:45 -0700 (PDT) Organization: http://groups.google.com Message-ID: <2151ad8e-d9be-4f1f-b832-de1c237ef27a@e6g2000prf.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1205296794 26857 80.91.229.12 (12 Mar 2008 04:39:54 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 12 Mar 2008 04:39:54 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Mar 12 05:40:22 2008 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 1JZIlJ-00067a-PQ for geh-help-gnu-emacs@m.gmane.org; Wed, 12 Mar 2008 05:40:22 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1JZIkl-0007ks-4H for geh-help-gnu-emacs@m.gmane.org; Wed, 12 Mar 2008 00:39:47 -0400 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!postnews.google.com!e6g2000prf.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 102 Original-NNTP-Posting-Host: 128.231.88.5 Original-X-Trace: posting.google.com 1205263006 23259 127.0.0.1 (11 Mar 2008 19:16:46 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Tue, 11 Mar 2008 19:16:46 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: e6g2000prf.googlegroups.com; posting-host=128.231.88.5; posting-account=lS1GBwoAAACJn5964PXhxUPcftgd72C2 User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12,gzip(gfe),gzip(gfe) Original-Xref: shelby.stanford.edu gnu.emacs.help:156891 X-Mailman-Approved-At: Wed, 12 Mar 2008 00:39:18 -0400 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:52274 Archived-At: Hi, I wrote a very simple emacs mode to deal with highlighting/indentation for CHARMM files (CHARMM is a computational biology package that has its own input syntax). The indentation and highlighting basically do what I want except for the fact that calling charmm-indent-line moves the point to the beginning of the line. This is the first time I've touched lisp in 11 years, so I'm a little rusty. Can someone help me make this function behave? Thanks, -michael (defun charmm-continuation-line () "Returns non-nil if current line continus a previous one" (eq ?- (char-before (line-end-position 0))) ) (defun charmm-indent-line () "Indent current line for CHARMM input files." ; copied in large part from http://two-wugs.net/emacs/mode-tutorial.html ; 1. if we are at the beginning of the buffer, indent to column 0 ; 2. if we are at an END line (starts with "end" "quit" "goto loop" or "label end"), dedent relative to previous line ; 3. if we first see an end line before our current line, indent our line to the same indentation as the end line ; 4. if we first see a start line ("if" "vibran" "label loop" or "label start" at this point), increase indentation ; relevant to that line. ; this allows for some very simple parsing of labels and gotos. see comments below. ; 5. if we're on a continuation line, indent two more spaces, but don't change cur-indent ; 6. else, don't indent. (interactive) (beginning-of-line) ; set the point to the beginning of the line (if (bobp) ; bobp tells us if it's the first line of a buffer (indent-line-to 0) ; First line is always non-indented (let ((not-indented t) cur-indent) ; so i'm not about to do real parsing of things involving gotos ; however, if you give all of your looping labels names starting with ; the word "loop" this will work. ; if you want blocks to be indented inside of labels, call the initial label startXXX and the final one endXXX (setq start-pattern-with-simple-labels "^[ \t]*\\(if.*then\\| vibran\\|label[ \t]+loop\\|label[ \t]+start\\)") (setq start-pattern-without-labels "^[ \t]*\\(if.*then\\| vibran\\)") (setq end-pattern-with-simple-labels "^[ \t]*\\(end\\|quit\ \|.*[ \t]+goto[ \t]+loop\\|label[ \t]+end\\)") (setq end-pattern-without-labels "^[ \t]*\\(end\\|quit\ \)") (setq use-simple-labels t) (if use-simple-labels (setq start-pattern start-pattern-with-simple-labels) (setq start-pattern start-pattern-without-labels) ) (if use-simple-labels (setq end-pattern end-pattern-with-simple-labels) (setq end-pattern end-pattern-without-labels) ) (if (looking-at end-pattern) ; If the line we are looking at is the end of a block, then decrease the indentation (progn (save-excursion (forward-line -1) (setq cur-indent (- (current-indentation) default-tab- width))) (if (< cur-indent 0) ; We can't indent past the left margin (setq cur-indent 0))) (save-excursion (while not-indented ; Iterate backwards until we find an indentation hint (forward-line -1) (if (looking-at end-pattern) ; This hint indicates that we need to indent at the level of the END token (progn (setq cur-indent (current-indentation)) (setq not-indented nil)) (if (or (looking-at start-pattern) ) ; This hint indicates that we need to indent an extra level (progn (setq cur-indent (+ (current-indentation) default- tab-width)) ; Do the actual indenting (setq not-indented nil)) (if (bobp) (setq not-indented nil)))) ) ) ) (if (charmm-continuation-line) (setq cur-indent (+ 2 cur- indent))) (if cur-indent (indent-line-to cur-indent) (indent-line-to 0)) )) ) ; If we didn't see an indentation hint, then allow no indentation