| @@ -1,32 +1,11 @@ | |||
| -- ec = require("editorconfig_core") | |||
| local function getProperties(fullpath) | |||
| -- FIXME: Commands can be injected here! | |||
| -- For example, issuing this command will create file b.txt | |||
| -- $ micro "a.txt'; touch 'b.txt" | |||
| local file = io.popen("editorconfig '" .. fullpath .. "'", "r") | |||
| local output = file:read("*all") | |||
| file:close() | |||
| local properties = {} | |||
| -- TODO: Which is better? output:gmatch(), string.gmatch(output, ...) | |||
| for line in output:gmatch('([^\n]+)') do | |||
| -- TODO: Fix regex | |||
| local key, value = line:match('([^=]*)=(.*)') | |||
| key = key:gsub('^%s(.-)%s*$', '%1') | |||
| value = value:gsub('^%s(.-)%s*$', '%1') | |||
| -- TODO: Throw error when key is empty string | |||
| properties[key] = value | |||
| end | |||
| return properties | |||
| end | |||
| local function setIndentation(properties, view) | |||
| local indent_size_str = properties["indent_size"] | |||
| local tab_width_str = properties["tab_width"] | |||
| local indent_style = properties["indent_style"] | |||
| -- TODO: Fix logic to decide indent_size | |||
| local indent_size = tonumber(indent_size_str, 10) | |||
| if indent_size ~= nil then | |||
| messenger:Message("set tabsize to " .. indent_size_str) | |||
| @@ -51,19 +30,20 @@ local function setInsertFinalNewline(properties, view) | |||
| end | |||
| end | |||
| function onViewOpen(view) | |||
| -- Is this portable? (work on windows?) | |||
| local pwd = os.getenv("PWD") | |||
| local filename = view.Buf.Path | |||
| -- prop, names = ec.parse(filepath) | |||
| local fullpath = JoinPaths(pwd, filename) | |||
| local properties = getProperties(fullpath) | |||
| if properties["indent_style"] == nil then | |||
| messenger:Message("edconf: nil") | |||
| else | |||
| messenger:Message("edconf: " .. properties["indent_style"]) | |||
| function onEditorConfigExit(output) | |||
| local properties = {} | |||
| -- TODO: Which is better? output:gmatch(), string.gmatch(output, ...) | |||
| for line in output:gmatch('([^\n]+)') do | |||
| -- TODO: Fix regex | |||
| local key, value = line:match('([^=]*)=(.*)') | |||
| key = key:gsub('^%s(.-)%s*$', '%1') | |||
| value = value:gsub('^%s(.-)%s*$', '%1') | |||
| -- TODO: Throw error when key is empty string | |||
| properties[key] = value | |||
| end | |||
| local view = CurView() | |||
| setIndentation(properties, view) | |||
| -- Currently micro does not support changing coding-systems | |||
| -- (Always use utf-8 with LF?) | |||
| @@ -74,6 +54,23 @@ function onViewOpen(view) | |||
| setInsertFinalNewline(properties, view) | |||
| end | |||
| local function applyProperties(view) | |||
| -- Is this portable? (work on windows?) | |||
| local pwd = os.getenv("PWD") | |||
| local filename = view.Buf.Path | |||
| -- prop, names = ec.parse(filepath) | |||
| local fullpath = JoinPaths(pwd, filename) | |||
| -- FIXME: Commands can be injected here! | |||
| -- For example, issuing this command will create file b.txt | |||
| -- $ micro "a.txt'; touch 'b.txt" | |||
| JobStart("editorconfig " .. fullpath, "", "", "init.onEditorConfigExit") | |||
| end | |||
| function onViewOpen(view) | |||
| applyProperties(view) | |||
| end | |||
| function onSave(view) | |||
| -- messenger:Message("Saved!") | |||
| end | |||