" Enable spelling except in Windows terminal vim.
if !has("win32") || has("gui_running")
  setlocal spell
endif
setlocal foldmethod=expr
setlocal foldexpr=MarkdownFold(v:lnum)
function! MarkdownFold(lnum)
  let l:line = getline(a:lnum)
  if match(l:line, '^######.*$') == 0
    return '>6'
  elseif match(l:line, '^#####.*$') == 0
    return '>5'
  elseif match(l:line, '^####.*$') == 0
    return '>4'
  elseif match(l:line, '^###.*$') == 0
    return '>3'
  elseif match(l:line, '^##.*$') == 0
    return '>2'
  elseif match(l:line, '^#.*$') == 0
    return '>1'
  endif
  return '='
endfunction
hi link markdownCode Special
hi link markdownCodeDelimiter PreProc
hi link markdownHeadingDelimiter Statement
hi link markdownLinkDelimiter PreProc
hi link markdownLinkTextDelimiter PreProc
hi link markdownUrl Include
" Max 80 chars wide.
setlocal textwidth=80
" Use tabwidth of 4 to be markdown complient.
setlocal tabstop=4 shiftwidth=4 softtabstop=4
" Custom surround for markdown link syntax.
" "ys{motion}l" makes link out of "word" -> "[word]()"
let b:surround_{char2nr("l")} = "[\r]()"
" "ys{motion}L" makes link out of "url" -> "[](url)"
let b:surround_{char2nr("L")} = "[](\r)"
finish " Experiment functionality (disabled). {{{
nnoremap yh :<C-U>set opfunc=<SID>yh<CR>g@
function! <SID>yh(type, ...)
  let save_reg_h = getreg('h')
  " Mark positions
  let begin = getpos("'[")[1:2]
  let end = getpos("']")[1:2]
  " Append after the end mark.
  call cursor(end)
  call setreg('h', ']()')
  normal "hp
  " Prepend before the begin mark.
  call cursor(begin)
  call setreg('h', '[')
  normal "hP
  " Place cursor at final )
  call cursor(end)
  normal f)
  call setreg('h', save_reg_h)
endfunction
" }}}