;**
;**		indenth.m:
;**	Templates for Fortran and Modula-2.  To use them, just use Setup
;**	to turn template editing on for Fortran and/or Modula-2, and
;**	start BRIEF with the -mindenth flag.
;**		Sample BPACKAGES=for,mod:t;c:s;m,h,asm:r
;**
;**	Note: these have been converted to BRIEF v2.0, but they do not
;**	support any more constructs than they did under BRIEF v1.3.
;**

#define	SEMI_COLON			1			;** Code for semicolon.

#define	MIN_ABBREV			1			;** Increase this to specify longer
												;** minimum abbreviations.
(extern		amt
				open_line
)

(macro _init
	(
		(int		
					_for_template
               _mod_template
		)
		(global	
					_for_template
               _mod_template
		)

		(keyboard_push)
		(assign_to_key "#7181" ".for_indent")
		(assign_to_key "#3849" "slide_in")
		(assign_to_key "#3840" "slide_out")
		(assign_to_key "#14624" ".for_abbrev")
		(= _for_template (inq_keyboard))
		(keyboard_pop 1)

		(keyboard_push)
		(assign_to_key "#7181" ".mod_indent")
		(assign_to_key "#3849" "slide_in")
		(assign_to_key "#3840" "slide_out")
		(assign_to_key "#14624" ".mod_abbrev")
		(= _mod_template (inq_keyboard))
		(keyboard_pop 1)
	)
)

;**
;**		.for_template_first, .mod_template_first:
;**
;**		These macros are called by the BPACKAGES parser in language.m.
;**	They initialize the local keymaps for the various indenting functions.
;**

(macro .for_template_first
	(
		(use_local_keyboard _for_template)
		(returns "")
	)
)

(macro .mod_template_first
	(
		(use_local_keyboard _mod_template)
		(returns "")
	)
)

;**
;**		Template editing macros.
;**

(macro .for_abbrev
	(
		(if (|| (== (read 1) "\n") (== (read 1) " "))
			(
				(string		line)

				(int			loc startloc)

            (inq_position NULL startloc)
				(save_position)
				(beginning_of_line)
				(= line (read))
				(restore_position)

				(if (= loc (search_string "[~ \t\n]" line))
					(
						(int		length)

						(= line (+ "á" (substr line loc (+ (- startloc loc) 1))))
						(= line (substr line 1 (- (strlen line) 1)))

						(if (&& (< MIN_ABBREV (strlen line))
								(= loc (search_string line "ádo áif () thenáelse áelseif () thená" length 0)))
							(
								(string		completion)

								(= completion (substr "ádo áif () thenáelse áelseif () thená" (+ loc length)))
								(= completion (substr completion 1 (- (index completion "á") 1)))
								(insert completion)

								(if (== (substr line 2 1) "d")
									(
										(save_position)
                              (.for_indent)
										(insert "continue")
                              (.for_indent)
										(restore_position)
									)
								;else
									(if (== (substr line 2 1) "i")
										(
											(save_position)
                                 (.for_indent)
											(insert "endif")
                                 (.for_indent)
   	      							(restore_position)
											(move_rel 0 -6)
										)
									;else
                              (if (== (substr line 2 1) "e")
                                 (
                                    (if (== (substr line 2 5) "elsei")
                                       (
                                          (save_position)
                                          (.for_indent)
                                          (restore_position)
                                          (move_rel 0 -6)
                                       )
                                    )
                                 )
                              ;else
   										(if (!= loc 1)
	   										(search_back ")")
		   								)
                              )
									)
								)
							)
						;else
							(self_insert)
						)
					)
				;else
					(self_insert)
				)
			)
		;else
			(self_insert)
		)
	)
)


(macro .for_indent
	(
		(int		curr_indent_col		;** Current unmodified indent col.
					following_position	;** Column of end of line following cursor.
					curr_col					;** Column cursor is on when called.
					curr_line				;** Column line is on when called.
					what_is_char_1			;** End of first line's character identifier.
					what_is_char_2			;** End of second line's indentifier.
		)
		(string	ch
               following_string)		;** All characters following the cursor.

;**
;**		Gather information on the two previous non-blank lines.
;**

		(inq_position curr_line curr_col)
		(end_of_line)
		(inq_position NULL following_position)
		(= following_position (- curr_col (-- following_position)))

		;**		If there are characters following the cursor, save them in
		;**	following_string.

		(if (<= following_position 0)
			(
				(int		non_blank_pos)

				(move_rel 0 -1)
				(drop_anchor)
				(move_rel 0 following_position)
				(= following_string (read))

				(if (= non_blank_pos (search_string "[~ \t]" following_string))
					(= following_string (substr following_string non_blank_pos))
				)
				(delete_block)
			)
		)
		(if (search_back "<*\\c[~ \t\n]")
			(
				(inq_position NULL curr_indent_col)
			)
		;else
			(
				(= curr_indent_col 1)
			)
		)
		(move_abs curr_line curr_col)

;**
;**		We've determined the last two non-blank lines' last characters
;**	as well as the column position of the first non-blank character.
;**	Now we position the cursor on the new line's proper level.
;**

		(if (< curr_indent_col 1)
			(= curr_indent_col 1)
		)
		(insert "\n")
		(move_abs 0 curr_indent_col)

		(if (<= following_position 0)
			(
				(= following_string (substr following_string 1 (- (strlen following_string) 1)))
				(insert following_string)
				(move_abs 0 curr_indent_col)
			)
		)
	)
)



