From 775bd1d0918982d744f07d4163a7db48a855e4a3 Mon Sep 17 00:00:00 2001 From: rocky Date: Mon, 21 Jul 2025 18:37:47 -0400 Subject: [PATCH] function key enhancements f2 - go to next pygments style f4 - toggle autobrace now works These also update the corresponding Settings`$ variable. --- mathicsscript/bindkeys.py | 19 ++++++++++++--- mathicsscript/termshell_gnu.py | 6 +---- mathicsscript/termshell_prompt.py | 40 +++++++++++++++++++++++++------ 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/mathicsscript/bindkeys.py b/mathicsscript/bindkeys.py index 2fcad78..1bc8979 100644 --- a/mathicsscript/bindkeys.py +++ b/mathicsscript/bindkeys.py @@ -27,6 +27,7 @@ import pathlib import re +from mathicsscript.termshell import ALL_PYGMENTS_STYLES from mathicsscript.settings import definitions from mathics.session import get_settings_value @@ -136,12 +137,24 @@ def _editor_toggle(event): def _group_autocomplete_toggle(event): """Complete braces.""" app = event.app - - if not hasattr(app, "group_autocomplete"): - app.group_autocomplete = False app.group_autocomplete = not app.group_autocomplete +# Add an additional key binding for toggling this flag. +@bindings.add("f2") +def _next_pygements_style(event): + """Set Pygments style to the next sytle in .""" + app = event.app + + try: + i = ALL_PYGMENTS_STYLES.index(app.pygments_style) + except ValueError: + pass + else: + i = (i + 1) % len(ALL_PYGMENTS_STYLES) + app.pygments_style = ALL_PYGMENTS_STYLES[i] + + def read_inputrc(read_init_file_fn: Callable, use_unicode: bool) -> None: """ Read GNU Readline style inputrc diff --git a/mathicsscript/termshell_gnu.py b/mathicsscript/termshell_gnu.py index 15862f3..b3f0624 100644 --- a/mathicsscript/termshell_gnu.py +++ b/mathicsscript/termshell_gnu.py @@ -11,7 +11,7 @@ from readline import read_init_file except ImportError: # Not sure what to do here: nothing is probably safe. - def read_init_file(path: str): + def read_init_file(_: str): return @@ -25,10 +25,6 @@ def read_init_file(path: str): ) from mathics.core.symbols import strip_context -from pygments.styles import get_all_styles - -ALL_PYGMENTS_STYLES = list(get_all_styles()) - try: from readline import ( parse_and_bind, diff --git a/mathicsscript/termshell_prompt.py b/mathicsscript/termshell_prompt.py index 55adbee..cc451c3 100644 --- a/mathicsscript/termshell_prompt.py +++ b/mathicsscript/termshell_prompt.py @@ -13,7 +13,7 @@ from mathics.core.attributes import attribute_string_to_number from mathics.core.expression import Expression, from_python from mathics.core.rules import Rule -from mathics.core.symbols import SymbolNull +from mathics.core.symbols import SymbolNull, SymbolFalse, SymbolTrue from mathics.core.systemsymbols import SymbolMessageName from mathics_pygments.lexer import MathematicaLexer, MToken from mathics_scanner.location import ContainerKind @@ -35,6 +35,7 @@ from mathicsscript.bindkeys import bindings, read_init_file, read_inputrc from mathicsscript.completion import MathicsCompleter from mathicsscript.termshell import ( + ALL_PYGMENTS_STYLES, CONFIGDIR, HISTSIZE, USER_INPUTRC, @@ -47,8 +48,6 @@ mma_lexer = MathematicaLexer() -ALL_PYGMENTS_STYLES = list(get_all_styles()) - color_scheme = TERMINAL_COLORS.copy() color_scheme[MToken.SYMBOL] = ("yellow", "ansibrightyellow") color_scheme[MToken.BUILTIN] = ("ansigreen", "ansibrightgreen") @@ -163,16 +162,43 @@ def bottom_toolbar(self): app = get_app() edit_mode = "Vi" if app.editing_mode == EditingMode.VI else "Emacs" - app.group_autocomplete = True - - if self.definitions.get_ownvalue("Settings`$GroupAutocomplete"): + # The first time around, app.group_autocomplete has not been set, + # so use the value from Settings`GroupAutocomplete. + # However, after that we may have changed this value internally using + # function key f3, so update Settings`GroupAutocomplete from that. + if hasattr(app, "group_autocomplete"): + self.definitions.set_ownvalue( + "Settings`$GroupAutocomplete", + SymbolTrue if app.group_autocomplete else SymbolFalse, + ) + elif self.definitions.get_ownvalue("Settings`$GroupAutocomplete"): app.group_autocomplete = self.definitions.get_ownvalue( "Settings`$GroupAutocomplete" ).to_python() + else: + # First time around and there is no value set via + # Settings`GroupAutocomplete. + app.group_autocomplete = True + self.definitions.set_ownvalue("Settings`$GroupAutocomplete", SymbolTrue) + + if hasattr(app, "pygments_style"): + self.definitions.set_ownvalue( + "Settings`$PygmentsStyle", String(app.pygments_style) + ) + elif self.definitions.get_ownvalue("Settings`$PygmentsStyle") is not SymbolNull: + app.pygments_style = self.definitions.get_ownvalue( + "Settings`$PygmentsStyle" + ) + else: + # First time around and there is no value set via + app.pygments_style = self.pygments_style + self.definitions.set_ownvalue( + "Settings`$PygmentsStyle", String(app.pygments_style) + ) edit_mode = "Vi" if app.editing_mode == EditingMode.VI else "Emacs" return HTML( - f" mathicsscript: {__version__}, Style: {self.pygments_style}, Mode: {edit_mode}, Autobrace: {app.group_autocomplete}" + f" mathicsscript: {__version__}, Style: {app.pygments_style}, Mode: {edit_mode}, Autobrace: {app.group_autocomplete}" ) def get_in_prompt(self):