help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Indent goes to beginning of line


From: Paulo J. Matos
Subject: Indent goes to beginning of line
Date: Fri, 07 Aug 2009 14:22:51 +0100

Hi all,

I implemented an indent function for a mode that I created.
The function is as follows:
(defun eventb-indent-line ()
  "Indent current line as EventB code"
  (interactive)
  (beginning-of-line)
  (cond ((or (bobp) (looking-at "^[ \t]*End")) ;; Rule 1
         (indent-line-to 0)) 
        ((looking-at "^[ \t]*\\(Axioms\\|Constants\\|Contexts\\|Event\\|
Extends\\|Initialisation\\|Invariants\\|Machine\\|Sees\\|Sets\\|\
\Theorems\\|Variables\\|Variants\\)") ;; Rule 2
         (indent-line-to default-tab-width))
        ((looking-at "^[ \t]*end") ; Rule 3
         (let (cur-indent)
           (save-excursion 
             (forward-line -1)
             (setq cur-indent (- (current-indentation)
default-tab-width)))
           (indent-line-to cur-indent)))
        ((looking-at "^[ \t]*\\(any\\|when\\|where\\|then\\|status\
\)") ; Rule 4
         (let ((not-indented t) cur-indent)
           (save-excursion 
             (while not-indented
               (forward-line -1)
               (if (looking-at "^[ \t]*\\(Axioms\\|Constants\\|Contexts\
\|Event\\|Extends\\|Initialisation\\|Invariants\\|Machine\\|Sees\\|Sets\
\|\\Theorems\\|Variables\\|Variants\\)") 
                   (progn 
                     (setq cur-indent (+ (current-indentation)
default-tab-width))
                     (setq not-indented nil))
                 (if (bobp) ;; Rule 5
                     (setq not-indented nil)))))
           (if cur-indent 
               (indent-line-to cur-indent)
             (indent-line-to 0))))
        (t 
         (let (cur-indent)
           (save-excursion
             (forward-line -1)
             (if (or (looking-at "^[ \t]*\\(any\\|when\\|where\\|then\\|
status\\)")
                     (looking-at "^[ \t]*\\(Axioms\\|Constants\\|
Contexts\\|Event\\|Extends\\|Initialisation\\|Invariants\\|Machine\\|
Sees\\|Sets\\|\\Theorems\\|Variables\\|Variants\\)"))
                 (setq cur-indent (+ (current-indentation)
default-tab-width))
               (setq cur-indent (current-indentation))))
           (indent-line-to cur-indent)))))

Interestingly enough, when I press <tab> to indent a line, the cursor
instead of staying in the same place moves to the first character of the
line, why is this? I thought save-excursion would avoid this from
happening.

Cheers,

Paulo Matos





reply via email to

[Prev in Thread] Current Thread [Next in Thread]