]> code.delx.au - gnu-emacs/blobdiff - lisp/replace.el
(Vchar_unify_table): Comment added.
[gnu-emacs] / lisp / replace.el
index 8a83ca62b314c071db4e942edaa3dcbafbc20072..0b57ea35fc19b9da7ea76ea0cc2a243dea8f629b 100644 (file)
@@ -223,20 +223,21 @@ 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 (let ((common
-                     (query-replace-read-args
-                      (if (and transient-mark-mode mark-active)
-                        "Query replace in region"
-                        "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)))))
+  (interactive
+   (let ((common
+         (query-replace-read-args
+          (concat "Query replace"
+                  (if current-prefix-arg " word" "")
+                  (if (and transient-mark-mode mark-active) " in region" ""))
+          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)
@@ -289,9 +290,10 @@ Use \\[repeat-complex-command] after this command for details."
   (interactive
    (let ((common
          (query-replace-read-args
-          (if (and transient-mark-mode mark-active)
-              "Query replace regexp in region"
-            "Query replace regexp")
+          (concat "Query replace"
+                  (if current-prefix-arg " word" "")
+                  " regexp"
+                  (if (and transient-mark-mode mark-active) " in region" ""))
           t)))
      (list (nth 0 common) (nth 1 common) (nth 2 common)
           ;; These are done separately here
@@ -447,9 +449,10 @@ and TO-STRING is also null.)"
   (interactive
    (let ((common
          (query-replace-read-args
-          (if (and transient-mark-mode mark-active)
-              "Replace string in region"
-            "Replace string")
+          (concat "Replace"
+                  (if current-prefix-arg " word" "")
+                  " string"
+                  (if (and transient-mark-mode mark-active) " in region" ""))
           nil)))
      (list (nth 0 common) (nth 1 common) (nth 2 common)
           (if (and transient-mark-mode mark-active)
@@ -504,9 +507,10 @@ which will run faster and will not set the mark or print anything."
   (interactive
    (let ((common
          (query-replace-read-args
-          (if (and transient-mark-mode mark-active)
-              "Replace regexp in region"
-            "Replace regexp")
+          (concat "Replace"
+                  (if current-prefix-arg " word" "")
+                  " regexp"
+                  (if (and transient-mark-mode mark-active) " in region" ""))
           t)))
      (list (nth 0 common) (nth 1 common) (nth 2 common)
           (if (and transient-mark-mode mark-active)
@@ -522,15 +526,15 @@ which will run faster and will not set the mark or print anything."
 Maximum length of the history list is determined by the value
 of `history-length', which see.")
 
-(defun read-regexp (prompt)
+(defun read-regexp (prompt &optional default-value)
   "Read regexp as a string using the regexp history and some useful defaults.
-Prompt for a regular expression with PROMPT in the minibuffer.
-Provide the last element of the regexp history as the basic default,
-and return it on typing RET.  Additional defaults are the string
-at point, the last isearch regexp, the last isearch string, and the
-last replacement regexp.  Return the regexp as a string."
-  (let* ((default (car regexp-history))
-        (defaults
+Prompt for a regular expression with PROMPT (without a colon and
+space) in the minibuffer.  The optional argument DEFAULT-VALUE
+provides the value to display in the minibuffer prompt that is
+returned if the user just types RET.
+Values available via M-n are the string at point, the last isearch
+regexp, the last isearch string, and the last replacement regexp."
+  (let* ((defaults
           (list (regexp-quote
                  (or (funcall (or find-tag-default-function
                                   (get major-mode 'find-tag-default-function)
@@ -545,12 +549,13 @@ last replacement regexp.  Return the regexp as a string."
         (history-add-new-input nil)
         (input
          (read-from-minibuffer
-          (if default
-              (format "%s (default %s): " prompt (query-replace-descr default))
+          (if default-value
+              (format "%s (default %s): " prompt
+                      (query-replace-descr default-value))
             (format "%s: " prompt))
           nil nil nil 'regexp-history defaults t)))
     (if (equal input "")
-       default
+       default-value
       (prog1 input
        (add-to-history 'regexp-history input)))))
 
@@ -698,7 +703,7 @@ starting on the same line at which another match ended is ignored."
   "Print and return number of matches for REGEXP following point.
 When called from Lisp and INTERACTIVE is omitted or nil, just return
 the number, do not print it; if INTERACTIVE is t, the function behaves
-in all respects has if it had been called interactively.
+in all respects as if it had been called interactively.
 
 If REGEXP contains upper case characters (excluding those preceded by `\\')
 and `search-upper-case' is non-nil, the matching is case-sensitive.
@@ -1022,7 +1027,8 @@ which means to discard all text properties."
       (nreverse result))))
 
 (defun occur-read-primary-args ()
-  (list (read-regexp "List lines matching regexp")
+  (list (read-regexp "List lines matching regexp"
+                    (car regexp-history))
        (when current-prefix-arg
          (prefix-numeric-value current-prefix-arg))))
 
@@ -1372,6 +1378,20 @@ The valid answers include `act', `skip', `act-and-show',
 `exit', `act-and-exit', `edit', `delete-and-edit', `recenter',
 `automatic', `backup', `exit-prefix', and `help'.")
 
+(defvar multi-query-replace-map
+  (let ((map (make-sparse-keymap)))
+    (set-keymap-parent map query-replace-map)
+    (define-key map "Y" 'automatic-all)
+    (define-key map "N" 'exit-current)
+    map)
+  "Keymap that defines additional bindings for multi-buffer replacements.
+It extends its parent map `query-replace-map' with new bindings to
+operate on a set of buffers/files.  The difference with its parent map
+is the additional answers `automatic-all' to replace all remaining
+matches in all remaining buffers with no more questions, and
+`exit-current' to skip remaining matches in the current buffer
+and to continue with the next buffer in the sequence.")
+
 (defun replace-match-string-symbols (n)
   "Process a list (and any sub-lists), expanding certain symbols.
 Symbol  Expands To
@@ -1527,6 +1547,7 @@ make, or the user didn't cancel the call."
          (stack nil)
          (replace-count 0)
          (nonempty-match nil)
+        (multi-buffer nil)
 
          ;; If non-nil, it is marker saying where in the buffer to stop.
          (limit nil)
@@ -1548,6 +1569,11 @@ make, or the user didn't cancel the call."
       (goto-char (min start end))
       (deactivate-mark))
 
+    ;; If last typed key in previous call of multi-buffer perform-replace
+    ;; was `automatic-all', don't ask more questions in next files
+    (when (eq (lookup-key map (vector last-input-char)) 'automatic-all)
+      (setq query-flag nil multi-buffer t))
+
     ;; 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:
@@ -1645,8 +1671,7 @@ make, or the user didn't cancel the call."
                    (funcall (car replacements) (cdr replacements)
                             replace-count)))
            (if (not query-flag)
-               (let ((inhibit-read-only
-                      query-replace-skip-read-only))
+               (progn
                  (unless (or literal noedit)
                    (replace-highlight
                     (nth 0 real-match-data) (nth 1 real-match-data)
@@ -1695,6 +1720,7 @@ make, or the user didn't cancel the call."
                         (with-output-to-temp-buffer "*Help*"
                           (princ
                            (concat "Query replacing "
+                                   (if delimited-flag "word " "")
                                    (if regexp-flag "regexp " "")
                                    from-string " with "
                                    next-replacement ".\n\n"
@@ -1705,6 +1731,8 @@ make, or the user didn't cancel the call."
                        ((eq def 'exit)
                         (setq keep-going nil)
                         (setq done t))
+                       ((eq def 'exit-current)
+                        (setq multi-buffer t keep-going nil done t))
                        ((eq def 'backup)
                         (if stack
                             (let ((elt (pop stack)))
@@ -1744,14 +1772,15 @@ make, or the user didn't cancel the call."
                                   real-match-data (replace-match-data
                                                    t real-match-data)
                                   replaced t)))
-                       ((eq def 'automatic)
+                       ((or (eq def 'automatic) (eq def 'automatic-all))
                         (or replaced
                             (setq noedit
                                   (replace-match-maybe-edit
                                    next-replacement nocasify literal
                                    noedit real-match-data)
                                   replace-count (1+ replace-count)))
-                        (setq done t query-flag nil replaced t))
+                        (setq done t query-flag nil replaced t)
+                        (if (eq def 'automatic-all) (setq multi-buffer t)))
                        ((eq def 'skip)
                         (setq done t))
                        ((eq def 'recenter)
@@ -1838,7 +1867,7 @@ make, or the user didn't cancel the call."
        (message "Replaced %d occurrence%s"
                 replace-count
                 (if (= replace-count 1) "" "s")))
-    (and keep-going stack)))
+    (or (and keep-going stack) multi-buffer)))
 
 (defvar replace-overlay nil)