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

Re: Fixing the year in a copyright line

Expand Messages
  • Martin Metz
    Hello, I m also very glad of the below given code and use it with an autocommand when writing a buffer. But one detail is ugly: Because of the search() the
    Message 1 of 13 , Jan 3, 2002
    • 0 Attachment
      Hello,

      I'm also very glad of the below given code and use it with an
      autocommand when writing a buffer. But one detail is ugly:

      Because of the "search()" the cursor position changes. That's not
      important if I close the VIM. But if I just save the current buffer e.
      g. with ":w" I loose the current position and stay at the copyright
      message. :-(

      Does any one have a good idea to jump back to the old position? Maybe
      with marks? I'm currently to stupid to find a solution.

      Gary Holloway wrote:
      >
      > I liked the concept of this, so I took Benji's answer as a start and modified
      > it a bit (see below).
      >
      [...]
      >
      > " Update copyright message (if any) with current year, if not present.
      >
      > function! UpdateCopyright()
      > if filewritable (expand ("%"))
      > let current_year = strftime("%Y")
      >
      > if search('Copyright *(c) Cast & Crew.*\<\d\{4}\(-\d\{4}\)\=\>', "w") > 0
      > let copyright_line = getline(".")
      > let docyear = matchstr(copyright_line, '\<\d\{4}-\d\{4}\>')
      >
      > if docyear == ""
      > let docyear = matchstr(getline("."), '\<\d\{4}\>')
      > if docyear != "" && docyear < current_year
      > " Only one year listed; and it's old; append current year.
      > execute 's/\<\(\d\{4}\)\>/\1-' . current_year
      > endif
      > else
      > " Two years listed; extract second one.
      > let docyear = matchstr(docyear, '\d\{4}$')
      > if docyear < current_year
      > " Second year is old; make it the current year.
      > execute 's/\<\(\d\{4}\)-\d\{4}\>/\1-' . current_year
      > endif
      > endif
      >
      > if getline(".") != copyright_line
      > se cmdheight+=5
      > echohl Warningmsg
      > echo 'OLD:'
      > echohl Comment
      > echo copyright_line
      > echohl Warningmsg
      > echo 'NEW:'
      > echohl Comment
      > echo getline(".")
      > echohl Question
      > let foo = input("Updated copyright (press RETURN)")
      > echohl NONE
      > se cmdheight-=5
      > endif
      > endif
      > endif
      >
      > endfunction

      --
      Gruesse,

      Martin Metz

      /***************************************************************
      * Dipl. Inform. (FH) Martin Metz - Software engineering
      *
      * OneVision Software AG | Phone: +49-941-780040
      * Dr.-Leo-Ritter-Str. 9 | FAX: +49-941-7800449
      * 93049 Regensburg | mailto:Martin.Metz@...
      * Germany | WWW: http://www.OneVision.de
      **************************************************************/
    • Benji Fisher
      ... [snip] To do it with marks, see ... If you are doing it from inside a script, and want to avoid using marks (which are global) then look at the Mark()
      Message 2 of 13 , Jan 3, 2002
      • 0 Attachment
        Martin Metz wrote:
        >
        > Hello,
        >
        > I'm also very glad of the below given code and use it with an
        > autocommand when writing a buffer. But one detail is ugly:
        >
        > Because of the "search()" the cursor position changes. That's not
        > important if I close the VIM. But if I just save the current buffer e.
        > g. with ":w" I loose the current position and stay at the copyright
        > message. :-(
        >
        > Does any one have a good idea to jump back to the old position? Maybe
        > with marks? I'm currently to stupid to find a solution.
        [snip]

        To do it with marks, see

        :help restore-position

        If you are doing it from inside a script, and want to avoid using marks (which
        are global) then look at the Mark() function in foo.vim:

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

        HTH --Benji Fisher
      • Gary Holloway
        Here s a more recent version... it utilizes the preview window to avoid that, and leaves the copyright line visible in the preview window if it was changed;
        Message 3 of 13 , Jan 3, 2002
        • 0 Attachment
          Here's a more recent version... it utilizes the preview window to avoid that,
          and leaves the copyright line visible in the preview window if it was changed;
          and fixed several "oddities" (ok, bugs :).

          I edited out some company-specifics; I don't think I created any bug doing
          that... :)

          -gary

          -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-

          " Update copyright message (if any) with current year, if not present.
          "
          " EXAMPLE USAGE:
          " au BufWrite * call UpdateCopyright()

          " $Header: /usr/home/common/vim/autoload/RCS/UpdateCopyright.vim,v 1.5 2002/01/03 02:17:25 gary Exp $

          function! UpdateCopyright()
          if !&modified || !filewritable(expand("%"))
          return
          endif

          if version >= 600
          " Using the preview window, we don't mess up the current
          " session's jump stack with the search, and it's nice to leave
          " the window open showing the change, if any.
          "
          silent! pedit %
          wincmd p
          if !&previewwindow
          " Should never happen; oh well, just do it in the normal
          " window.
          wincmd p
          pclose
          endif
          endif

          if !&previewwindow
          " Save the current cursor position. This could be done with marks,
          " but we don't like to do that because we might overwrite a user's
          " mark.
          "
          " NOTE: We only do it this way if we don't have a preview window.
          " That way, we will normally avoid moving with 'H' so we don't mess
          " up the jump history.
          "
          let current_line = line(".")
          let current_col = virtcol(".")
          normal H
          let top_line = line(".")
          endif

          let found_copyright = 0

          if search('Copyright *(c).*\<\d\{4}\(\s*-\s*\d\{4}\)\=\>', "w") > 0 "{
          " Found a Copyright line.
          let found_copyright = 1

          let current_year = strftime("%Y")
          let copyright_line = getline(".")
          let docyear = matchstr(copyright_line, '\<\d\{4}\s*-\s*\d\{4}\>')

          if docyear == ""
          let docyear = matchstr(getline("."), '\<\d\{4}\>')
          if docyear != "" && docyear < current_year
          " Only one year listed; and it's old; append current year.
          execute 's/\<\(\d\{4}\)\>/\1-' . current_year
          endif
          else
          " Two years listed; extract second one.
          let docyear = matchstr(docyear, '\d\{4}$')
          if docyear < current_year
          " Second year is old; make it the current year.
          execute 's/\<\(\d\{4}\)\s*-\s*\d\{4}\>/\1-' . current_year
          endif
          endif

          " Center the copyright line (on an 80-character line).
          "
          " We do this in a buffer, and then see if what we came up with
          " differs from what's already there. That way we don't modify this
          " line unneccessarily (putting it into the 'undo' chain, etc.)
          "
          " NOTE: This is expecting vim's auto formatting turned on (e.g.,
          " formatoptions=cloqr)
          "
          " Look for various language's comment leader...
          "
          let leader = matchstr(getline("."), '^\([/ ]\=[*]\|#\{1,2}\|//\)\s')
          if leader != ''
          let cline = getline(".")
          let cline = substitute(cline, '^.\{' . strlen(leader) . '}', '', '')
          let cline = substitute(cline, '^\s\+', '', '')
          let cline = substitute(cline, '\s\+$', '', '')
          let length = (80 - strlen(cline) - strlen(leader)) / 2
          " Don't know a way to mimic repeat(c, n), so...
          if (length > 0)
          let padding = matchstr(" ", ' \{' . length . '}')
          else
          let padding = ''
          endif
          let leader = substitute(leader, '\s$', '', '')
          let cline = leader . padding . cline

          if cline != getline(".")
          exe "normal 0Da" . cline
          endif
          endif

          if getline(".") != copyright_line
          se cmdheight+=6
          echohl Question
          echo "Updated copyright..."
          echohl Warningmsg
          echo 'OLD:'
          echohl Comment
          echo copyright_line
          echohl Warningmsg
          echo 'NEW:'
          echohl Comment
          echo getline(".")
          echohl Question
          let foo = input("Press RETURN")
          echohl NONE
          se cmdheight-=6
          endif
          endif "}

          if found_copyright && getline(".") != copyright_line
          " We found, and changed a Copyright line; if we're in the preview
          " window, return to the normal window, otherwise stay on the
          " copyright line.
          "
          if &previewwindow
          3 wincmd _
          normal zz
          silent wincmd p
          endif
          else
          " We did not find a Copyright line; if we're in the preview window,
          " close it, otherwise put the cursor back where it was.
          "
          if &previewwindow
          pclose
          else
          " Put the cursor back in the original location the hard way.
          exe top_line
          normal zt
          exe current_line
          exe "normal 0" . current_col . "|"
          endif
          endif

          endfunction

          -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-

          / FROM: Martin Metz <martin.metz@...>, Jan 3 13:18 2002
          | ABOUT: Re: Fixing the year in a copyright line
          |
          | Hello,
          |
          | I'm also very glad of the below given code and use it with an
          | autocommand when writing a buffer. But one detail is ugly:
          |
          | Because of the "search()" the cursor position changes. That's not
          | important if I close the VIM. But if I just save the current buffer e.
          | g. with ":w" I loose the current position and stay at the copyright
          | message. :-(
          |
          | Does any one have a good idea to jump back to the old position? Maybe
          | with marks? I'm currently to stupid to find a solution.
          |
          | Gary Holloway wrote:
          | >
          | > I liked the concept of this, so I took Benji's answer as a start and modified
          | > it a bit (see below).
          | >
          | [...]
          | >
          | > " Update copyright message (if any) with current year, if not present.
          | >
          | > function! UpdateCopyright()
          | > if filewritable (expand ("%"))
          | > let current_year = strftime("%Y")
          | >
          | > if search('Copyright *(c) Cast & Crew.*\<\d\{4}\(-\d\{4}\)\=\>', "w") > 0
          | > let copyright_line = getline(".")
          | > let docyear = matchstr(copyright_line, '\<\d\{4}-\d\{4}\>')
          | >
          | > if docyear == ""
          | > let docyear = matchstr(getline("."), '\<\d\{4}\>')
          | > if docyear != "" && docyear < current_year
          | > " Only one year listed; and it's old; append current year.
          | > execute 's/\<\(\d\{4}\)\>/\1-' . current_year
          | > endif
          | > else
          | > " Two years listed; extract second one.
          | > let docyear = matchstr(docyear, '\d\{4}$')
          | > if docyear < current_year
          | > " Second year is old; make it the current year.
          | > execute 's/\<\(\d\{4}\)-\d\{4}\>/\1-' . current_year
          | > endif
          | > endif
          | >
          | > if getline(".") != copyright_line
          | > se cmdheight+=5
          | > echohl Warningmsg
          | > echo 'OLD:'
          | > echohl Comment
          | > echo copyright_line
          | > echohl Warningmsg
          | > echo 'NEW:'
          | > echohl Comment
          | > echo getline(".")
          | > echohl Question
          | > let foo = input("Updated copyright (press RETURN)")
          | > echohl NONE
          | > se cmdheight-=5
          | > endif
          | > endif
          | > endif
          | >
          | > endfunction
          |
          | --
          | Gruesse,
          |
          | Martin Metz
          |
          | /***************************************************************
          | * Dipl. Inform. (FH) Martin Metz - Software engineering
          | *
          | * OneVision Software AG | Phone: +49-941-780040
          | * Dr.-Leo-Ritter-Str. 9 | FAX: +49-941-7800449
          | * 93049 Regensburg | mailto:Martin.Metz@...
          | * Germany | WWW: http://www.OneVision.de
          | **************************************************************/
          |
          \ END: Martin Metz
        • Douglas L . Potts
          ... [--- snip ---] ... Or using the VirMark.vim plugin on: http://vim.sourceforge.net/scripts/script.php?script_id=140 Which expands on Benji s Mark() idea.
          Message 4 of 13 , Jan 3, 2002
          • 0 Attachment
            On Thu, Jan 03, 2002 at 09:08:46AM -0500 Benji Fisher wrote:
            > Martin Metz wrote:
            [--- snip ---]
            > If you are doing it from inside a script, and want to avoid using marks (which
            > are global) then look at the Mark() function in foo.vim:
            >
            > http://vim.sourceforge.net/scripts/script.php?script_id=72

            Or using the VirMark.vim plugin on:
            http://vim.sourceforge.net/scripts/script.php?script_id=140

            Which expands on Benji's Mark() idea.

            -Doug

            --
            *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
            Douglas L. Potts
            "Where do you want to crash, toady?" -Rich Paul (or at least his .sig)
            GPG Fingerprint: 768A EEF8 197A 4C9A 5EF7 DA5B 464C 97DF DCD5 68C2
            *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
          • Gary Johnson
            ... I ve been using your UpdateCopyright() since you first posted it, but with the following change to the search() function call to make the copyright string
            Message 5 of 13 , Jan 3, 2002
            • 0 Attachment
              On Thu, Jan 03, 2002 at 09:02:59AM -0800, Gary Holloway wrote:
              > Here's a more recent version... it utilizes the preview window to avoid that,
              > and leaves the copyright line visible in the preview window if it was changed;
              > and fixed several "oddities" (ok, bugs :).
              >
              > I edited out some company-specifics; I don't think I created any bug doing
              > that... :)

              I've been using your UpdateCopyright() since you first posted it, but
              with the following change to the search() function call to make the
              copyright string customizable without having to further modify your
              function:

              if search(g:copyright_pre.'\<\d\{4}\(-\d\{4}\)\=\>'.g:copyright_post, "w") > 0

              I have copyright_pre and copyright_post defined in my .vimrc as follows
              to agree with our company standards:

              let g:copyright_pre = "(c) Copyright *"
              let g:copyright_post = " *Agilent Technologies"

              One could also use autocommands to change among different strings for
              company files and personal files.

              With the little bit of testing I did last year, it worked great. Now
              that a new year is here, I'll have a chance to use it for real when I
              get back to work next week.

              If one could choose whether or not to center the copyright notice, e.g.,
              using a g:copyright_centered flag, this would make a great plugin to
              contribute to vim online.

              Gary

              --
              Gary Johnson | Agilent Technologies
              garyjohn@... | Spokane, Washington, USA
            • HALIM,SALMAN (HP-NewJersey,ex2)
              once you have the line in question (copyright_line), you could just try something like: if (g:copyright_centered == 1) execute copyright_line . center endif
              Message 6 of 13 , Jan 3, 2002
              • 0 Attachment
                once you have the line in question (copyright_line), you could just try
                something like:

                if (g:copyright_centered == 1)
                execute copyright_line . "center"
                endif

                this will center it in the current textwidth -- you could force a specific
                textwidth by doing this:

                if (g:copyright_centered == 1)
                let savetw=&tw
                set tw=80
                execute copyright_line . "center"
                let &tw=savetw
                endif

                hope this helps,

                salman.

                -----Original Message-----
                From: Gary Johnson [mailto:garyjohn@...]
                Sent: Thursday, January 03, 2002 4:25 PM
                To: Vim (E-mail)
                Subject: Re: Fixing the year in a copyright line


                On Thu, Jan 03, 2002 at 09:02:59AM -0800, Gary Holloway wrote:
                > Here's a more recent version... it utilizes the preview window to avoid
                that,
                > and leaves the copyright line visible in the preview window if it was
                changed;
                > and fixed several "oddities" (ok, bugs :).
                >
                > I edited out some company-specifics; I don't think I created any bug doing
                > that... :)

                I've been using your UpdateCopyright() since you first posted it, but
                with the following change to the search() function call to make the
                copyright string customizable without having to further modify your
                function:

                if search(g:copyright_pre.'\<\d\{4}\(-\d\{4}\)\=\>'.g:copyright_post,
                "w") > 0

                I have copyright_pre and copyright_post defined in my .vimrc as follows
                to agree with our company standards:

                let g:copyright_pre = "(c) Copyright *"
                let g:copyright_post = " *Agilent Technologies"

                One could also use autocommands to change among different strings for
                company files and personal files.

                With the little bit of testing I did last year, it worked great. Now
                that a new year is here, I'll have a chance to use it for real when I
                get back to work next week.

                If one could choose whether or not to center the copyright notice, e.g.,
                using a g:copyright_centered flag, this would make a great plugin to
                contribute to vim online.

                Gary

                --
                Gary Johnson | Agilent Technologies
                garyjohn@... | Spokane, Washington, USA
              • Gary Holloway
                / FROM: HALIM,SALMAN (HP-NewJersey,ex2) , Jan 3 14:02 2002 ... [snip] Actually, centered is a little bit of a misnomer... It ll change
                Message 7 of 13 , Jan 3, 2002
                • 0 Attachment
                  / FROM: "HALIM,SALMAN (HP-NewJersey,ex2)" <salman@...>, Jan 3 14:02 2002
                  | ABOUT: RE: Fixing the year in a copyright line
                  |
                  | once you have the line in question (copyright_line), you could just try
                  | something like:
                  |
                  | if (g:copyright_centered == 1)
                  | execute copyright_line . "center"
                  | endif
                  |
                  | this will center it in the current textwidth -- you could force a specific
                  | textwidth by doing this:
                  |
                  [snip]

                  Actually, "centered" is a little bit of a misnomer...
                  It'll change something like:

                  * Copyright (c) Cast & Crew Entertainment Services, Inc. 1998-2002

                  to:

                  * Copyright (c) Cast & Crew Entertainment Services, Inc. 1998-2002

                  (Note the leading '*')
                  So, the 'center' command (alone) wouldn't quite do the job.

                  In context, we've got file-header comments like:

                  /******************************************************************************
                  *
                  [snip]
                  *
                  * Copyright (c) Cast & Crew Entertainment Services, Inc. 1998-2002
                  *
                  [snip]
                  *
                  * Description:
                  * This makes a foobar
                  *
                  ******************************************************************************/

                  And the function is trying to maintain the "centeredness" of the line after it
                  [potentially] appends a year to it.

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