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

feat: undojoin timestamp updates #1272

Merged
merged 1 commit into from
Feb 22, 2024

Conversation

benlubas
Copy link
Contributor

addresses issues brought up in #1112 (currently closed)

Problem

Updating the time stamp can cause issues with undo-ing things. This is particularly bad when an autosave plugin is used b/c the user will make a change > autosave > timestamp update > autosave > undo (undoes timestamp update) > autosave > timestamp update > loop. The scenario's described assume there is an autosave plugin saving on text change/insert leave.

Making it impossible to undo changes in the norg file with autosave (while the updated: field is present in the metadata).

Proposed Solution

This PR makes two changes that make it possible to bind keys to mitigate this issue:

  • ["core.esupports.metagen"].config.undojoin_updates = true - When true, we call :undojoin just before updating the updated: field in the metadata. With this set to true you can make a change, write the file and press u to undo your change and the timestamp update. But, you will still get another time stamp update after the undo. Making multiple undos in a row impossible b/c of branches. Enter: the second addition..
  • require("neorg.modules.core.esupports.metagen.module").public.skip_next_update() - a function which will disable exactly one metadata 'updated at' update. This function can prevent the issue above, and makes it possible to remap the u and <c-r> keys so that they behave mostly normally in a norg doc with autosave.

This solution is maybe not the most elegant. I'm open to feedback on how to make it better, I threw this together quickly and without much thought after I realized that it would mostly work.

I'd like to test drive this for a few more days as well before it potentially gets merged.

Example Usage

--- config..
["core.esupports.metagen"] = {
  config = {
    undojoin_updates = true,
  },
},
--- config..
["core.keybinds"] = {
  config = {
    hook = function(keybinds)
      keybinds.map("norg", "n", "u", function()
        require("neorg.modules.core.esupports.metagen.module").public.skip_next_update()
        local k = vim.api.nvim_replace_termcodes("u<c-o>", true, false, true)
        vim.api.nvim_feedkeys(k, 'n', false)
      end)
      keybinds.map("norg", "n", "<c-r>", function()
        require("neorg.modules.core.esupports.metagen.module").public.skip_next_update()
        local k = vim.api.nvim_replace_termcodes("<c-r><c-o>", true, false, true)
        vim.api.nvim_feedkeys(k, 'n', false)
      end)
    end,
  },
},
--- config..

@benlubas benlubas marked this pull request as ready for review January 20, 2024 18:52
@benlubas
Copy link
Contributor Author

I've used this for about a week and haven't had major issues with it.

This definitely feels like a hack. But I can't think of a better way to do this (other than, just don't use autosave in norg files).

@vhyrro
Copy link
Member

vhyrro commented Feb 22, 2024

Sounds like a good idea for the time being. I can't really think of any alternative solution either. Thank you!

@vhyrro vhyrro merged commit fe25e93 into nvim-neorg:main Feb 22, 2024
1 of 2 checks passed
terrortylor pushed a commit to terrortylor/neovim-environment that referenced this pull request Apr 23, 2024
sahinf pushed a commit to sahinf/neorg that referenced this pull request May 15, 2024
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.

2 participants