vis-config
lua scripts to configure vis editor
git clone https://9o.is/git/vis-config.git
commit d4b641f882f968fe3ec2838ff5f9dcb013d28279 parent eb1663830fe8a6513519eac5d5ba9a33645f18c6 Author: Jul <jul@9o.is> Date: Fri, 20 Feb 2026 18:16:09 +0800 set up lspc/ctags for c files Diffstat:
| M | lib/ctags.lua | | | 70 | ++++++++++++++++++++++++++++++++++++++++------------------------------ |
| A | lib/utils.lua | | | 72 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | visrc.lua | | | 83 | +++++++++++++++++++++---------------------------------------------------------- |
3 files changed, 134 insertions(+), 91 deletions(-)
diff --git a/lib/ctags.lua b/lib/ctags.lua @@ -3,11 +3,47 @@ local M = { flags = '-R', } +local function generate_tags(win) + if not win.ctags then + vis:info('ctags not enabled') + return + end + + local fd = io.popen(string.format('ctags %s -n -f %s', M.flags, M.file)) + fd:close() +end + local tagstack = {} vis:option_register('ctags', 'bool', function(value, toggle) - if not vis.win then return false end - vis.win.ctags = toggle and not vis.win.ctags or value + local win = vis.win + if not win then return false end + + win.ctags = toggle and not win.ctags or value + + if win.ctags then + win:map(vis.modes.NORMAL, '<C-t>', function() + vis:command('tag-back') + end) + + win:map(vis.modes.NORMAL, '<C-]>', function() + local tag = win.file:content(win.file:text_object_word(win.selection.pos)) + vis:command('tag ' .. tag) + end) + + win:map(vis.modes.VISUAL, '<C-]>', function() + local tag = win.file:content(win.selection.range) + vis:command('tag ' .. tag) + end) + + local f = io.open(M.file, 'r') + if not f then + generate_tags(win) + else + f:close() + end + end + return true end, 'Enables ctags') @@ -24,13 +60,7 @@ vis:option_register('ctags-read-flags', 'string', function(value) end, 'Set ctags readtags flags') vis:command_register('tag-generate', function(_, _, win) - if not win.ctags then - vis:info('ctags not enabled') - return - end - - local fd = io.popen(string.format('ctags %s -n -f %s', M.flags, M.file)) - fd:close() + generate_tags(win) end, 'Generate ctags file') vis:command_register('tag', function(argv, _, win) @@ -95,28 +125,8 @@ vis:command_register('tag-back', function() vis.win.selection:to(item.line, item.col) end, 'Go back in tag stack') -vis.events.subscribe(vis.events.WIN_OPEN, function(win) - if not vis.win.ctags then - return - end - - win:map(vis.modes.NORMAL, '<C-t>', function() - vis:command('tag-back') - end) - - win:map(vis.modes.NORMAL, '<C-]>', function() - local tag = win.file:content(win.file:text_object_word(win.selection.pos)) - vis:command('tag ' .. tag) - end) - - win:map(vis.modes.VISUAL, '<C-]>', function() - local tag = win.file:content(win.selection.range) - vis:command('tag ' .. tag) - end) -end) - vis.events.subscribe(vis.events.FILE_SAVE_POST, function() if vis.win.ctags and vis.win.ctags_autosave then - vis:command('tag-generate') + generate_tags(vis.win) end end) diff --git a/lib/utils.lua b/lib/utils.lua @@ -0,0 +1,72 @@ +function exists(prog) + return os.execute('type ' .. prog .. ' >/dev/null 2>&1') +end + +function start_lspc(lspc, name) + if not lspc.autostart and exists(name) and not lspc.running[name] then + vis:command('lspc-start-server') + return true + end + + if lspc.running[name] then + return true + end +end + +function map(modes, ...) + if type(modes) ~= 'table' then + vis:map(modes, ...) + return + end + for _, mode in pairs(modes) do + vis:map(mode, ...) + end +end + +function mapwin(win, modes, ...) + if type(modes) ~= 'table' then + win:map(modes, ...) + return + end + for _, mode in pairs(modes) do + win:map(mode, ...) + end +end + +function ins(x) + return function() + local pos = vis.win.selection.pos + vis:feedkeys(x) + vis.win.selection.pos = pos + return 1 + end +end + +function cmd(x) + return function() + local fn = vis.win.file.name + if fn then + x = string.format(x, fn) + end + vis:command(x) + end +end + +function content(win) + local sel = win.selection + local range = sel.anchored and sel.range or win.file:text_object_word(sel.pos) + return win.file:content(range) +end + +vis.events.subscribe(vis.events.WIN_OPEN, function(win) + if settings == nil then return end + local window_settings = settings[win.syntax] + + if type(window_settings) == "table" then + for _, setting in pairs(window_settings) do + vis:command(setting) + end + elseif type(window_settings) == "function" then + window_settings(win) + end +end) diff --git a/visrc.lua b/visrc.lua @@ -8,6 +8,13 @@ require('lib/ctags') require('lib/session') require('lib/router') require('lib/search') +require('lib/utils') + +local lspc = require('plugins/vis-lspc') +lspc.autostart = false +lspc.menu_cmd = 'fzy' +lspc.confirm_cmd = 'fzy' +lspc.universal_root_globs = {'*.git'} local m = vis.modes local ll = ' ' -- left leader @@ -67,55 +74,27 @@ vis.events.subscribe(vis.events.INIT, function() -- center map(m.NORMAL, 'n', '<vis-motion-search-repeat-forward>zz') map(m.NORMAL, 'N', '<vis-motion-search-repeat-backward>zz') - - map({m.NORMAL, m.VISUAL}, 'K', function() - local sec = vis.count or 1 - local name = content(vis.win) - vis.count = nil - vis:command(string.format('terminal man %d %s', sec, name)) - end, 'Man') end) vis.events.subscribe(vis.events.WIN_OPEN, function() vis:command('set tabwidth 4') - -- vis:command('set ctags on') - -- vis:command('set ctags-autosave on') -end, 1) - -function map(modes, ...) - if type(modes) ~= 'table' then - vis:map(modes, ...) - return - end - for _, mode in pairs(modes) do - vis:map(mode, ...) - end -end - -function ins(x) - return function() - local pos = vis.win.selection.pos - vis:feedkeys(x) - vis.win.selection.pos = pos - return 1 - end -end +end) -function cmd(x) - return function() - local fn = vis.win.file.name - if fn then - x = string.format(x, fn) +settings = { + c = function(win) + if not start_lspc(lspc, 'clangd') then + vis:command('set ctags on') + vis:command('set ctags-autosave on') + + mapwin(win, {m.NORMAL, m.VISUAL}, 'K', function() + local sec = vis.count or 1 + local name = content(vis.win) + vis.count = nil + vis:command(string.format('terminal man %d %s', sec, name)) + end, 'Man') end - vis:command(x) - end -end - -function content(win) - local sel = win.selection - local range = sel.anchored and sel.range or win.file:text_object_word(sel.pos) - return win.file:content(range) -end + end, +} -- useful for startup (vis +help-fullscreen) vis:command_register('help-fullscreen', function(argv) @@ -128,21 +107,3 @@ vis:command_register('normal', function(argv) vis.mode = vis.modes.NORMAL vis:feedkeys(table.concat(argv)) end) - --- move this to an autocomplete module --- vis.events.subscribe(vis.events.INIT, function() - -- map(vis.modes.INSERT, '<C-x><C-o>', function() - -- local win = vis.win - -- local sel = win.selection - -- local pos = sel.pos > 1 and sel.pos - 1 or sel.pos - -- local range = win.file:text_object_word(pos) - -- local pattern = win.file:content(range) - -- pattern = pattern:gsub('%s', '') == '' and '.' or string.format("'%s'", pattern) - - -- local success, out = popen(string.format("ag --vimgrep -g %s | fzy", pattern)) - -- if success then - -- vis:insert(out:sub(range.finish - range.start + 1)) - -- end - -- vis:redraw() - -- end, 'Complete file name') --- end)