1 " Vim script
  2 " Maintainer: Peter Odding <peter@peterodding.com>
  3 " Last Change: August 30, 2010
  4 " URL: http://peterodding.com/code/vim/profile/autoload/xolox.vim
  5 
  6 " Miscellaneous functions used throughout my Vim profile and plug-ins.
  7 
  8 " Lately I've been losing my message history a lot so I've added this option
  9 " which keeps a ring buffer with the last N messages in "g:xolox_messages".
 10 if !exists('g:xolox_message_buffer')
 11   let g:xolox_message_buffer = 100
 12 endif
 13 
 14 if !exists('g:xolox_messages')
 15   let g:xolox_messages = []
 16 endif
 17 
 18 function! xolox#trim(s) " -- trim whitespace from start/end of {s} {{{1
 19   return substitute(a:s, '^\s*\(.\{-}\)\s*$', '\1', '')
 20 endfunction
 21 
 22 function! xolox#quote_pattern(s) " -- convert {s} to pattern that matches {s} literally (on word boundaries!) {{{1
 23   let patt = xolox#escape#pattern(a:s)
 24   if patt =~ '^\w'
 25     let patt = '\<' . patt
 26   endif
 27   if patt =~ '\w$'
 28     let patt = patt . '\>'
 29   endif
 30   return patt
 31 endfunction
 32 
 33 function! xolox#unique(list) " -- remove duplicate values from {list} (in-place) {{{1
 34   let index = 0
 35   while index < len(a:list)
 36     let value = a:list[index]
 37     let match = index(a:list, value, index+1)
 38     if match >= 0
 39       call remove(a:list, match)
 40     else
 41       let index += 1
 42     endif
 43     unlet value
 44   endwhile
 45   return a:list
 46 endfunction
 47 
 48 function! xolox#message(...) " -- show a formatted informational message to the user {{{1
 49   call s:message('title', a:000)
 50 endfunction
 51 
 52 function! xolox#warning(...) " -- show a formatted warning message to the user {{{1
 53   call s:message('warningmsg', a:000)
 54 endfunction
 55 
 56 function! xolox#debug(...) " -- show a formatted debugging message to the user {{{1
 57   if &vbs >= 1
 58     call s:message('question', a:000)
 59   endif
 60 endfunction
 61 
 62 function! s:message(hlgroup, args) " -- implementation of message() and warning() {{{1
 63   let nargs = len(a:args)
 64   if nargs == 1
 65     let message = a:args[0]
 66   elseif nargs >= 2
 67     let message = call('printf', a:args)
 68   endif
 69   if exists('message')
 70     try
 71       " Temporarily disable Vim's |hit-enter| prompt and mode display.
 72       if !exists('s:more_save')
 73         let s:more_save = &more
 74         let s:ruler_save = &ruler
 75         let s:smd_save = &showmode
 76       endif
 77       set nomore noshowmode
 78       if winnr('$') == 1 | set noruler | endif
 79       augroup PluginXoloxHideMode
 80         autocmd! CursorHold,CursorHoldI * call s:clear_message()
 81       augroup END
 82       execute 'echohl' a:hlgroup
 83       " Redraw to avoid |hit-enter| prompt.
 84       redraw | echomsg message
 85       if g:xolox_message_buffer > 0
 86         call add(g:xolox_messages, message)
 87         if len(g:xolox_messages) > g:xolox_message_buffer
 88           call remove(g:xolox_messages, 0)
 89         endif
 90       endif
 91     finally
 92       " Always clear message highlighting -- even when interrupted by Ctrl-C.
 93       echohl none
 94     endtry
 95   endif
 96 endfunction
 97 
 98 function! s:clear_message()
 99   echo ''
100   let &more = s:more_save
101   let &showmode = s:smd_save
102   let &ruler = s:ruler_save
103   unlet s:more_save s:ruler_save s:smd_save
104   autocmd! PluginXoloxHideMode
105   augroup! PluginXoloxHideMode
106 endfunction