Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " Vim indent file |
| 2 | " Language: Dylan |
Bram Moolenaar | 6e64922 | 2021-10-04 21:32:54 +0100 | [diff] [blame] | 3 | " Maintainer: Brent A. Fulgham <bfulgham@debian.org> (Invalid email address) |
| 4 | " Doug Kearns <dougkearns@gmail.com> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5 | " Version: 0.01 |
Bram Moolenaar | cbaff5e | 2022-04-08 17:45:08 +0100 | [diff] [blame] | 6 | " Last Change: 2022 Apr 06 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7 | |
| 8 | " Only load this indent file when no other was loaded. |
| 9 | if exists("b:did_indent") |
| 10 | finish |
| 11 | endif |
| 12 | let b:did_indent = 1 |
| 13 | |
| 14 | setlocal indentkeys+==~begin,=~block,=~case,=~cleanup,=~define,=~end,=~else,=~elseif,=~exception,=~for,=~finally,=~if,=~otherwise,=~select,=~unless,=~while |
| 15 | |
| 16 | " Define the appropriate indent function but only once |
| 17 | setlocal indentexpr=DylanGetIndent() |
Bram Moolenaar | cbaff5e | 2022-04-08 17:45:08 +0100 | [diff] [blame] | 18 | |
| 19 | let b:undo_indent = "setl inde< indk<" |
| 20 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 21 | if exists("*DylanGetIndent") |
| 22 | finish |
| 23 | endif |
| 24 | |
| 25 | function DylanGetIndent() |
| 26 | " Get the line to be indented |
| 27 | let cline = getline(v:lnum) |
| 28 | |
| 29 | " Don't reindent comments on first column |
| 30 | if cline =~ '^/\[/\*]' |
| 31 | return 0 |
| 32 | endif |
| 33 | |
| 34 | "Find the previous non-blank line |
| 35 | let lnum = prevnonblank(v:lnum - 1) |
| 36 | "Use zero indent at the top of the file |
| 37 | if lnum == 0 |
| 38 | return 0 |
| 39 | endif |
| 40 | |
| 41 | let prevline=getline(lnum) |
| 42 | let ind = indent(lnum) |
| 43 | let chg = 0 |
| 44 | |
| 45 | " If previous line was a comment, use its indent |
| 46 | if prevline =~ '^\s*//' |
| 47 | return ind |
| 48 | endif |
| 49 | |
| 50 | " If previous line was a 'define', indent |
| 51 | if prevline =~? '\(^\s*\(begin\|block\|case\|define\|else\|elseif\|for\|finally\|if\|select\|unless\|while\)\|\s*\S*\s*=>$\)' |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 52 | let chg = shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 53 | " local methods indent the shift-width, plus 6 for the 'local' |
| 54 | elseif prevline =~? '^\s*local' |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 55 | let chg = shiftwidth() + 6 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 56 | " If previous line was a let with no closing semicolon, indent |
| 57 | elseif prevline =~? '^\s*let.*[^;]\s*$' |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 58 | let chg = shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 59 | " If previous line opened a parenthesis, and did not close it, indent |
| 60 | elseif prevline =~ '^.*(\s*[^)]*\((.*)\)*[^)]*$' |
| 61 | return = match( prevline, '(.*\((.*)\|[^)]\)*.*$') + 1 |
| 62 | "elseif prevline =~ '^.*(\s*[^)]*\((.*)\)*[^)]*$' |
| 63 | elseif prevline =~ '^[^(]*)\s*$' |
| 64 | " This line closes a parenthesis. Find opening |
| 65 | let curr_line = prevnonblank(lnum - 1) |
| 66 | while curr_line >= 0 |
| 67 | let str = getline(curr_line) |
| 68 | if str !~ '^.*(\s*[^)]*\((.*)\)*[^)]*$' |
| 69 | let curr_line = prevnonblank(curr_line - 1) |
| 70 | else |
| 71 | break |
| 72 | endif |
| 73 | endwhile |
| 74 | if curr_line < 0 |
| 75 | return -1 |
| 76 | endif |
| 77 | let ind = indent(curr_line) |
| 78 | " Although we found the closing parenthesis, make sure this |
| 79 | " line doesn't start with an indentable command: |
| 80 | let curr_str = getline(curr_line) |
| 81 | if curr_str =~? '^\s*\(begin\|block\|case\|define\|else\|elseif\|for\|finally\|if\|select\|unless\|while\)' |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 82 | let chg = shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 83 | endif |
| 84 | endif |
| 85 | |
| 86 | " If a line starts with end, un-indent (even if we just indented!) |
| 87 | if cline =~? '^\s*\(cleanup\|end\|else\|elseif\|exception\|finally\|otherwise\)' |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 88 | let chg = chg - shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 89 | endif |
| 90 | |
| 91 | return ind + chg |
| 92 | endfunction |
| 93 | |
| 94 | " vim:sw=2 tw=130 |