### Copyright (C) 1995-1997 Jesper K. Pedersen
### This program is free software; you can redistribute it and/or modify
### it under the terms of the GNU General Public License as published by
### the Free Software Foundation; either version 2 of the License, or
### (at your option) any later version.
###
### This program is distributed in the hope that it will be useful,
### but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
### GNU General Public License for more details.
###
### You should have received a copy of the GNU General Public License
### along with this program; if not, write to the Free Software
### Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


proc alias {} {
  # alias

  Desc "Here you configure aliases. An alias is a sort of macro, or maybe"\
      "rather a nickname for a command.  Eg. you can have 'll' as a nickname"\
      "for 'ls -l', or you can have a \"macro\" which packs a directory"\
      "and all its children, called 'pack', which takes one argument,"\
      "which is the directory to be packed."

  ShortDesc "aliases"
  
  Entry alias \
      -text Alias \
      -help "This is the nickname for your command." \
      -packFrame:anchor n -packLabel:side top -packLabel:padx 3

  ############################################################
  Int argument \
      -text "Argument number"\
      -textafter "(0 - command name, -1 last word)" 
  
  FillOutElm single \
      -text "A single word" \
      -entries argument\
      -show {
        switch -- $argument {
          0 {print \[command\]}
          -1 {print \[last\]}
          default {print \[$argument\]}
        }
      } \
      -save {
        switch -- $argument {
          -1 {print "\\!\$"}
          default {print "\\!:$argument"}
        }
      } \
      -pageEnd {
        if {$argument == ""} {
          error "Entry with text \"Argument number\" is empty"
        }
      }

  ############################################################
  Header fromHeader -text From
  Radio from \
      -entries {"Command name" "First Argument" "Other:"} \
      -count 1
  Int fromOther \
      -packFrame:anchor s
  Frame fromFrame \
      -entries fromHeader from fromOther

  Header toHeader -text To
  Radio to \
      -entries {"First argument" "Last argument"
        "Last but one argument" "Other"} \
      -count 1
  Int toOther \
      -packFrame:anchor s
  Frame toFrame \
      -entries toHeader to toOther

  FillOutElm range \
      -text "A range of words" \
      -help "This gives you a range of words, eg. 2-last" \
      -entries fromFrame toFrame \
      -change {
        switch $changeElm {
          from {
            if {$from(index) == 2} {
              Enable fromOther
            } else {
              Disable fromOther
            }
          }
          to {
            if {$to(index) == 3} {
              Enable toOther
            } else {
              Disable toOther
            }
          }
        }
      } \
      -show {
        switch $from(index) {
          0 {set fromText command}
          1 {set fromText 1}
          default {set fromText $fromOther}
        }
        switch $to(index) {
          0 {set toText 1}
          1 {set toText last}
          2 {set toText last-1}
          default {set toText $toOther}
        }
        print "\[$fromText - $toText\]"
      } \
      -pageEnd {
        if {$from(index) == 2 && $fromOther == ""} {
          error "Entry \"from other\" is empty"
        }
        if {$to(index) == 3 && $toOther == ""} {
          error "Entry \"to other\" is empty"
        }
      } \
      -save {
        switch $from(index) {
          0 {set fromText 0}
          1 {set fromText 1}
          2 {set fromText $fromOther}
        }
        switch $to(index) {
          0 {set toText -1}
          1 {set toText *}
          2 {set toText -}
          default {set toText -$toOther}
        }
        print "\"\\!\\!:$fromText$toText\""
      }
  
  ############################################################
  FillOut command \
      -text "Command" \
      -entries single range\
      -listbox:height 2\
      -listbox:width 30

  ExtEntry aliases \
      -lines 1 \
      -entries alias command \
      -index alias

  ################################ Help ################################
  Help argument\
      "Here you can select a single word from the command line\n0 -"\
      "command name\nn - the n'th argument\n-1 - the last argument"
  
  Help single \
      "Here you can select a single element from the command line\n"\
      "Eg. if you have an alias called pack, where the first argument tells"\
      "which files should to be packed"

  Help command \
      "This is the command that you want to be executed when the"\
      "nickname is invoked"

  
  ############################## Page End ##############################
  PageEnd {
    set line 0
    forevery aliases {
      incr line
      if {$alias == "" && $command == ""} continue
      if {$alias == "" || $command == ""} {
        error "Either alias or command was empty in line $line"
      }
    }
  }
	
  ################################ Save ################################
  Save {
    ########## alias ##########
    forevery aliases {
      if {$alias == ""} continue
      regsub -all ' $command {'"'"'} res
     print "alias $alias '$res'"
    }
  }
}
