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

  1. /* ------------------------------------------------------------------ */
  2. /* Macro:        TEMPLATE.AML                                         */
  3. /* Written by:   nuText Systems                                       */
  4. /*                                                                    */
  5. /* Description:  This macro adds a 'Template Editing' capability to   */
  6. /*               Aurora which is based on the extension of the file   */
  7. /*               you are editing. Template editing allows you include */
  8. /*               commonly-used blocks of code or text (called         */
  9. /*               'templates') with just a few keystrokes.             */
  10. /*                                                                    */
  11. /* Note:         This macro requires the data file TEMPLATE.DAT,      */
  12. /*               which contains user-modifiable templates.            */
  13. /*               See TEMPLATE.DAT for further instructions.           */
  14. /*                                                                    */
  15. /* Usage:        Select this macro from the Macro List (on the Macro  */
  16. /*               menu), or run it from the macro picklist <shift f12> */
  17. /* ------------------------------------------------------------------ */
  18.  
  19.  
  20.   include bootpath "define.aml"
  21.  
  22.   // check for a previous install
  23.   obj = lookup "templobj" "prf"
  24.   if obj then
  25.     if (okbox "TEMPLATE.AML is already installed. Remove it?") == 'Ok' then
  26.       destroyobject obj
  27.       unsetx "templobj" "prf"
  28.       msgbox "TEMPLATE.DAT removed."
  29.     end
  30.     return
  31.   else
  32.     setxobj "templobj" (getcurrobj) "prf"
  33.   end
  34.  
  35.   // keep this object resident
  36.   stayresident
  37.  
  38.   // template expansion only in edit windows
  39.   object edit
  40.  
  41.   // template data file name
  42.   templatefile = getbootpath + "MACRO\\TEMPLATE.DAT"
  43.  
  44.   // template expansion key
  45.   // (change this key definition to suit your own preferences)
  46.  
  47.   key  <ctrl f3>
  48.  
  49.     // get the template keyword and start column
  50.     pushcursor
  51.     if find _CSet "~lr" then
  52.       right
  53.     else
  54.       col 1
  55.     end
  56.     startcol = getcol
  57.     word = getword
  58.     popcursor
  59.  
  60.     // if no template keyword is found, then return
  61.     if not word then
  62.       display
  63.       say "No template keyword" 'b'
  64.       return
  65.     end
  66.  
  67.     // get the current buffer id
  68.     buffer = getcurrbuf
  69.  
  70.     // build the template key
  71.     tempkey = "^{" + word + '[.a-zA-Z]*' + (locase (getext (getbufname))) +
  72.                "}|{" + word + ".$} |$"
  73.  
  74.     undobegin
  75.  
  76.     // load the template file
  77.     if loadbuf templatefile then
  78.  
  79.       // look for the template key
  80.       if find tempkey 'xi*' then
  81.  
  82.         // look for explicitly specified number of lines, if any
  83.         length = find '[0-9]#' 'xl'
  84.         if length then
  85.           lines = gettext (getcol) length
  86.  
  87.         // no explicit lines specified -- look for the next blank line
  88.         else
  89.           top = getrow
  90.           pushcursor
  91.           if find '^$' 'x' then
  92.             lines = getrow - top - 1
  93.           else
  94.             lines = getlines - top
  95.           end
  96.           popcursor
  97.         end
  98.  
  99.         // delete the original word in the text
  100.         delchar (sizeof word) startcol '' buffer
  101.  
  102.         oldmark = usemark 'T'
  103.  
  104.         // insert the first template line horizontally
  105.         down
  106.         markcolumn 1 (getlinelen)
  107.         copyblock 'T' buffer startcol
  108.  
  109.         // insert the following lines vertically, and shift right if needed
  110.         if lines > 1 then
  111.           down
  112.           markline '' getrow + lines - 2
  113.           copyblock 'T' buffer
  114.           if startcol > 1 then
  115.             shiftblock startcol - 1
  116.           end
  117.         end
  118.  
  119.         // destroy the template buffer
  120.         destroybuf
  121.  
  122.         //gotobuf buffer
  123.         markline (getrow) getrow + lines - 1
  124.  
  125.         // expand embedded macro expressions, if any
  126.         pushcursor
  127.         loop
  128.           length = find "\\\\\\{.+\\}" "xb*"
  129.           if not length
  130.             break
  131.           end
  132.  
  133.           // get the macro expression
  134.           begcolumn = getcol
  135.           expression = gettext begcolumn + 2  length - 3
  136.  
  137.           // delete the expression in the copied text
  138.           delchar length
  139.  
  140.           // evaluate the expression
  141.           value = eval expression
  142.           // (expression may have changed the current buffer)
  143.           gotobuf buffer
  144.  
  145.           // insert the value of the expression into the text
  146.           // or indicate a compilation error
  147.           error = geterror 'c'
  148.           if error then
  149.             errorstr = "Template expression error: " + (errormsg error)
  150.             break
  151.           else
  152.             instext value
  153.             right (sizeof value)
  154.           end
  155.         end
  156.         popcursor
  157.  
  158.         // move to the cursor position specified in the template (if any)
  159.         replace "\\c" ' ' "gbi*"
  160.  
  161.         destroymark
  162.         usemark oldmark
  163.       else
  164.         errorstr = "Template '" + word + "' not found"
  165.         destroybuf
  166.       end
  167.     else
  168.       errorstr = "Can't open " + templatefile
  169.     end
  170.  
  171.     undoend
  172.  
  173.     // display any error messages
  174.     if errorstr then
  175.       display
  176.       say errorstr 'b'
  177.     end
  178.   end
  179.  
  180.   // installation message
  181.   if (okbox "TEMPLATE.AML is now installed.\nOpen " +
  182.             templatefile + " for further instructions?") == 'Ok' then
  183.     queue "open" templatefile
  184.   end
  185.  
  186.