Skip to content

Commit

Permalink
Add ability to immediately toggle case folding and hidden search from…
Browse files Browse the repository at this point in the history
… results
  • Loading branch information
jimporter committed May 11, 2024
1 parent 3bead25 commit dbfefe5
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 29 deletions.
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
## v0.5.0 (in progress)

### New features
- Add ability to adjust the context of search results in the results buffer
- Add ability to immediately adjust search options in the results buffer

### Breaking changes
- In the Urgrep minibuffer, toggling search of hidden files now uses `M-s H`

---

Expand Down
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ Isearch-like key bindings to let you modify the search's behavior:

| Key binding | Action |
|:----------------------------|:-----------------------------------------|
| <kbd>M-s</kbd> <kbd>h</kbd> | Describe key bindings |
| <kbd>M-s</kbd> <kbd>r</kbd> | Toggle regexp search |
| <kbd>M-s</kbd> <kbd>c</kbd> | Toggle case sensitivity |
| <kbd>M-s</kbd> <kbd>h</kbd> | Toggle searching in hidden files |
| <kbd>M-s</kbd> <kbd>c</kbd> | Toggle case folding |
| <kbd>M-s</kbd> <kbd>H</kbd> | Toggle searching in hidden files |
| <kbd>M-s</kbd> <kbd>f</kbd> | Set wildcards to filter files¹ |
| <kbd>M-s</kbd> <kbd>C</kbd> | Set number of lines of context² |
| <kbd>M-s</kbd> <kbd>B</kbd> | Set number of lines of leading context² |
Expand All @@ -56,11 +57,18 @@ In addition to the above, you can call `urgrep-run-command`, which works like
### 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 <kbd>C</kbd>, <kbd>B</kbd>, or <kbd>A</kbd> 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.
<kbd>g</kbd>, reopening the original search prompt. You can also adjust some of
the search options, such as case folding, immediately:

| Key binding | Action |
|:-------------|:----------------------------------|
| <kbd>c</kbd> | Toggle case folding |
| <kdb>H</kbd> | Toggle searching in hidden files |
| <kbd>C</kbd> | Expand lines of context¹ |
| <kbd>B</kbd> | Expand lines of leading context¹ |
| <kbd>A</kbd> | Expand lines of trailing context¹ |

> 1. Expand by one line by default, or by *N* lines with a prefix argument
### Configuring the tool to use

