preservim / vim-pencil

Rethinking Vim as a tool for writing
Other
1.58k stars 39 forks source link

Deleting words/text in command mode does not reformat paragraph #39

Closed mlawren closed 8 years ago

mlawren commented 8 years ago

I was hoping (expecting?) that running "dw" in the middle of a paragraph (with hard wrapping) that the paragraph would be reformatted. That doesn't seem to be the case. Is that possible in some way?

reedes commented 8 years ago

A good question.

It appears that Vim's autoformat (which pencil relies upon) will reformat when inserting characters and using backspace, but not with "dw", as you observed.

Marking as an enhancement, as it'll require someone to research and come up with an implementation.

mlawren commented 8 years ago

Marking as an enhancement, as it'll require someone to research and come up with an implementation.

The best I could come up with so far is to use the TextChanged autocmd trigger, however the following suffers from destroying the undo history.

    function! Preserve(command)
        " Preparation: save last search, and cursor position.
        let _s=@/
        let _cursor_pos = getpos('.')

        " Do the business:
        execute a:command

        " Clean up: restore previous search history, and cursor position
        let @/=_s
        call setpos('.', _cursor_pos)
    endfunction

    autocmd TextChanged * call Preserve('normal gqap')

Unfortunately my vimscript skills are quite rudimentary and the above is no doubt a very naive approach. It also quite clearly has global impact and is not related to the pencil plugin. Perhaps it at least helps the next person who takes a look to know where not to go :-)

Mark Lawrence

mlawren commented 8 years ago

I may have just found out the correct way to fix this issue. Reading about the formatoptions variable I see that 'a' has the following effect:

a       Automatic formatting of paragraphs.  Every time text is inserted or
        deleted the paragraph will be reformatted.  See auto-format.
        When the 'c' flag is present this only happens for recognized
        comments.

When I manually set formatoptions to "croqln1ta" (appending "a" to what I discovered when in HardPencil mode) then the text appears to be rebalanced properly when using "dw" and similar commands. I have no idea what other effects the "a" has that may conflict with the goals of pencil.

Mark Lawrence

reedes commented 8 years ago

The a formatoption is how pencil enables Vim's autoformat, as found here https://github.com/reedes/vim-pencil/blob/master/autoload/pencil.vim#L147

So, it should be deleting while autoformat is active, but isn't. A mystery that may be related to other formatoption configuration.

reedes commented 8 years ago

Ah, I'm understanding this better now that I'm using Vim regularly again.

You're absolutely right that there's no reformatting in Command/Normal mode. Pencil only enables autoformat during insert mode.

Enabling autoformat in Command/Normal mode is a dual-edged sword. In my experience developing the plugin, it was disruptive, formatting things that I didn't want touched, such as quoted code in a markdown document.

I personally use a normal-mode mapping to Q or K to gqip to manually reformat text that I'm manipulating. It's not idea, but seems to be the best tradeoff.

mlawren commented 8 years ago

Ah, ok. So no easy solution then. Thanks anyway for taking the time to research this issue. I might steal your normal mappings for myself :-)

Mark Lawrence