(macro .mod_abbrev
   (
		(if (|| (== (read 1) "\n") (== (read 1) " "))
			(
				(string		line)

				(int			loc 
                        temp
                        startloc)

            (inq_position NULL startloc)
				(save_position)
				(beginning_of_line)
				(= line (read))
				(restore_position)

				(if (= loc (search_string "[~ \t\n]" line))
					(
						(int		length)

                  (= line (+ "á" (substr line loc (+ (- startloc loc) 1))))
						(= line (substr line 1 (- (strlen line) 1)))

                  (if (&& (< MIN_ABBREV (strlen line))
								(= loc (search_string line "áCASE () OFáLOOPáWHILE () DOáREPEAT áIF () THENáELSEáELSIF () THENá" length 0)))
							(
								(string		completion)

                        (= completion (substr "áCASE () OFáLOOPáWHILE () DOáREPEAT áIF () THENáELSEáELSIF () THENá" (+ loc length)))
								(= completion (substr completion 1 (- (index completion "á") 1)))
								(insert completion)

								(if (== (substr line 2 1) "R")
									(
										(save_position)
										(open_line)
                              (move_rel 0 (- 0 amt))
                              (insert "UNTIL ()\;")
 										(search_back ")")
									)
								;else
									(if (index "IWLC" (substr line 2 1))
                              (
											(save_position)
											(open_line)
                                 (move_rel 0 (- 0 amt))
                                 (insert "END\;")
											(restore_position)
    										(if (!= (substr line 2 1) "L")
                                    (search_back ")")
                                 )
                              )
									;else
                              (if (== (substr line 2 1) "E")
                                 (
                                    (if (== (substr line 2 5) "ELSI")
   	   										(search_back ")")
                                    )
                                 )
                              ;else
   										(if (!= loc 1)
	   										(search_back ")")
		   								)
                              )
									)
								)
							)
						;else
							(self_insert)
						)
					)
				;else
					(self_insert)
				)
			)
		;else
			(self_insert)
		)
   )
)


(macro .mod_indent
   (
		(int		curr_indent_col		;** Current unmodified indent col.
					following_position	;** Column of end of line following cursor.
					curr_col					;** Column cursor is on when called.
					curr_line				;** Column line is on when called.
					what_is_char_1			;** End of first line's character identifier.
					what_is_char_2			;** End of second line's indentifier.
		)
		(string	ch
               following_string)		;** All characters following the cursor.

;**
;**		Gather information on the two previous non-blank lines.
;**

		(inq_position curr_line curr_col)
		(end_of_line)
		(inq_position NULL following_position)
		(= following_position (- curr_col (-- following_position)))

		;**		If there are characters following the cursor, save them in
		;**	following_string.

		(if (<= following_position 0)
			(
				(int		non_blank_pos)

				(move_rel 0 -1)
				(drop_anchor)
				(move_rel 0 following_position)
				(= following_string (read))

				(if (= non_blank_pos (search_string "[~ \t]" following_string))
					(= following_string (substr following_string non_blank_pos))
				)
				(delete_block)
			)
		)
		(if (search_back "<*\\c[~ \t\n]")
			(
				(inq_position NULL curr_indent_col)
				(end_of_line)
				(search_back "[~ \t\n]")
				(= what_is_char_2 (|| (== "\;" (= ch (read 1))) (== ch "|")))
				(next_char)
				(delete_to_eol)
			)
		;else
			(
				(= what_is_char_2 SEMI_COLON)
				(= curr_indent_col 1)
			)
		)
		(move_abs curr_line curr_col)

;**
;**		We've determined the last two non-blank lines' last characters
;**	as well as the column position of the first non-blank character.
;**	Now we position the cursor on the new line's proper level.
;**

		(if (!= curr_indent_col 1)
			(
				(if (!= what_is_char_2 SEMI_COLON)
      			(+= curr_indent_col amt)
				)
			)
		)
		(if (< curr_indent_col 1)
			(= curr_indent_col 1)
		)
		(insert "\n")
		(move_abs 0 curr_indent_col)

		(if (<= following_position 0)
			(
				(= following_string (substr following_string 1 (- (strlen following_string) 1)))
				(insert following_string)
				(move_abs 0 curr_indent_col)
			)
		)
   )
)

