FileApplicationNoteBook.st
changeset 5528 54e3fbee2822
parent 5506 80106aab345b
child 5534 1536730810c0
--- a/FileApplicationNoteBook.st	Tue Feb 17 12:08:22 2004 +0100
+++ b/FileApplicationNoteBook.st	Tue Feb 17 13:08:05 2004 +0100
@@ -73,7 +73,7 @@
 		semaChangeItem wantToPrintAsHexDump printAsHexDump itemChanged
 		itemRemoved enableHexToggle md5CheckSum
 		md5HashValueComputationProcess viewModifiedChannel
-		textEditorModificationTime checkModifiedBlock fileEncoding'
+		textEditorModificationTime checkModifiedBlock fileEncodingHolder'
 	classVariableNames:''
 	poolDictionaries:''
 	privateIn:FileApplicationNoteBook
@@ -810,6 +810,14 @@
         ]
 !
 
+fileEncodingHolder
+    ^ self 
+        aspectFor:#fileEncodingHolder 
+        ifAbsent:[
+            IndirectValue for:('ascii' asValue)
+        ]
+!
+
 modeLabelHolder
     ^ self 
         aspectFor:#modeLabelHolder 
@@ -863,6 +871,7 @@
     self cursorLineLabelHolder valueHolder: (app cursorLineLabelHolder).
     self cursorColLabelHolder valueHolder: (app cursorColLabelHolder).
     self modeLabelHolder valueHolder: (app modeLabelHolder).
+    self fileEncodingHolder valueHolder: (app fileEncodingHolder).
 !
 
 update:something with:aParameter from:changedObject
@@ -4057,6 +4066,18 @@
 
 !FileApplicationNoteBook::TextEditor methodsFor:'accessing'!
 
+fileEncoding
+    ^ self fileEncodingHolder value
+!
+
+fileEncoding:fileEncodingArg
+    self fileEncodingHolder value:fileEncodingArg.
+
+    editView notNil ifTrue:[
+        editView externalEncoding:fileEncodingArg
+    ].
+!
+
 item:aDirContentsItem
 
     self releaseCheckModify.
