<settings>
language=Z80 Assembly
compiler=glimmerout tasm %2 2>&1
debugger=
execution=
auto_indent=1
use_spaces=0
tab_stop=4
</settings>

<extensions>
 <item name="z80 assembly" ext="z80">
 <item name="source file" ext="asm">
</extensions>

<syntax>
 <item name="string" start="\"" end="\"" color="string">
 <item name="comment" start=";" end="\n" color="comment">
</syntax>

<pattern>
 <item name="defs" words="^#\ *\t*\(include\|if\|ifdef\|ifndef\|else\|elif\|define\|endif\|pragma\)\b" color="preprocessor">
 <item name="directives" words="\(\.equ\|equ\|\|^\.org\|^\.end\)\b" color="preprocessor">
 <item name="instructions" words="\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" color="keyword">
 <item name="INSTRUCTIONS" words="\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" color="keyword">
 <item name="flags" words="\b\(z\|nz\|nc\|p\|pe\|po\|m\)\b" color="keyword">
 <item name="registers" words="\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" color="variable">
 <item name="labels" words="^[a-zA-Z_][a-zA-Z0-9_]*\:" color="function">
 <item name="operators" words="\(+\|-\|\*\|/\|=\|(\|)\)" color="operator">
 <item name="hex1" words="\$[a-fA-F0-9]+" color="hexidecimal">
 <item name="hex2" words="[a-fA-F0-9][a-fA-F0-9]+\(h\|H\)" color="hexidecimal">
 <item name="numbers" words="\b[0-9]+\b" color="decimal">
 <item name="binary" words="%[0-1]+\b" color="octal">
 <item name="dbs" words="\(\.db\|\.dw\)" color="keyword">
</pattern>

<python>
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()

i = glimmer.add_sub_to_insert_menu("Z80 Assembly", -1)
glimmer.add_item_to_insert_menu("Comment (;)", i, insert_z80_comment)
glimmer.add_item_to_popup_menu("Comment (;)", -1, insert_z80_comment)
</python>
