Skip to content

Commit 77c5943

Browse files
committed
function key enhancements
f2 - go to next pygments style f4 - toggle autobrace now works These also update the corresponding Settings`$ variable.
1 parent efe2664 commit 77c5943

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed

mathicsscript/bindkeys.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import pathlib
2828
import re
2929

30+
from mathicsscript.termshell import ALL_PYGMENTS_STYLES
3031
from mathicsscript.settings import definitions
3132
from mathics.session import get_settings_value
3233

@@ -136,12 +137,24 @@ def _editor_toggle(event):
136137
def _group_autocomplete_toggle(event):
137138
"""Complete braces."""
138139
app = event.app
139-
140-
if not hasattr(app, "group_autocomplete"):
141-
app.group_autocomplete = False
142140
app.group_autocomplete = not app.group_autocomplete
143141

144142

143+
# Add an additional key binding for toggling this flag.
144+
@bindings.add("f2")
145+
def _next_pygements_style(event):
146+
"""Set Pygments style to the next sytle in ALL_PYGMENTS_STYLE."""
147+
app = event.app
148+
149+
try:
150+
i = ALL_PYGMENTS_STYLES.index(app.pygments_style)
151+
except ValueError:
152+
pass
153+
else:
154+
i = (i + 1) % len(ALL_PYGMENTS_STYLES)
155+
app.pygments_style = ALL_PYGMENTS_STYLES[i]
156+
157+
145158
def read_inputrc(read_init_file_fn: Callable, use_unicode: bool) -> None:
146159
"""
147160
Read GNU Readline style inputrc

mathicsscript/termshell_gnu.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from readline import read_init_file
1212
except ImportError:
1313
# Not sure what to do here: nothing is probably safe.
14-
def read_init_file(path: str):
14+
def read_init_file(_: str):
1515
return
1616

1717

@@ -25,10 +25,6 @@ def read_init_file(path: str):
2525
)
2626
from mathics.core.symbols import strip_context
2727

28-
from pygments.styles import get_all_styles
29-
30-
ALL_PYGMENTS_STYLES = list(get_all_styles())
31-
3228
try:
3329
from readline import (
3430
parse_and_bind,

mathicsscript/termshell_prompt.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from mathics.core.attributes import attribute_string_to_number
1414
from mathics.core.expression import Expression, from_python
1515
from mathics.core.rules import Rule
16-
from mathics.core.symbols import SymbolNull
16+
from mathics.core.symbols import SymbolNull, SymbolFalse, SymbolTrue
1717
from mathics.core.systemsymbols import SymbolMessageName
1818
from mathics_pygments.lexer import MathematicaLexer, MToken
1919
from mathics_scanner.location import ContainerKind
@@ -35,6 +35,7 @@
3535
from mathicsscript.bindkeys import bindings, read_init_file, read_inputrc
3636
from mathicsscript.completion import MathicsCompleter
3737
from mathicsscript.termshell import (
38+
ALL_PYGMENTS_STYLES,
3839
CONFIGDIR,
3940
HISTSIZE,
4041
USER_INPUTRC,
@@ -47,8 +48,6 @@
4748

4849
mma_lexer = MathematicaLexer()
4950

50-
ALL_PYGMENTS_STYLES = list(get_all_styles())
51-
5251
color_scheme = TERMINAL_COLORS.copy()
5352
color_scheme[MToken.SYMBOL] = ("yellow", "ansibrightyellow")
5453
color_scheme[MToken.BUILTIN] = ("ansigreen", "ansibrightgreen")
@@ -163,16 +162,43 @@ def bottom_toolbar(self):
163162
app = get_app()
164163
edit_mode = "Vi" if app.editing_mode == EditingMode.VI else "Emacs"
165164

166-
app.group_autocomplete = True
167-
168-
if self.definitions.get_ownvalue("Settings`$GroupAutocomplete"):
165+
# The first time around, app.group_autocomplete has not been set,
166+
# so use the value from Settings`GroupAutocomplete.
167+
# However, after that we may have changed this value internally using
168+
# function key f3, so update Settings`GroupAutocomplete from that.
169+
if hasattr(app, "group_autocomplete"):
170+
self.definitions.set_ownvalue(
171+
"Settings`$GroupAutocomplete",
172+
SymbolTrue if app.group_autocomplete else SymbolFalse,
173+
)
174+
elif self.definitions.get_ownvalue("Settings`$GroupAutocomplete"):
169175
app.group_autocomplete = self.definitions.get_ownvalue(
170176
"Settings`$GroupAutocomplete"
171177
).to_python()
178+
else:
179+
# First time around and there is no value set via
180+
# Settings`GroupAutocomplete.
181+
app.group_autocomplete = True
182+
self.definitions.set_ownvalue("Settings`$GroupAutocomplete", SymbolTrue)
183+
184+
if hasattr(app, "pygments_style"):
185+
self.definitions.set_ownvalue(
186+
"Settings`$PygmentsStyle", String(app.pygments_style)
187+
)
188+
elif self.definitions.get_ownvalue("Settings`$PygmentsStyle") is not SymbolNull:
189+
app.pygments_style = self.definitions.get_ownvalue(
190+
"Settings`$PygmentsStyle"
191+
)
192+
else:
193+
# First time around and there is no value set via
194+
app.pygments_style = self.pygments_style
195+
self.definitions.set_ownvalue(
196+
"Settings`$PygmentsStyle", String(app.pygments_style)
197+
)
172198

173199
edit_mode = "Vi" if app.editing_mode == EditingMode.VI else "Emacs"
174200
return HTML(
175-
f" mathicsscript: {__version__}, Style: {self.pygments_style}, Mode: {edit_mode}, Autobrace: {app.group_autocomplete}"
201+
f" mathicsscript: {__version__}, Style: {app.pygments_style}, Mode: {edit_mode}, Autobrace: {app.group_autocomplete}"
176202
)
177203

178204
def get_in_prompt(self):

0 commit comments

Comments
 (0)