Function: replace-preview-update

replace-preview-update is a byte-compiled function defined in replace.el.gz.

Signature

(replace-preview-update FROM TO REGEXP-FLAG DELIMITED-FLAG CASE-FOLD &optional START END)

Documentation

Preview the result of replacing FROM with TO in the current buffer.

Each match of FROM visible in the selected window gets an overlay showing the text that replace-preview--format returns for it, which depends on query-replace-show-preview. Matches for which replace-preview--format returns nil are left alone.

START and END limit the portion of the buffer where preview is shown, as in perform-replace: a visible match outside of the region START..END will not be replaced, so previewing it would be misleading. Matches to leave alone for other reasons are still expected to be rejected by isearch-filter-predicate.

REGEXP-FLAG, DELIMITED-FLAG and CASE-FOLD say how to search for FROM, as in replace-search.

Source Code

;; Defined in /usr/src/emacs/lisp/replace.el.gz
(defun replace-preview-update (from to regexp-flag delimited-flag case-fold
				    &optional start end)
  "Preview the result of replacing FROM with TO in the current buffer.
Each match of FROM visible in the selected window gets an overlay
showing the text that `replace-preview--format' returns for it,
which depends on `query-replace-show-preview'.  Matches for
which `replace-preview--format' returns nil are left alone.

START and END limit the portion of the buffer where preview is shown,
as in `perform-replace': a visible match outside of the region START..END
will not be replaced, so previewing it would be misleading.  Matches
to leave alone for other reasons are still expected to be rejected
by `isearch-filter-predicate'.

REGEXP-FLAG, DELIMITED-FLAG and CASE-FOLD say how to search for FROM, as
in `replace-search'."
  (replace-preview-cleanup)
  (let ((nocasify (not (and case-replace case-fold)))
	(literal (or (not regexp-flag) (eq regexp-flag 'literal)))
	(limit (min (window-end nil t) (or end (point-max)))))
    (save-excursion
      (save-match-data
	(goto-char (max (window-start) (or start (point-min))))
	(while (and (< (point) limit)
		    (replace-search from limit regexp-flag delimited-flag
				    case-fold))
	  (let* ((beg (match-beginning 0))
		 (end (match-end 0))
		 (text (replace-preview--format
			(buffer-substring beg end)
			(match-substitute-replacement to nocasify literal))))
	    (when (and text (funcall isearch-filter-predicate beg end))
	      (let ((ov (make-overlay beg end)))
		;; A zero-length overlay displays nothing, so for an
		;; empty match show the preview next to it instead.
		(if (= beg end)
		    (overlay-put ov 'before-string text)
		  (overlay-put ov 'display text))
		(overlay-put ov 'priority 1001) ;higher than lazy overlays
		(push ov replace-preview-overlays)))
	    ;; Don't loop forever on a zero-length match.
	    (when (and (= beg end) (not (eobp)))
	      (forward-char 1))))))))