Difference between revisions of "TMGTPSTP.m"

From VistApedia
Jump to: navigation, search
Line 1: Line 1:
        ;"------------------------------------------------------------
+
;"------------------------------------------------------------
        ;"------------------------------------------------------------
+
;"------------------------------------------------------------
        ;"
+
;"
        ;" GT.M STEP TRAP
+
;" GT.M STEP TRAP
        ;"
+
;"
        ;" K. Toppenberg
+
;" K. Toppenberg
        ;" 4-13-2005
+
;" 4-13-2005
        ;" License: GPL Applies
+
;" License: GPL Applies
        ;"  
+
;"
        ;" This code module will allow tracing through code.
+
;" This code module will allow tracing through code.
        ;" It is used as follows:
+
;" It is used as follows:
        ;"
+
;"
        ;" set $ZSTEP="do STEPTRAP^TMGTRSTP($ZPOS) zstep into zcontinue"
+
;" set $ZSTEP="do STEPTRAP^TMGTRSTP($ZPOS) zstep into zcontinue"
        ;" zstep into
+
;" zstep into
        ;" do ^MyFunction  ;"<--- put the function you want to trace here
+
;" do ^MyFunction  ;"<--- put the function you want to trace here
        ;"
+
;"
        ;" set $ZSTEP=""  ;"<---turn off step capture
+
;" set $ZSTEP=""  ;"<---turn off step capture
        ;" quit
+
;" quit
        ;"
+
;"
        ;"
+
;"
        ;" Dependencies:
+
;" Dependencies:
        ;"  Uses TMGTERM
+
;"  Uses TMGTERM
        ;"
+
;"
        ;"Notes:
+
;"Notes:
        ;"  This function will be called inbetween lines of the main  
+
;"  This function will be called inbetween lines of the main
        ;"  program that is being traced.  Thus is function can't do
+
;"  program that is being traced.  Thus is function can't do
        ;"  anything that might change the environment of the main
+
;"  anything that might change the environment of the main
        ;"  program.  This includes accessing global variables --
+
;"  program.  This includes accessing global variables --
        ;"  because it will mess up the "naked reference".
+
;"  because it will mess up the "naked reference".
        ;"------------------------------------------------------------
+
;"------------------------------------------------------------
        ;"------------------------------------------------------------
+
;"------------------------------------------------------------
       
+
    STEPTRAP(Pos)
+
STEPTRAP(idePos,Msg)
                ;"Purpose: This is the line that is called by GT.M for each zstep event.
+
        ;"Purpose: This is the line that is called by GT.M for each zstep event.
                ;" It will be used to display the current code execution point, and  
+
        ;"     It will be used to display the current code execution point, and
                ;" query user as to plans for future execution: run/step/ etc.
+
        ;"     query user as to plans for future execution: run/step/ etc.
          
+
         ;"Input: idePos -- a text line containing position, as returned bye $ZPOS
         new tpBlankLine
+
         ;"          Msg -- OPTIONAL -- can be used by programs to pass in info.
        new tpAction
+
         ;"                      If Msg=1, then this function was called without the
        new tpKeyIn
+
         ;"                              $ZTEP value set, so this function should set it.
         new tpRunMode,tpStepMode
+
         new tpI
+
      new tpBlankLine
        new tpDone
+
      new tpAction
        new result set result=1  ;1=step into, 2=step over
+
      new tpKeyIn
       
+
      new tpRunMode,tpStepMode
        ;"Run modes: 0=running mode
+
      new tpI
        ;"          1=stepping mode  
+
      new tpDone
        ;"          2=Don't show code  
+
      new result set result=1  ;1=step into, 2=step over
        ;"     3=running SLOW mode
+
        ;"          -1=quit
+
      ;"Run modes: 0=running mode
       
+
      ;"          1=stepping mode
        set tpRunMode=$get(TMGRunMode,1)
+
      ;"          2=Don't show code
        set tpStepMode=$get(TMGStepMode,"into")
+
      ;"           3=running SLOW mode
       
+
      ;"          -1=quit
        new ScrHeight,ScrWidth
+
        set ScrHeight=$get(TMGScrHeight,10)
