;;; replace.el --- replace commands for Emacs
-;; Copyright (C) 1985-1987, 1992, 1994, 1996-1997, 2000-2015 Free
+;; Copyright (C) 1985-1987, 1992, 1994, 1996-1997, 2000-2016 Free
;; Software Foundation, Inc.
;; Maintainer: emacs-devel@gnu.org
:group 'matching)
(defcustom replace-character-fold nil
- "Non-nil means `query-replace' should do character folding in matches.
+ "Non-nil means replacement commands should do character folding in matches.
This means, for instance, that \\=' will match a large variety of
-unicode quotes."
+unicode quotes.
+This variable affects `query-replace' and `replace-string', but not
+`replace-regexp'."
:type 'boolean
:group 'matching
:version "25.1")
:version "22.1")
(defcustom query-replace-show-replacement t
- "Non-nil means to show what actual replacement text will be."
+ "Non-nil means show substituted replacement text in the minibuffer.
+This variable affects only `query-replace-regexp'."
:type 'boolean
:group 'matching
:version "23.1")
to be replaced will match a sequence of whitespace chars defined by the
regexp in `search-whitespace-regexp'.
+If `replace-character-fold' is non-nil, matching uses character folding,
+i.e. it ignores diacritics and other differences between equivalent
+character strings.
+
Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
only matches surrounded by word boundaries. A negative prefix arg means
replace backward.
to be replaced will match a sequence of whitespace chars defined by the
regexp in `search-whitespace-regexp'.
+This function is not affected by `replace-character-fold'.
+
Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
only matches surrounded by word boundaries. A negative prefix arg means
replace backward.
to be replaced will match a sequence of whitespace chars defined by the
regexp in `search-whitespace-regexp'.
+This function is not affected by `replace-character-fold'.
+
Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
only matches that are surrounded by word boundaries.
Fourth and fifth arg START and END specify the region to operate on."
to be replaced will match a sequence of whitespace chars defined by the
regexp in `search-whitespace-regexp'.
+If `replace-character-fold' is non-nil, matching uses character folding,
+i.e. it ignores diacritics and other differences between equivalent
+character strings.
+
Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
only matches surrounded by word boundaries. A negative prefix arg means
replace backward.
to be replaced will match a sequence of whitespace chars defined by the
regexp in `search-whitespace-regexp'.
+This function is not affected by `replace-character-fold'
+
In Transient Mark mode, if the mark is active, operate on the contents
of the region. Otherwise, operate from point to the end of the buffer.
buf))
(buffer-list))))))
+(defun occur-regexp-descr (regexp)
+ (format " for %s\"%s\""
+ (or (get-text-property 0 'isearch-regexp-function-descr regexp)
+ "")
+ (if (get-text-property 0 'isearch-string regexp)
+ (propertize
+ (query-replace-descr
+ (get-text-property 0 'isearch-string regexp))
+ 'help-echo regexp)
+ (query-replace-descr regexp))))
+
(defun occur-1 (regexp nlines bufs &optional buf-name)
(unless (and regexp (not (equal regexp "")))
(error "Occur doesn't work with the empty regexp"))
(if (= count 1) "" "es")
;; Don't display regexp if with remaining text
;; it is longer than window-width.
- (if (> (+ (length regexp) 42) (window-width))
- "" (format-message
- " for `%s'" (query-replace-descr regexp)))))
+ (if (> (+ (length (or (get-text-property 0 'isearch-string regexp)
+ regexp))
+ 42)
+ (window-width))
+ "" (occur-regexp-descr regexp))))
(setq occur-revert-arguments (list regexp nlines bufs))
(if (= count 0)
(kill-buffer occur-buf)
;; Highlight the matches
(let ((len (length curstring))
(start 0))
+ ;; Count empty lines that don't use next loop (Bug#22062).
+ (when (zerop len)
+ (setq matches (1+ matches)))
(while (and (< start len)
(string-match regexp curstring start))
(setq matches (1+ matches))
lines (if (= lines 1) "" "s")))
;; Don't display regexp for multi-buffer.
(if (> (length buffers) 1)
- "" (format " for \"%s\""
- (query-replace-descr regexp)))
+ "" (occur-regexp-descr regexp))
(buffer-name buf))
'read-only t))
(setq end (point))
(goto-char (point-min))
(let ((beg (point))
end)
- (insert (format "%d match%s%s total for \"%s\":\n"
+ (insert (format "%d match%s%s total%s:\n"
global-matches (if (= global-matches 1) "" "es")
;; Don't display the same number of lines
;; and matches in case of 1 match per line.
(if (= global-lines global-matches)
"" (format " in %d line%s"
global-lines (if (= global-lines 1) "" "s")))
- (query-replace-descr regexp)))
+ (occur-regexp-descr regexp)))
(setq end (point))
(when title-face
(add-face-text-property beg end title-face)))
(when backward (goto-char (nth 0 match-data)))
noedit)
+(defvar replace-update-post-hook nil
+ "Function(s) to call after query-replace has found a match in the buffer.")
+
(defvar replace-search-function nil
"Function to use when searching for strings to replace.
It is used by `query-replace' and `replace-string', and is called
(and nonempty-match
(or (not regexp-flag)
(and (if backward
- (looking-back search-string)
+ (looking-back search-string nil)
(looking-at search-string))
(let ((match (match-data)))
(and (/= (nth 0 match) (nth 1 match))
;; `real-match-data'.
(while (not done)
(set-match-data real-match-data)
- (replace-highlight
+ (run-hooks 'replace-update-post-hook) ; Before `replace-highlight'.
+ (replace-highlight
(match-beginning 0) (match-end 0)
start end search-string
regexp-flag delimited-flag case-fold-search backward)