/vim/ftplugin/latex-suite/smartspace.vim
Vim Script | 102 lines | 61 code | 8 blank | 33 comment | 23 complexity | 5bbfd407ee08588e2b1ec4f364d7397c MD5 | raw file
1"=============================================================================
2" File: smartspace.vim
3" Author: Carl Muller
4" Created: Fri Dec 06 12:00 AM 2002 PST
5"
6" Description:
7" Maps the <space> key in insert mode so that mathematical formulaes are
8" always kept on the same line. i.e, $$'s dont get broken across multiple
9" lines.
10"=============================================================================
11
12" Avoid reinclusion or if the user doesn't want us.
13if exists('b:done_smartspace')
14 \ || (exists('g:Tex_SmartKeySpace') && !g:Tex_SmartKeySpace)
15 finish
16endif
17let b:done_smartspace = 1
18
19" Smart space relies on taking over vim's insertion of carriage returns in
20" order to keep $$'s on the same line. The only way to get vim not to break
21" lines is to set tw=0.
22"
23" NOTE: setting tw != 0 will break smartspace
24" the user's 'tw' setting is still respected in the insert mode.
25" However, normal mode actions which rely on 'tw' such as gqap will be
26" broken because of the faulty 'tw' setting.
27let b:tw = &l:tw
28setlocal tw=0
29
30inoremap <buffer> <silent> <Space> <Space><Esc>:call <SID>TexFill(b:tw)<CR>a
31
32" Do not redefine the function.
33if exists('*s:TexFill')
34 finish
35endif
36
37" TexFormatLine: format line retaining $$'s on the same line. {{{
38function! s:TexFill(width)
39 if a:width != 0 && col(".") > a:width
40 " For future use, record the current line and the number of the current column
41 let current_line = getline(".")
42 let current_column = col(".")
43 exe "normal! a##\<Esc>"
44 call <SID>TexFormatLine(a:width,current_line,current_column)
45 exe "normal! ?##\<CR>2s\<Esc>"
46 " Remove ## from the search history.
47 call histdel("/", -1)|let @/=histget("/", -1)
48 endif
49endfunction
50
51" }}}
52function! s:TexFormatLine(width, current_line, current_column) " {{{
53 " get the first non-blank character.
54 let first = matchstr(getline('.'), '\S')
55 normal! $
56 let length = col('.')
57 let go = 1
58 while length > a:width+2 && go
59 let between = 0
60 let string = strpart(getline('.'), 0, a:width)
61 " Count the dollar signs
62 let number_of_dollars = 0
63 let evendollars = 1
64 let counter = 0
65 while counter <= a:width-1
66 " Pay attention to '$$'.
67 if string[counter] == '$' && string[counter-1] != '$'
68 let evendollars = 1 - evendollars
69 let number_of_dollars = number_of_dollars + 1
70 endif
71 let counter = counter + 1
72 endwhile
73 " Get ready to split the line.
74 exe 'normal! ' . (a:width + 1) . '|'
75 if evendollars
76 " Then you are not between dollars.
77 exe "normal! ?\\$\\+\\| \<CR>W"
78 else
79 " Then you are between dollars.
80 normal! F$
81 if col(".") == 1 || getline('.')[col(".")-1] != "$"
82 let go = 0
83 endif
84 endif
85 if first == '$' && number_of_dollars == 1
86 let go = 0
87 else
88 exe "normal! i\<CR>\<Esc>$"
89 " get the first non-blank character.
90 let first = matchstr(getline('.'), '\S')
91 endif
92 let length = col(".")
93 endwhile
94 if go == 0 && strpart(a:current_line, 0, a:current_column) =~ '.*\$.*\$.*'
95 exe "normal! ^f$a\<CR>\<Esc>"
96 call <SID>TexFormatLine(a:width, a:current_line, a:current_column)
97 endif
98endfunction
99
100" }}}
101
102" vim:fdm=marker:ts=4:sw=4:noet