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