aboutsummaryrefslogtreecommitdiff
path: root/vim/bundle/tlib_vim/autoload/tlib/paragraph.vim
blob: dd0d11231214b7c448386abb0019e2731eba8cf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
" paragraph.vim
" @Author:      Tom Link (mailto:micathom AT gmail com?subject=[vim])
" @Website:     http://www.vim.org/account/profile.php?user_id=4037
" @License:     GPL (see http://www.gnu.org/licenses/gpl.txt)
" @Created:     2009-10-26.
" @Last Change: 2011-04-03.
" @Revision:    62

let s:save_cpo = &cpo
set cpo&vim


" Return an object describing a |paragraph|.
function! tlib#paragraph#GetMetric() "{{{3
    let sp = {'text_start': line("'{") + 1}
    if line("'}") == line("$")
        let sp.last = 1
        let sp.text_end = line("'}")
        if line("'{") == 1
            let sp.ws_start = 0
            let sp.ws_end = 0
            let sp.top = sp.text_start
            let sp.bottom = sp.text_end
        else
            let sp.ws_start = prevnonblank(line("'{")) + 1
            let sp.ws_end = line("'{")
            let sp.top = sp.ws_start
            let sp.bottom = sp.text_end
        endif
    else
        let sp.last = 0
        let sp.text_end = line("'}") - 1
        let sp.ws_start = line("'}")
        for i in range(line("'}"), line('$'))
            if getline(i) =~ '\w'
                let sp.ws_end = i - 1
                break
            elseif i == line("$")
                let sp.ws_end = i
            endif
        endfor
        let sp.top = sp.text_start
        let sp.bottom = sp.ws_end
    endif
    return sp
endf


" This function can be used with the tinymode plugin to move around 
" paragraphs.
"
" Example configuration: >
" 
"   call tinymode#EnterMap("para_move", "gp")
"   call tinymode#ModeMsg("para_move", "Move paragraph: j/k")
"   call tinymode#Map("para_move", "j", "silent call tlib#paragraph#Move('Down', '[N]')")
"   call tinymode#Map("para_move", "k", "silent call tlib#paragraph#Move('Up', '[N]')")
"   call tinymode#ModeArg("para_move", "owncount", 1)
function! tlib#paragraph#Move(direction, count)
    " TLogVAR a:direction, a:count
    let mycount = empty(a:count) ? 1 : a:count
    for i in range(1, mycount)
        let para = tlib#paragraph#GetMetric()
        " TLogVAR para
        let text = getline(para.text_start, para.text_end)
        let ws = getline(para.ws_start, para.ws_end)
        " TLogVAR text, ws
        exec para.top .','. para.bottom .'delete'
        if a:direction == "Down"
            let other = tlib#paragraph#GetMetric()
            let target = other.bottom + 1
            if other.last
                let lines = ws + text
                let pos = target + len(ws)
            else
                let lines = text + ws
                let pos = target
            endif
        elseif a:direction == "Up"
            if !para.last
                norm! {
            endif
            let other = tlib#paragraph#GetMetric()
            let target = other.text_start
            let lines = text + ws
            let pos = target
        endif
        " TLogVAR other, target
        " TLogVAR lines
        call append(target - 1, lines)
        exec pos
    endfor
endf


let &cpo = s:save_cpo
unlet s:save_cpo