Customize Buffer Context Menu

The text buffers context menu can be customized through an API provided by the Editra Message System. See the below example for usage.

import wx
import ed_msg
ID_MY_MENU_ID = wx.NewId()
 
def OnCustomizeBufferMenu(msg):
    data = msg.GetData()
    menu = data.GetMenu()
    menu.Append(ID_MY_MENU_ID, "Hello World")
    data.AddHandler(ID_MY_MENU_ID, MyMenuHandler)
 
ed_msg.Subscribe(OnCustomizeBufferMenu, ed_msg.EDMSG_UI_STC_CONTEXT_MENU)
def MyMenuHandler(text_buffer, event):
    wx.MessageBox("Hello World")
This code will create a menu item in the right click menu of the text editor area. When the menu is clicked the MyMenuHandler function will be called.