<glimmer-definition>
   <settings>
      <language>Z80 Assembly Language</language>
      <compiler>glimmerout tasm %2 2&gt;&amp;1</compiler>
      <debugger></debugger>
      <execution></execution>
      <auto-indent>1</auto-indent>
      <use-spaces>0</use-spaces>
      <tab-stop>4</tab-stop>
   </settings>

   <mime-types>
      <extension>z80</extension>
      <extension>asm</extension>
   </mime-types>

   <syntax-items>
      <syntax-item>
         <name>string</name>
         <start-regex>&quot;</start-regex>
         <end-regex>&quot;</end-regex>
         <style>string</style>
      </syntax-item>
      <syntax-item>
         <name>comment</name>
         <start-regex>;</start-regex>
         <end-regex>\n</end-regex>
         <style>comment</style>
      </syntax-item>
   </syntax-items>

   <pattern-items>
      <pattern-item>
         <name>defs</name>
         <regex>^#\ *\t*\(include\|if\|ifdef\|ifndef\|else\|elif\|define\|endif\|pragma\)\b</regex>
         <style>preprocessor</style>
      </pattern-item>
      <pattern-item>
         <name>directives</name>
         <regex>\(\.equ\|equ\|\|^\.org\|^\.end\)\b</regex>
         <style>preprocessor</style>
      </pattern-item>
      <pattern-item>
         <name>instructions</name>
         <regex>\b\(add\|and\|bit\|call\|cp\|cpd\|ccf\|cpdr\|cpir\|cpi\|cpl\|daa\|dec\|djnz\|ex\|exx\|halt\|di\|ei\|im\|in\|out\|inc\|jp\|jr\|ld\|neg\|nop\|or\|pop\|push\|res\|ret\|rl\|rla\|rlc\|rlca\|rld\|rr\|rra\|rrc\|rrca\|rrd\|rst\|set\|sla\|sra\|srl\|sub\|sbc\|xor\)\b</regex>
         <style>keyword</style>
      </pattern-item>
      <pattern-item>
         <name>INSTRUCTIONS</name>
         <regex>\B\(ADD\|AND\|BIT\|CALL\|CP\|CPD\|CCF\|CPDR\|CPIR\|CPI\|CPL\|DAA\|DEC\|DJNZ\|EX\|EXX\|HALT\|DI\|EI\|IM\|IN\|OUT\|INC\|JP\|JR\|LD\|NEG\|NOP\|OR\|POP\|PUSH\|RED\|RET\|RL\|RLA\|RLC\|RLCA\|RLD\|RR\|RRA\|RRC\|RRCA\|RRD\|RST\|SET\|SLA\|SRA\|SRL\|SUB\|SBC\|XOR\)\b</regex>
         <style>keyword</style>
      </pattern-item>
      <pattern-item>
         <name>flags</name>
         <regex>\b\(z\|nz\|nc\|p\|pe\|po\|m\)\b</regex>
         <style>keyword</style>
      </pattern-item>
      <pattern-item>
         <name>registers</name>
         <regex>\b\(a\|A\|f\|F\|af\|AF\|b\|B\|c\|C\|bc\|BC\|d\|D\|e\|E\|de\|DE\|h\|H\|l\|L\|hl\|HL\|ix\|IX\|iy\|IY\|r\|R\)\b</regex>
         <style>variable</style>
      </pattern-item>
      <pattern-item>
         <name>labels</name>
         <regex>^[a-zA-Z_][a-zA-Z0-9_]*\:</regex>
         <style>function</style>
      </pattern-item>
      <pattern-item>
         <name>operators</name>
         <regex>\(+\|-\|\*\|/\|=\|(\|)\)</regex>
         <style>operator</style>
      </pattern-item>
      <pattern-item>
         <name>hex1</name>
         <regex>\$[a-fA-F0-9]+</regex>
         <style>hexidecimal</style>
      </pattern-item>
      <pattern-item>
         <name>hex2</name>
         <regex>[a-fA-F0-9][a-fA-F0-9]+\(h\|H\)</regex>
         <style>hexidecimal</style>
      </pattern-item>
      <pattern-item>
         <name>numbers</name>
         <regex>\b[0-9]+\b</regex>
         <style>decimal</style>
      </pattern-item>
      <pattern-item>
         <name>binary</name>
         <regex>%[0-1]+\b</regex>
         <style>octal</style>
      </pattern-item>
      <pattern-item>
         <name>dbs</name>
         <regex>\(\.db\|\.dw\)</regex>
         <style>keyword</style>
      </pattern-item>
   </pattern-items>

</glimmer-definition>

<python-definition>
import glimmer
import string

def z80_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", z80_enter_callback)

def z80_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", z80_tab_callback)

def insert_z80_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()

glimmer.add_sub_to_menu("_Insert/", "Z80 Assembly", -1)
glimmer.add_item_to_menu("_Insert/Z80 Assembly/", "Comment (;)", "", "", i, insert_z80_comment)
glimmer.add_item_to_popup_menu("Comment (;)", -1, insert_z80_comment)
</python-definition>
