home *** CD-ROM | disk | FTP | other *** search
/ vim.ftp.fu-berlin.de / 2015-02-03.vim.ftp.fu-berlin.de.tar / vim.ftp.fu-berlin.de / unix / vim-6.2.tar.bz2 / vim-6.2.tar / vim62 / runtime / ftplugin / changelog.vim < prev    next >
Encoding:
Text File  |  2003-01-15  |  6.6 KB  |  239 lines

  1. " Vim filetype plugin
  2. " Language:        generic Changelog file
  3. " Maintainer:        Nikolai 'pcp' Weibull <da.box@home.se>
  4. " URL:            http://www.pcppopper.org/
  5. " Latest Revision:  2003-01-14
  6. " Variables:
  7. "   g:changelog_timeformat -
  8. "    description: the timeformat used in ChangeLog entries.
  9. "    default: "%Y-%m-%d".
  10. "   g:changelog_username -
  11. "    description: the username to use in ChangeLog entries
  12. "    default: try to deduce it from environment variables and system    files.
  13. " Local Mappings:
  14. "   <Leader>o -
  15. "    adds a new changelog entry for the current user for the current date.
  16. " Global Mappings:
  17. "   <Leader>o -
  18. "    switches to the ChangeLog buffer opened for the current directory, or
  19. "    opens it in a new buffer if it exists in the current directory.  Then
  20. "    it does the same as the local <Leader>o described above.
  21. "
  22. " Notes:
  23. "   run 'runtime ftplugin/changelog.vim' to enable the global mapping for
  24. "   changelog files.
  25. " TODO:
  26. "  should we perhaps open the ChangeLog file even if it doesn't exist already?
  27. "  Problem is that you might end up with ChangeLog files all over the place.
  28.  
  29. " If 'filetype' isn't "changelog", we must have been to add ChangeLog opener
  30. if &filetype == "changelog"
  31.     " Only do this when not done yet for this buffer
  32.     if exists("b:did_ftplugin")
  33.     finish
  34.     endif
  35.  
  36.     " Don't load another plugin for this buffer
  37.     let b:did_ftplugin = 1
  38.  
  39.     " The format of the date-time field (should have been called dateformat)
  40.     if !exists("g:changelog_timeformat")
  41.     let g:changelog_timeformat = "%Y-%m-%d"
  42.     endif
  43.  
  44.     " Try to figure out a reasonable username of the form:
  45.     " Full Name <user@host>
  46.     if !exists("g:changelog_username")
  47.  
  48.     " Get the users login name
  49.     let login = system('whoami')
  50.     if v:shell_error
  51.         let login = 'unknown'
  52.     else
  53.         let newline = stridx(login, "\n")
  54.         if newline != -1
  55.         let login = strpart(login, 0, newline)
  56.         endif
  57.     endif
  58.  
  59.     " Try to full name from gecos field in /etc/passwd
  60.     if filereadable('/etc/passwd')
  61.         let name = substitute(
  62.             \system('cat /etc/passwd | grep ^`whoami`'),
  63.             \'^\%([^:]*:\)\{4}\([^:]*\):.*$', '\1', '')
  64.     endif
  65.  
  66.     " If there is no such file, or there was some other problem try others
  67.     if !filereadable('/etc/passwd') || v:shell_error
  68.         " Maybe the environment has something of interest
  69.         if exists("$NAME")
  70.         let name = $NAME
  71.         else
  72.         " No? well, use the login name and capitalize first character
  73.         let name = toupper(login[0]) . strpart(login, 1)
  74.         endif
  75.     endif
  76.  
  77.     " Only keep stuff before the first comma
  78.     let comma = stridx(name, ',')
  79.     if comma != -1
  80.         let name = strpart(name, 0, comma)
  81.     endif
  82.  
  83.     " And substitute & in the real name with the login of our user
  84.     let amp = stridx(name, '&')
  85.     if amp != -1
  86.         let name = strpart(name, 0, amp) . toupper(login[0]) .
  87.             \strpart(login, 1) . strpart(name, amp + 1)
  88.     endif
  89.  
  90.     " Get our hostname
  91.     let hostname = system("hostname")
  92.     if v:shell_error
  93.         let hostname = 'unknownhost'
  94.     else
  95.         let newline = stridx(hostname, "\n")
  96.         if newline != -1
  97.         let hostname = strpart(hostname, 0, newline)
  98.         endif
  99.     endif
  100.  
  101.     " And finally set the username
  102.     let g:changelog_username = name.'  <'.login.'@'.hostname.'>'
  103.     endif
  104.  
  105.     " Format used for new date-entries
  106.     if !exists("g:changelog_new_date_format")
  107.     let g:changelog_new_date_format = "%d  %u\n\n\t* %c\n\n"
  108.     endif
  109.  
  110.     " Format used for new entries to current date-entry
  111.     if !exists("g:changelog_new_entry_format")
  112.     let g:changelog_new_entry_format = "\t* %c"
  113.     endif
  114.  
  115.     if !exists("g:changelog_date_entry_search")
  116.     let g:changelog_date_entry_search = '^\s*%d\_s*%u'
  117.     endif
  118.  
  119.     " Substitutes specific items in new date-entry formats and search strings
  120.     " Can be done with substitute of course, but unclean, and need \@! then
  121.     function! s:substitute_items(str, date, user)
  122.     let str = a:str
  123.     let i = stridx(str, '%')
  124.     while i != -1
  125.         let char = str[i + 1]
  126.         if char == '%'
  127.         let middle = '%'
  128.         elseif char == 'd'
  129.         let middle = a:date
  130.         elseif char == 'u'
  131.         let middle = a:user
  132.         elseif char == 'c'
  133.         let middle = '{cursor}'
  134.         else
  135.         let middle = char
  136.         endif
  137.         let str = strpart(str, 0, i) . middle . strpart(str, i + 2)
  138.         let i = stridx(str, '%')
  139.     endwhile
  140.     return str
  141.     endfunction
  142.  
  143.     function! s:position_cursor()
  144.     if search('{cursor}') > 0
  145.         let pos = line('.')
  146.         let line = getline(pos)
  147.         let cursor = stridx(line, '{cursor}')
  148.         call setline(pos, substitute(line, '{cursor}', '', ''))
  149.     endif
  150.     startinsert!
  151.     endfunction
  152.  
  153.     " Internal function to create a new entry in the ChangeLog
  154.     function! s:new_changelog_entry()
  155.     " Deal with 'paste' option
  156.     let save_paste = &paste
  157.     let &paste = 1
  158.     1
  159.     " Look for an entry for today by our user
  160.     let date = strftime(g:changelog_timeformat)
  161.     let search = s:substitute_items(g:changelog_date_entry_search, date,
  162.                    \g:changelog_username)
  163.     if search(search) > 0
  164.         " Ok, now we look for the end of the date-entry, and add an entry
  165.         let pos = nextnonblank(line('.') + 1)
  166.         let line = getline(pos)
  167.         while line =~ '^\s\+\S\+'
  168.         let pos = pos + 1
  169.         let line = getline(pos)
  170.         endwhile
  171.         let insert = s:substitute_items(g:changelog_new_entry_format,
  172.                        \'', '')
  173.         execute "normal! ".(pos - 1)."Go".insert
  174.         execute pos
  175.     else
  176.         " Flag for removing empty lines at end of new ChangeLogs
  177.         let remove_empty = line('$') == 1
  178.  
  179.         " No entry today, so create a date-user header and insert an entry
  180.         let todays_entry = s:substitute_items(g:changelog_new_date_format,
  181.                          \date, g:changelog_username)
  182.         " Make sure we have a cursor positioning
  183.         if stridx(todays_entry, '{cursor}') == -1
  184.         let todays_entry = todays_entry.'{cursor}'
  185.         endif
  186.  
  187.         " Now do the work
  188.         execute "normal! i".todays_entry
  189.         if remove_empty
  190.         while getline('$') == ''
  191.             $delete
  192.         endwhile
  193.         endif
  194.  
  195.         1
  196.     endif
  197.  
  198.     call s:position_cursor()
  199.  
  200.     " And reset 'paste' option
  201.     let &paste = save_paste
  202.     endfunction
  203.  
  204.     if exists(":NewChangelogEntry") != 2
  205.     map <buffer> <silent> <Leader>o <Esc>:call <SID>new_changelog_entry()<CR>
  206.     command! -nargs=0 NewChangelogEntry call s:new_changelog_entry()
  207.     endif
  208.  
  209.     setlocal comments=
  210.     setlocal textwidth=78
  211.     setlocal formatoptions+=t
  212. else
  213.     " Add the Changelog opening mapping
  214.     nmap <silent> <Leader>o   :call <SID>open_changelog()<CR>
  215.  
  216.     function! s:open_changelog()
  217.     if filereadable('ChangeLog')
  218.         if bufloaded('ChangeLog')
  219.         let buf = bufnr('ChangeLog')
  220.         execute "normal! \<C-W>t"
  221.         while winbufnr(winnr()) != buf
  222.             execute "normal! \<C-W>w"
  223.         endwhile
  224.         else
  225.         split ChangeLog
  226.         endif
  227.  
  228.         if exists("g:mapleader")
  229.         execute "normal " . g:mapleader . "o"
  230.         else
  231.         execute "normal \\o"
  232.         endif
  233.         startinsert!
  234.     endif
  235.     endfunction
  236. endif
  237.  
  238. " vim: set sw=4 sts=4:
  239.