开发者

Configuring Django SnipMate snippets only for Django projects

I'm using SnipMate with Rob Hudson's snipmate_for_django snippets for Django development with MacVim.

To activate the snippets automatically based on filetype, Rob recommends adding the following to ~/.vimrc:

autocmd FileType python set ft=python.django " For SnipMate
autocmd FileType html set ft=htmldjango.html " For SnipM开发者_运维技巧ate

This enables the htmldjango snippets for all html files.

Is there a way to enable the htmldjango snippets only for html files located in a Django project?

For instance, I don't want to enable htmldjango snippets if I'm working on an html file related to a Rails project.


I also have that line, but I wonder if it's required. The default filetype.vim that comes with MacVim - find it at /Applications/MacVim.app/Contents/Resources/vim/runtime/filetype.vim - contains a bit of code that is supposed to automatically distinguish between plain HTML and Django, by testing to see if there's an extends or block tag in the first few lines:

au BufNewFile,BufRead *.html,*.htm,*.shtml,*.stm  call s:FThtml()

" Distinguish between HTML, XHTML and Django
func! s:FThtml()
  let n = 1
  while n < 10 && n < line("$")
    if getline(n) =~ '\<DTD\s\+XHTML\s'
      setf xhtml
      return
    endif
    if getline(n) =~ '{%\s*\(extends\|block\)\>'
      setf htmldjango
      return
    endif
    let n = n + 1
  endwhile
  setf html
endfunc

I've actually hacked that to add load to the determining tags, but that seems like it's enough in almost all cases.


You should check how it is done in rails.vim, the Rails plugin from Tim Pope, which works that way.

If you are editing a *.rb (Ruby file) from a Rails project, you enable some specific Rails configuration. But not if you are editing a non-Rails ruby file.

You should check the source code yourself but the entry point of the detection is below :

augroup railsPluginDetect
  autocmd!
  autocmd BufNewFile,BufRead * call s:Detect(expand("<afile>:p"))
  autocmd VimEnter * if expand("<amatch>") == "" && !exists("b:rails_root") | call s:Detect(getcwd()) | endif | if exists("b:rails_root") | silent doau User BufEnterRails | endif
  autocmd FileType netrw if !exists("b:rails_root") | call s:Detect(expand("<afile>:p")) | endif | if exists("b:rails_root") | silent doau User BufEnterRails | endif
  autocmd BufEnter * if exists("b:rails_root")|silent doau User BufEnterRails|endif
  autocmd BufLeave * if exists("b:rails_root")|silent doau User BufLeaveRails|endif
  autocmd Syntax railslog if s:autoload()|call rails#log_syntax()|endif
augroup END

Basically what s:Detect does is finding the root of the Rails project and check if there is an ./config/environment.rb and if this is the case, it create an event BufEnterRails with silent doau User BufEnterRails and there is another autocommand in case BufEnterRails happens.

You must follow the same path for your plugin. On opening a buffer, you should try to find a specific Django file or directory in the html file path you are editing, and then decide if you are in a Django project or not.

Since I don't know Django, I can't tell which file to look for, but there is likely a project config file common to every Django project.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