blob: 9656151e74d13686aca46516a6c35175514c7dfa [file] [log] [blame]
Bram Moolenaar7dd54322022-08-26 18:01:12 +01001vim9script
2
3# Language: ConTeXt typesetting engine
4# Maintainer: Nicola Vitacolonna <nvitacolonna@gmail.com>
5# Former Maintainers: Nikolai Weibull <now@bitwi.se>
Lifepillar0bca4a02023-12-27 18:49:50 +01006# Latest Revision: 2023 Dec 26
Bram Moolenaar46fceaa2016-10-23 21:21:08 +02007
8if exists("b:did_indent")
9 finish
10endif
11
Bram Moolenaar7dd54322022-08-26 18:01:12 +010012# Load MetaPost indentation script (this will also set b:did_indent)
Bram Moolenaar46fceaa2016-10-23 21:21:08 +020013runtime! indent/mp.vim
14
Bram Moolenaar7dd54322022-08-26 18:01:12 +010015setlocal indentexpr=ConTeXtIndent()
Bram Moolenaar46fceaa2016-10-23 21:21:08 +020016
Bram Moolenaar7dd54322022-08-26 18:01:12 +010017b:undo_indent = "setl indentexpr<"
Bram Moolenaar46fceaa2016-10-23 21:21:08 +020018
Bram Moolenaar7dd54322022-08-26 18:01:12 +010019def PrevNotComment(l: number): number
20 var prevlnum = prevnonblank(l)
Bram Moolenaar46fceaa2016-10-23 21:21:08 +020021
Bram Moolenaar7dd54322022-08-26 18:01:12 +010022 while prevlnum > 0 && getline(prevlnum) =~# '^\s*%'
23 prevlnum = prevnonblank(prevlnum - 1)
24 endwhile
25
26 return prevlnum
27enddef
28
29def FindPair(pstart: string, pmid: string, pend: string): number
30 cursor(v:lnum, 1)
31 return indent(searchpair(pstart, pmid, pend, 'bWn',
32 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"'))
33enddef
34
35def ConTeXtIndent(): number
36 # Use MetaPost rules inside MetaPost graphic environments
Bram Moolenaar46fceaa2016-10-23 21:21:08 +020037 if len(synstack(v:lnum, 1)) > 0 &&
Bram Moolenaar7dd54322022-08-26 18:01:12 +010038 synIDattr(synstack(v:lnum, 1)[0], "name") ==# 'contextMPGraphic'
39 return g:MetaPostIndent()
Bram Moolenaar46fceaa2016-10-23 21:21:08 +020040 endif
Bram Moolenaar7dd54322022-08-26 18:01:12 +010041
42 const prevlnum = PrevNotComment(v:lnum - 1)
43 const prevind = indent(prevlnum)
44 const prevline = getline(prevlnum)
45 const currline = getline(v:lnum)
46
47 # If the current line starts with ], match indentation.
48 if currline =~# '^\s*\]'
49 return FindPair('\[', '', '\]')
50 endif
51
52 # If the current line starts with }, match indentation.
53 if currline =~# '^\s*}'
54 return FindPair('{', '', '}')
55 endif
56
57 # If the previous line ends with [ or { (possibly followed by a comment) then indent.
58 if prevline =~# '[{[]\s*\%(%.*\)\=$'
59 return prevind + shiftwidth()
60 endif
61
Bram Moolenaar46fceaa2016-10-23 21:21:08 +020062 return -1
Bram Moolenaar7dd54322022-08-26 18:01:12 +010063enddef
Bram Moolenaar46fceaa2016-10-23 21:21:08 +020064
Bram Moolenaar7dd54322022-08-26 18:01:12 +010065# vim: sw=2 fdm=marker