[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: replace history
From: |
Juri Linkov |
Subject: |
Re: replace history |
Date: |
Thu, 27 Dec 2007 22:11:31 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/23.0.50 (gnu/linux) |
> In Emacs 22 it's great the way that you are given the last search/replace
> pair as a default for query-replace, but is there a way to get a history
> of pairs rather than alternating search item/replace item?
One way to implement this feature is to use one input string with the
ed-like syntax "s/regex/substitution/g".
Below is the simplest function that implements this. It reads such
a string from the minibuffer (with the query-replace history list and
a convenient default input string), parses it and calls perform-replace
with proper arguments querying for each replacement when the input string
contains the "g" flag.
(defun substitute-regexp (substitution)
(interactive
(list
(read-from-minibuffer "Substitute regexp: " '("s///g" . 3) nil nil
'query-replace-history nil t)))
(if (string-match "^s/\\(.*\\)/\\(.*\\)/\\([gi]*\\)" substitution)
(let* ((regex (match-string 1 substitution))
(subst (match-string 2 substitution))
(flags (match-string 3 substitution))
(case-fold-search (string-match "i" flags)))
(perform-replace
regex subst (string-match "g" flags)
t nil nil nil
(if (and transient-mark-mode mark-active) (region-beginning))
(if (and transient-mark-mode mark-active) (region-end))))
(error "Invalid syntax")))
--
Juri Linkov
http://www.jurta.org/emacs/