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