#!/usr/bin/env python

# This program does its best to create a kate syntax file for the kate
# editor. I'm using a program to do this (rather than just editing the xml file)
# due to the long keyword lists ... and since mma can generate them ... let it
# do the hard work.

# And if you can improve this ... don't be shy!!!

# bvdp  May/2006
#   converted to run in py2 or py3 June/2020

import sys, os, subprocess

# First create a item list of keywords from mma
#l = commands.getoutput("mma -Dk")
l = subprocess.check_output(["mma", "-Dk"],universal_newlines=True )

for a in l.split('\n'):
    if a.startswith("Base"):
        classnames = a.split(":")[1:][0].split()
    if a.startswith("Commands"):
        sfuncs = a.split(":")[1:][0].split()
    if a.startswith("Track"):
        tfuncs = a.split(":")[1:][0].split()

# Combine the track and simple functions.

for a in tfuncs:
    if sfuncs.count(a) == 0:
        sfuncs.append(a)
sfuncs.sort()

# Got our info. Create xml ... this is printed to stdout.

print( """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE language SYSTEM "language.dtd">
<language name="Musical Midi Accompaniment" section="Other" version="1.00" kateversion="2.3" extensions="*.mma" author="Bob van der Poel" license="LGPL" >

<!--
    Autogenerated with katexml.py. If you modify this file you'll
    probably lose the changes. Hack the source instead!

    Highlighting file for kate editor and MMA - Musical Midi Accompaniment
    source files.  <bob@mellowood.ca>
-->

""")

print("""
<highlighting>
  <list name = "keys"> """)

for a in sfuncs:
    print('     <item> %s </item>' % a.title() )

print("   </list> ")

print('   <list name = "classes"> ')

for a in classnames:
    print('     <item> %s </item>' % a.title() )
print("   </list> ")

print('   <list name = "block"> ')

for a in ('Begin', 'End'):
    print('     <item> %s </item>' % a )
print("   </list>")

print(" <contexts>")
print('   <context attribute="Normal Text" lineEndContext="#pop" name="Normal Text">')

# All the keywords get the same hilight.

print('     <keyword attribute="Keyword" context="#stay" String="keys" />')

# This creates a regexpr for each class so we can hilight words
# like Bass-Sus ... is there an easier way by modifying the
# basic Class attribute?????

for a in classnames:
    print('     <RegExpr attribute="Class" context="#stay" insensitive="True" String="%s\\-[A-Z0-9]*" />' % a)
print('     <keyword attribute="Class" content="#stay" String="classes" />')

# The Begin/End block get a separate hilight

print('     <keyword attribute="Block" content="#stay" String="block" />')

# comments

print('     <Detect2Chars attribute="Comment" context="Comment" char="/" char1="/"/>')

# Macros - This does both $_xx and $xx. Note $__xx will not hilight (this illegal in MMA)

print('     <RegExpr attribute="Macro" context="#stay" String="\\$_?[a-zA-Z0-1]*\\b" />')

# linenumber, only if they start a line.

print('     <RegExpr attribute="Linenumber" context="#stay" String="^[0-9]*\\b" />')
print('   </context>')
print( '   <context attribute="Comment" lineEndContext="#pop" name="Comment"/>')
print( ' </contexts>')

# Set the hilight colors, etc.

print(' <itemDatas>')
print('   <itemData name="Normal Text" defStyleNum="dsNormal"/>')
print('   <itemData name="Keyword" defStyleNum="dsKeyword" color="Blue"  />')
print('   <itemData name="Class" defStyleNum="dsKeyword" color="Brown" />')
print('   <itemData name="Block" defStyleNum="dsKeyword" color="Red" />')
print('   <itemData name="Comment" defStyleNum="dsComment" />')
print('   <itemData name="Linenumber" defStyleNum="dsFloat" />')
print('   <itemData name="Macro" defStyleNum="dsKeyword" />')
print(' </itemDatas>')

print(' </highlighting>')

# finish off with the general stuff.

print( ' <general>')
print('   <comments>')
print('     <comment name="singleLine" start="//"/>')
print('   </comments>')
print('   <keywords casesensitive="0" />')
print(' </general>')
print( '</language>')



