#!/bin/sh
#Macros  -*- TCL -*- Created: ramsan Apr-2000, Modified: ramsan Apr-2000


#----------------------Macros ALONE---------------------------
# the next line restarts using wish \
	exec wish8.0 "$0" "$@"

set MacrosRunningAlone 0
catch {
    if {$argv0 == [info script] } {
	set MacrosRunningAlone 1
	set tcl_interactive 0
	set tk_strictMotif 1
	wm withdraw .

	if { [catch { set GIDDEFAULT $env(GIDDEFAULT)}]!=1 } {
	    set GIDDEFAULTTCL $GIDDEFAULT
	    catch { set GIDDEFAULTTCL $env(GIDDEFAULTTCL) }
	    set auto_path "$GIDDEFAULTTCL"
	    source $GIDDEFAULTTCL/init.tcl
	    source $GIDDEFAULTTCL/tk.tcl
	}
    }
}

#  LOOK AT THE END OF THE FILE !!!
#-------------------------------------------------------------------

proc McrWriteMacros { fileid } {
    global MacrosList 
    
    if { ![info exists MacrosList] || \
	    [llength [array names MacrosList]] == 0 } {
	return
    }
    puts $fileid "*****BEGIN MACROS"
    foreach i [array names MacrosList] {
	puts $fileid $i
	puts $fileid $MacrosList($i)
    }
    puts $fileid "*****END MACROS"
}

proc McrReadMacros { fileid } {
    global MacrosList

    set NumMacros 0
    while { ![eof $fileid] } {
	gets $fileid aa
	if { [regexp {^[ ]*$} $aa] } { continue }
	if { [regexp {^[*][*][*][*][*]END} $aa] } {
	    break
	}
	set name $aa
	set contents ""
	set good 0
	while { ![eof $fileid] } {
	    gets $fileid aa
	    append contents $aa\n
	    if { [regexp {^END ACTION} $aa] } {
		set good 1
		break
	    }
	}
	if { $good } {
	    set MacrosList($name) $contents
	    incr NumMacros
	} else { break }
    }
    return $NumMacros
}

proc McrReadMacrosFile { w } {

    set file [Browser-ramR file read $w [_ "Read macros file"]]
    if { $file == "" } { return }

    if { [catch { set fileid [open $file r] }] } {
	WarnWin [_ "Could not open file '%s'" $file]
	return
    }
    set NumMacros 0
    while { ![eof $fileid] } {
	set aa [gets $fileid]
	if { [regexp {^[*][*][*][*][*]BEGIN MACROS} $aa] } {
	    set NumMacros [McrReadMacros $fileid]
	    break
	}
    }
    close $fileid
    WarnWin [_ "Read %s macros" $NumMacros]
    McrBindMacros
    McrUpdate $w
}

proc McrWriteMacrosFile { w } {

    set file [Browser-ramR file write $w [_ "Write macros file"]]
    if { $file == "" } { return }

    if { [catch { set fileid [open $file w] }] } {
	WarnWin [_ "Could not open file '%s' to write" $file]
	return
    }
    McrWriteMacros $fileid
    close $fileid
}

proc McrBindMacros {} {
    global MacrosList

    foreach i [array names MacrosList] {
	regexp [join [list "ACCELERATORS\n(.*)END ACCELERATORS.*" \
		"COMMENTS\n(.*)END COMMENTS.*ACTION\n(.*)END ACTION"] ""] \
		$MacrosList($i) {} acc comments action
	foreach j $acc {
	    after idle [list CreateGlobalBinding $j "ExecuteMacro $i"]
	}
    }
}

