1
0
mirror of https://github.com/gryf/python.vim.git synced 2025-12-17 19:40:28 +01:00

6 Commits
1.12 ... master

Author SHA1 Message Date
fad734d042 Changed mapping for function 2017-06-04 14:46:08 +02:00
e484838bc9 Changed some of the default shortcuts 2017-05-28 18:13:03 +02:00
47a79f4251 Clean up indentation and commented code 2017-05-28 14:42:09 +02:00
e799ed8118 Moved plugin to ftplugin/python 2017-01-03 13:54:10 +01:00
Markus Törnqvist
669040957d Python3.5 async def support 2017-01-03 13:52:11 +01:00
Jon Franklin
0877af4dff Version 1.13
Fix for bug that prevented multi-line class and function definitions from showing up in the IM-Python menu.  Patch provided by Branden Rolston.
-
3 changed files with 101 additions and 126 deletions

15
README
View File

@@ -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
View 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

View File

@@ -1,44 +1,15 @@
" -*- vim -*-
" FILE: python.vim
" LAST MODIFICATION: 2008-05-17 6:29pm
" FILE: python_fn.vim
" LAST MODIFICATION: 2017-05-28 17:47:13
" (C) Copyright 2001-2005 Mikael Berthe <bmikael@lists.lilotux.net>
" Maintained by Jon Franklin <jvfranklin@gmail.com>
" Version: 1.12
" Modifed by Roman Dobosz <gryf73@gmail.com>
" Version: 1.14
" USAGE:
"
" Save this file to $VIMFILES/ftplugin/python.vim. You can have multiple
" 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
" 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 (>= 7)
"
" Shortcuts:
" ]t -- Jump to beginning of block
" ]e -- Jump to end of block
" ]v -- Select (Visual Line Mode) block
" ]< -- Shift block to left
" ]> -- Shift block to right
" ]# -- Comment selection
" ]u -- Uncomment selection
" ]c -- Select current/previous class
" ]d -- Select current/previous function
" ]<up> -- Jump to previous line with the same/lower indentation
" ]<down> -- Jump to next line with the same/lower indentation
" See README.rst
" Shortcuts:
" Only do this when not done yet for this buffer
if exists("b:loaded_py_ftplugin")
finish
@@ -50,7 +21,7 @@ vmap ]t :<C-U>PBOB<CR>m'gv``
map ]e :PEoB<CR>
vmap ]e :<C-U>PEoB<CR>m'gv``
map ]v ]tV]e
map vab ]tV]e
map ]< ]tV]e<
vmap ]< <
map ]> ]tV]e>
@@ -61,78 +32,56 @@ vmap ]# :call PythonCommentSelection()<CR>
map ]u :call PythonUncommentSelection()<CR>
vmap ]u :call PythonUncommentSelection()<CR>
map ]c :call PythonSelectObject("class")<CR>
map ]d :call PythonSelectObject("function")<CR>
map vac :call PythonSelectObject("class")<CR>
map vaf :call PythonSelectObject("function")<CR>
map ]<up> :call PythonNextLine(-1)<CR>
map ]<down> :call PythonNextLine(1)<CR>
" 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>
map [[ :call PythonDec("class", -1)<CR>
vmap [[ :call PythonDec("class", -1)<CR>
" jump to next class
map ]j :call PythonDec("class", 1)<CR>
vmap ]j :call PythonDec("class", 1)<CR>
map ]] :call PythonDec("class", 1)<CR>
vmap ]] :call PythonDec("class", 1)<CR>
" jump to previous function
map ]F :call PythonDec("function", -1)<CR>
vmap ]F :call PythonDec("function", -1)<CR>
map (( :call PythonDec("function", -1)<CR>
vmap (( :call PythonDec("function", -1)<CR>
" jump to next function
map ]f :call PythonDec("function", 1)<CR>
vmap ]f :call PythonDec("function", 1)<CR>
map )) :call PythonDec("function", 1)<CR>
vmap )) :call PythonDec("function", 1)<CR>
" Menu entries
nmenu <silent> &Python.Update\ IM-Python\ Menu
\:call UpdateMenu()<CR>
nmenu <silent> &Python.Update\ IM-Python\ Menu :call UpdateMenu()<CR>
nmenu &Python.-Sep1- :
nmenu <silent> &Python.Beginning\ of\ Block<Tab>[t
\]t
nmenu <silent> &Python.End\ of\ Block<Tab>]e
\]e
nmenu <silent> &Python.Beginning\ of\ Block<Tab>[t ]t
nmenu <silent> &Python.End\ of\ Block<Tab>]e ]e
nmenu &Python.-Sep2- :
nmenu <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\ Left<Tab>]< ]<
vmenu <silent> &Python.Shift\ Block\ Left<Tab>]< ]<
nmenu <silent> &Python.Shift\ Block\ Right<Tab>]> ]>
vmenu <silent> &Python.Shift\ Block\ Right<Tab>]> ]>
nmenu &Python.-Sep3- :
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
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<Tab>]J
\]J
nmenu <silent> &Python.Next\ Class<Tab>]j
\]j
nmenu <silent> &Python.Previous\ Function<Tab>]F
\]F
nmenu <silent> &Python.Next\ Function<Tab>]f
\]f
nmenu <silent> &Python.Previous\ Class<Tab>[[ [[
nmenu <silent> &Python.Next\ Class<Tab>]] ]]
nmenu <silent> &Python.Previous\ Function<Tab>{{ ((
nmenu <silent> &Python.Next\ Function<Tab>}} ))
nmenu &Python.-Sep5- :
nmenu <silent> &Python.Select\ Block<Tab>]v
\]v
nmenu <silent> &Python.Select\ Function<Tab>]d
\]d
nmenu <silent> &Python.Select\ Class<Tab>]c
\]c
nmenu <silent> &Python.Select\ Block<Tab>vab vab
nmenu <silent> &Python.Select\ Function<Tab>vaf vaf
nmenu <silent> &Python.Select\ Class<Tab>vac vac
nmenu &Python.-Sep6- :
nmenu <silent> &Python.Previous\ Line\ wrt\ indent<Tab>]<up>
\]<up>
nmenu <silent> &Python.Next\ Line\ wrt\ indent<Tab>]<down>
\]<down>
nmenu <silent> &Python.Previous\ Line\ wrt\ indent<Tab>]<up> ]<up>
nmenu <silent> &Python.Next\ Line\ wrt\ indent<Tab>]<down> ]<down>
:com! PBoB execute "normal ".PythonBoB(line('.'), -1, 1)."G"
:com! PEoB execute "normal ".PythonBoB(line('.'), 1, 1)."G"
@@ -183,7 +132,7 @@ function! PythonDec(obj, direction)
let objregexp = "^\\s*class\\s\\+[a-zA-Z0-9_]\\+"
\ . "\\s*\\((\\([a-zA-Z0-9_,. \\t\\n]\\)*)\\)\\=\\s*:"
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
let flag = "W"
if (a:direction == -1)
@@ -197,7 +146,7 @@ endfunction
" commentString is inserted in non-empty lines, and should be aligned with
" the block
function! PythonCommentSelection() range
let commentString = "#"
let commentString = "# "
let cl = a:firstline
let ind = 1000 " I hope nobody use so long lines! :)
@@ -231,7 +180,7 @@ endfunction
function! PythonUncommentSelection() range
" commentString could be different than the one from CommentSelection()
" For example, this could be "# \\="
let commentString = "#"
let commentString = "# "
let cl = a:firstline
while (cl <= a:lastline)
let ul = substitute(getline(cl),
@@ -266,7 +215,7 @@ function! PythonSelectObject(obj)
let eod = "\\(^\\s*class\\s\\+[a-zA-Z0-9_]\\+\\s*"
\ . "\\((\\([a-zA-Z0-9_,. \\t\\n]\\)*)\\)\\=\\s*\\)\\@<=:"
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
" Look for the end of the declaration (not always the same line!)
call search(eod, "")
@@ -341,7 +290,7 @@ function! MenuBuilder()
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
if match ( getline("."), '^\s*class\s\+[_a-zA-Z].*\|^\s*\(async def\|def\)\s\+[_a-zA-Z].*' ) != -1
norm ^
let linenum = line('.')
let indentcol = col('.')
@@ -432,15 +381,4 @@ function! s:JumpToAndUnfold(line)
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: