Edited at

macros & regex - two headed monsters

they are so hard and esoteric that very few even attempt to put any questions let alone answer them. well, there are some ways to make life easier dealing with them as the following shows. but that doesn't change the fact that lisp macros are quite different:rolling_eyes: and emacs regex sucks big time:frowning2:


there's a nice package called macrostep.el that allows on the spot macro expansion and is widely mentioned in forums and blogs; only i didn't know about it until recently:sweat_smile: do make the following change not to compile macros during expansion:

(defadvice macrostep-expand (around macrostep-expand-no-compile activate)

(flet ((byte-compile (s) s))


there's a builtin package called re-builder.el but it's a bit clunky:tools: to use imo even with the addition of pcre2el.el. so what i do is use anzu.el (who doesn't?) and define some operations in isearch-mode-map.

customize anzu

(require 'anzu)

(global-anzu-mode +1)
(global-set-key [remap query-replace] 'anzu-query-replace)
(global-set-key [remap query-replace-regexp] 'anzu-query-replace-regexp)
(unless (keymap-parent lisp-mode-shared-map)
(set-keymap-parent lisp-mode-shared-map prog-mode-map))
(define-key prog-mode-map [convert] 'anzu-query-replace-at-cursor-thing)
(put 'anzu-mode-lighter 'risky-local-variable t)
(setq anzu-mode-lighter `(" " ,(propertize "杏" 'face 'anzu-mode-line)))

customize isearch

(define-key isearch-mode-map (kbd "C-S-y")

(lambda ()
"Pull string from kill ring into search string literally."
(setq isearch-yank-flag t)
(let ((string (current-kill 0)))
(if (eq (string-to-char string) ?\") (setq string (read string)))
(mapconcat 'isearch-text-char-description string "")))))

(define-key isearch-mode-map [convert]
(lambda ()
"copy escaped regex string & exit"
(when isearch-regexp
(kill-new (format "%S" (if pcre-mode (pcre-to-elisp isearch-string) isearch-string)))

with that, you can paste existing regex strings into isearch and when you are satisfied with the regex hits after some edits, copy the final isearch regex string into the kill ring:zap: ready for use in font locks and regex searches:ocean:


if you use pcre2el.el, maybe the following is helpful:

(require 'pcre2el)

(add-hook 'prog-mode-hook 'rxt-mode)
(setq reb-re-syntax 'pcre)

if you activate pcre-mode, the above isearch works in pcre regex. but the author of this package warns pcre-mode is experimental so use it at your own risk:skull_crossbones: