]> git.lyx.org Git - lyx.git/blob - lib/scripts/prefs2prefs_prefs.py
Pass parameters by reference (performance)
[lyx.git] / lib / scripts / prefs2prefs_prefs.py
1 # -*- coding: utf-8 -*-
2
3 # file prefs2prefs-prefs.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 preferences file.
12
13 # The converter functions take a line as argument and return a list: 
14 #       (Bool, NewLine), 
15 # where the Bool says if  we've modified anything and the NewLine is 
16 # the new line, if so, which will be used to replace the old line.
17
18 # Incremented to format 2, r39670 by jrioux
19 #   Support for multiple file extensions per format.
20 #   No conversion necessary.
21
22 # Incremented to format 3, r39705 by tommaso
23 #   Support for file formats that are natively (g)zipped.
24 #   We must add the flag zipped=native to formats that
25 #   were previously hardcoded in the C++ source: dia.
26
27 # Incremented to format 4, r40028 by vfr
28 #   Remove support for default paper size.
29
30 # Incremented to format 5, r40030 by vfr
31 #   Add a default length unit.
32 #   No conversion necessary.
33
34 # Incremented to format 6, r40515 by younes
35 #   Add use_qimage option.
36 #   No conversion necessary.
37
38 # Incremented to format 7, r40789 by gb
39 #   Add mime type to file format
40
41 # Incremented to format 8, 288c1e0f by rgh
42 #   Add "nice" flag for converters
43 #   No conversion necessary.
44
45 # Incremented to format 9, a18af620 by spitz
46 #  Remove default_language rc.
47
48 # Incremented to format 10, 4985015 by tommaso
49 #  Add close_buffer_with_last_view in preferences.
50 #  No conversion necessary.
51
52 # Incremented to format 11, by gb
53 #   Split pdf format into pdf and pdf6
54
55 # Incremented to format 12, by vfr
56 #   Add option to use the system's theme icons
57 #   No conversion necessary.
58
59 # Incremented to format 13, by bh
60 #   Rename mac_like_word_movement to mac_like_cursor_movement
61
62 # Incremented to format 14, by spitz
63 #   New RC default_otf_view_format
64 #   No conversion necessary.
65
66 # Incremented to format 15, by prannoy
67 #   Add fullscreen_statusbar
68 #   No conversion necessary.
69
70 # Incremented to format 16, by lasgouttes
71 #  Remove force_paint_single_char rc.
72
73 # Incremented to format 17, by lasgouttes
74 #  Remove rtl_support rc.
75
76 # NOTE: The format should also be updated in LYXRC.cpp and
77 # in configure.py.
78
79 import re
80
81 ###########################################################
82 #
83 # Conversion chain
84
85 def get_format(line):
86         entries = []
87         i = 0
88         while i < len(line):
89                 if line[i] == '"':
90                         beg = i + 1
91                         i = i + 1
92                         while i < len(line) and line[i] != '"':
93                                 if line[i] == '\\' and i < len(line) - 1 and line[i+1] == '"':
94                                         # convert \" to "
95                                         i = i + 1
96                                 i = i + 1
97                         end = i
98                         entries.append(line[beg:end].replace('\\"', '"'))
99                 elif line[i] == '#':
100                         return entries
101                 elif not line[i].isspace():
102                         beg = i
103                         while i < len(line) and not line[i].isspace():
104                                 i = i + 1
105                         end = i
106                         entries.append(line[beg:end])
107                 i = i + 1
108         return entries
109
110
111 def simple_renaming(line, old, new):
112         i = line.lower().find(old.lower())
113         if i == -1:
114                 return no_match
115         line = line[:i] + new + line[i+len(old):]
116         return (True, line)
117
118 no_match = (False, [])
119
120 ######################################
121 ### Format 1 conversions (for LyX 2.0)
122
123 def remove_obsolete(line):
124         tags = ("\\use_tempdir", "\\spell_command", "\\personal_dictionary",
125                                 "\\plaintext_roff_command", "\\use_alt_language", 
126                                 "\\use_escape_chars", "\\use_input_encoding",
127                                 "\\use_personal_dictionary", "\\use_pspell",
128                                 "\\use_spell_lib")
129         line = line.lower().lstrip()
130         for tag in tags:
131                 if line.lower().startswith(tag):
132                         return (True, "")
133         return no_match
134
135
136 def language_use_babel(line):
137         if not line.lower().startswith("\language_use_babel"):
138                 return no_match
139         re_lub = re.compile(r'^\\language_use_babel\s+"?(true|false)', re.IGNORECASE)
140         m = re_lub.match(line)
141         val = m.group(1)
142         newval = '0'
143         if val == 'false':
144                 newval = '3'
145         newline = "\\language_package_selection " + newval
146         return (True, newline)
147
148
149 def language_package(line):
150         return simple_renaming(line, "\\language_package", "\\language_custom_package")
151
152
153 lfre = re.compile(r'^\\converter\s+"?(\w+)"?\s+"?(\w+)"?\s+"([^"]*?)"\s+"latex"', re.IGNORECASE)
154 def latex_flavor(line):
155         if not line.lower().startswith("\\converter"):
156                 return no_match
157         m = lfre.match(line)
158         if not m:
159                 return no_match
160         conv = m.group(1)
161         fmat = m.group(2)
162         args = m.group(3)
163         conv2fl = {
164                    "luatex":   "lualatex",
165                    "pplatex":  "latex",
166                    "xetex":    "xelatex",
167                   }
168         if conv in conv2fl.keys():
169                 flavor = conv2fl[conv]
170         else:
171                 flavor = conv
172         if flavor == "latex":
173                 return no_match
174         return (True,
175                 "\\converter \"%s\" \"%s\" \"%s\" \"latex=%s\"" % (conv, fmat, args, flavor))
176
177
178 emre = re.compile(r'^\\format\s+(.*)\s+"(document[^"]*?)"', re.IGNORECASE)
179 def export_menu(line):
180         if not line.lower().startswith("\\format"):
181                 return no_match
182         m = emre.match(line)
183         if not m:
184                 return no_match
185         fmat = m.group(1)
186         opts = m.group(2)
187         return (True,
188                 "\\Format %s \"%s,menu=export\"" % (fmat, opts))
189
190 # End format 1 conversions (for LyX 2.0)
191 ########################################
192
193 #################################
194 # Conversions from LyX 2.0 to 2.1
195 zipre = re.compile(r'^\\format\s+("?dia"?\s+.*)\s+"([^"]*?)"', re.IGNORECASE)
196 def zipped_native(line):
197         if not line.lower().startswith("\\format"):
198                 return no_match
199         m = zipre.match(line)
200         if not m:
201                 return no_match
202         fmat = m.group(1)
203         opts = m.group(2)
204         return (True,
205                 "\\Format %s \"%s,zipped=native\"" % (fmat, opts))
206
207 def remove_default_papersize(line):
208         if not line.lower().startswith("\\default_papersize"):
209                 return no_match
210         return (True, "")
211
212 def add_mime_types(line):
213         if not line.lower().startswith("\\format"):
214                 return no_match
215         entries = get_format(line)
216         converted = line
217         i = len(entries)
218         while i < 7:
219                 converted = converted + '       ""'
220                 i = i + 1
221         formats = {'tgif':'application/x-tgif', \
222                 'fig':'application/x-xfig', \
223                 'dia':'application/x-dia-diagram', \
224                 'odg':'application/vnd.oasis.opendocument.graphics', \
225                 'svg':'image/svg+xml', \
226                 'bmp':'image/x-bmp', \
227                 'gif':'image/gif', \
228                 'jpg':'image/jpeg', \
229                 'pbm':'image/x-portable-bitmap', \
230                 'pgm':'image/x-portable-graymap', \
231                 'png':'image/x-png', \
232                 'ppm':'image/x-portable-pixmap', \
233                 'tiff':'image/tiff', \
234                 'xbm':'image/x-xbitmap', \
235                 'xpm':'image/x-xpixmap', \
236                 'docbook-xml':'application/docbook+xml', \
237                 'dot':'text/vnd.graphviz', \
238                 'ly':'text/x-lilypond', \
239                 'latex':'text/x-tex', \
240                 'text':'text/plain', \
241                 'gnumeric':'application/x-gnumeric', \
242                 'excel':'application/vnd.ms-excel', \
243                 'oocalc':'application/vnd.oasis.opendocument.spreadsheet', \
244                 'xhtml':'application/xhtml+xml', \
245                 'bib':'text/x-bibtex', \
246                 'eps':'image/x-eps', \
247                 'ps':'application/postscript', \
248                 'pdf':'application/pdf', \
249                 'dvi':'application/x-dvi', \
250                 'html':'text/html', \
251                 'odt':'application/vnd.oasis.opendocument.text', \
252                 'sxw':'application/vnd.sun.xml.writer', \
253                 'rtf':'application/rtf', \
254                 'doc':'application/msword', \
255                 'csv':'text/csv', \
256                 'lyx':'application/x-lyx', \
257                 'wmf':'image/x-wmf', \
258                 'emf':'image/x-emf'}
259         if entries[1] in formats.keys():
260                 converted = converted + '       "' + formats[entries[1]] + '"'
261         else:
262                 converted = converted + '       ""'
263         return (True, converted)
264
265 re_converter = re.compile(r'^\\converter\s+', re.IGNORECASE)
266
267 def split_pdf_format(line):
268         # strictly speaking, a new format would not require to bump the
269         # version number, but the old pdf format was hardcoded at several
270         # places in the C++ code, so an update seemed like a good idea.
271         if line.lower().startswith("\\format"):
272                 entries = get_format(line)
273                 if entries[1] == 'pdf':
274                         if len(entries) < 6:
275                                 viewer = ''
276                         else:
277                                 viewer = entries[5]
278                         converted = line.replace('application/pdf', '') + '''
279 \Format pdf6       pdf    "PDF (graphics)"        "" "''' + viewer + '" ""      "vector"        "application/pdf"'
280                         return (True, converted)
281         elif line.lower().startswith("\\viewer_alternatives") or \
282              line.lower().startswith("\\editor_alternatives"):
283                 entries = get_format(line)
284                 if entries[1] == 'pdf':
285                         converted = line + "\n" + entries[0] + ' pdf6 "' + entries[2] + '"'
286                         return (True, converted)
287         elif re_converter.match(line):
288                 entries = get_format(line)
289                 # The only converter from pdf that is touched is pdf->eps:
290                 # All other converters are likely meant for further processing on export.
291                 # The only converter to pdf that stays untouched is dvi->pdf:
292                 # All other converters are likely meant for graphics.
293                 if len(entries) > 2 and \
294                    ((entries[1] == 'pdf' and entries[2] == 'eps') or \
295                    (entries[1] != 'ps'  and entries[2] == 'pdf')):
296                         if entries[1] == 'pdf':
297                                 converted = entries[0] + ' pdf6 ' + entries[2]
298                         else:
299                                 converted = entries[0] + ' ' + entries[1] + ' pdf6'
300                         i = 3
301                         while i < len(entries):
302                                 converted = converted + ' "' + entries[i] + '"'
303                                 i = i + 1
304                         return (True, converted)
305         return no_match
306
307 def remove_default_language(line):
308         if not line.lower().startswith("\\default_language"):
309                 return no_match
310         return (True, "")
311
312 def mac_cursor_movement(line):
313         return simple_renaming(line, "\\mac_like_word_movement", "\\mac_like_cursor_movement")
314
315 # End conversions for LyX 2.0 to 2.1
316 ####################################
317
318
319 #################################
320 # Conversions from LyX 2.1 to 2.2
321
322 def remove_force_paint_single_char(line):
323         if not line.lower().startswith("\\force_paint_single_char"):
324                 return no_match
325         return (True, "")
326
327 def remove_rtl(line):
328         if not line.lower().startswith("\\rtl "):
329                 return no_match
330         return (True, "")
331
332 # End conversions for LyX 2.1 to 2.2
333 ####################################
334
335 conversions = [
336         [  1, [ # there were several conversions for format 1
337                 export_menu,
338                 latex_flavor,
339                 remove_obsolete,
340                 language_use_babel,
341                 language_package
342         ]],
343         [ 2, []],
344         [ 3, [ zipped_native ]],
345         [ 4, [ remove_default_papersize ]],
346         [ 5, []],
347         [ 6, []],
348         [ 7, [add_mime_types]],
349         [ 8, []],
350         [ 9, [ remove_default_language ]],
351         [ 10, []],
352         [ 11, [split_pdf_format]],
353         [ 12, []],
354         [ 13, [mac_cursor_movement]],
355         [ 14, []],
356         [ 15, []],
357         [ 16, [remove_force_paint_single_char]],
358         [ 17, [remove_rtl]]
359 ]