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

Possible bug: gw appears to use 'formatexpr' although Vim help indicates it shouldn't

Expand Messages
  • Stahlman Family
    According to the Vim help on gw{motion}, the gw variants of the formatting commands do not use formatprg or formatexpr . I set up a simple test, however,
    Message 1 of 3 , Mar 2, 2008
    • 0 Attachment
      According to the Vim help on gw{motion}, the gw variants of the
      formatting commands do not use 'formatprg' or 'formatexpr'. I set up a
      simple test, however, which appears to show that 'formatexpr' is
      evaluated when formatting is performed with gw.

      The test is as follows...

      Put several lines of text in a test buffer.
      From the test buffer, source the following Vim script:

      set formatexpr=MyFormatExpr()
      fu! MyFormatExpr()
      call append('$', 'v:lnum='.v:lnum.' v:count='.v:count)
      return 0
      endfu

      Now, when I execute either gwgw or gqgq, the behavior is the same:
      specifically, the values of v:lnum and v:count are appended to the
      buffer and formatting is unchanged. (Note that formatting is unchanged
      because I return 0 and don't do any formatting within MyFormatExpr().)
      The point to note here is that 'formatexpr' is obviously being evaluated
      not only when I use gq and friends, but also when I use gw and gwgw.
      This appears to contradict the Vim help.

      The reason this behavior is problematic for me is that my 'formatexpr'
      function may be invoked to format a range of lines that comprises both
      "normal" ranges and ranges that require "special" formatting. I want to
      use gw to format the "normal" ranges just as they would have been
      formatted if 'formatexpr' were unset; i.e., based upon 'textwidth' alone.

      Thanks,
      Brett S.

      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    • Bram Moolenaar
      ... Yes, this doesn t work as documented. For gw formatting is split up into formatting single lines, but formatexpr is still. I ll have to think of a
      Message 2 of 3 , Mar 2, 2008
      • 0 Attachment
        Brett Stahlman wrote:

        > According to the Vim help on gw{motion}, the gw variants of the
        > formatting commands do not use 'formatprg' or 'formatexpr'. I set up a
        > simple test, however, which appears to show that 'formatexpr' is
        > evaluated when formatting is performed with gw.
        >
        > The test is as follows...
        >
        > Put several lines of text in a test buffer.
        > From the test buffer, source the following Vim script:
        >
        > set formatexpr=MyFormatExpr()
        > fu! MyFormatExpr()
        > call append('$', 'v:lnum='.v:lnum.' v:count='.v:count)
        > return 0
        > endfu
        >
        > Now, when I execute either gwgw or gqgq, the behavior is the same:
        > specifically, the values of v:lnum and v:count are appended to the
        > buffer and formatting is unchanged. (Note that formatting is unchanged
        > because I return 0 and don't do any formatting within MyFormatExpr().)
        > The point to note here is that 'formatexpr' is obviously being evaluated
        > not only when I use gq and friends, but also when I use gw and gwgw.
        > This appears to contradict the Vim help.
        >
        > The reason this behavior is problematic for me is that my 'formatexpr'
        > function may be invoked to format a range of lines that comprises both
        > "normal" ranges and ranges that require "special" formatting. I want to
        > use gw to format the "normal" ranges just as they would have been
        > formatted if 'formatexpr' were unset; i.e., based upon 'textwidth' alone.

        Yes, this doesn't work as documented. For "gw" formatting is split up
        into formatting single lines, but 'formatexpr' is still. I'll have to
        think of a consistent solution that is still backwards compatible.

        Note that not using 'formatexpr' for "gw" was not really intentional,
        but because it's nearly impossible to keep the cursor in the same place
        when the formatting function may do anything.

        --
        hundred-and-one symptoms of being an internet addict:
        84. Books in your bookcase bear the names Bongo, WinSock and Inside OLE

        /// Bram Moolenaar -- Bram@... -- http://www.Moolenaar.net \\\
        /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
        \\\ download, build and distribute -- http://www.A-A-P.org ///
        \\\ help me help AIDS victims -- http://ICCF-Holland.org ///

        --~--~---------~--~----~------------~-------~--~----~
        You received this message from the "vim_use" maillist.
        For more information, visit http://www.vim.org/maillist.php
        -~----------~----~----~----~------~----~------~--~---
      • Stahlman Family
        ... Ok. Interestingly, when I use gw from within the formatexpr function, things seem to work as expected. Does this mean that a gw executed from within the
        Message 3 of 3 , Mar 2, 2008
        • 0 Attachment
          Bram Moolenaar wrote:
          > Brett Stahlman wrote:
          >
          >> According to the Vim help on gw{motion}, the gw variants of the
          >> formatting commands do not use 'formatprg' or 'formatexpr'. I set up a
          >> simple test, however, which appears to show that 'formatexpr' is
          >> evaluated when formatting is performed with gw.
          >>
          >> The test is as follows...
          >>
          >> Put several lines of text in a test buffer.
          >> From the test buffer, source the following Vim script:
          >>
          >> set formatexpr=MyFormatExpr()
          >> fu! MyFormatExpr()
          >> call append('$', 'v:lnum='.v:lnum.' v:count='.v:count)
          >> return 0
          >> endfu
          >>
          >> Now, when I execute either gwgw or gqgq, the behavior is the same:
          >> specifically, the values of v:lnum and v:count are appended to the
          >> buffer and formatting is unchanged. (Note that formatting is unchanged
          >> because I return 0 and don't do any formatting within MyFormatExpr().)
          >> The point to note here is that 'formatexpr' is obviously being evaluated
          >> not only when I use gq and friends, but also when I use gw and gwgw.
          >> This appears to contradict the Vim help.
          >>
          >> The reason this behavior is problematic for me is that my 'formatexpr'
          >> function may be invoked to format a range of lines that comprises both
          >> "normal" ranges and ranges that require "special" formatting. I want to
          >> use gw to format the "normal" ranges just as they would have been
          >> formatted if 'formatexpr' were unset; i.e., based upon 'textwidth' alone.
          >
          > Yes, this doesn't work as documented. For "gw" formatting is split up
          > into formatting single lines, but 'formatexpr' is still. I'll have to
          > think of a consistent solution that is still backwards compatible.
          >
          > Note that not using 'formatexpr' for "gw" was not really intentional,
          > but because it's nearly impossible to keep the cursor in the same place
          > when the formatting function may do anything.
          >

          Ok. Interestingly, when I use gw from within the 'formatexpr' function,
          things seem to work as expected. Does this mean that a gw executed from
          within the 'formatexpr' function will not trigger an evaluation of
          'formatexpr'? (I guess it couldn't, else I would have infinite
          recursion.) Going back to the case I mentioned above of "normal" and
          "special" regions, is there currently a "correct" way to accomplish
          that? e.g., a command that says "break the following range of lines
          according to 'textwidth' only"? I thought of unsetting 'formatexpr'
          temporarily within the 'formatexpr' function, just prior to the gwgw,
          but perhaps that's not even necessary, given that 'formatexpr' doesn't
          appear to be used recursively.

          Thanks,
          Brett S.


          --~--~---------~--~----~------------~-------~--~----~
          You received this message from the "vim_use" maillist.
          For more information, visit http://www.vim.org/maillist.php
          -~----------~----~----~----~------~----~------~--~---
        Your message has been successfully submitted and would be delivered to recipients shortly.