1 " Vim filetype plugin file
3 " Maintainer: Tim Allen <screwtape@froup.com>
4 " Last Change: 2011 Apr 05
6 " Only do this when not done yet for this buffer
7 if exists("b:did_ftplugin")
10 let b:did_ftplugin = 1
12 " Code formatting settings loosely adapted from:
13 " http://concatenative.org/wiki/view/Factor/Coding%20Style
15 " Tabs are not allowed in Factor source files; use four spaces
17 setlocal expandtab tabstop=4 shiftwidth=4 softtabstop=4
19 " Try to limit lines to 64 characters.
20 setlocal textwidth=64 colorcolumn=+1
22 " Teach Vim what comments look like.
23 setlocal comments=b:!,f:#!
24 setlocal commentstring=!\ %s
26 " Make all of these characters part of a word (useful for
27 " skipping over words with w, e, and b)
28 setlocal iskeyword=33-126,128-255
31 let b:match_words = '\<<PRIVATE\>:\<PRIVATE>\>'
33 " Insert closing brackets and quotes, spaces, stack effects ...
34 " examples ("|" is the position of the cursor):
36 " [|] + <Space> → [ | ]
40 " (|) + <Space> → ( |-- )
43 if exists("g:EnableFactorAutopairs") &&
44 \g:EnableFactorAutopairs == 1
46 function s:RemoveTrailingSpaces()
47 let save_view = winsaveview()
49 call winrestview(save_view)
52 au BufWrite <buffer> :call s:RemoveTrailingSpaces()
54 function s:Insert(before, after)
55 return a:before .. a:after ..
56 \repeat("\<C-G>U\<Left>", strlen(a:after))
60 return getline('.')[col('.') - 1] != ' ' ?
61 \s:Insert('', ' ') : ''
65 return strpart(getline('.'), col('.') - 2, 2)
68 function s:WiderContext()
69 return strpart(getline('.'), col('.') - 3, 4)
72 function s:OpenParenthesis()
73 return (s:Context() != '()' ? s:PadAfter() : '') ..
77 function s:OpenBracket()
78 let context = s:Context()
80 \context != '==' && context != '[]' ? s:PadAfter() : ''
81 \) .. s:Insert('[', ']')
84 function s:OpenBrace()
85 return s:PadAfter() .. s:Insert('{', '}')
89 let context = s:Context()
90 if context == '[]' || context == '=='
91 return s:Insert('=', '=')
93 return s:Insert('=', '')
98 return s:Context() == '""' ? '' :
99 \(s:PadAfter() .. s:Insert('"', '"'))
103 let context = s:Context()
104 let widercontext = s:WiderContext()
105 if context == '[]' || context == '{}' ||
106 \widercontext == '[ ]' || widercontext == '{ }'
107 return "\<CR>\<C-O>O"
113 function s:Backspace()
114 let context = s:Context()
115 let widercontext = s:WiderContext()
116 if widercontext == '[ ]' || widercontext == '( )' ||
117 \widercontext == '{ }' || context == '""' ||
118 \context == '==' || context == '()' ||
119 \context == '[]' || context == '{}'
127 let context = s:Context()
128 if context == '[]' || context == '{}' ||
129 \s:WiderContext() == '(())' ||
130 \strpart(getline('.'), col('.') - 5, 5) == ':> ()'
131 return s:Insert(' ', ' ')
132 elseif context == '()'
133 return s:Insert(' ', '-- ')
135 return s:Insert(' ', '')
139 inoremap <buffer> <expr> ( <SID>OpenParenthesis()
140 inoremap <buffer> <expr> [ <SID>OpenBracket()
141 inoremap <buffer> <expr> { <SID>OpenBrace()
142 inoremap <buffer> <expr> = <SID>Equal()
143 inoremap <buffer> <expr> " <SID>Quote()
144 inoremap <buffer> <expr> <CR> <SID>Return()
145 inoremap <buffer> <expr> <BS> <SID>Backspace()
146 inoremap <buffer> <expr> <Space> <SID>Space()