ctext(3tk)
β¨ ctext β the text widget that's literally so extra (and we love her for it) β¨
okay bestie so this one is from the ctext manual page in section 3tk of tklib β it's the glow-up of your regular text widget, and honestly? she ate. π
π¦ NAME π¦
ctext β Ctext, a text widget with highlighting support. that's it, that's the name, no cap. π
πΈ SYNOPSIS πΈ
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 ?...?
like look at that whole list, she's serving range π β¨
π DESCRIPTION π
the ctext package hands you the ctext widget, which is an enhanced text widget with support for configurable syntax highlighting plus a couple of extra commands. it's giving main character energy for real. π₯
Ctext overloads the text widget and provides new commands, named highlight, copy, paste, cut, append, and edit. it also provides several commands that let you define classes. each class corresponds to a tag in the widget. π
π COMMANDS π
ctext pathName ?options?β creates and configures a ctext widget. that's the whole vibe right there. π«Ά
π HIGHLIGHTING π
okay so this is the part that's genuinely iconic. highlighting is controlled with text widget tags, and those tags are called highlight classes. a 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. iykyk. β¨
::ctext::addHighlightClass pathName class color keywordlistβ adds a highlighting classclassto the ctext widgetpathName. the highlighting is done with the colorcolor. all words in thekeywordlistget highlighted. obsessed.
# 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 is done with the colorcolor. every word that starts withchargets highlighted. lowkey so useful. π
::ctext::addHighlightClassWithOnlyCharStart .t vars blue \$
::ctext::addHighlightClassForSpecialChars pathName class color charstringβ adds a highlighting classclassto the ctext widgetpathName. the highlighting is done with the colorcolor. every char incharstringgets highlighted. β¨::ctext::addHighlightClassForRegexp pathName class color patternβ adds a highlighting classclassto the ctext widgetpathName. the highlighting is done with the colorcolor. every text part that matches the regexppatterngets highlighted. regex girlies rise π§::ctext::clearHighlightClasses pathNameβ removes all highlight classes from the widgetpathName. total reset, fresh start, new era. π::ctext::getHighlightClasses pathNameβ lists all highlight classes for the widgetpathName. show me what you got. π::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. the C comment highlighting is disabled by default. π::ctext::disableComments enableβ disables C comment highlighting.
π WIDGET COMMANDS π
every ctext widget you create with the command up above supports these commands and options on top of the standard text widget commands and options. she does everything the regular one does, just with more flair. β¨
pathName highlight startIndex endIndexβ highlights the text betweenstartIndexandendIndex. π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.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.pathName configure option value ?...?β sets the options for the ctext widget. each option name must be followed by the new value. one option, one value, no skipping. periodt. π
π WIDGET OPTIONS π
heads up: every one of these has a Database Name and a Database Class slot, and both of them are left blank in this doc β so it's just the command-line switch doing the talking. π
- Command-Line Switch:
-linemap- Database Name: (blank)
- Database Class: (blank)
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. β¨
- Command-Line Switch:
-linemapfg- Database Name: (blank)
- Database Class: (blank)
changes the foreground of the linemap. the default is the same color as the main text widget. π¨
- Command-Line Switch:
-linemapbg- Database Name: (blank)
- Database Class: (blank)
changes the background of the linemap. the default is the same color as the main text widget. πΈ
- Command-Line Switch:
-linemap_select_fg- Database Name: (blank)
- Database Class: (blank)
changes the selected line foreground. the default is black. π€
- Command-Line Switch:
-linemap_select_bg- Database Name: (blank)
- Database Class: (blank)
changes the selected line background. the default is yellow. π
- Command-Line Switch:
-linemap_mark_command- Database Name: (blank)
- Database Class: (blank)
calls a procedure or command with the pathName of the ctext window, the type β which is either marked or unmarked β and finally the line number selected. the proc prototype is:
proc linemark_cmd {win type line}.
see also ctext_test_interactive.tcl. π
- Command-Line Switch:
-highlight- Database Name: (blank)
- Database Class: (blank)
takes a boolean value which defines whether or not to highlight text which is inserted or deleted. the default is 1. π₯
- Command-Line Switch:
-linemap_markable- Database Name: (blank)
- Database Class: (blank)
takes a boolean value which specifies whether or not lines in the linemap are markable with the mouse. the default is 1. π±οΈ
β EXAMPLE β
okay this is the whole thing, and yes you're gonna copy it, no shame in that, we all do it:
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 dig, bestie π
π«Ά THANKS π«Ά
Kevin Kenny, Neil Madden, Jeffrey Hobbs, Richard Suchenwirth, Johan Bengtsson, Mac Cody, GΓΌnther, Andreas Sievers, and Michael Schlenker.
the way these legends carried this whole thing, we stan ππ
π 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 [http://core.tcl.tk/tklib/reportlist]. please also report any ideas for enhancements you may have for either package and/or documentation. don't be shy, the maintainers wanna hear it fr fr. π
π SEE ALSO π
re_syntax, text
π KEYWORDS π
syntax highlighting, text, widget
π COPYRIGHT π
Copyright (c) George Peter Staplin <GeorgePS@XMission.com>
and that's the whole page, bestie β this one's tklib version 3.3, dated right alongside ctext(3tk), so if you see 3.3 floating around, that's your girl. go highlight something gorgeous. β¨ππ
π¦
π¦ where this page comes from
- debian trixie: tklib 0.9-1
- ubuntu questing: tklib 0.9-1
- ubuntu resolute: tklib 0.9-1build1
- ubuntu stonking: tklib 0.9-1build1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms