Text Marks

   

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 33.  The Text Widget


A mark is a symbolic name for a position between two characters. Marks have the property that when text is inserted or deleted they retain their logical position, not their numerical index position. Marks are persistent: If you delete the text surrounding a mark, it remains intact. Marks are created with the mark set operation and must be explicitly deleted with the mark unset operation. Once defined, a mark can be used in operations that require indices. The following commands define a mark at the beginning of the word containing the insert cursor and delete from there up to the end of the line:

 $t mark set foobar "insert wordstart" $t delete foobar "foobar lineend" $t mark unset foobar 

When a mark is defined, it is set to be just before the character specified by the index expression. In the previous example, this is just before the first character of the word where the insert cursor is. When a mark is used in an operation that requires an index, it refers to the character just after the mark. So, in many ways the mark seems associated with the character right after it, except that the mark remains even if that character is deleted.

You can use almost any string for the name of a mark. However, do not use pure numbers and do not include spaces, plus (+) or minus (-). These characters are used in the mark arithmetic and may cause problems if you put them into mark names. The mark names operation returns a list of all defined marks.

The insert mark defines where the insert cursor is displayed. The insert mark is treated specially: you cannot remove it with the mark unset operation. Attempting to do so does not raise an error, though, so the following is a quick way to unset all marks. The eval is necessary to join the list of mark names into the mark unset command:

 eval {$t mark unset}[$t mark names] 

Mark Gravity

graphics/tip_icon.gif

Each mark has a gravity that determines what happens when characters are inserted at the mark. The default gravity is right, which means that the mark sticks to the character that is to its right. Inserting text at a mark with right gravity causes the mark to be pushed along so it is always after the inserted text. With left gravity the mark stays with the character to its left, so inserted text goes after the mark and the mark does not move. In versions of Tk before 4.0, marks had only right gravity, which made some uses of marks awkward. The mark gravity operation is used to query and modify the gravity of a mark:

 $t mark gravity foobar => right $t mark gravity foobar left 

       
    Top
     



    Practical Programming in Tcl and Tk
    Practical Programming in Tcl and Tk (4th Edition)
    ISBN: 0130385603
    EAN: 2147483647
    Year: 1999
    Pages: 478

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net