2
$\begingroup$

I'm using a command-line interface of Mathematica, or /usr/bin/wolfram. In wrapping this cui program with GNU Readline library, I would like to omit the In[n] := prompt. In other words, I'd like to change

~ $ /usr/bin/wolfram
Wolfram Language 11.3.0 Engine for Linux ARM (32-bit)
Copyright 1988-2018 Wolfram Research, Inc.

In[1]:= 3 + 5                                                                                           

Out[1]= 8

In[2]:= f[x_] := x^

to

~ $ /usr/bin/wolfram
Wolfram Language 11.3.0 Engine for Linux ARM (32-bit)
Copyright 1988-2018 Wolfram Research, Inc.

3 + 5                                                                                           

Out[1]= 8

f[x_] := x^

Is there any options which omit or change the prompt?


Supplement:

-noprompt command line option gives this.

~ $ /usr/bin/wolfram -noprompt
3 + 5                                                                                           
8
f[x_] := x^

This also omits the Out[n]= prompt and a blank line, both of which I don't want to remove. (If these can be shown in some way, the -noprompt options is enough for me.)


Finally, thanks to the really great help of Somos' answer, I came true what I wanted to do by writing the code below to the configuration file ~/.WolframEngine/Kernel/init.m.

ST[] :=
Module[
    {input},
    Print[];
    While[True,
        input = InputString[""];
        If[input == EndOfFile, Quit[]];
        If[input == "", Continue[]];
        Print[];
        Print["Out[", ++$Line, "]= ", ToExpression[input]];
        Print[]
    ]       
]

What I get now: enter image description here

By combining wolfram command and GNU Readline Library, you can equip mathematica with a highly functional line editor. As shown in the gif file above,

  • completion of words
  • highlighting an opening bracket when the corresponding closing bracket is typed
  • rapid cursor movement not only to line's head or tail but also to any position like "next word" or "next occurrence of an alphabet Z".
  • partly editing a line (e.g. delete next two words from the current position, or delete from this opening bracket to the corresponding closing bracket)
  • history search (not shown in the gif)
  • etc.

are realized with custom key-binding which you like (e.g. I'm using wolfram command with vim-like keybindings).

(rlwrap suggested in MATHEMATICA - Completion in terminal? doesn't work well with recent wolfram command. Also JMath: A GNU Readline based frontend for Mathematica is out-dated. What I did is similar to Mathematica with a GNU readline input (Python recipe), though I write a longer C++ code to explicitly use Readline library and turn on additional functionality like completion.)

$\endgroup$
  • $\begingroup$ What do you mean exactly by "wrapping this cui program with GNU Readline library"? $\endgroup$ – Somos Jun 22 at 7:05
  • $\begingroup$ "wrap" is a general word (at least in linux community I reside in) that means "to program a functional or user-friendly interface for some hard-to-use or too-complicated program". In my case, by using GNU Readline library, I wanted to compensate the non-sophisticated line editing functionality of wolfram command. $\endgroup$ – ynn Jun 22 at 10:33
  • $\begingroup$ And you plan to do this with the "rlwrap" program perhaps? Or some other way to get Mathematica to use "readline"? $\endgroup$ – Somos Jun 22 at 10:36
  • $\begingroup$ @Somos No. There is no "plan" any more. Thanks to your help, I succeeded in using Readline with mathematica. (Actually In[] := prompt interferes with Readline, so I wanted to omit this to make my program more useful.) $\endgroup$ – ynn Jun 22 at 10:42
  • $\begingroup$ Surprising to me, I just discovered that the Mathematica CLI that I was using already has "readline" editing and history, but no tab completion. How strange. $\endgroup$ – Somos Jun 22 at 11:02
5
$\begingroup$

You may be able to use some code like this

repl[prompt_: ""] := Module[{input}, Print[]; While[True,
  input = InputString[prompt]; If[input == "quit", Break[],
     Print[]; Print["Out[", ++$Line, "]= ",   
       ToExpression@input]]; Print[]]];

an example of a Read-eval-print loop function which may do what you want and can be easily customized.

An example session is as follows

Mathematica 10.2.0 for Microsoft Windows (32-bit)
Copyright 1988-2015 Wolfram Research, Inc.

In[1]:= repl[prompt_: ""] := Module[
       {input}, Print[]; While[True,
        input = InputString[prompt];
        If[input == "quit", Break[], Print[];
         Print["Out[", $Line++, "]= ",   
           ToExpression@input]]; Print[]]]];

In[2]:= repl["? "]

? 3 + 5

Out[3]= 8

? f[x_] := x^2;

Out[4]= Null

? f[3 + 5]

Out[5]= 64

? quit

In[6]:=
$\endgroup$
  • $\begingroup$ Thank you very much. This is truly what I wanted. By the way, do I have to call repl[] explicitly every time I start wolfram command? ($Line variable is not available before the first In[1]:= prompt is displayed, so I couldn't call repl[] in the configuration file.) $\endgroup$ – ynn Jun 22 at 10:36
  • 1
    $\begingroup$ The example session was just an example. I don't have access to Wolfram Language engine so I can't test it out for myself. There is probably a way to get repl[] defined and called at startup. I don't know any details. I don't think it is a problem to call repl[] in a config file. Also I am not sure about $Line. You can also use your own line counter. $\endgroup$ – Somos Jun 22 at 10:41
  • $\begingroup$ By using my own line counter, now I can call repl[] from the config file. That's perfect. Thank you :) $\endgroup$ – ynn Jun 22 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.