]> code.delx.au - gnu-emacs/blobdiff - lisp/progmodes/grep.el
Add rx.el support for numbered groups (Bug#8776).
[gnu-emacs] / lisp / progmodes / grep.el
index a4c9b7fccba97001544aaa8be25d4af64a63f528..db8e82193b340fdd4c5c6061350cc618d766ef8f 100644 (file)
@@ -1,4 +1,4 @@
-;;; grep.el --- run Grep as inferior of Emacs, parse match messages
+;;; grep.el --- run `grep' and display the results
 
 ;; Copyright (C) 1985-1987, 1993-1999, 2001-2011
 ;;   Free Software Foundation, Inc.
@@ -33,7 +33,7 @@
 
 
 (defgroup grep nil
-  "Run grep as inferior of Emacs, parse error messages."
+  "Run `grep' and display the results."
   :group 'tools
   :group 'processes)
 
@@ -399,26 +399,7 @@ Notice that using \\[next-error] or \\[compile-goto-error] modifies
       (0 '(face nil compilation-message nil help-echo nil mouse-face nil) t)
       (1 grep-error-face)
       (2 grep-error-face nil t))
-     ("^.+?-[0-9]+-.*\n" (0 grep-context-face))
-     ;; Highlight grep matches and delete markers.
-     ;; FIXME: Modifying the buffer text from font-lock is a bad idea!
-     ("\\(\033\\[01;31m\\)\\(.*?\\)\\(\033\\[[0-9]*m\\)"
-      ;; Refontification does not work after the markers have been
-      ;; deleted.  So we use the font-lock-face property here as Font
-      ;; Lock does not clear that.
-      (2 (list 'face nil 'font-lock-face grep-match-face))
-      ((lambda (bound))
-       (progn
-        ;; Delete markers with `replace-match' because it updates
-        ;; the match-data, whereas `delete-region' would render it obsolete.
-        (syntax-ppss-flush-cache (match-beginning 0))
-        (replace-match "" t t nil 3)
-        (replace-match "" t t nil 1))))
-     ("\033\\[[0-9;]*[mK]"
-      ;; Delete all remaining escape sequences
-      ((lambda (bound))
-       (syntax-ppss-flush-cache (match-beginning 0))
-       (replace-match "" t t))))
+     ("^.+?-[0-9]+-.*\n" (0 grep-context-face)))
    "Additional things to highlight in grep output.
 This gets tacked on the end of the generated expressions.")
 
