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

Conversation

handcart2034
Copy link

Hi, this is a new function that toggles \\ at line end. In environments like align, sometimes the formulas are changed thus the line break is not optimal. One needs $ or A to move to end of line, then either xx or \\, finally go back to the original position, which can be quite tiring for more than a couple lines.

So wrote this function and map tsb to it. If everything looks good will add tests and docs to finalize this request.

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

@lervag
Copy link
Owner

lervag commented May 8, 2024

Hi, this is a new function that toggles \\ at line end. In environments like align, sometimes the formulas are changed thus the line break is not optimal. One needs $ or A to move to end of line, then either xx or \\, finally go back to the original position, which can be quite tiring for more than a couple lines.

I don't mind adding this, even though I don't think I'll end up using it. It falls nicely into the current patterns and shouldn't be "in the way" of anything that I can think of. I had a comment the implementation, see above.

So wrote this function and map tsb to it. If everything looks good will add tests and docs to finalize this request.

I think it looks good, although I would prefer if you made the suggested change. And, of course, that you add a couple of tests and docs.

And thanks! I appreciate the effort you've made here in actually implementing the feature you want added!

@handcart2034
Copy link
Author

Thanks for the suggestions and the RegEx simplification, all of them have been implemented, along with a couple tests and document updates.

lervag added a commit that referenced this pull request May 13, 2024
@lervag
Copy link
Owner

lervag commented May 13, 2024

Thanks! I made a minor additional change and then merged this now. The change was basically to change "Toggle line end \\" to "Toggle line-break macro \\" and to use this name consistently.

@lervag lervag closed this May 13, 2024
@lervag
Copy link
Owner

lervag commented May 13, 2024

Note, though: I rebased and merged locally; for some reason, Github doesn't recognize this.

@clason
Copy link
Contributor

clason commented May 13, 2024

Neat! Small follow-up suggestion: support for visual mode ;)

@lervag
Copy link
Owner

lervag commented May 13, 2024

Heh, yes. @handcart2034 are you up for the challenge? One way to implement it would be to reuse the current implementation and just apply it to every line in the visual selection.

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.

None yet

3 participants