1

I recently discovered emacs's "regex-builder" mode and am absolutely loving it. However, there doesn't seem to be an easy way to type in an expression in the "RE-Bulider" buffer and use that expression to search for matches.

My use case for regexp-builder is currently to write a regular expression, and once I see some matches, I scan through the document manually to see all the matching strings. I currently use the "string" regex syntax for my regexp-builder, as recommended here, so I don't think I would need to modify the reb-lisp-mode hooks.

Right now, if I type in an expression in the RE-Builder buffer and press C-M-s or C-M-r (isearch forward/backward regexp), I get whichever regular expression I used with isearch last, which is oftentimes different than what I'm currently searching for. Additionally, these isearch commands only perform their search on the current buffer, which happens to be the regex-builder buffer, which leads to no matches being performed on the buffer I want to search.

What I want to do is to bind the C-s and C-r keys to re-search-forward and re-search-backward functions on the buffer I'm searching in whenever I begin using the regex builder.

I've made the following function in my ~/.emacs, but after evaluating the buffer, putting an expression in the RE Builder buffer and pressing the corresponding shortcuts, I still get isearch-forward, the default behavior.

(require 're-builder)
(setq reb-re-syntax 'string)

;; Improve regex searching through regexp builder
(defun regexp-builder-mode-config()
  "For use in `reb-mode-hook'."
  (local-set-key (kbd "C-s") 're-search-forward)
  (local-set-key (kbd "C-r") 're-search-backward)
  )
(add-hook 'reb-mode-hook 'regexp-builder-mode-config)

Essentially, what I want this to do is two-fold:

  1. Once I make a regular expression, press the key combination <C-s> or <C-r> and be able to search for a regular expression in the buffer.

  2. Solve the problem of emacs implicitly converting from one syntax (string) to the other (read). When I copy the expression I'm building to the kill ring (C-c C-w), emacs adds a bunch of extraneous backslashes (which I presume is due to how it handles regular expressions in the interactive buffer), which makes my regex not work anymore.

At the moment, I believe I'm close to solving the first point, which is being able to edit a regex and then conveniently search for it in the buffer I'm working on. However, when I press C-s or C-r, I get the usual isearch-forward/backward functionality I'm trying to avoid, almost as if me changing the hook didn't affect anything.

I checked the documentation, and I'm fairly sure I'm using the correct hook, but any and all help is welcome!

  • As well as reb-mode note that there's also reb-lisp-mode which is used for the rx and sregex syntax options; so you might not have the right mode hook for your use-case. – phils Aug 3 '18 at 0:16
  • That said, I'm not sure what you expect calling re-search-forward to do? It's not going to automatically use the regexp you're building, and you're going to be searching in the *RE-Builder* buffer, not in the original buffer. Unless that really is what you're wanting to do, I think you should update the question to describe the functionality you're trying to achieve, rather than just the details of an approach which may not actually be applicable. – phils Aug 3 '18 at 0:18
  • Thank you! I have since expanded on the problem I'm facing. Hopefully it's a bit clearer now! – Ricardo Iglesias Aug 3 '18 at 3:11
  • If you can copy the resulting regexp then you can paste into Isearch at the end of your search string, using M-y. – Drew Aug 3 '18 at 14:30
  • If I were you, I'd just use C-c C-s and C-c C-r which are bound to reb-next-match and reb-prev-match to move between the matches. (Of course you could rebind those functions to C-s and C-r.) – Omar Aug 4 '18 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.