Beispiel #1
0
 def test_dedent(self):
     # Empty string, return empty string
     self.assertEqual("", utils.dedent(""))
     # No leading whitespace
     self.assertEqual("TestDedent", utils.dedent("TestDedent"))
     # Leading whitespace, single line
     self.assertEqual("TestDedent", utils.dedent("   TestDedent"))
     # Leading whitespace, multi line
     input_string = "  hello\n  world"
     expected_string = "hello\nworld"
     self.assertEqual(expected_string, utils.dedent(input_string))
Beispiel #2
0
def menunode_end(caller, raw_string):
    """Farewell message."""
    caller.new_char.db.chargen_complete = True
    text = dedent("""
        Congratulations!

        You have completed |mAinneve|n character creation.
        This could be some more informative message eventually...""")
    return text, None
def at_initial_setup():
    limbo = search.objects('Limbo')[0]
    limbo.db.desc = dedent("""
        Welcome to {mAinneve{n, the example game for Evennia!

        The project is still in early development, and we welcome your contributions.

        {YDeveloper/Builder Resources{n
          * Issue tracking: https://github.com/evennia/ainneve/issues
          * Discussion list: https://groups.google.com/forum/?fromgroups#!categories/evennia/ainneve
          * Ainneve Wiki: https://github.com/evennia/ainneve/wiki
          * Evennia Developer IRC: http://webchat.freenode.net/?channels=evennia

        {YGetting Started{n
          As Player #1 you can use the {w@batchcmd{n or {w@batchcode{n commands to
          build components of Ainneve, or the entire world (once it has been created).

          Build scripts are in the {wworld/build/{n directory and have {w*.ev{n or {w*.py{n extensions.

        """)
Beispiel #4
0
def menunode_welcome_archetypes(caller):
    """Starting page and Archetype listing."""
    text = dedent("""\
        |wWelcome to |mAinneve|w, the example game for |yEvennia|w.|n

        To begin, select an |cArchetype|n. There are three base
        archetypes to choose from, plus three dual archetypes which
        combine the strengths and weaknesses of two archetypes into one.

        Select an Archetype by number below to view its details, or |whelp|n
        at any time for more info.
    """)
    help = fill("In |mAinneve|n, character |cArchetypes|n represent the "
                "characters' class or primary role. The various archetypes "
                "have different strengths and weaknesses, which are reflected "
                "in your character's starting traits.")
    options = []
    for arch in archetypes.VALID_ARCHETYPES:
        a = archetypes.load_archetype(arch)
        options.append({"desc": "|c{}|n".format(a.name),
                        "goto": "menunode_select_archetype"})
    return (text, help), options
Beispiel #5
0
    def func(self):
        """
        This command handles all the in-editor :-style commands. Since
        each command is small and very limited, this makes for a more
        efficient presentation.
        """
        caller = self.caller
        editor = self.editor
        linebuffer = self.linebuffer
        lstart, lend = self.lstart, self.lend
        cmd = self.cmdstring
        echo_mode = self.editor._echo_mode

        if cmd == ":":
            # Echo buffer
            if self.linerange:
                buf = linebuffer[lstart:lend]
                editor.display_buffer(buf=buf, offset=lstart)
            else:
                editor.display_buffer()
        elif cmd == "::":
            # Echo buffer without the line numbers and syntax parsing
            if self.linerange:
                buf = linebuffer[lstart:lend]
                editor.display_buffer(buf=buf,
                                      offset=lstart,
                                      linenums=False, raw=True)
            else:
                editor.display_buffer(linenums=False, raw=True)
        elif cmd == ":::":
            # Insert single colon alone on a line
            editor.update_buffer(editor.buffer + "\n:")
            if echo_mode:
                caller.msg("Single ':' added to buffer.")
        elif cmd == ":h":
            # help entry
            editor.display_help()
        elif cmd == ":w":
            # save without quitting
            editor.save_buffer()
        elif cmd == ":wq":
            # save and quit
            editor.save_buffer()
            editor.quit()
        elif cmd == ":q":
            # quit. If not saved, will ask
            if self.editor._unsaved:
                caller.cmdset.add(SaveYesNoCmdSet)
                caller.msg("Save before quitting? {lcyes{lt[Y]{le/{lcno{ltN{le")
            else:
                editor.quit()
        elif cmd == ":q!":
            # force quit, not checking saving
            editor.quit()
        elif cmd == ":u":
            # undo
            editor.update_undo(-1)
        elif cmd == ":uu":
            # redo
            editor.update_undo(1)
        elif cmd == ":UU":
            # reset buffer
            editor.update_buffer(editor._pristine_buffer)
            caller.msg("Reverted all changes to the buffer back to original state.")
        elif cmd == ":dd":
            # :dd <l> - delete line <l>
            buf = linebuffer[:lstart] + linebuffer[lend:]
            editor.update_buffer(buf)
            caller.msg("Deleted %s." % (self.lstr))
        elif cmd == ":dw":
            # :dw <w> - delete word in entire buffer
            # :dw <l> <w> delete word only on line(s) <l>
            if not self.arg1:
                caller.msg("You must give a search word to delete.")
            else:
                if not self.linerange:
                    lstart = 0
                    lend = self.cline + 1
                    caller.msg("Removed %s for lines %i-%i." % (self.arg1, lstart + 1, lend + 1))
                else:
                    caller.msg("Removed %s for %s." % (self.arg1, self.lstr))
                sarea = "\n".join(linebuffer[lstart:lend])
                sarea = re.sub(r"%s" % self.arg1.strip("\'").strip('\"'), "", sarea, re.MULTILINE)
                buf = linebuffer[:lstart] + sarea.split("\n") + linebuffer[lend:]
                editor.update_buffer(buf)
        elif cmd == ":DD":
            # clear buffer
            editor.update_buffer("")
            caller.msg("Cleared %i lines from buffer." % self.nlines)
        elif cmd == ":y":
            # :y <l> - yank line(s) to copy buffer
            cbuf = linebuffer[lstart:lend]
            editor._copy_buffer = cbuf
            caller.msg("%s, %s yanked." % (self.lstr.capitalize(), cbuf))
        elif cmd == ":x":
            # :x <l> - cut line to copy buffer
            cbuf = linebuffer[lstart:lend]
            editor._copy_buffer = cbuf
            buf = linebuffer[:lstart] + linebuffer[lend:]
            editor.update_buffer(buf)
            caller.msg("%s, %s cut." % (self.lstr.capitalize(), cbuf))
        elif cmd == ":p":
            # :p <l> paste line(s) from copy buffer
            if not editor._copy_buffer:
                caller.msg("Copy buffer is empty.")
            else:
                buf = linebuffer[:lstart] + editor._copy_buffer + linebuffer[lstart:]
                editor.update_buffer(buf)
                caller.msg("Copied buffer %s to %s." % (editor._copy_buffer, self.lstr))
        elif cmd == ":i":
            # :i <l> <txt> - insert new line
            new_lines = self.args.split('\n')
            if not new_lines:
                caller.msg("You need to enter a new line and where to insert it.")
            else:
                buf = linebuffer[:lstart] + new_lines + linebuffer[lstart:]
                editor.update_buffer(buf)
                caller.msg("Inserted %i new line(s) at %s." % (len(new_lines), self.lstr))
        elif cmd == ":r":
            # :r <l> <txt> - replace lines
            new_lines = self.args.split('\n')
            if not new_lines:
                caller.msg("You need to enter a replacement string.")
            else:
                buf = linebuffer[:lstart] + new_lines + linebuffer[lend:]
                editor.update_buffer(buf)
                caller.msg("Replaced %i line(s) at %s." % (len(new_lines), self.lstr))
        elif cmd == ":I":
            # :I <l> <txt> - insert text at beginning of line(s) <l>
            if not self.args:
                caller.msg("You need to enter text to insert.")
            else:
                buf = linebuffer[:lstart] + ["%s%s" % (self.args, line) for line in linebuffer[lstart:lend]] + linebuffer[lend:]
                editor.update_buffer(buf)
                caller.msg("Inserted text at beginning of %s." % self.lstr)
        elif cmd == ":A":
            # :A <l> <txt> - append text after end of line(s)
            if not self.args:
                caller.msg("You need to enter text to append.")
            else:
                buf = linebuffer[:lstart] + ["%s%s" % (line, self.args) for line in linebuffer[lstart:lend]] + linebuffer[lend:]
                editor.update_buffer(buf)
                caller.msg("Appended text to end of %s." % self.lstr)
        elif cmd == ":s":
            # :s <li> <w> <txt> - search and replace words
            # in entire buffer or on certain lines
            if not self.arg1 or not self.arg2:
                caller.msg("You must give a search word and something to replace it with.")
            else:
                if not self.linerange:
                    lstart = 0
                    lend = self.cline + 1
                    caller.msg("Search-replaced %s -> %s for lines %i-%i." % (self.arg1, self.arg2, lstart + 1 , lend))
                else:
                    caller.msg("Search-replaced %s -> %s for %s." % (self.arg1, self.arg2, self.lstr))
                sarea = "\n".join(linebuffer[lstart:lend])

                regex = r"%s|^%s(?=\s)|(?<=\s)%s(?=\s)|^%s$|(?<=\s)%s$"
                regarg = self.arg1.strip("\'").strip('\"')
                if " " in regarg:
                    regarg = regarg.replace(" ", " +")
                sarea = re.sub(regex % (regarg, regarg, regarg, regarg, regarg), self.arg2.strip("\'").strip('\"'), sarea, re.MULTILINE)
                buf = linebuffer[:lstart] + sarea.split("\n") + linebuffer[lend:]
                editor.update_buffer(buf)
        elif cmd == ":f":
            # :f <l> flood-fill buffer or <l> lines of buffer.
            width = _DEFAULT_WIDTH
            if not self.linerange:
                lstart = 0
                lend = self.cline + 1
                caller.msg("Flood filled lines %i-%i." % (lstart + 1 , lend))
            else:
                caller.msg("Flood filled %s." % self.lstr)
            fbuf = "\n".join(linebuffer[lstart:lend])
            fbuf = fill(fbuf, width=width)
            buf = linebuffer[:lstart] + fbuf.split("\n") + linebuffer[lend:]
            editor.update_buffer(buf)
        elif cmd == ":fi":
            # :fi <l> indent buffer or lines <l> of buffer.
            indent = " " * 4
            if not self.linerange:
                lstart = 0
                lend = self.cline + 1
                caller.msg("Indented lines %i-%i." % (lstart + 1 , lend))
            else:
                caller.msg("Indented %s." % self.lstr)
            fbuf = [indent + line for line in linebuffer[lstart:lend]]
            buf = linebuffer[:lstart] + fbuf + linebuffer[lend:]
            editor.update_buffer(buf)
        elif cmd == ":fd":
            # :fi <l> indent buffer or lines <l> of buffer.
            if not self.linerange:
                lstart = 0
                lend = self.cline + 1
                caller.msg("Removed left margin (dedented) lines %i-%i." % (lstart + 1 , lend))
            else:
                caller.msg("Removed left margin (dedented) %s." % self.lstr)
            fbuf = "\n".join(linebuffer[lstart:lend])
            fbuf = dedent(fbuf)
            buf = linebuffer[:lstart] + fbuf.split("\n") + linebuffer[lend:]
            editor.update_buffer(buf)
        elif cmd == ":echo":
            # set echoing on/off
            editor._echo_mode = not editor._echo_mode
            caller.msg("Echo mode set to %s" % editor._echo_mode)
