Loading ...
Sorry, an error occurred while loading the content.

29398Re: % matching for Python

Expand Messages
  • Bram Moolenaar
    Aug 5, 2002
    • 0 Attachment
      Benji Fisher wrote:

      > Python users:
      >
      > I decided that matchit.vim is not the answer to everything, so I
      > wrote the attached script to do % matching based on indent in Python
      > files. You can add this to your ftplugin/ directory with its current
      > name (python_match.vim) or put it in ftplugin/python/ or :source it
      > from your ftplugin/python.vim file. It implements the following
      > motion commands:

      I'm afraid this is not really the kind of behavior I expect from "%". I
      would expect it go to over if/elif/else and try/except/finally
      constructs. While checking the indent, of course.

      I'm using mappings for [[ and ]] to move over functions and [{ and ]} to
      move to the start/end of a block. See below.

      > [% Go to the start of the current block. If currently at the start of a
      > block, move to the start of the enclosing block.
      >
      > ]% Go to the end of the current block. If currently at the start of a
      > block, move to the end of that block.

      These get stuck at an empty line. That's not where a block ends, thus I
      wouldn't want this to happen.

      > * I use indent() to get the indent of a line. I am not sure how Python treats
      > tabs and spaces; is there a better way than indent()?

      I think Python always uses a tabstop of eight, nobody should edit a
      Python file with a tabstop unequal 8 anyway. indent() matches with what
      you see on the screen, thus it should work well.


      Here are the mappings and functions I'm currently using:

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

      if exists('*<SID>Python_jump') | finish | endif

      fun! <SID>Python_jump(motion) range
      let cnt = v:count1
      let save = @/ " save last search pattern
      mark '
      while cnt > 0
      silent! exe a:motion
      let cnt = cnt - 1
      endwhile
      call histdel('/', -1)
      let @/ = save " restore last search pattern
      endfun

      nnoremap <silent> <buffer> ]} :call <SID>Python_find(1)<cr>
      nnoremap <silent> <buffer> [{ :call <SID>Python_find(-1)<cr>

      fun! <SID>Python_find(add)
      let lnum = line(".")
      let indent = indent(lnum)
      while 1
      let lnum = lnum + a:add
      if lnum < 1 || lnum > line("$")
      return
      endif
      if indent(lnum) < indent && getline(lnum) =~ '\S'
      break
      endif
      endwhile
      exe lnum
      endfun



      --
      hundred-and-one symptoms of being an internet addict:
      107. When using your phone you forget that you don't have to use your
      keyboard.

      /// Bram Moolenaar -- Bram@... -- http://www.moolenaar.net \\\
      /// Creator of Vim -- http://vim.sf.net -- ftp://ftp.vim.org/pub/vim \\\
      \\\ Project leader for A-A-P -- http://www.a-a-p.org ///
      \\\ Lord Of The Rings helps Uganda - http://iccf-holland.org/lotr.html ///
    • Show all 16 messages in this topic