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

Mapping <cr> #43

Closed
dbmrq opened this issue Jun 3, 2016 · 11 comments
Closed

Mapping <cr> #43

dbmrq opened this issue Jun 3, 2016 · 11 comments

Comments

@dbmrq
Copy link

dbmrq commented Jun 3, 2016

Hello,
I really want to use Pencil, but it breaks my own mapping of <cr>. Could add an option to change or disable the default mappings?

@reedes
Copy link
Collaborator

reedes commented Jun 8, 2016

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?

@dbmrq
Copy link
Author

dbmrq commented Jun 8, 2016

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.

@reedes
Copy link
Collaborator

reedes commented Jun 11, 2016

<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.

@dbmrq
Copy link
Author

dbmrq commented Jun 11, 2016

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

@reedes
Copy link
Collaborator

reedes commented Jun 18, 2016

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.

@dbmrq
Copy link
Author

dbmrq commented Jun 18, 2016

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.

@reedes
Copy link
Collaborator

reedes commented Jun 20, 2016

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

@dbmrq
Copy link
Author

dbmrq commented Jun 22, 2016

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.

@reedes
Copy link
Collaborator

reedes commented Jun 26, 2016

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.

@dbmrq
Copy link
Author

dbmrq commented Jun 27, 2016

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!

reedes pushed a commit that referenced this issue Jun 30, 2016
@reedes
Copy link
Collaborator

reedes commented Jun 30, 2016

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.

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

No branches or pull requests

2 participants