import re import wx from pwiki import wxHelper WIKIDPAD_PLUGIN = (("MenuFunctions",1),) def describeMenuItems(wiki): """ wiki -- Calling PersonalWikiFrame Returns a sequence of tuples to describe the menu items, where each must contain (in this order): - callback function - menu item string - menu item description (string to show in status bar) It can contain the following additional items (in this order), each of them can be replaced by None: - icon descriptor (see below, if no icon found, it won't show one) - menu item id. - update function - kind of menu item (wx.ITEM_NORMAL, wx.ITEM_CHECK) The callback function must take 2 parameters: wiki - Calling PersonalWikiFrame evt - wx.CommandEvent If the menu item string contains one or more vertical bars '|' these are taken as delimiters to describe a "path" of submenus where the item should be placed. E.g. the item string "Admin|Maintenance|Reset Settings" will create in plugins menu a submenu "Admin" containing a submenu "Maintenance" containing the item "Reset Settings". An icon descriptor can be one of the following: - a wx.Bitmap object - the filename of a bitmap (if file not found, no icon is used) - a tuple of filenames, first existing file is used """ kb = wiki.getKeyBindings() return ((wordCount, u"Word count", u"Word count"),) _splitterRE = re.compile(ur"\s+", re.UNICODE) _removeRE = re.compile(ur"(?:(?!\s)(?:\W))+", re.UNICODE) def wordCount(wiki, evt): wiki.showStatusMessage("wordCount") editor = wiki.getActiveEditor() if editor is None: return text = editor.GetSelectedText() if text == "": # Whole editor content text = editor.GetText() text = _removeRE.sub(u"", text) splits = _splitterRE.split(text) try: while splits[0] == "": del splits[0] except IndexError: pass try: while splits[-1] == "": del splits[-1] except IndexError: pass count = len(splits) wx.CallAfter(wiki.showStatusMessage, "Counted %i words" % count, -1)