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)

Documentation

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

Each match of FROM visible in the selected window is displayed as the text it would be replaced with, using the query-replace-preview face. 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)
  "Preview the result of replacing FROM with TO in the current buffer.
Each match of FROM visible in the selected window is displayed as the
text it would be replaced with, using the `query-replace-preview' face.
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 (window-end nil t)))
    (save-excursion
      (save-match-data
	(goto-char (window-start))
	(while (and (< (point) limit)
		    (replace-search from limit regexp-flag delimited-flag
				    case-fold))
	  (let* ((beg (match-beginning 0))
		 (end (match-end 0))
		 (text (propertize (match-substitute-replacement
				    to nocasify literal)
				   'face 'query-replace-preview)))
	    (when (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 replacement 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))))))))