sort functions
authorClaus Gittinger <cg@exept.de>
Wed, 21 Dec 2005 11:27:14 +0100
changeset 3221 12028ba1582d
parent 3220 246c5a3bfefa
child 3222 7910868ec31e
sort functions
EditTextView.st
--- a/EditTextView.st	Wed Dec 21 11:26:58 2005 +0100
+++ b/EditTextView.st	Wed Dec 21 11:27:14 2005 +0100
@@ -4316,28 +4316,31 @@
 !EditTextView methodsFor:'formatting'!
 
 indent
-    "indent selected line-range"
+    "indent a line-range - this is done by searching for the
+     last non-empty line before the selection, and changing the indent
+     of the selected line-range based on that lines indent."
 
     |start end|
 
     selectionStartLine isNil ifTrue:[^ self].
+
     start := selectionStartLine.
     end := selectionEndLine.
     (selectionEndCol == 0) ifTrue:[
-	end := end - 1
+        end := end - 1
     ].
     self unselect.
     self
-	undoableDo:[
-	    self indentFromLine:start toLine:end
-	]
-	info:'indent'
+        undoableDo:[
+            self indentFromLine:start toLine:end
+        ]
+        info:'indent'
 !
 
 indentFromLine:start toLine:end
-    "indent a line-range - this is don by searching for the
+    "indent a line-range - this is done by searching for the
      last non-empty line before start, and change the indent
-     of the line based on that indent."
+     of the selected line-range based on that lines indent."
 
     |leftStart delta d line spaces|
 
@@ -4347,36 +4350,36 @@
     delta := leftStart - (self leftIndentOfLine:start).
     (delta == 0) ifTrue:[^ self].
     (delta > 0) ifTrue:[
-	spaces := String new:delta
+        spaces := String new:delta
     ].
     start to:end do:[:lineNr |
-	line := self listAt:lineNr.
-	line notNil ifTrue:[
-	    line isBlank ifTrue:[
-		list at:lineNr put:nil
-	    ] ifFalse:[
-		(delta > 0) ifTrue:[
-		    line := spaces , line.
-		    widthOfWidestLine notNil ifTrue:[
-			widthOfWidestLine := widthOfWidestLine max:(self widthOfLineString:line).
-		    ]
-		] ifFalse:[
-		    "check if deletion is ok"
-		    d := delta negated + 1.
-
-		    line size > d ifTrue:[
-			(line copyTo:(d - 1)) withoutSeparators isEmpty ifTrue:[
-			    line := line copyFrom:d
-			]
-		    ].
-		    widthOfWidestLine := nil
-		].
-		list at:lineNr put:line.
-		self textChanged.
-	    ]
-	]
-    ].
-    self redrawFromLine:start to:end
+        line := self listAt:lineNr.
+        line notNil ifTrue:[
+            line isBlank ifTrue:[
+                list at:lineNr put:nil
+            ] ifFalse:[
+                (delta > 0) ifTrue:[
+                    line := spaces , line.
+                    widthOfWidestLine notNil ifTrue:[
+                        widthOfWidestLine := widthOfWidestLine max:(self widthOfLineString:line).
+                    ]
+                ] ifFalse:[
+                    "check if deletion is ok"
+                    d := delta negated + 1.
+
+                    line size > d ifTrue:[
+                        (line copyTo:(d - 1)) withoutSeparators isEmpty ifTrue:[
+                            line := line copyFrom:d
+                        ]
+                    ].
+                    widthOfWidestLine := nil
+                ].
+                self replaceLine:lineNr with:line.
+                self textChanged.
+            ]
+        ]
+    ].
+    "/ self redrawFromLine:start to:end
 
     "Modified: 5.3.1996 / 14:59:18 / cg"
 !
@@ -4631,59 +4634,72 @@
     <resource: #keyboard (#Again #Copy #Cut #Paste #Accept #Find #GotoLine #SaveAs #Print)>
     <resource: #programMenu>
 
-    |items m sub translateItems miscItems toolItems subSub toolSub what undoIdx|
+    |items m sub translateItems sortItems miscItems toolItems subSub toolSub what undoIdx|
 
     items := #(
