Tk Library Source Code

View Ticket
Login
Ticket UUID: 05344d311af68d431efefe154587bc783ca9c0ee
Title: history: Up skips the newest entry, glob matching in add, one entry too many
Type: Bug Version: trunk
Submitter: serhiy.storchaka Created on: 2026-11-21 18:08:47
Subsystem: tklib :: history Assigned To: nobody
Priority: 5 Medium Severity: Important
Status: Open Last Modified: 2026-11-22 16:44:35
Resolution: None Closed By: nobody
    Closed on:
Description:

The bugs below were found by the tests in branch tests-history; each is covered by a test with the knownBug constraint (history-4.7, history-4.11, history-7.3 to history-7.7). They occur with both Tk 8.6 and 9.1.

  • Navigation is off by one. In version 0.1 the current position started at -1; since version 0.2 (the change made for [5e324065bf]) it starts at 0, which is also the index of the newest entry, and ::history::up first increments it. After adding one, two, three, the first Up shows two instead of three, so the newest entry can never be recalled with Up, also right after ::history::add (history-7.3, history-7.4, history-7.7). With only one entry, Up just calls the alert command and the entry can never be recalled (history-7.5). Down never restores the text typed before pressing Up: in ::history::down the branch if {$history($w,cur) == 0} is inside if {$history($w,cur) != 0} and is never taken, so Down stops at the newest entry (history-7.6).
  • ::history::add searches for a duplicate with lsearch -dictionary, which does glob matching (-dictionary only matters with -sorted). Adding a* after abc removes abc: ::history::get returns a* instead of a* abc. Using -exact would fix this (history-4.7).
  • The history keeps one entry more than the length: lrange $history($w,list) 0 $prefs(maxlen,$w) keeps maxlen+1 entries. With ::history::init .e 3 and six added entries, ::history::get returns 6 5 4 3. The manual page says the length "determines the number of history entries to keep" (history-4.11).

User Comments: serhiy.storchaka added on 2026-11-22 16:44:35:

Proposed fix in branch history-navigation-off-by-one. The position starts at -1 again as in 0.1; to keep the intent of [5e324065bf], Up and Down skip the newest entry when the entry still shows it (it was not cleared after ::history::add). ::history::add also uses exact matching and keeps at most length entries.