;**
;**
;**		BRIEF -- Basic Reconfigurable Interactive Editing Facility
;**
;**
;**		Macro File:
;**
;**			calls.m
;**
;**		Written by:
;**
;**			Dar¡o Alcocer (9/21/88)
;**			CompuServ 72411,430
;**
;**		Comments:
;**
;**		This package, which uses the Dialog Manager Package,
;**		is used to create a C function call from function
;**		prototype information.  If you have any comments or
;**		suggestions, please leave E-mail at address above.
;**
;**		Hope you enjoy it!
;**
;**

#include "dialog.h"

#define SYSTEM					 1

;**	Change the following define if you would
;**	like to use a different name for the C
;**	function file.
#define C_CALL_DATA 			"calls.dat"

#define DIALOG_DATA 			"cdialog.dat"

#define LEFTX					30
#define RIGHTX					75
#define TOPY					10
#define BOTTOMY				21

#define STARTLINE				 3	

(extern add_to_path)

;**		c_call:
;**
;**		Prompts user for C function.  If function is found,
;**		user is prompted for actual parameter list and inserts
;**		function call built into current buffer.
;**
(macro c_call
	(
		(int orgbuf c_callbuf c_databuf nargs)
		(string fname arg1 arg2 arg3 arg4 arg5 arg6 dialogdata)
		(global fname arg1 arg2 arg3 arg4 arg5 arg6 nargs)

		;** Get function name. If string is empty,
		;** display error and return.
		;**
		(get_parm 0 fname "Function: ")
		(if (!(strlen fname))
			(
				(error "Invalid function name.")
				(return)
			)
		)

		;** Open C call data file to search
		;** for function. If zero buffer id is returned,
		;** could not open buffer.
		;**
		(if (!(= c_callbuf (create_buffer C_CALL_DATA
									(add_to_path _dialog_dir C_CALL_DATA) SYSTEM)))
			(
				(error "C_call data buffer not opened.")
				(return)
			)
		)

		;** Make C call data file current buffer,
		;** preparing for search.
		;**
		(= orgbuf (inq_buffer))
		(set_buffer c_callbuf)

		;** Search for function. If not found,
		;** report error and restore original buffer.
		;**
		(top_of_buffer)
		(if (! (search_fwd (+ (+ ";" fname) ";")))
			(
				(message "Function not found.")
				(set_buffer orgbuf)
				(return)
			)
		)

		;** Now, open dialog box data buffer.
		;** If zero buffer id is returned, show
		;** error and restore previous buffer.
		;**
		(if (!(= c_databuf (create_buffer "data" DIALOG_DATA SYSTEM)))
			(
				(error "Dialog data buffer not opened.")
				(set_buffer orgbuf)
				(return)
			)
		)

		;** Read dialog box information for function
		;** a line at a time. Write line read to dialog
		;** data buffer.
		;**
		(down)
		(beginning_of_line)
		(
			(string tmpbuf savbuf)
			(while (!= (substr (= tmpbuf (read)) 1 1) ";")
				(
					(set_buffer c_databuf)
					(insert tmpbuf)
					(set_buffer c_callbuf)
					(down)
				)
			)
		)

		;** Find out how many arguments the function has,
		;** and save this number.
		(down)
		(= nargs (atoi (read)))

		;** Restore orginal buffer in order to
		;** insert function call.
		;**
		(set_buffer orgbuf)

		;** Append left parenthesis, will append
		;** actual parameters for function call later.
		;**
		(+= fname "(")

		;** Create dialog box and retrieve actual
		;** parameters for function call.
		;**
		(_process_dialog_box LEFTX BOTTOMY RIGHTX TOPY
								"call" "Build function call"
								NULL c_databuf "read_parms")

		;** Delete dialog data buffer just used,
		;** no longer needed.
		;**
		(delete_buffer c_databuf)

		;** Insert function call just built by user.
		;**
		(insert fname)
	)
)


;**		read_parms:
;**
;**		Basically, this function is the action macro that
;**		works with _process_dialog_box macro to retrieve
;**		the individual parameters from the user.
;**
(macro read_parms
	(
		(int event_type line_no)
		(string strvar)

		(get_parm 0 event_type)

		(switch event_type
			DIALOG_EXIT_FIELD
			(
				(get_parm 1 line_no)
				(get_parm 2 strvar)
				;** Get arguments from user.
				;**
				(switch (- line_no STARTLINE)
					0
					(= arg1 strvar)
					1
					(= arg2 strvar)
					2
					(= arg3 strvar)
					3
					(= arg4 strvar)
					4
					(= arg5 strvar)
					5
					(= arg6 strvar)
				)
			)
			DIALOG_F10
			(
				;** Combine all arguments retrieved
				;** with function name.
				;**
				(+= fname arg1)
				(if (== nargs 1)
					(
						(+= fname ")")
						(return)
					)
				)

				(+= fname ",")
				(+= fname arg2)
				(if (== nargs 2)
					(
						(+= fname ")")
						(return)
					)
				)

				(+= fname ",")
				(+= fname arg3)
				(if (== nargs 3)
					(
						(+= fname ")")
						(return)
					)
				)

				(+= fname ",")
				(+= fname arg4)
				(+= fname ")")
			)
		)

		(returns TRUE)
	)
)
