The Perl/Tk Text Widget Extended Methods
These methods are available only with the Perl/Tk Text widget and are documented nowhere else but in this tutorial. Another unique feature is the built-in Menu, activated by a <Button-3> click, which calls many of these methods for common text-related activities. The built-in Menu is shown in Figure 8-7.
Figure 8-7. Built-in Menu
To disable the default Menu, use $text->menu(undef). To supply your own Menu, first create it and then use $text->menu(my_menu).
Here, then, is the complete list of extended Text widget methods. These methods should be considered experimental, so use them with caution and expect that you may have to change your code in the future.
$text->adjustSelect- Moves the end point of selection and anchor point to the mouse pointer location.
$text->clipboardColumnCopy- Performs a rectangular copy of the currently selected text, with basic compensation for tab characters.
$text->clipboardColumnCut- Performs a rectangular cut of the currently selected text, with basic compensation for tab characters.
$text->clipboardColumnPaste- Performs a rectangular paste of the text in the clipboard. The upper-left corner is specified by the current position of the
insertmark, with basic compensation for tab characters. $text->deleteSelected- Deletes the currently selected text.
$text->DeleteTextTaggedWith(tag)- Deletes the text tagged with the
parameter.tag $text->deleteToEndofLine- Deletes from the
insertmark location to end of line. $text->FindAll(modecasepattern)- First removes any current selections, then performs a global text search, tagging all matches with the
seltag.can bemode-exactor-regexp, andcan becase-nocaseor-case.is an exact string to match ifpatternismode-exactor a regular expression if the matchismode-regexp. $text->FindAndReplaceAll(modecasefindreplace)- Works just like
FindAll, but additionally substitutes the matched text with the characters.replace $text->FindAndReplacePopUp- Creates a find-and-replace popup if one doesn't already exist. If there is text currently selected, pre-fills the "find" field with it.
$text->FindNext(directionmodecasepattern)- First removes any current selections, then performs a forward or reverse text search, tagging a match with the
seltag.can bedirection-forwardor-reverse., andmode,caseare as forpatternFindAll. $text->FindPopUp- Creates a find popup, if one doesn't already exist. If there is text currently selected, pre-fills the "find" field with it.
$text->FindSelectionNext- Gets the currently selected text and removes all selections. It then finds the next exact, case-sensitive string that matches in a forward direction, selects the new text, and makes it visible.
$text->FindSelectionPrevious- Gets the currently selected text and removes all selections. It then finds the next exact, case-sensitive string that matches in a reverse direction, selects the new text, and makes it visible.
$text->getSelected- Returns the currently selected text.
$text->GetTextTaggedWith(tag)- Returns the text tagged with the
parameter.tag $text->GotoLineNumber(line_number)- Sets the insert mark to
and displays the line.line_number $text->GotoLineNumberPopUp- Displays a popup, pre-filling it with selected numeric text, if any, or the line number from
GotoLineNumber, if any. $text->Insert(string)- Inserts
at the point of the insertion cursor. If there is a selection in the text, and it covers the point of the insertion cursor, delete the selection before inserting.string $text->InsertKeypress(character)- Inserts
at thecharacterinsertmark. If in overstrike mode, delete the character at theinsertmark first. $text->InsertSelection- Inserts the current selection at the
insertmark. $text->insertTab- Inserts a tab (
t) character at theinsertmark. $text->markExists(markname)- Returns true if
exists.markname $text->menu(?menu?)- Returns the Text widget's Menu reference if
is omitted, disables the Menu ifmenuismenuundef, or changes the Menu ifis another Menu reference.menu $text->openLine- Inserts a newline (
n) at theinsertmark. $text->OverstrikeMode(?boolean?)- Returns the overstrike mode if
is omitted or sets the overstrike mode toboolean. True means overstrike mode is enabled.boolean $text->PostPopupMenu(x, y)- Creates a popup Menu at the specified (x, y) pixel coordinates. The default Menu has File, Edit, Search, and View menu items that cascade to submenus for further commands. There is an implicit
<Button-3>binding to this method that posts the Menu over the cursor. $text->ResetAnchor- Sets the selection anchor to whichever end is farthest from the index argument.
$text->selectAll- Selects all the text in the widget.
$text->selectLine- Selects the line with the
insertmark. $text->selectWord- Selects the word with the
insertmark. $text->SetCursor(position)- Moves the
insertmark to.position $text->ToggleInsertMode- Toggles the current overstrike mode.
$text->unselectAll- Unselects all the text in the widget.
$text->WhatLineNumberPopUp- Creates a popup that displays the current line number of the
insertmark.
For a demonstration of these extended Text widget features, run the widget demonstration "Gedi master advanced text editor"[18] from the "User Contributed Demonstrations" section of the program.
[18] Yes, that's "Gedi," not "Jedi" (with apologies to George Lucas).