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

  1. /* ------------------------------------------------------------------ */
  2. /* Macro:        SCAN2.AML                                            */
  3. /* Written by:   nuText Systems                                       */
  4. /*                                                                    */
  5. /* Description:  This macro can be used as an alternative to the File */
  6. /*               Scan command. Unlike like the File Scan command,     */
  7. /*               this macro lists all occurrences of the search       */
  8. /*               string in each file scanned.                         */
  9. /*                                                                    */
  10. /*               Each occurrence of the scan search string is         */
  11. /*               displayed in a popup menu similar to the menu used   */
  12. /*               in the Find Occurrences command. Selecting a line    */
  13. /*               from the menu opens the file containing the          */
  14. /*               occurrence of the string and positions the cursor    */
  15. /*               appropriately. Selecting the first line in the       */
  16. /*               menu allows you to edit all scanned occurrences in   */
  17. /*               a separate file.                                     */
  18. /*                                                                    */
  19. /* Usage:        Select this macro from the Macro List (on the Macro  */
  20. /*               menu), or run it from the macro picklist <shift f12> */
  21. /* ------------------------------------------------------------------ */
  22.  
  23.   include bootpath "define.aml"
  24.  
  25.   // disable 'onloading' in EXT.AML while this macro is running
  26.   function onloading
  27.   end
  28.  
  29.   // get scan multi-string from the user
  30.   scanstring = if _PromptStyle == 'd' then
  31.                  scandlg
  32.                else
  33.                  ask "[string/files/iwx] Scan" "_scan"
  34.                end
  35.   if not scanstring then
  36.     return
  37.   end
  38.  
  39.   // add the scan string to _scan history buffer
  40.   addhistory "_scan" scanstring
  41.  
  42.   var search_string
  43.   var filespec
  44.   var options
  45.  
  46.   // parse the scan multi-string
  47.   n = splitstr '' scanstring
  48.           ref search_string  ref filespec  ref options
  49.   if n < 3 then
  50.     options = _SearchOpt
  51.     if n < 2 then
  52.       filespec = '.'
  53.     end
  54.   end
  55.   if not search_string then
  56.     return
  57.   end
  58.   options = options + '*'
  59.  
  60.   // fully qualify the filespec
  61.   dirname = qualify filespec (getbufname)
  62.   if not dir? dirname then
  63.     display
  64.     say dirname + " is not a directory" 'b'
  65.     return
  66.   end
  67.  
  68.   // create the buffer to hold occurrences
  69.   buffer = createbuf
  70.   ovltext "≡≡≡≡≡≡ Select this line to edit occurrences ≡≡≡≡≡≡"
  71.  
  72.   // load the directory into dirbuffer
  73.   dirbuffer = loadbuf dirname
  74.   if not dirbuffer then
  75.     destroybuf
  76.     return
  77.   end
  78.   if not getlinelen then
  79.     destroybuf
  80.     destroybuf
  81.     display
  82.     say  "'" + dirname + "' not found"  'b'
  83.     return
  84.   end
  85.  
  86.   // sort the directory buffer
  87.   sendobject "fmgr" "fsort" 'n'
  88.  
  89.   // do for all files in the directory buffer
  90.   notify = "onscanning"
  91.   repeat
  92.     file = sendobject "fmgr" "getffile"
  93.  
  94.     // load each file in the directory
  95.     if loadbuf file then
  96.       lines = getlines buffer
  97.       name = getname file
  98.  
  99.       // use 'onscanning' to display the scan progress window
  100.       send notify name
  101.  
  102.       // find all occurrences of the search string in the file
  103.       while find  search_string options  do
  104.         addline  name + ' (' + getrow + "): " + gettext  '' '' buffer
  105.         col MAX_COL
  106.       end
  107.  
  108.       // remove the file from memory
  109.       destroybuf
  110.  
  111.       // notify 'onscanning' if any occurrences were found
  112.       if lines < (getlines buffer) then
  113.         send notify name 1
  114.       end
  115.     end
  116.   until not down
  117.  
  118.   // remove the scan progress window
  119.   send notify
  120.  
  121.   // destroy the directory buffer
  122.   destroybuf
  123.  
  124.   // display occurrences
  125.   if (getlines buffer) > 1 then
  126.     bname = getbufname
  127.     title = "Occurrences of '" + search_string + "' in " +
  128.             dirname + " - " + ((getlines buffer) - 1) + " lines"
  129.     line = popup buffer title getvidcols - 11 getvidrows - 8
  130.     if line then
  131.  
  132.       // edit the occurrences
  133.       if line [1] == '≡' then
  134.         delline 1 1 buffer
  135.         setbufname (qualify "TEMP.TXT" bname) buffer
  136.         openbuf buffer
  137.  
  138.       // position the cursor to the selected occurrence in the file
  139.       else
  140.         destroybuf buffer
  141.         p = pos ' ' line
  142.         name = line [1 : p - 1]
  143.         line = line [p + 2 : TO_END]
  144.         linenumber = line [1 : (pos ')' line 'r') - 1]
  145.         open (qualify name dirname)
  146.         bufferflag '-m'
  147.         gotopos 1 linenumber
  148.         onfound (find search_string  options + '*')
  149.       end
  150.     end
  151.   else
  152.     destroybuf buffer
  153.     display
  154.     say  "'" + scanstring + "' not found"  'b'
  155.   end
  156.  
  157.