TIP 577: Enhanced index values for Tk

Login
Bounty program for improvements to Tcl and certain Tcl packages.
Author:         Jan Nijtmans <[email protected]>
State:          Draft
Type:           Project
Created:        8-June-2020
Post-History:   
Keywords:       Tcl index
Tcl-Version:    8.7
Tk-Branch:      strict-index

Abstract

Various commands handle errors in "index" values differently. This TIP attempts to treat the different uses of index values in Tk to a more uniform way, align them better with index usage in Tcl.

The main feature of this TIP is to let Tk indices handle all forms of Tcl indices. So "4+5" and "end-1" will now be accepted, just as any other form accepted by Tcl_GetIntFormIndex() (See also TIP #544).

In addition, the empty string is accepted in Tk as valid index. It means the same as -1 so not available. In Tk this is more natural, since the default of many options is already the empty string. The default value of the -underline option so because the empty string, and setting -underline to the empty string means no underlining. This default value change will be only done when Tk is compiled with Tk 9.0 headers, in order to give applications more time to adapt to this usage (see Compatibility section below).

Rationale

Some examples (Tk):

$ wish8.6
% listbox .l
% menu .m
% .l index 0
0
% .l index none
bad listbox index "none": must be active, anchor, end, @x,y, or a number
% .m index none
none
% .m index 0
none
% .m index foo
bad menu entry index "foo"

We see that menu's have a different way to indicate 'not found' compared to other widgets.

A request is done already a long time ago, to make index handling in Tk more flexible. See: widgets indices enhancement This TIP implements part of this request (only the active-int form is not implemented.

Specification

Compatibility

When compiled with Tcl 8.x headers, this is almost 100% compatible. Except for menu commands which currently use the inconsistent none keyword (see above example) they will start returning the empty string.

Code can easily be made compatible in any environment. Just don't check against the value -1 any more, but use the < 0 check: Since comparing the empty string with 0 in Tcl uses string compare, it gives the same result as comparing -1 with 0. All Tk utilities and demo's are already modified to do this check consistently.

Implementation

See the strict-index branch.

Copyright

This document has been placed in the public domain.