home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Partner Applications.iso / SunLabs / tclTK / src / tk4.0 / tkTextTag.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-25  |  39.1 KB  |  1,319 lines

  1. /* 
  2.  * tkTextTag.c --
  3.  *
  4.  *    This module implements the "tag" subcommand of the widget command
  5.  *    for text widgets, plus most of the other high-level functions
  6.  *    related to tags.
  7.  *
  8.  * Copyright (c) 1992-1994 The Regents of the University of California.
  9.  * Copyright (c) 1994-1995 Sun Microsystems, Inc.
  10.  *
  11.  * See the file "license.terms" for information on usage and redistribution
  12.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  13.  */
  14.  
  15. static char sccsid[] = "@(#) tkTextTag.c 1.28 95/05/25 10:36:17";
  16.  
  17. #include "default.h"
  18. #include "tkPort.h"
  19. #include "tk.h"
  20. #include "tkText.h"
  21.  
  22. /*
  23.  * Information used for parsing tag configuration information:
  24.  */
  25.  
  26. static Tk_ConfigSpec tagConfigSpecs[] = {
  27.     {TK_CONFIG_BORDER, "-background", (char *) NULL, (char *) NULL,
  28.     (char *) NULL, Tk_Offset(TkTextTag, border), TK_CONFIG_NULL_OK},
  29.     {TK_CONFIG_BITMAP, "-bgstipple", (char *) NULL, (char *) NULL,
  30.     (char *) NULL, Tk_Offset(TkTextTag, bgStipple), TK_CONFIG_NULL_OK},
  31.     {TK_CONFIG_STRING, "-borderwidth", (char *) NULL, (char *) NULL,
  32.     "0", Tk_Offset(TkTextTag, bdString),
  33.         TK_CONFIG_DONT_SET_DEFAULT|TK_CONFIG_NULL_OK},
  34.     {TK_CONFIG_BITMAP, "-fgstipple", (char *) NULL, (char *) NULL,
  35.     (char *) NULL, Tk_Offset(TkTextTag, fgStipple), TK_CONFIG_NULL_OK},
  36.     {TK_CONFIG_FONT, "-font", (char *) NULL, (char *) NULL,
  37.     (char *) NULL, Tk_Offset(TkTextTag, fontPtr), TK_CONFIG_NULL_OK},
  38.     {TK_CONFIG_COLOR, "-foreground", (char *) NULL, (char *) NULL,
  39.     (char *) NULL, Tk_Offset(TkTextTag, fgColor), TK_CONFIG_NULL_OK},
  40.     {TK_CONFIG_STRING, "-justify", (char *) NULL, (char *) NULL,
  41.     (char *) NULL, Tk_Offset(TkTextTag, justifyString), TK_CONFIG_NULL_OK},
  42.     {TK_CONFIG_STRING, "-lmargin1", (char *) NULL, (char *) NULL,
  43.     (char *) NULL, Tk_Offset(TkTextTag, lMargin1String), TK_CONFIG_NULL_OK},
  44.     {TK_CONFIG_STRING, "-lmargin2", (char *) NULL, (char *) NULL,
  45.     (char *) NULL, Tk_Offset(TkTextTag, lMargin2String), TK_CONFIG_NULL_OK},
  46.     {TK_CONFIG_STRING, "-offset", (char *) NULL, (char *) NULL,
  47.     (char *) NULL, Tk_Offset(TkTextTag, offsetString), TK_CONFIG_NULL_OK},
  48.     {TK_CONFIG_STRING, "-overstrike", (char *) NULL, (char *) NULL,
  49.     (char *) NULL, Tk_Offset(TkTextTag, overstrikeString),
  50.     TK_CONFIG_NULL_OK},
  51.     {TK_CONFIG_STRING, "-relief", (char *) NULL, (char *) NULL,
  52.     (char *) NULL, Tk_Offset(TkTextTag, reliefString), TK_CONFIG_NULL_OK},
  53.     {TK_CONFIG_STRING, "-rmargin", (char *) NULL, (char *) NULL,
  54.     (char *) NULL, Tk_Offset(TkTextTag, rMarginString), TK_CONFIG_NULL_OK},
  55.     {TK_CONFIG_STRING, "-spacing1", (char *) NULL, (char *) NULL,
  56.     (char *) NULL, Tk_Offset(TkTextTag, spacing1String), TK_CONFIG_NULL_OK},
  57.     {TK_CONFIG_STRING, "-spacing2", (char *) NULL, (char *) NULL,
  58.     (char *) NULL, Tk_Offset(TkTextTag, spacing2String), TK_CONFIG_NULL_OK},
  59.     {TK_CONFIG_STRING, "-spacing3", (char *) NULL, (char *) NULL,
  60.     (char *) NULL, Tk_Offset(TkTextTag, spacing3String), TK_CONFIG_NULL_OK},
  61.     {TK_CONFIG_STRING, "-tabs", (char *) NULL, (char *) NULL,
  62.     (char *) NULL, Tk_Offset(TkTextTag, tabString), TK_CONFIG_NULL_OK},
  63.     {TK_CONFIG_STRING, "-underline", (char *) NULL, (char *) NULL,
  64.     (char *) NULL, Tk_Offset(TkTextTag, underlineString),
  65.     TK_CONFIG_NULL_OK},
  66.     {TK_CONFIG_UID, "-wrap", (char *) NULL, (char *) NULL,
  67.     (char *) NULL, Tk_Offset(TkTextTag, wrapMode),
  68.     TK_CONFIG_NULL_OK},
  69.     {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
  70.     (char *) NULL, 0, 0}
  71. };
  72.  
  73. /*
  74.  * Forward declarations for procedures defined later in this file:
  75.  */
  76.  
  77. static void        ChangeTagPriority _ANSI_ARGS_((TkText *textPtr,
  78.                 TkTextTag *tagPtr, int prio));
  79. static TkTextTag *    FindTag _ANSI_ARGS_((Tcl_Interp *interp,
  80.                 TkText *textPtr, char *tagName));
  81. static void        SortTags _ANSI_ARGS_((int numTags,
  82.                 TkTextTag **tagArrayPtr));
  83. static int        TagSortProc _ANSI_ARGS_((CONST VOID *first,
  84.                 CONST VOID *second));
  85.  
  86. /*
  87.  *--------------------------------------------------------------
  88.  *
  89.  * TkTextTagCmd --
  90.  *
  91.  *    This procedure is invoked to process the "tag" options of
  92.  *    the widget command for text widgets. See the user documentation
  93.  *    for details on what it does.
  94.  *
  95.  * Results:
  96.  *    A standard Tcl result.
  97.  *
  98.  * Side effects:
  99.  *    See the user documentation.
  100.  *
  101.  *--------------------------------------------------------------
  102.  */
  103.  
  104. int
  105. TkTextTagCmd(textPtr, interp, argc, argv)
  106.     register TkText *textPtr;    /* Information about text widget. */
  107.     Tcl_Interp *interp;        /* Current interpreter. */
  108.     int argc;            /* Number of arguments. */
  109.     char **argv;        /* Argument strings.  Someone else has already
  110.                  * parsed this command enough to know that
  111.                  * argv[1] is "tag". */
  112. {
  113.     int c, i, addTag;
  114.     size_t length;
  115.     char *fullOption;
  116.     register TkTextTag *tagPtr;
  117.     TkTextIndex first, last, index1, index2;
  118.  
  119.     if (argc < 3) {
  120.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  121.         argv[0], " tag option ?arg arg ...?\"", (char *) NULL);
  122.     return TCL_ERROR;
  123.     }
  124.     c = argv[2][0];
  125.     length = strlen(argv[2]);
  126.     if ((c == 'a') && (strncmp(argv[2], "add", length) == 0)) {
  127.     fullOption = "add";
  128.     addTag = 1;
  129.  
  130.     addAndRemove:
  131.     if (argc < 5) {
  132.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  133.             argv[0], " tag ", fullOption,
  134.             " tagName index1 ?index2 index1 index2 ...?\"",
  135.             (char *) NULL);
  136.         return TCL_ERROR;
  137.     }
  138.     tagPtr = TkTextCreateTag(textPtr, argv[3]);
  139.     for (i = 4; i < argc; i += 2) {
  140.         if (TkTextGetIndex(interp, textPtr, argv[i], &index1) != TCL_OK) {
  141.         return TCL_ERROR;
  142.         }
  143.         if (argc > (i+1)) {
  144.         if (TkTextGetIndex(interp, textPtr, argv[i+1], &index2)
  145.             != TCL_OK) {
  146.             return TCL_ERROR;
  147.         }
  148.         if (TkTextIndexCmp(&index1, &index2) >= 0) {
  149.             return TCL_OK;
  150.         }
  151.         } else {
  152.         index2 = index1;
  153.         TkTextIndexForwChars(&index2, 1, &index2);
  154.         }
  155.     
  156.         if (tagPtr->affectsDisplay) {
  157.         TkTextRedrawTag(textPtr, &index1, &index2, tagPtr, !addTag);
  158.         } else {
  159.         /*
  160.          * Still need to trigger enter/leave events on tags that
  161.          * have changed.
  162.          */
  163.     
  164.         TkTextEventuallyRepick(textPtr);
  165.         }
  166.         TkBTreeTag(&index1, &index2, tagPtr, addTag);
  167.     
  168.         /*
  169.          * If the tag is "sel" then grab the selection if we're supposed
  170.          * to export it and don't already have it.  Also, invalidate
  171.          * partially-completed selection retrievals.
  172.          */
  173.     
  174.         if (tagPtr == textPtr->selTagPtr) {
  175.         if (addTag && textPtr->exportSelection
  176.             && !(textPtr->flags & GOT_SELECTION)) {
  177.             Tk_OwnSelection(textPtr->tkwin, XA_PRIMARY,
  178.                 TkTextLostSelection, (ClientData) textPtr);
  179.             textPtr->flags |= GOT_SELECTION;
  180.         }
  181.         textPtr->abortSelections = 1;
  182.         }
  183.     }
  184.     } else if ((c == 'b') && (strncmp(argv[2], "bind", length) == 0)) {
  185.     if ((argc < 4) || (argc > 6)) {
  186.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  187.             argv[0], " tag bind tagName ?sequence? ?command?\"",
  188.             (char *) NULL);
  189.         return TCL_ERROR;
  190.     }
  191.     tagPtr = TkTextCreateTag(textPtr, argv[3]);
  192.  
  193.     /*
  194.      * Make a binding table if the widget doesn't already have
  195.      * one.
  196.      */
  197.  
  198.     if (textPtr->bindingTable == NULL) {
  199.         textPtr->bindingTable = Tk_CreateBindingTable(interp);
  200.     }
  201.  
  202.     if (argc == 6) {
  203.         int append = 0;
  204.         unsigned long mask;
  205.  
  206.         if (argv[5][0] == 0) {
  207.         return Tk_DeleteBinding(interp, textPtr->bindingTable,
  208.             (ClientData) tagPtr, argv[4]);
  209.         }
  210.         if (argv[5][0] == '+') {
  211.         argv[5]++;
  212.         append = 1;
  213.         }
  214.         mask = Tk_CreateBinding(interp, textPtr->bindingTable,
  215.             (ClientData) tagPtr, argv[4], argv[5], append);
  216.         if (mask == 0) {
  217.         return TCL_ERROR;
  218.         }
  219.         if (mask & (unsigned) ~(ButtonMotionMask|Button1MotionMask
  220.             |Button2MotionMask|Button3MotionMask|Button4MotionMask
  221.             |Button5MotionMask|ButtonPressMask|ButtonReleaseMask
  222.             |EnterWindowMask|LeaveWindowMask|KeyPressMask
  223.             |KeyReleaseMask|PointerMotionMask)) {
  224.         Tk_DeleteBinding(interp, textPtr->bindingTable,
  225.             (ClientData) tagPtr, argv[4]);
  226.         Tcl_ResetResult(interp);
  227.         Tcl_AppendResult(interp, "requested illegal events; ",
  228.             "only key, button, motion, and enter/leave ",
  229.             "events may be used", (char *) NULL);
  230.         return TCL_ERROR;
  231.         }
  232.     } else if (argc == 5) {
  233.         char *command;
  234.     
  235.         command = Tk_GetBinding(interp, textPtr->bindingTable,
  236.             (ClientData) tagPtr, argv[4]);
  237.         if (command == NULL) {
  238.         return TCL_ERROR;
  239.         }
  240.         interp->result = command;
  241.     } else {
  242.         Tk_GetAllBindings(interp, textPtr->bindingTable,
  243.             (ClientData) tagPtr);
  244.     }
  245.     } else if ((c == 'c') && (strncmp(argv[2], "cget", length) == 0)
  246.         && (length >= 2)) {
  247.     if (argc != 5) {
  248.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  249.             argv[0], " tag cget tagName option\"",
  250.             (char *) NULL);
  251.         return TCL_ERROR;
  252.     }
  253.     tagPtr = FindTag(interp, textPtr, argv[3]);
  254.     if (tagPtr == NULL) {
  255.         return TCL_ERROR;
  256.     }
  257.     return Tk_ConfigureValue(interp, textPtr->tkwin, tagConfigSpecs,
  258.         (char *) tagPtr, argv[4], 0);
  259.     } else if ((c == 'c') && (strncmp(argv[2], "configure", length) == 0)
  260.         && (length >= 2)) {
  261.     if (argc < 4) {
  262.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  263.             argv[0], " tag configure tagName ?option? ?value? ",
  264.             "?option value ...?\"", (char *) NULL);
  265.         return TCL_ERROR;
  266.     }
  267.     tagPtr = TkTextCreateTag(textPtr, argv[3]);
  268.     if (argc == 4) {
  269.         return Tk_ConfigureInfo(interp, textPtr->tkwin, tagConfigSpecs,
  270.             (char *) tagPtr, (char *) NULL, 0);
  271.     } else if (argc == 5) {
  272.         return Tk_ConfigureInfo(interp, textPtr->tkwin, tagConfigSpecs,
  273.             (char *) tagPtr, argv[4], 0);
  274.     } else {
  275.         int result;
  276.  
  277.         result = Tk_ConfigureWidget(interp, textPtr->tkwin, tagConfigSpecs,
  278.             argc-4, argv+4, (char *) tagPtr, 0);
  279.         /*
  280.          * Some of the configuration options, like -underline
  281.          * and -justify, require additional translation (this is
  282.          * needed because we need to distinguish a particular value
  283.          * of an option from "unspecified").
  284.          */
  285.  
  286.         if (tagPtr->bdString != NULL) {
  287.         if (Tk_GetPixels(interp, textPtr->tkwin, tagPtr->bdString,
  288.             &tagPtr->borderWidth) != TCL_OK) {
  289.             return TCL_ERROR;
  290.         }
  291.         if (tagPtr->borderWidth < 0) {
  292.             tagPtr->borderWidth = 0;
  293.         }
  294.         }
  295.         if (tagPtr->reliefString != NULL) {
  296.         if (Tk_GetRelief(interp, tagPtr->reliefString,
  297.             &tagPtr->relief) != TCL_OK) {
  298.             return TCL_ERROR;
  299.         }
  300.         }
  301.         if (tagPtr->justifyString != NULL) {
  302.         if (Tk_GetJustify(interp, tagPtr->justifyString,
  303.             &tagPtr->justify) != TCL_OK) {
  304.             return TCL_ERROR;
  305.         }
  306.         }
  307.         if (tagPtr->lMargin1String != NULL) {
  308.         if (Tk_GetPixels(interp, textPtr->tkwin,
  309.             tagPtr->lMargin1String, &tagPtr->lMargin1) != TCL_OK) {
  310.             return TCL_ERROR;
  311.         }
  312.         }
  313.         if (tagPtr->lMargin2String != NULL) {
  314.         if (Tk_GetPixels(interp, textPtr->tkwin,
  315.             tagPtr->lMargin2String, &tagPtr->lMargin2) != TCL_OK) {
  316.             return TCL_ERROR;
  317.         }
  318.         }
  319.         if (tagPtr->offsetString != NULL) {
  320.         if (Tk_GetPixels(interp, textPtr->tkwin, tagPtr->offsetString,
  321.             &tagPtr->offset) != TCL_OK) {
  322.             return TCL_ERROR;
  323.         }
  324.         }
  325.         if (tagPtr->overstrikeString != NULL) {
  326.         if (Tcl_GetBoolean(interp, tagPtr->overstrikeString,
  327.             &tagPtr->overstrike) != TCL_OK) {
  328.             return TCL_ERROR;
  329.         }
  330.         }
  331.         if (tagPtr->rMarginString != NULL) {
  332.         if (Tk_GetPixels(interp, textPtr->tkwin,
  333.             tagPtr->rMarginString, &tagPtr->rMargin) != TCL_OK) {
  334.             return TCL_ERROR;
  335.         }
  336.         }
  337.         if (tagPtr->spacing1String != NULL) {
  338.         if (Tk_GetPixels(interp, textPtr->tkwin,
  339.             tagPtr->spacing1String, &tagPtr->spacing1) != TCL_OK) {
  340.             return TCL_ERROR;
  341.         }
  342.         if (tagPtr->spacing1 < 0) {
  343.             tagPtr->spacing1 = 0;
  344.         }
  345.         }
  346.         if (tagPtr->spacing2String != NULL) {
  347.         if (Tk_GetPixels(interp, textPtr->tkwin,
  348.             tagPtr->spacing2String, &tagPtr->spacing2) != TCL_OK) {
  349.             return TCL_ERROR;
  350.         }
  351.         if (tagPtr->spacing2 < 0) {
  352.             tagPtr->spacing2 = 0;
  353.         }
  354.         }
  355.         if (tagPtr->spacing3String != NULL) {
  356.         if (Tk_GetPixels(interp, textPtr->tkwin,
  357.             tagPtr->spacing3String, &tagPtr->spacing3) != TCL_OK) {
  358.             return TCL_ERROR;
  359.         }
  360.         if (tagPtr->spacing3 < 0) {
  361.             tagPtr->spacing3 = 0;
  362.         }
  363.         }
  364.         if (tagPtr->tabArrayPtr != NULL) {
  365.         ckfree((char *) tagPtr->tabArrayPtr);
  366.         tagPtr->tabArrayPtr = NULL;
  367.         }
  368.         if (tagPtr->tabString != NULL) {
  369.         tagPtr->tabArrayPtr = TkTextGetTabs(interp, textPtr->tkwin,
  370.             tagPtr->tabString);
  371.         if (tagPtr->tabArrayPtr == NULL) {
  372.             return TCL_ERROR;
  373.         }
  374.         }
  375.         if (tagPtr->underlineString != NULL) {
  376.         if (Tcl_GetBoolean(interp, tagPtr->underlineString,
  377.             &tagPtr->underline) != TCL_OK) {
  378.             return TCL_ERROR;
  379.         }
  380.         }
  381.         if ((tagPtr->wrapMode != NULL)
  382.             && (tagPtr->wrapMode != tkTextCharUid)
  383.             && (tagPtr->wrapMode != tkTextNoneUid)
  384.             && (tagPtr->wrapMode != tkTextWordUid)) {
  385.         Tcl_AppendResult(interp, "bad wrap mode \"", tagPtr->wrapMode,
  386.             "\":  must be char, none, or word", (char *) NULL);
  387.         tagPtr->wrapMode = NULL;
  388.         return TCL_ERROR;
  389.         }
  390.  
  391.         /*
  392.          * If the "sel" tag was changed, be sure to mirror information
  393.          * from the tag back into the text widget record.   NOTE: we
  394.          * don't have to free up information in the widget record
  395.          * before overwriting it, because it was mirrored in the tag
  396.          * and hence freed when the tag field was overwritten.
  397.          */
  398.  
  399.         if (tagPtr == textPtr->selTagPtr) {
  400.         textPtr->selBorder = tagPtr->border;
  401.         textPtr->selBdString = tagPtr->bdString;
  402.         textPtr->selFgColorPtr = tagPtr->fgColor;
  403.         }
  404.         tagPtr->affectsDisplay = 0;
  405.         if ((tagPtr->border != NULL)
  406.             || (tagPtr->bdString != NULL)
  407.             || (tagPtr->reliefString != NULL)
  408.             || (tagPtr->bgStipple != None)
  409.             || (tagPtr->fgColor != NULL) || (tagPtr->fontPtr != None)
  410.             || (tagPtr->fgStipple != None)
  411.             || (tagPtr->justifyString != NULL)
  412.             || (tagPtr->lMargin1String != NULL)
  413.             || (tagPtr->lMargin2String != NULL)
  414.             || (tagPtr->offsetString != NULL)
  415.             || (tagPtr->overstrikeString != NULL)
  416.             || (tagPtr->rMarginString != NULL)
  417.             || (tagPtr->spacing1String != NULL)
  418.             || (tagPtr->spacing2String != NULL)
  419.             || (tagPtr->spacing3String != NULL)
  420.             || (tagPtr->tabString != NULL)
  421.             || (tagPtr->underlineString != NULL)
  422.             || (tagPtr->wrapMode != NULL)) {
  423.         tagPtr->affectsDisplay = 1;
  424.         }
  425.         TkTextRedrawTag(textPtr, (TkTextIndex *) NULL,
  426.             (TkTextIndex *) NULL, tagPtr, 1);
  427.         return result;
  428.     }
  429.     } else if ((c == 'd') && (strncmp(argv[2], "delete", length) == 0)) {
  430.     Tcl_HashEntry *hPtr;
  431.  
  432.     if (argc < 4) {
  433.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  434.             argv[0], " tag delete tagName tagName ...\"",
  435.             (char *) NULL);
  436.         return TCL_ERROR;
  437.     }
  438.     for (i = 3; i < argc; i++) {
  439.         hPtr = Tcl_FindHashEntry(&textPtr->tagTable, argv[i]);
  440.         if (hPtr == NULL) {
  441.         continue;
  442.         }
  443.         tagPtr = (TkTextTag *) Tcl_GetHashValue(hPtr);
  444.         if (tagPtr == textPtr->selTagPtr) {
  445.         continue;
  446.         }
  447.         if (tagPtr->affectsDisplay) {
  448.         TkTextRedrawTag(textPtr, (TkTextIndex *) NULL,
  449.             (TkTextIndex *) NULL, tagPtr, 1);
  450.         }
  451.         TkBTreeTag(TkTextMakeIndex(textPtr->tree, 0, 0, &first),
  452.             TkTextMakeIndex(textPtr->tree,
  453.                 TkBTreeNumLines(textPtr->tree), 0, &last),
  454.             tagPtr, 0);
  455.         Tcl_DeleteHashEntry(hPtr);
  456.         if (textPtr->bindingTable != NULL) {
  457.         Tk_DeleteAllBindings(textPtr->bindingTable,
  458.             (ClientData) tagPtr);
  459.         }
  460.     
  461.         /*
  462.          * Update the tag priorities to reflect the deletion of this tag.
  463.          */
  464.  
  465.         ChangeTagPriority(textPtr, tagPtr, textPtr->numTags-1);
  466.         textPtr->numTags -= 1;
  467.         TkTextFreeTag(textPtr, tagPtr);
  468.     }
  469.     } else if ((c == 'l') && (strncmp(argv[2], "lower", length) == 0)) {
  470.     TkTextTag *tagPtr2;
  471.     int prio;
  472.  
  473.     if ((argc != 4) && (argc != 5)) {
  474.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  475.             argv[0], " tag lower tagName ?belowThis?\"",
  476.             (char *) NULL);
  477.         return TCL_ERROR;
  478.     }
  479.     tagPtr = FindTag(interp, textPtr, argv[3]);
  480.     if (tagPtr == NULL) {
  481.         return TCL_ERROR;
  482.     }
  483.     if (argc == 5) {
  484.         tagPtr2 = FindTag(interp, textPtr, argv[4]);
  485.         if (tagPtr2 == NULL) {
  486.         return TCL_ERROR;
  487.         }
  488.         if (tagPtr->priority < tagPtr2->priority) {
  489.         prio = tagPtr2->priority - 1;
  490.         } else {
  491.         prio = tagPtr2->priority;
  492.         }
  493.     } else {
  494.         prio = 0;
  495.     }
  496.     ChangeTagPriority(textPtr, tagPtr, prio);
  497.     TkTextRedrawTag(textPtr, (TkTextIndex *) NULL, (TkTextIndex *) NULL,
  498.         tagPtr, 1);
  499.     } else if ((c == 'n') && (strncmp(argv[2], "names", length) == 0)
  500.         && (length >= 2)) {
  501.     TkTextTag **arrayPtr;
  502.     int arraySize;
  503.  
  504.     if ((argc != 3) && (argc != 4)) {
  505.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  506.             argv[0], " tag names ?index?\"",
  507.             (char *) NULL);
  508.         return TCL_ERROR;
  509.     }
  510.     if (argc == 3) {
  511.         Tcl_HashSearch search;
  512.         Tcl_HashEntry *hPtr;
  513.  
  514.         arrayPtr = (TkTextTag **) ckalloc((unsigned)
  515.             (textPtr->numTags * sizeof(TkTextTag *)));
  516.         for (i = 0, hPtr = Tcl_FirstHashEntry(&textPtr->tagTable, &search);
  517.             hPtr != NULL; i++, hPtr = Tcl_NextHashEntry(&search)) {
  518.         arrayPtr[i] = (TkTextTag *) Tcl_GetHashValue(hPtr);
  519.         }
  520.         arraySize = textPtr->numTags;
  521.     } else {
  522.         if (TkTextGetIndex(interp, textPtr, argv[3], &index1)
  523.             != TCL_OK) {
  524.         return TCL_ERROR;
  525.         }
  526.         arrayPtr = TkBTreeGetTags(&index1, &arraySize);
  527.         if (arrayPtr == NULL) {
  528.         return TCL_OK;
  529.         }
  530.     }
  531.     SortTags(arraySize, arrayPtr);
  532.     for (i = 0; i < arraySize; i++) {
  533.         tagPtr = arrayPtr[i];
  534.         Tcl_AppendElement(interp, tagPtr->name);
  535.     }
  536.     ckfree((char *) arrayPtr);
  537.     } else if ((c == 'n') && (strncmp(argv[2], "nextrange", length) == 0)
  538.         && (length >= 2)) {
  539.     TkTextSearch tSearch;
  540.     char position[TK_POS_CHARS];
  541.  
  542.     if ((argc != 5) && (argc != 6)) {
  543.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  544.             argv[0], " tag nextrange tagName index1 ?index2?\"",
  545.             (char *) NULL);
  546.         return TCL_ERROR;
  547.     }
  548.     tagPtr = FindTag((Tcl_Interp *) NULL, textPtr, argv[3]);
  549.     if (tagPtr == NULL) {
  550.         return TCL_OK;
  551.     }
  552.     if (TkTextGetIndex(interp, textPtr, argv[4], &index1) != TCL_OK) {
  553.         return TCL_ERROR;
  554.     }
  555.     TkTextMakeIndex(textPtr->tree, TkBTreeNumLines(textPtr->tree),
  556.         0, &last);
  557.     if (argc == 5) {
  558.         index2 = last;
  559.     } else if (TkTextGetIndex(interp, textPtr, argv[5], &index2)
  560.         != TCL_OK) {
  561.         return TCL_ERROR;
  562.     }
  563.  
  564.     /*
  565.      * The search below is a bit tricky.  Rather than use the B-tree
  566.      * facilities to stop the search at index2, let it search up
  567.      * until the end of the file but check for a position past index2
  568.      * ourselves.  The reason for doing it this way is that we only
  569.      * care whether the *start* of the range is before index2;  once
  570.      * we find the start, we don't want TkBTreeNextTag to abort the
  571.      * search because the end of the range is after index2.
  572.      */
  573.  
  574.     TkBTreeStartSearch(&index1, &last, tagPtr, &tSearch);
  575.     if (TkBTreeCharTagged(&index1, tagPtr)) {
  576.         TkTextSegment *segPtr;
  577.         int offset;
  578.  
  579.         /*
  580.          * The first character is tagged.  See if there is an
  581.          * on-toggle just before the character.  If not, then
  582.          * skip to the end of this tagged range.
  583.          */
  584.  
  585.         for (segPtr = index1.linePtr->segPtr, offset = index1.charIndex; 
  586.             offset >= 0;
  587.             offset -= segPtr->size, segPtr = segPtr->nextPtr) {
  588.         if ((offset == 0) && (segPtr->typePtr == &tkTextToggleOnType)
  589.             && (segPtr->body.toggle.tagPtr == tagPtr)) {
  590.             goto gotStart;
  591.         }
  592.         }
  593.         if (!TkBTreeNextTag(&tSearch)) {
  594.          return TCL_OK;
  595.         }
  596.     }
  597.  
  598.     /*
  599.      * Find the start of the tagged range.
  600.      */
  601.  
  602.     if (!TkBTreeNextTag(&tSearch)) {
  603.         return TCL_OK;
  604.     }
  605.     gotStart:
  606.     if (TkTextIndexCmp(&tSearch.curIndex, &index2) >= 0) {
  607.         return TCL_OK;
  608.     }
  609.     TkTextPrintIndex(&tSearch.curIndex, position);
  610.     Tcl_AppendElement(interp, position);
  611.     TkBTreeNextTag(&tSearch);
  612.     TkTextPrintIndex(&tSearch.curIndex, position);
  613.     Tcl_AppendElement(interp, position);
  614.     } else if ((c == 'r') && (strncmp(argv[2], "raise", length) == 0)
  615.         && (length >= 3)) {
  616.     TkTextTag *tagPtr2;
  617.     int prio;
  618.  
  619.     if ((argc != 4) && (argc != 5)) {
  620.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  621.             argv[0], " tag raise tagName ?aboveThis?\"",
  622.             (char *) NULL);
  623.         return TCL_ERROR;
  624.     }
  625.     tagPtr = FindTag(interp, textPtr, argv[3]);
  626.     if (tagPtr == NULL) {
  627.         return TCL_ERROR;
  628.     }
  629.     if (argc == 5) {
  630.         tagPtr2 = FindTag(interp, textPtr, argv[4]);
  631.         if (tagPtr2 == NULL) {
  632.         return TCL_ERROR;
  633.         }
  634.         if (tagPtr->priority <= tagPtr2->priority) {
  635.         prio = tagPtr2->priority;
  636.         } else {
  637.         prio = tagPtr2->priority + 1;
  638.         }
  639.     } else {
  640.         prio = textPtr->numTags-1;
  641.     }
  642.     ChangeTagPriority(textPtr, tagPtr, prio);
  643.     TkTextRedrawTag(textPtr, (TkTextIndex *) NULL, (TkTextIndex *) NULL,
  644.         tagPtr, 1);
  645.     } else if ((c == 'r') && (strncmp(argv[2], "ranges", length) == 0)
  646.         && (length >= 3)) {
  647.     TkTextSearch tSearch;
  648.     char position[TK_POS_CHARS];
  649.  
  650.     if (argc != 4) {
  651.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  652.             argv[0], " tag ranges tagName\"", (char *) NULL);
  653.         return TCL_ERROR;
  654.     }
  655.     tagPtr = FindTag((Tcl_Interp *) NULL, textPtr, argv[3]);
  656.     if (tagPtr == NULL) {
  657.         return TCL_OK;
  658.     }
  659.     TkTextMakeIndex(textPtr->tree, 0, 0, &first);
  660.     TkTextMakeIndex(textPtr->tree, TkBTreeNumLines(textPtr->tree),
  661.         0, &last);
  662.     TkBTreeStartSearch(&first, &last, tagPtr, &tSearch);
  663.     if (TkBTreeCharTagged(&first, tagPtr)) {
  664.         TkTextPrintIndex(&first, position);
  665.         Tcl_AppendElement(interp, position);
  666.     }
  667.     while (TkBTreeNextTag(&tSearch)) {
  668.         TkTextPrintIndex(&tSearch.curIndex, position);
  669.         Tcl_AppendElement(interp, position);
  670.     }
  671.     } else if ((c == 'r') && (strncmp(argv[2], "remove", length) == 0)
  672.         && (length >= 2)) {
  673.     fullOption = "remove";
  674.     addTag = 0;
  675.     goto addAndRemove;
  676.     } else {
  677.     Tcl_AppendResult(interp, "bad tag option \"", argv[2],
  678.         "\":  must be add, bind, cget, configure, delete, lower, ",
  679.         "names, nextrange, raise, ranges, or remove",
  680.         (char *) NULL);
  681.     return TCL_ERROR;
  682.     }
  683.     return TCL_OK;
  684. }
  685.  
  686. /*
  687.  *----------------------------------------------------------------------
  688.  *
  689.  * TkTextCreateTag --
  690.  *
  691.  *    Find the record describing a tag within a given text widget,
  692.  *    creating a new record if one doesn't already exist.
  693.  *
  694.  * Results:
  695.  *    The return value is a pointer to the TkTextTag record for tagName.
  696.  *
  697.  * Side effects:
  698.  *    A new tag record is created if there isn't one already defined
  699.  *    for tagName.
  700.  *
  701.  *----------------------------------------------------------------------
  702.  */
  703.  
  704. TkTextTag *
  705. TkTextCreateTag(textPtr, tagName)
  706.     TkText *textPtr;        /* Widget in which tag is being used. */
  707.     char *tagName;        /* Name of desired tag. */
  708. {
  709.     register TkTextTag *tagPtr;
  710.     Tcl_HashEntry *hPtr;
  711.     int new;
  712.  
  713.     hPtr = Tcl_CreateHashEntry(&textPtr->tagTable, tagName, &new);
  714.     if (!new) {
  715.     return (TkTextTag *) Tcl_GetHashValue(hPtr);
  716.     }
  717.  
  718.     /*
  719.      * No existing entry.  Create a new one, initialize it, and add a
  720.      * pointer to it to the hash table entry.
  721.      */
  722.  
  723.     tagPtr = (TkTextTag *) ckalloc(sizeof(TkTextTag));
  724.     tagPtr->name = Tcl_GetHashKey(&textPtr->tagTable, hPtr);
  725.     tagPtr->priority = textPtr->numTags;
  726.     tagPtr->border = NULL;
  727.     tagPtr->bdString = NULL;
  728.     tagPtr->borderWidth = 0;
  729.     tagPtr->reliefString = NULL;
  730.     tagPtr->relief = TK_RELIEF_FLAT;
  731.     tagPtr->bgStipple = None;
  732.     tagPtr->fgColor = NULL;
  733.     tagPtr->fontPtr = NULL;
  734.     tagPtr->fgStipple = None;
  735.     tagPtr->justifyString = NULL;
  736.     tagPtr->justify = TK_JUSTIFY_LEFT;
  737.     tagPtr->lMargin1String = NULL;
  738.     tagPtr->lMargin1 = 0;
  739.     tagPtr->lMargin2String = NULL;
  740.     tagPtr->lMargin2 = 0;
  741.     tagPtr->offsetString = NULL;
  742.     tagPtr->offset = 0;
  743.     tagPtr->overstrikeString = NULL;
  744.     tagPtr->overstrike = 0;
  745.     tagPtr->rMarginString = NULL;
  746.     tagPtr->rMargin = 0;
  747.     tagPtr->spacing1String = NULL;
  748.     tagPtr->spacing1 = 0;
  749.     tagPtr->spacing2String = NULL;
  750.     tagPtr->spacing2 = 0;
  751.     tagPtr->spacing3String = NULL;
  752.     tagPtr->spacing3 = 0;
  753.     tagPtr->tabString = NULL;
  754.     tagPtr->tabArrayPtr = NULL;
  755.     tagPtr->underlineString = NULL;
  756.     tagPtr->underline = 0;
  757.     tagPtr->wrapMode = NULL;
  758.     tagPtr->affectsDisplay = 0;
  759.     textPtr->numTags++;
  760.     Tcl_SetHashValue(hPtr, tagPtr);
  761.     return tagPtr;
  762. }
  763.  
  764. /*
  765.  *----------------------------------------------------------------------
  766.  *
  767.  * FindTag --
  768.  *
  769.  *    See if tag is defined for a given widget.
  770.  *
  771.  * Results:
  772.  *    If tagName is defined in textPtr, a pointer to its TkTextTag
  773.  *    structure is returned.  Otherwise NULL is returned and an
  774.  *    error message is recorded in interp->result unless interp
  775.  *    is NULL.
  776.  *
  777.  * Side effects:
  778.  *    None.
  779.  *
  780.  *----------------------------------------------------------------------
  781.  */
  782.  
  783. static TkTextTag *
  784. FindTag(interp, textPtr, tagName)
  785.     Tcl_Interp *interp;        /* Interpreter to use for error message;
  786.                  * if NULL, then don't record an error
  787.                  * message. */
  788.     TkText *textPtr;        /* Widget in which tag is being used. */
  789.     char *tagName;        /* Name of desired tag. */
  790. {
  791.     Tcl_HashEntry *hPtr;
  792.  
  793.     hPtr = Tcl_FindHashEntry(&textPtr->tagTable, tagName);
  794.     if (hPtr != NULL) {
  795.     return (TkTextTag *) Tcl_GetHashValue(hPtr);
  796.     }
  797.     if (interp != NULL) {
  798.     Tcl_AppendResult(interp, "tag \"", tagName,
  799.         "\" isn't defined in text widget", (char *) NULL);
  800.     }
  801.     return NULL;
  802. }
  803.  
  804. /*
  805.  *----------------------------------------------------------------------
  806.  *
  807.  * TkTextFreeTag --
  808.  *
  809.  *    This procedure is called when a tag is deleted to free up the
  810.  *    memory and other resources associated with the tag.
  811.  *
  812.  * Results:
  813.  *    None.
  814.  *
  815.  * Side effects:
  816.  *    Memory and other resources are freed.
  817.  *
  818.  *----------------------------------------------------------------------
  819.  */
  820.  
  821. void
  822. TkTextFreeTag(textPtr, tagPtr)
  823.     TkText *textPtr;            /* Info about overall widget. */
  824.     register TkTextTag *tagPtr;        /* Tag being deleted. */
  825. {
  826.     if (tagPtr->border != None) {
  827.     Tk_Free3DBorder(tagPtr->border);
  828.     }
  829.     if (tagPtr->bdString != NULL) {
  830.     ckfree(tagPtr->bdString);
  831.     }
  832.     if (tagPtr->reliefString != NULL) {
  833.     ckfree(tagPtr->reliefString);
  834.     }
  835.     if (tagPtr->bgStipple != None) {
  836.     Tk_FreeBitmap(textPtr->display, tagPtr->bgStipple);
  837.     }
  838.     if (tagPtr->fgColor != None) {
  839.     Tk_FreeColor(tagPtr->fgColor);
  840.     }
  841.     if (tagPtr->fontPtr != None) {
  842.     Tk_FreeFontStruct(tagPtr->fontPtr);
  843.     }
  844.     if (tagPtr->fgStipple != None) {
  845.     Tk_FreeBitmap(textPtr->display, tagPtr->fgStipple);
  846.     }
  847.     if (tagPtr->justifyString != NULL) {
  848.     ckfree(tagPtr->justifyString);
  849.     }
  850.     if (tagPtr->lMargin1String != NULL) {
  851.     ckfree(tagPtr->lMargin1String);
  852.     }
  853.     if (tagPtr->lMargin2String != NULL) {
  854.     ckfree(tagPtr->lMargin2String);
  855.     }
  856.     if (tagPtr->offsetString != NULL) {
  857.     ckfree(tagPtr->offsetString);
  858.     }
  859.     if (tagPtr->overstrikeString != NULL) {
  860.     ckfree(tagPtr->overstrikeString);
  861.     }
  862.     if (tagPtr->rMarginString != NULL) {
  863.     ckfree(tagPtr->rMarginString);
  864.     }
  865.     if (tagPtr->spacing1String != NULL) {
  866.     ckfree(tagPtr->spacing1String);
  867.     }
  868.     if (tagPtr->spacing2String != NULL) {
  869.     ckfree(tagPtr->spacing2String);
  870.     }
  871.     if (tagPtr->spacing3String != NULL) {
  872.     ckfree(tagPtr->spacing3String);
  873.     }
  874.     if (tagPtr->tabString != NULL) {
  875.     ckfree(tagPtr->tabString);
  876.     }
  877.     if (tagPtr->tabArrayPtr != NULL) {
  878.     ckfree((char *) tagPtr->tabArrayPtr);
  879.     }
  880.     if (tagPtr->underlineString != NULL) {
  881.     ckfree(tagPtr->underlineString);
  882.     }
  883.     ckfree((char *) tagPtr);
  884. }
  885.  
  886. /*
  887.  *----------------------------------------------------------------------
  888.  *
  889.  * SortTags --
  890.  *
  891.  *    This procedure sorts an array of tag pointers in increasing
  892.  *    order of priority, optimizing for the common case where the
  893.  *    array is small.
  894.  *
  895.  * Results:
  896.  *    None.
  897.  *
  898.  * Side effects:
  899.  *    None.
  900.  *
  901.  *----------------------------------------------------------------------
  902.  */
  903.  
  904. static void
  905. SortTags(numTags, tagArrayPtr)
  906.     int numTags;        /* Number of tag pointers at *tagArrayPtr. */
  907.     TkTextTag **tagArrayPtr;    /* Pointer to array of pointers. */
  908. {
  909.     int i, j, prio;
  910.     register TkTextTag **tagPtrPtr;
  911.     TkTextTag **maxPtrPtr, *tmp;
  912.  
  913.     if (numTags < 2) {
  914.     return;
  915.     }
  916.     if (numTags < 20) {
  917.     for (i = numTags-1; i > 0; i--, tagArrayPtr++) {
  918.         maxPtrPtr = tagPtrPtr = tagArrayPtr;
  919.         prio = tagPtrPtr[0]->priority;
  920.         for (j = i, tagPtrPtr++; j > 0; j--, tagPtrPtr++) {
  921.         if (tagPtrPtr[0]->priority < prio) {
  922.             prio = tagPtrPtr[0]->priority;
  923.             maxPtrPtr = tagPtrPtr;
  924.         }
  925.         }
  926.         tmp = *maxPtrPtr;
  927.         *maxPtrPtr = *tagArrayPtr;
  928.         *tagArrayPtr = tmp;
  929.     }
  930.     } else {
  931.     qsort((VOID *) tagArrayPtr, (unsigned) numTags, sizeof (TkTextTag *),
  932.             TagSortProc);
  933.     }
  934. }
  935.  
  936. /*
  937.  *----------------------------------------------------------------------
  938.  *
  939.  * TagSortProc --
  940.  *
  941.  *    This procedure is called by qsort when sorting an array of
  942.  *    tags in priority order.
  943.  *
  944.  * Results:
  945.  *    The return value is -1 if the first argument should be before
  946.  *    the second element (i.e. it has lower priority), 0 if it's
  947.  *    equivalent (this should never happen!), and 1 if it should be
  948.  *    after the second element.
  949.  *
  950.  * Side effects:
  951.  *    None.
  952.  *
  953.  *----------------------------------------------------------------------
  954.  */
  955.  
  956. static int
  957. TagSortProc(first, second)
  958.     CONST VOID *first, *second;        /* Elements to be compared. */
  959. {
  960.     TkTextTag *tagPtr1, *tagPtr2;
  961.  
  962.     tagPtr1 = * (TkTextTag **) first;
  963.     tagPtr2 = * (TkTextTag **) second;
  964.     return tagPtr1->priority - tagPtr2->priority;
  965. }
  966.  
  967. /*
  968.  *----------------------------------------------------------------------
  969.  *
  970.  * ChangeTagPriority --
  971.  *
  972.  *    This procedure changes the priority of a tag by modifying
  973.  *    its priority and the priorities of other tags that are affected
  974.  *    by the change.
  975.  *
  976.  * Results:
  977.  *    None.
  978.  *
  979.  * Side effects:
  980.  *    Priorities may be changed for some or all of the tags in
  981.  *    textPtr.  The tags will be arranged so that there is exactly
  982.  *    one tag at each priority level between 0 and textPtr->numTags-1,
  983.  *    with tagPtr at priority "prio".
  984.  *
  985.  *----------------------------------------------------------------------
  986.  */
  987.  
  988. static void
  989. ChangeTagPriority(textPtr, tagPtr, prio)
  990.     TkText *textPtr;            /* Information about text widget. */
  991.     TkTextTag *tagPtr;            /* Tag whose priority is to be
  992.                      * changed. */
  993.     int prio;                /* New priority for tag. */
  994. {
  995.     int low, high, delta;
  996.     register TkTextTag *tagPtr2;
  997.     Tcl_HashEntry *hPtr;
  998.     Tcl_HashSearch search;
  999.  
  1000.     if (prio < 0) {
  1001.     prio = 0;
  1002.     }
  1003.     if (prio >= textPtr->numTags) {
  1004.     prio = textPtr->numTags-1;
  1005.     }
  1006.     if (prio == tagPtr->priority) {
  1007.     return;
  1008.     } else if (prio < tagPtr->priority) {
  1009.     low = prio;
  1010.     high = tagPtr->priority-1;
  1011.     delta = 1;
  1012.     } else {
  1013.     low = tagPtr->priority+1;
  1014.     high = prio;
  1015.     delta = -1;
  1016.     }
  1017.     for (hPtr = Tcl_FirstHashEntry(&textPtr->tagTable, &search);
  1018.         hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
  1019.     tagPtr2 = (TkTextTag *) Tcl_GetHashValue(hPtr);
  1020.     if ((tagPtr2->priority >= low) && (tagPtr2->priority <= high)) {
  1021.         tagPtr2->priority += delta;
  1022.     }
  1023.     }
  1024.     tagPtr->priority = prio;
  1025. }
  1026.  
  1027. /*
  1028.  *--------------------------------------------------------------
  1029.  *
  1030.  * TkTextBindProc --
  1031.  *
  1032.  *    This procedure is invoked by the Tk dispatcher to handle
  1033.  *    events associated with bindings on items.
  1034.  *
  1035.  * Results:
  1036.  *    None.
  1037.  *
  1038.  * Side effects:
  1039.  *    Depends on the command invoked as part of the binding
  1040.  *    (if there was any).
  1041.  *
  1042.  *--------------------------------------------------------------
  1043.  */
  1044.  
  1045. void
  1046. TkTextBindProc(clientData, eventPtr)
  1047.     ClientData clientData;        /* Pointer to canvas structure. */
  1048.     XEvent *eventPtr;            /* Pointer to X event that just
  1049.                      * happened. */
  1050. {
  1051.     TkText *textPtr = (TkText *) clientData;
  1052.     int repick  = 0;
  1053.  
  1054. # define AnyButtonMask (Button1Mask|Button2Mask|Button3Mask\
  1055.     |Button4Mask|Button5Mask)
  1056.  
  1057.     Tk_Preserve((ClientData) textPtr);
  1058.  
  1059.     /*
  1060.      * This code simulates grabs for mouse buttons by keeping track
  1061.      * of whether a button is pressed and refusing to pick a new current
  1062.      * character while a button is pressed.
  1063.      */
  1064.  
  1065.     if (eventPtr->type == ButtonPress) {
  1066.     textPtr->flags |= BUTTON_DOWN;
  1067.     } else if (eventPtr->type == ButtonRelease) {
  1068.     int mask;
  1069.  
  1070.     switch (eventPtr->xbutton.button) {
  1071.         case Button1:
  1072.         mask = Button1Mask;
  1073.         break;
  1074.         case Button2:
  1075.         mask = Button2Mask;
  1076.         break;
  1077.         case Button3:
  1078.         mask = Button3Mask;
  1079.         break;
  1080.         case Button4:
  1081.         mask = Button4Mask;
  1082.         break;
  1083.         case Button5:
  1084.         mask = Button5Mask;
  1085.         break;
  1086.         default:
  1087.         mask = 0;
  1088.         break;
  1089.     }
  1090.     if ((eventPtr->xbutton.state & AnyButtonMask) == mask) {
  1091.         textPtr->flags &= ~BUTTON_DOWN;
  1092.         repick = 1;
  1093.     }
  1094.     } else if ((eventPtr->type == EnterNotify)
  1095.         || (eventPtr->type == LeaveNotify)) {
  1096.     if (eventPtr->xcrossing.state & AnyButtonMask)  {
  1097.         textPtr->flags |= BUTTON_DOWN;
  1098.     } else {
  1099.         textPtr->flags &= ~BUTTON_DOWN;
  1100.     }
  1101.     TkTextPickCurrent(textPtr, eventPtr);
  1102.     goto done;
  1103.     } else if (eventPtr->type == MotionNotify) {
  1104.     if (eventPtr->xmotion.state & AnyButtonMask)  {
  1105.         textPtr->flags |= BUTTON_DOWN;
  1106.     } else {
  1107.         textPtr->flags &= ~BUTTON_DOWN;
  1108.     }
  1109.     TkTextPickCurrent(textPtr, eventPtr);
  1110.     }
  1111.     if ((textPtr->numCurTags > 0) && (textPtr->bindingTable != NULL)) {
  1112.     Tk_BindEvent(textPtr->bindingTable, eventPtr, textPtr->tkwin,
  1113.         textPtr->numCurTags, (ClientData *) textPtr->curTagArrayPtr);
  1114.     }
  1115.     if (repick) {
  1116.     unsigned int oldState;
  1117.  
  1118.     oldState = eventPtr->xbutton.state;
  1119.     eventPtr->xbutton.state &= ~(Button1Mask|Button2Mask
  1120.         |Button3Mask|Button4Mask|Button5Mask);
  1121.     TkTextPickCurrent(textPtr, eventPtr);
  1122.     eventPtr->xbutton.state = oldState;
  1123.     }
  1124.  
  1125.     done:
  1126.     Tk_Release((ClientData) textPtr);
  1127. }
  1128.  
  1129. /*
  1130.  *--------------------------------------------------------------
  1131.  *
  1132.  * TkTextPickCurrent --
  1133.  *
  1134.  *    Find the character containing the coordinates in an event
  1135.  *    and place the "current" mark on that character.  If the
  1136.  *    "current" mark has moved then generate a fake leave event
  1137.  *    on the old current character and a fake enter event on the new
  1138.  *    current character.
  1139.  *
  1140.  * Results:
  1141.  *    None.
  1142.  *
  1143.  * Side effects:
  1144.  *    The current mark for textPtr may change.  If it does,
  1145.  *    then the commands associated with character entry and leave
  1146.  *    could do just about anything.
  1147.  *
  1148.  *--------------------------------------------------------------
  1149.  */
  1150.  
  1151. void
  1152. TkTextPickCurrent(textPtr, eventPtr)
  1153.     register TkText *textPtr;        /* Text widget in which to select
  1154.                      * current character. */
  1155.     XEvent *eventPtr;            /* Event describing location of
  1156.                      * mouse cursor.  Must be EnterWindow,
  1157.                      * LeaveWindow, ButtonRelease, or
  1158.                      * MotionNotify. */
  1159. {
  1160.     TkTextIndex index;
  1161.     TkTextTag **oldArrayPtr, **newArrayPtr;
  1162.     TkTextTag **copyArrayPtr = NULL;    /* Initialization needed to prevent
  1163.                      * compiler warning. */
  1164.  
  1165.     int numOldTags, numNewTags, i, j, size;
  1166.     XEvent event;
  1167.  
  1168.     /*
  1169.      * If a button is down, then don't do anything at all;  we'll be
  1170.      * called again when all buttons are up, and we can repick then.
  1171.      * This implements a form of mouse grabbing.
  1172.      */
  1173.  
  1174.     if (textPtr->flags & BUTTON_DOWN) {
  1175.     if (((eventPtr->type == EnterNotify) || (eventPtr->type == LeaveNotify))
  1176.         && ((eventPtr->xcrossing.mode == NotifyGrab)
  1177.         || (eventPtr->xcrossing.mode == NotifyUngrab))) {
  1178.         /*
  1179.          * Special case:  the window is being entered or left because
  1180.          * of a grab or ungrab.  In this case, repick after all.
  1181.          * Furthermore, clear BUTTON_DOWN to release the simulated
  1182.          * grab.
  1183.          */
  1184.  
  1185.         textPtr->flags &= ~BUTTON_DOWN;
  1186.     } else {
  1187.         return;
  1188.     }
  1189.     }
  1190.  
  1191.     /*
  1192.      * Save information about this event in the widget in case we have
  1193.      * to synthesize more enter and leave events later (e.g. because a
  1194.      * character was deleted, causing a new character to be underneath
  1195.      * the mouse cursor).  Also translate MotionNotify events into
  1196.      * EnterNotify events, since that's what gets reported to event
  1197.      * handlers when the current character changes.
  1198.      */
  1199.  
  1200.     if (eventPtr != &textPtr->pickEvent) {
  1201.     if ((eventPtr->type == MotionNotify)
  1202.         || (eventPtr->type == ButtonRelease)) {
  1203.         textPtr->pickEvent.xcrossing.type = EnterNotify;
  1204.         textPtr->pickEvent.xcrossing.serial = eventPtr->xmotion.serial;
  1205.         textPtr->pickEvent.xcrossing.send_event
  1206.             = eventPtr->xmotion.send_event;
  1207.         textPtr->pickEvent.xcrossing.display = eventPtr->xmotion.display;
  1208.         textPtr->pickEvent.xcrossing.window = eventPtr->xmotion.window;
  1209.         textPtr->pickEvent.xcrossing.root = eventPtr->xmotion.root;
  1210.         textPtr->pickEvent.xcrossing.subwindow = None;
  1211.         textPtr->pickEvent.xcrossing.time = eventPtr->xmotion.time;
  1212.         textPtr->pickEvent.xcrossing.x = eventPtr->xmotion.x;
  1213.         textPtr->pickEvent.xcrossing.y = eventPtr->xmotion.y;
  1214.         textPtr->pickEvent.xcrossing.x_root = eventPtr->xmotion.x_root;
  1215.         textPtr->pickEvent.xcrossing.y_root = eventPtr->xmotion.y_root;
  1216.         textPtr->pickEvent.xcrossing.mode = NotifyNormal;
  1217.         textPtr->pickEvent.xcrossing.detail = NotifyNonlinear;
  1218.         textPtr->pickEvent.xcrossing.same_screen
  1219.             = eventPtr->xmotion.same_screen;
  1220.         textPtr->pickEvent.xcrossing.focus = False;
  1221.         textPtr->pickEvent.xcrossing.state = eventPtr->xmotion.state;
  1222.     } else  {
  1223.         textPtr->pickEvent = *eventPtr;
  1224.     }
  1225.     }
  1226.  
  1227.     /*
  1228.      * Find the new current character, then find and sort all of the
  1229.      * tags associated with it.
  1230.      */
  1231.  
  1232.     if (textPtr->pickEvent.type != LeaveNotify) {
  1233.     TkTextPixelIndex(textPtr, textPtr->pickEvent.xcrossing.x,
  1234.         textPtr->pickEvent.xcrossing.y, &index);
  1235.     newArrayPtr = TkBTreeGetTags(&index, &numNewTags);
  1236.     SortTags(numNewTags, newArrayPtr);
  1237.     } else {
  1238.     newArrayPtr = NULL;
  1239.     numNewTags = 0;
  1240.     }
  1241.  
  1242.     /*
  1243.      * Resort the tags associated with the previous marked character
  1244.      * (the priorities might have changed), then make a copy of the
  1245.      * new tags, and compare the old tags to the copy, nullifying
  1246.      * any tags that are present in both groups (i.e. the tags that
  1247.      * haven't changed).
  1248.      */
  1249.  
  1250.     SortTags(textPtr->numCurTags, textPtr->curTagArrayPtr);
  1251.     if (numNewTags > 0) {
  1252.     size = numNewTags * sizeof(TkTextTag *);
  1253.     copyArrayPtr = (TkTextTag **) ckalloc((unsigned) size);
  1254.     memcpy((VOID *) copyArrayPtr, (VOID *) newArrayPtr, (size_t) size);
  1255.     for (i = 0; i < textPtr->numCurTags; i++) {
  1256.         for (j = 0; j < numNewTags; j++) {
  1257.         if (textPtr->curTagArrayPtr[i] == copyArrayPtr[j]) {
  1258.             textPtr->curTagArrayPtr[i] = NULL;
  1259.             copyArrayPtr[j] = NULL;
  1260.             break;
  1261.         }
  1262.         }
  1263.     }
  1264.     }
  1265.  
  1266.     /*
  1267.      * Invoke the binding system with a LeaveNotify event for all of
  1268.      * the tags that have gone away.  We have to be careful here,
  1269.      * because it's possible that the binding could do something
  1270.      * (like calling tkwait) that eventually modifies
  1271.      * textPtr->curTagArrayPtr.  To avoid problems in situations like
  1272.      * this, update curTagArrayPtr to its new value before invoking
  1273.      * any bindings, and don't use it any more here.
  1274.      */
  1275.  
  1276.     numOldTags = textPtr->numCurTags;
  1277.     textPtr->numCurTags = numNewTags;
  1278.     oldArrayPtr = textPtr->curTagArrayPtr;
  1279.     textPtr->curTagArrayPtr = newArrayPtr;
  1280.     if (numOldTags != 0) {
  1281.     if (textPtr->bindingTable != NULL) {
  1282.         event = textPtr->pickEvent;
  1283.         event.type = LeaveNotify;
  1284.  
  1285.         /*
  1286.          * Always use a detail of NotifyAncestor.  Besides being
  1287.          * consistent, this avoids problems where the binding code
  1288.          * will discard NotifyInferior events.
  1289.          */
  1290.  
  1291.         event.xcrossing.detail = NotifyAncestor;
  1292.         Tk_BindEvent(textPtr->bindingTable, &event, textPtr->tkwin,
  1293.             numOldTags, (ClientData *) oldArrayPtr);
  1294.     }
  1295.     ckfree((char *) oldArrayPtr);
  1296.     }
  1297.  
  1298.     /*
  1299.      * Reset the "current" mark (be careful to recompute its location,
  1300.      * since it might have changed during an event binding).  Then
  1301.      * invoke the binding system with an EnterNotify event for all of
  1302.      * the tags that have just appeared.
  1303.      */
  1304.  
  1305.     TkTextPixelIndex(textPtr, textPtr->pickEvent.xcrossing.x,
  1306.         textPtr->pickEvent.xcrossing.y, &index);
  1307.     TkTextSetMark(textPtr, "current", &index);
  1308.     if (numNewTags != 0) {
  1309.     if (textPtr->bindingTable != NULL) {
  1310.         event = textPtr->pickEvent;
  1311.         event.type = EnterNotify;
  1312.         event.xcrossing.detail = NotifyAncestor;
  1313.         Tk_BindEvent(textPtr->bindingTable, &event, textPtr->tkwin,
  1314.             numNewTags, (ClientData *) copyArrayPtr);
  1315.     }
  1316.     ckfree((char *) copyArrayPtr);
  1317.     }
  1318. }
  1319.