2001-07-15 19:53:53 +00:00
|
|
|
|
;;; replace.el --- replace commands for Emacs
|
1992-05-30 21:11:25 +00:00
|
|
|
|
|
2004-07-06 00:06:05 +00:00
|
|
|
|
;; Copyright (C) 1985, 1986, 1987, 1992, 1994, 1996, 1997, 2000, 2001, 2002,
|
2005-02-25 11:45:05 +00:00
|
|
|
|
;; 2003, 2004, 2005 Free Software Foundation, Inc.
|
1992-07-22 04:22:42 +00:00
|
|
|
|
|
2002-05-02 05:41:46 +00:00
|
|
|
|
;; Maintainer: FSF
|
|
|
|
|
|
1990-05-11 20:07:49 +00:00
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
|
|
|
|
|
;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
1992-06-30 13:54:21 +00:00
|
|
|
|
;; the Free Software Foundation; either version 2, or (at your option)
|
1990-05-11 20:07:49 +00:00
|
|
|
|
;; any later version.
|
|
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
1996-01-14 07:34:30 +00:00
|
|
|
|
;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
|
|
|
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
|
;; Boston, MA 02111-1307, USA.
|
1990-05-11 20:07:49 +00:00
|
|
|
|
|
1993-03-22 16:53:22 +00:00
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
|
|
;; This package supplies the string and regular-expression replace functions
|
|
|
|
|
;; documented in the Emacs user's manual.
|
|
|
|
|
|
1992-07-15 22:39:32 +00:00
|
|
|
|
;;; Code:
|
1990-05-11 20:07:49 +00:00
|
|
|
|
|
1997-05-05 15:00:53 +00:00
|
|
|
|
(defcustom case-replace t
|
2001-06-14 08:17:19 +00:00
|
|
|
|
"*Non-nil means `query-replace' should preserve case in replacements."
|
1997-05-05 15:00:53 +00:00
|
|
|
|
:type 'boolean
|
|
|
|
|
:group 'matching)
|
1991-05-11 18:07:11 +00:00
|
|
|
|
|
1992-07-24 08:17:31 +00:00
|
|
|
|
(defvar query-replace-history nil)
|
|
|
|
|
|
2004-07-05 22:50:46 +00:00
|
|
|
|
(defvar query-replace-interactive nil
|
1994-09-20 04:26:12 +00:00
|
|
|
|
"Non-nil means `query-replace' uses the last search string.
|
2004-07-05 22:50:46 +00:00
|
|
|
|
That becomes the \"string to replace\".")
|
1994-09-20 04:26:12 +00:00
|
|
|
|
|
1998-01-30 23:34:50 +00:00
|
|
|
|
(defcustom query-replace-from-history-variable 'query-replace-history
|
2001-06-14 08:17:19 +00:00
|
|
|
|
"History list to use for the FROM argument of `query-replace' commands.
|
1998-01-30 23:34:50 +00:00
|
|
|
|
The value of this variable should be a symbol; that symbol
|
|
|
|
|
is used as a variable to hold a history list for the strings
|
|
|
|
|
or patterns to be replaced."
|
|
|
|
|
:group 'matching
|
1998-04-20 02:34:53 +00:00
|
|
|
|
:type 'symbol
|
|
|
|
|
:version "20.3")
|
1998-01-30 23:34:50 +00:00
|
|
|
|
|
|
|
|
|
(defcustom query-replace-to-history-variable 'query-replace-history
|
2001-06-14 08:17:19 +00:00
|
|
|
|
"History list to use for the TO argument of `query-replace' commands.
|
1998-01-30 23:34:50 +00:00
|
|
|
|
The value of this variable should be a symbol; that symbol
|
|
|
|
|
is used as a variable to hold a history list for replacement
|
|
|
|
|
strings or patterns."
|
|
|
|
|
:group 'matching
|
1998-04-20 02:34:53 +00:00
|
|
|
|
:type 'symbol
|
|
|
|
|
:version "20.3")
|
1998-01-30 23:34:50 +00:00
|
|
|
|
|
2001-11-11 20:05:39 +00:00
|
|
|
|
(defcustom query-replace-skip-read-only nil
|
|
|
|
|
"*Non-nil means `query-replace' and friends ignore read-only matches."
|
|
|
|
|
:type 'boolean
|
|
|
|
|
:group 'matching
|
2005-02-09 15:50:47 +00:00
|
|
|
|
:version "22.1")
|
2001-11-11 20:05:39 +00:00
|
|
|
|
|
2005-01-11 23:05:16 +00:00
|
|
|
|
(defcustom query-replace-highlight t
|
|
|
|
|
"*Non-nil means to highlight matches during query replacement."
|
|
|
|
|
:type 'boolean
|
|
|
|
|
:group 'matching)
|
|
|
|
|
|
|
|
|
|
(defcustom query-replace-lazy-highlight t
|
|
|
|
|
"*Controls the lazy-highlighting during query replacements.
|
|
|
|
|
When non-nil, all text in the buffer matching the current match
|
|
|
|
|
is highlighted lazily using isearch lazy highlighting (see
|
|
|
|
|
`lazy-highlight-initial-delay' and `lazy-highlight-interval')."
|
|
|
|
|
:type 'boolean
|
|
|
|
|
:group 'lazy-highlight
|
|
|
|
|
:group 'matching
|
2005-02-09 15:50:47 +00:00
|
|
|
|
:version "22.1")
|
2005-01-11 23:05:16 +00:00
|
|
|
|
|
|
|
|
|
(defface query-replace
|
|
|
|
|
'((t (:inherit isearch)))
|
|
|
|
|
"Face for highlighting query replacement matches."
|
|
|
|
|
:group 'matching
|
2005-02-09 15:50:47 +00:00
|
|
|
|
:version "22.1")
|
2005-01-11 23:05:16 +00:00
|
|
|
|
|
2004-07-06 00:06:05 +00:00
|
|
|
|
(defun query-replace-descr (string)
|
|
|
|
|
(mapconcat 'isearch-text-char-description string ""))
|
|
|
|
|
|
2004-07-05 23:12:28 +00:00
|
|
|
|
(defun query-replace-read-from (string regexp-flag)
|
2004-07-06 00:06:05 +00:00
|
|
|
|
"Query and return the `from' argument of a query-replace operation.
|
|
|
|
|
The return value can also be a pair (FROM . TO) indicating that the user
|
|
|
|
|
wants to replace FROM with TO."
|
2004-07-05 23:12:28 +00:00
|
|
|
|
(if query-replace-interactive
|
|
|
|
|
(car (if regexp-flag regexp-search-ring search-ring))
|
2004-07-06 00:06:05 +00:00
|
|
|
|
(let* ((lastfrom (car (symbol-value query-replace-from-history-variable)))
|
|
|
|
|
(lastto (car (symbol-value query-replace-to-history-variable)))
|
|
|
|
|
(from
|
2004-07-05 23:12:28 +00:00
|
|
|
|
;; The save-excursion here is in case the user marks and copies
|
|
|
|
|
;; a region in order to specify the minibuffer input.
|
|
|
|
|
;; That should not clobber the region for the query-replace itself.
|
|
|
|
|
(save-excursion
|
2004-07-06 00:06:05 +00:00
|
|
|
|
(when (equal lastfrom lastto)
|
|
|
|
|
;; Typically, this is because the two histlists are shared.
|
|
|
|
|
(setq lastfrom (cadr (symbol-value
|
|
|
|
|
query-replace-from-history-variable))))
|
2004-07-05 23:12:28 +00:00
|
|
|
|
(read-from-minibuffer
|
2004-07-06 00:06:05 +00:00
|
|
|
|
(if (and lastto lastfrom)
|
|
|
|
|
(format "%s (default %s -> %s): " string
|
|
|
|
|
(query-replace-descr lastfrom)
|
|
|
|
|
(query-replace-descr lastto))
|
|
|
|
|
(format "%s: " string))
|
2004-07-05 23:12:28 +00:00
|
|
|
|
nil nil nil
|
|
|
|
|
query-replace-from-history-variable
|
2004-11-20 22:09:50 +00:00
|
|
|
|
nil t t))))
|
2004-07-06 00:06:05 +00:00
|
|
|
|
(if (and (zerop (length from)) lastto lastfrom)
|
2005-02-19 13:12:21 +00:00
|
|
|
|
(progn
|
|
|
|
|
(set query-replace-from-history-variable
|
2005-02-25 11:45:05 +00:00
|
|
|
|
(cdr (symbol-value query-replace-from-history-variable)))
|
|
|
|
|
(cons lastfrom
|
|
|
|
|
(query-replace-compile-replacement lastto regexp-flag)))
|
2004-07-06 00:06:05 +00:00
|
|
|
|
;; Warn if user types \n or \t, but don't reject the input.
|
|
|
|
|
(and regexp-flag
|
|
|
|
|
(string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\[nt]\\)" from)
|
|
|
|
|
(let ((match (match-string 3 from)))
|
|
|
|
|
(cond
|
|
|
|
|
((string= match "\\n")
|
|
|
|
|
(message "Note: `\\n' here doesn't match a newline; to do that, type C-q C-j instead"))
|
|
|
|
|
((string= match "\\t")
|
|
|
|
|
(message "Note: `\\t' here doesn't match a tab; to do that, just type TAB")))
|
|
|
|
|
(sit-for 2)))
|
|
|
|
|
from))))
|
2004-07-05 23:12:28 +00:00
|
|
|
|
|
2004-08-01 12:59:09 +00:00
|
|
|
|
(defun query-replace-compile-replacement (to regexp-flag)
|
|
|
|
|
"Maybe convert a regexp replacement TO to Lisp.
|
|
|
|
|
Returns a list suitable for `perform-replace' if necessary,
|
|
|
|
|
the original string if not."
|
|
|
|
|
(if (and regexp-flag
|
|
|
|
|
(string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]" to))
|
2004-06-24 10:26:24 +00:00
|
|
|
|
(let (pos list char)
|
|
|
|
|
(while
|
|
|
|
|
(progn
|
|
|
|
|
(setq pos (match-end 0))
|
|
|
|
|
(push (substring to 0 (- pos 2)) list)
|
|
|
|
|
(setq char (aref to (1- pos))
|
|
|
|
|
to (substring to pos))
|
|
|
|
|
(cond ((eq char ?\#)
|
|
|
|
|
(push '(number-to-string replace-count) list))
|
|
|
|
|
((eq char ?\,)
|
|
|
|
|
(setq pos (read-from-string to))
|
|
|
|
|
(push `(replace-quote ,(car pos)) list)
|
2004-07-02 23:52:14 +00:00
|
|
|
|
(let ((end
|
|
|
|
|
;; Swallow a space after a symbol
|
|
|
|
|
;; if there is a space.
|
|
|
|
|
(if (and (or (symbolp (car pos))
|
|
|
|
|
;; Swallow a space after 'foo
|
|
|
|
|
;; but not after (quote foo).
|
|
|
|
|
(and (eq (car-safe (car pos)) 'quote)
|
2004-07-03 05:18:38 +00:00
|
|
|
|
(not (= ?\( (aref to 0)))))
|
|
|
|
|
(eq (string-match " " to (cdr pos))
|
|
|
|
|
(cdr pos)))
|
2004-07-02 23:52:14 +00:00
|
|
|
|
(1+ (cdr pos))
|
|
|
|
|
(cdr pos))))
|
|
|
|
|
(setq to (substring to end)))))
|
2004-06-24 10:26:24 +00:00
|
|
|
|
(string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]" to)))
|
2004-08-01 12:59:09 +00:00
|
|
|
|
(setq to (nreverse (delete "" (cons to list))))
|
|
|
|
|
(replace-match-string-symbols to)
|
|
|
|
|
(cons 'replace-eval-replacement
|
|
|
|
|
(if (cdr to)
|
|
|
|
|
(cons 'concat to)
|
|
|
|
|
(car to))))
|
2004-07-05 23:12:28 +00:00
|
|
|
|
to))
|
|
|
|
|
|
2004-08-01 12:59:09 +00:00
|
|
|
|
|
|
|
|
|
(defun query-replace-read-to (from string regexp-flag)
|
|
|
|
|
"Query and return the `to' argument of a query-replace operation."
|
|
|
|
|
(query-replace-compile-replacement
|
|
|
|
|
(save-excursion
|
|
|
|
|
(read-from-minibuffer
|
|
|
|
|
(format "%s %s with: " string (query-replace-descr from))
|
|
|
|
|
nil nil nil
|
2004-11-20 22:09:50 +00:00
|
|
|
|
query-replace-to-history-variable from t t))
|
2004-08-01 12:59:09 +00:00
|
|
|
|
regexp-flag))
|
|
|
|
|
|
2004-07-05 23:12:28 +00:00
|
|
|
|
(defun query-replace-read-args (string regexp-flag &optional noerror)
|
|
|
|
|
(unless noerror
|
|
|
|
|
(barf-if-buffer-read-only))
|
|
|
|
|
(let* ((from (query-replace-read-from string regexp-flag))
|
2004-07-06 00:06:05 +00:00
|
|
|
|
(to (if (consp from) (prog1 (cdr from) (setq from (car from)))
|
|
|
|
|
(query-replace-read-to from string regexp-flag))))
|
2003-02-24 16:45:47 +00:00
|
|
|
|
(list from to current-prefix-arg)))
|
1992-07-24 08:17:31 +00:00
|
|
|
|
|
2000-04-25 19:19:31 +00:00
|
|
|
|
(defun query-replace (from-string to-string &optional delimited start end)
|
1991-05-10 19:36:23 +00:00
|
|
|
|
"Replace some occurrences of FROM-STRING with TO-STRING.
|
|
|
|
|
As each match is found, the user must type a character saying
|
|
|
|
|
what to do with it. For directions, type \\[help-command] at that time.
|
|
|
|
|
|
1997-11-11 03:26:55 +00:00
|
|
|
|
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.
|
|
|
|
|
|
1994-09-20 04:26:12 +00:00
|
|
|
|
If `query-replace-interactive' is non-nil, the last incremental search
|
|
|
|
|
string is used as FROM-STRING--you don't have to specify it with the
|
|
|
|
|
minibuffer.
|
|
|
|
|
|
2002-11-06 05:08:16 +00:00
|
|
|
|
Matching is independent of case if `case-fold-search' is non-nil and
|
|
|
|
|
FROM-STRING has no uppercase letters. Replacement transfers the case
|
|
|
|
|
pattern of the old text to the new text, if `case-replace' and
|
|
|
|
|
`case-fold-search' are non-nil and FROM-STRING has no uppercase
|
|
|
|
|
letters. \(Transferring the case pattern means that if the old text
|
|
|
|
|
matched is all caps, or capitalized, then its replacement is upcased
|
|
|
|
|
or capitalized.)
|
1994-12-04 12:52:52 +00:00
|
|
|
|
|
1994-04-06 21:19:48 +00:00
|
|
|
|
Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
|
1993-03-09 19:51:29 +00:00
|
|
|
|
only matches surrounded by word boundaries.
|
2000-04-25 19:19:31 +00:00
|
|
|
|
Fourth and fifth arg START and END specify the region to operate on.
|
1993-03-09 19:51:29 +00:00
|
|
|
|
|
|
|
|
|
To customize possible responses, change the \"bindings\" in `query-replace-map'."
|
2003-02-24 16:45:47 +00:00
|
|
|
|
(interactive (let ((common
|
2005-02-19 21:08:43 +00:00
|
|
|
|
(query-replace-read-args
|
|
|
|
|
(if (and transient-mark-mode mark-active)
|
|
|
|
|
"Query replace in region"
|
|
|
|
|
"Query replace")
|
|
|
|
|
nil)))
|
2003-02-24 16:45:47 +00:00
|
|
|
|
(list (nth 0 common) (nth 1 common) (nth 2 common)
|
|
|
|
|
;; These are done separately here
|
|
|
|
|
;; so that command-history will record these expressions
|
|
|
|
|
;; rather than the values they had this time.
|
|
|
|
|
(if (and transient-mark-mode mark-active)
|
|
|
|
|
(region-beginning))
|
|
|
|
|
(if (and transient-mark-mode mark-active)
|
|
|
|
|
(region-end)))))
|
2001-10-24 12:35:48 +00:00
|
|
|
|
(perform-replace from-string to-string t nil delimited nil nil start end))
|
1997-11-11 03:26:55 +00:00
|
|
|
|
|
1991-05-13 22:05:10 +00:00
|
|
|
|
(define-key esc-map "%" 'query-replace)
|
1991-05-10 19:36:23 +00:00
|
|
|
|
|
2000-04-25 19:19:31 +00:00
|
|
|
|
(defun query-replace-regexp (regexp to-string &optional delimited start end)
|
1991-05-10 19:36:23 +00:00
|
|
|
|
"Replace some things after point matching REGEXP with TO-STRING.
|
|
|
|
|
As each match is found, the user must type a character saying
|
|
|
|
|
what to do with it. For directions, type \\[help-command] at that time.
|
|
|
|
|
|
1997-11-11 03:26:55 +00:00
|
|
|
|
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.
|
|
|
|
|
|
1994-09-20 04:26:12 +00:00
|
|
|
|
If `query-replace-interactive' is non-nil, the last incremental search
|
|
|
|
|
regexp is used as REGEXP--you don't have to specify it with the
|
|
|
|
|
minibuffer.
|
|
|
|
|
|
2002-11-06 05:08:16 +00:00
|
|
|
|
Matching is independent of case if `case-fold-search' is non-nil and
|
|
|
|
|
REGEXP has no uppercase letters. Replacement transfers the case
|
|
|
|
|
pattern of the old text to the new text, if `case-replace' and
|
|
|
|
|
`case-fold-search' are non-nil and REGEXP has no uppercase letters.
|
|
|
|
|
\(Transferring the case pattern means that if the old text matched is
|
|
|
|
|
all caps, or capitalized, then its replacement is upcased or
|
|
|
|
|
capitalized.)
|
2000-04-25 19:19:31 +00:00
|
|
|
|
|
1994-04-06 21:19:48 +00:00
|
|
|
|
Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
|
1991-05-10 19:36:23 +00:00
|
|
|
|
only matches surrounded by word boundaries.
|
2000-04-25 19:19:31 +00:00
|
|
|
|
Fourth and fifth arg START and END specify the region to operate on.
|
|
|
|
|
|
1994-04-06 21:19:48 +00:00
|
|
|
|
In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP,
|
|
|
|
|
and `\\=\\N' (where N is a digit) stands for
|
2004-06-17 14:44:02 +00:00
|
|
|
|
whatever what matched the Nth `\\(...\\)' in REGEXP.
|
2004-06-24 10:26:24 +00:00
|
|
|
|
`\\?' lets you edit the replacement text in the minibuffer
|
|
|
|
|
at the given position for each replacement.
|
|
|
|
|
|
2004-06-24 23:33:59 +00:00
|
|
|
|
In interactive calls, the replacement text can contain `\\,'
|
|
|
|
|
followed by a Lisp expression. Each
|
|
|
|
|
replacement evaluates that expression to compute the replacement
|
|
|
|
|
string. Inside of that expression, `\\&' is a string denoting the
|
2004-07-03 05:18:38 +00:00
|
|
|
|
whole match as a string, `\\N' for a partial match, `\\#&' and `\\#N'
|
2004-06-24 23:33:59 +00:00
|
|
|
|
for the whole or a partial match converted to a number with
|
|
|
|
|
`string-to-number', and `\\#' itself for the number of replacements
|
|
|
|
|
done so far (starting with zero).
|
2004-06-24 10:26:24 +00:00
|
|
|
|
|
2004-06-24 23:33:59 +00:00
|
|
|
|
If the replacement expression is a symbol, write a space after it
|
|
|
|
|
to terminate it. One space there, if any, will be discarded.
|
2004-06-24 10:26:24 +00:00
|
|
|
|
|
|
|
|
|
When using those Lisp features interactively in the replacement
|
|
|
|
|
text, TO-STRING is actually made a list instead of a string.
|
|
|
|
|
Use \\[repeat-complex-command] after this command for details."
|
2003-02-24 16:45:47 +00:00
|
|
|
|
(interactive
|
|
|
|
|
(let ((common
|
2005-02-19 21:08:43 +00:00
|
|
|
|
(query-replace-read-args
|
|
|
|
|
(if (and transient-mark-mode mark-active)
|
|
|
|
|
"Query replace regexp in region"
|
|
|
|
|
"Query replace regexp")
|
|
|
|
|
t)))
|
2004-06-24 10:26:24 +00:00
|
|
|
|
(list (nth 0 common) (nth 1 common) (nth 2 common)
|
|
|
|
|
;; These are done separately here
|
|
|
|
|
;; so that command-history will record these expressions
|
|
|
|
|
;; rather than the values they had this time.
|
|
|
|
|
(if (and transient-mark-mode mark-active)
|
|
|
|
|
(region-beginning))
|
|
|
|
|
(if (and transient-mark-mode mark-active)
|
|
|
|
|
(region-end)))))
|
2001-10-24 12:35:48 +00:00
|
|
|
|
(perform-replace regexp to-string t t delimited nil nil start end))
|
2004-06-17 14:44:02 +00:00
|
|
|
|
|
1998-06-04 06:57:32 +00:00
|
|
|
|
(define-key esc-map [?\C-%] 'query-replace-regexp)
|
1991-05-10 19:36:23 +00:00
|
|
|
|
|
2000-04-25 19:19:31 +00:00
|
|
|
|
(defun query-replace-regexp-eval (regexp to-expr &optional delimited start end)
|
1999-08-03 17:50:44 +00:00
|
|
|
|
"Replace some things after point matching REGEXP with the result of TO-EXPR.
|
|
|
|
|
As each match is found, the user must type a character saying
|
|
|
|
|
what to do with it. For directions, type \\[help-command] at that time.
|
|
|
|
|
|
|
|
|
|
TO-EXPR is a Lisp expression evaluated to compute each replacement. It may
|
|
|
|
|
reference `replace-count' to get the number of replacements already made.
|
|
|
|
|
If the result of TO-EXPR is not a string, it is converted to one using
|
|
|
|
|
`prin1-to-string' with the NOESCAPE argument (which see).
|
|
|
|
|
|
|
|
|
|
For convenience, when entering TO-EXPR interactively, you can use `\\&' or
|
2002-02-18 20:30:58 +00:00
|
|
|
|
`\\0' to stand for whatever matched the whole of REGEXP, and `\\N' (where
|
|
|
|
|
N is a digit) to stand for whatever matched the Nth `\\(...\\)' in REGEXP.
|
1999-08-03 17:50:44 +00:00
|
|
|
|
Use `\\#&' or `\\#N' if you want a number instead of a string.
|
2004-06-17 14:44:02 +00:00
|
|
|
|
In interactive use, `\\#' in itself stands for `replace-count'.
|
1999-08-03 17:50:44 +00:00
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
If `query-replace-interactive' is non-nil, the last incremental search
|
|
|
|
|
regexp is used as REGEXP--you don't have to specify it with the
|
|
|
|
|
minibuffer.
|
|
|
|
|
|
|
|
|
|
Preserves case in each replacement if `case-replace' and `case-fold-search'
|
|
|
|
|
are non-nil and REGEXP has no uppercase letters.
|
2000-04-25 19:19:31 +00:00
|
|
|
|
|
1999-08-03 17:50:44 +00:00
|
|
|
|
Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
|
2001-05-02 11:29:57 +00:00
|
|
|
|
only matches that are surrounded by word boundaries.
|
2000-04-25 19:19:31 +00:00
|
|
|
|
Fourth and fifth arg START and END specify the region to operate on."
|
1999-08-03 17:50:44 +00:00
|
|
|
|
(interactive
|
2004-07-06 12:31:24 +00:00
|
|
|
|
(progn
|
2004-07-06 00:06:05 +00:00
|
|
|
|
(barf-if-buffer-read-only)
|
|
|
|
|
(let* ((from
|
|
|
|
|
;; Let-bind the history var to disable the "foo -> bar" default.
|
|
|
|
|
;; Maybe we shouldn't disable this default, but for now I'll
|
|
|
|
|
;; leave it off. --Stef
|
|
|
|
|
(let ((query-replace-to-history-variable nil))
|
|
|
|
|
(query-replace-read-from "Query replace regexp" t)))
|
2004-07-05 22:50:46 +00:00
|
|
|
|
(to (list (read-from-minibuffer
|
2004-07-06 00:06:05 +00:00
|
|
|
|
(format "Query replace regexp %s with eval: "
|
|
|
|
|
(query-replace-descr from))
|
2004-07-05 22:50:46 +00:00
|
|
|
|
nil nil t query-replace-to-history-variable from t))))
|
1999-08-03 17:50:44 +00:00
|
|
|
|
;; We make TO a list because replace-match-string-symbols requires one,
|
|
|
|
|
;; and the user might enter a single token.
|
|
|
|
|
(replace-match-string-symbols to)
|
2003-02-24 16:45:47 +00:00
|
|
|
|
(list from (car to) current-prefix-arg
|
|
|
|
|
(if (and transient-mark-mode mark-active)
|
|
|
|
|
(region-beginning))
|
|
|
|
|
(if (and transient-mark-mode mark-active)
|
2004-07-06 12:31:24 +00:00
|
|
|
|
(region-end))))))
|
2000-01-22 19:08:48 +00:00
|
|
|
|
(perform-replace regexp (cons 'replace-eval-replacement to-expr)
|
2004-02-02 12:50:49 +00:00
|
|
|
|
t 'literal delimited nil nil start end))
|
1999-08-03 17:50:44 +00:00
|
|
|
|
|
2000-04-25 19:19:31 +00:00
|
|
|
|
(defun map-query-replace-regexp (regexp to-strings &optional n start end)
|
1991-05-10 19:36:23 +00:00
|
|
|
|
"Replace some matches for REGEXP with various strings, in rotation.
|
2000-05-21 17:04:47 +00:00
|
|
|
|
The second argument TO-STRINGS contains the replacement strings,
|
|
|
|
|
separated by spaces. Third arg DELIMITED (prefix arg if interactive),
|
|
|
|
|
if non-nil, means replace only matches surrounded by word boundaries.
|
|
|
|
|
This command works like `query-replace-regexp' except that each
|
|
|
|
|
successive replacement uses the next successive replacement string,
|
1991-05-10 19:36:23 +00:00
|
|
|
|
wrapping around from the last such string to the first.
|
|
|
|
|
|
1997-11-11 03:26:55 +00:00
|
|
|
|
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.
|
|
|
|
|
|
1991-05-10 19:36:23 +00:00
|
|
|
|
Non-interactively, TO-STRINGS may be a list of replacement strings.
|
|
|
|
|
|
1994-09-20 04:26:12 +00:00
|
|
|
|
If `query-replace-interactive' is non-nil, the last incremental search
|
|
|
|
|
regexp is used as REGEXP--you don't have to specify it with the minibuffer.
|
|
|
|
|
|
1991-05-10 19:36:23 +00:00
|
|
|
|
A prefix argument N says to use each replacement string N times
|
2000-04-25 19:19:31 +00:00
|
|
|
|
before rotating to the next.
|
|
|
|
|
Fourth and fifth arg START and END specify the region to operate on."
|
1992-07-24 08:17:31 +00:00
|
|
|
|
(interactive
|
2004-07-05 22:50:46 +00:00
|
|
|
|
(let* ((from (if query-replace-interactive
|
1994-09-20 04:26:12 +00:00
|
|
|
|
(car regexp-search-ring)
|
|
|
|
|
(read-from-minibuffer "Map query replace (regexp): "
|
|
|
|
|
nil nil nil
|
1997-08-26 11:45:49 +00:00
|
|
|
|
'query-replace-history nil t)))
|
2004-07-05 22:50:46 +00:00
|
|
|
|
(to (read-from-minibuffer
|
1992-07-24 08:17:31 +00:00
|
|
|
|
(format "Query replace %s with (space-separated strings): "
|
2004-07-06 00:06:05 +00:00
|
|
|
|
(query-replace-descr from))
|
1992-07-24 08:17:31 +00:00
|
|
|
|
nil nil nil
|
2004-07-05 22:50:46 +00:00
|
|
|
|
'query-replace-history from t)))
|
2003-03-05 20:51:33 +00:00
|
|
|
|
(list from to
|
|
|
|
|
(and current-prefix-arg
|
|
|
|
|
(prefix-numeric-value current-prefix-arg))
|
2003-02-24 16:45:47 +00:00
|
|
|
|
(if (and transient-mark-mode mark-active)
|
|
|
|
|
(region-beginning))
|
|
|
|
|
(if (and transient-mark-mode mark-active)
|
|
|
|
|
(region-end)))))
|
1991-05-10 19:36:23 +00:00
|
|
|
|
(let (replacements)
|
|
|
|
|
(if (listp to-strings)
|
|
|
|
|
(setq replacements to-strings)
|
|
|
|
|
(while (/= (length to-strings) 0)
|
|
|
|
|
(if (string-match " " to-strings)
|
|
|
|
|
(setq replacements
|
|
|
|
|
(append replacements
|
|
|
|
|
(list (substring to-strings 0
|
|
|
|
|
(string-match " " to-strings))))
|
|
|
|
|
to-strings (substring to-strings
|
|
|
|
|
(1+ (string-match " " to-strings))))
|
|
|
|
|
(setq replacements (append replacements (list to-strings))
|
|
|
|
|
to-strings ""))))
|
2001-10-24 12:35:48 +00:00
|
|
|
|
(perform-replace regexp replacements t t nil n nil start end)))
|
1991-05-10 19:36:23 +00:00
|
|
|
|
|
2000-04-25 19:19:31 +00:00
|
|
|
|
(defun replace-string (from-string to-string &optional delimited start end)
|
1991-05-10 19:36:23 +00:00
|
|
|
|
"Replace occurrences of FROM-STRING with TO-STRING.
|
|
|
|
|
Preserve case in each match if `case-replace' and `case-fold-search'
|
|
|
|
|
are non-nil and FROM-STRING has no uppercase letters.
|
1994-12-04 12:52:52 +00:00
|
|
|
|
\(Preserving case means that if the string matched is all caps, or capitalized,
|
|
|
|
|
then its replacement is upcased or capitalized.)
|
|
|
|
|
|
1997-11-11 03:26:55 +00:00
|
|
|
|
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.
|
|
|
|
|
|
1994-04-06 21:19:48 +00:00
|
|
|
|
Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
|
1991-05-10 19:36:23 +00:00
|
|
|
|
only matches surrounded by word boundaries.
|
2000-04-25 19:19:31 +00:00
|
|
|
|
Fourth and fifth arg START and END specify the region to operate on.
|
1991-05-10 19:36:23 +00:00
|
|
|
|
|
1994-09-20 04:26:12 +00:00
|
|
|
|
If `query-replace-interactive' is non-nil, the last incremental search
|
|
|
|
|
string is used as FROM-STRING--you don't have to specify it with the
|
|
|
|
|
minibuffer.
|
|
|
|
|
|
1991-05-10 19:36:23 +00:00
|
|
|
|
This function is usually the wrong thing to use in a Lisp program.
|
|
|
|
|
What you probably want is a loop like this:
|
1994-04-06 21:19:48 +00:00
|
|
|
|
(while (search-forward FROM-STRING nil t)
|
|
|
|
|
(replace-match TO-STRING nil t))
|
1997-03-22 03:51:36 +00:00
|
|
|
|
which will run faster and will not set the mark or print anything.
|
|
|
|
|
\(You may need a more complex loop if FROM-STRING can match the null string
|
|
|
|
|
and TO-STRING is also null.)"
|
2003-02-24 16:45:47 +00:00
|
|
|
|
(interactive
|
|
|
|
|
(let ((common
|
2005-02-19 21:08:43 +00:00
|
|
|
|
(query-replace-read-args
|
|
|
|
|
(if (and transient-mark-mode mark-active)
|
|
|
|
|
"Replace string in region"
|
|
|
|
|
"Replace string")
|
|
|
|
|
nil)))
|
2003-02-24 16:45:47 +00:00
|
|
|
|
(list (nth 0 common) (nth 1 common) (nth 2 common)
|
|
|
|
|
(if (and transient-mark-mode mark-active)
|
|
|
|
|
(region-beginning))
|
|
|
|
|
(if (and transient-mark-mode mark-active)
|
|
|
|
|
(region-end)))))
|
2001-10-24 12:35:48 +00:00
|
|
|
|
(perform-replace from-string to-string nil nil delimited nil nil start end))
|
1991-05-10 19:36:23 +00:00
|
|
|
|
|
2000-04-25 19:19:31 +00:00
|
|
|
|
(defun replace-regexp (regexp to-string &optional delimited start end)
|
1991-05-10 19:36:23 +00:00
|
|
|
|
"Replace things after point matching REGEXP with TO-STRING.
|
1994-04-06 21:19:48 +00:00
|
|
|
|
Preserve case in each match if `case-replace' and `case-fold-search'
|
1991-05-10 19:36:23 +00:00
|
|
|
|
are non-nil and REGEXP has no uppercase letters.
|
2000-04-25 19:19:31 +00:00
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
1994-04-06 21:19:48 +00:00
|
|
|
|
Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
|
1991-05-10 19:36:23 +00:00
|
|
|
|
only matches surrounded by word boundaries.
|
2000-04-25 19:19:31 +00:00
|
|
|
|
Fourth and fifth arg START and END specify the region to operate on.
|
|
|
|
|
|
1994-04-06 21:19:48 +00:00
|
|
|
|
In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP,
|
|
|
|
|
and `\\=\\N' (where N is a digit) stands for
|
2004-06-24 10:26:24 +00:00
|
|
|
|
whatever what matched the Nth `\\(...\\)' in REGEXP.
|
|
|
|
|
`\\?' lets you edit the replacement text in the minibuffer
|
|
|
|
|
at the given position for each replacement.
|
|
|
|
|
|
|
|
|
|
In interactive calls, the replacement text may contain `\\,'
|
|
|
|
|
followed by a Lisp expression used as part of the replacement
|
|
|
|
|
text. Inside of that expression, `\\&' is a string denoting the
|
|
|
|
|
whole match, `\\N' a partial matches, `\\#&' and `\\#N' the
|
|
|
|
|
respective numeric values from `string-to-number', and `\\#'
|
|
|
|
|
itself for `replace-count', the number of replacements occured so
|
|
|
|
|
far.
|
|
|
|
|
|
|
|
|
|
If your Lisp expression is an identifier and the next letter in
|
|
|
|
|
the replacement string would be interpreted as part of it, you
|
|
|
|
|
can wrap it with an expression like `\\,(or \\#)'. Incidentally,
|
|
|
|
|
for this particular case you may also enter `\\#' in the
|
|
|
|
|
replacement text directly.
|
|
|
|
|
|
|
|
|
|
When using those Lisp features interactively in the replacement
|
|
|
|
|
text, TO-STRING is actually made a list instead of a string.
|
|
|
|
|
Use \\[repeat-complex-command] after this command for details.
|
1991-05-10 19:36:23 +00:00
|
|
|
|
|
1994-09-20 04:26:12 +00:00
|
|
|
|
If `query-replace-interactive' is non-nil, the last incremental search
|
|
|
|
|
regexp is used as REGEXP--you don't have to specify it with the minibuffer.
|
|
|
|
|
|
1991-05-10 19:36:23 +00:00
|
|
|
|
This function is usually the wrong thing to use in a Lisp program.
|
|
|
|
|
What you probably want is a loop like this:
|
|
|
|
|
(while (re-search-forward REGEXP nil t)
|
1994-04-06 21:19:48 +00:00
|
|
|
|
(replace-match TO-STRING nil nil))
|
1991-05-10 19:36:23 +00:00
|
|
|
|
which will run faster and will not set the mark or print anything."
|
2003-02-24 16:45:47 +00:00
|
|
|
|
(interactive
|
|
|
|
|
(let ((common
|
2005-02-19 21:08:43 +00:00
|
|
|
|
(query-replace-read-args
|
|
|
|
|
(if (and transient-mark-mode mark-active)
|
|
|
|
|
"Replace regexp in region"
|
|
|
|
|
"Replace regexp")
|
|
|
|
|
t)))
|
2003-02-24 16:45:47 +00:00
|
|
|
|
(list (nth 0 common) (nth 1 common) (nth 2 common)
|
|
|
|
|
(if (and transient-mark-mode mark-active)
|
|
|
|
|
(region-beginning))
|
|
|
|
|
(if (and transient-mark-mode mark-active)
|
|
|
|
|
(region-end)))))
|
2001-10-24 12:35:48 +00:00
|
|
|
|
(perform-replace regexp to-string nil t delimited nil nil start end))
|
2000-09-30 16:00:54 +00:00
|
|
|
|
|
1993-03-28 20:17:39 +00:00
|
|
|
|
|
|
|
|
|
(defvar regexp-history nil
|
|
|
|
|
"History list for some commands that read regular expressions.")
|
1991-05-10 19:36:23 +00:00
|
|
|
|
|
2000-09-30 16:00:54 +00:00
|
|
|
|
|
1993-04-23 06:51:44 +00:00
|
|
|
|
(defalias 'delete-non-matching-lines 'keep-lines)
|
2000-09-30 16:00:54 +00:00
|
|
|
|
(defalias 'delete-matching-lines 'flush-lines)
|
|
|
|
|
(defalias 'count-matches 'how-many)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun keep-lines-read-args (prompt)
|
|
|
|
|
"Read arguments for `keep-lines' and friends.
|
|
|
|
|
Prompt for a regexp with PROMPT.
|
2001-06-14 20:54:34 +00:00
|
|
|
|
Value is a list, (REGEXP)."
|
|
|
|
|
(list (read-from-minibuffer prompt nil nil nil
|
|
|
|
|
'regexp-history nil t)))
|
2000-09-30 16:00:54 +00:00
|
|
|
|
|
|
|
|
|
(defun keep-lines (regexp &optional rstart rend)
|
1990-05-11 20:07:49 +00:00
|
|
|
|
"Delete all lines except those containing matches for REGEXP.
|
|
|
|
|
A match split across lines preserves all the lines it lies in.
|
1998-10-14 12:47:18 +00:00
|
|
|
|
Applies to all lines after point.
|
|
|
|
|
|
|
|
|
|
If REGEXP contains upper case characters (excluding those preceded by `\\'),
|
2000-09-30 16:00:54 +00:00
|
|
|
|
the matching is case-sensitive.
|
|
|
|
|
|
|
|
|
|
Second and third arg RSTART and REND specify the region to operate on.
|
|
|
|
|
|
2001-06-14 20:54:34 +00:00
|
|
|
|
Interactively, in Transient Mark mode when the mark is active, operate
|
|
|
|
|
on the contents of the region. Otherwise, operate from point to the
|
|
|
|
|
end of the buffer."
|
|
|
|
|
|
2000-09-30 16:00:54 +00:00
|
|
|
|
(interactive
|
2002-07-18 15:00:56 +00:00
|
|
|
|
(progn
|
|
|
|
|
(barf-if-buffer-read-only)
|
|
|
|
|
(keep-lines-read-args "Keep lines (containing match for regexp): ")))
|
2000-09-30 16:00:54 +00:00
|
|
|
|
(if rstart
|
2002-07-12 23:28:15 +00:00
|
|
|
|
(progn
|
|
|
|
|
(goto-char (min rstart rend))
|
|
|
|
|
(setq rend (copy-marker (max rstart rend))))
|
2001-06-14 20:54:34 +00:00
|
|
|
|
(if (and transient-mark-mode mark-active)
|
|
|
|
|
(setq rstart (region-beginning)
|
|
|
|
|
rend (copy-marker (region-end)))
|
|
|
|
|
(setq rstart (point)
|
|
|
|
|
rend (point-max-marker)))
|
|
|
|
|
(goto-char rstart))
|
1990-05-11 20:07:49 +00:00
|
|
|
|
(save-excursion
|
|
|
|
|
(or (bolp) (forward-line 1))
|
1998-10-14 12:47:18 +00:00
|
|
|
|
(let ((start (point))
|
|
|
|
|
(case-fold-search (and case-fold-search
|
|
|
|
|
(isearch-no-upper-case-p regexp t))))
|
2000-09-30 16:00:54 +00:00
|
|
|
|
(while (< (point) rend)
|
1990-05-11 20:07:49 +00:00
|
|
|
|
;; Start is first char not preserved by previous match.
|
2000-09-30 16:00:54 +00:00
|
|
|
|
(if (not (re-search-forward regexp rend 'move))
|
|
|
|
|
(delete-region start rend)
|
1990-05-11 20:07:49 +00:00
|
|
|
|
(let ((end (save-excursion (goto-char (match-beginning 0))
|
|
|
|
|
(beginning-of-line)
|
|
|
|
|
(point))))
|
|
|
|
|
;; Now end is first char preserved by the new match.
|
|
|
|
|
(if (< start end)
|
|
|
|
|
(delete-region start end))))
|
2002-07-25 19:31:25 +00:00
|
|
|
|
|
2000-09-30 16:00:54 +00:00
|
|
|
|
(setq start (save-excursion (forward-line 1) (point)))
|
1990-05-11 20:07:49 +00:00
|
|
|
|
;; If the match was empty, avoid matching again at same place.
|
2000-09-30 16:00:54 +00:00
|
|
|
|
(and (< (point) rend)
|
|
|
|
|
(= (match-beginning 0) (match-end 0))
|
1990-05-11 20:07:49 +00:00
|
|
|
|
(forward-char 1))))))
|
|
|
|
|
|
2000-09-30 16:00:54 +00:00
|
|
|
|
|
|
|
|
|
(defun flush-lines (regexp &optional rstart rend)
|
1990-05-11 20:07:49 +00:00
|
|
|
|
"Delete lines containing matches for REGEXP.
|
|
|
|
|
If a match is split across lines, all the lines it lies in are deleted.
|
1998-10-14 12:47:18 +00:00
|
|
|
|
Applies to lines after point.
|
|
|
|
|
|
|
|
|
|
If REGEXP contains upper case characters (excluding those preceded by `\\'),
|
2000-09-30 16:00:54 +00:00
|
|
|
|
the matching is case-sensitive.
|
|
|
|
|
|
|
|
|
|
Second and third arg RSTART and REND specify the region to operate on.
|
|
|
|
|
|
2001-06-14 20:54:34 +00:00
|
|
|
|
Interactively, in Transient Mark mode when the mark is active, operate
|
|
|
|
|
on the contents of the region. Otherwise, operate from point to the
|
|
|
|
|
end of the buffer."
|
|
|
|
|
|
2000-09-30 16:00:54 +00:00
|
|
|
|
(interactive
|
2002-07-18 15:00:56 +00:00
|
|
|
|
(progn
|
|
|
|
|
(barf-if-buffer-read-only)
|
|
|
|
|
(keep-lines-read-args "Flush lines (containing match for regexp): ")))
|
2000-09-30 16:00:54 +00:00
|
|
|
|
(if rstart
|
2002-07-12 23:28:15 +00:00
|
|
|
|
(progn
|
|
|
|
|
(goto-char (min rstart rend))
|
|
|
|
|
(setq rend (copy-marker (max rstart rend))))
|
2001-06-14 20:54:34 +00:00
|
|
|
|
(if (and transient-mark-mode mark-active)
|
|
|
|
|
(setq rstart (region-beginning)
|
|
|
|
|
rend (copy-marker (region-end)))
|
|
|
|
|
(setq rstart (point)
|
|
|
|
|
rend (point-max-marker)))
|
|
|
|
|
(goto-char rstart))
|
1998-10-14 12:47:18 +00:00
|
|
|
|
(let ((case-fold-search (and case-fold-search
|
|
|
|
|
(isearch-no-upper-case-p regexp t))))
|
|
|
|
|
(save-excursion
|
2000-09-30 16:00:54 +00:00
|
|
|
|
(while (and (< (point) rend)
|
|
|
|
|
(re-search-forward regexp rend t))
|
1998-10-14 12:47:18 +00:00
|
|
|
|
(delete-region (save-excursion (goto-char (match-beginning 0))
|
|
|
|
|
(beginning-of-line)
|
|
|
|
|
(point))
|
|
|
|
|
(progn (forward-line 1) (point)))))))
|
1990-05-11 20:07:49 +00:00
|
|
|
|
|
2000-09-30 16:00:54 +00:00
|
|
|
|
|
|
|
|
|
(defun how-many (regexp &optional rstart rend)
|
1998-10-14 12:47:18 +00:00
|
|
|
|
"Print number of matches for REGEXP following point.
|
|
|
|
|
|
|
|
|
|
If REGEXP contains upper case characters (excluding those preceded by `\\'),
|
2000-09-30 16:00:54 +00:00
|
|
|
|
the matching is case-sensitive.
|
|
|
|
|
|
|
|
|
|
Second and third arg RSTART and REND specify the region to operate on.
|
|
|
|
|
|
2001-06-14 20:54:34 +00:00
|
|
|
|
Interactively, in Transient Mark mode when the mark is active, operate
|
|
|
|
|
on the contents of the region. Otherwise, operate from point to the
|
|
|
|
|
end of the buffer."
|
|
|
|
|
|
2000-09-30 16:00:54 +00:00
|
|
|
|
(interactive
|
|
|
|
|
(keep-lines-read-args "How many matches for (regexp): "))
|
2001-05-20 16:10:39 +00:00
|
|
|
|
(save-excursion
|
|
|
|
|
(if rstart
|
|
|
|
|
(goto-char (min rstart rend))
|
2001-06-14 20:54:34 +00:00
|
|
|
|
(if (and transient-mark-mode mark-active)
|
|
|
|
|
(setq rstart (region-beginning)
|
|
|
|
|
rend (copy-marker (region-end)))
|
|
|
|
|
(setq rstart (point)
|
|
|
|
|
rend (point-max-marker)))
|
|
|
|
|
(goto-char rstart))
|
2001-05-20 16:10:39 +00:00
|
|
|
|
(let ((count 0)
|
|
|
|
|
opoint
|
|
|
|
|
(case-fold-search (and case-fold-search
|
|
|
|
|
(isearch-no-upper-case-p regexp t))))
|
|
|
|
|
(while (and (< (point) rend)
|
|
|
|
|
(progn (setq opoint (point))
|
|
|
|
|
(re-search-forward regexp rend t)))
|
|
|
|
|
(if (= opoint (point))
|
|
|
|
|
(forward-char 1)
|
|
|
|
|
(setq count (1+ count))))
|
|
|
|
|
(message "%d occurrences" count))))
|
2000-09-30 16:00:54 +00:00
|
|
|
|
|
1993-03-28 20:17:39 +00:00
|
|
|
|
|
2001-05-20 16:10:39 +00:00
|
|
|
|
(defvar occur-mode-map
|
|
|
|
|
(let ((map (make-sparse-keymap)))
|
|
|
|
|
(define-key map [mouse-2] 'occur-mode-mouse-goto)
|
|
|
|
|
(define-key map "\C-c\C-c" 'occur-mode-goto-occurrence)
|
|
|
|
|
(define-key map "\C-m" 'occur-mode-goto-occurrence)
|
2002-05-21 21:01:14 +00:00
|
|
|
|
(define-key map "o" 'occur-mode-goto-occurrence-other-window)
|
2001-12-24 16:27:20 +00:00
|
|
|
|
(define-key map "\C-o" 'occur-mode-display-occurrence)
|
2001-05-20 16:10:39 +00:00
|
|
|
|
(define-key map "\M-n" 'occur-next)
|
|
|
|
|
(define-key map "\M-p" 'occur-prev)
|
2002-07-25 19:31:25 +00:00
|
|
|
|
(define-key map "r" 'occur-rename-buffer)
|
|
|
|
|
(define-key map "c" 'clone-buffer)
|
2001-05-20 16:10:39 +00:00
|
|
|
|
(define-key map "g" 'revert-buffer)
|
2002-07-25 19:31:25 +00:00
|
|
|
|
(define-key map "q" 'quit-window)
|
|
|
|
|
(define-key map "z" 'kill-this-buffer)
|
2004-09-08 20:24:12 +00:00
|
|
|
|
(define-key map "\C-c\C-f" 'next-error-follow-minor-mode)
|
2001-05-20 16:10:39 +00:00
|
|
|
|
map)
|
|
|
|
|
"Keymap for `occur-mode'.")
|
1990-05-11 20:07:49 +00:00
|
|
|
|
|
2002-04-28 17:48:31 +00:00
|
|
|
|
(defvar occur-revert-arguments nil
|
|
|
|
|
"Arguments to pass to `occur-1' to revert an Occur mode buffer.
|
|
|
|
|
See `occur-revert-function'.")
|
1990-05-11 20:07:49 +00:00
|
|
|
|
|
2002-08-29 21:40:28 +00:00
|
|
|
|
(defcustom occur-mode-hook '(turn-on-font-lock)
|
|
|
|
|
"Hook run when entering Occur mode."
|
|
|
|
|
:type 'hook
|
|
|
|
|
:group 'matching)
|
|
|
|
|
|
|
|
|
|
(defcustom occur-hook nil
|
|
|
|
|
"Hook run when `occur' is called."
|
2002-05-21 21:01:14 +00:00
|
|
|
|
:type 'hook
|
|
|
|
|
:group 'matching)
|
|
|
|
|
|
1997-05-05 01:11:30 +00:00
|
|
|
|
(put 'occur-mode 'mode-class 'special)
|
2001-11-20 23:28:57 +00:00
|
|
|
|
(defun occur-mode ()
|
1990-05-11 20:07:49 +00:00
|
|
|
|
"Major mode for output from \\[occur].
|
1994-12-27 03:41:48 +00:00
|
|
|
|
\\<occur-mode-map>Move point to one of the items in this buffer, then use
|
|
|
|
|
\\[occur-mode-goto-occurrence] to go to the occurrence that the item refers to.
|
|
|
|
|
Alternatively, click \\[occur-mode-mouse-goto] on an item to go to it.
|
|
|
|
|
|
1990-05-11 20:07:49 +00:00
|
|
|
|
\\{occur-mode-map}"
|
2002-08-30 11:51:42 +00:00
|
|
|
|
(interactive)
|
2001-11-20 23:28:57 +00:00
|
|
|
|
(kill-all-local-variables)
|
|
|
|
|
(use-local-map occur-mode-map)
|
|
|
|
|
(setq major-mode 'occur-mode)
|
|
|
|
|
(setq mode-name "Occur")
|
2001-05-20 16:10:39 +00:00
|
|
|
|
(set (make-local-variable 'revert-buffer-function) 'occur-revert-function)
|
2002-08-29 21:40:28 +00:00
|
|
|
|
(make-local-variable 'occur-revert-arguments)
|
2002-09-10 16:44:17 +00:00
|
|
|
|
(add-hook 'change-major-mode-hook 'font-lock-defontify nil t)
|
2004-04-21 21:36:42 +00:00
|
|
|
|
(setq next-error-function 'occur-next-error)
|
2005-05-26 13:08:57 +00:00
|
|
|
|
(run-mode-hooks 'occur-mode-hook))
|
1990-05-11 20:07:49 +00:00
|
|
|
|
|
1997-01-13 08:45:17 +00:00
|
|
|
|
(defun occur-revert-function (ignore1 ignore2)
|
2002-04-28 17:48:31 +00:00
|
|
|
|
"Handle `revert-buffer' for Occur mode buffers."
|
2002-05-13 03:46:56 +00:00
|
|
|
|
(apply 'occur-1 (append occur-revert-arguments (list (buffer-name)))))
|
1997-01-13 08:45:17 +00:00
|
|
|
|
|
1994-03-30 17:54:38 +00:00
|
|
|
|
(defun occur-mode-mouse-goto (event)
|
|
|
|
|
"In Occur mode, go to the occurrence whose line you click on."
|
|
|
|
|
(interactive "e")
|
2002-04-28 17:48:31 +00:00
|
|
|
|
(let (pos)
|
1994-03-30 17:54:38 +00:00
|
|
|
|
(save-excursion
|
|
|
|
|
(set-buffer (window-buffer (posn-window (event-end event))))
|
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char (posn-point (event-end event)))
|
2002-04-28 17:48:31 +00:00
|
|
|
|
(setq pos (occur-mode-find-occurrence))))
|
|
|
|
|
(pop-to-buffer (marker-buffer pos))
|
|
|
|
|
(goto-char pos)))
|
1994-03-30 17:54:38 +00:00
|
|
|
|
|
|
|
|
|
(defun occur-mode-find-occurrence ()
|
2002-04-28 17:48:31 +00:00
|
|
|
|
(let ((pos (get-text-property (point) 'occur-target)))
|
|
|
|
|
(unless pos
|
2002-04-23 20:34:58 +00:00
|
|
|
|
(error "No occurrence on this line"))
|
2002-04-28 17:48:31 +00:00
|
|
|
|
(unless (buffer-live-p (marker-buffer pos))
|
|
|
|
|
(error "Buffer for this occurrence was killed"))
|
|
|
|
|
pos))
|
1994-03-30 17:54:38 +00:00
|
|
|
|
|
|
|
|
|
(defun occur-mode-goto-occurrence ()
|
|
|
|
|
"Go to the occurrence the current line describes."
|
|
|
|
|
(interactive)
|
2002-04-28 17:48:31 +00:00
|
|
|
|
(let ((pos (occur-mode-find-occurrence)))
|
|
|
|
|
(pop-to-buffer (marker-buffer pos))
|
|
|
|
|
(goto-char pos)))
|
1997-07-23 02:52:57 +00:00
|
|
|
|
|
2002-01-08 06:12:37 +00:00
|
|
|
|
(defun occur-mode-goto-occurrence-other-window ()
|
|
|
|
|
"Go to the occurrence the current line describes, in another window."
|
|
|
|
|
(interactive)
|
2002-04-28 17:48:31 +00:00
|
|
|
|
(let ((pos (occur-mode-find-occurrence)))
|
|
|
|
|
(switch-to-buffer-other-window (marker-buffer pos))
|
|
|
|
|
(goto-char pos)))
|
2002-01-08 06:12:37 +00:00
|
|
|
|
|
2001-12-24 16:27:20 +00:00
|
|
|
|
(defun occur-mode-display-occurrence ()
|
|
|
|
|
"Display in another window the occurrence the current line describes."
|
|
|
|
|
(interactive)
|
2002-04-28 17:48:31 +00:00
|
|
|
|
(let ((pos (occur-mode-find-occurrence))
|
|
|
|
|
window
|
|
|
|
|
;; Bind these to ensure `display-buffer' puts it in another window.
|
2001-12-24 16:27:20 +00:00
|
|
|
|
same-window-buffer-names
|
2002-04-28 17:48:31 +00:00
|
|
|
|
same-window-regexps)
|
|
|
|
|
(setq window (display-buffer (marker-buffer pos)))
|
2001-12-24 16:27:20 +00:00
|
|
|
|
;; This is the way to set point in the proper window.
|
|
|
|
|
(save-selected-window
|
|
|
|
|
(select-window window)
|
2002-04-28 17:48:31 +00:00
|
|
|
|
(goto-char pos))))
|
2001-12-24 16:27:20 +00:00
|
|
|
|
|
2002-09-24 08:35:43 +00:00
|
|
|
|
(defun occur-find-match (n search message)
|
1997-07-23 02:52:57 +00:00
|
|
|
|
(if (not n) (setq n 1))
|
|
|
|
|
(let ((r))
|
|
|
|
|
(while (> n 0)
|
2002-09-24 08:35:43 +00:00
|
|
|
|
(setq r (funcall search (point) 'occur-match))
|
|
|
|
|
(and r
|
|
|
|
|
(get-text-property r 'occur-match)
|
|
|
|
|
(setq r (funcall search r 'occur-match)))
|
1997-07-23 02:52:57 +00:00
|
|
|
|
(if r
|
2002-09-24 08:35:43 +00:00
|
|
|
|
(goto-char r)
|
|
|
|
|
(error message))
|
1997-07-23 02:52:57 +00:00
|
|
|
|
(setq n (1- n)))))
|
|
|
|
|
|
2002-09-24 08:35:43 +00:00
|
|
|
|
(defun occur-next (&optional n)
|
|
|
|
|
"Move to the Nth (default 1) next match in an Occur mode buffer."
|
|
|
|
|
(interactive "p")
|
|
|
|
|
(occur-find-match n #'next-single-property-change "No more matches"))
|
|
|
|
|
|
1997-07-23 02:52:57 +00:00
|
|
|
|
(defun occur-prev (&optional n)
|
2002-04-28 17:48:31 +00:00
|
|
|
|
"Move to the Nth (default 1) previous match in an Occur mode buffer."
|
1997-07-23 02:52:57 +00:00
|
|
|
|
(interactive "p")
|
2002-09-24 08:35:43 +00:00
|
|
|
|
(occur-find-match n #'previous-single-property-change "No earlier matches"))
|
2004-04-21 21:36:42 +00:00
|
|
|
|
|
|
|
|
|
(defun occur-next-error (&optional argp reset)
|
|
|
|
|
"Move to the Nth (default 1) next match in an Occur mode buffer.
|
|
|
|
|
Compatibility function for \\[next-error] invocations."
|
|
|
|
|
(interactive "p")
|
2004-11-29 18:44:29 +00:00
|
|
|
|
;; we need to run occur-find-match from within the Occur buffer
|
2004-12-16 13:17:32 +00:00
|
|
|
|
(with-current-buffer
|
2005-01-01 22:29:54 +00:00
|
|
|
|
;; Choose the buffer and make it current.
|
2004-11-29 18:44:29 +00:00
|
|
|
|
(if (next-error-buffer-p (current-buffer))
|
|
|
|
|
(current-buffer)
|
2005-01-01 22:29:54 +00:00
|
|
|
|
(next-error-find-buffer nil nil
|
|
|
|
|
(lambda ()
|
|
|
|
|
(eq major-mode 'occur-mode))))
|
2004-12-16 13:17:32 +00:00
|
|
|
|
|
|
|
|
|
(goto-char (cond (reset (point-min))
|
|
|
|
|
((< argp 0) (line-beginning-position))
|
|
|
|
|
((line-end-position))))
|
2004-11-29 18:44:29 +00:00
|
|
|
|
(occur-find-match
|
2004-12-16 13:17:32 +00:00
|
|
|
|
(abs argp)
|
|
|
|
|
(if (> 0 argp)
|
2004-11-29 18:44:29 +00:00
|
|
|
|
#'previous-single-property-change
|
|
|
|
|
#'next-single-property-change)
|
|
|
|
|
"No more matches")
|
|
|
|
|
;; In case the *Occur* buffer is visible in a nonselected window.
|
|
|
|
|
(set-window-point (get-buffer-window (current-buffer)) (point))
|
|
|
|
|
(occur-mode-goto-occurrence)))
|
1993-03-28 20:17:39 +00:00
|
|
|
|
|
2004-12-16 07:16:17 +00:00
|
|
|
|
(defface match
|
|
|
|
|
'((((class color) (min-colors 88) (background light))
|
|
|
|
|
:background "Tan")
|
|
|
|
|
(((class color) (min-colors 88) (background dark))
|
|
|
|
|
:background "RoyalBlue4")
|
|
|
|
|
(((class color) (min-colors 8))
|
|
|
|
|
:background "blue" :foreground "white")
|
|
|
|
|
(((type tty) (class mono))
|
|
|
|
|
:inverse-video t)
|
|
|
|
|
(t :background "gray"))
|
|
|
|
|
"Face used to highlight matches permanently."
|
|
|
|
|
:group 'matching
|
2005-02-09 15:50:47 +00:00
|
|
|
|
:version "22.1")
|
2004-12-16 07:16:17 +00:00
|
|
|
|
|
1997-05-05 15:00:53 +00:00
|
|
|
|
(defcustom list-matching-lines-default-context-lines 0
|
2000-05-21 17:04:47 +00:00
|
|
|
|
"*Default number of context lines included around `list-matching-lines' matches.
|
|
|
|
|
A negative number means to include that many lines before the match.
|
1997-05-05 15:00:53 +00:00
|
|
|
|
A positive number means to include that many lines both before and after."
|
|
|
|
|
:type 'integer
|
|
|
|
|
:group 'matching)
|
1990-05-11 20:07:49 +00:00
|
|
|
|
|
1993-04-23 06:51:44 +00:00
|
|
|
|
(defalias 'list-matching-lines 'occur)
|
1990-05-11 20:07:49 +00:00
|
|
|
|
|
2004-12-16 07:16:17 +00:00
|
|
|
|
(defcustom list-matching-lines-face 'match
|
2000-05-21 17:04:47 +00:00
|
|
|
|
"*Face used by \\[list-matching-lines] to show the text that matches.
|
2002-04-23 20:34:58 +00:00
|
|
|
|
If the value is nil, don't highlight the matching portions specially."
|
|
|
|
|
:type 'face
|
|
|
|
|
:group 'matching)
|
|
|
|
|
|
|
|
|
|
(defcustom list-matching-lines-buffer-name-face 'underline
|
|
|
|
|
"*Face used by \\[list-matching-lines] to show the names of buffers.
|
|
|
|
|
If the value is nil, don't highlight the buffer names specially."
|
|
|
|
|
:type 'face
|
|
|
|
|
:group 'matching)
|
|
|
|
|
|
2004-12-03 00:19:52 +00:00
|
|
|
|
(defun occur-accumulate-lines (count &optional keep-props)
|
2002-04-23 20:34:58 +00:00
|
|
|
|
(save-excursion
|
|
|
|
|
(let ((forwardp (> count 0))
|
2004-12-16 13:17:32 +00:00
|
|
|
|
result beg end)
|
2002-04-23 20:34:58 +00:00
|
|
|
|
(while (not (or (zerop count)
|
|
|
|
|
(if forwardp
|
|
|
|
|
(eobp)
|
|
|
|
|
(bobp))))
|
2002-05-08 03:53:19 +00:00
|
|
|
|
(setq count (+ count (if forwardp -1 1)))
|
2004-12-16 13:17:32 +00:00
|
|
|
|
(setq beg (line-beginning-position)
|
|
|
|
|
end (line-end-position))
|
2005-01-15 18:10:59 +00:00
|
|
|
|
(if (and keep-props (if (boundp 'jit-lock-mode) jit-lock-mode)
|
2004-12-16 13:17:32 +00:00
|
|
|
|
(text-property-not-all beg end 'fontified t))
|
2005-01-15 18:10:59 +00:00
|
|
|
|
(if (fboundp 'jit-lock-fontify-now)
|
|
|
|
|
(jit-lock-fontify-now beg end)))
|
2002-04-23 20:34:58 +00:00
|
|
|
|
(push
|
2004-12-03 00:19:52 +00:00
|
|
|
|
(funcall (if keep-props
|
|
|
|
|
#'buffer-substring
|
|
|
|
|
#'buffer-substring-no-properties)
|
2004-12-16 13:17:32 +00:00
|
|
|
|
beg end)
|
2002-04-23 20:34:58 +00:00
|
|
|
|
result)
|
|
|
|
|
(forward-line (if forwardp 1 -1)))
|
|
|
|
|
(nreverse result))))
|
|
|
|
|
|
|
|
|
|
(defun occur-read-primary-args ()
|
|
|
|
|
(list (let* ((default (car regexp-history))
|
|
|
|
|
(input
|
|
|
|
|
(read-from-minibuffer
|
|
|
|
|
(if default
|
|
|
|
|
(format "List lines matching regexp (default `%s'): "
|
2004-07-06 00:06:05 +00:00
|
|
|
|
(query-replace-descr default))
|
2002-04-23 20:34:58 +00:00
|
|
|
|
"List lines matching regexp: ")
|
|
|
|
|
nil
|
|
|
|
|
nil
|
|
|
|
|
nil
|
2004-07-17 14:34:42 +00:00
|
|
|
|
'regexp-history
|
|
|
|
|
default)))
|
2002-04-23 20:34:58 +00:00
|
|
|
|
(if (equal input "")
|
|
|
|
|
default
|
|
|
|
|
input))
|
2002-05-15 19:35:54 +00:00
|
|
|
|
(when current-prefix-arg
|
|
|
|
|
(prefix-numeric-value current-prefix-arg))))
|
1997-01-03 03:11:11 +00:00
|
|
|
|
|
2002-07-25 19:31:25 +00:00
|
|
|
|
(defun occur-rename-buffer (&optional unique-p)
|
|
|
|
|
"Rename the current *Occur* buffer to *Occur: original-buffer-name*.
|
|
|
|
|
Here `original-buffer-name' is the buffer name were occur was originally run.
|
|
|
|
|
When given the prefix argument, the renaming will not clobber the existing
|
|
|
|
|
buffer(s) of that name, but use `generate-new-buffer-name' instead.
|
2002-08-28 15:21:33 +00:00
|
|
|
|
You can add this to `occur-hook' if you always want a separate *Occur*
|
2002-07-25 19:31:25 +00:00
|
|
|
|
buffer for each buffer where you invoke `occur'."
|
|
|
|
|
(interactive "P")
|
|
|
|
|
(with-current-buffer
|
|
|
|
|
(if (eq major-mode 'occur-mode) (current-buffer) (get-buffer "*Occur*"))
|
|
|
|
|
(rename-buffer (concat "*Occur: "
|
|
|
|
|
(mapconcat #'buffer-name
|
|
|
|
|
(car (cddr occur-revert-arguments)) "/")
|
|
|
|
|
"*")
|
|
|
|
|
unique-p)))
|
|
|
|
|
|
1990-05-11 20:07:49 +00:00
|
|
|
|
(defun occur (regexp &optional nlines)
|
1992-10-18 01:06:40 +00:00
|
|
|
|
"Show all lines in the current buffer containing a match for REGEXP.
|
1991-05-10 19:36:23 +00:00
|
|
|
|
|
|
|
|
|
If a match spreads across multiple lines, all those lines are shown.
|
1990-05-11 20:07:49 +00:00
|
|
|
|
|
1991-05-10 19:36:23 +00:00
|
|
|
|
Each line is displayed with NLINES lines before and after, or -NLINES
|
|
|
|
|
before if NLINES is negative.
|
|
|
|
|
NLINES defaults to `list-matching-lines-default-context-lines'.
|
1990-05-11 20:07:49 +00:00
|
|
|
|
Interactively it is the prefix arg.
|
|
|
|
|
|
1993-03-28 20:17:39 +00:00
|
|
|
|
The lines are shown in a buffer named `*Occur*'.
|
1990-05-11 20:07:49 +00:00
|
|
|
|
It serves as a menu to find any of the occurrences in this buffer.
|
1997-05-05 01:11:30 +00:00
|
|
|
|
\\<occur-mode-map>\\[describe-mode] in that buffer will explain how.
|
1997-05-04 01:20:08 +00:00
|
|
|
|
|
1997-05-05 01:11:30 +00:00
|
|
|
|
If REGEXP contains upper case characters (excluding those preceded by `\\'),
|
|
|
|
|
the matching is case-sensitive."
|
2002-04-23 20:34:58 +00:00
|
|
|
|
(interactive (occur-read-primary-args))
|
|
|
|
|
(occur-1 regexp nlines (list (current-buffer))))
|
|
|
|
|
|
|
|
|
|
(defun multi-occur (bufs regexp &optional nlines)
|
|
|
|
|
"Show all lines in buffers BUFS containing a match for REGEXP.
|
|
|
|
|
This function acts on multiple buffers; otherwise, it is exactly like
|
|
|
|
|
`occur'."
|
1996-04-17 17:30:51 +00:00
|
|
|
|
(interactive
|
2002-04-23 20:34:58 +00:00
|
|
|
|
(cons
|
2002-09-02 10:41:25 +00:00
|
|
|
|
(let* ((bufs (list (read-buffer "First buffer to search: "
|
|
|
|
|
(current-buffer) t)))
|
|
|
|
|
(buf nil)
|
|
|
|
|
(ido-ignore-item-temp-list bufs))
|
2002-04-23 20:34:58 +00:00
|
|
|
|
(while (not (string-equal
|
2003-02-04 12:29:42 +00:00
|
|
|
|
(setq buf (read-buffer
|
2002-09-02 10:41:25 +00:00
|
|
|
|
(if (eq read-buffer-function 'ido-read-buffer)
|
|
|
|
|
"Next buffer to search (C-j to end): "
|
|
|
|
|
"Next buffer to search (RET to end): ")
|
|
|
|
|
nil t))
|
2002-04-23 20:34:58 +00:00
|
|
|
|
""))
|
2002-09-02 10:41:25 +00:00
|
|
|
|
(add-to-list 'bufs buf)
|
|
|
|
|
(setq ido-ignore-item-temp-list bufs))
|
2002-04-23 20:34:58 +00:00
|
|
|
|
(nreverse (mapcar #'get-buffer bufs)))
|
|
|
|
|
(occur-read-primary-args)))
|
|
|
|
|
(occur-1 regexp nlines bufs))
|
|
|
|
|
|
|
|
|
|
(defun multi-occur-by-filename-regexp (bufregexp regexp &optional nlines)
|
2002-04-25 19:02:46 +00:00
|
|
|
|
"Show all lines matching REGEXP in buffers named by BUFREGEXP.
|
2002-04-23 20:34:58 +00:00
|
|
|
|
See also `multi-occur'."
|
|
|
|
|
(interactive
|
|
|
|
|
(cons
|
|
|
|
|
(let* ((default (car regexp-history))
|
|
|
|
|
(input
|
|
|
|
|
(read-from-minibuffer
|
|
|
|
|
"List lines in buffers whose filename matches regexp: "
|
|
|
|
|
nil
|
|
|
|
|
nil
|
|
|
|
|
nil
|
|
|
|
|
'regexp-history)))
|
|
|
|
|
(if (equal input "")
|
|
|
|
|
default
|
|
|
|
|
input))
|
|
|
|
|
(occur-read-primary-args)))
|
|
|
|
|
(when bufregexp
|
|
|
|
|
(occur-1 regexp nlines
|
|
|
|
|
(delq nil
|
|
|
|
|
(mapcar (lambda (buf)
|
|
|
|
|
(when (and (buffer-file-name buf)
|
|
|
|
|
(string-match bufregexp
|
|
|
|
|
(buffer-file-name buf)))
|
|
|
|
|
buf))
|
|
|
|
|
(buffer-list))))))
|
|
|
|
|
|
2002-05-13 03:46:56 +00:00
|
|
|
|
(defun occur-1 (regexp nlines bufs &optional buf-name)
|
|
|
|
|
(unless buf-name
|
|
|
|
|
(setq buf-name "*Occur*"))
|
2005-01-01 22:29:54 +00:00
|
|
|
|
(let (occur-buf
|
2002-05-02 21:22:56 +00:00
|
|
|
|
(active-bufs (delq nil (mapcar #'(lambda (buf)
|
|
|
|
|
(when (buffer-live-p buf) buf))
|
|
|
|
|
bufs))))
|
|
|
|
|
;; Handle the case where one of the buffers we're searching is the
|
2005-01-01 22:29:54 +00:00
|
|
|
|
;; output buffer. Just rename it.
|
|
|
|
|
(when (member buf-name (mapcar 'buffer-name active-bufs))
|
|
|
|
|
(with-current-buffer (get-buffer buf-name)
|
|
|
|
|
(rename-uniquely)))
|
|
|
|
|
|
|
|
|
|
;; Now find or create the output buffer.
|
|
|
|
|
;; If we just renamed that buffer, we will make a new one here.
|
|
|
|
|
(setq occur-buf (get-buffer-create buf-name))
|
|
|
|
|
|
2002-04-23 20:34:58 +00:00
|
|
|
|
(with-current-buffer occur-buf
|
|
|
|
|
(occur-mode)
|
2005-05-07 13:46:42 +00:00
|
|
|
|
(let ((inhibit-read-only t))
|
|
|
|
|
(erase-buffer)
|
|
|
|
|
(let ((count (occur-engine
|
|
|
|
|
regexp active-bufs occur-buf
|
|
|
|
|
(or nlines list-matching-lines-default-context-lines)
|
|
|
|
|
(and case-fold-search
|
|
|
|
|
(isearch-no-upper-case-p regexp t))
|
|
|
|
|
list-matching-lines-buffer-name-face
|
|
|
|
|
nil list-matching-lines-face t)))
|
|
|
|
|
(let* ((bufcount (length active-bufs))
|
|
|
|
|
(diff (- (length bufs) bufcount)))
|
|
|
|
|
(message "Searched %d buffer%s%s; %s match%s for `%s'"
|
|
|
|
|
bufcount (if (= bufcount 1) "" "s")
|
|
|
|
|
(if (zerop diff) "" (format " (%d killed)" diff))
|
|
|
|
|
(if (zerop count) "no" (format "%d" count))
|
|
|
|
|
(if (= count 1) "" "es")
|
|
|
|
|
regexp))
|
|
|
|
|
(setq occur-revert-arguments (list regexp nlines bufs))
|
|
|
|
|
(if (> count 0)
|
|
|
|
|
(progn
|
|
|
|
|
(display-buffer occur-buf)
|
|
|
|
|
(setq next-error-last-buffer occur-buf))
|
|
|
|
|
(kill-buffer occur-buf)))
|
|
|
|
|
(run-hooks 'occur-hook))
|
|
|
|
|
(setq buffer-read-only t)
|
|
|
|
|
(set-buffer-modified-p nil))))
|
2002-04-23 20:34:58 +00:00
|
|
|
|
|
2002-04-28 17:48:31 +00:00
|
|
|
|
(defun occur-engine-add-prefix (lines)
|
|
|
|
|
(mapcar
|
|
|
|
|
#'(lambda (line)
|
2002-09-18 16:03:58 +00:00
|
|
|
|
(concat " :" line "\n"))
|
2002-04-28 17:48:31 +00:00
|
|
|
|
lines))
|
|
|
|
|
|
|
|
|
|
(defun occur-engine (regexp buffers out-buf nlines case-fold-search
|
|
|
|
|
title-face prefix-face match-face keep-props)
|
|
|
|
|
(with-current-buffer out-buf
|
2002-11-13 07:06:07 +00:00
|
|
|
|
(let ((globalcount 0)
|
|
|
|
|
(coding nil))
|
2002-04-28 17:48:31 +00:00
|
|
|
|
;; Map over all the buffers
|
|
|
|
|
(dolist (buf buffers)
|
|
|
|
|
(when (buffer-live-p buf)
|
|
|
|
|
(let ((matches 0) ;; count of matched lines
|
|
|
|
|
(lines 1) ;; line count
|
|
|
|
|
(matchbeg 0)
|
|
|
|
|
(origpt nil)
|
|
|
|
|
(begpt nil)
|
|
|
|
|
(endpt nil)
|
|
|
|
|
(marker nil)
|
|
|
|
|
(curstring "")
|
|
|
|
|
(headerpt (with-current-buffer out-buf (point))))
|
|
|
|
|
(save-excursion
|
|
|
|
|
(set-buffer buf)
|
2002-11-13 07:06:07 +00:00
|
|
|
|
(or coding
|
|
|
|
|
;; Set CODING only if the current buffer locally
|
|
|
|
|
;; binds buffer-file-coding-system.
|
|
|
|
|
(not (local-variable-p 'buffer-file-coding-system))
|
|
|
|
|
(setq coding buffer-file-coding-system))
|
2002-04-23 20:34:58 +00:00
|
|
|
|
(save-excursion
|
2002-04-28 17:48:31 +00:00
|
|
|
|
(goto-char (point-min)) ;; begin searching in the buffer
|
|
|
|
|
(while (not (eobp))
|
|
|
|
|
(setq origpt (point))
|
|
|
|
|
(when (setq endpt (re-search-forward regexp nil t))
|
|
|
|
|
(setq matches (1+ matches)) ;; increment match count
|
2004-07-05 22:50:46 +00:00
|
|
|
|
(setq matchbeg (match-beginning 0))
|
2002-04-28 17:48:31 +00:00
|
|
|
|
(setq lines (+ lines (1- (count-lines origpt endpt))))
|
2004-12-16 13:17:32 +00:00
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char matchbeg)
|
|
|
|
|
(setq begpt (line-beginning-position)
|
|
|
|
|
endpt (line-end-position)))
|
2002-04-28 17:48:31 +00:00
|
|
|
|
(setq marker (make-marker))
|
|
|
|
|
(set-marker marker matchbeg)
|
2005-01-15 18:10:59 +00:00
|
|
|
|
(if (and keep-props
|
|
|
|
|
(if (boundp 'jit-lock-mode) jit-lock-mode)
|
2004-12-16 13:17:32 +00:00
|
|
|
|
(text-property-not-all begpt endpt 'fontified t))
|
2005-01-15 18:10:59 +00:00
|
|
|
|
(if (fboundp 'jit-lock-fontify-now)
|
|
|
|
|
(jit-lock-fontify-now begpt endpt)))
|
2004-12-16 13:17:32 +00:00
|
|
|
|
(setq curstring (buffer-substring begpt endpt))
|
2002-04-28 17:48:31 +00:00
|
|
|
|
;; Depropertize the string, and maybe
|
|
|
|
|
;; highlight the matches
|
|
|
|
|
(let ((len (length curstring))
|
|
|
|
|
(start 0))
|
|
|
|
|
(unless keep-props
|
|
|
|
|
(set-text-properties 0 len nil curstring))
|
|
|
|
|
(while (and (< start len)
|
|
|
|
|
(string-match regexp curstring start))
|
2004-12-16 13:17:32 +00:00
|
|
|
|
(add-text-properties
|
|
|
|
|
(match-beginning 0) (match-end 0)
|
|
|
|
|
(append
|
|
|
|
|
`(occur-match t)
|
|
|
|
|
(when match-face
|
|
|
|
|
;; Use `face' rather than `font-lock-face' here
|
|
|
|
|
;; so as to override faces copied from the buffer.
|
|
|
|
|
`(face ,match-face)))
|
|
|
|
|
curstring)
|
2002-04-28 17:48:31 +00:00
|
|
|
|
(setq start (match-end 0))))
|
|
|
|
|
;; Generate the string to insert for this match
|
|
|
|
|
(let* ((out-line
|
|
|
|
|
(concat
|
2002-09-18 16:03:58 +00:00
|
|
|
|
;; Using 7 digits aligns tabs properly.
|
|
|
|
|
(apply #'propertize (format "%7d:" lines)
|
2002-04-28 17:48:31 +00:00
|
|
|
|
(append
|
|
|
|
|
(when prefix-face
|
2002-06-08 20:44:42 +00:00
|
|
|
|
`(font-lock-face prefix-face))
|
2002-04-28 17:48:31 +00:00
|
|
|
|
'(occur-prefix t)))
|
2004-12-16 13:17:32 +00:00
|
|
|
|
;; We don't put `mouse-face' on the newline,
|
|
|
|
|
;; because that loses. And don't put it
|
|
|
|
|
;; on context lines to reduce flicker.
|
|
|
|
|
(propertize curstring 'mouse-face 'highlight)
|
2002-04-28 17:48:31 +00:00
|
|
|
|
"\n"))
|
|
|
|
|
(data
|
|
|
|
|
(if (= nlines 0)
|
|
|
|
|
;; The simple display style
|
|
|
|
|
out-line
|
|
|
|
|
;; The complex multi-line display
|
|
|
|
|
;; style. Generate a list of lines,
|
|
|
|
|
;; concatenate them all together.
|
|
|
|
|
(apply #'concat
|
|
|
|
|
(nconc
|
2004-07-24 21:34:47 +00:00
|
|
|
|
(occur-engine-add-prefix (nreverse (cdr (occur-accumulate-lines (- (1+ (abs nlines))) keep-props))))
|
2002-04-28 17:48:31 +00:00
|
|
|
|
(list out-line)
|
2004-07-24 21:34:47 +00:00
|
|
|
|
(if (> nlines 0)
|
|
|
|
|
(occur-engine-add-prefix
|
|
|
|
|
(cdr (occur-accumulate-lines (1+ nlines) keep-props)))))))))
|
2002-04-28 17:48:31 +00:00
|
|
|
|
;; Actually insert the match display data
|
|
|
|
|
(with-current-buffer out-buf
|
|
|
|
|
(let ((beg (point))
|
|
|
|
|
(end (progn (insert data) (point))))
|
|
|
|
|
(unless (= nlines 0)
|
|
|
|
|
(insert "-------\n"))
|
|
|
|
|
(add-text-properties
|
2002-05-24 00:56:46 +00:00
|
|
|
|
beg end
|
2004-12-16 13:17:32 +00:00
|
|
|
|
`(occur-target ,marker help-echo "mouse-2: go to this occurrence")))))
|
2002-04-28 17:48:31 +00:00
|
|
|
|
(goto-char endpt))
|
2002-05-13 03:46:56 +00:00
|
|
|
|
(if endpt
|
|
|
|
|
(progn
|
|
|
|
|
(setq lines (1+ lines))
|
|
|
|
|
;; On to the next match...
|
|
|
|
|
(forward-line 1))
|
|
|
|
|
(goto-char (point-max))))))
|
2002-04-28 17:48:31 +00:00
|
|
|
|
(when (not (zerop matches)) ;; is the count zero?
|
2002-05-21 21:01:14 +00:00
|
|
|
|
(setq globalcount (+ globalcount matches))
|
2002-04-28 17:48:31 +00:00
|
|
|
|
(with-current-buffer out-buf
|
|
|
|
|
(goto-char headerpt)
|
|
|
|
|
(let ((beg (point))
|
|
|
|
|
end)
|
2004-04-11 04:57:44 +00:00
|
|
|
|
(insert (format "%d match%s for \"%s\" in buffer: %s\n"
|
|
|
|
|
matches (if (= matches 1) "" "es")
|
|
|
|
|
regexp (buffer-name buf)))
|
2002-04-28 17:48:31 +00:00
|
|
|
|
(setq end (point))
|
|
|
|
|
(add-text-properties beg end
|
|
|
|
|
(append
|
|
|
|
|
(when title-face
|
2002-06-08 20:44:42 +00:00
|
|
|
|
`(font-lock-face ,title-face))
|
|
|
|
|
`(occur-title ,buf))))
|
2002-04-28 17:48:31 +00:00
|
|
|
|
(goto-char (point-min)))))))
|
2002-11-13 07:06:07 +00:00
|
|
|
|
(if coding
|
|
|
|
|
;; CODING is buffer-file-coding-system of the first buffer
|
|
|
|
|
;; that locally binds it. Let's use it also for the output
|
|
|
|
|
;; buffer.
|
|
|
|
|
(set-buffer-file-coding-system coding))
|
2002-04-28 17:48:31 +00:00
|
|
|
|
;; Return the number of matches
|
|
|
|
|
globalcount)))
|
2002-04-23 20:34:58 +00:00
|
|
|
|
|
1990-05-11 20:07:49 +00:00
|
|
|
|
|
1993-03-09 19:51:29 +00:00
|
|
|
|
;; It would be nice to use \\[...], but there is no reasonable way
|
|
|
|
|
;; to make that display both SPC and Y.
|
1990-05-11 20:07:49 +00:00
|
|
|
|
(defconst query-replace-help
|
|
|
|
|
"Type Space or `y' to replace one match, Delete or `n' to skip to next,
|
1993-12-24 22:55:03 +00:00
|
|
|
|
RET or `q' to exit, Period to replace one match and exit,
|
1990-05-11 20:07:49 +00:00
|
|
|
|
Comma to replace but not move point immediately,
|
|
|
|
|
C-r to enter recursive edit (\\[exit-recursive-edit] to get out again),
|
|
|
|
|
C-w to delete match and recursive edit,
|
|
|
|
|
C-l to clear the screen, redisplay, and offer same replacement again,
|
|
|
|
|
! to replace all remaining matches with no more questions,
|
2000-05-03 19:23:28 +00:00
|
|
|
|
^ to move point back to previous match,
|
|
|
|
|
E to edit the replacement string"
|
2001-06-14 08:17:19 +00:00
|
|
|
|
"Help message while in `query-replace'.")
|
1990-05-11 20:07:49 +00:00
|
|
|
|
|
1993-03-09 19:51:29 +00:00
|
|
|
|
(defvar query-replace-map (make-sparse-keymap)
|
|
|
|
|
"Keymap that defines the responses to questions in `query-replace'.
|
|
|
|
|
The \"bindings\" in this map are not commands; they are answers.
|
|
|
|
|
The valid answers include `act', `skip', `act-and-show',
|
|
|
|
|
`exit', `act-and-exit', `edit', `delete-and-edit', `recenter',
|
1994-11-23 09:10:36 +00:00
|
|
|
|
`automatic', `backup', `exit-prefix', and `help'.")
|
1993-03-09 19:51:29 +00:00
|
|
|
|
|
|
|
|
|
(define-key query-replace-map " " 'act)
|
|
|
|
|
(define-key query-replace-map "\d" 'skip)
|
|
|
|
|
(define-key query-replace-map [delete] 'skip)
|
1993-03-23 02:26:42 +00:00
|
|
|
|
(define-key query-replace-map [backspace] 'skip)
|
1993-03-09 19:51:29 +00:00
|
|
|
|
(define-key query-replace-map "y" 'act)
|
|
|
|
|
(define-key query-replace-map "n" 'skip)
|
1994-11-14 01:35:08 +00:00
|
|
|
|
(define-key query-replace-map "Y" 'act)
|
|
|
|
|
(define-key query-replace-map "N" 'skip)
|
2000-06-28 11:58:24 +00:00
|
|
|
|
(define-key query-replace-map "e" 'edit-replacement)
|
2000-05-03 19:23:28 +00:00
|
|
|
|
(define-key query-replace-map "E" 'edit-replacement)
|
1993-03-09 19:51:29 +00:00
|
|
|
|
(define-key query-replace-map "," 'act-and-show)
|
|
|
|
|
(define-key query-replace-map "q" 'exit)
|
1993-06-21 00:45:17 +00:00
|
|
|
|
(define-key query-replace-map "\r" 'exit)
|
1993-06-22 08:12:10 +00:00
|
|
|
|
(define-key query-replace-map [return] 'exit)
|
1993-03-09 19:51:29 +00:00
|
|
|
|
(define-key query-replace-map "." 'act-and-exit)
|
|
|
|
|
(define-key query-replace-map "\C-r" 'edit)
|
|
|
|
|
(define-key query-replace-map "\C-w" 'delete-and-edit)
|
|
|
|
|
(define-key query-replace-map "\C-l" 'recenter)
|
|
|
|
|
(define-key query-replace-map "!" 'automatic)
|
|
|
|
|
(define-key query-replace-map "^" 'backup)
|
|
|
|
|
(define-key query-replace-map "\C-h" 'help)
|
1995-06-07 20:54:21 +00:00
|
|
|
|
(define-key query-replace-map [f1] 'help)
|
|
|
|
|
(define-key query-replace-map [help] 'help)
|
1993-03-09 19:51:29 +00:00
|
|
|
|
(define-key query-replace-map "?" 'help)
|
1993-03-09 21:18:31 +00:00
|
|
|
|
(define-key query-replace-map "\C-g" 'quit)
|
|
|
|
|
(define-key query-replace-map "\C-]" 'quit)
|
1994-11-23 09:10:36 +00:00
|
|
|
|
(define-key query-replace-map "\e" 'exit-prefix)
|
|
|
|
|
(define-key query-replace-map [escape] 'exit-prefix)
|
1993-03-09 19:51:29 +00:00
|
|
|
|
|
1999-08-03 17:50:44 +00:00
|
|
|
|
(defun replace-match-string-symbols (n)
|
2000-05-21 17:04:47 +00:00
|
|
|
|
"Process a list (and any sub-lists), expanding certain symbols.
|
|
|
|
|
Symbol Expands To
|
|
|
|
|
N (match-string N) (where N is a string of digits)
|
|
|
|
|
#N (string-to-number (match-string N))
|
|
|
|
|
& (match-string 0)
|
|
|
|
|
#& (string-to-number (match-string 0))
|
2004-06-17 14:44:02 +00:00
|
|
|
|
# replace-count
|
2000-05-21 17:04:47 +00:00
|
|
|
|
|
|
|
|
|
Note that these symbols must be preceeded by a backslash in order to
|
|
|
|
|
type them."
|
1999-08-03 17:50:44 +00:00
|
|
|
|
(while n
|
|
|
|
|
(cond
|
|
|
|
|
((consp (car n))
|
|
|
|
|
(replace-match-string-symbols (car n))) ;Process sub-list
|
|
|
|
|
((symbolp (car n))
|
|
|
|
|
(let ((name (symbol-name (car n))))
|
|
|
|
|
(cond
|
|
|
|
|
((string-match "^[0-9]+$" name)
|
|
|
|
|
(setcar n (list 'match-string (string-to-number name))))
|
|
|
|
|
((string-match "^#[0-9]+$" name)
|
|
|
|
|
(setcar n (list 'string-to-number
|
|
|
|
|
(list 'match-string
|
|
|
|
|
(string-to-number (substring name 1))))))
|
|
|
|
|
((string= "&" name)
|
|
|
|
|
(setcar n '(match-string 0)))
|
|
|
|
|
((string= "#&" name)
|
2004-06-17 14:44:02 +00:00
|
|
|
|
(setcar n '(string-to-number (match-string 0))))
|
|
|
|
|
((string= "#" name)
|
|
|
|
|
(setcar n 'replace-count))))))
|
1999-08-03 17:50:44 +00:00
|
|
|
|
(setq n (cdr n))))
|
|
|
|
|
|
|
|
|
|
(defun replace-eval-replacement (expression replace-count)
|
|
|
|
|
(let ((replacement (eval expression)))
|
|
|
|
|
(if (stringp replacement)
|
|
|
|
|
replacement
|
|
|
|
|
(prin1-to-string replacement t))))
|
|
|
|
|
|
2004-06-17 14:44:02 +00:00
|
|
|
|
(defun replace-quote (replacement)
|
|
|
|
|
"Quote a replacement string.
|
|
|
|
|
This just doubles all backslashes in REPLACEMENT and
|
|
|
|
|
returns the resulting string. If REPLACEMENT is not
|
|
|
|
|
a string, it is first passed through `prin1-to-string'
|
|
|
|
|
with the `noescape' argument set.
|
|
|
|
|
|
|
|
|
|
`match-data' is preserved across the call."
|
|
|
|
|
(save-match-data
|
|
|
|
|
(replace-regexp-in-string "\\\\" "\\\\"
|
|
|
|
|
(if (stringp replacement)
|
|
|
|
|
replacement
|
|
|
|
|
(prin1-to-string replacement t))
|
|
|
|
|
t t)))
|
|
|
|
|
|
1999-08-03 17:50:44 +00:00
|
|
|
|
(defun replace-loop-through-replacements (data replace-count)
|
|
|
|
|
;; DATA is a vector contaning the following values:
|
|
|
|
|
;; 0 next-rotate-count
|
|
|
|
|
;; 1 repeat-count
|
|
|
|
|
;; 2 next-replacement
|
|
|
|
|
;; 3 replacements
|
|
|
|
|
(if (= (aref data 0) replace-count)
|
|
|
|
|
(progn
|
|
|
|
|
(aset data 0 (+ replace-count (aref data 1)))
|
|
|
|
|
(let ((next (cdr (aref data 2))))
|
|
|
|
|
(aset data 2 (if (consp next) next (aref data 3))))))
|
|
|
|
|
(car (aref data 2)))
|
|
|
|
|
|
2004-06-24 10:26:24 +00:00
|
|
|
|
(defun replace-match-data (integers reuse &optional new)
|
|
|
|
|
"Like `match-data', but markers in REUSE get invalidated.
|
|
|
|
|
If NEW is non-NIL, it is set and returned instead of fresh data,
|
|
|
|
|
but coerced to the correct value of INTEGERS."
|
|
|
|
|
(or (and new
|
|
|
|
|
(progn
|
|
|
|
|
(set-match-data new)
|
|
|
|
|
(and (eq new reuse)
|
|
|
|
|
(eq (null integers) (markerp (car reuse)))
|
|
|
|
|
new)))
|
|
|
|
|
(match-data integers
|
|
|
|
|
(prog1 reuse
|
|
|
|
|
(while reuse
|
|
|
|
|
(if (markerp (car reuse))
|
|
|
|
|
(set-marker (car reuse) nil))
|
|
|
|
|
(setq reuse (cdr reuse)))))))
|
|
|
|
|
|
|
|
|
|
(defun replace-match-maybe-edit (newtext fixedcase literal noedit match-data)
|
|
|
|
|
"Make a replacement with `replace-match', editing `\\?'.
|
2004-12-12 22:06:30 +00:00
|
|
|
|
NEWTEXT, FIXEDCASE, LITERAL are just passed on. If NOEDIT is true, no
|
2004-06-24 10:26:24 +00:00
|
|
|
|
check for `\\?' is made to save time. MATCH-DATA is used for the
|
|
|
|
|
replacement. In case editing is done, it is changed to use markers.
|
|
|
|
|
|
|
|
|
|
The return value is non-NIL if there has been no `\\?' or NOEDIT was
|
|
|
|
|
passed in. If LITERAL is set, no checking is done, anyway."
|
|
|
|
|
(unless (or literal noedit)
|
|
|
|
|
(setq noedit t)
|
|
|
|
|
(while (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\\\?\\)"
|
|
|
|
|
newtext)
|
|
|
|
|
(setq newtext
|
2005-05-18 10:17:18 +00:00
|
|
|
|
(read-string "Edit replacement string: "
|
|
|
|
|
(prog1
|
|
|
|
|
(cons
|
|
|
|
|
(replace-match "" t t newtext 3)
|
|
|
|
|
(1+ (match-beginning 3)))
|
|
|
|
|
(setq match-data
|
|
|
|
|
(replace-match-data
|
|
|
|
|
nil match-data match-data))))
|
2004-06-24 10:26:24 +00:00
|
|
|
|
noedit nil)))
|
|
|
|
|
(set-match-data match-data)
|
|
|
|
|
(replace-match newtext fixedcase literal)
|
|
|
|
|
noedit)
|
|
|
|
|
|
2002-07-25 19:31:25 +00:00
|
|
|
|
(defun perform-replace (from-string replacements
|
1990-05-11 20:07:49 +00:00
|
|
|
|
query-flag regexp-flag delimited-flag
|
2001-10-24 12:35:48 +00:00
|
|
|
|
&optional repeat-count map start end)
|
1990-05-11 20:07:49 +00:00
|
|
|
|
"Subroutine of `query-replace'. Its complexity handles interactive queries.
|
|
|
|
|
Don't use this in your own program unless you want to query and set the mark
|
|
|
|
|
just as `query-replace' does. Instead, write a simple loop like this:
|
2001-08-03 11:57:27 +00:00
|
|
|
|
|
|
|
|
|
(while (re-search-forward \"foo[ \\t]+bar\" nil t)
|
1990-05-11 20:07:49 +00:00
|
|
|
|
(replace-match \"foobar\" nil nil))
|
2001-08-03 11:57:27 +00:00
|
|
|
|
|
|
|
|
|
which will run faster and probably do exactly what you want. Please
|
|
|
|
|
see the documentation of `replace-match' to find out how to simulate
|
2002-05-26 20:42:51 +00:00
|
|
|
|
`case-replace'.
|
|
|
|
|
|
|
|
|
|
This function returns nil if and only if there were no matches to
|
|
|
|
|
make, or the user didn't cancel the call."
|
1993-03-09 19:51:29 +00:00
|
|
|
|
(or map (setq map query-replace-map))
|
1996-12-07 21:23:37 +00:00
|
|
|
|
(and query-flag minibuffer-auto-raise
|
|
|
|
|
(raise-frame (window-frame (minibuffer-window))))
|
1990-05-11 20:07:49 +00:00
|
|
|
|
(let ((nocasify (not (and case-fold-search case-replace
|
|
|
|
|
(string-equal from-string
|
|
|
|
|
(downcase from-string)))))
|
1999-07-21 23:01:38 +00:00
|
|
|
|
(case-fold-search (and case-fold-search
|
|
|
|
|
(string-equal from-string
|
|
|
|
|
(downcase from-string))))
|
2004-02-02 12:50:49 +00:00
|
|
|
|
(literal (or (not regexp-flag) (eq regexp-flag 'literal)))
|
1990-05-11 20:07:49 +00:00
|
|
|
|
(search-function (if regexp-flag 're-search-forward 'search-forward))
|
|
|
|
|
(search-string from-string)
|
1992-06-30 13:54:21 +00:00
|
|
|
|
(real-match-data nil) ; the match data for the current match
|
1990-05-11 20:07:49 +00:00
|
|
|
|
(next-replacement nil)
|
2004-06-24 10:26:24 +00:00
|
|
|
|
(noedit nil)
|
1990-05-11 20:07:49 +00:00
|
|
|
|
(keep-going t)
|
|
|
|
|
(stack nil)
|
|
|
|
|
(replace-count 0)
|
1997-06-24 10:48:10 +00:00
|
|
|
|
(nonempty-match nil)
|
|
|
|
|
|
1997-11-11 03:26:55 +00:00
|
|
|
|
;; If non-nil, it is marker saying where in the buffer to stop.
|
|
|
|
|
(limit nil)
|
|
|
|
|
|
1997-06-24 10:48:10 +00:00
|
|
|
|
;; Data for the next match. If a cons, it has the same format as
|
|
|
|
|
;; (match-data); otherwise it is t if a match is possible at point.
|
1996-12-18 23:36:24 +00:00
|
|
|
|
(match-again t)
|
1997-06-24 10:48:10 +00:00
|
|
|
|
|
1994-05-01 22:56:54 +00:00
|
|
|
|
(message
|
|
|
|
|
(if query-flag
|
|
|
|
|
(substitute-command-keys
|
|
|
|
|
"Query replacing %s with %s: (\\<query-replace-map>\\[help] for help) "))))
|
1997-11-11 03:26:55 +00:00
|
|
|
|
|
|
|
|
|
;; If region is active, in Transient Mark mode, operate on region.
|
2000-04-25 19:19:31 +00:00
|
|
|
|
(when start
|
|
|
|
|
(setq limit (copy-marker (max start end)))
|
|
|
|
|
(goto-char (min start end))
|
|
|
|
|
(deactivate-mark))
|
1999-08-03 17:50:44 +00:00
|
|
|
|
|
|
|
|
|
;; REPLACEMENTS is either a string, a list of strings, or a cons cell
|
|
|
|
|
;; containing a function and its first argument. The function is
|
|
|
|
|
;; called to generate each replacement like this:
|
|
|
|
|
;; (funcall (car replacements) (cdr replacements) replace-count)
|
|
|
|
|
;; It must return a string.
|
|
|
|
|
(cond
|
|
|
|
|
((stringp replacements)
|
|
|
|
|
(setq next-replacement replacements
|
|
|
|
|
replacements nil))
|
|
|
|
|
((stringp (car replacements)) ; If it isn't a string, it must be a cons
|
|
|
|
|
(or repeat-count (setq repeat-count 1))
|
|
|
|
|
(setq replacements (cons 'replace-loop-through-replacements
|
|
|
|
|
(vector repeat-count repeat-count
|
|
|
|
|
replacements replacements)))))
|
|
|
|
|
|
1990-05-11 20:07:49 +00:00
|
|
|
|
(if delimited-flag
|
|
|
|
|
(setq search-function 're-search-forward
|
|
|
|
|
search-string (concat "\\b"
|
|
|
|
|
(if regexp-flag from-string
|
|
|
|
|
(regexp-quote from-string))
|
|
|
|
|
"\\b")))
|
2004-12-15 10:10:54 +00:00
|
|
|
|
(when query-replace-lazy-highlight
|
2005-03-18 09:59:45 +00:00
|
|
|
|
(setq isearch-lazy-highlight-last-string nil))
|
2004-12-12 22:33:28 +00:00
|
|
|
|
|
1990-05-11 20:07:49 +00:00
|
|
|
|
(push-mark)
|
|
|
|
|
(undo-boundary)
|
1993-12-31 15:04:23 +00:00
|
|
|
|
(unwind-protect
|
|
|
|
|
;; Loop finding occurrences that perhaps should be replaced.
|
|
|
|
|
(while (and keep-going
|
2004-06-10 04:21:14 +00:00
|
|
|
|
(not (or (eobp) (and limit (>= (point) limit))))
|
1997-06-24 10:48:10 +00:00
|
|
|
|
;; Use the next match if it is already known;
|
|
|
|
|
;; otherwise, search for a match after moving forward
|
|
|
|
|
;; one char if progress is required.
|
|
|
|
|
(setq real-match-data
|
|
|
|
|
(if (consp match-again)
|
|
|
|
|
(progn (goto-char (nth 1 match-again))
|
2004-06-24 10:26:24 +00:00
|
|
|
|
(replace-match-data t
|
|
|
|
|
real-match-data
|
|
|
|
|
match-again))
|
1997-06-24 10:48:10 +00:00
|
|
|
|
(and (or match-again
|
2000-06-26 20:02:42 +00:00
|
|
|
|
;; MATCH-AGAIN non-nil means we
|
|
|
|
|
;; accept an adjacent match. If
|
|
|
|
|
;; we don't, move one char to the
|
|
|
|
|
;; right. This takes us a
|
|
|
|
|
;; character too far at the end,
|
|
|
|
|
;; but this is undone after the
|
|
|
|
|
;; while-loop.
|
2004-06-10 04:21:14 +00:00
|
|
|
|
(progn
|
|
|
|
|
(forward-char 1)
|
|
|
|
|
(not (or (eobp)
|
|
|
|
|
(and limit (>= (point) limit))))))
|
1997-11-11 03:26:55 +00:00
|
|
|
|
(funcall search-function search-string limit t)
|
1997-06-24 10:48:10 +00:00
|
|
|
|
;; For speed, use only integers and
|
|
|
|
|
;; reuse the list used last time.
|
2004-06-24 10:26:24 +00:00
|
|
|
|
(replace-match-data t real-match-data)))))
|
2001-11-11 20:05:39 +00:00
|
|
|
|
;; Optionally ignore matches that have a read-only property.
|
|
|
|
|
(unless (and query-replace-skip-read-only
|
|
|
|
|
(text-property-not-all
|
|
|
|
|
(match-beginning 0) (match-end 0)
|
|
|
|
|
'read-only nil))
|
|
|
|
|
|
|
|
|
|
;; Record whether the match is nonempty, to avoid an infinite loop
|
|
|
|
|
;; repeatedly matching the same empty string.
|
|
|
|
|
(setq nonempty-match
|
|
|
|
|
(/= (nth 0 real-match-data) (nth 1 real-match-data)))
|
|
|
|
|
|
|
|
|
|
;; If the match is empty, record that the next one can't be
|
|
|
|
|
;; adjacent.
|
|
|
|
|
|
|
|
|
|
;; Otherwise, if matching a regular expression, do the next
|
|
|
|
|
;; match now, since the replacement for this match may
|
|
|
|
|
;; affect whether the next match is adjacent to this one.
|
|
|
|
|
;; If that match is empty, don't use it.
|
|
|
|
|
(setq match-again
|
|
|
|
|
(and nonempty-match
|
|
|
|
|
(or (not regexp-flag)
|
|
|
|
|
(and (looking-at search-string)
|
|
|
|
|
(let ((match (match-data)))
|
|
|
|
|
(and (/= (nth 0 match) (nth 1 match))
|
|
|
|
|
match))))))
|
|
|
|
|
|
|
|
|
|
;; Calculate the replacement string, if necessary.
|
|
|
|
|
(when replacements
|
|
|
|
|
(set-match-data real-match-data)
|
|
|
|
|
(setq next-replacement
|
|
|
|
|
(funcall (car replacements) (cdr replacements)
|
2004-06-24 10:26:24 +00:00
|
|
|
|
replace-count)
|
|
|
|
|
noedit nil))
|
2001-11-11 20:05:39 +00:00
|
|
|
|
(if (not query-flag)
|
2004-06-26 18:35:18 +00:00
|
|
|
|
(let ((inhibit-read-only
|
|
|
|
|
query-replace-skip-read-only))
|
2004-12-12 22:06:30 +00:00
|
|
|
|
(unless (or literal noedit)
|
2005-03-18 09:59:45 +00:00
|
|
|
|
(replace-highlight
|
|
|
|
|
(nth 0 real-match-data) (nth 1 real-match-data)
|
|
|
|
|
start end search-string
|
|
|
|
|
(or delimited-flag regexp-flag) case-fold-search))
|
2004-06-24 10:26:24 +00:00
|
|
|
|
(setq noedit
|
|
|
|
|
(replace-match-maybe-edit
|
|
|
|
|
next-replacement nocasify literal
|
|
|
|
|
noedit real-match-data)
|
|
|
|
|
replace-count (1+ replace-count)))
|
2001-11-11 20:05:39 +00:00
|
|
|
|
(undo-boundary)
|
|
|
|
|
(let (done replaced key def)
|
|
|
|
|
;; Loop reading commands until one of them sets done,
|
2004-06-24 10:26:24 +00:00
|
|
|
|
;; which means it has finished handling this
|
|
|
|
|
;; occurrence. Any command that sets `done' should
|
|
|
|
|
;; leave behind proper match data for the stack.
|
|
|
|
|
;; Commands not setting `done' need to adjust
|
|
|
|
|
;; `real-match-data'.
|
2001-11-11 20:05:39 +00:00
|
|
|
|
(while (not done)
|
|
|
|
|
(set-match-data real-match-data)
|
2005-03-18 09:59:45 +00:00
|
|
|
|
(replace-highlight
|
|
|
|
|
(match-beginning 0) (match-end 0)
|
|
|
|
|
start end search-string
|
|
|
|
|
(or delimited-flag regexp-flag) case-fold-search)
|
2001-11-11 20:05:39 +00:00
|
|
|
|
;; Bind message-log-max so we don't fill up the message log
|
|
|
|
|
;; with a bunch of identical messages.
|
|
|
|
|
(let ((message-log-max nil))
|
2004-09-08 20:24:12 +00:00
|
|
|
|
(message message
|
|
|
|
|
(query-replace-descr from-string)
|
|
|
|
|
(query-replace-descr next-replacement)))
|
2001-11-11 20:05:39 +00:00
|
|
|
|
(setq key (read-event))
|
|
|
|
|
;; Necessary in case something happens during read-event
|
|
|
|
|
;; that clobbers the match data.
|
|
|
|
|
(set-match-data real-match-data)
|
|
|
|
|
(setq key (vector key))
|
|
|
|
|
(setq def (lookup-key map key))
|
|
|
|
|
;; Restore the match data while we process the command.
|
|
|
|
|
(cond ((eq def 'help)
|
|
|
|
|
(with-output-to-temp-buffer "*Help*"
|
|
|
|
|
(princ
|
|
|
|
|
(concat "Query replacing "
|
|
|
|
|
(if regexp-flag "regexp " "")
|
|
|
|
|
from-string " with "
|
|
|
|
|
next-replacement ".\n\n"
|
|
|
|
|
(substitute-command-keys
|
|
|
|
|
query-replace-help)))
|
|
|
|
|
(with-current-buffer standard-output
|
|
|
|
|
(help-mode))))
|
|
|
|
|
((eq def 'exit)
|
|
|
|
|
(setq keep-going nil)
|
|
|
|
|
(setq done t))
|
|
|
|
|
((eq def 'backup)
|
|
|
|
|
(if stack
|
2002-05-26 20:42:51 +00:00
|
|
|
|
(let ((elt (pop stack)))
|
2004-06-24 10:26:24 +00:00
|
|
|
|
(goto-char (nth 0 elt))
|
|
|
|
|
(setq replaced (nth 1 elt)
|
|
|
|
|
real-match-data
|
|
|
|
|
(replace-match-data
|
|
|
|
|
t real-match-data
|
|
|
|
|
(nth 2 elt))))
|
2001-11-11 20:05:39 +00:00
|
|
|
|
(message "No previous match")
|
|
|
|
|
(ding 'no-terminate)
|
|
|
|
|
(sit-for 1)))
|
|
|
|
|
((eq def 'act)
|
|
|
|
|
(or replaced
|
2004-06-24 10:26:24 +00:00
|
|
|
|
(setq noedit
|
|
|
|
|
(replace-match-maybe-edit
|
|
|
|
|
next-replacement nocasify literal
|
|
|
|
|
noedit real-match-data)
|
|
|
|
|
replace-count (1+ replace-count)))
|
2001-11-11 20:05:39 +00:00
|
|
|
|
(setq done t replaced t))
|
|
|
|
|
((eq def 'act-and-exit)
|
|
|
|
|
(or replaced
|
2004-06-24 10:26:24 +00:00
|
|
|
|
(setq noedit
|
2004-06-28 23:03:06 +00:00
|
|
|
|
(replace-match-maybe-edit
|
2004-06-24 10:26:24 +00:00
|
|
|
|
next-replacement nocasify literal
|
|
|
|
|
noedit real-match-data)
|
|
|
|
|
replace-count (1+ replace-count)))
|
2001-11-11 20:05:39 +00:00
|
|
|
|
(setq keep-going nil)
|
|
|
|
|
(setq done t replaced t))
|
|
|
|
|
((eq def 'act-and-show)
|
|
|
|
|
(if (not replaced)
|
2004-06-24 10:26:24 +00:00
|
|
|
|
(setq noedit
|
|
|
|
|
(replace-match-maybe-edit
|
|
|
|
|
next-replacement nocasify literal
|
|
|
|
|
noedit real-match-data)
|
|
|
|
|
replace-count (1+ replace-count)
|
|
|
|
|
real-match-data (replace-match-data
|
|
|
|
|
t real-match-data)
|
|
|
|
|
replaced t)))
|
2001-11-11 20:05:39 +00:00
|
|
|
|
((eq def 'automatic)
|
|
|
|
|
(or replaced
|
2004-06-24 10:26:24 +00:00
|
|
|
|
(setq noedit
|
|
|
|
|
(replace-match-maybe-edit
|
|
|
|
|
next-replacement nocasify literal
|
|
|
|
|
noedit real-match-data)
|
|
|
|
|
replace-count (1+ replace-count)))
|
2001-11-11 20:05:39 +00:00
|
|
|
|
(setq done t query-flag nil replaced t))
|
|
|
|
|
((eq def 'skip)
|
|
|
|
|
(setq done t))
|
|
|
|
|
((eq def 'recenter)
|
|
|
|
|
(recenter nil))
|
|
|
|
|
((eq def 'edit)
|
|
|
|
|
(let ((opos (point-marker)))
|
2004-06-24 10:26:24 +00:00
|
|
|
|
(setq real-match-data (replace-match-data
|
|
|
|
|
nil real-match-data
|
|
|
|
|
real-match-data))
|
2001-11-11 20:05:39 +00:00
|
|
|
|
(goto-char (match-beginning 0))
|
2002-03-25 00:38:46 +00:00
|
|
|
|
(save-excursion
|
|
|
|
|
(save-window-excursion
|
|
|
|
|
(recursive-edit)))
|
2004-06-24 10:26:24 +00:00
|
|
|
|
(goto-char opos)
|
|
|
|
|
(set-marker opos nil))
|
2001-11-11 20:05:39 +00:00
|
|
|
|
;; Before we make the replacement,
|
|
|
|
|
;; decide whether the search string
|
|
|
|
|
;; can match again just after this match.
|
|
|
|
|
(if (and regexp-flag nonempty-match)
|
|
|
|
|
(setq match-again (and (looking-at search-string)
|
|
|
|
|
(match-data)))))
|
|
|
|
|
;; Edit replacement.
|
|
|
|
|
((eq def 'edit-replacement)
|
2004-06-24 10:26:24 +00:00
|
|
|
|
(setq real-match-data (replace-match-data
|
|
|
|
|
nil real-match-data
|
|
|
|
|
real-match-data)
|
|
|
|
|
next-replacement
|
2005-05-18 10:17:18 +00:00
|
|
|
|
(read-string "Edit replacement string: "
|
|
|
|
|
next-replacement)
|
2004-06-24 10:26:24 +00:00
|
|
|
|
noedit nil)
|
|
|
|
|
(if replaced
|
|
|
|
|
(set-match-data real-match-data)
|
|
|
|
|
(setq noedit
|
|
|
|
|
(replace-match-maybe-edit
|
|
|
|
|
next-replacement nocasify literal noedit
|
|
|
|
|
real-match-data)
|
|
|
|
|
replaced t))
|
2001-11-11 20:05:39 +00:00
|
|
|
|
(setq done t))
|
2002-07-25 19:31:25 +00:00
|
|
|
|
|
2001-11-11 20:05:39 +00:00
|
|
|
|
((eq def 'delete-and-edit)
|
2004-06-24 10:26:24 +00:00
|
|
|
|
(replace-match "" t t)
|
|
|
|
|
(setq real-match-data (replace-match-data
|
|
|
|
|
nil real-match-data))
|
|
|
|
|
(replace-dehighlight)
|
|
|
|
|
(save-excursion (recursive-edit))
|
2001-11-11 20:05:39 +00:00
|
|
|
|
(setq replaced t))
|
|
|
|
|
;; Note: we do not need to treat `exit-prefix'
|
|
|
|
|
;; specially here, since we reread
|
|
|
|
|
;; any unrecognized character.
|
|
|
|
|
(t
|
|
|
|
|
(setq this-command 'mode-exited)
|
|
|
|
|
(setq keep-going nil)
|
|
|
|
|
(setq unread-command-events
|
|
|
|
|
(append (listify-key-sequence key)
|
|
|
|
|
unread-command-events))
|
2004-12-12 22:33:28 +00:00
|
|
|
|
(setq done t)))
|
2004-12-15 10:10:54 +00:00
|
|
|
|
(when query-replace-lazy-highlight
|
|
|
|
|
;; Force lazy rehighlighting only after replacements
|
|
|
|
|
(if (not (memq def '(skip backup)))
|
|
|
|
|
(setq isearch-lazy-highlight-last-string nil))))
|
2001-11-11 20:05:39 +00:00
|
|
|
|
;; Record previous position for ^ when we move on.
|
|
|
|
|
;; Change markers to numbers in the match data
|
|
|
|
|
;; since lots of markers slow down editing.
|
2004-06-24 10:26:24 +00:00
|
|
|
|
(push (list (point) replaced
|
|
|
|
|
;;; If the replacement has already happened, all we need is the
|
|
|
|
|
;;; current match start and end. We could get this with a trivial
|
|
|
|
|
;;; match like
|
|
|
|
|
;;; (save-excursion (goto-char (match-beginning 0))
|
|
|
|
|
;;; (search-forward (match-string 0))
|
|
|
|
|
;;; (match-data t))
|
|
|
|
|
;;; if we really wanted to avoid manually constructing match data.
|
|
|
|
|
;;; Adding current-buffer is necessary so that match-data calls can
|
|
|
|
|
;;; return markers which are appropriate for editing.
|
|
|
|
|
(if replaced
|
|
|
|
|
(list
|
|
|
|
|
(match-beginning 0)
|
|
|
|
|
(match-end 0)
|
|
|
|
|
(current-buffer))
|
|
|
|
|
(match-data t)))
|
|
|
|
|
stack)))))
|
2000-06-26 20:02:42 +00:00
|
|
|
|
|
|
|
|
|
;; The code preventing adjacent regexp matches in the condition
|
|
|
|
|
;; of the while-loop above will haven taken us one character
|
|
|
|
|
;; beyond the last replacement. Undo that.
|
|
|
|
|
(when (and regexp-flag (not match-again) (> replace-count 0))
|
|
|
|
|
(backward-char 1))
|
2002-07-25 19:31:25 +00:00
|
|
|
|
|
1993-12-31 15:04:23 +00:00
|
|
|
|
(replace-dehighlight))
|
1994-12-14 03:44:51 +00:00
|
|
|
|
(or unread-command-events
|
|
|
|
|
(message "Replaced %d occurrence%s"
|
|
|
|
|
replace-count
|
|
|
|
|
(if (= replace-count 1) "" "s")))
|
|
|
|
|
(and keep-going stack)))
|
1990-05-11 20:07:49 +00:00
|
|
|
|
|
1993-12-31 15:04:23 +00:00
|
|
|
|
(defvar replace-overlay nil)
|
|
|
|
|
|
2005-03-18 09:59:45 +00:00
|
|
|
|
(defun replace-highlight (match-beg match-end range-beg range-end
|
|
|
|
|
string regexp case-fold)
|
2004-12-15 10:10:54 +00:00
|
|
|
|
(if query-replace-highlight
|
|
|
|
|
(if replace-overlay
|
2005-02-16 09:49:45 +00:00
|
|
|
|
(move-overlay replace-overlay match-beg match-end (current-buffer))
|
|
|
|
|
(setq replace-overlay (make-overlay match-beg match-end))
|
2004-12-15 10:10:54 +00:00
|
|
|
|
(overlay-put replace-overlay 'priority 1) ;higher than lazy overlays
|
|
|
|
|
(overlay-put replace-overlay 'face 'query-replace)))
|
2005-03-18 09:59:45 +00:00
|
|
|
|
(if query-replace-lazy-highlight
|
|
|
|
|
(let ((isearch-string string)
|
|
|
|
|
(isearch-regexp regexp)
|
|
|
|
|
(isearch-case-fold-search case-fold))
|
|
|
|
|
(isearch-lazy-highlight-new-loop range-beg range-end))))
|
2004-12-15 10:10:54 +00:00
|
|
|
|
|
1993-12-31 15:04:23 +00:00
|
|
|
|
(defun replace-dehighlight ()
|
2004-12-15 10:10:54 +00:00
|
|
|
|
(when replace-overlay
|
|
|
|
|
(delete-overlay replace-overlay))
|
|
|
|
|
(when query-replace-lazy-highlight
|
2005-03-18 09:59:45 +00:00
|
|
|
|
(lazy-highlight-cleanup lazy-highlight-cleanup)
|
2004-12-15 10:10:54 +00:00
|
|
|
|
(setq isearch-lazy-highlight-last-string nil)))
|
1993-12-31 15:04:23 +00:00
|
|
|
|
|
2004-07-06 00:06:05 +00:00
|
|
|
|
;; arch-tag: 16b4cd61-fd40-497b-b86f-b667c4cf88e4
|
1992-05-30 21:11:25 +00:00
|
|
|
|
;;; replace.el ends here
|