-		    ('Redo'             redo                    )
-		    ('Again (for All)'  multipleAgain           )
-		    ('-'                                        )
-		    ('Search...'        search         Find     )
-		    ('Goto Line...'     gotoLine       GotoLine )
-		    ('-'                                        )
-		    ('Tools'            tools                   )
-		    ('Misc'             misc                    )
-		    ('-'                                        )
-		    ('Insert File...'   insertFile              )
-		    ('-'                                        )
-		    ('Save As...'       save           SaveAs   )
-		    ('Print'            doPrint        Print    )
-	      ).
+                    ('Redo'             redo                    )
+                    ('Again (for All)'  multipleAgain           )
+                    ('-'                                        )
+                    ('Search...'        search         Find     )
+                    ('Goto Line...'     gotoLine       GotoLine )
+                    ('-'                                        )
+                    ('Tools'            tools                   )
+                    ('Misc'             misc                    )
+                    ('-'                                        )
+                    ('Insert File...'   insertFile              )
+                    ('-'                                        )
+                    ('Save As...'       save           SaveAs   )
+                    ('Print'            doPrint        Print    )
+              ).
 
     miscItems := #(
-		    ('AutoIndent \c'    autoIndent:             )
-		    ('InsertMode \c'    insertMode:             )
-		    ('-'                                        )
-		    ('Learn Macro \c'     learnMode:                  LearnKeyboardMacro)
-		    ('Execute Macro'    executeLearnedKeyboardMacro ExecuteKeyboardMacro )
-		    ('-'                                        )
-		    ('Font...'          changeFont              )
+                    ('Special Characters...'       specialCharacters          )
+                    ('-'                                                      )
+                    ('AutoIndent \c'    autoIndent:             )
+                    ('InsertMode \c'    insertMode:             )
+                    ('-'                                        )
+                    ('Learn Macro \c'     learnMode:                  LearnKeyboardMacro)
+                    ('Execute Macro'    executeLearnedKeyboardMacro ExecuteKeyboardMacro )
+                    ('-'                                        )
+                    ('Font...'          changeFont              )
 "/                    ('Encoding...'      changeEncoding          )
-	    ).
+            ).
 
     translateItems := #(
