Sublime Forum

Dynamically adding syntax

#1

Probably a crazy question, but is it possible to add syntax rules based on the content of the file being viewed?

For instance, I have a homebrew language where you can define macros:

Macro:(Sausages,…)
EndM

Sausages:(…

I’d love to be able to get Sublime to recognise and highlight ‘Sausages:(’ as the use of a macro. Is this remotely feasible?

Thanks :smile:

0 Likes

#2

You need also a continuous check, if an macro, that is declared in the current view, is now in use.
I’ve no idea, how to make this continuous. But the following workaround give you the ability to show the macros, that in use, with an hotkey.

[code]import sublime
import sublime_plugin
import re

class MacroShowCommand(sublime_plugin.TextCommand):
def run(self, edit):
text = self.view.substr(sublime.Region(0,self.view.size()))
match = re.findall(“Macro:((\w+)”, text)
if not match:return
selections = self.view.sel()
selections.clear()
for i in range(0, len(match)):
regions = self.view.find_all("%s:(" % match*)
selections.add_all(regions)[/code]
Bind the command “macro_show” to an hotkey.*

0 Likes