robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
61eddb3fe7
master
1" Language: ConTeXt typesetting engine 2" Maintainer: Nicola Vitacolonna <nvitacolonna@gmail.com> 3" Latest Revision: 2016 Oct 21 4 5let s: keepcpo = & cpo 6set cpo & vim 7 8" Helper functions {{{ 9function ! s: context_echo ( message , mode ) 10redraw 11echo "\r" 12execute 'echohl' a: mode 13echomsg '[ConTeXt]' a: message 14echohl None 15endf 16 17function ! s: sh () 18return has ( 'win32' ) || has ( 'win64' ) || has ( 'win16' ) || has ( 'win95' ) 19 \? [ 'cmd.exe' , '/C' ] 20 \: [ '/bin/sh' , '-c' ] 21endfunction 22 23" For backward compatibility 24if exists ( '*win_getid' ) 25 26function ! s: win_getid () 27return win_getid () 28endf 29 30function ! s: win_id2win ( winid ) 31return win_id2win ( a: winid ) 32endf 33 34else 35 36function ! s: win_getid () 37return winnr () 38endf 39 40function ! s: win_id2win ( winnr ) 41return a: winnr 42endf 43 44endif 45" }}} 46 47" ConTeXt jobs {{{ 48if has ( 'job' ) 49 50let g: context_jobs = [] 51 52" Print the status of ConTeXt jobs 53function ! context#job_status () 54let l: jobs = filter ( g: context_jobs , 'job_status(v:val) == "run"' ) 55let l: n = len ( l: jobs ) 56call s: context_echo ( 57 \'There ' . ( l: n == 1 ? 'is' : 'are' ) . ' ' . ( l: n == 0 ? 'no' : l: n ) 58 \. ' job' . ( l: n == 1 ? '' : 's' ) . ' running' 59 \. ( l: n == 0 ? '.' : ' (' . join ( l: jobs , ', ' ) . ').' ), 60 \'ModeMsg' ) 61endfunction 62 63" Stop all ConTeXt jobs 64function ! context#stop_jobs () 65let l: jobs = filter ( g: context_jobs , 'job_status(v:val) == "run"' ) 66for job in l: jobs 67call job_stop ( job ) 68endfor 69sleep 1 70let l: tmp = [] 71for job in l: jobs 72if job_status ( job ) == "run" 73call add ( l: tmp , job ) 74endif 75endfor 76let g: context_jobs = l: tmp 77if empty ( g: context_jobs ) 78call s: context_echo ( 'Done. No jobs running.' , 'ModeMsg' ) 79else 80call s: context_echo ( 'There are still some jobs running. Please try again.' , 'WarningMsg' ) 81endif 82endfunction 83 84function ! context#callback ( path , job , status ) 85if index ( g: context_jobs , a: job ) != -1 && job_status ( a: job ) != 'run' " just in case 86call remove ( g: context_jobs , index ( g: context_jobs , a: job )) 87endif 88call s: callback ( a: path , a: job , a: status ) 89endfunction 90 91function ! context#close_cb ( channel ) 92call job_status ( ch_getjob ( a: channel )) " Trigger exit_cb's callback for faster feedback 93endfunction 94 95function ! s: typeset ( path ) 96call add ( g: context_jobs , 97 \job_start ( add ( s: sh (), context#command () . ' ' . shellescape ( fnamemodify ( a: path , ":t" ))), { 98 \'close_cb' : 'context#close_cb' , 99 \'exit_cb' : function ( get ( b:, 'context_callback' , get ( g:, 'context_callback' , 'context#callback' )), 100 \[ a: path ]), 101 \'in_io' : 'null' 102 \})) 103endfunction 104 105else " No jobs 106 107function ! context#job_status () 108call s: context_echo ( 'Not implemented' , 'WarningMsg' ) 109endfunction ! 110 111function ! context#stop_jobs () 112call s: context_echo ( 'Not implemented' , 'WarningMsg' ) 113endfunction 114 115function ! context#callback ( path , job , status ) 116call s: callback ( a: path , a: job , a: status ) 117endfunction 118 119function ! s: typeset ( path ) 120execute '!' . context#command () . ' ' . shellescape ( fnamemodify ( a: path , ":t" ), 1 ) 121call call ( get ( b:, 'context_callback' , get ( g:, 'context_callback' , 'context#callback' )), 122 \[ a: path , 0 , v: shell_error ]) 123endfunction 124 125endif " has('job') 126 127function ! s: callback ( path , job , status ) abort 128if a: status < 0 " Assume the job was terminated 129return 130endif 131" Get info about the current window 132let l: winid = s: win_getid () " Save window id 133let l: efm = & l: errorformat " Save local errorformat 134let l: cwd = fnamemodify ( getcwd (), ":p" ) " Save local working directory 135" Set errorformat to parse ConTeXt errors 136execute 'setl efm=' . escape ( b: context_errorformat , ' ' ) 137try " Set cwd to expand error file correctly 138execute 'lcd' fnameescape ( fnamemodify ( a: path , ':h' )) 139catch /.*/ 140execute 'setl efm=' . escape ( l: efm , ' ' ) 141throw v: exception 142endtry 143try 144execute 'cgetfile' fnameescape ( fnamemodify ( a: path , ':r' ) . '.log' ) 145botright cwindow 146finally " Restore cwd and errorformat 147execute s: win_id2win ( l: winid ) . 'wincmd w' 148execute 'lcd ' . fnameescape ( l: cwd ) 149execute 'setl efm=' . escape ( l: efm , ' ' ) 150endtry 151if a: status == 0 152call s: context_echo ( 'Success!' , 'ModeMsg' ) 153else 154call s: context_echo ( 'There are errors. ' , 'ErrorMsg' ) 155endif 156endfunction 157 158function ! context#command () 159return get ( b:, 'context_mtxrun' , get ( g:, 'context_mtxrun' , 'mtxrun' )) 160 \. ' --script context --autogenerate --nonstopmode' 161 \. ' --synctex=' . ( get ( b:, 'context_synctex' , get ( g:, 'context_synctex' , 0 )) ? '1' : '0' ) 162 \. ' ' . get ( b:, 'context_extra_options' , get ( g:, 'context_extra_options' , '' )) 163endfunction 164 165" Accepts an optional path (useful for big projects, when the file you are 166" editing is not the project's root document). If no argument is given, uses 167" the path of the current buffer. 168function ! context#typeset (...) abort 169let l: path = fnamemodify ( strlen ( a: 000 [ 0 ]) > 0 ? a: 1 : expand ( "%" ), ":p" ) 170let l: cwd = fnamemodify ( getcwd (), ":p" ) " Save local working directory 171call s: context_echo ( 'Typesetting...' , 'ModeMsg' ) 172execute 'lcd' fnameescape ( fnamemodify ( l: path , ":h" )) 173try 174call s: typeset ( l: path ) 175finally " Restore local working directory 176execute 'lcd ' . fnameescape ( l: cwd ) 177endtry 178endfunction ! 179"}}} 180 181let & cpo = s: keepcpo 182unlet s: keepcpo 183 184" vim: sw=2 fdm=marker