Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 51 lines (45 sloc) 1.575 kb
8713dce @jedahu added folding.vim
authored
1 " # Code folding for Vim
2 "
3 " Add this code to your `~/.vimrc` to enable folding of Markdown files and
4 " source files with Markdown comments.
5 "
6 " The fold level is determined by the number of `#` at the start of each
7 " heading. Underlined headings are not supported.
2c38d2c @jedahu updated vim folding
authored
8 "
9 " Test code is also made foldable.
8713dce @jedahu added folding.vim
authored
10 function! MarkdownLevel(token)
f113a2c @jedahu hidden sections and indented comments and ;; instead of ; for clojure
authored
11 if 0 == empty(matchstr(getline(v:lnum), '^'.a:token.'<\(?\|\.\)'))
2c38d2c @jedahu updated vim folding
authored
12 return 'a1'
13 endif
f113a2c @jedahu hidden sections and indented comments and ;; instead of ; for clojure
authored
14 if 0 == empty(matchstr(getline(v:lnum), '^'.a:token.'\(?\|\.\)>'))
2c38d2c @jedahu updated vim folding
authored
15 return 's1'
16 endif
8713dce @jedahu added folding.vim
authored
17 let level = strlen(matchstr(getline(v:lnum),
2c38d2c @jedahu updated vim folding
authored
18 \ '\(^'.a:token.' \)\@<=#\+\( \)\@='))
8713dce @jedahu added folding.vim
authored
19 if level == 0
20 return "="
21 else
22 return ">".level
23 endif
24 endfunction
25
26 " Different comment tokens are supported.
27 let g:comment#none = ''
f113a2c @jedahu hidden sections and indented comments and ;; instead of ; for clojure
authored
28 let g:comment#semi = ';;'
2c38d2c @jedahu updated vim folding
authored
29 let g:comment#slashes = '//'
30 let g:comment#hash = '#'
31 let g:comment#dblquote = '"'
8713dce @jedahu added folding.vim
authored
32
33 au BufEnter *.md,*.clj,*.cljs,*.js,*.sh setlocal foldmethod=expr
34
35 " I don't know why passing a string literal to `MarkdownLevel` doesn't work
36 " here. Vimscript is not my forte.
37 au BufEnter *.md setlocal foldexpr=MarkdownLevel(comment#none)
38 au BufEnter *.clj,*.cljs setlocal foldexpr=MarkdownLevel(comment#semi)
39 au BufEnter *.js setlocal foldexpr=MarkdownLevel(comment#slashes)
40 au BufEnter *.sh setlocal foldexpr=MarkdownLevel(comment#hash)
41 au BufEnter *.vim setlocal foldexpr=MarkdownLevel(comment#dblquote)
42
e14be54 @jedahu formatting
authored
43
8713dce @jedahu added folding.vim
authored
44 " ## Key bindings
45 "
46 " `za` toggle current fold
47 " `zR` open all folds
48 " `zM` close all folds
49 " `zr` open one more level of folds
50 " `zm` close one more level of folds
Something went wrong with that request. Please try again.