Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-save for specific file #59

Open
xypnox opened this issue Sep 26, 2022 · 2 comments
Open

Auto-save for specific file #59

xypnox opened this issue Sep 26, 2022 · 2 comments

Comments

@xypnox
Copy link

xypnox commented Sep 26, 2022

I recently upgraded to the new version and setup the config as:

Is there a way to have utils.in(fn.getbufvar(buf, "&filetype"), {'md', 'tex', 'org', 'txt'}) as in:

local autosave = require("auto-save")

autosave.setup({
    enabled = true,
    execution_message = "AutoSave: saved at " .. vim.fn.strftime("%H:%M:%S"),
    events = {"InsertLeave", "TextChanged"},
    -- function that determines whether to save the current buffer or not
	-- return true: if buffer is ok to be saved
	-- return false: if it's not ok to be saved
	condition = function(buf)
		local fn = vim.fn
		local utils = require("auto-save.utils.data")
		if
			fn.getbufvar(buf, "&modifiable") == 1 and
            utils.in(fn.getbufvar(buf, "&filetype"), {'md', 'tex', 'org', 'txt'}) then
			return true -- met condition(s), can save
		end
		return false -- can't save
	end,
    write_all_buffers = false,
    debounce_delay = 135
})

Where I only want to save for 'md' 'tex' etc.

I am not well versed in lua and thus am having difficulty with checking whether the buf type is in the provided list of allowed filetypes.

@okuuva
Copy link

okuuva commented Oct 9, 2022

AFAIK there's no built in way in Lua to check if element exists in a table. While there's no explicit in method in utils either you can use the set_of method there to do exactly that:

	condition = function(buf)
		local fn = vim.fn
		local utils = require("auto-save.utils.data")
		if
			fn.getbufvar(buf, "&modifiable") == 1
			and utils.set_of({'md', 'tex', 'org', 'txt'})[fn.getbufvar(buf, "&filetype")]
		then
			return true -- met condition(s), can save
		end
		return false -- can't save
	end,

You can check what set_of and not_in do here: https://github.com/Pocco81/auto-save.nvim/blob/main/lua/auto-save/utils/data.lua

So basically if one would add in method there it would look like this:

function M.in(var, arr)
	return M.set_of(arr)[var]
end

@pkulak
Copy link

pkulak commented Mar 24, 2024

My file types are the full names for some reason, so I had to do like so (plus I wanted to NOT save instead):

and not utils.set_of({'md', 'markdown'})[fn.getbufvar(buf, "&filetype")]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants