add vim conf files
[arrow.git] / conf_slk120 / vim / _vim / plugin / qf.vim
blob7c658cd32020794aafd7f76872ec6d44f6975a50
1 " Copyright (c) 1998-2004
2 " Michael Sharpe <feline@irendi.com>
4 " We grant permission to use, copy modify, distribute, and sell this
5 " software for any purpose without fee, provided that the above copyright
6 " notice and this text are not removed. We make no guarantee about the
7 " suitability of this software for any purpose and we are not liable
8 " for any damages resulting from its use. Further, we are under no
9 " obligation to maintain or extend this software. It is provided on an
10 " "as is" basis without any expressed or implied warranty.
12 " TODO + allow for saving of the quick-fix window...by default it is
13 "        nonmodifiable
14 "      + when th quick fix window is modified it needs to be saved a reloaded
15 "        to have any effect.
17 if exists("loaded_qf")
18    finish
19 endif
20 let loaded_qf = 1
22 " Function : QF_addProgramAndFormat (PUBLIC)
23 " Purpose  : Alls for the registration of a grepprg which will be used with the
24 "            quick-fix buffer
25 " Args     : id -- unique id for lookup
26 "            program -- program to run (will be a &grepprg program)
27 "            format -- quick-fix format for the program (can be empty)
28 " Returns  : nothing
29 " Author   : Michael Sharpe <feline@irendi.com>
30 function! <SID>QF_addProgramAndFormat(id, program, format)
31    let varName = "g:QFProgram_".a:id
32    if (!exists(varName))
33       let g:QFProgram_{a:id} = a:program
34       let g:QFFormat_{a:id} = a:format
35    endif
36 endfunction
38 " Register some default programs. These seem to work well on linux (redhat9)
39 call <SID>QF_addProgramAndFormat('lid', 'lid -f $HOME/IDDB -R grep "$*"', "")
40 call <SID>QF_addProgramAndFormat('grep', 'grep -n $*', "")
41 call <SID>QF_addProgramAndFormat('rgrep', 'grep -n -r $* /dev/null', "")
42 call <SID>QF_addProgramAndFormat('locate', 'locate $*', '%f')
43 call <SID>QF_addProgramAndFormat('find', 'find $* -print', '%f')
45 " Some special case helpers which can be useful
46 call <SID>QF_addProgramAndFormat('raw', '$*', '')
47 call <SID>QF_addProgramAndFormat('loadfile', 'cat $*', '%f|%l|%m')
49 " Function : QF_execProgram (PUBLIC)
50 " Purpose  : Executes a specified program via using vim's builtin grep command
51 "            in conjuction with the quick-fix functionality
52 " Args     : program -- program to execute (via &grepprg)
53 "            format -- format for the quick-fix window
54 "            args -- args for the program (args to :grep command)
55 "            addFlag -- if non-empty results are add to existing quick-fix list
56 " Returns  : Nothing
57 " Author   : Michael Sharpe <feline@irendi.com>
58 function! <SID>QF_execProgram(program, format, args, addFlag)
59    " save the old grepprg and grepformat so that they can be restored later
60    let old_grepprg=&grepprg
61    let old_grepformat=&grepformat
63    " install the specified grepprg
64    let &grepprg=a:program
66    " and install the format if one was specified (TODO should support appending)
67    if (a:format != "")
68       let &grepformat=a:format
69    endif
71    " Run vim's builtin grep command which will invoke the grepprg
72    if (a:addFlag != "")
73      exec "grepadd ".a:args
74    else
75      exec "grep ".a:args
76    endif
77    " Open the quick-fix window (TODO should be configurable via global?)
78    exec "cwin"
79    " Restore the old settings
80    let &grepprg=old_grepprg
81    let &grepformat=old_grepformat
82 endfunction
84 " Function : QF_doExecute (PUBLIC)
85 " Purpose  : Looks up a registered program and executes it via vim's builtin
86 "            grep command
87 " Args     : id -- id of the program
88 "            args -- arguments for the program
89 "            addFlag -- if non-empty result are added to existing quickfix list
90 " Returns  : Nothing
91 " Author   : Michael Sharpe <feline@irendi.com>
92 function! <SID>QF_doExecute(id, args, addFlag)
93    let varName = "g:QFProgram_".a:id
94    " if the command identified by id exists, execute it
95    if (exists(varName))
96       call <SID>QF_execProgram(g:QFProgram_{a:id}, g:QFFormat_{a:id}, a:args, a:addFlag)
97    endif
98 endfunction
100 " Function : QF_lookupAndRunProgram (PUBLIC)
101 " Purpose  : Looks up a registered program and executes it, as above, and using
102 "            above actually. This is simply a wrapper for QF_doExecute()
103 " Args     : allArgs -- argument of the form "<id> <args>" for use with
104 "                       QF_doExecute()
105 " Returns  : Nothing
106 " Author   : Michael Sharpe <feline@irendi.com>
107 function! <SID>QF_lookupAndRunProgram(allArgs, addFlag)
108    " find the first space
109    let pos = stridx(a:allArgs, ' ')
110    if (pos)
111       " first field is the id of the command
112       let id =  strpart(a:allArgs, 0, pos)
113       " rest of the string is the arguments for the corresponding program
114       let args = strpart(a:allArgs, pos)
115       call <SID>QF_doExecute(id, args, a:addFlag)
116    endif
117 endfunction
119 " In all cases below, the ! (i.e.<bang> will append new results to existing
120 " quick-fix window. 
122 " Allows any registered quick-fix command to be executed
123 " QF <id> <args> <-- finds the program corresponding to "id" in the
124 " table and executes it with the specified arguments "args"
125 " e.g. :QF grep foobar *.cpp <-- will grep all cpp files for foobar
126 command -nargs=* -bang QF call <SID>QF_lookupAndRunProgram("<args>", "<bang>")
128 " The following commands are short cuts for the QF command (very short short
129 " cuts....in that they really do not save too much typing :)
131 " E.g. :QFlid static   <-- will find all lines with the word "static" using GNU
132 " id-utils
133 command -nargs=* -bang QFlid call <SID>QF_doExecute('lid', "<args>", "<bang>")
135 " E.g. :QFgrep static *.cpp <-- will find all lines matching "static" in the
136 " .cpp files of the current directory
137 command -nargs=* -bang QFgrep call <SID>QF_doExecute('grep', "<args>", "<bang>")
139 " E.g. :QFrgrep static src  <-- will recursively grep the src directory for all
140 " files containing static
141 command -nargs=* -bang QFrgrep call <SID>QF_doExecute('rgrep',"<args>", "<bang>")
143 " E.g. :QFlocate pattern <-- will locate all files matching pattern via the
144 " slocate functionality available on linux and elsewhere
145 command -nargs=* -bang QFlocate call <SID>QF_doExecute('locate', "<args>", "<bang>")
147 " E.g. QFfind . -name '*.cpp' <-- will find all cpp files under the current
148 " directory
149 command -nargs=* -bang QFfind call <SID>QF_doExecute('find', "<args>", "<bang>")
152 " Allows a saved Quick-fix window which was previously saved to be restored
153 " E.g. :QFload /tmp/foo <-- will load contents of /tmp/foo into quick-fix
154 " buffer assuming /tmp/foo contains the contents of a previously save quick-fix
155 " buffer
156 command -nargs=* -bang QFload call <SID>QF_doExecute('loadfile', "<args>", "<bang>")
158 " Allows the entry of a raw command, useful in some cases, and for testing
159 command -nargs=+ -bang QFE call <SID>QF_execProgram(<f-args>, "<bang>")
160 " Allows the entry of a raw command
161 command -nargs=* -bang QFR call <SID>QF_doExecute('raw', "<args>", "<bang>")