Giter Club home page Giter Club logo

Comments (11)

reedes avatar reedes commented on May 25, 2024

Rather than an option, how about if pencil declines to override an existing mapping, if one is present?

I'm curious, how have you remapped CR in Insert mode?

from vim-pencil.

dbmrq avatar dbmrq commented on May 25, 2024

Sure, that would work, if it doesn't break Pencil. I didn't look into it much, but I couldn't figure out what you map <cr> for.

I actually have more than one mapping for <cr>. I use it to expand UltiSnips snippets, so if I have a snippet for item and another for itemize I can type it, use <tab> to iterate through YouCompleteMe's suggestions and then tap <cr> to select and expand one.
Then I also have another mapping… when formatoptions is set to t (I think it's t) comments are added automatically to new lines. So I'm typing a comment, then I tap <cr> and another line opens with a comment character already in it. So far so good. But sometimes I finish typing a comment and impulsively type <cr> many times, and I end up with many new lines of comments, while I expected blank lines. So I map <cr> to remove the comment when there's nothing but a comment character in a line. Then I can type a comment, type <cr><cr><cr> and I'll end up with two blank lines, instead of three lines of comments. Or I can just type <cr> once and keep writing a comment in the second line. This is also useful to auto-complete list items, I can set -, * and \item (in LaTeX) as comments and when I tap <cr> there's already another \item there, I don't have to type it again. And then if I don't actually want that new \item I can just tap <cr> again and it's gone.
So I actually map it to to a function that decides if it should just add a regular line break or one of those options when appropriate.

from vim-pencil.

reedes avatar reedes commented on May 25, 2024

<cr> is mapped in pencil as an undo point, along with several other keys. This allows you to back out of changes in smaller increments.

I'm trying out the change locally. If it looks good I'll post in the next few days.

from vim-pencil.

dbmrq avatar dbmrq commented on May 25, 2024

Great, thank you!
And I love your other plugins, by the way. My vimrc has its own section called "reedes". :)

from vim-pencil.

reedes avatar reedes commented on May 25, 2024

This should have been a single, properly-labeled, commit, but was three.

Nevertheless it should be working. Please give it a try and let me know if it respects your existing <cr> mapping in insert mode.

from vim-pencil.

dbmrq avatar dbmrq commented on May 25, 2024

Uhm, weird…
Now it doesn't completely override my mapping, but it still does something to it.

Here's what happens without pencil:

without pencil

A first <cr> expands the item snippet. The next <cr>s are regular <cr>s, but \item is defined as a comment, so it's added automatically to the next line. Then after item 5 I do <cr><cr>, and the second one deletes the item that was just added.

Now if I use Pencil and do the same thing, here's what happens:

with pencil

I just start writing the next item and the line is joined with the previous one.

Maybe my code for doing all this is less than ideal, but still it seems like Pencil is messing with something it shouldn't mess with, right?

If it matters, this is my mapping that causes the problem:

function! CROrUncomment()
    for comment in map(split(&l:comments, ','),
        \ 'substitute(v:val, "^.\\{-}:", "", "")')
            if getline('.') =~ '\V' . escape(comment, '\') . ' \$'
                return repeat("\<BS>", strchars(comment) + 1)
            endif
    endfor
    return "\<cr>"
endfunction
inoremap <expr> <CR> CROrUncomment()

The problem happens with any comment character, tapping <cr> should begin a new line with a comment and tapping <cr> right after that should delete that comment, but with pencil the lines are joined.

from vim-pencil.

reedes avatar reedes commented on May 25, 2024

That looks like autoformat behavior.

As mentioned in the README, Pencil will typically enable Vim's autoformat when entering Insert mode. This is normally a good thing, as we want text to automatically wrap.

Because autoformat isn't always desired, such as with code blocks, pencil has a blacklisting mechanism which looks at the highlight group that the cursor is in (or near).

SO, with my config, the text of the \item is in the texDocZone highlight group. Adding that to the blacklist would prevent autoformat from being enabled, getting the behavior you want.

I don't know offhand how texDocZone is otherwise used, so blacklisting it may not be desirable.

You can define your own blacklist to override the default at https://github.com/reedes/vim-pencil/blob/master/plugin/pencil.vim#L74-L114

from vim-pencil.

dbmrq avatar dbmrq commented on May 25, 2024

I'm sorry for the delay, I couldn't look into it until now.
I copied the let g:pencil#autoformat_config to my vimrc and changed the part that says

'tex': {
        \     'black': [
        \       'tex(BeginEndName|Delimiter|DocType|InputFile|Math|RefZone|Title)',
        \       'texSection$',
        \     ],
        \     'enforce-previous-line': 1,
        \   }

to

'tex': {
        \     'black': [
        \       'tex(BeginEndName|Delimiter|DocType|InputFile|Math|RefZone|Title)',
        \       'texSection$',
        \       'texDocZone',
        \     ],
        \     'enforce-previous-line': 1,
        \   }

Is that what I was supposed to do? It didn't work.

from vim-pencil.

reedes avatar reedes commented on May 25, 2024

With the change you made, if you enter insert mode from a cursor position in a texDocZone highlight group, it should no longer be enabling Vim's autoformat. If it is enabling, there's something we're missing.

To determine highlight group for the blacklists, I use a function key mapping...

nmap <F9> :call <SID>SynStack()<CR>
function! <SID>SynStack()
  if !exists("*synstack")
    return
  endif
  echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')
endfunc

Give that a try to see if texDocZone highlight group is at the cursor position.

from vim-pencil.

dbmrq avatar dbmrq commented on May 25, 2024

Aha, the highlight group was texStatement. I blacklisted that and now it works perfectly.
I don't know if this has any undesirable consequences, but, well, it won't Pencil's fault, then
Thanks again for all the help!

from vim-pencil.

reedes avatar reedes commented on May 25, 2024

texStatement now added to default blacklist, so you can get rid of your override.

For others, if this degrades pencil's usefulness with tex filetype, please let me know. We can dig in further to find a better solution.

from vim-pencil.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.