vim-md/ftplugin/markdown.vim

54 lines
2.2 KiB
VimL
Raw Normal View History

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" File: ftplugin/markdown.vim
" Maintainer: Andrew Nechaev <i@geekfrom.ru>
" Version: 0.1.0
2022-05-16 12:48:45 +03:00
" Modified: 2022-05-16 13:48+0400
" License: MIT
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
2022-05-16 12:48:45 +03:00
set textwidth=120
set formatoptions+=t
set colorcolumn=-1
set wm=0
set wrap linebreak
nnoremap <buffer> <silent> <Leader>t :call winrestview(<SID>toggle({' ': 'x', 'x': '-', '-': ' '}))<cr>:w!<cr>
vnoremap <buffer> <silent> <Leader>t :call winrestview(<SID>toggle({' ': 'x', 'x': '-', '-': ' '}))<cr><esc>:w!<cr>
nnoremap <buffer> <silent> <Leader>r :call winrestview(<SID>toggleState('(', '( ) '))<cr>A
vnoremap <buffer> <silent> <Leader>r :call winrestview(<SID>toggleState('(', '( ) '))<cr>
nnoremap <buffer> <silent> <Leader>c :call winrestview(<SID>toggleState('[', '[ ] '))<cr>A
vnoremap <buffer> <silent> <Leader>c :call winrestview(<SID>toggleState('[', '[ ] '))<cr>
inoremap <buffer> <CR> <CR><Esc>:call <SID>auto_list()<CR>A
nnoremap <buffer> o o<Esc>:call <SID>auto_list()<CR>A
nnoremap <buffer> O O<Esc>:call <SID>auto_list()<CR>A
set tabstop=2 softtabstop=2 shiftwidth=2 expandtab
function s:toggle(dict)
let view = winsaveview()
execute 'keeppatterns s/^\s*\(-\s*\|+\s*\|*\s\)\?\s*[\[(]\zs.\ze[\])]/\=get(a:dict, submatch(0), a:0 ? a:1 : " ")/e'
return view
endfunction
function s:toggleState(search, replacement)
let view = winsaveview()
execute 'keeppatterns s/^\s*\(-\s*\|+\s*\|*\s\)\?\zs\(\[.\]\|(.)\)\?\s*\ze/\=match(submatch(0), a:search) != -1 ? "" : a:replacement/e'
return view
endfunction
function! s:auto_list()
let l:current_text = getline(line("."))<
let l:preceding_line = getline(line(".") - 1)
if l:preceding_line =~ '\v^\s*\d+\.'
let l:list_index = matchstr(l:preceding_line, '\v\d+')
let l:next = l:list_index + 1
let l:whitespaces = matchstr(l:preceding_line, '\v^\s*')
call setline(".", l:whitespaces.l:next.". ".l:current_text)
elseif l:preceding_line =~ '^\s*\(-\s*\|+\s*\|*\s\)\?\(\[.\]\|\(.\)\)\?'
let l:starter = matchstr(l:preceding_line, '^\s*\(-\s*\|+\s*\|*\s\)\?\(\[.\]\|(.)\)\?\s*')
call setline(".", l:starter.l:current_text)
endif
endfunction