Bram Moolenaar | 4a74803 | 2010-09-30 21:47:56 +0200 | [diff] [blame] | 1 | " Vim indent file |
| 2 | " Language: Falcon |
| 3 | " Maintainer: Steven Oliver <oliver.steven@gmail.com> |
| 4 | " Website: https://steveno@github.com/steveno/falconpl-vim.git |
| 5 | " Credits: Thanks to the ruby.vim authors, I borrow a lot! |
| 6 | " Previous Maintainer: Brent A. Fulgham <bfulgham@debian.org> |
| 7 | " ----------------------------------------------------------- |
| 8 | " GetLatestVimScripts: 2752 1 :AutoInstall: falcon.vim |
| 9 | |
| 10 | "====================================== |
| 11 | " SETUP |
| 12 | "====================================== |
| 13 | |
| 14 | " Only load this indent file when no other was loaded. |
| 15 | if exists("b:did_indent") |
| 16 | finish |
| 17 | endif |
| 18 | let b:did_indent = 1 |
| 19 | |
| 20 | setlocal nosmartindent |
| 21 | |
| 22 | " Setup indent function and when to use it |
| 23 | setlocal indentexpr=FalconGetIndent() |
| 24 | setlocal indentkeys=0{,0},0),0],!^F,o,O,e |
| 25 | setlocal indentkeys+==~case,=~catch,=~default,=~elif,=~else,=~end,=~\" |
| 26 | |
| 27 | " Define the appropriate indent function but only once |
| 28 | if exists("*FalconGetIndent") |
| 29 | finish |
| 30 | endif |
| 31 | |
| 32 | let s:cpo_save = &cpo |
| 33 | set cpo&vim |
| 34 | |
| 35 | "====================================== |
| 36 | " VARIABLES |
| 37 | "====================================== |
| 38 | |
| 39 | " Regex of syntax group names that are strings AND comments |
| 40 | let s:syng_strcom = '\<falcon\%(String\|StringEscape\|Comment\)\>' |
| 41 | |
| 42 | " Regex of syntax group names that are strings |
| 43 | let s:syng_string = '\<falcon\%(String\|StringEscape\)\>' |
| 44 | |
| 45 | " Keywords to indent on |
| 46 | let s:falcon_indent_keywords = '^\s*\(case\|catch\|class\|enum\|default\|elif\|else' . |
| 47 | \ '\|for\|function\|if.*"[^"]*:.*"\|if \(\(:\)\@!.\)*$\|loop\|object\|select' . |
| 48 | \ '\|switch\|try\|while\|\w*\s*=\s*\w*([$\)' |
| 49 | |
| 50 | " Keywords to deindent on |
| 51 | let s:falcon_deindent_keywords = '^\s*\(case\|catch\|default\|elif\|else\|end\)' |
| 52 | |
| 53 | "====================================== |
| 54 | " FUNCTIONS |
| 55 | "====================================== |
| 56 | |
| 57 | " Check if the character at lnum:col is inside a string |
| 58 | function s:IsInStringOrComment(lnum, col) |
| 59 | return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_strcom |
| 60 | endfunction |
| 61 | |
| 62 | "====================================== |
| 63 | " INDENT ROUTINE |
| 64 | "====================================== |
| 65 | |
| 66 | function FalconGetIndent() |
| 67 | " Get the line to be indented |
| 68 | let cline = getline(v:lnum) |
| 69 | |
| 70 | " Don't reindent comments on first column |
| 71 | if cline =~ '^\/\/' |
| 72 | return 0 |
| 73 | endif |
| 74 | |
| 75 | " Find the previous non-blank line |
| 76 | let lnum = prevnonblank(v:lnum - 1) |
| 77 | |
| 78 | " Use zero indent at the top of the file |
| 79 | if lnum == 0 |
| 80 | return 0 |
| 81 | endif |
| 82 | |
| 83 | let prevline=getline(lnum) |
| 84 | let ind = indent(lnum) |
| 85 | let chg = 0 |
| 86 | |
| 87 | " If we are in a multi-line string or line-comment, don't do anything |
| 88 | if s:IsInStringOrComment(v:lnum, matchend(cline, '^\s*') + 1 ) |
| 89 | return indent('.') |
| 90 | endif |
| 91 | |
| 92 | " If the start of the line equals a double quote, then indent to the |
| 93 | " previous lines first double quote |
| 94 | if cline =~? '^\s*"' |
| 95 | let chg = chg + &sw |
| 96 | endif |
| 97 | |
| 98 | " If previous line started with a double quote and this one |
| 99 | " doesn't, unindent |
| 100 | if prevline =~? '^\s*"' && cline =~? '^\s*' |
| 101 | let chg = chg - &sw |
| 102 | endif |
| 103 | |
| 104 | " Indent if proper keyword |
| 105 | if prevline =~? s:falcon_indent_keywords |
| 106 | let chg = &sw |
| 107 | " If previous line opened a parenthesis, and did not close it, indent |
| 108 | elseif prevline =~ '^.*(\s*[^)]*\((.*)\)*[^)]*$' |
| 109 | " Make sure this isn't just a function split between two lines |
| 110 | if prevline =~ ',\s*$' |
| 111 | return indent(prevnonblank(v:lnum - 1)) + &sw |
| 112 | else |
| 113 | return match(prevline, '(.*\((.*)\|[^)]\)*.*$') + 1 |
| 114 | endif |
| 115 | elseif prevline =~ '^[^(]*)\s*$' |
| 116 | " This line closes a parenthesis. Finds opening. |
| 117 | let curr_line = prevnonblank(lnum - 1) |
| 118 | while curr_line >= 0 |
| 119 | let str = getline(curr_line) |
| 120 | if str !~ '^.*(\s*[^)]*\((.*)\)*[^)]*$' |
| 121 | let curr_line = prevnonblank(curr_line - 1) |
| 122 | else |
| 123 | break |
| 124 | endif |
| 125 | endwhile |
| 126 | if curr_line < 0 |
| 127 | return -1 |
| 128 | endif |
| 129 | let ind = indent(curr_line) |
| 130 | endif |
| 131 | |
| 132 | " If previous line ends in a semi-colon reset indent to previous |
| 133 | " lines setting |
| 134 | if prevline =~? ';\s*$' && prevnonblank(prevline) =~? ',\s*$' |
| 135 | return chg = chg - (2 * &sw) |
| 136 | endif |
| 137 | |
| 138 | " If previous line ended in a comma, indent again |
| 139 | if prevline =~? ',\s*$' |
| 140 | let chg = chg + &sw |
| 141 | endif |
| 142 | |
| 143 | " If previous line ended in a =>, indent again |
| 144 | if prevline =~? '=>\s*$' |
| 145 | let chg = chg + &sw |
| 146 | endif |
| 147 | |
| 148 | " Deindent on proper keywords |
| 149 | if cline =~? s:falcon_deindent_keywords |
| 150 | let chg = chg - &sw |
| 151 | endif |
| 152 | |
| 153 | return ind + chg |
| 154 | endfunction |
| 155 | |
| 156 | " vim: set sw=4 sts=4 et tw=80 : |