Code Monkey home page Code Monkey logo

nvim-insx's Introduction

nvim-insx

nvim-insx is a flexible key mapping manager.

2023-06-01.3.07.56.mov

Limitations and Warnings

  • Dot-repeat is supported by only basic recipes.
    • Note that dot-repeat may not work with the complicated recipes.
  • This plugin usually works as expected.
    • It may fire the accidental action, because the regular expression does not capture the structure of whole text.
    • We do not intend to integrate with tree-sitter as control over the regular expression is more convenient and sufficient.
  • It is more convenient when used with vim-matchup.
    • A demonstration of vim-match usage can be referred to above.
  • This plugin provides basic cmdline-mode pairwise features.
    • The advanced recipes do not support cmdline-mode.
  • We can not accept the proposal for preset if it incorporates breaking changes.
    • Please write your own mapping definitions in your vimrc. ๐Ÿ˜ข

Usage

This plugin provides no default mappings. You need to define your custom mappings as follows.

Preset

require('insx.preset.standard').setup()

Recipe

local insx = require('insx')

insx.add(
  "'",
  insx.with(require('insx.recipe.auto_pair')({
    open = "'",
    close = "'"
  }), {
    insx.with.in_string(false),
    insx.with.in_comment(false),
    insx.with.nomatch([[\\\%#]]),
    insx.with.nomatch([[\a\%#]])
  })
)

Custom recipe

-- Simple pair deletion recipe.
local function your_recipe(option)
  return {
    action = function(ctx)
      if option.allow_space then
        ctx.remove([[\s*\%#\s*]])
      end
      ctx.send('<BS><Right><BS>')
    end,
    enabled = function(ctx)
      if option.allow_space then
        return ctx.match([[(\s*\%#\s*)]])
      end
      return ctx.match([[(\%#)]])
    end
  }
end

The standard preset enables some advanced features.

Status

The API is stable except for the helper-related APIs.

Bug report & feature request are welcome.

nvim-insx's People

Contributors

avimitin avatar futsuuu avatar hrsh7th avatar mimikun avatar ogaken-1 avatar staticwagomu avatar tani avatar yuki-yano avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

nvim-insx's Issues

Rule priority?

FileType

markdown

Initial buffer

[|]

Input key-sequence

]

Expected buffer

[](|)

Actual buffer

[]|

Information

I'm trying to create a mapping for ] in markdown that inserts a () after the square brackets (for links). This is the configuration I'm using to test this (it also supports Pandoc markdown citations, which are of the form [@citation_key] and do not need parentheses afterwards):

		local insx = require("insx")
		local esc = require("insx.helper.regex").esc

		insx.add(
			"[",
			require("insx.recipe.auto_pair").strings({
				open = "[",
				close = "]",
			})
		)
		insx.add(
			"<BS>",
			require("insx.recipe.delete_pair").strings({
				open_pat = esc("["),
				close_pat = esc("]"),
			})
		)
		insx.add(
			"]",
			require("insx.recipe.jump_next")({
				jump_pat = {
					[[\\\@<!\%#]] .. esc("]") .. [[\zs]],
				},
			})
		)
		insx.add("]", {
			action = function(ctx)
				ctx.move(ctx.row(), ctx.col() + 1)
				if not ctx.data.citation then
					ctx.send("()")
					ctx.move(ctx.row(), ctx.col() - 1)
				end
			end,
			enabled = function(ctx)
				if not vim.tbl_contains({ "markdown", "text" }, ctx.filetype, {}) then
					return false
				end
				if ctx.match([==[\[@[^]]*\%#[^]]*\]]==]) then
					ctx.data.citation = true
					return true
				end
				return ctx.match([==[\%#[^]]*\]]==])
			end,
		})

The issue is that there's no way to prioritize one rule over another, as far as I can tell. If I remove the general jump_next rule, the one for Markdown works (but only in Markdown).

I'm coming from lexima.vim which prioritizes rules (either explicitly by weighting them or implicitly by doing things like applying filetype matches over general rules), but this doesn't seem to be possible in insx.

I don't think this is a bug, but it seems like there could be a use for having multiple mappings for the same key.

Behavior when entering three consecutive backquotes in Markdown

FileType

markdown

Initial buffer

``|

Input key-sequence

`

Expected buffer

```|

Actual buffer

```|`

Information

When entering three consecutive backquotes in markdown, the following behavior is observed:

```|`

It would be appreciated if changes could be made to edit this as below, for writing the markdown code block:

```|

abbrev is not working when we setup for `<space>`

Description

Hi!
Thank you for creating and maintaining this amazing plugin!

I found that when we set up insx for <space> key, the abbrev feature does not work.

Here is a minimal configuration.

local tmp_dir = "/tmp"
local commit_hash = "c1d9efb592787f3f8c6d99db6ed5f51dd9475d4f"

-- fectch insx if not exists {{
local insx_path = tmp_dir .. "/insx"
if not (vim.uv or vim.loop).fs_stat(insx_path) then
	vim.fn.system({
		"git",
		"clone",
		"--filter=blob:none",
		"--single-branch",
		"https://github.com/hrsh7th/nvim-insx.git",
		insx_path,
	})
end

-- checkout commit_hash
vim.fn.system({
	"git",
	"-C",
	insx_path,
	"checkout",
	commit_hash,
})

vim.opt.runtimepath:prepend(insx_path)
-- }}

-- setup insx
require("insx").add("<Space>", {
	enabled = function()
		return false
	end,
	action = function(ctx) end,
})

-- define abbreviations
if (vim.fn.has("nvim-0.10")) ~= 0 then
	vim.keymap.set("ia", "fu", "fuhahahaha")
	vim.keymap.set("ca", "sa", "sasumatu")
else
	vim.cmd([[iabbrev <buffer> fu fuhahahaha]])
	vim.cmd([[cabbrev <buffer> sa sasumatu]])
end

How to reproduce

  1. Create some lua file(like init.lua) and yank-paste the above code to the file.
  2. Open Neovim with the file described earlier (ex. NVIM_APPNAME=insx_abbr nvim -u init.lua)
  3. Go to insert mode, then type fu<space> to the buffer (and you can see that abbr does not work)
  4. Go to command mode, then type sa<space> (and you can see that ca works)

I tested on [email protected] and [email protected]+ga41546d4a, and the OS is macOS 14.

Thanks

Add the ability to compose multiple recipes as single recipe

This feature is very similar to express/koa's middleware.

local delete_pair = {
  action = function(ctx)
    if not ctx.match('(|)') then
      return ctx.next()
    end
    ctx.send('<BS><Del>')
  end
}

local remove_cursor_around_spaces = {
  action = function(ctx)
    local before_space = ctx.before():match('%s+$') or ''
    ctx.send(('<BS>'):rep(#before_space)
    local after_space = ctx.after():match('^%s+') or ''
    ctx.send(('<Del>'):rep(#after_space)
    ctx.next()
  end
}

local delete_pair_with_spaces = insx.compose(remove_cursor_around_spaces, delete_pair)

auto_pair does not work with \( \)

Description

The following bug could be avoided using insx.recipe.substitute.

Code

local insx = require('insx')
local pair = require('insx.recipe.auto_pair')
insx.add('(', pair({
  open = [[\\(]],
  close = [[\)]]
}))

Expected behaviour

| is a cursor. When I type ( before the backslash, I expect to complete the close bracket \).

\| ==> \(| ==> \(|\)

Actual behaviour

| is a cursor. When I type ( before the backslash, I expect to complete the close bracket \) and \.

\| ==> \(| ==> \\(|\)

The order of closing brackets is reversed

')' and '}' are set to fast_wrap, but if I enter {} with console.log(<cursor>foo), the order of the parentheses is reversed.
If you put a space inside the parentheses before executing, the problem does not occur.

local minx = require('minx')
minx.add(')', require('minx.recipe.fast_wrap')({ close = ')' }))
minx.add('}', require('minx.recipe.fast_wrap')({ close = '}' }))
CleanShot.2023-01-12.at.16.50.59.mp4

README.md .mov is broken

FileType

n/a

Initial buffer

n/a

Input key-sequence

n/a

Expected buffer

n/a

Actual buffer

n/a

Information

I haven't been able to get the .mov currently in the README.md (https://user-images.githubusercontent.com/629908/212733495-f8e5486c-215c-4c01-b53c-b720b9779c3f.mov) to work as of this writing.

I've tried it on:

  • Firefox Linux (type error -- by the way, why isn't this a .webm?)
  • Chrome Linux (starts to load, then stalls)
  • Chrome Android (starts to load, then stalls)
  • Firefox Android (starts to load, then stalls)

My guess is there are two snags, one of which is a problem with the data hosted by github for the .mov, and the other is the choice to host the data as a .mov in the first place, when .webm (with an open codec) would be the obvious cross-platform choice in 2023.

attempt to call field 'keycode'

Description

Since i updated my plugin, i got :

E5113: Error while calling lua chunk: .../pack/packer/start/nvim-insx/lua/insx/kit/Vim/Keymap.lua:29: attempt to call field 'keycode' (a nil value)
stack traceback:
        .../pack/packer/start/nvim-insx/lua/insx/kit/Vim/Keymap.lua:29: in function 'normalize'
        .../nvim/site/pack/packer/start/nvim-insx/lua/insx/init.lua:297: in function 'add'
        ...pack/packer/start/nvim-insx/lua/insx/preset/standard.lua:16: in function 'setup_insert_mode'
        ...pack/packer/start/nvim-insx/lua/insx/preset/standard.lua:10: in function 'setup'
        /home/dk/.config/nvim//lua/plugins_config.lua:439: in main chunk
        [C]: in function 'require'
        /home/dk/.config/nvim/init.lua:21: in main chunk

Maybe from latest updates on the plugin ?

[Feature Request] Allow disable some of the default preset in certain circumstance

I've been using this plugin for a long time and it is always been great. But there was one annoying problem that came with the standard preset: the ' single quote got auto-pairing when I was coding in Rust or OCaml. In Rust programming language, 'a is used to denote the lifetime of a reference, and in OCaml, 'a is used to represent a type variable. These two usages are very common, and getting automatic single quote auto pairing means that every time when I was working around with the type system, I have to press single quotes twice and delete the pair, then I can finally got a type, not an 'a' character.

Actually it can be fixed by manually adding filetype to the single quote rule myself, but I don't want to copy and paste all the other default presets just to fix this issue. So if there is a way to inject a condition into the default preset and override a part of it, it can save me from getting a huge but redundant neovim dotfiles.

FR: do not auto-add `'` when in comment region

Using the presets, when typing a ' anywhere automatically adds another '. This is usually great, but inside comments, this is rather annoying, since in comments, I often want to write something like "isn't" or "it's", and always get an unnecessary extra ' there.

I'd suggest either not auto-enclosing ' in comments, or adding some conditionals like for example only adding ' if the previous character is a non-letter.

Change request: remove ["<"] pair rule in standard preset

The < operator is often use individually as a comparison operator. But the nvim-insx add <> as default preset which will force the user to delete extra operator. This also let writing code like std::cout << "Hello World" << std::endl harder when coding in C++. I will prefer letting user to manually add < by their own instead of passing this preset as default.

unexpected dot-repeat: `[]()` -> `[()]`, `[](foo.html)` -> `[(foo.html)]`

Description

When inserting empty brackets followed by brackets, dot-repeats will nest the latter by the prior.

config

vim.opt.runtimepath:prepend("path/to/nvim-insx")
require("insx.preset.standard").setup()

steps to reproduce

o[]()<Esc>.

becomes


[]()
[()]

o[](foo.html)<Esc>.

becomes


[](foo.html)
[(foo.html)]

Consider endwise recipe

Currently the endwise recipe is very basic. I think it should be smarter.

However, I don't use endwise, so I don't know what kind of functionality is needed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.