proc McrNewMacro { w } {
    global McrPriv MacrosList 

    set newmacro $McrPriv(currentmacro)
    if { ![regexp {^[a-zA-Z][-a-zA-Z0-9_]*$} $newmacro] } {
	tk_dialogRAM $w.tempwinpr [_ "Error"] \
	    [_ "Name of the macro must begin with a letter and contain only letters, numbers, _ and -"] \
	    error 0 [_ "Ok"]
	return
    }
    if { [info exists MacrosList($newmacro)] } {
	set retval [tk_dialogRAM $w.tempwinpr [_ "Error"] \
			[_ "Are you sure to redefine macro '%s'?" $newmacro ] \
			error 0 [_ "OK"] [_ "Cancel"]]
	if { $retval == 1 } { return }
    }
    set macrotext [MrcGetContentsFromText $w.middle.t]

    if { [regexp "^\[ \n\t]*$" $macrotext]  } {
	tk_dialogRAM $w.tempwinpr [_ "Error"] \
	    [_ "Contents of the macro are void. Please, fill in them with text or with the button 'Record'"] \
	    error 0 [_ "OK"]
	return
    }

    set acc ""
    set comments [$w.comments.t get 1.0 end]

    set MacrosList($newmacro) [join [list "ACCELERATORS\n${acc}" \
	    "\nEND ACCELERATORS\nCOMMENTS\n${comments}END COMMENTS" \
	    "\nACTION\n${macrotext}END ACTION"] ""]

    $w.f1.c1 conf -keephistory 1
    $w.f1.c1 add $newmacro
    $w.f1.c1 conf -keephistory 0
     
    McrShowMessage $w.f1.c1 "Created macro '$newmacro'     "
}

proc McrShowMessage { w buf } {

    catch { destroy $w.errr }
    label $w.errr -text $buf -relief ridge
    place  $w.errr -x 0 -rely 1 -in $w -anchor sw
    tkwait visibility $w.errr
    
    CalcFlashlabel 0 0 $w.errr
}

proc McrViewMacro { w } {
    global McrPriv MacrosList

    if { ![info exists MacrosList($McrPriv(currentmacro))] } {
	tk_dialogRAM $w.tempwinpr [_ "Error"] \
	    [_ "Macro '%s' does not exist. Its code cannot be viewed" $McrPriv(currentmacro)] \
	    error 0 [_ "OK"]
	return
    }
    regexp "COMMENTS\n(.*)END COMMENTS.*ACTION\n(.*)END ACTION" \
	    $MacrosList($McrPriv(currentmacro)) {} comments action
    $w.middle.t del 1.0 end
    McrInsertContentsInText $w.middle.t $action

    $w.comments.t del 1.0 end
    McrInsertContentsInText $w.comments.t $comments
    
}

