branch: externals/urgrep
commit 92e3fc9141f55bed40f0e0e3e5d5476a44dec8fd
Author: Jim Porter <jporterb...@gmail.com>
Commit: Jim Porter <jporterb...@gmail.com>

    Add a single-key way to expand/contract context in the results buffer
---
 NEWS.md         |  7 +++++++
 README.md       |  8 ++++++++
 urgrep-wgrep.el |  2 +-
 urgrep-xref.el  |  2 +-
 urgrep.el       | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 5 files changed, 76 insertions(+), 4 deletions(-)

diff --git a/NEWS.md b/NEWS.md
index f9102ec4dc..d634303494 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -1,5 +1,12 @@
 # Urgrep News
 
+## v0.5.0 (in progress)
+
+### New features
+- Add ability to adjust the context of search results in the results buffer
+
+---
+
 ## v0.4.1 (2024-03-10)
 
 ### Bug fixes
diff --git a/README.md b/README.md
index 540a3b0a21..10098a8ca9 100644
--- a/README.md
+++ b/README.md
@@ -53,6 +53,14 @@ Isearch-like key bindings to let you modify the search's 
behavior:
 In addition to the above, you can call `urgrep-run-command`, which works like
 `urgrep` but allows you to manually edit the command before executing it.
 
+### Modifying your search
+
+After performing a search, you can adjust an existing query with <kbd>C-u</kbd>
+<kbd>g</kbd>, reopening the original search prompt. You can also adjust the
+context directly via `C`, `B`, or `A` to change the context, leading context, 
or
+trailing context, respectively. By default, these expand the context by one
+line, but with a prefix argument, you can change by *N* lines.
+
 ### Configuring the tool to use
 
 By default, Urgrep tries all search tools it's aware of to find the best 
option.
diff --git a/urgrep-wgrep.el b/urgrep-wgrep.el
index 1c9d59d4a0..1a6957e50a 100644
--- a/urgrep-wgrep.el
+++ b/urgrep-wgrep.el
@@ -4,7 +4,7 @@
 
 ;; Author: Jim Porter
 ;; URL: https://github.com/jimporter/urgrep
-;; Version: 0.4.2-git
+;; Version: 0.5.0-git
 ;; Keywords: grep, search
 
 ;; This file is NOT part of GNU Emacs.
diff --git a/urgrep-xref.el b/urgrep-xref.el
index fb176aae65..b8607cacdf 100644
--- a/urgrep-xref.el
+++ b/urgrep-xref.el
@@ -4,7 +4,7 @@
 
 ;; Author: Jim Porter
 ;; URL: https://github.com/jimporter/urgrep
-;; Version: 0.4.2-git
+;; Version: 0.5.0-git
 ;; Keywords: grep, search
 
 ;; This file is NOT part of GNU Emacs.
diff --git a/urgrep.el b/urgrep.el
index 227814f56a..a5bfb29984 100644
--- a/urgrep.el
+++ b/urgrep.el
@@ -4,7 +4,7 @@
 
 ;; Author: Jim Porter
 ;; URL: https://github.com/jimporter/urgrep
-;; Version: 0.4.2-git
+;; Version: 0.5.0-git
 ;; Keywords: grep, search
 ;; Package-Requires: ((emacs "27.1") (compat "29.1.0.1") (project "0.3.0"))
 
@@ -743,6 +743,60 @@ If EDIT-COMMAND is non-nil, the search can be edited."
                     query)))
     (urgrep--start command query urgrep-current-tool)))
 
+(defmacro urgrep-adjust-query (prop &rest body)
+  "Adjust the current search query by updating PROP.
+This gets PROP from `urgrep-current-query' and let-binds it to a
+variable of the same name, excluding the leading colon (so,
+e.g. `:context' becomes `context'), and then evaluates BODY.
+Finally, this sets PROP to BODY's return value and re-runs the
+search."
+  (declare (indent 1))
+  (unless (keywordp prop)
+    (error "invalid PROP %S" prop))
+  (let ((prop-variable (intern (substring (symbol-name prop) 1))))
+    `(if (not (listp urgrep-current-query))
+         (error "nope")
+       (let* ((new-value
+               (let ((,prop-variable (plist-get (cdr urgrep-current-query)
+                                                ,prop)))
+                 ,@body))
+              (new-query (cons (car urgrep-current-query)
+                               (plist-put (cdr urgrep-current-query)
+                                          ,prop new-value))))
+         (urgrep--start (apply #'urgrep-command new-query) new-query
+                        urgrep-current-tool)))))
+
+(defun urgrep-expand-context (&optional lines)
+  "Expand the context of the current query by LINES.
+If LINES is nil, expand the context by one line."
+  (interactive "p")
+  (setq lines (or lines 1))
+  (urgrep-adjust-query :context
+    (if (consp context)
+        (cons (max (+ (car context) lines) 0)
+              (max (+ (cdr context) lines) 0))
+      (max (+ (or context 0) lines) 0))))
+
+(defun urgrep-expand-before-context (&optional lines)
+  "Expand the before-context of the current query by LINES.
+If LINES is nil, expand the before-context by one line."
+  (interactive "p")
+  (setq lines (or lines 1))
+  (urgrep-adjust-query :context
+    (let ((context (if (consp context) context
+                     (cons (or context 0) (or context 0)))))
+      (cons (max (+ (car context) lines) 0) (cdr context)))))
+
+(defun urgrep-expand-after-context (&optional lines)
+  "Expand the after-context of the current query by LINES.
+If LINES is nil, expand the after-context by one line."
+  (interactive "p")
+  (setq lines (or lines 1))
+  (urgrep-adjust-query :context
+    (let ((context (if (consp context) context
+                     (cons (or context 0) (or context 0)))))
+      (cons (car context) (max (+ (cdr context) lines) 0)))))
+
 (defvar-keymap urgrep-mode-map
   ;; Don't inherit from `compilation-minor-mode-map', because that introduces a
   ;; menu bar item we don't want.
@@ -763,7 +817,10 @@ If EDIT-COMMAND is non-nil, the search can be edited."
   "}"             #'compilation-next-file
   "TAB"           #'compilation-next-error
   "<backtab>"     #'compilation-previous-error
-  "g"             #'urgrep-search-again)
+  "g"             #'urgrep-search-again
+  "C"             #'urgrep-expand-context
+  "B"             #'urgrep-expand-before-context
+  "A"             #'urgrep-expand-after-context)
 
 (easy-menu-define urgrep-menu-map urgrep-mode-map
   "Menu for urgrep buffers."

Reply via email to