All pastes #81691 Raw Edit

Miscellany

public text v1 · immutable
·published 2006-07-07 19:37 UTC
rendered paste body
inoremap <CR> <ESC>:call SmartLineBreak()<CR>A<CR>

function! SmartLineBreak()
    if ( &syntax =~ '^\(c\|cpp\|php\|java\)$' )
        let current_line = getline( '.' )
        let space_at_end = '\s\+$'
        if match( current_line, space_at_end ) >= 0
            :execute ':s/\s*$//'
        endif
        let need_brace_on_same_line = '\<\(if\|while\|switch\|do\|foreach\|forever\|enum\|for\)\>'
        if match( current_line, need_brace_on_same_line ) >= 0
            let brace_at_end = '{$'
            if match( current_line, brace_at_end ) > 0
                if match( current_line, '[^ ]{$' ) > 0
                    :execute ':s/{$/ {/'
                endif
            else
                :execute ':s/$/ {/'
            endif
            if match( current_line, '\<enum\>' ) >= 0
                :execute "normal o};\<ESC>k"
            else
                :execute "normal o}\<ESC>k"
            endif
        else
            let need_brace_on_next_line = '\<\(class\|namespace\|struct\)\>'
            if match( current_line, need_brace_on_next_line ) >= 0
                let brace_at_end = '{$'
                if match( current_line, brace_at_end ) > 0
                    :execute ':s/\s*{$//'
                endif
                :execute "normal o{"
                if match( current_line, '\<namespace\>' ) >= 0
                    :execute "normal o}\<ESC>k"
                else
                    :execute "normal o};\<ESC>k"
                endif
            endif
        endif
    endif
endfunction