emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/urgrep c0dfeab713 2/3: Rework `urgrep-filter` to be sin


From: ELPA Syncer
Subject: [elpa] externals/urgrep c0dfeab713 2/3: Rework `urgrep-filter` to be single-pass
Date: Tue, 26 Dec 2023 21:58:46 -0500 (EST)

branch: externals/urgrep
commit c0dfeab7133c380678af94cdae10063ff8b8c4dc
Author: Jim Porter <jporterbugs@gmail.com>
Commit: Jim Porter <jporterbugs@gmail.com>

    Rework `urgrep-filter` to be single-pass
---
 urgrep.el | 102 ++++++++++++++++++++++++++++++++++++--------------------------
 1 file changed, 60 insertions(+), 42 deletions(-)

diff --git a/urgrep.el b/urgrep.el
index 56f8e75e8a..8518d5479e 100644
--- a/urgrep.el
+++ b/urgrep.el
@@ -711,6 +711,9 @@ COLOR: non-nil (the default) if the output should use 
color."
 (defvar-local urgrep-current-tool nil
   "The most recent search tool used in this buffer.")
 
+(defvar-local urgrep--filter-start nil
+  "The in-buffer position to start `urgrep-filter'.")
+
 ;; Set the first column to 0 because that's how we currently count.
 ;; XXX: It might be worth changing this to 1 if we allow reading the column
 ;; number explicitly in the output.
@@ -949,47 +952,61 @@ See `compilation-error-regexp-alist' for format details.")
 (defun urgrep-filter ()
   "Handle match highlighting escape sequences inserted by the process.
 This function is called from `compilation-filter-hook'."
-  (rx-let ((ansi-sgr (&rest rest) (seq "\033[" rest "m")))
-    (save-excursion
-      (forward-line 0)
-      (let ((end (point)) beg)
-        (goto-char compilation-filter-start)
-        (forward-line 0)
-        (setq beg (point))
-        ;; 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 matches and delete ANSI escapes.
-          (while (re-search-forward
-                  (rx (or ;; Find the escapes together...
-                       (ansi-sgr (or "01" "1") ";31")
-                       ;; ... or apart.
-                       (seq (ansi-sgr (or "01" "1"))
-                            (ansi-sgr "31")))
-                      (group (*? nonl) (? "\n"))
-                      (ansi-sgr (? "0")))
-                  end 1)
-            (replace-match
-             (propertize (match-string 1) 'face nil
-                         'font-lock-face 'urgrep-match)
-             t t)
-            (cl-incf urgrep-num-matches-found))
-          ;; Highlight matching filenames and delete ANSI escapes.
-          (goto-char beg)
-          (while (re-search-forward
-                  (rx (ansi-sgr "35") (group (*? nonl)) (ansi-sgr (? "0")))
-                  end 1)
-            (replace-match
-             (propertize (match-string 1) 'face nil 'font-lock-face 'urgrep-hit
-                         'urgrep-file-name t)
-             t t))
-          ;; Delete all remaining escape sequences.
-          (goto-char beg)
-          (while (re-search-forward
-                  (rx "\033[" (* (any digit ";")) (any "m" "K"))
-                  end 1)
-            (replace-match "" t t)))))))
+  (unless urgrep--filter-start
+    (setq urgrep--filter-start compilation-filter-start))
+  (save-excursion
+    (rx-let ((ansi-cseq (&rest rest) (seq "\033[" rest))
+             (ansi-any-cseq (ansi-cseq (*? anything) (any "@-~")))
+             (ansi-sgr (&rest rest) (ansi-cseq rest "m")))
+      (let ((end (point)))
+        (goto-char urgrep--filter-start)
+        (catch 'done
+          (while (search-forward "\033" end 'limit)
+            (when (eq (char-after) ?\[)
+              (backward-char)
+              (unless (looking-at (rx ansi-any-cseq))
+                ;; This control sequence is incomplete.  Try again next time.
+                (throw 'done nil))
+              (cond
+               ;; Delete "erase in line" ANSI CSI sequence.
+               ((re-search-forward (rx point (ansi-cseq (* digit) "K")) end t)
+                (replace-match "" t t))
+               ;; Highlight matching filenames and delete ANSI SGR escapes.
+               ((re-search-forward (rx bol point (ansi-sgr "35")
+                                       (group (*? anything))
+                                       (ansi-sgr (? "0")))
+                                   end t)
+                (replace-match
+                 (propertize (match-string 1) 'face nil
+                             'font-lock-face 'urgrep-hit 'urgrep-file-name t)
+                 t t))
+               ;; Highlight matches and delete ANSI SGR escapes.
+               ((re-search-forward (rx point
+                                       (or ;; Find the escapes together...
+                                        (ansi-sgr (or "01" "1") ";31")
+                                        ;; ... or apart.
+                                        (seq (ansi-sgr (or "01" "1"))
+                                             (ansi-sgr "31")))
+                                       ;; Matches always stop by end of line.
+                                       ;; (Multi-line matches get restarted
+                                       ;; after the line number is printed.)
+                                       (group (*? nonl) (? "\n"))
+                                       (ansi-sgr (? "0")))
+                                   end t)
+                (replace-match
+                 (propertize (match-string 1) 'face nil
+                             'font-lock-face 'urgrep-match)
+                 t t)
+                (cl-incf urgrep-num-matches-found))
+               ;; Delete no-op ANSI SGR escapes.
+               ((re-search-forward (rx point (ansi-sgr (? "0"))
+                                       (group (*? anything))
+                                       (ansi-sgr (? "0")))
+                                   end t)
+                (replace-match (match-string 1) t t))
+               ;; If nothing matched, try again next time.
+               (t (throw 'done nil))))))
+        (setq urgrep--filter-start (point))))))
 
 (define-compilation-mode urgrep-mode "Urgrep"
   "A compilation mode for various grep-like tools."
@@ -999,7 +1016,8 @@ This function is called from `compilation-filter-hook'."
               compilation-error-regexp-alist urgrep-regexp-alist
               compilation-mode-line-errors urgrep-mode-line-matches
               compilation-disable-input t
-              compilation-error-screen-columns nil)
+              compilation-error-screen-columns nil
+              urgrep--filter-start nil)
   (add-hook 'compilation-filter-hook #'urgrep-filter nil t))
 
 (defun urgrep--hide-abbreviations (command)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]