Skip to content
Merged
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
31 changes: 17 additions & 14 deletions mathicsscript/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
from pygments import highlight

from mathicsscript.asymptote import asymptote_version
from mathicsscript.interrupt import setup_signal_handler
from mathicsscript.settings import definitions
from mathicsscript.termshell import ShellEscapeException, mma_lexer
from mathicsscript.termshell_gnu import TerminalShellGNUReadline
from mathicsscript.termshell_prompt import (
TerminalShellCommon,
TerminalShellPromptToolKit,
)
from mathicsscript.termshell import TerminalShellCommon
from mathicsscript.termshell_prompt import TerminalShellPromptToolKit
from mathicsscript.version import __version__

try:
Expand Down Expand Up @@ -123,7 +122,7 @@ def load_settings(shell):
continue
evaluation.evaluate(query)
except KeyboardInterrupt:
print("\nKeyboardInterrupt")
shell.errmsg("\nKeyboardInterrupt")
return True


Expand All @@ -145,16 +144,17 @@ def interactive_eval_loop(
shell: TerminalShellCommon,
unicode,
prompt,
matplotlib: bool,
asymptote: bool,
strict_wl_output: bool,
):
setup_signal_handler()

def identity(x: Any) -> Any:
return x

def fmt_fun(query: Any) -> Any:
return highlight(str(query), mma_lexer, shell.terminal_formatter)

shell.fmt_fn = fmt_fun
while True:
try:
if have_readline and shell.using_readline:
Expand All @@ -173,6 +173,11 @@ def fmt_fun(query: Any) -> Any:
fmt = fmt_fun

evaluation = Evaluation(shell.definitions, output=TerminalOutput(shell))

# Store shell into the evaluation so that an interrupt handler
# has access to this
evaluation.shell = shell

query, source_code = evaluation.parse_feeder_returning_code(shell)
if mathics_core.PRE_EVALUATION_HOOK is not None:
mathics_core.PRE_EVALUATION_HOOK(query, evaluation)
Expand Down Expand Up @@ -214,7 +219,7 @@ def fmt_fun(query: Any) -> Any:
try:
print(open(source_code[2:], "r").read())
except Exception:
print(str(sys.exc_info()[1]))
shell.errmsg(str(sys.exc_info()[1]))
else:
subprocess.run(source_code[1:], shell=True)

Expand All @@ -224,13 +229,13 @@ def fmt_fun(query: Any) -> Any:
# shell.definitions.increment_line(1)

except KeyboardInterrupt:
print("\nKeyboardInterrupt")
shell.errmsg("\nKeyboardInterrupt")
except EOFError:
if prompt:
print("\n\nGoodbye!\n")
shell.errmsg("\n\nGoodbye!\n")
break
except SystemExit:
print("\n\nGoodbye!\n")
shell.errmsg("\n\nGoodbye!\n")
# raise to pass the error code on, e.g. Quit[1]
raise
finally:
Expand Down Expand Up @@ -526,9 +531,7 @@ def main(
)

definitions.set_line_no(0)
interactive_eval_loop(
shell, charset, prompt, asymptote, matplotlib, strict_wl_output
)
interactive_eval_loop(shell, charset, prompt, strict_wl_output)
return exit_rc


Expand Down
29 changes: 28 additions & 1 deletion mathicsscript/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@
from mathics.core.symbols import strip_context
from mathics_scanner import named_characters
from mathics_pygments.lexer import Regex
from prompt_toolkit.completion import CompleteEvent, Completion, WordCompleter
from prompt_toolkit.completion import (
CompleteEvent,
Completer,
Completion,
WordCompleter,
)
from prompt_toolkit.document import Document

SYMBOLS = rf"[`]?({Regex.IDENTIFIER}|{Regex.NAMED_CHARACTER})(`({Regex.IDENTIFIER}|{Regex.NAMED_CHARACTER}))+[`]?"
Expand Down Expand Up @@ -54,6 +59,28 @@ def get_datadir():
return osp.realpath(datadir)


class InterruptCompleter(Completer):
"""
Completer for the simple command set: 'continue', 'abort', 'exit', 'show'.
"""

COMMANDS = [
"abort",
"continue",
"exit",
"inspect",
"show",
]

def get_completions(
self, document: Document, complete_event
) -> Iterable[Completion]:
word = document.get_word_before_cursor()
for cmd in self.COMMANDS:
if cmd.startswith(word):
yield Completion(cmd, -len(word))


class MathicsCompleter(WordCompleter):
def __init__(self, definitions):
self.definitions = definitions
Expand Down
12 changes: 11 additions & 1 deletion mathicsscript/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from mathics.core.atoms import String
from mathics.core.symbols import Symbol
from mathics.core.systemsymbols import (
SymbolAborted,
SymbolExport,
SymbolExportString,
SymbolFullForm,
Expand All @@ -21,6 +22,7 @@
SymbolOutputForm,
SymbolPlot,
SymbolStandardForm,
SymbolStringForm,
SymbolTeXForm,
)
from mathics.session import get_settings_value
Expand Down Expand Up @@ -61,7 +63,7 @@

def format_output(obj, expr, format=None):
"""
Handle unformatted output using the *specific* capabilities of mathics-django.
Handle unformatted output using the *specific* capabilities of mathicsscript

evaluation.py format_output() from which this was derived is similar but
it can't make use of a front-ends specific capabilities.
Expand Down Expand Up @@ -161,7 +163,15 @@ def eval_boxes(result, fn: Callable, obj, **options):
write_asy_and_view(asy_str)
return expr_type

if expr is SymbolAborted:
obj.out = ["$Aborted"]
obj.last_eval = SymbolAborted
return "$Aborted"
if format == "text":
if expr_head is SymbolStringForm:
return expr.elements[0].value
elif isinstance(expr, String):
return expr.value
result = expr.format(obj, SymbolOutputForm)
elif format == "xml":
result = Expression(SymbolStandardForm, expr).format(obj, SymbolMathMLForm)
Expand Down
Loading
Loading