[comp.emacs] Aborting a search nicely

mal@coyote.draper.com (Mark Lamourine) (11/08/90)

Jonathan Sweedler writes:

>I have the following psuedo-code:

>    (re-search-forward "String")
>    (some-commands-follow)

> If "String" does not exist in my buffer then the search-forward command
> bombs out.  There must be a way to stop the search-forward from bombing
> out and to do something if the string is not found.  For instance, if 
> the string is found I want to execute certain commands and if it is
> not found then I want to do something else.

The following text was obtained by using <backspace> f re-search-forward
(that is: help function re-search-forward)

re-search-forward:
Search forward from point for regular expression REGEXP.
Set point to the end of the occurrence found, and return t.
An optional second argument bounds the search; it is a buffer position.
The match found must not extend after that position.
> Optional third argument, if t, means if fail just return nil (no error).
  If not nil and not t, move to limit of search and return nil.
Optional fourth argument is repeat count--search for successive occurrences.
See also the functions match-beginning and match-end and replace-match.

Therefore, what you want is something like

(if (re-search-forward "String" (point-max) t)
    (some-commands-follow))

-------------------------------------------------------------------------------
Mark Lamourine
internet: lamourine@draper.com                   "It's not my fault!", Han Solo
-------------------------------------------------------------------------------

gildea@ALEXANDER.BBN.COM (Stephen Gildea) (11/09/90)

    (re-search-forward "String")

    If "String" does not exist in my buffer then the search-forward command
    bombs out.  There must be a way to stop the search-forward from bombing
    out and to do something if the string is not found.


RTFM: a quick look at "C-h f re-search-forward" shows you how to supply
the extra arguments to do this:

(if (re-search-forward "String" nil t)
    (progn
      success commands here...)
    not found commands here...)


 < Stephen				Bolt Beranek and Newman Inc.
   gildea@bbn.com			Cambridge, Massachusetts

worley@AI.MIT.EDU (Dale Worley) (11/09/90)

   From: think!samsung!olivea!orc!inews!jsweedle@sdd.hp.com  (Jonathan Sweedler)
       (re-search-forward "String")
       (some-commands-follow)

   If "String" does not exist in my buffer then the search-forward command
   bombs out.  There must be a way to stop the search-forward from bombing
   out and to do something if the string is not found.

C-h f re-search-forward RET reveals, in part:

   Optional third argument, if t, means if fail just return nil (no error).

Since re-search-forward returns t if it succeeds, testing the return
value can be used to determine if the search succeeded.

Dale Worley		Compass, Inc.			worley@compass.com
--
Give yourself over to absolute pleasure
Swim the warm waters of sins of the flesh
Erotic madness beyond any measure
And sensual daydreams to treasure... forever.
-- Rocky Horror Picture Show