Readline (was Re: state of ghc6 on sparc)

Malcolm Wallace Malcolm.Wallace@cs.york.ac.uk
Thu, 19 Jun 2003 17:38:43 +0100


This is a multi-part message in MIME format.

--Multipart_Thu__19_Jun_2003_17:38:43_+0100_083cf9e8
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit

Alastair Reid <alastair@reid-consulting-uk.ltd.uk> writes:

> It would be nice to have those bindings but just having backspace and 
> left-right cursors work would already be a huge improvement over nothing.

OK, here is my contribution.  The attached module SimpleLineEditor
is API-compatible with readline, and is a slight elaboration of
the line editor currently distributed as part of hmake interactive.
It does the basic stuff like backspace and left and right arrows.
Today's addition was a simple history mechanism using (uggh!) an IORef.

Because of the way I chose to implement a separation of
keystroke-recognition from interpretation of the associated editing
command, it should be reasonably straightforward to extend/change
the keystrokes for different terminal types.  It should also be
fairly easy to add more editing commands (e.g. there are commands
for word-movement, and begin/end of line, but no key-binding and no
interpretation yet either.)

Perhaps we should add something like this to the hierarchical libs,
in the readline package?  Then we can have some basic line-editing
functionality available in a portable fashion, independent of whether
any particular machine has the real readline library installed.

Regards,
    Malcolm

--Multipart_Thu__19_Jun_2003_17:38:43_+0100_083cf9e8
Content-Type: text/plain;
 name="SimpleLineEditor.hs"
Content-Disposition: attachment;
 filename="SimpleLineEditor.hs"
Content-Transfer-Encoding: base64

bW9kdWxlIFNpbXBsZUxpbmVFZGl0b3IKICAoIGluaXRpYWxpc2UJCS0tCTo6IElPICgpCiAgLCBn
ZXRMaW5lRWRpdGVkCS0tCTo6IFN0cmluZyAtPiBJTyBTdHJpbmcKICAsIGRlbENoYXJzCQktLQk6
OiBTdHJpbmcgLT4gSU8gKCkKICApIHdoZXJlCgppbXBvcnQgSU8KaW1wb3J0IE1vbmFkICh3aGVu
KQppbXBvcnQgQ2hhcgppbXBvcnQgU3lzdGVtLklPLlVuc2FmZSAodW5zYWZlUGVyZm9ybUlPKQpp
bXBvcnQgRGF0YS5JT1JlZgppbXBvcnQgTWF5YmUKaW1wb3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gbnVsbCBzIHRo
ZW4gcyBlbHNlIHRhaWwgcykgMCBoaXN0CiAgICAgICAgRGVsZXRlIEJlZ2luIC0+IGRlbENoYXJz
IHMgPj4gZ2wgIiIgMCBoaXN0CiAgICAgICAgTW92ZSBMICAgLT4gaWYgbm90IChudWxsIHMpIHRo
ZW4gcHV0U3RyICgiXEJTIikgPj4gZ2wgcyAxIGhpc3QKICAgICAgICAgICAgICAgICAgICBlbHNl
IGdsIHMgMCBoaXN0CiAgICAgICAgSGlzdG9yeSAgLT4gY2FzZSBoaXN0IG9mCiAgICAgICAgICAg
ICAgICAgICAgICAoZnV0LCBbXSkgICAgIC0+IGdsIHMgMCBoaXN0CiAgICAgICAgICAgICAgICAg
ICAgICAoZnV0LCBwOnBhc3QpIC0+IGRvIGRlbENoYXJzIHMKICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgcHV0U3RyIChyZXZlcnNlIHApCiAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgIGdsIHAgMCAoczpmdXQsIHBhc3QpCiAgICAgICAgRnV0
dXJlICAgLT4gY2FzZSBoaXN0IG9mCiAgICAgICAgICAgICAgICAgICAgICAoW10sIHBhc3QpICAg
IC0+IGdsIHMgMCBoaXN0CiAgICAgICAgICAgICAgICAgICAgICAoZjpmdXQsIHBhc3QpIC0+IGRv
IGRlbENoYXJzIHMKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcHV0
U3RyIChyZXZlcnNlIGYpCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
IGdsIGYgMCAoZnV0LCBzOnBhc3QpCiAgICAgICAgXyAgICAgICAgLT4gZ2wgcyAwIGhpc3QKCiAg
ICBnbCBzIG4gaGlzdCA9IGRvCS0tIHMgaXMgYWNjdW11bGF0ZWQgbGluZSwgbigvPTApIGlzIGN1
cnNvciBwb3NpdGlvbgogICAgICBjbWQgPC0gbGluZUNtZAogICAgICBjYXNlIGNtZCBvZgogICAg
ICAgIENoYXIgYyAgIC0+IGRvIHB1dFN0ciAocmV2ZXJzZSAodGFrZSBuIHMpKQogICAgICAgICAg
ICAgICAgICAgICAgIHB1dFN0ciAocmVwbGljYXRlIG4gJ1xCUycpCiAgICAgICAgICAgICAgICAg
ICAgICAgZ2wgKHRha2UgbiBzICsrIGM6IGRyb3AgbiBzKSBuIGhpc3QKICAgICAgICBBY2NlcHQg
ICAtPiByZXR1cm4gKEp1c3QgKHJldmVyc2UgcykpCiAgICAgICAgQ2FuY2VsICAgLT4gcmV0dXJu
IE5vdGhpbmcKICAgICAgICBNb3ZlIFIgICAtPiBkbyBsZXQgbjEgPSBuLTEKICAgICAgICAgICAg
ICAgICAgICAgICBwdXRTdHIgKHJldmVyc2UgKHRha2UgbiBzKSsrIiAiKQogICAgICAgICAgICAg
ICAgICAgICAgIHB1dFN0ciAocmVwbGljYXRlIG4gJ1xCUycpCiAgICAgICAgICAgICAgICAgICAg
ICAgZ2wgcyBuMSBoaXN0CiAgICAgICAgRGVsZXRlIFIgLT4gZG8gbGV0IG4xID0gbi0xCiAgICAg
ICAgICAgICAgICAgICAgICAgcHV0U3RyIChyZXZlcnNlICh0YWtlIG4xIHMpICsrICIgIikKICAg
ICAgICAgICAgICAgICAgICAgICBwdXRTdHIgKHJlcGxpY2F0ZSAobjErMSkgJ1xCUycpCiAgICAg
ICAgICAgICAgICAgICAgICAgZ2wgKHRha2UgbjEgcyArKyBkcm9wIG4gcykgbjEgaGlzdAogICAg
ICAgIE1vdmUgTCAgIC0+IGRvIGxldCBuMSA9IG4rMQogICAgICAgICAgICAgICAgICAgICAgIGlm
IG4xIDw9IGxlbmd0aCBzIHRoZW4gZG8KICAgICAgICAgICAgICAgICAgICAgICAgICAgcHV0U3Ry
ICgnXEJTJzpyZXZlcnNlICh0YWtlIG4xIHMpKQogICAgICAgICAgICAgICAgICAgICAgICAgICBw
dXRTdHIgKHJlcGxpY2F0ZSBuMSAnXEJTJykKICAgICAgICAgICAgICAgICAgICAgICAgICAgZ2wg
cyBuMSBoaXN0CiAgICAgICAgICAgICAgICAgICAgICAgICBlbHNlIGRvCiAgICAgICAgICAgICAg
ICAgICAgICAgICAgIHB1dFN0ciAocmV2ZXJzZSBzKysiICIpCiAgICAgICAgICAgICAgICAgICAg
ICAgICAgIHB1dFN0ciAocmVwbGljYXRlIG4xICdcQlMnKQogICAgICAgICAgICAgICAgICAgICAg
ICAgICBnbCBzIG4gaGlzdAogICAgICAgIERlbGV0ZSBMIC0+IGRvIGxldCBuMSA9IG4rMQogICAg
ICAgICAgICAgICAgICAgICAgIGlmIG4xIDw9IGxlbmd0aCBzIHRoZW4gZG8KICAgICAgICAgICAg
ICAgICAgICAgICAgICAgcHV0U3RyICgnXEJTJzpyZXZlcnNlICh0YWtlIG4gcykrKyIgIikKICAg
ICAgICAgICAgICAgICAgICAgICAgICAgcHV0U3RyIChyZXBsaWNhdGUgbjEgJ1xCUycpCiAgICAg
ICAgICAgICAgICAgICAgICAgICAgIGdsICh0YWtlIG4gcyArKyBkcm9wIG4xIHMpIG4gaGlzdAog
ICAgICAgICAgICAgICAgICAgICAgICAgZWxzZSBkbwogICAgICAgICAgICAgICAgICAgICAgICAg
ICBwdXRTdHIgKHJldmVyc2UgcysrIiAiKQogICAgICAgICAgICAgICAgICAgICAgICAgICBwdXRT
dHIgKHJlcGxpY2F0ZSBuMSAnXEJTJykKICAgICAgICAgICAgICAgICAgICAgICAgICAgZ2wgcyBu
IGhpc3QKICAgICAgICBIaXN0b3J5ICAtPiBjYXNlIGhpc3Qgb2YKICAgICAgICAgICAgICAgICAg
ICAgIChmdXQsIFtdKSAgICAgLT4gZ2wgcyBuIGhpc3QKICAgICAgICAgICAgICAgICAgICAgIChm
dXQsIHA6cGFzdCkgLT4gZG8gZGVsQ2hhcnMgcwogICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICBwdXRTdHIgcAogICAgICAgICAgICAgICAgICAgICAgICAgICA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tIGhHZXRDaGFyIHN0ZGluCiAgICAgICAgICAgICAg
Y2FzZSBjIG9mCiAgICAgICAgICAgICAgICAnRCcgLT4gZGVsQ2hhcnMgIkQiID4+IHJldHVybiAo
TW92ZSBMKQogICAgICAgICAgICAgICAgJ0MnIC0+IGRlbENoYXJzICJDIiA+PiByZXR1cm4gKE1v
dmUgUikKICAgICAgICAgICAgICAgICdBJyAtPiBkZWxDaGFycyAiQSIgPj4gcmV0dXJuIEhpc3Rv
cnkKICAgICAgICAgICAgICAgICdCJyAtPiBkZWxDaGFycyAiQiIgPj4gcmV0dXJuIEZ1dHVyZQog
ICAgICAgICAgICAgICAgJzMnIC0+IGRvIGRlbENoYXJzICIzIgogICAgICAgICAgICAgICAgICAg
ICAgICAgIGMgPC0gaEdldENoYXIgc3RkaW4KICAgICAgICAgICAgICAgICAgICAgICAgICBjYXNl
IGMgb2YKICAgICAgICAgICAgICAgICAgICAgICAgICAgICd+JyAtPiBkZWxDaGFycyAifiIgPj4g
cmV0dXJuIChEZWxldGUgUikKICAgICAgICAgICAgICAgICAgICAgICAgICAgIF8gICAtPiBkZWxD
aGFycyAiXyIgPj4gcmV0dXJuIE5vT3AKICAgICAgICAgICAgICAgIF8gICAtPiBkZWxDaGFycyAi
XyIgPj4gcmV0dXJuIE5vT3AKICAgICAgICAgICdPJyAtPiBkbwogICAgICAgICAgICAgIGRlbENo
YXJzICJPIgogICAgICAgICAgICAgIGMgPC0gaEdldENoYXIgc3RkaW4KICAgICAgICAgICAgICBj
YXNlIGMgb2YKICAgICAgICAgICAgICAgICdEJyAtPiBkZWxDaGFycyAiRCIgPj4gcmV0dXJuIChN
b3ZlIEwpCiAgICAgICAgICAgICAgICAnQycgLT4gZGVsQ2hhcnMgIkMiID4+IHJldHVybiAoTW92
ZSBSKQogICAgICAgICAgICAgICAgJ0EnIC0+IGRlbENoYXJzICJBIiA+PiByZXR1cm4gSGlzdG9y
eQogICAgICAgICAgICAgICAgJ0InIC0+IGRlbENoYXJzICJCIiA+PiByZXR1cm4gRnV0dXJlCiAg
ICAgICAgICAgICAgICBfICAgLT4gZGVsQ2hhcnMgIl8iID4+IHJldHVybiBOb09wCiAgICAgICAg
ICBfICAgLT4gZGVsQ2hhcnMgIl8iID4+IHJldHVybiBOb09wCiAgICAgIF8gLT4gcmV0dXJuIChD
aGFyIGMpCgojZW5kaWYgLS0gVVNFX1JFQURMSU5FCg==

--Multipart_Thu__19_Jun_2003_17:38:43_+0100_083cf9e8
Content-Type: text/plain;
 name="testLineEditor.hs"
Content-Disposition: attachment;
 filename="testLineEditor.hs"
Content-Transfer-Encoding: base64

bW9kdWxlIE1haW4gd2hlcmUKCmltcG9ydCBTaW1wbGVMaW5lRWRpdG9yCmltcG9ydCBNb25hZCAo
d2hlbikKaW1wb3J0IE1heWJlCgptYWluID0gaW5pdGlhbGlzZSA+PiBsb29wCiAgd2hlcmUgbG9v
cCA9IGRvIGwgPC0gZ2V0TGluZUVkaXRlZCAicHJvbXB0PiAiCiAgICAgICAgICAgICAgICAgIHdo
ZW4gKGlzSnVzdCBsKSAocHV0U3RyTG4gKGZyb21KdXN0IGwpKQogICAgICAgICAgICAgICAgICB3
aGVuIChsLz1KdXN0ICJxdWl0IikgbG9vcAogICAgICAgICAgICAgICAgICByZXR1cm4gKCkK

--Multipart_Thu__19_Jun_2003_17:38:43_+0100_083cf9e8--