Implement #571: Fold newcommands and similar

This commit is contained in:
Karl Yngve Lervåg 2016-10-02 15:42:32 +02:00
parent 9ee94751e7
commit 57aeb0fe63
3 changed files with 126 additions and 0 deletions

View File

@ -28,6 +28,7 @@ function! vimtex#fold#init_options() " {{{1
\ ]) \ ])
call vimtex#util#set_default('g:vimtex_fold_documentclass', 0) call vimtex#util#set_default('g:vimtex_fold_documentclass', 0)
call vimtex#util#set_default('g:vimtex_fold_usepackage', 1) call vimtex#util#set_default('g:vimtex_fold_usepackage', 1)
call vimtex#util#set_default('g:vimtex_fold_newcommands', 1)
" Disable manual mode in vimdiff " Disable manual mode in vimdiff
let g:vimtex_fold_manual = &diff ? 0 : g:vimtex_fold_manual let g:vimtex_fold_manual = &diff ? 0 : g:vimtex_fold_manual
@ -44,6 +45,7 @@ function! vimtex#fold#init_script() " {{{1
" List of identifiers for improving efficiency " List of identifiers for improving efficiency
" "
let s:folded = '\v^\s*\%|^\s*\]\{' let s:folded = '\v^\s*\%|^\s*\]\{'
let s:folded .= '|^\s*}\s*$'
let s:folded .= '|\\%(' . join([ let s:folded .= '|\\%(' . join([
\ 'begin', \ 'begin',
\ 'end', \ 'end',
@ -54,6 +56,7 @@ function! vimtex#fold#init_script() " {{{1
\ 'appendix', \ 'appendix',
\ 'part', \ 'part',
\ 'usepackage', \ 'usepackage',
\ '%(re)?new%(command|environment)',
\ ], '|') . ')' \ ], '|') . ')'
endfunction endfunction
@ -171,6 +174,20 @@ function! vimtex#fold#level(lnum) " {{{1
endif endif
endif endif
" Fold newcommands (and similar)
if g:vimtex_fold_newcommands
if line =~# '\v^\s*\\%(re)?new%(command|environment)\*?'
\ && indent(a:lnum+1) > indent(a:lnum)
let s:newcommand_indent = indent(a:lnum)
return 'a1'
elseif exists('s:newcommand_indent')
\ && indent(a:lnum) == s:newcommand_indent
\ && line =~# '^\s*}\s*$'
unlet s:newcommand_indent
return 's1'
endif
endif
" Fold chapters and sections " Fold chapters and sections
for [part, level] in b:vimtex_fold.parts for [part, level] in b:vimtex_fold.parts
if line =~# part if line =~# part
@ -275,6 +292,13 @@ function! vimtex#fold#text() " {{{1
\ . '}' \ . '}'
endif endif
" Text for newcommand (and similar)
if g:vimtex_fold_newcommands
\ && line =~# '\v^\s*\\%(re)?new%(command|environment)'
return matchstr(line,
\ '\v^\s*\\%(re)?new%(command|environment)\*?\{[^}]*\}') . ' ...'
endif
" Text for documentclass " Text for documentclass
if g:vimtex_fold_documentclass && line =~# '^\s*\\documentclass' if g:vimtex_fold_documentclass && line =~# '^\s*\\documentclass'
return '\documentclass[...]{' return '\documentclass[...]{'

View File

@ -488,6 +488,20 @@ Options~
< <
Default value: 1 Default value: 1
*g:vimtex_fold_newcommands*
Use this option to disable/enable folding of long `\[re]newcommand` and
`\[re]newenvironment` lines. The lines must be formatted like this for
folding to work properly: >
\[re]newcommand{\command}{
...,
}
<
That is, the fold region starts at the command and ends when at the final `}`
at the same indent level as the start of the command.
Default value: 1
*g:vimtex_fold_documentclass* *g:vimtex_fold_documentclass*
Use this option to disable/enable folding of long `\documentclass` lines. This Use this option to disable/enable folding of long `\documentclass` lines. This
works similar to |g:vimtex_fold_usepackage|. works similar to |g:vimtex_fold_usepackage|.
@ -1667,6 +1681,7 @@ Associated settings:
|g:vimtex_fold_sections| |g:vimtex_fold_sections|
|g:vimtex_fold_envs| |g:vimtex_fold_envs|
|g:vimtex_fold_usepackage| |g:vimtex_fold_usepackage|
|g:vimtex_fold_newcommands|
============================================================================== ==============================================================================
INDENTATION *vimtex-indent* INDENTATION *vimtex-indent*

View File

@ -0,0 +1,87 @@
\documentclass[% Options of scrbook
% draft,
fontsize=12pt,
%smallheadings,
headings=big,
english,
paper=a4,
twoside,
open=right,
DIV=14,
BCOR=20mm,
headinclude=false,
footinclude=false,
mpinclude=false,
pagesize,
titlepage,
parskip=half,
headsepline,
chapterprefix=false,
appendixprefix=Appendix,
appendixwithprefixline=true,
bibliography=totoc,
toc=graduated,
numbers=noenddot,
]{scrbook}
%
% Fold usepackages
%
\usepackage[
...
]{test}
\usepackage[
backend=biber,
style=numeric-comp,
maxcitenames=99,
doi=false,
url=false,
giveninits=true,
]{biblatex}
%
% Fold newcommands and similar
%
\renewcommand{\marginpar}{%
\marginnote%
}
\newcommand*{\StoreCiteField}[3]{%
\begingroup
\global\let\StoreCiteField@Result\relax
\citefield{#2}[StoreCiteField]{#3}%
\endgroup
\let#1\StoreCiteField@Result
}
\newenvironment{theo}[1][]{%
\stepcounter{theo}%
\ifstrempty{#1}{%
\mdfsetup{
frametitle={%
\tikz[baseline=(current bounding box.east),outer sep=0pt]%
\node[anchor=east,rectangle,fill=blue!20] {\strut Theorem~\thetheo};%
}
}%
}%
{% else ifstrempty:
\mdfsetup{
frametitle={
\tikz[baseline=(current bounding box.east),outer sep=0pt]%
\node[anchor=east,rectangle,fill=blue!20]{\strut Theorem~\thetheo:~#1};}%
}
}%
\mdfsetup{
innertopmargin=10pt,
linecolor=blue!20,
linewidth=2pt,topline=true,
frametitleaboveskip=\dimexpr-\ht\strutbox\relax,
}
\begin{mdframed}[]\relax%
}{%
\end{mdframed}%
}
\begin{document}
Hello World
\end{document}