Mercurial > vim-piecrust
comparison plugin/piecrust.vim @ 0:d09875b9f100
Initial commit.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sat, 31 Mar 2012 11:29:12 -0700 |
parents | |
children | 6185e60279c0 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d09875b9f100 |
---|---|
1 " piecrust.vim - PieCrust plugin for Vim | |
2 " Maintainer: Ludovic Chabant <http://ludovic.chabant.com> | |
3 " Version: 0.1 | |
4 | |
5 " Globals {{{ | |
6 | |
7 if !exists('g:piecrust_debug') | |
8 let g:piecrust_debug = 0 | |
9 endif | |
10 | |
11 if (exists('g:loaded_piecrust') || &cp) && !g:piecrust_debug | |
12 finish | |
13 endif | |
14 if (exists('g:loaded_piecrust') && g:piecrust_debug) | |
15 echom "Reloaded PieCrust." | |
16 endif | |
17 let g:loaded_piecrust = 1 | |
18 | |
19 if !exists('g:piecrust_chef_executable') | |
20 let g:piecrust_chef_executable = 'chef' | |
21 endif | |
22 | |
23 if !exists('g:piecrust_trace') | |
24 let g:piecrust_trace = 0 | |
25 endif | |
26 | |
27 " }}} | |
28 | |
29 " Utility {{{ | |
30 | |
31 " Strips the ending slash in a path. | |
32 function! s:stripslash(path) | |
33 return fnamemodify(a:path, ':s?[/\\]$??') | |
34 endfunction | |
35 | |
36 " Normalizes the slashes in a path. | |
37 function! s:normalizepath(path) | |
38 if exists('+shellslash') && &shellslash | |
39 return substitute(a:path, '\\', '/', '') | |
40 elseif has('win32') | |
41 return substitute(a:path, '/', '\\', '') | |
42 else | |
43 return a:path | |
44 endif | |
45 endfunction | |
46 | |
47 " Prints a message if debug tracing is enabled. | |
48 function! s:trace(message, ...) | |
49 if g:piecrust_trace || (a:0 && a:1) | |
50 let l:message = "piecrust: " . a:message | |
51 echom l:message | |
52 endif | |
53 endfunction | |
54 | |
55 " Prints an error message with 'piecrust error' prefixed to it. | |
56 function! s:error(message) | |
57 echom "piecrust error: " . a:message | |
58 endfunction | |
59 | |
60 " Throw a PieCrust exception message. | |
61 function! s:throw(message) | |
62 let v:errmsg = "piecrust: " . a:message | |
63 throw v:errmsg | |
64 endfunction | |
65 | |
66 " Finds the website root given a path inside that website. | |
67 " Throw an error if not repository is found. | |
68 function! s:find_website_root(path) | |
69 let l:path = s:stripslash(a:path) | |
70 let l:previous_path = "" | |
71 while l:path != l:previous_path | |
72 if filereadable(l:path . '/_content/config.yml') | |
73 return simplify(fnamemodify(l:path, ':p')) | |
74 endif | |
75 let l:previous_path = l:path | |
76 let l:path = fnamemodify(l:path, ':h') | |
77 endwhile | |
78 call s:throw("No PieCrust website found above: " . a:path) | |
79 endfunction | |
80 | |
81 " }}} | |
82 | |
83 " PieCrust website {{{ | |
84 | |
85 " Let's define a PieCrust website 'class' using prototype-based object-oriented | |
86 " programming. | |
87 " | |
88 " The prototype dictionary. | |
89 let s:PieCrust = {} | |
90 | |
91 " Constructor | |
92 function! s:PieCrust.New(path) abort | |
93 let l:newSite = copy(self) | |
94 let l:newSite.root_dir = s:find_website_root(a:path) | |
95 call s:trace("Built new PieCrust website object at : " . l:newSite.root_dir) | |
96 return l:newSite | |
97 endfunction | |
98 | |
99 " Gets a full path given a repo-relative path | |
100 function! s:PieCrust.GetFullPath(path) abort | |
101 let l:root_dir = self.root_dir | |
102 if a:path =~# '\v^[/\\]' | |
103 let l:root_dir = s:stripslash(l:root_dir) | |
104 endif | |
105 return l:root_dir . a:path | |
106 endfunction | |
107 | |
108 " Runs a Chef command in the website | |
109 function! s:PieCrust.RunCommand(command, ...) abort | |
110 " If there's only one argument, and it's a list, then use that as the | |
111 " argument list. | |
112 let l:arg_list = a:000 | |
113 if a:0 == 1 && type(a:1) == type([]) | |
114 let l:arg_list = a:1 | |
115 endif | |
116 let l:chef_command = g:piecrust_chef_executable . ' ' . a:command | |
117 let l:chef_command = l:chef_command . ' --root=' . shellescape(s:stripslash(self.root_dir)) | |
118 let l:chef_command = l:chef_command . ' ' . join(l:arg_list, ' ') | |
119 call s:trace("Running Chef command: " . l:chef_command) | |
120 return system(l:chef_command) | |
121 endfunction | |
122 | |
123 " Website cache map | |
124 let s:buffer_websites = {} | |
125 | |
126 " Get a cached website | |
127 function! s:piecrust_website(...) abort | |
128 " Use the given path, or the website directory of the current buffer. | |
129 if a:0 == 0 | |
130 if exists('b:piecrust_dir') | |
131 let l:path = b:piecrust_dir | |
132 else | |
133 let l:path = s:find_website_root(expand('%:p')) | |
134 endif | |
135 else | |
136 let l:path = a:1 | |
137 endif | |
138 " Find a cache website instance, or make a new one. | |
139 if has_key(s:buffer_websites, l:path) | |
140 return get(s:buffer_websites, l:path) | |
141 else | |
142 let l:website = s:PieCrust.New(l:path) | |
143 let s:buffer_websites[l:path] = l:website | |
144 return l:website | |
145 endif | |
146 endfunction | |
147 | |
148 " Sets up the current buffer with PieCrust commands if it contains a file from a PieCrust website. | |
149 " If the file is not in a PieCrust website, just exit silently. | |
150 function! s:setup_buffer_commands() abort | |
151 call s:trace("Scanning buffer '" . bufname('%') . "' for PieCrust setup...") | |
152 let l:do_setup = 1 | |
153 if exists('b:piecrust_dir') | |
154 if b:piecrust_dir =~# '\v^\s*$' | |
155 unlet b:piecrust_dir | |
156 else | |
157 let l:do_setup = 0 | |
158 endif | |
159 endif | |
160 try | |
161 let l:website = s:piecrust_website() | |
162 catch /^piecrust\:/ | |
163 return | |
164 endtry | |
165 let b:piecrust_dir = l:website.root_dir | |
166 if exists('b:piecrust_dir') && l:do_setup | |
167 call s:trace("Setting PieCrust commands for buffer '" . bufname('%')) | |
168 call s:trace(" with website : " . expand(b:piecrust_dir)) | |
169 silent doautocmd User PieCrust | |
170 endif | |
171 endfunction | |
172 | |
173 augroup piecrust_detect | |
174 autocmd! | |
175 autocmd BufNewFile,BufReadPost * call s:setup_buffer_commands() | |
176 autocmd VimEnter * if expand('<amatch>')==''|call s:setup_buffer_commands()|endif | |
177 augroup end | |
178 | |
179 " }}} | |
180 | |
181 " Buffer Commands Management {{{ | |
182 | |
183 " Store the commands for PieCrust-enabled buffers so that we can add them in | |
184 " batch when we need to. | |
185 let s:main_commands = [] | |
186 | |
187 function! s:AddMainCommand(command) abort | |
188 let s:main_commands += [a:command] | |
189 endfunction | |
190 | |
191 function! s:DefineMainCommands() | |
192 for l:command in s:main_commands | |
193 execute 'command! -buffer ' . l:command | |
194 endfor | |
195 endfunction | |
196 | |
197 augroup piecrust_main | |
198 autocmd! | |
199 autocmd User PieCrust call s:DefineMainCommands() | |
200 augroup end | |
201 | |
202 " }}} | |
203 | |
204 " Pcedit {{{ | |
205 | |
206 function! s:PcEdit(bang, filename) abort | |
207 let l:full_path = s:piecrust_website().GetFullPath(a:filename) | |
208 if a:bang | |
209 execute "edit! " . l:full_path | |
210 else | |
211 execute "edit " . l:full_path | |
212 endif | |
213 endfunction | |
214 | |
215 function! s:FindWebsiteFiles(ArgLead, CmdLine, CursorPos) abort | |
216 let l:website = s:piecrust_website() | |
217 let l:output = l:website.RunCommand('find', a:ArgLead) | |
218 let l:matches = split(l:output, '\n') | |
219 call map(l:matches, 's:normalizepath(v:val)') | |
220 return l:matches | |
221 endfunction | |
222 | |
223 call s:AddMainCommand("-bang -nargs=? -complete=customlist,s:FindWebsiteFiles Pcedit :call s:PcEdit(<bang>0, <f-args>)") | |
224 | |
225 " }}} | |
226 | |
227 " Autoload Functions {{{ | |
228 | |
229 " Rescans the current buffer for setting up PieCrust commands. | |
230 " Passing '1' as the parameter enables debug traces temporarily. | |
231 function! piecrust#rescan(...) | |
232 if exists('b:piecrust_dir') | |
233 unlet b:piecrust_dir | |
234 endif | |
235 if a:0 && a:1 | |
236 let l:trace_backup = g:piecrust_trace | |
237 let g:piecrust_trace = 1 | |
238 endif | |
239 call s:setup_buffer_commands() | |
240 if a:0 && a:1 | |
241 let g:piecrust_trace = l:trace_backup | |
242 endif | |
243 endfunction | |
244 | |
245 " Enables/disables the debug trace. | |
246 function! piecrust#debugtrace(...) | |
247 let g:piecrust_trace = (a:0 == 0 || (a:0 && a:1)) | |
248 echom "PieCrust debug trace is now " . (g:piecrust_trace ? "enabled." : "disabled.") | |
249 endfunction | |
250 | |
251 " }}} |