Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " Vim indent file |
| 2 | " Language: Objective-C |
| 3 | " Maintainer: Kazunobu Kuriyama <kazunobu.kuriyama@nifty.com> |
| 4 | " Last Change: 2004 May 16 |
| 5 | " |
| 6 | |
| 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 | setlocal cindent |
| 14 | |
| 15 | " Set the function to do the work. |
| 16 | setlocal indentexpr=GetObjCIndent() |
| 17 | |
Bram Moolenaar | 6c391a7 | 2021-09-09 21:55:11 +0200 | [diff] [blame] | 18 | " To make a colon (:) suggest an indentation other than a goto/switch label, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 19 | setlocal indentkeys-=: |
| 20 | setlocal indentkeys+=<:> |
| 21 | |
| 22 | " Only define the function once. |
| 23 | if exists("*GetObjCIndent") |
| 24 | finish |
| 25 | endif |
| 26 | |
| 27 | function s:GetWidth(line, regexp) |
| 28 | let end = matchend(a:line, a:regexp) |
| 29 | let width = 0 |
| 30 | let i = 0 |
| 31 | while i < end |
| 32 | if a:line[i] != "\t" |
| 33 | let width = width + 1 |
| 34 | else |
| 35 | let width = width + &ts - (width % &ts) |
| 36 | endif |
| 37 | let i = i + 1 |
| 38 | endwhile |
| 39 | return width |
| 40 | endfunction |
| 41 | |
| 42 | function s:LeadingWhiteSpace(line) |
| 43 | let end = strlen(a:line) |
| 44 | let width = 0 |
| 45 | let i = 0 |
| 46 | while i < end |
| 47 | let char = a:line[i] |
| 48 | if char != " " && char != "\t" |
| 49 | break |
| 50 | endif |
| 51 | if char != "\t" |
| 52 | let width = width + 1 |
| 53 | else |
| 54 | let width = width + &ts - (width % &ts) |
| 55 | endif |
| 56 | let i = i + 1 |
| 57 | endwhile |
| 58 | return width |
| 59 | endfunction |
| 60 | |
| 61 | |
| 62 | function GetObjCIndent() |
| 63 | let theIndent = cindent(v:lnum) |
| 64 | |
| 65 | let prev_line = getline(v:lnum - 1) |
| 66 | let cur_line = getline(v:lnum) |
| 67 | |
| 68 | if prev_line !~# ":" || cur_line !~# ":" |
| 69 | return theIndent |
| 70 | endif |
| 71 | |
| 72 | if prev_line !~# ";" |
| 73 | let prev_colon_pos = s:GetWidth(prev_line, ":") |
| 74 | let delta = s:GetWidth(cur_line, ":") - s:LeadingWhiteSpace(cur_line) |
| 75 | let theIndent = prev_colon_pos - delta |
| 76 | endif |
| 77 | |
| 78 | return theIndent |
| 79 | endfunction |