orgmode icon indicating copy to clipboard operation
orgmode copied to clipboard

`OrgMappings:org_return()` inserts "<CR>" in the presence of an existing <CR> map when <CR> pressed

Open oncomouse opened this issue 2 years ago • 1 comments
trafficstars

Describe the bug

I've started using lexima.vim with org. It creates an expression map for <CR>. When I press Enter, Neovim inserts the characters "<CR>" instead of adding a newline. The problem has to do with how OrgMappings:org_return() handles the value of b:org_old_cr_mapping.

I did some digging and the problem is caused on L520-L525. For an <expr> map the evaluation sequence needs to be:

  1. nvim_eval to get the keymap from the function
  2. nvim_replace_termcodes to escape the keymap (which is what's called by util.esc())
  3. nvim_feedkeys to send the keys

Currently, the sequence reverses steps 1 and 2, which is, I think, why I'm getting the error that I am. I tried simply reversing L521 and L523-L525, but I noticed that L527-L545 will also evaluate an expression if rhs.expr is true.

I've got a fix that works for <expr> mappings, but <script> mappings are still broken. I'll submit a PR when this is all fixed, but I wanted to go ahead and document the problem.

Steps to reproduce

  1. Using the minimal init or with a vim config with an :imap <expr> <CR> set (such as :lua function _G.foobar() return "fgr<CR>" end; vim.keymap.set("i", "<CR>", "v:lua.foobar()", { expr = true })), open an org buffer.
  2. Press Enter

Neovim will insert "fgr<CR>" instead of fgr plus a linebreak.

Expected behavior

Neovim should not insert "<CR>" when <CR> is used in an <expr> mapping.

Emacs functionality

No response

Minimal init.lua

vim.g.mapleader = " "
local lazypath = os.getenv("HOME") .. "/.local/share/lazy-org/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
	vim.fn.system({
		"git",
		"clone",
		"--filter=blob:none",
		"https://github.com/folke/lazy.nvim.git",
		"--branch=stable", -- latest stable release
		lazypath,
	})
end
vim.opt.rtp:prepend(lazypath)
require("lazy").setup({
	root = vim.fn.stdpath("data") .. "/lazy-org",
	spec = {
		{
			"nvim-orgmode/orgmode",
			opts = {},
			config = function(_, opts)
				require("orgmode").setup_ts_grammar()
				require("nvim-treesitter.configs").setup({
					highlight = {
						enable = true,
						additional_vim_regex_highlighting = { "org" },
					},
					ensure_installed = { "org" }, -- Or run :TSUpdate org
				})
				require("orgmode").setup(opts)
			end,
			dependencies = {
				{
					"nvim-treesitter/nvim-treesitter",
				},
			},
		},
	},
})

function _G.foobar()
	return "fgr<CR>"
end
vim.keymap.set("i", "<CR>", "v:lua.foobar()", { expr = true })

Screenshots and recordings

No response

OS / Distro

macOS 13.4.1

Neovim version/commit

NVIM v0.10.0-dev-596+g7d0a23973

Additional context

No response

oncomouse avatar Jun 29 '23 16:06 oncomouse

The fix was easier than I thought. I'll have a PR out soon.

oncomouse avatar Jun 29 '23 17:06 oncomouse

This issue should already be fixed. Let me know if it's still broken.

kristijanhusak avatar Feb 25 '24 16:02 kristijanhusak