0
0
mirror of https://github.com/vim/vim.git synced 2025-07-04 23:07:33 -04:00
vim/runtime/indent/context.vim

66 lines
1.7 KiB
VimL
Raw Normal View History

2022-08-26 18:01:12 +01:00
vim9script
2016-10-23 21:21:08 +02:00
2022-08-26 18:01:12 +01:00
# Language: ConTeXt typesetting engine
# Maintainer: Nicola Vitacolonna <nvitacolonna@gmail.com>
# Former Maintainers: Nikolai Weibull <now@bitwi.se>
# Latest Revision: 2022 Aug 12
2016-10-23 21:21:08 +02:00
2022-08-26 18:01:12 +01:00
if exists("b:did_indent")
2016-10-23 21:21:08 +02:00
finish
endif
2022-08-26 18:01:12 +01:00
# Load MetaPost indentation script (this will also set b:did_indent)
2016-10-23 21:21:08 +02:00
runtime! indent/mp.vim
2022-08-26 18:01:12 +01:00
setlocal indentexpr=ConTeXtIndent()
2016-10-23 21:21:08 +02:00
2022-08-26 18:01:12 +01:00
b:undo_indent = "setl indentexpr<"
2016-10-23 21:21:08 +02:00
2022-08-26 18:01:12 +01:00
def PrevNotComment(l: number): number
var prevlnum = prevnonblank(l)
2016-10-23 21:21:08 +02:00
2022-08-26 18:01:12 +01:00
while prevlnum > 0 && getline(prevlnum) =~# '^\s*%'
prevlnum = prevnonblank(prevlnum - 1)
endwhile
return prevlnum
enddef
def FindPair(pstart: string, pmid: string, pend: string): number
cursor(v:lnum, 1)
return indent(searchpair(pstart, pmid, pend, 'bWn',
'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"'))
enddef
def ConTeXtIndent(): number
# Use MetaPost rules inside MetaPost graphic environments
2016-10-23 21:21:08 +02:00
if len(synstack(v:lnum, 1)) > 0 &&
2022-08-26 18:01:12 +01:00
synIDattr(synstack(v:lnum, 1)[0], "name") ==# 'contextMPGraphic'
return g:MetaPostIndent()
2016-10-23 21:21:08 +02:00
endif
2022-08-26 18:01:12 +01:00
const prevlnum = PrevNotComment(v:lnum - 1)
const prevind = indent(prevlnum)
const prevline = getline(prevlnum)
const currline = getline(v:lnum)
# If the current line starts with ], match indentation.
if currline =~# '^\s*\]'
return FindPair('\[', '', '\]')
endif
# If the current line starts with }, match indentation.
if currline =~# '^\s*}'
return FindPair('{', '', '}')
endif
# If the previous line ends with [ or { (possibly followed by a comment) then indent.
if prevline =~# '[{[]\s*\%(%.*\)\=$'
return prevind + shiftwidth()
endif
return -1
enddef
2016-10-23 21:21:08 +02:00
2022-08-26 18:01:12 +01:00
# vim: sw=2 fdm=marker