TerminalView.st
author Claus Gittinger <cg@exept.de>
Tue, 21 Jul 1998 19:33:37 +0200
changeset 997 c899373aac1d
parent 992 2f4282cab560
child 999 51cf32d15468
permissions -rw-r--r--
speedup (line buffering)

TextCollector subclass:#TerminalView
	instanceVariableNames:'inStream outStream readerProcess shellPid kbdSequences
		escapeSequenceTree currentSequence currentTree kbdMap
		escapeLeadingChars numberOfColumns numberOfLines
		shellTerminateAction rangeStartLine rangeEndLine state'
	classVariableNames:''
	poolDictionaries:''
	category:'Views-TerminalViews'
!

!TerminalView class methodsFor:'documentation'!

documentation
"
    I provide terminal functionality, by forking a command interpreter
    and comunicating with it via a pty.
    I am abstract - concrete terminal characteristics are defined
    by concrete subclasses (see VT52TerminalView).

    [author:]
        Claus Gittinger

    [start with:]
        VT52TerminalView openShell
        VT100TerminalView openShell
"
! !

!TerminalView class methodsFor:'opening'!

open
    ^ self openShell

    "
     VT100TerminalView open
    "

    "Created: / 10.6.1998 / 15:47:25 / cg"
    "Modified: / 9.7.1998 / 17:55:37 / cg"
!

openDummy
    "for testing purposes only - opens a dummy tty-view, which simply
     echoes whatever is typed in"

    |in vt52|

    vt52 := self new.
    in := ForwardingStream on:''.

    in fwdStream:vt52.
    vt52 inStream:in.
    vt52 outStream:in.
    vt52 open

    "
     self openDummy
    "
!

openOnCommand:aCommandString
    "start a command on a pseudo-TTY, open a terminalView on it
     (i.e. this is kind of an xterm)"

    ^ self openOnCommand:aCommandString onExit:[]

    "
     VT100TerminalView openOnCommand:'ls -l'
    "

    "Created: / 9.7.1998 / 17:50:53 / cg"
    "Modified: / 9.7.1998 / 17:57:41 / cg"
!

openOnCommand:aCommandString onExit:aBlock
    "start a command on a pseudo-TTY, open a terminalView on its output
     (i.e. this is kind of an xterm).
     When the command finishes, evaluate aBlock."

    |in top scr vt52|

    top := StandardSystemView new.
    scr := ScrollableView for:self in:top.
    scr origin:0.0@0.0 corner:1.0@1.0.
    vt52 := scr scrolledView.

    vt52 startCommand:aCommandString.
    vt52 shellTerminateAction:[
        top label:('finished: "' , aCommandString , '"'). aBlock value
    ].

    top extent:(scr preferredExtent).
    top label:('executing: "' , aCommandString , '"').
    top open.

    ^ vt52

    "
     VT100TerminalView openOnCommand:'ls -lR'
     VT100TerminalView openOnCommand:'vi /etc/hosts'
    "

    "Created: / 9.7.1998 / 17:54:34 / cg"
    "Modified: / 9.7.1998 / 18:03:31 / cg"
!

openOnInput:inStream  output:outStream
    "open a terminalView on the given streams (which are typically some
     kind of socket). Keys pressed are sent to inStream, text appearing
     from outStream are displayed in the terminal view.
     This can be used to implement things like rlogin
     or telnet views (if connected to a modem, a call-unix can also be
     implemented this way)."

    |in top scr vt52|

    top := StandardSystemView new.
    scr := ScrollableView for:self in:top.
    scr origin:0.0@0.0 corner:1.0@1.0.
    vt52 := scr scrolledView.

    vt52 inStream:inStream.
    vt52 outStream:outStream.
    vt52 startReaderProcess.

    top extent:(scr preferredExtent).
    top label:'shell'.
    top open.
    ^ top

    "Modified: / 20.6.1998 / 17:32:38 / cg"
!

openShell
    "start a shell on a pseudo-TTY, open a terminalView on it
     (i.e. this is kind of an xterm)"

    ^ self openShellIn:nil

    "
     VT100TerminalView openShell
    "

    "Modified: / 21.7.1998 / 18:24:55 / cg"
!

