Sublime Forum

Syntax for conditional preprocessing directive

#1

Explanation

In AutoHotkey you can define key sequences which act as hotkeys. There is an existing .tmLanguage file which enables highlighting of (among other things) those key sequences:

!a:: Send,Text to send when Alt+A is pressed
!b:: Send,Text to send when Alt+B is pressed

I’m using the converted .sublime-syntax file instead of the original tmLanguage file. Here’s the relevant portion:

- match: ^\s*(.+)(::)(.*)$
  captures:
    1: entity.name.function.label.hotkey.ahk
    2: punctuation.definition.equals.colon
    3: hotkeytext.ahk

AutoHotkey allows preprocessor directives such as #IfWinExists to enable hotkey combinations only when a given window exists. To do so you surround hotkey definitions with (for example):

#IfWinExists,My Application Window
!a:: Send,Text to send when Alt+A is pressed
!b:: Send,Text to send when Alt+B is pressed
#IfWinExists

Question:
I want Sublime to continue to do the bolding of the key sequences (and all the other syntax formatting goodies it does) and also to change the background of all the text between #IfWinExists markers so I know those keys are only conditionally active. For example, a line not enclosed by #IfWindExists directives might appear with a white background and the key sequence bolded, but the two lines above might appear with a light blue background and the key sequences bolded. Everything would be the same except a different color background.

I defined a syntax rule in the “main” context to recognize the #IfWinExists lines. When I implement it as a meta-scope, the background color changes as expected, but that scope overrides all other scopes so all the code becomes scope ‘source’ and therefore nothing is bolded:

- match: ^(?i:#ifwin[a-z]+)(\s*,?\s*)(.+)$
  push:
    - meta_scope: ifwin_conditional.ahk
    - match: ^(?i:#ifwin[a-z]+)$
      pop: true

How can I keep all the current scope definitions intact but create a separate-but-equal scope to allow overriding the background of all subsequent scopes without affecting the foreground or fontStyle (unless of course an individual scope is explicitly defined to override the background)?

Thanks!

0 Likes