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

Re: Python indentation script...

Expand Messages
  • Benjamin R. Haskell
    ... The following seems to do what I think you re describing. Put it in a file called after/syntax/python.vim, somewhere in your .vim directory in your home
    Message 1 of 6 , Aug 27, 2011
    • 0 Attachment
      On Fri, 26 Aug 2011, Burnett, Rick wrote:

      > Hello All!
      > I've been looking around to see if it is possible to create a script
      > or add a feature that I would use quite a bit.
      > When editing python code, how much a line is indented sets the scope
      > of the code, as many of you know, however, sometimes with long scopes
      > that are heavily nested, it can be confusing to easily see how many
      > indents you need.
      > What I would LOVE is to set vim similar to where it shows numbers, but
      > instead, have the number show the number of indents relative to the
      > current setting of ts.
      > What would happen is this
      > 1) The number is computed by the number of spaces at the lead
      > (^\s*) / tab space setting (ts)
      > 2) If the number had any remainder (so not zero remainder) this
      > means something isn't spaced right, set the text color to orange.
      > 3) If ANY tabs (\t) are in the line, set the color to red to
      > indicate the line needs to be fixed.
      > I realize computing every line is expensive, but I'd be willing to
      > take that hit on turning this one for some of the difficult functions
      > I write. It would also be nice if it ran on what was displayed only
      > maybe.
      > Is this possible? Or will I need to actually edit the gvim sourcecode
      > for this sort of functionality?

      The following seems to do what I think you're describing. Put it in a
      file called after/syntax/python.vim, somewhere in your .vim directory in
      your home dir. E.g., mine is at: ~/.vim/after/syntax/python.vim:

      ==> ~/.vim/after/syntax/python.vim <==
      " Match any lines with tabs as PythonTabError
      syn match PythonTabError /^.*\t.*$/

      " Set up something to match lines w/ leading spaces not divisible by 'ts'
      if &l:ts > 1
      let s:badindent = '^\( \{'.&l:ts.'\}\)* \{1,'.(&l:ts-1).'\}[^ ].*$'
      let s:badindent = ':syn match PythonSpacingError /'.s:badindent.'/'
      exe s:badindent

      " PythonSpacingErrors should be orange (assumes 256-color terminal)
      hi PythonSpacingError ctermbg=208 ctermfg=0 guifg=#ff9900

      " PythonTabErrors should be red (just show the line the same as an error)
      hi link PythonTabError Error

      An example of the 'badindent' regular expression that's generated when

      syn match PythonSpacingError /^\( \{4\}\)* \{1,3\}[^ ].*$/

      That matches:
      ^ -- start of line
      \( \{4\}\)* -- any number of correct indentations
      \{1,3\} -- followed by a number of spaces not divisible by 'ts'
      [^ ] -- followed by a non-space
      .*$ -- and the rest of the line

      For more on everything:

      :help :syn-match
      :help :highlight
      :help :highlight-link
      :help after-directory


      You received this message from the "vim_use" maillist.
      Do not top-post! Type your reply below the text you are replying to.
      For more information, visit http://www.vim.org/maillist.php
    Your message has been successfully submitted and would be delivered to recipients shortly.