aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormattn <mattn.jp@gmail.com>2012-11-28 21:53:22 +0900
committermattn <mattn.jp@gmail.com>2012-11-28 21:53:22 +0900
commite0fcf5a243a8ca47e2276e02a05254b4aa003b2b (patch)
treef96709ca62da39c825613596a7dae8a255cbdef8
parente97be0835bc225d673fe2a71d186943b09f7a29c (diff)
downloadvim-sonictemplate-e0fcf5a243a8ca47e2276e02a05254b4aa003b2b.tar.gz
vim-sonictemplate-e0fcf5a243a8ca47e2276e02a05254b4aa003b2b.tar.bz2
vim-sonictemplate-e0fcf5a243a8ca47e2276e02a05254b4aa003b2b.zip
add template filter.
-rw-r--r--autoload/sonictemplate.vim37
-rw-r--r--template/go/base-http-server.go2
-rw-r--r--template/go/base-webgo.go13
3 files changed, 49 insertions, 3 deletions
diff --git a/autoload/sonictemplate.vim b/autoload/sonictemplate.vim
index 13c1651..7326186 100644
--- a/autoload/sonictemplate.vim
+++ b/autoload/sonictemplate.vim
@@ -1,7 +1,7 @@
"=============================================================================
" sonictemplate.vim
" Author: Yasuhiro Matsumoto <mattn.jp@gmail.com>
-" Last Change: 27-Jun-2012.
+" Last Change: 28-Nov-2012.
let s:save_cpo = &cpo
set cpo&vim
@@ -72,6 +72,18 @@ function! s:get_candidate(fts, lead)
call add(candidate, c)
endif
endfor
+ let filter = s:getopt('filter')
+ if filter != ''
+ let [lhs, rhs] = [[], []]
+ for c in candidate
+ if stridx(c, filter) == 0
+ call add(lhs, c)
+ else
+ call add(rhs, c)
+ endif
+ endfor
+ let candidate = lhs + rhs
+ endif
return candidate
endfunction
@@ -83,6 +95,20 @@ function! sonictemplate#complete_intelligent(lead, cmdline, curpos) abort
return s:get_candidate([sonictemplate#get_filetype(), &ft], a:lead)
endfunction
+function! s:setopt(k, v)
+ if !exists('b:sonictemplate')
+ let b:sonictemplate = {}
+ endif
+ let b:sonictemplate[a:k] = a:v
+endfunction
+
+function! s:getopt(k)
+ if !exists('b:sonictemplate') || !has_key(b:sonictemplate, a:k)
+ return ''
+ endif
+ return b:sonictemplate[a:k]
+endfunction
+
function! sonictemplate#apply(name, mode, ...) abort
let name = matchstr(a:name, '\S\+')
let buffer_is_not_empty = search('[^ \t]', 'wn')
@@ -134,8 +160,15 @@ function! sonictemplate#apply(name, mode, ...) abort
if len(c) == 0
return
endif
+ let mx = '{{_filter_:\(\w\+\)}}\s*'
+ let bf = matchstr(c, mx)
+ if len(bf) > 0
+ call s:setopt('filter', substitute(bf, mx, '\1', ''))
+ let c = substitute(c, mx, '', 'g')
+ endif
+
if !buffer_is_not_empty
- let c = substitute(c, '{{_inline_}}', '', 'g')
+ let c = substitute(c, '{{_inline_}}\s*', '', 'g')
if &expandtab || &tabstop != &shiftwidth
let c = substitute(c, "\t", repeat(' ', &shiftwidth), 'g')
endif
diff --git a/template/go/base-http-server.go b/template/go/base-http-server.go
index abe84d4..b760d93 100644
--- a/template/go/base-http-server.go
+++ b/template/go/base-http-server.go
@@ -7,7 +7,7 @@ import (
func main() {
http.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) {
- fmt.Fprintf(w, "hello world")
+ fmt.Fprintf(w, "{{_cursor_}}")
})
http.ListenAndServe(":8080", nil)
}
diff --git a/template/go/base-webgo.go b/template/go/base-webgo.go
new file mode 100644
index 0000000..c67b6a7
--- /dev/null
+++ b/template/go/base-webgo.go
@@ -0,0 +1,13 @@
+package main
+
+import (
+ "github.com/hoisie/web"
+)
+
+func main() {
+ web.Get("/", func() string {
+ return "{{_cursor_}}"
+ })
+ web.Run(":8080")
+}
+{{_filter_:webgo}}