How to use the rules.emacs.Cmd.Cmd function in rules

To help you get started, we’ve selected a few rules examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jgarvin / mandimus / rules / emacs / Keywords.py View on Github external
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]
        if command == "key":
            EmacsText("%s" % writtenKeyword, lower=False)()
        elif command == "doog key":
            return "(md-go-to-previous \"%s\")" % writtenKeyword
github jgarvin / mandimus / rules / emacs / SymbolPicker.py View on Github external
_mapping = {
    "  []"      : PickSymbol("md-hl-insert-symbol", 0),
    "jump   []" : PickSymbol("md-hl-jump-symbol"),
}

_extras = emacsExtras + [
    RuleRef(AccentRule, "accentrule"),
    RuleRef(ColorRule, "colorrule"),
    ]

SymbolPickerRule = makeContextualRule("SymbolPicker", _mapping, _extras, emacsDefaults)
SymbolPickerRule.context.addRequirement(IsEmacs)
SymbolPickerRule.context.addRequirement(VarRequirement("md-symbol-picker-mode", "t"))

_mapping = {
    "toggle picker"                               : Cmd("(md-toggle-symbol-picker-mode)"),
}
SymbolPickerToggleRule = makeContextualRule("SymbolPickerToggle", _mapping, _extras, emacsDefaults)
SymbolPickerToggleRule.context.addRequirement(IsEmacs)
github jgarvin / mandimus / rules / emacs / Snippet.py View on Github external
import mdlog
log = mdlog.getLogger(__name__)
from rules.emacs.Cmd import CharCmd, Cmd, runEmacsCmd
from rules.ContextualRule import makeContextualRule
from requirements.Emacs import IsEmacs
from rules.emacs.common import emacsExtras, emacsDefaults
from EventList import WordListEvent, MajorModeEvent
from EventLoop import getLoop, pushEvent
from protocol import ListRef
import grammar

_mapping = {
    "warp "   : CharCmd('(md-sn-find-slot %s)'),
    "blank"             : Cmd('(md-sn-next-slot)'),
    "make blank"        : Cmd('(md-sn-drop-slot)'),
    "funk call [<i>]"   : Cmd("(md-insert-call-snippet %(i)d)"),
    "make " : Cmd("(md-insert-snippet \"%(snippetList)s\")"),
}

_extras = [
    ListRef("SnippetList", "snippetList", [])
]

SnippetRule = makeContextualRule("Snippet", _mapping, emacsExtras + _extras, emacsDefaults)
SnippetRule.context.addRequirement(IsEmacs)

def _onModeChange(self):
    snippetList = grammar.getStringList(runEmacsCmd("(md-get-snippet-names)"))
    log.info("Snippet names: [%s]" % snippetList)
    pushEvent(WordListEvent("SnippetList", snippetList))
</i>
github jgarvin / mandimus / rules / emacs / Cmd.py View on Github external
1)
        return repeat

    def __call__(self, extras={}):
        code = self._lisp(extras)
        if code is None:
            if self.log or self.classLog:
                log.info("%s no lisp code" % (type(self).__name__))
            return
        if self.log or self.classLog:
            log.info("%s lisp code: [%s]" % (type(self).__name__, code))

        for i in range(self._repetitions(extras)):
            runEmacsCmd(code, dolog=(self.log or self.classLog), queryOnly=self.queryOnly)

class CharCmd(Cmd):
    classLog = False
    def _lisp(self, extras={}):
        char = BaseRules.lookup(extras["charrule"])
        char = emacsChar(char)
        return self.data % char

class InsertString(Cmd):
    def __init__(self, stringReturningElisp):
        data = "(md-insert-text %s nil nil)" % stringReturningElisp
        Cmd.__init__(self, data)

def emacsChar(char):
    c = ["?"]
    # most characters don't need escaping, but some do
    if char in " \n\t()\\|;'`\"#.,\a\b\f\r":
        c.append("\\")
