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

Re: Simple hiding all comments by folding

Expand Messages
  • A.Politz
    ... Ok, here is a working and faster version. ... set fdm=expr set fde=FoldComments(v:lnum) func! FoldComments( lnum ) if getline( . ) =~ S return
    Message 1 of 17 , Jan 1, 2008
    • 0 Attachment
      A.Politz wrote:

      >A.Politz wrote:
      >
      >
      >
      >>arctgx wrote:
      >>
      >>
      >>
      >>
      >>
      >>>Is there a simple universal command which hides all comments in file
      >>>by current syntax? I would like, for example hide comments in my shell
      >>>scripts (syntax=sh) or xorg.conf (syntax=xf86conf). There is a lot
      >>>text about folding but I still can't find a good solution.
      >>>
      >>>I tried
      >>>set foldmarker=#,$ | set foldmethod=marker
      >>>
      >>>but it is not universal solution and... hides all lines of shell
      >>>script ;) ($ doesn't denote end of line?)
      >>>
      >>>
      >>>
      >>>
      >>>
      >>>
      >>>
      >>>
      >>The following will fold commented lines,including pre- and succeeding
      >>empty ones. All it needs is a correct syntax plugin.
      >>This can be slow, as noted at ':h synID'. It would maybe be faster to
      >>use 'comment*' options, but then it wouldn't be simple anymore ;-).
      >>
      >>...
      >>
      >
      >

      Ok, here is a working and faster version.


      -----------%<--------------
      set fdm=expr
      set fde=FoldComments(v:lnum)

      func! FoldComments( lnum )
      if getline('.') =~ '\S'
      return IsComment(a:lnum)
      else
      return IsComment(prevnonblank(a:lnum)) ||
      IsComment(nextnonblank(a:lnum))
      endif
      endfun

      func! IsComment( lnum )
      return synIDattr(synID(a:lnum, match(getline(a:lnum),'\S')+1, 1),
      "name") =~? 'comment'
      endfun
      -----------%<--------------

      -ap


      --
      Ich hab geträumt, der Krieg wär vorbei.


      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    • arctgx
      ...after this discussion it seems that folding all comments isn t so basic operation and requires some special work. Thank you all very much for rich answer.
      Message 2 of 17 , Jan 1, 2008
      • 0 Attachment
        ...after this discussion it seems that folding all comments isn't so
        basic operation and requires some special work.

        Thank you all very much for rich answer. I need some time to test it
        (and much time to understand those tips in though partly detail). I
        would like to describe effects of those tips there.

        best regards
        Tomasz
        --~--~---------~--~----~------------~-------~--~----~
        You received this message from the "vim_use" maillist.
        For more information, visit http://www.vim.org/maillist.php
        -~----------~----~----~----~------~----~------~--~---
      • arctgx
        I wouldn t like to insert into file any strings ({{{, token etc.) that are deserve only to define fold range, especially as I open some files only for
        Message 3 of 17 , Jan 6, 2008
        • 0 Attachment
          I wouldn't like to insert into file any strings ({{{, 'token' etc.)
          that are deserve only to define fold range, especially as I open some
          files only for reading.

          Vince Negri's patch doesn't exists for 7.1 vim version. I tried to
          patch 7.1 but patching exited with fails. Charles, does your commands
          work only with this patch (E492: Not an editor command:
          cmmnt_toggle=1<bar>hi link shComment Ign)?

          A.Politz, I tested your functions on following text:

          *************************************
          # vim syntax=sh :
          # c1cccccccc
          # c2cccccccc
          nnn1nnnnnnnn
          nnn2nnnnnnnn
          nnn3nnnnnnnn
          # c3cccccccc
          # c4cccccccc
          # c5cccccccc
          # c6cccccccc
          nnn4nnnnnnnn
          nnn5nnnnnnnn
          nnn6nnnnnnnn
          # c7cccccccc
          # c8cccccccc
          # c9cccccccc
          nnn7nnnnnnnn
          # c10ccccccc
          nnn8nnnnnnnn
          # c11ccccccc
          nnn9nnnnnnnn
          *****************************************************


          Loading source with
          ******************
          set fdm=expr
          set fde=IsComment(v:lnum)?1:IsComment(prevnonblank(v:lnum-1))?
          1:IsComment(nextnonblank(v:lnum+1))?1:0
          func! FoldComments( lnum )
          if getline('.') =~ '\S'
          return IsComment(a:lnum)
          else
          return IsComment(prevnonblank(a:lnum)) ||
          IsComment(nextnonblank(a:lnum))
          endif
          endfun

          func! IsComment( lnum )
          return synIDattr(synID(a:lnum, match(getline(a:lnum),'\S')+1,
          1),"name") =~? 'comment'
          endfun

          ******************
          gives
          ******************
          +-- 4 lines: #
          c1cccccccc-----------------------------------------------------------------------------------
          nnn2nnnnnnnn
          +-- 6 lines:
          nnn3nnnnnnnn-----------------------------------------------------------------------------------
          nnn5nnnnnnnn
          +-- 9 lines:
          nnn6nnnnnnnn-----------------------------------------------------------------------------------
          ******************



          Loading source with
          ******************
          gives
          ******************
          +-- 4 lines: #
          c1cccccccc-----------------------------------------------------------------------------------
          nnn2nnnnnnnn
          +-- 6 lines:
          nnn3nnnnnnnn-----------------------------------------------------------------------------------
          nnn5nnnnnnnn
          +-- 9 lines:
          nnn6nnnnnnnn-----------------------------------------------------------------------------------
          ******************

          ******************
          set fdm=expr
          set fde=FoldComments(v:lnum)
          func! FoldComments( lnum )
          if getline('.') =~ '\S'
          return IsComment(a:lnum)
          else
          return IsComment(prevnonblank(a:lnum)) ||
          IsComment(nextnonblank(a:lnum))
          endif
          endfun

          func! IsComment( lnum )
          return synIDattr(synID(a:lnum, match(getline(a:lnum),'\S')+1,
          1),"name") =~? 'comment'
          endfun
          ******************
          gives
          ******************
          +-- 3 lines: #
          c1cccccccc-----------------------------------------------------------------------------------
          nnn1nnnnnnnn
          nnn2nnnnnnnn
          nnn3nnnnnnnn
          +-- 4 lines: #
          c3cccccccc-----------------------------------------------------------------------------------
          nnn4nnnnnnnn
          nnn5nnnnnnnn
          nnn6nnnnnnnn
          +-- 3 lines: #
          c7cccccccc-----------------------------------------------------------------------------------
          nnn7nnnnnnnn
          # c10ccccccc
          nnn8nnnnnnnn
          # c11ccccccc
          nnn9nnnnnnnn
          ******************
          but in practice those solutions may sometimes work fine after loading
          them into vim :)
          --~--~---------~--~----~------------~-------~--~----~
          You received this message from the "vim_use" maillist.
          For more information, visit http://www.vim.org/maillist.php
          -~----------~----~----~----~------~----~------~--~---
        • arctgx
          Warning: there isn t any new line char in source between e.g. +-- 4 lines: # and c3cccccccc----------------------------------... . They became in output
          Message 4 of 17 , Jan 6, 2008
          • 0 Attachment
            Warning: there isn't any new line char in source between e.g. "+-- 4
            lines: # " and "c3cccccccc----------------------------------...".
            They became in output text after sending.
            --~--~---------~--~----~------------~-------~--~----~
            You received this message from the "vim_use" maillist.
            For more information, visit http://www.vim.org/maillist.php
            -~----------~----~----~----~------~----~------~--~---
          • A.Politz
            ... The 1st and 3rd versions work, the 2nd doesn t, as I said. Your 1st version is my 2nd, which don t work and your 2nd, which is my 3rd does work, but maybe
            Message 5 of 17 , Jan 6, 2008
            • 0 Attachment
              arctgx wrote:

              > I wouldn't like to insert into file any strings ({{{, 'token' etc.)
              >that are deserve only to define fold range, especially as I open some
              >files only for reading.
              >
              >Vince Negri's patch doesn't exists for 7.1 vim version. I tried to
              >patch 7.1 but patching exited with fails. Charles, does your commands
              >work only with this patch (E492: Not an editor command:
              >cmmnt_toggle=1<bar>hi link shComment Ign)?
              >
              >A.Politz, I tested your functions on following text:
              >
              >...
              >
              >******************
              > set fdm=expr
              > set fde=FoldComments(v:lnum)
              > func! FoldComments( lnum )
              > if getline('.') =~ '\S'
              > return IsComment(a:lnum)
              > else
              > return IsComment(prevnonblank(a:lnum)) ||
              >IsComment(nextnonblank(a:lnum))
              > endif
              > endfun
              >
              > func! IsComment( lnum )
              > return synIDattr(synID(a:lnum, match(getline(a:lnum),'\S')+1,
              >1),"name") =~? 'comment'
              > endfun
              >******************
              >gives
              >******************
              >+-- 3 lines: #
              >c1cccccccc-----------------------------------------------------------------------------------
              >nnn1nnnnnnnn
              >nnn2nnnnnnnn
              >nnn3nnnnnnnn
              >+-- 4 lines: #
              >c3cccccccc-----------------------------------------------------------------------------------
              >nnn4nnnnnnnn
              >nnn5nnnnnnnn
              >nnn6nnnnnnnn
              >+-- 3 lines: #
              >c7cccccccc-----------------------------------------------------------------------------------
              >nnn7nnnnnnnn
              ># c10ccccccc
              >nnn8nnnnnnnn
              ># c11ccccccc
              >nnn9nnnnnnnn
              >******************
              >but in practice those solutions may sometimes work fine after loading
              >them into vim :)
              >
              >
              The 1st and 3rd versions work, the 2nd doesn't, as I said.
              Your 1st version is my 2nd, which don't work and your 2nd,
              which is my 3rd does work, but maybe I should haved added
              set foldminlines=0
              for the nonbeliever.Heh.

              -ap

              --
              Ich hab geträumt, der Krieg wär vorbei.


              --~--~---------~--~----~------------~-------~--~----~
              You received this message from the "vim_use" maillist.
              For more information, visit http://www.vim.org/maillist.php
              -~----------~----~----~----~------~----~------~--~---
            • arctgx
              +-- 3 wierszy: # vim syntax=sh :------------------------------------------------------------------------------- nnn1nnnnnnnn nnn2nnnnnnnn nnn3nnnnnnnn +-- 4
              Message 6 of 17 , Jan 6, 2008
              • 0 Attachment
                +-- 3 wierszy: # vim
                syntax=sh :-------------------------------------------------------------------------------
                nnn1nnnnnnnn
                nnn2nnnnnnnn
                nnn3nnnnnnnn
                +-- 4 wierszy: #
                c3cccccccc------------------------------------------------------------------------------------
                nnn4nnnnnnnn
                nnn5nnnnnnnn
                nnn6nnnnnnnn
                +-- 3 wierszy: #
                c7cccccccc------------------------------------------------------------------------------------
                nnn7nnnnnnnn
                +-- 1 wierszy: #
                c10ccccccc------------------------------------------------------------------------------------
                nnn8nnnnnnnn
                +-- 1 wierszy: #
                c11ccccccc------------------------------------------------------------------------------------
                nnn9nnnnnnnn

                Indeed, your third version with foldminlines=0 works :) The script
                seems to work fine on other files also - not only on shell scripts. I
                shall put in spare time your version on my Polish website (
                http://www-users.mat.uni.torun.pl/~arctgx/lin/vim.html ) with several
                tips about Vim, and not forgot to add mention about your autorship, of
                course, and link to this thread as source.

                Thx once again
                Tomasz
                --~--~---------~--~----~------------~-------~--~----~
                You received this message from the "vim_use" maillist.
                For more information, visit http://www.vim.org/maillist.php
                -~----------~----~----~----~------~----~------~--~---
              • Ben Schmidt
                ... If you have time, I suspect the community would appreciate it if you added it to the official Vim Tips wiki: http://vim.wikia.com/wiki/Main_Page Ben. Send
                Message 7 of 17 , Jan 6, 2008
                • 0 Attachment
                  > Indeed, your third version with foldminlines=0 works :) The script
                  > seems to work fine on other files also - not only on shell scripts. I
                  > shall put in spare time your version on my Polish website (
                  > http://www-users.mat.uni.torun.pl/~arctgx/lin/vim.html ) with several
                  > tips about Vim, and not forgot to add mention about your autorship, of
                  > course, and link to this thread as source.

                  If you have time, I suspect the community would appreciate it if you added it to
                  the official Vim Tips wiki:

                  http://vim.wikia.com/wiki/Main_Page

                  Ben.




                  Send instant messages to your online friends http://au.messenger.yahoo.com


                  --~--~---------~--~----~------------~-------~--~----~
                  You received this message from the "vim_use" maillist.
                  For more information, visit http://www.vim.org/maillist.php
                  -~----------~----~----~----~------~----~------~--~---
                • Charles E. Campbell, Jr.
                  ... I did say that the mapping was untested! Which means some debugging may be involved. OK, I ve gone and done it: map :if
                  Message 8 of 17 , Jan 6, 2008
                  • 0 Attachment
                    arctgx wrote:

                    > I wouldn't like to insert into file any strings ({{{, 'token' etc.)
                    >that are deserve only to define fold range, especially as I open some
                    >files only for reading.
                    >
                    >Vince Negri's patch doesn't exists for 7.1 vim version. I tried to
                    >patch 7.1 but patching exited with fails. Charles, does your commands
                    >work only with this patch (E492: Not an editor command:
                    >cmmnt_toggle=1<bar>hi link shComment Ign)?
                    >
                    >
                    >
                    I did say that the mapping was untested! Which means some debugging may
                    be involved. OK, I've gone and done it:

                    map <silent> <f1> :if !exists("g:cmmnt_toggle")<bar><bar>g:cmmnt_toggle
                    == 0<cr>:let g:cmmnt_toggle= 1<cr>hi link shComment
                    Ignore<cr>else<bar>let g:cmmnt_toggle= 0<bar>hi link shComment
                    Comment<cr>endif<cr>

                    (all one line) This mapping toggles shComment between linking to
                    Comment and Ignore highlighting. It doesn't fold the lines; instead, it
                    "Ignore"s them. Of course, if your colorscheme is obnoxious and makes
                    Ignore visible, it won't help, but I don't think that's common.

                    Regards,
                    Chip Campbell



                    --~--~---------~--~----~------------~-------~--~----~
                    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.