Function: search-within-boundaries

search-within-boundaries is a byte-compiled function defined in isearch.el.gz.

Signature

(search-within-boundaries SEARCH-FUN GET-FUN NEXT-FUN STRING &optional BOUND NOERROR COUNT)

Source Code

;; Defined in /usr/src/emacs/lisp/isearch.el.gz
(defun search-within-boundaries ( search-fun get-fun next-fun
                                  string &optional bound noerror count)
  (let* ((old (point))
         beg end found skip (i 0)
         (subregexp
          (and isearch-regexp
               (save-match-data
                 (catch 'subregexp
                   (while (string-match "\\^\\|\\$" string i)
                     (setq i (match-end 0))
                     (when (subregexp-context-p string (match-beginning 0))
                       ;; The ^/$ is not inside a char-range or escaped.
                       (throw 'subregexp t))))))))

    ;; Optimization for non-subregexp case to set the initial position
    ;; on the first match assuming there is no need to check boundaries
    ;; for a search string/regexp without anchors (bug#78520).
    (unless subregexp
      (save-match-data
        (if (funcall (or search-fun (isearch-search-fun-default))
                     string bound noerror count)
            (goto-char (if isearch-forward (match-beginning 0) (match-end 0)))
          (setq skip t))))

    (unless skip
      ;; Check if point is already on the property.
      (setq beg (when (funcall get-fun (point)) (point)))
      ;; Otherwise, try to search for the next property.
      (unless beg
        (setq beg (funcall next-fun (point)))
        (when beg
          (if (or (null bound)
                  (if isearch-forward
                      (< beg bound)
                    (> beg bound)))
              (goto-char beg)
            (setq beg nil)))))

    ;; Non-nil `beg' means there are more properties.
    (while (and beg (not found) (not skip))
      ;; Search for the end of the current property.
      (setq end (funcall next-fun beg))
      ;; Handle ^/$ specially by matching in a temporary buffer.
      (if subregexp
          (let* ((prop-beg
                  (if (or (if isearch-forward (bobp) (eobp))
                          (null (funcall get-fun
                                         (+ (point)
                                            (if isearch-forward -1 1)))))
                      ;; Already at the beginning of the field.
                      beg
                    ;; Get the real beginning of the field when
                    ;; the search was started in the middle.
                    (let ((isearch-forward (not isearch-forward)))
                      ;; Search in the reverse direction.
                      (funcall next-fun beg))))
                 (substring (buffer-substring prop-beg end))
                 (offset (if isearch-forward prop-beg end))
                 match-data)
            (with-temp-buffer
              (insert substring)
              (goto-char (- beg offset -1))
              ;; Apply ^/$ regexp on the whole extracted substring.
              (setq found (funcall
                           (or search-fun (isearch-search-fun-default))
                           string (and bound (max (point-min)
                                                  (min (point-max)
                                                       (- bound offset -1))))
                           noerror count))
              ;; Adjust match data as if it's matched in original buffer.
              (when found
                (setq found (+ found offset -1)
                      match-data (mapcar (lambda (m) (+ m offset -1))
                                         (match-data)))))
            (when found (goto-char found))
            (when match-data (set-match-data
                              (mapcar #'copy-marker match-data))))
        (setq found (funcall
                     (or search-fun (isearch-search-fun-default))
                     string (if bound (if isearch-forward
                                          (min bound end)
                                        (max bound end))
                              end)
                     noerror count)))
      ;; Get the next text property.
      (unless found
        (setq beg (funcall next-fun end))
        (when beg
          (if (or (null bound)
                  (if isearch-forward
                      (< beg bound)
                    (> beg bound)))
              (goto-char beg)
            (setq beg nil)))))
    (unless found (goto-char old))
    found))