ctext(n)

πŸ“¦
version 0.7 Β· 3 versions
, in alpine 3.24

πŸ’… 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 class class to the ctext widget pathName. The highlighting gets done with the color color. All words in the keywordlist will 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 class class to the ctext widget pathName. The highlighting gets done with the color color. All words starting with char will be highlighted.
::ctext::addHighlightClassWithOnlyCharStart .t vars blue \$
  • ::ctext::addHighlightClassForSpecialChars pathName class color charstring β€” adds a highlighting class class to the ctext widget pathName. The highlighting gets done with the color color. All chars in charstring will be highlighted. πŸ’…
  • ::ctext::addHighlightClassForRegexp pathName class color pattern β€” adds a highlighting class class to the ctext widget pathName. The highlighting gets done with the color color. All text parts matching the regexp pattern will be highlighted. regex girlies rise 🌈
  • ::ctext::clearHighlightClasses pathName β€” removes all highlight classes from the widget pathName. total reset, fresh start, we love a clean slate ✨
  • ::ctext::getHighlightClasses pathName β€” lists all highlight classes for the widget pathName. show me what you're working with πŸ‘€
  • ::ctext::deleteHighlightClass pathName class β€” deletes the highlight class class from the widget pathName. 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 between startIndex and endIndex. periodt.
  • pathName fastdelete index1 ?index2? β€” deletes a text range without updating the highlighting. The arguments are identical to the pathName delete command inherited from the standard text widget. fast and slightly feral fr.
  • pathName fastinsert β€” inserts text without updating the highlighting. The arguments are identical to the pathName insert command inherited from the standard text widget.
  • pathName copy β€” calls tk_textCopy for the ctext instance.
  • pathName cut β€” calls tk_textCut for the ctext instance.
  • pathName paste β€” calls tk_textPaste for 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, the type (which is either marked or unmarked), 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 is 1. 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 is 1. 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 (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