Tk Library Source Code


[ Main Table Of Contents | Table Of Contents | Keyword Index | Categories | Modules | Applications ]


ctext - Ctext a text widget with highlighting support

Table Of Contents


package require Tk
package require ctext ?3.3?

ctext pathName ?options?
::ctext::addHighlightClass pathName class color keywordlist
::ctext::addHighlightClassWithOnlyCharStart pathName class color char
::ctext::addHighlightClassForSpecialChars pathName class color charstring
::ctext::addHighlightClassForRegexp pathName class color pattern
::ctext::clearHighlightClasses pathName
::ctext::getHighlightClasses pathName
::ctext::deleteHighlightClass pathName class
::ctext::enableComments enable
::ctext::disableComments enable
pathName highlight startIndex endIndex
pathName fastdelete index1 ?index2?
pathName fastinsert
pathName copy
pathName cut
pathName paste
pathName append
pathName configure option value ?...?


The ctext package provides the ctext widget which is an enhanced text widget with support for configurable syntax highlighting and some extra commands.

Ctext overloads the text widget and provides new commands, named highlight, copy, paste,cut, append, and edit. It also provides several commands that allow you to define classes. Each class corresponds to a tag in the widget.



Highlighting is controlled with text widget tags, that are called highlight classes. The class is a tag name and can be configured like any text widget tag. Four types of highlight classes are supported. All highlight classes are automatically used by the highlight method of the widget.


Each ctext widget created with the above command supports the following commands and options in addition to the standard text widget commands and options.



package require Tk
package require ctext

proc main {} {
pack [frame .f] -fill both -expand 1
pack [scrollbar .f.s -command {.f.t yview}] -side right -fill y

pack [ctext .f.t -bg black -fg white -insertbackground yellow  -yscrollcommand {.f.s set}] -fill both -expand 1

ctext::addHighlightClass .f.t widgets purple  [list ctext button label text frame toplevel  scrollbar checkbutton canvas listbox menu menubar menubutton  radiobutton scale entry message tk_chooseDir tk_getSaveFile  tk_getOpenFile tk_chooseColor tk_optionMenu]

ctext::addHighlightClass .f.t flags orange  [list -text -command -yscrollcommand  -xscrollcommand -background -foreground -fg -bg  -highlightbackground -y -x -highlightcolor -relief -width  -height -wrap -font -fill -side -outline -style -insertwidth  -textvariable -activebackground -activeforeground -insertbackground  -anchor -orient -troughcolor -nonewline -expand -type -message  -title -offset -in -after -yscroll -xscroll -forward -regexp -count  -exact -padx -ipadx -filetypes -all -from -to -label -value -variable  -regexp -backwards -forwards -bd -pady -ipady -state -row -column  -cursor -highlightcolors -linemap -menu -tearoff -displayof -cursor  -underline -tags -tag]

ctext::addHighlightClass .f.t stackControl red  {proc uplevel namespace while for foreach if else}
ctext::addHighlightClassWithOnlyCharStart .f.t vars mediumspringgreen "\$"
ctext::addHighlightClass .f.t variable_funcs gold {set global variable unset}
ctext::addHighlightClassForSpecialChars .f.t brackets green {[]{}}
ctext::addHighlightClassForRegexp .f.t paths lightblue {\.[a-zA-Z0-9\_\-]+}
ctext::addHighlightClassForRegexp .f.t comments khaki {#[^\n\r]*}
.f.t fastinsert end [info body main]

pack [frame .f1] -fill x

.f.t highlight 1.0 end

pack [button .f1.exit -text Exit -command exit] -side left

pack [entry .e] -side bottom -fill x
.e insert end "ctext::deleteHighlightClass .f.t "
bind .e <Return> {eval [.e get]}

Further examples are in the source package for ctext.


Kevin Kenny, Neil Madden, Jeffrey Hobbs, Richard Suchenwirth, Johan Bengtsson, Mac Cody, G√ľnther, Andreas Sievers, and Michael Schlenker.

Bugs, Ideas, Feedback

This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category ctext of the Tklib Trackers. Please also report any ideas for enhancements you may have for either package and/or documentation.


re_syntax, text


syntax highlighting, text, widget


Copyright © George Peter Staplin