* lisp/emacs-lisp/re-builder.el: Cosmetic changes

(reb-valid-string): Declare it risky so the mode-line will obey its
text-properties.
(reb-lisp-syntax-p): No need to make it `defsubst`.
(reb-target-value): New function to replace the
`reb-target-binding` macro.  Adjust all callers.
(reb-auto-update): Give a more informative error message than just
"invalid" and give it a warning face.
(reb-update-regexp): Always update `reb-regexp-src` (it's harmless),
rather than only when it's necessary (reduces the need for advice in pcre.el).
This commit is contained in:
Stefan Monnier 2022-10-28 09:36:40 -04:00
parent 1073e16960
commit c0f7a276cf

View file

@ -211,6 +211,7 @@ Except for Lisp syntax this is the same as `reb-regexp'.")
(defvar reb-valid-string "" (defvar reb-valid-string ""
"String in mode line showing validity of RE.") "String in mode line showing validity of RE.")
(put 'reb-valid-string 'risky-local-variable t)
(defconst reb-buffer "*RE-Builder*" (defconst reb-buffer "*RE-Builder*"
"Buffer to use for the RE Builder.") "Buffer to use for the RE Builder.")
@ -308,13 +309,13 @@ Except for Lisp syntax this is the same as `reb-regexp'.")
"Return t if display is capable of displaying colors." "Return t if display is capable of displaying colors."
(eq 'color (frame-parameter nil 'display-type))) (eq 'color (frame-parameter nil 'display-type)))
(defsubst reb-lisp-syntax-p () (defun reb-lisp-syntax-p ()
"Return non-nil if RE Builder uses `rx' syntax." "Return non-nil if RE Builder uses `rx' syntax."
(eq reb-re-syntax 'rx)) (eq reb-re-syntax 'rx))
(defmacro reb-target-binding (symbol) (defun reb-target-value (symbol)
"Return binding for SYMBOL in the RE Builder target buffer." "Return binding for SYMBOL in the RE Builder target buffer."
`(with-current-buffer reb-target-buffer ,symbol)) (buffer-local-value symbol reb-target-buffer))
(defun reb-initialize-buffer () (defun reb-initialize-buffer ()
"Initialize the current buffer as a RE Builder buffer." "Initialize the current buffer as a RE Builder buffer."
@ -440,7 +441,7 @@ provided in the Commentary section of this library."
(interactive) (interactive)
(reb-update-regexp) (reb-update-regexp)
(let ((re (with-output-to-string (let ((re (with-output-to-string
(print (reb-target-binding reb-regexp))))) (print (reb-target-value 'reb-regexp)))))
(setq re (substring re 1 (1- (length re)))) (setq re (substring re 1 (1- (length re))))
(setq re (string-replace "\n" "\\n" re)) (setq re (string-replace "\n" "\\n" re))
(kill-new re) (kill-new re)
@ -518,12 +519,17 @@ An actual update is only done if the regexp has changed or if the
optional fourth argument FORCE is non-nil." optional fourth argument FORCE is non-nil."
(let ((prev-valid reb-valid-string) (let ((prev-valid reb-valid-string)
(new-valid (new-valid
(condition-case nil (condition-case err
(progn (progn
(when (or (reb-update-regexp) force) (when (or (reb-update-regexp) force)
(reb-do-update)) (reb-do-update))
"") "")
(error " *invalid*")))) (error (propertize
(format " %s"
(if (and (consp (cdr err)) (stringp (cadr err)))
(format "%s: %s" (car err) (cadr err))
(car err)))
'face 'font-lock-warning-face)))))
(setq reb-valid-string new-valid) (setq reb-valid-string new-valid)
(force-mode-line-update) (force-mode-line-update)
@ -554,7 +560,7 @@ optional fourth argument FORCE is non-nil."
(if reb-subexp-mode (if reb-subexp-mode
(format " (subexp %s)" (or reb-subexp-displayed "-")) (format " (subexp %s)" (or reb-subexp-displayed "-"))
"") "")
(if (not (reb-target-binding case-fold-search)) (if (not (reb-target-value 'case-fold-search))
" Case" " Case"
""))) "")))
(force-mode-line-update)) (force-mode-line-update))
@ -600,7 +606,7 @@ optional fourth argument FORCE is non-nil."
(defun reb-insert-regexp () (defun reb-insert-regexp ()
"Insert current RE." "Insert current RE."
(let ((re (or (reb-target-binding reb-regexp) (let ((re (or (reb-target-value 'reb-regexp)
(reb-empty-regexp)))) (reb-empty-regexp))))
(cond ((eq reb-re-syntax 'read) (cond ((eq reb-re-syntax 'read)
(print re (current-buffer))) (print re (current-buffer)))
@ -608,7 +614,7 @@ optional fourth argument FORCE is non-nil."
(insert "\n\"" re "\"")) (insert "\n\"" re "\""))
;; For the Lisp syntax we need the "source" of the regexp ;; For the Lisp syntax we need the "source" of the regexp
((reb-lisp-syntax-p) ((reb-lisp-syntax-p)
(insert (or (reb-target-binding reb-regexp-src) (insert (or (reb-target-value 'reb-regexp-src)
(reb-empty-regexp))))))) (reb-empty-regexp)))))))
(defun reb-cook-regexp (re) (defun reb-cook-regexp (re)
@ -627,9 +633,8 @@ Return t if the (cooked) expression changed."
(prog1 (prog1
(not (string= oldre re)) (not (string= oldre re))
(setq reb-regexp re) (setq reb-regexp re)
;; Only update the source re for the lisp formats ;; Update the source re for the Lisp formats.
(when (reb-lisp-syntax-p) (setq reb-regexp-src re-src))))))
(setq reb-regexp-src re-src)))))))
;; And now the real core of the whole thing ;; And now the real core of the whole thing
@ -644,7 +649,7 @@ Return t if the (cooked) expression changed."
(defun reb-update-overlays (&optional subexp) (defun reb-update-overlays (&optional subexp)
"Switch to `reb-target-buffer' and mark all matches of `reb-regexp'. "Switch to `reb-target-buffer' and mark all matches of `reb-regexp'.
If SUBEXP is non-nil mark only the corresponding sub-expressions." If SUBEXP is non-nil mark only the corresponding sub-expressions."
(let* ((re (reb-target-binding reb-regexp)) (let* ((re (reb-target-value 'reb-regexp))
(subexps (reb-count-subexps re)) (subexps (reb-count-subexps re))
(matches 0) (matches 0)
(submatches 0) (submatches 0)