;**	Basic.m - compiles IBM BASIC within Brief.
;**
;**   Written by: Stan Johnson
;**	
;**	Revision History:
;**	------------------
;**  March 10,1987        -    Macro was created.
;**
;**	Notes on usage:	
;**
;**   Basic.m uses the generic error correction macro developed by Soluion
;**   Systems. The errorfix_generic macro directs the error output file
;**   of the IBM BASIC Complier to a horizontal window on the top part of 
;**   the screen and the code to a window on the bottom.
;**
;**   THIS MACRO REQUIRES THAT TABS BE CONVERTED TO SPACES
;**
;**   The symbol BSWITCH must conatain the switches required for the compiler

#define TRUE  1
#define FALSE 0

(macro basic
	(
      (int last_basic_error)
      (global last_basic_error)
		(assign_to_key "%#113" "basic_compile")
		(message "BASIC compile macros loaded.")
	)
)

(macro basic_compile
	(
		(string	extension err_file file_name command)
		(global extension err_file file_name )
      (inq_names NULL extension file_name)
  	 	(if (== extension "bas")
  	 		(		
            (= command "bascom %s")
            (+= command (inq_environment "BBAS_SW"))
            (+= command "\; >&")
            (cc command "bas")
            (error_file)
            (if (exist err_file)
               (errorfix_generic)
            ;else
               (message "No errors found -- PROCEED WITH LINK")
            )
			)
		;else
		  	(error "Can't compile: not a .bas file.")
		)
	)
)


(macro errorfix_generic
	(
		(string error_file)
		(int old_buffer new_buffer wl dl)
		(global old_buffer new_buffer)

	 	(inq_names error_file NULL NULL)
		(= error_file (+ (substr error_file 1 (rindex error_file ".")) "err"))
		(= old_buffer (inq_buffer))
		(= new_buffer (create_buffer "Errors" error_file))
		(create_edge 0)
      (inq_window_size wl)
      (= dl 5)
      (-= dl wl)
      (move_edge 2 dl)
		(attach_buffer new_buffer)
		(set_buffer new_buffer)
		(change_window 2)
		(assign_to_key "#14" "nxt_error")
		(assign_to_key "#16" "prvious_error")
		(assign_to_key "#27" "no_more_errors")
		(error "Next: [Ctrl-n]  Previous: [Ctrl-p]  or [Esc]")
      (first_error)
	)
)

(macro nxt_error
	(
      (string s_line)
      (int temp col line success)
		(change_window 0)
      (while (< line last_basic_error)
         (
            (= success (search_fwd "^" 0))
            (if (> success 0)
               (
                  (inq_position line col)
                  (if (< line last_basic_error)
                     (
                        (++ last_basic_error)
                        (move_rel 0 2)
                     )
                  )
               )
            ;else
               (= last_basic_error 0)
            )
         )
      )
      (if (> success 0)
         (
            (= last_basic_error line)
            (+= last_basic_error 1)
            (move_rel -1 0)
            (beginning_of_line)
            (move_rel 0 16)
            (= s_line (read))
            (to_top)
            (end_of_line)
            (right)
            (right)
            (right)
            (right)
       		(change_window 2)
            (if (search_fwd s_line 0)
               (
                  (-= col 17)
                  (move_rel 0 col)
            		(error "Next: [Ctrl-n]  Previous: [Ctrl-p]  or [Esc]")
               )
            ;else
               (
                  (message "That error line has been changed")
                  (beep)
               )
            )
         )
      ;else
         (
            (change_window 2)
            (beep)
            (message "No more errors press esc to close error window")
         )
      )
	)
)

(macro first_error
   (
      (string s_line)
      (int ncol col line)
	 	(change_window 0)
      (search_fwd "^" 0)
      (inq_position line col)
      (= last_basic_error line)
      (+= last_basic_error 1)
      (move_rel -1 0)
      (beginning_of_line)
      (move_rel 0 16)
      (= s_line (read))
      (to_top)
      (end_of_line)
      (right)
      (right)
      (right)
      (right)
		(change_window 2)
      (top_of_buffer)
      (if (search_fwd s_line 0)
         (
            (-= col 17)
            (move_rel 0 col)
         )
      ;else
         (
            (message "That error line has been changed")
            (beep)
         )
      )
	)
)
(macro prvious_error
	(
      (int success)
		(change_window 0)
      (move_rel -1 0)
		(= success (search_back "% [0-9A-F]" 1))
      (if (> success 0)
         (
      		(change_window 2)
            (top_of_buffer)
            (first_error)
         )
      ;else
         (
      		(change_window 2)
            (beep)
            (message "No previous error to correct")
         )
      )
	)
)

(macro no_more_errors
	(
		(delete_edge 0)
		(assign_to_key "#27" "self_insert")
	)
)
(macro error_file
   (
      (if (first_time)
         (
            (= err_file (substr file_name 1 (index file_name ".")))
            (+= err_file "err")
         )
      )
   )
)