openShellIn:aDirectory
    "start a shell on a pseudo-TTY, open a terminalView on it
     (i.e. this is kind of an xterm)"

    |in top scr vt52|

    top := StandardSystemView new.
    scr := ScrollableView for:self in:top.
    scr origin:0.0@0.0 corner:1.0@1.0.
    vt52 := scr scrolledView.

    vt52 startShellIn:aDirectory.
    vt52 shellTerminateAction:[top destroy].

    top extent:(scr preferredExtent).
    top label:'shell'.
    top open.

    ^ vt52

    "
     VT100TerminalView openShellIn:'/etc'
    "

    "Created: / 20.7.1998 / 18:28:15 / cg"
    "Modified: / 20.7.1998 / 18:28:31 / cg"
! !

!TerminalView class methodsFor:'queries'!

isVisualStartable
    "returns whether this application class can be started via #open"

    self == TerminalView ifTrue:[^ false].
    ^ true

    "Created: / 10.6.1998 / 15:48:43 / cg"
! !

!TerminalView methodsFor:'accessing'!

inStream
    "return the value of the instance variable 'inStream' (automatically generated)"

    ^ inStream!

inStream:something
    "set the value of the instance variable 'inStream' (automatically generated)"

    inStream := something.!

outStream
    "return the value of the instance variable 'outStream' (automatically generated)"

    ^ outStream!

outStream:something
    "set the value of the instance variable 'outStream' (automatically generated)"

    outStream := something.!

shellTerminateAction:aBlock
    "set the block which is evaluated when the shell terminates.
     Can be used to close down the application in this case."

    shellTerminateAction := aBlock.

    "Created: / 12.6.1998 / 17:02:58 / cg"
! !

!TerminalView methodsFor:'cursor handling'!

cursorCol:col
    "check of col is a valid cursor position; return a new col-nr if not.
     Here, the linelength is enforced"

    ^ super cursorCol:(col min:numberOfColumns)

    "Modified: / 10.6.1998 / 15:09:34 / cg"
!

cursorDown:n
    cursorLine + n > list size ifTrue:[
        list := list , (Array new:n).
        self textChanged.
    ].
    super cursorDown:n

    "Modified: / 10.6.1998 / 17:18:41 / cg"
    "Created: / 10.6.1998 / 17:18:50 / cg"
!

cursorLine:l col:col
    "check of col is a valid cursor position; return a new col-nr if not.
     Here, the linelength is enforced"

    ^ super cursorLine:l col:(col min:numberOfColumns)

    "Modified: / 10.6.1998 / 15:09:38 / cg"
!

cursorMovementAllowed
    "return true, if the user may move the cursor around
     (via button-click, or cursor-key with selection).
     Here false is returned - the cursor is only moved by
     cursor positioning escape sequences arriving from the
     stream."

    ^ false

    "Created: / 18.6.1998 / 14:12:02 / cg"
!

validateCursorCol:col inLine:line
    "check of col is a valid cursor position; return a new col-nr if not.
     Here, the linelength is enforced"

    ^ col min:numberOfColumns

    "Modified: / 10.6.1998 / 15:09:41 / cg"
! !

!TerminalView methodsFor:'event handling'!

