home *** CD-ROM | disk | FTP | other *** search
Text File | 1999-10-27 | 11.6 KB | 430 lines | [TEXT/ALFA] |
- #===========================================================================
- # Information about a selection or window.
- #===========================================================================
- proc wordCount {{text ""}} {
- if {$text == ""} {
- if {[set chars [string length [set text [getSelect]]]]} {
- set lines [expr {[lindex [posToRowCol [selEnd]] 0] - [lindex [posToRowCol [getPos]] 0]}]
- set text [getSelect]
- } else {
- set chars [maxPos]
- set lines [lindex [posToRowCol $chars] 0]
- set text [getText [minPos] [maxPos]]
- }
- }
- regsub -all {[!=;.,\(\#\=\):\{\"\}]} $text " " text
- set words [llength $text]
- alertnote [format "%d chars, %d words, %d lines" $chars $words $lines]
- }
-
-
- # FILE: sortLines.tcl
- #
- # This version of sortLines has the option of ignoring blanks/whitespace (-b)
- # and case-insensitive sorting (-i), or reverse sorting, and removing duplicates
- # if desired [-d]
- # sortLines [-b] [-i] [-r] [-d]
-
- # COPYRIGHT:
- #
- # Copyright © 1992,1993 by David C. Black All rights reserved.
- # Portions copyright © 1990, 1991, 1992 Pete Keleher. All Rights Reserved.
- # Portions copyright (c) 1999 Vince Darley, no rights reserved.
- #
- # Redistribution and use in source and binary forms are permitted
- # provided that the above copyright notice and this paragraph are
- # duplicated in all such forms and that any documentation,
- # advertising materials, and other materials related to such
- # distribution and use acknowledge that the software was developed
- # by David C. Black.
- #
- # THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
- # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
- #
- ################################################################################
-
- # AUTHOR
- #
- # David C. Black
- # GEnie: D.C.Black
- # Internet: black@mpd.tandem.com (preferred)
- # USnail: 6217 John Chisum Lane, Austin, TX 78749
- #
- ################################################################################
-
- proc reverseSort {} {sortLines -r}
-
- proc sortLines {args} {
- getOpts
-
- if {[info exists opts(-r)]} {
- set mode "-decreas"
- } else {
- set mode "-increas"
- }
-
- set start [getPos]
- set end [selEnd]
- if {[pos::compare $start == $end]} {
- alertnote "You must highlight the section you wish to sort."
- return
- }
- if {[lookAt [pos::math $end - 1]] != "\r"} {
- alertnote "The selection must consist only of complete lines."
- return
- }
- set text [split [getText $start [pos::math $end - 1]] "\r"]
- if {[info exists opts(-b)] || [info exists opts(-i)] || [info exists opts(-d)]} {
- foreach line $text {
- if {[info exists opts(-i)]} {
- set key [string tolower $line]
- } else {
- set key $line
- }
- if {[info exists opts(-b)]} {
- regsub -all "\[ \t\]+" $key " " key
- }
- if {[info exists opts(-d)]} {
- if {![info exists orig($key)]} {
- set orig($key) $line
- lappend list $key
- }
- } else {
- while {[info exists orig($key)]} {
- append key "z"
- }
- set orig($key) $line
- lappend list $key
- }
- }
- unset text
- foreach key [lsort $mode $list] {
- lappend text $orig($key)
- }
- } else {
- set text [lsort $mode $text]
- }
- set text [join $text "\r"]
- replaceText $start [pos::math $end - 1] $text
- select $start [pos::math $start + [string length $text] +1]
- }
- # Test case:
- #
- # a black
- # A black dog
- # a black cat
- # A Black dog
- # A black dog
-
-
- ##
- # -------------------------------------------------------------------------
- #
- # "sortParagraphs" --
- #
- # Sorts selected paragraphs according to their first 30 characters,
- # it's case insensitive and removes all non alpha-numeric characters
- # before the sort.
- # -------------------------------------------------------------------------
- ##
- proc sortParagraphs {args} {
- set start [getPos]
- set end [selEnd]
- if {[pos::compare $start == $end]} {
- alertnote "You must highlight the section you wish to sort."
- return
- }
- if {[lookAt [pos::math $end - 1]] != "\r"} {
- alertnote "The selection must consist only of complete lines."
- return
- }
- set text [getText $start $end]
- if {[string first "•" $text] != -1} {
- alertnote "Sorry, can't sort paragraphs with bullets '•'."
- return
- }
- regsub -all "\[\r\n\]\[ \t\]*\[\r\n]" $text "\r•" text
- set paras [split $text "•"]
- unset text
- # now each paragraph ends in \r
- foreach para $paras {
- set key [string tolower [string range $para 0 30]]
- regsub -all {[^-a-z0-9]} $key "" key
- # so we don't clobber duplicates!
- while {[info exists orig($key)]} {append key "z"}
- set orig($key) $para
- }
- unset para
- foreach key [lsort [array names orig]] {
- lappend text $orig($key)
- }
- replaceText $start $end [join $text "\r"]
- select $start $end
- }
-
-
-
- #================================================================================
- # Block shift left and right.
- #================================================================================
-
- proc shiftBy {amount} {
- set start [lineStart [getPos]]
- set end [nextLineStart [pos::math [selEnd] - 1]]
- if {[pos::compare $start >= $end]} {set end [nextLineStart $start]}
- set text [text::indentBy [getText $start $end] $amount]
- replaceText $start $end $text
- set end [pos::math $start + [string length $text]]
- if {[pos::compare [nextLineStart $start] == $end]} {
- goto [pos::math $start + [string length $text] - [string length [string trimleft $text]]]
- } else {
- select $start $end
- }
- }
-
- proc shiftRight {} {
- global indentationAmount
- shiftBy $indentationAmount
- }
-
- proc shiftLeft {} {
- global indentationAmount
- shiftBy -$indentationAmount
- }
-
- proc shiftLeftSpace {} {
- shiftBy -1
- }
-
- proc shiftRightSpace {} {
- shiftBy 1
- }
-
- proc doShiftLeft {shiftChar} {
- set start [lineStart [getPos]]
- set end [nextLineStart [pos::math [selEnd] - 1]]
- if {[pos::compare $start >= $end]} {set end [nextLineStart $start]}
-
- set text [split [getText $start [pos::math $end - 1]] "\r\n"]
-
- set textout ""
-
- foreach line $text {
- if {[regexp "($shiftChar)(.*)$" $line "" "" c]} {
- lappend textout $c
- } else {
- lappend textout $line
- }
- }
-
- set text [join $textout "\r"]
- replaceText $start [pos::math $end - 1] $text
- select $start [pos::math $start + [expr {1 + [string length $text]}]]
- }
-
- proc doShiftRight {shiftChar} {
- set start [lineStart [getPos]]
- set end [nextLineStart [pos::math [selEnd] - 1]]
- if {[pos::compare $start >= $end]} {set end [nextLineStart $start]}
-
- set text [split [getText $start [pos::math $end - 1]] "\r\n"]
-
- set text "$shiftChar[join $text \r${shiftChar}]"
- replaceText $start [pos::math $end - 1] $text
- select $start [pos::math $start + [expr {1 + [string length $text]}]]
- }
-
- proc selectAll {} {
- select [minPos] [maxPos]
- }
-
- # Select the next or current word. If word already selected, will go to next.
- proc hiliteWord {} {
- if {[pos::compare [getPos] != [selEnd]]} forwardChar
- forwardWord
- set start [getPos]
- backwardWord
- select $start [getPos]
- }
-
- ##
- # -------------------------------------------------------------------------
- #
- # "togglePrefix" --
- #
- # Useful for e.g. Tcl mode to add/remove a '$', TeX to add/remove
- # a backslash, etc. Only works for single character prefixes.
- # -------------------------------------------------------------------------
- ##
- proc togglePrefix {pref} {
- set p [getPos]
- backwardWord
- if {[lookAt [getPos]] == $pref} {
- deleteChar
- goto [pos::math $p -1]
- } else {
- insertText $pref
- goto [pos::math $p +1]
- }
- }
-
- proc twiddle {} {
- set orSelStart [getPos]
- set orPos [selEnd]
- if {[pos::compare $orPos < [pos::math [minPos] + 2]]} return
-
- set pos $orPos
- set one [lookAt [pos::math $pos -1]]
-
- if {[string first $one " \r\n\t"] > -1} {
- set searchResult [search -s -n -f 0 -m 0 -i 1 -r 1 {[^\s]} [pos::math $pos - 1]]
- if {[llength $searchResult] != 0} then {
- set pos [pos::math [lindex $searchResult 0] + 1]
- set one [lookAt [pos::math $pos - 1]]
- }
- }
- set two [lookAt [pos::math $pos - 2]]
- if {[string first $two " \r\n\t"] > -1} {
- message "transposeChars aborted. A space is involved"
- select $orSelStart $orPos
- return
- }
- replaceText [pos::math $pos -2] $pos "$one$two"
- select $orSelStart $orPos
- message "transposed chars: ‘$one$two’"
- }
-
-
- # transposeWords transpose correctly the two words before the cursor
- # taking into account any other chars in between. We must be after a word, then
- # the proc will be reversible.
-
- proc twiddleWords {} {
- set orSelStart [getPos]
- set pos [selEnd]
- if {[pos::compare $orSelStart != $pos]} {
- goto $pos; # deselect
- }
-
- backwardWord; backwardWord;
- set start1 [getPos]
- forwardWord;
- set end1 [getPos]
- forwardWord;
- set end2 [getPos]
- backwardWord;
- set start2 [getPos]
-
- if {[pos::compare $end2 > $pos] || [pos::compare $start2 > $pos] \
- || [pos::compare $end1 > $pos]} {
- message "transposeWords error: two words must be before"
- select $orSelStart $pos
- return
- }
- if {[pos::compare $start1 != $start2]} {
- set mid [getText $end1 $start2]
- set one [getText $start2 $end2]
- set two [getText $start1 $end1]
- replaceText $start1 $end2 "$one$mid$two"
- # the original selection could be shorter than the words interchanged
- goto $pos
- message "transposed words “$one” with “$two”"
- }
- }
-
-
- proc insertPrefix {} {doPrefix insert}
- proc removePrefix {} {doPrefix remove}
- proc doPrefix {which} {
- global prefixString
- if {[pos::compare [set start [getPos]] == [set end [selEnd]]]} {
- set end [nextLineStart $start]
- }
- set start [lineStart $start]
- set text [getText $start $end]
- replaceText $start $end [doPrefixText $which $prefixString $text]
- goto $start
- endOfLine
- }
-
- proc quoteChar {} {
- message "Literal keystroke to be inserted:"
- insertText [getChar]
- }
-
- proc setPrefix {} {
- global prefixString
- if {[catch {prompt "New Prefix String:" $prefixString} res] == 1} return
- set prefixString $res
- }
-
- proc setSuffix {} {
- global suffixString
- if {[catch {prompt "New Suffix String:" $suffixString} res] == 1} return
- set suffixString $res
- }
-
- proc insertSuffix {} {doSuffix insert}
- proc removeSuffix {} {doSuffix remove}
- proc doSuffix {which} {
- global suffixString
- set pts [getEndpts]
- set start [lindex $pts 0]
- set end [lindex $pts 1]
- set start [lineStart $start]
- set end [nextLineStart [pos::math $end - 1]]
- set text [getText $start $end]
- set text [doSuffixText $which $suffixString $text]
- replaceText $start $end $text
- select $start [getPos]
- }
-
- proc prevLineStart { pos } {
- return [lineStart [pos::math [lineStart $pos] - 1]]
- }
-
-
- proc frontTabsToSpaces { start end } {
- select $start $end
- tabsToSpaces
- }
-
- proc frontSpacesToTabs { start end } {
- getWinInfo a
- set sp [string range " " 1 $a(tabsize) ]
- set from [lindex [posToRowCol $start] 0]
- set to [lindex [posToRowCol $end] 0]
- while {$from <= $to} {
- set pos [rowColToPos $from 0]
- # get the leading whitespace of the current line
- set res [search -s -n -f 1 -r 1 "^\[ \t\]*" $pos]
- if {![llength $res]} {
- # end of the file
- return
- }
- regsub -all "($sp| +\t)" [eval getText $res] "\t" front
- eval replaceText $res [list $front]
- incr from
- }
- }
-
- proc forwardDeleteUntil {{c ""}} {
- if {$c == ""} {
- message "Forward delete up to next:"
- set c [getChar]
- }
- set p [lindex [search -s -n -f 1 -r 1 [quote::Regfind $c] [getPos]] 0]
- if {$p != ""} {
- deleteText [getPos] [pos::math $p + 1]
- }
- }
-
- proc forwardDeleteWhitespace {} {
- set p [lindex [search -s -n -f 1 -r 1 "\[^ \t\r\n\]" [getPos]] 0]
- if {$p != ""} {
- deleteText [getPos] $p
- }
- }
-
-