0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

macros & regex - two headed monsters

Last updated at Posted at 2019-03-31

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:
#macros
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))
    ad-do-it))

#regex
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."
    (interactive)
    (setq isearch-yank-flag t)
    (let ((string (current-kill 0)))
      (if (eq (string-to-char string) ?\") (setq string (read string)))
      (isearch-process-search-string
       string
       (mapconcat 'isearch-text-char-description string "")))))

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

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:
###pcre2el
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:

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?