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

Re: Simple hiding all comments by folding

Expand Messages
  • A.Politz
    ... fde=IsComment(v:lnum)?1:IsComment(prevnonblank(v:lnum-1))?1:IsComment(nextnonblank(v:lnum+1))?1:0 ... Added [+-]1 to *nonblank , which is what I
    Message 1 of 17 , Jan 1, 2008
    View Source
    • 0 Attachment
      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 ;-).
      >
      >-----------------%<-----------------
      >"(no linebreaks intended)
      >set fdm=expr
      >set
      >
      >
      fde=IsComment(v:lnum)?1:IsComment(prevnonblank(v:lnum-1))?1:IsComment(nextnonblank(v:lnum+1))?1:0


      >func! IsComment( lnum )
      > return synIDattr(synID(a:lnum, match(getline(a:lnum),'\S')+1, 1),
      >"name") =~? 'comment'
      >endfun
      >-----------------%<-----------------
      >
      >-ap
      >
      >
      >
      >
      Added '[+-]1' to '*nonblank', which is what I actually wanted to
      do in the first place.

      -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
      -~----------~----~----~----~------~----~------~--~---
    • Martin Lange
      ... bashism ahead! Protect the innocent! You can fake a multiline comment in bash-scripts. Use a here script , which is defined as command
      Message 2 of 17 , Jan 1, 2008
      View Source
      • 0 Attachment
        At Tuesday 01 January 2008 05:00:05, Ben Schmidt wrote:

        > You could conceivably write or modify syntax files to do it and use
        > foldmethod=syntax, but there'd be plenty of work involved,
        > particularly since, for the most part, 'long' comments in shell
        > scripts and such are actually not long comments, but lots of short
        > comments one after the other. (...)

        bashism ahead! Protect the innocent!

        You can fake a multiline comment in bash-scripts. Use a "here script",
        which is defined as

        command << token
        content to be used as command's standard input
        token

        For "command" use the colon:

        $ help :
        :: :
        No effect; the command does nothing. A zero exit code is returned.

        For "token" invent something nice, which didn't have sideeffects.
        (Unfortunally, the "standard" 3 curly brackets have.) Add that "token"
        to the syntax file and proceed with "foldmethod=syntax".

        The idea is stolen from the 'Net, but I didn't remember the URL. So,
        credits to whoever it was.

        HTH.

        Martin "vi"
      • Ben Schmidt
        ... LOL. What fun. Of course, the downside of most of the approaches everyone has mentioned is that they need the user to do work while writing the file. Nicer
        Message 3 of 17 , Jan 1, 2008
        View Source
        • 0 Attachment
          Martin Lange wrote:
          > At Tuesday 01 January 2008 05:00:05, Ben Schmidt wrote:
          >
          >> You could conceivably write or modify syntax files to do it and use
          >> foldmethod=syntax, but there'd be plenty of work involved,
          >> particularly since, for the most part, 'long' comments in shell
          >> scripts and such are actually not long comments, but lots of short
          >> comments one after the other. (...)
          >
          > bashism ahead! Protect the innocent!
          >
          > You can fake a multiline comment in bash-scripts. Use a "here script",
          > which is defined as
          >
          > command << token
          > content to be used as command's standard input
          > token
          >
          > For "command" use the colon:
          >
          > $ help :
          > :: :
          > No effect; the command does nothing. A zero exit code is returned.
          >
          > For "token" invent something nice, which didn't have sideeffects.
          > (Unfortunally, the "standard" 3 curly brackets have.) Add that "token"
          > to the syntax file and proceed with "foldmethod=syntax".
          >
          > The idea is stolen from the 'Net, but I didn't remember the URL. So,
          > credits to whoever it was.
          >
          > HTH.
          >
          > Martin "vi"

          LOL.

          What fun.

          Of course, the downside of most of the approaches everyone has mentioned is that
          they need the user to do work while writing the file. Nicer would be if we could
          just open anybody's file and have the functionality there. I think A. Politz'
          approach is closest so far--a good bit of lateral thinking. Having only had a
          quick look, I think it could probably do with refinement, and expect that would be
          slow too, but still, with a bit of work...

          Grins,

          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
          -~----------~----~----~----~------~----~------~--~---
        • A.Politz
          ... I just noticed, that this is wrong and the first solution is correct. I was to busy looking at the performance. -ap -- Ich hab geträumt, der Krieg wär
          Message 4 of 17 , Jan 1, 2008
          View Source
          • 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 ;-).
            >>
            >>-----------------%<-----------------
            >>"(no linebreaks intended)
            >>set fdm=expr
            >>set
            >>
            >>
            >>
            >>
            >fde=IsComment(v:lnum)?1:IsComment(prevnonblank(v:lnum-1))?1:IsComment(nextnonblank(v:lnum+1))?1:0
            >
            >
            >
            >
            >>func! IsComment( lnum )
            >> return synIDattr(synID(a:lnum, match(getline(a:lnum),'\S')+1, 1),
            >>"name") =~? 'comment'
            >>endfun
            >>-----------------%<-----------------
            >>
            >>-ap
            >>
            >>
            >>
            >>
            >>
            >>
            >Added '[+-]1' to '*nonblank', which is what I actually wanted to
            >do in the first place.
            >
            >-ap
            >
            >
            >
            I just noticed, that this is wrong and the first solution is correct.
            I was to busy looking at the performance.

            -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
            -~----------~----~----~----~------~----~------~--~---
          • 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 5 of 17 , Jan 1, 2008
            View Source
            • 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 6 of 17 , Jan 1, 2008
              View Source
              • 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 7 of 17 , Jan 6, 2008
                View Source
                • 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 8 of 17 , Jan 6, 2008
                  View Source
                  • 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 9 of 17 , Jan 6, 2008
                    View Source
                    • 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 10 of 17 , Jan 6, 2008
                      View Source
                      • 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 11 of 17 , Jan 6, 2008
                        View Source
                        • 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 12 of 17 , Jan 6, 2008
                          View Source
                          • 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.