diff options
author | Laurence Warne <laurencewarne@gmail.com> | 2022-08-09 08:33:18 +0100 |
---|---|---|
committer | Lars Ingebrigtsen <larsi@gnus.org> | 2022-08-09 19:21:36 +0200 |
commit | b92e88875802724af9e08201ea65a96dd5f20ff1 (patch) | |
tree | 9a0136781ddab7618c4752d60c1c84830a07d95a /lisp/progmodes/python.el | |
parent | 3ef18c7a213f4f3c03eec033fcb8219fb17cd53d (diff) | |
download | emacs-b92e88875802724af9e08201ea65a96dd5f20ff1.tar.gz emacs-b92e88875802724af9e08201ea65a96dd5f20ff1.tar.bz2 emacs-b92e88875802724af9e08201ea65a96dd5f20ff1.zip |
Fix python escape code fontification for multi-line literals
* lisp/progmodes/python.el (python--string-bytes-literal-matcher): Go
backward one char after a match so that consecutive escape codes are
highlighted
(python--not-raw-string-literal-start-regexp): Make regular expression
more comprehensive, so multi-line bytes literals are not caught
(python-rx): Accept one to three octal digits in octal escape codes
instead of always three
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r-- | lisp/progmodes/python.el | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 5edd6e7df56..96f9d14832d 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -432,7 +432,7 @@ This variant of `rx' supports common Python named REGEXPS." (seq (not "\\") (group (or "\\\\" "\\'" "\\a" "\\b" "\\f" "\\n" "\\r" "\\t" "\\v" - (seq "\\" (= 3 (in "0-7"))) + (seq "\\" (** 1 3 (in "0-7"))) (seq "\\x" hex hex))))) (string-escape-sequence (or bytes-escape-sequence @@ -556,7 +556,14 @@ the {...} holes that appear within f-strings." "A regular expression matching the start of a not-raw bytes literal.") (defconst python--not-raw-string-literal-start-regexp - (rx (or bos (not alnum)) (? (or "u" "U" "F" "f")) (or "\"" "\"\"\"" "'" "'''") eos) + (rx bos (or + ;; Multi-line string literals + (seq (? (? (not alnum)) (or "u" "U" "F" "f")) (or "\"\"\"" "'''")) + (seq (? anychar) (not alnum) (or "\"\"\"" "'''")) + ;; Single line string literals + (seq (? (** 0 2 anychar) (not alnum)) (or "u" "U" "F" "f") (or "'" "\"")) + (seq (? (** 0 3 anychar) (not (any "'\"" alnum))) (or "'" "\""))) + eos) "A regular expression matching the start of a not-raw string literal.") (defun python--string-bytes-literal-matcher (regexp start-regexp) @@ -565,11 +572,12 @@ the {...} holes that appear within f-strings." (cl-loop for result = (re-search-forward regexp limit t) for result-valid = (and result - (let* ((pos (nth 8 (syntax-ppss))) - (before-quote - (buffer-substring-no-properties - (max (- pos 5) (point-min)) - (min (+ pos 1) (point-max))))) + (when-let* ((pos (nth 8 (syntax-ppss))) + (before-quote + (buffer-substring-no-properties + (max (- pos 4) (point-min)) + (min (+ pos 1) (point-max))))) + (backward-char) (string-match-p start-regexp before-quote))) until (or (not result) result-valid) finally return (and result-valid result)))) |