robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
ae7ab90159
master
1" Vim indent file 2" Language: LaTeX 3" Maintainer: Yichao Zhou <broken.zhou AT gmail.com> 4" Created: Sat, 16 Feb 2002 16:50:19 +0100 5" Version: 1.0.0 6" Please email me if you found something I can do. Comments, bug report and 7" feature request are welcome. 8 9" Last Update: {{{ 10" 25th Sep 2002, by LH : 11" (*) better support for the option 12" (*) use some regex instead of several '||'. 13" Oct 9th, 2003, by JT: 14" (*) don't change indentation of lines starting with '%' 15" 2005/06/15, Moshe Kaminsky <kaminsky AT math.huji.ac.il> 16" (*) New variables: 17" g:tex_items, g:tex_itemize_env, g:tex_noindent_env 18" 2011/3/6, by Yichao Zhou <broken.zhou AT gmail.com> 19" (*) Don't change indentation of lines starting with '%' 20" I don't see any code with '%' and it doesn't work properly 21" so I add some code. 22" (*) New features: Add smartindent-like indent for "{}" and "[]". 23" (*) New variables: g:tex_indent_brace 24" 2011/9/25, by Yichao Zhou <broken.zhou AT gmail.com> 25" (*) Bug fix: smartindent-like indent for "[]" 26" (*) New features: Align with "&". 27" (*) New variable: g:tex_indent_and. 28" 2011/10/23 by Yichao Zhou <broken.zhou AT gmail.com> 29" (*) Bug fix: improve the smartindent-like indent for "{}" and 30" "[]". 31" 2012/02/27 by Yichao Zhou <broken.zhou AT gmail.com> 32" (*) Bug fix: support default folding marker. 33" (*) Indent with "&" is not very handy. Make it not enable by 34" default. 35" 2012/03/06 by Yichao Zhou <broken.zhou AT gmail.com> 36" (*) Modify "&" behavior and make it default again. Now "&" 37" won't align when there are more then one "&" in the previous 38" line. 39" (*) Add indent "\left(" and "\right)" 40" (*) Trust user when in "verbatim" and "lstlisting" 41" 2012/03/11 by Yichao Zhou <broken.zhou AT gmail.com> 42" (*) Modify "&" so that only indent when current line start with 43" "&". 44" 2012/03/12 by Yichao Zhou <broken.zhou AT gmail.com> 45" (*) Modify indentkeys. 46" 2012/03/18 by Yichao Zhou <broken.zhou AT gmail.com> 47" (*) Add &cpo 48" 2013/05/02 by Yichao Zhou <broken.zhou AT gmail.com> 49" (*) Fix problem about GetTeXIndent checker. Thank Albert Netymk 50" for reporting this. 51" 2014/06/23 by Yichao Zhou <broken.zhou AT gmail.com> 52" (*) Remove the feature g:tex_indent_and because it is buggy. 53" (*) If there is not any obvious indentation hints, we do not 54" alert our user's current indentation. 55" (*) g:tex_indent_brace now only works if the open brace is the 56" last character of that line. 57" 2014/08/03 by Yichao Zhou <broken.zhou AT gmail.com> 58" (*) Indent current line if last line has larger indentation 59" 2016/11/08 by Yichao Zhou <broken.zhou AT gmail.com> 60" (*) Fix problems for \[ and \]. Thanks Bruno for reporting. 61" 2017/04/30 by Yichao Zhou <broken.zhou AT gmail.com> 62" (*) Fix a bug between g:tex_noindent_env and g:tex_indent_items 63" Now g:tex_noindent_env='document\|verbatim\|itemize' (Emacs 64" style) is supported. Thanks Miles Wheeler for reporting. 65" 2018/02/07 by Yichao Zhou <broken.zhou AT gmail.com> 66" (*) Make indentation more smart in the normal mode 67" 2020/04/26 by Yichao Zhou <broken.zhou AT gmail.com> 68" (*) Fix a bug related to \[ & \]. Thanks Manuel Boni for 69" reporting. 70" 2023/08/28 by Vim Project 71" (*) Set b:undo_indent. 72" }}} 73 74" Document: {{{ 75" 76" For proper latex experience, please put 77" let g:tex_flavor = "latex" 78" into your vimrc. 79" 80" * g:tex_indent_brace 81" 82" If this variable is unset or non-zero, it will use smartindent-like style 83" for "{}" and "[]". Now this only works if the open brace is the last 84" character of that line. 85" 86" % Example 1 87" \usetikzlibrary{ 88" external 89" } 90" 91" % Example 2 92" \tikzexternalize[ 93" prefix=tikz] 94" 95" * g:tex_indent_items 96" 97" If this variable is set, item-environments are indented like Emacs does 98" it, i.e., continuation lines are indented with a shiftwidth. 99" 100" set unset 101" ------------------------------------------------------ 102" \begin{itemize} \begin{itemize} 103" \item blablabla \item blablabla 104" bla bla bla bla bla bla 105" \item blablabla \item blablabla 106" bla bla bla bla bla bla 107" \end{itemize} \end{itemize} 108" 109" 110" * g:tex_items 111" 112" A list of tokens to be considered as commands for the beginning of an item 113" command. The tokens should be separated with '\|'. The initial '\' should 114" be escaped. The default is '\\bibitem\|\\item'. 115" 116" * g:tex_itemize_env 117" 118" A list of environment names, separated with '\|', where the items (item 119" commands matching g:tex_items) may appear. The default is 120" 'itemize\|description\|enumerate\|thebibliography'. 121" 122" * g:tex_noindent_env 123" 124" A list of environment names. separated with '\|', where no indentation is 125" required. The default is 'document\|verbatim'. 126" }}} 127 128" Only define the function once 129if exists ( "b:did_indent" ) 130finish 131endif 132 133let s: cpo_save = & cpo 134set cpo & vim 135 136" Define global variable {{{ 137 138let b: did_indent = 1 139 140if ! exists ( "g:tex_indent_items" ) 141let g: tex_indent_items = 1 142endif 143if ! exists ( "g:tex_indent_brace" ) 144let g: tex_indent_brace = 1 145endif 146if ! exists ( "g:tex_max_scan_line" ) 147let g: tex_max_scan_line = 60 148endif 149if g: tex_indent_items 150if ! exists ( "g:tex_itemize_env" ) 151let g: tex_itemize_env = 'itemize\|description\|enumerate\|thebibliography' 152endif 153if ! exists ( 'g:tex_items' ) 154let g: tex_items = '\\bibitem\|\\item' 155endif 156else 157let g: tex_items = '' 158endif 159 160if ! exists ( "g:tex_noindent_env" ) 161let g: tex_noindent_env = 'document\|verbatim\|lstlisting' 162endif "}}} 163 164" VIM Setting " {{{ 165setlocal autoindent 166setlocal nosmartindent 167setlocal indentexpr = GetTeXIndent () 168setlocal indentkeys & 169exec 'setlocal indentkeys+=[,(,{,),},],\&' . substitute ( g: tex_items , '^\|\(\\|\)' , ',=' , 'g' ) 170let g: tex_items = '^\s*' . substitute ( g: tex_items , '^\(\^\\s\*\)*' , '' , '' ) 171let b: undo_indent = "setlocal autoindent< indentexpr< indentkeys< smartindent<" 172" }}} 173 174function ! GetTeXIndent () " {{{ 175" Find a non-blank line above the current line. 176let lnum = prevnonblank ( v: lnum - 1 ) 177let cnum = v: lnum 178 179" Comment line is not what we need. 180while lnum != 0 && getline ( lnum ) =~ '^\s*%' 181let lnum = prevnonblank ( lnum - 1 ) 182endwhile 183 184" At the start of the file use zero indent. 185if lnum == 0 186return 0 187endif 188 189let line = substitute ( getline ( lnum ), '\s*%.*' , '' , 'g' ) " last line 190let cline = substitute ( getline ( v: lnum ), '\s*%.*' , '' , 'g' ) " current line 191 192let ccol = 1 193while cline [ ccol ] =~ '\s' 194let ccol += 1 195endwhile 196 197" We are in verbatim, so do what our user what. 198if synIDattr ( synID ( v: lnum , ccol , 1 ), "name" ) == "texZone" 199if empty ( cline ) 200return indent ( lnum ) 201else 202return indent ( v: lnum ) 203endif 204endif 205 206if lnum == 0 207return 0 208endif 209 210let ind = indent ( lnum ) 211let stay = 1 212 213" New code for comment: retain the indent of current line 214if cline =~ '^\s*%' 215return indent ( v: lnum ) 216endif 217 218" Add a 'shiftwidth' after beginning of environments. 219" Don't add it for \begin{document} and \begin{verbatim} 220" if line =~ '^\s*\\begin{\(.*\)}' && line !~ 'verbatim' 221" LH modification : \begin does not always start a line 222" ZYC modification : \end after \begin won't cause wrong indent anymore 223if line =~ '\\begin{.*}' 224if line !~ g: tex_noindent_env 225let ind = ind + shiftwidth () 226let stay = 0 227endif 228 229if g: tex_indent_items 230" Add another sw for item-environments 231if line =~ g: tex_itemize_env 232let ind = ind + shiftwidth () 233let stay = 0 234endif 235endif 236endif 237 238if cline =~ '\\end{.*}' 239let retn = s: GetEndIndentation ( v: lnum ) 240if retn != -1 241return retn 242endif 243end 244" Subtract a 'shiftwidth' when an environment ends 245if cline =~ '\\end{.*}' 246 \&& cline !~ g: tex_noindent_env 247 \&& cline !~ '\\begin{.*}.*\\end{.*}' 248if g: tex_indent_items 249" Remove another sw for item-environments 250if cline =~ g: tex_itemize_env 251let ind = ind - shiftwidth () 252let stay = 0 253endif 254endif 255 256let ind = ind - shiftwidth () 257let stay = 0 258endif 259 260if g: tex_indent_brace 261if line =~ '[[{]$' 262let ind += shiftwidth () 263let stay = 0 264endif 265 266if cline =~ '^\s*\\\?[\]}]' && s: CheckPairedIsLastCharacter ( v: lnum , ccol ) 267let ind -= shiftwidth () 268let stay = 0 269endif 270 271if line !~ '^\s*\\\?[\]}]' 272for i in range ( 1 , strlen ( line ) - 1 ) 273let char = line [ i ] 274if char == ']' || char == '}' 275if s: CheckPairedIsLastCharacter ( lnum , i ) 276let ind -= shiftwidth () 277let stay = 0 278endif 279endif 280endfor 281endif 282endif 283 284" Special treatment for 'item' 285" ---------------------------- 286 287if g: tex_indent_items 288" '\item' or '\bibitem' itself: 289if cline =~ g: tex_items 290let ind = ind - shiftwidth () 291let stay = 0 292endif 293" lines following to '\item' are indented once again: 294if line =~ g: tex_items 295let ind = ind + shiftwidth () 296let stay = 0 297endif 298endif 299 300if stay && mode () == 'i' 301" If there is no obvious indentation hint, and indentation is triggered 302" in insert mode, we trust our user. 303if empty ( cline ) 304return ind 305else 306return max ([ indent ( v: lnum ), s: GetLastBeginIndentation ( v: lnum )]) 307endif 308else 309return ind 310endif 311endfunction "}}} 312 313function ! s: GetLastBeginIndentation ( lnum ) " {{{ 314let matchend = 1 315for lnum in range ( a: lnum - 1 , max ([ a: lnum - g: tex_max_scan_line , 1 ]), -1 ) 316let line = getline ( lnum ) 317if line =~ '\\end{.*}' 318let matchend += 1 319endif 320if line =~ '\\begin{.*}' 321let matchend -= 1 322endif 323if matchend == 0 324if line =~ g: tex_noindent_env 325return indent ( lnum ) 326endif 327if line =~ g: tex_itemize_env 328return indent ( lnum ) + 2 * shiftwidth () 329endif 330return indent ( lnum ) + shiftwidth () 331endif 332endfor 333return -1 334endfunction 335 336function ! s: GetEndIndentation ( lnum ) " {{{ 337if getline ( a: lnum ) =~ '\\begin{.*}.*\\end{.*}' 338return -1 339endif 340 341let min_indent = 100 342let matchend = 1 343for lnum in range ( a: lnum - 1 , max ([ a: lnum - g: tex_max_scan_line , 1 ]), -1 ) 344let line = getline ( lnum ) 345if line =~ '\\end{.*}' 346let matchend += 1 347endif 348if line =~ '\\begin{.*}' 349let matchend -= 1 350endif 351if matchend == 0 352return indent ( lnum ) 353endif 354if ! empty ( line ) 355let min_indent = min ([ min_indent , indent ( lnum )]) 356endif 357endfor 358return min_indent - shiftwidth () 359endfunction 360 361" Most of the code is from matchparen.vim 362function ! s: CheckPairedIsLastCharacter ( lnum , col ) "{{{ 363let c_lnum = a: lnum 364let c_col = a: col + 1 365 366let line = getline ( c_lnum ) 367if line [ c_col - 1 ] == '\' 368let c_col = c_col + 1 369endif 370let c = line [ c_col - 1 ] 371 372let plist = split ( & matchpairs , '.\zs[:,]' ) 373let i = index ( plist , c ) 374if i < 0 375return 0 376endif 377 378" Figure out the arguments for searchpairpos(). 379if i % 2 == 0 380let s_flags = 'nW' 381let c2 = plist [ i + 1 ] 382else 383let s_flags = 'nbW' 384let c2 = c 385let c = plist [ i - 1 ] 386endif 387if c == '[' 388let c = '\[' 389let c2 = '\]' 390endif 391 392" Find the match. When it was just before the cursor move it there for a 393" moment. 394let save_cursor = winsaveview () 395call cursor ( c_lnum , c_col ) 396 397" When not in a string or comment ignore matches inside them. 398" We match "escape" for special items, such as lispEscapeSpecial. 399let s_skip = 'synIDattr(synID(line("."), col("."), 0), "name") ' . 400 \'=~? "string\\|character\\|singlequote\\|escape\\|comment"' 401execute 'if' s_skip '| let s_skip = 0 | endif' 402 403let stopline = max ([ 0 , c_lnum - g: tex_max_scan_line ]) 404 405" Limit the search time to 300 msec to avoid a hang on very long lines. 406" This fails when a timeout is not supported. 407try 408let [ m_lnum , m_col ] = searchpairpos ( c , '' , c2 , s_flags , s_skip , stopline , 100 ) 409catch /E118/ 410endtry 411 412call winrestview ( save_cursor ) 413 414if m_lnum > 0 415let line = getline ( m_lnum ) 416return strlen ( line ) == m_col 417endif 418 419return 0 420endfunction "}}} 421 422let & cpo = s: cpo_save 423unlet s: cpo_save 424 425" vim: set sw=4 textwidth=80: