Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #5 from Shougo/master

import asynchronous feature from Shougo the unite author.
  • Loading branch information...
commit a63e09f44fdd4b7ca8caa943afbfe482bfc4ee18 2 parents 5f1ba43 + 820867a
@Sixeight authored
Showing with 219 additions and 56 deletions.
  1. +103 −56 autoload/unite/sources/grep.vim
  2. +116 −0 doc/unite-grep.txt
View
159 autoload/unite/sources/grep.vim
@@ -1,7 +1,8 @@
"=============================================================================
" FILE: grep.vim
-" AUTHOR: Tomohiro Nishimura <tomohiro68@gmail.com>
-" Last Modified: 19 Jan 2011.
+" Modified AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
+" Original AUTHOR: Tomohiro Nishimura <tomohiro68 at gmail.com>
+" Last Modified: 17 Jun 2011.
" License: MIT license {{{
" Permission is hereby granted, free of charge, to any person obtaining
" a copy of this software and associated documentation files (the
@@ -23,46 +24,37 @@
" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
" }}}
"=============================================================================
-"
-" Description:
-" You can grep with unite interactive.
-" And use grep action on file/buffer kind.
-"
-" Usage:
-" :Unite grep:target:-options -input=pattern
-" (:Unite grep:~/.vim/autoload/unite/sources:-iR -input=file)
-"
-" Special Target:
-" % : Current buffer name
-" # : Alternate buffer name
-" $buffers : All buffer names
-"
-" Setting Examples:
-" let g:unite_source_grep_default_opts = '-iRHn'
-"
-" TODO:
-" * show current target
-" * support ignore pattern
-" * the goal is general unix command source :)
-"
+
" Variables "{{{
call unite#util#set_default('g:unite_source_grep_command', 'grep')
call unite#util#set_default('g:unite_source_grep_default_opts', '-Hn')
+call unite#util#set_default('g:unite_source_grep_recursive_opt', '-R')
call unite#util#set_default('g:unite_source_grep_max_candidates', 100)
"}}}
" Actions "{{{
-let s:action_grep = {
- \ 'description': 'grep this',
+let s:action_grep_file = {
+ \ 'description': 'grep this files',
\ 'is_quit': 1,
\ 'is_invalidate_cache': 1,
\ 'is_selectable': 1,
\ }
-function! s:action_grep.func(candidates) "{{{
- call unite#start([insert(map(copy(a:candidates), 'v:val.action__path'), 'grep')])
+function! s:action_grep_file.func(candidates) "{{{
+ call unite#start([['grep', map(copy(a:candidates), 'v:val.action__path')]])
endfunction "}}}
-if executable(g:unite_source_grep_command)
- call unite#custom_action('file,buffer', 'grep', s:action_grep)
+
+let s:action_grep_directory = {
+ \ 'description': 'grep this directories',
+ \ 'is_quit': 1,
+ \ 'is_invalidate_cache': 1,
+ \ 'is_selectable': 1,
+ \ }
+function! s:action_grep_directory.func(candidates) "{{{
+ call unite#start([['grep', map(copy(a:candidates), 'v:val.action__directory'), g:unite_source_grep_recursive_opt]])
+endfunction "}}}
+if executable(g:unite_source_grep_command) && unite#util#has_vimproc()
+ call unite#custom_action('file,buffer', 'grep', s:action_grep_file)
+ call unite#custom_action('file,buffer', 'grep_directory', s:action_grep_directory)
endif
" }}}
@@ -73,7 +65,7 @@ function! unite#sources#grep#define() "{{{
return []
endif
- return executable('grep') ? s:grep_source : []
+ return executable(g:unite_source_grep_command) && unite#util#has_vimproc() ? s:grep_source : []
endfunction "}}}
let s:grep_source = {
@@ -84,44 +76,95 @@ let s:grep_source = {
function! s:grep_source.hooks.on_init(args, context) "{{{
let l:target = get(a:args, 0, '')
+ if type(l:target) != type([])
+ if l:target == ''
+ let l:target = input('Target: ', '**', 'file')
+ endif
- if get(a:args, 0, '') =~ '^-'
- let l:target = get(a:args, 1, '')
- endif
+ if l:target == '%' || l:target == '#'
+ let l:target = unite#util#escape_file_searching(bufname(l:target))
+ elseif l:target ==# '$buffers'
+ let l:target = join(map(filter(range(1, bufnr('$')), 'buflisted(v:val)'),
+ \ 'unite#util#escape_file_searching(bufname(v:val))'))
+ elseif l:target == '**'
+ " Optimized.
+ let l:target = '* ' . g:unite_source_grep_recursive_opt
+ endif
- if l:target == ''
- let l:target = input('Target: ', '', 'file')
+ let a:context.source__target = [l:target]
+ else
+ let a:context.source__target = l:target
endif
- if l:target == '%' || l:target == '#'
- let l:target = unite#util#escape_file_searching(bufname(l:target))
- elseif l:target ==# '$buffers'
- let l:target = join(map(filter(range(1, bufnr('$')), 'buflisted(v:val)'),
- \ 'unite#util#escape_file_searching(bufname(v:val))'))
+ let a:context.source__extra_opts = get(a:args, 1, '')
+
+ let a:context.source__input = get(a:args, 2, '')
+ if a:context.source__input == ''
+ let a:context.source__input = input('Pattern: ')
endif
- let a:context.source__target = l:target
+ call unite#print_message('[grep] Target: ' . join(a:context.source__target))
+ call unite#print_message('[grep] Pattern: ' . a:context.source__input)
endfunction"}}}
function! s:grep_source.gather_candidates(args, context) "{{{
- if a:context.source__target == ''
+ if empty(a:context.source__target)
+ \ || a:context.source__input == ''
+ let a:context.is_async = 0
+ call unite#print_message('[grep] Completed.')
return []
endif
- let l:input = input('Pattern: ')
-
- let l:extra_opts = get(a:args, 0, '') =~ '^-' ?
- \ a:args[0] : get(a:args, 1, '')
-
- let l:candidates = map(filter(split(
- \ unite#util#system(printf(
- \ '%s %s %s %s %s',
+ let l:cmdline = printf('%s %s %s %s %s',
\ g:unite_source_grep_command,
\ g:unite_source_grep_default_opts,
- \ l:input,
- \ a:context.source__target,
- \ l:extra_opts)),
- \ "\n"), 'v:val =~ "^.\\+:.\\+:.\\+$"'), '[v:val, split(v:val[2:], ":")]')
+ \ a:context.source__input,
+ \ join(a:context.source__target),
+ \ a:context.source__extra_opts)
+ call unite#print_message('[grep] Command-line: ' . l:cmdline)
+ let a:context.source__proc = vimproc#pgroup_open(l:cmdline)
+ " let a:context.source__proc = vimproc#popen3(l:cmdline)
+
+ " Close handles.
+ call a:context.source__proc.stdin.close()
+ call a:context.source__proc.stderr.close()
+
+ return []
+endfunction "}}}
+
+function! s:grep_source.async_gather_candidates(args, context) "{{{
+ let l:stdout = a:context.source__proc.stdout
+ if l:stdout.eof
+ " Disable async.
+ call unite#print_message('[grep] Completed.')
+ let a:context.is_async = 0
+ endif
+
+ let l:result = []
+ if has('reltime') && has('float')
+ let l:time = reltime()
+ while str2float(reltimestr(reltime(l:time))) < 0.2
+ \ && !l:stdout.eof
+ let l:output = l:stdout.read_line()
+ if l:output != ''
+ call add(l:result, l:output)
+ endif
+ endwhile
+ else
+ let i = 100
+ while 0 < i && !l:stdout.eof
+ let l:output = l:stdout.read_line()
+ if l:output != ''
+ call add(l:result, l:output)
+ endif
+
+ let i -= 1
+ endwhile
+ endif
+
+ let l:candidates = map(filter(l:result,
+ \ 'v:val =~ "^.\\+:.\\+:.\\+$"'),
+ \ '[v:val, split(v:val[2:], ":")]')
return map(l:candidates,
\ '{
@@ -130,8 +173,12 @@ function! s:grep_source.gather_candidates(args, context) "{{{
\ "kind": "jump_list",
\ "action__path": v:val[0][:1].v:val[1][0],
\ "action__line": v:val[1][1],
- \ "action__pattern": "^".unite#util#escape_pattern(join(v:val[1][2:], ":"))."$",
+ \ "action__text": join(v:val[1][2:], ":"),
\ }')
endfunction "}}}
+function! s:grep_source.on_close(args, context) "{{{
+ call a:context.source__proc.close()
+endfunction "}}}
+
" vim: foldmethod=marker
View
116 doc/unite-grep.txt
@@ -0,0 +1,116 @@
+*unite-grep.txt* Unite grep source
+
+Version: 1.0
+Modified Author: Shougo Matsushita <Shougo.Matsu at gmail.com>
+Original Author: Tomohiro Nishimura <tomohiro68 at gmail.com>
+License: MIT license {{{
+ Permission is hereby granted, free of charge, to any person obtaining
+ a copy of this software and associated documentation files (the
+ "Software"), to deal in the Software without restriction, including
+ without limitation the rights to use, copy, modify, merge, publish,
+ distribute, sublicense, and/or sell copies of the Software, and to
+ permit persons to whom the Software is furnished to do so, subject to
+ the following conditions:
+ The above copyright notice and this permission notice shall be included
+ in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+}}}
+
+CONTENTS *unite-grep-contents*
+
+Introduction |unite-grep-introduction|
+Usage |unite-grep-usage|
+Interface |unite-grep-interface|
+ Variables |unite-grep-variables|
+ Actions |unite-grep-actions|
+ToDo |unite-grep-todo|
+Changelog |unite-grep-changelog|
+
+==============================================================================
+INTRODUCTION *unite-grep-introduction*
+
+Note: This plugin requre vimproc. Please install.
+http://github.com/Shougo/vimproc/tree/master
+
+==============================================================================
+USAGE *unite-grep-usage*
+
+:Unite grep:target:-options:pattern
+
+Example:
+>
+ :Unite grep:~/.vim/autoload/unite/sources:-iR:file
+<
+
+Special Target:
+% : Current buffer name
+# : Alternate buffer name
+$buffers : All buffer names
+
+Setting Examples:
+let g:unite_source_grep_default_opts = '-iRHn'
+
+==============================================================================
+INTERFACE *unite-grep-interface*
+
+------------------------------------------------------------------------------
+VARIABLES *unite-grep-variables*
+
+g:unite_source_grep_command *g:unite_source_grep_command*
+ Set grep command.
+
+ Default value is "grep".
+
+g:unite_source_grep_recursive_opt *g:unite_source_grep_recursive_opt*
+ Set grep recursive option.
+
+ Default value is "-R".
+
+g:unite_source_grep_default_opts *g:unite_source_grep_default_opts*
+ Set grep default options.
+ Note: grep output must be this pattern.
+ filename:number:pattern
+
+ Default value is "-Hn".
+
+g:unite_source_grep_max_candidates *g:unite_source_grep_max_candidates*
+ Set max candidates number.
+
+ Default value is 100.
+
+------------------------------------------------------------------------------
+ACTIONS *unite-grep-actions*
+
+Actions of file and buffer kinds
+ grep grep this files
+ grep_directory grep this directories
+
+==============================================================================
+TODO *unite-grep-todo*
+
+* support ignore pattern
+* the goal is general unix command source :)
+
+==============================================================================
+CHANGELOG *unite-grep-changelog*
+
+2011-06-17
+- Fixed unite actions.
+- Added g:unite_source_grep_recursive_opt.
+
+2011-06-13
+- Improved perfomance.
+
+2011-06-10
+- Added documentation.
+- Ver.1.0
+
+==============================================================================
+vim:tw=78:ts=8:ft=help:norl:noet:fen:fdl=0:
Please sign in to comment.
Something went wrong with that request. Please try again.