glimpse/plugin/glimpse.vim
author mahlon
Sat, 28 Mar 2009 02:57:39 +0000
branchvim-stuff
changeset 4 4ffc0380b228
parent 1 fd505fd00e63
child 10 a8f9271a7cab
permissions -rw-r--r--
Fix empty boolean error (32 chars) on -F flag.
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
	"
4
4ffc0380b228 Fix empty boolean error (32 chars) on -F flag.
mahlon
parents: 1
diff changeset
    91
	let l:cmd = "glimpse " . g:glimpseFlags . " -F '" . l:cwd
4ffc0380b228 Fix empty boolean error (32 chars) on -F flag.
mahlon
parents: 1
diff changeset
    92
	if ( l:file_pattern != '' )
4ffc0380b228 Fix empty boolean error (32 chars) on -F flag.
mahlon
parents: 1
diff changeset
    93
		let l:cmd = l:cmd . ";" . l:file_pattern
4ffc0380b228 Fix empty boolean error (32 chars) on -F flag.
mahlon
parents: 1
diff changeset
    94
	endif
4ffc0380b228 Fix empty boolean error (32 chars) on -F flag.
mahlon
parents: 1
diff changeset
    95
	let l:cmd = l:cmd . "' '" . l:search_pattern . "'"
4ffc0380b228 Fix empty boolean error (32 chars) on -F flag.
mahlon
parents: 1
diff changeset
    96
1
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    97
	let l:result_list = split( system( l:cmd ), "\n" )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    98
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    99
	" no results, escape now
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   100
	"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   101
	if ( empty(l:result_list) )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   102
		let l:no_match_msg = "No matches for '" . l:search_pattern . "'"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   103
		if ( l:file_pattern != '' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   104
			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
   105
		endif
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   106
		call s:err( l:no_match_msg )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   107
		execute 'set errorformat=' . escape( l:orig_errorformat, ' ' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   108
		return
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   109
	endif
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   110
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   111
	" populate the quickfix window.
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   112
	"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   113
	let l:results = ''
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   114
	for l:result in l:result_list
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   115
		" 'pretty up' the glimpse output.
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   116
		let l:result = substitute( l:result, ('^' . l:cwd . '/'), '', '' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   117
		let l:result = substitute( l:result, '\(: \d\+:\)\s\+', '\=submatch(1) . " "', '' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   118
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   119
		let l:results = l:results . l:result . "\n"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   120
	endfor
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   121
	cgetexpr( l:results )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   122
	execute ':copen' . g:glimpseWindowResultSize
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   123
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   124
	" quick close
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   125
    nnoremap <silent> <buffer> q :ccl<CR>
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   126
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   127
	" reset error format to the original
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   128
	execute 'set errorformat=' . escape( l:orig_errorformat, ' ' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   129
endfunction
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   130
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   131
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   132
" }}}
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   133
" GlimpseReindex() {{{
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   134
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   135
" Update local indexes for the current working directory.
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   136
" Results of the index are placed into the location list. (:lopen to view.)
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   137
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   138
function! <SID>GlimpseIndex()
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   139
	let l:cmd = 'glimpseindex ' . g:glimpseindexFlags . ' -f .'
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   140
	let l:cwd = getcwd()
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   141
	silent! lgetexpr( system(l:cmd) )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   142
	call s:err( "Updated indexes for '" . l:cwd . "'" )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   143
endfunction
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   144
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   145
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   146
" }}}
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   147
" s:err( msg ) {{{
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   148
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   149
" Notify user in a consistent fashion.
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   150
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   151
function! s:err( msg )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   152
    echohl WarningMsg|echomsg 'glimpse: ' . a:msg|echohl None
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   153
endfunction " }}}
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   154