From 415ceee46b6ef0870b32de98991286f6c1fa5a5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6ran=20Karl?= <3951388+JoeKar@users.noreply.github.com> Date: Fri, 13 Oct 2023 01:56:16 +0200 Subject: [PATCH 1/5] plugin: Add new `onBufferOptionChanged` callback This can become handy in the moment a plugin needs to react on e.g. changed file type. --- internal/buffer/settings.go | 11 ++++++++++- runtime/help/plugins.md | 4 ++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/internal/buffer/settings.go b/internal/buffer/settings.go index 838df4a568..3bb3d0c073 100644 --- a/internal/buffer/settings.go +++ b/internal/buffer/settings.go @@ -5,7 +5,9 @@ import ( "reflect" "github.com/zyedidia/micro/v2/internal/config" + ulua "github.com/zyedidia/micro/v2/internal/lua" "github.com/zyedidia/micro/v2/internal/screen" + luar "layeh.com/gopher-luar" ) func (b *Buffer) ReloadSettings(reloadFiletype bool) { @@ -46,7 +48,8 @@ func (b *Buffer) ReloadSettings(reloadFiletype bool) { } func (b *Buffer) DoSetOptionNative(option string, nativeValue interface{}) { - if reflect.DeepEqual(b.Settings[option], nativeValue) { + oldValue := b.Settings[option] + if reflect.DeepEqual(oldValue, nativeValue) { return } @@ -117,6 +120,12 @@ func (b *Buffer) DoSetOptionNative(option string, nativeValue interface{}) { if b.OptionCallback != nil { b.OptionCallback(option, nativeValue) } + + if err := config.RunPluginFn("onBufferOptionChanged", + luar.New(ulua.L, b), luar.New(ulua.L, option), + luar.New(ulua.L, oldValue), luar.New(ulua.L, nativeValue)); err != nil { + screen.TermMessage(err) + } } func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error { diff --git a/runtime/help/plugins.md b/runtime/help/plugins.md index fbab646943..5d4830fbc9 100644 --- a/runtime/help/plugins.md +++ b/runtime/help/plugins.md @@ -62,6 +62,10 @@ that micro defines: * `onBufferOpen(buf)`: runs when a buffer is opened. The input contains the buffer object. +* `onBufferOptionChanged(buf, option, old, new)`: runs when an option of the + buffer has changed. The input contains the buffer object, the option name, + the old and the new value. + * `onBufPaneOpen(bufpane)`: runs when a bufpane is opened. The input contains the bufpane object. From 771b84141fedc0ca221992e287a86eaa929e8bed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6ran=20Karl?= <3951388+JoeKar@users.noreply.github.com> Date: Sun, 29 Dec 2024 13:22:35 +0100 Subject: [PATCH 2/5] plugin: linter: Use new `onBufferOptionChanged` callback --- runtime/plugins/linter/linter.lua | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/runtime/plugins/linter/linter.lua b/runtime/plugins/linter/linter.lua index 0ce4b2b01e..d5bd9541e0 100644 --- a/runtime/plugins/linter/linter.lua +++ b/runtime/plugins/linter/linter.lua @@ -141,6 +141,32 @@ function onSave(bp) return true end +function onBufferOptionChanged(buf, option, old, new) + if option == "filetype" then + if old ~= new then + for k, v in pairs(linters) do + local ftmatch = old == v.filetype + if v.domatch then + ftmatch = string.match(old, v.filetype) + end + + local hasOS = contains(v.os, runtime.GOOS) + if not hasOS and v.whitelist then + ftmatch = false + end + if hasOS and not v.whitelist then + ftmatch = false + end + + if ftmatch then + buf:ClearMessages(k) + end + end + end + end + return true +end + function lint(buf, linter, cmd, args, errorformat, loff, coff, callback) buf:ClearMessages(linter) From de84da068dd039fc70dc457103e1c8490f8fe787 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6ran=20Karl?= <3951388+JoeKar@users.noreply.github.com> Date: Fri, 13 Oct 2023 02:15:48 +0200 Subject: [PATCH 3/5] plugin: linter: Move file type check into a dedicated function --- runtime/plugins/linter/linter.lua | 46 ++++++++++++------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/runtime/plugins/linter/linter.lua b/runtime/plugins/linter/linter.lua index d5bd9541e0..fe09f068a9 100644 --- a/runtime/plugins/linter/linter.lua +++ b/runtime/plugins/linter/linter.lua @@ -107,26 +107,29 @@ function contains(list, element) return false end +function checkFtMatch(ft, v) + local ftmatch = ft == v.filetype + if v.domatch then + ftmatch = string.match(ft, v.filetype) + end + + local hasOS = contains(v.os, runtime.GOOS) + if not hasOS and v.whitelist then + ftmatch = false + end + if hasOS and not v.whitelist then + ftmatch = false + end + return ftmatch +end + function runLinter(buf) local ft = buf:FileType() local file = buf.Path local dir = "." .. util.RuneStr(os.PathSeparator) .. filepath.Dir(file) for k, v in pairs(linters) do - local ftmatch = ft == v.filetype - if v.domatch then - ftmatch = string.match(ft, v.filetype) - end - - local hasOS = contains(v.os, runtime.GOOS) - if not hasOS and v.whitelist then - ftmatch = false - end - if hasOS and not v.whitelist then - ftmatch = false - end - - if ftmatch then + if checkFtMatch(ft, v) then local args = {} for k, arg in pairs(v.args) do args[k] = arg:gsub("%%f", file):gsub("%%d", dir) @@ -145,20 +148,7 @@ function onBufferOptionChanged(buf, option, old, new) if option == "filetype" then if old ~= new then for k, v in pairs(linters) do - local ftmatch = old == v.filetype - if v.domatch then - ftmatch = string.match(old, v.filetype) - end - - local hasOS = contains(v.os, runtime.GOOS) - if not hasOS and v.whitelist then - ftmatch = false - end - if hasOS and not v.whitelist then - ftmatch = false - end - - if ftmatch then + if checkFtMatch(old, v) then buf:ClearMessages(k) end end From 6b21fc5f929e99d1d8b2282568ca124a4d3e0a83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6ran=20Karl?= <3951388+JoeKar@users.noreply.github.com> Date: Fri, 13 Oct 2023 01:56:52 +0200 Subject: [PATCH 4/5] plugin: linter: Invoke g++ for c++ instead of gcc --- runtime/plugins/linter/linter.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/plugins/linter/linter.lua b/runtime/plugins/linter/linter.lua index fe09f068a9..8ea1c80e98 100644 --- a/runtime/plugins/linter/linter.lua +++ b/runtime/plugins/linter/linter.lua @@ -66,7 +66,7 @@ function preinit() end makeLinter("gcc", "c", "gcc", {"-fsyntax-only", "-Wall", "-Wextra", "%f"}, "%f:%l:%c:.+: %m") - makeLinter("g++", "c++", "gcc", {"-fsyntax-only","-std=c++14", "-Wall", "-Wextra", "%f"}, "%f:%l:%c:.+: %m") + makeLinter("g++", "c++", "g++", {"-fsyntax-only","-std=c++14", "-Wall", "-Wextra", "%f"}, "%f:%l:%c:.+: %m") makeLinter("dmd", "d", "dmd", {"-color=off", "-o-", "-w", "-wi", "-c", "%f"}, "%f%(%l%):.+: %m") makeLinter("eslint", "javascript", "eslint", {"-f","compact","%f"}, "%f: line %l, col %c, %m") makeLinter("gobuild", "go", "go", {"build", "-o", devnull, "%d"}, "%f:%l:%c:? %m") From d1f54ea2a40716fa562b6b633cde97c8c2d94710 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6ran=20Karl?= <3951388+JoeKar@users.noreply.github.com> Date: Wed, 1 Nov 2023 21:17:26 +0100 Subject: [PATCH 5/5] plugin: linter: Remove the forced C++14 standard to keep GCCs default --- runtime/plugins/linter/linter.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/plugins/linter/linter.lua b/runtime/plugins/linter/linter.lua index 8ea1c80e98..068a8abfdf 100644 --- a/runtime/plugins/linter/linter.lua +++ b/runtime/plugins/linter/linter.lua @@ -66,7 +66,7 @@ function preinit() end makeLinter("gcc", "c", "gcc", {"-fsyntax-only", "-Wall", "-Wextra", "%f"}, "%f:%l:%c:.+: %m") - makeLinter("g++", "c++", "g++", {"-fsyntax-only","-std=c++14", "-Wall", "-Wextra", "%f"}, "%f:%l:%c:.+: %m") + makeLinter("g++", "c++", "g++", {"-fsyntax-only","-Wall", "-Wextra", "%f"}, "%f:%l:%c:.+: %m") makeLinter("dmd", "d", "dmd", {"-color=off", "-o-", "-w", "-wi", "-c", "%f"}, "%f%(%l%):.+: %m") makeLinter("eslint", "javascript", "eslint", {"-f","compact","%f"}, "%f: line %l, col %c, %m") makeLinter("gobuild", "go", "go", {"build", "-o", devnull, "%d"}, "%f:%l:%c:? %m")