<settings>
language=C++
compiler=glimmerout g++ -g %2 -o %3 2>&1
debugger=%term% gdb %3
execution=%term% %1/%3
auto_indent=1
use_spaces=0
tab_stop=4
</settings>

<extensions>
 <item name="include file" ext="H">
 <item name="c++ source file" ext="C">
 <item name="include file" ext="hpp">
 <item name="c++ source file" ext="cpp">
 <item name="c++ source file" ext="cxx">
 <item name="c++ source file" ext="cc">
 <item name="c++ source file" ext="CC">
</extensions>

<syntax>
 <item name="defs_full" start="^#\ *\t*\(include\|pragma\)" end="\(\n\)" color="preprocessor">
 <item name="cpp-comment" start="//" end="\n" color="comment">
 <item name="c-comment" start="/\*" end="\*/" color="comment">
 <item name="string" start="\"" end="\(\"\|\n\)" color="string">
 <item name="define_comment" start="^#if 0" end="^#endif" color="comment">
</syntax>

<pattern>
 <item name="chars" words="'\\?[a-zA-Z0-9_\\()#@!$%^&*-=+\"{}<)]'", color="char_string">
 <item name="defs" words="^#\ *\t*\(if\|ifdef\|ifndef\|else\|elif\|define\|endif\|undef\)\b" color="preprocessor">
 <item name="typedefs" words="\b\(bool\|char\|float\|double\|short\|int\|long\|signed\|unsigned\|register\|void\)\b" color="data_type">
 <item name="keywords" words="\b\(asm\|auto\|break\|case\|const\|continue\|default\|do\|else\|enum\|extern\|for\|goto\|if\|return\|sizeof\|static\|struct\|switch\|typedef\|union\|volotile\|while\)\b" color="keyword">
 <item name="cpp" words="\b\(and\|and_eq\|bitend\|bitor\|catch\|class\|compl\|const_cast\|delete\|dynamic_cast\|explicit\|export\|false\|friend\|inline\|mutable\|namespace\|new\|not\|not_eq\|operator\|or\|or_eq\|private\|protected\|public\|reinterpret_cast\|static_cast\|template\|this\|throw\|true\|try\|typeid\|typename\|using\|virtual\|volatile\|wchar_t\|xor\|xor_eq\)\b" color="keyword">
 <item name="glib" words="\b\(gint\|gfloat\|gdouble\|gboolean\|gchar\|gpointer\|guint\|guchar\|GList\|GSList\|GHash\|GString\|GModule\|GTable\|GTree\)\b" color="data_type">
 <item name="gnutypes" words="\b\(Gtk\|Gds\|Gdk\|Gnome\)[a-zA-Z0-9_]+" color="data_type">
 <item name="macros" words="\b[A-Z_][A-Z0-9_\-]+\b" color="macro">
 <item name="gnufuncs" words="\b\(gtk\|gdk\|g\|gnome\|gds\)_[a-zA-Z0-9_]+" color="function">
 <item name="labels" words="^[a-zA-Z_]*:" color="function">
 <item name="whole_numbers1" words="\b[0-9]\b" color="decimal">
 <item name="whole_numbers2" words="\b[1-9][0-9]*\b" color="decimal">
 <item name="float_numbers1" words="\.\b[0-9]+\b" color="decimal">
 <item name="float_numbers2" words="\b[0-9]*\.\W" color="decimal">
 <item name="hex_numbers" words="\b0x[0-9a-fA-F]+\b" color="hexidecimal">
 <item name="oct_numbers" words="\b0[0-7]+\b" color="octal">
 <item name="bin_numbers" words="\b0[01]+\b" color="octal">
 <item name="operators" words="\(\*\|\*\*\|->\|::\|<<\|>>\|>\|<\|=\|==\|!=\|<=\|>=\|++\|--\|%\|+\|-\|||\|&&\|!\|+=\|-=\|\*=\|/=\|%=\|~\)" color="operator">
 <item name="punctuation" words="\((\|)\|;\|:\|{\|}\|,\|\[\|\]\|\.\)" color="punctuation">
</pattern>

<python>
import glimmer
import string

def cpp_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", cpp_enter_callback)

def cpp_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", cpp_tab_callback)

def insert_c_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_cpp_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 open_cpp_header():
    import string
    file = 0
    fname = ""
    file = glimmer.get_file_number()
    fname = glimmer.get_filename(file)
    path = ""
    noext = ""
    fullfile = ""
    openfile = 0
    if string.count(fname, ".C"):
        openfile = 1
        path = glimmer.get_path(file)
        noext = glimmer.get_file(file)
        fullfile = path + "/" + noext + ".H"
    elif string.count(fname, ".h"):
        openfile = 1
        path = glimmer.get_path(file)
        noext = glimmer.get_file(file)
        fullfile = path + "/" + noext + ".cxx"
    elif string.count(fname, ".H"):
        openfile = 1
        path = glimmer.get_path(file)
        noext = glimmer.get_file(file)
        fullfile = path + "/" + noext + ".C"
    elif string.count(fname, ".cxx"):
        openfile = 1
        path = glimmer.get_path(file)
        noext = glimmer.get_file(file)
        fullfile = path + "/" + noext + ".h"
    if openfile:
        openedfile = open(fullfile)
        if openedfile:
            del openedfile
            glimmer.open_file(fullfile)

i = glimmer.add_sub_to_insert_menu("C++", -1)
glimmer.add_item_to_insert_menu("Comment (C)", i, insert_c_comment)
glimmer.add_item_to_insert_menu("Comment (C++)", i, insert_cpp_comment)
glimmer.add_item_to_popup_menu("Open Reverse", -1, open_cpp_header)
glimmer.add_item_to_popup_menu("Comment (C)", -1, insert_c_comment)
glimmer.add_item_to_popup_menu("Comment (C++)", -1, insert_cpp_comment)
</python>
