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

Feature request: Indent level objects

Expand Messages
  • Yegappan Lakshmanan
    Hi all, I saw a feature request on the comp.editors newsgroup for adding indent level objects to Vim. I think this will be useful. I have the attached a Vim
    Message 1 of 4 , Oct 2, 2002
    • 0 Attachment
      Hi all,

      I saw a feature request on the comp.editors newsgroup for adding
      indent level objects to Vim. I think this will be useful.

      I have the attached a Vim plugin (somebody posted this plugin to
      the vim mailing list sometime back) that allows you to jump
      between different indents. I think this feature should be part
      of Vim. This can be used across different language source files
      and other files.

      The tip for this plugin is at:

      http://vim.sourceforge.net/tip_view.php?tip_id=112

      - Yegappan

      -----------------------------------------------------------------------------
      "
      " next_indent.vim
      "
      " Vim 6.0 plugin script to jump to the previous/next indent.
      "
      " Use the following keys:
      "
      " [l - Jump to last line with the same indent as the current line
      " ]l - Jump to next line with the same indent as the current line
      " [L - Jump to last line with the indent less than that of the current line
      " ]L - Jump to next line with the indent less than that of the current line
      "
      " The above keys work in normal mode, visual mode and operator pending mode
      "

      if exists("loaded_next_indent")
      finish
      endif
      let loaded_next_indent = 1

      " --------------------- Do not edit after this line ------------------------

      "
      " NextIndent()
      "
      " Jump to the next or previous line that has the same level or a lower
      " level of indentation than the current line.
      "
      " exclusive (bool): true: Motion is exclusive
      " false: Motion is inclusive
      " fwd (bool): true: Go to next line
      " false: Go to previous line
      " lowerlevel (bool): true: Go to line with lower indentation level
      " false: Go to line with the same indentation level
      " skipblanks (bool): true: Skip blank lines
      " false: Don't skip blank lines
      function! s:NextIndent(exclusive, fwd, lowerlevel, skipblanks)
      let line = line('.')
      let lastline = line('$')
      let indent = indent(line)
      let stepvalue = a:fwd ? 1 : -1

      while (line > 0 && line < lastline)
      let line = line + stepvalue
      if (! a:lowerlevel && indent(line) == indent ||
      \ a:lowerlevel && indent(line) < indent)
      if (! a:skipblanks || strlen(getline(line)) > 0)
      if (a:exclusive)
      let line = line - stepvalue
      endif
      exe line
      return
      endif
      endif
      endwhile
      endfunc

      " Normal mode key mappings
      nnoremap <silent> <unique> [l :call <SID>NextIndent(0, 0, 0, 1)<cr>
      nnoremap <silent> <unique> ]l :call <SID>NextIndent(0, 1, 0, 1)<cr>
      nnoremap <silent> <unique> [L :call <SID>NextIndent(0, 0, 1, 1)<cr>
      nnoremap <silent> <unique> ]L :call <SID>NextIndent(0, 1, 1, 1)<cr>

      " Visual mode key mappings
      vnoremap <silent> <unique> [l <esc>:call <SID>NextIndent(0, 0, 0, 1)<cr>m'gv''
      vnoremap <silent> <unique> ]l <esc>:call <SID>NextIndent(0, 1, 0, 1)<cr>m'gv''
      vnoremap <silent> <unique> [L <esc>:call <SID>NextIndent(0, 0, 1, 1)<cr>m'gv''
      vnoremap <silent> <unique> ]L <esc>:call <SID>NextIndent(0, 1, 1, 1)<cr>m'gv''

      " Operator pending mode key mappings
      onoremap <silent> <unique> [l :call <SID>NextIndent(1, 0, 0, 1)<cr>
      onoremap <silent> <unique> ]l :call <SID>NextIndent(1, 1, 0, 1)<cr>
      onoremap <silent> <unique> [L :call <SID>NextIndent(1, 0, 1, 1)<cr>
      onoremap <silent> <unique> ]L :call <SID>NextIndent(1, 1, 1, 1)<cr>



      __________________________________________________
      Do you Yahoo!?
      New DSL Internet Access from SBC & Yahoo!
      http://sbc.yahoo.com
    • Bram Moolenaar
      ... I have been trying out a number of similar commands for Python. I ll include them below. Also, Benji Fisher wrote something for this. It s mostly for
      Message 2 of 4 , Oct 3, 2002
      • 0 Attachment
        Yegappan Lakshmanan wrote:

        > I saw a feature request on the comp.editors newsgroup for adding
        > indent level objects to Vim. I think this will be useful.
        >
        > I have the attached a Vim plugin (somebody posted this plugin to
        > the vim mailing list sometime back) that allows you to jump
        > between different indents. I think this feature should be part
        > of Vim. This can be used across different language source files
        > and other files.

        I have been trying out a number of similar commands for Python. I'll
        include them below. Also, Benji Fisher wrote something for this. It's
        mostly for using "%", but also other things. I don't know where he made
        it available.

        When comments are given I can decide what should end up in the Python
        filetype plugin.


        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:
        116. You are living with your boyfriend who networks your respective
        computers so you can sit in separate rooms and email each other

        /// Bram Moolenaar -- Bram@... -- http://www.moolenaar.net \\\
        /// Creator of Vim - Vi IMproved -- http://www.vim.org \\\
        \\\ Project leader for A-A-P -- http://www.a-a-p.org ///
        \\\ Lord Of The Rings helps Uganda - http://iccf-holland.org/lotr.html ///
      • Yegappan Lakshmanan
        ... If this support is available only in the python filetype plugin, then this cannot be used while editing other files. For example, when you are editing a C
        Message 3 of 4 , Oct 3, 2002
        • 0 Attachment
          --- Bram Moolenaar <Bram@...> wrote:
          >
          > Yegappan Lakshmanan wrote:
          >
          > > I saw a feature request on the comp.editors newsgroup for adding
          > > indent level objects to Vim. I think this will be useful.
          > >
          > > I have the attached a Vim plugin (somebody posted this plugin to
          > > the vim mailing list sometime back) that allows you to jump
          > > between different indents. I think this feature should be part
          > > of Vim. This can be used across different language source files
          > > and other files.
          >
          > I have been trying out a number of similar commands for Python. I'll
          > include them below. Also, Benji Fisher wrote something for this. It's
          > mostly for using "%", but also other things. I don't know where he made
          > it available.
          >
          > When comments are given I can decide what should end up in the Python
          > filetype plugin.

          If this support is available only in the python filetype plugin, then
          this cannot be used while editing other files. For example, when you
          are editing a C file, you can use this feature to jump from one case
          statement to the start of next case statement.

          - Yegappan

          >
          >
          > 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:
          > 116. You are living with your boyfriend who networks your respective
          > computers so you can sit in separate rooms and email each other
          >
          > /// Bram Moolenaar -- Bram@... -- http://www.moolenaar.net \\\
          > /// Creator of Vim - Vi IMproved -- http://www.vim.org \\\
          > \\\ Project leader for A-A-P -- http://www.a-a-p.org ///
          > \\\ Lord Of The Rings helps Uganda - http://iccf-holland.org/lotr.html ///


          __________________________________________________
          Do you Yahoo!?
          New DSL Internet Access from SBC & Yahoo!
          http://sbc.yahoo.com
        • Benji Fisher
          ... http://vim.sourceforge.net/script.php?script_id=386 The script, python_match.vim, defines % and g% to cycle through if/elif/else and try/except/catch
          Message 4 of 4 , Oct 3, 2002
          • 0 Attachment
            On Thursday, October 3, 2002, at 02:06 PM, Bram Moolenaar wrote:
            >
            > I have been trying out a number of similar commands for Python. I'll
            > include them below. Also, Benji Fisher wrote something for this. It's
            > mostly for using "%", but also other things. I don't know where he made
            > it available.

            http://vim.sourceforge.net/script.php?script_id=386

            The script, python_match.vim, defines % and g% to cycle through
            if/elif/else and try/except/catch structures in either direction. It
            also defines [% and ]% to move to the start or end of a block. I think
            that % and g% are pretty Python-specific, but the other two may be
            useful in other languages as well.

            All of these should work in Normal, Visual, and Operator-pending
            modes.

            --Benji Fisher
          Your message has been successfully submitted and would be delivered to recipients shortly.