github jgarvin / mandimus / rules / emacs / Nav.py View on Github external
# TODO: vertical versions of commands? could be handy for navigation to have
# a jump that considers vertical to cost 0 and horizontal to cost normal,
# so you could say whatever mike to go from the closing brace to mapping.

# TODO: maybe different commands for letters vs. symbols? help break up
# possibilities?

_mapping = {
    "home"                    : Key("home"),
    "edge"                    : Key("end"),
    "top side"                : Key("a-langle"),
    "bottom"                  : Key("a-rangle"),
    "pro []"               : Key("a-f:%(n)d"),
    "per []"               : Key("a-b:%(n)d"),
    "over []"              : Cmd("(forward-symbol 1)"),
    "under []"             : Cmd("(forward-symbol -1)"),
    "leaf []"              : Key("pgdown:%(n)d"),
    "feel []"              : Key("pgup:%(n)d"),

    "gruff []"             : Key("a-lbrace:%(n)d"),
    "graph []"             : Key("a-rbrace:%(n)d"),
    "left []"              : Key("c-b:%(n)d"),
    "right []"             : Key("c-f:%(n)d"),
    "hike []"              : Key("c-p:%(n)d"),
    "slide []"             : Key("c-n:%(n)d"),

    "go  [<i>]"     : CharCmd("(md-move-up-to-char 1 %s)"),
    "doog  [<i>]"   : CharCmd("(md-move-up-to-char -1 %s)"),
    "function"                : Key("ca-a"),
    "snug [<i>]"              : Cmd("(md-find-indentation-change 1 '&gt;)"),
    "guns [<i>]"              : Cmd("(md-find-indentation-change -1 '&gt;)"),
    "loosen [<i>]"            : Cmd("(md-find-indentation-change 1 '&lt;)"),</i></i></i></i></i>
github jgarvin / mandimus / rules / emacs / Emacs.py View on Github external
"stop irc"                    : Minibuf("stop-irc"),
    "toggle tail mode"            : Cmd("(auto-revert-tail-mode)"),
    "list packages"               : Minibuf("list-packages"),
    "get status"                  : Key("c-t,g"),
    "open terminal"               : Cmd("(etc-start-or-open-terminal)"),
    # "show top"                  : Cmd("(etc-start-or-open-top)"),
    "open temp"                   : Cmd("(md-create-temp-file \"temp\")"),
    "open tramp"                   : Key("c-c,s,t"),
    "toggle command logging"      : toggleCommandLogging,
    "toggle refresh client"       : toggleRefreshClientSources,
    "magnify [<i>]"               : Key("c-t,c-plus:%(i)d"),
    "demagnify [<i>]"             : Key("c-t,c-minus:%(i)d"),
    # "compile"                   : Minibuf("compile"),
    "visual line mode"            : Minibuf("visual-line-mode"),
    "set indent "              : Cmd("(etc-set-indent-preference %(j)d)"),
    "toggle namespace indent"     : Cmd("(etc-toggle-namespace-indent)"),
    "toggle read only"            : Key("c-t,c-q"),
}


# EmacsIsolatedRule = makeContextualRule("EmacsIsolated", _mapping, emacsExtras, emacsDefaults, ruleType=RuleType.INDEPENDENT)
EmacsIsolatedRule = makeContextualRule("EmacsIsolated", _mapping, emacsExtras, emacsDefaults)
EmacsIsolatedRule.context.addRequirement(IsEmacs)

