Skip to content

Instantly share code, notes, and snippets.

@baruchel
Last active October 21, 2015 11:24
Show Gist options
  • Save baruchel/b6d8bee16a818dd5d64d to your computer and use it in GitHub Desktop.
Save baruchel/b6d8bee16a818dd5d64d to your computer and use it in GitHub Desktop.

Hacking Dyalog APL # 1

Dyalog APL is a great tool but the interface for working in an interactive session is rather poor. Editing previous lines by moving the cursor to some other location is rather painful. Of course Dyalog APL isn't allowed to use the great readline library. But it is not too difficult to replace the officiel ncurses interface by some more modern readline-based one as long as you do it with an external tool.

What you will get is:

  • a modern readline-based input;
  • a great history system (persistent accross sessions if you wish);
  • the ability to launch your favorite editor on the fly for editing a more complex line of input (or defining a traditional function) whenever you want;
  • leaving Dyalog APL with the Ctrl-D signal;
  • a quicker startup time;
  • etc.

You will lose things like the default editor for editing a function; the following hack is rather intended to work with one-liners and direct functions, but on the other hand you will be able to define traditional functions in your favorite editor rather than in the default one.

The following solutions have been tested on a Raspberry Pi.

First of all, you have to install the great rlwrap tool:

sudo apt-get install rlwrap

Setup

Then create an alias in your ~/.bashrc file by choosing one of the following ideas:

alias dyalog="rlwrap -m sh -c \"stdbuf -i0 -o0 -e0 cat - | dyalog\""

alias dyalog="rlwrap -m sh -c \"grep --line-buffered '^' | dyalog\""

alias dyalog="rlwrap -m sh -c \"sed -u -e '#' | dyalog\""

alias dyalog="rlwrap -m sh -c \"awk -W interactive '{print}' | dyalog\""

You can add some more options to rlwrap (for instance if you want a persistent history accross sessions). If you are sure that you won't need launching an external editor, you may remove the -m option.

The trick above is feeding the input of dyalog from another process rather than from a terminal or a pseudo-terminal. Directly using rlwrap will not work because Dyalog would still detect being used in a terminal and launch the ncurses-based interface.

Configuration

You can configure the behaviour of your readline editor in ~/.inputrc as usual (type man readline for some more information about it). One interesting feature is choosing a key binding for launching your favorite editor; for instance by adding the line:

"\C-e":   rlwrap-call-editor

This means that hitting Ctrl-e will launch your editor and edit the current line inside it. Selecting the right editor is done by adding a line in ~/.bashrc like:

export RLWRAP_EDITOR=/usr/bin/vim

Usage

Now close your current terminal and start a new one (in order to get a new bash session) and type dyalog.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment