def _updateMajorMode(ev): global _majorMode, _oldMajorMode _majorMode = runEmacsCmd("(md-get-all-modes)") _majorMode = _getEmacsList(_majorMode) if _majorMode != _oldMajorMode: pushEvent(MajorModeEvent(_majorMode)) _oldMajorMode = _majorMode
def _updateMajorMode(ev): global _majorMode, _oldMajorMode _majorMode = runEmacsCmd("(md-get-all-modes)") _majorMode = _getEmacsList(_majorMode) if _majorMode != _oldMajorMode: log.info("Switching major mode: {}".format(_majorMode)) pushEvent(MajorModeEvent(_majorMode)) _oldMajorMode = _majorMode
def _sendWords(self, ev=None): # We want to iterate the whole possible space, not just what's present, # because we want to initialize empty word lists to empty in case there # are left over values from previous runs of the server. for i in range(self.MAX_SUBWORDS): for j in range(i+1): try: words = self.words[i][j] except IndexError: words = [] pushEvent(WordListEvent(self._wordListName(i, j), words))
def update(self, ev=None): window = ev.window if ev else getFocusedWindow() newOutput = runEmacsCmd(self.cmd, inFrame=self.inFrame, allowError=self.allowError, dolog=(self.logging or self.clsLogging)) newOutput = self._postProcess(newOutput) if newOutput == self.lastOutput: return self.lastOutput = newOutput #log.info("New output!") pushEvent(self._makeEvent(newOutput))
def _buildRule(self): mapping = {} for command in self.cmdWords: mapping[command] = None self.actionRule = makeHashedRule(self._actionRuleName, mapping, ruleType=RuleType.INDEPENDENT) pushEvent(RuleRegisterEvent(self.actionRule)) mapping = {} extras = [] for i in range(self.MAX_SUBWORDS): for j in range(i+1): phrase = [] for k in range(j, self.MAX_SUBWORDS): optional = (k != j) refString = "<" + self._wordListRefName(i, k) + ">" if optional: refString = "[%s]" % refString phrase.append(refString) extras.append(ListRef(self._wordListName(i, k), self._wordListRefName(i, k), [])) phrase = " ".join(phrase) mapping[phrase] = None self.wordRule = makeHashedRule(self._wordRuleName, mapping, extras, ruleType=RuleType.INDEPENDENT) pushEvent(RuleRegisterEvent(self.wordRule)) wordRulePart = "<%s>" % self._wordRuleRefName if self.allowNoChoice: wordRulePart = "[%s]" % wordRulePart phrase = ("<%s>" % self._actionRuleRefName) + " " + wordRulePart mapping = { phrase : self._onSelection } extras = [ RuleRef(self.actionRule, self._actionRuleRefName), RuleRef(self.wordRule, self._wordRuleRefName), ] self.rule = makeContextualRule(self._ruleName, mapping, extras, ruleType=self.ruleType) log.info("new crazy rule: [%s]" % self.rule.rule.rule.mapping) log.info("new crazy rule extras: [%s]" % self.rule.rule.rule.extras)
def dataCb(data, deviceId, error): global oldPedals pedals = [0] * 3 pedals[0] = (data[3] & 2) != 0 pedals[1] = (data[3] & 4) != 0 pedals[2] = (data[3] & 8) != 0 for i in range(3): if pedals[i] != oldPedals[i]: changed = i oldPedals = pedals log.info("Pedals: [%s]" % pedals) pushEvent(PedalsEvent(pedals, changed)) if _pedalCb: _pedalCb(pedals, changed) global wakeupFd # We use eventfd to wakeup the main thread so it will # see the pedal event written = os.write(wakeupFd, c_longlong(1)) if written != 8: log.error("Error writing to eventfd.") return 0
def _buildRule(self, requirements, keywords): listRule = makeKeywordListRule(self.name + "-list", keywords) pushEvent(RuleRegisterEvent(listRule)) verbRule = "_".join([self.name, "mode_verb_rule"]) kwRule = "_".join([self.name, "keyword"]) mapping = { ("<%s> <%s>" % (verbRule, kwRule)) : KeywordCmd(keywords, verbRule, kwRule), } extras = [ RuleRef(VerbRule, verbRule), RuleRef(listRule, kwRule), ] KeywordRule = makeContextualRule(self.name, mapping, extras) KeywordRule.context.addRequirement(IsEmacs) for r in self.requirements: KeywordRule.context.addRequirement(r) return KeywordRule
from requirements.VarRequirement import VarRequirement from rules.emacs.common import emacsExtras, emacsDefaults _mapping = { "red": "red", "green": "green", "white": "white", "purple": "purple", "yellow": "yellow", "orange": "orange", } ColorRule = makeHashedRule("ColorRule", _mapping, ruleType=RuleType.INDEPENDENT) pushEvent(RuleRegisterEvent(ColorRule)) _mapping = { "circle": 0x030a, "corner": 0x031a, "hair": 0x030f, } AccentRule = makeHashedRule("AccentRule", _mapping, ruleType=RuleType.INDEPENDENT) pushEvent(RuleRegisterEvent(AccentRule)) class PickSymbol(Cmd): classLog = True
"osh" : "o", "pig" : "p", "quid" : "q", "robe" : "r", "shoe" : "s", "toss" : "t", "use" : "u", "vict" : "v", "was" : "w", "xray" : "x", "yes" : "y", "zulu" : "z", } AlphaRule = makeHashedRule("AlphaRule", _mapping, ruleType=RuleType.INDEPENDENT) pushEvent(RuleRegisterEvent(AlphaRule)) _mapping = { "zero" : "0", "one" : "1", "two" : "2", "three" : "3", "four" : "4", "five" : "5", "six" : "6", "seven" : "7", "eight" : "8", "nine" : "9", } DigitRule = makeHashedRule("DigitRule", _mapping, ruleType=RuleType.INDEPENDENT)
def sendRepeatEvent(): pushEvent(RepeatRequestEvent())
"osh" : "o", "pig" : "p", "quid" : "q", "road" : "r", "shoe" : "s", "toss" : "t", "use" : "u", "vict" : "v", "was" : "w", "xray" : "x", "yes" : "y", "zulu" : "z", } AlphaRule = makeHashedRule("AlphaRule", _mapping, ruleType=RuleType.INDEPENDENT) pushEvent(RuleRegisterEvent(AlphaRule)) _mapping = { "zero" : "0", "one" : "1", "two" : "2", "three" : "3", "four" : "4", "five" : "5", "six" : "6", "seven" : "7", "eight" : "8", "nine" : "9", } DigitRule = makeHashedRule("DigitRule", _mapping, ruleType=RuleType.INDEPENDENT)
def _onModeChange(self): snippetList = grammar.getStringList(runEmacsCmd("(md-get-snippet-names)")) log.info("Snippet names: [%s]" % snippetList) pushEvent(WordListEvent("SnippetList", snippetList))
def _onSelection(self, extras={}): words = self._getWords(extras) if not words: if self.allowNoChoice: self._noChoice() else: log.error("Command [%s] must be used with a choice." % extras[self._actionRuleRefName]["words"]) pushEvent(RecognitionStateEvent("failure")) return # Selection process works as follows # -Not all words are required to be given, only a subset # -But all candidates must include all given words in given order # -Consecutive words score higher than separated ones # -If a candidate is already selected, we go for the # next highest score, cycling if necessary. candidates = [] for winWords, window in self.selectionMap: totalHoleSize = 0 lastIdx = None # We do the search backwards so say the window name # is "file: emacs/emacs.py" and the spoken form is "emacs py" # it's probably more likely we meant to match the emacs # after the slash rather than the former. # TODO: we should just do multiple passes over the list # assuming different instances of "emacs" is the start of # what's being spoken and take the best total hole size. revWinWords = list(reversed(winWords)) try: for w in reversed(words): if lastIdx: # start at index we last left off at so we can # enforce word order idx = revWinWords.index(w, lastIdx) else: idx = revWinWords.index(w) if lastIdx is not None: # we subract one because there should be no # penalty if the words are adjacent. totalHoleSize += (idx - lastIdx) - 1 lastIdx = idx lengthDifference = abs(len(winWords) - len(words)) candidates.append((totalHoleSize, lengthDifference, window)) except ValueError: # all words that were given must be present continue if not candidates: log.error("No choice with name containing words in order: [%s]" % words) log.error("selectionMap: [%s]" % self.selectionMap) pushEvent(RecognitionStateEvent("failure")) return # sort by total hole size #candidates.sort(key=lambda x: (x[0], x[1])) candidates.sort() log.debug("candidates: [%s]" % candidates) # remove hole sizes leaving just the windows candidates = [c[-1] for c in candidates] log.debug("selectionMap: [%s]" % self.selectionMap) # check if the current window is a candidate. if so # just cycle through to the next best scoring candidate. currentSelection = self._currentChoice() try: idx = candidates.index(currentSelection) self._select(extras[self._actionRuleRefName], candidates[(idx+1) % len(candidates)]) return except ValueError: pass # otherwise go with the best self._select(extras[self._actionRuleRefName], candidates[0])
from requirements.Emacs import IsEmacs from requirements.ModeRequirement import ModeRequirement from rules.emacs.Cmd import Cmd, runEmacsCmd from rules.emacs.Text import EmacsText from protocol import makeHashedRule, RuleType, RuleRef from EventLoop import pushEvent from EventList import RuleRegisterEvent _mapping = { "key": None, "doog key": None, "go key": None, } VerbRule = makeHashedRule("VerbRule", _mapping, ruleType=RuleType.INDEPENDENT) pushEvent(RuleRegisterEvent(VerbRule)) class KeywordCmd(Cmd): def __init__(self, keywords, verbRule, kwRule, log=False): self.writtenForms = {} self.verbRule = verbRule self.kwRule = kwRule for i in keywords: self.writtenForms[i[1]] = i[0] Cmd.__init__(self, None, log) def _lisp(self, extras={}): command = " ".join(extras[self.verbRule]['words']) spokenKeyword = " ".join(extras[self.kwRule]['words']) writtenKeyword = self.writtenForms[spokenKeyword]