_mapping = {
    "help function"      : Key("c-h,f"),
    "help language"      : Key("c-h,g"),
    "help function slap" : Key("c-h,f,enter"),
    "help variable"      : Key("c-h,v"),
    "help variable slap" : Key("c-h,v,enter"),
    "help key"           : Key("c-h,k"),
    "help mode"          : Key("c-h,m"),</i></i>
github jgarvin / mandimus / rules / emacs / Nav.py View on Github external
# TODO: vertical versions of commands? could be handy for navigation to have
# a jump that considers vertical to cost 0 and horizontal to cost normal,
# so you could say whatever mike to go from the closing brace to mapping.

# TODO: maybe different commands for letters vs. symbols? help break up
# possibilities?

_mapping = {
    "home"                    : Key("home"),
    "edge"                    : Key("end"),
    "top side"                : Key("a-langle"),
    "bottom"                  : Key("a-rangle"),
    "pro []"               : Key("a-f:%(n)d"),
    "per []"               : Key("a-b:%(n)d"),
    "over []"              : Cmd("(forward-symbol 1)"),
    "under []"             : Cmd("(forward-symbol -1)"),
    "leaf []"              : Key("pgdown:%(n)d"),
    "feel []"              : Key("pgup:%(n)d"),

    "gruff []"             : Key("a-lbrace:%(n)d"),
    "graph []"             : Key("a-rbrace:%(n)d"),
    "left []"              : Key("c-b:%(n)d"),
    "right []"             : Key("c-f:%(n)d"),
    "hike []"              : Key("c-p:%(n)d"),
    "slide []"             : Key("c-n:%(n)d"),

    "go  [<i>]"     : CharCmd("(md-move-up-to-char 1 %s)"),
    "doog  [<i>]"   : CharCmd("(md-move-up-to-char -1 %s)"),
    "function"                : Key("ca-a"),
    "snug [<i>]"              : Cmd("(md-find-indentation-change 1 '&gt;)"),
    "guns [<i>]"              : Cmd("(md-find-indentation-change -1 '&gt;)"),</i></i></i></i>
github jgarvin / mandimus / rules / emacs / Words.py View on Github external
def _select(self, action, choice):
        if "go" in action["words"]:
            Cmd("(md-go-to-next \"%s\")" % choice)()
        elif "doog" in action["words"]:
            Cmd("(md-go-to-previous \"%s\")" % choice)()
        else:
            EmacsText("%s" % choice, lower=False)()
github jgarvin / mandimus / rules / emacs / Cmd.py View on Github external
log.info("%s no lisp code" % (type(self).__name__))
            return
        if self.log or self.classLog:
            log.info("%s lisp code: [%s]" % (type(self).__name__, code))

        for i in range(self._repetitions(extras)):
            runEmacsCmd(code, dolog=(self.log or self.classLog), queryOnly=self.queryOnly)

class CharCmd(Cmd):
    classLog = False
    def _lisp(self, extras={}):
        char = BaseRules.lookup(extras["charrule"])
        char = emacsChar(char)
        return self.data % char

class InsertString(Cmd):
    def __init__(self, stringReturningElisp):
        data = "(md-insert-text %s nil nil)" % stringReturningElisp
        Cmd.__init__(self, data)

def emacsChar(char):
    c = ["?"]
    # most characters don't need escaping, but some do
    if char in " \n\t()\\|;'`\"#.,\a\b\f\r":
        c.append("\\")
    c.append(char)
    return "".join(c)

getCommandsEl = """
(let ((y '()))
  (mapatoms
   (lambda (x)
github jgarvin / mandimus / rules / emacs / SymbolPicker.py View on Github external
"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
    def __init__(self, data, leadingWords=1):
        self.leadingWords = leadingWords
        Cmd.__init__(self, data)

    def _lisp(self, extras={}):
        color = extras['colorrule']["words"][0]
        letter = BaseRules.lookup(extras)
        mark = AccentRule.rule.mapping[extras['accentrule']["words"][0]] if 'accentrule' in extras else None
        mark = ("#x%x" % mark) if mark else "nil"
        return '(%s "%s" %s "%s")' % (self.data, letter, mark, color)
 
_mapping = {
    "  []"      : PickSymbol("md-hl-insert-symbol", 0),
    "jump   []" : PickSymbol("md-hl-jump-symbol"),
}