commit 9a1b26a156c913f484ca2293fb2ec1c4986d2a3e Author: Richard Kimberly Heck <rikih...@lyx.org> Date: Thu Apr 30 21:57:50 2020 -0400
Format and reversion code for new counter inset. --- lib/lyx2lyx/lyx_2_4.py | 78 +++++++++++++++++++++++++++++++++++++++++- lib/lyx2lyx/parser_tools.py | 1 + src/version.h | 4 +- 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/lib/lyx2lyx/lyx_2_4.py b/lib/lyx2lyx/lyx_2_4.py index 1b4c940..98173df 100644 --- a/lib/lyx2lyx/lyx_2_4.py +++ b/lib/lyx2lyx/lyx_2_4.py @@ -3687,6 +3687,78 @@ def revert_counter_maintenance(document): else: document.header[i] = "\\maintain_unincluded_children true" + +def revert_counter_inset(document): + " Revert counter inset to ERT, where possible" + i = 0 + needed_counters = {} + while True: + i = find_token(document.body, "\\begin_inset CommandInset counter", i) + if i == -1: + break + j = find_end_of_inset(document.body, i) + if j == -1: + document.warning("Can't find end of counter inset at line %d!" % i) + i += 1 + continue + lyx = get_quoted_value(document.body, "lyxonly", i, j) + if lyx == "true": + # there is nothing we can do to affect the LyX counters + document.body[i : j + 1] = [] + i = j + 1 + continue + cnt = get_quoted_value(document.body, "counter", i, j) + if not cnt: + document.warning("No counter given for inset at line %d!" % i) + i = j + 1 + continue + + cmd = get_quoted_value(document.body, "LatexCommand", i, j) + document.warning(cmd) + ert = "" + if cmd == "set": + val = get_quoted_value(document.body, "value", i, j) + if not val: + document.warning("Can't convert counter inset at line %d!" % i) + else: + ert = put_cmd_in_ert("\\setcounter{%s}{%s}" % (cnt, val)) + elif cmd == "addto": + val = get_quoted_value(document.body, "value", i, j) + if not val: + document.warning("Can't convert counter inset at line %d!" % i) + else: + ert = put_cmd_in_ert("\\addtocounter{%s}{%s}" % (cnt, val)) + elif cmd == "reset": + ert = put_cmd_in_ert("\\setcounter{%s}{0}" % (cnt)) + elif cmd == "value": + vty = get_quoted_value(document.body, "vtype", i, j) + if not vty: + document.warning("Can't convert counter inset at line %d!" % i) + else: + ert = put_cmd_in_ert("\\%s{%s}" % (vty, cnt)) + elif cmd == "save": + needed_counters[cnt] = 1 + savecnt = "LyXSave" + cnt + ert = put_cmd_in_ert("\\setcounter{%s}{\\value{%s}}" % (savecnt, cnt)) + elif cmd == "restore": + needed_counters[cnt] = 1 + savecnt = "LyXSave" + cnt + ert = put_cmd_in_ert("\\setcounter{%s}{\\value{%s}}" % (cnt, savecnt)) + else: + document.warning("Unknown counter command `%s' in inset at line %d!" % (cnt, i)) + + if ert: + document.body[i : j + 1] = ert + i += 1 + continue + + pretext = [] + for cnt in needed_counters: + pretext.append("\\newcounter{LyXSave%s}" % (cnt)) + if pretext: + add_to_preamble(document, pretext) + + ## # Conversion hub # @@ -3741,10 +3813,12 @@ convert = [ [590, [convert_changebars]], [591, [convert_postpone_fragile]], [592, []], - [593, [convert_counter_maintenance]] + [593, [convert_counter_maintenance]], + [594, []] ] -revert = [[592, [revert_counter_maintenance]], +revert = [[593, [revert_counter_inset]], + [592, [revert_counter_maintenance]], [591, [revert_colrow_tracking]], [590, [revert_postpone_fragile]], [589, [revert_changebars]], diff --git a/lib/lyx2lyx/parser_tools.py b/lib/lyx2lyx/parser_tools.py index 175db00..44131e5 100644 --- a/lib/lyx2lyx/parser_tools.py +++ b/lib/lyx2lyx/parser_tools.py @@ -66,6 +66,7 @@ get_value(lines, token[, start[, end[, default[, delete]]]]): is stripped.) The final argument, default, defaults to "", and is what is returned if we do not find anything. So you can use that to set a default. + If delete is True, then delete the line if found. get_quoted_value(lines, token[, start[, end[, default[, delete]]]]): Similar to get_value, but it will strip quotes off the diff --git a/src/version.h b/src/version.h index a664926..866fe10 100644 --- a/src/version.h +++ b/src/version.h @@ -32,8 +32,8 @@ extern char const * const lyx_version_info; // Do not remove the comment below, so we get merge conflict in // independent branches. Instead add your own. -#define LYX_FORMAT_LYX 593 // spitz: maintain counter/references -#define LYX_FORMAT_TEX2LYX 593 +#define LYX_FORMAT_LYX 594 // rikiheck: counter insets +#define LYX_FORMAT_TEX2LYX 594 #if LYX_FORMAT_TEX2LYX != LYX_FORMAT_LYX #ifndef _MSC_VER -- lyx-cvs mailing list lyx-cvs@lists.lyx.org http://lists.lyx.org/mailman/listinfo/lyx-cvs