Olical / conjure

Interactive evaluation for Neovim (Clojure, Fennel, Janet, Racket, Hy, MIT Scheme, Guile, Python and more!)
https://conjure.oli.me.uk
The Unlicense
1.8k stars 111 forks source link

Disable all default mappings? #533

Closed bloznelis closed 11 months ago

bloznelis commented 1 year ago

Similar to https://github.com/Olical/conjure/issues/448

The ticket mentions that it default mappings can be disabled one-by-one, but is it possible to disable all default mappings? From the documentation it seems that vim.g["conjure#mapping#enable_ft_mappings"] = false should do the trick, but it does not.

This is the config that I'm trying to use

return {
    "Olical/conjure",
    ft = { "clojure" },
    config = function(_, opts)
        require("conjure.main").main()
       -- require("conjure.mapping")["init"]()
       -- EDIT: I actually use `on-filetype` here
          require("conjure.mapping")["on-filetype"]()
    end,
    init = function()
        vim.g["conjure#mapping#enable_ft_mappings"] = false
        vim.g["conjure#debug"] = true

        vim.g["conjure#mapping#eval_current_form"] = "ee"
        vim.g["conjure#mapping#eval_root_form"] = "er"
        vim.g["conjure#mapping#eval_comment_root_form"] = "e;"
        vim.g["conjure#mapping#eval_buf"] = "eb"
        vim.g["conjure#mapping#eval_visual"] = "E"
        vim.g["conjure#mapping#doc_word"] = ""
    end,
}

I would like to have only a subset of commands mapped to my own keybindings. Thanks in advance!

Olical commented 1 year ago

I'll take a closer look tomorrow but I suspect this is just because the option is being set too late. You need to set the option BEFORE Conjure loads anything and I have a feeling it's already too late there. I'm not sure but hopefully that breadcrumb leads you to the solution.

As soon as you require conjure.main it loads conjure.mapping and calls mapping.init which is what looks at that flag and decides to set up the filetype autocmds or not.

bloznelis commented 1 year ago

I updated the configuration to what I actually use, sorry. The change is at the config function.

I have a feeling it's already too late there.

Hmm, that would be a bit weird then. Lazy's init is supposed to run on startup, and for example other global variables set in the same init stage (e.g., vim.g["conjure#mapping#eval_comment_root_form"] = "e;") are respected.

Olical commented 1 year ago

init functions are always executed during startup

Oh yeah! Interesting!

And I think there's you're problem, the enable_ft_mappings flag is what decides to either call on-filetype or skip it. The chain is as follows:

The conjure#mapping#... configuration options are ONLY consumed inside the on-filetype autocmd callback. I think what you might actually want for now is something like this:

vim.g["conjure#mapping#prefix"] = "<localleader>"
vim.g["conjure#mapping#log_split"] = "ls" -- example using the prefix, so <localleader>ls
vim.g["conjure#mapping#log_vsplit"] = ["Lv"] -- example not using the prefix, just press Lv
vim.g["conjure#mapping#log_tab"] = false
vim.g["conjure#mapping#log_buf"] = false
vim.g["conjure#mapping#log_toggle"] = false
vim.g["conjure#mapping#log_close_visible"] = false
vim.g["conjure#mapping#log_reset_soft"] = false
vim.g["conjure#mapping#log_reset_hard"] = false
vim.g["conjure#mapping#log_jump_to_latest"] = false
vim.g["conjure#mapping#eval_current_form"] = false
vim.g["conjure#mapping#eval_comment_current_form"] = false
vim.g["conjure#mapping#eval_root_form"] = false
vim.g["conjure#mapping#eval_comment_root_form"] = false
vim.g["conjure#mapping#eval_word"] = false
vim.g["conjure#mapping#eval_comment_word"] = false
vim.g["conjure#mapping#eval_replace_form"] = false
vim.g["conjure#mapping#eval_marked_form"] = false
vim.g["conjure#mapping#eval_file"] = false
vim.g["conjure#mapping#eval_buf"] = false
vim.g["conjure#mapping#eval_visual"] = false
vim.g["conjure#mapping#eval_motion"] = false
vim.g["conjure#mapping#eval_previous"] = false
vim.g["conjure#mapping#def_word"] = false
vim.g["conjure#mapping#doc_word"] = false

This doesn't account for some mappings that some clients will introduce though. Another thing you can do is set the prefix key to something obscure but possible to use in a pinch. Then replace each of the mappings you care about with a binding to ["<localleader>ee"] or whatever. The table syntax is a kinda DSL that says "don't use the automatic prefixing on this mapping".

I think this route will be better because some of the mappings are quite hard to set up which is why Conjure does it for you, like mappings off the back of motions and wiring into vim-repeat. If you completely bypass Conjure's mapping system you'll have to do a lot more wiring yourself.

But I think the crux of this issue is the misconception that setting enable_ft_mappings = false and then setting individual mapping config will work. That mapping config is only read when you have the filetype callback firing.

bloznelis commented 1 year ago

EDIT: Sorry, missed your previous comment, so this is purely based on the knowledge before it.

Ok, I think I kind of understand of what is going on.

If we have enable_ft_mappings set to false, while the config loads it skips the mapping.on-filetype function. But this function is also responsible for mapping custom keybindings as well.

To have it work without manually disabling all the default bindings one-by-one, we would need a way to differentiate between default and custom mapping, it seems.

Olical commented 1 year ago

Maybe a flag that sets all of the mappings to false somehow would be good, then the user just sets the config options they care about but knows they all default to off πŸ€”

I can't even remember why I added that enable_ft_mappings flag now, it doesn't seem very useful. Well. It's very useful at misleading people 😬

Olical commented 1 year ago

And glad you came to the same conclusions! I should've been slightly faster πŸ˜›

bloznelis commented 1 year ago

Maybe a flag that sets all of the mappings to false somehow would be good, then the user just sets the config options they care about but knows they all default to off πŸ€”

IMO this would be great.

Olical commented 1 year ago

Let me make another coffee and I'll get a first draft up on the develop branch.

On Fri, 20 Oct 2023, 12:15 Lukas Bloznelis, @.***> wrote:

Maybe a flag that sets all of the mappings to false somehow would be good, then the user just sets the config options they care about but knows they all default to off πŸ€”

IMO this would be great.

β€” Reply to this email directly, view it on GitHub https://github.com/Olical/conjure/issues/533#issuecomment-1772550694, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACM6XKNUMWU3BF27JSUU7TYAJMMTAVCNFSM6AAAAAA6HNGVA6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONZSGU2TANRZGQ . You are receiving this because you commented.Message ID: @.***>

Olical commented 1 year ago

Okay, pushed! Set g:conjure#mapping#enable_defaults = v:false / vim.g["conjure#mapping#enable_defaults = false before Conjure loads and all core mappings will be nil, allowing you to set the ones you care about.

I have not spread this to the clients yet, so they will set their own mappings for some things, I will spread it to them after lunch / after we've verified this is the right solution for you and others.

bloznelis commented 1 year ago

Just tested it out and it works just perfectly! Thank you for being so swift to add this, it will improve my DX massively <3

Olical commented 1 year ago

Pushed integration for this flag for every client in Conjure, hope that works for you too!