Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add buffer local var rg-match-positions to record all match positions #172

Merged
merged 1 commit into from
Sep 8, 2024

Conversation

liuyinz
Copy link
Contributor

@liuyinz liuyinz commented Jun 17, 2024

with the new var, user can replace matches very easily, how about this ? @dajva

@liuyinz
Copy link
Contributor Author

liuyinz commented Jun 17, 2024

A simple replace command

(defun rg-replace (to-string)
  "Replace matched result in rg-mode buffer."
  ;; SEE https://emacs.stackexchange.com/a/72155
  (interactive (list (minibuffer-with-setup-hook
                         (lambda () (set-mark (minibuffer-prompt-end)))
                       (read-string "Rg replace matched string with: "
                                    (pcase-let ((`(,begin . ,length)
                                                 (car rg-match-positions)))
                                      (buffer-substring-no-properties
                                       begin (+ length (marker-position begin))))))))
  (let ((stop-pos (point)))
    (unwind-protect
        (let ((keep-asking t)
              (replace-quit nil)
              (prompt (format "Replace match string with %s: (y,n,q,!,.) ?" to-string))
              (to-replaces (seq-filter (lambda (match)
                                         (< stop-pos
                                            (+ (marker-position (car match))
                                               (cdr match))))
                                       rg-match-positions)))
          (wgrep-change-to-wgrep-mode)
          (catch 'quit
            (dolist (cur-match to-replaces)
              (goto-char (setq stop-pos (car cur-match)))
              (let ((replace-p (not keep-asking)))
                (when keep-asking
                  (catch 'pass
                    (while-let ((key (single-key-description (read-key prompt) t)))
                      (when (member key '("q" "C-g" "ESC" "." "!" "y"
                                          "Y" "SPC" "n" "N" "DEL"))
                        (setq keep-asking (not (string= key "!")))
                        (setq replace-p (member key '("." "!" "y" "Y" "SPC")))
                        (setq replace-quit (member key '("q" "C-g" "ESC" ".")))
                        (throw 'pass nil)))))
                (when replace-p
                  (let ((begin (marker-position (car cur-match))))
                    (delete-region begin (+ begin (cdr cur-match)))
                    (insert to-string)))
                (when replace-quit (throw 'quit nil))))))
      (wgrep-finish-edit)
      (goto-char stop-pos))))

Copy link
Owner

@dajva dajva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR and sorry for the slow response. The feature looks fine, just one thing that needs to be fixed before merging.

rg-result.el Outdated Show resolved Hide resolved
@dajva dajva merged commit 8c7bdce into dajva:master Sep 8, 2024
13 checks passed
@coveralls
Copy link

Coverage Status

coverage: 81.869% (+0.1%) from 81.767%
when pulling 0c23e58 on liuyinz:feat/rgmatch-positions
into 6f1e39e on dajva:master.

@liuyinz liuyinz deleted the feat/rgmatch-positions branch September 8, 2024 11:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants