In this post we will go over how to create a custom MGrammar Command in Intellipad.
Launch Intellipad with Samples enabled (See Quetzal's post on Intellipad Samples if you are unfamiliar). Let's open up %ProgramFiles%\Microsoft Oslo SDK 1.0\Bin\Intellipad\Samples\Microsoft.M.Grammar.IntellipadPlugin\MGrammarCommands.py in Intellipad. WordWrap is turned on below via Ctrl+W,W as the lines are too long to fit on the screen below:
Now let's copy the following python snippet to the end of the MGrammarCommands file. It is a slight modification to the TreePreview command to support launching the MGrammar panes without requiring an existing MGrammar (Caveat: APIs used by the following command is not yet locked down and are expected to change significantly before release):
@Metadata.CommandExecuted('{Microsoft.Intellipad}BufferView', '{Microsoft.M.Grammar}NewMGrammar', 'Ctrl+Shift+Z')
def NewMGrammar(target, sender, args):
from Microsoft.Win32 import OpenFileDialog
from Microsoft.Intellipad.Host import HostWindow
from System.Windows.Controls import Orientation
transformSource = Core.ComponentDomain.GetBoundValue[System.Object]('{Microsoft.M.Grammar.IntellipadPlugin}MGTreePreviewBufferSource')
hostWindow = HostWindow.GetHostWindowForBufferView(sender)
#reset the panes if needed
if hostWindow.BufferViews.Count > 1:
for (idx, view) in enumerate(list(hostWindow.BufferViews)):
if view != sender:
view.Close()
Common.DrainDispatcher()
uri = System.Uri('file://')
sender.Buffer = Common.BufferManager.GetBuffer(uri)
errorUri = System.Uri('transient://errors')
errorView = hostWindow.SplitBufferView(sender, Orientation.Vertical)
hostWindow.ChangeBufferViewLength(errorView, 0.20, Orientation.Vertical)
errors = Common.BufferManager.GetBuffer(errorUri)
errorView.Buffer = errors
errorView.Mode = Core.ComponentDomain.GetBoundValue[System.Object]('{Microsoft.Intellipad}HyperlinkMode')
grammar = Common.BufferManager.GetBuffer(uri)
hostWindow = HostWindow.GetHostWindowForBufferView(sender)
grammarView = hostWindow.SplitBufferView(sender, Orientation.Horizontal)
grammarView.Buffer = grammar
Common.Write(grammarView.Buffer, 'module M1\r\n{\r\n\tlanguage L1\r\n\t{\r\n\t\tsyntax Main=empty;\r\n\t}\r\n}')
SetMGMode(target, grammarView, args)
hostWindow.ChangeBufferViewLength(sender, 0.4, Orientation.Horizontal)
Common.DrainDispatcher()
uri = System.Uri('mg2tree://TreeView')
(hasBuffer, treeViewBuffer) = Common.BufferManager.TryGetBuffer(uri)
if hasBuffer: treeViewBuffer.Close()
buffer = transformSource.CreateBuffer(uri, sender.Buffer, grammar)
Common.BufferManager.OpenBuffers.Add(buffer)
hostWindow = HostWindow.GetHostWindowForBufferView(grammarView)
newView = hostWindow.SplitBufferView(grammarView, Orientation.Horizontal)
hostWindow.ChangeBufferViewLength(newView, 0.5, Orientation.Horizontal)
newView.Buffer = buffer
SetPreviewMode(newView)
SetDynamicParserMode(sender)
Common.SetActiveView(sender)
Now that I have pasted the above snippet, let's go over the python snippet.
@Metadata.CommandExecuted is a decorator for the immediate python function followed. It registers a command to Intellipad:
1st parameter: Command Target: This command is targeting a BufferView (i.e {Microsoft.Intellipad}BufferView), just like the TreePreview command
2nd Parameter: Command Name: The name is {Microsoft.M.Grammar}NewMGrammar, which will also be displayed in help -> commands
3rd parameter: Command Keyboard Binding: In this case, we are mapping this command to Ctrl+Shift+Z
This new command is similar to the existing TreePreview command except the following:
- It allows one to launch MGrammar panes without requiring a pre-existing MGrammar on disk.
-
Reset the splits/panes prior launching the MGrammar panes so it will always have enough space to display them
Save all the changes. Remember you will need administrator permissions to overwrite MgrammarCommands.py in the October 2008 CTP on Vista. Either launch Intellipad as Administrator or change the permission on the file from the windows explorer.
Now refresh the settings. This can be accomplished by invoking the '{Microsoft.Intellipad}ReloadSettings' command or Ctrl+Alt+F5. After a short delay, invoke Help -> Commands, you should see the newly created command:
So far so good, it will be even better if we can add this command to the MGrammar Mode specific drop down menu. You can access the Mode specific menu for MGrammar Mode in %ProgramFiles%\Microsoft Oslo SDK 1.0\Bin\Intellipad\Samples\Microsoft.M.Grammar.IntellipadPlugin\Private\ModeMenuItem.xcml. For details on adding the command to the mode specific menu, you can refer to Quetzal's post on Customizing Menu Bar.
With the new command, you can now launch the MGrammar panes without a pre-existing MGrammar file, like below: