(concat): Take modulus of thisindex before shifting.
[bpt/emacs.git] / lisp / replace.el
index 21aa051..95b791a 100644 (file)
@@ -1,6 +1,6 @@
 ;;; replace.el --- replace commands for Emacs.
 
-;; Copyright (C) 1985, 1986, 1987, 1992, 1994 Free Software Foundation, Inc.
+;; Copyright (C) 1985, 1986, 1987, 1992, 1994, 1996 Free Software Foundation, Inc.
 
 ;; This file is part of GNU Emacs.
 
@@ -15,8 +15,9 @@
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to
-;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+;; 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.
 
 ;;; Commentary:
 
@@ -25,7 +26,7 @@
 
 ;;; Code:
 
-(defconst case-replace t "\
+(defvar case-replace t "\
 *Non-nil means query-replace should preserve case in replacements.")
 
 (defvar query-replace-history nil)
@@ -240,8 +241,7 @@ Applies to lines after point."
   (setq occur-mode-map (make-sparse-keymap))
   (define-key occur-mode-map [mouse-2] 'occur-mode-mouse-goto)
   (define-key occur-mode-map "\C-c\C-c" 'occur-mode-goto-occurrence)
-  (define-key occur-mode-map "\C-m" 'occur-mode-goto-occurrence)
-  (define-key occur-mode-map [return] 'occur-mode-goto-occurrence))
+  (define-key occur-mode-map "\C-m" 'occur-mode-goto-occurrence))
 
 (defvar occur-buffer nil)
 (defvar occur-nlines nil)
@@ -317,6 +317,10 @@ A positive number means to include that many lines both before and after.")
 
 (defalias 'list-matching-lines 'occur)
 
+(defvar list-matching-lines-face 'bold
+  "*Face used by M-x list-matching-lines to show the text that matches.
+If the value is nil, don't highlight the matching portions specially.")
+
 (defun occur (regexp &optional nlines)
   "Show all lines in the current buffer containing a match for REGEXP.
 
@@ -330,21 +334,26 @@ Interactively it is the prefix arg.
 The lines are shown in a buffer named `*Occur*'.
 It serves as a menu to find any of the occurrences in this buffer.
 \\[describe-mode] in that buffer will explain how."
-  (interactive (list (let* ((default (car regexp-history))
-                           (input 
-                            (read-from-minibuffer
-                             (if default
-                                 (format "List lines matching regexp (default `%s'): " default)
-                               "List lines matching regexp: ")
-                             nil nil nil
-                             'regexp-history)))
-                      (if (> (length input) 0) input
-                        (setcar regexp-history default)))
-                    current-prefix-arg))
-  (setq nlines (if nlines (prefix-numeric-value nlines)
-                list-matching-lines-default-context-lines))
-  (let ((first t)
+  (interactive
+   (list (let* ((default (car regexp-history))
+               (input
+                (read-from-minibuffer
+                 (if default
+                     (format "List lines matching regexp (default `%s'): "
+                             default)
+                   "List lines matching regexp: ")
+                 nil nil nil 'regexp-history)))
+          (if (string-equal input "")
+              default
+            (set-text-properties 0 (length input) nil input)
+            input))
+        current-prefix-arg))
+  (let ((nlines (if nlines
+                   (prefix-numeric-value nlines)
+                 list-matching-lines-default-context-lines))
+       (first t)
        (buffer (current-buffer))
+       (dir default-directory)
        (linenum 1)
        (prevpos (point-min))
        (final-context-start (make-marker)))
@@ -352,90 +361,120 @@ It serves as a menu to find any of the occurrences in this buffer.
 ;;;      (beginning-of-line)
 ;;;      (setq linenum (1+ (count-lines (point-min) (point))))
 ;;;      (setq prevpos (point)))
-    (with-output-to-temp-buffer "*Occur*"
-      (save-excursion
-       (set-buffer standard-output)
-       ;; We will insert the number of lines, and "lines", later.
-       (insert " matching ")
-       (let ((print-escape-newlines t))
-         (prin1 regexp))
-       (insert " in buffer " (buffer-name buffer) ?. ?\n)
-       (occur-mode)
-       (setq occur-buffer buffer)
-       (setq occur-nlines nlines)
-       (setq occur-pos-list ()))
-      (if (eq buffer standard-output)
-         (goto-char (point-max)))
-      (save-excursion
-       (beginning-of-buffer)
-       ;; Find next match, but give up if prev match was at end of buffer.
-       (while (and (not (= prevpos (point-max)))
-                   (re-search-forward regexp nil t))
-         (goto-char (match-beginning 0))
-         (beginning-of-line)
-         (save-match-data
-           (setq linenum (+ linenum (count-lines prevpos (point)))))
-         (setq prevpos (point))
-         (goto-char (match-end 0))
-         (let* ((start (save-excursion
-                         (goto-char (match-beginning 0))
-                         (forward-line (if (< nlines 0) nlines (- nlines)))
-                         (point)))
-                (end (save-excursion
-                       (goto-char (match-end 0))
-                       (if (> nlines 0)
-                           (forward-line (1+ nlines))
-                           (forward-line 1))
-                       (point)))
-                (tag (format "%3d" linenum))
-                (empty (make-string (length tag) ?\ ))
-                tem)
-           (save-excursion
-             (setq tem (make-marker))
-             (set-marker tem (point))
-             (set-buffer standard-output)
-             (setq occur-pos-list (cons tem occur-pos-list))
-             (or first (zerop nlines)
-                 (insert "--------\n"))
-             (setq first nil)
-             (insert-buffer-substring buffer start end)
-             (backward-char (- end start))
-             (setq tem nlines)
-             (while (> tem 0)
-               (insert empty ?:)
-               (forward-line 1)
-               (setq tem (1- tem)))
-             (let ((this-linenum linenum))
-               (set-marker final-context-start
-                           (+ (point) (- (match-end 0) (match-beginning 0))))
-               (while (< (point) final-context-start)
-                 (if (null tag)
-                     (setq tag (format "%3d" this-linenum)))
-                 (insert tag ?:)
-                 (put-text-property (save-excursion
-                                      (beginning-of-line)
-                                      (point))
-                                    (save-excursion
-                                      (end-of-line)
-                                      (point))
-                                    'mouse-face 'highlight)
-                 (setq tag nil)
-                 (forward-line 1)
-                 (setq this-linenum (1+ this-linenum))))
-             (while (< tem nlines)
-               (insert empty ?:)
-               (forward-line 1)
-               (setq tem (1+ tem))))                           
-           (forward-line 1)))
-       (set-buffer standard-output)
-       ;; Put positions in increasing order to go with buffer.
-       (setq occur-pos-list (nreverse occur-pos-list))
-       (goto-char (point-min))
-       (if (= (length occur-pos-list) 1)
-           (insert "1 line")
-         (insert (format "%d lines" (length occur-pos-list))))
-       (if (interactive-p)
-           (message "%d matching lines." (length occur-pos-list)))))))
+    (save-excursion
+      (goto-char (point-min))
+      ;; Check first whether there are any matches at all.
+      (if (not (re-search-forward regexp nil t))
+         (message "No matches for `%s'" regexp)
+       ;; Back up, so the search loop below will find the first match.
+       (goto-char (match-beginning 0))
+       (with-output-to-temp-buffer "*Occur*"
+         (save-excursion
+           (set-buffer standard-output)
+           (setq default-directory dir)
+           ;; We will insert the number of lines, and "lines", later.
+           (insert " matching ")
+           (let ((print-escape-newlines t))
+             (prin1 regexp))
+           (insert " in buffer " (buffer-name buffer) ?. ?\n)
+           (occur-mode)
+           (setq occur-buffer buffer)
+           (setq occur-nlines nlines)
+           (setq occur-pos-list ()))
+         (if (eq buffer standard-output)
+             (goto-char (point-max)))
+         (save-excursion
+           ;; Find next match, but give up if prev match was at end of buffer.
+           (while (and (not (= prevpos (point-max)))
+                       (re-search-forward regexp nil t))
+             (goto-char (match-beginning 0))
+             (beginning-of-line)
+             (save-match-data
+               (setq linenum (+ linenum (count-lines prevpos (point)))))
+             (setq prevpos (point))
+             (goto-char (match-end 0))
+             (let* ((start (save-excursion
+                             (goto-char (match-beginning 0))
+                             (forward-line (if (< nlines 0) nlines (- nlines)))
+                             (point)))
+                    (end (save-excursion
+                           (goto-char (match-end 0))
+                           (if (> nlines 0)
+                               (forward-line (1+ nlines))
+                               (forward-line 1))
+                           (point)))
+                    ;; Record where the actual match 
+                    (match-offset
+                     (save-excursion
+                       (goto-char (match-beginning 0))
+                       (beginning-of-line)
+                       ;; +6 to skip over line number
+                       (+ 6 (- (match-beginning 0) (point)))))
+                    (match-len (- (match-end 0) (match-beginning 0)))
+                    (tag (format "%5d" linenum))
+                    (empty (make-string (length tag) ?\ ))
+                    tem)
+               (save-excursion
+                 (setq tem (make-marker))
+                 (set-marker tem (point))
+                 (set-buffer standard-output)
+                 (setq occur-pos-list (cons tem occur-pos-list))
+                 (or first (zerop nlines)
+                     (insert "--------\n"))
+                 (setq first nil)
+                 (insert-buffer-substring buffer start end)
+                 (set-marker final-context-start 
+                             (- (point) (- end (match-end 0))))
+                 (backward-char (- end start))
+                 (setq tem nlines)
+                 (while (> tem 0)
+                   (insert empty ?:)
+                   (forward-line 1)
+                   (setq tem (1- tem)))
+                 (let ((this-linenum linenum)
+                       line-start)
+                   (while (< (point) final-context-start)
+                     (if (null tag)
+                         (setq tag (format "%5d" this-linenum)))
+                     (insert tag ?:)
+                     (setq line-start
+                           (save-excursion
+                             (beginning-of-line)
+                             (point)))
+                     (put-text-property line-start
+                                        (save-excursion
+                                          (end-of-line)
+                                          (point))
+                                        'mouse-face 'highlight)
+                     (if list-matching-lines-face
+                         (put-text-property
+                          (+ line-start match-offset)
+                          (+ line-start match-offset match-len)
+                          'face list-matching-lines-face))
+                     (forward-line 1)
+                     (setq tag nil)
+                     (setq this-linenum (1+ this-linenum)))
+                   (while (<= (point) final-context-start)
+                     (insert empty ?:)
+                     (forward-line 1)
+                     (setq this-linenum (1+ this-linenum))))
+                 (while (< tem nlines)
+                   (insert empty ?:)
+                   (forward-line 1)
+                   (setq tem (1+ tem)))
+                 (goto-char (point-max)))
+               (forward-line 1)))
+           (set-buffer standard-output)
+           ;; Put positions in increasing order to go with buffer.
+           (setq occur-pos-list (nreverse occur-pos-list))
+           (goto-char (point-min))
+           (let ((message-string
+                  (if (= (length occur-pos-list) 1)
+                      "1 line"
+                    (format "%d lines" (length occur-pos-list)))))
+             (insert message-string)
+             (if (interactive-p)
+                 (message "%s matched" message-string)))))))))
 \f
 ;; It would be nice to use \\[...], but there is no reasonable way
 ;; to make that display both SPC and Y.
@@ -476,6 +515,8 @@ The valid answers include `act', `skip', `act-and-show',
 (define-key query-replace-map "!" 'automatic)
 (define-key query-replace-map "^" 'backup)
 (define-key query-replace-map "\C-h" 'help)
+(define-key query-replace-map [f1] 'help)
+(define-key query-replace-map [help] 'help)
 (define-key query-replace-map "?" 'help)
 (define-key query-replace-map "\C-g" 'quit)
 (define-key query-replace-map "\C-]" 'quit)
@@ -492,6 +533,8 @@ just as `query-replace' does.  Instead, write a simple loop like this:
     (replace-match \"foobar\" nil nil))
 which will run faster and probably do exactly what you want."
   (or map (setq map query-replace-map))
+  (and query-flag minibuffer-auto-raise
+       (raise-frame (window-frame (minibuffer-window))))
   (let ((nocasify (not (and case-fold-search case-replace
                            (string-equal from-string
                                          (downcase from-string)))))
@@ -543,7 +586,8 @@ which will run faster and probably do exactly what you want."
                      t))
 
          ;; Save the data associated with the real match.
-         (setq real-match-data (match-data))
+         ;; For speed, use only integers and reuse the list used last time.
+         (setq real-match-data (match-data t real-match-data))
 
          ;; Before we make the replacement, decide whether the search string
          ;; can match again just after this match.
@@ -569,7 +613,10 @@ which will run faster and probably do exactly what you want."
              (while (not done)
                (store-match-data real-match-data)
                (replace-highlight (match-beginning 0) (match-end 0))
-               (message message from-string next-replacement)
+               ;; Bind message-log-max so we don't fill up the message log
+               ;; with a bunch of identical messages.
+               (let ((message-log-max nil))
+                 (message message from-string next-replacement))
                (setq key (read-event))
                (setq key (vector key))
                (setq def (lookup-key map key))
@@ -602,21 +649,28 @@ which will run faster and probably do exactly what you want."
                         (sit-for 1)))
                      ((eq def 'act)
                       (or replaced
-                          (replace-match next-replacement nocasify literal))
+                          (progn
+                            (replace-match next-replacement nocasify literal)
+                            (setq replace-count (1+ replace-count))))
                       (setq done t replaced t))
                      ((eq def 'act-and-exit)
                       (or replaced
-                          (replace-match next-replacement nocasify literal))
+                          (progn
+                            (replace-match next-replacement nocasify literal)
+                            (setq replace-count (1+ replace-count))))
                       (setq keep-going nil)
                       (setq done t replaced t))
                      ((eq def 'act-and-show)
                       (if (not replaced)
                           (progn
                             (replace-match next-replacement nocasify literal)
+                            (setq replace-count (1+ replace-count))
                             (setq replaced t))))
                      ((eq def 'automatic)
                       (or replaced
-                          (replace-match next-replacement nocasify literal))
+                          (progn
+                            (replace-match next-replacement nocasify literal)
+                            (setq replace-count (1+ replace-count))))
                       (setq done t query-flag nil replaced t))
                      ((eq def 'skip)
                       (setq done t))
@@ -652,14 +706,8 @@ which will run faster and probably do exactly what you want."
              ;; since lots of markers slow down editing.
              (setq stack
                    (cons (cons (point)
-                               (or replaced
-                                   (mapcar (lambda (elt)
-                                             (and elt
-                                                  (prog1 (marker-position elt)
-                                                    (set-marker elt nil))))
-                                    (match-data))))
-                         stack))
-             (if replaced (setq replace-count (1+ replace-count)))))
+                               (or replaced (match-data t)))
+                         stack))))
          (setq lastrepl (point)))
       (replace-dehighlight))
     (or unread-command-events