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

New function to toggle \\ #2948

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions autoload/vimtex.vim
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ function! s:init_default_mappings() abort " {{{1
call s:map(0, 'n', 'tsc', '<plug>(vimtex-cmd-toggle-star)')
call s:map(0, 'n', 'tsf', '<plug>(vimtex-cmd-toggle-frac)')
call s:map(0, 'x', 'tsf', '<plug>(vimtex-cmd-toggle-frac)')
call s:map(0, 'n', 'tsb', '<plug>(vimtex-cmd-toggle-break)')
call s:map(0, 'i', '<F7>', '<plug>(vimtex-cmd-create)')
call s:map(0, 'n', '<F7>', '<plug>(vimtex-cmd-create)')
call s:map(0, 'x', '<F7>', '<plug>(vimtex-cmd-create)')
Expand Down
23 changes: 23 additions & 0 deletions autoload/vimtex/cmd.vim
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ function! vimtex#cmd#init_buffer() abort " {{{1

xnoremap <silent><buffer> <plug>(vimtex-cmd-toggle-frac)
\ :<c-u>call vimtex#cmd#toggle_frac_visual()<cr>

nnoremap <silent><buffer> <plug>(vimtex-cmd-toggle-break)
\ :<c-u>call <sid>operator_setup('toggle_break')<bar>normal! g@l<cr>
endfunction

" }}}1
Expand Down Expand Up @@ -267,6 +270,25 @@ function! vimtex#cmd#toggle_frac_visual() abort " {{{1
call setreg('a', l:save_reg)
endfunction

" }}}1
function! vimtex#cmd#toggle_break() abort " {{{1
let l:lnum = line('.')
let l:line = getline(l:lnum)
let l:len = col('$') - 1

if l:len >= 3 && strpart(l:line, l:len - 3) == ' \\'
call setline(l:lnum,
\ strpart(l:line, 0, l:len - 3))
elseif l:len >= 2 && strpart(l:line, l:len - 2) == '\\'
call setline(l:lnum,
\ strpart(l:line, 0, l:len - 2))
else
call setline(l:lnum,
Copy link
Owner

Choose a reason for hiding this comment

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

In lines 280, 283 and 286 you've included an unnecessary end-of-line space. I prefer to avoid that.

Copy link
Owner

Choose a reason for hiding this comment

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

Also, I think you've unnecessarily broken these statements into multiple lines. I would write it like this:

  if l:len >= 3 && strpart(l:line, l:len - 3) == ' \\'
    call setline(l:lnum, strpart(l:line, 0, l:len - 3))
  elseif l:len >= 2 && strpart(l:line, l:len - 2) == '\\'
    call setline(l:lnum, strpart(l:line, 0, l:len - 2))
  else
    call setline(l:lnum, l:line . ' \\')
  endif

Copy link
Owner

Choose a reason for hiding this comment

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

In fact, I think I would solve this with regexes. Something like this:

function! vimtex#cmd#toggle_break() abort " {{{1
  let l:lnum = line('.')
  let l:line = getline(l:lnum)

  let l:replace = l:line =~# '\s*\\\\\s*$'
        \ ? substitute(l:line, '\s*\\\\\s*$', '', '')
        \ : substitute(l:line, '\s*$', ' \\\\', '')

  call setline(l:lnum, l:replace)
endfunction

" }}}1

\ l:line
\ . ' \\')
endif
endfunction

" }}}1

function! vimtex#cmd#parser_separator_check(separator_string) abort " {{{1
Expand Down Expand Up @@ -609,6 +631,7 @@ function! s:operator_function(_) abort " {{{1
\ 'delete': 'delete()',
\ 'toggle_star': 'toggle_star()',
\ 'toggle_frac': 'toggle_frac()',
\ 'toggle_break': 'toggle_break()',
\ }[s:operator]
endfunction

Expand Down