unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* trouble making an indent-line function that doesn't move the point
@ 2008-03-11 19:16 Michael George Lerner
  2008-03-12 14:36 ` Joel J. Adamson
  0 siblings, 1 reply; 2+ messages in thread
From: Michael George Lerner @ 2008-03-11 19:16 UTC (permalink / raw)
  To: help-gnu-emacs

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


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

* Re: trouble making an indent-line function that doesn't move the point
  2008-03-11 19:16 trouble making an indent-line function that doesn't move the point Michael George Lerner
@ 2008-03-12 14:36 ` Joel J. Adamson
  0 siblings, 0 replies; 2+ messages in thread
From: Joel J. Adamson @ 2008-03-12 14:36 UTC (permalink / raw)
  To: Michael George Lerner; +Cc: help-gnu-emacs

Michael George Lerner <mglerner@gmail.com> writes:

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

One note on your own indenting: put the trailing parentheses all on the
same line, it makes the code much more readable.  Emacs Lisp mode should
indent correctly to help you "parse" the s-expressions as you read them.

Check out Emacswiki, and check out "Writing GNU Emacs Extensions."  I'll
take a more detailed look at the code later.

Joel

-- 
Joel J. Adamson
Biostatistician
Pediatric Psychopharmacology Research Unit
Massachusetts General Hospital
Boston, MA  02114
(617) 643-1432
(303) 880-3109

The information transmitted in this electronic communication is intended only
for the person or entity to whom it is addressed and may contain confidential
and/or privileged material. Any review, retransmission, dissemination or other
use of or taking of any action in reliance upon this information by persons or
entities other than the intended recipient is prohibited. If you received this
information in error, please contact the Compliance HelpLine at 800-856-1983 and
properly dispose of this information.







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

end of thread, other threads:[~2008-03-12 14:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-03-11 19:16 trouble making an indent-line function that doesn't move the point Michael George Lerner
2008-03-12 14:36 ` Joel J. Adamson

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