- (if (looking-at ,re) (incf count))
- (if (not (re-search-forward ,re nil t count))
- (if (looking-at ,re)
- (goto-char (or ,(if endfun `(,endfun)) (point-max)))
- (error ,(format "No next %s" name)))
- (goto-char (match-beginning 0))
- (when (and (eq (current-buffer) (window-buffer (selected-window)))
- (interactive-p))
- (let ((endpt (or (save-excursion
- ,(if endfun `(,endfun)
- `(re-search-forward ,re nil t 2)))
- (point-max))))
- (unless (pos-visible-in-window-p endpt nil t)
- (recenter '(0))))))))
+ (if (looking-at ,re) (setq count (1+ count)))
+ (let (was-narrowed)
+ ,check-narrow-maybe
+ (if (not (re-search-forward ,re nil t count))
+ (if (looking-at ,re)
+ (goto-char (or ,(if endfun `(,endfun)) (point-max)))
+ (error "No next %s" ,name))
+ (goto-char (match-beginning 0))
+ (when (and (eq (current-buffer) (window-buffer (selected-window)))
+ (interactive-p))
+ (let ((endpt (or (save-excursion
+ ,(if endfun `(,endfun)
+ `(re-search-forward ,re nil t 2)))
+ (point-max))))
+ (unless (pos-visible-in-window-p endpt nil t)
+ (recenter '(0))))))
+ ,re-narrow-maybe)))