import string

def go_eol ():
    move_to (eol (current_line ()))

def count_braces ():
    i = current_line ()
    s = ""
    for j in xrange (i, 0, -1):
	s = get_line (j)
	if string.strip (s):		# find non-blank line
	    break
    j = 0
    for c in s:
	if c == "{":			# count curly braces
	    j = j + 1
    return j

def c_generic(s, d):
    i = count_braces ()
    go_eol ()
    insert ("\n")
    indent (i)
    k = current ()
    insert (s + "\n")
    indent (0)
    insert ("}")
    move_to (k + d)
    redraw_page ()

def c_case():
    i = current_line ()
    s = ""
    for j in xrange (i, 0, -1):
	s = get_line (j)
	if s:
	    if string.find (s, "switch") >= 0 or s[0] == "{":		# find switch statement
		break
    s = s[:len (s) - len (string.lstrip (s))]
    go_eol ()
    insert ("\n")
    insert (s);
    k = current ()
    insert ("case :\n")
    indent (1)
    insert ("break;")
    move_to (k + 5)
    redraw_page ()

def c_do_while():
    i = count_braces ()
    go_eol ()
    insert ("\n")
    indent (i)
    insert ("do {\n")
    indent (0)
    insert ("} while ()")
    move (-1)
    redraw_page ()

def c_main():
    go_eol ()
    insert ('\nint main (int argc, char **argv)\n{\n\n}\n')
    move (-3)
    redraw_page ()

def c_include():
    go_eol ()
    insert ('\n#include <>')
    move (-1)
    redraw_page ()

def c_printf():
    i = count_braces ()
    go_eol ()
    insert ("\n")
    indent (i)
    k = current ()
    insert ('printf ("");\n')
    move_to (k + 9)
    redraw_page ()

def type_change(s):
    menu ("Util")	# clear the Util menu
    if s == "C/C++ Program":
	menu ("Util", "for(;;) {", "c_generic('for (;;) {', 5)")
	menu ("Util", "while() {", "c_generic('while () {', 7)")
	menu ("Util", "do {", "c_do_while()")
	menu ("Util", "switch() {", "c_generic('switch () {', 8)")
	menu ("Util", "case:", "c_case()")
	menu ("Util", "if() {", "c_generic('if () {', 4)")
	menu ("Util", "main() {", "c_main()")
	menu ("Util", "#include ", "c_include()")
	menu ("Util", "printf();", "c_printf()")


