Skip to content

Commit d461448

Browse files
committed
interrupt completion for GNU readline...
and also handle $Abort properly Out[] response
1 parent 9bd2fbd commit d461448

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

mathicsscript/format.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ def eval_boxes(result, fn: Callable, obj, **options):
164164
return expr_type
165165

166166
if expr is SymbolAborted:
167+
obj.out = ["$Aborted"]
168+
obj.last_eval = SymbolAborted
167169
return "$Aborted"
168170
if format == "text":
169171
if expr_head is SymbolStringForm:

mathicsscript/interrupt.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
from types import FrameType
1111
from typing import Callable, Optional
1212

13-
from numpy import true_divide
14-
1513
from mathics import settings
1614
from mathics.core.evaluation import Evaluation
1715
from mathics.core.interrupt import AbortInterrupt, ReturnInterrupt, TimeoutInterrupt
@@ -53,6 +51,7 @@ def inspect_eval_loop(evaluation: Evaluation):
5351
if result is not None and shell is not None:
5452
shell.print_result(result, prompt=False, strict_wl_output=True)
5553
except TimeoutInterrupt:
54+
print("\nTimeout occurred - ignored.")
5655
pass
5756
except ReturnInterrupt:
5857
evaluation.last_eval = None
@@ -79,6 +78,7 @@ def Mathics3_interrupt_handler(
7978

8079
shell = evaluation.shell
8180
incolors = shell.incolors
81+
is_gnu_readline = False
8282
if hasattr(shell, "bottom_toolbar"):
8383
from mathicsscript.completion import InterruptCompleter
8484

@@ -87,8 +87,13 @@ def Mathics3_interrupt_handler(
8787
completer = InterruptCompleter()
8888
else:
8989
is_prompt_toolkit = False
90+
is_gnu_readline = shell.using_readline
9091
use_HTML = False
92+
from readline import set_completer
93+
94+
set_completer(lambda text, state: shell.complete_interrupt_command(text, state))
9195
completer = None
96+
9297
while True:
9398
try:
9499
prompt = (
@@ -162,6 +167,12 @@ def Mathics3_interrupt_handler(
162167
except RuntimeError:
163168
break
164169
finally:
170+
if is_gnu_readline:
171+
from readline import set_completer
172+
173+
set_completer(
174+
lambda text, state: shell.complete_symbol_name(text, state)
175+
)
165176
pass
166177

167178

mathicsscript/termshell_gnu.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,18 @@ def __init__(
116116
set_history_length(self.history_length)
117117
atexit.register(self.user_write_history_file)
118118

119+
def complete_interrupt_command(self, text, state):
120+
# Only complete from this fixed set
121+
completions = [
122+
w
123+
for w in ["abort", "continue", "exit", "inspect", "show"]
124+
if w.startswith(text)
125+
]
126+
try:
127+
return completions[state]
128+
except IndexError:
129+
return None
130+
119131
def complete_symbol_name(self, text, state):
120132
try:
121133
match = re.match(r"^(.*\\\[)([A-Z][a-z]*)$", text)

0 commit comments

Comments
 (0)