Skip to content

Commit f96ca42

Browse files
authored
Add --edit-mode option ... (#92)
Add --edit-mode option for prompt-toolkit only. Fixes #85
1 parent 9bb2381 commit f96ca42

File tree

2 files changed

+42
-29
lines changed

2 files changed

+42
-29
lines changed

mathicsscript/__main__.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python3
2+
# Copyright (C) 2025 Rocky Bernstein <[email protected]>
23
# -*- coding: utf-8 -*-
34

45
import os
@@ -239,7 +240,19 @@ def fmt_fun(query: Any) -> Any:
239240
shell.reset_lineno()
240241

241242

243+
if click.__version__ >= "7.":
244+
case_sensitive = {"case_sensitive": False}
245+
else:
246+
case_sensitive = {}
247+
248+
242249
@click.command()
250+
@click.option(
251+
"--edit-mode",
252+
"-e",
253+
type=click.Choice(["emacs", "vi"], **case_sensitive),
254+
help="Set initial edit mode (when using prompt toolkit only)",
255+
)
243256
@click.version_option(version=__version__)
244257
@click.option(
245258
"--full-form",
@@ -364,6 +377,7 @@ def fmt_fun(query: Any) -> Any:
364377
)
365378
@click.argument("file", nargs=1, type=click.Path(readable=True), required=False)
366379
def main(
380+
edit_mode,
367381
full_form,
368382
persist,
369383
quiet,
@@ -422,7 +436,7 @@ def main(
422436
readline = "none" if (execute or file and not persist) else readline.lower()
423437
if readline == "prompt":
424438
shell = TerminalShellPromptToolKit(
425-
definitions, style, completion, unicode, prompt
439+
definitions, style, completion, unicode, prompt, edit_mode
426440
)
427441
else:
428442
want_readline = readline == "gnu"

mathicsscript/termshell_prompt.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
# Copyright (C) 2021-2022 Rocky Bernstein <[email protected]>
2+
# Copyright (C) 2021-2022, 2025 Rocky Bernstein <[email protected]>
33

44
import locale
55
import os
@@ -8,42 +8,41 @@
88
import sys
99
from typing import Optional
1010

11-
from mathics_pygments.lexer import MathematicaLexer, MToken
12-
from mathicsscript.completion import MathicsCompleter
13-
from mathicsscript.termshell import (
14-
CONFIGDIR,
15-
HISTSIZE,
16-
USER_INPUTRC,
17-
is_pygments_style,
18-
ShellEscapeException,
19-
SymbolPygmentsStylesAvailable,
20-
TerminalShellCommon,
21-
)
22-
from mathicsscript.version import __version__
23-
11+
from colorama import init as colorama_init
2412
from mathics.core.atoms import String
2513
from mathics.core.attributes import attribute_string_to_number
2614
from mathics.core.expression import Expression, from_python
2715
from mathics.core.rules import Rule
2816
from mathics.core.systemsymbols import SymbolMessageName
29-
30-
from mathicsscript.bindkeys import bindings, read_inputrc, read_init_file
31-
32-
from prompt_toolkit import PromptSession, HTML, print_formatted_text
17+
from mathics_pygments.lexer import MathematicaLexer, MToken
18+
from prompt_toolkit import HTML, PromptSession, print_formatted_text
3319
from prompt_toolkit.application.current import get_app
3420
from prompt_toolkit.enums import EditingMode
3521
from prompt_toolkit.history import FileHistory
3622
from prompt_toolkit.lexers import PygmentsLexer
3723
from prompt_toolkit.styles.pygments import style_from_pygments_cls
38-
39-
4024
from pygments import format, highlight, lex
41-
from pygments.styles import get_style_by_name
42-
from pygments.formatters.terminal import TERMINAL_COLORS
4325
from pygments.formatters import Terminal256Formatter
44-
from pygments.styles import get_all_styles
26+
from pygments.formatters.terminal import TERMINAL_COLORS
27+
from pygments.styles import get_all_styles, get_style_by_name
4528
from pygments.util import ClassNotFound
4629

30+
# FIXME: __main__ shouldn't be needed. Fix term_background
31+
from term_background.__main__ import is_dark_background
32+
33+
from mathicsscript.bindkeys import bindings, read_init_file, read_inputrc
34+
from mathicsscript.completion import MathicsCompleter
35+
from mathicsscript.termshell import (
36+
CONFIGDIR,
37+
HISTSIZE,
38+
USER_INPUTRC,
39+
ShellEscapeException,
40+
SymbolPygmentsStylesAvailable,
41+
TerminalShellCommon,
42+
is_pygments_style,
43+
)
44+
from mathicsscript.version import __version__
45+
4746
mma_lexer = MathematicaLexer()
4847

4948
ALL_PYGMENTS_STYLES = list(get_all_styles())
@@ -54,11 +53,6 @@
5453
color_scheme[MToken.OPERATOR] = ("magenta", "ansibrightmagenta")
5554
color_scheme[MToken.NUMBER] = ("ansiblue", "ansibrightblue")
5655

57-
from colorama import init as colorama_init
58-
59-
# FIXME: __main__ shouldn't be needed. Fix term_background
60-
from term_background.__main__ import is_dark_background
61-
6256
HISTFILE = osp.join(CONFIGDIR, "history-ptk")
6357

6458

@@ -70,6 +64,7 @@ def __init__(
7064
want_completion: bool,
7165
use_unicode: bool,
7266
prompt: bool,
67+
edit_mode: Optional[str],
7368
):
7469
super(TerminalShellCommon, self).__init__("<stdin>")
7570
self.input_encoding = locale.getpreferredencoding()
@@ -79,6 +74,10 @@ def __init__(
7974
self.prompt = prompt
8075

8176
self.session = PromptSession(history=FileHistory(HISTFILE))
77+
if edit_mode is not None:
78+
self.session.editing_mode = (
79+
EditingMode.VI if edit_mode == "vi" else EditingMode.EMACS
80+
)
8281

8382
# Try importing readline to enable arrow keys support etc.
8483
self.using_readline = False

0 commit comments

Comments
 (0)