+
      set tpRunMode=$get(TMGRunMode,1)
        set ScrWidth=$get(TMGScrWidth,80)
+
      set tpStepMode=$get(TMGStepMode,"into")
       
+
        set tpBlankLine=" "
+
      new ScrHeight,ScrWidth
        for tpI=1:1:ScrWidth-1 set tpBlankLine=tpBlankLine_" "
+
      set ScrHeight=$get(TMGScrHeight,10)
       
+
      set ScrWidth=$get(TMGScrWidth,80)
        do VCUSAV2^TMGTERM
+
        if tpRunMode'=2 do
+
      set tpBlankLine=" "
        . do ShowCodePos(Pos,ScrWidth,ScrHeight)
+
      for tpI=1:1:ScrWidth-1 set tpBlankLine=tpBlankLine_" "
        else  do
+
        . do CUP^TMGTERM(1,2)
+
      do VCUSAV2^TMGTERM
        write tpBlankLine,!
+
      if tpRunMode'=2 do
        write tpBlankLine,!
+
      . do ShowCodePos(idePos,ScrWidth,ScrHeight)
        do CUU^TMGTERM(2)
+
      else  do
       
+
      . do CUP^TMGTERM(1,2)
        if (tpRunMode=0)!(tpRunMode=3)!(tpRunMode=2) do
+
      write tpBlankLine,!
        . write tpBlankLine,!
+
      write tpBlankLine,!
        . do CUU^TMGTERM(1)
+
      do CUU^TMGTERM(2)
        . write "(Press any key to pause)",!
+
        . read *tpKeyIn:0
+
      if (tpRunMode=0)!(tpRunMode=3)!(tpRunMode=2) do
        . if (tpKeyIn>0) set tpRunMode=1
+
      . write tpBlankLine,!
        . else  if tpRunMode=3 hang 1
+
      . do CUU^TMGTERM(1)
       
+
      . write "(Press any key to pause)",!
        if tpRunMode=2 goto SPDone ;"Don't showmode --> goto SPDone
+
      . read *tpKeyIn:0
       
+
      . if (tpKeyIn>0) set tpRunMode=1
        set tpDone=0
+
      . else  if tpRunMode=3 hang 1
        if tpRunMode=1 for  do  quit:tpDone=1
+
        . new DefAction set DefAction="O"
+
      if tpRunMode=2 goto SPDone ;"Don't showmode --> goto SPDone
        . do ShowCodePos(Pos,ScrWidth,ScrHeight)
+
        . do CUP^TMGTERM(1,ScrHeight+4) ;"Cursor to line (x,y)  
+
      set tpDone=0
        . write tpBlankLine,!
+
      if tpRunMode=1 for  do  quit:tpDone=1
        . do CUU^TMGTERM(1)
+
      . new DefAction set DefAction="O"
        . write "Action (? for help): "
+
      . do ShowCodePos(idePos,ScrWidth,ScrHeight)
        . if tpStepMode="into" write "step INTO// " set DefAction="I"
+
      . do CUP^TMGTERM(1,ScrHeight+4) ;"Cursor to line (x,y)
        . else  write "step OVER// " set DefAction="O"
+
      . write tpBlankLine,!
        . read tpAction,!
+
      . do CUU^TMGTERM(1)
        . if tpAction="" set tpAction=DefAction
