aboutsummaryrefslogtreecommitdiff
path: root/vim/autoload/markdown.vim
blob: 0f03961c9bd73d834c4308a12acfc0635a38d1b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
" Let's try this heading-based fold method out (Tim Pope)
function! markdown#Fold() abort
  let line = getline(v:lnum)

  " Regular headers
  let depth = match(line, '\(^#\+\)\@<=\( .*$\)\@=')
  if depth > 0
    return '>' . depth
  endif

  " Setext style headings
  if line =~# '^.\+$'
    let nextline = getline(v:lnum + 1)
    if nextline =~# '^=\+$'
      return '>1'
    elseif nextline =~# '^-\+$'
      return '>2'
    endif
  endif

  return '='
endfunction

" Add an underline under a heading
function! markdown#Heading(char) abort

  " Get current position
  let pos = getpos('.')

  " Get heading text from current line
  let heading = getline(pos[1])

  " Build underline string by repeating character by the string length of the
  " heading text
  "
  let underline = repeat(a:char, strlen(heading))

  " If the line after this one looks like it's already an underline, replace
  " it; otherwise, create a new underline
  "
  if getline(pos[1] + 1) =~# '^[-=]\{2,}$'
    call setline(pos[1] + 1, underline)
  else
    call append(pos[1], underline)
  endif

  " Move to the first column of the underline we just inserted
  let pos[1] += 1
  let pos[2] = 1
  call setpos('.', pos)

endfunction