]> gitweb.factorcode.org Git - factor.git/blob - misc/vim/ftplugin/factor.vim
ui.listener: document that ~/.factor-history persists input history
[factor.git] / misc / vim / ftplugin / factor.vim
1 " Vim filetype plugin file
2 " Language: Factor
3 " Maintainer: Tim Allen <screwtape@froup.com>
4 " Last Change: 2011 Apr 05
5
6 " Only do this when not done yet for this buffer
7 if exists("b:did_ftplugin")
8   finish
9 endif
10 let b:did_ftplugin = 1
11
12 " Code formatting settings loosely adapted from:
13 " http://concatenative.org/wiki/view/Factor/Coding%20Style
14
15 " Tabs are not allowed in Factor source files; use four spaces
16 " instead.
17 setlocal expandtab tabstop=4 shiftwidth=4 softtabstop=4
18
19 " Try to limit lines to 64 characters.
20 setlocal textwidth=64 colorcolumn=+1
21
22 " Teach Vim what comments look like.
23 setlocal comments=b:!,f:#!
24 setlocal commentstring=!\ %s
25
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
29
30 " matchit patterns
31 let b:match_words = '\<<PRIVATE\>:\<PRIVATE>\>'
32
33 " Insert closing brackets and quotes, spaces, stack effects ...
34 " examples ("|" is the position of the cursor):
35 " |if + [ → [|] if
36 " [|] + <Space> → [ | ]
37 " [|] + <CR> → [
38 "     |
39 " ]
40 " (|) + <Space> → ( |-- )
41 " [|] + = → [=|=]
42 " [|] + <BS> → |
43 if exists("g:EnableFactorAutopairs") &&
44       \g:EnableFactorAutopairs == 1
45
46   function s:RemoveTrailingSpaces()
47     let save_view = winsaveview()
48     %s/ \+$//e
49     call winrestview(save_view)
50   endfunction
51
52   au BufWrite <buffer> :call s:RemoveTrailingSpaces()
53
54   function s:Insert(before, after)
55     return a:before .. a:after ..
56           \repeat("\<C-G>U\<Left>", strlen(a:after))
57   endfunction
58
59   function s:PadAfter()
60     return getline('.')[col('.') - 1] != ' ' ?
61           \s:Insert('', ' ') : ''
62   endfunction
63
64   function s:Context()
65     return strpart(getline('.'), col('.') - 2, 2)
66   endfunction
67
68   function s:WiderContext()
69     return strpart(getline('.'), col('.') - 3, 4)
70   endfunction
71
72   function s:OpenParenthesis()
73     return (s:Context() != '()' ? s:PadAfter() : '') ..
74           \s:Insert('(', ')')
75   endfunction
76
77   function s:OpenBracket()
78     let context = s:Context()
79     return (
80           \context != '==' && context != '[]' ? s:PadAfter() : ''
81           \) .. s:Insert('[', ']')
82   endfunction
83
84   function s:OpenBrace()
85     return s:PadAfter() .. s:Insert('{', '}')
86   endfunction
87
88   function s:Equal()
89     let context = s:Context()
90     if context == '[]' || context == '=='
91       return s:Insert('=', '=')
92     else
93       return s:Insert('=', '')
94     endif
95   endfunction
96
97   function s:Quote()
98     return s:Context() == '""' ? '' :
99           \(s:PadAfter() .. s:Insert('"', '"'))
100   endfunction
101
102   function s:Return()
103     let context = s:Context()
104     let widercontext = s:WiderContext()
105     if context == '[]' || context == '{}' ||
106           \widercontext == '[  ]' || widercontext == '{  }'
107       return "\<CR>\<C-O>O"
108     else
109       return "\<CR>"
110     endif
111   endfunction
112
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 == '{}'
120       return "\<Del>\<BS>"
121     else
122       return "\<BS>"
123     endif
124   endfunction
125
126   function s:Space()
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(' ', '-- ')
134     else
135       return s:Insert(' ', '')
136     endif
137   endfunction
138
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()
147
148 endif
149
150 " vim:sw=2:et: