Easy Ctext Commenting
# ------------------------------------------------------------------------------
# easyCtextCommenting.tcl for [tG²]
# Copyright (C) 2012-2013 by Sedat Serper
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the ``Software''), to deal in
# the Software without restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
# Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Purpose : set's bindings of ctext widget to enable user to insert or remove
# hash character by mouse control, assumes line numbers of ctext
# widget is set visible
# Scripting & idea : Sedat Serper [SeS]
# Related wiki pages: http://wiki.tcl.tk/24080
# Implementation: easyCtextCommenting <ctext widget path>
# Usage : click with button-3, drag along the line numbers to toggle hash character
# Note : As implemented in Source Editor of tG² (C) 2011-2013
# ------------------------------------------------------------------------------
# OVER-QUOTED VERSION (less preferred way of scripting)
proc easyCtextCommenting {wid} {
set ::oLS 0
set ::lineSelected -1
eval "
bind \$wid.l <ButtonPress-3> {
set markChar \[$wid.l index @0,%y\]
set ::lineSelected \[lindex \[split \$markChar .\] 0\]
set ::lineSelected \[$wid.l get \$lineSelected.0 \$lineSelected.end\]
set ::oLs -1
$wid configure -autoseparators false
}
bind \$wid.l <ButtonRelease-3> {
set ::lineSelected -1
$wid configure -autoseparators true
}
bind \$wid.l <Motion> {
if {\$::lineSelected >=0} {
catch {
set markChar \[$wid.l index @0,%y\]
set ::lineSelected \[lindex \[split \$markChar .\] 0\]
set ::lineSelected \[$wid.l get \$lineSelected.0 \$lineSelected.end\]
if {\$::oLs != \$::lineSelected} {
set ::oLs \$::lineSelected
if {\[$wid get \$::oLs.0 \$::oLs.1\] != \"\#\"} {
$wid insert \$::oLs.0 \#
} else {
$wid delete \$::oLs.0 \$::oLs.1
}
}
}
}
}
"
}
# BRACED VERSION (more preferred way of scripting)
proc easyCtextCommenting {wid {sensButton 3}} {
set ::oLS 0
set ::lineSelected -1
bind $wid.l <ButtonPress-$sensButton> {
set markChar [%W index @0,%y]
set ::lineSelected [lindex [split $markChar .] 0]
set ::lineSelected [%W get $lineSelected.0 $lineSelected.end]
set ::oLs -1
[file rootname %W] configure -autoseparators false
}
bind $wid.l <ButtonRelease-$sensButton> {
set ::lineSelected -1
[file rootname %W] configure -autoseparators true
}
bind $wid.l <Motion> {
if {$::lineSelected >=0} {
catch {
set markChar [%W index @0,%y]
set ::lineSelected [lindex [split $markChar .] 0]
set ::lineSelected [%W get $lineSelected.0 $lineSelected.end]
if {$::oLs != $::lineSelected} {
set ::oLs $::lineSelected
if {[[file rootname %W] get $::oLs.0 $::oLs.1] != "#"} {
[file rootname %W] insert $::oLs.0 #
} else {
[file rootname %W] delete $::oLs.0 $::oLs.1
}
}
}
}
}
}
# ------------------------------------------------------------------------------
# Test code (tested with ActiveState's tcl/tk v8.4.19 on a Win Vista machine)
# ------------------------------------------------------------------------------
package require ctext
pack [ctext .ct] -expand 1 -fill both
easyCtextCommenting .ct