summaryrefslogtreecommitdiffstats
path: root/autoload/vimtex/qf/latexlog.vim
blob: 0046543e46085cb05a22aca830869eee470b8d76 (plain) (blame)
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
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'latex') == -1

" vimtex - LaTeX plugin for Vim
"
" Maintainer: Karl Yngve Lervåg
" Email:      karl.yngve@gmail.com
"

function! vimtex#qf#latexlog#new() abort " {{{1
  return deepcopy(s:qf)
endfunction

" }}}1


let s:qf = {
      \ 'name' : 'LaTeX logfile',
      \}

function! s:qf.init(state) abort dict "{{{1
  let self.config = get(g:, 'vimtex_quickfix_latexlog', {})
  let self.config.default = get(self.config, 'default', 1)
  let self.config.packages = get(self.config, 'packages', {})
  let self.config.packages.default = get(self.config.packages, 'default',
        \ self.config.default)

  let self.types = map(
        \ filter(items(s:), 'v:val[0] =~# ''^type_'''),
        \ 'v:val[1]')
endfunction

" }}}1
function! s:qf.set_errorformat() abort dict "{{{1
  "
  " Note: The errorformat assumes we're using the -file-line-error with
  "       [pdf]latex. For more info, see |errorformat-LaTeX|.
  "

  " Push file to file stack
  setlocal errorformat=%-P**%f
  setlocal errorformat+=%-P**\"%f\"

  " Match errors
  setlocal errorformat+=%E!\ LaTeX\ %trror:\ %m
  setlocal errorformat+=%E%f:%l:\ %m
  setlocal errorformat+=%E!\ %m

  " More info for undefined control sequences
  setlocal errorformat+=%Z<argument>\ %m

  " More info for some errors
  setlocal errorformat+=%Cl.%l\ %m

  "
  " Define general warnings
  "
  let l:default = self.config.default
  if get(self.config, 'font', l:default)
    setlocal errorformat+=%+WLaTeX\ Font\ Warning:\ %.%#line\ %l%.%#
    setlocal errorformat+=%-CLaTeX\ Font\ Warning:\ %m
    setlocal errorformat+=%-C(Font)%m
  else
    setlocal errorformat+=%-WLaTeX\ Font\ Warning:\ %m
  endif

  if !get(self.config, 'references', l:default)
    setlocal errorformat+=%-WLaTeX\ %.%#Warning:\ %.%#eference%.%#undefined%.%#line\ %l%.%#
    setlocal errorformat+=%-WLaTeX\ %.%#Warning:\ %.%#undefined\ references.
  endif

  if get(self.config, 'general', l:default)
    setlocal errorformat+=%+WLaTeX\ %.%#Warning:\ %.%#line\ %l%.%#
    setlocal errorformat+=%+WLaTeX\ %.%#Warning:\ %m
  endif

  if get(self.config, 'overfull', l:default)
    setlocal errorformat+=%+WOverfull\ %\\%\\hbox%.%#\ at\ lines\ %l--%*\\d
    setlocal errorformat+=%+WOverfull\ %\\%\\hbox%.%#\ at\ line\ %l
    setlocal errorformat+=%+WOverfull\ %\\%\\vbox%.%#\ at\ line\ %l
  endif

  if get(self.config, 'underfull', l:default)
    setlocal errorformat+=%+WUnderfull\ %\\%\\hbox%.%#\ at\ lines\ %l--%*\\d
    setlocal errorformat+=%+WUnderfull\ %\\%\\vbox%.%#\ at\ line\ %l
  endif

  "
  " Define package related warnings
  "
  let l:default = self.config.packages.default
  if get(self.config.packages, 'natbib', l:default)
    setlocal errorformat+=%+WPackage\ natbib\ Warning:\ %m\ on\ input\ line\ %l.
  else
    setlocal errorformat+=%-WPackage\ natbib\ Warning:\ %m\ on\ input\ line\ %l.
  endif

  if get(self.config.packages, 'biblatex', l:default)
    setlocal errorformat+=%+WPackage\ biblatex\ Warning:\ %m
    setlocal errorformat+=%-C(biblatex)%.%#in\ t%.%#
    setlocal errorformat+=%-C(biblatex)%.%#Please\ v%.%#
    setlocal errorformat+=%-C(biblatex)%.%#LaTeX\ a%.%#
    setlocal errorformat+=%-C(biblatex)%m
  else
    setlocal errorformat+=%-WPackage\ biblatex\ Warning:\ %m
  endif

  if get(self.config.packages, 'babel', l:default)
    setlocal errorformat+=%+WPackage\ babel\ Warning:\ %m
    setlocal errorformat+=%-Z(babel)%.%#input\ line\ %l.
    setlocal errorformat+=%-C(babel)%m
  else
    setlocal errorformat+=%-WPackage\ babel\ Warning:\ %m
  endif

  if get(self.config.packages, 'hyperref', l:default)
    setlocal errorformat+=%+WPackage\ hyperref\ Warning:\ %m
    setlocal errorformat+=%-C(hyperref)%m\ on\ input\ line\ %l.
    setlocal errorformat+=%-C(hyperref)%m
  else
    setlocal errorformat+=%-WPackage\ hyperref\ Warning:\ %m
  endif

  if get(self.config.packages, 'scrreprt', l:default)
    setlocal errorformat+=%+WPackage\ scrreprt\ Warning:\ %m
    setlocal errorformat+=%-C(scrreprt)%m
  else
    setlocal errorformat+=%-WPackage\ scrreprt\ Warning:\ %m
  endif

  if get(self.config.packages, 'fixltx2e', l:default)
    setlocal errorformat+=%+WPackage\ fixltx2e\ Warning:\ %m
    setlocal errorformat+=%-C(fixltx2e)%m
  else
    setlocal errorformat+=%-WPackage\ fixltx2e\ Warning:\ %m
  endif

  if get(self.config.packages, 'titlesec', l:default)
    setlocal errorformat+=%+WPackage\ titlesec\ Warning:\ %m
    setlocal errorformat+=%-C(titlesec)%m
  else
    setlocal errorformat+=%-WPackage\ titlesec\ Warning:\ %m
  endif

  if get(self.config.packages, 'general', l:default)
    setlocal errorformat+=%+WPackage\ %.%#\ Warning:\ %m\ on\ input\ line\ %l.
    setlocal errorformat+=%+WPackage\ %.%#\ Warning:\ %m
    setlocal errorformat+=%-Z(%.%#)\ %m\ on\ input\ line\ %l.
    setlocal errorformat+=%-C(%.%#)\ %m
  endif

  " Ignore unmatched lines
  setlocal errorformat+=%-G%.%#
endfunction

" }}}1
function! s:qf.addqflist(tex, log) abort dict "{{{1
  if empty(a:log) || !filereadable(a:log)
    throw 'Vimtex: No log file found'
  endif

  let self.errorformat_saved = &l:errorformat
  call self.set_errorformat()
  execute 'caddfile' fnameescape(a:log)
  let &l:errorformat = self.errorformat_saved

  " Apply some post processing of the quickfix list
  let self.main = a:tex
  let self.root = b:vimtex.root
  call self.fix_paths()
endfunction

" }}}1
function! s:qf.pprint_items() abort dict " {{{1
  return [[ 'config', self.config ]]
endfunction

" }}}1
function! s:qf.fix_paths() abort dict " {{{1
  let l:qflist = getqflist()

  for l:qf in l:qflist
    " For errors and warnings that don't supply a file, the basename of the
    " main file is used. However, if the working directory is not the root of
    " the LaTeX project, than this results in bufnr = 0.
    if l:qf.bufnr == 0
      let l:qf.bufnr = bufnr(self.main)
      continue
    endif

    " The buffer names of all file:line type errors are relative to the root of
    " the main LaTeX file.
    let l:file = fnamemodify(
          \ simplify(self.root . '/' . bufname(l:qf.bufnr)), ':.')
    if !filereadable(l:file) | continue | endif

    if !bufexists(l:file)
      execute 'badd' l:file
    endif

    let l:qf.filename = l:file
    let l:qf.bufnr = bufnr(l:file)
  endfor

  call setqflist(l:qflist, 'r')
endfunction

" }}}1

endif