;## ************************************************************************
;## *    Brief macro to draw comment boxes in C source code, MASM source   *
;## *    code, DOS batch files and Brief macros.                           *
;## ************************************************************************
#define MAXLINES 101

;##
;## make sure that the box has a valid file extension, it must be one
;## of the following: .c   .y (yacc)  .asm   .m (brief macro)  .bat
;##
(macro box
	(
	(string ext)
	(string path)
	(string msg)
	(int curr_line curr_col num_lines mode)

	(inq_names path ext NULL)					;## get extension
	(if (&& (&& (&& (&& (!= ext "c") (!= ext "y")) (!= ext "bat")) (!= ext "asm")) (!= ext "for"))
		(message "%s" "Invalid file extension for macro \"box\"")
		;else
		(
			(sprintf msg "%s comment lines added (overtype activated)" "%d")
			(= mode (inq_mode))					;## are we inserting	?
;##
;## If we are currently in overlay mode, change the mode to insert (so that
;## nothing is destroyed).  At the end of processing, if we changed to 
;## insert mode, then change back.   If already in insert mode, do nothing.
;##
			(if (== 0 mode)						;## if overlay 
				(insert_mode)						;##    change to insert
			)
	
			(sprintf msg "%s comment lines added (overtype activated)" "%d")
;##
;## Do the work.  The return code is the number of lines added or it is
;## 0 if an invalid number of lines was requested.
;##
			(= num_lines (do_it ext))				;## add the comments
			(if num_lines
				(message msg num_lines)			;## no errors
				(message "Invalid number of lines")	;## oops!
			)
			(insert_mode)								;##  change to overtype
		)
	)
	)
)
;##
;## based upon data set type (extension), determine the variables
;## to be passed to the routine that actually draws the box.
;##
(macro do_it
	(
		(string path)
		(string ext)								;## extension save area
		(string box_top)
		(string box_side)
		(string box_bottom)
		(int num_lines)							;## hold number of lines to insert
		(int sav_num_lines)

		(get_parm 0 ext)
		(get_parm 1 num_lines "Enter the number of comment lines you want: ")
		(= sav_num_lines num_lines)			;## save it
		(if ( || (<= num_lines 0) (>= num_lines MAXLINES))
			(returns 0)								;## exit with error code
			(
				(inq_names path ext NULL)	
 			   (if (== ext "c")
         		(
						(= box_top    "/**********************************************************************\\\n")
						(= box_side   "|*                                                                    *|\n")
						(= box_bottom "\\**********************************************************************/\n")
						(_do_box num_lines box_top box_side box_bottom 4)
					)
      		;else
				(if (== ext "bat")
					(
						(= box_top  "REM *******************************************************************\n")
						(= box_bottom box_top)
						(= box_side "REM *                                                                 *\n")
						(_do_box num_lines box_top box_side box_bottom 7)
					)
       	  ;else
				(if ( == ext "for") 
					(
						(= box_top    "\C**********************************************************************\n")
						(= box_bottom box_top)
						(= box_side   "\C*                                                                    *\n")
					   (_do_box num_lines box_top box_side box_bottom 4)
 					)
			   ;else if( (== ext "asm")
					(
						(= box_top    "\;**********************************************************************\n")
						(= box_bottom box_top)
						(= box_side   "\;*                                                                    *\n")
					   (_do_box num_lines box_top box_side box_bottom 4)
					)
 				)))
				(returns sav_num_lines)
			)
		)
	)
)
;##
;## logic to draw a box for all acceptable data set types
;##
(macro _do_box
	(
		(int num_lines)
		(string box_top)
		(string box_side)
		(string box_bottom)
		(int end_column)						;## where the cursor finally rests
		(int i)
;##
;## get passed parameters
;##
		(get_parm 0 num_lines)
		(get_parm 1 box_top)
		(get_parm 2 box_side)
		(get_parm 3 box_bottom)
		(get_parm 4 end_column)
;##
;## build the box	on the line following current line
;##
		(beginning_of_line)
		(down)
		(insert box_top)
		(= i (+ num_lines 1))
		(while num_lines
			(
				(insert box_side)
				(-- num_lines)
			)
		)
		(insert box_bottom)
		(beginning_of_line)
;##
;## position to first line of column, 2nd space
;##
		(while i
			(
				(up)
				(-- i)
			)
		)
		(move_abs 0 end_column)
		)
)