@@ -4066,6 +4087,9 @@
     super item:aDirContentsItem.
     self printAsHexDump value:(self presentation == #hexDump) withoutNotifying:self.
     self setContents ifFalse:[ ^ false].
+
+    self setUpTextView.
+
     checkModifiedBlock isNil ifTrue:[
         checkModifiedBlock := [
             self enqueueMessage:#checkItemForChangesWithNewSetup 
@@ -4229,7 +4253,7 @@
         ] do:[
             present := self presentation.
             present == #asText ifTrue:[
-                contents := file contents.
+                contents := self getContentsAsText.
             ] ifFalse:[
                 present == #hexDump ifTrue:[
                     self withWaitCursorDo:[
@@ -4242,6 +4266,54 @@
     ^ contents.
 !
 
+getContentsAsText
+    |text guessedEncoding preferredFontEncoding fontsEncoding doNotShowFontDialog action
+     s|
+
+    guessedEncoding := FileBrowser guessEncodingOfFile:item fileName asFilename.
+    guessedEncoding := guessedEncoding ? (self fileEncoding).
+
+    editView notNil ifTrue:[
+        fontsEncoding := editView font encoding ? 'ascii'.
+
+        preferredFontEncoding := FontDescription preferredFontEncodingFor:guessedEncoding.
+        (CharacterEncoder isEncoding:preferredFontEncoding subSetOf:fontsEncoding) ifFalse:[
+                doNotShowFontDialog == true ifTrue:[
+                    action := #show
+                ] ifFalse:[
+                    action := Dialog choose:(resources string:'''%1'' seems to require a %2 font (file encoding is %3).' 
+                                                         with:self fileName baseName 
+                                                         with:preferredFontEncoding allBold 
+                                                         with:guessedEncoding)
+                                   labels:(resources array:#('Cancel' 'Show' 'Don''t Ask Again' 'Change Font'))
+                                   values:#(nil #show #showAlways #encoding)
+                                   default:#encoding.
+                ].
+                action == #showAlways ifTrue:[
+                    doNotShowFontDialog := true.
+                    action := #show.
+                ].
+                action isNil ifTrue:[
+                    AbortSignal raise
+                ].
+                action == #encoding ifTrue:[
+                    self fileEncoding:(guessedEncoding asSymbol).
+                    editView validateFontEncodingFor:self fileEncoding ask:false.
+                ] ifFalse:[
+"/                    doNotShowFontDialog ~~ true ifTrue:[
+"/                        self information:(resources string:'Individual characters may be invisible/wrong in this font.')
+"/                    ]
+                ].
+        ].
+        editView externalEncoding:self fileEncoding.
+    ].
+    self fileEncoding:(guessedEncoding asSymbol).
+
+    s := self fileName readStream.
+    text := self readStream:s lineDelimiter:(Character cr) encoding:self fileEncoding.
+    ^ text
+!
+
 getHashForContents:contents
     | hashStream|
 
@@ -4273,6 +4345,49 @@
     diffView topView label:'File ' , self fileName baseName , ' vs. Editor Contents'.
 !
 
+readStream:aStream lineDelimiter:aCharacter encoding:fileEncodingArg 
+    "read from aStream, answer its contents as StringCollection. 
+     The files lines are delimited by aCharacter.
+     If encoding is nonNil, the file is assumed to be coded according to
+     that symbol, and #decodeString: should be able to convert it."
+
+    |text line fileEncoding editorsEncoding encoder|
+
+    editView isNil ifTrue:[
+        editorsEncoding := #'iso8859-1'.
+    ] ifFalse:[
+        editorsEncoding := editView characterEncoding ? #'iso8859-1'.
+    ].
+    fileEncoding := fileEncodingArg ? #'iso8859-1'.
+    encoder := CharacterEncoder encoderToEncodeFrom:editorsEncoding into:fileEncoding.
+
+    text := StringCollection new.
+
+DecodingFailedError handle:[:ex|self halt]
+do:[
+    aCharacter == Character cr ifTrue:[
+        FileStream lineTooLongErrorSignal handle:[:ex |
+            |s partialLine|
+
+            s := ex parameter at:1.
+            partialLine := ex parameter at:2.
+            ex proceedWith:(partialLine , s upTo:aCharacter)
+        ] do:[
+            [aStream atEnd] whileFalse:[
+                line := aStream nextLine withTabsExpanded.
+                text add:(encoder decodeString:line)
+            ].
+        ].
+    ] ifFalse:[
+        [aStream atEnd] whileFalse:[
+            line := (aStream upTo:aCharacter) withTabsExpanded.
+            text add:(encoder decodeString:line)
+        ].
+    ].
+].
+    ^ text
+!
+
 reload
 
     textEditorModificationTime := self fileName modificationTime.
@@ -4335,12 +4450,15 @@
 !
 
 setContents
-    | text|
+    |text|
 
     text := self getContents.
     text isNil ifTrue:[ ^ false].
+
     self setContentsFor:text.
     ^ true
+
+
 !
 
 setContentsFor:aText
@@ -4459,6 +4577,13 @@
     ^ fileContentsModel.
 !
 
+fileEncodingHolder
+    fileEncodingHolder isNil ifTrue:[
+        fileEncodingHolder := #'utf-8' asValue
+    ].
+    ^ fileEncodingHolder
+!
+
 itemChanged
 
     itemChanged isNil ifTrue:[
@@ -4694,6 +4819,8 @@
     editView saveAction:[ self doSaveAs ].
 
     aWidget modifiedChannel:self viewModifiedChannel.
+
+    editView externalEncoding:self fileEncoding.
 !
 
 postBuildWith:aBuilder
@@ -4744,6 +4871,9 @@
                 editView parenthesisSpecification:parenthesis
             ].
         ].
+    ].
+    editView notNil ifTrue:[
+        editView externalEncoding:self fileEncoding.
     ]
 ! !
 
@@ -5238,5 +5368,5 @@
 !FileApplicationNoteBook class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libtool/FileApplicationNoteBook.st,v 1.145 2004-02-12 13:20:12 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libtool/FileApplicationNoteBook.st,v 1.146 2004-02-17 12:07:52 cg Exp $'
 ! !