]> code.delx.au - gnu-emacs/blobdiff - lisp/replace.el
Revision: miles@gnu.org--gnu-2004/emacs--unicode--0--patch-15
[gnu-emacs] / lisp / replace.el
index c2c39cbd219131be4ddee6fe036d3efab5ea359e..89f55c2829e9cecf370c423ca85252d14b94d14d 100644 (file)
@@ -1,7 +1,7 @@
 ;;; replace.el --- replace commands for Emacs
 
-;; Copyright (C) 1985, 86, 87, 92, 94, 96, 1997, 2000, 2001, 2002
-;;  Free Software Foundation, Inc.
+;; Copyright (C) 1985, 86, 87, 92, 94, 96, 1997, 2000, 2001, 2002,
+;;   2003, 2004 Free Software Foundation, Inc.
 
 ;; Maintainer: FSF
 
 
 (defvar query-replace-history nil)
 
-(defvar query-replace-interactive nil
+(defcustom query-replace-interactive nil
   "Non-nil means `query-replace' uses the last search string.
-That becomes the \"string to replace\".")
+That becomes the \"string to replace\"."
+  :type 'boolean
+  :group 'matching)
 
 (defcustom query-replace-from-history-variable 'query-replace-history
   "History list to use for the FROM argument of `query-replace' commands.
@@ -70,26 +72,30 @@ strings or patterns."
   (let (from to)
     (if query-replace-interactive
        (setq from (car (if regexp-flag regexp-search-ring search-ring)))
-      (setq from (read-from-minibuffer (format "%s: " string)
-                                      nil nil nil
-                                      query-replace-from-history-variable
-                                      nil t))
+      ;; 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
+       (setq from (read-from-minibuffer (format "%s: " string)
+                                        nil nil nil
+                                        query-replace-from-history-variable
+                                        nil t)))
       ;; Warn if user types \n or \t, but don't reject the input.
-      (if (string-match "\\\\[nt]" from)
-         (let ((match (match-string 0 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))))
-
-    (setq to (read-from-minibuffer (format "%s %s with: " string from)
-                                  nil nil nil
-                                  query-replace-to-history-variable from t))
-    (if (and transient-mark-mode mark-active)
-       (list from to current-prefix-arg (region-beginning) (region-end))
-      (list from to current-prefix-arg nil nil))))
+      (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))))
+
+    (save-excursion
+      (setq to (read-from-minibuffer (format "%s %s with: " string from)
+                                    nil nil nil
+                                    query-replace-to-history-variable from t)))
+    (list from to current-prefix-arg)))
 
 (defun query-replace (from-string to-string &optional delimited start end)
   "Replace some occurrences of FROM-STRING with TO-STRING.
@@ -103,18 +109,29 @@ 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.
 
-Replacement transfers the case 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.
-\(Preserving case means that if the string matched is all caps, or capitalized,
-then its replacement is upcased or capitalized.)
+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.)
 
 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
 only matches surrounded by word boundaries.
 Fourth and fifth arg START and END specify the region to operate on.
 
 To customize possible responses, change the \"bindings\" in `query-replace-map'."
-  (interactive (query-replace-read-args "Query replace" nil))
+  (interactive (let ((common
+                     (query-replace-read-args "Query replace" nil)))
+                (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)))))
   (perform-replace from-string to-string t nil delimited nil nil start end))
 
 (define-key esc-map "%" 'query-replace)
@@ -131,8 +148,13 @@ 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.
+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.)
 
 Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
 only matches surrounded by word boundaries.
@@ -140,9 +162,62 @@ Fourth and fifth arg START and END specify the region to operate on.
 
 In TO-STRING, `\\&' stands for whatever matched the whole of REGEXP,
 and `\\=\\N' (where N is a digit) stands for
