diff --git a/ftplugin/markdown.vim b/ftplugin/markdown.vim index 022da06..7d82638 100644 --- a/ftplugin/markdown.vim +++ b/ftplugin/markdown.vim @@ -18,4 +18,70 @@ else let b:undo_ftplugin = "setl cms< com< fo< flp<" endif +" Markdown auto lists +" BUG: If the cursor is on a list item at `col('$') - 1`, then a new list item +" will be added on a new line instead of splitting the line +function! AutoMDList() + let line=getline('.') + + " Potential unordered list match + let umatches=matchstr(line, '^[*+-]') + + " Potential ordered list match + let omatches=matchstr(line, '^\d\+\.') + + if empty(umatches) && empty(omatches) + " TODO: Handle the case where a list item spans over several lines. + " + " In that case, a new list item needs to be added. + " + " I don't know (yet) how to do this without going up a line in a loop and + " checking if it's the first line of the list item (that is a terrible + " solution!!) + + " If the user is not in a list, use the default behaviour for + call feedkeys("\", "n") + elseif empty(omatches) + " The case of an unordered list + + if !empty(matchstr(line, '^[*+-]\s\?$')) + " If the user is on a blank list item (i.e.: "- ") and presses , end + " the list... + exec ':normal! cc' | call feedkeys("\", "n") + elseif col('.') == len(getline('.')) + " ...otherwise, if the cursor is at the end of the line, add a list + " item... + + exec ':normal! o' . umatches . ' ' | exec ':startinsert!' + else + " ...and if the cursor is in the middle of a line, use the default + " behaviour for + call feedkeys("\", "n") + endif + elseif empty(umatches) + " The case of an ordered list + + if !empty(matchstr(line, '^\d\+\.\s\?$')) + " If the user is on a blank list item (i.e.: "42. ") and presses , + " end the list... + exec ':normal! cc' | call feedkeys("\", "n") + elseif col('.') == len(getline('.')) + " ...otherwise, if the cursor is at the end of the line, increment the list item number... + let l:nln=omatches + 1 + + " ...and add a new item + exec ':normal! o' . l:nln . '. ' | exec ':startinsert!' + else + " ...and if the cursor is in the middle of a line, use the default + " behaviour for + call feedkeys("\", "n") + endif + endif + + return +endf + +" Remap for all .md files +au BufEnter *.md inoremap :call AutoMDList() + " vim:set sw=2: