contrib/vim/hgcommand.vim
changeset 2734 0b7206a65325
parent 2638 8dadff054acf
child 2759 19436facb073
equal deleted inserted replaced
2733:42a02b5773d4 2734:0b7206a65325
   351     else
   351     else
   352       " The file is tracked, we can try to get is revision number
   352       " The file is tracked, we can try to get is revision number
   353       let hgCommand = s:HGGetOption("HGCommandHGExec", "hg") . " parents -b  " 
   353       let hgCommand = s:HGGetOption("HGCommandHGExec", "hg") . " parents -b  " 
   354       let statustext=system(hgCommand)
   354       let statustext=system(hgCommand)
   355       if(v:shell_error)
   355       if(v:shell_error)
   356 	  return ""
   356           return ""
   357       endif
   357       endif
   358       let revision=substitute(statustext, '^changeset:\s*\(\d\+\):.*\_$\_.*$', '\1', "")
   358       let revision=substitute(statustext, '^changeset:\s*\(\d\+\):.*\_$\_.*$', '\1', "")
   359 
   359 
   360       if a:branchVar != "" && match(statustext, '^\_.*\_^branch:') >= 0
   360       if a:branchVar != "" && match(statustext, '^\_.*\_^branch:') >= 0
   361 	let branch=substitute(statustext, '^\_.*\_^branch:\s*\(\S\+\)\n\_.*$', '\1', "")
   361         let branch=substitute(statustext, '^\_.*\_^branch:\s*\(\S\+\)\n\_.*$', '\1', "")
   362 	let returnExpression=returnExpression . " | let " . a:branchVar . "='" . branch . "'"
   362         let returnExpression=returnExpression . " | let " . a:branchVar . "='" . branch . "'"
   363       endif
   363       endif
   364     endif
   364     endif
   365     if (exists('revision'))
   365     if (exists('revision'))
   366       let returnExpression = "let " . a:revisionVar . "='" . revision . "' " . returnExpression
   366       let returnExpression = "let " . a:revisionVar . "='" . revision . "' " . returnExpression
   367     endif
   367     endif
   476 "              will be replaced with this string with 'v' prefix.
   476 "              will be replaced with this string with 'v' prefix.
   477 " Return:
   477 " Return:
   478 "   1 if new document installed, 0 otherwise.
   478 "   1 if new document installed, 0 otherwise.
   479 " Note: Cleaned and generalized by guo-peng Wen
   479 " Note: Cleaned and generalized by guo-peng Wen
   480 "'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
   480 "'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
       
   481 " Helper function to make mkdir as portable as possible
       
   482 function! s:HGFlexiMkdir(dir)
       
   483   if exists("*mkdir") " we can use Vim's own mkdir()
       
   484     call mkdir(a:dir)
       
   485   elseif !exists("+shellslash")
       
   486     call system('mkdir -p "'.a:dir.'"')
       
   487   else " M$
       
   488     let l:ssl = &shellslash
       
   489     try
       
   490       set shellslash
       
   491       call system('mkdir "'.a:dir.'"')
       
   492     finally
       
   493       let &shellslash = l:ssl
       
   494     endtry
       
   495   endif
       
   496 endfunction
   481 
   497 
   482 function! s:HGInstallDocumentation(full_name, revision)
   498 function! s:HGInstallDocumentation(full_name, revision)
   483     " Name of the document path based on the system we use:
   499   " Figure out document path based on full name of this script:
   484     if (has("unix"))
   500   let l:vim_plugin_path = fnamemodify(a:full_name, ':h')
   485         " On UNIX like system, using forward slash:
   501   let l:vim_doc_path  = fnamemodify(a:full_name, ':h:h') . "/doc"
   486         let l:slash_char = '/'
   502   if filewritable(l:vim_doc_path) != 2
   487         let l:mkdir_cmd  = ':silent !mkdir -p '
   503     echomsg "hgcommand: Trying to update docs at: " . l:vim_doc_path
   488     else
   504     silent! call <SID>HGFlexiMkdir(l:vim_doc_path)
   489         " On M$ system, use backslash. Also mkdir syntax is different.
   505     if filewritable(l:vim_doc_path) != 2
   490         " This should only work on W2K and up.
   506       " Try first item in 'runtimepath':
   491         let l:slash_char = '\'
   507       let l:vimfiles = matchstr(&runtimepath, '[^,]\+\ze,')
   492         let l:mkdir_cmd  = ':silent !mkdir '
   508       let l:vim_doc_path = l:vimfiles . "/doc"
   493     endif
   509       if filewritable(l:vim_doc_path) != 2
   494 
   510         echomsg "hgcommand: Trying to update docs at: " . l:vim_doc_path
   495     let l:doc_path = l:slash_char . 'doc'
   511         silent! call <SID>HGFlexiMkdir(l:vim_doc_path)
   496     let l:doc_home = l:slash_char . '.vim' . l:slash_char . 'doc'
   512         if filewritable(l:vim_doc_path) != 2
   497 
   513           " Put a warning:
   498     " Figure out document path based on full name of this script:
   514           echomsg "Unable to open documentation directory"
   499     let l:vim_plugin_path = fnamemodify(a:full_name, ':h')
   515           echomsg " type `:help add-local-help' for more information."
   500     let l:vim_doc_path    = fnamemodify(a:full_name, ':h:h') . l:doc_path
   516           return 0
   501     if (!(filewritable(l:vim_doc_path) == 2))
       
   502         echomsg "Doc path: " . l:vim_doc_path
       
   503         execute l:mkdir_cmd . '"' . l:vim_doc_path . '"'
       
   504         if (!(filewritable(l:vim_doc_path) == 2))
       
   505             " Try a default configuration in user home:
       
   506             let l:vim_doc_path = expand("~") . l:doc_home
       
   507             if (!(filewritable(l:vim_doc_path) == 2))
       
   508                 execute l:mkdir_cmd . '"' . l:vim_doc_path . '"'
       
   509                 if (!(filewritable(l:vim_doc_path) == 2))
       
   510                     " Put a warning:
       
   511                     echomsg "Unable to open documentation directory"
       
   512                     echomsg " type :help add-local-help for more informations."
       
   513                     return 0
       
   514                 endif
       
   515             endif
       
   516         endif
   517         endif
   517     endif
   518       endif
   518 
   519     endif
   519     " Exit if we have problem to access the document directory:
   520   endif
   520     if (!isdirectory(l:vim_plugin_path)
   521 
   521         \ || !isdirectory(l:vim_doc_path)
   522   " Full name of script and documentation file:
   522         \ || filewritable(l:vim_doc_path) != 2)
   523   let l:script_name = fnamemodify(a:full_name, ':t')
   523         return 0
   524   let l:doc_name  = fnamemodify(a:full_name, ':t:r') . '.txt'
   524     endif
   525   let l:doc_file  = l:vim_doc_path . "/" . l:doc_name
   525 
   526 
   526     " Full name of script and documentation file:
   527   " Bail out if document file is still up to date:
   527     let l:script_name = fnamemodify(a:full_name, ':t')
   528   if filereadable(l:doc_file)  && getftime(a:full_name) < getftime(l:doc_file)
   528     let l:doc_name    = fnamemodify(a:full_name, ':t:r') . '.txt'
   529     return 0
   529     let l:plugin_file = l:vim_plugin_path . l:slash_char . l:script_name
   530   endif
   530     let l:doc_file    = l:vim_doc_path    . l:slash_char . l:doc_name
   531 
   531 
   532   " Create a new buffer & read in the plugin file (me):
   532     " Bail out if document file is still up to date:
   533   setl nomodeline
   533     if (filereadable(l:doc_file)  &&
   534   1 new!
   534         \ getftime(l:plugin_file) < getftime(l:doc_file))
   535   setl noswapfile modifiable
   535         return 0
   536   sil exe 'read ' . a:full_name
   536     endif
   537 
   537 
   538   setl modeline
   538     " Prepare window position restoring command:
   539   let l:buf = bufnr("%")
   539     if (strlen(@%))
   540 
   540         let l:go_back = 'b ' . bufnr("%")
   541   norm zR
   541     else
   542   norm gg
   542         let l:go_back = 'enew!'
   543 
   543     endif
   544   " Delete from first line to a line starts with
   544 
   545   " === START_DOC
   545     " Create a new buffer & read in the plugin file (me):
   546   sil 1,/^=\{3,}\s\+START_DOC\C/ d
   546     setl nomodeline
   547 
   547     exe 'enew!'
   548   " Delete from a line starts with
   548     exe 'r ' . l:plugin_file
   549   " === END_DOC
   549 
   550   " to the end of the documents:
   550     setl modeline
   551   sil /^=\{3,}\s\+END_DOC\C/,$ d
   551     let l:buf = bufnr("%")
   552 
   552     setl noswapfile modifiable
   553   " Remove fold marks:
   553 
   554   sil %s/{\{3}[1-9]/  /e
   554     norm zR
   555 
   555     norm gg
   556   " Add modeline for help doc: the modeline string is mangled intentionally
   556 
   557   " to avoid it be recognized by VIM:
   557     " Delete from first line to a line starts with
   558   call append(line('$'), '')
   558     " === START_DOC
   559   call append(line('$'), ' v' . 'im:tw=78:ts=8:ft=help:norl:')
   559     1,/^=\{3,}\s\+START_DOC\C/ d
   560 
   560 
   561   " Replace revision:
   561     " Delete from a line starts with
   562   sil exe "normal :1s/#version#/ v" . a:revision . "/\<CR>"
   562     " === END_DOC
   563 
   563     " to the end of the documents:
   564   " Save the help document and wipe out buffer:
   564     /^=\{3,}\s\+END_DOC\C/,$ d
   565   sil exe 'wq! ' . l:doc_file . ' | bw ' . l:buf
   565 
   566 
   566     " Remove fold marks:
   567   " Build help tags:
   567     %s/{\{3}[1-9]/    /
   568   sil exe 'helptags ' . l:vim_doc_path
   568 
   569 
   569     " Add modeline for help doc: the modeline string is mangled intentionally
   570   return 1
   570     " to avoid it be recognized by VIM:
       
   571     call append(line('$'), '')
       
   572     call append(line('$'), ' v' . 'im:tw=78:ts=8:ft=help:norl:')
       
   573 
       
   574     " Replace revision:
       
   575     exe "normal :1s/#version#/ v" . a:revision . "/\<CR>"
       
   576 
       
   577     " Save the help document:
       
   578     exe 'w! ' . l:doc_file
       
   579     exe l:go_back
       
   580     exe 'bw ' . l:buf
       
   581 
       
   582     " Build help tags:
       
   583     exe 'helptags ' . l:vim_doc_path
       
   584 
       
   585     return 1
       
   586 endfunction
   571 endfunction
   587 
   572 
   588 " Section: Public functions {{{1
   573 " Section: Public functions {{{1
   589 
   574 
   590 " Function: HGGetRevision() {{{2
   575 " Function: HGGetRevision() {{{2
  1183 endif
  1168 endif
  1184 
  1169 
  1185 " Section: Doc installation {{{1
  1170 " Section: Doc installation {{{1
  1186 "
  1171 "
  1187   let s:revision="0.1"
  1172   let s:revision="0.1"
  1188   silent! let s:install_status =
  1173   if s:HGInstallDocumentation(escape(expand('<sfile>:p'), ' '), s:revision)
  1189       \ s:HGInstallDocumentation(expand('<sfile>:p'), s:revision)
  1174       echom expand('<sfile>:t:r') . ' v' . s:revision .
  1190   if (s:install_status == 1)
  1175                 \ ': Help-documentation installed.'
  1191       echom expand("<sfile>:t:r") . ' v' . s:revision .
  1176   endif
  1192 		\ ': Help-documentation installed.'
  1177 
  1193   endif
  1178   " delete one-time vars and functions
       
  1179   delfunction <SID>HGInstallDocumentation
       
  1180   delfunction <SID>HGFlexiMkdir
       
  1181   unlet s:revision
  1194 
  1182 
  1195 
  1183 
  1196 " Section: Plugin completion {{{1
  1184 " Section: Plugin completion {{{1
  1197 
  1185 
  1198 let loaded_hgcommand=2
  1186 let loaded_hgcommand=2
  1636    that it attempts to set two buffer variables ('HGRevision' and 'HGBranch') 
  1624    that it attempts to set two buffer variables ('HGRevision' and 'HGBranch') 
  1637    upon entry into a buffer.  This is rather slow because it means that 'hg 
  1625    upon entry into a buffer.  This is rather slow because it means that 'hg 
  1638    status' will be invoked at each entry into a buffer (during the |BufEnter| 
  1626    status' will be invoked at each entry into a buffer (during the |BufEnter| 
  1639    autocommand).
  1627    autocommand).
  1640 
  1628 
  1641    This mode is enablmed by default.  In order to disable it, set the 
  1629    This mode is enabled by default.  In order to disable it, set the 
  1642    |HGCommandEnableBufferSetup| variable to a false (zero) value.  Enabling 
  1630    |HGCommandEnableBufferSetup| variable to a false (zero) value.  Enabling 
  1643    this mode simply provides the buffer variables mentioned above.  The user 
  1631    this mode simply provides the buffer variables mentioned above.  The user 
  1644    must explicitly include those in the |'statusline'| option if they are to 
  1632    must explicitly include those in the |'statusline'| option if they are to 
  1645    appear in the status line (but see |hgcommand-statusline| for a simple way
  1633    appear in the status line (but see |hgcommand-statusline| for a simple way
  1646    to do that).
  1634    to do that).