diff --git a/TeXmacs/packages/themes/dark/dark.ts b/TeXmacs/packages/themes/dark/dark.ts index 78d689d709..6be800f793 100644 --- a/TeXmacs/packages/themes/dark/dark.ts +++ b/TeXmacs/packages/themes/dark/dark.ts @@ -76,6 +76,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/TeXmacs/plugins/python/packages/code/python.ts b/TeXmacs/plugins/python/packages/code/python.ts index 62fcb47572..0bda28b99f 100644 --- a/TeXmacs/plugins/python/packages/code/python.ts +++ b/TeXmacs/plugins/python/packages/code/python.ts @@ -58,6 +58,40 @@ > + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \; diff --git a/TeXmacs/plugins/python/progs/code/python-lang.scm b/TeXmacs/plugins/python/progs/code/python-lang.scm index 8d1def5a84..6fb6da523b 100644 --- a/TeXmacs/plugins/python/progs/code/python-lang.scm +++ b/TeXmacs/plugins/python/progs/code/python-lang.scm @@ -1,4 +1,3 @@ - ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; MODULE : python-lang.scm @@ -108,26 +107,24 @@ ;; 语法高亮偏好设置 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; 通知Python语法更改的函数 -(define (notify-python-syntax var val) +(define (notify-python-pref var val) (syntax-read-preferences "python")) -;; 定义语法高亮颜色偏好设置 (define-preferences - ("syntax:python:none" "#ff6b6b" notify-python-syntax) ;; 浅红色 (light red) - ("syntax:python:comment" "#d4a373" notify-python-syntax) ;; 浅棕色 (light brown) - ("syntax:python:error" "#ff4d4d" notify-python-syntax) ;; 亮红色 (bright red) - ("syntax:python:constant" "#6a8cff" notify-python-syntax) ;; 浅蓝色 (light blue) - ("syntax:python:constant_number" "#6a8cff" notify-python-syntax) ;; 浅蓝色 (light blue) - ("syntax:python:constant_string" "#a9a9a9" notify-python-syntax) ;; 浅灰色 (light grey) - ("syntax:python:constant_char" "#b0b0b0" notify-python-syntax) ;; 浅灰色 (light grey) - ("syntax:python:declare_function" "#4d7eff" notify-python-syntax) ;; 浅蓝色 (light blue) - ("syntax:python:declare_type" "#4d7eff" notify-python-syntax) ;; 浅蓝色 (light blue) - ("syntax:python:declare_module" "#4d7eff" notify-python-syntax) ;; 浅蓝色 (light blue) - ("syntax:python:operator" "#e066ff" notify-python-syntax) ;; 浅洋红色 (light magenta) - ("syntax:python:operator_openclose" "#ff6666" notify-python-syntax) ;; 浅红色 (light red) - ("syntax:python:operator_field" "#d1d1d1" notify-python-syntax) ;; 浅灰色 (light grey) - ("syntax:python:operator_special" "#ffb347" notify-python-syntax) ;; 浅橙色 (light orange) - ("syntax:python:keyword" "#4db8b8" notify-python-syntax) ;; 浅青色 (light teal/cyan) - ("syntax:python:keyword_conditional" "#4db8b8" notify-python-syntax) ;; 浅青色 (light teal/cyan) - ("syntax:python:keyword_control" "#4db8b8" notify-python-syntax)) \ No newline at end of file + ("syntax:python:none" "syntax-python-none" notify-python-pref) + ("syntax:python:comment" "syntax-python-comment" notify-python-pref) + ("syntax:python:error" "syntax-python-error" notify-python-pref) + ("syntax:python:constant" "syntax-python-constant" notify-python-pref) + ("syntax:python:constant_number" "syntax-python-constant-number" notify-python-pref) + ("syntax:python:constant_string" "syntax-python-constant-string" notify-python-pref) + ("syntax:python:constant_char" "syntax-python-constant-char" notify-python-pref) + ("syntax:python:declare_function" "syntax-python-declare-function" notify-python-pref) + ("syntax:python:declare_type" "syntax-python-declare-type" notify-python-pref) + ("syntax:python:declare_module" "syntax-python-declare-module" notify-python-pref) + ("syntax:python:operator" "syntax-python-operator" notify-python-pref) + ("syntax:python:operator_openclose" "syntax-python-operator-openclose" notify-python-pref) + ("syntax:python:operator_field" "syntax-python-operator-field" notify-python-pref) + ("syntax:python:operator_special" "syntax-python-operator-special" notify-python-pref) + ("syntax:python:keyword" "syntax-python-keyword" notify-python-pref) + ("syntax:python:keyword_conditional" "syntax-python-keyword-conditional" notify-python-pref) + ("syntax:python:keyword_control" "syntax-python-keyword-control" notify-python-pref)) diff --git a/TeXmacs/tests/tmu/209_17.tmu b/TeXmacs/tests/tmu/209_17.tmu new file mode 100644 index 0000000000..008f7ebedc --- /dev/null +++ b/TeXmacs/tests/tmu/209_17.tmu @@ -0,0 +1,67 @@ +> + +> + +<\body> + <\python-code> + # Test for Python syntax highlighting (Issue 209_17) + + import math + + from datetime import datetime + + class TestClass: + + \ \ \ \ """A simple class for testing.""" + + \ \ \ \ def __init__(self, value): + + \ \ \ \ \ \ \ \ self.value = value + + \ \ \ \ \ \ \ \ self.is_active = True + + \ \ \ \ @staticmethod + + \ \ \ \ def calculate(x, y): + + \ \ \ \ \ \ \ \ if x is None or y is None: + + \ \ \ \ \ \ \ \ \ \ \ \ raise ValueError("Arguments cannot be None") + + \ \ \ \ \ \ \ \ result = (x ** 2) + (y / 2.0) - 10 + + \ \ \ \ \ \ \ \ return result + + def main(): + + \ \ \ \ string_val = "Hello World" + + \ \ \ \ char_val = 'c' + + \ \ \ \ num_val = 42 + + \ \ \ \ hex_val = 0xFF + + \ \ \ \ complex_val = 1 + 2j + + \ \ \ \ try: + + \ \ \ \ \ \ \ \ obj = TestClass(num_val) + + \ \ \ \ \ \ \ \ print(f"Result: {obj.calculate(10, 5)}") + + \ \ \ \ except Exception as e: + + \ \ \ \ \ \ \ \ print(f"Error: {e}") + + if __name__ == "__main__": + + \ \ \ \ main() + + + +<\initial> + <\collection> + + + diff --git a/devel/209_17.md b/devel/209_17.md new file mode 100644 index 0000000000..6ff832a001 --- /dev/null +++ b/devel/209_17.md @@ -0,0 +1,28 @@ +# [209_17] Implement theme-aware syntax highlighting for Python via style variables + +### What +1. Refactored Python syntax highlighting to use a declarative variable-based system instead of hardcoded colors or manual theme-switching logic. +2. Defined a set of symbolic syntax variables in the base Python package (`python.ts`) to provide default colors for light backgrounds. +3. Added theme-specific color overrides in the dark theme package (`dark.ts`) to ensure optimal legibility on dark backgrounds. +4. Updated `python-lang.scm` to map syntax categories to these new symbolic variables. + +### Why +The previous implementation used a single set of colors for both light and dark modes, leading to readability issues. Although better colors were previously chosen, a more robust solution was needed that allows colors to change dynamically based on the active theme. This new approach aligns with Mogan's native styling system, allowing for instant theme updates, side-by-side theme support, and better performance. + +### How +In `TeXmacs/plugins/python/progs/code/python-lang.scm`: +- Replaced hardcoded color values in `define-preferences` with symbolic variable names (e.g., `syntax-python-keyword`). +- Added a `notify-python-pref` callback to handle real-time preference updates. +- Simplified the module by removing imperative theme-switching Scheme logic. + +In `TeXmacs/plugins/python/packages/code/python.ts`: +- Defined the default (light) values for all `syntax-python-*` variables. + +In `TeXmacs/packages/themes/dark/dark.ts`: +- Defined the dark-mode overrides for the same `syntax-python-*` variables. + +## How to test +1. Open Mogan and write some Python code in a code block or session. +2. Toggle between `Document -> Theme -> Dark` and `Document -> Theme -> Plain`. +3. Verify that the syntax highlighting colors update instantly and remain legible in both themes. +4. Verify that no errors appear in the terminal during theme switching.