summaryrefslogtreecommitdiffstats
path: root/ftplugin/mustache.vim
blob: 61b5896dc2b4fe6f7c7562fed4552d53c9bbba22 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
if exists('g:loaded_mustache_handlebars') && g:loaded_mustache_handlebars
  finish
endif
let g:loaded_mustache_handlebars = 1

let s:cpo_save = &cpo
set cpo&vim

" Matchit support for Mustache & Handlebars
" extending HTML matchit groups
if exists("loaded_matchit") && exists("b:match_words")
  let b:match_words = b:match_words
  \ . ',{:},[:],(:),'
  \ . '\%({{\)\@<=#\s*\%(if\|unless\)\s*.\{-}}}'
  \ . ':'
  \ . '\%({{\)\@<=\s*else\s*}}'
  \ . ':'
  \ . '\%({{\)\@<=/\s*\%(if\|unless\)\s*}},'
  \ . '\%({{\)\@<=[#^]\s*\([-0-9a-zA-Z_?!/.]\+\).\{-}}}'
  \ . ':'
  \ . '\%({{\)\@<=/\s*\1\s*}}'
endif

if exists("g:mustache_abbreviations")
  inoremap <buffer> {{{ {{{}}}<left><left><left>
  inoremap <buffer> {{ {{}}<left><left>
  inoremap <buffer> {{! {{!}}<left><left>
  inoremap <buffer> {{< {{<}}<left><left>
  inoremap <buffer> {{> {{>}}<left><left>
  inoremap <buffer> {{# {{#}}<cr>{{/}}<up><left><left>
  inoremap <buffer> {{if {{#if }}<cr>{{/if}}<up><left>
  inoremap <buffer> {{ife {{#if }}<cr>{{else}}<cr>{{/if}}<up><up><left>
endif


" Section movement
" Adapted from vim-ruby - many thanks to the maintainers of that plugin

function! s:sectionmovement(pattern,flags,mode,count)
  norm! m'
  if a:mode ==# 'v'
    norm! gv
  endif
  let i = 0
  while i < a:count
    let i = i + 1
    " saving current position
    let line = line('.')
    let col  = col('.')
    let pos = search(a:pattern,'W'.a:flags)
    " if there's no more matches, return to last position
    if pos == 0
      call cursor(line,col)
      return
    endif
  endwhile
endfunction

nnoremap <silent> <buffer> [[ :<C-U>call <SID>sectionmovement('{{','b','n',v:count1)<CR>
nnoremap <silent> <buffer> ]] :<C-U>call <SID>sectionmovement('{{','' ,'n',v:count1)<CR>
xnoremap <silent> <buffer> [[ :<C-U>call <SID>sectionmovement('{{','b','v',v:count1)<CR>
xnoremap <silent> <buffer> ]] :<C-U>call <SID>sectionmovement('{{','' ,'v',v:count1)<CR>


" Operator pending mappings

" Operators are available by default. Set `let g:mustache_operators = 0` in
" your .vimrc to disable them.
if ! exists("g:mustache_operators")
  let g:mustache_operators = 1
endif

if exists("g:mustache_operators") && g:mustache_operators
  onoremap <silent> <buffer> ie :<C-U>call <SID>wrap_inside()<CR>
  onoremap <silent> <buffer> ae :<C-U>call <SID>wrap_around()<CR>
  xnoremap <silent> <buffer> ie :<C-U>call <SID>wrap_inside()<CR>
  xnoremap <silent> <buffer> ae :<C-U>call <SID>wrap_around()<CR>
endif

function! s:wrap_around()
  " If the cursor is at the end of the tag element, move back
  " so that the end tag can be detected.
  while getline('.')[col('.')-1] ==# '}'
    execute 'norm h'
  endwhile

  " Moves to the end of the closing tag
  let pos = search('}}','We')
  if pos != 0
    if getline('.')[col('.')] ==# '}'
      " Ending tag contains 3 closing brackets '}}}',
      " move to the last bracket.
      execute 'norm l'
    endif

    " select the whole tag
    execute 'norm v%'
  endif
endfunction

function! s:wrap_inside()
  " If the cursor is at the end of the tag element, move back
  " so that the end tag can be detected.
  while getline('.')[col('.')-1] ==# '}'
    execute 'norm h'
  endwhile

  " Moves to the end of the closing tag
  let pos = search('}}','W')
  if pos != 0
    " select only inside the tag
    execute 'norm v%loho'
  endif
endfunction


let &cpo = s:cpo_save
unlet s:cpo_save

" vim: nofoldenable