;dBRIEF Library  - v3.10
;Copyright (c) 1991 - Global Technologies Corporation
;ALL RIGHTS RESERVED
#include "dbrief.h"
(macro _init
	(
		(int				lib_current_buffer
		)
		(= lib_current_buffer (inq_buffer))
		(if library_buffer
			(
				(delete_buffer library_buffer)
				(= library_buffer 0)
			)
		)
		(if (exist dbr_library)
			(= library_buffer (create_buffer "Library" dbr_library 1))
		;else
			(
				(= library_buffer (create_buffer "Library" "library.src" 1))
				(set_buffer library_buffer)
				(insert "\n* eof\n")
				(set_buffer lib_current_buffer)
			)
		)
	)
)
(macro _sv
	(returns "")
)
(macro sv
	(
		(if (== (inq_assignment "_implode_object_window" 1) "")
			(
				(string			note_
									name_
				)
				(if (inq_marked)
					(
						(if (get_parm 0 note_ "Module description: ")
							(if (get_parm 1 name_ "Module name: ")
								(_process_library_call note_ name_)
							)
						)
					)
				;else
					(libr)
				)
			)
		;else
			(_display_popup_message "SourceView unavailable during object edit!" "" 1)
		)
	)
)
(macro libr
	(
		(int		program_line
					start_line
					end_line
					screen_lines
					lib_menu_buffer
					lib_current_buffer
		)
		(string  menu_line
					routine_desc
					routine_name
		)
		(= temp_int 0)
		(= lib_current_buffer (inq_buffer))
		(= lib_menu_buffer (create_buffer "SourceView" NULL 1))
		(set_buffer library_buffer)
		(top_of_buffer)
		(message "Scanning for modules...")
		(while (search_fwd "note-" 1 0)
			(
				(= routine_desc (substr (read) 6))
				(move_abs 0 1)
				(move_rel 1 0)
				(= routine_name (substr (read) 3))
				(if (strlen routine_desc)
					(
						(++ temp_int)
						(message "Scanning for modules [#%d]..." temp_int)
						(inq_position program_line)
						(sprintf menu_line "%s  %s Line:%d		 |;_r_goto \"%d\"\n" (_pad_right routine_desc 48) (_pad_right routine_name 16) program_line program_line)
						(set_buffer lib_menu_buffer)
						(insert menu_line)
						(set_buffer library_buffer)
					)
				)
				(move_rel 1 0)
				(move_abs 0 1)
			)
		)
		(= temp_int 0)
		(set_buffer lib_menu_buffer)
		(end_of_buffer)
		(delete_line)
		(inq_position program_line)
		(top_of_buffer)
		(set_buffer lib_current_buffer)
		(if (> program_line 1)
			(
				(_pick_menu "" "" 10 (_menu_lines program_line) 76 03 "_dbr_library_goto" lib_menu_buffer (substr (+ "Library: " dbr_library) 1 48))
				(if temp_int
					(
						(set_buffer library_buffer)
						(goto_line (+ temp_int 1))
						(beginning_of_line)
						(inq_position start_line NULL)
						(search_fwd "{note-}|{\\* eof}" 1 0)
						(move_rel -1 0)
						(inq_position end_line NULL)
						(set_buffer lib_current_buffer)
						(inq_position NULL temp_int)
						(set_buffer library_buffer)
						(while (<= start_line end_line)
							(
								(move_abs start_line 1)
								(= temp_str (read))
								(++ start_line)
								(set_buffer lib_current_buffer)
								(move_abs 0 temp_int)
								(insert temp_str)
								(set_buffer library_buffer)
							)
						)
						(set_buffer lib_current_buffer)
						(move_abs 0 temp_int)
					)
				)
			)
		;else
			(_display_popup_message "No library modules found in %s!" (upper dbr_library) 1)
		)
	)
)
(macro _dbr_library_goto
	(
		(int				lib_event_type
		)
		(string			lib_event_string
		)
		(get_parm 0 lib_event_type)
		(get_parm 1 lib_event_string)
		(switch lib_event_type
			45		NULL
			27
				(
					(= temp_int 0)
					(exit)
				)
			13
				(
					(= temp_int (atoi (substr lib_event_string (+ (index lib_event_string ";") 10))))
					(exit)
				)
			00		NULL
			01
				(message "Paste: %s" (substr lib_event_string 1 30))
		)
		(return 1)
	)
)
(macro _process_library_call
	(
		(int				lib_file_position
							lib_current_buffer
							start_line
							end_line
		)
		(string			lib_file_name
							lib_temp_str
							lib_temp_name
							note_
							name_
		)
		(if (&& (inq_marked) (== (inq_assignment "_implode_object_window" 1) ""))
			(
				(if (get_parm 0 note_ "Module description: ")
					(if (get_parm 1 name_ "Module name: ")
						(
							(copy)
							(= lib_current_buffer (inq_buffer))
							(set_buffer library_buffer)
							(end_of_buffer)
							(search_back "\* eof" 1 0)
							(beginning_of_line)
							(sprintf lib_temp_str "note- %s\n" note_)
							(insert lib_temp_str)
							(sprintf lib_temp_str "* %s\n" name_)
							(insert lib_temp_str)
							(paste)
							(insert "\n")
							(write_buffer)
							(set_buffer lib_current_buffer)
						)
					)
				)
			)
		;else
			(
				(save_position)
				(beginning_of_line)
				(= lib_temp_str (read))
				(= temp_int (index lib_temp_str (ltrim (trim lib_temp_str))))
				(= lib_temp_str (ltrim (trim lib_temp_str)))
				(= lib_file_position (index (upper lib_temp_str) ".SRC"))
				(if lib_file_position
					(
						(= lib_file_name (trim (ltrim (substr lib_temp_str 1 (+ lib_file_position 3)))))
						(= lib_temp_str (trim (ltrim (substr lib_temp_str (+ lib_file_position 5) 24))))
						(= lib_temp_name dbr_library)
						(= dbr_library lib_file_name)
						(_init)
					)
				)
				(if (strlen lib_temp_str)
					(
						(= lib_current_buffer (inq_buffer))
						(set_buffer library_buffer)
						(top_of_buffer)
						(+= lib_temp_str "\n")
						(if (search_fwd (+ "* " lib_temp_str) 0 0)
							(
								(beginning_of_line)
								(move_rel 1 0)
								(inq_position start_line NULL)
								(search_fwd "{note-}|{\\* eof}" 1 0)
								(move_rel -1 0)
								(inq_position end_line NULL)
								(set_buffer lib_current_buffer)
								(beginning_of_line)
								(delete_to_eol)
								(set_buffer library_buffer)
								(while (<= start_line end_line)
									(
										(move_abs start_line 1)
										(= lib_temp_str (read))
										(++ start_line)
										(set_buffer lib_current_buffer)
										(move_abs 0 temp_int)
										(insert lib_temp_str)
										(set_buffer library_buffer)
									)
								)
								(set_buffer lib_current_buffer)
								(move_abs 0 temp_int)
								(restore_position 0)
							)
						;else
							(
								(if (index lib_temp_str " ")
									(= lib_temp_str (substr lib_temp_str 1 (- (index lib_temp_str " ") 1)))
								)
								(_display_popup_message "Library module %s!" (+ (+ (trim (upper lib_temp_str)) " not found in ") (upper dbr_library)) 1)
								(set_buffer lib_current_buffer)
								(attach_buffer lib_current_buffer)
								(restore_position)
							)
						)
					)
				;else
					(
						(restore_position)
						(_complete_conditional)
					)
				)
				(if lib_file_position
					(
						(= dbr_library lib_temp_name)
						(_init)
					)
				)
			)
		)
	)
)
