forked from tidalcycles/vim-tidal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tidal.vim
110 lines (92 loc) · 3.14 KB
/
tidal.vim
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
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Functions
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
let s:not_prefixable_keywords = [ "import", "data", "instance", "class", "{-#", "type", "case", "do", "let", "default", "foreign", "--"]
" guess correct number of spaces to indent
" (tabs are not allowed)
function! Get_indent_string()
return repeat(" ", 4)
endfunction
" replace tabs by spaces
function! Tab_to_spaces(text)
return substitute(a:text, " ", Get_indent_string(), "g")
endfunction
" Check if line is commented out
function! Is_comment(line)
return (match(a:line, "^[ \t]*--.*") >= 0)
endfunction
" Remove commented out lines
function! Remove_line_comments(lines)
let l:i = 0
let l:len = len(a:lines)
let l:ret = []
while l:i < l:len
if !Is_comment(a:lines[l:i])
call add(l:ret, a:lines[l:i])
endif
let l:i += 1
endwhile
return l:ret
endfunction
" remove block comments
function! Remove_block_comments(text)
return substitute(a:text, "{-.*-}", "", "g")
endfunction
" remove line comments
" todo: fix this! it only removes one occurence whilst it should remove all.
" function! Remove_line_comments(text)
" return substitute(a:text, "^[ \t]*--[^\n]*\n", "", "g")
" endfunction
" Wrap in :{ :} if there's more than one line
function! Wrap_if_multi(lines)
if len(a:lines) > 1
return [":{"] + a:lines + [":}"]
else
return a:lines
endif
endfunction
" change string into array of lines
function! Lines(text)
return split(a:text, "\n")
endfunction
" change lines back into text
function! Unlines(lines)
return join(a:lines, "\n") . "\n"
endfunction
" vim slime handler
function! _EscapeText_tidal(text)
let l:text = Remove_block_comments(a:text)
let l:lines = Lines(Tab_to_spaces(l:text))
let l:lines = Remove_line_comments(l:lines)
let l:lines = Wrap_if_multi(l:lines)
return Unlines(l:lines)
endfunction
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Mappings
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
if !exists("g:tidal_no_mappings") || !g:tidal_no_mappings
if !hasmapto('<Plug>TidalConfig', 'n')
nmap <buffer> <localleader>c <Plug>TidalConfig
endif
if !hasmapto('<Plug>TidalRegionSend', 'x')
xmap <buffer> <localleader>s <Plug>TidalRegionSend
xmap <buffer> <c-e> <Plug>TidalRegionSend
endif
if !hasmapto('<Plug>TidalLineSend', 'n')
nmap <buffer> <localleader>s <Plug>TidalLineSend
endif
if !hasmapto('<Plug>TidalParagraphSend', 'n')
nmap <buffer> <localleader>ss <Plug>TidalParagraphSend
nmap <buffer> <c-e> <Plug>TidalParagraphSend
endif
imap <buffer> <c-e> <Esc><Plug>TidalParagraphSend<Esc>i<Right>
nnoremap <buffer> <localleader>h :TidalHush<cr>
nnoremap <buffer> <c-h> :TidalHush<cr>
let i = 1
while i <= 9
execute 'nnoremap <buffer> <localleader>'.i.' :TidalSilence '.i.'<cr>'
execute 'nnoremap <buffer> <c-'.i.'> :TidalSilence '.i.'<cr>'
execute 'nnoremap <buffer> <localleader>s'.i.' :TidalPlay '.i.'<cr>'
let i += 1
endwhile
endif