3 " Maintainer: Zhou YiChao <broken.zhou@gmail.com>
4 " Created: Sat, 16 Feb 2002 16:50:19 +0100
5 " Last Change: Tue, 25 Sep 2011
6 " Last Update: 25th Sep 2002, by LH :
7 " (*) better support for the option
8 " (*) use some regex instead of several '||'.
9 " Oct 9th, 2003, by JT:
10 " (*) don't change indentation of lines starting with '%'
11 " 2005/06/15, Moshe Kaminsky <kaminsky@math.huji.ac.il>
13 " g:tex_items, g:tex_itemize_env, g:tex_noindent_env
14 " 2011/3/6, by Zhou YiChao <broken.zhou@gmail.com>
15 " (*) Don't change indentation of lines starting with '%'
16 " I don't see any code with '%' and it doesn't work properly
18 " (*) New features: Add smartindent-like indent for "{}" and "[]".
19 " (*) New variables: g:tex_indent_brace
20 " 2011/9/25, by Zhou Yichao <broken.zhou@gmail.com>
21 " (*) Bug fix: smartindent-like indent for "[]"
22 " (*) New features: Align with "&".
23 " (*) New variable: g:tex_indent_and
24 " 2011/10/23 by Zhou Yichao <broken.zhou@gmail.com>
25 " (*) Bug fix: improve the smartindent-like indent for "{}" and
32 " To set the following options (ok, currently it's just one), add a line like
33 " let g:tex_indent_items = 1
36 " * g:tex_indent_brace
38 " If this variable is unset or non-zero, it will use smartindent-like style
41 " * g:tex_indent_items
43 " If this variable is set, item-environments are indented like Emacs does
44 " it, i.e., continuation lines are indented with a shiftwidth.
46 " NOTE: I've already set the variable below; delete the corresponding line
47 " if you don't like this behaviour.
49 " Per default, it is unset.
52 " ----------------------------------------------------------------
53 " \begin{itemize} \begin{itemize}
54 " \item blablabla \item blablabla
55 " bla bla bla bla bla bla
56 " \item blablabla \item blablabla
57 " bla bla bla bla bla bla
58 " \end{itemize} \end{itemize}
63 " A list of tokens to be considered as commands for the beginning of an item
64 " command. The tokens should be separated with '\|'. The initial '\' should
65 " be escaped. The default is '\\bibitem\|\\item'.
69 " A list of environment names, separated with '\|', where the items (item
70 " commands matching g:tex_items) may appear. The default is
71 " 'itemize\|description\|enumerate\|thebibliography'.
73 " * g:tex_noindent_env
75 " A list of environment names. separated with '\|', where no indentation is
76 " required. The default is 'document\|verbatim'.
80 " If this variable is unset or zero, vim will try to align the line with first
81 " "&". This is pretty useful when you use environment like table or align.
82 " Note that this feature need to search back some line, so vim may become
87 if exists("b:did_indent")
92 " Delete the next line to avoid the special indention of items
93 if !exists("g:tex_indent_items")
94 let g:tex_indent_items = 1
96 if !exists("g:tex_indent_brace")
97 let g:tex_indent_brace = 1
99 if !exists("g:tex_indent_and")
100 let g:tex_indent_and = 1
102 if g:tex_indent_items
103 if !exists("g:tex_itemize_env")
104 let g:tex_itemize_env = 'itemize\|description\|enumerate\|thebibliography'
106 if !exists('g:tex_items')
107 let g:tex_items = '\\bibitem\|\\item'
113 if !exists("g:tex_noindent_env")
114 let g:tex_noindent_env = 'document\|verbatim\|lstlisting'
118 setlocal nosmartindent
119 setlocal indentexpr=GetTeXIndent()
120 exec 'setlocal indentkeys+=},],\&' . substitute(g:tex_items, '^\|\(\\|\)', ',=', 'g')
121 let g:tex_items = '^\s*' . g:tex_items
124 " Only define the function once
125 if exists("*GetTeXIndent") | finish
129 function GetTeXIndent()
130 " Find a non-blank line above the current line.
131 let lnum = prevnonblank(v:lnum - 1)
133 " Comment line is not what we need.
134 while lnum != 0 && getline(lnum) =~ '^\s*%'
135 let lnum = prevnonblank(lnum - 1)
138 " At the start of the file use zero indent.
143 let line = getline(lnum) " last line
144 let cline = getline(v:lnum) " current line
146 " You want to align with "&"
148 " Align with last line if last line has a "&"
149 if stridx(cline, "&") != -1 && stridx(line, "&") != -1
150 return indent(v:lnum) + stridx(line, "&") - stridx(cline, "&")
153 " set line & lnum to the line which doesn't contain "&"
154 while lnum != 0 && (stridx(line, "&") != -1 || line =~ '^\s*%')
155 let lnum = prevnonblank(lnum - 1)
156 let line = getline(lnum)
165 let ind = indent(lnum)
167 " New code for comment: retain the indent of current line
169 return indent(v:lnum)
172 " Add a 'shiftwidth' after beginning of environments.
173 " Don't add it for \begin{document} and \begin{verbatim}
174 ""if line =~ '^\s*\\begin{\(.*\)}' && line !~ 'verbatim'
175 " LH modification : \begin does not always start a line
176 " ZYC modification : \end after \begin won't cause wrong indent anymore
177 if line =~ '\\begin{.*}' && line !~ g:tex_noindent_env
178 \ && line !~ '\\begin{.\{-}}.*\\end{.*}'
182 if g:tex_indent_items
183 " Add another sw for item-environments
184 if line =~ g:tex_itemize_env
191 " Subtract a 'shiftwidth' when an environment ends
192 if cline =~ '^\s*\\end' && cline !~ g:tex_noindent_env
194 if g:tex_indent_items
195 " Remove another sw for item-environments
196 if cline =~ g:tex_itemize_env
204 if g:tex_indent_brace
205 " Add a 'shiftwidth' after a "{" or "[".
207 for i in range(0, strlen(line)-1)
208 if line[i] == "}" || line[i] == "]"
209 let sum1 = max([0, sum1-1])
211 if line[i] == "{" || line[i] == "["
217 for i in reverse(range(0, strlen(cline)-1))
218 if cline[i] == "{" || cline[i] == "["
219 let sum2 = max([0, sum2-1])
221 if cline[i] == "}" || cline[i] == "]"
226 let ind += (sum1 - sum2) * &sw
230 " Special treatment for 'item'
231 " ----------------------------
233 if g:tex_indent_items
235 " '\item' or '\bibitem' itself:
236 if cline =~ g:tex_items
240 " lines following to '\item' are intented once again:
241 if line =~ g:tex_items
250 " vim: set sw=4 textwidth=80: