<settings>
language=Java
compiler=glimmerout javac %1/%2 2>&1
debugger=
execution=%term% %1/%3
auto_indent=1
use_spaces=0
tab_stop=4
</settings>

<extensions> 
 <item name="java source file" ext="java">
</extensions> 

<syntax>
 <item name="string" start="\"" end="\(\"\|\n\)" color="string">
 <item name="chars" start="'" end="\(\'\|\n\)" color="char_string">
 <item name="cplus_comment" start="//" end="\n" color="comment">
 <item name="jcomment" start="/\*" end="\*/" color="comment">
 <item name="doc_comment" start="/\*\*" end="\*/" color="comment">
</syntax>

<pattern>
 <item name="numeric_constant" words="<(?i0[X][\\dA-F]+|\\d+(\\.\\d*)?(?:E[+-]?[\\d]+)?F?|\\.\\d+(?:E[+-]?[\\d]+)?F?|[\\d]+[LF]?))" color="character">
 <item name="string_escape" words="(?:\\\\u[\\dA-Faf]{4}|\\\\[0-7]{1,3}|\\\\[btnfr'\"\"\\\\])" color="character">
 <item name="declarations" words="\b\(class\|extends\|implements\|import\|instanceof\|interface\|native\|package\|volatile\|throws\)\b" color="keyword">
 <item name="primitive" words="\b\(boolean\|byte\|char\|double\|float\|int\|long\|short\)\b" color="keyword">
 <item name="flow" words="\b\(break\|case\|catch\|continue\|default\|do\|else\|for\|if\|return\|for\|goto\|if\|return\|switch\|switch\|synchronized\|throw\|try\|while\)\b" color="keyword">
 <item name="modifiers" words="\b\(abstract\|final\|finally\|private\|protected\|public\|static\)\b" color="keyword">
 <item name="memory" words="\b\(new\|null\|super\this\|void\)\b" color="keyword">
 <item name="unused" words="\b\(const\|gotol\|transient\)\b" color="keyword">
 <item name="operators" words="\(\*\|-)\|::\|<<\|<\|=\|==\|!=\|<=\|)=\|++\|--\|%\|+\|-\|||\|&&\|!\|+=\|-=\|\*=\|/=\|%=\)" color="operator">
</pattern>

<python>
import glimmer
import string

def java_enter_callback():
    insert = "\n"
    extralen = 0
    slen = 0
    file = glimmer.get_file_number()
    tstart = glimmer.line_start()
    tend = glimmer.current_position()
    if (tend-tstart) > 0:
        chars = glimmer.get_text(tstart, tend)
        slen = len(chars)
        if slen > 0:
            index = 0
            while index < slen:
                c = chars[index]
                if c == '\t' or c == ' ':
                    insert = insert + chars[index]
                    index = index+1
                else:
                    break
            c = chars[slen-1]
            if c == '{':
                extralen = glimmer.get_file_tab_width(file)
                if glimmer.get_file_use_spaces(file):
                    while extralen > 0:
                        insert = insert + " "
                        extralen = extralen-1
                else:
                    insert = insert + '\t'
    glimmer.insert(insert)

glimmer.register_signal_hook("enter-pressed", java_enter_callback)

def java_tab_callback():
   tablen = 0
   insertchars = ""
   file = glimmer.get_file_number()
   if glimmer.get_file_use_spaces(file):
      tablen = glimmer.get_file_tab_width(file)
      pos = glimmer.current_position()
      lstart = glimmer.line_start()
      if pos - lstart > 0:
         str1 = glimmer.get_text(lstart, pos)
      else:
         str1 = ""
      if glimmer.get_tab_stops():
         str1 = string.expandtabs(str1, tablen)
         len1 = len(str1)
         del str1
         tablen = tablen - (len1 % tablen)
      while tablen > 0:
         insertchars = insertchars + " "
         tablen = tablen-1
   else:
      insertchars = insertchars + "\t"
   glimmer.insert(insertchars)

glimmer.register_signal_hook("tab-pressed", java_tab_callback)

def insert_cs_comment():
    glimmer.freeze()
    if glimmer.has_selection():
        start = glimmer.selection_start()
        end = glimmer.selection_end()
        file = glimmer.get_file_number()
        glimmer.move(0)
        glimmer.insert_in(file, start, "/* ")
        glimmer.insert_in(file, end+3, " */")
        glimmer.move_to(end+6)
    else:
        glimmer.insert("/*\n *\n*/")
        glimmer.move(-3)
    glimmer.thaw()

def insert_cpps_comment():
    glimmer.freeze()
    if glimmer.has_selection():
        start = glimmer.selection_start()
        end = glimmer.selection_end()
        file = glimmer.get_file_number()
        glimmer.move(0)
        line1 = glimmer.line_by_pos(start)
        line2 = glimmer.line_by_pos(end)
        cur = line1
        print start
        print end
        while cur <= line2:
           glimmer.move_to_col(0)
           glimmer.move_to_line(cur)
           glimmer.insert("//")
           cur = cur+1
        glimmer.move_to(end+((line2-line1)+1)*2)
    else:
        glimmer.insert("//")
    glimmer.thaw()

def insert_doc_comment():
    glimmer.freeze()
    if glimmer.has_selection():
        start = glimmer.selection_start()
        end = glimmer.selection_end()
        file = glimmer.get_file_number()
        glimmer.move(0)
        glimmer.insert_in(file, start, "/** ")
        glimmer.insert_in(file, end+4, " */")
        glimmer.move_to(end+7)
    else:
        glimmer.insert("/**\n *\n*/")
        glimmer.move(-3)
    glimmer.thaw()

i = glimmer.add_sub_to_insert_menu("Java", -1);
glimmer.add_item_to_insert_menu("Comment (C)", i, insert_cs_comment)
glimmer.add_item_to_insert_menu("Comment (C++)", i, insert_cpps_comment)
glimmer.add_item_to_insert_menu("Comment (Doc)", i, insert_doc_comment)
glimmer.add_item_to_popup_menu("Comment (C)", -1, insert_cs_comment)
glimmer.add_item_to_popup_menu("Comment (C++)", -1, insert_cpps_comment)
glimmer.add_item_to_popup_menu("Comment (Doc)", -1, insert_doc_comment)
</python>
