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

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

Re: Is it possible to set a "goal column" for 'beginning-of-line'?


From: Stefan Monnier
Subject: Re: Is it possible to set a "goal column" for 'beginning-of-line'?
Date: Thu, 01 Dec 2005 10:49:31 -0500
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux)

> I'll try to explain it more clearly as follows.
> Typically a php file contains content like this:

> <html>
>   <body>
>     <h1><?php
>       if (something) {
>         action;
>       } else {
>         action;
>       }
>     ?></h1>

>   </body>
> </html>

> As you see, code between '<?php' and '?>'
> should be idented more than <h1>.  But if
> you really put point before 'if (something)'
> and press <TAB>, it will be idented to
> column 0. That's why I said it's poor. This
> is because ident commands first go to
> column 0 with 'beginning-of-line' and then
> count columns starting from there.

I see.  While I'd be quite happy to see the PHP code at column 0, I can
understand that some people may prefer it to be somewhere else.
But I think the problem is slightly more complex than what you describe:

- indentation code typically uses `current-column' and `current-indentation'
  to figure out the indentation of previous lines (and then compute the
  desired indentation of the current line) and then `indent-to' or
  `indent-line-to' in order to do the actual indentation.
  Those are coded directly in C and don't use beginning-of-line.

- [ I don't know php-mode in particular. ] For some major modes column 0 is
  treated specially, typically by assuming that function definitions are at
  column 0.  So if you really force all indentation to start at column 8,
  several things may not work correctly (e.g. font-lock highlighting, imenu,
  outline-minor-mode, ...).

- Some indentation algorithms first go back to "the beginning of the
  function" or something similar and then move forward from there.
  This starting point is often searched with a regexp that assumes that it's
  placed at column 0.

For the first point, maybe the best solution is to use `defadvice' on
current-column, current-indentation, and indent-to.  In many cases, it
may be sufficient to pretend that the starting point of the PHP block is "at
column 8" and then the indentation algorithm will automatically preserve
this indentation.

For the second point, the only solution I can think of is to really place
the text in column 0.  In order for you to see the text in column 8, you can
do things like add an overlay on every newline and place on the overlay an
after-string of "        ".  So the text will *appear* on screen in column
8, even though in the buffer and in the file it's actually in column 0.

As for using fields, you can try, but I'd be surprised if it works.


        Stefan


reply via email to

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