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

Add imaps of the type <leader>xc -> \$command{c}. #1612

Closed
wants to merge 3 commits into from
Closed

Add imaps of the type <leader>xc -> \$command{c}. #1612

wants to merge 3 commits into from

Conversation

rsekman
Copy link
Contributor

@rsekman rsekman commented Feb 29, 2020

Useful for commands such as mathbf, mathfrak, mathcal. For now, specify these like
\ { 'lhs' : 'b', 'rhs' : 'vimtex#imaps#style("mathbf")', 'expr' : 1, 'leader' : '#'},
where 'expr' makes vimtex evaluate the rhs as an expression, and vimtex#imaps#style is a helper function.

Refactoring and generalization of #1611.

Useful for commands such as mathbf, mathfrak, mathcal.  For now, specify these like
    \ { 'lhs' : 'b',  'rhs' : 'vimtex#imaps#style("mathbf")', 'expr' : 1, 'leader' : '#'},
where 'expr' makes vimtex evaluate the rhs as an expression, and
vimtex#imaps#style is a helper function.
@lervag
Copy link
Owner

lervag commented Mar 1, 2020

This looks well done. I see one minor issue: Writing #b and similar outside math mode now "glitches" because the mapping is still activated. The expansion is not done, since it is stilled wrapped in math mode, but the getchar function is executed and ignored.

If you want to look into it, then I'll wait before I merge. Else I think it is probably not something people will notice and care about. That is, I'll gladly merge this as is, but I will first let you consider if it is possible to avoid the minor inconvenience.

@@ -155,6 +155,9 @@ function! vimtex#imaps#wrap_environment(lhs, rhs) abort " {{{1
endfunction

function! vimtex#imaps#style(command)
if !s:is_math()
return ''
endif
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but: What if we wanted to use this for non math stuff? E.g. \emph{...}?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use another helper function, perhaps rename this one to vimtex#imaps#style_math.

I guess ideally the wrapper should prevent the rhs from being called, but I think this requires passing in function handles, testing for this in every wrapper, and so on.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the simplest solution is to simply rename the function. I'll merge this now and just do that. Pragmatism is imporant to avoid over engineering.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good! However, the name of the function also needs to be changed in the pre-configured imaps. I'll push a commit with this change.

lervag added a commit that referenced this pull request Mar 1, 2020
@lervag
Copy link
Owner

lervag commented Mar 1, 2020

Thanks, I've merged this now. I made very minor modifications and generally appreciate that you've adapted so well to my style when adding this. Also, very good work, this seems like it could be useful.

Btw: Perhaps we should also update the docs?

@lervag lervag closed this Mar 1, 2020
@postylem
Copy link
Contributor

I was trying to make a new imap macro for easily typing characters in \mathrm when in math mode, and stumbled around a lot before finding I needed to have 'expr' = 1 in the map. I'm not sure if I've looked in the right places, but is this mentioned in the docs anywhere?

It might be useful to have an example in the docs for neophytes like me that don't understand what they're doing: If you want to make it so typing #rX will result in the string \mathrm{X}, put the following in tex.vim:

call vimtex#imaps#add_map({
			\ 'lhs' : 'r',
			\ 'rhs' : 'vimtex#imaps#style_math("mathrm")',
			\ 'expr' : 1,
			\ 'leader' : '#',
			\ 'wrapper' : 'vimtex#imaps#wrap_math'
			\})

lervag added a commit that referenced this pull request Feb 21, 2021
@lervag
Copy link
Owner

lervag commented Feb 21, 2021

Thanks, I've pushed an update to the docs.

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

Successfully merging this pull request may close these issues.

3 participants