robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
dbdd73e846
master
1" vim: fdm=marker et ts=4 sw=4 2 3" Setup: {{{1 4function ! tutor#SetupVim () 5if ! exists ( 'g:did_load_ftplugin' ) || g: did_load_ftplugin != 1 6filetype plugin on 7endif 8if has ( 'syntax' ) 9if ! exists ( 'g:syntax_on' ) || g: syntax_on == 0 10syntax on 11endif 12endif 13endfunction 14 15" Load inline metadata 16function ! tutor#LoadMetadata () 17lua require ( 'nvim.tutor' ). load_metadata () 18endfunction 19 20" Mappings: {{{1 21 22function ! tutor#SetNormalMappings () 23nnoremap <silent> <buffer> < CR > :call tutor#FollowLink(0)< cr > 24nnoremap <silent> <buffer> < 2-LeftMouse> :call tutor#MouseDoubleClick()< cr > 25nnoremap <buffer> >> :call tutor#InjectCommand()< cr > 26endfunction 27 28function ! tutor#MouseDoubleClick () 29if foldclosed ( line ( '.' )) > -1 30normal ! zo 31else 32if match ( getline ( '.' ), '^#\{1,} ' ) > -1 && foldlevel ( line ( '.' )) > 0 33silent normal ! zc 34else 35call tutor#FollowLink ( 0 ) 36endif 37endif 38endfunction 39 40function ! tutor#InjectCommand () 41let l: cmd = substitute ( getline ( '.' ), '^\s*' , '' , '' ) 42exe l: cmd 43redraw |echohl WarningMsg |echon "tutor: ran" |echohl None |echon " " |echohl Statement |echon l: cmd 44endfunction 45 46function ! tutor#FollowLink ( force ) 47let l: stack_s = join ( map ( synstack ( line ( '.' ), col ( '.' )), 'synIDattr(v:val, "name")' ), '' ) 48if l: stack_s =~# 'tutorLink' 49let l: link_start = searchpairpos ( '\[' , '' , ')' , 'nbcW' ) 50let l: link_end = searchpairpos ( '\[' , '' , ')' , 'ncW' ) 51if l: link_start [ 0 ] == l: link_end [ 0 ] 52let l: linkData = getline ( l: link_start [ 0 ])[ l: link_start [ 1 ] - 1 : l: link_end [ 1 ] - 1 ] 53else 54return 55endif 56let l: target = matchstr ( l: linkData , '(\@<=.*)\@=' ) 57if a: force != 1 && match ( l: target , '\*.\+\*' ) > -1 58call cursor ( l: link_start [ 0 ], l: link_end [ 1 ]) 59call search ( l: target , '' ) 60normal ! ^ 61elseif a: force != 1 && match ( l: target , '^@tutor:' ) > -1 62let l: tutor = matchstr ( l: target , '@tutor:\zs.*' ) 63exe "Tutor " . l: tutor 64else 65exe "help " . l: target 66endif 67endif 68endfunction 69 70" Folding And Info: {{{1 71 72function ! tutor#TutorFolds () 73if getline ( v: lnum ) =~# '^#\{1,6}' 74return ">" . len ( matchstr ( getline ( v: lnum ), '^#\{1,6}' )) 75else 76return "=" 77endif 78endfunction 79 80" Tutor Cmd: {{{1 81 82function ! s: Locale () 83" Make sure l:lang exists before returning. 84let l: lang = 'en_US' 85if exists ( 'v:lang' ) && v: lang =~ '\a\a' 86let l: lang = v: lang 87elseif $ LC_ALL =~ '\a\a' 88let l: lang = $ LC_ALL 89elseif $ LC_MESSAGES =~ '\a\a' || $ LC_MESSAGES ==# "C" 90" LC_MESSAGES=C can be used to explicitly ask for English messages while 91" keeping LANG non-English; don't set l:lang then. 92if $ LC_MESSAGES =~ '\a\a' 93let l: lang = $ LC_MESSAGES 94endif 95elseif $ LANG =~ '\a\a' 96let l: lang = $ LANG 97endif 98return split ( l: lang , '_' ) 99endfunction 100 101function ! s: GlobPath ( lp , pat ) 102if version >= 704 && has ( 'patch279' ) 103return globpath ( a: lp , a: pat , 1 , 1 ) 104else 105return split ( globpath ( a: lp , a: pat , 1 ), '\n' ) 106endif 107endfunction 108 109function ! s: Sort ( a , b ) 110let mod_a = fnamemodify ( a: a , ':t' ) 111let mod_b = fnamemodify ( a: b , ':t' ) 112if mod_a == mod_b 113let retval = 0 114elseif mod_a > mod_b 115if match ( mod_a , '^vim-' ) > -1 && match ( mod_b , '^vim-' ) == -1 116let retval = -1 117else 118let retval = 1 119endif 120else 121if match ( mod_b , '^vim-' ) > -1 && match ( mod_a , '^vim-' ) == -1 122let retval = 1 123else 124let retval = -1 125endif 126endif 127return retval 128endfunction 129 130" returns a list of all tutor files matching the given name 131function ! tutor#GlobTutorials ( name , locale ) 132let locale = a: locale 133" pack/*/start/* are not reported in &rtp 134let rtp = nvim_list_runtime_paths () 135 \ ->map ({ _ , v ->escape ( v: lua . vim . fs . normalize ( v ), ',' )}) 136 \ ->join ( ',' ) 137" search for tutorials: 138" 1. non-localized 139let l: tutors = s: GlobPath ( rtp , 'tutor/' . a: name . '.tutor' ) 140" 2. localized for current locale 141let l: locale_tutors = s: GlobPath ( rtp , 'tutor/' . locale . '/' . a: name . '.tutor' ) 142" 3. fallback to 'en' 143if len ( l: locale_tutors ) == 0 144let l: locale_tutors = s: GlobPath ( rtp , 'tutor/en/' . a: name . '.tutor' ) 145endif 146call extend ( l: tutors , l: locale_tutors ) 147return uniq ( sort ( l: tutors , 's:Sort' ), 's:Sort' ) 148endfunction 149 150function ! tutor#TutorCmd ( tutor_name ) 151if match ( a: tutor_name , '[[:space:]]' ) > 0 152echom "Only one argument accepted (check spaces)" 153return 154endif 155 156if a: tutor_name == '' 157let l: tutor_name = 'vim-01-beginner.tutor' 158else 159let l: tutor_name = a: tutor_name 160endif 161 162if match ( l: tutor_name , '\.tutor$' ) > 0 163let l: tutor_name = fnamemodify ( l: tutor_name , ':r' ) 164endif 165 166let l: tutors = tutor#GlobTutorials ( l: tutor_name , s: Locale ()[ 0 ]) 167 168if len ( l: tutors ) == 0 169echom "No tutorial with that name found" 170return 171endif 172 173if len ( l: tutors ) == 1 174let l: to_open = l: tutors [ 0 ] 175else 176let l: idx = 0 177let l: candidates = [ 'Several tutorials with that name found. Select one:' ] 178for candidate in map ( copy ( l: tutors ), 179 \'fnamemodify(v:val, ":h:h:t")."/".s:Locale()[0]."/".fnamemodify(v:val, ":t")' ) 180let l: idx += 1 181call add ( l: candidates , l: idx . '. ' . candidate ) 182endfor 183let l: tutor_to_open = inputlist ( l: candidates ) 184let l: to_open = l: tutors [ l: tutor_to_open - 1 ] 185endif 186 187let l: tutor_file_og = l: to_open 188let l: tutor_file_tmp = tempname () .. '.' .. fnamemodify ( l: tutor_file_og , ':t' ) 189call filecopy ( l: tutor_file_og , l: tutor_file_tmp ) 190 191call tutor#SetupVim () 192exe "drop" fnameescape ( l: tutor_file_tmp ) 193let b: tutor_file = l: tutor_file_og 194call tutor#LoadMetadata () 195call tutor#EnableInteractive ( v: true ) 196call tutor#ApplyTransform () 197endfunction 198 199function ! tutor#TutorCmdComplete ( lead , line , pos ) 200let l: tutors = tutor#GlobTutorials ( '*' , s: Locale ()[ 0 ]) 201let l: names = uniq ( sort ( map ( l: tutors , 'fnamemodify(v:val, ":t:r")' ), 's:Sort' )) 202return join ( l: names , "\n" ) 203endfunction 204 205" Enables/disables interactive mode. 206function ! tutor#EnableInteractive ( enable ) 207let enable = a: enable 208if enable 209setlocal buftype = nowrite 210setlocal concealcursor += inv 211setlocal conceallevel = 2 212lua require ( 'nvim.tutor' ). apply_marks () 213augroup tutor_interactive 214autocmd ! TextChanged , TextChangedI <buffer> lua require ( 'nvim.tutor' ). apply_marks_on_changed () 215augroup END 216else 217setlocal buftype < 218setlocal concealcursor < 219setlocal conceallevel < 220if exists ( '#tutor_interactive' ) 221autocmd ! tutor_interactive * <buffer> 222endif 223endif 224endfunction 225 226function ! tutor#ApplyTransform () 227if has ( 'win32' ) 228sil ! %s /{unix:(\(.\{-}\)),win:(\(.\{-}\))}/\2/g 229else 230sil ! %s /{unix:(\(.\{-}\)),win:(\(.\{-}\))}/\1/g 231endif 232normal ! gg0 233endfunction