Skip to content

Commit

Permalink
chore(Autocommand): refactor and also add auto save cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
rizkyilhampra committed Jan 28, 2024
1 parent 2bb8dbc commit 7e74151
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 21 deletions.
65 changes: 65 additions & 0 deletions lua/aquila/core/autocommands/auto-save.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
local autocmd = vim.api.nvim_create_autocmd
local augroup = vim.api.nvim_create_augroup("my_cfg", {})

---@type table<buffer, uv_timer_t>
local timers = {}
local timeout = 10000

local function save(buf)
vim.api.nvim_buf_call(buf, function() vim.cmd("noautocmd update") end)
end

autocmd({ "InsertLeave", "TextChanged" }, {
group = augroup,
desc = "Schedule auto-saving",
callback = function(event)
local bo = vim.bo[event.buf]
if event.file == "" or bo.buftype ~= "" or bo.filetype == "gitcommit" or bo.readonly or not bo.modified then
return
end

local timer = timers[event.buf]
if timer then
if timer:is_active() then timer:stop() end
else
timer = vim.uv.new_timer()
timers[event.buf] = timer
end
timer:start(timeout, 0, vim.schedule_wrap(function() save(event.buf) end))
end,
})

autocmd({ "FocusLost", "ExitPre", "TermEnter" }, {
group = augroup,
desc = "Save immediately",
callback = function(event)
for buf, timer in pairs(timers) do
if timer:is_active() then
timer:stop()
save(buf)
end
end
end,
})

autocmd({ "BufWritePost", "InsertEnter" }, {
group = augroup,
desc = "Cancel scheduled auto-saving",
callback = function(event)
local timer = timers[event.buf]
if timer and timer:is_active() then timer:stop() end
end,
})

autocmd({ "BufDelete" }, {
group = augroup,
desc = "Remove timer",
callback = function(event)
local timer = timers[event.buf]
if timer then
timers[event.buf] = nil
if timer:is_active() then timer:stop() end
timer:close()
end
end,
})
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,29 @@ vim.api.nvim_create_autocmd("BufEnter", {
end,
desc = "Disable New Line Comment",
})

vim.api.nvim_create_autocmd("BufReadPost", {
callback = function()
local mark = vim.api.nvim_buf_get_mark(0, '"')
if mark[1] > 1 and mark[1] <= vim.api.nvim_buf_line_count(0) then
vim.api.nvim_win_set_cursor(0, mark)
end
end,
desc = "Restore cursor position",
})

vim.api.nvim_create_autocmd("BufReadPost", {
callback = function()
local home = os.getenv("HOME")
if not vim.fn.isdirectory(home .. "/.vim") then
vim.fn.mkdir(home .. "/.vim", "", "rwxrw-")
end
if not vim.fn.isdirectory(home .. "/.vim/undo-dir") then
vim.fn.mkdir(home .. "/.vim/undo-dir", "", "rwx")
end
end,
desc = "Create undo-dir",
})

--automatically save the current buffer (file) under certain conditions.
require('aquila.core.autocommands.auto-save')
23 changes: 2 additions & 21 deletions lua/aquila/core/options.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
vim.g.mapleader = " "

--vim.opt.clipboard = 'unnamedplus'
vim.opt.mouse = 'a'

vim.opt.tabstop = 4
vim.opt.softtabstop = 4
vim.opt.shiftwidth = 4
vim.opt.expandtab = true

vim.opt.smartindent = true

vim.opt.wrap = false

vim.opt.nu = true
vim.opt.relativenumber = true

vim.opt.termguicolors = true

-- vim.opt.hlsearch = false
-- vim.opt.incsearch = true

vim.opt.smartcase = true
vim.opt.ignorecase = true

Expand All @@ -30,7 +21,7 @@ vim.opt.foldlevelstart = 99
vim.opt.foldenable = true
vim.opt.fillchars = [[eob: ,fold: ,foldopen:,foldsep: ,foldclose:]]

vim.opt.smoothscroll = true
vim.opt.smoothscroll = true -- only work in 10.0+
vim.opt.cursorline = true
vim.opt.scrolloff = 14
vim.opt.sidescrolloff = 8
Expand Down Expand Up @@ -59,15 +50,5 @@ vim.opt.splitbelow = true
vim.opt.splitright = true
vim.opt.showmode = false
vim.opt.cmdheight = 0

local home = os.getenv("HOME")
if not vim.fn.isdirectory(home .. "/.vim") then
vim.fn.mkdir(home .. "/.vim", "", "rwxrw-")
end

if not vim.fn.isdirectory(home .. "/.vim/undo-dir") then
vim.fn.mkdir(home .. "/.vim/undo-dir", "", "rwx")
end
vim.opt.undodir = home .. "/.vim/undodir"

vim.opt.undodir = os.getenv("HOME") .. "/.vim/undodir"
vim.opt.undofile = true

0 comments on commit 7e74151

Please sign in to comment.