Beispiel #6
0
    def func(self):
        """
        This command handles all the in-editor :-style commands. Since
        each command is small and very limited, this makes for a more
        efficient presentation.
        """
        caller = self.caller
        editor = caller.ndb._eveditor

        linebuffer = self.linebuffer
        lstart, lend = self.lstart, self.lend
        cmd = self.cmdstring
        echo_mode = self.editor._echo_mode

        if cmd == ":":
            # Echo buffer
            if self.linerange:
                buf = linebuffer[lstart:lend]
                editor.display_buffer(buf=buf, offset=lstart)
            else:
                editor.display_buffer()
        elif cmd == "::":
            # Echo buffer without the line numbers and syntax parsing
            if self.linerange:
                buf = linebuffer[lstart:lend]
                editor.display_buffer(buf=buf,
                                      offset=lstart,
                                      linenums=False,
                                      options={"raw": True})
            else:
                editor.display_buffer(linenums=False, options={"raw": True})
        elif cmd == ":::":
            # Insert single colon alone on a line
            editor.update_buffer([":"] if lstart == 0 else linebuffer + [":"])
            if echo_mode:
                caller.msg("Single ':' added to buffer.")
        elif cmd == ":h":
            # help entry
            editor.display_help()
        elif cmd == ":w":
            # save without quitting
            editor.save_buffer()
        elif cmd == ":wq":
            # save and quit
            editor.save_buffer()
            editor.quit()
        elif cmd == ":q":
            # quit. If not saved, will ask
            if self.editor._unsaved:
                caller.cmdset.add(SaveYesNoCmdSet)
                caller.msg(
                    "Save before quitting? |lcyes|lt[Y]|le/|lcno|ltN|le")
            else:
                editor.quit()
        elif cmd == ":q!":
            # force quit, not checking saving
            editor.quit()
        elif cmd == ":u":
            # undo
            editor.update_undo(-1)
        elif cmd == ":uu":
            # redo
            editor.update_undo(1)
        elif cmd == ":UU":
            # reset buffer
            editor.update_buffer(editor._pristine_buffer)
            caller.msg(
                "Reverted all changes to the buffer back to original state.")
        elif cmd == ":dd":
            # :dd <l> - delete line <l>
            buf = linebuffer[:lstart] + linebuffer[lend:]
            editor.update_buffer(buf)
            caller.msg("Deleted %s." % self.lstr)
        elif cmd == ":dw":
            # :dw <w> - delete word in entire buffer
            # :dw <l> <w> delete word only on line(s) <l>
            if not self.arg1:
                caller.msg("You must give a search word to delete.")
            else:
                if not self.linerange:
                    lstart = 0
                    lend = self.cline + 1
                    caller.msg("Removed %s for lines %i-%i." %
                               (self.arg1, lstart + 1, lend + 1))
                else:
                    caller.msg("Removed %s for %s." % (self.arg1, self.lstr))
                sarea = "\n".join(linebuffer[lstart:lend])
                sarea = re.sub(r"%s" % self.arg1.strip("\'").strip('\"'), "",
                               sarea, re.MULTILINE)
                buf = linebuffer[:lstart] + sarea.split(
                    "\n") + linebuffer[lend:]
                editor.update_buffer(buf)
        elif cmd == ":DD":
            # clear buffer
            editor.update_buffer("")

            # Reset indentation level to 0
            if editor._codefunc:
                if editor._indent >= 0:
                    editor._indent = 0
                    if editor._persistent:
                        caller.attributes.add("_eveditor_indent", 0)
            caller.msg("Cleared %i lines from buffer." % self.nlines)
        elif cmd == ":y":
            # :y <l> - yank line(s) to copy buffer
            cbuf = linebuffer[lstart:lend]
            editor._copy_buffer = cbuf
            caller.msg("%s, %s yanked." % (self.lstr.capitalize(), cbuf))
        elif cmd == ":x":
            # :x <l> - cut line to copy buffer
            cbuf = linebuffer[lstart:lend]
            editor._copy_buffer = cbuf
            buf = linebuffer[:lstart] + linebuffer[lend:]
            editor.update_buffer(buf)
            caller.msg("%s, %s cut." % (self.lstr.capitalize(), cbuf))
        elif cmd == ":p":
            # :p <l> paste line(s) from copy buffer
            if not editor._copy_buffer:
                caller.msg("Copy buffer is empty.")
            else:
                buf = linebuffer[:lstart] + editor._copy_buffer + linebuffer[
                    lstart:]
                editor.update_buffer(buf)
                caller.msg("Copied buffer %s to %s." %
                           (editor._copy_buffer, self.lstr))
        elif cmd == ":i":
            # :i <l> <txt> - insert new line
            new_lines = self.args.split('\n')
            if not new_lines:
                caller.msg(
                    "You need to enter a new line and where to insert it.")
            else:
                buf = linebuffer[:lstart] + new_lines + linebuffer[lstart:]
                editor.update_buffer(buf)
                caller.msg("Inserted %i new line(s) at %s." %
                           (len(new_lines), self.lstr))
        elif cmd == ":r":
            # :r <l> <txt> - replace lines
            new_lines = self.args.split('\n')
            if not new_lines:
                caller.msg("You need to enter a replacement string.")
            else:
                buf = linebuffer[:lstart] + new_lines + linebuffer[lend:]
                editor.update_buffer(buf)
                caller.msg("Replaced %i line(s) at %s." %
                           (len(new_lines), self.lstr))
        elif cmd == ":I":
            # :I <l> <txt> - insert text at beginning of line(s) <l>
            if not self.raw_string and not editor._codefunc:
                caller.msg("You need to enter text to insert.")
            else:
                buf = linebuffer[:lstart] + [
                    "%s%s" % (self.args, line)
                    for line in linebuffer[lstart:lend]
                ] + linebuffer[lend:]
                editor.update_buffer(buf)
                caller.msg("Inserted text at beginning of %s." % self.lstr)
        elif cmd == ":A":
            # :A <l> <txt> - append text after end of line(s)
            if not self.args:
                caller.msg("You need to enter text to append.")
            else:
                buf = linebuffer[:lstart] + [
                    "%s%s" % (line, self.args)
                    for line in linebuffer[lstart:lend]
                ] + linebuffer[lend:]
                editor.update_buffer(buf)
                caller.msg("Appended text to end of %s." % self.lstr)
        elif cmd == ":s":
            # :s <li> <w> <txt> - search and replace words
            # in entire buffer or on certain lines
            if not self.arg1 or not self.arg2:
                caller.msg(
                    "You must give a search word and something to replace it with."
                )
            else:
                if not self.linerange:
                    lstart = 0
                    lend = self.cline + 1
                    caller.msg("Search-replaced %s -> %s for lines %i-%i." %
                               (self.arg1, self.arg2, lstart + 1, lend))
                else:
                    caller.msg("Search-replaced %s -> %s for %s." %
                               (self.arg1, self.arg2, self.lstr))
                sarea = "\n".join(linebuffer[lstart:lend])

                regex = r"%s|^%s(?=\s)|(?<=\s)%s(?=\s)|^%s$|(?<=\s)%s$"
                regarg = self.arg1.strip("\'").strip('\"')
                if " " in regarg:
                    regarg = regarg.replace(" ", " +")
                sarea = re.sub(
                    regex % (regarg, regarg, regarg, regarg, regarg),
                    self.arg2.strip("\'").strip('\"'), sarea, re.MULTILINE)
                buf = linebuffer[:lstart] + sarea.split(
                    "\n") + linebuffer[lend:]
                editor.update_buffer(buf)
        elif cmd == ":f":
            # :f <l> flood-fill buffer or <l> lines of buffer.
            width = _DEFAULT_WIDTH
            if not self.linerange:
                lstart = 0
                lend = self.cline + 1
                caller.msg("Flood filled lines %i-%i." % (lstart + 1, lend))
            else:
                caller.msg("Flood filled %s." % self.lstr)
            fbuf = "\n".join(linebuffer[lstart:lend])
            fbuf = fill(fbuf, width=width)
            buf = linebuffer[:lstart] + fbuf.split("\n") + linebuffer[lend:]
            editor.update_buffer(buf)
        elif cmd == ":j":
            # :f <l> <w>  justify buffer of <l> with <w> as align (one of
            # f(ull), c(enter), r(ight) or l(left). Default is full.
            align_map = {
                "full": "f",
                "f": "f",
                "center": "c",
                "c": "c",
                "right": "r",
                "r": "r",
                "left": "l",
                "l": "l"
            }
            align_name = {
                "f": "Full",
                "c": "Center",
                "l": "Left",
                "r": "Right"
            }
            width = _DEFAULT_WIDTH
            if self.arg1 and self.arg1.lower() not in align_map:
                self.caller.msg(
                    "Valid justifications are [f]ull (default), [c]enter, [r]right or [l]eft"
                )
                return
            align = align_map[self.arg1.lower()] if self.arg1 else 'f'
            if not self.linerange:
                lstart = 0
                lend = self.cline + 1
                self.caller.msg("%s-justified lines %i-%i." %
                                (align_name[align], lstart + 1, lend))
            else:
                self.caller.msg("%s-justified %s." %
                                (align_name[align], self.lstr))
            jbuf = "\n".join(linebuffer[lstart:lend])
            jbuf = justify(jbuf, width=width, align=align)
            buf = linebuffer[:lstart] + jbuf.split("\n") + linebuffer[lend:]
            editor.update_buffer(buf)
        elif cmd == ":fi":
            # :fi <l> indent buffer or lines <l> of buffer.
            indent = " " * 4
            if not self.linerange:
                lstart = 0
                lend = self.cline + 1
                caller.msg("Indented lines %i-%i." % (lstart + 1, lend))
            else:
                caller.msg("Indented %s." % self.lstr)
            fbuf = [indent + line for line in linebuffer[lstart:lend]]
            buf = linebuffer[:lstart] + fbuf + linebuffer[lend:]
            editor.update_buffer(buf)
        elif cmd == ":fd":
            # :fi <l> indent buffer or lines <l> of buffer.
            if not self.linerange:
                lstart = 0
                lend = self.cline + 1
                caller.msg("Removed left margin (dedented) lines %i-%i." %
                           (lstart + 1, lend))
            else:
                caller.msg("Removed left margin (dedented) %s." % self.lstr)
            fbuf = "\n".join(linebuffer[lstart:lend])
            fbuf = dedent(fbuf)
            buf = linebuffer[:lstart] + fbuf.split("\n") + linebuffer[lend:]
            editor.update_buffer(buf)
        elif cmd == ":echo":
            # set echoing on/off
            editor._echo_mode = not editor._echo_mode
            caller.msg("Echo mode set to %s" % editor._echo_mode)
        elif cmd == ":!":
            if editor._codefunc:
                editor._codefunc(caller, editor._buffer)
            else:
                caller.msg(
                    "This command is only available in code editor mode.")
        elif cmd == ":<":
            # :<
            if editor._codefunc:
                editor.decrease_indent()
                indent = editor._indent
                if indent >= 0:
                    caller.msg(
                        "Decreased indentation: new indentation is {}.".format(
                            indent))
                else:
                    caller.msg(
                        "|rManual indentation is OFF.|n Use := to turn it on.")
            else:
                caller.msg(
                    "This command is only available in code editor mode.")
        elif cmd == ":>":
            # :>
            if editor._codefunc:
                editor.increase_indent()
                indent = editor._indent
                if indent >= 0:
                    caller.msg(
                        "Increased indentation: new indentation is {}.".format(
                            indent))
                else:
                    caller.msg(
                        "|rManual indentation is OFF.|n Use := to turn it on.")
            else:
                caller.msg(
                    "This command is only available in code editor mode.")
        elif cmd == ":=":
            # :=
            if editor._codefunc:
                editor.swap_autoindent()
                indent = editor._indent
                if indent >= 0:
                    caller.msg("Auto-indentation turned on.")
                else:
                    caller.msg("Auto-indentation turned off.")
            else:
                caller.msg(
                    "This command is only available in code editor mode.")
Beispiel #7
0
    def func(self):
        """
        This command handles all the in-editor :-style commands. Since
        each command is small and very limited, this makes for a more
        efficient presentation.
        """
        caller = self.caller
        editor = self.editor
        linebuffer = self.linebuffer
        lstart, lend = self.lstart, self.lend
        cmd = self.cmdstring
        echo_mode = self.editor._echo_mode

        if cmd == ":":
            # Echo buffer
            if self.linerange:
                buf = linebuffer[lstart:lend]
                editor.display_buffer(buf=buf, offset=lstart)
            else:
                editor.display_buffer()
        elif cmd == "::":
            # Echo buffer without the line numbers and syntax parsing
            if self.linerange:
                buf = linebuffer[lstart:lend]
                editor.display_buffer(buf=buf,
                                      offset=lstart,
                                      linenums=False, raw=True)
            else:
                editor.display_buffer(linenums=False, raw=True)
        elif cmd == ":::":
            # Insert single colon alone on a line
            editor.update_buffer(editor.buffer + "\n:")
            if echo_mode:
                caller.msg("Single ':' added to buffer.")
        elif cmd == ":h":
            # help entry
            editor.display_help()
        elif cmd == ":w":
            # save without quitting
            editor.save_buffer()
        elif cmd == ":wq":
            # save and quit
            editor.save_buffer()
            editor.quit()
        elif cmd == ":q":
            # quit. If not saved, will ask
            if self.editor._unsaved:
                caller.cmdset.add(SaveYesNoCmdSet)
                caller.msg("Save before quitting? {lcyes{lt[Y]{le/{lcno{ltN{le")
            else:
                editor.quit()
        elif cmd == ":q!":
            # force quit, not checking saving
            editor.quit()
        elif cmd == ":u":
            # undo
            editor.update_undo(-1)
        elif cmd == ":uu":
            # redo
            editor.update_undo(1)
        elif cmd == ":UU":
            # reset buffer
            editor.update_buffer(editor._pristine_buffer)
            caller.msg("Reverted all changes to the buffer back to original state.")
        elif cmd == ":dd":
            # :dd <l> - delete line <l>
            buf = linebuffer[:lstart] + linebuffer[lend:]
            editor.update_buffer(buf)
            caller.msg("Deleted %s." % (self.lstr))
        elif cmd == ":dw":
            # :dw <w> - delete word in entire buffer
            # :dw <l> <w> delete word only on line(s) <l>
            if not self.arg1:
                caller.msg("You must give a search word to delete.")
            else:
                if not self.linerange:
                    lstart = 0
                    lend = self.cline + 1
                    caller.msg("Removed %s for lines %i-%i." % (self.arg1, lstart + 1, lend + 1))
                else:
                    caller.msg("Removed %s for %s." % (self.arg1, self.lstr))
                sarea = "\n".join(linebuffer[lstart:lend])
                sarea = re.sub(r"%s" % self.arg1.strip("\'").strip('\"'), "", sarea, re.MULTILINE)
                buf = linebuffer[:lstart] + sarea.split("\n") + linebuffer[lend:]
                editor.update_buffer(buf)
        elif cmd == ":DD":
            # clear buffer
            editor.update_buffer("")
            caller.msg("Cleared %i lines from buffer." % self.nlines)
        elif cmd == ":y":
            # :y <l> - yank line(s) to copy buffer
            cbuf = linebuffer[lstart:lend]
            editor._copy_buffer = cbuf
            caller.msg("%s, %s yanked." % (self.lstr.capitalize(), cbuf))
        elif cmd == ":x":
            # :x <l> - cut line to copy buffer
            cbuf = linebuffer[lstart:lend]
            editor._copy_buffer = cbuf
            buf = linebuffer[:lstart] + linebuffer[lend:]
            editor.update_buffer(buf)
            caller.msg("%s, %s cut." % (self.lstr.capitalize(), cbuf))
        elif cmd == ":p":
            # :p <l> paste line(s) from copy buffer
            if not editor._copy_buffer:
                caller.msg("Copy buffer is empty.")
            else:
                buf = linebuffer[:lstart] + editor._copy_buffer + linebuffer[lstart:]
                editor.update_buffer(buf)
                caller.msg("Copied buffer %s to %s." % (editor._copy_buffer, self.lstr))
        elif cmd == ":i":
            # :i <l> <txt> - insert new line
            new_lines = self.args.split('\n')
            if not new_lines:
                caller.msg("You need to enter a new line and where to insert it.")
            else:
                buf = linebuffer[:lstart] + new_lines + linebuffer[lstart:]
                editor.update_buffer(buf)
                caller.msg("Inserted %i new line(s) at %s." % (len(new_lines), self.lstr))
        elif cmd == ":r":
            # :r <l> <txt> - replace lines
            new_lines = self.args.split('\n')
            if not new_lines:
                caller.msg("You need to enter a replacement string.")
            else:
                buf = linebuffer[:lstart] + new_lines + linebuffer[lend:]
                editor.update_buffer(buf)
                caller.msg("Replaced %i line(s) at %s." % (len(new_lines), self.lstr))
        elif cmd == ":I":
            # :I <l> <txt> - insert text at beginning of line(s) <l>
            if not self.args:
                caller.msg("You need to enter text to insert.")
            else:
                buf = linebuffer[:lstart] + ["%s%s" % (self.args, line) for line in linebuffer[lstart:lend]] + linebuffer[lend:]
                editor.update_buffer(buf)
                caller.msg("Inserted text at beginning of %s." % self.lstr)
        elif cmd == ":A":
            # :A <l> <txt> - append text after end of line(s)
            if not self.args:
                caller.msg("You need to enter text to append.")
            else:
                buf = linebuffer[:lstart] + ["%s%s" % (line, self.args) for line in linebuffer[lstart:lend]] + linebuffer[lend:]
                editor.update_buffer(buf)
                caller.msg("Appended text to end of %s." % self.lstr)
        elif cmd == ":s":
            # :s <li> <w> <txt> - search and replace words
            # in entire buffer or on certain lines
            if not self.arg1 or not self.arg2:
                caller.msg("You must give a search word and something to replace it with.")
            else:
                if not self.linerange:
                    lstart = 0
                    lend = self.cline + 1
                    caller.msg("Search-replaced %s -> %s for lines %i-%i." % (self.arg1, self.arg2, lstart + 1 , lend))
                else:
                    caller.msg("Search-replaced %s -> %s for %s." % (self.arg1, self.arg2, self.lstr))
                sarea = "\n".join(linebuffer[lstart:lend])

                regex = r"%s|^%s(?=\s)|(?<=\s)%s(?=\s)|^%s$|(?<=\s)%s$"
                regarg = self.arg1.strip("\'").strip('\"')
                if " " in regarg:
                    regarg = regarg.replace(" ", " +")
                sarea = re.sub(regex % (regarg, regarg, regarg, regarg, regarg), self.arg2.strip("\'").strip('\"'), sarea, re.MULTILINE)
                buf = linebuffer[:lstart] + sarea.split("\n") + linebuffer[lend:]
                editor.update_buffer(buf)
        elif cmd == ":f":
            # :f <l> flood-fill buffer or <l> lines of buffer.
            width = _DEFAULT_WIDTH
            if not self.linerange:
                lstart = 0
                lend = self.cline + 1
                caller.msg("Flood filled lines %i-%i." % (lstart + 1 , lend))
            else:
                caller.msg("Flood filled %s." % self.lstr)
            fbuf = "\n".join(linebuffer[lstart:lend])
            fbuf = fill(fbuf, width=width)
            buf = linebuffer[:lstart] + fbuf.split("\n") + linebuffer[lend:]
            editor.update_buffer(buf)
        elif cmd == ":fi":
            # :fi <l> indent buffer or lines <l> of buffer.
            indent = " " * 4
            if not self.linerange:
                lstart = 0
                lend = self.cline + 1
                caller.msg("Indented lines %i-%i." % (lstart + 1 , lend))
            else:
                caller.msg("Indented %s." % self.lstr)
            fbuf = [indent + line for line in linebuffer[lstart:lend]]
            buf = linebuffer[:lstart] + fbuf + linebuffer[lend:]
            editor.update_buffer(buf)
        elif cmd == ":fd":
            # :fi <l> indent buffer or lines <l> of buffer.
            if not self.linerange:
                lstart = 0
                lend = self.cline + 1
                caller.msg("Removed left margin (dedented) lines %i-%i." % (lstart + 1 , lend))
            else:
                caller.msg("Removed left margin (dedented) %s." % self.lstr)
            fbuf = "\n".join(linebuffer[lstart:lend])
            fbuf = dedent(fbuf)
            buf = linebuffer[:lstart] + fbuf.split("\n") + linebuffer[lend:]
            editor.update_buffer(buf)
        elif cmd == ":echo":
            # set echoing on/off
            editor._echo_mode = not editor._echo_mode
            caller.msg("Echo mode set to %s" % editor._echo_mode)
