unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Search within function for text
@ 2015-03-24 13:28 Glen Stark
  2015-03-24 15:21 ` Drew Adams
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Glen Stark @ 2015-03-24 13:28 UTC (permalink / raw)
  To: help-gnu-emacs

Hi Everyone.

I would like to find all occurences of a bit of text that fall within a 
certain function name.  I'm programming in C++, and several classes have 
their own implementations of foo(), and I'd like to find all occurrences 
of the string "bar" within functions named "foo()".

Does anyone know of a way I can do this?

cheers,

Glen 


^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: Search within function for text
  2015-03-24 13:28 Search within function for text Glen Stark
@ 2015-03-24 15:21 ` Drew Adams
  2015-03-24 15:44 ` Tassilo Horn
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Drew Adams @ 2015-03-24 15:21 UTC (permalink / raw)
  To: Glen Stark, help-gnu-emacs

> I would like to find all occurences of a bit of text that fall within a
> certain function name.  I'm programming in C++, and several classes have
> their own implementations of foo(), and I'd like to find all occurrences
> of the string "bar" within functions named "foo()".
> 
> Does anyone know of a way I can do this?

When you say "within functions named 'foo()'", what do you mean?
Do you mean within each `foo' function call?  Within the various
_definitions_ functions named `foo'?

In either case, if you can define either a regexp or a function
that can locate those occurrences then yes, you can use Icicles
search to interactively search only within those contexts (i.e.,
the `foo' function calls or the `foo' function definitions or
whatever).

The function (and command) `icicle-search' takes, as one of its
arguments, a regexp or a function that defines the set of
contexts to search.  (It also accepts a set of files or buffers
that contain the contexts to search.  The default is to search
only the current buffer.)

There are several predefined Icicles commands that specialize
`icicle-search' to limit search to contexts that are similar to
what you are asking for.  You can use their definitions as models.
These commands take advantage of Imenu definitions for Emacs Lisp. 

For function (or whatever) definitions as search contexts, you
too can likely take advantage of Imenu, which provides regexps
that locate definitions in various languages (modes).

For example, for Emacs Lisp, Imenu can locate definitions of
functions, global variables, types, etc.  Icicles then uses the
Imenu regexps to define commands that search only the definitions
of such kinds of things.  (You can also search outside the search
contexts BTW, i.e., the complement buffer zones.)

There are thus predefined Icicles commands for searching the
definitions of these kinds of things in an Emacs-Lisp buffer:

