Function: evil-ex-get-substitute-info

evil-ex-get-substitute-info is a byte-compiled function defined in evil-search.el.

Signature

(evil-ex-get-substitute-info STRING &optional IMPLICIT-R)

Documentation

Return the substitution info of :substitute argument STRING.

The result is a triplet (PATTERN REPLACEMENT FLAGS). PATTERN is an Ex-pattern (see evil-ex-make-pattern) and REPLACEMENT is a compiled replacement expression (see evil-compile-replacement). The information returned is the actual substitution information w.r.t. to special situations like empty patterns or repetition of previous substitution commands. If IMPLICIT-R is non-nil, then the "r" flag is assumed, i.e. in case of an empty pattern the last search pattern is used. It is meant for :substitute commands with arguments.

Source Code

;; Defined in ~/.emacs.d/elpa/evil-20251108.138/evil-search.el
(defun evil-ex-get-substitute-info (string &optional implicit-r)
  "Return the substitution info of :substitute argument STRING.
The result is a triplet \(PATTERN REPLACEMENT FLAGS). PATTERN is an
Ex-pattern (see `evil-ex-make-pattern') and REPLACEMENT is a compiled
replacement expression (see `evil-compile-replacement'). The
information returned is the actual substitution information w.r.t. to
special situations like empty patterns or repetition of previous
substitution commands. If IMPLICIT-R is non-nil, then the \"r\" flag
is assumed, i.e. in case of an empty pattern the last search pattern
is used. It is meant for :substitute commands with arguments."
  (let (pattern replacement flags using-prev-pattern)
    (cond
     ((or (string= string "") (string-match-p "\\`[a-zA-Z]" string))
      ;; No pattern, since it starts with a letter which cannot be a
      ;; separator: Repeat last substitute.
      (setq replacement evil-ex-substitute-replacement)
      ;; flags are everything that is not whitespace
      (when (string-match "[^[:space:]]+" string)
        (setq flags (match-string 0 string))))
     (t (let ((args (evil-delimited-arguments string 3)))
          (setq pattern (pop args)
                replacement (pop args)
                flags (pop args))
          ;; if replacment equals "~" use previous replacement
          (setq replacement (if (equal replacement "~")
                                evil-ex-substitute-replacement
                              (evil-compile-replacement replacement)))
          ;; append implicit "r" flag if required
          (when (and implicit-r (not (memq ?r (append flags nil))))
            (setq flags (concat flags "r"))))))
    ;; if flags equals "&" add previous flags
    (setq flags
          (if (and (> (length flags) 0) (= (aref flags 0) ?&))
              (append (substring flags 1) evil-ex-substitute-flags)
            (append flags nil)))
    ;; if no pattern, use previous pattern, either search or
    ;; substitute pattern depending on `evil-ex-last-was-search' and
    ;; the r flag
    (when (zerop (length pattern))
      (setq pattern
            (if (eq evil-search-module 'evil-search)
                (if (and evil-ex-last-was-search (memq ?r flags))
                    (and evil-ex-search-pattern
                         (setq using-prev-pattern t)
                         (evil-ex-pattern-regex evil-ex-search-pattern))
                  (and evil-ex-substitute-pattern
                       (setq using-prev-pattern t)
                       (evil-ex-pattern-regex evil-ex-substitute-pattern)))
              (if (eq case-fold-search t)
                  isearch-string
                (concat isearch-string "\\C")))
            flags (remq ?r flags)))
    (when (and using-prev-pattern
               (not (evil-ex-pattern-ignore-case evil-ex-search-pattern)))
      (setq pattern (concat pattern "\\C")))
    ;; generate pattern
    (when pattern
      ;; Disable vim-style regexp conversion if using a previous pattern, because
      ;; this conversion will already have been done before storing it
      (let ((evil-ex-search-vim-style-regexp (and evil-ex-search-vim-style-regexp
                                                  (not using-prev-pattern))))
        (setq pattern (evil-ex-make-substitute-pattern pattern flags))))
    (list pattern replacement flags)))