]> git.lyx.org Git - lyx.git/blob - lib/scripts/prefs2prefs_lfuns.py
Don't use widest label for numerical citations.
[lyx.git] / lib / scripts / prefs2prefs_lfuns.py
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # file prefs2prefs-lfuns.py
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
7
8 # author Richard Heck
9
10 # Full author contact details are available in file CREDITS
11
12 # This file houses conversion information for the bind and ui files,
13 # i.e., for files where we are converting lfuns.
14
15 # The converter functions take a line as argument and return a list: 
16 #       (Bool, NewLine), 
17 # where the Bool says if  we've modified anything and the NewLine is 
18 # the new line, if so, which will be used to replace the old line.
19
20
21 import sys, re
22
23 ###########################################################
24 #
25 # Actual converter functions
26 #
27 # These accept a line as argument and should return a list:
28 #  (bool, newline)
29 # where the bool indicates whether we changed anything. If not,
30 # one normally returns: (False, []).
31
32 no_match = (False, [])
33
34 def simple_renaming(line, old, new):
35         if line.find(old) == -1:
36                 return no_match
37         line = line.replace(old, new)
38         return (True, line)
39
40
41 def next_inset_modify(line):
42         return simple_renaming(line, "next-inset-modify", "inset-modify")
43
44
45 def next_inset_toggle(line):
46         return simple_renaming(line, "next-inset-toggle", "inset-toggle")
47
48
49 def optional_insert(line):
50         return simple_renaming(line, "optional-insert", "argument-insert")
51
52
53 re_nm = re.compile(r'^(.*)notes-mutate\s+(\w+)\s+(\w+)(.*)$')
54 def notes_mutate(line):
55         m = re_nm.search(line)
56         if not m:
57                 return no_match
58
59         prefix = m.group(1)
60         source = m.group(2)
61         target = m.group(3)
62         suffix = m.group(4)
63         newline = prefix + "inset-forall Note:" + source + \
64                 " inset-modify note Note " + target + suffix
65         return (True, newline)
66
67
68 re_ait = re.compile(r'^(.*)all-insets-toggle\s+(\w+)(?:\s+(\w+))?(.*)$')
69 def all_insets_toggle(line):
70         m = re_ait.search(line)
71         if not m:
72                 return no_match
73
74         prefix = m.group(1)
75         action = m.group(2)
76         target = m.group(3) 
77         suffix = m.group(4)
78
79         # we need to transform the target to match the inset layout names
80         # this will not be perfect
81         if target == "ert":
82                 target = "ERT"
83         elif target == None:
84                 target = "*"
85         elif target == "tabular":
86                 # There does not seem to be an InsetLayout for tables, so
87                 # I do not know what to do here. If anyone does, then please
88                 # fix this. For now, we just have to remove this line.
89                 return (True, "")
90         else:
91                 target = target.capitalize()
92         
93         newline = prefix + "inset-forall " + target + " inset-toggle " + \
94                 action + suffix
95         return (True, newline)
96
97
98 re_li = re.compile(r'^(.*)\bline-insert\b(.*)$')
99 def line_insert(line):
100         m = re_li.search(line)
101         if not m: 
102                 return no_match
103         newline = m.group(1) + \
104                 "inset-insert line rule height 0.25ex width 100col% \\end_inset" + \
105                 m.group(2)
106         return (True, newline)
107
108
109 def toc_insert(line):
110         return simple_renaming(line, "toc-insert", "inset-insert toc")
111
112
113 re_ps = re.compile(r'^(.*)paragraph-spacing\s+(default|single|onehalf|double)\b(.*)$')
114 re_psother = re.compile(r'^(.*)paragraph-spacing\s+other\s+(\d+\.\d?|\d?\.\d+|\d+)(.*)$')
115 def paragraph_spacing(line):
116         # possible args: default, single, onehalf, double, other FLOAT
117         m = re_ps.search(line)
118         if m:
119                 arg = m.group(2)
120                 newline = m.group(1) + "paragraph-params \\paragraph-spacing " + arg + \
121                         m.group(3)
122                 return (True, newline)
123
124         m = re_psother.search(line)
125         if not m:
126                 return no_match
127
128         arg = m.group(2)
129         newline = m.group(1) + "paragraph-params \\paragraph-spacing other " + \
130                 arg + m.group(3)
131         return (True, newline)
132
133
134 def tabular_feature(line):
135         return simple_renaming(line, "tabular-feature", "inset-modify tabular")
136
137 re_Bar2bar = re.compile(r'^(\\(?:bind|unbind))\s+"([^"]*)Bar"(\s+"[^"]+")')
138 def Bar2bar(line):
139         m = re_Bar2bar.search(line)
140         if not m:
141                 return no_match
142
143         btype = m.group(1)
144         mod = m.group(2)
145         rest = m.group(3)
146         newline = btype + " \"" + mod + "bar\"" + rest
147         return (True, newline)
148
149 def paragraph_break(line):
150         return simple_renaming(line, "break-paragraph", "paragraph-break")
151
152 def tab_group_close(line):
153         return simple_renaming(line, "close-tab-group", "tab-group-close")
154
155 def view_split(line):
156         return simple_renaming(line, "split-view", "view-split")
157
158 #
159 #
160 ###########################################################
161
162
163 # Conversion chain
164
165 conversions = [
166         [  1, [ # this will be a long list of conversions to format 1, LyX 2.0
167                 next_inset_toggle,
168                 next_inset_modify,
169                 optional_insert,
170                 notes_mutate,
171                 all_insets_toggle,
172                 line_insert,
173                 toc_insert,
174                 paragraph_spacing,
175                 tabular_feature,
176                 Bar2bar
177         ]],
178         [  2, [ # list of conversions to format 2, LyX 2.1
179                 paragraph_break,
180                 tab_group_close,
181                 view_split
182         ]],
183 ]
184