aboutsummaryrefslogblamecommitdiff
path: root/dot_vim/ftplugin/latex-suite/compiler.vim
blob: 245f6cd00ce4ff0967a6985e1abeab55b4fe9a1a (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820



















































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                                           
"=============================================================================
" 	     File: compiler.vim
"      Author: Srinath Avadhanula
"     Created: Tue Apr 23 05:00 PM 2002 PST
" 
"  Description: functions for compiling/viewing/searching latex documents
"          CVS: $Id: compiler.vim 997 2006-03-20 09:45:45Z srinathava $
"=============================================================================

" Tex_SetTeXCompilerTarget: sets the 'target' for the next call to Tex_RunLaTeX() {{{
function! Tex_SetTeXCompilerTarget(type, target)
	call Tex_Debug("+Tex_SetTeXCompilerTarget: setting target to [".a:target."] for ".a:type."r", "comp")

	if a:target == ''
		let target = Tex_GetVarValue('Tex_DefaultTargetFormat')
		let target = input('Enter the target format for '.a:type.'r: ', target)
	else
		let target = a:target
	endif
	if target == ''
		let target = 'dvi'
	endif

	let targetRule = Tex_GetVarValue('Tex_'.a:type.'Rule_'.target)

	if targetRule != ''
		if a:type == 'Compile'
			let &l:makeprg = escape(targetRule, Tex_GetVarValue('Tex_EscapeChars'))
		elseif a:type == 'View'
			let s:viewer = targetRule
		endif
		let s:target = target

	elseif Tex_GetVarValue('Tex_'.a:type.'RuleComplete_'.target) != ''
		let s:target = target

	else
		let curd = getcwd()
		exe 'cd '.expand('%:p:h')
		if !Tex_GetVarValue('Tex_UseMakefile') || (glob('makefile*') == '' && glob('Makefile*') == '')
			if has('gui_running')
				call confirm(
					\'No '.a:type.' rule defined for target '.target."\n".
					\'Please specify a rule in $VIMRUNTIME/ftplugin/tex/texrc'."\n".
					\'     :help Tex_'.a:type.'Rule_format'."\n".
					\'for more information',
					\"&ok", 1, 'Warning')
			else
				call input( 
					\'No '.a:type.' rule defined for target '.target."\n".
					\'Please specify a rule in $VIMRUNTIME/ftplugin/tex/texrc'."\n".
					\'     :help Tex_'.a:type.'Rule_format'."\n".
					\'for more information'
					\)
			endif
		else
			echomsg 'Assuming target is for makefile'
			let s:target = target
		endif
		exe 'cd '.curd
	endif
endfunction 

function! SetTeXTarget(...)
	if a:0 < 1
		let target = Tex_GetVarValue('Tex_DefaultTargetFormat')
		let target = input('Enter the target format for compiler and viewer: ', target)
	else
		let target = a:1
	endif
	if target == ''
		let target = 'dvi'
	endif

	call Tex_SetTeXCompilerTarget('Compile', target)
	call Tex_SetTeXCompilerTarget('View', target)
endfunction

com! -nargs=1 TCTarget :call Tex_SetTeXCompilerTarget('Compile', <f-args>)
com! -nargs=1 TVTarget :call Tex_SetTeXCompilerTarget('View', <f-args>)
com! -nargs=? TTarget :call SetTeXTarget(<f-args>)

" }}}
" Tex_CompileLatex: compiles the present file. {{{
" Description: 
function! Tex_CompileLatex()
	if &ft != 'tex'
		echo "calling Tex_RunLaTeX from a non-tex file"
		return
	end

	" close any preview windows left open.
	pclose!

	let curd = getcwd()

	" Find the main file corresponding to this file. Always cd to the
	" directory containing the file to avoid problems with the directory
	" containing spaces.
	" Latex on linux seems to be unable to handle file names with spaces at
	" all! Therefore for the moment, do not attempt to handle spaces in the
	" file name.
	if exists('b:fragmentFile')
		let mainfname = expand('%:p:t')
		call Tex_CD(expand('%:p:h'))
	else
		let mainfname = Tex_GetMainFileName(':p:t')
		call Tex_CD(Tex_GetMainFileName(':p:h'))
	end

	call Tex_Debug('Tex_CompileLatex: getting mainfname = ['.mainfname.'] from Tex_GetMainFileName', 'comp')

	" if a makefile exists and the user wants to use it, then use that
	" irrespective of whether *.latexmain exists or not. mainfname is still
	" extracted from *.latexmain (if possible) log file name depends on the
	" main file which will be compiled.
	if Tex_GetVarValue('Tex_UseMakefile') && (glob('makefile') != '' || glob('Makefile') != '')
		let _makeprg = &l:makeprg
		call Tex_Debug("Tex_CompileLatex: using the makefile in the current directory", "comp")
		let &l:makeprg = 'make $*'
		if exists('s:target')
			call Tex_Debug('Tex_CompileLatex: execing [make! '.s:target.']', 'comp')
			exec 'make! '.s:target
		else
			call Tex_Debug('Tex_CompileLatex: execing [make!]', 'comp')
			exec 'make!'
		endif
		let &l:makeprg = _makeprg
	else
		" If &makeprg has something like "$*.ps", it means that it wants the
		" file-name without the extension... Therefore remove it.
		if &makeprg =~ '\$\*\.\w\+'
			let mainfname = fnamemodify(mainfname, ':r')
		endif
		call Tex_Debug('Tex_CompileLatex: execing [make! '.mainfname.']', 'comp')
		exec 'make! '.mainfname
	endif
	redraw!

	call Tex_CD(curd)
endfunction " }}}
" Tex_RunLaTeX: compilation function {{{
" this function runs the latex command on the currently open file. often times
" the file being currently edited is only a fragment being \input'ed into some
" master tex file. in this case, make a file called mainfile.latexmain in the
" directory containig the file. in other words, if the current file is
" ~/thesis/chapter.tex
" so that doing "latex chapter.tex" doesnt make sense, then make a file called 
" main.tex.latexmain 
" in the ~/thesis directory. this will then run "latex main.tex" when
" Tex_RunLaTeX() is called.
function! Tex_RunLaTeX()
	call Tex_Debug('+Tex_RunLaTeX, b:fragmentFile = '.exists('b:fragmentFile'), 'comp')

	let dir = expand("%:p:h").'/'
	let curd = getcwd()
	call Tex_CD(expand("%:p:h"))

	let initTarget = s:target

	" first get the dependency chain of this format.
	call Tex_Debug("Tex_RunLaTeX: compiling to target [".s:target."]", "comp")

	if Tex_GetVarValue('Tex_FormatDependency_'.s:target) != ''
		let dependency = Tex_GetVarValue('Tex_FormatDependency_'.s:target)
		if dependency !~ ','.s:target.'$'
			let dependency = dependency.','.s:target
		endif
	else
		let dependency = s:target
	endif

	call Tex_Debug('Tex_RunLaTeX: getting dependency chain = ['.dependency.']', 'comp')

	" now compile to the final target format via each dependency.
	let i = 1
	while Tex_Strntok(dependency, ',', i) != ''
		let s:target = Tex_Strntok(dependency, ',', i)

		call Tex_SetTeXCompilerTarget('Compile', s:target)
		call Tex_Debug('Tex_RunLaTeX: setting target to '.s:target, 'comp')

		if Tex_GetVarValue('Tex_MultipleCompileFormats') =~ '\<'.s:target.'\>'
			call Tex_Debug("Tex_RunLaTeX: compiling file multiple times via Tex_CompileMultipleTimes", "comp")
			call Tex_CompileMultipleTimes()
		else
			call Tex_Debug("Tex_RunLaTeX: compiling file once via Tex_CompileLatex", "comp")
			call Tex_CompileLatex()
		endif

		let errlist = Tex_GetErrorList()
		call Tex_Debug("Tex_RunLaTeX: errlist = [".errlist."]", "comp")

		" If there are any errors, then break from the rest of the steps
		if errlist =~  '\v(error|warning)'
			call Tex_Debug('Tex_RunLaTeX: There were errors in compiling, breaking chain...', 'comp')
			break
		endif

		let i = i + 1
	endwhile
	
	let s:target = initTarget
	let s:origwinnum = winnr()
	call Tex_SetupErrorWindow()

	call Tex_CD(curd)
	call Tex_Debug("-Tex_RunLaTeX", "comp")
endfunction

" }}}
" Tex_ViewLaTeX: opens viewer {{{
" Description: opens the DVI viewer for the file being currently edited.
" Again, if the current file is a \input in a master file, see text above
" Tex_RunLaTeX() to see how to set this information.
function! Tex_ViewLaTeX()
	if &ft != 'tex'
		echo "calling Tex_ViewLaTeX from a non-tex file"
		return
	end
	
	let curd = getcwd()
	
	" If b:fragmentFile is set, it means this file was compiled as a fragment
	" using Tex_PartCompile, which means that we want to ignore any
	" *.latexmain or makefile's.
	if !exists('b:fragmentFile')
		" cd to the location of the file to avoid having to deal with spaces
		" in the directory name.
		let mainfname = Tex_GetMainFileName(':p:t:r')
		call Tex_CD(Tex_GetMainFileName(':p:h'))
	else
		let mainfname = expand("%:p:t:r")
		call Tex_CD(expand("%:p:h"))
	endif

	if Tex_GetVarValue('Tex_ViewRuleComplete_'.s:target) != ''

		let execString = Tex_GetVarValue('Tex_ViewRuleComplete_'.s:target)
		let execString = substitute(execString, '{v:servername}', v:servername, 'g')

	elseif has('win32')
		" unfortunately, yap does not allow the specification of an external
		" editor from the command line. that would have really helped ensure
		" that this particular vim and yap are connected.
		let execString = 'start '.s:viewer.' "$*.'.s:target.'"'

	elseif has('macunix')
		if strlen(s:viewer)
			let s:viewer = '-a '.s:viewer
		endif
		let execString = 'open '.s:viewer.' $*.'.s:target

	else
		" taken from Dimitri Antoniou's tip on vim.sf.net (tip #225).
		" slight change to actually use the current servername instead of
		" hardcoding it as xdvi.
		" Using an option for specifying the editor in the command line
		" because that seems to not work on older bash'es.
		if s:target == 'dvi'

			if Tex_GetVarValue('Tex_UseEditorSettingInDVIViewer') == 1 &&
						\ v:servername != '' &&
						\ (s:viewer == "xdvi" || s:viewer == "xdvik")

				let execString = s:viewer.' -editor "gvim --servername '.v:servername.
							\ ' --remote-silent +\%l \%f" $*.dvi &'

			elseif Tex_GetVarValue('Tex_UseEditorSettingInDVIViewer') == 1 &&
						\ s:viewer == "kdvi"

				let execString = 'kdvi --unique $*.dvi &'

			else

				let execString = s:viewer.' $*.dvi &'

			endif

		else

			let execString = s:viewer.' $*.'.s:target.' &'

		endif
	end

	let execString = substitute(execString, '\V$*', mainfname, 'g')
	call Tex_Debug("Tex_ViewLaTeX: execString = ".execString, "comp")

	exec 'silent! !'.execString

	if !has('gui_running')
		redraw!
	endif

	call Tex_CD(curd)
endfunction

" }}}
" Tex_ForwardSearchLaTeX: searches for current location in dvi file. {{{
" Description: if the DVI viewr is compatible, then take the viewer to that
"              position in the dvi file. see docs for Tex_RunLaTeX() to set a
"              master file if this is an \input'ed file. 
" Tip: With YAP on Windows, it is possible to do forward and inverse searches
"      on DVI files. to do forward search, you'll have to compile the file
"      with the --src-specials option. then set the following as the command
"      line in the 'view/options/inverse search' dialog box:
"           gvim --servername LATEX --remote-silent +%l "%f"
"      For inverse search, if you are reading this, then just pressing \ls
"      will work.
function! Tex_ForwardSearchLaTeX()
	if &ft != 'tex'
		echo "calling Tex_ViewLaTeX from a non-tex file"
		return
	end

	" only know how to do forward search for yap on windows and xdvik (and
	" some newer versions of xdvi) on unices. Therefore forward searching will
	" automatically open the DVI viewer irrespective of what the user chose as
	" the default view format.
	if Tex_GetVarValue('Tex_ViewRule_dvi') == ''
		return
	endif
	let viewer = Tex_GetVarValue('Tex_ViewRule_dvi')
	
	let curd = getcwd()

	let mainfname = Tex_GetMainFileName(':t')
	let mainfnameRoot = fnamemodify(Tex_GetMainFileName(), ':t:r')
	" cd to the location of the file to avoid problems with directory name
	" containing spaces.
	call Tex_CD(Tex_GetMainFileName(':p:h'))
	
	" inverse search tips taken from Dimitri Antoniou's tip and Benji Fisher's
	" tips on vim.sf.net (vim.sf.net tip #225)
	if has('win32')

		let execString = 'silent! !start '. viewer.' -s '.line('.').expand('%').' '.mainfnameRoot

	else
		if Tex_GetVarValue('Tex_UseEditorSettingInDVIViewer') == 1 &&
					\ exists('v:servername') &&
					\ (viewer == "xdvi" || viewer == "xdvik") 

			let execString = 'silent! !'.viewer.' -name xdvi -sourceposition '.line('.').expand("%").
						\ ' -editor "gvim --servername '.v:servername.' --remote-silent +\%l \%f" '.
						\ mainfnameRoot.'.dvi &'

		elseif Tex_GetVarValue('Tex_UseEditorSettingInDVIViewer') == 1 && viewer == "kdvi"

			let execString = 'silent! !kdvi --unique file:'.mainfnameRoot.'.dvi\#src:'.line('.').expand("%").' &'

		else

			let execString = 'silent! !'.viewer.' -name xdvi -sourceposition '.line('.').expand("%").' '.mainfnameRoot.'.dvi &'

		endif
	end

	call Tex_Debug("Tex_ForwardSearchLaTeX: execString = ".execString, "comp")
	execute execString
	if !has('gui_running')
		redraw!
	endif

	call Tex_CD(curd)
endfunction

" }}}

" ==============================================================================
" Functions for compiling parts of a file. 
" ============================================================================== 
" Tex_PartCompile: compiles selected fragment {{{
" Description: creates a temporary file from the selected fragment of text
"       prepending the preamble and \end{document} and then asks Tex_RunLaTeX() to
"       compile it.
function! Tex_PartCompile() range
	call Tex_Debug('+Tex_PartCompile', 'comp')
	" Save position
	let pos = line('.').' | normal! '.virtcol('.').'|'

	" Get a temporary file in the same directory as the file from which
	" fragment is being extracted. This is to enable the use of relative path
	" names in the fragment.
	let tmpfile = Tex_GetTempName(expand('%:p:h'))

	" Remember all the temp files and for each temp file created, remember
	" where the temp file came from.
	let s:Tex_NumTempFiles = (exists('s:Tex_NumTempFiles') ? s:Tex_NumTempFiles + 1 : 1)
	let s:Tex_TempFiles = (exists('s:Tex_TempFiles') ? s:Tex_TempFiles : '')
		\ . tmpfile."\n"
	let s:Tex_TempFile_{s:Tex_NumTempFiles} = tmpfile
	" TODO: For a function Tex_RestoreFragment which restores a temp file to
	"       its original location.
	let s:Tex_TempFileOrig_{s:Tex_NumTempFiles} = expand('%:p')
	let s:Tex_TempFileRange_{s:Tex_NumTempFiles} = a:firstline.','.a:lastline

	" Set up an autocmd to clean up the temp files when Vim exits.
	if Tex_GetVarValue('Tex_RemoveTempFiles')
		augroup RemoveTmpFiles
			au!
			au VimLeave * :call Tex_RemoveTempFiles()
		augroup END
	endif

	" If mainfile exists open it in tiny window and extract preamble there,
	" otherwise do it from current file
	let mainfile = Tex_GetMainFileName(":p")
	exe 'bot 1 split '.escape(mainfile, ' ')
	exe '1,/\s*\\begin{document}/w '.tmpfile
	wincmd q

	exe a:firstline.','.a:lastline."w! >> ".tmpfile

	" edit the temporary file
	exec 'drop '.tmpfile

	" append the \end{document} line.
	$ put ='\end{document}'
	w
	
	" set this as a fragment file.
	let b:fragmentFile = 1

	silent! call Tex_RunLaTeX()
endfunction " }}}
" Tex_RemoveTempFiles: cleans up temporary files created during part compilation {{{
" Description: During part compilation, temporary files containing the
"              visually selected text are created. These files need to be
"              removed when Vim exits to avoid "file leakage".
function! Tex_RemoveTempFiles()
	if !exists('s:Tex_NumTempFiles') || !Tex_GetVarValue('Tex_RemoveTempFiles')
		return
	endif
	let i = 1
	while i <= s:Tex_NumTempFiles
		let tmpfile = s:Tex_TempFile_{i}
		" Remove the tmp file and all other associated files such as the
		" .log files etc.
		call Tex_DeleteFile(fnamemodify(tmpfile, ':p:r').'.*')
		let i = i + 1
	endwhile
endfunction " }}}

" ==============================================================================
" Compiling a file multiple times to resolve references/citations etc.
" ============================================================================== 
" Tex_CompileMultipleTimes: The main function {{{
" Description: compiles a file multiple times to get cross-references right.
function! Tex_CompileMultipleTimes()
	" Just extract the root without any extension because we want to construct
	" the log file names etc from it.
	let curd = getcwd()
	let mainFileName_root = Tex_GetMainFileName(':p:t:r')
	call Tex_CD(Tex_GetMainFileName(':p:h'))

	" First ignore undefined references and the 
	" "rerun to get cross-references right" message from 
	" the compiler output.
	let origlevel = Tex_GetVarValue('Tex_IgnoreLevel')
	let origpats = Tex_GetVarValue('Tex_IgnoredWarnings')

	let g:Tex_IgnoredWarnings = g:Tex_IgnoredWarnings."\n"
		\ . 'Reference %.%# undefined'."\n"
		\ . 'Rerun to get cross-references right'
	TCLevel 1000

	let idxFileName = mainFileName_root.'.idx'
	let auxFileName = mainFileName_root.'.aux'

	let runCount = 0
	let needToRerun = 1
	while needToRerun == 1 && runCount < 5
		" assume we need to run only once.
		let needToRerun = 0

		let idxlinesBefore = Tex_CatFile(idxFileName)
		let auxlinesBefore = Tex_GetAuxFile(auxFileName)

		" first run latex.
		echomsg "latex run number : ".(runCount+1)
		call Tex_Debug("Tex_CompileMultipleTimes: latex run number : ".(runCount+1), "comp") 
		silent! call Tex_CompileLatex()
		
		" If there are errors in any latex compilation step, immediately
		" return. For now, do not bother with warnings because those might go
		" away after compiling again or after bibtex is run etc.
		let errlist = Tex_GetErrorList()
		call Tex_Debug("Tex_CompileMultipleTimes: errors = [".errlist."]", "comp")

		if errlist =~ 'error'
			let g:Tex_IgnoredWarnings = origpats
			exec 'TCLevel '.origlevel

			return
		endif

		let idxlinesAfter = Tex_CatFile(idxFileName)

		" If .idx file changed, then run makeindex to generate the new .ind
		" file and remember to rerun latex.
		if runCount == 0 && glob(idxFileName) != '' && idxlinesBefore != idxlinesAfter
			echomsg "Running makeindex..."
			let temp_mp = &mp | let &mp = Tex_GetVarValue('Tex_MakeIndexFlavor')
			exec 'silent! make '.mainFileName_root
			let &mp = temp_mp

			let needToRerun = 1
		endif

		" The first time we see if we need to run bibtex and if the .bbl file
		" changes, we will rerun latex.
		if runCount == 0 && Tex_IsPresentInFile('\\bibdata', mainFileName_root.'.aux')
			let bibFileName = mainFileName_root.'.bbl'

			let biblinesBefore = Tex_CatFile(bibFileName)

			echomsg "Running '".Tex_GetVarValue('Tex_BibtexFlavor')."' ..."
			let temp_mp = &mp | let &mp = Tex_GetVarValue('Tex_BibtexFlavor')
			exec 'silent! make '.mainFileName_root
			let &mp = temp_mp

			let biblinesAfter = Tex_CatFile(bibFileName)

			" If the .bbl file changed after running bibtex, we need to
			" latex again.
			if biblinesAfter != biblinesBefore
				echomsg 'Need to rerun because bibliography file changed...'
				call Tex_Debug('Tex_CompileMultipleTimes: Need to rerun because bibliography file changed...', 'comp')
				let needToRerun = 1
			endif
		endif

		" check if latex asks us to rerun
		let auxlinesAfter = Tex_GetAuxFile(auxFileName)
		if auxlinesAfter != auxlinesBefore
			echomsg "Need to rerun because the AUX file changed..."
			call Tex_Debug("Tex_CompileMultipleTimes: Need to rerun to get cross-references right...", 'comp')
			let needToRerun = 1
		endif

		let runCount = runCount + 1
	endwhile

	call Tex_Debug("Tex_CompileMultipleTimes: Ran latex ".runCount." time(s)", "comp")
	echomsg "Ran latex ".runCount." time(s)"

	let g:Tex_IgnoredWarnings = origpats
	exec 'TCLevel '.origlevel
	" After all compiler calls are done, reparse the .log file for
	" errors/warnings to handle the situation where the clist might have been
	" emptied because of bibtex/makeindex being run as the last step.
	exec 'silent! cfile '.mainFileName_root.'.log'

	call Tex_CD(curd)
endfunction " }}}
" Tex_GetAuxFile: get the contents of the AUX file {{{
" Description: get the contents of the AUX file recursively including any
" @\input'ted AUX files.
function! Tex_GetAuxFile(auxFile)
	if !filereadable(a:auxFile)
		return ''
	endif

	let auxContents = Tex_CatFile(a:auxFile)
	let pattern = '@\input{\(.\{-}\)}'

	let auxContents = substitute(auxContents, pattern, '\=Tex_GetAuxFile(submatch(1))', 'g')

	return auxContents
endfunction " }}}

" ==============================================================================
" Helper functions for 
" . viewing the log file in preview mode.
" . syncing the display between the quickfix window and preview window
" . going to the correct line _and column_ number from from the quick fix
"   window.
" ============================================================================== 
" Tex_SetupErrorWindow: sets up the cwindow and preview of the .log file {{{
" Description: 
function! Tex_SetupErrorWindow()
	let mainfname = Tex_GetMainFileName()

	let winnum = winnr()

	" close the quickfix window before trying to open it again, otherwise
	" whether or not we end up in the quickfix window after the :cwindow
	" command is not fixed.
	cclose
	cwindow
	" create log file name from mainfname
	let mfnlog = fnamemodify(mainfname, ":t:r").'.log'
	call Tex_Debug('Tex_SetupErrorWindow: mfnlog = '.mfnlog, 'comp')
	" if we moved to a different window, then it means we had some errors.
	if winnum != winnr()
		if Tex_GetVarValue('Tex_ShowErrorContext')
			call Tex_UpdatePreviewWindow(mfnlog)
			exe 'nnoremap <buffer> <silent> j j:call Tex_UpdatePreviewWindow("'.mfnlog.'")<CR>'
			exe 'nnoremap <buffer> <silent> k k:call Tex_UpdatePreviewWindow("'.mfnlog.'")<CR>'
			exe 'nnoremap <buffer> <silent> <up> <up>:call Tex_UpdatePreviewWindow("'.mfnlog.'")<CR>'
			exe 'nnoremap <buffer> <silent> <down> <down>:call Tex_UpdatePreviewWindow("'.mfnlog.'")<CR>'
		endif
		exe 'nnoremap <buffer> <silent> <enter> :call Tex_GotoErrorLocation("'.mfnlog.'")<CR>'

		setlocal nowrap

		" resize the window to just fit in with the number of lines.
		exec ( line('$') < 4 ? line('$') : 4 ).' wincmd _'
        if Tex_GetVarValue('Tex_GotoError') == 1
 	        call Tex_GotoErrorLocation(mfnlog)
        else
			exec s:origwinnum.' wincmd w'
        endif
	endif

endfunction " }}}
" Tex_PositionPreviewWindow: positions the preview window correctly. {{{
" Description: 
" 	The purpose of this function is to count the number of times an error
" 	occurs on the same line. or in other words, if the current line is
" 	something like |10 error|, then we want to count the number of
" 	lines in the quickfix window before this line which also contain lines
" 	like |10 error|. 
"
function! Tex_PositionPreviewWindow(filename)

	if getline('.') !~ '|\d\+ \(error\|warning\)|'
		if !search('|\d\+ \(error\|warning\)|')
			call Tex_Debug("not finding error pattern anywhere in quickfix window :".bufname(bufnr('%')),
						\ 'comp')
			pclose!
			return
		endif
	endif

	" extract the error pattern (something like 'file.tex|10 error|') on the
	" current line.
	let errpat = matchstr(getline('.'), '^\f*|\d\+ \(error\|warning\)|\ze')
	let errfile = matchstr(getline('.'), '^\f*\ze|\d\+ \(error\|warning\)|')
	" extract the line number from the error pattern.
	let linenum = matchstr(getline('.'), '|\zs\d\+\ze \(error\|warning\)|')

	" if we are on an error, then count the number of lines before this in the
	" quickfix window with an error on the same line.
	if errpat =~ 'error|$'
		" our location in the quick fix window.
		let errline = line('.')

		" goto the beginning of the quickfix window and begin counting the lines
		" which show an error on the same line.
		0
		let numrep = 0
		while 1
			" if we are on the same kind of error line, then means we have another
			" line containing the same error pattern.
			if getline('.') =~ errpat
				let numrep = numrep + 1
				normal! 0
			endif
			" if we have reached the original location in the quick fix window,
			" then break.
			if line('.') == errline
				break
			else
				" otherwise, search for the next line which contains the same
				" error pattern again. goto the end of the current line so we
				" dont count this line again.
				normal! $
				call search(errpat, 'W')
			endif
		endwhile
	else
		let numrep = 1
	endif

	if getline('.') =~ '|\d\+ warning|'
		let searchpat = escape(matchstr(getline('.'), '|\d\+ warning|\s*\zs.*'), '\ ')
	else
		let searchpat = 'l\.'.linenum
	endif

	" We first need to be in the scope of the correct file in the .log file.
	" This is important for example, when a.tex and b.tex both have errors on
	" line 9 of the file and we want to go to the error of b.tex. Merely
	" searching forward from the beginning of the log file for l.9 will always
	" land us on the error in a.tex.
	if errfile != ''
		exec 'silent! bot pedit +/(\\(\\f\\|\\[\\|\]\\|\\s\\)*'.errfile.'/ '.a:filename
	else
		exec 'bot pedit +0 '.a:filename
	endif
	" Goto the preview window
	" TODO: This is not robust enough. Check that a wincmd j actually takes
	" us to the preview window.
	wincmd j
	" now search forward from this position in the preview window for the
	" numrep^th error of the current line in the quickfix window.
	while numrep > 0
		call search(searchpat, 'W')
		let numrep = numrep - 1
	endwhile
	normal! z.

endfunction " }}}
" Tex_UpdatePreviewWindow: updates the view of the log file {{{
" Description: 
"       This function should be called when focus is in a quickfix window.
"       It opens the log file in a preview window and makes it display that
"       part of the log file which corresponds to the error which the user is
"       currently on in the quickfix window. Control returns to the quickfix
"       window when the function returns. 
"
function! Tex_UpdatePreviewWindow(filename)
	call Tex_PositionPreviewWindow(a:filename)

	if &previewwindow
		6 wincmd _
		wincmd p
	endif
endfunction " }}}
" Tex_GotoErrorLocation: goes to the correct location of error in the tex file {{{
" Description: 
"   This function should be called when focus is in a quickfix window. This
"   function will first open the preview window of the log file (if it is not
"   already open), position the display of the preview to coincide with the
"   current error under the cursor and then take the user to the file in
"   which this error has occured. 
"
"   The position is both the correct line number and the column number.
function! Tex_GotoErrorLocation(filename)

	" first use vim's functionality to take us to the location of the error
	" accurate to the line (not column). This lets us go to the correct file
	" without applying any logic.
	exec "normal! \<enter>"
	" If the log file is not found, then going to the correct line number is
	" all we can do.
 	if glob(a:filename) == ''
		return
	endif

	let winnum = winnr()
	" then come back to the quickfix window
	wincmd w

	" find out where in the file we had the error.
	let linenum = matchstr(getline('.'), '|\zs\d\+\ze \(warning\|error\)|')
	call Tex_PositionPreviewWindow(a:filename)

	if getline('.') =~ 'l.\d\+'

		let brokenline = matchstr(getline('.'), 'l.'.linenum.' \zs.*\ze')
		" If the line is of the form
		" 	l.10 ...and then there was some error
		" it means (most probably) that only part of the erroneous line is
		" shown. In this case, finding the length of the broken line is not
		" correct.  Instead goto the beginning of the line and search forward
		" for the part which is displayed and then go to its end.
		if brokenline =~ '^\M...'
			let partline = matchstr(brokenline, '^\M...\m\zs.*')
			let normcmd = "0/\\V".escape(partline, "\\")."/e+1\<CR>"
		else
			let column = strlen(brokenline) + 1
			let normcmd = column.'|'
		endif

	elseif getline('.') =~ 'LaTeX Warning: \(Citation\|Reference\) `.*'

		let ref = matchstr(getline('.'), "LaTeX Warning: \\(Citation\\|Reference\\) `\\zs[^']\\+\\ze'")
		let normcmd = '0/'.ref."\<CR>"

	else

		let normcmd = '0'

	endif

	" go back to the window where we came from.
	exec winnum.' wincmd w'
	exec 'silent! '.linenum.' | normal! '.normcmd

	if !Tex_GetVarValue('Tex_ShowErrorContext')
		pclose!
	endif
endfunction " }}}
" Tex_SetCompilerMaps: sets maps for compiling/viewing/searching {{{
" Description: 
function! <SID>Tex_SetCompilerMaps()
	if exists('b:Tex_doneCompilerMaps')
		return
	endif
	let s:ml = exists('g:mapleader') ? g:mapleader : "\\"

	nnoremap <buffer> <Plug>Tex_Compile :call Tex_RunLaTeX()<cr>
	vnoremap <buffer> <Plug>Tex_Compile :call Tex_PartCompile()<cr>
	nnoremap <buffer> <Plug>Tex_View :call Tex_ViewLaTeX()<cr>
	nnoremap <buffer> <Plug>Tex_ForwardSearch :call Tex_ForwardSearchLaTeX()<cr>

	call Tex_MakeMap(s:ml."ll", "<Plug>Tex_Compile", 'n', '<buffer>')
	call Tex_MakeMap(s:ml."ll", "<Plug>Tex_Compile", 'v', '<buffer>')
	call Tex_MakeMap(s:ml."lv", "<Plug>Tex_View", 'n', '<buffer>')
	call Tex_MakeMap(s:ml."ls", "<Plug>Tex_ForwardSearch", 'n', '<buffer>')
endfunction 
" }}}

augroup LatexSuite
	au LatexSuite User LatexSuiteFileType 
		\ call Tex_Debug('compiler.vim: Catching LatexSuiteFileType event', 'comp') | 
		\ call <SID>Tex_SetCompilerMaps()
augroup END

command! -nargs=0 -range=% TPartCompile :<line1>, <line2> silent! call Tex_PartCompile()
" Setting b:fragmentFile = 1 makes Tex_CompileLatex consider the present file
" the _main_ file irrespective of the presence of a .latexmain file.
command! -nargs=0 TCompileThis let b:fragmentFile = 1
command! -nargs=0 TCompileMainFile let b:fragmentFile = 0

" vim:fdm=marker:ff=unix:noet:ts=4:sw=4