'Saves or updates macro or other definitions in .pronsolerc
key is prefix that determines what is being defined/updated (e.g. \'macro foo\')
definition is the full definition (that is written to file). (e.g. \'macro foo move x 10\')
Set key as empty string to just add (and not overwrite)
Set definition as empty string to remove it from .pronsolerc
To delete line from .pronsolerc, set key as the line contents, and definition as empty string
Only first definition with given key is overwritten.
Updates are made in the same file position.
Additions are made to the end of the file.'

def save_in_rc(self, key, definition):

    (rci, rco) = (None, None)
    if ((definition != '') and (not definition.endswith('\n'))):
        definition += '\n'
    try:
        written = False
        if os.path.exists(self.rc_filename):
            shutil.copy(self.rc_filename, (self.rc_filename + '~bak'))
            rci = codecs.open((self.rc_filename + '~bak'), 'r', 'utf-8')
        rco = codecs.open((self.rc_filename + '~new'), 'w', 'utf-8')
        if (rci is not None):
            overwriting = False
            for rc_cmd in rci:
                l = rc_cmd.rstrip()
                ls = l.lstrip()
                ws = l[:(len(l) - len(ls))]
                if (overwriting and (len(ws) == 0)):
                    overwriting = False
                if ((not written) and (key != '') and rc_cmd.startswith(key) and (rc_cmd + '\n')[len(key)].isspace()):
                    overwriting = True
                    written = True
                    rco.write(definition)
                if (not overwriting):
                    rco.write(rc_cmd)
                    if (not rc_cmd.endswith('\n')):
                        rco.write('\n')
        if (not written):
            rco.write(definition)
        if (rci is not None):
            rci.close()
        rco.close()
        shutil.move((self.rc_filename + '~new'), self.rc_filename)
    except Exception as e:
        self.logError('Saving    failed    for    ', (key + ':'), str(e))
    finally:
        del rci, rco
