ctext(n)
π ctext(n) β Ctext, the text widget with highlighting support β¨
okay so first things first: this whole page is the ctext(n) man page, and it's basically the glow-up era of your fave text widget β Ctext a text widget with highlighting support. That's the whole vibe. Let's get into it π«Ά
β¨ SYNOPSIS β¨
like, the actual invocation tea β:
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 ?...?
(yes the ?3.3? has a question-mark question-mark around it, that means it's optional bestie, don't come for me π)
πΈ DESCRIPTION πΈ
so the ctext package hands you the ctext widget β and it's literally an enhanced text widget with support for configurable syntax highlighting plus some extra commands. main character energy fr.
Ctext overloads the text widget and adds new commands to it, named highlight, copy, paste, cut, append, and edit. It also gives you several commands that let you define classes β and each class corresponds to a tag in the widget. iconic behavior honestly π
π COMMANDS π
ctext pathName ?options?β creates and configures a ctext widget. that's it, that's the whole job, and it does it beautifully.
π¦ HIGHLIGHTING π¦
Highlighting is controlled with text widget tags, which get called highlight classes. A class is a tag name, so you can configure it exactly like any text widget tag. There are four types of highlight classes supported. All highlight classes are automatically used by the highlight method of the widget β no cap, they just work π
::ctext::addHighlightClass pathName class color keywordlistβ adds a highlighting classclassto the ctext widgetpathName. The highlighting gets done with the colorcolor. All words in thekeywordlistwill be highlighted. slay.
# highlight some tcl keywords
::ctext::addHighlightClass .t tclkeywords red [list set info interp uplevel upvar]
::ctext::addHighlightClassWithOnlyCharStart pathName class color charβ adds a highlighting classclassto the ctext widgetpathName. The highlighting gets done with the colorcolor. All words starting withcharwill be highlighted.
::ctext::addHighlightClassWithOnlyCharStart .t vars blue \$
::ctext::addHighlightClassForSpecialChars pathName class color charstringβ adds a highlighting classclassto the ctext widgetpathName. The highlighting gets done with the colorcolor. All chars incharstringwill be highlighted. π::ctext::addHighlightClassForRegexp pathName class color patternβ adds a highlighting classclassto the ctext widgetpathName. The highlighting gets done with the colorcolor. All text parts matching the regexppatternwill be highlighted. regex girlies rise π::ctext::clearHighlightClasses pathNameβ removes all highlight classes from the widgetpathName. total reset, fresh start, we love a clean slate β¨::ctext::getHighlightClasses pathNameβ lists all highlight classes for the widgetpathName. show me what you're working with π::ctext::deleteHighlightClass pathName classβ deletes the highlight classclassfrom the widgetpathName. bye bestie π¦::ctext::enableComments enableβ enables C comment highlighting. The class for c-style comments is_cComment. Heads up: the C comment highlighting is disabled by default. lowkey sneaky of it tbh.::ctext::disableComments enableβ disables C comment highlighting. that's the whole thing π
β WIDGET COMMANDS β
Every ctext widget you create with the command above supports all of the following commands and options in addition to the standard text widget commands and options. so it's the standard widget plus β¨extraβ¨, iykyk.
pathName highlight startIndex endIndexβ highlights the text betweenstartIndexandendIndex. periodt.pathName fastdelete index1 ?index2?β deletes a text range without updating the highlighting. The arguments are identical to thepathName deletecommand inherited from the standard text widget. fast and slightly feral fr.pathName fastinsertβ inserts text without updating the highlighting. The arguments are identical to thepathName insertcommand inherited from the standard text widget.pathName copyβ callstk_textCopyfor the ctext instance.pathName cutβ callstk_textCutfor the ctext instance.pathName pasteβ callstk_textPastefor the ctext instance.pathName appendβ appends the current selection to the clipboard. cute little clipboard goblin behavior πpathName configure option value ?...?β sets the options for the ctext widget. Each option name must be followed by the new value. no skipping, no shortcuts π
π§ WIDGET OPTIONS π§
so every one of these has a Command-Line Switch (the one you actually type), plus a Database Name and a Database Class slot β both of which are listed as empty on this page, so like, nothing to report there, they're just vibing as blank entries π
-linemapβ creates (-linemap 1) or deletes (-linemap 0) a line number list on the left of the widget. The default is to have a linemap displayed. very demure, very mindful π-linemapfgβ changes the foreground of the linemap. The default is the same color as the main text widget.-linemapbgβ changes the background of the linemap. The default is the same color as the main text widget.-linemap_select_fgβ changes the selected line foreground. The default is black. π€-linemap_select_bgβ changes the selected line background. The default is yellow. π-linemap_mark_commandβ calls a procedure or command with the pathName of the ctext window, thetype(which is eithermarkedorunmarked), and finally the line number selected. The proc prototype is:
proc linemark_cmd {win type line}.
also see ctext_test_interactive.tcl π
-highlightβ takes a boolean value which defines whether or not to highlight text which is inserted or deleted. The default is1. turned on and thriving β¨-linemap_markableβ takes a boolean value which specifies whether or not lines in the linemap are markable with the mouse. The default is1. mouse girlies, this one's for you π±οΈ
π₯ EXAMPLE π₯
here's the full thing, copy it, run it, live your truth:
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]}
}
main
Further examples are in the source package for ctext. go snoop, it's fine π«Ά
π THANKS π
shoutout to these absolute legends: Kevin Kenny, Neil Madden, Jeffrey Hobbs, Richard Suchenwirth, Johan Bengtsson, Mac Cody, GΓΌnther, Andreas Sievers, and Michael Schlenker. kings, all of them π
π BUGS, IDEAS, FEEDBACK π
okay so real talk: this document, and the package it describes, will undoubtedly contain bugs and other problems. not me manifesting it, it's literally what it says π Please report such in the category ctext of the Tklib Trackers [http://core.tcl.tk/tklib/reportlist]. Please also report any ideas for enhancements you may have for either package and/or documentation β they want to hear it fr fr π
π SEE ALSO π
re_syntax, text β go read those too, be well-rounded.
β KEYWORDS β
syntax highlighting, text, widget
π COPYRIGHT π
Copyright (c) George Peter Staplin <GeorgePS@XMission.com>
okay that's the whole page bestie, every flag and every word, nothing dropped π β¨
and lastly, the cute little sign-off from the bottom of the page: this is from tklib, version 3.3, under ctext(n). slay, go highlight something πΈπ¦
π¦ where this page comes from
- alpine 3.24: tk-lib-doc 0.7-r0 Β· TCL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms