Skip to content

Commit

Permalink
docs: add better event examples
Browse files Browse the repository at this point in the history
  • Loading branch information
olimorris committed Nov 6, 2023
1 parent 71352aa commit 5b5ec1c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ vim.api.nvim_create_autocmd({ "User" }, {
})
```

Session data is also made available to the callbacks:
Another and more commonly requested example is to use the Telescope extension to load a session, saving the current session before clearing all of the open buffers. This can be achieved by utilising some of the session data that is made available to the callbacks:

```lua
local group = vim.api.nvim_create_augroup("PersistedHooks", {})
Expand All @@ -326,7 +326,11 @@ vim.api.nvim_create_autocmd({ "User" }, {
pattern = "PersistedTelescopeLoadPre",
group = group,
callback = function(session)
print(session.data.branch) -- Print the session's branch
-- Save the currently loaded session
require("persisted").save({ session = vim.g.persisted_loaded_session })

-- Delete all of the open buffers
vim.api.nvim_input("<ESC>:%bd!<CR>")
end,
})
```
Expand Down
33 changes: 23 additions & 10 deletions doc/persisted.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ Install the plugin with your preferred package manager:

>lua
-- Lua
use({
"olimorris/persisted.nvim"
{
"olimorris/persisted.nvim",
config = true
})
}
<

**Packer**
Expand Down Expand Up @@ -124,12 +124,13 @@ Once opened, the available keymaps are:
- `<CR>` - Source the session file
- `<C-d>` - Delete the session file

**Statuslines**
**Global variables**

The plugin sets global variables which can be utilised in your configuration:

The plugin sets a global variable, `vim.g.persisting`, which is set to `true`
when a session is started and `false` when it is stopped. Also, the plugin
offers a `PersistedStateChange` event that can be hooked into via an autocmd
(see the |persisted.nvim-events/callbacks| section).

- `vim.g.persisting` - This is set to `true` when a session is started and `false` when a session is stopped
- `vim.g.persisted_loaded_session` - The file path to the currently loaded session


CONFIGURATION *persisted.nvim-configuration*
Expand Down Expand Up @@ -197,6 +198,10 @@ files for a given project, by using git branches. To enable git branching:

**Note**If git branching is enabled on a non git enabled repo, then `main` will
be used as the default branch
If you switch branches in a repository, the plugin will try to load a session
which corresponds to that branch. If it can’t find one, then it will load the
session from the `main` branch.


AUTOSAVING ~

Expand Down Expand Up @@ -340,6 +345,7 @@ hook into:
- `PersistedDeletePre` - For _before_ a session is deleted
- `PersistedDeletePost` - For _after_ a session is deleted
- `PersistedStateChange` - For when a session is _started_ or _stopped_
- `PersistedToggled` - For when a session is toggled

For example, to ensure that the excellent minimap
<https://github.com/wfxr/minimap.vim> plugin is not saved into a session, an
Expand All @@ -357,7 +363,10 @@ autocmd can be created to hook into the `PersistedSavePre` event:
})
<

Session data is also made available to the callbacks:
Another and more commonly requested example is to use the Telescope extension
to load a session, saving the current session before clearing all of the open
buffers. This can be achieved by utilising some of the session data that is
made available to the callbacks:

>lua
local group = vim.api.nvim_create_augroup("PersistedHooks", {})
Expand All @@ -366,7 +375,11 @@ Session data is also made available to the callbacks:
pattern = "PersistedTelescopeLoadPre",
group = group,
callback = function(session)
print(session.data.branch) -- Print the session's branch
-- Save the currently loaded session
require("persisted").save({ session = vim.g.persisted_loaded_session })

-- Delete all of the open buffers
vim.api.nvim_input("<ESC>:%bd!<CR>")
end,
})
<
Expand Down

0 comments on commit 5b5ec1c

Please sign in to comment.