Home Download Buy Blog Forum Support

Set buffer syntax from a macro

Set buffer syntax from a macro

Postby Kicweed on Wed Nov 28, 2012 12:12 pm

Is it possible to set the syntax of the current (possibly unsaved) buffer from within a macro? I've looked at the existing macros in my Packages folder but I couldn't find a command to do this. I looked for something like

Code: Select all
{
   "command": "set_syntax",
   "args":   {"syntax_name": "XML"}
}


but I could not find anything.


I need to be able to:

1. Ctrl+N to open a new buffer
2. Paste some text
3. Hit a shortcut that sets a predefined syntax and runs a macro on the contents
Kicweed
 
Posts: 22
Joined: Mon Jan 09, 2012 9:48 am

Re: Set buffer syntax from a macro

Postby phillip.koebbe on Wed Nov 28, 2012 1:52 pm

If you view the console and enter the command

Code: Select all
sublime.log_commands(True)


You'll see all the commands that ST2 is running. Doing that and then using the syntax selector at the bottom right of the window, I saw this command being run when I set a syntax:

Code: Select all
set_file_type {"syntax": "Packages/ShellScript/Shell-Unix-Generic.tmLanguage"}


Obviously you'd need to replace the syntax file with whatever is appropriate for your situation. Let us know how this works out for you!
-- Phillip

Code: Select all
while self.living? do
    mistake = Mistake.new
    self.learn_from!(mistake) unless mistake.fatal?
end
phillip.koebbe
 
Posts: 208
Joined: Tue Dec 20, 2011 4:58 pm

Re: Set buffer syntax from a macro

Postby Kicweed on Wed Nov 28, 2012 2:40 pm

It worked! Thank you very much, that was exactly the step I was missing.

Following your hints, I added the following into my macro and now it works exactly as I need it.

Code: Select all
{
   "command": "set_file_type",
   "args": {"syntax": "Packages/XML/XML.tmLanguage"}
}



Initially I tried the following thinking it would be more abstract (and thus safer between different OSes), but it doesn't work reliably

Code: Select all
{
   "command": "set_file_type",
   "args": {"file": "${packages}/XML/XML.tmLanguage"}
}


Thanks again.
Kicweed
 
Posts: 22
Joined: Mon Jan 09, 2012 9:48 am


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 32 guests