TerminalView.st
changeset 4593 370db87f8232
parent 4576 313dc2dc2b7b
child 4594 344e42610a24
--- a/TerminalView.st	Sun Jun 01 13:05:28 2014 +0200
+++ b/TerminalView.st	Sun Jun 01 17:00:28 2014 +0200
@@ -19,7 +19,9 @@
 		filterStream recorderStream localEcho translateNLToCRNL
 		inputTranslateCRToNL inputTranslateBackspaceToDelete autoWrapFlag
 		masterWindow alternateKeypadMode noColors
-		sizeOfOutstandingInputToBeProcessed'
+		sizeOfOutstandingInputToBeProcessed lineEditMode lineBuffer
+		lineBufferCursorPosition lineBufferHistory
+		lineBufferHistoryPosition'
 	classVariableNames:'Debug DebugKeyboard'
 	poolDictionaries:''
 	category:'Views-TerminalViews'
@@ -418,6 +420,10 @@
     inputTranslateCRToNL := aBoolean.
 !
 
+lineEditMode:something
+    lineEditMode := something.
+!
+
 localEcho:aBoolean
     "enable/disable local echo"
 
@@ -684,6 +690,9 @@
     DebugKeyboard ifTrue:[
         Transcript showCR:'----'; show:'keyPress:' ; showCR:aKey printString.
     ].
+    lineEditMode == true ifTrue:[
+        (self keyPressInLineEditMode:aKey) ifTrue:[^ self].
+    ].
 
     aKey isCharacter ifTrue:[
         self deselect.
@@ -807,6 +816,126 @@
     "Modified: / 25-01-2012 / 10:43:06 / cg"
 !
 
+keyPressInLineEditMode:aKey
+    "readline alike line editing.
+     return true, if the character was processed,
+     false if not. Then, the caller should proceed as usual."
+
+    |clearLine|
+
+    clearLine :=
+        [
+            lineBufferCursorPosition-1 timesRepeat:[ self deleteCharBeforeCursor ].
+            (lineBuffer size-(lineBufferCursorPosition-1)) timesRepeat:[ self deleteCharAtCursor].
+        ].
+
+    "/ in lineEditMode, defer sending to the pty, until a newline is entered
+    lineBuffer isNil ifTrue:[
+        lineBuffer := String new.
+        lineBufferCursorPosition := 1.
+    ].
+
+    aKey isCharacter ifTrue:[
+        lineBuffer := (lineBuffer copyTo:lineBufferCursorPosition-1)
+                      , aKey
+                      , (lineBuffer copyFrom:lineBufferCursorPosition).
+        self insertCharAtCursor:aKey.
+        lineBufferCursorPosition := lineBufferCursorPosition + 1.
+        ^ true.
+    ].
+    aKey == #Return ifTrue:[
+        "/ as the pty is in echo mode,
+        "/ we should either disable echo for the following,
+        "/ or remove from the textview and let the program redraw them.
+        "/ the second alternative looks easier for now...
+        clearLine value.
+
+        lineBuffer do:[:ch |
+            self sendCharacter:ch.
+        ].
+        inputTranslateCRToNL ifTrue:[
+            self sendCharacter:(Character nl).
+        ] ifFalse:[
+            self sendCharacter:(Character return).
+        ].
+        lineBufferHistory isNil ifTrue:[
+            lineBufferHistory := OrderedCollection new.
+        ].
+        lineBufferHistory addLast:lineBuffer.
+        lineBufferHistoryPosition := lineBufferHistory size + 1.
+        lineBuffer := nil.
+        ^ true.
+    ].
+
+    aKey == #BackSpace ifTrue:[
+        lineBufferCursorPosition > 1 ifFalse:[
+            self beep.  
+            ^ true
+        ].
+        lineBuffer := (lineBuffer copyTo:lineBufferCursorPosition-2)
+                      , (lineBuffer copyFrom:lineBufferCursorPosition).
+        self deleteCharBeforeCursor.
+        lineBufferCursorPosition := lineBufferCursorPosition - 1.
+        ^ true.
+    ].
+
+    aKey == #CursorLeft ifTrue:[
+        lineBufferCursorPosition > 1 ifFalse:[
+            self beep.  
+            ^ true
+        ].
+        lineBufferCursorPosition := lineBufferCursorPosition - 1.
+        self cursorLeft.
+        ^ true.
+    ].
+    aKey == #CursorRight ifTrue:[
+        lineBufferCursorPosition < lineBuffer size ifFalse:[
+            self beep.  
+            ^ true
+        ].
+        lineBufferCursorPosition := lineBufferCursorPosition + 1.
+        self cursorRight.
+        ^ true.
+    ].
+    aKey == #CursorUp ifTrue:[
+        lineBufferHistoryPosition > 1 ifFalse:[
+            self beep.
+            ^ true
+        ].
+
+        "/ remember the current lineBuffer
+        lineBufferHistoryPosition > lineBufferHistory size ifTrue:[
+            lineBufferHistory add:lineBuffer.
+        ] ifFalse:[
+            lineBufferHistory at:lineBufferHistoryPosition put:lineBuffer.
+        ].
+        clearLine value.
+
+        lineBufferHistoryPosition := lineBufferHistoryPosition - 1.
+        lineBuffer := lineBufferHistory at:lineBufferHistoryPosition.
+        self insertStringAtCursor:lineBuffer.
+        lineBufferCursorPosition := lineBuffer size + 1.
+        ^ true.    
+    ].
+    aKey == #CursorDown ifTrue:[
+        lineBufferHistoryPosition < lineBufferHistory size ifFalse:[
+            self beep.
+            ^ true
+        ].
+
+        "/ remember the current lineBuffer
+        lineBufferHistory at:lineBufferHistoryPosition put:lineBuffer.
+        clearLine value.
+
+        lineBufferHistoryPosition := lineBufferHistoryPosition + 1.
+        lineBuffer := lineBufferHistory at:lineBufferHistoryPosition.
+        self insertStringAtCursor:lineBuffer.
+        lineBufferCursorPosition := lineBuffer size + 1.
+        ^ true.    
+    ].
+    ^ false.
+!
+
 shellTerminated
     "shell has terminated"
 
@@ -1153,6 +1282,7 @@
     inputTranslateBackspaceToDelete := false.
     autoWrapFlag := true.
     noColors := false.
+    lineEditMode := false.
     "/ cursorType := #block.
     "/ cursorTypeNoFocus := #frame.
 
@@ -1695,9 +1825,8 @@
     Debug == true ifTrue:[
         Transcript showCR:'send <',aCharacter,'>'
     ].
-    "/ inStream nextPutAll:(String with:aCharacter).
+
     inStream nextPut:aCharacter.
-    "/ inStream flush.
 
     "Created: / 29-07-2013 / 18:18:24 / cg"
 ! !
@@ -2285,11 +2414,11 @@
 !TerminalView class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libwidg2/TerminalView.st,v 1.175 2014-04-30 13:34:42 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libwidg2/TerminalView.st,v 1.176 2014-06-01 15:00:28 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libwidg2/TerminalView.st,v 1.175 2014-04-30 13:34:42 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libwidg2/TerminalView.st,v 1.176 2014-06-01 15:00:28 cg Exp $'
 ! !