From 4cb2a07856318340405acec7ab2cd52d112f73b9 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Sat, 16 Jan 2021 14:17:16 +0100 Subject: Make settings accessible from ~/.config/vis/visrc.lua. --- README.md | 25 +++++++++++++------------ init.lua | 23 +++++++++++++++-------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 98ba0e0..45c760b 100644 --- a/README.md +++ b/README.md @@ -3,22 +3,23 @@ This plugin provides a declarative interface for setting vis options depending on filetype. -It expects a global variable called `settings` to be defined: +It expects an attribute called `settings` to be defined: ```lua -settings = { +ftset.settings = { markdown = {"set expandtab on", "set tabwidth 4"} } ``` -In this variable, filetypes are mapped to sets of settings that are -to be executed when a window containing the specified filetype is -opened. +In this variable, filetypes are mapped to sets of settings that +toare be executed when a window containing the specified filetype +opis ened. -If you want to do more than setting simple options, you can specify a function instead: +If you want to do more than setting simple options, you can +specify a function instead: ```lua -settings = { +ftset.settings = { bash = function(win) -- do things for shell scripts end @@ -27,8 +28,8 @@ settings = { ### More events -By default, all settings are run on the WIN_OPEN event, but if you -want, you can specify your own event. +By default, all settings are run on the WIN_OPEN event, but if +wayou nt, you can specify your own event. For more information about these events, check out the [documentation for the Lua API][doc]. Below is a description of which events are @@ -44,7 +45,7 @@ supported by this plugin. - FILE_SAVE_PRE ```lua -settings = { +ftset.settings = { go = {"set et off", "set tw 8", FILE_SAVE_PRE = "gofmt"} } ``` @@ -77,9 +78,9 @@ As a suggestion, copy `vis-filetype-settings.lua` into `~/.config/vis/plugins/` and add the following to your `visrc.lua`: ```lua -require("plugins/vis-filetype-settings") +ftset = require("plugins/vis-filetype-settings") -settings = { +ftset.settings = { filetype = settings } ``` diff --git a/init.lua b/init.lua index 9f2665a..0c74216 100644 --- a/init.lua +++ b/init.lua @@ -25,6 +25,10 @@ -- Be sure not to run commands that open another window with the same -- filetype, leading to an infinite loop. +local M = {} + +M.settings = {} + function execute(s, arg, arg2) if type(s) == "table" then for key, setting in pairs(s) do @@ -44,8 +48,8 @@ end -- Register events vis.events.subscribe(vis.events.INPUT, function() - if settings[vis.win.syntax] and settings[vis.win.syntax].INPUT then - execute(settings[vis.win.syntax].INPUT, nil) + if M.settings[vis.win.syntax] and M.settings[vis.win.syntax].INPUT then + execute(M.settings[vis.win.syntax].INPUT, nil) end end) @@ -60,8 +64,8 @@ for _, event in pairs(file_events) do vis.events.subscribe(vis.events[event], function(file, path) for win in vis:windows() do if win.file == file then - if settings[win.syntax] and settings[win.syntax][event] then - execute(settings[win.syntax][event], file, path) + if M.settings[win.syntax] and M.settings[win.syntax][event] then + execute(M.settings[win.syntax][event], file, path) end end end @@ -85,12 +89,15 @@ local win_events = { for _, event in pairs(win_events) do vis.events.subscribe(vis.events[event], function(win) - if settings[win.syntax] then - if settings[win.syntax][event] then - execute(settings[win.syntax][event], win) + if M.settings[win.syntax] == nil then return end + if M.settings[win.syntax] then + if M.settings[win.syntax][event] then + execute(M.settings[win.syntax][event], win) elseif event == "WIN_OPEN" then -- default event - execute(settings[win.syntax], win) + execute(M.settings[win.syntax], win) end end end) end + +return M -- cgit