Changeset 910 for trunk/gsascmds.tcl


Ignore:
Timestamp:
Dec 4, 2009 5:14:10 PM (11 years ago)
Author:
toby
Message:

# on 2008/04/15 17:20:39, toby did:
macro recording
implement prompt/no prompt in GSAS window
implement run macros
new proc to run GSAS programs w/o a terminal window

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsascmds.tcl

    • Property rcs:date changed from 2006/03/29 03:52:11 to 2008/04/15 17:20:39
    • Property rcs:lines changed from +11 -1 to +404 -12
    • Property rcs:rev changed from 1.63 to 1.64
    r876 r910  
    735735    foreach phase $phaselist hist $histlist {
    736736        hapinfo $hist $phase proftype set $expgui(newpeaktype)
     737        RecordMacroEntry "hapinfo $hist $phase proftype set $expgui(newpeaktype)" 0
    737738        hapinfo $hist $phase profterms set $expgui(newProfileTerms)
     739        RecordMacroEntry "hapinfo $hist $phase profterms set $expgui(newProfileTerms)" 0
    738740        for {set i 1} {$i <=  $expgui(newProfileTerms)} {incr i} {
    739741            hapinfo $hist $phase pterm$i set [$w.ent${i} get]
     742            RecordMacroEntry "hapinfo $hist $phase pterm$i set [$w.ent${i} get]" 0
    740743            hapinfo $hist $phase pref$i set $expgui(ProfRef$i)
     744            RecordMacroEntry "hapinfo $hist $phase pref$i set $expgui(ProfRef$i)" 0
    741745        }
    742746        set i [expr 1+$expgui(newProfileTerms)]
    743747        hapinfo $hist $phase pcut set [$w.ent$i get]
     748        RecordMacroEntry "hapinfo $hist $phase pcut set [$w.ent$i get]" 0
    744749        incr expgui(changed) [expr 3 + $expgui(newProfileTerms)]
     750        RecordMacroEntry "incr expgui(changed)" 0
    745751    }
    746752}
     
    11401146    # Save the current exp file
    11411147    savearchiveexp
     1148    CantRecordMacroEntry "bkgedit"
    11421149    # disable the file change monitor if we will reload the .EXP file automatically
    11431150    if {$expgui(autoexpload)} {set expgui(expModifiedLast) 0}
     
    11581165# run excledt
    11591166proc excledit {} {
    1160     global expgui liveplot wishshell expmap
     1167    global expgui liveplot expmap
    11611168    set expnam [file root [file tail $expgui(expfile)]]
    11621169    # which histograms are ready for use?
     
    13361343# run a GSAS program that does not require an experiment file
    13371344proc runGSASprog {proglist "concurrent 1"} {
     1345    # save call to Macro file
     1346    RecordMacroEntry "runGSASprog [list $proglist] $concurrent" 0
    13381347    # if concurrent is 0, EXPGUI runs the GSAS program in background
    13391348    # -- this is not currently needed anywhere where the .EXP file is not.
     
    13581367# run a GSAS program that requires an experiment file for input/output
    13591368proc runGSASwEXP {proglist "concurrent 0"} {
     1369    # save call to Macro file
     1370    RecordMacroEntry "runGSASwEXP [list $proglist] $concurrent" 0
    13601371    # most programs that require the .EXP file change it and
    13611372    # cannot be run concurrently
     
    13931404        if {$tcl_platform(platform) == "windows"} {
    13941405            append cmd " \"$expgui(gsasexe)/${prog}.exe $expnam \" "
     1406        } elseif {$expgui(MacroRunning) && !$expgui(ShowGENLES)} {
     1407            append cmd " \" [file join $expgui(gsasexe) $prog] $expnam \" "
    13951408        } else {
    13961409            if {$cmd != ""} {append cmd "\;"}
     
    13981411        }
    13991412    }
    1400     forknewterm "$prog -- $expnam" $cmd [expr !$concurrent] 1
     1413    if {$expgui(MacroRunning) && !$expgui(ShowGENLES)} {
     1414        set outfile ${expnam}_macout.LST
     1415        runnoterm $cmd $outfile
     1416    } else {
     1417        forknewterm "$prog -- $expnam" $cmd [expr !$concurrent] 1
     1418    }
    14011419    # load the changed .EXP file automatically?
    14021420    if {$expgui(autoexpload)} {
    14031421        # load the revised exp file
    14041422        loadexp $expgui(expfile)
     1423    }
     1424    if {$expgui(MacroRunning)} {
     1425        if {[file exists  abort_${expnam}_macro.flag]} {
     1426            file delete abort_${expnam}_macro.flag
     1427            error "User requested to abort the macro"
     1428        }
     1429        update idletasks
    14051430    }
    14061431}
     
    21762201            if {$expgui(useflag_$i) != [histinfo $i use]} {
    21772202                histinfo $i use set $expgui(useflag_$i)
     2203                RecordMacroEntry "histinfo $i use set $expgui(useflag_$i)" 0
    21782204                incr expgui(changed)
     2205                RecordMacroEntry "incr expgui(changed)" 0
    21792206            }
    21802207#       }
     
    24642491            return
    24652492        }
    2466         # N. B. pause is now hard coded in the .BAT file
    2467         #
    24682493        # loop over multiple commands
    24692494        foreach cmd $command {
     
    24932518                checklockfile expgui.lck .lock
    24942519            }
     2520
     2521            # pause is hard coded in the GSASTCL.BAT file
     2522            if {$expgui(execprompt)} {
     2523                set script gsastcl.bat
     2524            } else {
     2525                set script gsasnowt.bat
     2526            }
     2527
    24952528            # replace the forward slashes with backward
    24962529            regsub -all / $cmd \\ cmd
    24972530            if {[info command winutils::shell] != ""} {
    2498                 winutils::shell [file join $expgui(scriptdir) gsastcl.bat] $cmd
     2531                winutils::shell [file join $expgui(scriptdir) $script] $cmd
    24992532            } else {
    25002533                winexec -d [file nativename [pwd]] \
    2501                     [file join $expgui(scriptdir) gsastcl.bat] $cmd
     2534                    [file join $expgui(scriptdir) $script] $cmd
     2535            }
     2536            if {$expgui(MacroRunning)} {
     2537                update
     2538                update idletasks
    25022539            }
    25032540            if {$wait} {
     
    25322569        }
    25332570        # pause is hard coded in the .BAT file
     2571        if {$expgui(execprompt)} {
     2572            set script gsastcl.bat
     2573        } else {
     2574            set script gsasnowt.bat
     2575        }
    25342576
    25352577        if {$wait} {
     
    25422584                regsub -all / $cmd \\ cmd
    25432585                exec $env(COMSPEC) /c \
    2544                         "start [file join $expgui(scriptdir) gsastcl.bat] $cmd"
     2586                        "start [file join $expgui(scriptdir) $script] $cmd"
    25452587            }
    25462588            file delete -force expgui.lck
     
    25552597                # run in background
    25562598                exec $env(COMSPEC) /c \
    2557                         "start [file join $expgui(scriptdir) gsastcl.bat] $cmd" &
     2599                        "start [file join $expgui(scriptdir) $script] $cmd" &
     2600                if {$expgui(MacroRunning)} {
     2601                    update
     2602                    update idletasks
     2603                }
    25582604            }
    25592605        }
     
    25882634            set suffix {&}
    25892635        }
    2590         #
    2591         #if $wait {
     2636
     2637        # hold window open after commands finish
     2638        if {$expgui(execprompt)} {
    25922639            append command "\; echo -n Press Enter to continue \; read x"
    2593         #}
     2640        }
    25942641        if {$wait && $expgui(autoiconify)} {wm iconify .}
    25952642        catch {eval exec xterm $termopts -title [list $title] \
    25962643                -e /bin/sh -c [list $command] $suffix} errmsg
    25972644        if $expgui(debug) {puts "xterm result = $errmsg"}
     2645        if {$expgui(MacroRunning)} {
     2646            update
     2647            update idletasks
     2648        }
    25982649        if {$wait} {
    25992650            if {$expgui(autoiconify)} {wm deiconify .}
     
    26022653        }
    26032654    }
     2655}
     2656
     2657# run commands without a terminal window
     2658proc runnoterm {command outfile} {
     2659    global env expgui tcl_platform
     2660    if {$tcl_platform(platform) == "windows"} {
     2661        # Windows environment variables
     2662        set env(GSAS) [file nativename $expgui(gsasdir)]
     2663        # PGPLOT_FONT is needed by PGPLOT
     2664        set env(PGPLOT_FONT) [file nativename [file join $expgui(gsasdir) pgl grfont.dat]]
     2665        # this is the number of lines/page in the .LST (etc.) file
     2666        set env(LENPAGE) 60
     2667        set pwd [file nativename [pwd]]
     2668        # loop over multiple commands
     2669        foreach cmd $command {
     2670            # replace the forward slashes with backward
     2671            regsub -all / $cmd \\ cmd
     2672            exec $cmd >>& $outfile
     2673            update
     2674            update idletasks
     2675        }
     2676    } else {
     2677        # UNIX environment variables
     2678        set env(GSAS) [file nativename $expgui(gsasdir)]
     2679        set env(gsas) [file nativename $expgui(gsasdir)]
     2680        set env(GSASEXE) $expgui(gsasexe)
     2681        set env(ATOMDATA) [file join $expgui(gsasdir) data atmdata.dat]
     2682        set env(ATMXSECT) [file join $expgui(gsasdir) data atmxsect.dat]
     2683        # PGPLOT_DIR is needed by PGPLOT
     2684        set env(PGPLOT_DIR) [file join $expgui(gsasdir) pgl]
     2685        # this is the number of lines/page in the .LST (etc.) file
     2686        set env(LENPAGE) 60
     2687        foreach cmd $command {
     2688            catch {eval exec $cmd >>& $outfile} errmsg
     2689        }
     2690        update
     2691        update idletasks
     2692    }
     2693    # check for changes in the .EXP file immediately
     2694    #whenidle
    26042695}
    26052696
     
    26522743    }
    26532744}
     2745
     2746#-------------------------------------------------------------------------------
     2747# Macro Recording
     2748#-------------------------------------------------------------------------------
     2749set expgui(MacroBufferedCommand) ""
     2750set expgui(fpMacroFile) ""
     2751set expgui(MacroFile) ""
     2752# Turn on/off mode to save commands in MacroFile
     2753proc SetRecordMacroOnOff {args} {
     2754    global expgui
     2755    if {$expgui(RecordMacro)} {
     2756        set expgui(fpMacroFile) ""
     2757        set expgui(MacroBufferedCommand) ""
     2758        while {$expgui(fpMacroFile) == ""} {
     2759            set expgui(MacroFile) [tk_getSaveFile -initialdir [pwd] \
     2760                                       -parent . \
     2761                                       -filetypes {{"EXPGUI Macro file" .expmac}} \
     2762                                       -defaultextension .expmac  \
     2763                                       -initialfile EXPGUI.expmac \
     2764                                       -title "Choose location to save macro"]
     2765            if {$expgui(MacroFile) == ""} {
     2766                # respond to cancel
     2767                set expgui(fpMacroFile) ""
     2768                set expgui(MacroFile) ""
     2769                set expgui(RecordMacro) 0
     2770                return
     2771            }
     2772            if {[catch {
     2773                set expgui(fpMacroFile) [open $expgui(MacroFile) w]
     2774                puts $expgui(fpMacroFile) "# [clock format [clock seconds] -format %Y-%m-%dT%T]"
     2775            } errmsg]} {
     2776                MyMessageBox -parent . -title "Error opening selected file" \
     2777                    -message "Error opening macro file:\n$errmsg" \
     2778                    -icon warning -type TryAgain -default tryagain
     2779                catch {close $expgui(fpMacroFile)}
     2780                set expgui(fpMacroFile) ""
     2781                set expgui(MacroFile) ""
     2782                set expgui(RecordMacro) 0
     2783            }
     2784        }
     2785    } else {
     2786        if {[string trim $expgui(MacroBufferedCommand)] != ""} {
     2787            puts $expgui(fpMacroFile) $expgui(MacroBufferedCommand)
     2788        }
     2789        catch {close $expgui(fpMacroFile)}
     2790        set expgui(fpMacroFile) ""
     2791        set expgui(MacroFile) ""
     2792        set expgui(MacroBufferedCommand) ""
     2793    }
     2794}
     2795
     2796# record a command in the Macro File
     2797proc RecordMacroEntry {command buffer} {
     2798    global expgui
     2799    if {! $expgui(RecordMacro)} return
     2800    # in buffered mode: hold the last command in memory and compare to the
     2801    # next. If two commands differ only in the final argument, then the
     2802    # second command makes the previous redundant so only the latter version
     2803    # is retained (This will happen when a user types a string into a box).
     2804    # When the commands differ, then the previous is written to file
     2805    # and the next is retained in memory.
     2806    if {$buffer} {
     2807        if {[string trim $expgui(MacroBufferedCommand)] == ""} {
     2808            set expgui(MacroBufferedCommand) $command
     2809            return
     2810        }
     2811        set diff 0
     2812        # is command a repeat of previous?
     2813        foreach a $command b $expgui(MacroBufferedCommand) {
     2814            if {$diff} {
     2815                # found a difference, other than in the last arg
     2816                puts $expgui(fpMacroFile) $expgui(MacroBufferedCommand)
     2817                break
     2818            }
     2819            if {$a != $b} {set diff 1}
     2820        }
     2821        set expgui(MacroBufferedCommand) $command
     2822    } else {
     2823        # no buffering on current command; write the old and new to file.
     2824        if {[string trim $expgui(MacroBufferedCommand)] != ""} {
     2825            puts $expgui(fpMacroFile) $expgui(MacroBufferedCommand)
     2826        }
     2827        puts $expgui(fpMacroFile) $command
     2828        set expgui(MacroBufferedCommand) ""
     2829    }
     2830}
     2831
     2832proc CantRecordMacroEntry {comment} {
     2833    global expgui
     2834    if {! $expgui(RecordMacro)} return
     2835
     2836    # no buffering on current command; write the old and new to file.
     2837    if {[string trim $expgui(MacroBufferedCommand)] != ""} {
     2838        puts $expgui(fpMacroFile) $expgui(MacroBufferedCommand)
     2839    }
     2840    puts $expgui(fpMacroFile) "# unrecorded: $comment"
     2841    set expgui(MacroBufferedCommand) ""
     2842    MyMessageBox -parent . -title "No command record" \
     2843        -message "EXPGUI is not able to record this action in the macro file: $comment" \
     2844        -icon warning
     2845}
     2846
     2847
     2848# Play back commands in Macro File
     2849proc ReplayMacroFile {"lineatatime 0"} {
     2850    global expgui
     2851    set expnam [file root [file tail $expgui(expfile)]]
     2852    file delete abort_${expnam}_macro.flag
     2853    set expgui(MacroRunning) 0
     2854    set MacroFile [tk_getOpenFile -initialdir [pwd] \
     2855                       -parent . \
     2856                       -filetypes {{"EXPGUI Macro file" .expmac} {Everything .*}} \
     2857                       -defaultextension .expmac  \
     2858                       -title "Choose location to read macro"]
     2859    if {$MacroFile == ""} return
     2860    set expgui(MacroRunning) 1
     2861    if {$lineatatime} {
     2862        set expgui(MacroChanged) 0
     2863        set top1 .macro
     2864        catch {destroy $top1}
     2865        toplevel $top1
     2866        set txt $top1.t
     2867        grid [text $txt -width 30 -height 20 -yscrollcommand "$top1.s set"] \
     2868            -column 0 -row 0 -sticky news
     2869        wm title $top1 "File $MacroFile"
     2870        grid [scrollbar $top1.s -command "$txt yview"] \
     2871            -column 1 -row 0 -sticky ns
     2872        grid [frame $top1.b] -column 0 -columnspan 2 -row 1 -sticky ew
     2873        grid columnconfig $top1 0 -weight 1
     2874        grid rowconfig $top1 0 -weight 1
     2875        grid [button $top1.b.e -text "Execute line" \
     2876                  -command "MacroExecuteCurrentLine $txt"] \
     2877            -column 0 -row 0 -sticky w
     2878        grid columnconfig $top1.b 1 -weight 1
     2879        grid [button $top1.b.s -text "Save As" -state disabled \
     2880                  -command "MacroResave $txt"] -column 1 -row 0
     2881        set expgui(MacroSaveButton) $top1.b.s
     2882        grid [button $top1.b.c -text "Close " \
     2883                  -command "MacroCloseWindow $txt"] -column 2 -row 0
     2884        $txt delete 0.0 end
     2885        set fp [open $MacroFile r]
     2886        $txt insert 0.0 [read $fp]
     2887        close $fp
     2888        MacroHighlightText $txt 1
     2889        # deal with editing in the box
     2890        $txt configure -undo 1
     2891        $txt edit modified 0
     2892        bind $txt <<Modified>> {
     2893            $expgui(MacroSaveButton) configure -state normal
     2894            set expgui(MacroChanged) 1
     2895        }
     2896    } else {
     2897        close [open running_${expnam}_macro.flag w]
     2898        set saveprompt $expgui(execprompt)
     2899        set saveautold $expgui(autoexpload)
     2900        set expgui(execprompt) 0
     2901        set expgui(autoexpload) 1
     2902        set expnam [file root [file tail $expgui(expfile)]]
     2903        if {$expgui(MacroRunning) && !$expgui(ShowGENLES)} {
     2904            set outfile ${expnam}_macout.LST
     2905            # create an empty file
     2906            catch {file delete $outfile}
     2907            close [open $outfile w]
     2908            # view it with LSTVIEW
     2909            set outfile ${expnam}_macout
     2910            exec $::wishshell [file join $expgui(scriptdir) lstview] $outfile &
     2911        } else {
     2912            # show status, offer abort with MACROMON
     2913            exec $::wishshell [file join $expgui(scriptdir) macromon] $expnam &
     2914        }
     2915
     2916        set  expgui(MacroStatus) "starting script"
     2917        pleasewait "\nrunning macro\n\nStatus:" expgui(MacroStatus)
     2918
     2919        if {[catch {
     2920            source $MacroFile
     2921        } errmsg]} {
     2922            set txt $::errorInfo
     2923            catch {
     2924                set fp [open error.txt a]
     2925                puts $fp "#  [clock format [clock seconds] -format %Y-%m-%dT%T]"
     2926                puts $fp $txt
     2927                close $fp
     2928            }
     2929            donewait
     2930            MyMessageBox -parent . -title "Error running Macro file" \
     2931                -message "Error running macro file:\n$errmsg\n(details in file error.txt)" \
     2932                -icon error -type OK -default ok
     2933        } else {
     2934            donewait
     2935        }
     2936        file delete running_${expnam}_macro.flag
     2937        set expgui(execprompt) $saveprompt
     2938        set expgui(autoexpload) $saveautold
     2939        set expgui(MacroRunning) 0
     2940        # show changes
     2941        PaintEXPGUIpages
     2942        # put comment in output file
     2943        if {$expgui(MacroRunning) && !$expgui(ShowGENLES)} {
     2944            set outfile ${expnam}_macout.LST
     2945            set fp [open $outfile a]
     2946            puts $fp "\n**** Macro ended ****"
     2947            close $fp
     2948        }
     2949    }
     2950}
     2951
     2952# highlight a line in the Macro file display
     2953proc MacroHighlightText {txt line} {
     2954    $txt tag delete next
     2955    $txt tag add next $line.0 $line.end
     2956    $txt see $line.0
     2957    $txt tag configure next -background yellow
     2958    # tag all text
     2959    $txt tag delete all
     2960    $txt tag add all 0.0 end
     2961    # double-click moves the current line
     2962    $txt tag bind all <Double-1> "after idle [list MacroDoubleClick $txt]"
     2963}
     2964
     2965# respond to a double click by moving the next line to be executed to
     2966# the line where the double click occurred
     2967proc MacroDoubleClick {txt} {
     2968    set line [lindex [split [$txt tag ranges sel] "."] 0]
     2969    MacroHighlightText $txt $line
     2970}
     2971
     2972# respond to Execute button: execute the current line
     2973# close window after last command
     2974proc MacroExecuteCurrentLine {txt} {
     2975    global expgui
     2976    set linenum [lindex [split [$txt tag ranges next] "."] 0]
     2977    if {$linenum == ""} {return}
     2978    set line [$txt get $linenum.0 $linenum.end]
     2979    # is this continued (ends with \)?
     2980    while {[string range $line end end] == "\\" } {
     2981        incr linenum
     2982        # get rid of trailing backslash
     2983        set line [string range $line 0 end-1]
     2984        #append next line
     2985        append line [$txt get $linenum.0 $linenum.end]
     2986    }
     2987     if {[catch $line errmsg]} {
     2988        MyMessageBox -parent $txt -title "Error on line" \
     2989            -message "Error on line $linenum:\n$errmsg" \
     2990            -icon warning -type Continue -default continue
     2991    }
     2992    # show changes
     2993    PaintEXPGUIpages
     2994    # move forward in macrofile
     2995    incr linenum
     2996    MacroHighlightText $txt $linenum
     2997    set linenum [lindex [split [$txt tag ranges next] "."] 0]
     2998    # at end?
     2999    if {$linenum == ""} {MacroCloseWindow $txt}
     3000}
     3001
     3002# Save a modified macro file
     3003proc MacroResave {txt} {
     3004    global expgui
     3005    set MacroFile [tk_getSaveFile -initialdir [pwd] \
     3006                       -parent $txt \
     3007                       -filetypes {{"EXPGUI Macro file" .expmac}} \
     3008                       -defaultextension .expmac  \
     3009                       -initialfile $expgui(MacroFile) \
     3010                       -title "Choose location to save macro"]
     3011    if {[string trim $MacroFile] == ""} {return}
     3012    if {[catch {
     3013        set fp [open $MacroFile w]
     3014        puts $fp [string trim [$txt get 0.0 end]]
     3015        close $fp
     3016    } errmsg]} {
     3017        MyMessageBox -parent $txt -title "Error writing to file" \
     3018            -message "Error writing macro file:\n$errmsg" \
     3019            -icon warning -type TryAgain -default tryagain
     3020        return
     3021    }
     3022    set expgui(MacroChanged) 0
     3023    # gray out the button
     3024    $expgui(MacroSaveButton) configure -state disabled
     3025}
     3026
     3027# close the window, but provide a chance to save the file first, if modified
     3028proc MacroCloseWindow {txt} {
     3029    global expgui
     3030    if {$expgui(MacroChanged)} {
     3031        set ans [MyMessageBox -parent $txt -title "Save macro file?" \
     3032                     -message "Macro file has been changed, do you want to save it?" \
     3033                     -icon warning -type "Yes No" -default no]
     3034        if {$ans != "no"} {MacroResave $txt}
     3035    }
     3036    set expgui(MacroRunning) 0
     3037    destroy [winfo toplevel $txt]
     3038}
     3039
     3040# Add a comment to a macro file
     3041proc AddCommentMacroFile {} {
     3042    global expgui
     3043    if {! $expgui(RecordMacro)} return
     3044    RecordMacroEntry "# [getstring "comment for macro file"]" 0
     3045}
Note: See TracChangeset for help on using the changeset viewer.