+
      . write "Action (? for help): "
        . if "rR"[tpAction do  quit
+
      . if tpStepMode="into" write "step INTO// " set DefAction="I"
        . . set tpRunMode=0
+
      . else  write "step OVER// " set DefAction="O"
        . . set tpDone=1
+
      . read tpAction,!
        . if "lL"[tpAction do  quit
+
      . if tpAction="" set tpAction=DefAction
        . . set tpRunMode=3
+
      . if "rR"[tpAction do  quit
        . . set tpDone=1
+
      . . set tpRunMode=0
        . if "mM"[tpAction do  quit
+
      . . set tpDone=1
        . . write tpBlankLine,!
+
      . if "lL"[tpAction do  quit
        . . do CUU^TMGTERM(1)
+
      . . set tpRunMode=3
        . . new tpLine
+
      . . set tpDone=1
        . . read " enter M code: ",tpLine,!
+
      . if "mM"[tpAction do  quit
        . . xecute tpLine
+
      . . write tpBlankLine,!
        . if "iI"[tpAction do  quit
+
      . . do CUU^TMGTERM(1)
        . . set tpStepMode="into"
+
      . . new tpLine
        . . ;"set $ZSTEP="do STEPTRAP^TMGTPSTP($ZPOS) zstep into zcontinue"
+
      . . read " enter M code: ",tpLine,!
        . . set tpDone=1
+
      . . xecute tpLine
        . if "Oo"[tpAction do  quit
+
      . if "iI"[tpAction do  quit
        . . set tpStepMode="over"
+
      . . set tpStepMode="into"
        . . ;"set $ZSTEP="do STEPTRAP^TMGTPSTP($ZPOS) zstep over zcontinue"
+
      . . ;"set $ZSTEP="do STEPTRAP^TMGTPSTP($ZPOS) zstep into zcontinue"
        . . set tpDone=1
+
      . . set tpDone=1
        . if "Hh"[tpAction do  quit
+
      . if "Oo"[tpAction do  quit
        . . set tpRunMode=2
+
      . . set tpStepMode="over"
        . . set tpDone=1
+
      . . ;"set $ZSTEP="do STEPTRAP^TMGTPSTP($ZPOS) zstep over zcontinue"
        . else  do  quit
+
      . . set tpDone=1
        . . new tpNLines
+
      . if "Bb"[tpAction do  quit
        . . for tpNLines=1:1:5 write tpBlankLine,!
+
      . . new idePos
        . . do CUU^TMGTERM(5)
+
      . . read "Enter breakpoint (e.g. Label+8^MyFunct): ",idePos,!
        . . write " L -- run in sLow mode",!
+
      . . set idePos=Pos_":""n tmg s tmg=$$STEPTRAP^TMGTPSTP($ZPOS,1)"""
        . . write " M  -- enter any line of M code",!
+
      . . ZBREAK @idePos
        . . write " O -- step OVER line",!
+
      . if "Hh"[tpAction do quit
        . . write " I  -- step INTO line",!
+
      . . set tpRunMode=2
        . . write " R -- run",!
+
      . . set tpDone=1
        . . write " H -- Hide debug code",!
+
      . else  do quit
       
+
      . . new tpNLines
    SPDone
+
      . . for tpNLines=1:1:5 write tpBlankLine,!
        do VCULOAD2^TMGTERM
+
      . . do CUU^TMGTERM(5)
        set TMGRunMode=tpRunMode
+
      . . write " L -- run in sLow mode      M  -- enter any line of M code",!
        if tpStepMode="into" set result=1
+
      . . write " O  -- step OVER line        I -- step INTO line",!
        else  set result=2
+
      . . write " R  -- run                      H  -- Hide debug code",!
        set TMGStepMode=tpStepMode
+
      . . write " B  -- set Breakpoint",!
        quit result
+
       
+
SPDone
       
+
      do VCULOAD2^TMGTERM
    ErrTrap(Pos)
+
      set TMGRunMode=tpRunMode
                ;"Purpose: This is the line that is called by GT.M for each ztrap event.
+
      if tpStepMode="into" set result=1
                ;" It will be used to display the current code execution point
+
      else  set result=2
       
+
      set TMGStepMode=tpStepMode
        new ScrHeight,ScrWidth
+
        set ScrHeight=$get(TMGScrHeight,10)
+
      if $get(Msg)=1 do
        set ScrWidth=$get(TMGScrWidth,70)
+
      . set $ZSTEP="N TMGTrap S TMGTrap=$$STEPTRAP^TMGTPSTP($ZPOS) zstep:(TMGTrap=1) into zstep:(TMGTrap=2) over zcontinue"
       
+
      . zstep:(result=1) into zstep:(result=2) over
        do VCUSAV2^TMGTERM
+
        do ShowCodePos(Pos,ScrWidth,ScrHeight)
+
      quit result
          
+
         ETDone
+
        do VCULOAD2^TMGTERM
+
ErrTrap(idePos)
        quit
+
         ;"Purpose: This is the line that is called by GT.M for each ztrap event.
       
+
         ;"     It will be used to display the current code execution point
       
+
       
+
      new ScrHeight,ScrWidth
       
+
      set ScrHeight=$get(TMGScrHeight,10)
     ShowCode(Pos,ScrWidth,ScrHeight,Wipe)
+
      set ScrWidth=$get(TMGScrWidth,70)
        ;"Purpose: This will display code at the top of the screen
+
        ;"Input: Pos -- string like this: X+2^ROUTINE[$DMOD]
+
      do VCUSAV2^TMGTERM
        ;"      ScrWidth -- width of code display (Num of columns)
+
      do ShowCodePos(idePos,ScrWidth,ScrHeight)
        ;
+
        ;"     Wipe -- OPTIONAL.  if 1, then code area is wiped blank
+
ETDone
       
+
      do VCULOAD2^TMGTERM
        new i
+
      quit
        new Routine,Label,Offest,s
+
        new LastRou,LastLabel,LastOffset
+
ShowCode(idePos,ScrWidth,ScrHeight,Wipe)
        new dbFGColor,bBGColor,nlFGColor,nlBGColor
+
      ;"Purpose: This will display code at the top of the screen
        new BlankLine         
+
      ;"Input: idePos -- string like this: X+2^ROUTINE[$DMOD]
        new StartOffset
+
      ;"      ScrWidth -- width of code display (Num of columns)
       
+
      ;
        set ScrWidth=$get(ScrWidth,80)
+
      ;"      Wipe -- OPTIONAL.  if 1, then code area is wiped blank
        set ScrHeight=$get(ScrHeight,10)
+
       
+
      new i
        set nlFGColor=$get(TMGNlFGColor,3)
+
      new Routine,Label,Offest,s
        set nlBGColor=$get(TMGNlBGColor,0)
+
      new LastRou,LastLabel,LastOffset
        set dbFGColor=$get(TMGDbFGColor,0)
+
      new dbFGColor,bBGColor,nlFGColor,nlBGColor
        set dbBGColor=$get(TMGDbBGColor,3)
+
      new BlankLine
       
+
       new StartOffset
        set BlankLine=" "
+
        for i=1:1:ScrWidth-1 set BlankLine=BlankLine_" "
+
      set ScrWidth=$get(ScrWidth,80)
       
+
      set ScrHeight=$get(ScrHeight,10)
        do VCOLORS^TMGTERM(dbFGColor,dbBGColor)
+
       
+
      set nlFGColor=$get(TMGNlFGColor,3)
        do CUP^TMGTERM(1,1) ;"Cursor to line (1,1)  
+
      set nlBGColor=$get(TMGNlBGColor,0)
        write BlankLine,!  ;"This is needed for some reason...
+
      set dbFGColor=$get(TMGDbFGColor,0)
        do CUU^TMGTERM(2)
+
      set dbBGColor=$get(TMGDbBGColor,3)
       
+
        if $get(Wipe)=1 do  goto SCDone
+
      set BlankLine=" "
        . do VCOLORS^TMGTERM(nlFGColor,nlBGColor)
+
      for i=1:1:ScrWidth-1 set BlankLine=BlankLine_" "
        . for i=0:1:ScrHeight+1 write BlankLine
+
       
+
      do VCOLORS^TMGTERM(dbFGColor,dbBGColor)
        set s=$piece(Pos,"$",1)  ;"e.g. X+2^ROUTINE$DMOD-->X+2^ROUTINE
+
        set Routine=$piece(s,"^",2)
+
      do CUP^TMGTERM(1,1) ;"Cursor to line (1,1)
        set Label=$piece(s,"^",1)
+
      write BlankLine,!  ;"This is needed for some reason...
        set Offset=+$piece(Label,"+",2)
+
      do CUU^TMGTERM(2)
        set Label=$piece(Label,"+",1)
+
       
+
      if $get(Wipe)=1 do  goto SCDone
        set s="=== Routine: ^"_Routine_" " write s
+
      . do VCOLORS^TMGTERM(nlFGColor,nlBGColor)
        for i=1:1:ScrWidth-$length(s) write "="
+
      . for i=0:1:ScrHeight+1 write BlankLine
        write !
+
       
+
      set s=$piece(idePos,"$",1)  ;"e.g. X+2^ROUTINE$DMOD-->X+2^ROUTINE
        if Offset>(ScrHeight) do
+
      set Routine=$piece(s,"^",2)
                set StartOffset=(Offset-ScrHeight)
+
      set Label=$piece(s,"^",1)
        else  set StartOffset=0
+
      set Offset=+$piece(Label,"+",2)
       
+
      set Label=$piece(Label,"+",1)
        for i=StartOffset:1:(ScrHeight+StartOffset) do
+
        . new line,Bl,ref,LoopOffset
+
      set s="=== Routine: ^"_Routine_" " write s
        . set ref=Label_"+"_i_"^"_Routine
+
      for i=1:1:ScrWidth-$length(s) write "="
        . set line=$text(@ref)
+
      write !
        . if (i=Offset) do
+
        . . do VCOLORS^TMGTERM(nlFGColor,nlBGColor)
+
      if Offset>(ScrHeight) do
        . . write ">"
+
        set StartOffset=(Offset-ScrHeight)
        . else  write " "
+
      else  set StartOffset=0
        . if $length(line)>(ScrWidth-1) do
+
        . . write $extract(line,1,ScrWidth-4),"...",!
+
      for i=StartOffset:1:(ScrHeight+StartOffset) do
        . else  do
+
      . new line,Bl,ref,LoopOffset
        . . write $extract(line,1,ScrWidth-1)
+
      . set ref=Label_"+"_i_"^"_Routine
        . . write $extract(BlankLine,1,ScrWidth-$length(line)-1),!
+
      . set line=$text(@ref)
        . if (i=Offset) do VCOLORS^TMGTERM(dbFGColor,dbBGColor)
+
      . set line=$$Substitute^TMGSTUTL(line,$Char(9),">>>>>")
       
+
      . if (i=Offset) do
        for i=1:1:ScrWidth write "~"
+
      . . do VCOLORS^TMGTERM(nlFGColor,nlBGColor)
        write !
+
      . . write ">"
       
+
      . else  write " "
    SCDone
+
      . if $length(line)>(ScrWidth-1) do
        ;"do VCULOAD^TMGTERM
+
      . . write $extract(line,1,ScrWidth-4),"...",!
        do VCOLORS^TMGTERM(nlFGColor,nlBGColor)
+
      . else  do
       
+
      . . write $extract(line,1,ScrWidth-1)
        ;"do CUD^TMGTERM(2)
+
      . . write $extract(BlankLine,1,ScrWidth-$length(line)-1),!
       
+
      . if (i=Offset) do VCOLORS^TMGTERM(dbFGColor,dbBGColor)
        quit
+
 +
      for i=1:1:ScrWidth write "~"
 +
      write !
 +
 +
SCDone
 +
      ;"do VCULOAD^TMGTERM
 +
      do VCOLORS^TMGTERM(nlFGColor,nlBGColor)
 +
 +
      ;"do CUD^TMGTERM(2)
 +
 +
      quit

Revision as of 16:51, 21 June 2005

;"------------------------------------------------------------
;"------------------------------------------------------------
;"
;" GT.M STEP TRAP
;"
;" K. Toppenberg
;" 4-13-2005
;" License: GPL Applies
;"
;" This code module will allow tracing through code.
;" It is used as follows:
;"
;" set $ZSTEP="do STEPTRAP^TMGTRSTP($ZPOS) zstep into zcontinue"
;" zstep into
;" do ^MyFunction   ;"<--- put the function you want to trace here
;"
;" set $ZSTEP=""  ;"<---turn off step capture
;" quit
;"
;"
;" Dependencies:
;"   Uses TMGTERM
;"
;"Notes:
;"  This function will be called inbetween lines of the main
;"  program that is being traced.  Thus is function can't do
;"  anything that might change the environment of the main
;"  program.  This includes accessing global variables --
;"  because it will mess up the "naked reference".
;"------------------------------------------------------------
;"------------------------------------------------------------

STEPTRAP(idePos,Msg)
       ;"Purpose: This is the line that is called by GT.M for each zstep event.
       ;"      It will be used to display the current code execution point, and
       ;"      query user as to plans for future execution: run/step/ etc.
       ;"Input: idePos -- a text line containing position, as returned bye $ZPOS
       ;"          Msg -- OPTIONAL -- can be used by programs to pass in info.
       ;"                      If Msg=1, then this function was called without the
       ;"                              $ZTEP value set, so this function should set it.

      new tpBlankLine
      new tpAction
      new tpKeyIn
      new tpRunMode,tpStepMode
      new tpI
      new tpDone
      new result set result=1  ;1=step into, 2=step over

      ;"Run modes: 0=running mode
      ;"           1=stepping mode
      ;"           2=Don't show code
      ;"           3=running SLOW mode
      ;"          -1=quit

      set tpRunMode=$get(TMGRunMode,1)
      set tpStepMode=$get(TMGStepMode,"into")

      new ScrHeight,ScrWidth
      set ScrHeight=$get(TMGScrHeight,10)
      set ScrWidth=$get(TMGScrWidth,80)

      set tpBlankLine=" "
      for tpI=1:1:ScrWidth-1 set tpBlankLine=tpBlankLine_" "

      do VCUSAV2^TMGTERM
      if tpRunMode'=2 do
      . do ShowCodePos(idePos,ScrWidth,ScrHeight)
      else  do
      . do CUP^TMGTERM(1,2)
      write tpBlankLine,!
      write tpBlankLine,!
      do CUU^TMGTERM(2)

      if (tpRunMode=0)!(tpRunMode=3)!(tpRunMode=2) do
      . write tpBlankLine,!
      . do CUU^TMGTERM(1)
      . write "(Press any key to pause)",!
      . read *tpKeyIn:0
      . if (tpKeyIn>0) set tpRunMode=1
      . else  if tpRunMode=3 hang 1

      if tpRunMode=2 goto SPDone ;"Don't showmode --> goto SPDone

      set tpDone=0
      if tpRunMode=1 for  do  quit:tpDone=1
      . new DefAction set DefAction="O"
      . do ShowCodePos(idePos,ScrWidth,ScrHeight)
      . do CUP^TMGTERM(1,ScrHeight+4) ;"Cursor to line (x,y)
      . write tpBlankLine,!
      . do CUU^TMGTERM(1)
      . write "Action (? for help): "
      . if tpStepMode="into" write "step INTO// " set DefAction="I"
      . else  write "step OVER// " set DefAction="O"
      . read tpAction,!
      . if tpAction="" set tpAction=DefAction
      . if "rR"[tpAction do  quit
      . . set tpRunMode=0
      . . set tpDone=1
      . if "lL"[tpAction do  quit
      . . set tpRunMode=3
      . . set tpDone=1
      . if "mM"[tpAction do  quit
      . . write tpBlankLine,!
      . . do CUU^TMGTERM(1)
      . . new tpLine
      . . read " enter M code: ",tpLine,!
      . . xecute tpLine
      . if "iI"[tpAction do  quit
      . . set tpStepMode="into"
      . . ;"set $ZSTEP="do STEPTRAP^TMGTPSTP($ZPOS) zstep into zcontinue"
      . . set tpDone=1
      . if "Oo"[tpAction do  quit
      . . set tpStepMode="over"
      . . ;"set $ZSTEP="do STEPTRAP^TMGTPSTP($ZPOS) zstep over zcontinue"
      . . set tpDone=1
      . if "Bb"[tpAction do  quit
      . . new idePos
      . . read "Enter breakpoint (e.g. Label+8^MyFunct): ",idePos,!
      . . set idePos=Pos_":""n tmg s tmg=$$STEPTRAP^TMGTPSTP($ZPOS,1)"""
      . . ZBREAK @idePos
      . if "Hh"[tpAction do  quit
      . . set tpRunMode=2
      . . set tpDone=1
      . else  do  quit
      . . new tpNLines
      . . for tpNLines=1:1:5 write tpBlankLine,!
      . . do CUU^TMGTERM(5)
      . . write " L  -- run in sLow mode       M  -- enter any line of M code",!
      . . write " O  -- step OVER line        I  -- step INTO line",!
      . . write " R  -- run                       H  -- Hide debug code",!
      . . write " B  -- set Breakpoint",!

SPDone

      do VCULOAD2^TMGTERM
      set TMGRunMode=tpRunMode
      if tpStepMode="into" set result=1
      else  set result=2
      set TMGStepMode=tpStepMode

      if $get(Msg)=1 do
      . set $ZSTEP="N TMGTrap S TMGTrap=$$STEPTRAP^TMGTPSTP($ZPOS) zstep:(TMGTrap=1) into zstep:(TMGTrap=2) over zcontinue"
      . zstep:(result=1) into zstep:(result=2) over

      quit result


ErrTrap(idePos)
       ;"Purpose: This is the line that is called by GT.M for each ztrap event.
       ;"      It will be used to display the current code execution point

      new ScrHeight,ScrWidth
      set ScrHeight=$get(TMGScrHeight,10)
      set ScrWidth=$get(TMGScrWidth,70)

      do VCUSAV2^TMGTERM
      do ShowCodePos(idePos,ScrWidth,ScrHeight)

ETDone

      do VCULOAD2^TMGTERM
      quit

ShowCode(idePos,ScrWidth,ScrHeight,Wipe)

      ;"Purpose: This will display code at the top of the screen
      ;"Input: idePos -- string like this: X+2^ROUTINE[$DMOD]
      ;"      ScrWidth -- width of code display (Num of columns)
      ;
      ;"      Wipe -- OPTIONAL.  if 1, then code area is wiped blank

      new i
      new Routine,Label,Offest,s
      new LastRou,LastLabel,LastOffset
      new dbFGColor,bBGColor,nlFGColor,nlBGColor
      new BlankLine
      new StartOffset

      set ScrWidth=$get(ScrWidth,80)
      set ScrHeight=$get(ScrHeight,10)

      set nlFGColor=$get(TMGNlFGColor,3)
      set nlBGColor=$get(TMGNlBGColor,0)
      set dbFGColor=$get(TMGDbFGColor,0)
      set dbBGColor=$get(TMGDbBGColor,3)

      set BlankLine=" "
      for i=1:1:ScrWidth-1 set BlankLine=BlankLine_" "

      do VCOLORS^TMGTERM(dbFGColor,dbBGColor)

      do CUP^TMGTERM(1,1) ;"Cursor to line (1,1)
      write BlankLine,!  ;"This is needed for some reason...
      do CUU^TMGTERM(2)

      if $get(Wipe)=1 do  goto SCDone
      . do VCOLORS^TMGTERM(nlFGColor,nlBGColor)
      . for i=0:1:ScrHeight+1 write BlankLine

      set s=$piece(idePos,"$",1)  ;"e.g. X+2^ROUTINE$DMOD-->X+2^ROUTINE
      set Routine=$piece(s,"^",2)
      set Label=$piece(s,"^",1)
      set Offset=+$piece(Label,"+",2)
      set Label=$piece(Label,"+",1)

      set s="=== Routine: ^"_Routine_" " write s
      for i=1:1:ScrWidth-$length(s) write "="
      write !

      if Offset>(ScrHeight) do
        set StartOffset=(Offset-ScrHeight)
      else  set StartOffset=0

      for i=StartOffset:1:(ScrHeight+StartOffset) do
      . new line,Bl,ref,LoopOffset
      . set ref=Label_"+"_i_"^"_Routine
      . set line=$text(@ref)
      . set line=$$Substitute^TMGSTUTL(line,$Char(9),">>>>>")
      . if (i=Offset) do
      . . do VCOLORS^TMGTERM(nlFGColor,nlBGColor)
      . . write ">"
      . else  write " "
      . if $length(line)>(ScrWidth-1) do
      . . write $extract(line,1,ScrWidth-4),"...",!
      . else  do
      . . write $extract(line,1,ScrWidth-1)
      . . write $extract(BlankLine,1,ScrWidth-$length(line)-1),!
      . if (i=Offset) do VCOLORS^TMGTERM(dbFGColor,dbBGColor)

      for i=1:1:ScrWidth write "~"
      write !

SCDone
      ;"do VCULOAD^TMGTERM
      do VCOLORS^TMGTERM(nlFGColor,nlBGColor)

      ;"do CUD^TMGTERM(2)

      quit