Skip to content

Commit 5fdd711

Browse files
committed
Refactor indent script for standalone use
Does not use VimClojure specific functions, or depend on VimClojure syntax group names.
1 parent a4a4486 commit 5fdd711

1 file changed

Lines changed: 76 additions & 26 deletions

File tree

indent/clojure.vim

Lines changed: 76 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,65 @@ setlocal indentkeys=!,o,O
2323

2424
if exists("*searchpairpos")
2525

26+
if !exists('g:clojure_maxlines')
27+
let g:clojure_maxlines = 100
28+
endif
29+
30+
if !exists('g:clojure_fuzzy_indent')
31+
let g:clojure_fuzzy_indent = 1
32+
endif
33+
34+
if !exists("g:clojure_fuzzy_indent_patterns")
35+
let g:clojure_fuzzy_indent_patterns = "with.*,def.*,let.*"
36+
endif
37+
38+
function! s:SynIdName()
39+
return synIDattr(synID(line("."), col("."), 0), "name")
40+
endfunction
41+
42+
function! s:CurrentChar()
43+
return getline('.')[col('.')-1]
44+
endfunction
45+
46+
function! s:CurrentWord()
47+
return getline('.')[col('.')-1 : searchpos('\v>', 'n', line('.'))[1]-2]
48+
endfunction
49+
50+
function! s:IsParen()
51+
return s:CurrentChar() =~ '\v[\(\)\[\]\{\}]' &&
52+
\ s:SynIdName() !~? '\vstring|comment'
53+
endfunction
54+
55+
function! s:SavePosition()
56+
let [ _b, l, c, _o ] = getpos(".")
57+
let b = bufnr("%")
58+
return [b, l, c]
59+
endfunction
60+
61+
function! s:RestorePosition(value)
62+
let [b, l, c] = a:value
63+
if bufnr("%") != b
64+
execute b "buffer!"
65+
endif
66+
call setpos(".", [0, l, c, 0])
67+
endfunction
68+
2669
function! s:MatchPairs(open, close, stopat)
2770
" Stop only on vector and map [ resp. {. Ignore the ones in strings and
2871
" comments.
2972
if a:stopat == 0
30-
let stopat = max([line(".") - g:vimclojure#SearchThreshold, 0])
73+
let stopat = max([line(".") - g:clojure_maxlines, 0])
3174
else
3275
let stopat = a:stopat
3376
endif
3477

3578
let pos = searchpairpos(a:open, '', a:close, 'bWn',
36-
\ 'vimclojure#util#SynIdName() !~ "clojureParen\\d"',
79+
\ "!s:IsParen()",
3780
\ stopat)
3881
return [ pos[0], virtcol(pos) ]
3982
endfunction
4083

41-
function! ClojureCheckForStringWorker() dict
84+
function! ClojureCheckForStringWorker()
4285
" Check whether there is the last character of the previous line is
4386
" highlighted as a string. If so, we check whether it's a ". In this
4487
" case we have to check also the previous character. The " might be the
@@ -52,17 +95,17 @@ function! ClojureCheckForStringWorker() dict
5295

5396
call cursor(nb, 0)
5497
call cursor(0, col("$") - 1)
55-
if vimclojure#util#SynIdName() != "clojureString"
98+
if s:SynIdName() !~? "string"
5699
return -1
57100
endif
58101

59102
" This will not work for a " in the first column...
60-
if vimclojure#util#Yank('l', 'normal! "lyl') == '"'
103+
if s:CurrentChar() == '"'
61104
call cursor(0, col("$") - 2)
62-
if vimclojure#util#SynIdName() != "clojureString"
105+
if s:SynIdName() !~? "string"
63106
return -1
64107
endif
65-
if vimclojure#util#Yank('l', 'normal! "lyl') != '\\'
108+
if s:CurrentChar() != '\\'
66109
return -1
67110
endif
68111
call cursor(0, col("$") - 1)
@@ -78,20 +121,24 @@ function! ClojureCheckForStringWorker() dict
78121
endfunction
79122

80123
function! s:CheckForString()
81-
return vimclojure#util#WithSavedPosition({
82-
\ 'f' : function("ClojureCheckForStringWorker")
83-
\ })
124+
let pos = s:SavePosition()
125+
try
126+
let val = ClojureCheckForStringWorker()
127+
finally
128+
call s:RestorePosition(pos)
129+
endtry
130+
return val
84131
endfunction
85132

