From ec67d0706524a23768c8a964a7940a34f057edb8 Mon Sep 17 00:00:00 2001 From: Felipe Morales Date: Thu, 13 Jul 2017 19:04:15 -0400 Subject: tutor: allow metadata to exist outside of the documents. this makes 'expect' regions simpler to handle. --- runtime/autoload/tutor.vim | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) (limited to 'runtime/autoload/tutor.vim') diff --git a/runtime/autoload/tutor.vim b/runtime/autoload/tutor.vim index 43d8a87886..1f0668c1b3 100644 --- a/runtime/autoload/tutor.vim +++ b/runtime/autoload/tutor.vim @@ -209,7 +209,15 @@ function! tutor#InfoText() return join(l:info_parts, " ") endfunction -" Marks {{{1 +function! tutor#LoadMetadata() + try + let b:tutor_metadata = json_decode(join(readfile(expand('%').'.json'), "\n")) + catch + endtry +endfunction + +" Marks: {{{1 +" Internal: {{{2 function! tutor#PlaceXMarks() call cursor(1, 1) let b:tutor_sign_id = 1 @@ -241,13 +249,47 @@ function! tutor#CheckText(text) endif endfunction -function! tutor#OnTextChanged() +function! tutor#XmarksOnTextChanged() let l:text = getline('.') if match(l:text, '^--->') > -1 call tutor#CheckText(l:text) endif endfunction +" External: {{{2 +function! tutor#ApplyMarks() + if exists('b:tutor_metadata') && has_key(b:tutor_metadata, 'expect') + let b:tutor_sign_id = 1 + for expct in keys(b:tutor_metadata['expect']) + let lnum = eval(expct) + call matchaddpos('tutorSampleText', [lnum]) + call tutor#CheckLine(lnum) + endfor + endif +endfunction + +function! tutor#ApplyMarksOnChanged() + if exists('b:tutor_metadata') && has_key(b:tutor_metadata, 'expect') + let lnum = line('.') + if index(keys(b:tutor_metadata['expect']), string(lnum)) > -1 + call tutor#CheckLine(lnum) + endif + endif +endfunction + +function! tutor#CheckLine(line) + if exists('b:tutor_metadata') && has_key(b:tutor_metadata, 'expect') + let bufn = bufnr('%') + let ctext = getline(a:line) + if b:tutor_metadata['expect'][string(a:line)] == -1 || ctext ==# b:tutor_metadata['expect'][string(a:line)] + exe "sign place ".b:tutor_sign_id." line=".a:line." name=tutorok buffer=".bufn + else + exe "sign place ".b:tutor_sign_id." line=".a:line." name=tutorbad buffer=".bufn + endif + let b:tutor_sign_id+=1 + endif +endfunction + " Tutor Cmd: {{{1 function! s:Locale() -- cgit From 8e48d7c19e0a01d17fbda1c21a8fa69c039cec7a Mon Sep 17 00:00:00 2001 From: Felipe Morales Date: Thu, 13 Jul 2017 19:02:53 -0400 Subject: tutor: disable old method for setting "expect" regions tutor: remove movement mappings --- runtime/autoload/tutor.vim | 163 --------------------------------------------- 1 file changed, 163 deletions(-) (limited to 'runtime/autoload/tutor.vim') diff --git a/runtime/autoload/tutor.vim b/runtime/autoload/tutor.vim index 1f0668c1b3..2217fabeda 100644 --- a/runtime/autoload/tutor.vim +++ b/runtime/autoload/tutor.vim @@ -17,30 +17,6 @@ endfunction " Mappings: {{{1 -function! s:CheckMaps() - nmap -endfunction - -function! s:MapKeyWithRedirect(key, cmd) - if maparg(a:key) !=# '' - redir => l:keys - silent call s:CheckMaps() - redir END - let l:key_list = split(l:keys, '\n') - - let l:raw_map = filter(copy(l:key_list), "v:val =~# '\\* ".a:key."'") - if len(l:raw_map) == 0 - exe "nnoremap ".a:key." ".a:cmd - return - endif - let l:map_data = split(l:raw_map[0], '\s*') - - exe "nnoremap ".l:map_data[0]." ".a:cmd - else - exe "nnoremap ".a:key." ".a:cmd - endif -endfunction - function! tutor#MouseDoubleClick() if foldclosed(line('.')) > -1 normal! zo @@ -60,111 +36,11 @@ function! tutor#InjectCommand() endfunction function! tutor#SetNormalMappings() - call s:MapKeyWithRedirect('l', 'tutor#ForwardSkipConceal(v:count1)') - call s:MapKeyWithRedirect('h', 'tutor#BackwardSkipConceal(v:count1)') - call s:MapKeyWithRedirect('', 'tutor#ForwardSkipConceal(v:count1)') - call s:MapKeyWithRedirect('', 'tutor#BackwardSkipConceal(v:count1)') - nnoremap :call tutor#FollowLink(0) nnoremap <2-LeftMouse> :call tutor#MouseDoubleClick() nnoremap >> :call tutor#InjectCommand() endfunction -function! tutor#SetSampleTextMappings() - noremap A :if match(getline('.'), '^--->') > -1 \| call search('\s{\@=', 'Wc') \| startinsert \| else \| startinsert! \| endif - noremap $ :if match(getline('.'), '^--->') > -1 \| call search('.\s{\@=', 'Wc') \| else \| call search('$', 'Wc') \| endif - onoremap $ :if match(getline('.'), '^--->') > -1 \| call search('.\s{\@=', 'Wc') \| else \| call search('$', 'Wc') \| endif - noremap ^ :if match(getline('.'), '^--->') > -1 \| call search('\(--->\s\)\@<=.', 'bcW') \| else \| call search('^', 'bcW') \|endif - onoremap ^ :if match(getline('.'), '^--->') > -1 \| call search('\(--->\s\)\@<=.', 'bcW') \| else \| call search('^', 'bcW') \|endif - nmap 0 ^ - nmap ^ - nmap $ - imap ^:startinsert - imap $:startinsert - noremap I :exe "normal! 0" \| startinsert -endfunction - -" Navigation: {{{1 - -" taken from http://stackoverflow.com/a/24224578 - -function! tutor#ForwardSkipConceal(count) - let cnt=a:count - let mvcnt=0 - let c=col('.') - let l=line('.') - let lc=col('$') - let line=getline('.') - while cnt - if c>=lc - let mvcnt+=cnt - break - endif - if stridx(&concealcursor, 'n')==-1 - let isconcealed=0 - else - let [isconcealed, cchar, group] = synconcealed(l, c) - endif - if isconcealed - let cnt-=strchars(cchar) - let oldc=c - let c+=1 - while c < lc - let [isconcealed2, cchar2, group2] = synconcealed(l, c) - if !isconcealed2 || cchar2 != cchar - break - endif - let c+= 1 - endwhile - let mvcnt+=strchars(line[oldc-1:c-2]) - else - let cnt-=1 - let mvcnt+=1 - let c+=len(matchstr(line[c-1:], '.')) - endif - endwhile - return mvcnt.'l' -endfunction - -function! tutor#BackwardSkipConceal(count) - let cnt=a:count - let mvcnt=0 - let c=col('.') - let l=line('.') - let lc=0 - let line=getline('.') - while cnt - if c<=1 - let mvcnt+=cnt - break - endif - if stridx(&concealcursor, 'n')==-1 || c == 0 - let isconcealed=0 - else - let [isconcealed, cchar, group]=synconcealed(l, c-1) - endif - if isconcealed - let cnt-=strchars(cchar) - let oldc=c - let c-=1 - while c>1 - let [isconcealed2, cchar2, group2] = synconcealed(l, c-1) - if !isconcealed2 || cchar2 != cchar - break - endif - let c-=1 - endwhile - let c = max([c, 1]) - let mvcnt+=strchars(line[c-1:oldc-2]) - else - let cnt-=1 - let mvcnt+=1 - let c-=len(matchstr(line[:c-2], '.$')) - endif - endwhile - return mvcnt.'h' -endfunction - " Hypertext: {{{1 function! tutor#FollowLink(force) @@ -217,46 +93,7 @@ function! tutor#LoadMetadata() endfunction " Marks: {{{1 -" Internal: {{{2 -function! tutor#PlaceXMarks() - call cursor(1, 1) - let b:tutor_sign_id = 1 - while search('^--->', 'W') > 0 - call tutor#CheckText(getline('.')) - let b:tutor_sign_id+=1 - endwhile - call cursor(1, 1) -endfunction - -function! tutor#CheckText(text) - if match(a:text, '{expect:ANYTHING}\s*$') == -1 - if match(getline('.'), '^--->\s*$') > -1 - exe "sign place ".b:tutor_sign_id." line=".line('.')." name=tutorbad buffer=".bufnr('%') - else - if match(getline('.'), '|expect:.\+|') == -1 - let l:cur_text = matchstr(a:text, '---> \zs.\{-}\ze {expect:') - let l:expected_text = matchstr(a:text, '{expect:\zs.*\ze}\s*$') - else - let l:cur_text = matchstr(a:text, '---> \zs.\{-}\ze |expect:') - let l:expected_text = matchstr(a:text, '|expect:\zs.*\ze|\s*$') - endif - if l:cur_text ==# l:expected_text - exe "sign place ".b:tutor_sign_id." line=".line('.')." name=tutorok buffer=".bufnr('%') - else - exe "sign place ".b:tutor_sign_id." line=".line('.')." name=tutorbad buffer=".bufnr('%') - endif - endif - endif -endfunction - -function! tutor#XmarksOnTextChanged() - let l:text = getline('.') - if match(l:text, '^--->') > -1 - call tutor#CheckText(l:text) - endif -endfunction -" External: {{{2 function! tutor#ApplyMarks() if exists('b:tutor_metadata') && has_key(b:tutor_metadata, 'expect') let b:tutor_sign_id = 1 -- cgit From 37af8599307819c66da287ed3cb0babf6295dbfc Mon Sep 17 00:00:00 2001 From: Felipe Morales Date: Fri, 14 Jul 2017 19:46:31 -0400 Subject: tutor: update syntax sampletext regions no longer supported make sure tutorExpect is available don't conceal code region delimiters --- runtime/autoload/tutor.vim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'runtime/autoload/tutor.vim') diff --git a/runtime/autoload/tutor.vim b/runtime/autoload/tutor.vim index 2217fabeda..c03326c7fb 100644 --- a/runtime/autoload/tutor.vim +++ b/runtime/autoload/tutor.vim @@ -95,11 +95,12 @@ endfunction " Marks: {{{1 function! tutor#ApplyMarks() + hi! link tutorExpect Special if exists('b:tutor_metadata') && has_key(b:tutor_metadata, 'expect') let b:tutor_sign_id = 1 for expct in keys(b:tutor_metadata['expect']) let lnum = eval(expct) - call matchaddpos('tutorSampleText', [lnum]) + call matchaddpos('tutorExpect', [lnum]) call tutor#CheckLine(lnum) endfor endif -- cgit From 3241bce6116cdd17f93651f12b5908e57e1d3136 Mon Sep 17 00:00:00 2001 From: Felipe Morales Date: Sat, 15 Jul 2017 09:14:26 -0400 Subject: tutor: reorganize --- runtime/autoload/tutor.vim | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) (limited to 'runtime/autoload/tutor.vim') diff --git a/runtime/autoload/tutor.vim b/runtime/autoload/tutor.vim index c03326c7fb..56e2283465 100644 --- a/runtime/autoload/tutor.vim +++ b/runtime/autoload/tutor.vim @@ -15,14 +15,25 @@ function! tutor#SetupVim() endif endfunction +" Loads metadata file, if available +function! tutor#LoadMetadata() + let b:tutor_metadata = json_decode(join(readfile(expand('%').'.json'), "\n")) +endfunction + " Mappings: {{{1 +function! tutor#SetNormalMappings() + nnoremap :call tutor#FollowLink(0) + nnoremap <2-LeftMouse> :call tutor#MouseDoubleClick() + nnoremap >> :call tutor#InjectCommand() +endfunction + function! tutor#MouseDoubleClick() if foldclosed(line('.')) > -1 normal! zo else if match(getline('.'), '^#\{1,} ') > -1 - normal! zc + silent normal! zc else call tutor#FollowLink(0) endif @@ -35,14 +46,6 @@ function! tutor#InjectCommand() redraw | echohl WarningMsg | echon "tutor: ran" | echohl None | echon " " | echohl Statement | echon l:cmd endfunction -function! tutor#SetNormalMappings() - nnoremap :call tutor#FollowLink(0) - nnoremap <2-LeftMouse> :call tutor#MouseDoubleClick() - nnoremap >> :call tutor#InjectCommand() -endfunction - -" Hypertext: {{{1 - function! tutor#FollowLink(force) let l:stack_s = join(map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")'), '') if l:stack_s =~# 'tutorLink' @@ -85,12 +88,6 @@ function! tutor#InfoText() return join(l:info_parts, " ") endfunction -function! tutor#LoadMetadata() - try - let b:tutor_metadata = json_decode(join(readfile(expand('%').'.json'), "\n")) - catch - endtry -endfunction " Marks: {{{1 -- cgit