proc McrInsertEscape { text more } {

    set inum 0
    while { [winfo exists $text.l$inum] } { incr inum }
    if { !$more } {
	label $text.l$inum -image [ibitmapfrom arrow-l #903e9c] -text escape
    } else {
	label $text.l$inum -image [ibitmapfrom arrow-l-plus #903e9c] \
				       -text escape4
    }
    $text window create "insert" -padx 1 -window $text.l$inum
}

proc McrInsertTclTk { text } {

    regexp {^([0-9]+)} [$text index "insert linestart"] lines
    foreach "l1 l2" [$text tag nextrange sel 1.0 end] break
    if { [info exists l1] } {
	set lines ""
	regexp {^([0-9]+)} $l1 l1
	regexp {^([0-9]+)} $l2 l2
	for { set i $l1 } { $i <= $l2 } { incr i } {
	    lappend lines $i
	}
    }
    foreach i $lines {
	set idx $i.0
	set found 0
	catch {
	    set win [$text window cget $idx -window]
	    if { [$win cget -text] == "-np-" } {
		$text delete $idx
		set found 1
	    }
	}
	if { !$found } {
	    set inum 0
	    while { [winfo exists $text.tcl$inum] } { incr inum }
	    label $text.tcl$inum -image [imagefrom tcltk16.gif] -text "-np-"
	    $text window create $idx -padx 1 -window $text.tcl$inum
	}
    }
}

proc McrDeleteAllMacros { w } {
    global McrPriv MacrosList

    set retval [tk_dialogRAM $w.tempwinpr [_ "Error"] \
		    [_ "Sure delete all macros?"] \
		    error 0 [_ "OK"] [_ "Cancel"]]
    if { $retval == 1 } { return }

    foreach j [array names MacrosList] {
	regexp "ACCELERATORS\n(.*)END ACCELERATORS" \
		$MacrosList($j) {} acc

	foreach i $acc {
	    CreateGlobalBinding $i ""
	}
	unset MacrosList($j)
    }
    McrShowMessage $w.f1.c1 "Deleted all macros     "
    $w.middle.t del 1.0 end
    set McrPriv(currentmacro) ""
    McrUpdate $w
}

proc McrDeleteMacro { w } {
    global McrPriv MacrosList
    
    if { ![info exists MacrosList($McrPriv(currentmacro))] } {
	tk_dialogRAM $w.tempwinpr [_ "Error"] \
	    [_ "Macro '%s' does not exist. Its cannot be deleted" $McrPriv(currentmacro)]  \
	    error 0 [_ "OK"]
	return
    }
    set retval [tk_dialogRAM $w.tempwinpr [_ "Warning"] \
		    [_ "Sure delete Macro '%s'?" $McrPriv(currentmacro)]  \
		    error 0 [_ "OK"] [_ "Cancel"]]
    if { $retval == 1 } { return }

    regexp "ACCELERATORS\n(.*)END ACCELERATORS" \
	    $MacrosList($McrPriv(currentmacro)) {} acc

    foreach i $acc {
	set ipos [lsearch [$w.middle.sc.l get 0 end] $i]
	$w.middle.sc.l del $ipos
	set ipos [lsearch $acc $i]
	set acc [lreplace $acc $ipos $ipos]
	CreateGlobalBinding $i ""
    }
    unset MacrosList($McrPriv(currentmacro))
    set pos [lsearch  [$w.f1.c1 listbox get 0 end] $McrPriv(currentmacro)]
    $w.f1.c1 listbox del $pos
    McrShowMessage $w.f1.c1 "Deleted macro '$McrPriv(currentmacro)'     "
    set McrPriv(currentmacro) ""
    McrUpdate $w
}

proc McrPressAcceleratorkeyError { w } {

    set w [winfo toplevel $w]
    tk_dialogRAM $w.tempwinpr [_ "Error"] \
	[_ "Error: Accelerators can be of the types:\nCtrl-s\nAlt-a\nF1\nCtrl-F1\nCtrl-x Ctrl-a"] \
	error 0 [_ "OK"]
    return
}

proc McrPressAcceleratorkey { w K A } {
    global McrPriv

    regexp {(Ctrl|Alt)?-?([^ ]*)[ ]*(Ctrl|Alt)?-?([^ ]*)} \
	    $McrPriv(currentshortcut) \
  	    {} ctrl(1) l(1) ctrl(2) l(2)

    if { $K == "BackSpace" || $K == "Delete" } {
	set McrPriv(currentshortcut) ""
	return
    }
    if { $l(2) != "" } { McrPressAcceleratorkeyError $w ; return }
    if { $l(1) != "" } { set i 2 } else { set i 1 }

    if { [string match Control* $K] } {
	if { $ctrl($i) == "" } {
	    tkEntryInsert $w Ctrl-
	} else { McrPressAcceleratorkeyError $w }
    } elseif { [string match Alt* $K] } {
	if { $ctrl($i) == "" } {
	    tkEntryInsert $w Alt-
	} else { McrPressAcceleratorkeyError $w }
    } elseif { $A == "" && [string match F* $K] } {
	tkEntryInsert $w "$K "
    } elseif { $A != "" } {
	if { $ctrl($i) != "" } {
	    tkEntryInsert $w "$K "
	} else { McrPressAcceleratorkeyError $w }
    }
}

proc McrAddAcceleratorKey { w } {
    global McrPriv MacrosList

    set sc [string trim $McrPriv(currentshortcut) " "]
    if { $sc == "" } {
	tk_dialogRAM $w.tempwinpr [_ "Error"] \
	    [_ "Enter first an accelerator"] \
	    error 0 [_ "OK"]
	return
    }
    if { [lsearch [$w.middle.sc.l get 0 end] $sc] != -1 } {
	tk_dialogRAM $w.tempwinpr [_ "Error"] \
	    [_ "Accelerator is already assigned to this macro"] \
	    error 0 [_ "OK"]
	return
    }
    if { ![info exists MacrosList($McrPriv(currentmacro))] } {
	tk_dialogRAM $w.tempwinpr [_ "Error"] \
	    [_ "Select first an existing macro or create new one"] \
	    error 0 [_ "OK"]
	return
    }
    regexp [join [list "ACCELERATORS\n(.*)END ACCELERATORS.*" \
	    "COMMENTS\n(.*)END COMMENTS.*ACTION\n(.*)END ACTION"] ""] \
	    $MacrosList($McrPriv(currentmacro)) {} acc comments action
    lappend acc $sc
    set MacrosList($McrPriv(currentmacro)) [join [list "ACCELERATORS\n${acc}" \
	    "\nEND ACCELERATORS\nCOMMENTS\n${comments}END COMMENTS" \
	    "\nACTION\n${action}END ACTION"] ""]

    CreateGlobalBinding $sc "ExecuteMacro $McrPriv(currentmacro)"
    
    $w.middle.sc.l ins end $sc
    set McrPriv(currentshortcut) ""
}

proc McrDeleteAcceleratorKey { w } {
    global McrPriv MacrosList

    set list ""
    foreach i [$w.middle.sc.l curselection] {
	lappend list [$w.middle.sc.l get $i]
    }
    
    if { $list == "" } {
	tk_dialogRAM $w.tempwinpr [_ "Error"] \
	    [_ "Select first an existing accelerator to delete"] \
	    error 0 [_ "OK"]
	return
    }

    if { [llength $list] == 1 } {
	set retval [tk_dialogRAM $w.tempwinpr [_ "Error"] \
			[_ "Sure delete accelerator '%s'?" $list] \
			error 0 [_ "OK"] [_ "Cancel"]]
	if { $retval == 1 } { return }
    } else {
	set retval [tk_dialogRAM $w.tempwinpr [_ "Error"] \
			[_ "Sure delete accelerators '%s'?" $list] \
			error 0 [_ "OK"] [_ "Cancel"]
	if { $retval == 1 } { return }
    }
    
    set cm $McrPriv(currentmacro)

    regexp [join [list "ACCELERATORS\n(.*)END ACCELERATORS.*" \
	    "COMMENTS\n(.*)END COMMENTS.*ACTION\n(.*)END ACTION"] ""] \
	    $MacrosList($cm) {} acc comments action

    foreach i $list {
	set ipos [lsearch [$w.middle.sc.l get 0 end] $i]
	$w.middle.sc.l del $ipos
	set ipos [lsearch $acc $i]
	set acc [lreplace $acc $ipos $ipos]
	CreateGlobalBinding $i ""
    }
    set MacrosList($cm) [join [list "ACCELERATORS\n${acc}" \
	    "\nEND ACCELERATORS\nCOMMENTS\n${comments}END COMMENTS" \
	    "\nACTION\n${action}END ACTION"] ""]

}

proc McrShowAccCode { w } {
    global McrPriv

    $w.down.t conf -state normal
    $w.down.t del 1.0 end
    set cs [string trim $McrPriv(currentshortcut)]
    if { $cs == "" } {
	$w.down.t conf -state disabled
	return
    }
    set comm [ViewGlobalBinding $cs]

    if { [regexp \
	    {^[ ]*([.a-zA-Z_0-9]+)[ ]*invoke[ ]*([0-9]+)[ ]*;[ ]*break[ ]*$} \
	    $comm {} menu idx] } {
	set comm [$menu entrycget $idx -command]
    }

    if { $comm != "" } {
	$w.down.t ins end $comm
    }
    $w.down.t conf -state disabled
}

proc McrChangeCurrentAccs { w } {
    global McrPriv MacrosList

    $w.middle.sc.l del 0 end
    set cm $McrPriv(currentmacro)

    if { ![info exists MacrosList($cm)] } { return }

    regexp [join [list "ACCELERATORS\n(.*)END ACCELERATORS.*" \
	    "COMMENTS\n(.*)END COMMENTS.*ACTION\n(.*)END ACTION"] ""] \
	    $MacrosList($cm) {} acc comments action

    foreach i $acc {
	$w.middle.sc.l ins end $i
    }
    McrViewMacro $w
}

proc McrInsertContentsInText { text contents } {
    
    set numescapes 0
    regsub -all \n $contents \\n contents
    foreach i $contents {
	if { $i == "-np-" } {
	    McrInsertTclTk $text
	} elseif { [regexp "escape\[\n]?" $i] } {
	    incr numescapes
	} else {
	    while { $numescapes >= 4 } {
		McrInsertEscape $text 1
		incr numescapes -4
	    }
	    while { $numescapes > 0 } {
		McrInsertEscape $text 0
		incr numescapes -1
	    }
	    $text insert insert "$i "
	} 
    }
    while { $numescapes > 4 } {
	McrInsertEscape $text 1
	incr numescapes -4
    }
    while { $numescapes > 0 } {
	McrInsertEscape $text 0
	incr numescapes -1
    }
}

proc MrcGetContentsFromText { text } {

    set retval ""
    foreach {key value index} [$text dump -text -window 1.0 end] {
	switch $key {
	    text {
		append retval $value
	    }
	    window {
		set name [$value cget -text]
		if { $name == "escape" } {
		    append retval "escape "
		} elseif { $name == "escape4" } {
		    append retval "escape escape escape escape "
		} elseif { $name == "-np-" } {
		    append retval "-np- "
		}
	    }
	}
    }
    return $retval
}

proc McrRecordMacroEnd { w } {

    destroy $w.stop
    wm deiconify $w
    McrInsertContentsInText $w.middle.t [.central.s recordbatch end]
}

proc McrRecordMacro { w } {
    
    wm withdraw $w
    toplevel $w.stop
    wm transient $w.stop .gid
    WmGidGeom $w.stop ""
    button $w.stop.b -image [ibitmapfrom stop19x15 red] \
	    -command "McrRecordMacroEnd $w"
    pack $w.stop.b 

    wm protocol $w.stop WM_DELETE_WINDOW "$w.stop.b invoke"

    .central.s recordbatch begin
}

proc McrExecuteMacro { w } {
    global McrPriv MacrosList
    
    if { ![info exists MacrosList($McrPriv(currentmacro))] } {
	tk_dialogRAM $w.tempwinpr [_ "Error"] \
	    [_ "Macro '%s' does not exist. It cannot be executed" $McrPriv(currentmacro)] \
	    error 0 [_ "OK"]
	return
    }
    ExecuteMacro $McrPriv(currentmacro)
}

proc ExecuteMacro { macro } {
    global MacrosList

    regexp [join [list "ACCELERATORS\n(.*)END ACCELERATORS.*" \
	    "COMMENTS\n(.*)END COMMENTS.*ACTION\n(.*)END ACTION"] ""] \
	    $MacrosList($macro) {} acc comments action

    foreach i [split $action \n] {
	if { [regexp {[ \t]*-np-(.*)} $i {} tclaction] } {
	    eval $tclaction
	} else {
	    .central.s process $i
	}
    }
}

proc McrUpdate { w } {
    global McrPriv MacrosList

    $w.f1.c1 conf -history [lsort [array names MacrosList]]
    if { $McrPriv(currentmacro) == "" } {
	set McrPriv(currentmacro) [lindex [array names MacrosList] 0]
    }
}

proc McrBegin { { w .gid.macros } } {
    global McrPriv MacrosList

    if { ![winfo exists $w] } {

	InitWindow $w [_ "Macros"] MacrosWindowGeom McrBegin ""

	frame $w.f1
	combobox $w.f1.c1 -editable 1 -textvariable McrPriv(currentmacro) \
		-keephistory 0 -history [lsort [array names MacrosList]]

	focus $w.f1.c1
	trace var McrPriv(currentmacro) w "McrChangeCurrentAccs $w ;#"

	button $w.f1.b1 -image [imagefrom check.gif] -command "McrNewMacro $w"
	GidHelp $w.f1.b1 [_ "It creates a new macro with the current name or\
		modifies current macro"]
	button $w.f1.b2 -image [imagefrom erase-cross.gif] \
		-command "McrDeleteMacro $w"
	
	bind $w.f1.b2 <Control-ButtonRelease-1> {
	    if {[string equal $tkPriv(buttonWindow) %W]} {
		set tkPriv(buttonWindow) ""
		%W configure -relief $tkPriv(relief)
		if {[string equal $tkPriv(window) %W]
		&& [string compare [%W cget -state] "disabled"]} {
		    %W configure -state normal
		    McrDeleteAllMacros [winfo toplevel %W]
		}
	    }
	}

	GidHelp $w.f1.b2 [_ "Deletes current macro.\nControl-button1 deletes all macros."]
	button $w.f1.b3 -image [ibitmapfrom record19x15 red] \
		-command "McrRecordMacro $w"
	GidHelp $w.f1.b3 [_ "After pressing this button, some actions can be\
		performed in the program and they will be recorded in the\
		macro. Press later the create new button."]
	button $w.f1.b4 -image [imagefrom go18x15.gif] \
		-command "McrExecuteMacro $w"
	GidHelp $w.f1.b4 [_ "Execute current macro."]
	button $w.f1.b5 -image [imagefrom open15.gif] \
		-command "McrReadMacrosFile $w"
	GidHelp $w.f1.b5 [_ "Import macros from file."]
	button $w.f1.b6 -image [imagefrom save15.gif] \
		-command "McrWriteMacrosFile $w"
	GidHelp $w.f1.b6 [_ "Export macros to file."]
	pack $w.f1.c1 $w.f1.b1 $w.f1.b2 $w.f1.b3 $w.f1.b4 $w.f1.b5 $w.f1.b6 -side left

	label $w.title1 -text [_ "Current macro code:"]

	frame $w.middle

	frame $w.middle.but -relief groove -bd 2
	button $w.middle.but.b1 -image [ibitmapfrom downdoublearrow16x16 red] \
		-command "McrViewMacro $w"
	GidHelp $w.middle.but.b1 [_ "Shows the code of the selected macro"]
	
	button $w.middle.but.b2 -image [imagefrom erase-cross.gif] \
		-command "$w.middle.t del 1.0 end"
	GidHelp $w.middle.but.b2 [_ "Clears active code. It does not change\
		any macro definition."]

	button $w.middle.but.b3 -image [ibitmapfrom arrow-l #903e9c] \
		-command "McrInsertEscape $w.middle.t 0"

	GidHelp $w.middle.but.b3 [_ "Inserts a 'escape' code"]

	button $w.middle.but.b4 -image [ibitmapfrom arrow-l-plus #903e9c] \
		-command "McrInsertEscape $w.middle.t 1"
	GidHelp $w.middle.but.b4 [_ "Inserts a set of 'escape' codes (4)"]

	button $w.middle.but.b5 -image [imagefrom tcltk16.gif] \
		-command "McrInsertTclTk $w.middle.t"
	GidHelp $w.middle.but.b5 [_ "Insert (deletes) a marker to show that the line will be\
		interpreted as TCL"]

	pack $w.middle.but.b1 $w.middle.but.b2 $w.middle.but.b3 \
		$w.middle.but.b4 $w.middle.but.b5 -side top

	text $w.middle.t -relief sunken \
		-yscroll "$w.middle.yscroll set" -width 35 -height 5
	$w.middle.t delete 0.0 end
	scrollbar $w.middle.yscroll -relief sunken \
		-orient vertical -command "$w.middle.t yview"

	frame $w.middle.sc
	label $w.middle.sc.t -text [_ "Current accelerators"] -justify center -wraplength 80
	listbox $w.middle.sc.l -width 6 -height 4

	button $w.middle.sc.b -image [imagefrom erase-cross.gif] -command \
		"McrDeleteAcceleratorKey $w"
	GidHelp $w.middle.sc.b [_ "Delete selected accelerator from macro"]
	pack $w.middle.sc.t -side top 
	pack $w.middle.sc.b -side bottom -anchor e
	pack $w.middle.sc.l -side top -fill both -expand 1

	grid $w.middle.but $w.middle.t $w.middle.yscroll $w.middle.sc \
		-sticky nsew -ipady 3 -row 1
	grid columnconf $w.middle 1 -weight 1
	grid rowconf $w.middle 1 -weight 1

	frame $w.shortcut
	frame $w.tecla
	label $w.tecla.l -text [_ "Keyboard Shortcut:"]
	entry $w.tecla.e -relief sunken -textvar McrPriv(currentshortcut)
	button $w.tecla.b -text [_ "Add"] -command "McrAddAcceleratorKey $w" \
		-und 0
	pack $w.tecla.l $w.tecla.e $w.tecla.b -side left


	bind $w.tecla.e <KeyPress> "McrPressAcceleratorkey %W %K %A; break"
	trace var McrPriv(currentshortcut) w "McrShowAccCode $w ;#"

	label $w.title2 -text [_ "Current keyboard shortcut code:"]
	
	frame $w.down
	text $w.down.t -relief sunken \
		-yscroll "$w.down.yscroll set" -width 10 -height 5
	$w.down.t delete 0.0 end
	$w.down.t conf -state disabled
	scrollbar $w.down.yscroll -relief sunken \
		-orient vertical -command "$w.down.t yview"

	grid $w.down.t $w.down.yscroll -sticky nsew -ipady 3
	grid columnconf $w.down 0 -weight 1
	grid rowconf $w.down 0 -weight 1

	label $w.title2comments -text [_ "Comments"]:
	
	frame $w.comments
	text $w.comments.t -relief sunken \
		-yscroll "$w.comments.yscroll set" -width 10 -height 5
	$w.comments.t delete 0.0 end
	scrollbar $w.comments.yscroll -relief sunken \
		-orient vertical -command "$w.comments.t yview"

	grid $w.comments.t $w.comments.yscroll -sticky nsew -ipady 3
	grid columnconf $w.comments 0 -weight 1
	grid rowconf $w.comments 0 -weight 1


	grid $w.tecla -in $w.shortcut -row 1 -column 1 -columnspan 2 -sticky w
	grid $w.title2 -in $w.shortcut -row 2 -column 1 -sticky w
	grid $w.down -in $w.shortcut -row 3 -column 1 -sticky ewns -padx 3 -pady 2
	grid $w.title2comments -in $w.shortcut -row 2 -column 2 -sticky w -padx 3 -pady 2
	grid $w.comments -in $w.shortcut -row 3 -column 2 -sticky ewns
	grid columnconf $w.shortcut 1 -weight 3
	grid columnconf $w.shortcut 2 -weight 1
	grid rowconf $w.shortcut 3 -weight 1

	frame $w.buts -relief ridge -bd 2
	button $w.buts.close -text [_ "Close"] -width 6 -command "destroy $w" \
		-und 0
	grid $w.buts.close -row 1 -column 1

	bind $w <Alt-c> "$w.buts.close invoke ;break"
	bind $w <Escape> "$w.buts.close invoke"

	grid $w.f1 -row 1 -column 1 -sticky ew -pady 3
	grid $w.title1 -row 2 -column 1 -sticky w 
	grid $w.middle -row 3 -column 1 -sticky nsew -padx 3
	grid $w.shortcut  -row 4 -column 1 -sticky ewns
	grid $w.buts  -row 5 -column 1 -sticky ew 

	grid columnconf $w 1 -weight 1 -pad 15
	grid rowconf $w 3 -weight 1
	grid rowconf $w 4 -weight 1

    } else { wm deiconify $w ; raise $w}

    if { $McrPriv(currentmacro) == "" && [array size MacrosList] } {
	set McrPriv(currentmacro) [lindex [array names MacrosList] 0]
    }

}

#----------------------Macros ALONE---------------------------
if { $MacrosRunningAlone == 1 } {
    McrBegin .macros
} 
#-----------------------------------------------------------------