computeNumberOfLinesShown
    |prevNLines prevNCols|

    prevNCols := (innerWidth // font width).
    prevNLines := nFullLinesShown.

    super computeNumberOfLinesShown.

    ((innerWidth // font width) ~~ prevNCols
    or:[prevNLines ~~ nFullLinesShown]) ifTrue:[
        self defineWindowSize.
    ]

    "Created: / 12.6.1998 / 22:34:39 / cg"
    "Modified: / 20.6.1998 / 19:45:28 / cg"
!

defineWindowSize
    inStream notNil ifTrue:[
        (OperatingSystem 
            setWindowSizeOnFileDescriptor:(inStream fileDescriptor)
            width:(innerWidth // font width)
            height:(nFullLinesShown)) ifFalse:[
"/            Transcript showCR:'VT100: cannot change windowSize'.
        ].
        numberOfLines := nFullLinesShown.
"/        Transcript showCR:'VT100: changed len to ', numberOfLines printString.
    ].
    shellPid notNil ifTrue:[
        OperatingSystem sendSignal:OperatingSystem sigWINCH to:shellPid
    ]

    "Created: / 11.6.1998 / 22:51:39 / cg"
    "Modified: / 20.6.1998 / 20:29:13 / cg"
!

keyPress:aKey x:x y:y
    |rest rawKey seq|

    inStream isNil ifTrue:[^ self].

"/ Transcript showCR:'----'; show:'key:' ; showCR:aKey printString.

    aKey isCharacter ifTrue:[
        "/ send it down to inStream ...
        inStream nextPut:aKey.
        ^ self
    ].

    aKey == #Tab ifTrue:[
"/ Transcript show:'->' ; showCR:Character tab storeString.
        inStream nextPut:Character tab.
        ^ self
    ].

    seq := kbdSequences at:aKey ifAbsent:nil.
    seq notNil ifTrue:[
"/ Transcript show:'->' ; showCR:seq storeString.
        inStream nextPutAll:(seq withEscapes).
        ^ self
    ].

    rawKey := device keyboardMap keyAtValue:aKey ifAbsent:aKey.
    seq := kbdSequences at:rawKey ifAbsent:nil.
    seq notNil ifTrue:[
"/ Transcript show:'->' ; showCR:seq storeString.
        inStream nextPutAll:(seq withEscapes).
        ^ self
    ].

    (rawKey startsWith:'Ctrl') ifTrue:[
        rest := rawKey copyFrom:5.
        rest size == 1 ifTrue:[
            rest := rest at:1.
            (rest >= $a and:[rest <= $z]) ifTrue:[
"/ Transcript show:'->' ; showCR:(Character value:(rest - $a + 1)) storeString.
                inStream nextPut:(Character value:(rest - $a + 1)).
                ^ self
            ].
            (rest >= $A and:[rest <= $Z]) ifTrue:[
"/ Transcript show:'->' ; showCR:(Character value:(rest - $a + 1)) storeString.
                inStream nextPut:(Character value:(rest - $A + 1)).
                ^ self
            ].
        ]
    ].

    (rawKey startsWith:'Control') ifTrue:[
        ^ self
    ].
    (rawKey startsWith:'Shift') ifTrue:[
        ^ self
    ].
    (rawKey startsWith:'Alt') ifTrue:[
        ^ self
    ].
    (rawKey startsWith:'Cmd') ifTrue:[
        ^ super keyPress:aKey x:x y:y
    ].

    Transcript show:'unhandled: '; showCR:rawKey.

    "Modified: / 12.6.1998 / 22:16:50 / cg"
!

shellTerminated
    self closeDownShell.
    shellTerminateAction notNil ifTrue:[
        shellTerminateAction value
    ] ifFalse:[
        "/ may be removed ...
        self warn:'shell terminated'.
    ]

    "Modified: / 12.6.1998 / 17:03:26 / cg"
!

sizeChanged:how
    super sizeChanged:how.
    self defineWindowSize.

    "Modified: / 11.6.1998 / 22:51:48 / cg"
! !

!TerminalView methodsFor:'functions'!

doBackspace
    self cursorLeft.
    self replaceCharAtCursor:(Character space).
    self cursorLeft.

    "Modified: / 10.6.1998 / 17:09:12 / cg"
!

doClearDisplay
    firstLineShown to:(list size) do:[:l |
        self at:l put:''
    ].

    "Created: / 10.6.1998 / 14:43:06 / cg"
    "Modified: / 10.6.1998 / 14:58:07 / cg"
!

doClearToEnd
    self doClearToEndOfLine.
    cursorLine+1 to:(list size) do:[:l |
        self at:l put:''
    ].

    "Modified: / 10.6.1998 / 14:45:43 / cg"
!

doClearToEndOfLine
    |l|

    l := self listAt:cursorLine.
    (l size >= (cursorCol-1)) ifTrue:[
        l notNil ifTrue:[
            l := l copyTo:cursorCol-1.
            self withoutRedrawAt:cursorLine put:l.
            self invalidateLine:cursorLine
            "/ self at:cursorLine put:l.
        ]
    ]

    "Created: / 10.6.1998 / 14:45:01 / cg"
    "Modified: / 20.6.1998 / 19:10:21 / cg"
!

doCursorDown:n
    |wasOn rEnd|

"/    rangeEndLine == numberOfLines ifTrue:[
"/        ^ super cursorDown:n
"/    ].
    cursorLine + 1 - firstLineShown + n <= rangeEndLine ifTrue:[
        "/ no special action req'd
        ^ super cursorDown:n
    ].

    n timesRepeat:[
        wasOn := self hideCursor.
        rEnd := rangeEndLine+firstLineShown-1.
        cursorLine ==  rEnd ifTrue:[
            self deleteLine:(rangeStartLine+firstLineShown-1).
            self insertLine:'' before:rEnd.
        ] ifFalse:[
            super cursorDown
        ].
        wasOn ifTrue:[self showCursor]. "/ self makeCursorVisibleAndShowCursor:wasOn.
    ]

    "Modified: / 20.6.1998 / 20:29:39 / cg"
!

doCursorHome
    self cursorVisibleLine:1 col:1
    "/ super cursorHome

    "Modified: / 10.6.1998 / 20:47:31 / cg"
!

doCursorLeft:n
    n timesRepeat:[
        super cursorLeft
    ]

    "Created: / 11.6.1998 / 22:30:00 / cg"
!

doCursorNewLine
    super cursorDown:1

    "Modified: / 10.6.1998 / 16:55:57 / cg"
!

doCursorReturn
    super cursorToBeginOfLine
!

doCursorRight:n
    self cursorCol:(cursorCol + n)

    "Created: / 10.6.1998 / 15:10:08 / cg"
!

doCursorUp:n
    |wasOn rStart|

"/    rangeStartLine == 1 ifTrue:[
"/        ^ super cursorUp:n
"/    ].
    cursorLine + 1 - firstLineShown - n >= rangeStartLine ifTrue:[
        "/ no special action req'd
        ^ super cursorUp:n
    ].
    n timesRepeat:[
        wasOn := self hideCursor.
        rStart := rangeStartLine+firstLineShown-1.
        cursorLine == rStart ifTrue:[
            (rangeEndLine+firstLineShown-1) <= list size ifTrue:[
                self deleteLine:(rangeEndLine+firstLineShown-1).
                self insertLine:'' before:rStart.
            ].
        ] ifFalse:[
            super cursorUp
        ].
        self makeCursorVisibleAndShowCursor:wasOn.
    ]

    "Created: / 11.6.1998 / 22:29:46 / cg"
    "Modified: / 20.6.1998 / 20:30:34 / cg"
!

doSendInterrupt
    OperatingSystem sendSignal:(OperatingSystem sigINT) to:shellPid negated.

    "Modified: / 10.6.1998 / 17:49:49 / cg"
! !

!TerminalView methodsFor:'initialization'!

closeDownShell
    |pid|

    (pid := shellPid) notNil ifTrue:[
"/        Transcript print:'killing shell pid='; showCR:pid.

        OperatingSystem terminateProcessGroup:pid.
        OperatingSystem terminateProcess:pid.
        Delay waitForSeconds:1.
        shellPid notNil ifTrue:[
            OperatingSystem killProcessGroup:pid.
            OperatingSystem killProcess:pid.
            shellPid := nil.
        ].
        OperatingSystem closePid:pid.
    ].

    readerProcess notNil ifTrue:[
        readerProcess terminate.
        "/ give it a chance to really terminate
        Processor yield.
        readerProcess := nil
    ].
    inStream notNil ifTrue:[
        inStream close.
        inStream := nil
    ].
    outStream notNil ifTrue:[
        outStream close.
        outStream := nil
    ].

    "/ flush any leftover input-processing events
    self sensor flushEventsFor:self withType:#processInput:n:.

    "Modified: / 21.7.1998 / 19:00:13 / cg"
!

destroy
    self closeDownShell.
    super destroy
!

escapeSequences:codes
    |tree|

    tree isNil ifTrue:[tree := escapeSequenceTree := IdentityDictionary new].

    codes do:[:specEntry |
        |sequence function|

        sequence := (specEntry at:1) withEscapes.
        function := specEntry at:2.

        tree := escapeSequenceTree.

        sequence keysAndValuesDo:[:idx :char |
            |followup|

            idx == sequence size ifTrue:[
                tree at:char put:function
            ] ifFalse:[
                followup := tree at:char ifAbsent:nil.
                followup isNil ifTrue:[
                    tree at:char put:(followup := IdentityDictionary new).
                ].
                tree := followup
            ]
        ]
    ].
    escapeLeadingChars := escapeSequenceTree keys asSet.
    escapeLeadingChars add:(Character cr).
    escapeLeadingChars add:(Character return).
    escapeLeadingChars add:(Character backspace).

    escapeLeadingChars := escapeLeadingChars asArray

    "Modified: / 9.6.1998 / 19:43:12 / cg"
!

initialize
    super initialize.

    showMatchingParenthesis := false.
    insertMode := false.
    alwaysAppendAtEnd := false.
    collectSize := 100.
    st80Mode := false.
    trimBlankLines := true.

    numberOfColumns := 80.
    numberOfLines := 24.
    rangeStartLine := 1.
    rangeEndLine := numberOfLines.

    self initializeKeyboardSequences.
    list := OrderedCollection new:24 withAll:''.

    self initializeKeyboardMap.

    "
     VT52TerminalView openShell
     VT100TerminalView openShell
    "

    "Modified: / 20.6.1998 / 20:06:49 / cg"
!

initializeKeyboardMap
    |ctrlKeys cmdKeys|

    "/ setup my own keyboardMap, where control-keys are
    "/ not translated.
    kbdMap := device keyboardMap copy.

    ctrlKeys := kbdMap keys select:[:key | key startsWith:'Ctrl'].
    ctrlKeys do:[:key | kbdMap removeKey:key].

    cmdKeys := kbdMap keys select:[:key | key startsWith:'Cmd'].
    cmdKeys do:[:key | 
        (#(Copy Paste SaveAs Print) includes:(kbdMap at:key)) ifFalse:[
            kbdMap removeKey:key
        ]
    ].

    kbdMap removeKey:#Delete ifAbsent:[].
    kbdMap removeKey:#BackSpace ifAbsent:[].

    "
     VT52TerminalView openShell
    "

    "Modified: / 12.6.1998 / 22:18:23 / cg"
!

initializeKeyboardSequences
    self subclassResponsibility.
!

keyboardMap
    "return my keyboard map. This has control keys removed and
     those will be passed unchanged to the shell"

    ^ kbdMap

    "Modified: / 10.6.1998 / 17:46:59 / cg"
!

startReaderProcess
    "Start a reader process, which looks for the commands output,
     and sends me #peocessInput:n: events whenever something arrives."

    readerProcess isNil ifTrue:[
        readerProcess := [
            [
                self waitUntilVisible.

                Stream streamErrorSignal handle:[:ex |
                    Transcript showCR:ex errorString.
                    Transcript showCR:OperatingSystem lastErrorString.
                ] do:[
                    [true] whileTrue:[
                        Object abortSignal handle:[:ex |
                            self showCursor.
                        ] do:[
                            |buffer n|

                            outStream readWait.
                            (self sensor hasKeyPressEventFor:self) ifTrue:[
                                (self sensor eventCount > 10) ifTrue:[
                                    "/ give terminalView a chance to
                                    "/ send out the character.
                                    Delay waitForSeconds:0.1.
                                ]
                            ] ifFalse:[
                                buffer := String new:1024.
                                n := outStream nextAvailableBytes:1024 into:buffer startingAt:1.

                                n > 0 ifTrue:[
                                    self pushEvent:#processInput:n: with:buffer with:n.
                                    (self sensor eventCount > 50) ifTrue:[
                                        [self sensor eventCount > 10] whileTrue:[
                                            "/ give terminalView a chance to
                                            "/ catch up.
                                            Delay waitForSeconds:0.1.
                                        ]
                                    ].
                                ] ifFalse:[
                                    n == 0 ifTrue:[
                                        outStream atEnd ifTrue:[
                                            outStream close. outStream := nil.
                                            inStream close.  inStream := nil.
                                            
                                            Processor activeProcess terminate.
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ] valueOnUnwindDo:[
                readerProcess := nil    
            ]
        ] fork. "/ forkAt:9.
        readerProcess name:'pty reader'.
    ]

    "
     VT100TerminalView openShell
    "

    "Modified: / 21.7.1998 / 19:32:57 / cg"
! !

!TerminalView methodsFor:'initialization-shell'!

startCommand:aCommand
    "start a command on a pseudo terminal. If the command arg is nil,
     a shell is started. The command is started in the current directory.
     Also fork a reader process, to read the shells output and
     tell me, whenever something arrives"

    ^ self startCommand:aCommand in:nil

    "Modified: / 20.7.1998 / 18:30:24 / cg"
!

startCommand:aCommand in:aDirectory
    "start a command on a pseudo terminal. If the command arg is nil,
     a shell is started. If aDirectory is not nil, the command is
     executed in that directory.
     Also fork a reader process, to read the shells output and
     tell me, whenever something arrives"

    |p slaveFD execFdArray blocked exitStatus|

    self create.  "/ need my windowID (to pass down in environment)

    p := ExternalStream makePTYPair.
    p isNil ifTrue:[
        self warn:'cannot open pty'.
        ^ self.
    ].

    "/ p at:1 is the master;
    "/ p at:2 is the slave
    inStream := outStream := (p at:1).
    inStream buffered:false.

    self defineWindowSize.

    "/ fork a shell process on the slave-side
    slaveFD := (p at:2) fileDescriptor.

    execFdArray := Array with:slaveFD with:slaveFD with:slaveFD.

    blocked := OperatingSystem blockInterrupts.

    shellPid := Processor
               monitor:[
                  |e shell cmd args|

                  e := Dictionary new.
                  e at:'TERM'     put:(self terminalType).
                  e at:'LINES'    put:(numberOfLines printString).
                  e at:'COLUMNS'  put:(numberOfColumns printString).
                  drawableId notNil ifTrue:[
                      e at:'WINDOWID' put:(drawableId address printString).
                  ].

                  aCommand isNil ifTrue:[
                      shell := OperatingSystem getEnvironment:'SHELL'.
                      shell size == 0 ifTrue:[
                          shell := '/bin/sh'.
                      ].
                      cmd := shell asFilename baseName.
                      args := (Array with:cmd).
                  ] ifFalse:[
                      shell := '/bin/sh'.
                      args := (Array with:'sh' with:'-c' with:aCommand).
                  ].
                  e at:'SHELL'  put:shell.
                
                  OperatingSystem
                      exec:shell
                      withArguments:args
                      environment:e
                      fileDescriptors:execFdArray
                      closeDescriptors:#()
                      fork:true
                      newPgrp:true
                      inDirectory:aDirectory.
               ]
               action:[:status |
"/                  Transcript show:'pid:'; showCR:status pid.
"/                  Transcript show:'status:'; showCR:status status.
"/                  Transcript show:'code:'; showCR:status code.
"/                  Transcript show:'core:'; showCR:status core.
                  status stillAlive ifFalse:[
                      exitStatus := status.
                      OperatingSystem closePid:shellPid.
                      shellPid := nil.
                      self pushEvent:#shellTerminated
                  ].
               ].

    shellPid isNil ifTrue:[
        self halt.
        (p at:1) close.
        (p at:2) close.
    ].

    blocked ifFalse:[
        OperatingSystem unblockInterrupts
    ].

    self startReaderProcess.

    "Created: / 20.7.1998 / 18:19:32 / cg"
    "Modified: / 20.7.1998 / 18:30:12 / cg"
!

startShell
    "start a shell on a pseudo terminal in the current directory.
     Also fork a reader process, to read the shells output and
     tell me, whenever something arrives"

    ^ self startCommand:nil

    "
     VT100TerminalView openShell
    "

    "Modified: / 20.7.1998 / 18:29:54 / cg"
!

startShellIn:aDirectory
    "start a shell on a pseudo terminal in some directory.
     Also fork a reader process, to read the shells output and
     tell me, whenever something arrives"

    ^ self startCommand:nil in:aDirectory

    "
     VT100TerminalView openShellIn:'/etc'
    "

    "Modified: / 20.7.1998 / 18:29:46 / cg"
! !

!TerminalView methodsFor:'menu'!

editMenu
    "return the views middleButtonMenu"

    <resource: #keyboard (#Copy #Paste #Print)>
    <resource: #programMenu>

    |items m sub shortKeys sensor|

    ((sensor := self sensor) notNil and:[sensor ctrlDown]) ifTrue:[
        items := #(
                        ('Interrupt'      doSendInterrupt)  
                  ).
    ] ifFalse:[
        items := #(
                        ('copy'         copySelection    Copy   )
                        ('paste'        pasteOrReplace   Paste  )
                        ('-'                                    )
                        ('font ...'     changeFont              )
                        ('-'                                    )
                        ('save as ...'  save             SaveAs )
                        ('print'        doPrint          Print  )
                  ).
    ].

    m := PopUpMenu itemList:items resources:resources.

    self hasSelection not ifTrue:[
        m disable:#copySelection.
    ].
    ^ m.

    "Modified: / 12.6.1998 / 22:33:33 / cg"
! !

!TerminalView methodsFor:'misc'!

removeTrailingBlankLines
    ^ self
! !

!TerminalView methodsFor:'processing - input'!

processInput:buffer n:count
    |sensor|

    self hideCursor.

access critical:[
    |i i2 s crnlFollows|

    i := 1.
    [i <= count] whileTrue:[
        (state == 0) ifTrue:[
            "/ in initial state.
            "/ quick scan forward for next control character ...
            i2 := buffer indexOfControlCharacterStartingAt:i.
            i2 == 0 ifTrue:[
                "/ no control characters - simply append all
                "/ to the outstanding lines ...
                s := buffer copyFrom:i to:count.
                i := count + 1. "/ leave loop.
                crnlFollows := false.
            ] ifFalse:[
                i2 > i ifTrue:[
                    s := buffer copyFrom:i to:i2-1.
                    i := i2. "/ proceed withcontrol character
                    crnlFollows := false.
                    i < (count - 1) ifTrue:[
                        (buffer at:i) == Character return ifTrue:[
                            (buffer at:i+1) == Character nl ifTrue:[
                                crnlFollows := true.
                                i := i + 2.
                            ]
                        ]
                    ].
                ]
            ].
        ].

        s notNil ifTrue:[
            currentEmphasis notNil ifTrue:[
                s := s emphasizeAllWith:currentEmphasis
            ].

            outstandingLine size > 0 ifTrue:[
                outstandingLine := outstandingLine , s.
            ] ifFalse:[
                outstandingLine := s.
            ].
            crnlFollows ifTrue:[
                outstandingLines isNil ifTrue:[
                    outstandingLines := OrderedCollection with:outstandingLine
                ] ifFalse:[
                    outstandingLines add:outstandingLine.
                ].
                outstandingLine := ''.
            ].
            s := nil.

            collecting ifTrue:[
                flushPending ifFalse:[
                    self installDelayedUpdate
                ] ifTrue:[
"/                    outstandingLines size > collectSize ifTrue:[
"/                        self endEntry
"/                    ]
                ]
            ] ifFalse:[
                self endEntry
            ].
        ] ifFalse:[
            "/ no chunk to append (in an escape sequence)
            "/ must handle individual characters
            "/ to update the state machine.
            self nextPut:(buffer at:i).
            i := i + 1.
        ]
    ].

].

    (sensor := self sensor) notNil ifTrue:[
        (sensor hasEvent:#processInput:n: for:self) ifFalse:[
            self endEntry.
            self showCursor.
            "/ self makeCursorVisible.
        ] ifTrue:[
            outstandingLines size > collectSize ifTrue:[ 
                self endEntry.
                self showCursor.
                "/ self makeCursorVisible.
                "/ redraw
                windowGroup notNil ifTrue:[
                    windowGroup processRealExposeEventsFor:self.
                ]
            ].
        ]
    ].

    "Created: / 10.6.1998 / 17:26:09 / cg"
    "Modified: / 21.7.1998 / 19:30:18 / cg"
! !

!TerminalView methodsFor:'queries'!

preferredExtent
    ^ (fontWidth * numberOfColumns + (leftMargin * 2))
      @ 
      ((self heightForLines:numberOfLines) + 8)

    "Modified: / 20.6.1998 / 20:06:57 / cg"
!

terminalType
    ^ #dump

    "Modified: / 10.6.1998 / 14:47:03 / cg"
    "Created: / 10.6.1998 / 16:22:30 / cg"
! !

!TerminalView methodsFor:'selection handling'!

paste:someText
    "paste - redefined to send the chars to the shell instead
     of pasting into the view"

    |s nLines|

    s := someText.
    s isString ifTrue:[
        s := s asStringCollection
    ] ifFalse:[
        (s isKindOf:StringCollection) ifFalse:[
            self warn:'selection (' , s class name , ') is not convertable to Text'.
            ^ self
        ]
    ].
    (nLines := s size) == 0 ifTrue:[^ self].
    (nLines == 1 and:[(s at:1) size == 0]) ifTrue:[^ self].
    s keysAndValuesDo:[:idx :line |
        inStream nextPutAll:line.
        idx ~~ nLines ifTrue:[
            inStream nextPut:(Character return).
        ]
    ].

    "Modified: / 12.6.1998 / 22:12:47 / cg"
! !

!TerminalView class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libwidg2/TerminalView.st,v 1.46 1998-07-21 17:33:37 cg Exp $'
! !