Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions TeXmacs/packages/themes/dark/dark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,40 @@

<assign|focus-color|#3A86FF33>

<assign|syntax-python-none|#ff6b6b>

<assign|syntax-python-comment|#d4a373>

<assign|syntax-python-error|#ff4d4d>

<assign|syntax-python-constant|#6a8cff>

<assign|syntax-python-constant-number|#6a8cff>

<assign|syntax-python-constant-string|#a9a9a9>

<assign|syntax-python-constant-char|#b0b0b0>

<assign|syntax-python-declare-function|#4d7eff>

<assign|syntax-python-declare-type|#4d7eff>

<assign|syntax-python-declare-module|#4d7eff>

<assign|syntax-python-operator|#e066ff>

<assign|syntax-python-operator-openclose|#ff6666>

<assign|syntax-python-operator-field|#d1d1d1>

<assign|syntax-python-operator-special|#ffb347>

<assign|syntax-python-keyword|#4db8b8>

<assign|syntax-python-keyword-conditional|#4db8b8>

<assign|syntax-python-keyword-control|#4db8b8>

<assign|selection-color|#3A86FF33>

<assign|scheme-prompt-color|dark green>
Expand Down
34 changes: 34 additions & 0 deletions TeXmacs/plugins/python/packages/code/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,40 @@
</with>
</macro>>

<assign|syntax-python-none|red>

<assign|syntax-python-comment|brown>

<assign|syntax-python-error|dark red>

<assign|syntax-python-constant|#4040c0>

<assign|syntax-python-constant-number|#4040c0>

<assign|syntax-python-constant-string|dark grey>

<assign|syntax-python-constant-char|#333333>

<assign|syntax-python-declare-function|#0000c0>

<assign|syntax-python-declare-type|#0000c0>

<assign|syntax-python-declare-module|#0000c0>

<assign|syntax-python-operator|#8b008b>

<assign|syntax-python-operator-openclose|#B02020>

<assign|syntax-python-operator-field|#888888>

<assign|syntax-python-operator-special|orange>

<assign|syntax-python-keyword|#309090>

<assign|syntax-python-keyword-conditional|#309090>

<assign|syntax-python-keyword-control|#309090>

\;
</body>

Expand Down
39 changes: 18 additions & 21 deletions TeXmacs/plugins/python/progs/code/python-lang.scm
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; MODULE : python-lang.scm
Expand Down Expand Up @@ -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))
("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))
67 changes: 67 additions & 0 deletions TeXmacs/tests/tmu/209_17.tmu
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<TMU|<tuple|1.1.0|2026.2.1-rc3>>

<style|<tuple|generic|python>>

<\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()
</python-code>
</body>

<\initial>
<\collection>
<associate|page-screen-margin|false>
</collection>
</initial>
28 changes: 28 additions & 0 deletions devel/209_17.md
Original file line number Diff line number Diff line change
@@ -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.