; ======================
; = Tab-completion script by Pass
; ----------------------
; How it works 
; - A hashtable is created, and then filled with 
; - data to be called by the TAB button within the
; - @tab window.
; ----------------------
; Usage: /_tab -admcs <text> <data>
; - /_tab : creates/loads the hashtable
; - /_tab -a <item> : Adds an item to the tab list
; - /_tab -d <item/N> : Deletes an item from the tab list
; - /_tab -m <window> <string> : Matches a string to the list
; - /_tab -c : Clears the tab list
; - /_tab -s : Saves the tab hashtable
; ----------------------
; = Do not edit below this line
; ======================

; --------------
; Example code
; --------------
on *:tabcomp:@tab:{
  if ($1-) { 
    var %match = $gettok($1-,$numtok($1-,32),32)
    if (%tab.match.@tab == $null) set -u2 %tab.match.@tab %match
    _tab -m @tab $$1-
  }
}

alias tabwin {
  if ($window(@tab)) window -c @tab
  window -daCek0w1 +fts @tab -1 -1 300 200
  titlebar @tab - /tabwin
  _tab
}
; --------------
; End of example
; --------------

alias _tab {
  if ($left($1,1) == -) {
    var %s = $right($1,-1)
    if (a isincs %s) hadd tab $2 $true
    if (d isincs %s) { 
      if ($hget(tab,$2).item) var %item = $v1
      if (%item) hdel tab %item
    }
    if (c isincs %s) { hfree tab | hmake tab } 
    if (s isincs %s) hsave -o tab tab.hsh
    elseif (m === %s) {
      var %new.match = $($+(%,tab.match.,$2),2) $+ *
      if ( $($+(%,tab.,$2),2) >= $hfind(tab,%new.match,0,w) ) set -u2 $+(%,tab.,$2) 0
      inc -u2 $+(%,tab.,$2) 1
      var %win = $($+(%,tab.,$2),2)
      if ($hfind(tab,%new.match,%win,w)) {
        var %v1 = $v1,%num = $calc($numtok($3-,32) - 1),%nums = $+(1-,%num)
        editbox $2 $iif(%num > 0,$gettok($3-,%nums,32)) %v1
      }
    } 
    elseif (l === %s) {
      var %n = 1,%list
      while ($hget(tab,%n).item) {
        var %list = %list $v1
        inc %n
      }
      return %list
    }
  }
  else {
    if ($hget(tab)) hfree tab 
    hmake tab
    if ($isfile(tab.hsh)) hload tab tab.hsh
  }
}
