;dBRIEF Optimize - v3.10
;Copyright (c) 1991 - Global Technologies Corporation
;ALL RIGHTS RESERVED
#include "dbrief.h"
(macro opti
	(
		(string		pass_1
						pass_2
						pass_3
						pass_4
						pass_5
						srce_file
						dest_file
						buff_name
		)
		(if (! (get_parm 0 pass_1 "Remove indentations? [yN]: " 1 "N"))
			(return 0)
		)
		(if (! (get_parm 1 pass_2 "Remove comments? [yN]: " 1 "N"))
			(return 0)
		)
		(if (! (get_parm 2 pass_3 "Remove blank lines? [yN]: " 1 "N"))
			(return 0)
		)
		(if (! (get_parm 3 pass_4 "Remove trailing spaces? [yN]: " 1 "N"))
			(return 0)
		)
		(if (!= (upper pass_1) "Y")
			(if (! (get_parm 3 pass_5 "Convert spaces to tabs? [yN]: " 1 "N"))
				(return 0)
			)
		)
		(message "Please wait...")
		(if (inq_modified)
			(write_buffer)
		)
		(inq_names buff_name NULL NULL)
		(= srce_file buff_name)
		(= buff_name (substr buff_name 1 (search_string "?." buff_name)))
		(= dest_file (+ buff_name ".bak"))
		(if (exist dest_file)
			(del dest_file)
		)
		(edit_file dest_file)
		(read_file srce_file)
		(write_buffer)
		(delete_buffer (inq_buffer))
		(edit_file srce_file)
		(top_of_buffer)
		(if (== (upper pass_1) "Y")
			(
				(message "Removing indentations, stand by...")
				(translate "<[ \\t]" "" 1 1)
			)
		)
		;**  Remove comments.
		(if (== (upper pass_2) "Y")
			(
				(message "Removing comments, stand by...")
				(translate "{{&&}|{<[ \\t]@\\*}|{<[ \\t]@NOTE}|{<[ \\t]@note}}*>" "" 1 1 0)
			)
		)
		(if (== (upper pass_3) "Y")
			(
				(message "Removing blank lines, stand by...")
				(translate "<[ \\t]@\\n" "" 1 1)
			)
		)
		(if (== (upper pass_4) "Y")
			(
				(message "Removing trailing spaces, stand by...")
				(translate "[ \\t]+>" "" 1 1)
			)
		)
		(if (== (upper pass_5) "Y")
			(
				(message "Replacing spaces with tabs, stand by...")
				(_tab_support "T")
			)
		)
		(message "Saving optimized file %s, stand by..." buff_name)
		(if (inq_modified)
			(write_buffer)
		)
		(top_of_buffer)
		(_display_popup_message "Optimization complete." "" 0)
	)
)
(macro _tab_support
	(
		(int			curr_line
						last_line
						old_col
						new_col
		)
		(string		orig_line
						message_string
						tab_operation
		)
		(extern _block_search)
		(if (get_parm 0 tab_operation "Convert spaces->Tabs or tabs->Spaces? [Ts]: " 1 "T")
			(
				(save_position)
				(if (! (inq_marked))
					(top_of_buffer)
				;else
					(
						(swap_anchor)
						(= _block_search 1)
					)
				)
				(if (== (upper tab_operation) "T")
					(
						(= orig_line (_replicate " " (distance_to_tab)))
						(= tab_operation (use_tab_char "y"))
						(while (&& (search_fwd orig_line 1)(! (inq_kbd_char)))
							(
								(inq_position NULL old_col)
								(self_insert (key_to_int "<Tab>"))
								(inq_position curr_line new_col)
								(delete_char (- new_col old_col))
								(= orig_line (_replicate " " (distance_to_tab)))
								(message "Inserting tabs - line: %d, any key to abort..." curr_line)
							)
						)
						(use_tab_char tab_operation)
					)
				;else
					(
						(while (&& (search_fwd "\t" 1)(! (inq_kbd_char)))
							(
								(delete_char)
								(insert (_replicate " " (distance_to_tab)))
								(inq_position curr_line NULL)
								(message "Inserting spaces - line: %d, any key to abort..." curr_line)
							)
						)
					)
				)
				(raise_anchor)
				(restore_position)
				(_display_popup_message "Tab operation complete." "" 0)
			)
		)
	)
)
