refactor(python): autoload syntax highlighter fix for 28.1

A summary of the issue:
- emacs-mirror/emacs@c44908c059 broke syntax highlighting for
  python-mode.
- emacs-mirror/emacs@a8acb9516e fixes the issue shortly after, but the
  fix did not make it into the 28.1 release (but made it into 28.2).
- This fix was deployed in #6414 (773122f1ec).
- This commit ensures the fix is only effective for 28.1 users, and
  documents it for posterity.

Ref: #6414
Ref: emacs-mirror/emacs@c44908c059
Ref: emacs-mirror/emacs@a8acb9516e
Amend: 773122f1ec
Co-authored-by: dani84bs <dani84bs@users.noreply.github.com>
This commit is contained in:
Henrik Lissner 2022-09-12 12:40:42 +02:00
parent f49953ab56
commit a18ea683d2
No known key found for this signature in database
GPG key ID: B60957CA074D39A3
2 changed files with 95 additions and 84 deletions

View file

@ -0,0 +1,94 @@
;;; lang/python/autoload/compat-28.1.el -*- lexical-binding: t; -*-
;;;###if (versionp! "28.0" <= emacs-version <= "28.1")
;;; Commentary:
;;
;; HACK: 28.1 introduced breakage into the syntax highlighter for `python-mode'.
;; The fix was introduced shortly after, but did not make the 28.1 release.
;; This fix exists solely to patch that issue for 28.1 users.
;;
;; DEPRECATED: Remove when 28.1 support is dropped
;;
;;; Code:
;;;###autoload
(defadvice! +python--font-lock-assignment-matcher-a (regexp)
:override #'python-font-lock-assignment-matcher
(lambda (limit)
(cl-loop while (re-search-forward regexp limit t)
unless (or (python-syntax-context 'paren)
(equal (char-after) ?=))
return t)))
;;;###autoload
(defadvice! +python--rx-a (&rest regexps)
:override #'python-rx
`(rx-let ((block-start (seq symbol-start
(or "def" "class" "if" "elif" "else" "try"
"except" "finally" "for" "while" "with"
;; Python 3.5+ PEP492
(and "async" (+ space)
(or "def" "for" "with")))
symbol-end))
(dedenter (seq symbol-start
(or "elif" "else" "except" "finally")
symbol-end))
(block-ender (seq symbol-start
(or
"break" "continue" "pass" "raise" "return")
symbol-end))
(decorator (seq line-start (* space) ?@ (any letter ?_)
(* (any word ?_))))
(defun (seq symbol-start
(or "def" "class"
;; Python 3.5+ PEP492
(and "async" (+ space) "def"))
symbol-end))
(if-name-main (seq line-start "if" (+ space) "__name__"
(+ space) "==" (+ space)
(any ?' ?\") "__main__" (any ?' ?\")
(* space) ?:))
(symbol-name (seq (any letter ?_) (* (any word ?_))))
(assignment-target (seq (? ?*)
(* symbol-name ?.) symbol-name
(? ?\[ (+ (not ?\])) ?\])))
(grouped-assignment-target (seq (? ?*)
(* symbol-name ?.) (group symbol-name)
(? ?\[ (+ (not ?\])) ?\])))
(open-paren (or "{" "[" "("))
(close-paren (or "}" "]" ")"))
(simple-operator (any ?+ ?- ?/ ?& ?^ ?~ ?| ?* ?< ?> ?= ?%))
(not-simple-operator (not (or simple-operator ?\n)))
(operator (or "==" ">=" "is" "not"
"**" "//" "<<" ">>" "<=" "!="
"+" "-" "/" "&" "^" "~" "|" "*" "<" ">"
"=" "%"))
(assignment-operator (or "+=" "-=" "*=" "/=" "//=" "%=" "**="
">>=" "<<=" "&=" "^=" "|="
"="))
(string-delimiter (seq
;; Match even number of backslashes.
(or (not (any ?\\ ?\' ?\")) point
;; Quotes might be preceded by an
;; escaped quote.
(and (or (not (any ?\\)) point) ?\\
(* ?\\ ?\\) (any ?\' ?\")))
(* ?\\ ?\\)
;; Match single or triple quotes of any kind.
(group (or "\"\"\"" "\"" "'''" "'"))))
(coding-cookie (seq line-start ?# (* space)
(or
;; # coding=<encoding name>
(: "coding" (or ?: ?=) (* space)
(group-n 1 (+ (or word ?-))))
;; # -*- coding: <encoding name> -*-
(: "-*-" (* space) "coding:" (* space)
(group-n 1 (+ (or word ?-)))
(* space) "-*-")
;; # vim: set fileencoding=<encoding name> :
(: "vim:" (* space) "set" (+ space)
"fileencoding" (* space) ?= (* space)
(group-n 1 (+ (or word ?-)))
(* space) ":")))))
(rx ,@regexps)))
;;; compat-28.1.el ends here

View file

@ -86,90 +86,7 @@
(advice-add #'pythonic-activate :after-while #'+modeline-update-env-in-all-windows-h) (advice-add #'pythonic-activate :after-while #'+modeline-update-env-in-all-windows-h)
(advice-add #'pythonic-deactivate :after #'+modeline-clear-env-in-all-windows-h)) (advice-add #'pythonic-deactivate :after #'+modeline-clear-env-in-all-windows-h))
(setq-hook! 'python-mode-hook tab-width python-indent-offset) (setq-hook! 'python-mode-hook tab-width python-indent-offset))
;; HACK Fix syntax highlighting on Emacs 28.1
;; DEPRECATED Remove when 28.1 support is dropped
;; REVIEW Revisit if a 28.2 is released with a fix
(when (= emacs-major-version 28)
(defadvice! +python--font-lock-assignment-matcher-a (regexp)
:override #'python-font-lock-assignment-matcher
(lambda (limit)
(cl-loop while (re-search-forward regexp limit t)
unless (or (python-syntax-context 'paren)
(equal (char-after) ?=))
return t)))
(defadvice! +python--rx-a (&rest regexps)
:override #'python-rx
`(rx-let ((block-start (seq symbol-start
(or "def" "class" "if" "elif" "else" "try"
"except" "finally" "for" "while" "with"
;; Python 3.5+ PEP492
(and "async" (+ space)
(or "def" "for" "with")))
symbol-end))
(dedenter (seq symbol-start
(or "elif" "else" "except" "finally")
symbol-end))
(block-ender (seq symbol-start
(or
"break" "continue" "pass" "raise" "return")
symbol-end))
(decorator (seq line-start (* space) ?@ (any letter ?_)
(* (any word ?_))))
(defun (seq symbol-start
(or "def" "class"
;; Python 3.5+ PEP492
(and "async" (+ space) "def"))
symbol-end))
(if-name-main (seq line-start "if" (+ space) "__name__"
(+ space) "==" (+ space)
(any ?' ?\") "__main__" (any ?' ?\")
(* space) ?:))
(symbol-name (seq (any letter ?_) (* (any word ?_))))
(assignment-target (seq (? ?*)
(* symbol-name ?.) symbol-name
(? ?\[ (+ (not ?\])) ?\])))
(grouped-assignment-target (seq (? ?*)
(* symbol-name ?.) (group symbol-name)
(? ?\[ (+ (not ?\])) ?\])))
(open-paren (or "{" "[" "("))
(close-paren (or "}" "]" ")"))
(simple-operator (any ?+ ?- ?/ ?& ?^ ?~ ?| ?* ?< ?> ?= ?%))
(not-simple-operator (not (or simple-operator ?\n)))
(operator (or "==" ">=" "is" "not"
"**" "//" "<<" ">>" "<=" "!="
"+" "-" "/" "&" "^" "~" "|" "*" "<" ">"
"=" "%"))
(assignment-operator (or "+=" "-=" "*=" "/=" "//=" "%=" "**="
">>=" "<<=" "&=" "^=" "|="
"="))
(string-delimiter (seq
;; Match even number of backslashes.
(or (not (any ?\\ ?\' ?\")) point
;; Quotes might be preceded by an
;; escaped quote.
(and (or (not (any ?\\)) point) ?\\
(* ?\\ ?\\) (any ?\' ?\")))
(* ?\\ ?\\)
;; Match single or triple quotes of any kind.
(group (or "\"\"\"" "\"" "'''" "'"))))
(coding-cookie (seq line-start ?# (* space)
(or
;; # coding=<encoding name>
(: "coding" (or ?: ?=) (* space)
(group-n 1 (+ (or word ?-))))
;; # -*- coding: <encoding name> -*-
(: "-*-" (* space) "coding:" (* space)
(group-n 1 (+ (or word ?-)))
(* space) "-*-")
;; # vim: set fileencoding=<encoding name> :
(: "vim:" (* space) "set" (+ space)
"fileencoding" (* space) ?= (* space)
(group-n 1 (+ (or word ?-)))
(* space) ":")))))
(rx ,@regexps)))))
(use-package! anaconda-mode (use-package! anaconda-mode