mirror of
https://github.com/gryf/python.vim.git
synced 2025-12-17 11:30:22 +01:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fad734d042 | |||
| e484838bc9 | |||
| 47a79f4251 | |||
| e799ed8118 | |||
|
|
669040957d | ||
|
|
0877af4dff | - | |
|
|
6411a284c5 | - | |
|
|
bf1056d729 | - | |
|
|
ef20af29a9 | - | |
|
|
6dbc984278 | - |
15
README
15
README
@@ -1,15 +0,0 @@
|
|||||||
This is a mirror of http://www.vim.org/scripts/script.php?script_id=30
|
|
||||||
|
|
||||||
Written by Mikael Berthe.
|
|
||||||
|
|
||||||
This script can be useful when editing Python scripts. It provides the following menus:
|
|
||||||
- Select a block of lines with the same indentation
|
|
||||||
- Select a function
|
|
||||||
- Select a class
|
|
||||||
- Go to previous/next class/function
|
|
||||||
- Go to the beginning/end of a block
|
|
||||||
- Comment/uncomment the selection
|
|
||||||
- Jump to the last/next line with the same indent
|
|
||||||
- Shift a block (left/right)
|
|
||||||
- Creates list of classes and functions for easy code navigation
|
|
||||||
Version 1.8+ requires Vim 7, earlier versions require Vim 6
|
|
||||||
52
README.rst
Normal file
52
README.rst
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
Python.vim
|
||||||
|
==========
|
||||||
|
|
||||||
|
This is a set of menus/shortcuts to work with Python files. This work is kind
|
||||||
|
continuation of `Mikael Berthe script`_, with some improvements and fixes.
|
||||||
|
|
||||||
|
Installation
|
||||||
|
------------
|
||||||
|
|
||||||
|
To install it, any kind of Vim package manager can be used, like NeoBundle_,
|
||||||
|
Pathogen_, Vundle_ or vim-plug_.
|
||||||
|
|
||||||
|
For manual installation, copy subdirectories from this repository to your
|
||||||
|
``~/.vim`` directory.
|
||||||
|
|
||||||
|
Usage
|
||||||
|
-----
|
||||||
|
|
||||||
|
Default shortcuts are as follows:
|
||||||
|
|
||||||
|
- ``]]`` - jump to next class
|
||||||
|
- ``[[`` - jump to previous class
|
||||||
|
- ``))`` - jump to next function or method
|
||||||
|
- ``((`` - jump to previous function or method
|
||||||
|
- ``]t`` - jump to beginning of block
|
||||||
|
- ``]e`` - jump to end of block
|
||||||
|
- ``]<up>`` - jump to previous line with the same/lower indentation
|
||||||
|
- ``]<down>`` - jump to next line with the same/lower indentation
|
||||||
|
- ``]<`` - shift block to left
|
||||||
|
- ``]>`` - shift block to right
|
||||||
|
- ``]#`` - comment selection
|
||||||
|
- ``]u`` - uncomment selection
|
||||||
|
- ``vac`` - select (Visual Line Mode) current/previous class
|
||||||
|
- ``vaf`` - select (Visual Line Mode) current/previous function
|
||||||
|
- ``vab`` - select (Visual Line Mode) block
|
||||||
|
|
||||||
|
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
|
||||||
|
are usually the description of the function/class).
|
||||||
|
You can set the global variable ``g:py_select_trailing_comments`` to 0
|
||||||
|
if you don't want to select comments at the end of a function/class.
|
||||||
|
If these variables are not defined, both leading and trailing comments
|
||||||
|
are selected.
|
||||||
|
|
||||||
|
If you use graphical version of vim (like gvim) you can access those options
|
||||||
|
through the menu called *Python*.
|
||||||
|
|
||||||
|
.. _Mikael Berthe script: http://www.vim.org/scripts/script.php?script_id=30
|
||||||
|
.. _Pathogen: https://github.com/tpope/vim-pathogen
|
||||||
|
.. _Vundle: https://github.com/gmarik/Vundle.vim
|
||||||
|
.. _NeoBundle: https://github.com/Shougo/neobundle.vim
|
||||||
|
.. _vim-plug: https://github.com/junegunn/vim-plug
|
||||||
@@ -1,107 +1,87 @@
|
|||||||
" -*- vim -*-
|
" -*- vim -*-
|
||||||
" FILE: python.vim
|
" FILE: python_fn.vim
|
||||||
" LAST MODIFICATION: 2006-08-18 07:30
|
" LAST MODIFICATION: 2017-05-28 17:47:13
|
||||||
" (C) Copyright 2001-2005 Mikael Berthe <bmikael@lists.lilotux.net>
|
" (C) Copyright 2001-2005 Mikael Berthe <bmikael@lists.lilotux.net>
|
||||||
" Version: 1.8
|
" Maintained by Jon Franklin <jvfranklin@gmail.com>
|
||||||
|
" Modifed by Roman Dobosz <gryf73@gmail.com>
|
||||||
|
" Version: 1.14
|
||||||
|
|
||||||
" USAGE:
|
" USAGE:
|
||||||
"
|
" See README.rst
|
||||||
" 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
|
|
||||||
" are usually the description of the function/class).
|
|
||||||
" You can set the global variable "g:py_select_trailing_comments" to 0
|
|
||||||
" if you don't want to select comments at the end of a function/class.
|
|
||||||
" If these variables are not defined, both leading and trailing comments
|
|
||||||
" are selected.
|
|
||||||
" Example: (in your .vimrc) "let g:py_select_leading_comments = 0"
|
|
||||||
" You may want to take a look at the 'shiftwidth' option for the
|
|
||||||
" shift commands...
|
|
||||||
"
|
|
||||||
" REQUIREMENTS:
|
|
||||||
" vim (>= 6)
|
|
||||||
"
|
|
||||||
" Shortcuts:
|
" Shortcuts:
|
||||||
" [[ -- Jump to beginning of block
|
" Only do this when not done yet for this buffer
|
||||||
" ]] -- Jump to end of block
|
if exists("b:loaded_py_ftplugin")
|
||||||
" ]v -- Select (Visual Line Mode) block
|
finish
|
||||||
" ]< -- Shift block to left
|
endif
|
||||||
" ]> -- Shift block to right
|
let b:loaded_py_ftplugin = 1
|
||||||
" ]c -- Select current/previous class
|
|
||||||
" ]f -- Select current/previous function
|
|
||||||
" ]<up> -- Jump to previous line with the same/lower indentation
|
|
||||||
" ]<down> -- Jump to next line with the same/lower indentation
|
|
||||||
|
|
||||||
|
map ]t :PBoB<CR>
|
||||||
|
vmap ]t :<C-U>PBOB<CR>m'gv``
|
||||||
|
map ]e :PEoB<CR>
|
||||||
|
vmap ]e :<C-U>PEoB<CR>m'gv``
|
||||||
|
|
||||||
map [[ :PBoB<CR>
|
map vab ]tV]e
|
||||||
vmap [[ :<C-U>PBoB<CR>m'gv``
|
map ]< ]tV]e<
|
||||||
map ]] :PEoB<CR>
|
|
||||||
vmap ]] :<C-U>PEoB<CR>m'gv``
|
|
||||||
|
|
||||||
map ]v [[V]]
|
|
||||||
map ]< [[V]]<
|
|
||||||
vmap ]< <
|
vmap ]< <
|
||||||
map ]> [[V]]>
|
map ]> ]tV]e>
|
||||||
vmap ]> >
|
vmap ]> >
|
||||||
|
|
||||||
map ]c :call PythonSelectObject("class")<CR>
|
map ]# :call PythonCommentSelection()<CR>
|
||||||
map ]f :call PythonSelectObject("function")<CR>
|
vmap ]# :call PythonCommentSelection()<CR>
|
||||||
|
map ]u :call PythonUncommentSelection()<CR>
|
||||||
|
vmap ]u :call PythonUncommentSelection()<CR>
|
||||||
|
|
||||||
|
map vac :call PythonSelectObject("class")<CR>
|
||||||
|
map vaf :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 [[ :call PythonDec("class", -1)<CR>
|
||||||
|
vmap [[ :call PythonDec("class", -1)<CR>
|
||||||
|
|
||||||
|
" jump to next class
|
||||||
|
map ]] :call PythonDec("class", 1)<CR>
|
||||||
|
vmap ]] :call PythonDec("class", 1)<CR>
|
||||||
|
|
||||||
|
" jump to previous function
|
||||||
|
map (( :call PythonDec("function", -1)<CR>
|
||||||
|
vmap (( :call PythonDec("function", -1)<CR>
|
||||||
|
|
||||||
|
" jump to next function
|
||||||
|
map )) :call PythonDec("function", 1)<CR>
|
||||||
|
vmap )) :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>]e ]e
|
||||||
nmenu <silent> &Python.End\ of\ Block<Tab>]]
|
|
||||||
\]]
|
|
||||||
nmenu &Python.-Sep2- :
|
nmenu &Python.-Sep2- :
|
||||||
nmenu <silent> &Python.Shift\ Block\ Left<Tab>]<
|
nmenu <silent> &Python.Shift\ Block\ Left<Tab>]< ]<
|
||||||
\]<
|
vmenu <silent> &Python.Shift\ Block\ Left<Tab>]< ]<
|
||||||
vmenu <silent> &Python.Shift\ Block\ Left<Tab>]<
|
nmenu <silent> &Python.Shift\ Block\ Right<Tab>]> ]>
|
||||||
\]<
|
vmenu <silent> &Python.Shift\ Block\ Right<Tab>]> ]>
|
||||||
nmenu <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<Tab>]# ]#
|
||||||
nmenu <silent> &Python.Comment\ Selection
|
vmenu <silent> &Python.Uncomment\ Selection<Tab>]u ]u
|
||||||
\:call PythonCommentSelection()<CR>
|
nmenu <silent> &Python.Uncomment\ Selection<Tab>]u ]u
|
||||||
vmenu <silent> &Python.Uncomment\ Selection
|
|
||||||
\:call PythonUncommentSelection()<CR>
|
|
||||||
nmenu <silent> &Python.Uncomment\ Selection
|
|
||||||
\:call PythonUncommentSelection()<CR>
|
|
||||||
nmenu &Python.-Sep4- :
|
nmenu &Python.-Sep4- :
|
||||||
nmenu <silent> &Python.Previous\ Class
|
nmenu <silent> &Python.Previous\ Class<Tab>[[ [[
|
||||||
\:call PythonDec("class", -1)<CR>
|
nmenu <silent> &Python.Next\ Class<Tab>]] ]]
|
||||||
nmenu <silent> &Python.Next\ Class
|
nmenu <silent> &Python.Previous\ Function<Tab>{{ ((
|
||||||
\:call PythonDec("class", 1)<CR>
|
nmenu <silent> &Python.Next\ Function<Tab>}} ))
|
||||||
nmenu <silent> &Python.Previous\ Function
|
|
||||||
\:call PythonDec("function", -1)<CR>
|
|
||||||
nmenu <silent> &Python.Next\ Function
|
|
||||||
\:call PythonDec("function", 1)<CR>
|
|
||||||
nmenu &Python.-Sep5- :
|
nmenu &Python.-Sep5- :
|
||||||
nmenu <silent> &Python.Select\ Block<Tab>]v
|
nmenu <silent> &Python.Select\ Block<Tab>vab vab
|
||||||
\]v
|
nmenu <silent> &Python.Select\ Function<Tab>vaf vaf
|
||||||
nmenu <silent> &Python.Select\ Function<Tab>]f
|
nmenu <silent> &Python.Select\ Class<Tab>vac vac
|
||||||
\]f
|
|
||||||
nmenu <silent> &Python.Select\ Class<Tab>]c
|
|
||||||
\]c
|
|
||||||
nmenu &Python.-Sep6- :
|
nmenu &Python.-Sep6- :
|
||||||
nmenu <silent> &Python.Previous\ Line\ wrt\ indent<Tab>]<up>
|
nmenu <silent> &Python.Previous\ Line\ wrt\ indent<Tab>]<up> ]<up>
|
||||||
\]<up>
|
nmenu <silent> &Python.Next\ Line\ wrt\ indent<Tab>]<down> ]<down>
|
||||||
nmenu <silent> &Python.Next\ Line\ wrt\ indent<Tab>]<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"
|
||||||
@@ -117,15 +97,15 @@ function! PythonBoB(line, direction, force_sel_comments)
|
|||||||
let indent_valid = strlen(getline(ln))
|
let indent_valid = strlen(getline(ln))
|
||||||
let ln = ln + a:direction
|
let ln = ln + a:direction
|
||||||
if (a:direction == 1) && (!a:force_sel_comments) &&
|
if (a:direction == 1) && (!a:force_sel_comments) &&
|
||||||
\ exists("g:py_select_trailing_comments") &&
|
\ exists("g:py_select_trailing_comments") &&
|
||||||
\ (!g:py_select_trailing_comments)
|
\ (!g:py_select_trailing_comments)
|
||||||
let sel_comments = 0
|
let sel_comments = 0
|
||||||
else
|
else
|
||||||
let sel_comments = 1
|
let sel_comments = 1
|
||||||
endif
|
endif
|
||||||
|
|
||||||
while((ln >= 1) && (ln <= line('$')))
|
while((ln >= 1) && (ln <= line('$')))
|
||||||
if (sel_comments) || (match(getline(ln), "^\\s*#") == -1)
|
if (sel_comments) || (match(getline(ln), "^\\s*#") == -1)
|
||||||
if (!indent_valid)
|
if (!indent_valid)
|
||||||
let indent_valid = strlen(getline(ln))
|
let indent_valid = strlen(getline(ln))
|
||||||
let ind = indent(ln)
|
let ind = indent(ln)
|
||||||
@@ -150,9 +130,9 @@ endfunction
|
|||||||
function! PythonDec(obj, direction)
|
function! PythonDec(obj, direction)
|
||||||
if (a:obj == "class")
|
if (a:obj == "class")
|
||||||
let objregexp = "^\\s*class\\s\\+[a-zA-Z0-9_]\\+"
|
let objregexp = "^\\s*class\\s\\+[a-zA-Z0-9_]\\+"
|
||||||
\ . "\\s*\\((\\([a-zA-Z0-9_,. \\t\\n]\\)*)\\)\\=\\s*:"
|
\ . "\\s*\\((\\([a-zA-Z0-9_,. \\t\\n]\\)*)\\)\\=\\s*:"
|
||||||
else
|
else
|
||||||
let objregexp = "^\\s*def\\s\\+[a-zA-Z0-9_]\\+\\s*(\\_[^:#]*)\\s*:"
|
let objregexp = "^\\s*\\(async def\\|def\\)\\s\\+[a-zA-Z0-9_]\\+\\s*(\\_[^:#]*)\\s*:"
|
||||||
endif
|
endif
|
||||||
let flag = "W"
|
let flag = "W"
|
||||||
if (a:direction == -1)
|
if (a:direction == -1)
|
||||||
@@ -166,7 +146,7 @@ endfunction
|
|||||||
" commentString is inserted in non-empty lines, and should be aligned with
|
" commentString is inserted in non-empty lines, and should be aligned with
|
||||||
" the block
|
" the block
|
||||||
function! PythonCommentSelection() range
|
function! PythonCommentSelection() range
|
||||||
let commentString = "#"
|
let commentString = "# "
|
||||||
let cl = a:firstline
|
let cl = a:firstline
|
||||||
let ind = 1000 " I hope nobody use so long lines! :)
|
let ind = 1000 " I hope nobody use so long lines! :)
|
||||||
|
|
||||||
@@ -200,11 +180,11 @@ endfunction
|
|||||||
function! PythonUncommentSelection() range
|
function! PythonUncommentSelection() range
|
||||||
" commentString could be different than the one from CommentSelection()
|
" commentString could be different than the one from CommentSelection()
|
||||||
" For example, this could be "# \\="
|
" For example, this could be "# \\="
|
||||||
let commentString = "#"
|
let commentString = "# "
|
||||||
let cl = a:firstline
|
let cl = a:firstline
|
||||||
while (cl <= a:lastline)
|
while (cl <= a:lastline)
|
||||||
let ul = substitute(getline(cl),
|
let ul = substitute(getline(cl),
|
||||||
\"\\(\\s*\\)".commentString."\\(.*\\)$", "\\1\\2", "")
|
\"\\(\\s*\\)".commentString."\\(.*\\)$", "\\1\\2", "")
|
||||||
call setline(cl, ul)
|
call setline(cl, ul)
|
||||||
let cl = cl + 1
|
let cl = cl + 1
|
||||||
endwhile
|
endwhile
|
||||||
@@ -233,9 +213,9 @@ function! PythonSelectObject(obj)
|
|||||||
|
|
||||||
if (a:obj == "class")
|
if (a:obj == "class")
|
||||||
let eod = "\\(^\\s*class\\s\\+[a-zA-Z0-9_]\\+\\s*"
|
let eod = "\\(^\\s*class\\s\\+[a-zA-Z0-9_]\\+\\s*"
|
||||||
\ . "\\((\\([a-zA-Z0-9_,. \\t\\n]\\)*)\\)\\=\\s*\\)\\@<=:"
|
\ . "\\((\\([a-zA-Z0-9_,. \\t\\n]\\)*)\\)\\=\\s*\\)\\@<=:"
|
||||||
else
|
else
|
||||||
let eod = "\\(^\\s*def\\s\\+[a-zA-Z0-9_]\\+\\s*(\\_[^:#]*)\\s*\\)\\@<=:"
|
let eod = "\\(^\\s*\\(async def\\|def\\)\\s\\+[a-zA-Z0-9_]\\+\\s*(\\_[^:#]*)\\s*\\)\\@<=:"
|
||||||
endif
|
endif
|
||||||
" Look for the end of the declaration (not always the same line!)
|
" Look for the end of the declaration (not always the same line!)
|
||||||
call search(eod, "")
|
call search(eod, "")
|
||||||
@@ -265,7 +245,7 @@ function! PythonNextLine(direction)
|
|||||||
|
|
||||||
while((ln >= 1) && (ln <= line('$')))
|
while((ln >= 1) && (ln <= line('$')))
|
||||||
if (!indent_valid) && strlen(getline(ln))
|
if (!indent_valid) && strlen(getline(ln))
|
||||||
break
|
break
|
||||||
else
|
else
|
||||||
if (strlen(getline(ln)))
|
if (strlen(getline(ln)))
|
||||||
if (indent(ln) <= ind)
|
if (indent(ln) <= ind)
|
||||||
@@ -286,8 +266,6 @@ function! UpdateMenu()
|
|||||||
" all buffers currently open, and group classes and functions by buffer
|
" all buffers currently open, and group classes and functions by buffer
|
||||||
if exists("g:menuran")
|
if exists("g:menuran")
|
||||||
aunmenu IM-Python
|
aunmenu IM-Python
|
||||||
else
|
|
||||||
let g:menuran=1
|
|
||||||
endif
|
endif
|
||||||
let restore_fe = &foldenable
|
let restore_fe = &foldenable
|
||||||
set nofoldenable
|
set nofoldenable
|
||||||
@@ -312,7 +290,7 @@ function! MenuBuilder()
|
|||||||
let parentclass = ""
|
let parentclass = ""
|
||||||
while line(".") < line("$")
|
while line(".") < line("$")
|
||||||
" search for a class or function
|
" search for a class or function
|
||||||
if match ( getline("."), '^\s*class\s\+[a-zA-Z].*:\|^\s*def\s\+[a-zA-Z].*:' ) != -1
|
if match ( getline("."), '^\s*class\s\+[_a-zA-Z].*\|^\s*\(async def\|def\)\s\+[_a-zA-Z].*' ) != -1
|
||||||
norm ^
|
norm ^
|
||||||
let linenum = line('.')
|
let linenum = line('.')
|
||||||
let indentcol = col('.')
|
let indentcol = col('.')
|
||||||
@@ -326,6 +304,8 @@ function! MenuBuilder()
|
|||||||
else " this is a function
|
else " this is a function
|
||||||
call AddFunction(objname, linenum, parentclass)
|
call AddFunction(objname, linenum, parentclass)
|
||||||
endif
|
endif
|
||||||
|
" We actually created a menu, so lets set the global variable
|
||||||
|
let g:menuran=1
|
||||||
call RebuildClassList(classlist, [objname, indentcol], classordef)
|
call RebuildClassList(classlist, [objname, indentcol], classordef)
|
||||||
endif " line matched
|
endif " line matched
|
||||||
norm j
|
norm j
|
||||||
@@ -401,15 +381,4 @@ function! s:JumpToAndUnfold(line)
|
|||||||
endif
|
endif
|
||||||
endfunction
|
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:
|
" vim:set et sts=2 sw=2:
|
||||||
|
|
||||||
Reference in New Issue
Block a user