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

[FEATURE] Intercept Vim-triggered newlines for the Matrix environment \\ shortcut #352

Open
llakala opened this issue Nov 17, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@llakala
Copy link
Contributor

llakala commented Nov 17, 2024

Description of the Problem

Currently, within a Matrix environment, a \\ symbol is inserted whenever you press Enter. However, if you have have Obsidian's Vim mode enabled, and press o to add a newline and enter Insert mode, no \\ is added.

Description of the Solution

Hypothetically, the plugin would be able to detect the Vim action and add \\ when pressing o, just like when pressing Enter normally.

I don't know if this is possible. If the newline isn't being detected by KeyboardEvent, I assume that Obsidian's does something like this when pressing o:

  1. add the newline through its own systems, undetectable to us
  2. enter Insert mode, where we can now detect inputs (but a bit late for that)

If it is possible to hook into this mechanism, it's not obvious to me how one would do it. I originally planned on making a PR to implement this, but realized I didn't know what direction to go in. I'd love some feedback on whether people think this would be possible, and if so, how it could be implemented.

@llakala llakala added the enhancement New feature or request label Nov 17, 2024
@llakala
Copy link
Contributor Author

llakala commented Dec 2, 2024

No luck after some hacking away at this. I was hoping that this line would work:

map o g$a<CR>

However, it seems that Obsidian doesn't recognize as pressing Enter, which is what Matrix shortcuts expect to insert \\. This would work perfectly if either:

  • The matrix shortcuts would be able to accept <CR> as a valid newline somehow. Currently, the plugin just taps into keybinds, so I assume this would require extra functionality to tap into what Vim sends to Obsidian.
  • Some form of vim shortcut could somehow manually trigger the literal Enter symbol rather than <CR>, which the matrix shortcuts would then accept.

This is where I hit a wall when it came to knowledge, and searching didn't help much. If anyone has any knowledge in either of these areas, please let me know.

@superle3
Copy link

superle3 commented Dec 7, 2024

export function getVimSpecialEnterCommand(settings: LatexSuitePluginSettings): vimCommand {
	return {
		id: "latex-suite-vim-special-enter",
		defineType: "defineAction",
		type: "action",
		action: (cm: CodeMirrorEditor) => {
			//@ts-ignore
			const vimObj: Vim | null = window?.CodeMirrorAdapter?.Vim;
			if (!vimObj) return;
			vimObj.handleKey(cm, "A");
			const event = new KeyboardEvent("keydown", { key: "Enter" });
			cm.getInputField().dispatchEvent(event);
		},
		key: "o",
		context: "normal",
	}
}

something like this? it works, only it puts 2 enters instead of one.

@superle3
Copy link

superle3 commented Dec 7, 2024

export function getVimSpecialEnterCommand(settings: LatexSuitePluginSettings, plugin: LatexSuitePlugin): vimCommand {
	return {
		id: "latex-suite-vim-special-enter",
		defineType: "defineAction",
		type: "action",
		action: (cm: CodeMirrorEditor) => {
			//@ts-ignore
			const vimObj: Vim | null = window?.CodeMirrorAdapter?.Vim;
			if (!vimObj) return;
			vimObj.handleKey(cm, "A");
			const view = EditorView.findFromDOM(cm.getWrapperElement());
			const ctx = Context.fromView(view);
			runMatrixShortcuts(view, ctx, "Enter", false);
		},
		key: "o",
		context: "normal",
	}
}

fixed it, couldn't figure out how to get the current EditorVIew or whatever is used

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants