Bram Moolenaar | c81e5e7 | 2007-05-05 18:24:42 +0000 | [diff] [blame] | 1 | " Vim indent file |
| 2 | " Language: Hamster Script |
Bram Moolenaar | 2286304 | 2021-10-16 15:23:36 +0100 | [diff] [blame] | 3 | " Version: 2.0.6.1 |
| 4 | " Last Change: 2021 Oct 11 |
| 5 | " Maintainer: David Fishburn <dfishburn dot vim at gmail dot com> |
| 6 | " Download: https://www.vim.org/scripts/script.php?script_id=1099 |
| 7 | " |
| 8 | " 2.0.6.1 (Oct 2021) |
| 9 | " Added b:undo_indent |
| 10 | " Added cpo check |
| 11 | " |
Bram Moolenaar | c81e5e7 | 2007-05-05 18:24:42 +0000 | [diff] [blame] | 12 | |
| 13 | " Only load this indent file when no other was loaded. |
| 14 | if exists("b:did_indent") |
| 15 | finish |
| 16 | endif |
| 17 | let b:did_indent = 1 |
| 18 | |
| 19 | setlocal indentkeys+==~if,=~else,=~endif,=~endfor,=~endwhile |
| 20 | setlocal indentkeys+==~do,=~until,=~while,=~repeat,=~for,=~loop |
| 21 | setlocal indentkeys+==~sub,=~endsub |
| 22 | |
Bram Moolenaar | 2286304 | 2021-10-16 15:23:36 +0100 | [diff] [blame] | 23 | let b:undo_indent = "setl indentkeys<" |
| 24 | |
Bram Moolenaar | c81e5e7 | 2007-05-05 18:24:42 +0000 | [diff] [blame] | 25 | " Define the appropriate indent function but only once |
| 26 | setlocal indentexpr=HamGetFreeIndent() |
| 27 | if exists("*HamGetFreeIndent") |
| 28 | finish |
| 29 | endif |
| 30 | |
Bram Moolenaar | 2286304 | 2021-10-16 15:23:36 +0100 | [diff] [blame] | 31 | let s:keepcpo = &cpo |
| 32 | set cpo&vim |
| 33 | |
Bram Moolenaar | c81e5e7 | 2007-05-05 18:24:42 +0000 | [diff] [blame] | 34 | function HamGetIndent(lnum) |
| 35 | let ind = indent(a:lnum) |
| 36 | let prevline=getline(a:lnum) |
| 37 | |
| 38 | " Add a shiftwidth to statements following if, else, elseif, |
| 39 | " case, select, default, do, until, while, for, start |
| 40 | if prevline =~? '^\s*\<\(if\|else\%(if\)\?\|for\|repeat\|do\|while\|sub\)\>' |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 41 | let ind = ind + shiftwidth() |
Bram Moolenaar | c81e5e7 | 2007-05-05 18:24:42 +0000 | [diff] [blame] | 42 | endif |
| 43 | |
| 44 | " Subtract a shiftwidth from else, elseif, end(if|while|for), until |
| 45 | let line = getline(v:lnum) |
| 46 | if line =~? '^\s*\(else\|elseif\|loop\|until\|end\%(if\|while\|for\|sub\)\)\>' |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 47 | let ind = ind - shiftwidth() |
Bram Moolenaar | c81e5e7 | 2007-05-05 18:24:42 +0000 | [diff] [blame] | 48 | endif |
| 49 | |
| 50 | return ind |
| 51 | endfunction |
| 52 | |
| 53 | function HamGetFreeIndent() |
| 54 | " Find the previous non-blank line |
| 55 | let lnum = prevnonblank(v:lnum - 1) |
| 56 | |
| 57 | " Use zero indent at the top of the file |
| 58 | if lnum == 0 |
| 59 | return 0 |
| 60 | endif |
| 61 | |
| 62 | let ind=HamGetIndent(lnum) |
| 63 | return ind |
| 64 | endfunction |
| 65 | |
Bram Moolenaar | 2286304 | 2021-10-16 15:23:36 +0100 | [diff] [blame] | 66 | " Restore: |
| 67 | let &cpo = s:keepcpo |
| 68 | unlet s:keepcpo |
| 69 | |
Bram Moolenaar | c81e5e7 | 2007-05-05 18:24:42 +0000 | [diff] [blame] | 70 | " vim:sw=2 tw=80 |