Function: ex-substitute

ex-substitute is a byte-compiled function defined in viper-ex.el.gz.

Signature

(ex-substitute &optional REPEAT R-FLAG)

Source Code

;; Defined in /usr/src/emacs/lisp/emulation/viper-ex.el.gz
;; Ex substitute command
;; If REPEAT use previous regexp which is ex-reg-exp or viper-s-string
(defun ex-substitute (&optional repeat r-flag)
  (let ((opt-g nil)
	(opt-c nil)
	(matched-pos nil)
	(case-fold-search viper-case-fold-search)
	delim pat repl)
    (if repeat (setq ex-token nil) (setq delim (viper-get-ex-pat)))
    (if (null ex-token)
	(progn
	  (setq pat (if r-flag viper-s-string ex-reg-exp))
	  (or (stringp pat)
	      (error "No previous pattern to use in substitution"))
	  (setq repl ex-repl
		delim (string-to-char pat)))
      (setq pat (if (string= ex-token "") viper-s-string ex-token))
      (setq viper-s-string pat
	    ex-reg-exp pat)
      (setq delim (viper-get-ex-pat))
      (if (null ex-token)
	  (setq ex-token ""
		ex-repl "")
	(setq repl ex-token
	      ex-repl ex-token)))
    (while (viper-get-ex-opt-gc delim)
      (if (string= ex-token "g") (setq opt-g t) (setq opt-c t)))
    (viper-get-ex-count)
    (if ex-count
	(save-excursion
	  (if ex-addresses (goto-char (car ex-addresses)))
	  (set-mark (point))
	  (forward-line (1- ex-count))
	  (setq ex-addresses (cons (point) (cons (mark t) nil))))
      (if (null ex-addresses)
	  (setq ex-addresses (cons (point) (cons (point) nil)))
	(if (null (cdr ex-addresses))
	    (setq ex-addresses (cons (car ex-addresses) ex-addresses)))))
					;(setq G opt-g)
    (let ((beg (car ex-addresses))
	  (end (car (cdr ex-addresses)))
	  eol-mark)
      (save-excursion
	(viper-enlarge-region beg end)
	(let ((limit (save-excursion
		       (goto-char (max (point) (mark t)))
		       (point-marker))))
	  (goto-char (min (point) (mark t)))
	  (while (< (point) limit)
	    (save-excursion
	      (end-of-line)
	      ;; This move allows the use of newline as the last character in
	      ;; the substitution pattern
	      (viper-forward-char-carefully)
	      (setq eol-mark (point-marker)))
	    (beginning-of-line)
	    (if opt-g
		(progn
		  (while (and (not (eolp))
			      (re-search-forward pat eol-mark t))
		    (if (or (not opt-c)
			    (progn
			      (viper-put-on-search-overlay (match-beginning 0)
							   (match-end 0))
			      (y-or-n-p "Replace? ")))
			(progn
			  (viper-hide-search-overlay)
			  (setq matched-pos (point))
			  (if (not (stringp repl))
			      (error "Can't perform Ex substitution: No previous replacement pattern"))
			  (replace-match repl t))))
		  (end-of-line)
		  (viper-forward-char-carefully))
	      (if (null pat)
		  (error
		   "Can't repeat Ex substitution: No previous regular expression"))
	      (if (and (re-search-forward pat eol-mark t)
		       (or (not opt-c)
			   (progn
			     (viper-put-on-search-overlay (match-beginning 0)
							  (match-end 0))
			     (y-or-n-p "Replace? "))))
		  (progn
		    (viper-hide-search-overlay)
		    (setq matched-pos (point))
		    (if (not (stringp repl))
			(error "Can't perform Ex substitution: No previous replacement pattern"))
		    (replace-match repl t)))
	      ;;(end-of-line)
	      ;;(viper-forward-char-carefully)
	      (goto-char eol-mark)
	      )))))
    (if matched-pos (goto-char matched-pos))
    (beginning-of-line)
    (if opt-c (message "done"))))