mirror of
https://github.com/gryf/python.vim.git
synced 2025-12-17 19:40:28 +01:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0877af4dff | - | |
|
|
6411a284c5 | - | |
|
|
bf1056d729 | - | |
|
|
ef20af29a9 | - | |
|
|
6dbc984278 | - | |
|
|
97ed3fccdb | - | |
|
|
7a5bb2412f | - |
@@ -1,13 +1,17 @@
|
|||||||
" -*- vim -*-
|
" -*- vim -*-
|
||||||
" FILE: python.vim
|
" FILE: python_fn.vim
|
||||||
" LAST MODIFICATION: 2003/06/12 08:19
|
" LAST MODIFICATION: 2008-08-28 8:19pm
|
||||||
" (C) Copyright 2001-2003 Mikael Berthe <mikael.b@netcourrier.com>
|
" (C) Copyright 2001-2005 Mikael Berthe <bmikael@lists.lilotux.net>
|
||||||
" Version: 1.6
|
" Maintained by Jon Franklin <jvfranklin@gmail.com>
|
||||||
|
" Version: 1.13
|
||||||
|
|
||||||
" USAGE:
|
" USAGE:
|
||||||
"
|
"
|
||||||
" Just source this script when editing Python files.
|
" Save this file to $VIMFILES/ftplugin/python.vim. You can have multiple
|
||||||
" Example: au FileType python source ~me/.vim/scripts/python.vim
|
" python ftplugins by creating $VIMFILES/ftplugin/python and saving your
|
||||||
|
" ftplugins in that directory. If saving this to the global ftplugin
|
||||||
|
" directory, this is the recommended method, since vim ships with an
|
||||||
|
" ftplugin/python.vim file already.
|
||||||
" You can set the global variable "g:py_select_leading_comments" to 0
|
" You can set the global variable "g:py_select_leading_comments" to 0
|
||||||
" if you don't want to select comments preceding a declaration (these
|
" if you don't want to select comments preceding a declaration (these
|
||||||
" are usually the description of the function/class).
|
" are usually the description of the function/class).
|
||||||
@@ -20,48 +24,76 @@
|
|||||||
" shift commands...
|
" shift commands...
|
||||||
"
|
"
|
||||||
" REQUIREMENTS:
|
" REQUIREMENTS:
|
||||||
" vim (>= 6)
|
" vim (>= 7)
|
||||||
"
|
"
|
||||||
" Shortcuts:
|
" Shortcuts:
|
||||||
" [[ -- Jump to beginning of block
|
" ]t -- Jump to beginning of block
|
||||||
" ]] -- Jump to end of block
|
" ]e -- Jump to end of block
|
||||||
" ]v -- Select (Visual Line Mode) block
|
" ]v -- Select (Visual Line Mode) block
|
||||||
" ]< -- Shift block to left
|
" ]< -- Shift block to left
|
||||||
" ]> -- Shift block to right
|
" ]> -- Shift block to right
|
||||||
|
" ]# -- Comment selection
|
||||||
|
" ]u -- Uncomment selection
|
||||||
" ]c -- Select current/previous class
|
" ]c -- Select current/previous class
|
||||||
" ]f -- Select current/previous function
|
" ]d -- Select current/previous function
|
||||||
" ]<up> -- Jump to previous line with the same/lower indentation
|
" ]<up> -- Jump to previous line with the same/lower indentation
|
||||||
" ]<down> -- Jump to next line with the same/lower indentation
|
" ]<down> -- Jump to next line with the same/lower indentation
|
||||||
|
|
||||||
|
" Only do this when not done yet for this buffer
|
||||||
|
if exists("b:loaded_py_ftplugin")
|
||||||
|
finish
|
||||||
|
endif
|
||||||
|
let b:loaded_py_ftplugin = 1
|
||||||
|
|
||||||
map [[ :PBoB<CR>
|
map ]t :PBoB<CR>
|
||||||
vmap [[ :<C-U>PBoB<CR>m'gv``
|
vmap ]t :<C-U>PBOB<CR>m'gv``
|
||||||
map ]] :PEoB<CR>
|
map ]e :PEoB<CR>
|
||||||
vmap ]] :<C-U>PEoB<CR>m'gv``
|
vmap ]e :<C-U>PEoB<CR>m'gv``
|
||||||
|
|
||||||
map ]v [[V]]
|
map ]v ]tV]e
|
||||||
map ]< [[V]]<
|
map ]< ]tV]e<
|
||||||
vmap ]< <
|
vmap ]< <
|
||||||
map ]> [[V]]>
|
map ]> ]tV]e>
|
||||||
vmap ]> >
|
vmap ]> >
|
||||||
|
|
||||||
|
map ]# :call PythonCommentSelection()<CR>
|
||||||
|
vmap ]# :call PythonCommentSelection()<CR>
|
||||||
|
map ]u :call PythonUncommentSelection()<CR>
|
||||||
|
vmap ]u :call PythonUncommentSelection()<CR>
|
||||||
|
|
||||||
map ]c :call PythonSelectObject("class")<CR>
|
map ]c :call PythonSelectObject("class")<CR>
|
||||||
map ]f :call PythonSelectObject("function")<CR>
|
map ]d :call PythonSelectObject("function")<CR>
|
||||||
|
|
||||||
map ]<up> :call PythonNextLine(-1)<CR>
|
map ]<up> :call PythonNextLine(-1)<CR>
|
||||||
map ]<down> :call PythonNextLine(1)<CR>
|
map ]<down> :call PythonNextLine(1)<CR>
|
||||||
" You may prefer use <s-up> and <s-down>... :-)
|
" You may prefer use <s-up> and <s-down>... :-)
|
||||||
|
|
||||||
|
" jump to previous class
|
||||||
|
map ]J :call PythonDec("class", -1)<CR>
|
||||||
|
vmap ]J :call PythonDec("class", -1)<CR>
|
||||||
|
|
||||||
|
" jump to next class
|
||||||
|
map ]j :call PythonDec("class", 1)<CR>
|
||||||
|
vmap ]j :call PythonDec("class", 1)<CR>
|
||||||
|
|
||||||
|
" jump to previous function
|
||||||
|
map ]F :call PythonDec("function", -1)<CR>
|
||||||
|
vmap ]F :call PythonDec("function", -1)<CR>
|
||||||
|
|
||||||
|
" jump to next function
|
||||||
|
map ]f :call PythonDec("function", 1)<CR>
|
||||||
|
vmap ]f :call PythonDec("function", 1)<CR>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
" Menu entries
|
" Menu entries
|
||||||
nmenu <silent> &Python.Update\ IM-Python\ Menu
|
nmenu <silent> &Python.Update\ IM-Python\ Menu
|
||||||
\:call UpdateMenu()<CR>
|
\:call UpdateMenu()<CR>
|
||||||
nmenu &Python.-Sep1- :
|
nmenu &Python.-Sep1- :
|
||||||
nmenu <silent> &Python.Beginning\ of\ Block<Tab>[[
|
nmenu <silent> &Python.Beginning\ of\ Block<Tab>[t
|
||||||
\[[
|
\]t
|
||||||
nmenu <silent> &Python.End\ of\ Block<Tab>]]
|
nmenu <silent> &Python.End\ of\ Block<Tab>]e
|
||||||
\]]
|
\]e
|
||||||
nmenu &Python.-Sep2- :
|
nmenu &Python.-Sep2- :
|
||||||
nmenu <silent> &Python.Shift\ Block\ Left<Tab>]<
|
nmenu <silent> &Python.Shift\ Block\ Left<Tab>]<
|
||||||
\]<
|
\]<
|
||||||
@@ -72,28 +104,28 @@ nmenu <silent> &Python.Shift\ Block\ Right<Tab>]>
|
|||||||
vmenu <silent> &Python.Shift\ Block\ Right<Tab>]>
|
vmenu <silent> &Python.Shift\ Block\ Right<Tab>]>
|
||||||
\]>
|
\]>
|
||||||
nmenu &Python.-Sep3- :
|
nmenu &Python.-Sep3- :
|
||||||
vmenu <silent> &Python.Comment\ Selection
|
vmenu <silent> &Python.Comment\ Selection<Tab>]#
|
||||||
\:call PythonCommentSelection()<CR>
|
\]#
|
||||||
nmenu <silent> &Python.Comment\ Selection
|
nmenu <silent> &Python.Comment\ Selection<Tab>]#
|
||||||
\:call PythonCommentSelection()<CR>
|
\]#
|
||||||
vmenu <silent> &Python.Uncomment\ Selection
|
vmenu <silent> &Python.Uncomment\ Selection<Tab>]u
|
||||||
\:call PythonUncommentSelection()<CR>
|
\]u
|
||||||
nmenu <silent> &Python.Uncomment\ Selection
|
nmenu <silent> &Python.Uncomment\ Selection<Tab>]u
|
||||||
\:call PythonUncommentSelection()<CR>
|
\]u
|
||||||
nmenu &Python.-Sep4- :
|
nmenu &Python.-Sep4- :
|
||||||
nmenu <silent> &Python.Previous\ Class
|
nmenu <silent> &Python.Previous\ Class<Tab>]J
|
||||||
\:call PythonDec("class", -1)<CR>
|
\]J
|
||||||
nmenu <silent> &Python.Next\ Class
|
nmenu <silent> &Python.Next\ Class<Tab>]j
|
||||||
\:call PythonDec("class", 1)<CR>
|
\]j
|
||||||
nmenu <silent> &Python.Previous\ Function
|
nmenu <silent> &Python.Previous\ Function<Tab>]F
|
||||||
\:call PythonDec("function", -1)<CR>
|
\]F
|
||||||
nmenu <silent> &Python.Next\ Function
|
nmenu <silent> &Python.Next\ Function<Tab>]f
|
||||||
\:call PythonDec("function", 1)<CR>
|
\]f
|
||||||
nmenu &Python.-Sep5- :
|
nmenu &Python.-Sep5- :
|
||||||
nmenu <silent> &Python.Select\ Block<Tab>]v
|
nmenu <silent> &Python.Select\ Block<Tab>]v
|
||||||
\]v
|
\]v
|
||||||
nmenu <silent> &Python.Select\ Function<Tab>]f
|
nmenu <silent> &Python.Select\ Function<Tab>]d
|
||||||
\]f
|
\]d
|
||||||
nmenu <silent> &Python.Select\ Class<Tab>]c
|
nmenu <silent> &Python.Select\ Class<Tab>]c
|
||||||
\]c
|
\]c
|
||||||
nmenu &Python.-Sep6- :
|
nmenu &Python.-Sep6- :
|
||||||
@@ -102,7 +134,6 @@ nmenu <silent> &Python.Previous\ Line\ wrt\ indent<Tab>]<up>
|
|||||||
nmenu <silent> &Python.Next\ Line\ wrt\ indent<Tab>]<down>
|
nmenu <silent> &Python.Next\ Line\ wrt\ indent<Tab>]<down>
|
||||||
\]<down>
|
\]<down>
|
||||||
|
|
||||||
|
|
||||||
:com! PBoB execute "normal ".PythonBoB(line('.'), -1, 1)."G"
|
:com! PBoB execute "normal ".PythonBoB(line('.'), -1, 1)."G"
|
||||||
:com! PEoB execute "normal ".PythonBoB(line('.'), 1, 1)."G"
|
:com! PEoB execute "normal ".PythonBoB(line('.'), 1, 1)."G"
|
||||||
:com! UpdateMenu call UpdateMenu()
|
:com! UpdateMenu call UpdateMenu()
|
||||||
@@ -191,7 +222,7 @@ function! PythonCommentSelection() range
|
|||||||
if strlen(getline(cl))
|
if strlen(getline(cl))
|
||||||
execute "normal ".ind."|i".commentString
|
execute "normal ".ind."|i".commentString
|
||||||
endif
|
endif
|
||||||
execute "normal j"
|
execute "normal \<Down>"
|
||||||
let cl = cl + 1
|
let cl = cl + 1
|
||||||
endwhile
|
endwhile
|
||||||
endfunction
|
endfunction
|
||||||
@@ -247,7 +278,7 @@ function! PythonSelectObject(obj)
|
|||||||
execute "normal V".cl."G"
|
execute "normal V".cl."G"
|
||||||
else
|
else
|
||||||
" Select the whole block
|
" Select the whole block
|
||||||
normal j
|
execute "normal \<Down>"
|
||||||
let cl = line('.')
|
let cl = line('.')
|
||||||
execute ":".beg
|
execute ":".beg
|
||||||
execute "normal V".PythonBoB(cl, 1, 0)."G"
|
execute "normal V".PythonBoB(cl, 1, 0)."G"
|
||||||
@@ -279,54 +310,117 @@ function! PythonNextLine(direction)
|
|||||||
execute "normal ".ln."G"
|
execute "normal ".ln."G"
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
" Update the IM-Python menu, that holds Classes and Functions
|
|
||||||
function! UpdateMenu()
|
function! UpdateMenu()
|
||||||
|
" delete menu if it already exists, then rebuild it.
|
||||||
|
" this is necessary in case you've got multiple buffers open
|
||||||
|
" a future enhancement to this would be to make the menu aware of
|
||||||
|
" all buffers currently open, and group classes and functions by buffer
|
||||||
|
if exists("g:menuran")
|
||||||
|
aunmenu IM-Python
|
||||||
|
endif
|
||||||
let restore_fe = &foldenable
|
let restore_fe = &foldenable
|
||||||
set nofoldenable
|
set nofoldenable
|
||||||
|
" preserve disposition of window and cursor
|
||||||
let cline=line('.')
|
let cline=line('.')
|
||||||
call MakeClassStructure ()
|
let ccol=col('.') - 1
|
||||||
call MakeFuncStructure ()
|
norm H
|
||||||
execute "normal ".cline."Gzz"
|
let hline=line('.')
|
||||||
|
" create the menu
|
||||||
|
call MenuBuilder()
|
||||||
|
" restore disposition of window and cursor
|
||||||
|
exe "norm ".hline."Gzt"
|
||||||
|
let dnscroll=cline-hline
|
||||||
|
exe "norm ".dnscroll."j".ccol."l"
|
||||||
let &foldenable = restore_fe
|
let &foldenable = restore_fe
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
" Make a menu that holds all of the classes
|
function! MenuBuilder()
|
||||||
function! MakeClassStructure ()
|
norm gg0
|
||||||
norm mpgg0
|
let currentclass = -1
|
||||||
while line(".") <= line("$")
|
let classlist = []
|
||||||
if match ( getline("."), '^\s*class\s\+' ) != -1
|
let parentclass = ""
|
||||||
norm ^w"nyw
|
while line(".") < line("$")
|
||||||
let name=@n
|
" search for a class or function
|
||||||
"exe 'menu IM-Python.classes.'.name.' '.line(".").'gg15zo'
|
if match ( getline("."), '^\s*class\s\+[_a-zA-Z].*\|^\s*def\s\+[_a-zA-Z].*' ) != -1
|
||||||
exe 'menu IM-Python.classes.'.name.' :call <SID>JumpToAndUnfold('.line(".").')<CR>'
|
norm ^
|
||||||
endif
|
let linenum = line('.')
|
||||||
if line(".") == line("$")
|
let indentcol = col('.')
|
||||||
return
|
norm "nye
|
||||||
endif
|
let classordef=@n
|
||||||
|
norm w"nywge
|
||||||
|
let objname=@n
|
||||||
|
let parentclass = FindParentClass(classlist, indentcol)
|
||||||
|
if classordef == "class"
|
||||||
|
call AddClass(objname, linenum, parentclass)
|
||||||
|
else " this is a function
|
||||||
|
call AddFunction(objname, linenum, parentclass)
|
||||||
|
endif
|
||||||
|
" We actually created a menu, so lets set the global variable
|
||||||
|
let g:menuran=1
|
||||||
|
call RebuildClassList(classlist, [objname, indentcol], classordef)
|
||||||
|
endif " line matched
|
||||||
norm j
|
norm j
|
||||||
endwhile
|
endwhile
|
||||||
norm 'p
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
" Make a menu that holds all of the function definitions
|
" classlist contains the list of nested classes we are in.
|
||||||
function! MakeFuncStructure ()
|
" in most cases it will be empty or contain a single class
|
||||||
norm mpgg0
|
" but where a class is nested within another, it will contain 2 or more
|
||||||
while line(".") <= line("$")
|
" this function adds or removes classes from the list based on indentation
|
||||||
if match ( getline("."), '^\s*def\s\+' ) != -1
|
function! RebuildClassList(classlist, newclass, classordef)
|
||||||
norm ^w"nyw
|
let i = len(a:classlist) - 1
|
||||||
let name=@n
|
while i > -1
|
||||||
"exe 'menu IM-Python.functions.'.name.' '.line(".").'gg15zo'
|
if a:newclass[1] <= a:classlist[i][1]
|
||||||
exe 'menu IM-Python.functions.'.name.' :call <SID>JumpToAndUnfold('.line(".").')<CR>'
|
call remove(a:classlist, i)
|
||||||
endif
|
endif
|
||||||
if line(".") == line("$")
|
let i = i - 1
|
||||||
return
|
|
||||||
endif
|
|
||||||
norm j
|
|
||||||
endwhile
|
endwhile
|
||||||
norm 'p
|
if a:classordef == "class"
|
||||||
|
call add(a:classlist, a:newclass)
|
||||||
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
" we found a class or function, determine its parent class based on
|
||||||
|
" indentation and what's contained in classlist
|
||||||
|
function! FindParentClass(classlist, indentcol)
|
||||||
|
let i = 0
|
||||||
|
let parentclass = ""
|
||||||
|
while i < len(a:classlist)
|
||||||
|
if a:indentcol <= a:classlist[i][1]
|
||||||
|
break
|
||||||
|
else
|
||||||
|
if len(parentclass) == 0
|
||||||
|
let parentclass = a:classlist[i][0]
|
||||||
|
else
|
||||||
|
let parentclass = parentclass.'\.'.a:classlist[i][0]
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
let i = i + 1
|
||||||
|
endwhile
|
||||||
|
return parentclass
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
" add a class to the menu
|
||||||
|
function! AddClass(classname, lineno, parentclass)
|
||||||
|
if len(a:parentclass) > 0
|
||||||
|
let classstring = a:parentclass.'\.'.a:classname
|
||||||
|
else
|
||||||
|
let classstring = a:classname
|
||||||
|
endif
|
||||||
|
exe 'menu IM-Python.classes.'.classstring.' :call <SID>JumpToAndUnfold('.a:lineno.')<CR>'
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
" add a function to the menu, grouped by member class
|
||||||
|
function! AddFunction(functionname, lineno, parentclass)
|
||||||
|
if len(a:parentclass) > 0
|
||||||
|
let funcstring = a:parentclass.'.'.a:functionname
|
||||||
|
else
|
||||||
|
let funcstring = a:functionname
|
||||||
|
endif
|
||||||
|
exe 'menu IM-Python.functions.'.funcstring.' :call <SID>JumpToAndUnfold('.a:lineno.')<CR>'
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
function! s:JumpToAndUnfold(line)
|
function! s:JumpToAndUnfold(line)
|
||||||
" Go to the right line
|
" Go to the right line
|
||||||
execute 'normal '.a:line.'gg'
|
execute 'normal '.a:line.'gg'
|
||||||
Reference in New Issue
Block a user