Bram Moolenaar | 1aeaf8c | 2012-05-18 13:46:39 +0200 | [diff] [blame] | 1 | " Vim indent file |
| 2 | " Language: Zimbu |
Christian Brabandt | e978b45 | 2023-08-13 10:33:05 +0200 | [diff] [blame] | 3 | " Maintainer: The Vim Project <https://github.com/vim/vim> |
| 4 | " Last Change: 2023 Aug 10 |
| 5 | " Former Maintainer: Bram Moolenaar <Bram@vim.org> |
Bram Moolenaar | 1aeaf8c | 2012-05-18 13:46:39 +0200 | [diff] [blame] | 6 | |
| 7 | " Only load this indent file when no other was loaded. |
| 8 | if exists("b:did_indent") |
| 9 | finish |
| 10 | endif |
| 11 | let b:did_indent = 1 |
| 12 | |
| 13 | setlocal ai nolisp nocin |
| 14 | setlocal indentexpr=GetZimbuIndent(v:lnum) |
| 15 | setlocal indentkeys=0{,0},!^F,o,O,0=ELSE,0=ELSEIF,0=CASE,0=DEFAULT,0=FINALLY |
| 16 | |
| 17 | " We impose recommended defaults: no Tabs, 'shiftwidth' = 2 |
| 18 | setlocal sw=2 et |
| 19 | |
Bram Moolenaar | 6e64922 | 2021-10-04 21:32:54 +0100 | [diff] [blame] | 20 | let b:undo_indent = "setl ai< cin< et< indentkeys< indentexpr< lisp< sw<" |
Bram Moolenaar | 1aeaf8c | 2012-05-18 13:46:39 +0200 | [diff] [blame] | 21 | |
| 22 | " Only define the function once. |
| 23 | if exists("*GetZimbuIndent") |
| 24 | finish |
| 25 | endif |
| 26 | |
Bram Moolenaar | 8e52a59 | 2012-05-18 21:49:28 +0200 | [diff] [blame] | 27 | let s:cpo_save = &cpo |
| 28 | set cpo&vim |
| 29 | |
Bram Moolenaar | 1aeaf8c | 2012-05-18 13:46:39 +0200 | [diff] [blame] | 30 | " Come here when loading the script the first time. |
| 31 | |
| 32 | let s:maxoff = 50 " maximum number of lines to look backwards for () |
| 33 | |
| 34 | func GetZimbuIndent(lnum) |
| 35 | let prevLnum = prevnonblank(a:lnum - 1) |
| 36 | if prevLnum == 0 |
| 37 | " This is the first non-empty line, use zero indent. |
| 38 | return 0 |
| 39 | endif |
| 40 | |
| 41 | " Taken from Python indenting: |
| 42 | " If the previous line is inside parenthesis, use the indent of the starting |
| 43 | " line. |
| 44 | " Trick: use the non-existing "dummy" variable to break out of the loop when |
| 45 | " going too far back. |
| 46 | call cursor(prevLnum, 1) |
| 47 | let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW', |
| 48 | \ "line('.') < " . (prevLnum - s:maxoff) . " ? dummy :" |
| 49 | \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')" |
| 50 | \ . " =~ '\\(Comment\\|String\\|Char\\)$'") |
| 51 | if parlnum > 0 |
| 52 | let plindent = indent(parlnum) |
| 53 | let plnumstart = parlnum |
| 54 | else |
| 55 | let plindent = indent(prevLnum) |
| 56 | let plnumstart = prevLnum |
| 57 | endif |
| 58 | |
| 59 | |
| 60 | " When inside parenthesis: If at the first line below the parenthesis add |
| 61 | " two 'shiftwidth', otherwise same as previous line. |
| 62 | " i = (a |
| 63 | " + b |
| 64 | " + c) |
| 65 | call cursor(a:lnum, 1) |
| 66 | let p = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW', |
| 67 | \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :" |
| 68 | \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')" |
| 69 | \ . " =~ '\\(Comment\\|String\\|Char\\)$'") |
| 70 | if p > 0 |
| 71 | if p == prevLnum |
| 72 | " When the start is inside parenthesis, only indent one 'shiftwidth'. |
| 73 | let pp = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW', |
| 74 | \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :" |
| 75 | \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')" |
| 76 | \ . " =~ '\\(Comment\\|String\\|Char\\)$'") |
| 77 | if pp > 0 |
Bram Moolenaar | 298b440 | 2016-01-28 22:38:53 +0100 | [diff] [blame] | 78 | return indent(prevLnum) + shiftwidth() |
Bram Moolenaar | 1aeaf8c | 2012-05-18 13:46:39 +0200 | [diff] [blame] | 79 | endif |
Bram Moolenaar | 298b440 | 2016-01-28 22:38:53 +0100 | [diff] [blame] | 80 | return indent(prevLnum) + shiftwidth() * 2 |
Bram Moolenaar | 1aeaf8c | 2012-05-18 13:46:39 +0200 | [diff] [blame] | 81 | endif |
| 82 | if plnumstart == p |
| 83 | return indent(prevLnum) |
| 84 | endif |
| 85 | return plindent |
| 86 | endif |
| 87 | |
| 88 | let prevline = getline(prevLnum) |
| 89 | let thisline = getline(a:lnum) |
| 90 | |
| 91 | " If this line is not a comment and the previous one is then move the |
| 92 | " previous line further back. |
| 93 | if thisline !~ '^\s*#' |
| 94 | while prevline =~ '^\s*#' |
| 95 | let prevLnum = prevnonblank(prevLnum - 1) |
| 96 | if prevLnum == 0 |
| 97 | " Only comment lines before this, no indent |
| 98 | return 0 |
| 99 | endif |
| 100 | let prevline = getline(prevLnum) |
| 101 | let plindent = indent(prevLnum) |
| 102 | endwhile |
| 103 | endif |
| 104 | |
Bram Moolenaar | d09acef | 2012-09-21 14:54:30 +0200 | [diff] [blame] | 105 | if prevline =~ '^\s*\(IF\|\|ELSEIF\|ELSE\|GENERATE_IF\|\|GENERATE_ELSEIF\|GENERATE_ELSE\|WHILE\|REPEAT\|TRY\|CATCH\|FINALLY\|FOR\|DO\|SWITCH\|CASE\|DEFAULT\|FUNC\|VIRTUAL\|ABSTRACT\|DEFINE\|REPLACE\|FINAL\|PROC\|MAIN\|NEW\|ENUM\|CLASS\|INTERFACE\|BITS\|MODULE\|SHARED\)\>' |
Bram Moolenaar | 298b440 | 2016-01-28 22:38:53 +0100 | [diff] [blame] | 106 | let plindent += shiftwidth() |
Bram Moolenaar | 1aeaf8c | 2012-05-18 13:46:39 +0200 | [diff] [blame] | 107 | endif |
| 108 | if thisline =~ '^\s*\(}\|ELSEIF\>\|ELSE\>\|CATCH\|FINALLY\|GENERATE_ELSEIF\>\|GENERATE_ELSE\>\|UNTIL\>\)' |
Bram Moolenaar | 298b440 | 2016-01-28 22:38:53 +0100 | [diff] [blame] | 109 | let plindent -= shiftwidth() |
Bram Moolenaar | 1aeaf8c | 2012-05-18 13:46:39 +0200 | [diff] [blame] | 110 | endif |
| 111 | if thisline =~ '^\s*\(CASE\>\|DEFAULT\>\)' && prevline !~ '^\s*SWITCH\>' |
Bram Moolenaar | 298b440 | 2016-01-28 22:38:53 +0100 | [diff] [blame] | 112 | let plindent -= shiftwidth() |
Bram Moolenaar | 1aeaf8c | 2012-05-18 13:46:39 +0200 | [diff] [blame] | 113 | endif |
| 114 | |
| 115 | " line up continued comment that started after some code |
| 116 | " String something # comment comment |
| 117 | " # comment |
| 118 | if a:lnum == prevLnum + 1 && thisline =~ '^\s*#' && prevline !~ '^\s*#' |
| 119 | let n = match(prevline, '#') |
| 120 | if n > 1 |
| 121 | let plindent = n |
| 122 | endif |
| 123 | endif |
| 124 | |
| 125 | return plindent |
| 126 | endfunc |
| 127 | |
Bram Moolenaar | 8e52a59 | 2012-05-18 21:49:28 +0200 | [diff] [blame] | 128 | let &cpo = s:cpo_save |
| 129 | unlet s:cpo_save |