Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " PostScript indent file |
Bram Moolenaar | 6e64922 | 2021-10-04 21:32:54 +0100 | [diff] [blame] | 2 | " Language: PostScript |
Bram Moolenaar | 2d8ed02 | 2022-05-21 13:08:16 +0100 | [diff] [blame] | 3 | " Maintainer: Mike Williams <mrw@eandem.co.uk> |
Bram Moolenaar | cbaff5e | 2022-04-08 17:45:08 +0100 | [diff] [blame] | 4 | " Last Change: 2022 Apr 06 |
| 5 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6 | " Only load this indent file when no other was loaded. |
| 7 | if exists("b:did_indent") |
| 8 | finish |
| 9 | endif |
| 10 | let b:did_indent = 1 |
| 11 | |
| 12 | setlocal indentexpr=PostscrIndentGet(v:lnum) |
| 13 | setlocal indentkeys+=0],0=>>,0=%%,0=end,0=restore,0=grestore indentkeys-=:,0#,e |
| 14 | |
Bram Moolenaar | cbaff5e | 2022-04-08 17:45:08 +0100 | [diff] [blame] | 15 | let b:undo_indent = "setl inde< indk<" |
| 16 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 17 | " Catch multiple instantiations |
| 18 | if exists("*PostscrIndentGet") |
| 19 | finish |
| 20 | endif |
| 21 | |
| 22 | function! PostscrIndentGet(lnum) |
| 23 | " Find a non-empty non-comment line above the current line. |
| 24 | " Note: ignores DSC comments as well! |
| 25 | let lnum = a:lnum - 1 |
| 26 | while lnum != 0 |
| 27 | let lnum = prevnonblank(lnum) |
| 28 | if getline(lnum) !~ '^\s*%.*$' |
| 29 | break |
| 30 | endif |
| 31 | let lnum = lnum - 1 |
| 32 | endwhile |
| 33 | |
| 34 | " Hit the start of the file, use user indent. |
| 35 | if lnum == 0 |
| 36 | return -1 |
| 37 | endif |
| 38 | |
| 39 | " Start with the indent of the previous line |
| 40 | let ind = indent(lnum) |
| 41 | let pline = getline(lnum) |
| 42 | |
| 43 | " Indent for dicts, arrays, and saves with possible trailing comment |
| 44 | if pline =~ '\(begin\|<<\|g\=save\|{\|[\)\s*\(%.*\)\=$' |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 45 | let ind = ind + shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 46 | endif |
| 47 | |
| 48 | " Remove indent for popped dicts, and restores. |
| 49 | if pline =~ '\(end\|g\=restore\)\s*$' |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 50 | let ind = ind - shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 51 | |
| 52 | " Else handle immediate dedents of dicts, restores, and arrays. |
| 53 | elseif getline(a:lnum) =~ '\(end\|>>\|g\=restore\|}\|]\)' |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 54 | let ind = ind - shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 55 | |
| 56 | " Else handle DSC comments - always start of line. |
| 57 | elseif getline(a:lnum) =~ '^\s*%%' |
| 58 | let ind = 0 |
| 59 | endif |
| 60 | |
| 61 | " For now catch excessive left indents if they occur. |
| 62 | if ind < 0 |
| 63 | let ind = -1 |
| 64 | endif |
| 65 | |
| 66 | return ind |
| 67 | endfunction |
| 68 | |
| 69 | " vim:sw=2 |