@@ -440,10 +421,11 @@ This variable's value takes effect when `grep-compute-defaults' is called.")
 
 ;;;###autoload
 (defvar grep-find-use-xargs nil
-  "Non-nil means that `grep-find' uses the `xargs' utility by default.
-If `exec', use `find -exec'.
+  "How to invoke find and grep.
+If `exec', use `find -exec {} ;'.
+If `exec-plus' use `find -exec {} +'.
 If `gnu', use `find -print0' and `xargs -0'.
-Any other non-nil value means to use `find -print' and `xargs'.
+Any other value means to use `find -print' and `xargs'.
 
 This variable's value takes effect when `grep-compute-defaults' is called.")
 
@@ -490,6 +472,28 @@ Set up `compilation-exit-message-function' and run `grep-setup-hook'."
           (cons msg code))))
   (run-hooks 'grep-setup-hook))
 
+(defun grep-filter ()
+  "Handle match highlighting escape sequences inserted by the grep process.
+This function is called from `compilation-filter-hook'."
+  (save-excursion
+    (forward-line 0)
+    (let ((end (point)))
+      (goto-char compilation-filter-start)
+      (forward-line 0)
+      ;; Only operate on whole lines so we don't get caught with part of an
+      ;; escape sequence in one chunk and the rest in another.
+      (when (< (point) end)
+        (setq end (copy-marker end))
+        ;; Highlight grep matches and delete marking sequences.
+        (while (re-search-forward "\033\\[01;31m\\(.*?\\)\033\\[[0-9]*m" end 1)
+          (replace-match (propertize (match-string 1)
+                                     'face nil 'font-lock-face grep-match-face)
+                         t t))
+        ;; Delete all remaining escape sequences
+        (goto-char compilation-filter-start)
+        (while (re-search-forward "\033\\[[0-9;]*[mK]" end 1)
+          (replace-match "" t t))))))
+
 (defun grep-probe (command args &optional func result)
   (let (process-file-side-effects)
     (equal (condition-case nil
@@ -561,6 +565,10 @@ Set up `compilation-exit-message-function' and run `grep-setup-hook'."
        (unless grep-find-use-xargs
          (setq grep-find-use-xargs
                (cond
+                ((grep-probe find-program
+                             `(nil nil nil ,null-device "-exec" "echo"
+                                   "{}" "+"))
+                 'exec-plus)
                 ((and
                   (grep-probe find-program `(nil nil nil ,null-device "-print0"))
                   (grep-probe xargs-program `(nil nil nil "-0" "-e" "echo")))
@@ -575,13 +583,17 @@ Set up `compilation-exit-message-function' and run `grep-setup-hook'."
                       ;; forward slashes as directory separators.
                       (format "%s . -type f -print0 | \"%s\" -0 -e %s"
                               find-program xargs-program grep-command))
-                     ((eq grep-find-use-xargs 'exec)
+                     ((memq grep-find-use-xargs '(exec exec-plus))
                       (let ((cmd0 (format "%s . -type f -exec %s"
-                                          find-program grep-command)))
+                                          find-program grep-command))
+                            (null (if grep-use-null-device
+                                      (format "%s " null-device)
+                                    "")))
                         (cons
-                         (format "%s {} %s %s"
-                                 cmd0 null-device
-                                 (shell-quote-argument ";"))
+                         (if (eq grep-find-use-xargs 'exec-plus)
+                             (format "%s %s{} +" cmd0 null)
+                           (format "%s {} %s%s" cmd0 null
+                                   (shell-quote-argument ";")))
                          (1+ (length cmd0)))))
                      (t
                       (format "%s . -type f -print | \"%s\" %s"
@@ -589,14 +601,20 @@ Set up `compilation-exit-message-function' and run `grep-setup-hook'."
        (unless grep-find-template
          (setq grep-find-template
                (let ((gcmd (format "%s <C> %s <R>"
-                                   grep-program grep-options)))
+                                   grep-program grep-options))
+                     (null (if grep-use-null-device
+                               (format "%s " null-device)
+                             "")))
                  (cond ((eq grep-find-use-xargs 'gnu)
                         (format "%s . <X> -type f <F> -print0 | \"%s\" -0 -e %s"
                                 find-program xargs-program gcmd))
                        ((eq grep-find-use-xargs 'exec)
-                        (format "%s . <X> -type f <F> -exec %s {} %s %s"
-                                find-program gcmd null-device
+                        (format "%s . <X> -type f <F> -exec %s {} %s%s"
+                                find-program gcmd null
                                 (shell-quote-argument ";")))
+                       ((eq grep-find-use-xargs 'exec-plus)
+                        (format "%s . <X> -type f <F> -exec %s %s{} +"
+                                find-program gcmd null))
                        (t
                         (format "%s . <X> -type f <F> -print | \"%s\" %s"
                                 find-program xargs-program gcmd))))))))
@@ -680,9 +698,13 @@ Set up `compilation-exit-message-function' and run `grep-setup-hook'."
        grep-hit-face)
   (set (make-local-variable 'compilation-error-regexp-alist)
        grep-regexp-alist)
+  ;; compilation-directory-matcher can't be nil, so we set it to a regexp that
+  ;; can never match.
+  (set (make-local-variable 'compilation-directory-matcher) '("\\`a\\`"))
   (set (make-local-variable 'compilation-process-setup-function)
        'grep-process-setup)
-  (set (make-local-variable 'compilation-disable-input) t))
+  (set (make-local-variable 'compilation-disable-input) t)
+  (add-hook 'compilation-filter-hook 'grep-filter nil t))
 
 
 ;;;###autoload