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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions autoload/vimtex.vim
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ function! vimtex#init_options() abort " {{{1
\ { 'lhs' : 'vk', 'rhs' : '\varkappa' },
\ { 'lhs' : 'vq', 'rhs' : '\vartheta' },
\ { 'lhs' : 'vr', 'rhs' : '\varrho' },
\ { 'lhs' : '/', 'rhs' : 'vimtex#imaps#style("slashed")', 'expr' : 1, 'leader' : '#'},
\ { 'lhs' : 'b', 'rhs' : 'vimtex#imaps#style("mathbf")', 'expr' : 1, 'leader' : '#'},
\ { 'lhs' : 'f', 'rhs' : 'vimtex#imaps#style("mathfrak")', 'expr' : 1, 'leader' : '#'},
\ { 'lhs' : 'c', 'rhs' : 'vimtex#imaps#style("mathcal")', 'expr' : 1, 'leader' : '#'},
\ { 'lhs' : '-', 'rhs' : 'vimtex#imaps#style("overline")', 'expr' : 1, 'leader' : '#'},
\ { 'lhs' : 'B', 'rhs' : 'vimtex#imaps#style("mathbb")', 'expr' : 1, 'leader' : '#'},
\])

call s:init_option('vimtex_mappings_enabled', 1)
Expand Down
12 changes: 11 additions & 1 deletion autoload/vimtex/imaps.vim
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,12 @@ function! s:create_map(map) abort " {{{1
endif
let b:vimtex_context[l:key] = a:map.context
endif
if !get(a:map, 'expr')
let a:map.rhs = string(a:map.rhs)
endif

silent execute 'inoremap <expr><silent><nowait><buffer>' l:lhs
\ l:wrapper . '("' . escape(l:lhs, '\') . '", ' . string(a:map.rhs) . ')'
\ l:wrapper . '("' . escape(l:lhs, '\') . '", ' . a:map.rhs . ')'

let s:created_maps += [a:map]
endfunction
Expand Down Expand Up @@ -151,6 +154,13 @@ function! vimtex#imaps#wrap_environment(lhs, rhs) abort " {{{1
return l:return
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.

let l:c = getchar()
return '\' . a:command . '{' . nr2char(l:c) . '}'
endfunction
" }}}1

"
Expand Down
10 changes: 10 additions & 0 deletions test/tests/test-imaps/test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ call vimtex#test#keys('$i;b;;',
\['$2+2 = $'],
\['$2+2 = \beta;$'])

" Test #bv -> \mathbf{v}
call vimtex#test#keys('$i#bv',
\['$2+2 = $'],
\['$2+2 = \mathbf{v}$'])

" Should not gobble a character outside of math mode
call vimtex#test#keys('$a#bv',
\['$2+2 = $'],
\['$2+2 = $#bv'])

" Test ;; -> ; (leader escape)
call vimtex#test#keys('$i;;',
\['$;; = $'],
Expand Down