Coder Social home page Coder Social logo

Comments (7)

luisjakon avatar luisjakon commented on June 3, 2024 1

Hi, Just here in case anyone else is having similar issues with switching and restoring user state for command modes when going in and out of neo-tree with a mouse clicker or other means...

Thnxs!

-- initialize user command-mode state (just for neo-tree)
vim.g.forgetfulme = "n"

return {
  "nvim-neo-tree/neo-tree.nvim",
  config = function()
    require("neo-tree").setup {
      ...
      -- Set-up event handlers to manage and restore user command-mode state
      event_handlers = {
        {
          event = "neo_tree_buffer_enter",
          handler = function()
            vim.g.forgetfulme = vim.api.nvim_get_mode().mode
            if vim.g.forgetfulme == "i" then vim.cmd "stopinsert" end
          end,
        },
        {
          event = "neo_tree_buffer_leave",
          handler = function()
            if vim.g.forgetfulme == "i" then vim.cmd "startinsert" end
            vim.g.forgetfulme = vim.api.nvim_get_mode().mode
          end,
        },
      },
    }
  end,
}

from neo-tree.nvim.

pysan3 avatar pysan3 commented on June 3, 2024

Are you sure you want a plugin to mess around with your mode automatically?

from neo-tree.nvim.

luisjakon avatar luisjakon commented on June 3, 2024

Hi! thnxs @pysan3. Good point.

Perhaps it could be temporarily store the current mode when focus is moved to the neotree window and restore it when it leaves.

Otherwise, having focus while on insert mode has no value...adding an extra unnecessary keystroke to exit to normal mode and do something with it. Makes sense?

from neo-tree.nvim.

cseickel avatar cseickel commented on June 3, 2024

I don't think it is the place of any plugin to control the mode. Here's what I would suggest:

  1. If you are insert mode and done with your edits, exit insert mode. Always.
  2. If you have a mapping that allows you to switch windows while still in insert mode, you should update that mapping to leave insert mode before switching windows.
  3. Clicking with a mouse: 😱
  4. ...but seriously, see 1.

All that being said, the best solution is to create an autocmd to run stopinsert when you switch windows:

augroup ForgetfulMe
  autocmd!
  autocmd WinEnter * :stopinsert
augroup end

If someone wanted to put up a PR that does that autocmd specifically for neo-tree windows, I would probably accept it.

from neo-tree.nvim.

pysan3 avatar pysan3 commented on June 3, 2024

If you enter help page I think you'll get the same error. I don't think that is a bug but a feature so same for us.

from neo-tree.nvim.

luisjakon avatar luisjakon commented on June 3, 2024

Ok. Thanks for the response!

I'm pretty new at this so I thought I'd follow up with your suggestion with something like this, but somehow it is not triggering on neovim...may need to spend more time with it...Thnks!

vim.g.forgetfulme = "n"

vim.api.nvim_create_augroup("ForgetfulMe", { clear = true })

vim.api.nvim_create_autocmd({ "BufEnter", "FocusGained" }, {
  group = "ForgetfulMe",
  pattern = { "neo-tree" },
  callback = function()
    vim.g.forgetfulme = vim.api.nvim_get_mode().mode
    if vim.g.forgetfulme == "i" then
      vim.cmd "stopinsert"
    end
  end,
})

vim.api.nvim_create_autocmd({ "BufLeave", "FocusLost" }, {
  group = "ForgetfulMe",
  pattern = { "neo-tree" },
  callback = function()
    if vim.g.forgetfulme == "i" then 
      vim.cmd "startinsert" 
    end
    vim.g.forgetfulme = vim.api.nvim_get_mode().mode
  end,
})

from neo-tree.nvim.

pysan3 avatar pysan3 commented on June 3, 2024

Use neo-tree's event handlers instead of autocmds.

:h neo-tree-events

from neo-tree.nvim.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.