diff --git a/vim_plugins_src/grep.vim b/vim_plugins_src/grep.vim new file mode 100644 index 00000000..b9e8f329 --- /dev/null +++ b/vim_plugins_src/grep.vim @@ -0,0 +1,854 @@ +" File: grep.vim +" Author: Yegappan Lakshmanan (yegappan AT yahoo DOT com) +" Version: 1.9 +" Last Modified: September 10, 2007 +" +" Overview +" -------- +" The grep plugin integrates the grep, fgrep, egrep, and agrep tools with +" Vim and allows you to search for a pattern in one or more files and jump +" to them. +" +" To use this plugin, you need the grep, fgrep, egrep, agrep, find and +" xargs utilities. These tools are present in most of the Unix installations. +" For MS-Windows systems, you can download the GNU grep and find utilities +" from the following sites: +" +" http://gnuwin32.sourceforge.net/packages/grep.htm +" http://gnuwin32.sourceforge.net/packages/findutils.htm +" +" Installation +" ------------ +" 1. Copy the grep.vim file to the $HOME/.vim/plugin or $HOME/vimfiles/plugin +" or $VIM/vimfiles/plugin directory. +" Refer to the following Vim help topics for more information about Vim +" plugins: +" :help add-plugin +" :help add-global-plugin +" :help runtimepath +" 2. If the grep executables are not already present in one of the directories +" in the PATH environment variable, then set the Grep_Path and other _Path +" variables to point to the location of the grep utilites in the .vimrc +" file. +" 3. Restart Vim. +" 4. You can now use the ":Grep" and other commands to search for patterns in +" files. +" +" Usage +" ----- +" The grep.vim plugin introduces the following Vim commands: +" +" :Grep - Search for the specified pattern in the specified files +" :GrepAdd - Same as ":Grep" but adds the results to the current results +" :Rgrep - Run recursive grep +" :RgrepAdd - Same as ":Rgrep" but adds the results to the current results +" :GrepBuffer - Search for a pattern on all open buffers +" :GrepBufferAdd - Same as ":GrepBuffer" but adds the results to the current +" results +" :Bgrep - Same as :GrepBuffer +" :BgrepAdd - Same as :GrepBufferAdd +" :GrepArgs - Search for a pattern on all the Vim argument +" filenames (:args) +" :GrepArgsAdd - Same as ":GrepArgs" but adds the results to the current +" results +" :Fgrep - Run fgrep +" :FgrepAdd - Same as ":Fgrep" but adds the results to the current +" results +" :Rfgrep - Run recursive fgrep +" :RfgrepAdd - Same as ":Rfgrep" but adds the results to the current +" results +" :Egrep - Run egrep +" :EgrepAdd - Same as ":Egrep" but adds the results to the current +" results +" :Regrep - Run recursive egrep +" :RegrepAdd - Same as ":Regrep" but adds the results to the current +" results +" :Agrep - Run agrep +" :AgrepAdd - Same as ":Agrep" but adds the results to the current +" results +" :Ragrep - Run recursive agrep +" :RagrepAdd - Same as ":Ragrep" but adds the results to the current +" results +" +" The above commands can be invoked like this: +" +" :Grep [] [ []] +" :Rgrep [] [ []] +" :Fgrep [] [ []] +" :Rfgrep [] [ []] +" :Egrep [] [ []] +" :Regrep [] [ []] +" :Agrep [] [ []] +" :Ragrep [] [ []] +" +" :GrepAdd [] [ []] +" :RgrepAdd [] [ []] +" :FgrepAdd [] [ []] +" :RfgrepAdd [] [ []] +" :EgrepAdd [] [ []] +" :RegrepAdd [] [ []] +" :AgrepAdd [] [ []] +" :RagrepAdd [] [ []] +" +" :GrepBuffer [] [] +" :Bgrep [] [] +" :GrepArgs [] [] +" +" :GrepBufferAdd [] [] +" :BgrepAdd [] [] +" :GrepArgsAdd [] [] +" +" In the above commands, all the arguments are optional. +" +" You can specify grep options like -i (ignore case) or -w (search for a word) +" to the above commands. If the are not specified, then the +" default grep options specified by the variable Grep_Default_Options is +" used. +" +" You can specify the grep pattern to search as an argument to the above +" commands. If the is not specified, then you will be +" prompted to enter a search pattern. By default, the keyword under the cursor +" is displayed for the search pattern prompt. You can accept the default or +" modify it. +" +" The search pattern is automatically enclosed by the character specified in +" the Grep_Shell_Quote_Char variable. You should not enclose the search +" pattern with a shell escape character. +" +" If you want to specify a search pattern with space characters or a +" multi-word pattern, then you should use the Grep command pattern input +" prompt to supply the pattern. +" +" You can specify one or more file names (or file patterns) to the above +" commands. If the are not specified, then you will be prompted +" to enter file names. By default, the pattern specified by the +" Grep_Default_Filelist variable is used. To specify the file name(s) as an +" argument to the above commands, you have to specify the search pattern also. +" +" When you enter only the command name, you will be prompted to enter the +" search pattern and the files in which to search for the pattern. By default, +" the keyword under the cursor is displayed for the search pattern prompt. +" Depending on the command, you may prompted for additional parameters like +" the directories to search for the pattern. +" +" You can retrieve previously entered values for the Grep prompts using the up +" and down arrow keys. You can cancel the command by pressing the escape key. +" You can use CTRL-U to erase the default shown for the prompt and CTRL-W to +" erase the previous word in the prompt. For more information about editing +" the prompt, read ':help cmdline-editing' Vim help topic. +" +" After invoking any of the grep commands, you can cancel the command, when +" you are prompted for a search pattern or file names or a directory by +" pressing the key. You cannot cancel (or kill) the +" grep/fgrep/egrep/agrep commands after the external command is invoked. +" +" The GrepAdd, RgrepAdd and other *Add commands append the search output to +" the current search output. This is useful if you want to see the search +" results for multiple patterns at the same time. These commands are available +" only in Vim version 7.0 and above. +" +" You can map a key to invoke any of the above commands. For example, the +" following map invokes the :Grep command to search for the keyword under the +" cursor: +" +" nnoremap :Grep +" +" The output of the grep command will be listed in the Vim quickfix window. +" 1. You can select a line in the quickfix window and press or double +" click on a match to jump to that line. +" 2. You can use the ":cnext" and ":cprev" commands to the jump to the next or +" previous output line. +" 3. You can use the ":colder" and ":cnewer" commands to go between multiple +" grep quickfix output windows. +" 4. The quickfix window need not be opened always to use the grep output. +" You can close the quickfix window and use the quickfix commands to jump +" to the grep matches. Use the ":copen" command to open the quickfix +" window again. +" +" For more information about other quickfix commands read ":help quickfix" +" +" When using GUI Vim, the Tools->Search menu item with a few sub-menu items is +" created for few variations of the search command. +" +" Configuration +" ------------- +" By changing the following variables you can configure the behavior of this +" plugin. Set the following variables in your .vimrc file using the 'let' +" command. +" +" The 'Grep_Path' variable is used to locate the grep utility. By default, +" this is set to grep. You can change this using the let command: +" +" :let Grep_Path = 'd:\tools\grep.exe' +" +" The 'Fgrep_Path' variable is used to locate the fgrep utility. By default, +" this is set to fgrep. You can change this using the let command: +" +" :let Fgrep_Path = 'd:\tools\fgrep.exe' +" +" The 'Egrep_Path' variable is used to locate the egrep utility. By default, +" this is set to egrep. You can change this using the let command: +" +" :let Egrep_Path = 'd:\tools\egrep.exe' +" +" The 'Agrep_Path' variable is used to locate the agrep utility. By default, +" this is set to agrep. You can change this using the let command: +" +" :let Agrep_Path = 'd:\tools\agrep.exe' +" +" The 'Grep_Find_Path' variable is used to locate the find utility. By +" default, this is set to d:\tools\find.exe. You can change this using the let +" command: +" +" :let Grep_Find_Path = 'd:\tools\find.exe' +" +" The 'Grep_Xargs_Path' variable is used to locate the xargs utility. By +" default, this is set to xargs. You can change this using the let +" command: +" +" :let Grep_Xargs_Path = 'd:\tools\xargs.exe' +" +" When running any one of the Grep commands, you will be prompted for the +" files in which to search for the pattern. The 'Grep_Default_Filelist' +" variable is used to specify to default for this prompt. By default, this +" variable is set to '*'. You can specify multiple matching patterns separated +" by spaces. You can change this settings using the let command: +" +" :let Grep_Default_Filelist = '*.[chS]' +" :let Grep_Default_Filelist = '*.c *.cpp *.asm' +" +" The 'Grep_Default_Options' is used to pass default command line options to +" the grep/fgrep/egrep/agrep utilities. By default, this is set to an empty +" string. You can change this using the let command: +" +" :let Grep_Default_Options = '-i' +" +" The 'Grep_Skip_Dirs' variable specifies the list of directories to skip +" while doing recursive searches. By default, this is set to 'RCS CVS SCCS'. +" You can change this using the let command: +" +" :let Grep_Skip_Dirs = 'dir1 dir2 dir3' +" +" The 'Grep_Skip_Files' variable specifies the list of files to skip while +" doing recursive searches. By default, this is set to '*~ *,v s.*'. You can +" change this using the let command: +" +" :let Grep_Skip_Files = '*.bak *~' +" +" By default, when you invoke the Grep commands the quickfix window will be +" opened with the grep output. You can disable opening the quickfix window, +" by setting the 'Grep_OpenQuickfixWindow' variable to zero: +" +" :let Grep_OpenQuickfixWindow = 0 +" +" You can manually open the quickfix window using the :cwindow command. +" +" By default, for recursive searches, the 'find' and 'xargs' utilities are +" used. If you don't have the 'xargs' utility or don't want to use the +" 'xargs' utility, " then you can set the 'Grep_Find_Use_Xargs' variable to +" zero. If this is set to zero, then only the 'find' utility is used for +" recursive searches: +" +" :let Grep_Find_Use_Xargs = 0 +" +" To handle file names with space characters in them, the xargs utility +" is invoked with the '--null' argument. If the xargs utility in your system +" doesn't accept the '--null' argument, then you can change the +" Grep_Xargs_Options variable. For example, to use the '--print0' xargs +" argument, you can use the following command: +" +" :let Grep_Xargs_Options = '--print0' +" +" The Grep_Cygwin_Find variable should be set to 1, if you are using the find +" utility from the cygwin package. This setting is used to handle the +" difference between the backslash and forward slash path separators. +" +" :let Grep_Cygwin_Find = 1 +" +" The 'Grep_Null_Device' variable specifies the name of the null device to +" pass to the grep commands. This is needed to force the grep commands to +" print the name of the file in which a match is found, if only one filename +" is specified. For Unix systems, this is set to /dev/null and for MS-Windows +" systems, this is set to NUL. You can modify this by using the let command: +" +" :let Grep_Null_Device = '/dev/null' +" +" The 'Grep_Shell_Quote_Char' variable specifies the quote character to use +" for protecting patterns from being interpreted by the shell. For Unix +" systems, this is set to "'" and for MS-Window systems, this is set to an +" empty string. You can change this using the let command: +" +" :let Grep_Shell_Quote_Char = "'" +" +" The 'Grep_Shell_Escape_Char' variable specifies the escape character to use +" for protecting special characters from being interpreted by the shell. For +" Unix systems, this is set to '\' and for MS-Window systems, this is set to +" an empty string. You can change this using the let command: +" +" :let Grep_Shell_Escape_Char = "'" +" +" --------------------- Do not modify after this line --------------------- +if exists("loaded_grep") + finish +endif +let loaded_grep = 1 + +" Line continuation used here +let s:cpo_save = &cpo +set cpo&vim + +" Location of the grep utility +if !exists("Grep_Path") + let Grep_Path = 'grep' +endif + +" Location of the fgrep utility +if !exists("Fgrep_Path") + let Fgrep_Path = 'fgrep' +endif + +" Location of the egrep utility +if !exists("Egrep_Path") + let Egrep_Path = 'egrep' +endif + +" Location of the agrep utility +if !exists("Agrep_Path") + let Agrep_Path = 'agrep' +endif + +" Location of the find utility +if !exists("Grep_Find_Path") + let Grep_Find_Path = 'find' +endif + +" Location of the xargs utility +if !exists("Grep_Xargs_Path") + let Grep_Xargs_Path = 'xargs' +endif + +" Open the Grep output window. Set this variable to zero, to not open +" the Grep output window by default. You can open it manually by using +" the :cwindow command. +if !exists("Grep_OpenQuickfixWindow") + let Grep_OpenQuickfixWindow = 1 +endif + +" Default grep file list +if !exists("Grep_Default_Filelist") + let Grep_Default_Filelist = '*' +endif + +" Default grep options +if !exists("Grep_Default_Options") + let Grep_Default_Options = '' +endif + +" Use the 'xargs' utility in combination with the 'find' utility. Set this +" to zero to not use the xargs utility. +if !exists("Grep_Find_Use_Xargs") + let Grep_Find_Use_Xargs = 1 +endif + +" The command-line arguments to supply to the xargs utility +if !exists('Grep_Xargs_Options') + let Grep_Xargs_Options = '--null' +endif + +" The find utility is from the cygwin package or some other find utility. +if !exists("Grep_Cygwin_Find") + let Grep_Cygwin_Find = 0 +endif + +" NULL device name to supply to grep. We need this because, grep will not +" print the name of the file, if only one filename is supplied. We need the +" filename for Vim quickfix processing. +if !exists("Grep_Null_Device") + if has("win32") || has("win16") || has("win95") + let Grep_Null_Device = 'NUL' + else + let Grep_Null_Device = '/dev/null' + endif +endif + +" Character to use to quote patterns and filenames before passing to grep. +if !exists("Grep_Shell_Quote_Char") + if has("win32") || has("win16") || has("win95") + let Grep_Shell_Quote_Char = '' + else + let Grep_Shell_Quote_Char = "'" + endif +endif + +" Character to use to escape special characters before passing to grep. +if !exists("Grep_Shell_Escape_Char") + if has("win32") || has("win16") || has("win95") + let Grep_Shell_Escape_Char = '' + else + let Grep_Shell_Escape_Char = '\' + endif +endif + +" The list of directories to skip while searching for a pattern. Set this +" variable to '', if you don't want to skip directories. +if !exists("Grep_Skip_Dirs") + let Grep_Skip_Dirs = 'RCS CVS SCCS' +endif + +" The list of files to skip while searching for a pattern. Set this variable +" to '', if you don't want to skip any files. +if !exists("Grep_Skip_Files") + let Grep_Skip_Files = '*~ *,v s.*' +endif + +" RunGrepCmd() +" Run the specified grep command using the supplied pattern +function! s:RunGrepCmd(cmd, pattern, action) + let cmd_output = system(a:cmd) + + if cmd_output == "" + echohl WarningMsg | + \ echomsg "Error: Pattern " . a:pattern . " not found" | + \ echohl None + return + endif + + let tmpfile = tempname() + + let old_verbose = &verbose + set verbose&vim + + exe "redir! > " . tmpfile + silent echon '[Search results for pattern: ' . a:pattern . "]\n" + silent echon cmd_output + redir END + + let &verbose = old_verbose + + let old_efm = &efm + set efm=%f:%\\s%#%l:%m + + if v:version >= 700 && a:action == 'add' + execute "silent! caddfile " . tmpfile + else + if exists(":cgetfile") + execute "silent! cgetfile " . tmpfile + else + execute "silent! cfile " . tmpfile + endif + endif + + let &efm = old_efm + + " Open the grep output window + if g:Grep_OpenQuickfixWindow == 1 + " Open the quickfix window below the current window + botright copen + endif + + call delete(tmpfile) +endfunction + +" RunGrepRecursive() +" Run specified grep command recursively +function! s:RunGrepRecursive(cmd_name, grep_cmd, action, ...) + if a:0 > 0 && (a:1 == "-?" || a:1 == "-h") + echo 'Usage: ' . a:cmd_name . " [] [ " . + \ "[]]" + return + endif + + let grep_opt = "" + let pattern = "" + let filepattern = "" + + let argcnt = 1 + while argcnt <= a:0 + if a:{argcnt} =~ '^-' + let grep_opt = grep_opt . " " . a:{argcnt} + elseif pattern == "" + let pattern = g:Grep_Shell_Quote_Char . a:{argcnt} . + \ g:Grep_Shell_Quote_Char + else + let filepattern = filepattern . " " . a:{argcnt} + endif + let argcnt= argcnt + 1 + endwhile + if grep_opt == "" + let grep_opt = g:Grep_Default_Options + endif + + if a:grep_cmd != 'agrep' + " Don't display messages about non-existent files + " Agrep doesn't support the -s option + let grep_opt = grep_opt . " -s" + endif + + if a:grep_cmd == 'grep' + let grep_path = g:Grep_Path + let grep_expr_option = '--' + elseif a:grep_cmd == 'fgrep' + let grep_path = g:Fgrep_Path + let grep_expr_option = '-e' + elseif a:grep_cmd == 'egrep' + let grep_path = g:Egrep_Path + let grep_expr_option = '-e' + elseif a:grep_cmd == 'agrep' + let grep_path = g:Agrep_Path + let grep_expr_option = '' + else + return + endif + + " No argument supplied. Get the identifier and file list from user + if pattern == "" + let pattern = input("Search for pattern: ", expand("")) + if pattern == "" + return + endif + let pattern = g:Grep_Shell_Quote_Char . pattern . + \ g:Grep_Shell_Quote_Char + endif + + let cwd = getcwd() + if g:Grep_Cygwin_Find == 1 + let cwd = substitute(cwd, "\\", "/", "g") + endif + if v:version >= 700 + let startdir = input("Start searching from directory: ", cwd, "dir") + else + let startdir = input("Start searching from directory: ", cwd) + endif + if startdir == "" + return + endif + + if filepattern == "" + let filepattern = input("Search in files matching pattern: ", + \ g:Grep_Default_Filelist) + if filepattern == "" + return + endif + endif + + let txt = filepattern . ' ' + let find_file_pattern = '' + while txt != '' + let one_pattern = strpart(txt, 0, stridx(txt, ' ')) + if find_file_pattern != '' + let find_file_pattern = find_file_pattern . ' -o' + endif + let find_file_pattern = find_file_pattern . ' -name ' . + \ g:Grep_Shell_Quote_Char . one_pattern . g:Grep_Shell_Quote_Char + let txt = strpart(txt, stridx(txt, ' ') + 1) + endwhile + let find_file_pattern = g:Grep_Shell_Escape_Char . '(' . + \ find_file_pattern . ' ' . g:Grep_Shell_Escape_Char . ')' + + let txt = g:Grep_Skip_Dirs + let find_prune = '' + if txt != '' + let txt = txt . ' ' + while txt != '' + let one_dir = strpart(txt, 0, stridx(txt, ' ')) + if find_prune != '' + let find_prune = find_prune . ' -o' + endif + let find_prune = find_prune . ' -name ' . one_dir + let txt = strpart(txt, stridx(txt, ' ') + 1) + endwhile + let find_prune = '-type d ' . g:Grep_Shell_Escape_Char . '(' . + \ find_prune + let find_prune = find_prune . ' ' . g:Grep_Shell_Escape_Char . ')' + endif + + let txt = g:Grep_Skip_Files + let find_skip_files = '-type f' + if txt != '' + let txt = txt . ' ' + while txt != '' + let one_file = strpart(txt, 0, stridx(txt, ' ')) + let find_skip_files = find_skip_files . ' ! -name ' . + \ g:Grep_Shell_Quote_Char . one_file . + \ g:Grep_Shell_Quote_Char + let txt = strpart(txt, stridx(txt, ' ') + 1) + endwhile + endif + + if g:Grep_Find_Use_Xargs == 1 + let cmd = g:Grep_Find_Path . " " . startdir + let cmd = cmd . " " . find_prune . " -prune -o" + let cmd = cmd . " " . find_skip_files + let cmd = cmd . " " . find_file_pattern + let cmd = cmd . " -print0 | " + let cmd = cmd . g:Grep_Xargs_Path . ' ' . g:Grep_Xargs_Options + let cmd = cmd . ' ' . grep_path . " " . grep_opt . " -n " + let cmd = cmd . grep_expr_option . " " . pattern + let cmd = cmd . ' ' . g:Grep_Null_Device + else + let cmd = g:Grep_Find_Path . " " . startdir + let cmd = cmd . " " . find_prune . " -prune -o" + let cmd = cmd . " " . find_skip_files + let cmd = cmd . " " . find_file_pattern + let cmd = cmd . " -exec " . grep_path . " " . grep_opt . " -n " + let cmd = cmd . grep_expr_option . " " . pattern + let cmd = cmd . " {} " . g:Grep_Null_Device . ' ' . + \ g:Grep_Shell_Escape_Char . ';' + endif + + call s:RunGrepCmd(cmd, pattern, a:action) +endfunction + +" RunGrepSpecial() +" Search for a pattern in all the opened buffers or filenames in the +" argument list +function! s:RunGrepSpecial(cmd_name, which, action, ...) + if a:0 > 0 && (a:1 == "-?" || a:1 == "-h") + echo 'Usage: ' . a:cmd_name . " [] []" + return + endif + + " Search in all the Vim buffers + if a:which == 'buffer' + " Get a list of all the buffer names + let last_bufno = bufnr("$") + + let i = 1 + let filenames = "" + + while i <= last_bufno + if bufexists(i) && buflisted(i) + let filenames = filenames . " " . bufname(i) + endif + let i = i + 1 + endwhile + + " No buffers + if filenames == "" + return + endif + elseif a:which == 'args' + " Search in all the filenames in the argument list + let arg_cnt = argc() + + if arg_cnt == 0 + echohl WarningMsg + echomsg "Error: Argument list is empty" + echohl None + return + endif + + let i = 0 + let filenames = "" + + while i < arg_cnt + let filenames = filenames . " " . argv(i) + let i = i + 1 + endwhile + + " No arguments + if filenames == "" + echohl WarningMsg + echomsg "Error: Argument list is empty" + echohl None + return + endif + endif + + let grep_opt = "" + let pattern = "" + + " Get the list of optional grep command-line options (if present) + " supplied by the user. All the grep options will be preceded + " by a '-' + let argcnt= 1 + while argcnt <= a:0 && a:{argcnt} =~ '^-' + let grep_opt = grep_opt . " " . a:{argcnt} + let argcnt = argcnt + 1 + endwhile + + " If the user didn't specify the option, then use the defaults + if grep_opt == "" + let grep_opt = g:Grep_Default_Options + endif + + " Don't display messages about non-existent files + let grep_opt = grep_opt . " -s" + + " The last argument specified by the user is the pattern + if argcnt == a:0 + let pattern = a:{argcnt} + else + " No argument supplied. Get the identifier and file list from user + let pattern = input("Search for pattern: ", expand("")) + if pattern == "" + return + endif + endif + + let pattern = g:Grep_Shell_Quote_Char . pattern . g:Grep_Shell_Quote_Char + + " Add /dev/null to the list of filenames, so that grep print the + " filename and linenumber when grepping in a single file + let filenames = filenames . " " . g:Grep_Null_Device + let cmd = g:Grep_Path . " " . grep_opt . " -n -- " + let cmd = cmd . pattern . " " . filenames + + call s:RunGrepCmd(cmd, pattern, a:action) +endfunction + +" RunGrep() +" Run the specified grep command +function! s:RunGrep(cmd_name, grep_cmd, action, ...) + if a:0 > 0 && (a:1 == "-?" || a:1 == "-h") + echo 'Usage: ' . a:cmd_name . " [] [ " . + \ "[]]" + return + endif + + let grep_opt = "" + let pattern = "" + let filenames = "" + + " Parse the arguments + " grep command-line flags are specified using the "-flag" format + " the next argument is assumed to be the pattern + " and the next arguments are assumed to be filenames or file patterns + let argcnt = 1 + while argcnt <= a:0 + if a:{argcnt} =~ '^-' + let grep_opt = grep_opt . " " . a:{argcnt} + elseif pattern == "" + let pattern = g:Grep_Shell_Quote_Char . a:{argcnt} . + \ g:Grep_Shell_Quote_Char + else + let filenames= filenames . " " . a:{argcnt} + endif + let argcnt = argcnt + 1 + endwhile + + if grep_opt == "" + let grep_opt = g:Grep_Default_Options + endif + + if a:grep_cmd != 'agrep' + " Don't display messages about non-existent files + " Agrep doesn't support the -s option + let grep_opt = grep_opt . " -s" + endif + + if a:grep_cmd == 'grep' + let grep_path = g:Grep_Path + let grep_expr_option = '--' + elseif a:grep_cmd == 'fgrep' + let grep_path = g:Fgrep_Path + let grep_expr_option = '-e' + elseif a:grep_cmd == 'egrep' + let grep_path = g:Egrep_Path + let grep_expr_option = '-e' + elseif a:grep_cmd == 'agrep' + let grep_path = g:Agrep_Path + let grep_expr_option = '' + else + return + endif + + " Get the identifier and file list from user + if pattern == "" + let pattern = input("Search for pattern: ", expand("")) + if pattern == "" + return + endif + let pattern = g:Grep_Shell_Quote_Char . pattern . + \ g:Grep_Shell_Quote_Char + endif + + if filenames == "" + if v:version >= 700 + let filenames = input("Search in files: ", g:Grep_Default_Filelist, + \ "file") + else + let filenames = input("Search in files: ", g:Grep_Default_Filelist) + endif + if filenames == "" + return + endif + endif + + " Add /dev/null to the list of filenames, so that grep print the + " filename and linenumber when grepping in a single file + let filenames = filenames . " " . g:Grep_Null_Device + let cmd = grep_path . " " . grep_opt . " -n " + let cmd = cmd . grep_expr_option . " " . pattern + let cmd = cmd . " " . filenames + + call s:RunGrepCmd(cmd, pattern, a:action) +endfunction + +" Define the set of grep commands +command! -nargs=* -complete=file Grep + \ call s:RunGrep('Grep', 'grep', 'set', ) +command! -nargs=* -complete=file Rgrep + \ call s:RunGrepRecursive('Rgrep', 'grep', 'set', ) +command! -nargs=* GrepBuffer + \ call s:RunGrepSpecial('GrepBuffer', 'buffer', 'set', ) +command! -nargs=* Bgrep + \ call s:RunGrepSpecial('Bgrep', 'buffer', 'set', ) +command! -nargs=* GrepArgs + \ call s:RunGrepSpecial('GrepArgs', 'args', 'set', ) + +command! -nargs=* -complete=file Fgrep + \ call s:RunGrep('Fgrep', 'fgrep', 'set', ) +command! -nargs=* -complete=file Rfgrep + \ call s:RunGrepRecursive('Rfgrep', 'fgrep', 'set', ) +command! -nargs=* -complete=file Egrep + \ call s:RunGrep('Egrep', 'egrep', 'set', ) +command! -nargs=* -complete=file Regrep + \ call s:RunGrepRecursive('Regrep', 'egrep', 'set', ) +command! -nargs=* -complete=file Agrep + \ call s:RunGrep('Agrep', 'agrep', 'set', ) +command! -nargs=* -complete=file Ragrep + \ call s:RunGrepRecursive('Ragrep', 'agrep', 'set', ) + +if v:version >= 700 +command! -nargs=* -complete=file GrepAdd + \ call s:RunGrep('GrepAdd', 'grep', 'add', ) +command! -nargs=* -complete=file RgrepAdd + \ call s:RunGrepRecursive('RgrepAdd', 'grep', 'add', ) +command! -nargs=* GrepBufferAdd + \ call s:RunGrepSpecial('GrepBufferAdd', 'buffer', 'add', ) +command! -nargs=* BgrepAdd + \ call s:RunGrepSpecial('BgrepAdd', 'buffer', 'add', ) +command! -nargs=* GrepArgsAdd + \ call s:RunGrepSpecial('GrepArgsAdd', 'args', 'add', ) + +command! -nargs=* -complete=file FgrepAdd + \ call s:RunGrep('FgrepAdd', 'fgrep', 'add', ) +command! -nargs=* -complete=file RfgrepAdd + \ call s:RunGrepRecursive('RfgrepAdd', 'fgrep', 'add', ) +command! -nargs=* -complete=file EgrepAdd + \ call s:RunGrep('EgrepAdd', 'egrep', 'add', ) +command! -nargs=* -complete=file RegrepAdd + \ call s:RunGrepRecursive('RegrepAdd', 'egrep', 'add', ) +command! -nargs=* -complete=file AgrepAdd + \ call s:RunGrep('AgrepAdd', 'agrep', 'add', ) +command! -nargs=* -complete=file RagrepAdd + \ call s:RunGrepRecursive('RagrepAdd', 'agrep', 'add', ) +endif + +" Add the Tools->Search Files menu +if has('gui_running') + anoremenu Tools.Search.Current\ Directory:Grep + \ :Grep + anoremenu Tools.Search.Recursively:Rgrep + \ :Rgrep + anoremenu Tools.Search.Buffer\ List:Bgrep + \ :Bgrep + anoremenu Tools.Search.Argument\ List:GrepArgs + \ :GrepArgs +endif + +" restore 'cpo' +let &cpo = s:cpo_save +unlet s:cpo_save + diff --git a/vim_plugins_src/supertab.vmb b/vim_plugins_src/supertab.vmb new file mode 100644 index 00000000..8f70dce9 --- /dev/null +++ b/vim_plugins_src/supertab.vmb @@ -0,0 +1,1231 @@ +" Vimball Archiver by Charles E. Campbell, Jr., Ph.D. +UseVimball +finish +doc/supertab.txt [[[1 +350 +*supertab.txt* + +Author: Eric Van Dewoestine + Original concept and versions up to 0.32 written by + Gergely Kontra + +This plugin is licensed under the terms of the BSD License. Please see +supertab.vim for the license in its entirety. + +============================================================================== +Supertab *supertab* + +1. Introduction |supertab-intro| +2. Supertab Usage |supertab-usage| +3. Supertab Options |supertab-options| + Default completion type |supertab-defaultcompletion| + Secondary default completion type |supertab-contextdefault| + Completion contexts |supertab-completioncontexts| + Context text |supertab-contexttext| + Context Discover |supertab-contextdiscover| + Example |supertab-contextexample| + Completion Duration |supertab-duration| + Preventing Completion After/Before... |supertab-preventcomplete| + Changing default mapping |supertab-forwardbackward| + Inserting true tabs |supertab-mappingtabliteral| + Enhanced longest match support |supertab-longestenhanced| + Preselecting the first entry |supertab-longesthighlight| + Mapping to end completion |supertab-crmapping| + Auto close the preview window |supertab-closepreviewonpopupclose| + Completion Chaining |supertab-completionchaining| + +============================================================================== +1. Introduction *supertab-intro* + +Supertab is a plugin which allows you to perform all your insert completion +(|ins-completion|) using the tab key. + +Supertab requires Vim version 7.0 or above. + +============================================================================== +2. Supertab usage *supertab-usage* + +Using Supertab is as easy as hitting or (shift+tab) while in +insert mode, with at least one non whitespace character before the cursor, to +start the completion and then or again to cycle forwards or +backwards through the available completions. + +Example ('|' denotes the cursor location): + +bar +baz +b| Hitting here will start the completion, allowing you to + then cycle through the suggested words ('bar' and 'baz'). + +============================================================================== +3. Supertab Options *supertab-options* + +Supertab is configured via several global variables that you can set in your +|vimrc| file according to your needs. Below is a comprehensive list of +the variables available. + + +Default Completion Type *supertab-defaultcompletion* + *g:SuperTabDefaultCompletionType* + +g:SuperTabDefaultCompletionType (default value: "") + +Used to set the default completion type. There is no need to escape this +value as that will be done for you when the type is set. + + Example: setting the default completion to 'user' completion: + + let g:SuperTabDefaultCompletionType = "" + +Note: a special value of 'context' is supported which will result in +super tab attempting to use the text preceding the cursor to decide which +type of completion to attempt. Currently super tab can recognize method +calls or attribute references via '.', '::' or '->', and file path +references containing '/'. + + let g:SuperTabDefaultCompletionType = "context" + + /usr/l # will use filename completion + myvar.t # will use user completion if completefunc set, + # or omni completion if omnifunc set. + myvar-> # same as above + +When using context completion, super tab will fall back to a secondary default +completion type set by |g:SuperTabContextDefaultCompletionType|. + +Note: once the buffer has been initialized, changing the value of this setting +will not change the default complete type used. If you want to change the +default completion type for the current buffer after it has been set, perhaps +in an ftplugin, you'll need to call SuperTabSetDefaultCompletionType like so, +supplying the completion type you wish to switch to: + + call SuperTabSetDefaultCompletionType("") + + +Secondary default completion type *supertab-contextdefault* + *g:SuperTabContextDefaultCompletionType* + +g:SuperTabContextDefaultCompletionType (default value: "") + +Sets the default completion type used when g:SuperTabDefaultCompletionType is +set to 'context' and no completion type is returned by any of the configured +contexts. + + +Completion contexts *supertab-completioncontexts* + *g:SuperTabCompletionContexts* + +g:SuperTabCompletionContexts (default value: ['s:ContextText']) + +Sets the list of contexts used for context completion. This value should +be a list of function names which provide the context implementation. + +When supertab starts the default completion, each of these contexts will be +consulted, in the order they were supplied, to determine the completion type +to use. If a context returns a completion type, that type will be used, +otherwise the next context in the list will be consulted. If after executing +all the context functions, no completion type has been determined, then the +value of g:SuperTabContextDefaultCompletionType will be used. + +Built in completion contexts: + + s:ContextText *supertab-contexttext* + + The text context will examine the text near the cursor to decide which type + of completion to attempt. Currently the text context can recognize method + calls or attribute references via '.', '::' or '->', and file path + references containing '/'. + + /usr/l # will use filename completion + myvar.t # will use user completion if completefunc set, or + # omni completion if omnifunc set. + myvar-> # same as above + + Supported configuration attributes: + + g:SuperTabContextTextFileTypeExclusions + List of file types for which the text context will be skipped. + + g:SuperTabContextTextOmniPrecedence + List of omni completion option names in the order of precedence that they + should be used if available. By default, user completion will be given + precedence over omni completion, but you can use this variable to give + omni completion higher precedence by placing it first in the list. + + s:ContextDiscover *supertab-contextdiscover* + + This context will use the 'g:SuperTabContextDiscoverDiscovery' variable to + determine the completion type to use. It will evaluate each value, in the + order they were defined, until a variable evaluates to a non-zero or + non-empty value, then the associated completion type is used. + + Supported configuration properties: + + g:SuperTabContextDiscoverDiscovery + List of variable:completionType mappings. + + Example context configuration: *supertab-contextexample* + + let g:SuperTabCompletionContexts = ['s:ContextText', 's:ContextDiscover'] + let g:SuperTabContextTextOmniPrecedence = ['&omnifunc', '&completefunc'] + let g:SuperTabContextDiscoverDiscovery = + \ ["&completefunc:", "&omnifunc:"] + + In addition to the default completion contexts, you can plug in your own + implementation by creating a globally accessible function that returns + the completion type to use (eg. "\\"). + + function MyTagContext() + if filereadable(expand('%:p:h') . '/tags') + return "\\" + endif + " no return will result in the evaluation of the next + " configured context + endfunction + let g:SuperTabCompletionContexts = + \ ['MyTagContext', 's:ContextText', 's:ContextDiscover'] + + Note: supertab also supports the b:SuperTabCompletionContexts variable + allowing you to set the list of contexts separately for the current buffer, + like from an ftplugin for example. + + +Completion Duration *supertab-duration* + *g:SuperTabRetainCompletionDuration* + +g:SuperTabRetainCompletionDuration (default value: 'insert') + +Determines if, and for how long, the current completion type is retained. +The possible values include: +'completion' - The current completion type is only retained for the + current completion. Once you have chosen a completion + result or exited the completion mode, the default + completion type is restored. +'insert' - The current completion type is saved until you exit insert + mode (via ESC). Once you exit insert mode the default + completion type is restored. (supertab default) +'session' - The current completion type is saved for the duration of + your vim session or until you enter a different completion + mode. + + +Preventing completion after... *supertab-preventcomplete* + *g:SuperTabNoCompleteBefore* + *g:SuperTabNoCompleteAfter* + +g:SuperTabNoCompleteBefore (default value: []) +g:SuperTabNoCompleteAfter (default value: ['\s']) + +These two variables are used to control when supertab will attempt completion +or instead fall back to inserting a literal , by specifying a list of +patterns which are tested against the text before and after the current cursor +position that when matched, prevent completion. So if you don't want supertab +to start completion after a comma or space, you can set +g:SuperTabNoCompleteAfter to [',', '\s']. + +Note: That a buffer local version of these variables +(b:SuperTabNoCompleteBefore, b:SuperTabNoCompleteAfter) is also supported +should you wish to have different values depending on the file type for +instance. + +Changing the default mapping *supertab-forwardbackward* + *g:SuperTabMappingForward* + *g:SuperTabMappingBackward* + +g:SuperTabMappingForward (default value: '') +g:SuperTabMappingBackward (default value: '') + +These two variables allow you to set the keys used to kick off the current +completion. By default this is and . To change to something +like and , you can add the following to your |vimrc|. + + let g:SuperTabMappingForward = '' + let g:SuperTabMappingBackward = '' + +Note: if the above does not have the desired effect (which may happen in +console version of vim), you can try the following mappings. Although the +backwards mapping still doesn't seem to work in the console for me, your +milage may vary. + + let g:SuperTabMappingForward = '' + let g:SuperTabMappingBackward = '' + + +Inserting true tabs *supertab-mappingtabliteral* + *g:SuperTabMappingTabLiteral* + +g:SuperTabMappingTabLiteral (default value: '') + +Sets the key mapping used to insert a literal tab where supertab would +otherwise attempt to kick off insert completion. The default is '' +(ctrl-tab) which unfortunately might not work at the console. So if you are +using a console vim and want this functionality, you may have to change it to +something that is supported. Alternatively, you can escape the with + (see |i_CTRL-V| for more infos). + + +Enhanced longest match support *supertab-longestenhanced* + *g:SuperTabLongestEnhanced* + +g:SuperTabLongestEnhanced (default value: 0) + +When enabled and 'longest' is in your |completeopt| setting, supertab will +provide an enhanced longest match support where typing one or more letters and +hitting tab again while in a completion mode will complete the longest common +match using the new text in the buffer. + +For example, say you have a buffer with the following contents: + FooBarFoo + FooBar + Foo + FooBarBaz +And you then type F. Vim's builtin longest support will complete the +longest common text 'Foo' and offer 'FooBarFoo', 'FooBar', 'Foo', and +'FooBarBaz' as possible completions. With supertab's longest match +enhancement disabled, typing B while still in the completion mode will +end up completing 'FooBarBaz' or 'FooBarFoo' depending your settings, instead +of the next longest common match of 'FooBar'. With supertab's enhanced +longest match feature enabled, the typing of B will result in the next +longest text being completed. + + +Preselecting the first entry *supertab-longesthighlight* + *g:SuperTabLongestHighlight* + +g:SuperTabLongestHighlight (default value: 0) + +Sets whether or not to pre-highlight the first match when completeopt has the +popup menu enabled and the 'longest' option as well. When enabled, will +kick off completion and pre-select the first entry in the popup menu, allowing +you to simply hit to use it. + + +Mapping to end completion *supertab-crmapping* + *g:SuperTabCrMapping* + +g:SuperTabCrMapping (default value: 1) + +When enabled, will cancel completion mode preserving the current text. + +Compatibility with other plugins: + - endwise: compatible + - delimitMate: not compatible (disabled if the delimitMate mapping is + detected.) + + +Auto close the preview window *supertab-closepreviewonpopupclose* + *g:SuperTabClosePreviewOnPopupClose* + +g:SuperTabClosePreviewOnPopupClose (default value: 0) + +When enabled, supertab will attempt to close vim's completion preview window +when the completion popup closes (completion is finished or canceled). + +Completion Chaining *supertab-completionchaining* + +SuperTab provides the ability to chain one of the completion functions +(|completefunc| or |omnifunc|) together with a one of the default vim +completion key sequences (|ins-completion|), giving you the ability to attempt +completion with the first, and upon no results, fall back to the second. + +To utilize this feature you need to call the SuperTabChain function where +the first argument is the name of a vim compatible |complete-function| and the +second is one of vim's insert completion (|ins-completion|) key bindings +(, , , etc). Calling this function will set the current +buffer's |completefunc| option to a supertab provided implementation which +utilizes the supplied arguments to perform the completion. Since the +|completefunc| option is being set, this feature works best when also +setting |g:SuperTabDefaultCompletionType| to either "context" or "". + +Here is an example that can be added to your .vimrc which will setup the +supertab chaining for any filetype that has a provided |omnifunc| to first +try that, then fall back to supertab's default, , completion: + + autocmd FileType * + \ if &omnifunc != '' | + \ call SuperTabChain(&omnifunc, "") | + \ call SuperTabSetDefaultCompletionType("") | + \ endif + +Note: This feature does not support chaining any other combination of +completions (2 or more completion functions, 2 or more key bindings, etc.). It +can only support 1 completion function followed by 1 key binding. This is due +to limitations imposed by vim's code completion implementation. + +vim:tw=78:ts=8:ft=help:norl: +plugin/supertab.vim [[[1 +874 +" Author: Eric Van Dewoestine +" Original concept and versions up to 0.32 written by +" Gergely Kontra +" Version: 2.0 +" GetLatestVimScripts: 1643 1 :AutoInstall: supertab.vim +" +" Description: {{{ +" Use your tab key to do all your completion in insert mode! +" You can cycle forward and backward with the and keys +" Note: you must press once to be able to cycle back +" +" http://www.vim.org/scripts/script.php?script_id=1643 +" }}} +" +" License: {{{ +" Copyright (c) 2002 - 2012 +" All rights reserved. +" +" Redistribution and use of this software in source and binary forms, with +" or without modification, are permitted provided that the following +" conditions are met: +" +" * Redistributions of source code must retain the above +" copyright notice, this list of conditions and the +" following disclaimer. +" +" * Redistributions in binary form must reproduce the above +" copyright notice, this list of conditions and the +" following disclaimer in the documentation and/or other +" materials provided with the distribution. +" +" * Neither the name of Gergely Kontra or Eric Van Dewoestine nor the names +" of its contributors may be used to endorse or promote products derived +" from this software without specific prior written permission of Gergely +" Kontra or Eric Van Dewoestine. +" +" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS +" IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +" THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +" CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +" PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +" NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +" SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +" }}} +" +" Testing Info: {{{ +" Running vim + supertab with the absolute bar minimum settings: +" $ vim -u NONE -U NONE -c "set nocp | runtime plugin/supertab.vim" +" }}} + +if v:version < 700 + finish +endif + +if exists('complType') " Integration with other completion functions. + finish +endif + +let s:save_cpo=&cpo +set cpo&vim + +" Global Variables {{{ + + if !exists("g:SuperTabDefaultCompletionType") + let g:SuperTabDefaultCompletionType = "" + endif + + if !exists("g:SuperTabContextDefaultCompletionType") + let g:SuperTabContextDefaultCompletionType = "" + endif + + if !exists("g:SuperTabCompletionContexts") + let g:SuperTabCompletionContexts = ['s:ContextText'] + endif + + if !exists("g:SuperTabRetainCompletionDuration") + let g:SuperTabRetainCompletionDuration = 'insert' + endif + + if !exists("g:SuperTabNoCompleteBefore") + " retain backwards compatability + if exists("g:SuperTabMidWordCompletion") && !g:SuperTabMidWordCompletion + let g:SuperTabNoCompleteBefore = ['\k'] + else + let g:SuperTabNoCompleteBefore = [] + endif + endif + + if !exists("g:SuperTabNoCompleteAfter") + " retain backwards compatability + if exists("g:SuperTabLeadingSpaceCompletion") && g:SuperTabLeadingSpaceCompletion + let g:SuperTabNoCompleteAfter = [] + else + let g:SuperTabNoCompleteAfter = ['\s'] + endif + endif + + if !exists("g:SuperTabMappingForward") + let g:SuperTabMappingForward = '' + endif + if !exists("g:SuperTabMappingBackward") + let g:SuperTabMappingBackward = '' + endif + + if !exists("g:SuperTabMappingTabLiteral") + let g:SuperTabMappingTabLiteral = '' + endif + + if !exists("g:SuperTabLongestEnhanced") + let g:SuperTabLongestEnhanced = 0 + endif + + if !exists("g:SuperTabLongestHighlight") + let g:SuperTabLongestHighlight = 0 + endif + + if !exists("g:SuperTabCrMapping") + let g:SuperTabCrMapping = 1 + endif + + if !exists("g:SuperTabClosePreviewOnPopupClose") + let g:SuperTabClosePreviewOnPopupClose = 0 + endif + +" }}} + +" Script Variables {{{ + + " construct the help text. + let s:tabHelp = + \ "Hit or CTRL-] on the completion type you wish to switch to.\n" . + \ "Use :help ins-completion for more information.\n" . + \ "\n" . + \ "|| - Keywords in 'complete' searching down.\n" . + \ "|| - Keywords in 'complete' searching up (SuperTab default).\n" . + \ "|| - Whole lines.\n" . + \ "|| - Keywords in current file.\n" . + \ "|| - Keywords in 'dictionary'.\n" . + \ "|| - Keywords in 'thesaurus', thesaurus-style.\n" . + \ "|| - Keywords in the current and included files.\n" . + \ "|| - Tags.\n" . + \ "|| - File names.\n" . + \ "|| - Definitions or macros.\n" . + \ "|| - Vim command-line.\n" . + \ "|| - User defined completion.\n" . + \ "|| - Omni completion.\n" . + \ "|s| - Spelling suggestions." + + " set the available completion types and modes. + let s:types = + \ "\\\\\\\\" . + \ "\\\\\\\\\s" + let s:modes = '/^E/^Y/^L/^N/^K/^T/^I/^]/^F/^D/^V/^P/^U/^O/s' + let s:types = s:types . "np" + let s:modes = s:modes . '/n/p' + +" }}} + +" SuperTabSetDefaultCompletionType(type) {{{ +" Globally available function that users can use to set the default +" completion type for the current buffer, like in an ftplugin. +function! SuperTabSetDefaultCompletionType(type) + " init hack for workaround. + let b:complCommandLine = 0 + + let b:SuperTabDefaultCompletionType = a:type + + " set the current completion type to the default + call SuperTabSetCompletionType(b:SuperTabDefaultCompletionType) +endfunction " }}} + +" SuperTabSetCompletionType(type) {{{ +" Globally available function that users can use to create mappings to quickly +" switch completion modes. Useful when a user wants to restore the default or +" switch to another mode without having to kick off a completion of that type +" or use SuperTabHelp. Note, this function only changes the current +" completion type, not the default, meaning that the default will still be +" restored once the configured retension duration has been met (see +" g:SuperTabRetainCompletionDuration). To change the default for the current +" buffer, use SuperTabDefaultCompletionType(type) instead. Example mapping to +" restore SuperTab default: +" nmap :call SetSuperTabCompletionType("") +function! SuperTabSetCompletionType(type) + call s:InitBuffer() + exec "let b:complType = \"" . escape(a:type, '<') . "\"" +endfunction " }}} + +" SuperTabAlternateCompletion(type) {{{ +" Function which can be mapped to a key to kick off an alternate completion +" other than the default. For instance, if you have 'context' as the default +" and want to map ctrl+space to issue keyword completion. +" Note: due to the way vim expands ctrl characters in mappings, you cannot +" create the alternate mapping like so: +" imap =SuperTabAlternateCompletion("") +" instead, you have to use \ to prevent vim from expanding the key +" when creating the mapping. +" gvim: +" imap =SuperTabAlternateCompletion("\c-p>") +" console: +" imap =SuperTabAlternateCompletion("\c-p>") +function! SuperTabAlternateCompletion(type) + call SuperTabSetCompletionType(a:type) + " end any current completion before attempting to start the new one. + " use feedkeys to prevent possible remapping of from causing issues. + "call feedkeys("\", 'n') + " ^ since we can't detect completion mode vs regular insert mode, we force + " vim into keyword completion mode and end that mode to prevent the regular + " insert behavior of from occurring. + call feedkeys("\\\", 'n') + call feedkeys(b:complType, 'n') + return '' +endfunction " }}} + +" SuperTabLongestHighlight(dir) {{{ +" When longest highlight is enabled, this function is used to do the actual +" selection of the completion popup entry. +function! SuperTabLongestHighlight(dir) + if !pumvisible() + return '' + endif + return a:dir == -1 ? "\" : "\" +endfunction " }}} + +" s:Init {{{ +" Global initilization when supertab is loaded. +function! s:Init() + " Setup mechanism to restore original completion type upon leaving insert + " mode if configured to do so + if g:SuperTabRetainCompletionDuration == 'insert' + augroup supertab_retain + autocmd! + autocmd InsertLeave * call s:SetDefaultCompletionType() + augroup END + endif +endfunction " }}} + +" s:InitBuffer {{{ +" Per buffer initilization. +function! s:InitBuffer() + if exists('b:SuperTabNoCompleteBefore') + return + endif + + let b:complReset = 0 + let b:complTypeManual = !exists('b:complTypeManual') ? '' : b:complTypeManual + let b:complTypeContext = '' + + " init hack for workaround. + let b:complCommandLine = 0 + + if !exists('b:SuperTabNoCompleteBefore') + let b:SuperTabNoCompleteBefore = g:SuperTabNoCompleteBefore + endif + if !exists('b:SuperTabNoCompleteAfter') + let b:SuperTabNoCompleteAfter = g:SuperTabNoCompleteAfter + endif + + if !exists('b:SuperTabDefaultCompletionType') + let b:SuperTabDefaultCompletionType = g:SuperTabDefaultCompletionType + endif + + " set the current completion type to the default + call SuperTabSetCompletionType(b:SuperTabDefaultCompletionType) + + " hack to programatically revert a change to snipmate that breaks supertab + " but which the new maintainers don't care about: + " http://github.com/garbas/vim-snipmate/issues/37 + let snipmate = maparg('', 'i') + if snipmate =~ 'u' && g:SuperTabMappingForward =~? '' + let snipmate = substitute(snipmate, 'u', '', '') + iunmap + exec "inoremap " . snipmate + endif +endfunction " }}} + +" s:ManualCompletionEnter() {{{ +" Handles manual entrance into completion mode. +function! s:ManualCompletionEnter() + if &smd + echo '' | echohl ModeMsg | echo '-- ^X++ mode (' . s:modes . ')' | echohl None + endif + let complType = nr2char(getchar()) + if stridx(s:types, complType) != -1 + let b:supertab_close_preview = 1 + + if stridx("\\", complType) != -1 " no memory, just scroll... + return "\" . complType + elseif stridx('np', complType) != -1 + let complType = nr2char(char2nr(complType) - 96) + else + let complType = "\" . complType + endif + + let b:complTypeManual = complType + + if index(['insert', 'session'], g:SuperTabRetainCompletionDuration) != -1 + let b:complType = complType + endif + + " Hack to workaround bug when invoking command line completion via = + if complType == "\\" + return s:CommandLineCompletion() + endif + + " optionally enable enhanced longest completion + if g:SuperTabLongestEnhanced && &completeopt =~ 'longest' + call s:EnableLongestEnhancement() + endif + + if g:SuperTabLongestHighlight && + \ &completeopt =~ 'longest' && + \ &completeopt =~ 'menu' && + \ !pumvisible() + let dir = (complType == "\\") ? -1 : 1 + call feedkeys("\=SuperTabLongestHighlight(" . dir . ")\", 'n') + endif + + return complType + endif + + echohl "Unknown mode" + return complType +endfunction " }}} + +" s:SetCompletionType() {{{ +" Sets the completion type based on what the user has chosen from the help +" buffer. +function! s:SetCompletionType() + let chosen = substitute(getline('.'), '.*|\(.*\)|.*', '\1', '') + if chosen != getline('.') + let winnr = b:winnr + close + exec winnr . 'winc w' + call SuperTabSetCompletionType(chosen) + endif +endfunction " }}} + +function! s:SetDefaultCompletionType() " {{{ + if exists('b:SuperTabDefaultCompletionType') && + \ (!exists('b:complCommandLine') || !b:complCommandLine) + call SuperTabSetCompletionType(b:SuperTabDefaultCompletionType) + endif +endfunction " }}} + +" s:SuperTab(command) {{{ +" Used to perform proper cycle navigation as the user requests the next or +" previous entry in a completion list, and determines whether or not to simply +" retain the normal usage of based on the cursor position. +function! s:SuperTab(command) + if exists('b:SuperTabDisabled') && b:SuperTabDisabled + return "\" + endif + + call s:InitBuffer() + + if s:WillComplete() + let b:supertab_close_preview = 1 + + " optionally enable enhanced longest completion + if g:SuperTabLongestEnhanced && &completeopt =~ 'longest' + call s:EnableLongestEnhancement() + endif + + if !pumvisible() + let b:complTypeManual = '' + endif + + " exception: if in mode, then should move up the list, and + " down the list. + if a:command == 'p' && !b:complReset && + \ (b:complType == "\" || + \ (b:complType == 'context' && + \ b:complTypeManual == '' && + \ b:complTypeContext == "\")) + return "\" + + elseif a:command == 'p' && !b:complReset && + \ (b:complType == "\" || + \ (b:complType == 'context' && + \ b:complTypeManual == '' && + \ b:complTypeContext == "\")) + return "\" + + " already in completion mode and not resetting for longest enhancement, so + " just scroll to next/previous + elseif pumvisible() && !b:complReset + let type = b:complType == 'context' ? b:complTypeContext : b:complType + if a:command == 'n' + return type == "\" ? "\" : "\" + endif + return type == "\" ? "\" : "\" + endif + + " handle 'context' completion. + if b:complType == 'context' + let complType = s:ContextCompletion() + if complType == '' + exec "let complType = \"" . + \ escape(g:SuperTabContextDefaultCompletionType, '<') . "\"" + endif + let b:complTypeContext = complType + + " Hack to workaround bug when invoking command line completion via = + elseif b:complType == "\\" + let complType = s:CommandLineCompletion() + else + let complType = b:complType + endif + + " highlight first result if longest enabled + if g:SuperTabLongestHighlight && + \ &completeopt =~ 'longest' && + \ &completeopt =~ 'menu' && + \ (!pumvisible() || b:complReset) + let dir = (complType == "\") ? -1 : 1 + call feedkeys("\=SuperTabLongestHighlight(" . dir . ")\", 'n') + endif + + if b:complReset + let b:complReset = 0 + " not an accurate condition for everyone, but better than sending + " at the wrong time. + if pumvisible() + return "\" . complType + endif + endif + + return complType + endif + + return "\" +endfunction " }}} + +" s:SuperTabHelp() {{{ +" Opens a help window where the user can choose a completion type to enter. +function! s:SuperTabHelp() + let winnr = winnr() + if bufwinnr("SuperTabHelp") == -1 + botright split SuperTabHelp + + setlocal noswapfile + setlocal buftype=nowrite + setlocal bufhidden=delete + + let saved = @" + let @" = s:tabHelp + silent put + call cursor(1, 1) + silent 1,delete + call cursor(4, 1) + let @" = saved + exec "resize " . line('$') + + syntax match Special "|.\{-}|" + + setlocal readonly + setlocal nomodifiable + + nmap :call SetCompletionType() + nmap :call SetCompletionType() + else + exec bufwinnr("SuperTabHelp") . "winc w" + endif + let b:winnr = winnr +endfunction " }}} + +" s:WillComplete() {{{ +" Determines if completion should be kicked off at the current location. +function! s:WillComplete() + if pumvisible() + return 1 + endif + + let line = getline('.') + let cnum = col('.') + + " Start of line. + if line =~ '^\s*\%' . cnum . 'c' + return 0 + endif + + " honor SuperTabNoCompleteAfter + let pre = line[:cnum - 2] + for pattern in b:SuperTabNoCompleteAfter + if pre =~ pattern . '$' + return 0 + endif + endfor + + " honor SuperTabNoCompleteBefore + " Within a word, but user does not have mid word completion enabled. + let post = line[cnum - 1:] + for pattern in b:SuperTabNoCompleteBefore + if post =~ '^' . pattern + return 0 + endif + endfor + + return 1 +endfunction " }}} + +function! s:EnableLongestEnhancement() " {{{ + augroup supertab_reset + autocmd! + autocmd InsertLeave,CursorMovedI + \ call s:ReleaseKeyPresses() | autocmd! supertab_reset + augroup END + call s:CaptureKeyPresses() +endfunction " }}} + +function! s:CompletionReset(char) " {{{ + let b:complReset = 1 + return a:char +endfunction " }}} + +function! s:CaptureKeyPresses() " {{{ + if !exists('b:capturing') || !b:capturing + let b:capturing = 1 + " save any previous mappings + " TODO: capture additional info provided by vim 7.3.032 and up. + let b:captured = { + \ '': maparg('', 'i'), + \ '': maparg('', 'i'), + \ } + " TODO: use &keyword to get an accurate list of chars to map + for c in split('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_', '.\zs') + exec 'imap ' . c . ' =CompletionReset("' . c . '")' + endfor + imap =CompletionReset("\bs>") + imap =CompletionReset("\c-h>") + endif +endfunction " }}} + +function! s:ClosePreview() " {{{ + if exists('b:supertab_close_preview') && b:supertab_close_preview + let preview = 0 + for bufnum in tabpagebuflist() + if getwinvar(bufwinnr(bufnum), '&previewwindow') + let preview = 1 + break + endif + endfor + if preview + pclose + endif + unlet b:supertab_close_preview + endif +endfunction " }}} + +function! s:ReleaseKeyPresses() " {{{ + if exists('b:capturing') && b:capturing + let b:capturing = 0 + for c in split('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_', '.\zs') + exec 'iunmap ' . c + endfor + + iunmap + iunmap + + " restore any previous mappings + for [key, rhs] in items(b:captured) + if rhs != '' + let args = substitute(rhs, '.*\(".\{-}"\).*', '\1', '') + if args != rhs + let args = substitute(args, '<', '', 'g') + let expr = substitute(rhs, '\(.*\)".\{-}"\(.*\)', '\1%s\2', '') + let rhs = printf(expr, args) + endif + exec printf("imap %s %s", key, rhs) + endif + endfor + unlet b:captured + + if mode() == 'i' && &completeopt =~ 'menu' + " force full exit from completion mode (don't exit insert mode since + " that will break repeating with '.') + call feedkeys("\\", 'n') + endif + endif +endfunction " }}} + +" s:CommandLineCompletion() {{{ +" Hack needed to account for apparent bug in vim command line mode completion +" when invoked via = +function! s:CommandLineCompletion() + " This hack will trigger InsertLeave which will then invoke + " s:SetDefaultCompletionType. To prevent default completion from being + " restored prematurely, set an internal flag for s:SetDefaultCompletionType + " to check for. + let b:complCommandLine = 1 + return "\\:call feedkeys('\\\', 'n') | " . + \ "let b:complCommandLine = 0\" +endfunction " }}} + +function! s:ContextCompletion() " {{{ + let contexts = exists('b:SuperTabCompletionContexts') ? + \ b:SuperTabCompletionContexts : g:SuperTabCompletionContexts + + for context in contexts + try + let Context = function(context) + let complType = Context() + unlet Context + if type(complType) == 1 && complType != '' + return complType + endif + catch /E700/ + echohl Error + echom 'supertab: no context function "' . context . '" found.' + echohl None + endtry + endfor + return '' +endfunction " }}} + +function! s:ContextDiscover() " {{{ + let discovery = exists('g:SuperTabContextDiscoverDiscovery') ? + \ g:SuperTabContextDiscoverDiscovery : [] + + " loop through discovery list to find the default + if !empty(discovery) + for pair in discovery + let var = substitute(pair, '\(.*\):.*', '\1', '') + let type = substitute(pair, '.*:\(.*\)', '\1', '') + exec 'let value = ' . var + if value !~ '^\s*$' && value != '0' + exec "let complType = \"" . escape(type, '<') . "\"" + return complType + endif + endfor + endif +endfunction " }}} + +function! s:ContextText() " {{{ + let exclusions = exists('g:SuperTabContextTextFileTypeExclusions') ? + \ g:SuperTabContextTextFileTypeExclusions : [] + + if index(exclusions, &ft) == -1 + let curline = getline('.') + let cnum = col('.') + let synname = synIDattr(synID(line('.'), cnum - 1, 1), 'name') + if curline =~ '.*/\w*\%' . cnum . 'c' || + \ ((has('win32') || has('win64')) && curline =~ '.*\\\w*\%' . cnum . 'c') + return "\\" + + elseif curline =~ '.*\(\w\|[\])]\)\(\.\|::\|->\)\w*\%' . cnum . 'c' && + \ synname !~ '\(String\|Comment\)' + let omniPrecedence = exists('g:SuperTabContextTextOmniPrecedence') ? + \ g:SuperTabContextTextOmniPrecedence : ['&completefunc', '&omnifunc'] + + for omniFunc in omniPrecedence + if omniFunc !~ '^&' + let omniFunc = '&' . omniFunc + endif + if getbufvar(bufnr('%'), omniFunc) != '' + return omniFunc == '&omnifunc' ? "\\" : "\\" + endif + endfor + endif + endif +endfunction " }}} + +function! s:ExpandMap(map) " {{{ + let map = a:map + if map =~ '' + let plug = substitute(map, '.\{-}\(\w\+\).*', '\1', '') + let plug_map = maparg(plug, 'i') + let map = substitute(map, '.\{-}\(\w\+\).*', plug_map, '') + endif + return map +endfunction " }}} + +function! SuperTabChain(completefunc, completekeys) " {{{ + let b:SuperTabChain = [a:completefunc, a:completekeys] + setlocal completefunc=SuperTabCodeComplete +endfunction " }}} + +function! SuperTabCodeComplete(findstart, base) " {{{ + if !exists('b:SuperTabChain') + echoe 'No completion chain has been set.' + return -2 + endif + + if len(b:SuperTabChain) != 2 + echoe 'Completion chain can only be used with 1 completion function ' . + \ 'and 1 fallback completion key binding.' + return -2 + endif + + let Func = function(b:SuperTabChain[0]) + + if a:findstart + let start = Func(a:findstart, a:base) + if start >= 0 + return start + endif + + return col('.') - 1 + endif + + let results = Func(a:findstart, a:base) + if len(results) + return results + endif + + exec 'let keys = "' . escape(b:SuperTabChain[1], '<') . '"' + call feedkeys("\" . keys, 'nt') + return [] +endfunction " }}} + +" Autocmds {{{ + if g:SuperTabClosePreviewOnPopupClose + augroup supertab_close_preview + autocmd! + autocmd InsertLeave,CursorMovedI * call s:ClosePreview() + augroup END + endif +" }}} + +" Key Mappings {{{ + " map a regular tab to ctrl-tab (note: doesn't work in console vim) + exec 'inoremap ' . g:SuperTabMappingTabLiteral . ' ' + + imap =ManualCompletionEnter() + + imap