R-nvim / R.nvim

Neovim plugin to edit R files
GNU General Public License v3.0
128 stars 15 forks source link

Key mapping for focusing on R console in insert mode #124

Closed gueyenono closed 2 months ago

gueyenono commented 2 months ago

Greetings,

I very recently started learning Neovim and Lua and, as an R user, also recently installed the R.nvim plugin. I have been exploring the documentation to learn more and more.

I am trying to set a key binding that will: (i) focus on the R console, (ii) in insert mode. This would be an implementation of "Ctrl + 2" in RStudio. I want the key binding to be: <localleader>ri. Here is my (failed) attempt below:

-- Function for getting the console id
local get_r_console_winid = function()
  local bufno = require('r.term').get_buf_nr()
  if bufno == nil then
    return -1
  else
    return vim.fn.bufwinid(bufno)
  end
end

-- Function to focus on the R console
local move_to_r_console = function()
  local r_console_winid = get_r_console_winid()
  vim.api.nvim_set_keymap('n', '<localleader>ri', ':lua vim.fn.win_execute(r_console_winid, "wincmd w")<CR>', { noremap = true })
end

return {
  'R-nvim/R.nvim',
  lazy = false,

  config = function()
    local opts = {
      hook = {
        -- Execute this when files supported by R.vim are opened
        on_filetype = function()
          move_to_r_console()
        end,
      },
    }

    require('r').setup(opts)
  end,
}

I would appreciate your guidance.

jalvesaq commented 2 months ago

Vim/Neovim has key bindings to move from one window to another. From Neovim help:

    CTRL-W h    move to the window on the left
    CTRL-W j    move to the window below
    CTRL-W k    move to the window above
    CTRL-W l    move to the window on the right

You can change these key bindings to whatever you want in your Neovim configuration.

jalvesaq commented 2 months ago

You may create an auto command to switch to Insert mode if the buffer you are entering is the R Console.

gueyenono commented 2 months ago

Thank you for your recommendation @jalvesaq The idea behind my code was to be able to switch to the R Console automatically even if I had a multitude of open windows. With Ctrl-W, I have to navigate to the R console manually. Also, the idea about the auto command is very well noted. I will try to work on it. Thank you.

PMassicotte commented 2 months ago

Maybe something like that where buffer_number = require('r.term').get_buf_nr()

-- Define a function to focus on the window corresponding to a buffer number
function focus_window_by_buffer(buffer_number)
    -- Iterate over all windows in the current tabpage
    for _, win_id in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
        -- Get the buffer number of the window
        local buf_nr = vim.api.nvim_win_get_buf(win_id)
        -- If the buffer number matches the one provided, focus on that window
        if buf_nr == buffer_number then
            vim.api.nvim_set_current_win(win_id)
            return
        end
    end
    print("Window for buffer " .. buffer_number .. " not found.")
end