From efdf81b93665f15ea17ba7858e921663139e014c Mon Sep 17 00:00:00 2001 From: "R. Steve McKown" Date: Sun, 17 Jan 2016 15:25:59 -0700 Subject: [PATCH] Unite! integration for file selection Provide the mkbfiles source for Unite! --- autoload/unite/kinds/mkbfiles.vim | 19 ++++++ autoload/unite/sources/mkbfiles.vim | 27 +++++++++ plugin/mkbuild.vim | 90 +++++++++++++++++++---------- 3 files changed, 104 insertions(+), 32 deletions(-) create mode 100644 autoload/unite/kinds/mkbfiles.vim create mode 100644 autoload/unite/sources/mkbfiles.vim diff --git a/autoload/unite/kinds/mkbfiles.vim b/autoload/unite/kinds/mkbfiles.vim new file mode 100644 index 0000000..e7c2b4c --- /dev/null +++ b/autoload/unite/kinds/mkbfiles.vim @@ -0,0 +1,19 @@ +let s:kind = { + \ 'name': 'mkbfiles', + \ 'default_action': 'execute', + \ 'action_table': {}, + \ 'parents': [], + \ 'description': 'files used in mkbuild project' + \ } + +let s:kind.action_table.execute = { 'is_selectable': 1 } + +function! s:kind.action_table.execute.func(candidates) + if len(a:candidates) == 1 + execute "e" a:candidates[0].word + endif +endfunction + +function! unite#kinds#mkbfiles#define() + return s:kind +endfunction diff --git a/autoload/unite/sources/mkbfiles.vim b/autoload/unite/sources/mkbfiles.vim new file mode 100644 index 0000000..d69f8e3 --- /dev/null +++ b/autoload/unite/sources/mkbfiles.vim @@ -0,0 +1,27 @@ +" unite source: mkbuild filelist +" Version: 0.0.1 +" Author : R. Steve McKown +" License: MIT License + +let s:save_cpo = &cpo +set cpo&vim + +let s:source = { 'name': 'mkbfiles' } + +function! s:source.gather_candidates(args, context) + let sourcecmd = 'find . -type f' + let filelist = split(mkbuild#filelist(), '\n') + + return map(filelist, '{ + \ "word": v:val, + \ "source": "mkbfiles", + \ "kind": "mkbfiles", + \ }') +endfunction + +function! unite#sources#mkbfiles#define() + return s:source +endfunction + +let &cpo = s:save_cpo +unlet s:save_cpo diff --git a/plugin/mkbuild.vim b/plugin/mkbuild.vim index fd92caa..eb3fd2b 100644 --- a/plugin/mkbuild.vim +++ b/plugin/mkbuild.vim @@ -85,6 +85,61 @@ function! s:filels(filelist) return "(cat " . a:filelist . ")" endfunction +" Generate a command to generate a list of files. Return a dictionary +" containing two entries: +" source - The source of the files (generated by executing the sourcecmd) +" sourcecmd - The system command to use to generate the list of files +" +" The source command returned will output files from one of 4 locations, +" searched in priority order: +" 1. All filelist files found in the same directory(ies) vim looks for tags +" files. Files are extracted from the contents of these files. +" 2. Any tags files found in &tags. The file is parsed to get filenames. +" 3. The tracked files in the current git repository, and/or the tracked files +" in those valid git repositories named in the 'extrefs' file, if present. +" 4. Perform a 'find . -type f' in the current directory. +function! mkbuild#flcmd() + let files = s:filelists() + if empty(files) + let tags = s:tagfiles() + if empty(tags) + let gitdirs = s:gitdirs() + if empty(gitdirs) + let source = 'find' + let sourcecmd = 'find . -type f' + else " gitdirs + let source = 'git|extrefs' + let sourcecmd = '(true' + for g in gitdirs + let sourcecmd = sourcecmd . '; ' . s:gitls(g) + endfor + let sourcecmd = sourcecmd . ')' + endif + else " tags + let source = 'tags' + let sourcecmd = '(true' + for t in tags + let sourcecmd = sourcecmd . '; ' . s:tagls(t) + endfor + let sourcecmd = sourcecmd . ')' + endif + else " filelists + let source = 'filelists' + let sourcecmd = '(true' + for f in files + let sourcecmd = sourcecmd . '; ' . s:filels(f) + endfor + let sourcecmd = sourcecmd . ')' + endif + return { 'source': source, 'sourcecmd': sourcecmd } +endfunction + +" Generate a list of files using mkbuild#flcmd() +function! mkbuild#filelist() + let flcmdres = mkbuild#flcmd() + return s:chompsys(flcmdres['sourcecmd'] . ' | cat') +endfunction + " Use dmenu to select a file to execute with command 'cmd'. If no file is " selected, no action is taken. The list of files presented to dmenu comes " from one of four places, in the following priority order: @@ -98,38 +153,9 @@ function! mkbuild#DmenuOpen(cmd) if empty(system('which dmenu')) echo "Dmenu is not installed" else - let files = s:filelists() - if empty(files) - let tags = s:tagfiles() - if empty(tags) - let gitdirs = s:gitdirs() - if empty(gitdirs) - let source = 'find' - let sourcecmd = 'find . -type f' - else " gitdirs - let source = 'git|extrefs' - let sourcecmd = '(true' - for g in gitdirs - let sourcecmd = sourcecmd . '; ' . s:gitls(g) - endfor - let sourcecmd = sourcecmd . ')' - endif - else " tags - let source = 'tags' - let sourcecmd = '(true' - for t in tags - let sourcecmd = sourcecmd . '; ' . s:tagls(t) - endfor - let sourcecmd = sourcecmd . ')' - endif - else " filelists - let source = 'filelists' - let sourcecmd = '(true' - for f in files - let sourcecmd = sourcecmd . '; ' . s:filels(f) - endfor - let sourcecmd = sourcecmd . ')' - endif + let flcmdres = mkbuild#flcmd() + let source = flcmdres['source'] + let sourcecmd = flcmdres['sourcecmd'] let fname = s:chompsys(sourcecmd . ' | dmenu -i -l 20 -p "' . source . '(' . \ a:cmd . ')"') if empty(fname) -- 2.39.2