3396 fileInText |
3428 fileInText |
3397 self selectedWorkspacesTextView contentsAsString readStream fileIn |
3429 self selectedWorkspacesTextView contentsAsString readStream fileIn |
3398 ! |
3430 ! |
3399 |
3431 |
3400 filterText |
3432 filterText |
3401 |template filterBlock newList oldList answer nDeleted deletedLines| |
3433 |template filterBlock newList oldList answer nDeleted deletedLines |
|
3434 prevLine nextLine line| |
3402 |
3435 |
3403 template := |
3436 template := |
3404 '"/ general text filter; |
3437 '"/ general text filter; |
3405 "/ the following block should evaluate to true for all lines |
3438 "/ the following block should evaluate to true for all lines |
3406 "/ you want to keep - lines for which the block returns false will be removed. |
3439 "/ you want to KEEP. |
|
3440 "/ Lines for which the block returns false, will be removed. |
3407 "/ Beginner warning: Smalltalk know-how is useful here. |
3441 "/ Beginner warning: Smalltalk know-how is useful here. |
3408 |
3442 |
3409 [:line | |
3443 [:line :lineNr :previousLine :nextLine| |
3410 "/ any condition on line. |
3444 "/ any condition on line. |
3411 "/ Notice, that line might be a Text object (i.e. non-string), |
3445 "/ Notice, that line might be a Text object (i.e. non-string), |
3412 "/ so you may want to use line string. |
3446 "/ so you may want to use ''line string''. |
3413 "/ |
3447 "/ |
3414 "/ Useful queries on the line are: |
3448 "/ Useful queries on the line are: |
3415 "/ - size the length of the line |
3449 "/ - size the length of the line |
3416 "/ - hasChangeOfEmphasis any bold, italic etc. |
3450 "/ - hasChangeOfEmphasis any bold, italic etc. |
3417 "/ - startsWith:someString |
3451 "/ - startsWith:someString |
3418 "/ - endsWith:someString |
3452 "/ - endsWith:someString |
3419 |
3453 "/ - includesString:someString |
3420 "/ example filter (removes all empty lines) |
3454 "/ - pattern match: |
|
3455 |
|
3456 "/ example filters |
|
3457 "/ |
|
3458 "/ -------- removes all empty lines --------------- |
3421 "/ |
3459 "/ |
3422 "/ line size > 0 |
3460 "/ line size > 0 |
3423 |
3461 |
3424 "/ example filter (removes all lines which do not end with some suffix) |
3462 "/ -------- removes all lines which do not end with some suffix ---------- |
3425 "/ |
3463 "/ |
3426 "/ (line asLowercase endsWith:''foo'') not |
3464 "/ (line asLowercase endsWith:''foo'') not |
|
3465 |
|
3466 "/ -------- removes duplicates ---------- |
|
3467 "/ |
|
3468 "/ (line = nextLine) not |
3427 |
3469 |
3428 "/ dummy filter (keeps all lines) |
3470 "/ dummy filter (keeps all lines) |
3429 "/ |
3471 "/ |
3430 true |
3472 true |
3431 ] |
3473 ] |
3432 '. |
3474 '. |
3433 |
3475 |
3434 filterBlock := self askForFilterBlock:'Filter block:' |
3476 filterBlock := self askForFilterBlock:'Filter block (to remove lines):' |
3435 template:template |
3477 template:template |
3436 rememberIn:#LastFilterBlockString. |
3478 rememberIn:#LastFilterBlockString. |
3437 filterBlock isNil ifTrue:[^ self]. |
3479 filterBlock isNil ifTrue:[^ self]. |
3438 |
3480 |
3439 oldList := self selectedWorkspacesTextView list. |
3481 oldList := self selectedWorkspacesTextView list. |
3440 oldList := oldList collect:[:lineOrNil | lineOrNil ? '']. |
3482 newList := StringCollection new. |
3441 newList := oldList select:filterBlock. |
3483 deletedLines := OrderedCollection new. |
3442 newList := newList collect:[:line | (line isString and:[line size == 0]) ifTrue:[nil] ifFalse:[line]]. |
3484 |
3443 nDeleted := oldList size - newList size. |
3485 oldList notEmptyOrNil ifTrue:[ |
|
3486 line := nil. |
|
3487 nextLine := oldList at: 1. |
|
3488 1 to: oldList size do:[:lineNr | |
|
3489 prevLine := line. |
|
3490 line := nextLine. |
|
3491 nextLine := oldList at: lineNr + 1 ifAbsent: nil. |
|
3492 |
|
3493 (filterBlock |
|
3494 value:(line ? '') |
|
3495 value: lineNr |
|
3496 value: (prevLine ? '') |
|
3497 value: (nextLine ? '') |
|
3498 ) ifTrue: [ |
|
3499 newList add: line |
|
3500 ] ifFalse:[ |
|
3501 deletedLines add: line. |
|
3502 ] |
|
3503 ]. |
|
3504 |
|
3505 newList := newList collect:[:line | (line isString and:[line size == 0]) ifTrue:[nil] ifFalse:[line]]. |
|
3506 ]. |
|
3507 |
|
3508 nDeleted := deletedLines size. |
3444 nDeleted == 0 ifTrue:[ |
3509 nDeleted == 0 ifTrue:[ |
3445 self information:'No lines were deleted.'. |
3510 self information:'No lines were deleted.'. |
3446 ^ self |
3511 ^ self |
3447 ]. |
3512 ]. |
3448 |
3513 |
3449 answer := Dialog confirmWithCancel:(resources |
3514 answer := OptionBox |
|
3515 request:(resources |
3450 string:'%1 lines remain (%2 deleted). Change text ?' |
3516 string:'%1 lines remain (%2 deleted). Change text ?' |
3451 with:newList size |
3517 with:newList size |
3452 with:nDeleted) |
3518 with:nDeleted) |
3453 labels:#( 'Cancel' 'No, Show Deleted' 'Yes'). |
3519 buttonLabels:#( 'Cancel' 'No, Show Deleted' 'Yes, but Show Deleted' 'Yes') |
|
3520 values:#(nil false yesAndShow true) |
|
3521 default:true. |
3454 answer isNil ifTrue:[^ self]. |
3522 answer isNil ifTrue:[^ self]. |
3455 answer ifFalse:[ |
3523 (answer ~~ true) ifTrue:[ |
3456 deletedLines := oldList reject:filterBlock. |
|
3457 TextBox openOn:(deletedLines asStringCollection) title:'Filtered lines'. |
3524 TextBox openOn:(deletedLines asStringCollection) title:'Filtered lines'. |
3458 ^ self. |
3525 (answer == false) ifTrue:[ |
|
3526 ^ self. |
|
3527 ]. |
3459 ]. |
3528 ]. |
3460 |
3529 |
3461 self selectedWorkspacesTextView list:newList. |
3530 self selectedWorkspacesTextView list:newList. |
|
3531 |
|
3532 "Modified: / 04-05-2012 / 16:03:32 / cg" |
3462 ! |
3533 ! |
3463 |
3534 |
3464 googleSpellingSuggestion |
3535 googleSpellingSuggestion |
3465 self selectedWorkspacesTextView googleSpellingSuggestion |
3536 self selectedWorkspacesTextView googleSpellingSuggestion |
3466 ! |
3537 ! |