summaryrefslogtreecommitdiffstats
path: root/autoload/vimtex/log.vim
blob: f7569a653804dadcccc8af02b9a6f8d5c4617f21 (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
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#log#init_buffer() abort " {{{1
  command! -buffer -bang VimtexLog call vimtex#log#open()

  nnoremap <buffer> <plug>(vimtex-log) :VimtexLog<cr>
endfunction

" }}}1

function! vimtex#log#info(...) abort " {{{1
  call s:logger.add(a:000, 'info')
endfunction

" }}}1
function! vimtex#log#warning(...) abort " {{{1
  call s:logger.add(a:000, 'warning')
endfunction

" }}}1
function! vimtex#log#error(...) abort " {{{1
  call s:logger.add(a:000, 'error')
endfunction

" }}}1

function! vimtex#log#get() abort " {{{1
  return s:logger.entries
endfunction

" }}}1

function! vimtex#log#open() abort " {{{1
  call vimtex#scratch#new(s:logger)
endfunction

" }}}1
function! vimtex#log#toggle_verbose() abort " {{{1
  if s:logger.verbose
    let s:logger.verbose = 0
    call vimtex#log#info('Logging is now quiet')
  else
    call vimtex#log#info('Logging is now verbose')
    let s:logger.verbose = 1
  endif
endfunction

" }}}1


let s:logger = {
      \ 'name' : 'VimtexMessageLog',
      \ 'entries' : [],
      \ 'type_to_highlight' : {
      \   'info' : 'VimtexInfo',
      \   'warning' : 'VimtexWarning',
      \   'error' : 'VimtexError',
      \ },
      \ 'verbose' : get(g:, 'vimtex_log_verbose', 1),
      \}
function! s:logger.add(msg_arg, type) abort dict " {{{1
  let l:msg_list = []
  for l:msg in a:msg_arg
    if type(l:msg) == type('')
      call add(l:msg_list, l:msg)
    elseif type(l:msg) == type([])
      call extend(l:msg_list, filter(l:msg, "type(v:val) == type('')"))
    endif
  endfor

  let l:entry = {}
  let l:entry.type = a:type
  let l:entry.time = strftime('%T')
  let l:entry.callstack = vimtex#debug#stacktrace()[1:]
  let l:entry.msg = l:msg_list
  call add(self.entries, l:entry)

  if !self.verbose | return | endif

  " Ignore message
  for l:re in get(g:, 'vimtex_log_ignore', [])
    if join(l:msg_list) =~# l:re | return | endif
  endfor

  call vimtex#echo#formatted([
        \ [self.type_to_highlight[a:type], 'vimtex:'],
        \ ' ' . l:msg_list[0]
        \])
  for l:line in l:msg_list[1:]
    call vimtex#echo#echo('        ' . l:line)
  endfor
endfunction

" }}}1
function! s:logger.print_content() abort dict " {{{1
  for l:entry in self.entries
    call append('$', printf('%s: %s', l:entry.time, l:entry.type))
    for l:stack in l:entry.callstack
      if l:stack.lnum > 0
        call append('$', printf('  #%d %s:%d', l:stack.nr, l:stack.filename, l:stack.lnum))
      else
        call append('$', printf('  #%d %s', l:stack.nr, l:stack.filename))
      endif
      call append('$', printf('  In %s', l:stack.function))
      if !empty(l:stack.text)
        call append('$', printf('    %s', l:stack.text))
      endif
    endfor
    for l:msg in l:entry.msg
      call append('$', printf('  %s', l:msg))
    endfor
    call append('$', '')
  endfor
endfunction

" }}}1
function! s:logger.syntax() abort dict " {{{1
  syntax match VimtexInfoOther /.*/

  syntax include @VIM syntax/vim.vim
  syntax match VimtexInfoVimCode /^    .*/ transparent contains=@VIM

  syntax match VimtexInfoKey /^\S*:/ nextgroup=VimtexInfoValue
  syntax match VimtexInfoKey /^  #\d\+/ nextgroup=VimtexInfoValue
  syntax match VimtexInfoKey /^  In/ nextgroup=VimtexInfoValue
  syntax match VimtexInfoValue /.*/ contained
endfunction

" }}}1

endif