home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 16 / 16.iso / w / w055 / 4.ddi / SOURCES.LIF / MACROS.PEL < prev    next >
Encoding:
Text File  |  1990-09-27  |  7.8 KB  |  338 lines

  1. # $Header:   P:/source/ppee/macros/macros.pev   1.58   26 Jul 1990 16:18:18   ericj  $
  2.  
  3. ##############################################################################
  4. #
  5. #           Sage Software - POLYTRON Division
  6. #             1700 NW 167th Place
  7. #               Beaverton, OR 97006
  8. #
  9. #   Copyright 1990, Sage Software, Inc.
  10. #
  11. #   Permission is hereby granted for licensed users of Sage Professional
  12. #   Editor and PolyAwk to copy and modify this source code for their own
  13. #   personal use.  These derivative works may be distributed only to other
  14. #   licensed Sage Professional Editor and PolyAwk users.  All other usage
  15. #   is prohibited without express written permission from Sage Software.
  16. #
  17. ##############################################################################
  18.  
  19. #### $Workfile:   macros.pel  $: Support for Playback/Record and Macros
  20.  
  21.  
  22. # reserved global variables
  23.  
  24. global    historyBuffer
  25. global    macroHistoryBuffer
  26.  
  27. global    recording = -1
  28. global    recordString = ""
  29. global    playingback = 0
  30.  
  31. global function record_key() {
  32.  
  33.     if (playingback)
  34.         return
  35.  
  36.     if( recording > 0 ){
  37.         message("Keystroke macro defined.")
  38.     } else {
  39.         if (recording != -1) {
  40.             if (tolower(confirm("Overwrite existing keystroke macro [yn]? ","YyNn")) != "y")
  41.                 return
  42.         }
  43.         message("Defining keystroke macro.")
  44.         recording = 0
  45.     }
  46.  
  47.     recording = !recording
  48.  
  49.     return record()
  50. }
  51.  
  52. global function playback_macro( str ){
  53.     playingback = 1
  54.     if (argcount()) {
  55.         playback( str )
  56.     } else {
  57.         playback()
  58.     }
  59.     playingback = 0
  60. }
  61.  
  62.  
  63. local    instr
  64. local    inpos
  65.  
  66. ## functions supporting invoke_function:
  67.  
  68. # Get a backslash escape sequence from instr at inpos.
  69. # Return the single character string.
  70. #
  71. local function getescape() {
  72.     local acc, i, ch
  73.  
  74.     ch = substr(instr,inpos,1)
  75.     inpos++
  76.     if (ch >= "0" && ch <= "7") {
  77.         # Octal escape sequence.
  78.         acc = ch + 0
  79.         for (i=0; i<3; i++) {
  80.             ch = substr(instr,inpos,1)
  81.             if (ch >= "0" && ch <= "7") {
  82.                 inpos++
  83.                 acc = acc * 8 + ch
  84.             }
  85.         }
  86.         return( chr(acc) )
  87.     } else if (ch == "x") {
  88.         # Hex escape sequence.
  89.         for (i=0; i<2; i++) {
  90.             ch = toupper( substr(instr,inpos+i,1) )
  91.             if ( !((ch >= "0" && ch <= "9") || \
  92.                     (ch >= "A" && ch <= "F"))) {
  93.                 break
  94.             }
  95.         }
  96.         ch = chr( 0 + ("0x" substr(instr,inpos,i)) )
  97.         inpos += i
  98.         return(ch)
  99.     } else if ((i = index("btfnr\\",ch))) {
  100.         return( substr("\b\t\f\n\r\\",i,1) )
  101.     } else {
  102.         return(ch)
  103.     }
  104. }
  105.  
  106.  
  107. # Break the string into arguments and return an array of the arguments,
  108. # where arg[1] is the first argument, arg[2] the second, etc.
  109. # Allow spaces in quoted arguments.
  110. #
  111. local function quote_args(lp) {
  112.     local i, ch, outbuf
  113.     local fun_args
  114.     local fun_argc = 0
  115.  
  116.     instr = lp
  117.     inpos = 1
  118.     while (1) {
  119.         ch = substr(instr,inpos,1)
  120.         if ( !ch ) {
  121.             break
  122.         }
  123.         if ( index(" \t",ch) ) {
  124.             inpos++
  125.             continue
  126.         }
  127.         if ( ch == "\"" ) {
  128.             inpos++
  129.             outbuf = ""
  130.             while (1) {
  131.                 ch = substr(instr,inpos++,1)
  132.                 if ( ch == "" ) {
  133.                     error("improperly quoted string")
  134.                 }
  135.                 if ( ch == "\"" ) {
  136.                     break
  137.                 }
  138.                 if (ch == "\\") {
  139.                     outbuf = outbuf getescape()
  140.                 } else {
  141.                     outbuf = outbuf ch
  142.                 }
  143.             }
  144.             fun_args[ ++fun_argc ] = outbuf
  145.  
  146.         } else {
  147.             if ((i = cindex(substr(instr,inpos)," \t"))) {
  148.                 fun_args[++fun_argc] = substr(instr,inpos,i-1)
  149.                 inpos += i
  150.             } else {
  151.                 fun_args[++fun_argc] = substr(instr,inpos)
  152.                 break
  153.             }
  154.             #outbuf = ""
  155.             #do {
  156.             #    outbuf = outbuf ch
  157.             #    ch = substr(instr,inpos++,1)
  158.             #} while (ch && ch != " " && ch != "\t")
  159.         }
  160.     }
  161.     return( fun_args )
  162. }
  163.  
  164.  
  165. # This is like function_id but allows quoted arguments in both
  166. # function calls and variable assignments.
  167. # Backslash sequences and spaces are processed in the quoted strings.
  168. #
  169. global function quoted_function_id(mac) {
  170.     local    args
  171.     if ( match( mac, /^[^=]+[ \t]*=[ \t]*"/ )) {
  172.         # It is an assignment of a quoted string to a variable,
  173.         # for example:  var = "this and that"
  174.         args = quote_args( substr(mac,RLENGTH) )
  175.         # Concatenate the "var =" part with the first quoted argument.
  176.         # Trim spaces between the "=" and the quoted string
  177.         # so that the argument does not begin with extraneous spaces.
  178.         return function_id( trim(substr(mac,1,RLENGTH-1)) args[1] )
  179.  
  180.     } else if (index(mac,"\"") && match(mac, /[ \t]/)) {
  181.         # It is a function call with quoted arguments.
  182.         args = quote_args( substr(mac,RSTART+1) )
  183.         return function_id( substr(mac,1,RSTART-1), args )
  184.  
  185.     } else {
  186.         # No arguments.
  187.         return function_id( mac )
  188.     }
  189. }
  190.  
  191. global function display_array( val ){
  192.     local    priorWindow = current_window
  193.     local    scanCode, keyCode
  194.     local    typo
  195.     local    i, v
  196.  
  197.     # check argument type
  198.     if ( typeof( val ) != "array" ) {
  199.         return
  200.     }
  201.  
  202.     # create a system buffer for the array contents
  203.  
  204.     current_window = create_factory_window( 20, 2, 55, 20,    \
  205.             WINDOW_MENU + WINDOW_SB_RIGHT )
  206.     current_buffer = create_buffer( "", "", \
  207.             BUFFER_SYSTEM + BUFFER_NO_UNDO )
  208.     attach_window_buffer( current_window, current_buffer )
  209.     window_name = "Array Contents"
  210.  
  211.     # insert the array contents
  212.  
  213.     for ( i in val ) {
  214.         v = val[i]
  215.         typo = typeof( v )
  216.         if ( 0 + v ) {
  217.             v = sprintf( "%s  (hex 0x%04X)", v, v )
  218.         }
  219.         if( typo == "string" ) {
  220.             v = sprintf( "%10s: \"%s\"", i, v )
  221.         } else if ( typo == "int" ) {
  222.             v = sprintf( "%10s: %s", i, v )
  223.         } else {
  224.             v = sprintf( "%10s: %s, type: %s", i, v, typo )
  225.         }
  226.         insert_string( v )
  227.         insert_newline()
  228.     }
  229.     backspace()
  230.     goto_buffer_top()
  231.  
  232.     # put up an instructions
  233.     message( "Hit <Enter> or <Esc> to continue" )
  234.  
  235.     # command loop to process cursor motion keys
  236.     while (1) {
  237.         raise_anchor()
  238.         drop_anchor( LINE_SELECTION )
  239.         display_update()
  240.         keyCode = getkey()
  241.         scanCode = shiftr( keyCode, 8 )
  242.  
  243.         if ( !keypad_motion( scanCode )) {
  244.             if ( keyCode == KEYCODE_ENTER ||
  245.                     keyCode == KEYCODE_ESC ) {
  246.                 break
  247.             }
  248.         }
  249.     }
  250.  
  251.     # clear the message line
  252.     message( "" )
  253.  
  254.     # remove the temp buffer and window
  255.     delete_buffer()
  256.     delete_window()
  257.     if ( priorWindow ) {
  258.         current_window = priorWindow
  259.     }
  260. }
  261.  
  262.  
  263. global function invoke_function( mac ){
  264.     local    typo, val, fid
  265.  
  266.     if( !mac )
  267.         mac = ltrim( prompt_history("XMACRO", "Command: ", "") )
  268.  
  269.     if (mac) {
  270.         message( "" )
  271.         if ( tolower( mac ) ~ /^help[ \t]/) {
  272.             mac = ltrim( trim( tolower( substr( mac, 5 ) )))
  273.             if (mac) {
  274.                 display_help_item( mac )
  275.             } else {
  276.                 help()
  277.             }
  278.         } else if ( (mac ~ /^dos[ \t]/) ||  (mac ~ /^system[ \t]/) ) {
  279.  
  280.             # special handling for "dos" and related commands:
  281.  
  282.             sub( /^dos[ \t]+/, "", mac )
  283.             sub( /^system[ \t]+/, "", mac )
  284.             sub( /[ \t]+$/, "", mac )
  285.  
  286.             if( match( mac, /^".*"$/ )){
  287.                 # remove outer-most quotes, if any:
  288.                 mac = substr( mac, 2, length( mac ) - 2 )
  289.             }
  290.             if(( val = system( mac )))
  291.                 warning( "Command exited with %d", val )
  292.  
  293.         } else if (mac ~ /^filter[ \t]/ ){
  294.  
  295.             # special handling for "filter":
  296.             sub( /^filter[ \t]+/, "", mac )
  297.             if( match( mac, /^".*"$/ )){
  298.                 # remove outer-most quotes, if any:
  299.                 mac = substr( mac, 2, length( mac ) - 2 )
  300.             }
  301.             filter( mac )
  302.  
  303.         } else if ( substr(mac,1,1) == "?" ) {
  304.  
  305.             # special handling for variable inquiry:
  306.             mac = ltrim(substr(mac,2))
  307.             if( mac ) {
  308.                 fid = quoted_function_id(mac)
  309.                 if ( fid ) {
  310.                     val = execute_function( fid )
  311.                     display_update()
  312.                     typo = typeof( val )
  313.                     if ( typo == "array" ) {
  314.                         display_array( val )
  315.                         return
  316.                     } else if( 0 + val ) {
  317.                         val = sprintf( "%s  (hex 0x%04X)", val, val )
  318.                     } else if( typo == "string" ){
  319.                         val = "\"" val "\""
  320.                     }
  321.                     notify( "%s == %s, type: %s", mac, val, typo )
  322.                 } else {
  323.                     warning( "\"" mac "\" is invalid" )
  324.                 }
  325.             }
  326.  
  327.         } else {
  328.  
  329.             # normal F10 commands:
  330.             if ((fid = quoted_function_id(mac))) {
  331.                 execute_function( fid )
  332.             } else {
  333.                 warning( "\"%s\" is invalid", mac )
  334.             }
  335.         }
  336.     }
  337. }
  338.