- whatever what matched the Nth `\\(...\\)' in REGEXP."
-  (interactive (query-replace-read-args "Query replace regexp" t))
+whatever what matched the Nth `\\(...\\)' in REGEXP.
+
+When this function is called interactively, the replacement text
+can also contain `\\,' followed by a Lisp expression.  The escaped
+shorthands for `query-replace-regexp-eval' are also valid
+here: within the Lisp expression, you can use `\\&' for the whole
+match string, `\\N' for partial matches, `\\#&' and `\\#N' for
+the respective numeric values, and `\\#' for `replace-count'.
+
+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 you use `\\,' or `\\#' in the replacement, TO-STRING actually
+becomes a list with expanded shorthands.
+Use \\[repeat-complex-command] after this command to see details."
+  (interactive
+   (let ((common
+         (query-replace-read-args "Query replace regexp" t)))
+     (list
+      (nth 0 common)
+      (if (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]"
+                       (nth 1 common))
+         (let ((to-string (nth 1 common)) pos to-expr char prompt)
+           (while (string-match
+                   "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]"
+                   to-string)
+             (setq pos (match-end 0))
+             (push (substring to-string 0 (- pos 2)) to-expr)
+             (setq char (aref to-string (1- pos))
+                   to-string (substring to-string pos))
+             (cond ((eq char ?\#)
+                    (push '(number-to-string replace-count) to-expr))
+                   ((eq char ?\,)
+                    (setq pos (read-from-string to-string))
+                    (push `(replace-quote ,(car pos)) to-expr)
+                    (setq to-string (substring to-string (cdr pos))))))
+           (setq to-expr (nreverse (delete "" (cons to-string to-expr))))
+           (replace-match-string-symbols to-expr)
+           (cons 'replace-eval-replacement 
+                 (if (> (length to-expr) 1)
+                     (cons 'concat to-expr)
+                   (car to-expr))))
+       (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)))))
   (perform-replace regexp to-string t t delimited nil nil start end))
+
 (define-key esc-map [?\C-%] 'query-replace-regexp)
 
 (defun query-replace-regexp-eval (regexp to-expr &optional delimited start end)
@@ -159,6 +234,7 @@ For convenience, when entering TO-EXPR interactively, you can use `\\&' or
 `\\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.
 Use `\\#&' or `\\#N' if you want a number instead of a string.
+In interactive use, `\\#' in itself stands for `replace-count'.
 
 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.
@@ -174,10 +250,7 @@ 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."
   (interactive
-   (let (from to start end)
-     (when (and transient-mark-mode mark-active)
-       (setq start (region-beginning)
-            end (region-end)))
+   (let (from to)
      (if query-replace-interactive
          (setq from (car regexp-search-ring))
        (setq from (read-from-minibuffer "Query replace regexp: "
@@ -190,9 +263,13 @@ Fourth and fifth arg START and END specify the region to operate on."
      ;; 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)
-     (list from (car to) current-prefix-arg start end)))
+     (list from (car to) current-prefix-arg
+          (if (and transient-mark-mode mark-active)
+              (region-beginning))
+          (if (and transient-mark-mode mark-active)
+              (region-end)))))
   (perform-replace regexp (cons 'replace-eval-replacement to-expr)
-                  t t delimited nil nil start end))
+                  t 'literal delimited nil nil start end))
 
 (defun map-query-replace-regexp (regexp to-strings &optional n start end)
   "Replace some matches for REGEXP with various strings, in rotation.
@@ -215,10 +292,7 @@ A prefix argument N says to use each replacement string N times
 before rotating to the next.
 Fourth and fifth arg START and END specify the region to operate on."
   (interactive
-   (let (from to start end)
-     (when (and transient-mark-mode mark-active)
-       (setq start (region-beginning)
-            end (region-end)))
+   (let (from to)
      (setq from (if query-replace-interactive
                    (car regexp-search-ring)
                  (read-from-minibuffer "Map query replace (regexp): "
@@ -229,7 +303,13 @@ Fourth and fifth arg START and END specify the region to operate on."
                       from)
               nil nil nil
               'query-replace-history from t))
-     (list from to start end current-prefix-arg)))
+     (list from to
+          (and current-prefix-arg
+               (prefix-numeric-value current-prefix-arg))
+          (if (and transient-mark-mode mark-active)
+              (region-beginning))
+          (if (and transient-mark-mode mark-active)
+              (region-end)))))
   (let (replacements)
     (if (listp to-strings)
        (setq replacements to-strings)
@@ -270,7 +350,14 @@ What you probably want is a loop like this:
 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.)"
-  (interactive (query-replace-read-args "Replace string" nil))
+  (interactive
+   (let ((common
+         (query-replace-read-args "Replace string" nil)))
+     (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)))))
   (perform-replace from-string to-string nil nil delimited nil nil start end))
 
 (defun replace-regexp (regexp to-string &optional delimited start end)
@@ -297,7 +384,14 @@ What you probably want is a loop like this:
   (while (re-search-forward REGEXP nil t)
     (replace-match TO-STRING nil nil))
 which will run faster and will not set the mark or print anything."
-  (interactive (query-replace-read-args "Replace regexp" t))
+  (interactive
+   (let ((common
+         (query-replace-read-args "Replace regexp" t)))
+     (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)))))
   (perform-replace regexp to-string nil t delimited nil nil start end))
 
 \f
@@ -490,6 +584,7 @@ Alternatively, click \\[occur-mode-mouse-goto] on an item to go to it.
   (set (make-local-variable 'revert-buffer-function) 'occur-revert-function)
   (make-local-variable 'occur-revert-arguments)
   (add-hook 'change-major-mode-hook 'font-lock-defontify nil t)
+  (setq next-error-function 'occur-next-error)
   (run-hooks 'occur-mode-hook))
 
 (defun occur-revert-function (ignore1 ignore2)
@@ -566,6 +661,21 @@ Alternatively, click \\[occur-mode-mouse-goto] on an item to go to it.
   "Move to the Nth (default 1) previous match in an Occur mode buffer."
   (interactive "p")
   (occur-find-match n #'previous-single-property-change "No earlier matches"))
+
+(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")
+  (when reset
+    (occur-find-match 0 #'next-single-property-change "No first match"))
+  (occur-find-match
+   (prefix-numeric-value argp)
+   (if (> 0 (prefix-numeric-value argp))
+       #'previous-single-property-change
+     #'next-single-property-change)
+   "No more matches")
+  (occur-mode-goto-occurrence))
+
 \f
 (defcustom list-matching-lines-default-context-lines 0
   "*Default number of context lines included around `list-matching-lines' matches.
@@ -671,7 +781,7 @@ This function acts on multiple buffers; otherwise, it is exactly like
           (buf nil)
           (ido-ignore-item-temp-list bufs))
       (while (not (string-equal
-                  (setq buf (read-buffer 
+                  (setq buf (read-buffer
                              (if (eq read-buffer-function 'ido-read-buffer)
                                  "Next buffer to search (C-j to end): "
                                "Next buffer to search (RET to end): ")
@@ -752,7 +862,9 @@ See also `multi-occur'."
        (setq occur-revert-arguments (list regexp nlines bufs)
              buffer-read-only t)
        (if (> count 0)
-           (display-buffer occur-buf)
+           (progn
+             (display-buffer occur-buf)
+             (setq next-error-last-buffer occur-buf))
          (kill-buffer occur-buf)))
       (run-hooks 'occur-hook))))
 
@@ -766,7 +878,8 @@ See also `multi-occur'."
                            title-face prefix-face match-face keep-props)
   (with-current-buffer out-buf
     (setq buffer-read-only nil)
-    (let ((globalcount 0))
+    (let ((globalcount 0)
+         (coding nil))
       ;; Map over all the buffers
       (dolist (buf buffers)
        (when (buffer-live-p buf)
@@ -782,6 +895,11 @@ See also `multi-occur'."
                (headerpt (with-current-buffer out-buf (point))))
            (save-excursion
              (set-buffer buf)
+             (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))
              (save-excursion
                (goto-char (point-min)) ;; begin searching in the buffer
                (while (not (eobp))
@@ -862,8 +980,9 @@ See also `multi-occur'."
                (goto-char headerpt)
                (let ((beg (point))
                      end)
-                 (insert (format "%d lines matching \"%s\" in buffer: %s\n"
-                                 matches regexp (buffer-name buf)))
+                 (insert (format "%d match%s for \"%s\" in buffer: %s\n"
+                                 matches (if (= matches 1) "" "es")
+                                 regexp (buffer-name buf)))
                  (setq end (point))
                  (add-text-properties beg end
                                       (append
@@ -871,6 +990,11 @@ See also `multi-occur'."
                                          `(font-lock-face ,title-face))
                                        `(occur-title ,buf))))
                (goto-char (point-min)))))))
+      (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))
       ;; Return the number of matches
       globalcount)))
 
@@ -932,6 +1056,7 @@ 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))
+#     replace-count
 
 Note that these symbols must be preceeded by a backslash in order to
 type them."
@@ -951,7 +1076,9 @@ type them."
          ((string= "&" name)
           (setcar n '(match-string 0)))
          ((string= "#&" name)
-          (setcar n '(string-to-number (match-string 0))))))))
+          (setcar n '(string-to-number (match-string 0))))
+        ((string= "#" name)
+         (setcar n 'replace-count))))))
     (setq n (cdr n))))
 
 (defun replace-eval-replacement (expression replace-count)
@@ -960,6 +1087,21 @@ type them."
         replacement
       (prin1-to-string replacement t))))
 
+(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)))
+
 (defun replace-loop-through-replacements (data replace-count)
   ;; DATA is a vector contaning the following values:
   ;;   0 next-rotate-count
@@ -998,7 +1140,7 @@ make, or the user didn't cancel the call."
        (case-fold-search (and case-fold-search
                               (string-equal from-string
                                             (downcase from-string))))
-       (literal (not regexp-flag))
+       (literal (or (not regexp-flag) (eq regexp-flag 'literal)))
        (search-function (if regexp-flag 're-search-forward 'search-forward))
        (search-string from-string)
        (real-match-data nil)           ; the match data for the current match
@@ -1052,7 +1194,7 @@ make, or the user didn't cancel the call."
     (unwind-protect
        ;; Loop finding occurrences that perhaps should be replaced.
        (while (and keep-going
-                   (not (eobp))
+                   (not (or (eobp) (and limit (>= (point) limit))))
                    ;; Use the next match if it is already known;
                    ;; otherwise, search for a match after moving forward
                    ;; one char if progress is required.
@@ -1068,7 +1210,10 @@ make, or the user didn't cancel the call."
                                     ;; character too far at the end,
                                     ;; but this is undone after the
                                     ;; while-loop.
-                                    (progn (forward-char 1) (not (eobp))))
+                                    (progn
+                                      (forward-char 1)
+                                      (not (or (eobp)
+                                               (and limit (>= (point) limit))))))
                                 (funcall search-function search-string limit t)
                                 ;; For speed, use only integers and
                                 ;; reuse the list used last time.
@@ -1269,4 +1414,5 @@ make, or the user didn't cancel the call."
                                'query-replace 'region))))
         (move-overlay replace-overlay start end (current-buffer)))))
 
+;;; arch-tag: 16b4cd61-fd40-497b-b86f-b667c4cf88e4
 ;;; replace.el ends here