glimpse/plugin/glimpse.vim
author Mahlon E. Smith <mahlon@martini.nu>
Sat, 16 Jan 2016 11:39:19 -0800
branchvim-stuff
changeset 29 cc3094023778
parent 10 a8f9271a7cab
permissions -rw-r--r--
Split vim projects into separate repos.
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
"
10
a8f9271a7cab * Remove committed keyword expansions.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    11
" Here's a bash function that's nice too!
a8f9271a7cab * Remove committed keyword expansions.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    12
" function g() { command glimpse -winO -F `pwd` $1 | sed 's|^`pwd`/||'; }
a8f9271a7cab * Remove committed keyword expansions.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    13
"
a8f9271a7cab * Remove committed keyword expansions.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
    14
" And a tcsh alias, while we're at it!
1
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    15
" alias g 'glimpse -winO -F `pwd` \!:1 | sed -e "s|^`pwd`/||"'
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    16
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    17
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    18
" Loaded check {{{
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    19
if exists( 'g:glimpse_loaded' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    20
    finish
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    21
endif
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    22
let g:glimpse_loaded = '$Rev$'
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    23
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    24
" }}}
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    25
" Hook up the functions to the user supplied key bindings. {{{
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    26
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    27
if exists( 'g:glimpseKey' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    28
	execute 'map ' . g:glimpseKey . ' :call <SID>Glimpse()<CR>'
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    29
endif
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    30
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    31
if exists( 'g:glimpseindexKey' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    32
	execute 'map ' . g:glimpseindexKey . ' :call <SID>GlimpseIndex()<CR>'
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    33
endif
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    34
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    35
" }}}
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    36
" Defaults for misc settings {{{
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    37
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    38
if !exists( 'g:glimpseWindowResultSize' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    39
	let g:glimpseWindowResultSize = 15
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    40
endif
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    41
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    42
if !exists( 'g:glimpseFlags' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    43
	let g:glimpseFlags = "-iny"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    44
endif
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    45
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    46
if !exists( 'g:glimpseindexFlags' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    47
	let g:glimpseindexFlags = '-M 10 -B -o'
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    48
endif
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    49
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    50
if !exists( 'g:glimpseErrorFormat' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    51
	let g:glimpseErrorFormat = '%f: %l: %m'
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    52
endif
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    53
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    54
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    55
"}}}
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    56
" Commands {{{
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    57
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    58
command! -nargs=* Glimpse :call <SID>Glimpse( <f-args> )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    59
command! GlimpseIndex :call <SID>GlimpseIndex()
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    60
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    61
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    62
"}}}
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    63
" Glimpse( search_pattern, file_pattern ) {{{
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    64
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    65
" Run a glimpse search using a pattern from +search_pattern+ and an optional
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    66
" +file_pattern+ to limit the breadth of the search.  Places results into the
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    67
" quickfix window.
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    68
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    69
function! <SID>Glimpse( ... )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    70
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    71
	" parse command line opts
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    72
	"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    73
	let l:search_pattern = expand("<cword>")
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    74
	let l:file_pattern   = ''
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    75
	if exists( 'a:1' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    76
		let l:search_pattern = a:1
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
	if exists( 'a:2' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    79
		let l:file_pattern = a:2
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    80
	endif
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    81
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    82
	" everything is based on the cwd so results are relevant to the
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    83
	" current project.
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    84
	"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    85
	let l:cwd = getcwd()
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    86
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    87
	" save the original error format, we want to play nice with others.
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    88
	"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    89
	let l:orig_errorformat = &errorformat
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    90
	execute 'set errorformat=' . escape( g:glimpseErrorFormat, ' ' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    91
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    92
	" execute the search.
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
    93
	"
4
4ffc0380b228 Fix empty boolean error (32 chars) on -F flag.
mahlon
parents: 1
diff changeset
    94
	let l:cmd = "glimpse " . g:glimpseFlags . " -F '" . l:cwd
4ffc0380b228 Fix empty boolean error (32 chars) on -F flag.
mahlon
parents: 1
diff changeset
    95
	if ( l:file_pattern != '' )
4ffc0380b228 Fix empty boolean error (32 chars) on -F flag.
mahlon
parents: 1
diff changeset
    96
		let l:cmd = l:cmd . ";" . l:file_pattern
4ffc0380b228 Fix empty boolean error (32 chars) on -F flag.
mahlon
parents: 1
diff changeset
    97
	endif
4ffc0380b228 Fix empty boolean error (32 chars) on -F flag.
mahlon
parents: 1
diff changeset
    98
	let l:cmd = l:cmd . "' '" . l:search_pattern . "'"
4ffc0380b228 Fix empty boolean error (32 chars) on -F flag.
mahlon
parents: 1
diff changeset
    99
1
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   100
	let l:result_list = split( system( l:cmd ), "\n" )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   101
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   102
	" no results, escape now
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   103
	"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   104
	if ( empty(l:result_list) )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   105
		let l:no_match_msg = "No matches for '" . l:search_pattern . "'"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   106
		if ( l:file_pattern != '' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   107
			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
   108
		endif
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   109
		call s:err( l:no_match_msg )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   110
		execute 'set errorformat=' . escape( l:orig_errorformat, ' ' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   111
		return
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   112
	endif
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   113
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   114
	" populate the quickfix window.
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   115
	"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   116
	let l:results = ''
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   117
	for l:result in l:result_list
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   118
		" 'pretty up' the glimpse output.
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   119
		let l:result = substitute( l:result, ('^' . l:cwd . '/'), '', '' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   120
		let l:result = substitute( l:result, '\(: \d\+:\)\s\+', '\=submatch(1) . " "', '' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   121
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   122
		let l:results = l:results . l:result . "\n"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   123
	endfor
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   124
	cgetexpr( l:results )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   125
	execute ':copen' . g:glimpseWindowResultSize
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   126
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   127
	" quick close
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   128
    nnoremap <silent> <buffer> q :ccl<CR>
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   129
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   130
	" reset error format to the original
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   131
	execute 'set errorformat=' . escape( l:orig_errorformat, ' ' )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   132
endfunction
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   133
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   134
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   135
" }}}
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   136
" GlimpseReindex() {{{
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   137
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   138
" Update local indexes for the current working directory.
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   139
" Results of the index are placed into the location list. (:lopen to view.)
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   140
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   141
function! <SID>GlimpseIndex()
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   142
	let l:cmd = 'glimpseindex ' . g:glimpseindexFlags . ' -f .'
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   143
	let l:cwd = getcwd()
10
a8f9271a7cab * Remove committed keyword expansions.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   144
	let $LC_ALL = 'C'
a8f9271a7cab * Remove committed keyword expansions.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   145
	let l:index_output = system(l:cmd)
a8f9271a7cab * Remove committed keyword expansions.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   146
	let l:index_exit = v:shell_error
1
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   147
	call s:err( "Updated indexes for '" . l:cwd . "'" )
10
a8f9271a7cab * Remove committed keyword expansions.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   148
	if l:index_exit != 0
a8f9271a7cab * Remove committed keyword expansions.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   149
		call s:err( "Uh oh, " . l:cmd . " exited with " . l:index_exit. "!  Output follows:" )
a8f9271a7cab * Remove committed keyword expansions.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   150
		call s:err( l:index_output )
a8f9271a7cab * Remove committed keyword expansions.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   151
	endif
1
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   152
endfunction
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   153
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   154
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   155
" }}}
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   156
" s:err( msg ) {{{
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   157
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   158
" Notify user in a consistent fashion.
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   159
"
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   160
function! s:err( msg )
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   161
    echohl WarningMsg|echomsg 'glimpse: ' . a:msg|echohl None
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   162
endfunction " }}}
fd505fd00e63 Initial commit of the VimGlimpse plugin.
mahlon
parents:
diff changeset
   163