'This command handles all the in-editor :-style commands. Since
each command is small and very limited, this makes for a more
efficient presentation.'

def func(self):

    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 == ':'):
        if self.linerange:
            buf = linebuffer[lstart:lend]
            editor.display_buffer(buf=buf, offset=lstart)
        else:
            editor.display_buffer()
    elif (cmd == '::'):
        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 == ':::'):
        editor.update_buffer(([':'] if (lstart == 0) else (linebuffer + [':'])))
        if echo_mode:
            caller.msg("Single    ':'    added    to    buffer.")
    elif (cmd == ':h'):
        editor.display_help()
    elif (cmd == ':w'):
        editor.save_buffer()
    elif (cmd == ':wq'):
        editor.save_buffer()
        editor.quit()
    elif (cmd == ':q'):
        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!'):
        editor.quit()
    elif (cmd == ':u'):
        editor.update_undo((-1))
    elif (cmd == ':uu'):
        editor.update_undo(1)
    elif (cmd == ':UU'):
        editor.update_buffer(editor._pristine_buffer)
        caller.msg('Reverted    all    changes    to    the    buffer    back    to    original    state.')
    elif (cmd == ':dd'):
        buf = (linebuffer[:lstart] + linebuffer[lend:])
        editor.update_buffer(buf)
        caller.msg(('Deleted    %s.' % self.lstr))
    elif (cmd == ':dw'):
        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(('%s' % self.arg1.strip("'").strip('"')), '', sarea, re.MULTILINE)
            buf = ((linebuffer[:lstart] + sarea.split('\n')) + linebuffer[lend:])
            editor.update_buffer(buf)
    elif (cmd == ':DD'):
        editor.update_buffer('')
        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'):
        cbuf = linebuffer[lstart:lend]
        editor._copy_buffer = cbuf
        caller.msg(('%s,    %s    yanked.' % (self.lstr.capitalize(), cbuf)))
    elif (cmd == ':x'):
        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'):
        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'):
        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'):
        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'):
        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'):
        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'):
        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 = '%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'):
        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'):
        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'):
        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'):
        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'):
        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.')