* macros
* commands
* non-interactive functions
* user options
* all variables (including options)
* faces
* keys defined in named keymaps (e.g., `org-mode-map')
* global/local key definitions (no keymap specified explicitly)
* definitions of all types (i.e., all of the above)

Each of the command names starts with `icicle-imenu-' and ends
with `-full' (meaning that the full definition is the search
context - there are also command that just locate definitions,
without matching their full definitions.).

In between is the name of the definition type: `macro' (e.g.,
`icicle-imenu-macro-full'), `command', `non-interactive-function',
`user-option', `variable', `face', `key-explicit-map',
`key-implicit-map'.  

The last, most general command just has name `icicle-imenu-full'.

For a language other than Emacs Lisp, you would start with the
Imenu regexp(s) that exist for it - or you would roll your own
if you cannot find existing regexps that are useful.

However, Imenu regexps match only up to the end of the name being
defined, not the full definition.  Helper function `icicle-imenu-1'
accepts a flag argument, FULL, that tells it to use the full
definition.  Non-nil FULL means that the search context is from
the regexp-match beginning through `forward-sexp' from there.

So if `forward-sexp' works correctly for the language you use then
you need only (a) pass the Imenu regexp for the kind of object
(e.g. function, type, class) you want, and (b) pass non-nil FULL.

If `forward-sexp' is not sufficiently accurate for your language
then you will need to modify the existing Imenu regexp so that
it matches not just up to the end of the name being defined but
the full definition.

Once you have defined, for your language, an Icicles search
command for a particular type of object (e.g. functions) as
described above, you can use it.  When you invoke it, `S-TAB'
shows you all of the search contexts as candidates.

You type a pattern to match within the candidates.  As you type,
the set of candidates is narrowed by matching.  Your input can
be matched literally (as a substring) or as a regexp.  Change
your typed input on the fly to see the matching candidates change.

You can "lock in" a set of matches by hitting `S-SPC', and then
use another pattern to narrow further.  This is "progressive
completion".

You can use `C-~' to complement the current set of matches,
subtracting it from the previous set.  E.g., if you type `toto'
then the set of candidates is narrowed from whatever it was to
just those that contain `toto'.  `C-~' then gives you back the
wider set of matches shown previously, minus the matches for
`toto'.  This is "chipping away".

When you have a given set of candidates, you can navigate among
them in the source buffers/files.  Use `C-down' etc. to cycle.
Use `C-RET' or `C-mouse-2' to go to an individual candidate.
Use `down' to cycle among candidates without visiting the source
location (then hit `C-RET' to visit the current one).

For full-definition searching, the contexts, hence the candidates,
are typically multi-line.  Sometimes it is more convenient to not
see the full candidates (even though you are still using them)
but to just see their lines that match your current input (pattern).
You can hide the non-matching lines in each candidate definition
by using `C-u C-x .' (this is a toggle).

More information:

* Icicles overview
  http://www.emacswiki.org/emacs/Icicles_-_Nutshell_View

* Icicles search
  http://www.emacswiki.org/emacs/Icicles_-_Search_Commands%2c_Overview

* Icicles Imenu commands
  http://www.emacswiki.org/emacs/Icicles_-_Other_Search_Commands#IciclesImenu

  The screenshot there shows the use of non-full command
  `icicle-imenu-command', with candidates that match `kill' typed
  in the minibuffer.  Use of `icicle-imenu-command-full' would
  instead show the full command definitions highlighted in the
  source buffer and shown in buffer *Completions* (which is where
  candidates are shown).



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Search within function for text
  2015-03-24 13:28 Search within function for text Glen Stark
  2015-03-24 15:21 ` Drew Adams
@ 2015-03-24 15:44 ` Tassilo Horn
  2015-03-24 16:53 ` Louis Höfler
  2015-03-24 16:57 ` Louis Höfler
  3 siblings, 0 replies; 5+ messages in thread
From: Tassilo Horn @ 2015-03-24 15:44 UTC (permalink / raw)
  To: Glen Stark; +Cc: help-gnu-emacs

Glen Stark <mail@glenstark.net> writes:

Hi Glen,

> I would like to find all occurences of a bit of text that fall within
> a certain function name.  I'm programming in C++, and several classes
> have their own implementations of foo(), and I'd like to find all
> occurrences of the string "bar" within functions named "foo()".

At least you can't do that with regular expressions but you need to
something which actually understands C++ syntax.

> Does anyone know of a way I can do this?

Have a look at CEDET.  Assuming foo is not just a string but a variable
or function, `semantic-symref' might do exactly what you want:

  http://cedet.sourceforge.net/symref.shtml

Bye,
Tassilo



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Search within function for text
  2015-03-24 13:28 Search within function for text Glen Stark
  2015-03-24 15:21 ` Drew Adams
  2015-03-24 15:44 ` Tassilo Horn
@ 2015-03-24 16:53 ` Louis Höfler
  2015-03-24 16:57 ` Louis Höfler
  3 siblings, 0 replies; 5+ messages in thread
From: Louis Höfler @ 2015-03-24 16:53 UTC (permalink / raw)
  To: help-gnu-emacs

Am 24.03.2015 um 14:28 schrieb Glen Stark:
> Hi Everyone.
>
> I would like to find all occurences of a bit of text that fall within a
> certain function name.
>
This is possible with regex.
If you want to search within the current buffer use
(re-search-forward "foo() \{.* bar.*\}")

The task is more difficult if you want to search within all files in a 
directory.
You can either use
find -type f -name "*.cpp" | grep "foo() \{.* bar.*\}"
Or within emacs, by interfacing find and grep.
I also developed erfind for this purpose.
It does not need any thirdparty application and in addition can work on 
windows and linux.
Download it here, copy it to your emacs seach path:
http://scm.mathematek.de/repos.cgi/emacside/zip/emacside.zip

Then load the packages with

(require 'eide-filetools)
(require 'eide-erfind)

You can modify the source for your needs, but in your case maybe this 
should work
M+x erfind-cwd <RET> foo() \{.* bar.*\} <RET>

Hope this helped a little, Louis



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Search within function for text
  2015-03-24 13:28 Search within function for text Glen Stark
                   ` (2 preceding siblings ...)
  2015-03-24 16:53 ` Louis Höfler
@ 2015-03-24 16:57 ` Louis Höfler
  3 siblings, 0 replies; 5+ messages in thread
From: Louis Höfler @ 2015-03-24 16:57 UTC (permalink / raw)
  To: help-gnu-emacs

Should be
find -type f -name "*.cpp" | xargs grep "foo() \{.* bar.*\}"

Greetings, Louis




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-03-24 16:57 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-03-24 13:28 Search within function for text Glen Stark
2015-03-24 15:21 ` Drew Adams
2015-03-24 15:44 ` Tassilo Horn
2015-03-24 16:53 ` Louis Höfler
2015-03-24 16:57 ` Louis Höfler

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).