-		    ('English -> German'      (babelFishTranslate: 'en_de')   )
-		    ('English -> French'      (babelFishTranslate: 'en_fr')   )
-		    ('English -> Italian'     (babelFishTranslate: 'en_it')   )
-		    ('English -> Spanish'     (babelFishTranslate: 'en_es')   )
-		    ('English -> Portuguese'  (babelFishTranslate: 'en_pt')   )
-		    ('-'                                        )
-		    ('German -> English'      (babelFishTranslate: 'de_en')   )
-		    ('French -> English'      (babelFishTranslate: 'fr_en')   )
-		    ('Italian -> English'     (babelFishTranslate: 'it_en')   )
-		    ('Spanish -> English'     (babelFishTranslate: 'es_en')   )
-		    ('Portuguese -> English'  (babelFishTranslate: 'pt_en')   )
-	      ).
+                    ('English -> German'      (babelFishTranslate: 'en_de')   )
+                    ('English -> French'      (babelFishTranslate: 'en_fr')   )
+                    ('English -> Italian'     (babelFishTranslate: 'en_it')   )
+                    ('English -> Spanish'     (babelFishTranslate: 'en_es')   )
+                    ('English -> Portuguese'  (babelFishTranslate: 'en_pt')   )
+                    ('-'                                        )
+                    ('German -> English'      (babelFishTranslate: 'de_en')   )
+                    ('French -> English'      (babelFishTranslate: 'fr_en')   )
+                    ('Italian -> English'     (babelFishTranslate: 'it_en')   )
+                    ('Spanish -> English'     (babelFishTranslate: 'es_en')   )
+                    ('Portuguese -> English'  (babelFishTranslate: 'pt_en')   )
+              ).
+
+    sortItems := #(
+                    ('Lines'                            (sortSelection:ignoreCase: #lines false)        )
+                    ('Lines by First Word'              (sortSelection:ignoreCase: #linesByFirstWord false)     )
+                    ('Lines by nth Word'                (sortSelection:ignoreCase: #linesByNthWord false)       )
+                    ('Words'                            (sortSelection:ignoreCase: #words false)        )
+                    ('-'                                                      )
+                    ('Lines (ignore case)'              (sortSelection:ignoreCase: #lines true)         )
+                    ('Lines by First Word (ignore case)'(sortSelection:ignoreCase: #linesByFirstWord true)      )
+                    ('Lines by nth Word (ignore case)'  (sortSelection:ignoreCase: #linesByNthWord true)        )
+                    ('Words (ignore case)'              (sortSelection:ignoreCase: #words true)         )
+              ).
 
     toolItems := #(
-		    ('Indent'                      indent                     )
-		    ('-'                                                      )
-		    ('Google Spell'                googleSpellingSuggestion   )
-		    ('Translate'                   babelFishTranslate         )
-		    ('-'                                                      )
-		    ('Special Characters...'       specialCharacters          )
-		    ('-'                                                      )
-		    ('Open FileBrowser on It'      openFileBrowserOnIt        )
-	      ).
+                    ('Indent'                      indent                     )
+                    ('Sort'                        sort                       )
+                    ('-'                                                      )
+                    ('Google Spell'                googleSpellingSuggestion   )
+                    ('Translate'                   babelFishTranslate         )
+                    ('-'                                                      )
+                    ('Open FileBrowser on It'      openFileBrowserOnIt        )
+              ).
 
     sub := PopUpMenu itemList:items resources:resources performer:model.
     sub receiver:self.
@@ -4696,6 +4712,10 @@
     subSub receiver:self.
     toolSub subMenuAt:#babelFishTranslate put:subSub.
 
+    subSub := PopUpMenu itemList:sortItems resources:resources performer:model.
+    subSub receiver:self.
+    toolSub subMenuAt:#sort put:subSub.
+
     subSub := PopUpMenu itemList:miscItems resources:resources performer:model.
     subSub receiver:self.
     subSub checkToggleAt:#autoIndent: put:autoIndent.
@@ -4704,62 +4724,62 @@
     sub subMenuAt:#misc put:subSub.
 
     self sensor ctrlDown ifTrue:[
-	m := sub
+        m := sub
     ] ifFalse:[
-	items := #(
-			('Undo'    undo             Undo   )
-			('Again'   again            Again  )
-			('-'                               )
-			('Cut'     cut              Cut    )
-			('Copy'    copySelection    Copy   )
-			('Paste'   pasteOrReplace   Paste  )
-			('-'                               )
-			('Accept'  accept           Accept )
-			('='                               )
-			('More'    others           Ctrl   )
-		  ).
-	m := PopUpMenu itemList:items resources:resources.
-
-	m subMenuAt:#others put:sub.
+        items := #(
+                        ('Undo'    undo             Undo   )
+                        ('Again'   again            Again  )
+                        ('-'                               )
+                        ('Cut'     cut              Cut    )
+                        ('Copy'    copySelection    Copy   )
+                        ('Paste'   pasteOrReplace   Paste  )
+                        ('-'                               )
+                        ('Accept'  accept           Accept )
+                        ('='                               )
+                        ('More'    others           Ctrl   )
+                  ).
+        m := PopUpMenu itemList:items resources:resources.
+
+        m subMenuAt:#others put:sub.
     ].
 
     SOAP::GoogleClient isNil ifTrue:[
-	"/ GoogleClient new spellingSuggestionOf: 'Smmalltlaak and Soaap'.
-	m disable:#googleSpellingSuggestion
+        "/ GoogleClient new spellingSuggestionOf: 'Smmalltlaak and Soaap'.
+        m disable:#googleSpellingSuggestion
     ].
 
     self isReadOnly ifTrue:[
-	m disableAll:#(paste pasteOrReplace cut indent autoIndent: insertMode:
-		       insertFile babelFishTranslate googleSpellingSuggestion)
+        m disableAll:#(paste pasteOrReplace cut indent autoIndent: insertMode:
+                       insertFile babelFishTranslate googleSpellingSuggestion)
     ].
     self hasSelectionForCopy ifFalse:[
-	m disable:#copySelection.
+        m disable:#copySelection.
     ].
     self hasSelection ifFalse:[
-	m disableAll:#(cut googleSpellingSuggestion babelFishTranslate openFileBrowserOnIt).
+        m disableAll:#(cut googleSpellingSuggestion babelFishTranslate openFileBrowserOnIt).
     ] ifTrue:[
-	(Error handle:[:ex |
-	    ex return:false
-	] do:[
-	    self selectionAsString asSingleByteString asFilename exists
-	]) ifFalse:[
-	    m disableAll:#(openFileBrowserOnIt).
-	]
+        (Error handle:[:ex |
+            ex return:false
+        ] do:[
+            self selectionAsString asSingleByteString asFilename exists
+        ]) ifFalse:[
+            m disableAll:#(openFileBrowserOnIt).
+        ]
     ].
     self hasUndoAction ifFalse:[
-	m disable:#undo.
+        m disable:#undo.
     ] ifTrue:[
-	what := undoSupport undoActionInfo.
-	what notNil ifTrue:[
-	    undoIdx := m indexOf:#undo.
-	    m labelAt:undoIdx put:(resources string:'Undo (%1)' with:what).
-	]
+        what := undoSupport undoActionInfo.
+        what notNil ifTrue:[
+            undoIdx := m indexOf:#undo.
+            m labelAt:undoIdx put:(resources string:'Undo (%1)' with:what).
+        ]
     ].
     self hasRedoAction ifFalse:[
-	m disable:#redo.
+        m disable:#redo.
     ].
     acceptEnabled == false ifTrue:[
-	m disable:#accept
+        m disable:#accept
     ].
     ^ m.
 
@@ -5092,6 +5112,73 @@
     v topView label:'deleted text'.
 !
 
+sort:how ignoreCase:ignoreCase fromLine:start toLine:end
+    "sort the selected lines"
+
+    |lines extractor fetcher lineWise nStr n s words|
+
+    lineWise := true.
+
+    how == #lines ifTrue:[
+        extractor := [:l | l withoutLeadingSeparators].
+    ] ifFalse:[ how == #linesByFirstWord ifTrue:[
+        extractor := [:l | (l asCollectionOfWords at:1 ifAbsent:'')].
+    ] ifFalse:[ how == #linesByNthWord ifTrue:[
+        nStr := Dialog request:'Word (1..)'.
+        nStr isEmptyOrNil ifTrue:[^ self].
+        n := Integer readFrom:nStr onError:[^ self].
+
+        extractor := [:l | (l asCollectionOfWords at:n ifAbsent:'')].
+    ] ifFalse:[ how == #words ifTrue:[
+        lineWise := false.
+        extractor := [:w | w].
+    ] ifFalse:[
+        self error.
+    ]]]].
+
+    ignoreCase ifTrue:[
+        fetcher := [:l | (extractor value:l) asLowercase].
+    ] ifFalse:[
+        fetcher := extractor.
+    ].
+
+    lineWise ifTrue:[
+        start == end ifTrue:[^ self ].
+        lines := (start to:end) collect:[:lineNr | (self listAt:lineNr) ? ''].
+        lines := lines sort:[:l1 :l2 | (fetcher value:l1) < (fetcher value:l2)].
+        (start to:end) with:lines do:[:lineNr :line | self replaceLine:lineNr with:line].
+        self textChanged.
+        ^ self.
+    ].
+
+    s := self selectionAsString.
+    words := s asCollectionOfWords.
+    words := words sort:[:w1 :w2 | (fetcher value:w1) < (fetcher value:w2)].
+    s := words asStringCollection asStringWith:Character space.
+    self replace:s.
+    self textChanged.
+!
+
+sortSelection:how ignoreCase:ignoreCase
+    "sort the selected lines"
+
+    |start end|
+
+    selectionStartLine isNil ifTrue:[^ self].
+
+    start := selectionStartLine.
+    end := selectionEndLine.
+    (selectionEndCol == 0) ifTrue:[
+        end := end - 1
+    ].
+
+    self
+        undoableDo:[
+            self sort:how ignoreCase:ignoreCase fromLine:start toLine:end.
+        ]
+        info:'sort'
+!
+
 specialCharacters
     CharacterSetView
 	openAsInputFor:self
@@ -6165,5 +6252,5 @@
 !EditTextView class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libwidg/EditTextView.st,v 1.377 2005-07-13 15:53:27 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libwidg/EditTextView.st,v 1.378 2005-12-21 10:27:14 cg Exp $'
 ! !