home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / wp / bmacs.zip / COMPARE.M < prev    next >
Text File  |  1987-05-21  |  13KB  |  271 lines

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                         ;;
  3. ;; [This macro was written by Bob Young at Ernst & Whinney in Washington,  ;;
  4. ;;  D.C.  Any comments or questions would be appreciated.  You may make    ;;
  5. ;;  comments via the BRIEF EBB or contact me directly at (202) 862-6339. ] ;;
  6. ;;                                                                         ;;
  7. ;; MACRO COMPARE.M  (Version 2.0+ BRIEF ONLY)                              ;;
  8. ;; ==========================================                              ;;
  9. ;;                                                                         ;;
  10. ;; To compare two windows (representing DIFFERENT buffers) of text,        ;;
  11. ;; place the cursor at the points you wish to start comparing in EACH      ;;
  12. ;; window (normally top_of_buffer).                                        ;;
  13. ;;                                                                         ;;
  14. ;; Press <F-10> and type "compare".  The program will ask you to point     ;;
  15. ;; (with the arrow keys) to the second window.  If there is an appropriate ;;
  16. ;; window there, comparison will begin, otherwise an error message will    ;;
  17. ;; print.  You may abort the compare process by hitting <ESC> instead      ;;
  18. ;; of an arrow key.                                                        ;;
  19. ;;                                                                         ;;
  20. ;; If a discrepency is encountered the program stops, leaving the          ;;
  21. ;; cursors one character after the first difference encountered            ;;
  22. ;; in each window, and displays an error message showing an 11-character   ;;
  23. ;; fragment of each line with the disagreeing characters as the            ;;
  24. ;; middle-most characters in the two strings.                              ;;
  25. ;;                                                                         ;;
  26. ;; You may continue comparison by pressing <Alt-J>.  Once and end-of-file  ;;
  27. ;; is encountered the <Alt-J> is disabled, and you must type <F-10>        ;;
  28. ;; "compare" to re-start comparison.                                       ;;
  29. ;;                                                                         ;;
  30. ;; The <Alt-J> hot key may be replaced by one of your choosing by          ;;
  31. ;; editing the appropriate line, and recompiling this macro.               ;;
  32. ;;                                                                         ;;
  33. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  34.  
  35.  
  36. (macro compare
  37.    (
  38.       (extern center_line)
  39.       (int input_char char_cntr eob_flag err_flag loop)
  40.       (int wndw1 wndw2 buf1 buf2 line1 col1 line2 col2)
  41.       (int orig_line1 orig_col1 orig_line2 orig_col2)
  42.       (string string1 string2 err_string)
  43.       (string hot_key)
  44.  
  45.       (= hot_key "<Alt-J>")                        ;<=== EDIT THIS LINE
  46.                                                    ;     TO CHANGE HOTKEY
  47.  
  48.       (inq_position orig_line1 orig_col1)          ;save original position
  49.                                                    ;in window 1 if no
  50.                                                    ;mismatch is encountered
  51.  
  52.       (= buf1 (inq_buffer))                        ;get buffer_id #1
  53.  
  54.       (= wndw1 (inq_window))                       ;get window_id #1
  55.  
  56.       (while 1
  57.          (
  58.             (message "Point to window to compare ...")
  59.  
  60.             (while (! (inq_kbd_char)))             ;wait for input
  61.  
  62.             (= input_char (read_char))             ;read the character
  63.                                                     
  64.             (if (== 0 (% input_char 256))          ;check if it's extended
  65.                (
  66.                   (= input_char (/ input_char 256))   ;get scan code
  67.  
  68.                   (switch input_char                  ;set appropriate
  69.                                                       ;direction value   
  70.                      72                ;up arrow
  71.                         (
  72.                            (= wndw2 0)
  73.                            (break)
  74.                         )
  75.                      77                ;right arrow
  76.                         (
  77.                            (= wndw2 1)
  78.                            (break)
  79.                         )
  80.                      80                ;down arrow
  81.                         (
  82.                            (= wndw2 2)
  83.                            (break)
  84.                         )
  85.                      75                ;left arrow
  86.                         (
  87.                            (= wndw2 3)
  88.                            (break)
  89.                         )
  90.                      ;default
  91.                         NULL
  92.                   )
  93.                )
  94.             ;else
  95.                (if (== 27 (% input_char 256))      ;check for <ESC>
  96.                   (
  97.                      (beep)
  98.                      (message "Aborted...")
  99.                      (return -1)
  100.                   )
  101.                )
  102.             )
  103.             (beep)                                 ;neither arrow key
  104.                                                    ;nor <ESC> hit --
  105.                                                    ;loop back for another
  106.                                                    ;key
  107.          )
  108.       )
  109.  
  110.       (if (< (change_window wndw2) 0)              ;is change unsuccessful?
  111.                                                    ;i.e., checks for valid
  112.                                                    ;second window
  113.          (
  114.             (beep)                                 ;if not valid -- error
  115.             (return -1)
  116.          )
  117.       )
  118.  
  119.       (inq_position orig_line2 orig_col2)          ;save original position
  120.                                                    ;in window 2 if no
  121.                                                    ;mismatch is encountered
  122.  
  123.  
  124.       (= buf2 (inq_buffer))                        ;get buffer_id #2
  125.  
  126.       (= wndw2 (inq_window))                       ;get window_id #2
  127.  
  128.       (set_window wndw1)                           ;go back to original window
  129.  
  130.       (if (== buf1 buf2)                           ;check to see if we're
  131.                                                    ;attempting to compare
  132.                                                    ;same buffer -- pointless!!
  133.          (
  134.             (beep)                                 ;if so -- error
  135.             (error "Comparing same file ...")
  136.             (return -1)
  137.          )
  138.       )
  139.  
  140.       (message "Comparing windows ...")
  141.  
  142.  
  143.       (assign_to_key hot_key "compare")            ;sets up hot key to
  144.                                                    ;continuing comparing
  145.                                                    ;after mismatch
  146.       (while 1
  147.          (
  148.             (set_buffer buf1)                      ;compare off-screen
  149.             (inq_position line1 col1)              ;get current position
  150.             (= string1 (read))                     ;read string1 in first
  151.             (beginning_of_line)                    ;reposition
  152.             (= err_flag (move_rel 1 0))            ;go to next line
  153.             (if (== err_flag 0) (= eob_flag 1))    ;if no next line set flag
  154.    
  155.             (set_buffer buf2)                      ;go to second buffer
  156.             (inq_position line2 col2)              ;get current position
  157.             (= string2 (read))                     ;read string2 in next
  158.             (beginning_of_line)                    ;reposition
  159.             (= err_flag (move_rel 1 0))            ;go to next line
  160.             (if (&& (== err_flag 0) (== eob_flag 1))  ;if no next line
  161.                                                       ;and buffer #1 ALSO
  162.                                                       ;had no next line
  163.                                                       ;(i.e., flag set)
  164.                                                       ;we're at end of files
  165.                (
  166.                   (while (< loop 5)                ;signal OK with 5 beeps
  167.                      (
  168.                         (beep)
  169.                         (= char_cntr 0)
  170.                         (while (< char_cntr 50) (+= char_cntr 1)) ;time delay
  171.                         (+= loop 1)
  172.                      )
  173.                   )
  174.                   (set_window wndw2)               ;change to window #2
  175.                   (attach_buffer buf2)             ;attach buffer #2 to it
  176.                   (move_abs orig_line2 orig_col2)  ;go to position at start
  177.                                                    ;of this compare
  178.                   (refresh)                        ;refresh screen
  179.  
  180.                   (set_window wndw1)               ;change to window #1
  181.                                                    ;and do same thing
  182.                   (attach_buffer buf1)
  183.                   (move_abs orig_line1 orig_col1)
  184.                   (refresh)
  185.  
  186.                   (message "End of files reached ... no differences!")
  187.                   (assign_to_key hot_key "nothing")     ;clear hot key
  188.  
  189.                   (return 0)                            ;NORMAL EXIT
  190.                )
  191.             )
  192.    
  193.             (if (&& (== err_flag 0) (!= eob_flag 1))    ;Buffer #2 ran out
  194.                                                         ;before #1 did
  195.                (
  196.                   (beep)                                ;error return
  197.                   (refresh)
  198.                   (set_buffer buf1)
  199.                   (refresh)
  200.                   (error "Premature end-of-file reached!")
  201.                   (assign_to_key hot_key "nothing")     ;clear hot key
  202.  
  203.                   (return -1)
  204.                )
  205.             )
  206.    
  207.             (if (!= string1 string2)               ;mismatch found
  208.                (
  209.                   (= char_cntr 1)
  210.  
  211.                   (while (== (substr string1 char_cntr 1) (substr string2 char_cntr 1))
  212.                      (+= char_cntr 1)              ;find offending characters
  213.                   )
  214.  
  215.                   (set_window wndw2)               ;move to window #2
  216.                   (attach_buffer buf2)             ;atach buffer #2 to it
  217.                   (move_abs line2 col2)            ;move back to offending
  218.                                                    ;line
  219.                   (move_rel 0 char_cntr)           ;reposition cursor after
  220.                                                    ;offending character
  221.                   (center_line)                    ;center line in window
  222.                   (refresh)                        ;refresh
  223.  
  224.                   (set_window wndw1)               ;do same as above for
  225.                                                    ;window #1
  226.                   (attach_buffer buf1)
  227.                   (move_abs line1 col1)
  228.                   (move_rel 0 char_cntr)
  229.                   (center_line)
  230.                   (refresh)
  231.  
  232.                   (if (< char_cntr 6)              ;create fragment strings
  233.                                                    ;for error message
  234.  
  235.                      (                             ;do this section if
  236.                                                    ;first offending character
  237.                                                    ;is within 5 characters
  238.                                                    ;of beginning of line
  239.                         (= string1 (substr string1 1 (+ char_cntr 5)))
  240.                         (= string1 (+ string1 ".."))
  241.                         (= string2 (substr string2 1 (+ char_cntr 5)))
  242.                         (= string2 (+ string2 ".."))
  243.                      )
  244.                   ;else                            ;else do this ...
  245.                      (
  246.                         (= string1 (substr string1 (- char_cntr 5) 11))
  247.                         (= string1 (+ ".." string1))
  248.                         (= string1 (+ string1 ".."))
  249.                         (= string2 (substr string2 (- char_cntr 5) 11))
  250.                         (= string2 (+ ".." string2))
  251.                         (= string2 (+ string2 ".."))
  252.                      )
  253.                   )
  254.                   (= err_string (+ "'" string1))   ;build error string
  255.                   (= err_string (+ err_string "' | '"))
  256.                   (= err_string (+ err_string string2))
  257.                   (= err_string (+ err_string "'"))
  258.  
  259.                   (beep)
  260.                   (error "Mismatch: %s" err_string)   ;display error
  261.                   (return -1)                         ;exit, leaving
  262.                                                       ;hot key assigned
  263.                                                       ;for subsequent
  264.                                                       ;comparing
  265.                )
  266.             )
  267.          )
  268.       )
  269.    )
  270. )
  271.