rcarriga / nvim-notify

A fancy, configurable, notification manager for NeoVim
MIT License
2.97k stars 78 forks source link

Cancelling the timeout of an active notification? #151

Closed ianbattersby closed 1 year ago

ianbattersby commented 1 year ago

Sincere thanks for the amazing plugin and all the hard work!

I'm trying to mangle my config so that I can break out a notification into a different tab should (a) it be hard to read inside the popup window and/or (b) I want to take a better look. Breaking the buffer into a separate tab was quite straight forward using the on_open event (see below) but I'm struggling to see how I can cancel the associated timer that will close the buffer. Is this even possible or should I look to duplicate the buffer contents into a new one?

    on_open = function(win)
      local bufnr = vim.api.nvim_win_get_buf(win)

      if bufnr then
        vim.keymap.set(
          "n",
          "<C-Space>",
          require("notify").dismiss,
          { noremap = true, silent = true, desc = "Dismiss", buffer = bufnr }
        )

        vim.keymap.set(
          "n",
          "<C-t>",
          "<CMD>tab split<CR>",
          { noremap = true, silent = true, desc = "Dismiss", buffer = bufnr }
        )
      end
    end,
rcarriga commented 1 year ago

Went down a couple of routes to do this myself but the best way to do this is to avoid using the existing buffer, it's tied to the animation so even if you stop the timeout, there can be other issues with opacity changes.

Instead, I've added a new notify.open function which you can use to open and render a notification in a buffer including all of the highlights and extmarks (doing this manually would have been tricky :sweat_smile:).

Sample usage for your use case

local notify = require("notify")

local notif
notif = notify("Hello World", vim.log.levels.INFO, {
  on_open = function(win)
    local bufnr = vim.api.nvim_win_get_buf(win)

    vim.keymap.set("n", "<C-t>", function()
      local opened = require("notify").open(notif)
      vim.cmd("tab split")
      vim.api.nvim_win_set_buf(0, opened.buffer)
    end, { noremap = true, silent = true, desc = "Dismiss", buffer = bufnr })
  end,
})
ianbattersby commented 1 year ago

That is amazing - thank you! 🙇

FWIW, I'm currently using @folke's excellent noice.nvim so have coded this into its notify backend for the moment. I suspect @folke might be interested in adding this as a configuration option if the other backends can also support it.

folke commented 1 year ago

@ianbattersby for Noice, you can simply do :Noice last. That'll open the last noice message (which can be displayed anywhere like nvim-notify) in a popup.

ianbattersby commented 1 year ago

@folke This is an excellent point, and I've now wired up some mappings to take best advantage. I'm still going to experiment with being able to extend mvim-notify hooks as to facilitate more complex scenarios (and leverage @rcarriga's hard work).

Thanks again to you both, immensely grateful 🙇