[comp.editors] Restricted Searches in VI

fozzy@mips.bhpese.oz.au (Andrew Steele) (04/22/91)

In vi is it possible to restrict the range of a search.  Specifically
I want to be able to search through just one function in a .c file.
I've inherited some badly written code that has functions that just go on
and on and I'm trying to break them up into smaller units.  To do this I need
to be able to find where local variables are used but not if that name is
also used in some other function later on.

If it's too much of a hassel I'll continue as I have been by checking with
"[[" to see which function I am in, but I thought there may be some way of
combining either "[[" or "%" with a "/" to give a restricted search.

Thanx

wyle@inf.ethz.ch (Mitchell Wyle) (04/22/91)

Greetings vi fans,

RJ-rn screwed up again so I don't know who wrote this article; 
anyway, someone asked:

>In vi is it possible to restrict the range of a search.  Specifically
>I want to be able to search through just one function in a .c file.
>I've inherited some badly written code that has functions that just go on
>and on and I'm trying to break them up into smaller units.  To do this I need
>to be able to find where local variables are used but not if that name is
>also used in some other function later on.

I apologize that this method is not a direct answer to your question.

I solve this problem as follows: I start an edit session of a
non-existent file, read in the C module, trim off everything except the
function I want to change, and search inside it.  I must admit it
*sounds* complicated, but it works for me.

% vi tt               start vi on an empty file
:r bigmodule.c        read in the C file
/big_bad_function     find begining of big, bad C function
k:1,.d                delete everything above it
}}jj%j                find end of big, bad C function matching first brace
.,$d                  delete everything below it.
/foo_bar              start searching for use of variable foo_bar

>If it's too much of a hassel I'll continue as I have been by checking with
>"[[" to see which function I am in, but I thought there may be some way of
>combining either "[[" or "%" with a "/" to give a restricted search.

I have seen people using % inside vi macros to delete everything inside
a given parenthesis level, but I've never tried to restricted a search
inside a given file.  I usually use markers and [[ to mark and come
back to places when I am searching inside big files.

Also, check out using C cross-reference programs to tell you which variables
are used where in which functions.  It might be easier to work on paper than
on-line.

Cheers,

-Mitch

FFAAC09@cc1.kuleuven.ac.be (Nicole Delbecque & Paul Bijnens) (04/23/91)

In article <1991Apr22.071658.1059@cerberus.bhpese.oz.au>,
fozzy@mips.bhpese.oz.au (Andrew Steele) says:
>
>In vi is it possible to restrict the range of a search.  Specifically
>I want to be able to search through just one function in a .c file.
>I've inherited some badly written code that has functions that just go on
>and on and I'm trying to break them up into smaller units.  To do this I need
>to be able to find where local variables are used but not if that name is
>also used in some other function later on.

I don't know of a direct way to accomplish this, but I frequently
have the same problem.  I solve as follows:
    - go to the start of the function (using [[ or ]]).
    - jump to the matching parenthesis of the function (using %)
      (and complain loudly if the function is using unbalanced {}).
    - search backwards with ?\<variable\>
    - if I reach the declaration, I am at the top of the function.
--
Polleke   (Paul Bijnens)
Linguistics dept., K. University Leuven, Belgium
FFAAC09@cc1.kuleuven.ac.be

les@chinet.chi.il.us (Leslie Mikesell) (04/23/91)

fozzy@mips.bhpese.oz.au (Andrew Steele) says:

>In vi is it possible to restrict the range of a search.  Specifically
>I want to be able to search through just one function in a .c file.
>I've inherited some badly written code that has functions that just go on
>and on and I'm trying to break them up into smaller units.  To do this I need
>to be able to find where local variables are used but not if that name is
>also used in some other function later on.

If you just want to see the relevant lines, :address,addressg/pattern/p
will work and the start and end address can be represented by the usual
ways (line number, .=current line, marks, search patterns, etc.) so you
could position to the beginning of a function to search, then use
:.,/^}/g/pattern/p
But it might be just as informative to :set number, note the start and
end lines of the function, and search the whole file with :g/pattern/p.

Another way is to insert a temporary marker at the beginning of the relevant
lines (easy with ex commands) so you can easily see where you are and
restrict the search by including the marker in your search pattern (or
just pay attention to the display).  Often, though it is easier to
just use explicit temporary files.  Keep in mind that you have more
than the editor built-ins at your disposal - there is a general purpose
OS and file system out there as well.  If you need to use more than
the two files that vi handles internally, you can always run another
copy in a subshell without losing your current environment.

Les Mikesell
  les@chinet.chi.il.us