home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 24 / CD_ASCQ_24_0995.iso / dos / tools / aurora21 / example.aml < prev    next >
Text File  |  1995-08-10  |  20KB  |  695 lines

  1. /* ------------------------------------------------------------------ */
  2. /* Name:         EXAMPLE.AML                                          */
  3. /*                                                                    */
  4. /* Description:  AML examples and code fragments.                     */
  5. /*                                                                    */
  6. /* Usage:        This file is not an executable macro. It contains    */
  7. /*               AML examples and code fragments which can be used    */
  8. /*               in within other macro files.                         */
  9. /* ------------------------------------------------------------------ */
  10.  
  11.  
  12. // Example 1 --------------------------------------------------
  13.  
  14. // mark the entire file
  15. // (assigned to <f12> key - place in the "edit" object)
  16.  
  17.   key  <f12>
  18.     markline 1 (getlines)
  19.   end
  20.  
  21. // Example 2 --------------------------------------------------
  22.  
  23. // Demonstrates block marking without having the cursor resize the mark
  24. // (for compatability with the old 'DrawMark' config setting in v1.2)
  25.  
  26.   key  <alt l>
  27.                 markline
  28.                 stopmark    // stopmark closes the mark
  29.   key  <alt a>
  30.                 markchar
  31.                 stopmark
  32.   key  <alt b>
  33.                 markcolumn
  34.                 stopmark
  35.  
  36.  
  37. // Example 3 --------------------------------------------------
  38.  
  39. // block copy without moving the mark (provided for compatibility
  40. // with the old 'MoveMark' config setting in v1.2)
  41.  
  42.   // block copy
  43.   key  <alt c>
  44.     undobegin                // group together as one undoable operation
  45.     copymark curr_mark 'T'   // create temp copy of original mark
  46.     oldmark = usemark 'T'    // use the temp mark
  47.     copyblock2               // copy marked text (moves the mark also)
  48.     destroymark              // destroy the temp mark
  49.     usemark oldmark          // switch back to the original mark
  50.     undoend
  51.   end
  52.  
  53.  
  54. // Example 4 --------------------------------------------------
  55.  
  56. // Automark example - creates a temporary paragraph mark if
  57. // no mark exists (provided for compatibility with the old
  58. // AutoMark configuration setting in v1.2)
  59.  
  60.   // begin automark
  61.   function  begauto
  62.     if not mark? then        // if no mark exists...
  63.       set _am ON             // set a flag
  64.       markpara               // mark the current paragraph
  65.     end
  66.   end
  67.  
  68.   // end automark
  69.   function  endauto
  70.     if _am then              // if flag was set..
  71.       set _am OFF            // unset flag
  72.       destroymark            // destroy the temporary mark
  73.     end
  74.   end
  75.  
  76.   // usage example (indent a block)
  77.   key  <shift f8>
  78.     begauto
  79.     shiftblock 1
  80.     endauto
  81.   end
  82.  
  83.  
  84. // Example 5 --------------------------------------------------
  85.  
  86. // define <home> and <end> key behaviour to match the the old
  87. // RepEnd config setting in v1.2 (provided for compatibility
  88. // with v1.2)
  89.  
  90.   // <home> key
  91.   key  <home>
  92.     if getcol == 1 then             // at col 1?
  93.       up                            // then move up 1 line
  94.     else
  95.       col 1                         // otherwise move to column 1
  96.     end
  97.   end
  98.  
  99.   // <end> key
  100.   key  <end>
  101.     end_of_line = getlinelen + 1    // get column after end-of-line
  102.     if getcol == end_of_line then   // are we there already?
  103.       down                          // move down 1 line
  104.       col  getlinelen + 1           // move to end-of-line on new line
  105.     else
  106.       col end_of_line               // move to end-of-line
  107.     end
  108.   end
  109.  
  110.  
  111. // Example 6 --------------------------------------------------
  112.  
  113. // Find the last string with wrap around (provided for compatibility
  114. // with the old 'SearchWrap' config setting in v1.2)
  115.  
  116.   function findlastw
  117.  
  118.     var  search_str
  119.     var  options
  120.  
  121.     // get last find string
  122.     history_str = gethiststr "_find"
  123.  
  124.     // do the search. If not found...
  125.     if not search2 history_str '' TRUE then
  126.  
  127.       // wrap to top or bottom
  128.       if (splitstr '' history_str ref search_str ref options ) >= 2 and
  129.          (pos 'r' options) then
  130.         row (getlines)
  131.         col getlinelen + 1
  132.       else
  133.         gotopos 1 1
  134.       end
  135.  
  136.       // do the search again
  137.       search2 history_str
  138.     end
  139.   end
  140.  
  141.   // usage example
  142.   key <ctrl l>  findlastw
  143.  
  144.  
  145. // Example 7 --------------------------------------------------
  146.  
  147. // Change the left and right cursor keys to wrap to the previous or
  148. // next line
  149.  
  150.   // cursorleft with wrap
  151.   key  <left>
  152.     if getcol == 1 then             // wrap if at column 1
  153.       if up then                    // ..and not at first line
  154.         col getlinelen + 1
  155.       end
  156.     else
  157.       left
  158.     end
  159.   end
  160.  
  161.   // cursorright with wrap
  162.   key  <right>
  163.     if getcol > getlinelen then     // wrap if at end-of-line
  164.       if down then                  // ..and not at last line
  165.         col 1
  166.       end
  167.     else
  168.       right
  169.     end
  170.   end
  171.  
  172.  
  173.  
  174. // Example 8 --------------------------------------------------
  175.  
  176. // change the <del> key to delete a block if marked,
  177. // otherwise delete a character
  178.  
  179.   key  <del>
  180.     if mark? then
  181.       deleteblock                      // delete block if it exists,
  182.     else
  183.       delchar2                         // otherwise delete a character
  184.     end
  185.   end
  186.  
  187.  
  188. // Example 9 --------------------------------------------------
  189.  
  190. // find the word at the cursor
  191. // (assigned to <ctrl f11>)
  192.  
  193.   key  <ctrl f11>
  194.     wordstr = getword           // get word using the default charset
  195.     if wordstr then
  196.       search2 wordstr           // find the word
  197.     else
  198.       say "no word at the cursor" 'b'
  199.     end
  200.   end
  201.  
  202.  
  203. // Example 10 -------------------------------------------------
  204.  
  205. // insert a ruler line above the current line
  206. // (assigned to <alt k> key)
  207.  
  208.   key  <alt k>
  209.     insabove "····+····1····+····2····+····3····+····4····+····5····+····6····+····7····+····8"
  210.   end
  211.  
  212.  
  213. // Example 11 -------------------------------------------------
  214.  
  215. // emulate the QEdit 'GetPrev' <ctrl -> command:  copies characters
  216. // from the line above the current line (assigned to <alt 4> key)
  217.  
  218.   key  <alt 4>
  219.     if getrow > 1 then                         // if after first line...
  220.       writetext (getchar (getcol) getrow - 1)  // get char and write it
  221.     end
  222.   end
  223.  
  224.  
  225. // Example 12 -------------------------------------------------
  226.  
  227. // Emulation of Brief-style <home> and <end> keys. These keys should
  228. // be placed in the 'edit' object:
  229.  
  230.   // brief <home> key emulation (br)
  231.   key <home>
  232.     col 1                                      // goto column 1
  233.     smark                                      // cua marking
  234.     keycode = getkey                           // get next key
  235.     if keycode == <home> then                  // <home> pressed 2nd time?
  236.       row (getviewtop)                         // goto to page top
  237.       smark                                    // cua marking
  238.       keycode = getkey                         // get next key
  239.       if keycode == <home> then                // <home> pressed 3rd time?
  240.         row 1                                  // goto to top of file
  241.         smark                                  // cua marking
  242.       else
  243.         queuekey keycode                       // execute key normally
  244.       end
  245.     else
  246.       queuekey keycode                         // execute key normally
  247.     end
  248.   end
  249.  
  250.   // brief <end> key emulation (br)
  251.   key <end>
  252.     col  getlinelen + 1                        // goto end-of-line
  253.     smark                                      // cua marking
  254.     keycode = getkey                           // get next key
  255.     if keycode == <end> then                   // <end> pressed 2nd time?
  256.       row (getviewbot)                         // goto to page bottom
  257.       smark                                    // cua marking
  258.       keycode = getkey                         // get next key
  259.       if keycode == <end> then                 // <end> pressed 3rd time?
  260.         row (getlines)                         // goto to bottom of file
  261.         smark                                  // cua marking
  262.       else
  263.         queuekey keycode                       // execute key normally
  264.       end
  265.     else
  266.       queuekey keycode                         // execute key normally
  267.     end
  268.   end
  269.  
  270.  
  271. // Example 13 -------------------------------------------------
  272.  
  273. // shows how to 'double-up' key usage for some keys by testing
  274. // for the <shift> key
  275.  
  276.   key  <alt k>
  277.     if shiftkey? then             // <alt shift k>
  278.       .
  279.       .
  280.     else                          // <alt k>
  281.       .
  282.       .
  283.     end
  284.   end
  285.  
  286.  
  287. // Example 14 -------------------------------------------------
  288.  
  289. // prompts the user for the name of a program and passes the word
  290. // at the cursor to it as the first parameter
  291.  
  292.   key  <shift f12>
  293.     parm = getword _CSetB                  // get word using charset CSetB
  294.     if parm then
  295.       program = ask "Program to execute"   // prompt user for program
  296.       if program then                      // program entered?
  297.         run  program + ' ' + parm  "ck"    // concatenate program with word
  298.       end                                  //   and execute it
  299.     else
  300.       say "no word at the cursor" 'b'      // no word found at the cursor
  301.     end
  302.   end
  303.  
  304.  
  305. // Example 15 -------------------------------------------------
  306.  
  307. // strip leading or trailing spaces from a string
  308.  
  309.   // return string 'charstring' without leading spaces
  310.   function  stripl (charstring)
  311.     return  charstring [posnot ' ' charstring : 0]
  312.   end
  313.  
  314.   // return string 'charstring' without trailing spaces
  315.   function  stript (charstring)
  316.     return  charstring [1 : posnot ' ' charstring 'r']
  317.   end
  318.  
  319.  
  320. // Example 16 -------------------------------------------------
  321.  
  322. // attach "properties" to a string value in the current object
  323. // (demonstrates the use of setx, lookup, unsetx)
  324.  
  325.   // set a property
  326.   function  setprop (string prop value)
  327.     setx  string + '-' + prop  value
  328.   end
  329.  
  330.   // get a property
  331.   function  getprop (string prop)
  332.     lookup  string + '-' + prop
  333.   end
  334.  
  335.   // delete a property
  336.   function  delprop (string prop)
  337.     unsetx  string + '-' + prop
  338.   end
  339.  
  340.  
  341. // Example 17 -------------------------------------------------
  342.  
  343. // modify the save-as prompt to change the file name
  344. // (replace function "asksaveas" in EXT.AML)
  345.  
  346.   function  asksaveas (options)
  347.     file = ask "Save " + (getname (getbufname)) + " as"  "_load"
  348.     if file then
  349.       file = qualify file (getbufname)
  350.       addhistory "_load" file
  351.       save file options   // save file with name "file"
  352.       setname file        // name current file "file"
  353.     end
  354.   end
  355.  
  356.  
  357. // Example 18 -------------------------------------------------
  358.  
  359. // Compute the factorial of a number (illustrates AML recursion)
  360.   function  fact (x)
  361.     if? x <= 1  1  x * (fact x - 1)
  362.   end
  363.  
  364.   // display the factorial of 6
  365.   say (fact 6)
  366.  
  367.  
  368. // Example 19 -------------------------------------------------
  369.  
  370. // generate a random number between a and b
  371. // (demonstrates the use of the rand function)
  372.  
  373.   function  random (a b)
  374.     a + (rand mod b - a + 1)
  375.   end
  376.  
  377.   // display random number between 500 and 3000
  378.   say (random 500 3000)
  379.  
  380.  
  381. // Example 20 -------------------------------------------------
  382.  
  383. // asynchronous beep
  384. // (set a timer to stop the beep - demonstrates the use of timers)
  385.  
  386.   function  beep2 (freq duration)
  387.     if not arg then
  388.       // stop the beep
  389.       beep
  390.     else
  391.       // beep indefinitely
  392.       beep freq
  393.       // call this function again after 'duration' milliseconds
  394.       settimer "beep" duration '' "beep2"
  395.     end
  396.   end
  397.  
  398.  
  399. // Example 21 -------------------------------------------------
  400.  
  401. // Check newly open files for tab chars. Expand tabs if found.
  402.  
  403.   // (Place this section of aml code at the end of the 'onopen'
  404.   // function in the 'edit' object in EXT.AML - The 'onopen' function
  405.   // is called after a file is loaded and before it is displayed)
  406.  
  407.   if not getbinarylen then        // only for non-binary files
  408.     oldmark = usemark 'T'
  409.     markline 1 20                 // create a mark (first 20 lines)
  410.     if find "\t" "bgn*" then      // search for tab char in the mark
  411.       markline 1 (getlines)       // extend the mark to end-of-file
  412.       tabblock _TabWidth          // expand tabs using TabWidth
  413.       bufferflag '-m'             // turn off buffer-modified flag
  414.     end
  415.     destroymark                   // destroy the temporary mark
  416.     usemark oldmark
  417.   end
  418.  
  419.  
  420. // Example 22 -------------------------------------------------
  421.  
  422. // toggle the case of the character at the cursor
  423. // (assigned to <alt k>)
  424.  
  425.   key <alt k>
  426.     ovltext (flipcase (getchar))     // toggle the character
  427.     right                            // move right one column
  428.   end
  429.  
  430.  
  431. // Example 23 -------------------------------------------------
  432.  
  433. // transpose the characters at the cursor
  434. // (assigned to <alt k>)
  435.  
  436.    // method 1: using a marked block
  437.    key <alt k>
  438.      undobegin              // beginning of an undoable group
  439.      oldmark = usemark 'T'  // use temporary mark 'T'
  440.      markchar               // create a character mark
  441.      stopmark               // stop the cursor from extending the mark
  442.      right 2                // move right two columns
  443.      moveblock              // move the marked character
  444.      destroymark            // destroy the mark
  445.      usemark oldmark        // switch back to old mark
  446.      undoend                // end of an undoable group
  447.    end
  448.  
  449.   // method 2: using a string
  450.   key <alt k>
  451.     twochars = gettext (getcol) 2
  452.     ovltext (if? twochars [2] twochars [2] ' ') + twochars [1]
  453.     right
  454.   end
  455.  
  456.  
  457. // Example 24 -------------------------------------------------
  458.  
  459. // macro code fragment to synchronously dispatch all the events
  460. // currently in the event queue
  461.  
  462.   endprocess          // queue an internal 'queue quit' event
  463.   process             // process all events and return
  464.  
  465.  
  466. // Example 25 -------------------------------------------------
  467.  
  468. // macro code fragment to display a timed message box
  469.  
  470.   // set a timer (t1) to simulate the <esc> key one second from now
  471.   settimer "t1" 1000 '' <esc>
  472.  
  473.   // display a short-style message box
  474.   shortbox "timed message"
  475.  
  476.  
  477. // Example 26 -------------------------------------------------
  478.  
  479. // modify the prompt history menu to automatically enter selected
  480. // strings into the prompt
  481.  
  482.  
  483.   // replace the <pgup> and <pgdn> keys in the 'prompt' object
  484.   // in KBD.AML
  485.  
  486.   key  <pgup>
  487.     if askhistory then
  488.       queue <enter>
  489.     end
  490.   end
  491.  
  492.   key  <pgdn>  call <pgup>
  493.  
  494.   // ..also modify the <lbutton> function in the 'prompt' object
  495.   // in MOUSE.AML to handle mouse clicks on the prompt retrieve tab
  496.  
  497.   // left button down
  498.   function  <lbutton>
  499.      .
  500.      .
  501.     case getregion
  502.  
  503.       // modify this 'when' clause
  504.       when 14
  505.         if askhistory then
  506.           queue <enter>
  507.         end
  508.          .
  509.          .
  510.  
  511. // Example 27 -------------------------------------------------
  512.  
  513. // The following is an example of an external macro which can
  514. // be run from the command line (without actually starting the
  515. // editor). This macro replaces all occurrences of a search
  516. // string in a file:
  517.  
  518.   if loadbuf (arg 2) then                  // load a file
  519.     replace (arg 3) (arg 4) (arg 5) + 'a*' // replace all string(s)
  520.     savebuf (arg 2)                        // save the file
  521.   end
  522.  
  523. // assuming this macro was compiled to 'replace.x', the following
  524. // command will run the macro from the DOS command line:
  525.  
  526.   C>a -xreplace.x "myfile.txt" "apples" "oranges" 'i'
  527.          // replaces all occurrences of 'apples' with 'oranges',
  528.          // in 'myfile.txt', ignoring case
  529.  
  530.  
  531. // Example 28 -------------------------------------------------
  532.  
  533. // The following example shows how to locate a file in a path
  534. // defined by a DOS environment string:
  535.  
  536.    file = locatefile "program.exe" (getenv 'PATH')
  537.      // 'PATH' must be in caps
  538.  
  539.    msgbox "Filename is: " + file
  540.      // filename is fully qualified
  541.  
  542.  
  543. // Example 29 -------------------------------------------------
  544.  
  545. // To change the file manager to recognize unshifted characters as
  546. // command codes (like Aurora v1.2), modify the <char> key
  547. // definition in the fmgr object in KBD.AML:
  548.  
  549.   key  <char> (c)
  550.       .
  551.       .
  552.     // <shift-character> commands
  553.     elseif not shiftkey? then         // add 'not' to this line
  554.       .
  555.       .
  556.  
  557.   // (note that this will also change each file manager search
  558.   // 'hotkey' to <shift-hotkey>)
  559.  
  560.  
  561. // Example 30 -------------------------------------------------
  562.  
  563. // The following macro code shows how to put a simple real-time clock on
  564. // the edit window title bar:
  565.  
  566.   object edit
  567.      .
  568.     // called every second
  569.     function  onesec
  570.       settitle getbufname + ' ' + gettime
  571.     end
  572.      .
  573.      .
  574.   end
  575.  
  576.   // called when the editor is started
  577.   function  onentry
  578.       .
  579.     // set a timer to call 'onesec' every second
  580.     setrepeat 'sec' 1000 '' "onesec"
  581.       .
  582.       .
  583.   end
  584.  
  585.  
  586. // Example 31 -------------------------------------------------
  587.  
  588. // The following macro code shows how set an alarm to go off every
  589. // hour on the hour in edit windows and file manager windows
  590.  
  591.   object edit_fmgr
  592.      .
  593.     // called on the hour (Big Ben clock chime)
  594.     function  onhour
  595.       beep 330  600
  596.       beep 262  600
  597.       beep 294  600
  598.       beep 196 1200
  599.       beep 196  600
  600.       beep 294  600
  601.       beep 330  600
  602.       beep 262 1200
  603.     end
  604.      .
  605.      .
  606.   end
  607.  
  608.   // called when the editor is started
  609.   function  onentry
  610.      .
  611.     // set an alarm timer to call 'onhour' every hour on the hour
  612.     setalarm 'hr'
  613.        -1               // any year
  614.        -1               // any month
  615.        -1               // any day
  616.        -1               // any weekday
  617.        -1               // any hour
  618.         0               // only at zero minutes past the hour
  619.         0               // only at zero seconds past zero minutes
  620.        ''               // dispatch in the current event object
  621.        'onhour'         // execute the function 'onhour'
  622.      .
  623.      .
  624.   end
  625.  
  626.  
  627. // Example 32 -------------------------------------------------
  628.  
  629. // The following macro code shows how to create a persistent
  630. // real-time clock window which is updated every second:
  631.  
  632.   // create a new clock object descended from the 'win' object,
  633.   // so it can inherit the mouse handling and other capabilities
  634.   // of 'desktop' windows (like edit and file manager windows)
  635.   object  clock (win)
  636.  
  637.   // called every second to update the date and time
  638.   function tick
  639.     oldwindow = gotowindow "clock"
  640.     writestr getdate + ' ' + gettime  (color brightred on black)  4 2
  641.     gotowindow oldwindow
  642.   end
  643.  
  644.   // destroy the timer if the clock window is closed
  645.   key <esc>
  646.     destroytimer "clk"
  647.     pass
  648.   end
  649.  
  650.   function "≡"
  651.     call <esc>
  652.   end
  653.  
  654.   // provide keyboard support for switching windows
  655.   key <ctrl a>
  656.     nextwindow
  657.   end
  658.  
  659.   // (be sure to change the object if unrelated code follows)
  660.   .
  661.   .
  662.  
  663.   object  edit
  664.  
  665.   // create the clock window and start the clock (this could actually
  666.   // be placed in any convenient object or function, or it could
  667.   // be an external macro)
  668.   key <alt k>
  669.     createwindow 'clock'
  670.     // route window events to the clock object above
  671.     setwinobj "clock"
  672.  
  673.     setframe ">b"
  674.     setwinctrl '≡'
  675.     setcolor  border_color   color white on gray
  676.     setcolor  text_color     color black on gray
  677.     settitle "Clock" 'c'
  678.     setborder "i1"
  679.     setshadow 2 1
  680.  
  681.     // center the window
  682.     width = (sizeof getdate + gettime) + 9
  683.     height = 4
  684.     ox = (getvidcols - width) / 2
  685.     oy = (getvidrows - height) / 2
  686.     sizewindow ox oy ox + width oy + height "ad"
  687.  
  688.     // simulate the first tick
  689.     sendobject "clock" "tick"
  690.     // start the clock timer
  691.     setrepeat 'clk' 1000 'clock' "tick"
  692.   end
  693.  
  694.  
  695.