Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a possibility of accessing arguments of a plugin command from the lua plugin? #2906

Open
Twix53791 opened this issue Sep 6, 2023 · 3 comments

Comments

@Twix53791
Copy link

I am writing a couple of small plugins for micro and for one of them I would like to get user arguments: in the prompt the user would write . I saw than the function creating the plugin details the action as a function containing bp and args. I know how to use bp from the lua plugin to call micro functions, but how to use args?

// MakeCommand is a function to easily create new commands
// This can be called by plugins in Lua so that plugins can define their own commands
func MakeCommand(name string, action func(bp *BufPane, args []string), completer buffer.Completer) {
	if action != nil {
		commands[name] = Command{action, completer}
	}
}

In lua, when I pass an argument to my plugin function, I saw it if I print args[1], but its type is a userdata, and if I understood well (I don't know a lot of lua...), userdata are pure blocks of raw memory, and we can retrieves data from them, they just point to the C code. So is there a way to retrieve the argument I passed as a string? From micro?

Thanks for any clues!

@zyedidia
Copy link
Owner

zyedidia commented Sep 6, 2023

The type of args[1] should be a string. The type of args itself may be a userdata, but it has the [] operator overloaded to access the array elements from the underlying Go slice. Are you seeing a userdata type for args[1] even when passing an argument to the command?

For example, I put this in my `init.lua to test:

local config = import("micro/config")
local micro = import("micro")

function init()
    config.MakeCommand("foo", function(bp, args)
        micro.Log("foo", type(args[1]))
    end, config.NoComplete)
end

When I run > foo hello with micro -debug, I see in log.txt that the type of args[1] is string (and the value is hello).

@Twix53791
Copy link
Author

Thanks for the reply @zyedidia ! Actually, it works like a string that's right. But I run into another problem : when I pass no argument to the function, args[1], actually {args[1]} returns an error "index out of range". So I tried to check if args was empty or not before, but I did'nt find a way. "args" is not an empty string, is not nil, if args then is always true...

To give more context, I trying to add a functionality to the excellent plugin fzfinder, the possibility of execute fzf in a custom directory passed as an argument. So the code is the following:

function fzfinder(bp, args)
  -- if args then
  bp:CdCmd({args[1]})
...

PS: by the way, I think fzfinder, the fzf plugin of @MuratovAS should became the default fzf plugin in the micro plugin-channel repo. The actual fzf plugin listed, https://github.com/samdmarshall/micro-fzf-plugin, is archived since 2019, buggy and lacks many functionalities we added with @MuratovAS, like easy config via options, choose to open in new tab/split, or look in the current folder of the file open and not the root directory of the project.

@zyedidia
Copy link
Owner

You can try using the length operator: if #args == 0 then ....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants