diff --git a/autoload/latex/change.vim b/autoload/latex/change.vim index 60e1a94..2ea7caf 100644 --- a/autoload/latex/change.vim +++ b/autoload/latex/change.vim @@ -5,10 +5,8 @@ function! latex#change#init(initialized) nnoremap cse :call latex#change#env_prompt() - nnoremap tse :call latex#change#env_toggle_star() - - nnoremap tsl :call latex#change#delim_toggle() - nnoremap tsr :call latex#change#delim_toggle() + nnoremap tse :call latex#change#toggle_env_star() + nnoremap tsd :call latex#change#toggle_delim() endif endfunction @@ -24,40 +22,6 @@ function! latex#change#delim(open, close) call setline(l2, line) endfunction -" {{{1 latex#change#delim_toggle -function! latex#change#delim_toggle() - " - " Toggle \left and \right variants of delimiters - " - let [d1, l1, c1, d2, l2, c2] = latex#util#get_delim() - - if d1 =~ 'left' - let newd1 = substitute(d1, '\\left', '', '') - let newd2 = substitute(d2, '\\right', '', '') - elseif d1 !~ '\cbigg\?' - let newd1 = '\left' . d1 - let newd2 = '\right' . d2 - else - return - endif - - let line = getline(l1) - let line = strpart(line, 0, c1 - 1) . newd1 . strpart(line, c1 + len(d1) - 1) - call setline(l1, line) - - if l1 == l2 - let n = len(newd1) - len(d1) - let c2 += n - let pos = getpos('.') - let pos[2] += n - call setpos('.', pos) - endif - - let line = getline(l2) - let line = strpart(line, 0, c2 - 1) . newd2 . strpart(line, c2 + len(d2) - 1) - call setline(l2, line) -endfunction - " {{{1 latex#change#env function! latex#change#env(new_env) let [env, l1, c1, l2, c2] = latex#util#get_env(1) @@ -102,8 +66,42 @@ function! latex#change#env_prompt() endif endfunction -" {{{1 latex#change#env_toggle_star -function! latex#change#env_toggle_star() +" {{{1 latex#change#toggle_delim +function! latex#change#toggle_delim() + " + " Toggle \left and \right variants of delimiters + " + let [d1, l1, c1, d2, l2, c2] = latex#util#get_delim() + + if d1 =~ 'left' + let newd1 = substitute(d1, '\\left', '', '') + let newd2 = substitute(d2, '\\right', '', '') + elseif d1 !~ '\cbigg\?' + let newd1 = '\left' . d1 + let newd2 = '\right' . d2 + else + return + endif + + let line = getline(l1) + let line = strpart(line, 0, c1 - 1) . newd1 . strpart(line, c1 + len(d1) - 1) + call setline(l1, line) + + if l1 == l2 + let n = len(newd1) - len(d1) + let c2 += n + let pos = getpos('.') + let pos[2] += n + call setpos('.', pos) + endif + + let line = getline(l2) + let line = strpart(line, 0, c2 - 1) . newd2 . strpart(line, c2 + len(d2) - 1) + call setline(l2, line) +endfunction + +" {{{1 latex#change#toggle_env_star +function! latex#change#toggle_env_star() let env = latex#util#get_env() if env == '\(' @@ -155,32 +153,6 @@ function! latex#change#wrap_selection_prompt(...) endfunction " }}}1 -" Work in progress -" {{{1 latex#change#surround -function! latex#change#surround() - let pos = getpos('.') - - let a = getchar() - if a =~ '^\d\+$' - let a = nr2char(a) - endif - - if a == "e" - call latex#change#env_prompt() - elseif a == " - echo "Not implemented" - "let b = getchar() - "if b =~ '^\d\+$' - " let b = nr2char(b) - "endif - " - "echo 'a=' . a . ' b=' . b - endif - - call setpos('.', pos) -endfunction -"}}}1 - " {{{1 s:sidwrap let s:SID = matchstr(expand(''), '\zs\d\+_\ze.*$') function! s:sidwrap(func) diff --git a/autoload/latex/motion.vim b/autoload/latex/motion.vim index 3929143..c3ef26e 100644 --- a/autoload/latex/motion.vim +++ b/autoload/latex/motion.vim @@ -21,15 +21,20 @@ function! latex#motion#init(initialized) onoremap [] :normal v[] onoremap [[ :normal v[[ - vnoremap ie :latex#motion#select_current_env('inner') - vnoremap ae :latex#motion#select_current_env('outer') + vnoremap ie :call latex#motion#select_environment(1) + vnoremap ae :call latex#motion#select_environment() onoremap ie :normal vie onoremap ae :normal vae - vnoremap i$ :latex#motion#select_inline_math('inner') - vnoremap a$ :latex#motion#select_inline_math('outer') + vnoremap i$ :call latex#motion#select_inline_math(1) + vnoremap a$ :call latex#motion#select_inline_math() onoremap i$ :normal vi$ onoremap a$ :normal va$ + + vnoremap id :call latex#motion#select_delimiter(1) + vnoremap ad :call latex#motion#select_delimiter() + onoremap id :normal vi( + onoremap ad :normal va( endif " @@ -55,9 +60,6 @@ endfunction " {{{1 latex#motion#find_matching_pair function! latex#motion#find_matching_pair(...) - " - " Note: This code is ugly, but it seems to work. - " if a:0 > 0 normal! gv endif @@ -68,18 +70,14 @@ function! latex#motion#find_matching_pair(...) let nl = line('.') let nc = col('.') - " Combine all open/close pats - let all_pats = join(g:latex_motion_open_pats+g:latex_motion_close_pats, '\|') - let all_pats = '\C\(' . all_pats . '\|\$\)' - " Find delimiter under cursor - let [lnum, cnum] = searchpos(all_pats, 'cbnW', nl-2) - let delim = matchstr(getline(lnum), '^'. all_pats, cnum-1) + let [lnum, cnum] = searchpos(s:delimiters, 'cbnW', nl-2) + let delim = matchstr(getline(lnum), '^' . s:delimiters, cnum-1) " If delimiter not found, try to search forward instead if empty(delim) - let [lnum, cnum] = searchpos(all_pats, 'cnW', nl+2) - let delim = matchstr(getline(lnum), '^'. all_pats, cnum-1) + let [lnum, cnum] = searchpos(s:delimiters, 'cnW', nl+2) + let delim = matchstr(getline(lnum), '^'. s:delimiters, cnum-1) if empty(delim) return endif @@ -100,9 +98,9 @@ function! latex#motion#find_matching_pair(...) call cursor(lnum2,cnum2) else - for i in range(len(g:latex_motion_open_pats)) - let open_pat = '\C' . b:notbslash . g:latex_motion_open_pats[i] - let close_pat = '\C' . b:notbslash . g:latex_motion_close_pats[i] + for i in range(len(s:delimiters)) + let open_pat = '\C' . b:notbslash . s:delimiters_open[i] + let close_pat = '\C' . b:notbslash . s:delimiters_close[i] if delim =~# '^' . open_pat call searchpairpos(open_pat, '', close_pat, @@ -135,25 +133,13 @@ function! latex#motion#next_sec(type, backwards, visual) endif " Define search pattern and do the search while preserving "/ - let save_search = @/ let flags = 'W' if a:backwards let flags = 'b' . flags endif - " Define section pattern - let sec_pat = join([ - \ '(sub)*section', - \ 'chapter', - \ 'part', - \ 'appendix', - \ '(front|back|main)matter', - \ ], '|') - let sec_pat = b:notcomment . '\v\s*\\(' . sec_pat . ')>' - " Perform the search - call search(sec_pat, flags) - let @/ = save_search + call search(s:section, flags) " For the [] and ][ commands we move down or up after the search if a:type == 1 @@ -165,11 +151,48 @@ function! latex#motion#next_sec(type, backwards, visual) endif endfunction -" {{{1 latex#motion#select_current_env -function! latex#motion#select_current_env(seltype) +" {{{1 latex#motion#select_delimiter +function! latex#motion#select_delimiter(...) + let inner = a:0 > 0 + + let [d1, l1, c1, d2, l2, c2] = latex#util#get_delim() + + if inner + let c1 += len(d1) + if c1 != len(getline(l1)) + let l1 += 1 + let c1 = 1 + endif + endif + + if inner + let c2 -= 1 + if c2 < 1 + let l2 -= 1 + let c2 = len(getline(l2)) + endif + else + let c2 += len(d2) - 1 + endif + + if l1 < l2 || (l1 == l2 && c1 < c2) + call cursor(l1,c1) + if visualmode() ==# 'V' + normal! V + else + normal! v + endif + call cursor(l2,c2) + endif +endfunction + +" {{{1 latex#motion#select_environment +function! latex#motion#select_environment(...) + let inner = a:0 > 0 + let [env, lnum, cnum, lnum2, cnum2] = latex#util#get_env(1) call cursor(lnum, cnum) - if a:seltype == 'inner' + if inner if env =~ '^\' call search('\\.\_\s*\S', 'eW') else @@ -182,7 +205,7 @@ function! latex#motion#select_current_env(seltype) normal! v endif call cursor(lnum2, cnum2) - if a:seltype == 'inner' + if inner call search('\S\_\s*', 'bW') else if env =~ '^\' @@ -194,8 +217,8 @@ function! latex#motion#select_current_env(seltype) endfunction " {{{1 latex#motion#select_inline_math -function! latex#motion#select_inline_math(seltype) - " seltype is either 'inner' or 'outer' +function! latex#motion#select_inline_math(...) + let inner = a:0 > 0 let dollar_pat = '\\\@' + +" }}}1 + " {{{1 s:highlight_matching_pair function! s:highlight_matching_pair(...) if latex#util#in_comment() | return | endif @@ -237,13 +295,9 @@ function! s:highlight_matching_pair(...) let nc = col('.') let line = getline(nl) - " Combine all open/close pats - let all_pats = join(g:latex_motion_open_pats+g:latex_motion_close_pats, '\|') - let all_pats = '\C\(' . all_pats . '\|\$\)' - " Find delimiter under cursor - let cnum = searchpos(all_pats, 'cbnW', nl)[1] - let delim = matchstr(line, '^'. all_pats, cnum-1) + let cnum = searchpos(s:delimiters, 'cbnW', nl)[1] + let delim = matchstr(line, '^'. s:delimiters, cnum-1) " Only highlight when cursor is on delimiters if empty(delim) || strlen(delim)+cnum-hmode < nc @@ -270,26 +324,26 @@ function! s:highlight_matching_pair(...) " " Match other delimitors " - for i in range(len(g:latex_motion_open_pats)) - let open_pat = '\C' . b:notbslash . g:latex_motion_open_pats[i] - let close_pat = '\C' . b:notbslash . g:latex_motion_close_pats[i] + for i in range(len(s:delimiters_open)) + let open_pat = '\C' . b:notbslash . s:delimiters_open[i] + let close_pat = '\C' . b:notbslash . s:delimiters_close[i] if delim =~# '^' . open_pat let [lnum2, cnum2] = searchpairpos(open_pat, '', close_pat, \ 'nW', 'latex#util#in_comment()', line('w$'), 200) execute '2match MatchParen /\%(\%' . nl . 'l\%' . cnum - \ . 'c' . g:latex_motion_open_pats[i] . '\|\%' + \ . 'c' . s:delimiters_open[i] . '\|\%' \ . lnum2 . 'l\%' . cnum2 . 'c' - \ . g:latex_motion_close_pats[i] . '\)/' + \ . s:delimiters_close[i] . '\)/' return elseif delim =~# '^' . close_pat let [lnum2, cnum2] = searchpairpos(open_pat, '', \ '\C\%(\%'. nl . 'l\%' . cnum . 'c\)\@!' . close_pat, \ 'bnW', 'latex#util#in_comment()', line('w0'), 200) execute '2match MatchParen /\%(\%' . lnum2 . 'l\%' . cnum2 - \ . 'c' . g:latex_motion_open_pats[i] . '\|\%' + \ . 'c' . s:delimiters_open[i] . '\|\%' \ . nl . 'l\%' . cnum . 'c' - \ . g:latex_motion_close_pats[i] . '\)/' + \ . s:delimiters_close[i] . '\)/' return endif endfor diff --git a/autoload/latex/util.vim b/autoload/latex/util.vim index 22dac22..78fc17c 100644 --- a/autoload/latex/util.vim +++ b/autoload/latex/util.vim @@ -142,27 +142,7 @@ function! latex#util#get_env(...) endfunction " {{{1 latex#util#get_delim -let s:latex_delim_open = [ - \ '(', - \ '\[', - \ '\\{', - \ '\\\[', - \ '\\\Cleft\s*\%([^\\]\|\\.\|\\\a*\)', - \ '\\\cbigg\?\((\|\[\|\\{\)', - \ ] -let s:latex_delim_close = [ - \ ')', - \ '\]', - \ '\\}', - \ '\\\]', - \ '\\\Cright\s*\%([^\\]\|\\.\|\\\a*\)', - \ '\\\cbigg\?\()\|\]\|\\}\)', - \ ] function! latex#util#get_delim() - " - " Note: This functions has not been optimized - " - " Save position in order to restore before finishing let pos_original = getpos('.') @@ -170,7 +150,7 @@ function! latex#util#get_delim() let pos_save = getpos('.') " Check if the cursor is on top of a closing delimiter - let close_pats = '\(' . join(s:latex_delim_close, '\|') . '\)' + let close_pats = '\(' . join(s:delimiters_close, '\|') . '\)' let lnum = pos_save[1] let cnum = pos_save[2] let [lnum, cnum] = searchpos(close_pats, 'cbnW', lnum) @@ -187,10 +167,10 @@ function! latex#util#get_delim() let l2=1000000 let c1=1000000 let c2=1000000 - for i in range(len(s:latex_delim_open)) + for i in range(len(s:delimiters_open)) call setpos('.', pos_save) - let open = s:latex_delim_open[i] - let close = s:latex_delim_close[i] + let open = s:delimiters_open[i] + let close = s:delimiters_close[i] let flags = 'W' " Check if the cursor is on top of an opening delimiter. If it is not, @@ -221,6 +201,21 @@ function! latex#util#get_delim() return [d1,l1,c1,d2,l2,c2] endfunction +let s:delimiters_open = [ + \ '(', + \ '\[', + \ '\\{', + \ '\\\Cleft\s*\%([^\\]\|\\.\|\\\a*\)', + \ '\\\cbigg\?\((\|\[\|\\{\)', + \ ] +let s:delimiters_close = [ + \ ')', + \ '\]', + \ '\\}', + \ '\\\Cright\s*\%([^\\]\|\\.\|\\\a*\)', + \ '\\\cbigg\?\()\|\]\|\\}\)', + \ ] + " {{{1 latex#util#has_syntax function! latex#util#has_syntax(name, ...) " Usage: latex#util#has_syntax(name, [line], [col]) diff --git a/doc/latex.txt b/doc/latex.txt index 0dd47fe..5058aaf 100644 --- a/doc/latex.txt +++ b/doc/latex.txt @@ -33,9 +33,11 @@ CONTENTS *vim-latex-contents* 6. Folding .............................. |vim-latex-folding| 7. Latexmk .............................. |vim-latex-latexmk| 8. Motion ............................... |vim-latex-motion| - 9. Table of contents .................... |vim-latex-toc| - 10. Function reference ................... |vim-latex-functions| - 11. Limitations .......................... |vim-latex-limitations| + 9. Change ............................... |vim-latex-change| + 10. Table of contents .................... |vim-latex-toc| + 11. Utility functions .................... |vim-latex-util| + 12. Function reference ................... |vim-latex-functions| + 13. Limitations .......................... |vim-latex-limitations| ============================================================================== INTRO *vim-latex-intro* @@ -132,32 +134,37 @@ convenience to list all the defined mappings, and it is by default mapped to 'lh'. The default mappings may be disabled with the option |g:latex_default_mappings|, if the user wishes to create his own mappings. -n % *@:call latex#motion#find_matching_pair('n') -v % *@:call latex#motion#find_matching_pair('v') -o % *@:call latex#motion#find_matching_pair('o') +n % *@:call latex#motion#find_matching_pair() +v % *@:call latex#motion#find_matching_pair(1) +o % *@:normal v% -v a$ *@:latex#motion#select_inline_math('outer') -v i$ *@:latex#motion#select_inline_math('inner') -o a$ *@:normal va$ -o i$ *@:normal vi$ +v a$ *@:call latex#motion#select_inline_math() +v i$ *@:call latex#motion#select_inline_math(1) +o a$ *@:normal va$ +o i$ *@:normal vi$ -v ae *@:latex#motion#select_current_env('outer') -v ie *@:latex#motion#select_current_env('inner') -o ae *@:normal vae -o ie *@:normal vie +v ae *@:call latex#motion#select_environment() +v ie *@:call latex#motion#select_environment(1) +o ae *@:normal vae +o ie *@:normal vie -n [[ *@:call latex#motion#next_sec(0,1,0) -n [] *@:call latex#motion#next_sec(1,1,0) -n ][ *@:call latex#motion#next_sec(1,0,0) -n ]] *@:call latex#motion#next_sec(0,0,0) -v [[ *@:call latex#motion#next_sec(0,1,1) -v [] *@:call latex#motion#next_sec(1,1,1) -v ][ *@:call latex#motion#next_sec(1,0,1) -v ]] *@:call latex#motion#next_sec(0,0,1) -o [[ *@:normal v[[ -o [] *@:normal v[] -o ][ *@:normal v][ -o ]] *@:normal v]] +v ad *@:call latex#motion#select_delimiter() +v id *@:call latex#motion#select_delimiter(1) +o ad *@:normal va( +o id *@:normal vi( + +n [[ *@:call latex#motion#next_sec(0,1,0) +n [] *@:call latex#motion#next_sec(1,1,0) +n ][ *@:call latex#motion#next_sec(1,0,0) +n ]] *@:call latex#motion#next_sec(0,0,0) +v [[ *@:call latex#motion#next_sec(0,1,1) +v [] *@:call latex#motion#next_sec(1,1,1) +v ][ *@:call latex#motion#next_sec(1,0,1) +v ]] *@:call latex#motion#next_sec(0,0,1) +o [[ *@:normal v[[ +o [] *@:normal v[] +o ][ *@:normal v][ +o ]] *@:normal v]] n li *@:call latex#info() n lh *@:call latex#help() @@ -174,10 +181,27 @@ n lG *@:call latex#latexmk#status(1) n lc *@:call latex#latexmk#clean() n lC *@:call latex#latexmk#clean(1) +n li *@:call latex#info() +n lh *@:call latex#help() +n lv *@:call latex#view() +n lR *@:call latex#reinit() +n lt *@:call latex#toc#open() +n lT *@:call latex#toc#toggle() +n ll *@:call latex#latexmk#compile() +n lk *@:call latex#latexmk#stop(1) +n lK *@:call latex#latexmk#stop_all() +n le *@:call latex#latexmk#errors() +n lg *@:call latex#latexmk#status() +n lG *@:call latex#latexmk#status(1) +n lc *@:call latex#latexmk#clean() +n lC *@:call latex#latexmk#clean(1) + n zx *@:call latex#fold#refresh()zx -n dse *@:call latex#change#env('') -n cse *@:call latex#change#env_prompt() +n dse *@:call latex#change#env('') +n cse *@:call latex#change#env_prompt() +n tse *@:call latex#change#env_toggle_star() n tse *@:call latex#change#env_toggle_star() +n tsd *@:call latex#change#delim_toggle() ============================================================================== OPTIONS *vim-latex-options* @@ -206,10 +230,8 @@ Overview:~ |g:latex_latexmk_options| |g:latex_latexmk_output| |g:latex_main_tex_candidates| - |g:latex_motion_close_pats| |g:latex_motion_enabled| |g:latex_motion_matchparen| - |g:latex_motion_open_pats| |g:latex_toc_enabled| |g:latex_toc_fold_levels| |g:latex_toc_fold| @@ -345,20 +367,6 @@ main tex file in multi-file projects. > \] < - *g:latex_motion_close_pats* -Define a list of patterns that match closing braces and environments. > - let g:latex_motion_close_pats = [ - \ '}', - \ ')', - \ '\]', - \ '\\}', - \ '\\)', - \ '\\\]', - \ '\\end\s*{.\{-}}', - \ '\\right\s*\%([^\\]\|\\.\|\\\a*\)', - \ ] -< - *g:latex_motion_enabled* Whether to enable the motion interface. If it is disabled, then neither the default mappings nor the autocommands that enable highlighting of matching @@ -372,20 +380,6 @@ but disables the builtin |matchparen|. > let g:latex_motion_matchparen = 1 < - *g:latex_motion_open_pats* -Define a list of patterns that match opening braces and environments. > - let g:latex_motion_open_pats = [ - \ '{', - \ '(', - \ '\[', - \ '\\{', - \ '\\(', - \ '\\\[', - \ '\\begin\s*{.\{-}}', - \ '\\left\s*\%([^\\]\|\\.\|\\\a*\)', - \ ] -< - *g:latex_toc_enabled* Enable interface for TOC. If it is disabled, then mappings for the TOC will not be created. > @@ -584,6 +578,34 @@ Functions: |latex#motion#select_current_env| |latex#motion#select_inline_math| +============================================================================== +CHANGE *vim-latex-change* + +Utility functions are provided to manipulate the current environments or +delimiters. + +Functions: + |latex#change#env| + |latex#change#env_prompt| + |latex#change#toggle_delim| + |latex#change#toggle_env_star| + |latex#change#wrap_selection| + |latex#change#wrap_selection_prompt| + +============================================================================== +UTILITY FUNCTIONS *vim-latex-util* + +Functions: + |latex#util#convert_back| + |latex#util#get_delim| + |latex#util#get_env| + |latex#util#has_syntax| + |latex#util#in_comment| + |latex#util#kpsewhich| + |latex#util#set_default| + |latex#util#tex2tree| + |latex#util#tree2tex| + ============================================================================== TABLE OF CONTENTS *vim-latex-toc* @@ -613,6 +635,12 @@ descriptions follow. Overview:~ + |latex#change#env| + |latex#change#env_prompt| + |latex#change#toggle_delim| + |latex#change#toggle_env_star| + |latex#change#wrap_selection| + |latex#change#wrap_selection_prompt| |latex#complete#omnifunc| |latex#complete#labels| |latex#complete#bibtex| @@ -634,11 +662,32 @@ Overview:~ |latex#reinit| |latex#toc#open| |latex#toc#toggle| + |latex#util#convert_back| + |latex#util#get_delim| + |latex#util#get_env| + |latex#util#has_syntax| + |latex#util#in_comment| + |latex#util#kpsewhich| + |latex#util#set_default| + |latex#util#tex2tree| + |latex#util#tree2tex| |latex#view| ------------------------------------------------------------------------------ Detailed descriptions:~ + *latex#change#env* + + *latex#change#env_prompt* + + *latex#change#toggle_delim* + + *latex#change#toggle_env_star* + + *latex#change#wrap_selection* + + *latex#change#wrap_selection_prompt* + *latex#complete#omnifunc* An 'omnifunc' for label and citation completion, see |vim-latex-completion|. @@ -717,6 +766,24 @@ the TOC window. Toggle TOC window: If TOC window is open, then close it. If it is closed, then open it (but do not move the cursor to the window). + *latex#util#convert_back* + + *latex#util#get_delim* + + *latex#util#get_env* + + *latex#util#has_syntax* + + *latex#util#in_comment* + + *latex#util#kpsewhich* + + *latex#util#set_default* + + *latex#util#tex2tree* + + *latex#util#tree2tex* + *latex#view* Open the output file (specified with |g:latex_latexmk_output|) with the default viewer |g:latex_viewer|. diff --git a/ftplugin/tex.vim b/ftplugin/tex.vim index 2430999..782a1e5 100644 --- a/ftplugin/tex.vim +++ b/ftplugin/tex.vim @@ -71,30 +71,6 @@ call latex#util#set_default('g:latex_errorformat_ignore_warnings', " {{{1 Motion call latex#util#set_default('g:latex_motion_enabled', 1) call latex#util#set_default('g:latex_motion_matchparen', 1) -call latex#util#set_default('g:latex_motion_open_pats', - \ [ - \ '{', - \ '(', - \ '\[', - \ '\\{', - \ '\\(', - \ '\\\[', - \ '\\begin\s*{.\{-}}', - \ '\\left\s*\%([^\\]\|\\.\|\\\a*\)', - \ '\\\cbigg\?\((\|\[\|\\{\)', - \ ]) -call latex#util#set_default('g:latex_motion_close_pats', - \ [ - \ '}', - \ ')', - \ '\]', - \ '\\}', - \ '\\)', - \ '\\\]', - \ '\\end\s*{.\{-}}', - \ '\\right\s*\%([^\\]\|\\.\|\\\a*\)', - \ '\\\cbigg\?\()\|\]\|\\}\)', - \ ]) " {{{1 Toc call latex#util#set_default('g:latex_toc_enabled', 1)