Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Really protect against unwanted indentation after undo #119

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

joaotavora
Copy link
Contributor

@joaotavora joaotavora commented Nov 19, 2018

When I am undoing changes to a region I really want the changes to be
undone verbatim, i.e. I want the buffer's contents to be as they were before
the change I undid (this is the very definition of undo).

This is probably the rationale for have undo-in-progress in
aggressive-indent--internal-dont-indent-if in the first place.

But it doesn't fix the whole problem, because changes performed by
undo are still recorded into aggressive-indent--changed-list and the
very next command will indent those regions. This means it's
impossible in practice to use undo to undo an aggressive indent of a
region.

The fix proposed here checks undo-in-progress before registering a
change. It's possible that other elements (but maybe not all) in
aggressive-indent--internal-dont-indent-if merit this treatment, too.

  • aggressive-indent.el (aggressive-indent--keep-track-of-changes):
    Check undo-in-progress.

joaotavora added a commit to joaotavora/aggressive-indent-mode that referenced this pull request Nov 19, 2018
This protects against other similar "indent on very next command"
problems similar to the one described in Malabarba#119.

* aggressive-indent.el
(aggressive-indent--proccess-changed-list-and-indent): Clear
aggressive-indent--changed-list.
(aggressive-indent--keep-track-of-changes): Don't check undo-in-progress
joaotavora added a commit to joaotavora/aggressive-indent-mode that referenced this pull request Nov 19, 2018
This protects against other similar "indent on very next command"
problems similar to the one described in Malabarba#119.

* aggressive-indent.el
(aggressive-indent--proccess-changed-list-and-indent): Clear
aggressive-indent--changed-list.
(aggressive-indent--keep-track-of-changes): Don't check undo-in-progress
@Malabarba
Copy link
Owner

Thanks for the effort João.

As it stands, I think the solution is too aggressive. IIUC, this code clears out all previously stored changes, even if they are older than the undo.

@joaotavora
Copy link
Contributor Author

Too aggressive for aggressive-indent? It makes it less aggressive! :)

Jokes aside, can you describe a case where my approach wouldn't work or is it just a feeling?

@joaotavora
Copy link
Contributor Author

Right, I think I see the problem. Perhaps I can add a unique marker to the change list in the post-command-book and not forget past that marker. Is that ok?

@joaotavora
Copy link
Contributor Author

joaotavora commented Nov 22, 2018

@Malabarba I changed my approach completely. It seems simpler and less disruptive. There is still the off-chance, I think that clearing the changes, which happens in the post-command-hook clears some change performed by a previous command. We could circumvent this by marking the most recent change in pre-command-hook and not clearing past that one.

EDIT: this does make sense after all

@joaotavora
Copy link
Contributor Author

joaotavora commented Nov 22, 2018

Nevermind, it seems to be buggy too... EDIT: It's not

@joaotavora
Copy link
Contributor Author

Nevermind the nevermind, it is correct after all. I'll just take care of a minor detail in a further commit.

@joaotavora
Copy link
Contributor Author

@Malabarba that's it, uff. You can review this now :-)

@joaotavora
Copy link
Contributor Author

@Malabarba ping?

I'm sorry about the hesitation, but the current mode seems to be working currently in my testing, and is still a fairly surgical change.

@CeleritasCelery
Copy link
Contributor

This patch has made aggressive-indent much more predictable for me. I would recommend merging it.


Clears `aggressive-indent--changed-list' iff the current
command (the one that's now finished) lives in
`aggressive-indent-protected-current-commands'."
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo. It's actually the other variable that the code is using.

@Malabarba
Copy link
Owner

Sorry for taking so long, and thanks a lot for your effort João.
The code looks very surgical indeed. :-)

Just fix the typo and we're ready to merge.

@CeleritasCelery
Copy link
Contributor

@joaotavora
ping

Fix that typo and rebase and this can be merged in.

@joaotavora
Copy link
Contributor Author

Fix that typo and rebase and this can be merged in.

Sorry. I'm not using a-i-m anymore and I don't have time to confirm Artur's comment. But you can make a pull request that includes my commit and another one on top of it that fixes the typo, and explains why.

@joaotavora
Copy link
Contributor Author

But I can rebase the PR, that's easy to do.

@joaotavora
Copy link
Contributor Author

So I just rebased it. If you can find out what the "other variable" is, i.e.e how that comment should be, just tell me and I'll do it.

@CeleritasCelery
Copy link
Contributor

Replace aggressive-indent-protected-current-commands with aggressive-indent-protected-commands.

The current part is the typo.

@joaotavora
Copy link
Contributor Author

joaotavora commented Dec 13, 2019 via email

When I am undoing changes to a region I really want the changes to be
undone verbatim, that is, I want the buffer's contents to be like they
were before the change that I want to undo occured.  This is the very
definition of undo.

It is probably the rationale for giving undo special treatment in
aggressive-indent--internal-dont-indent-if in the first place.  It
means that the undo command won't immediately cause reindentation.

But it doesn't fix the whole problem, because changes performed by
undo itself are still recorded into aggressive-indent--changed-list
and the very next command, be it a move or an edit somewhere else,
will still cause those regions to be indented.  Among other things,
it's impossible in practice to use `undo' to undo an aggressive indent
of a region.

The proposed fix checks adds a function to the post-command hook to
check for aggressive-indent-protected-commands and clear the change
list if the current command is in that list.  This is safe if those
changes can only have been performed by the command whose
auto-indentation effects we want to prevent.

* aggressive-indent.el (aggressive-indent--keep-track-of-changes):
Check undo-in-progress.

* aggressive-indent.el
(aggressive-indent--internal-dont-indent-if): Remove check of
aggressive-indent-protected-commands and undo-in-progress.
(aggressive-indent--post-command): New helper.
(aggressive-indent-mode): Put aggressive-indent--post-command in
post-command-hook and remove it when exiting minor mode.
* aggressive-indent.el
(aggressive-indent--pre-command-change-head): New variable.
(aggressive-indent--pre-command): New function.
(aggressive-indent-mode): Add aggressive-indent--pre-command to
pre-command-hook and remove it when exiting minor mode.
@CeleritasCelery
Copy link
Contributor

@Malabarba this is ready to merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants