From 669040957dfc70537b4c94273f5ef1b7a381f2c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20T=C3=B6rnqvist?= Date: Fri, 28 Oct 2016 08:15:30 +0300 Subject: [PATCH 1/5] Python3.5 async def support --- plugin/python_fn.vim | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugin/python_fn.vim b/plugin/python_fn.vim index 7c7cf21..366bbb4 100644 --- a/plugin/python_fn.vim +++ b/plugin/python_fn.vim @@ -183,7 +183,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) @@ -266,7 +266,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 +341,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('.') @@ -434,7 +434,7 @@ endfunction "" This one will work only on vim 6.2 because of the try/catch expressions. " function! s:JumpToAndUnfoldWithExceptions(line) -" try +" try " execute 'normal '.a:line.'gg15zo' " catch /^Vim\((\a\+)\)\=:E490:/ " " Do nothing, just consume the error From e799ed8118785745de1d7a2173933cc75d52249b Mon Sep 17 00:00:00 2001 From: gryf Date: Tue, 3 Jan 2017 13:54:10 +0100 Subject: [PATCH 2/5] Moved plugin to ftplugin/python --- {plugin => ftplugin/python}/python_fn.vim | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {plugin => ftplugin/python}/python_fn.vim (100%) diff --git a/plugin/python_fn.vim b/ftplugin/python/python_fn.vim similarity index 100% rename from plugin/python_fn.vim rename to ftplugin/python/python_fn.vim From 47a79f425188850d29b9df69fb18a292d2a46ed9 Mon Sep 17 00:00:00 2001 From: gryf Date: Sun, 28 May 2017 14:26:28 +0200 Subject: [PATCH 3/5] Clean up indentation and commented code --- ftplugin/python/python_fn.vim | 75 ++++++++++++++--------------------- 1 file changed, 29 insertions(+), 46 deletions(-) diff --git a/ftplugin/python/python_fn.vim b/ftplugin/python/python_fn.vim index 366bbb4..278edd2 100644 --- a/ftplugin/python/python_fn.vim +++ b/ftplugin/python/python_fn.vim @@ -7,11 +7,7 @@ " 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. +" See README for installation. " 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). @@ -84,55 +80,53 @@ vmap ]F :call PythonDec("function", -1) map ]f :call PythonDec("function", 1) vmap ]f :call PythonDec("function", 1) - - " Menu entries nmenu &Python.Update\ IM-Python\ Menu - \:call UpdateMenu() + \:call UpdateMenu() nmenu &Python.-Sep1- : nmenu &Python.Beginning\ of\ Block[t - \]t + \]t nmenu &Python.End\ of\ Block]e - \]e + \]e nmenu &Python.-Sep2- : nmenu &Python.Shift\ Block\ Left]< - \]< + \]< vmenu &Python.Shift\ Block\ Left]< - \]< + \]< nmenu &Python.Shift\ Block\ Right]> - \]> + \]> vmenu &Python.Shift\ Block\ Right]> - \]> + \]> nmenu &Python.-Sep3- : vmenu &Python.Comment\ Selection]# - \]# + \]# nmenu &Python.Comment\ Selection]# - \]# + \]# vmenu &Python.Uncomment\ Selection]u - \]u + \]u nmenu &Python.Uncomment\ Selection]u - \]u + \]u nmenu &Python.-Sep4- : nmenu &Python.Previous\ Class]J - \]J + \]J nmenu &Python.Next\ Class]j - \]j + \]j nmenu &Python.Previous\ Function]F - \]F + \]F nmenu &Python.Next\ Function]f - \]f + \]f nmenu &Python.-Sep5- : nmenu &Python.Select\ Block]v - \]v + \]v nmenu &Python.Select\ Function]d - \]d + \]d nmenu &Python.Select\ Class]c - \]c + \]c nmenu &Python.-Sep6- : nmenu &Python.Previous\ Line\ wrt\ indent] - \] + \] nmenu &Python.Next\ Line\ wrt\ indent] - \] + \] :com! PBoB execute "normal ".PythonBoB(line('.'), -1, 1)."G" :com! PEoB execute "normal ".PythonBoB(line('.'), 1, 1)."G" @@ -148,15 +142,15 @@ function! PythonBoB(line, direction, force_sel_comments) let indent_valid = strlen(getline(ln)) let ln = ln + a:direction if (a:direction == 1) && (!a:force_sel_comments) && - \ exists("g:py_select_trailing_comments") && - \ (!g:py_select_trailing_comments) + \ exists("g:py_select_trailing_comments") && + \ (!g:py_select_trailing_comments) let sel_comments = 0 else let sel_comments = 1 endif while((ln >= 1) && (ln <= line('$'))) - if (sel_comments) || (match(getline(ln), "^\\s*#") == -1) + if (sel_comments) || (match(getline(ln), "^\\s*#") == -1) if (!indent_valid) let indent_valid = strlen(getline(ln)) let ind = indent(ln) @@ -181,7 +175,7 @@ endfunction function! PythonDec(obj, direction) if (a:obj == "class") 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 let objregexp = "^\\s*\\(async def\\|def\\)\\s\\+[a-zA-Z0-9_]\\+\\s*(\\_[^:#]*)\\s*:" endif @@ -235,7 +229,7 @@ function! PythonUncommentSelection() range let cl = a:firstline while (cl <= a:lastline) let ul = substitute(getline(cl), - \"\\(\\s*\\)".commentString."\\(.*\\)$", "\\1\\2", "") + \"\\(\\s*\\)".commentString."\\(.*\\)$", "\\1\\2", "") call setline(cl, ul) let cl = cl + 1 endwhile @@ -264,9 +258,9 @@ function! PythonSelectObject(obj) if (a:obj == "class") 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 - let eod = "\\(^\\s*\\(async def\\|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, "") @@ -296,7 +290,7 @@ function! PythonNextLine(direction) while((ln >= 1) && (ln <= line('$'))) if (!indent_valid) && strlen(getline(ln)) - break + break else if (strlen(getline(ln))) if (indent(ln) <= ind) @@ -432,15 +426,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: - From e484838bc9d971c05c97c2732beec32398eb58cc Mon Sep 17 00:00:00 2001 From: gryf Date: Sun, 28 May 2017 18:13:03 +0200 Subject: [PATCH 4/5] Changed some of the default shortcuts --- README | 15 ----- README.rst | 52 +++++++++++++++ ftplugin/python/python_fn.vim | 121 +++++++++++----------------------- 3 files changed, 90 insertions(+), 98 deletions(-) delete mode 100644 README create mode 100644 README.rst diff --git a/README b/README deleted file mode 100644 index 50fe96d..0000000 --- a/README +++ /dev/null @@ -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 diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..deaad47 --- /dev/null +++ b/README.rst @@ -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 +- ``]`` - jump to previous line with the same/lower indentation +- ``]`` - 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 diff --git a/ftplugin/python/python_fn.vim b/ftplugin/python/python_fn.vim index 278edd2..a3182d3 100644 --- a/ftplugin/python/python_fn.vim +++ b/ftplugin/python/python_fn.vim @@ -1,40 +1,15 @@ " -*- vim -*- " FILE: python_fn.vim -" LAST MODIFICATION: 2008-08-28 8:19pm +" LAST MODIFICATION: 2017-05-28 17:47:13 " (C) Copyright 2001-2005 Mikael Berthe " Maintained by Jon Franklin -" Version: 1.13 +" Modifed by Roman Dobosz +" Version: 1.14 " USAGE: -" -" See README for installation. -" 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 -" ] -- Jump to previous line with the same/lower indentation -" ] -- 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 @@ -46,7 +21,7 @@ vmap ]t :PBOBm'gv`` map ]e :PEoB vmap ]e :PEoBm'gv`` -map ]v ]tV]e +map vab ]tV]e map ]< ]tV]e< vmap ]< < map ]> ]tV]e> @@ -57,76 +32,56 @@ vmap ]# :call PythonCommentSelection() map ]u :call PythonUncommentSelection() vmap ]u :call PythonUncommentSelection() -map ]c :call PythonSelectObject("class") -map ]d :call PythonSelectObject("function") +map vac :call PythonSelectObject("class") +map vaf :call PythonSelectObject("function") map ] :call PythonNextLine(-1) map ] :call PythonNextLine(1) " You may prefer use and ... :-) " jump to previous class -map ]J :call PythonDec("class", -1) -vmap ]J :call PythonDec("class", -1) +map [[ :call PythonDec("class", -1) +vmap [[ :call PythonDec("class", -1) " jump to next class -map ]j :call PythonDec("class", 1) -vmap ]j :call PythonDec("class", 1) +map ]] :call PythonDec("class", 1) +vmap ]] :call PythonDec("class", 1) " jump to previous function -map ]F :call PythonDec("function", -1) -vmap ]F :call PythonDec("function", -1) +map {{ :call PythonDec("function", -1) +vmap {{ :call PythonDec("function", -1) " jump to next function -map ]f :call PythonDec("function", 1) -vmap ]f :call PythonDec("function", 1) +map }} :call PythonDec("function", 1) +vmap }} :call PythonDec("function", 1) " Menu entries -nmenu &Python.Update\ IM-Python\ Menu - \:call UpdateMenu() +nmenu &Python.Update\ IM-Python\ Menu :call UpdateMenu() nmenu &Python.-Sep1- : -nmenu &Python.Beginning\ of\ Block[t - \]t -nmenu &Python.End\ of\ Block]e - \]e +nmenu &Python.Beginning\ of\ Block[t ]t +nmenu &Python.End\ of\ Block]e ]e nmenu &Python.-Sep2- : -nmenu &Python.Shift\ Block\ Left]< - \]< -vmenu &Python.Shift\ Block\ Left]< - \]< -nmenu &Python.Shift\ Block\ Right]> - \]> -vmenu &Python.Shift\ Block\ Right]> - \]> +nmenu &Python.Shift\ Block\ Left]< ]< +vmenu &Python.Shift\ Block\ Left]< ]< +nmenu &Python.Shift\ Block\ Right]> ]> +vmenu &Python.Shift\ Block\ Right]> ]> nmenu &Python.-Sep3- : -vmenu &Python.Comment\ Selection]# - \]# -nmenu &Python.Comment\ Selection]# - \]# -vmenu &Python.Uncomment\ Selection]u - \]u -nmenu &Python.Uncomment\ Selection]u - \]u +vmenu &Python.Comment\ Selection]# ]# +nmenu &Python.Comment\ Selection]# ]# +vmenu &Python.Uncomment\ Selection]u ]u +nmenu &Python.Uncomment\ Selection]u ]u nmenu &Python.-Sep4- : -nmenu &Python.Previous\ Class]J - \]J -nmenu &Python.Next\ Class]j - \]j -nmenu &Python.Previous\ Function]F - \]F -nmenu &Python.Next\ Function]f - \]f +nmenu &Python.Previous\ Class[[ [[ +nmenu &Python.Next\ Class]] ]] +nmenu &Python.Previous\ Function{{ {{ +nmenu &Python.Next\ Function}} }} nmenu &Python.-Sep5- : -nmenu &Python.Select\ Block]v - \]v -nmenu &Python.Select\ Function]d - \]d -nmenu &Python.Select\ Class]c - \]c +nmenu &Python.Select\ Blockvab vab +nmenu &Python.Select\ Functionvaf vaf +nmenu &Python.Select\ Classvac vac nmenu &Python.-Sep6- : -nmenu &Python.Previous\ Line\ wrt\ indent] - \] -nmenu &Python.Next\ Line\ wrt\ indent] - \] +nmenu &Python.Previous\ Line\ wrt\ indent] ] +nmenu &Python.Next\ Line\ wrt\ indent] ] :com! PBoB execute "normal ".PythonBoB(line('.'), -1, 1)."G" :com! PEoB execute "normal ".PythonBoB(line('.'), 1, 1)."G" @@ -191,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! :) @@ -225,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), From fad734d0421f806d0653d056b3006a92b6ecf369 Mon Sep 17 00:00:00 2001 From: gryf Date: Sun, 4 Jun 2017 14:46:08 +0200 Subject: [PATCH 5/5] Changed mapping for function --- README.rst | 4 ++-- ftplugin/python/python_fn.vim | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.rst b/README.rst index deaad47..aa03663 100644 --- a/README.rst +++ b/README.rst @@ -20,8 +20,8 @@ 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 +- ``))`` - jump to next function or method +- ``((`` - jump to previous function or method - ``]t`` - jump to beginning of block - ``]e`` - jump to end of block - ``]`` - jump to previous line with the same/lower indentation diff --git a/ftplugin/python/python_fn.vim b/ftplugin/python/python_fn.vim index a3182d3..76d59e3 100644 --- a/ftplugin/python/python_fn.vim +++ b/ftplugin/python/python_fn.vim @@ -48,12 +48,12 @@ map ]] :call PythonDec("class", 1) vmap ]] :call PythonDec("class", 1) " jump to previous function -map {{ :call PythonDec("function", -1) -vmap {{ :call PythonDec("function", -1) +map (( :call PythonDec("function", -1) +vmap (( :call PythonDec("function", -1) " jump to next function -map }} :call PythonDec("function", 1) -vmap }} :call PythonDec("function", 1) +map )) :call PythonDec("function", 1) +vmap )) :call PythonDec("function", 1) " Menu entries nmenu &Python.Update\ IM-Python\ Menu :call UpdateMenu() @@ -73,8 +73,8 @@ nmenu &Python.Uncomment\ Selection]u ]u nmenu &Python.-Sep4- : nmenu &Python.Previous\ Class[[ [[ nmenu &Python.Next\ Class]] ]] -nmenu &Python.Previous\ Function{{ {{ -nmenu &Python.Next\ Function}} }} +nmenu &Python.Previous\ Function{{ (( +nmenu &Python.Next\ Function}} )) nmenu &Python.-Sep5- : nmenu &Python.Select\ Blockvab vab nmenu &Python.Select\ Functionvaf vaf