aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2021-12-04 13:09:34 +0900
committernsfisis <nsfisis@gmail.com>2021-12-04 15:38:47 +0900
commit7482bb03a7943a3fd9049c7ba61033b42387fac2 (patch)
treec5dcb4a27b723ca6fc14a22366e4d2b9477c694a
parent12f18a076f79470941f7ed54ac2de7effcbff740 (diff)
downloaddotfiles-7482bb03a7943a3fd9049c7ba61033b42387fac2.tar.gz
dotfiles-7482bb03a7943a3fd9049c7ba61033b42387fac2.tar.zst
dotfiles-7482bb03a7943a3fd9049c7ba61033b42387fac2.zip
neovim: refactor
-rw-r--r--.config/nvim/autoload/autosave.vim64
-rw-r--r--.config/nvim/autoload/dummy.vim39
-rw-r--r--.config/nvim/init.lua11
-rw-r--r--.config/nvim/lua/autosave.lua80
-rw-r--r--.config/nvim/lua/vimrc.lua68
-rw-r--r--.config/nvim/plugin/autosave.vim14
-rw-r--r--.config/nvim/plugin/dummy.vim42
7 files changed, 165 insertions, 153 deletions
diff --git a/.config/nvim/autoload/autosave.vim b/.config/nvim/autoload/autosave.vim
deleted file mode 100644
index be64707..0000000
--- a/.config/nvim/autoload/autosave.vim
+++ /dev/null
@@ -1,64 +0,0 @@
-scriptencoding utf-8
-
-
-
-if !exists('g:autosave_interval_ms')
- let g:autosave_interval_ms = 10 * 1000
-endif
-
-if !exists('g:autosave_silent')
- let g:autosave_silent = v:true
-endif
-
-
-
-function! autosave#handler(timer_id) abort
- if !&modified
- return
- endif
- if &readonly
- return
- endif
- if &buftype !=# ''
- return
- endif
- if expand('%') ==# ''
- return
- endif
-
- if !g:autosave_silent
- echohl Comment
- echo 'Auto-saving...'
- echohl None
- endif
-
- silent! write
-
- if !g:autosave_silent
- echohl Comment
- echo 'Saved.'
- echohl None
- endif
-endfunction
-
-
-function! autosave#enable() abort
- if exists('b:autosave_timer')
- return
- endif
-
- let b:autosave_timer = timer_start(
- \ g:autosave_interval_ms,
- \ 'autosave#handler',
- \ { 'repeat': -1 })
-endfunction
-
-
-function! autosave#disable() abort
- if !exists('b:autosave_timer')
- return
- endif
-
- call timer_stop('b:autosave_timer')
- unlet b:autosave_timer
-endfunction
diff --git a/.config/nvim/autoload/dummy.vim b/.config/nvim/autoload/dummy.vim
deleted file mode 100644
index 3019bdf..0000000
--- a/.config/nvim/autoload/dummy.vim
+++ /dev/null
@@ -1,39 +0,0 @@
-scriptencoding utf-8
-
-
-
-let s:text = {}
-
-let s:text.lipsum = [
- \ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor',
- \ 'incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis',
- \ 'nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.',
- \ 'Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore',
- \ 'eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt',
- \ 'in culpa qui officia deserunt mollit anim id est laborum.',
- \ ]
-
-let s:text.lipsum1 = join(s:text.lipsum)
-
-let s:text.quickbrownfox = 'The quick brown fox jumps over the lazy dog.'
-
-let s:text.ABC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
-
-let s:text.abc = 'abcdefghijklmnopqrstuvwxyz'
-
-let s:text.hiragana = 'あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわをん'
-
-let s:text.katakana = 'アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン'
-
-let s:text.ihatovo = 'あのイーハトーヴォのすきとおった風、夏でも底に冷たさをもつ青いそら、うつくしい森で飾られたモリーオ市、郊外のぎらぎらひかる草の波。'
-
-
-
-function! dummy#insert(type)
- call append(line('.'), get(s:text, a:type, ''))
-endfunction
-
-
-function! dummy#complete(arglead, cmdline, cursorpos) abort
- return sort(filter(keys(s:text), {idx, val -> val[0:len(a:arglead)-1] =~? a:arglead}))
-endfunction
diff --git a/.config/nvim/init.lua b/.config/nvim/init.lua
index 8559c5f..6a35d79 100644
--- a/.config/nvim/init.lua
+++ b/.config/nvim/init.lua
@@ -22,6 +22,11 @@ local vimrc = require('vimrc')
_G.vimrc = vimrc
+local function iabbrev(from, to)
+ vim.cmd(('inoreabbrev %s %s'):format(from, to))
+end
+
+
-- Global constants {{{2
local my_env = {}
@@ -1129,9 +1134,9 @@ vimrc.map('n', 'Z', '<Cmd>wqall<CR>', { nowait = true })
-- Abbreviations {{{1
-vim.cmd('inoreabbrev retrun return')
-vim.cmd('inoreabbrev reutrn return')
-vim.cmd('inoreabbrev tihs this')
+iabbrev('retrun', 'return')
+iabbrev('reutrn', 'return')
+iabbrev('tihs', 'this')
diff --git a/.config/nvim/lua/autosave.lua b/.config/nvim/lua/autosave.lua
new file mode 100644
index 0000000..ae481ed
--- /dev/null
+++ b/.config/nvim/lua/autosave.lua
@@ -0,0 +1,80 @@
+local M = {}
+
+
+local INTERVAL_MS = 10 * 1000
+local SILENT = true
+
+
+-- Because a timer cannot be converted to Vim value,
+-- store indice of timers instead of timer instances.
+local timers = {}
+
+
+local function handler()
+ if not vim.bo.modified then
+ return
+ end
+ if vim.bo.readonly then
+ return
+ end
+ if vim.bo.buftype ~= '' then
+ return
+ end
+ if vim.fn.bufname() == '' then
+ return
+ end
+
+ if not SILENT then
+ vim.cmd([[echohl Comment]])
+ vim.cmd([[echo 'Auto-saving...']])
+ vim.cmd([[echohl None]])
+ end
+
+ vim.cmd([[silent! write]])
+
+ if not SILENT then
+ vim.cmd([[echohl Comment]])
+ vim.cmd([[echo 'Saved.']])
+ vim.cmd([[echohl None]])
+ end
+end
+
+
+function M.enable()
+ if vim.b.autosave_timer_id then
+ return
+ end
+
+ local timer = vim.loop.new_timer()
+ timer:start(INTERVAL_MS, INTERVAL_MS, vim.schedule_wrap(handler))
+ local tid = #timers + 1
+ timers[tid] = timer
+ vim.b.autosave_timer_id = tid
+end
+
+
+function M.disable()
+ if not vim.b.autosave_timer_id then
+ return
+ end
+
+ local tid = vim.b.autosave_timer_id
+ vim.b.autosave_timer_id = nil
+ if not timers[tid] then
+ return
+ end
+ timers[tid]:close()
+ timers[tid] = nil
+end
+
+
+function M.toggle()
+ if vim.b.autosave_timer_id then
+ M.disable()
+ else
+ M.enable()
+ end
+end
+
+
+return M
diff --git a/.config/nvim/lua/vimrc.lua b/.config/nvim/lua/vimrc.lua
index 0a2b9d9..15925e1 100644
--- a/.config/nvim/lua/vimrc.lua
+++ b/.config/nvim/lua/vimrc.lua
@@ -1,11 +1,11 @@
-local vimrc = {}
+local M = {}
local autocmd_callbacks = {}
-vimrc.autocmd_callbacks = autocmd_callbacks
+M.autocmd_callbacks = autocmd_callbacks
-function vimrc.autocmd(event, filter, callback)
+function M.autocmd(event, filter, callback)
local callback_id = #autocmd_callbacks + 1
autocmd_callbacks[callback_id] = callback
vim.cmd(('autocmd Vimrc %s %s lua vimrc.autocmd_callbacks[%d]()'):format(
@@ -16,7 +16,7 @@ end
-function vimrc.after_ftplugin(ft, callback)
+function M.after_ftplugin(ft, callback)
local var_name = 'did_ftplugin_' .. ft .. '_after'
if vim.b[var_name] ~= nil then
return
@@ -29,42 +29,42 @@ end
-local SPACE = true
-local TAB = false
-
-local indentation_settings = {
- c = { style = SPACE, width = 4 },
- cmake = { style = SPACE, width = 2 },
- cpp = { style = SPACE, width = 4 },
- css = { style = SPACE, width = 2 },
- go = { style = TAB, width = 4 },
- haskell = { style = SPACE, width = 4 },
- html = { style = SPACE, width = 2 },
- javascript = { style = SPACE, width = 2 },
- json = { style = SPACE, width = 2 },
- lisp = { style = SPACE, width = 2 },
- lua = { style = SPACE, width = 3 },
- markdown = { style = SPACE, width = 4 },
- php = { style = SPACE, width = 2 },
- python = { style = SPACE, width = 4 },
- ruby = { style = SPACE, width = 2 },
- toml = { style = SPACE, width = 2 },
- typescript = { style = SPACE, width = 2 },
- vim = { style = SPACE, width = 4 },
- yaml = { style = SPACE, width = 2 },
-}
-
-function vimrc.register_filetype_autocmds_for_indentation()
+function M.register_filetype_autocmds_for_indentation()
+ local SPACE = true
+ local TAB = false
+
+ local indentation_settings = {
+ c = { style = SPACE, width = 4 },
+ cmake = { style = SPACE, width = 2 },
+ cpp = { style = SPACE, width = 4 },
+ css = { style = SPACE, width = 2 },
+ go = { style = TAB, width = 4 },
+ haskell = { style = SPACE, width = 4 },
+ html = { style = SPACE, width = 2 },
+ javascript = { style = SPACE, width = 2 },
+ json = { style = SPACE, width = 2 },
+ lisp = { style = SPACE, width = 2 },
+ lua = { style = SPACE, width = 3 },
+ markdown = { style = SPACE, width = 4 },
+ php = { style = SPACE, width = 2 },
+ python = { style = SPACE, width = 4 },
+ ruby = { style = SPACE, width = 2 },
+ toml = { style = SPACE, width = 2 },
+ typescript = { style = SPACE, width = 2 },
+ vim = { style = SPACE, width = 4 },
+ yaml = { style = SPACE, width = 2 },
+ }
+
for ft, setting in pairs(indentation_settings) do
- vim.cmd(([[autocmd Vimrc FileType %s call v:lua.vimrc._set_indentation(%s, %d)]]):format(
+ vim.cmd(([[autocmd Vimrc FileType %s lua vimrc._set_indentation(%s, %d)]]):format(
ft,
- setting.style and 'v:true' or 'v:false',
+ setting.style,
setting.width
))
end
end
-function vimrc._set_indentation(style, width)
+function M._set_indentation(style, width)
vim.bo.expandtab = style
vim.bo.tabstop = width
vim.bo.shiftwidth = width
@@ -77,4 +77,4 @@ end
-return vimrc
+return M
diff --git a/.config/nvim/plugin/autosave.vim b/.config/nvim/plugin/autosave.vim
index 6339dcb..ab8a227 100644
--- a/.config/nvim/plugin/autosave.vim
+++ b/.config/nvim/plugin/autosave.vim
@@ -5,25 +5,17 @@ if exists('g:loaded_autosave')
endif
-
command! -bar
\ AutosaveEnable
- \ call autosave#enable()
-
+ \ lua require('autosave').enable()
command! -bar
\ AutosaveDisable
- \ call autosave#disable()
-
+ \ lua require('autosave').disable()
command! -bar
\ AutosaveToggle
- \ if exists('b:autosave_timer') |
- \ call autosave#disable() |
- \ else |
- \ call autosave#enable() |
- \ endif
-
+ \ lua require('autosave').toggle()
let g:loaded_autosave = 1
diff --git a/.config/nvim/plugin/dummy.vim b/.config/nvim/plugin/dummy.vim
index ce7b901..dcf8ae4 100644
--- a/.config/nvim/plugin/dummy.vim
+++ b/.config/nvim/plugin/dummy.vim
@@ -6,9 +6,47 @@ endif
-command! -bar -complete=customlist,dummy#complete -nargs=1
+let s:text = {}
+
+let s:text.lipsum = [
+ \ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor',
+ \ 'incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis',
+ \ 'nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.',
+ \ 'Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore',
+ \ 'eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt',
+ \ 'in culpa qui officia deserunt mollit anim id est laborum.',
+ \ ]
+
+let s:text.lipsum1 = join(s:text.lipsum)
+
+let s:text.quickbrownfox = 'The quick brown fox jumps over the lazy dog.'
+
+let s:text.ABC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
+
+let s:text.abc = 'abcdefghijklmnopqrstuvwxyz'
+
+let s:text.hiragana = 'あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわをん'
+
+let s:text.katakana = 'アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン'
+
+let s:text.ihatovo = 'あのイーハトーヴォのすきとおった風、夏でも底に冷たさをもつ青いそら、うつくしい森で飾られたモリーオ市、郊外のぎらぎらひかる草の波。'
+
+
+
+function! Dummy_insert(type)
+ call append(line('.'), get(s:text, a:type, ''))
+endfunction
+
+
+function! Dummy_complete(arglead, cmdline, cursorpos) abort
+ return sort(filter(keys(s:text), {idx, val -> val[0:len(a:arglead)-1] =~? a:arglead}))
+endfunction
+
+
+
+command! -bar -complete=customlist,Dummy_complete -nargs=1
\ Dummy
- \ call dummy#insert(<f-args>)
+ \ call Dummy_insert(<f-args>)