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

24772Re: Fixing the year in a copyright line

Expand Messages
  • Gary Holloway
    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
    • Show all 13 messages in this topic