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

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

trouble making an indent-line function that doesn't move the point


From: Michael George Lerner
Subject: trouble making an indent-line function that doesn't move the point
Date: Tue, 11 Mar 2008 12:16:45 -0700 (PDT)
User-agent: G2/1.0

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


reply via email to

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