1

I would like use sublime for my Mercurial commit messages, so I set

HGEDITOR="C:\Program Files\Sublime Text 3\subl.exe" -w -n a

This will start Sublime with a file named something like hg-editor-mstdne.commit.hg.txt.

Then I would like to be able to have it automatically wrap at 72 characters (like Sublime Text line wrap and commit messages). This can be done per syntax, so I have defined a syntax for it

%YAML 1.2
---
name: hg-commit
file_extensions: [.commit.hg.txt]
scope: text.hg


contexts:
  main:
    - match: "^HG:.*"
      captures:
        0: comment.line.hg-commit

I can manually select the syntax, but the commit still opens as Plain Text. If I select View->Syntax->'Open all with current extension as' then all other .txt files are also opened with hg-commit syntax.

Are there any ways to get it to use my syntax for hg commit message files only?

mrtnlrsn
  • 438
  • 4
  • 15
  • Is the full filename always the same? If it is, setting the file extension to that would set it, I think. If it's sometimes different then I think you'd need a simple plugin with an `on_load` event listener. – OdatNurd Oct 16 '19 at 13:59
  • @OdatNurd No the `-mstdne` part is random. I will check out `on_load`. – mrtnlrsn Oct 17 '19 at 07:27

1 Answers1

0

It was pretty simple with the on_load event as @OdatNurd suggested.

import sublime_plugin
# Load special syntax for hg commit. Also sets line wrapping
class RtxOnload(sublime_plugin.EventListener):
    def on_load(self, view):
        if view.file_name().endswith('.commit.hg.txt'):
            view.settings().set('syntax', 'Packages/MLA/hg-commit.sublime-syntax')
            view.settings().set('wrap_width', 72)

Only problem is that it only works when Sublime is already running. That could probably be fixed with some other hook, but for me it is not an issue.

mrtnlrsn
  • 438
  • 4
  • 15