Beispiel #8
0
    def func(self):
        """
        This command handles all the in-editor :-style commands. Since
        each command is small and very limited, this makes for a more
        efficient presentation.
        """
        caller = self.caller
        editor = caller.ndb._eveditor

        linebuffer = self.linebuffer
        lstart, lend = self.lstart, self.lend
        cmd = self.cmdstring
        echo_mode = self.editor._echo_mode

        if cmd == ":":
            # Echo buffer
            if self.linerange:
                buf = linebuffer[lstart:lend]
                editor.display_buffer(buf=buf, offset=lstart)
            else:
                editor.display_buffer()
        elif cmd == "::":
            # Echo buffer without the line numbers and syntax parsing
            if self.linerange:
                buf = linebuffer[lstart:lend]
                editor.display_buffer(buf=buf,
                                      offset=lstart,
                                      linenums=False, options={"raw": True})
            else:
                editor.display_buffer(linenums=False, options={"raw": True})
        elif cmd == ":::":
            # Insert single colon alone on a line
            editor.update_buffer([":"] if lstart == 0 else linebuffer + [":"])
            if echo_mode:
                caller.msg("Single ':' added to buffer.")
        elif cmd == ":h":
            # help entry
            editor.display_help()
        elif cmd == ":w":
            # save without quitting
            editor.save_buffer()
        elif cmd == ":wq":
            # save and quit
            editor.save_buffer()
            editor.quit()
        elif cmd == ":q":
            # quit. If not saved, will ask
            if self.editor._unsaved:
                caller.cmdset.add(SaveYesNoCmdSet)
                caller.msg("Save before quitting? |lcyes|lt[Y]|le/|lcno|ltN|le")
            else:
                editor.quit()
        elif cmd == ":q!":
            # force quit, not checking saving
            editor.quit()
        elif cmd == ":u":
            # undo
            editor.update_undo(-1)
        elif cmd == ":uu":
            # redo
            editor.update_undo(1)
        elif cmd == ":UU":
            # reset buffer
            editor.update_buffer(editor._pristine_buffer)
            caller.msg("Reverted all changes to the buffer back to original state.")
        elif cmd == ":dd":
            # :dd <l> - delete line <l>
            buf = linebuffer[:lstart] + linebuffer[lend:]
            editor.update_buffer(buf)
            caller.msg("Deleted %s." % self.lstr)
        elif cmd == ":dw":
            # :dw <w> - delete word in entire buffer
            # :dw <l> <w> delete word only on line(s) <l>
            if not self.arg1:
                caller.msg("You must give a search word to delete.")
            else:
                if not self.linerange:
                    lstart = 0
                    lend = self.cline + 1
                    caller.msg("Removed %s for lines %i-%i." % (self.arg1, lstart + 1, lend + 1))
                else:
                    caller.msg("Removed %s for %s." % (self.arg1, self.lstr))
                sarea = "\n".join(linebuffer[lstart:lend])
                sarea = re.sub(r"%s" % self.arg1.strip("\'").strip('\"'), "", sarea, re.MULTILINE)
                buf = linebuffer[:lstart] + sarea.split("\n") + linebuffer[lend:]
                editor.update_buffer(buf)
        elif cmd == ":DD":
            # clear buffer
            editor.update_buffer("")

            # Reset indentation level to 0
            if editor._codefunc:
                if editor._indent >= 0:
                    editor._indent = 0
                    if editor._persistent:
                        caller.attributes.add("_eveditor_indent", 0)
            caller.msg("Cleared %i lines from buffer." % self.nlines)
        elif cmd == ":y":
            # :y <l> - yank line(s) to copy buffer
            cbuf = linebuffer[lstart:lend]
            editor._copy_buffer = cbuf
            caller.msg("%s, %s yanked." % (self.lstr.capitalize(), cbuf))
        elif cmd == ":x":
            # :x <l> - cut line to copy buffer
            cbuf = linebuffer[lstart:lend]
            editor._copy_buffer = cbuf
            buf = linebuffer[:lstart] + linebuffer[lend:]
            editor.update_buffer(buf)
            caller.msg("%s, %s cut." % (self.lstr.capitalize(), cbuf))
        elif cmd == ":p":
            # :p <l> paste line(s) from copy buffer
            if not editor._copy_buffer:
                caller.msg("Copy buffer is empty.")
            else:
                buf = linebuffer[:lstart] + editor._copy_buffer + linebuffer[lstart:]
                editor.update_buffer(buf)
                caller.msg("Copied buffer %s to %s." % (editor._copy_buffer, self.lstr))
        elif cmd == ":i":
            # :i <l> <txt> - insert new line
            new_lines = self.args.split('\n')
            if not new_lines:
                caller.msg("You need to enter a new line and where to insert it.")
            else:
                buf = linebuffer[:lstart] + new_lines + linebuffer[lstart:]
                editor.update_buffer(buf)
                caller.msg("Inserted %i new line(s) at %s." % (len(new_lines), self.lstr))
        elif cmd == ":r":
            # :r <l> <txt> - replace lines
            new_lines = self.args.split('\n')
            if not new_lines:
                caller.msg("You need to enter a replacement string.")
            else:
                buf = linebuffer[:lstart] + new_lines + linebuffer[lend:]
                editor.update_buffer(buf)
                caller.msg("Replaced %i line(s) at %s." % (len(new_lines), self.lstr))
        elif cmd == ":I":
            # :I <l> <txt> - insert text at beginning of line(s) <l>
            if not self.raw_string and not editor._codefunc:
                caller.msg("You need to enter text to insert.")
            else:
                buf = linebuffer[:lstart] + ["%s%s" % (self.args, line)
                                             for line in linebuffer[lstart:lend]] + linebuffer[lend:]
                editor.update_buffer(buf)
                caller.msg("Inserted text at beginning of %s." % self.lstr)
        elif cmd == ":A":
            # :A <l> <txt> - append text after end of line(s)
            if not self.args:
                caller.msg("You need to enter text to append.")
            else:
                buf = linebuffer[:lstart] + ["%s%s" % (line, self.args)
                                             for line in linebuffer[lstart:lend]] + linebuffer[lend:]
                editor.update_buffer(buf)
                caller.msg("Appended text to end of %s." % self.lstr)
        elif cmd == ":s":
            # :s <li> <w> <txt> - search and replace words
            # in entire buffer or on certain lines
            if not self.arg1 or not self.arg2:
                caller.msg("You must give a search word and something to replace it with.")
            else:
                if not self.linerange:
                    lstart = 0
                    lend = self.cline + 1
                    caller.msg("Search-replaced %s -> %s for lines %i-%i." % (self.arg1, self.arg2, lstart + 1, lend))
                else:
                    caller.msg("Search-replaced %s -> %s for %s." % (self.arg1, self.arg2, self.lstr))
                sarea = "\n".join(linebuffer[lstart:lend])

                regex = r"%s|^%s(?=\s)|(?<=\s)%s(?=\s)|^%s$|(?<=\s)%s$"
                regarg = self.arg1.strip("\'").strip('\"')
                if " " in regarg:
                    regarg = regarg.replace(" ", " +")
                sarea = re.sub(regex % (regarg, regarg, regarg, regarg, regarg), self.arg2.strip("\'").strip('\"'),
                               sarea, re.MULTILINE)
                buf = linebuffer[:lstart] + sarea.split("\n") + linebuffer[lend:]
                editor.update_buffer(buf)
        elif cmd == ":f":
            # :f <l> flood-fill buffer or <l> lines of buffer.
            width = _DEFAULT_WIDTH
            if not self.linerange:
                lstart = 0
                lend = self.cline + 1
                caller.msg("Flood filled lines %i-%i." % (lstart + 1, lend))
            else:
                caller.msg("Flood filled %s." % self.lstr)
            fbuf = "\n".join(linebuffer[lstart:lend])
            fbuf = fill(fbuf, width=width)
            buf = linebuffer[:lstart] + fbuf.split("\n") + linebuffer[lend:]
            editor.update_buffer(buf)
        elif cmd == ":j":
            # :f <l> <w>  justify buffer of <l> with <w> as align (one of
            # f(ull), c(enter), r(ight) or l(left). Default is full.
            align_map = {"full": "f", "f": "f", "center": "c", "c": "c",
                         "right": "r", "r": "r", "left": "l", "l": "l"}
            align_name = {"f": "Full", "c": "Center", "l": "Left", "r": "Right"}
            width = _DEFAULT_WIDTH
            if self.arg1 and self.arg1.lower() not in align_map:
                self.caller.msg("Valid justifications are [f]ull (default), [c]enter, [r]right or [l]eft")
                return
            align = align_map[self.arg1.lower()] if self.arg1 else 'f'
            if not self.linerange:
                lstart = 0
                lend = self.cline + 1
                self.caller.msg("%s-justified lines %i-%i." % (align_name[align], lstart + 1, lend))
            else:
                self.caller.msg("%s-justified %s." % (align_name[align], self.lstr))
            jbuf = "\n".join(linebuffer[lstart:lend])
            jbuf = justify(jbuf, width=width, align=align)
            buf = linebuffer[:lstart] + jbuf.split("\n") + linebuffer[lend:]
            editor.update_buffer(buf)
        elif cmd == ":fi":
            # :fi <l> indent buffer or lines <l> of buffer.
            indent = " " * 4
            if not self.linerange:
                lstart = 0
                lend = self.cline + 1
                caller.msg("Indented lines %i-%i." % (lstart + 1, lend))
            else:
                caller.msg("Indented %s." % self.lstr)
            fbuf = [indent + line for line in linebuffer[lstart:lend]]
            buf = linebuffer[:lstart] + fbuf + linebuffer[lend:]
            editor.update_buffer(buf)
        elif cmd == ":fd":
            # :fi <l> indent buffer or lines <l> of buffer.
            if not self.linerange:
                lstart = 0
                lend = self.cline + 1
                caller.msg("Removed left margin (dedented) lines %i-%i." % (lstart + 1, lend))
            else:
                caller.msg("Removed left margin (dedented) %s." % self.lstr)
            fbuf = "\n".join(linebuffer[lstart:lend])
            fbuf = dedent(fbuf)
            buf = linebuffer[:lstart] + fbuf.split("\n") + linebuffer[lend:]
            editor.update_buffer(buf)
        elif cmd == ":echo":
            # set echoing on/off
            editor._echo_mode = not editor._echo_mode
            caller.msg("Echo mode set to %s" % editor._echo_mode)
        elif cmd == ":!":
            if editor._codefunc:
                editor._codefunc(caller, editor._buffer)
            else:
                caller.msg("This command is only available in code editor mode.")
        elif cmd == ":<":
            # :<
            if editor._codefunc:
                editor.decrease_indent()
                indent = editor._indent
                if indent >= 0:
                    caller.msg("Decreased indentation: new indentation is {}.".format(
                        indent))
                else:
                    caller.msg("|rManual indentation is OFF.|n Use := to turn it on.")
            else:
                caller.msg("This command is only available in code editor mode.")
        elif cmd == ":>":
            # :>
            if editor._codefunc:
                editor.increase_indent()
                indent = editor._indent
                if indent >= 0:
                    caller.msg("Increased indentation: new indentation is {}.".format(
                        indent))
                else:
                    caller.msg("|rManual indentation is OFF.|n Use := to turn it on.")
            else:
                caller.msg("This command is only available in code editor mode.")
        elif cmd == ":=":
            # :=
            if editor._codefunc:
                editor.swap_autoindent()
                indent = editor._indent
                if indent >= 0:
                    caller.msg("Auto-indentation turned on.")
                else:
                    caller.msg("Auto-indentation turned off.")
            else:
                caller.msg("This command is only available in code editor mode.")