├── README.md └── plugin └── pony.vim /README.md: -------------------------------------------------------------------------------- 1 | # pony.vim 2 | 3 | Pony power for working with Django projects in Vim. 4 | 5 | ![Pony powered](http://media.djangopony.com/img/small/badge.png) 6 | 7 | ## Installation 8 | 9 | For those using [pathogen](https://github.com/tpope/vim-pathogen), 10 | simply copy/clone the entire repo in your ~/.vim/bundle. 11 | 12 | Otherwise, copy `plugin/pony.vim` to your ~/.vim/plugin folder. 13 | 14 | ## Jump commands 15 | 16 | Some basic commands are available to jump to commonly used files in Django: 17 | 18 | - Dadmin: admin.py 19 | - Dmodels: models.py 20 | - Dsettings: settings.py 21 | - Dtests: tests.py 22 | - Durls: urls.py 23 | - Dviews: views.py 24 | 25 | All these *jump commands* take an optional "app" argument, and will 26 | jump accordingly to the file in that app. Defaults to the current directory. 27 | 28 | Example : 29 | 30 | :Dviews app " opens app/views.py 31 | :Dmodels " opens app/models.py 32 | 33 | ## Managing commands 34 | 35 | The manage.py utility script is available via `:Dmanage`. 36 | Note that any command involving the manage.py utility must be run 37 | from that directory. 38 | 39 | Shortcuts are available for a few common manage.py commands: 40 | 41 | - Drunserver : manage.py runserver 42 | - Dsyncdb : manage.py syncdb 43 | - Dshell : manage.py shell 44 | - Ddbshell : manage.py dbshell 45 | 46 | Tip: I use `:Dr` for runserver, `:Dsy` for syncdb and `:Dsh` for shell. 47 | 48 | ## Configuration 49 | 50 | g:pony_prefix prefix to all Pony's commands (default: "D") 51 | g:pony_display_colors flag indicating if the manage.py should output colors (default: 1) 52 | g:pony_manage_filename filename of the manage.py script (default: manage.py) 53 | g:pony_python_cmd exact command to run on the manage.py script (default: python) 54 | 55 | ## Credits 56 | 57 | - [Rainer Borene](https://github.com/rainerborene), the initial contributor 58 | - [Jean-Marie Comets](https://github.com/jmcomets), current maintainer 59 | -------------------------------------------------------------------------------- /plugin/pony.vim: -------------------------------------------------------------------------------- 1 | " Vim plugin for working with Django projects. 2 | " Author: Rainer Borene 3 | " Licensed under the same terms as Vim. 4 | " 5 | " Maintainer: Jean-Marie Comets 6 | 7 | if exists("g:loaded_pony") 8 | finish 9 | endif 10 | let g:loaded_pony = 1 11 | 12 | " Configuration for "manage" script name 13 | if !exists('g:pony_manage_filename') 14 | let g:pony_manage_filename = findfile("manage.py") 15 | endif 16 | 17 | " function to wrap the check on this file 18 | function! s:ManageExists() 19 | return filereadable(g:pony_manage_filename) 20 | endfunction 21 | 22 | " Script error function, preferred to echoerr 23 | function! s:Error(msg) 24 | echohl ErrorMsg 25 | echo a:msg 26 | echohl None 27 | endfunction 28 | 29 | " Specify command to apply to the manage.py script (default: python) 30 | let g:pony_python_cmd = "python" 31 | 32 | " Dictionary containing mapping from command to possible files 33 | let s:goto_complete_dict = { 34 | \ "admin" : "admin.py", 35 | \ "models" : "models.py", 36 | \ "settings" : "settings.py", 37 | \ "tests" : "tests.py", 38 | \ "urls" : "urls.py", 39 | \ "views" : "views.py" 40 | \ } 41 | 42 | " Available commands for DjangoGoTo 43 | let s:goto_possible_keys = keys(s:goto_complete_dict) 44 | 45 | " Prefix for Pony commands 46 | let g:pony_prefix = "D" 47 | " helper for making command names, encapsulating the command format 48 | function! s:RealCommandName(CommandName) 49 | return g:pony_prefix . a:CommandName 50 | endfunction 51 | 52 | " Completion for DjangoGoto 53 | function! s:DjangoGoToComplete(ArgLead, CmdLine, CursorPos) 54 | " Check before continuing 55 | if !s:ManageExists() 56 | return [] 57 | endif 58 | 59 | " Check that there is a Goto defined for the given command 60 | let l:cmd_name = split(split(a:CmdLine, " ")[0], g:pony_prefix)[0] 61 | let l:goto_key_index = match(s:goto_possible_keys, l:cmd_name) 62 | if l:goto_key_index == -1 63 | return [] 64 | endif 65 | let l:goto_key = s:goto_possible_keys[l:goto_key_index] 66 | let l:filename = s:goto_complete_dict[l:goto_key] 67 | 68 | " Using find command, find folders holding python 69 | " files at "s:goto_complete_dict[s:filename]" 70 | let l:findcmd = "find */ -type f -name " 71 | \ . shellescape(l:filename) 72 | \ . " | sed 's/\\([^\\/]*\\)\\/" . escape(l:filename, './') . "/\\1/g'" 73 | \ . " | grep " . shellescape(a:ArgLead) 74 | let l:folders = system(l:findcmd) 75 | return split(l:folders, "\n") 76 | endfunction 77 | 78 | function! s:DjangoGoto(app_label, name) 79 | " Build app directory 80 | if len(a:app_label) > 0 81 | let l:real_app_label = a:app_label 82 | if !filereadable(l:real_app_label) 83 | let l:cmd = "ls -d " . l:real_app_label . "*" 84 | let l:app_label_candidates = split(system(l:cmd)) 85 | if len(l:app_label_candidates) == 0 86 | s:Error("File " . a:app_label . " doesn't have any candidates in CWD") 87 | return 88 | endif 89 | let l:real_app_label = l:app_label_candidates[0] 90 | endif 91 | let l:app_dir = getcwd() . "/" . l:real_app_label 92 | else 93 | let l:app_dir = expand("%:p:h") 94 | endif 95 | 96 | " Build filename 97 | let l:filename = l:app_dir . "/" . s:goto_complete_dict[a:name] 98 | 99 | " Edit file if it exists 100 | if filereadable(l:filename) 101 | execute "edit " . l:filename 102 | else 103 | call s:Error("File " . l:filename . " does not exists") 104 | endif 105 | endfunction 106 | 107 | " Return manage cmd via a function, because g:pony_manage_filename might 108 | " change. 109 | function! s:manage_cmd() 110 | return g:pony_python_cmd . " " . g:pony_manage_filename 111 | endfunction 112 | 113 | function! s:DjangoManageComplete(ArgLead, CmdLine, CursorPos) 114 | " Check before continuing 115 | if !s:ManageExists() 116 | return [] 117 | endif 118 | 119 | " Actually list commands 120 | let l:list_cmd = s:manage_cmd() 121 | \ . " help --commands" 122 | \ . " | grep " . shellescape(a:ArgLead) 123 | let l:commands = system(l:list_cmd) 124 | return split(l:commands, "\n") 125 | endfunction 126 | 127 | " Set this flag to allow Pony to display colors when 128 | " using the manage commmands 129 | let g:pony_display_colors = 1 130 | 131 | function! s:DjangoManage(arguments) 132 | " Check before continuing 133 | if !s:ManageExists() 134 | call s:Error("File '" . g:pony_manage_filename . "' does not exists in CWD") 135 | return 136 | endif 137 | 138 | " Build manage command from arguments 139 | let l:cmd = "!" 140 | if has("win32") 141 | let l:cmd .= " start /B " 142 | else 143 | if !g:pony_display_colors || has("gui_running") 144 | " Don't display colors 145 | let l:cmd .= "export DJANGO_COLORS=nocolor &&" 146 | endif 147 | endif 148 | execute l:cmd . " " . s:manage_cmd() . " " . a:arguments 149 | endfunction 150 | 151 | " Setup DjangoGoto commands 152 | for goto_key in s:goto_possible_keys 153 | execute "command! -nargs=? -complete=customlist,s:DjangoGoToComplete " 154 | \ . s:RealCommandName(goto_key) 155 | \ . " :call s:DjangoGoto('', '" . goto_key . "')" 156 | endfor 157 | 158 | " Manage and its shortcuts 159 | execute "command! -nargs=? -complete=customlist,s:DjangoManageComplete " 160 | \ . s:RealCommandName("manage") 161 | \ . " :call s:DjangoManage('')" 162 | " dictionary for configuration of the manage shortcuts 163 | let s:manage_shortcuts = { 164 | \ "dbshell" : "dbshell", 165 | \ "runserver" : "runserver", 166 | \ "syncdb" : "syncdb", 167 | \ "shell" : "shell" 168 | \ } 169 | 170 | " Make manage shortcut commands 171 | for pair in items(s:manage_shortcuts) 172 | let shortcut = pair[0] 173 | let shortcut_arg = pair[1] 174 | execute "command! -nargs=0 " 175 | \ . g:pony_prefix . shortcut 176 | \ . " :call s:DjangoManage('" . shortcut_arg . "')" 177 | unlet shortcut 178 | unlet shortcut_arg 179 | endfor 180 | 181 | " vim: ai et sw=2 sts=2 182 | --------------------------------------------------------------------------------