aggressive-indent-mode icon indicating copy to clipboard operation
aggressive-indent-mode copied to clipboard

[feature requrest] only auto indent when leaving the current line

Open TLINDEN opened this issue 9 years ago • 4 comments

Hello,

it would be good to have a flag to enable such a feature. I do often have the problem, that aggressive-indent-mode starts indenting stuff while I'm writing code in a line which is not finished yet.

For example: if(

The mode now indents everything after this. However, when I'm done the line looks like this: if($blah){return}

So, no re-indent would have been required.

If the mode would only try to re-indent after I leave the line (ie. pressing RET) it would be best (for me, at least).

Thanks, Tom

TLINDEN avatar Jul 29 '16 07:07 TLINDEN

You can avoid indenting in that situation with the dont-indent-if variable. Try the following:

(let ((line (thing-at-point 'line)))
  (and (stringp line)
       (string-match "\\bif *([^)]*$" line)))

Malabarba avatar Oct 12 '16 19:10 Malabarba

I tried:

(add-to-list 'aggressive-indent-dont-indent-if
             (let ((line (thing-at-point 'line)))
               (and (stringp line)
                    (string-match "\\bif *([^)]*$" line))))

but this doesn't work. Indenting still starts whenever I enter anything on a line. In the case of an if-statement, it starts after I entered "i". The same applies for while (starts at "w") or for (starts at "f") etc.

So I came up with this:

(setq my-last-line-num 0)
(add-to-list 'aggressive-indent-dont-indent-if
             (lambda ()
               (let ((line (line-number-at-pos)))
                 (if (eq line my-last-line-num)
                     (progn
                       t)
                   (progn
                     (setq my-last-line-num line)
                     nil )))))

I added a couple of debug-prints, but nothing gets printed, so it seems it doesn't get executed at all.

TLINDEN avatar Oct 13 '16 10:10 TLINDEN

This should do what you're trying to achieve.

(add-to-list 'aggressive-indent-dont-indent-if
             '(let ((line (line-number-at-pos)))
                (if (eq line my-last-line-num)
                    t
                  (progn
                    (setq my-last-line-num line)
                    nil)))))

Malabarba avatar Oct 13 '16 12:10 Malabarba

Unfortunately it doesn't. With this in place, no indenting at all happens any more (which was the same with my version), unless if I hit .

TLINDEN avatar Oct 13 '16 13:10 TLINDEN