Skip to content

Commit

Permalink
plugin: Add new onBufferOptionChanged callback
Browse files Browse the repository at this point in the history
This can become handy in the moment a plugin needs to react on e.g. changed
file type.
  • Loading branch information
JoeKar committed Oct 13, 2023
1 parent 180bf62 commit 7ba2514
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
10 changes: 10 additions & 0 deletions internal/buffer/settings.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package buffer

import (
luar "layeh.com/gopher-luar"
ulua "github.com/zyedidia/micro/v2/internal/lua"
"github.com/zyedidia/micro/v2/internal/config"
"github.com/zyedidia/micro/v2/internal/screen"
)

func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error {
oldValue := b.Settings[option]
b.Settings[option] = nativeValue

if oldValue != nativeValue {
_, err := config.RunPluginFnBool(b.Settings, "onBufferOptionChanged", luar.New(ulua.L, b), luar.New(ulua.L, option), luar.New(ulua.L, string(oldValue.(string))), luar.New(ulua.L, string(nativeValue.(string))))
if err != nil {
screen.TermMessage(err)
}
}

if option == "fastdirty" {
if !nativeValue.(bool) {
if !b.Modified() {
Expand Down
4 changes: 4 additions & 0 deletions runtime/help/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ which micro defines:
* `onBufferOpen(buf)`: runs when a buffer is opened. The input contains
the buffer object.

* `onBufferOptionChanged(buf, option, old, new)`: runs when a option of the
settings of a buffer has changed. The input contains the buffer object,
the option, the old and the new value.

* `onBufPaneOpen(bufpane)`: runs when a bufpane is opened. The input
contains the bufpane object.

Expand Down
27 changes: 27 additions & 0 deletions runtime/plugins/linter/linter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,33 @@ function onSave(bp)
return true
end

function onBufferOptionChanged(buf, option, old, new)
if option == "filetype" then
if old ~= new then
ft = old
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
buf:ClearMessages(k)
end
end
end
end
return true
end

function lint(buf, linter, cmd, args, errorformat, loff, coff, callback)
buf:ClearMessages(linter)

Expand Down

0 comments on commit 7ba2514

Please sign in to comment.