unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Automatic smart indentation
@ 2003-10-30 23:38 Martin Stone Davis
  0 siblings, 0 replies; 3+ messages in thread
From: Martin Stone Davis @ 2003-10-30 23:38 UTC (permalink / raw)


I've read the threads in this list pertaining to indentation, but I 
haven't found a solution to the following problem:  While editing a lisp 
program, I want to keep the indentation "correct" at all times.

i.e. If I yank I want to reindent. If I delete a ), I want it to 
reindent, If I insert a (, change the name of a function from if to 
when, undo etc. etc. etc., I want to reindent.

The solution I came up with is:

(setq lisp-indent-function 'common-lisp-indent-function)
(add-hook 'after-change-functions
  	  (lambda (start end pre-change-length)
              (indent-region start (buffer-size) nil)))

The problem is that it somehow gets into an endless loop *sometimes* 
when I undo.  It might have problems in other situations, but that's 
whet I've found so far.

Any ideas?

Thanks,
-Martin

P.S. Also, I've noticed that my solution is somewhat whimsical. 
Sometimes it gets the indentation completely wrong, and lots of things 
end up on the left margin.  I'll try to come up with an reproducible 
example of this problem after I get some solution to the above.  This 
may be a problem with common-lisp-indent-function or indent-region.

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

* Re: Automatic smart indentation
       [not found] <mailman.2862.1067557221.21628.help-gnu-emacs@gnu.org>
@ 2003-10-31 16:04 ` Kevin Rodgers
  2003-10-31 17:23 ` Stefan Monnier
  1 sibling, 0 replies; 3+ messages in thread
From: Kevin Rodgers @ 2003-10-31 16:04 UTC (permalink / raw)


Martin Stone Davis wrote:

> I've read the threads in this list pertaining to indentation, but I 
> haven't found a solution to the following problem:  While editing a lisp 
> program, I want to keep the indentation "correct" at all times.
> 
> i.e. If I yank I want to reindent. If I delete a ), I want it to 
> reindent, If I insert a (, change the name of a function from if to 
> when, undo etc. etc. etc., I want to reindent.
> 
> The solution I came up with is:
> 
> (setq lisp-indent-function 'common-lisp-indent-function)
> (add-hook 'after-change-functions
>        (lambda (start end pre-change-length)
>              (indent-region start (buffer-size) nil)))
> 
> The problem is that it somehow gets into an endless loop *sometimes* 
> when I undo.  It might have problems in other situations, but that's 
> whet I've found so far.

Sorry, I can't help you with that.  But I can tell you that (buffer-size)
should almost certainly be (point-max).  And you might want to add a check
for (eq major-mode 'lisp-mode), or make after-change-functions a local hook
variable in Lisp mode buffers (by putting the add-hook in a lisp-mode-hook
function, and specifying the optional LOCAL arg).

You might also have better luck if you call indent-region with a region
that starts at the beginning of the line:
(save-excursion (goto-char start) (line-beginning-position))

-- 
Kevin Rodgers

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

* Re: Automatic smart indentation
       [not found] <mailman.2862.1067557221.21628.help-gnu-emacs@gnu.org>
  2003-10-31 16:04 ` Automatic smart indentation Kevin Rodgers
@ 2003-10-31 17:23 ` Stefan Monnier
  1 sibling, 0 replies; 3+ messages in thread
From: Stefan Monnier @ 2003-10-31 17:23 UTC (permalink / raw)


> (add-hook 'after-change-functions
>   	  (lambda (start end pre-change-length)
>               (indent-region start (buffer-size) nil)))

Modifying the buffer from within after-change-functions is tricky
business.  A more reliable approach is to keep track in
after-change-functions of the part of the text that gets modified,
and reindent later (from a post-command-hook).

See diff-mode.el for an example of how to do that.


        Stefan

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

end of thread, other threads:[~2003-10-31 17:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.2862.1067557221.21628.help-gnu-emacs@gnu.org>
2003-10-31 16:04 ` Automatic smart indentation Kevin Rodgers
2003-10-31 17:23 ` Stefan Monnier
2003-10-30 23:38 Martin Stone Davis

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