Expand Down
78 changes: 57 additions & 21 deletions urgrep.el
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,18 @@ it up in `urgrep-tools'. Otherwise, return TOOL as-is."
((and (eq syntax 'pcre) (memq 'extended tool-syntaxes)) 'ere)
(t (car tool-syntaxes)))))

(defun urgrep--case-fold-p (case-fold query regexp-syntax)
"Return whether to enable case folding.
If CASE-FOLD is `inherit' or `smart', guess based on the QUERY
and REGEXP-SYNTAX. Otherwise, just return CASE-FOLD."
(when (eq case-fold 'inherit)
(setq case-fold (if case-fold-search 'smart nil)))
(when (eq case-fold 'smart)
(when regexp-syntax
(setq query (urgrep--convert-regexp query regexp-syntax 'bre)))
(setq case-fold (isearch-no-upper-case-p query regexp-syntax)))
case-fold)

;;;###autoload
(cl-defun urgrep-command (query &key tool regexp (case-fold 'inherit) hidden
file-wildcard (root ".") (group t) (context 0)
Expand Down Expand Up @@ -671,19 +683,15 @@ respectively).
COLOR: non-nil (the default) if the output should use color."
(with-connection-local-variables
(let* ((regexp-syntax (if (eq regexp t) urgrep-regexp-syntax regexp))
(file-wildcard (ensure-list file-wildcard))
(root (mapcar #'urgrep--safe-file-name (ensure-list root)))
(tool (or (urgrep-get-tool tool)
(let* ((tool (or (urgrep-get-tool tool)
(error "Unknown tool %s" tool)))
(regexp-syntax (if (eq regexp t) urgrep-regexp-syntax regexp))
(tool-re-syntax (urgrep--get-best-syntax regexp-syntax tool))
(case-fold (urgrep--case-fold-p case-fold query regexp-syntax))
(file-wildcard (ensure-list file-wildcard))
(root (mapcar #'urgrep--safe-file-name (ensure-list root)))
(query (urgrep--convert-regexp query regexp-syntax tool-re-syntax))
(cmd-fun (urgrep--get-prop 'command-function tool)))
;; Determine whether to search case-sensitively or not.
(when (eq case-fold 'inherit)
(setq case-fold (if case-fold-search 'smart nil)))
(when (eq case-fold 'smart)
(setq case-fold (isearch-no-upper-case-p query regexp-syntax)))
;; Build the command arguments.
(if cmd-fun
(funcall cmd-fun query :tool tool :regexp tool-re-syntax
Expand Down Expand Up @@ -766,9 +774,24 @@ search."
(urgrep--start (apply #'urgrep-command new-query) new-query
urgrep-current-tool)))))

(defun urgrep-expand-context (&optional lines)
(defun urgrep-toggle-current-case-fold ()
"Toggle case folding for the current query."
(interactive)
(urgrep-adjust-query :case-fold
(let ((query (car urgrep-current-query))
(regexp-syntax (plist-get (cdr urgrep-current-query) :regexp-syntax)))
(not (urgrep--case-fold-p case-fold query regexp-syntax)))))

(defun urgrep-toggle-current-search-hidden-files ()
"Toggle searching of hidden files for the current query."
(interactive)
(urgrep-adjust-query :hidden
(not hidden)))

(defun urgrep-expand-current-context (&optional lines)
"Expand the context of the current query by LINES.
If LINES is nil, expand the context by one line."
Interactively, LINES is the numeric prefix argument. If LINES is
nil, expand the context by one line."
(interactive "p")
(setq lines (or lines 1))
(urgrep-adjust-query :context
Expand All @@ -777,19 +800,21 @@ If LINES is nil, expand the context by one line."
(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."
(defun urgrep-expand-current-before-context (&optional lines)
"Expand the leading context of the current query by LINES.
Interactively, LINES is the numeric prefix argument.If LINES is
nil, expand the leading 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."
(defun urgrep-expand-current-after-context (&optional lines)
"Expand the trailing context of the current query by LINES.
Interactively, LINES is the numeric prefix argument. If LINES is
nil, expand the trailing context by one line."
(interactive "p")
(setq lines (or lines 1))
(urgrep-adjust-query :context
Expand Down Expand Up @@ -818,9 +843,11 @@ If LINES is nil, expand the after-context by one line."
"TAB" #'compilation-next-error
"<backtab>" #'compilation-previous-error
"g" #'urgrep-search-again
"C" #'urgrep-expand-context
"B" #'urgrep-expand-before-context
"A" #'urgrep-expand-after-context)
"c" #'urgrep-toggle-current-case-fold
"H" #'urgrep-toggle-current-search-hidden-files
"C" #'urgrep-expand-current-context
"B" #'urgrep-expand-current-before-context
"A" #'urgrep-expand-current-after-context)

(easy-menu-define urgrep-menu-map urgrep-mode-map
"Menu for urgrep buffers."
Expand Down Expand Up @@ -1325,15 +1352,24 @@ future searches."

(defvar-keymap urgrep-minibuffer-map
:parent minibuffer-local-map
"M-s h" #'urgrep-minibuffer-help
"M-s r" #'urgrep-toggle-regexp
"M-s c" #'urgrep-toggle-case-fold
"M-s f" #'urgrep-set-file-wildcards
"M-s h" #'urgrep-toggle-search-hidden-files
"M-s H" #'urgrep-toggle-search-hidden-files
"M-s C" #'urgrep-set-context
"M-s B" #'urgrep-set-before-context
"M-s A" #'urgrep-set-after-context
"M-s t" #'urgrep-set-tool)

(defun urgrep-minibuffer-help ()
"Describe key bindings for the Urgrep minibuffer."
(interactive)
;; `describe-keymap' is only available in Emacs 28+.
(if (fboundp 'describe-keymap)
(describe-keymap urgrep-minibuffer-map)
(describe-bindings)))

(cl-defun urgrep--read-query (initial &key tool (regexp urgrep-search-regexp)
(case-fold urgrep-case-fold)
(hidden urgrep-search-hidden-files)
Expand Down

0 comments on commit dbfefe5

Please sign in to comment.