[lice @ 1/2 busted local vars]
[lice.git] / search.lisp
blob230cf96497165c3a5de304072445ba655489c36d
1 (in-package :lice)
3 (defstruct match-data
4 buffer beg end)
6 (define-condition search-failed (lice-condition)
7 () (:documentation "raised when a search failed to match"))
9 (defun search-forward (string &optional bound noerror (count 1))
10 "Search forward from point for string.
11 Set point to the end of the occurrence found, and return point.
12 An optional second argument bounds the search; it is a buffer position.
13 The match found must not extend after that position. nil is equivalent
14 to (point-max).
15 Optional third argument, if t, means if fail just return nil (no error).
16 If not nil and not t, move to limit of search and return nil.
17 Optional fourth argument is repeat count--search for successive occurrences.
19 Search case-sensitivity is determined by the value of the variable
20 `case-fold-search', which see.
22 See also the functions `match-beginning', `match-end' and `replace-match'."
23 (gap-move-to (current-buffer) (buffer-point-aref (current-buffer)))
24 (let* ((buffer (current-buffer))
25 pos
26 (n (loop for i from 0 below count
27 count i
28 do (setf pos (search string (buffer-data buffer) :start2 (buffer-point-aref buffer)))
29 while pos)))
30 (if (/= n count)
31 (when (not noerror)
32 (signal 'search-failed))
33 (goto-char (+ (buffer-aref-to-char buffer pos) (length string))))))