glimpse/plugin/glimpse.vim
author mahlon
Thu, 19 Mar 2009 16:47:44 +0000
branchvim-stuff
changeset 1 fd505fd00e63
child 4 4ffc0380b228
permissions -rw-r--r--
Initial commit of the VimGlimpse plugin.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
     1
" vim: set noet nosta sw=4 ts=4 fdm=marker :
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
     2
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
     3
" A basic Glimpse interface for Vim
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
     4
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
     5
" Based on a Vim Tip from Jean-Rene David.
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
     6
" http://vim.wikia.com/wiki/Use_glimpse_from_within_Vim
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
     7
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
     8
" Mahlon E. Smith <mahlon@martini.nu>
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
     9
" $Id$
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    10
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    11
" Here's a tcsh alias that's nice too!
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    12
" alias g 'glimpse -winO -F `pwd` \!:1 | sed -e "s|^`pwd`/||"'
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    13
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    14
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    15
" Loaded check {{{
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    16
if exists( 'g:glimpse_loaded' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    17
    finish
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    18
endif
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    19
let g:glimpse_loaded = '$Rev$'
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    20
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    21
" }}}
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    22
" Hook up the functions to the user supplied key bindings. {{{
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    23
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    24
if exists( 'g:glimpseKey' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    25
	execute 'map ' . g:glimpseKey . ' :call <SID>Glimpse()<CR>'
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    26
endif
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    27
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    28
if exists( 'g:glimpseindexKey' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    29
	execute 'map ' . g:glimpseindexKey . ' :call <SID>GlimpseIndex()<CR>'
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    30
endif
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    31
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    32
" }}}
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    33
" Defaults for misc settings {{{
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    34
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    35
if !exists( 'g:glimpseWindowResultSize' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    36
	let g:glimpseWindowResultSize = 15
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    37
endif
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    38
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    39
if !exists( 'g:glimpseFlags' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    40
	let g:glimpseFlags = "-iny"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    41
endif
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    42
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    43
if !exists( 'g:glimpseindexFlags' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    44
	let g:glimpseindexFlags = '-M 10 -B -o'
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    45
endif
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    46
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    47
if !exists( 'g:glimpseErrorFormat' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    48
	let g:glimpseErrorFormat = '%f: %l: %m'
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    49
endif
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    50
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    51
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    52
"}}}
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    53
" Commands {{{
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    54
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    55
command! -nargs=* Glimpse :call <SID>Glimpse( <f-args> )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    56
command! GlimpseIndex :call <SID>GlimpseIndex()
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    57
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    58
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    59
"}}}
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    60
" Glimpse( search_pattern, file_pattern ) {{{
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    61
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    62
" Run a glimpse search using a pattern from +search_pattern+ and an optional
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    63
" +file_pattern+ to limit the breadth of the search.  Places results into the
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    64
" quickfix window.
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    65
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    66
function! <SID>Glimpse( ... )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    67
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    68
	" parse command line opts
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    69
	"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    70
	let l:search_pattern = expand("<cword>")
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    71
	let l:file_pattern   = ''
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    72
	if exists( 'a:1' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    73
		let l:search_pattern = a:1
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    74
	endif
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    75
	if exists( 'a:2' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    76
		let l:file_pattern = a:2
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    77
	endif
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    78
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    79
	" everything is based on the cwd so results are relevant to the
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    80
	" current project.
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    81
	"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    82
	let l:cwd = getcwd()
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    83
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    84
	" save the original error format, we want to play nice with others.
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    85
	"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    86
	let l:orig_errorformat = &errorformat
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    87
	execute 'set errorformat=' . escape( g:glimpseErrorFormat, ' ' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    88
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    89
	" execute the search.
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    90
	"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    91
	let l:cmd = "glimpse " . g:glimpseFlags . " -F '" . l:cwd . ";" . l:file_pattern . "' '" . l:search_pattern . "'"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    92
	let l:result_list = split( system( l:cmd ), "\n" )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    93
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    94
	" no results, escape now
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    95
	"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    96
	if ( empty(l:result_list) )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    97
		let l:no_match_msg = "No matches for '" . l:search_pattern . "'"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    98
		if ( l:file_pattern != '' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    99
			let l:no_match_msg = l:no_match_msg . " in files matching '" . l:file_pattern . "'"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   100
		endif
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   101
		call s:err( l:no_match_msg )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   102
		execute 'set errorformat=' . escape( l:orig_errorformat, ' ' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   103
		return
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   104
	endif
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   105
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   106
	" populate the quickfix window.
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   107
	"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   108
	let l:results = ''
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   109
	for l:result in l:result_list
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   110
		" 'pretty up' the glimpse output.
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   111
		let l:result = substitute( l:result, ('^' . l:cwd . '/'), '', '' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   112
		let l:result = substitute( l:result, '\(: \d\+:\)\s\+', '\=submatch(1) . " "', '' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   113
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   114
		let l:results = l:results . l:result . "\n"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   115
	endfor
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   116
	cgetexpr( l:results )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   117
	execute ':copen' . g:glimpseWindowResultSize
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   118
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   119
	" quick close
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   120
    nnoremap <silent> <buffer> q :ccl<CR>
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   121
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   122
	" reset error format to the original
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   123
	execute 'set errorformat=' . escape( l:orig_errorformat, ' ' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   124
endfunction
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   125
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   126
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   127
" }}}
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   128
" GlimpseReindex() {{{
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   129
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   130
" Update local indexes for the current working directory.
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   131
" Results of the index are placed into the location list. (:lopen to view.)
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   132
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   133
function! <SID>GlimpseIndex()
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   134
	let l:cmd = 'glimpseindex ' . g:glimpseindexFlags . ' -f .'
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   135
	let l:cwd = getcwd()
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   136
	silent! lgetexpr( system(l:cmd) )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   137
	call s:err( "Updated indexes for '" . l:cwd . "'" )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   138
endfunction
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   139
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   140
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   141
" }}}
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   142
" s:err( msg ) {{{
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   143
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   144
" Notify user in a consistent fashion.
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   145
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   146
function! s:err( msg )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   147
    echohl WarningMsg|echomsg 'glimpse: ' . a:msg|echohl None
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   148
endfunction " }}}
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   149