summaryrefslogtreecommitdiffstats
path: root/indent/org.vim
blob: 9eacb3a2e4e303c9d85c126c2bb3908f70e525b2 (plain) (blame)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'org') == -1
  
" Delete the next line to avoid the special indention of items
if !exists("g:org_indent")
  let g:org_indent = 0
endif

setlocal foldtext=GetOrgFoldtext()
setlocal fillchars-=fold:-
setlocal fillchars+=fold:\ 
setlocal foldexpr=GetOrgFolding()
setlocal foldmethod=expr
setlocal indentexpr=GetOrgIndent()
setlocal nolisp
setlocal nosmartindent
setlocal autoindent

if has('python3')
	let s:py_env = 'python3 << EOF'
else
	let s:py_env = 'python << EOF'
endif

function! GetOrgIndent()
	if g:org_indent == 0
		return -1
	endif

exe s:py_env
from orgmode._vim import indent_orgmode
indent_orgmode()
EOF

	if exists('b:indent_level')
		let l:tmp = b:indent_level
		unlet b:indent_level
		return l:tmp
	else
		return -1
	endif
endfunction

function! GetOrgFolding()
	let l:mode = mode()
	if l:mode == 'i'
		" the cache size is limited to 3, because vim queries the current and
		" both surrounding lines when the user is typing in insert mode. The
		" cache is shared between GetOrgFolding and GetOrgFoldtext
		if ! exists('b:org_folding_cache')
			let b:org_folding_cache = {}
		endif

		if has_key(b:org_folding_cache, v:lnum)
			if match(b:org_folding_cache[v:lnum], '^>') == 0 &&
						\ match(getline(v:lnum), '^\*\+\s') != 0
				" when the user pastes text or presses enter, it happens that
				" the cache starts to confuse vim's folding abilities
				" these entries can safely be removed
				unlet b:org_folding_cache[v:lnum]

				" the fold text cache is probably also damaged, delete it as
				" well
				unlet! b:org_foldtext_cache
			else
				return b:org_folding_cache[v:lnum]
			endif
		endif

		exe s:py_env
from orgmode._vim import fold_orgmode
fold_orgmode(allow_dirty=True)
EOF
	else

		exe s:py_env
from orgmode._vim import fold_orgmode
fold_orgmode()
EOF
	endif

	if exists('b:fold_expr')
		let l:tmp = b:fold_expr
		unlet b:fold_expr
		if l:mode == 'i'
			if ! has_key(b:org_folding_cache, v:lnum)
				if len(b:org_folding_cache) > 3
					let b:org_folding_cache = {}
				endif
				let b:org_folding_cache[v:lnum] = l:tmp
			endif
		endif
		return l:tmp
	else
		return -1
	endif
endfunction

function! SetOrgFoldtext(text)
	let b:foldtext = a:text
endfunction

function! GetOrgFoldtext()
	let l:mode = mode()
	if l:mode == 'i'
		" add a separate cache for fold text
		if ! exists('b:org_foldtext_cache') ||
					\ ! has_key(b:org_foldtext_cache, 'timestamp') ||
					\ b:org_foldtext_cache['timestamp'] > (localtime() + 10)
			let b:org_foldtext_cache = {'timestamp': localtime()}
		endif

		if has_key(b:org_foldtext_cache, v:foldstart)
			return b:org_foldtext_cache[v:foldstart]
		endif
		exe s:py_env
from orgmode._vim import fold_text
fold_text(allow_dirty=True)
EOF
	else
		unlet! b:org_foldtext_cache
		exec s:py_env
from orgmode._vim import fold_text
fold_text()
EOF
	endif

	if exists('b:foldtext')
		let l:tmp = b:foldtext
		unlet b:foldtext
		if l:mode == 'i'
			let b:org_foldtext_cache[v:foldstart] = l:tmp
		endif
		return l:tmp
	endif
endfunction

endif