home *** CD-ROM | disk | FTP | other *** search
- # $Header: P:/source/ppee/macros/macros.pev 1.58 26 Jul 1990 16:18:18 ericj $
-
- ##############################################################################
- #
- # Sage Software - POLYTRON Division
- # 1700 NW 167th Place
- # Beaverton, OR 97006
- #
- # Copyright 1990, Sage Software, Inc.
- #
- # Permission is hereby granted for licensed users of Sage Professional
- # Editor and PolyAwk to copy and modify this source code for their own
- # personal use. These derivative works may be distributed only to other
- # licensed Sage Professional Editor and PolyAwk users. All other usage
- # is prohibited without express written permission from Sage Software.
- #
- ##############################################################################
-
- #### $Workfile: macros.pel $: Support for Playback/Record and Macros
-
-
- # reserved global variables
-
- global historyBuffer
- global macroHistoryBuffer
-
- global recording = -1
- global recordString = ""
- global playingback = 0
-
- global function record_key() {
-
- if (playingback)
- return
-
- if( recording > 0 ){
- message("Keystroke macro defined.")
- } else {
- if (recording != -1) {
- if (tolower(confirm("Overwrite existing keystroke macro [yn]? ","YyNn")) != "y")
- return
- }
- message("Defining keystroke macro.")
- recording = 0
- }
-
- recording = !recording
-
- return record()
- }
-
- global function playback_macro( str ){
- playingback = 1
- if (argcount()) {
- playback( str )
- } else {
- playback()
- }
- playingback = 0
- }
-
-
- local instr
- local inpos
-
- ## functions supporting invoke_function:
-
- # Get a backslash escape sequence from instr at inpos.
- # Return the single character string.
- #
- local function getescape() {
- local acc, i, ch
-
- ch = substr(instr,inpos,1)
- inpos++
- if (ch >= "0" && ch <= "7") {
- # Octal escape sequence.
- acc = ch + 0
- for (i=0; i<3; i++) {
- ch = substr(instr,inpos,1)
- if (ch >= "0" && ch <= "7") {
- inpos++
- acc = acc * 8 + ch
- }
- }
- return( chr(acc) )
- } else if (ch == "x") {
- # Hex escape sequence.
- for (i=0; i<2; i++) {
- ch = toupper( substr(instr,inpos+i,1) )
- if ( !((ch >= "0" && ch <= "9") || \
- (ch >= "A" && ch <= "F"))) {
- break
- }
- }
- ch = chr( 0 + ("0x" substr(instr,inpos,i)) )
- inpos += i
- return(ch)
- } else if ((i = index("btfnr\\",ch))) {
- return( substr("\b\t\f\n\r\\",i,1) )
- } else {
- return(ch)
- }
- }
-
-
- # Break the string into arguments and return an array of the arguments,
- # where arg[1] is the first argument, arg[2] the second, etc.
- # Allow spaces in quoted arguments.
- #
- local function quote_args(lp) {
- local i, ch, outbuf
- local fun_args
- local fun_argc = 0
-
- instr = lp
- inpos = 1
- while (1) {
- ch = substr(instr,inpos,1)
- if ( !ch ) {
- break
- }
- if ( index(" \t",ch) ) {
- inpos++
- continue
- }
- if ( ch == "\"" ) {
- inpos++
- outbuf = ""
- while (1) {
- ch = substr(instr,inpos++,1)
- if ( ch == "" ) {
- error("improperly quoted string")
- }
- if ( ch == "\"" ) {
- break
- }
- if (ch == "\\") {
- outbuf = outbuf getescape()
- } else {
- outbuf = outbuf ch
- }
- }
- fun_args[ ++fun_argc ] = outbuf
-
- } else {
- if ((i = cindex(substr(instr,inpos)," \t"))) {
- fun_args[++fun_argc] = substr(instr,inpos,i-1)
- inpos += i
- } else {
- fun_args[++fun_argc] = substr(instr,inpos)
- break
- }
- #outbuf = ""
- #do {
- # outbuf = outbuf ch
- # ch = substr(instr,inpos++,1)
- #} while (ch && ch != " " && ch != "\t")
- }
- }
- return( fun_args )
- }
-
-
- # This is like function_id but allows quoted arguments in both
- # function calls and variable assignments.
- # Backslash sequences and spaces are processed in the quoted strings.
- #
- global function quoted_function_id(mac) {
- local args
- if ( match( mac, /^[^=]+[ \t]*=[ \t]*"/ )) {
- # It is an assignment of a quoted string to a variable,
- # for example: var = "this and that"
- args = quote_args( substr(mac,RLENGTH) )
- # Concatenate the "var =" part with the first quoted argument.
- # Trim spaces between the "=" and the quoted string
- # so that the argument does not begin with extraneous spaces.
- return function_id( trim(substr(mac,1,RLENGTH-1)) args[1] )
-
- } else if (index(mac,"\"") && match(mac, /[ \t]/)) {
- # It is a function call with quoted arguments.
- args = quote_args( substr(mac,RSTART+1) )
- return function_id( substr(mac,1,RSTART-1), args )
-
- } else {
- # No arguments.
- return function_id( mac )
- }
- }
-
- global function display_array( val ){
- local priorWindow = current_window
- local scanCode, keyCode
- local typo
- local i, v
-
- # check argument type
- if ( typeof( val ) != "array" ) {
- return
- }
-
- # create a system buffer for the array contents
-
- current_window = create_factory_window( 20, 2, 55, 20, \
- WINDOW_MENU + WINDOW_SB_RIGHT )
- current_buffer = create_buffer( "", "", \
- BUFFER_SYSTEM + BUFFER_NO_UNDO )
- attach_window_buffer( current_window, current_buffer )
- window_name = "Array Contents"
-
- # insert the array contents
-
- for ( i in val ) {
- v = val[i]
- typo = typeof( v )
- if ( 0 + v ) {
- v = sprintf( "%s (hex 0x%04X)", v, v )
- }
- if( typo == "string" ) {
- v = sprintf( "%10s: \"%s\"", i, v )
- } else if ( typo == "int" ) {
- v = sprintf( "%10s: %s", i, v )
- } else {
- v = sprintf( "%10s: %s, type: %s", i, v, typo )
- }
- insert_string( v )
- insert_newline()
- }
- backspace()
- goto_buffer_top()
-
- # put up an instructions
- message( "Hit <Enter> or <Esc> to continue" )
-
- # command loop to process cursor motion keys
- while (1) {
- raise_anchor()
- drop_anchor( LINE_SELECTION )
- display_update()
- keyCode = getkey()
- scanCode = shiftr( keyCode, 8 )
-
- if ( !keypad_motion( scanCode )) {
- if ( keyCode == KEYCODE_ENTER ||
- keyCode == KEYCODE_ESC ) {
- break
- }
- }
- }
-
- # clear the message line
- message( "" )
-
- # remove the temp buffer and window
- delete_buffer()
- delete_window()
- if ( priorWindow ) {
- current_window = priorWindow
- }
- }
-
-
- global function invoke_function( mac ){
- local typo, val, fid
-
- if( !mac )
- mac = ltrim( prompt_history("XMACRO", "Command: ", "") )
-
- if (mac) {
- message( "" )
- if ( tolower( mac ) ~ /^help[ \t]/) {
- mac = ltrim( trim( tolower( substr( mac, 5 ) )))
- if (mac) {
- display_help_item( mac )
- } else {
- help()
- }
- } else if ( (mac ~ /^dos[ \t]/) || (mac ~ /^system[ \t]/) ) {
-
- # special handling for "dos" and related commands:
-
- sub( /^dos[ \t]+/, "", mac )
- sub( /^system[ \t]+/, "", mac )
- sub( /[ \t]+$/, "", mac )
-
- if( match( mac, /^".*"$/ )){
- # remove outer-most quotes, if any:
- mac = substr( mac, 2, length( mac ) - 2 )
- }
- if(( val = system( mac )))
- warning( "Command exited with %d", val )
-
- } else if (mac ~ /^filter[ \t]/ ){
-
- # special handling for "filter":
- sub( /^filter[ \t]+/, "", mac )
- if( match( mac, /^".*"$/ )){
- # remove outer-most quotes, if any:
- mac = substr( mac, 2, length( mac ) - 2 )
- }
- filter( mac )
-
- } else if ( substr(mac,1,1) == "?" ) {
-
- # special handling for variable inquiry:
- mac = ltrim(substr(mac,2))
- if( mac ) {
- fid = quoted_function_id(mac)
- if ( fid ) {
- val = execute_function( fid )
- display_update()
- typo = typeof( val )
- if ( typo == "array" ) {
- display_array( val )
- return
- } else if( 0 + val ) {
- val = sprintf( "%s (hex 0x%04X)", val, val )
- } else if( typo == "string" ){
- val = "\"" val "\""
- }
- notify( "%s == %s, type: %s", mac, val, typo )
- } else {
- warning( "\"" mac "\" is invalid" )
- }
- }
-
- } else {
-
- # normal F10 commands:
- if ((fid = quoted_function_id(mac))) {
- execute_function( fid )
- } else {
- warning( "\"%s\" is invalid", mac )
- }
- }
- }
- }
-