mirror of
https://github.com/gryf/python.vim.git
synced 2025-12-17 11:30:22 +01:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ef20af29a9 | - | |
|
|
6dbc984278 | - | |
|
|
97ed3fccdb | - | |
|
|
7a5bb2412f | - | |
|
|
dc9722e464 | - | |
|
|
414dc1c5c0 | - |
@@ -1,12 +1,12 @@
|
||||
" -*- vim -*-
|
||||
" FILE: python.vim
|
||||
" LAST MODIFICATION: 2001/09/05
|
||||
" (C) Copyright 2001 Mikael Berthe <mikael.berthe@efrei.fr>
|
||||
" Version: 1.4
|
||||
" LAST MODIFICATION: 2006-11-07 4:29pm
|
||||
" (C) Copyright 2001-2005 Mikael Berthe <bmikael@lists.lilotux.net>
|
||||
" Version: 1.10
|
||||
|
||||
" USAGE:
|
||||
"
|
||||
" Juste source this script when editing Python files.
|
||||
" Just source this script when editing Python files.
|
||||
" Example: au FileType python source ~me/.vim/scripts/python.vim
|
||||
" 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
|
||||
@@ -20,7 +20,7 @@
|
||||
" shift commands...
|
||||
"
|
||||
" REQUIREMENTS:
|
||||
" vim (>= 600)
|
||||
" vim (>= 7)
|
||||
"
|
||||
" Shortcuts:
|
||||
" [[ -- Jump to beginning of block
|
||||
@@ -28,6 +28,8 @@
|
||||
" ]v -- Select (Visual Line Mode) block
|
||||
" ]< -- Shift block to left
|
||||
" ]> -- Shift block to right
|
||||
" ]# -- Comment selection
|
||||
" ]u -- Uncomment selection
|
||||
" ]c -- Select current/previous class
|
||||
" ]f -- Select current/previous function
|
||||
" ]<up> -- Jump to previous line with the same/lower indentation
|
||||
@@ -45,6 +47,11 @@ vmap ]< <
|
||||
map ]> [[V]]>
|
||||
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 ]f :call PythonSelectObject("function")<CR>
|
||||
|
||||
@@ -72,14 +79,14 @@ nmenu <silent> &Python.Shift\ Block\ Right<Tab>]>
|
||||
vmenu <silent> &Python.Shift\ Block\ Right<Tab>]>
|
||||
\]>
|
||||
nmenu &Python.-Sep3- :
|
||||
vmenu <silent> &Python.Comment\ Selection
|
||||
\:call PythonCommentSelection()<CR>
|
||||
nmenu <silent> &Python.Comment\ Selection
|
||||
\:call PythonCommentSelection()<CR>
|
||||
vmenu <silent> &Python.Uncomment\ Selection
|
||||
\:call PythonUncommentSelection()<CR>
|
||||
nmenu <silent> &Python.Uncomment\ Selection
|
||||
\:call PythonUncommentSelection()<CR>
|
||||
vmenu <silent> &Python.Comment\ Selection<Tab>]#
|
||||
\]#
|
||||
nmenu <silent> &Python.Comment\ Selection<Tab>]#
|
||||
\]#
|
||||
vmenu <silent> &Python.Uncomment\ Selection<Tab>]u
|
||||
\]u
|
||||
nmenu <silent> &Python.Uncomment\ Selection<Tab>]u
|
||||
\]u
|
||||
nmenu &Python.-Sep4- :
|
||||
nmenu <silent> &Python.Previous\ Class
|
||||
\:call PythonDec("class", -1)<CR>
|
||||
@@ -191,7 +198,7 @@ function! PythonCommentSelection() range
|
||||
if strlen(getline(cl))
|
||||
execute "normal ".ind."|i".commentString
|
||||
endif
|
||||
execute "normal j"
|
||||
execute "normal \<Down>"
|
||||
let cl = cl + 1
|
||||
endwhile
|
||||
endfunction
|
||||
@@ -247,7 +254,7 @@ function! PythonSelectObject(obj)
|
||||
execute "normal V".cl."G"
|
||||
else
|
||||
" Select the whole block
|
||||
normal j
|
||||
execute "normal \<Down>"
|
||||
let cl = line('.')
|
||||
execute ":".beg
|
||||
execute "normal V".PythonBoB(cl, 1, 0)."G"
|
||||
@@ -279,49 +286,137 @@ function! PythonNextLine(direction)
|
||||
execute "normal ".ln."G"
|
||||
endfunction
|
||||
|
||||
|
||||
" update the IM-Python menu, that holds Classes and Functions
|
||||
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
|
||||
else
|
||||
let g:menuran=1
|
||||
endif
|
||||
let restore_fe = &foldenable
|
||||
set nofoldenable
|
||||
" preserve disposition of window and cursor
|
||||
let cline=line('.')
|
||||
call MakeClassStructure ()
|
||||
call MakeFuncStructure ()
|
||||
execute "normal ".cline."Gzz"
|
||||
let ccol=col('.') - 1
|
||||
norm H
|
||||
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
|
||||
endfunction
|
||||
|
||||
" make a menu that holds all of the classes
|
||||
function! MakeClassStructure ()
|
||||
norm mpgg0
|
||||
while line(".") <= line("$")
|
||||
if match ( getline("."), '^\s*class\s\+' ) != -1
|
||||
norm ^w"nyw
|
||||
let name=@n
|
||||
exe 'menu IM-Python.classes.'.name.' '.line(".").'gg'
|
||||
endif
|
||||
if line(".") == line("$")
|
||||
return
|
||||
endif
|
||||
function! MenuBuilder()
|
||||
norm gg0
|
||||
let currentclass = -1
|
||||
let classlist = []
|
||||
let parentclass = ""
|
||||
while line(".") < line("$")
|
||||
" search for a class or function
|
||||
if match ( getline("."), '^\s*class\s\+[_a-zA-Z].*:\|^\s*def\s\+[_a-zA-Z].*:' ) != -1
|
||||
norm ^
|
||||
let linenum = line('.')
|
||||
let indentcol = col('.')
|
||||
norm "nye
|
||||
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
|
||||
call RebuildClassList(classlist, [objname, indentcol], classordef)
|
||||
endif " line matched
|
||||
norm j
|
||||
endwhile
|
||||
norm 'p
|
||||
endfunction
|
||||
|
||||
" make a menu that holds all of the function definitions
|
||||
function! MakeFuncStructure ()
|
||||
norm mpgg0
|
||||
while line(".") <= line("$")
|
||||
if match ( getline("."), '^\s*def\s\+' ) != -1
|
||||
norm ^w"nyw
|
||||
let name=@n
|
||||
exe 'menu IM-Python.functions.'.name.' '.line(".").'gg'
|
||||
" classlist contains the list of nested classes we are in.
|
||||
" in most cases it will be empty or contain a single class
|
||||
" but where a class is nested within another, it will contain 2 or more
|
||||
" this function adds or removes classes from the list based on indentation
|
||||
function! RebuildClassList(classlist, newclass, classordef)
|
||||
let i = len(a:classlist) - 1
|
||||
while i > -1
|
||||
if a:newclass[1] <= a:classlist[i][1]
|
||||
call remove(a:classlist, i)
|
||||
endif
|
||||
if line(".") == line("$")
|
||||
return
|
||||
endif
|
||||
norm j
|
||||
let i = i - 1
|
||||
endwhile
|
||||
norm 'p
|
||||
if a:classordef == "class"
|
||||
call add(a:classlist, a:newclass)
|
||||
endif
|
||||
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)
|
||||
" Go to the right line
|
||||
execute 'normal '.a:line.'gg'
|
||||
" Check to see if we are in a fold
|
||||
let lvl = foldlevel(a:line)
|
||||
if lvl != 0
|
||||
" and if so, then expand the fold out, other wise, ignore this part.
|
||||
execute 'normal 15zo'
|
||||
endif
|
||||
endfunction
|
||||
|
||||
"" This one will work only on vim 6.2 because of the try/catch expressions.
|
||||
" function! s:JumpToAndUnfoldWithExceptions(line)
|
||||
" try
|
||||
" execute 'normal '.a:line.'gg15zo'
|
||||
" catch /^Vim\((\a\+)\)\=:E490:/
|
||||
" " Do nothing, just consume the error
|
||||
" endtry
|
||||
"endfunction
|
||||
|
||||
|
||||
" vim:set et sts=2 sw=2:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user