/*
* tkText.c --
*
* This module provides a big chunk of the implementation of
* multi-line editable text widgets for Tk. Among other things,
* it provides the Tcl command interfaces to text widgets and
* the display code. The B-tree representation of text is
* implemented elsewhere.
*
* Copyright (c) 1992-1994 The Regents of the University of California.
* Copyright (c) 1994-1996 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* SCCS: @(#) tkText.c 1.91 96/05/16 13:19:58
*/
#include "tkInt.h"
#include "tkDefault.h"
#ifdef MAC_TCL
#endif
#include "tkText.h"
/*
* Information used to parse text configuration options:
*/
(char *) NULL, 0, 0},
(char *) NULL, 0, 0},
"ExportSelection", DEF_TEXT_EXPORT_SELECTION,
(char *) NULL, 0, 0},
"HighlightBackground", DEF_TEXT_HIGHLIGHT_BG,
"HighlightThickness",
(char *) NULL, 0, 0}
};
/*
* Tk_Uid's used to represent text states:
*/
/*
* Boolean variable indicating whether or not special debugging code
* should be executed.
*/
int tkTextDebug = 0;
/*
* Forward declarations for procedures defined later in this file:
*/
char *index1String, char *index2String));
static void TextCmdDeletedProc _ANSI_ARGS_((
int what));
/*
*--------------------------------------------------------------
*
* Tk_TextCmd --
*
* This procedure is invoked to process the "text" Tcl command.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*--------------------------------------------------------------
*/
int
* interpreter. */
int argc; /* Number of arguments. */
char **argv; /* Argument strings. */
{
if (argc < 2) {
return TCL_ERROR;
}
/*
* Perform once-only initialization:
*/
if (tkTextNormalUid == NULL) {
}
/*
* Create the window.
*/
return TCL_ERROR;
}
textPtr->borderWidth = 0;
textPtr->highlightWidth = 0;
textPtr->abortSelections = 0;
textPtr->insertWidth = 0;
textPtr->insertBorderWidth = 0;
textPtr->insertOnTime = 0;
textPtr->insertOffTime = 0;
textPtr->numCurTags = 0;
/*
* Create the "sel" tag and the "current" and "insert" marks.
*/
(ClientData) textPtr);
return TCL_ERROR;
}
return TCL_OK;
}
/*
*--------------------------------------------------------------
*
* TextWidgetCmd --
*
* This procedure is invoked to process the Tcl command
* that corresponds to a text widget. See the user
* documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*--------------------------------------------------------------
*/
static int
int argc; /* Number of arguments. */
char **argv; /* Argument strings. */
{
int c;
if (argc < 2) {
return TCL_ERROR;
}
c = argv[1][0];
if (argc != 3) {
goto done;
}
goto done;
}
}
&& (length >= 2)) {
if (argc != 3) {
argv[0], " cget option\"",
(char *) NULL);
goto done;
}
&& (length >= 3)) {
char *p;
if (argc != 5) {
goto done;
}
!= TCL_OK)) {
goto done;
}
p = argv[3];
if (p[0] == '<') {
if ((p[1] == '=') && (p[2] == 0)) {
} else if (p[1] != 0) {
(char *) NULL);
goto done;
}
} else if (p[0] == '>') {
if ((p[1] == '=') && (p[2] == 0)) {
} else if (p[1] != 0) {
goto compareError;
}
} else if ((p[0] == '=') && (p[1] == '=') && (p[2] == 0)) {
} else if ((p[0] == '!') && (p[1] == '=') && (p[2] == 0)) {
} else {
goto compareError;
}
&& (length >= 3)) {
if (argc == 2) {
} else if (argc == 3) {
} else {
}
&& (length >= 3)) {
if (argc > 3) {
goto done;
}
if (argc == 2) {
} else {
goto done;
}
}
&& (length >= 3)) {
goto done;
}
}
&& (length >= 2)) {
if (argc != 3) {
goto done;
}
goto done;
}
== 0) {
}
goto done;
}
goto done;
}
if (argc == 3) {
!= TCL_OK) {
goto done;
}
goto done;
}
while (1) {
int last2;
break;
}
}
}
(char *) NULL);
}
}
&& (length >= 3)) {
if (argc != 3) {
argv[0], " index index\"",
(char *) NULL);
goto done;
}
goto done;
}
&& (length >= 3)) {
int i, j, numTags;
char **tagNames;
if (argc < 4) {
argv[0],
" insert index chars ?tagList chars tagList ...?\"",
(char *) NULL);
goto done;
}
goto done;
}
if (argc > (j+1)) {
&index2);
if (oldTagArrayPtr != NULL) {
for (i = 0; i < numTags; i++) {
}
ckfree((char *) oldTagArrayPtr);
}
!= TCL_OK) {
goto done;
}
for (i = 0; i < numTags; i++) {
}
}
}
}
&& (length >= 3)) {
&& (length >= 2)) {
} else {
"\": must be bbox, cget, compare, configure, debug, delete, ",
"dlineinfo, get, index, insert, mark, scan, search, see, ",
"tag, window, xview, or yview",
(char *) NULL);
}
done:
return result;
}
/*
*----------------------------------------------------------------------
*
* DestroyText --
*
* This procedure is invoked by Tcl_EventuallyFree or Tcl_Release
* to clean up the internal structure of a text at a safe time
* (when no-one is using it anymore).
*
* Results:
* None.
*
* Side effects:
* Everything associated with the text is freed up.
*
*----------------------------------------------------------------------
*/
static void
char *memPtr; /* Info about text widget. */
{
/*
* Free up all the stuff that requires special handling, then
* let Tk_FreeOptions handle all the standard option-related
* stuff. Special note: free up display-related information
* before deleting the B-tree, since display-related stuff
* may refer to stuff in the B-tree.
*/
}
}
}
}
}
/*
* NOTE: do NOT free up selBorder, selBdString, or selFgColorPtr:
* they are duplicates of information in the "sel" tag, which was
* freed up as part of deleting the tags above.
*/
}
/*
*----------------------------------------------------------------------
*
* ConfigureText --
*
* the Tk option database, in order to configure (or
* reconfigure) a text widget.
*
* Results:
* The return value is a standard Tcl result. If TCL_ERROR is
* returned, then interp->result contains an error message.
*
* Side effects:
* Configuration information, such as text string, colors, font,
* etc. get set for textPtr; old resources get freed, if there
* were any.
*
*----------------------------------------------------------------------
*/
static int
* not already have values for some fields. */
int argc; /* Number of valid entries in argv. */
char **argv; /* Arguments. */
int flags; /* Flags to pass to Tk_ConfigureWidget. */
{
int charHeight;
return TCL_ERROR;
}
/*
* A few other options also need special processing, such as parsing
* the geometry and setting the background from a 3-D border.
*/
"\": must be normal or disabled", (char *) NULL);
return TCL_ERROR;
}
"\": must be char, none, or word", (char *) NULL);
return TCL_ERROR;
}
/*
* Don't allow negative spacings.
*/
}
}
}
/*
* Parse tab stops.
*/
}
return TCL_ERROR;
}
}
/*
* Make sure that configuration options are properly mirrored
* between the widget record and the "sel" tags. NOTE: we don't
* have to free up information during the mirroring; old
* information was freed when it was replaced in the widget
* record.
*/
return TCL_ERROR;
}
}
}
}
}
/*
* Claim the selection if we've suddenly started exporting it and there
* are tagged characters.
*/
|| TkBTreeNextTag(&search)) {
(ClientData) textPtr);
}
}
/*
* Register the desired geometry for the window, and arrange for
* the window to be redisplayed.
*/
}
}
}
} else {
}
return TCL_OK;
}
/*
*--------------------------------------------------------------
*
* TextEventProc --
*
* This procedure is invoked by the Tk dispatcher on
* structure changes to a text. For texts with 3D
* borders, this procedure is also invoked for exposures.
*
* Results:
* None.
*
* Side effects:
* When the window gets deleted, internal structures get
* cleaned up. When it gets exposed, it is redisplayed.
*
*--------------------------------------------------------------
*/
static void
{
}
}
}
if (textPtr->insertOffTime != 0) {
(ClientData) textPtr);
}
} else {
}
if (textPtr->highlightWidth > 0) {
}
}
}
}
/*
*----------------------------------------------------------------------
*
* TextCmdDeletedProc --
*
* This procedure is invoked when a widget command is deleted. If
* the widget isn't already in the process of being destroyed,
* this command destroys it.
*
* Results:
* None.
*
* Side effects:
* The widget is destroyed.
*
*----------------------------------------------------------------------
*/
static void
{
/*
* This procedure could be invoked either because the window was
* destroyed and the command was then deleted (in which case tkwin
* is NULL) or because the command was deleted, and then this procedure
* destroys the widget.
*/
}
}
}
/*
*----------------------------------------------------------------------
*
* InsertChars --
*
* This procedure implements most of the functionality of the
* "insert" widget command.
*
* Results:
* None.
*
* Side effects:
* The characters in "string" get added to the text just before
* the character indicated by "indexPtr".
*
*----------------------------------------------------------------------
*/
static void
char *string; /* Null-terminated string containing new
* information to add to text. */
{
/*
* Don't allow insertions on the last (dummy) line of the text.
*/
lineIndex--;
}
/*
* Notify the display module that lines are about to change, then do
* the insertion. If the insertion occurs on the top line of the
* widget (textPtr->topIndex), then we have to recompute topIndex
* after the insertion, since the insertion could invalidate it.
*/
resetView = 1;
}
}
if (resetView) {
}
/*
* Invalidate any selection retrievals in progress.
*/
}
/*
*----------------------------------------------------------------------
*
* DeleteChars --
*
* This procedure implements most of the functionality of the
* "delete" widget command.
*
* Results:
* Returns a standard Tcl result, and leaves an error message
* in textPtr->interp if there is an error.
*
* Side effects:
* Characters get deleted from the text.
*
*----------------------------------------------------------------------
*/
static int
char *index1String; /* String describing location of first
* character to delete. */
char *index2String; /* String describing location of last
* character to delete. NULL means just
* delete the one character given by
* index1String. */
{
/*
* Parse the starting and stopping indices.
*/
!= TCL_OK) {
return TCL_ERROR;
}
if (index2String != NULL) {
!= TCL_OK) {
return TCL_ERROR;
}
} else {
}
/*
* Make sure there's really something to delete.
*/
return TCL_OK;
}
/*
* The code below is ugly, but it's needed to make sure there
* is always a dummy empty line at the end of the text. If the
* final newline of the file (just before the dummy line) is being
* deleted, then back up index to just before the newline. If
* there is a newline just before the first character being deleted,
* then back up the first index too, so that an even number of lines
* gets deleted. Furthermore, remove any tags that are present on
* the newline that isn't going to be deleted after all (this simulates
* deleting the newline and then adding a "clean" one back again).
*/
int arraySize, i;
line2--;
line1--;
}
for (i = 0; i < arraySize; i++) {
}
}
}
/*
* Tell the display what's about to happen so it can discard
* obsolete display information, then do the deletion. Also,
* if the deletion involves the top line on the screen, then
* we have to reset the view (the deletion will invalidate
* textPtr->topIndex). Compute what the new first character
* will be, then do the deletion, then reset the view.
*/
/*
* Deletion range straddles topIndex: use the beginning
* of the range as the new topIndex.
*/
resetView = 1;
/*
* Deletion range starts on top line but after topIndex.
* Use the current topIndex as the new one.
*/
resetView = 1;
}
/*
* Deletion range ends on top line but before topIndex.
* Figure out what will be the new character index for
* the character currently pointed to by topIndex.
*/
resetView = 1;
} else {
}
}
if (resetView) {
}
/*
* Invalidate any selection retrievals in progress.
*/
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* TextFetchSelection --
*
* This procedure is called back by Tk when the selection is
* requested by someone. It returns part or all of the selection
* in a buffer provided by the caller.
*
* Results:
* The return value is the number of non-NULL bytes stored
* at buffer. Buffer is filled (or partially filled) with a
* NULL-terminated string containing part or all of the selection,
* as given by offset and maxBytes.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
int offset; /* Offset within selection of first
* character to be returned. */
char *buffer; /* Location in which to place
* selection. */
int maxBytes; /* Maximum number of bytes to place
* at buffer, not including terminating
* NULL character. */
{
if (!textPtr->exportSelection) {
return -1;
}
/*
* Find the beginning of the next range of selected text. Note: if
* the selection is being retrieved in multiple pieces (offset != 0)
* and some modification has been made to the text that affects the
* selection then reject the selection request (make 'em start over
* again).
*/
if (offset == 0) {
textPtr->abortSelections = 0;
} else if (textPtr->abortSelections) {
return 0;
}
if (!TkBTreeNextTag(&search)) {
if (offset == 0) {
return -1;
} else {
return 0;
}
}
}
/*
* Each iteration through the outer loop below scans one selected range.
* Each iteration through the inner loop scans one segment in the
* selected range.
*/
count = 0;
while (1) {
/*
* Find the end of the current range of selected text.
*/
if (!TkBTreeNextTag(&search)) {
panic("TextFetchSelection couldn't find end of range");
}
/*
* Copy information from character segments into the buffer
* until either we run out of space in the buffer or we get
* to the end of this range of text.
*/
while (1) {
if (maxBytes == 0) {
goto done;
}
}
int leftInRange;
if (leftInRange < chunkSize) {
if (chunkSize <= 0) {
break;
}
}
}
}
}
/*
* Find the beginning of the next range of selected text.
*/
if (!TkBTreeNextTag(&search)) {
break;
}
}
done:
*buffer = 0;
return count;
}
/*
*----------------------------------------------------------------------
*
* TkTextLostSelection --
*
* This procedure is called back by Tk when the selection is
* grabbed away from a text widget.
*
* Results:
* None.
*
* Side effects:
* The "sel" tag is cleared from the window.
*
*----------------------------------------------------------------------
*/
void
{
if (!textPtr->exportSelection) {
return;
}
/*
* Just remove the "sel" tag from everything in the widget.
*/
}
/*
*----------------------------------------------------------------------
*
* TextBlinkProc --
*
* This procedure is called as a timer handler to blink the
* insertion cursor off and on.
*
* Results:
* None.
*
* Side effects:
* The cursor gets turned on or off, redisplay gets invoked,
* and this procedure reschedules itself.
*
*----------------------------------------------------------------------
*/
static void
{
return;
}
} else {
}
}
/*
*----------------------------------------------------------------------
*
* TextSearchCmd --
*
* This procedure is invoked to process the "search" widget command
* for text widgets. See the user documentation for details on what
* it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*----------------------------------------------------------------------
*/
static int
int argc; /* Number of arguments. */
char **argv; /* Argument strings. */
{
int patLength;
* prevent compiler warning. */
/*
* Parse switches and other arguments.
*/
exact = 1;
backwards = 0;
noCase = 0;
for (i = 2; i < argc; i++) {
if (arg[0] != '-') {
break;
}
if (length < 2) {
"\": must be -forward, -backward, -exact, -regexp, ",
"-nocase, -count, or --", (char *) NULL);
return TCL_ERROR;
}
c = arg[1];
backwards = 1;
if (i >= (argc-1)) {
return TCL_ERROR;
}
i++;
exact = 1;
backwards = 0;
noCase = 1;
exact = 0;
i++;
break;
} else {
goto badSwitch;
}
}
argv[0], " search ?switches? pattern index ?stopIndex?",
(char *) NULL);
return TCL_ERROR;
}
/*
* Convert the pattern to lower-case if we're supposed to ignore case.
*/
if (noCase) {
for (p = pattern; *p != 0; p++) {
}
}
}
return TCL_ERROR;
}
if (startingLine >= numLines) {
if (backwards) {
startingLine));
} else {
startingLine = 0;
startingChar = 0;
}
}
if (argsLeft == 1) {
return TCL_ERROR;
}
}
searchWholeText = 0;
} else {
stopLine = 0;
searchWholeText = 1;
}
/*
* Scan through all of the lines of the text circularly, starting
* at the given index.
*/
* warnings. */
if (exact) {
} else {
return TCL_ERROR;
}
}
/*
* Don't search the dummy last line of the text.
*/
goto nextLine;
}
/*
* Extract the text from the line. If we're doing regular
* expression matching, drop the newline from the line, so
* that "$" can be used to match the end of the line.
*/
continue;
}
}
if (!exact) {
}
/*
* If we're ignoring case, convert the line to lower case.
*/
if (noCase) {
for (p = Tcl_DStringValue(&line); *p != 0; p++) {
}
}
}
/*
* Check for matches within the current line. If so, and if we're
* searching backwards, repeat the search to find the last match
* in the line.
*/
matchChar = -1;
firstChar = 0;
if (lineNum == startingLine) {
int indexInDString;
/*
* The starting line is tricky: the first time we see it
* we check one part of the line, and the second pass through
* we check the other part of the line. We have to be very
* careful here because there could be embedded windows or
* other things that are not in the extracted line. Rescan
* the original line to compute the index in it of the first
* character.
*/
}
}
passes++;
/*
* Only use the last part of the line.
*/
goto nextLine;
}
} else {
/*
* Use only the first part of the line.
*/
}
}
do {
int thisLength;
if (exact) {
if (p == NULL) {
break;
}
i = p - startOfLine;
} else {
int match;
if (match < 0) {
goto done;
}
if (!match) {
break;
}
i = start - startOfLine;
}
if (i >= lastChar) {
break;
}
matchChar = i;
} while (backwards);
/*
* If we found a match then we're done. Make sure that
* the match occurred before the stopping index, if one was
* specified.
*/
if (matchChar >= 0) {
/*
* The index information returned by the regular expression
* parser only considers textual information: it doesn't
* account for embedded windows or any other non-textual info.
* Scan through the line's segments again to adjust both
* matchChar and matchCount.
*/
continue;
}
}
continue;
}
}
if (!searchWholeText) {
goto done;
}
goto done;
}
}
== NULL) {
goto done;
}
}
goto done;
}
/*
* Go to the next (or previous) line;
*/
if (backwards) {
lineNum--;
if (!searchWholeText) {
break;
}
} else if (lineNum < 0) {
}
} else {
lineNum++;
if (!searchWholeText) {
break;
}
lineNum = 0;
}
}
Tcl_DStringSetLength(&line, 0);
}
done:
if (noCase) {
}
return code;
}
/*
*----------------------------------------------------------------------
*
* TkTextGetTabs --
*
* Parses a string description of a set of tab stops.
*
* Results:
* The return value is a pointer to a malloc'ed structure holding
* parsed information about the tab stops. If an error occurred
* then the return value is NULL and an error message is left in
* interp->result.
*
* Side effects:
* Memory is allocated for the structure that is returned. It is
* up to the caller to free this structure when it is no longer
* needed.
*
*----------------------------------------------------------------------
*/
* used. */
char *string; /* Description of the tab stops. See
* the text manual entry for details. */
{
char **argv;
return NULL;
}
/*
* First find out how many entries we need to allocate in the
* tab array.
*/
count = 0;
for (i = 0; i < argc; i++) {
c = argv[i][0];
if ((c != 'l') && (c != 'r') && (c != 'c') && (c != 'n')) {
count++;
}
}
/*
* Parse the elements of the list one at a time to fill in the
* array.
*/
tabArrayPtr->numTabs = 0;
!= TCL_OK) {
goto error;
}
tabArrayPtr->numTabs++;
/*
* See if there is an explicit alignment in the next list
* element. Otherwise just use "left".
*/
if ((i+1) == argc) {
continue;
}
if (!isalpha(c)) {
continue;
}
i += 1;
} else {
argv[i], "\": must be left, right, center, or numeric",
(char *) NULL);
goto error;
}
}
return tabArrayPtr;
ckfree((char *) tabArrayPtr);
return NULL;
}
/*
*----------------------------------------------------------------------
*
* TextDumpCmd --
*
* Return information about the text, tags, marks, and embedded windows
* in a text widget. See the man page for the description of the
* text dump operation for all the details.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* Memory is allocated for the result, if needed (standard Tcl result
* side effects).
*
*----------------------------------------------------------------------
*/
static int
int argc; /* Number of arguments. */
char **argv; /* Argument strings. Someone else has already
* parsed this command enough to know that
* argv[1] is "dump". */
{
int arg;
break;
}
what = TK_DUMP_ALL;
what |= TK_DUMP_TEXT;
what |= TK_DUMP_TAG;
what |= TK_DUMP_MARK;
what |= TK_DUMP_WIN;
arg++;
Tcl_AppendResult(interp, "Usage: ", argv[0], " dump ?-all -text -mark -tag -window? ?-command script? index ?index2?", NULL);
return TCL_ERROR;
}
} else {
Tcl_AppendResult(interp, "Usage: ", argv[0], " dump ?-all -text -mark -tag -window? ?-command script? index ?index2?", NULL);
return TCL_ERROR;
}
}
Tcl_AppendResult(interp, "Usage: ", argv[0], " dump ?-all -text -mark -tag -window? ?-command script? index ?index2?", NULL);
return TCL_ERROR;
}
if (what == 0) {
what = TK_DUMP_ALL;
}
return TCL_ERROR;
}
arg++;
atEnd = 0;
} else {
return TCL_ERROR;
}
atEnd = 1;
}
}
return TCL_OK;
}
} else {
lineno++;
break;
}
}
}
/*
* Special case to get the leftovers hiding at the end mark.
*/
if (atEnd) {
}
return TCL_OK;
}
/*
* DumpLine
* Return information about a given text line from character
* position "start" up to, but not including, "end".
*
* Results:
* A standard Tcl result.
*
* Side effects:
* None, but see DumpSegment.
*/
static void
int what; /* bit flags to select segment types */
int lineno; /* Line number for indices dump */
char *command; /* Script to apply to the segment */
{
int offset;
/*
* Must loop through line looking at its segments.
* character
* toggleOn, toggleOff
* mark
* window
*/
}
}
} else if ((what & TK_DUMP_TAG) &&
} else if ((what & TK_DUMP_TAG) &&
} else if ((what & TK_DUMP_WIN) &&
char *pathname;
pathname = "";
} else {
}
}
}
}
}
/*
* DumpSegment
* Either append information about the current segment to the result,
* or make a script callback with that information as arguments.
*
* Results:
* None
*
* Side effects:
* Either evals the callback or appends elements to the result string.
*/
static int
char *key; /* Segment type key */
char *value; /* Segment value */
char *command; /* Script callback */
int lineno; /* Line number for indices dump */
int offset; /* Character position */
int what; /* Look for TK_DUMP_INDEX bit */
{
return TCL_OK;
} else {
char *list;
int result;
return result;
}
}