Skip to content

Commit

Permalink
feat: :SosBufToggle command
Browse files Browse the repository at this point in the history
This command allows for ignoring individual buffers so that they do not
get autosaved. Accepts an optional `{bufname}` argument to specify a
buffer other than the current one. Deleting a buffer (e.g. `:bdelete`)
resets its ignored status. Ignore status is stored at `b:sos_ignore`.

Also add dedicated API functions for this, as well as functions to
enable or disable the entire plugin. These are all found in the
`require('sos')` module.

Also do some refactoring/cleanup.
  • Loading branch information
tmillr committed Sep 9, 2024
1 parent 0351264 commit b7f8d4c
Show file tree
Hide file tree
Showing 11 changed files with 582 additions and 348 deletions.
1 change: 0 additions & 1 deletion lua/sos/autocmds.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local M = {}
local commands = require 'sos.commands'
local impl = require 'sos.impl'
local api = vim.api
local augroup = 'sos-autosaver'
Expand Down
203 changes: 0 additions & 203 deletions lua/sos/bufevents.lua

This file was deleted.

87 changes: 77 additions & 10 deletions lua/sos/commands.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
local errmsg = require('sos.util').errmsg
local api = vim.api
local extkeys = { action = true }
local extkeys = { [1] = true }

-- TODO: types

---@class (exact) sos.Command: vim.api.keyset.user_command
---@field [1] string|function

local function Command(def)
return setmetatable(def, {
__call = function(f, ...) return f.action(...) end,
__call = function(self, ...) return self[1](...) end,
})
end

Expand All @@ -19,13 +23,14 @@ local function filter_extkeys(tbl)
return ret
end

---@param parent table<string, sos.Command>
local function Commands(parent, ret)
ret = ret or {}

for k, v in pairs(parent) do
if type(v) == 'table' and v.action then
if type(v) == 'table' and v[1] then
ret[k] = Command(v)
api.nvim_create_user_command(k, v.action, filter_extkeys(v))
api.nvim_create_user_command(k, v[1], filter_extkeys(v))
else
Commands(v, ret)
end
Expand All @@ -34,23 +39,85 @@ local function Commands(parent, ret)
return ret
end

-- local function assert_msg(cond, msg, ...)
-- if not cond then
-- api.nvim_notify(msg:format(...), vim.log.levels.ERROR, {})
-- coroutine.yield(cond)
-- error 'resumed dead coroutine'
-- end
--
-- return cond
-- end
--
-- local function ignore_error(f)
-- return function(...)
-- local ok, e = pcall(f)
-- if not ok and e and e ~= '' then error(e) end
-- end
-- end
--
-- local function resolve_bufspec(bufspec) end

return Commands {
SosEnable = {
desc = 'Enable sos autosaver',
action = function() require('sos').setup { enabled = true } end,
nargs = 0,
force = true,
function() require('sos').enable(true) end,
},

SosDisable = {
desc = 'Disable sos autosaver',
action = function() require('sos').setup { enabled = false } end,
nargs = 0,
force = true,
function() require('sos').disable(true) end,
},

SosToggle = {
desc = 'Toggle sos autosaver',
action = function()
require('sos').setup {
enabled = not require('sos.config').enabled,
}
nargs = 0,
force = true,
function()
if require('sos.config').enabled then
require('sos').disable(true)
else
require('sos').enable(true)
end
end,
},

SosBufToggle = {
desc = 'Toggle autosaver for buffer',
nargs = '?',
count = -1,
addr = 'buffers',
complete = 'buffer',
force = true,
function(info)
if #info.fargs > 1 then return errmsg 'only 1 argument is allowed' end
local buf

if info.range > 0 then
-- Here we either have range and int arg, or just range. No way to
-- decipher between the two. `count` is rightmost of the two on cmdline.
buf = info.count

if #info.fargs > 0 then
return errmsg 'only 1 arg or count is allowed, got both'
elseif info.range > 1 then
return errmsg 'only 1 arg or count is allowed, got range'
elseif buf < 1 or not api.nvim_buf_is_valid(buf) then
return errmsg('invalid buffer: ' .. buf)
end
else
local arg = info.fargs[1]

-- Use `[$]` for `$`, otherwise we'll get highest bufnr.
buf = vim.fn.bufnr(arg == '$' and '[$]' or arg or '')
if buf < 1 then errmsg 'argument matched none or multiple buffers' end
end

require('sos').toggle_buf(buf, true)
end,
},
}
19 changes: 10 additions & 9 deletions lua/sos/config.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
---@class sos.Config # Plugin options passed to `setup()`.
---@field enabled boolean | nil # Whether to enable or disable the plugin.
---@field timeout integer | nil # Timeout in ms. Buffer changes debounce the timer.
---@field autowrite boolean | "all" | nil # Set and manage Vim's 'autowrite' option.
---@field save_on_cmd "all" | "some" | table<string, true> | false | nil # Save all buffers before executing a command on cmdline
---@field save_on_bufleave boolean | nil # Save current buffer on `BufLeave` (see `:h BufLeave`)
---@field save_on_focuslost boolean | nil # Save all bufs when Neovim loses focus or is suspended.
---@field should_observe_buf nil | fun(buf: integer): boolean # Return true to observe/attach to buf.
---@field on_timer function # The function to call when the timer fires.
---@class sos.Config # Plugin options passed to `setup()`.
---@field enabled? boolean # Whether to enable or disable the plugin.
---@field timeout? integer # Timeout in ms. Buffer changes debounce the timer.
---@field autowrite? boolean | "all" # Set and manage Vim's 'autowrite' option.
---@field save_on_cmd? "all" | "some" | table<string, true> | false # Save all buffers before executing a command on cmdline
---@field save_on_bufleave? boolean # Save current buffer on `BufLeave` (see `:h BufLeave`)
---@field save_on_focuslost? boolean # Save all bufs when Neovim loses focus or is suspended.
---@field should_observe_buf? fun(buf: integer): boolean # Return true to observe/attach to buf.
---@field on_timer? function # The function to call when the timer fires.

local defaults = {
enabled = true,
timeout = 20000,
Expand Down
Loading

0 comments on commit b7f8d4c

Please sign in to comment.