Re: [vim-perl] ]m next-method, [m previous-method

128 views
Skip to first unread message

Andy Lester

unread,
Feb 14, 2013, 10:04:22 AM2/14/13
to vim-...@googlegroups.com

On Feb 14, 2013, at 4:19 AM, Catalin Ciurea <catalin...@gmail.com> wrote:

au FileType perl map <silent> ]m :call search('\v^\s*sub\s*\w+\s*(\(\s*.{-}\s*\))?\s*\{', 'W')<CR>
au FileType perl map <silent> [m :call search('\v^\s*sub\s*\w+\s*(\(\s*.{-}\s*\))?\s*\{', 'bW')<CR>

They are not good enaugh though as they need to be created like motion commands to be able to type 2]m to jump 2 methods. 

But I wanted to ask you guys what do you think about it and if it would be appropriate to integrate with vim-perl along the syntax files and the others.

I think it would be great to have.  Isn't there a way to, rather than define a new keymapping, tell vim "this is how you decide what a method looks like?" so we're just changing the function definition, and not the actual keymapping?

Thanks,
xoa

Catalin Ciurea

unread,
Feb 15, 2013, 3:53:48 AM2/15/13
to vim-...@googlegroups.com
Unfortunately there is not option to change the behavior of these motions similar to 'includeexpr' or 'grepprg' or that kind of stuff.
This is the description from the vim documentation: (I also searched in the list of options for something related to this and there is nothing).

(sorry for the long paste from the vim doc)

]m                      Go to [count] next start of a method (for Java or
                        similar structured language).  When not before the
                        start of a method, jump to the start or end of the
                        class.  When no '{' is found after the cursor, this is
                        an error.  exclusive motion. {not in Vi}

The above two commands assume that the file contains a class with methods.
The class definition is surrounded in '{' and '}'.  Each method in the class
is also surrounded with '{' and '}'.  This applies to the Java language.  The
file looks like this: >

        // comment
        class foo {
                int method_one() {
                        body_one();
                }
                int method_two() {
                        body_two();
                }
        }
Starting with the cursor on "body_two()", using "[m" will jump to the '{' at
the start of "method_two()" (obviously this is much more useful when the
method is long!).  Using "2[m" will jump to the start of "method_one()".
Using "3[m" will jump to the start of the class.


Here is an interesting thing. I noticed that [m and ]m works on python so I was curious (looking a the documentation ) how it works since Python doesn't have braces to enclose functions.

I checked the vim source code and here is what I found in share/vim/vim73/ftplugin/python.vim:

nnoremap <silent> <buffer> ]m :call <SID>Python_jump('/^\s*\(class\\|def\)')<cr>
nnoremap <silent> <buffer> [m :call <SID>Python_jump('?^\s*\(class\\|def\)')<cr>

They did it in a similar fashion. 

I can see they did it as a motion and their commands can jump multiple defs and it also jumps to class.

I will work on refining the regexp as it misses some cases and also to make it a motion command (I wanted to do that for a while, I just never actually did it :) ). I will post it here after that.  
If there is a desire to add it in the main distribution it's fine. If not people can add those in their .vimrc if they want to. 

Regards.
Catalin.

Catalin Ciurea

unread,
Feb 24, 2013, 5:12:09 PM2/24/13
to vim-...@googlegroups.com
I came up with something as simple as:

map <silent> ]m :call Perl_method_jump('')<CR>
map <silent> [m :call Perl_method_jump('b')<CR>

function! Perl_method_jump(type)
    let pattern =  '\v^\s*sub\s*\w+\s*(\(\s*.{-}\s*\))?\s*\{*'
    for i in range(1, v:count1)
        silent! call searchpos(pattern, a:type . 'W')
    endfor
endfunction

Comments/suggestions ?

Vim also has ]M and [M motions for jumping to the end of the next and previous method end. I am working on those.

Thanks.
Catalin

Catalin Ciurea

unread,
Mar 11, 2013, 5:22:25 PM3/11/13
to vim-...@googlegroups.com
I managed to implement all the motions:
[m - jumps to previous perl subroutine
]m - jumps to next perl subroutine
[M - jumps to previous subroutine end
]M - jumps to next subroutine end.

All the motions accept a count before it so someone can jumps more than one subroutine at a time. Although not tested enaugh they can also be used in combination with operators such as delete (d]m) or yank (y]m).

I added all the code in github at https://github.com/catalinciurea/perl-nextmethod under the form of a lightweight vim plugin.

Some things that still need to be done:
- prevent the user environment from being changed: keep the marks, jumps list and change list. (keepjumps ex cmd is a start but not enaugh)
- make them work in visual select mode (v]m): visual select until the next method.

In the end should I make a pull request ? Are these good to have in the official distribution ? 

Andy Lester

unread,
May 11, 2013, 1:31:46 PM5/11/13
to vim-...@googlegroups.com

On Mar 11, 2013, at 4:22 PM, Catalin Ciurea <catalin...@gmail.com> wrote:

> In the end should I make a pull request ? Are these good to have in the official distribution ?

Please make a pull request so that we can all have a look at it and discuss.
Reply all
Reply to author
Forward
0 new messages