preservim / vim-markdown

Markdown Vim Mode
4.68k stars 521 forks source link

Markdown folding disturbs diff mode #427

Open yous opened 5 years ago

yous commented 5 years ago

For example, add a random line to this project's README.md and save it as README2.md:

# Vim Markdown
Test

[![Build Status](...)](...)

Run vimdiff README.md README2.md, then one of the buffer will have weird folding:

Screenshot 2019-04-11 17 03 16

According to after/ftplugin/markdown.vim, the s:MarkdownSetupFolding() is called on BufWinEnter, BufWritePost , InsertEnter, InsertLeave, CursorHold, CursorHoldI. When Vim shows diff, 'foldmethod' is set to 'diff', and s:MarkdownSetupFolding() resets it. So I think checking if the buffer is diff or not (using &diff) would be appropriate.

kmac commented 5 years ago

I'm seeing this as well. For me, diffs are completely broken for markdown files when this plugin is active. i.e. [c and ]c don't work either. I'm working around it by not activating the plugin if started in diff mode.

KSR-Yasuda commented 5 years ago

I'm suffering from this, too.

Setting below in .vimrc, the vim-markdown folding is suppressed to work only at opening file, at least (Instead of vim-markdown auto setting, set folding settings by oneself).

Plugin 'plasticboy/vim-markdown'
let g:vim_markdown_folding_disabled = 1
autocmd BufNewFile,BufRead *.md setlocal foldmethod=expr foldexpr=Foldexpr_markdown(v:lnum)
KSR-Yasuda commented 5 years ago

So I think checking if the buffer is diff or not (using &diff) would be appropriate.

This patch may work:

diff --git a/after/ftplugin/markdown.vim b/after/ftplugin/markdown.vim
index 8be6ff9..9840217 100644
--- a/after/ftplugin/markdown.vim
+++ b/after/ftplugin/markdown.vim
@@ -177,8 +177,12 @@ function! s:MarkdownSetupFolding()
                 setlocal foldtext=Foldtext_markdown()
             endif
         endif
-        setlocal foldexpr=Foldexpr_markdown(v:lnum)
-        setlocal foldmethod=expr
+        if &foldexpr == ""
+            setlocal foldexpr=Foldexpr_markdown(v:lnum)
+        endif
+        if &foldmethod != "diff"
+            setlocal foldmethod=expr
+        endif
     endif
 endfunction

Besides foldmethod, if foldexpr is set, do not override it.

But I'm not sure whether this is a better solution. Could someone review and merge it?

KSR-Yasuda commented 4 years ago

Perhaps just checking foldexpr might be fine. Sometimes manual folding is preferred, to add extra folding by zf.

diff --git a/after/ftplugin/markdown.vim b/after/ftplugin/markdown.vim
index 8be6ff9..991a470 100644
--- a/after/ftplugin/markdown.vim
+++ b/after/ftplugin/markdown.vim
@@ -177,8 +177,10 @@ function! s:MarkdownSetupFolding()
                 setlocal foldtext=Foldtext_markdown()
             endif
         endif
-        setlocal foldexpr=Foldexpr_markdown(v:lnum)
-        setlocal foldmethod=expr
+        if &foldexpr == ""
+            setlocal foldexpr=Foldexpr_markdown(v:lnum)
+            setlocal foldmethod=expr
+        endif
     endif
 endfunction
KSR-Yasuda commented 3 years ago

This issue being left for a long time, raised a pull-request formally.

yous commented 3 years ago

The default value of 'foldexpr' is "0", so comparing against "" would disable the entire feature.

                                                'foldexpr' 'fde'
'foldexpr' 'fde'        string (default: "0")
                        local to window
                        {not available when compiled without the +folding
                        or +eval features}
KSR-Yasuda commented 3 years ago

Oh, this was because I set foldexpr for *.md in .vimrc. Got it.

KSR-Yasuda commented 3 years ago

Fixed.