]> git.lyx.org Git - lyx.git/blob - lib/scripts/prefs2prefs_lfuns.py
Revert "DocBook: add new layout parameter DocBookWrapperMergeWithPrevious."
[lyx.git] / lib / scripts / prefs2prefs_lfuns.py
1 # -*- coding: utf-8 -*-
2
3 # file prefs2prefs-lfuns.py
4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
6
7 # author Richard Heck
8
9 # Full author contact details are available in file CREDITS
10
11 # This file houses conversion information for the bind and ui files,
12 # i.e., for files where we are converting lfuns.
13
14 # The converter functions take a line as argument and return a list: 
15 #       (Bool, NewLine), 
16 # where the Bool says if  we've modified anything and the NewLine is 
17 # the new line, if so, which will be used to replace the old line.
18
19
20 import sys, re
21
22 ###########################################################
23 #
24 # Actual converter functions
25 #
26 # These accept a line as argument and should return a list:
27 #  (bool, newline)
28 # where the bool indicates whether we changed anything. If not,
29 # one normally returns: (False, "").
30
31 no_match = (False, "")
32
33 def simple_renaming(line, old, new):
34         if line.find(old) == -1:
35                 return no_match
36         line = line.replace(old, new)
37         return (True, line)
38
39
40 def simple_remove(line, old):
41         if line.find(old) == -1:
42                 return no_match
43         return (True, "")
44
45
46 def next_inset_modify(line):
47         return simple_renaming(line, "next-inset-modify", "inset-modify")
48
49
50 def next_inset_toggle(line):
51         return simple_renaming(line, "next-inset-toggle", "inset-toggle")
52
53
54 def optional_insert(line):
55         return simple_renaming(line, "optional-insert", "argument-insert")
56
57
58 re_nm = re.compile(r'^(.*)notes-mutate\s+(\w+)\s+(\w+)(.*)$')
59 def notes_mutate(line):
60         m = re_nm.search(line)
61         if not m:
62                 return no_match
63
64         prefix = m.group(1)
65         source = m.group(2)
66         target = m.group(3)
67         suffix = m.group(4)
68         newline = prefix + "inset-forall Note:" + source + \
69                 " inset-modify note Note " + target + suffix
70         return (True, newline)
71
72
73 re_ait = re.compile(r'^(.*)all-insets-toggle\s+(\w+)(?:\s+(\w+))?(.*)$')
74 def all_insets_toggle(line):
75         m = re_ait.search(line)
76         if not m:
77                 return no_match
78
79         prefix = m.group(1)
80         action = m.group(2)
81         target = m.group(3) 
82         suffix = m.group(4)
83
84         # we need to transform the target to match the inset layout names
85         # this will not be perfect
86         if target == "ert":
87                 target = "ERT"
88         elif target == None:
89                 target = "*"
90         elif target == "tabular":
91                 # There does not seem to be an InsetLayout for tables, so
92                 # I do not know what to do here. If anyone does, then please
93                 # fix this. For now, we just have to remove this line.
94                 return (True, "")
95         else:
96                 target = target.capitalize()
97         
98         newline = prefix + "inset-forall " + target + " inset-toggle " + \
99                 action + suffix
100         return (True, newline)
101
102
103 re_li = re.compile(r'^(.*)\bline-insert\b(.*)$')
104 def line_insert(line):
105         m = re_li.search(line)
106         if not m: 
107                 return no_match
108         newline = m.group(1) + \
109                 "inset-insert line rule height 0.25ex width 100col% \\end_inset" + \
110                 m.group(2)
111         return (True, newline)
112
113
114 def toc_insert(line):
115         return simple_renaming(line, "toc-insert", "inset-insert toc")
116
117
118 re_ps = re.compile(r'^(.*)paragraph-spacing\s+(default|single|onehalf|double)\b(.*)$')
119 re_psother = re.compile(r'^(.*)paragraph-spacing\s+other\s+(\d+\.\d?|\d?\.\d+|\d+)(.*)$')
120 def paragraph_spacing(line):
121         # possible args: default, single, onehalf, double, other FLOAT
122         m = re_ps.search(line)
123         if m:
124                 arg = m.group(2)
125                 newline = m.group(1) + "paragraph-params \\paragraph-spacing " + arg + \
126                         m.group(3)
127                 return (True, newline)
128
129         m = re_psother.search(line)
130         if not m:
131                 return no_match
132
133         arg = m.group(2)
134         newline = m.group(1) + "paragraph-params \\paragraph-spacing other " + \
135                 arg + m.group(3)
136         return (True, newline)
137
138
139 def tabular_feature(line):
140         return simple_renaming(line, "tabular-feature", "inset-modify tabular")
141
142
143 re_tabular_feature = re.compile(r"inset-modify\s+tabular(\s+from-dialog)?")
144 def redo_tabular_feature(line):
145         # we change as follows:
146         # inset-modify tabular -> tabular-feature
147         # but:
148         # inset-modify tabular from-dialog -> inset-modify tabular
149         #
150         # "from-dialog" was never used directly but the user might do, if they
151         # followed the standard instructions to create a custom shortcut by looking
152         # at the message panel. The equivalent functionality is now provided by
153         # inset-modify tabular (without from-dialog).
154         def change(match):
155                 if match.group(1):
156                         return "inset-modify tabular"
157                 else:
158                         return "tabular-feature"
159
160         result = re_tabular_feature.subn(change, line)
161         if result[1]:
162                 return (True, result[0])
163         else:
164                 return no_match
165
166
167 re_Bar2bar = re.compile(r'^(\\(?:bind|unbind))\s+"([^"]*)Bar"(\s+"[^"]+")')
168 def Bar2bar(line):
169         m = re_Bar2bar.search(line)
170         if not m:
171                 return no_match
172
173         btype = m.group(1)
174         mod = m.group(2)
175         rest = m.group(3)
176         newline = btype + " \"" + mod + "bar\"" + rest
177         return (True, newline)
178
179
180 def paragraph_break(line):
181         return simple_renaming(line, "break-paragraph", "paragraph-break")
182
183
184 def tab_group_close(line):
185         return simple_renaming(line, "close-tab-group", "tab-group-close")
186
187
188 def view_split(line):
189         return simple_renaming(line, "split-view", "view-split")
190
191
192 def label_copy_as_reference(line):
193         return simple_renaming(line, "copy-label-as-reference", "label-copy-as-reference")
194
195
196 def remove_print_support(line):
197         return simple_remove(line, "dialog-show print")
198
199
200 def info_rename_vcsauthor(line):
201         return simple_renaming(line, "info-insert buffer vcs-author", "info-insert vcs author")
202
203
204 def info_rename_vcsdate(line):
205         return simple_renaming(line, "info-insert buffer vcs-date", "info-insert vcs date")
206
207
208 def info_rename_vcstime(line):
209         return simple_renaming(line, "info-insert buffer vcs-time", "info-insert vcs time")
210
211
212 def info_rename_vcsrevision(line):
213         return simple_renaming(line, "info-insert buffer vcs-revision", "info-insert vcs revision")
214
215
216 def info_rename_vcstreerevision(line):
217         return simple_renaming(line, "info-insert buffer vcs-tree-revision", "info-insert vcs tree-revision")
218
219
220 def remove_date_insert(line):
221         return simple_remove(line, "date-insert")
222
223
224 #
225 ###########################################################
226
227
228 # Conversion chain
229
230 conversions = [
231         [  1, [ # this will be a long list of conversions to format 1, LyX 2.0
232                 next_inset_toggle,
233                 next_inset_modify,
234                 optional_insert,
235                 notes_mutate,
236                 all_insets_toggle,
237                 line_insert,
238                 toc_insert,
239                 paragraph_spacing,
240                 tabular_feature,
241                 Bar2bar
242         ]],
243         [  2, [ # list of conversions to format 2, LyX 2.1
244                 paragraph_break,
245                 tab_group_close,
246                 view_split,
247                 label_copy_as_reference
248         ]],
249         [ 3, [ # list of conversions to format 3
250                 remove_print_support
251         ]],
252         [ 4, [ # list of conversions to format 4, LyX 2.2
253                 redo_tabular_feature
254         ]],
255         [ 5, [ # list of conversions to format 5, LyX 2.4
256                 info_rename_vcsauthor,
257                 info_rename_vcsdate,
258                 info_rename_vcstime,
259                 info_rename_vcsrevision,
260                 info_rename_vcstreerevision,
261                 remove_date_insert
262         ]]
263 ]