persisted.nvim icon indicating copy to clipboard operation
persisted.nvim copied to clipboard

User contributed callbacks

Open olimorris opened this issue 1 year ago • 1 comments

Below are a collection of callbacks which you may find useful.

olimorris avatar Aug 29 '22 13:08 olimorris

These callbacks are for showing notify window when loading session via telescope as suggested in #24, also clearing all buffers before loading session.

Callbacks Example
    telescope = {
      before_source = function()
        local status_ok, _ = pcall(require, "bufdelete")
        if status_ok then
          vim.api.nvim_input("<Esc><Cmd>bufdo Bdelete<CR>")
        else
          vim.api.nvim_input("<Esc><Cmd>%bd<CR>")
        end
      end,
      after_source = function(session)
        vim.defer_fn(function()
          local status_ok, notify = pcall(require, "notify")
          if status_ok then
            notify(
              "Loaded session " .. session.name,
              vim.log.levels.INFO,
              { title = title }
            )
          else
            print("Loaded session " .. session.name)
          end
        end, 0)
      end,
    },

latipun7 avatar Aug 29 '22 23:08 latipun7

These callbacks are for showing notify window when loading session via telescope as suggested in #24, also clearing all buffers before loading session.

Just wanted to point out that you could use just plain vim.notify() instead of pcalling notify in the after_source function. I think that notify users are gonna override the defaut notify-function as the notify readme suggest setting vim.notify = require("notify").

Here's the one I use for both regular and telescope after_source leveraging the aforementioned trick:

Allpurpose `after_source` example
local after_source = function(session)
  local message = "Autoloaded session"
  if session then
    message = "Loaded session " .. session.name
  end

  vim.defer_fn(function()
    vim.notify(message, vim.log.levels.INFO, { title = "Session manager" })
  end, 0)
end

okuuva avatar Sep 27 '22 14:09 okuuva

jump between sessions smoothly using telescope and do not autosave when dirpath starts with /tmp:

callback snippet #40
should_autosave = function()                                                 
    local path = vim.fn.expand "%:p"                                         
    if vim.bo.filetype == "alpha" or string.find(path, "/tmp") == 1 then     
        return false                                                         
    else                                                                     
        return true                                                          
    end                                                                      
end,                                                                         
                                                                    
telescope = {                          
    -- jump between session smoothly                                         
    after_source = function(param)                                           
        vim.api.nvim_command "%bd"                                           
        local path = param.dir_path                                                                                                    
        if string.find(path, "/") ~= 1 then                                  
            vim.api.nvim_command("cd " .. vim.fn.expand "~" .. "/" .. path)  
            vim.api.nvim_command("tcd " .. vim.fn.expand "~" .. "/" .. path) 
        else                                                                 
            vim.api.nvim_command("cd " .. path)                              
            vim.api.nvim_command("tcd " .. path)                             
        end                                                                  
    end,                                                                     
},                                                                           

iujakchu avatar Nov 01 '22 09:11 iujakchu