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

File opened in multiple tabs #3507

Open
RichardFevrier opened this issue Oct 17, 2024 · 6 comments
Open

File opened in multiple tabs #3507

RichardFevrier opened this issue Oct 17, 2024 · 6 comments

Comments

@RichardFevrier
Copy link

Hi everyone 👋

Probably a skill issue but I can't find a way to prevent a file to be opened multiple times in different tabs and switch to the tab instead.

Thank you.

@dmaluka
Copy link
Collaborator

dmaluka commented Oct 19, 2024

You can implement a custom command in lua for that. Add e.g. something like this to your init.lua:

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

function switchCmd(bp, args)
    if #args < 1 then
        return
    end
    local abspath = filepath.Abs(args[1])
    local tabs = micro.Tabs()
    if tabs ~= nil then
        for i, t in tabs.List() do
            for j, p in t.Panes() do
                if p.Buf.Path ~= "" and p.Buf.AbsPath == abspath then
                    tabs:SetActive(i-1)
                    t:SetActive(j-1)
                    return
                end
            end
        end
    end
    bp:NewTabCmd(args)
end

function init()
    config.MakeCommand("switch", switchCmd, config.FileComplete)
end

@RichardFevrier
Copy link
Author

Thanks for your smart answer 🙏

Unfortunately doing so you don't change the behavior of NewTabCmd (applies probably to OpenCmd too) meanings that any other call, from plugins for ex. will not beneficiate this functionality.

And that's specifically my case now, I've done a plugin last week that you can find here (pull requeted in the plugin-channel (let me dream 😅)) to integrate Yazi with Micro.

@Andriamanitra
Copy link
Contributor

@RichardFevrier maybe you could use preOpenFile Lua callback?

@RichardFevrier
Copy link
Author

Can't make it work with NewTabCmd from a plugin. With binded action no problem otherwise. Can you @Andriamanitra ?

@Andriamanitra
Copy link
Contributor

I think BufPane.NewTabCmd method does not trigger OpenFile action because it creates a buffer directly (a bug?) but there is also AddTab action that you could hook into. Or you could add a check in your plugin before calling bp:NewTabCmd.

@RichardFevrier
Copy link
Author

RichardFevrier commented Oct 24, 2024

Sorry I didn't mention yesterday that I tried AddTab, OpenTab and so on.. without success.

IMO the last option is a big no, because the idea is to not ask every plugins developer to implement such check, it as to be backed into micro.

But your initial idea of using a callback was cleaver, it just doesn't work in this case (maybe I did implement it wrong,
totally possible).

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

3 participants