86-
function! ClojureIsMethodSpecialCaseWorker() dict
133+
function! ClojureIsMethodSpecialCaseWorker(position)
87134
" Find the next enclosing form.
88-
call vimclojure#util#MoveBackward()
135+
call search('\S', 'Wb')
89136

90137
" Special case: we are at a '(('.
91-
if vimclojure#util#Yank('l', 'normal! "lyl') == '('
138+
if s:CurrentChar() == '('
92139
return 0
93140
endif
94-
call cursor(self.pos)
141+
call cursor(a:position)
95142

96143
let nextParen = s:MatchPairs('(', ')', 0)
97144

@@ -101,8 +148,8 @@ function! ClojureIsMethodSpecialCaseWorker() dict
101148
endif
102149
call cursor(nextParen)
103150

104-
call vimclojure#util#MoveForward()
105-
let keyword = vimclojure#util#Yank('l', 'normal! "lye')
151+
call search('\S', 'W')
152+
let keyword = s:CurrentWord()
106153
if index([ 'deftype', 'defrecord', 'reify', 'proxy',
107154
\ 'extend-type', 'extend-protocol',
108155
\ 'letfn' ], keyword) >= 0
@@ -113,12 +160,13 @@ function! ClojureIsMethodSpecialCaseWorker() dict
113160
endfunction
114161

115162
function! s:IsMethodSpecialCase(position)
116-
let closure = {
117-
\ 'pos': a:position,
118-
\ 'f' : function("ClojureIsMethodSpecialCaseWorker")
119-
\ }
120-
121-
return vimclojure#util#WithSavedPosition(closure)
163+
let pos = s:SavePosition()
164+
try
165+
let val = ClojureIsMethodSpecialCaseWorker(a:position)
166+
finally
167+
call s:RestorePosition(pos)
168+
endtry
169+
return val
122170
endfunction
123171

124172
function! GetClojureIndent()
@@ -189,7 +237,7 @@ function! GetClojureIndent()
189237

190238
" In case after the paren is a whitespace, we search for the next word.
191239
normal! l
192-
if vimclojure#util#Yank('l', 'normal! "lyl') == ' '
240+
if s:CurrentChar() == ' '
193241
normal! w
194242
endif
195243

@@ -201,7 +249,7 @@ function! GetClojureIndent()
201249

202250
" We still have to check, whether the keyword starts with a (, [ or {.
203251
" In that case we use the ( position for indent.
204-
let w = vimclojure#util#Yank('l', 'normal! "lye')
252+
let w = s:CurrentWord()
205253
if stridx('([{', w[0]) > 0
206254
return paren[1]
207255
endif
@@ -212,10 +260,10 @@ function! GetClojureIndent()
212260

213261
" XXX: Slight glitch here with special cases. However it's only
214262
" a heureustic. Offline we can't do more.
215-
if g:vimclojure#FuzzyIndent
263+
if g:clojure_fuzzy_indent
216264
\ && w != 'with-meta'
217265
\ && w != 'clojure.core/with-meta'
218-
for pat in split(g:vimclojure#FuzzyIndentPatterns, ",")
266+
for pat in split(g:clojure_fuzzy_indent_patterns, ",")
219267
if w =~ '\(^\|/\)' . pat . '$'
220268
\ && w !~ '\(^\|/\)' . pat . '\*$'
221269
\ && w !~ '\(^\|/\)' . pat . '-fn$'
@@ -267,3 +315,5 @@ setlocal lispwords+=ns,clojure.core/ns
267315
setlocal lispwords+=gen-class,gen-interface
268316

269317
let &cpo = s:save_cpo
318+
319+
" vim:ts=8 sts=8 sw=8 noet:

0 commit comments

Comments
 (0)