]> git.lyx.org Git - lyx.git/blob - lib/scripts/prefs2prefs_prefs.py
Fix typo
[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 # Incremented to format 18, by ef
77 #   Add option to allow saving the document directory
78 #   No conversion necessary.
79
80 # Incremented to format 19, by rgh
81 #   remove print support
82
83 # Incremented to format 20, by tommaso
84 #   Add options to forbid/ignore 'needauth' option
85 #   No conversion necessary.
86
87 # Incremented to format 21, by spitz
88 #   Add jbibtex_alternatives, allow "automatic" value
89 #   of bibtex_command and jbibtex_command (actually the
90 #   default now)
91 #   No conversion necessary.
92
93 # Incremented to format 22, by ef
94 #   Add pygmentize_command for the python pygments syntax highlighter
95 #   No conversion necessary.
96
97 # Incremented to format 23, by spitz
98 #   Add default_platex_view_format, a default output format for
99 #   Japanese documents via pLaTeX.
100 #   No conversion necessary.
101
102 # Incremented to format 24, by spitz
103 #   Rename collapsable to collapsible
104
105 # Incremented to format 25, by lasgouttes
106 #   Remove use_qimage preference
107
108 # Incremented to format 26, by spitz
109 #   Rename font_encoding preference
110
111 # Incremented to format 27, by spitz
112 #   Add optional flavor value to needaux flag
113
114 # Incremented to format 28, by spitz
115 #   Remove date_insert_format
116
117 # Incremented to format 29, by lasgouttes
118 #   Remove use_pixmap_cache
119
120 # Incremented to format 30, by lasgouttes
121 #   Add respect_os_kbd_language.
122 #   No convergence necessary.
123
124 # NOTE: The format should also be updated in LYXRC.cpp and
125 # in configure.py.
126
127 import re
128
129 ###########################################################
130 #
131 # Conversion chain
132
133 def get_format(line):
134         entries = []
135         i = 0
136         while i < len(line):
137                 if line[i] == '"':
138                         beg = i + 1
139                         i = i + 1
140                         while i < len(line) and line[i] != '"':
141                                 if line[i] == '\\' and i < len(line) - 1 and line[i+1] == '"':
142                                         # convert \" to "
143                                         i = i + 1
144                                 i = i + 1
145                         end = i
146                         entries.append(line[beg:end].replace('\\"', '"'))
147                 elif line[i] == '#':
148                         return entries
149                 elif not line[i].isspace():
150                         beg = i
151                         while i < len(line) and not line[i].isspace():
152                                 i = i + 1
153                         end = i
154                         entries.append(line[beg:end])
155                 i = i + 1
156         return entries
157
158
159 def simple_renaming(line, old, new):
160         i = line.lower().find(old.lower())
161         if i == -1:
162                 return no_match
163         line = line[:i] + new + line[i+len(old):]
164         return (True, line)
165
166 no_match = (False, [])
167
168 ######################################
169 ### Format 1 conversions (for LyX 2.0)
170
171 def remove_obsolete(line):
172         tags = ("\\use_tempdir", "\\spell_command", "\\personal_dictionary",
173                 "\\plaintext_roff_command", "\\use_alt_language",
174                 "\\use_escape_chars", "\\use_input_encoding",
175                 "\\use_personal_dictionary", "\\use_pspell",
176                 "\\use_spell_lib")
177         line = line.lower().lstrip()
178         for tag in tags:
179                 if line.lower().startswith(tag):
180                         return (True, "")
181         return no_match
182
183
184 def language_use_babel(line):
185         if not line.lower().startswith("\language_use_babel"):
186                 return no_match
187         re_lub = re.compile(r'^\\language_use_babel\s+"?(true|false)', re.IGNORECASE)
188         m = re_lub.match(line)
189         val = m.group(1)
190         newval = '0'
191         if val == 'false':
192                 newval = '3'
193         newline = "\\language_package_selection " + newval
194         return (True, newline)
195
196
197 def language_package(line):
198         return simple_renaming(line, "\\language_package", "\\language_custom_package")
199
200
201 lfre = re.compile(r'^\\converter\s+"?(\w+)"?\s+"?(\w+)"?\s+"([^"]*?)"\s+"latex"', re.IGNORECASE)
202 def latex_flavor(line):
203         if not line.lower().startswith("\\converter"):
204                 return no_match
205         m = lfre.match(line)
206         if not m:
207                 return no_match
208         conv = m.group(1)
209         fmat = m.group(2)
210         args = m.group(3)
211         conv2fl = {
212                    "luatex":   "lualatex",
213                    "pplatex":  "latex",
214                    "xetex":    "xelatex",
215                   }
216         if conv in conv2fl.keys():
217                 flavor = conv2fl[conv]
218         else:
219                 flavor = conv
220         if flavor == "latex":
221                 return no_match
222         return (True,
223                 "\\converter \"%s\" \"%s\" \"%s\" \"latex=%s\"" % (conv, fmat, args, flavor))
224
225
226 emre = re.compile(r'^\\format\s+(.*)\s+"(document[^"]*?)"', re.IGNORECASE)
227 def export_menu(line):
228         if not line.lower().startswith("\\format"):
229                 return no_match
230         m = emre.match(line)
231         if not m:
232                 return no_match
233         fmat = m.group(1)
234         opts = m.group(2)
235         return (True,
236                 "\\Format %s \"%s,menu=export\"" % (fmat, opts))
237
238 # End format 1 conversions (for LyX 2.0)
239 ########################################
240
241 #################################
242 # Conversions from LyX 2.0 to 2.1
243 zipre = re.compile(r'^\\format\s+("?dia"?\s+.*)\s+"([^"]*?)"', re.IGNORECASE)
244 def zipped_native(line):
245         if not line.lower().startswith("\\format"):
246                 return no_match
247         m = zipre.match(line)
248         if not m:
249                 return no_match
250         fmat = m.group(1)
251         opts = m.group(2)
252         return (True,
253                 "\\Format %s \"%s,zipped=native\"" % (fmat, opts))
254
255 def remove_default_papersize(line):
256         if not line.lower().startswith("\\default_papersize"):
257                 return no_match
258         return (True, "")
259
260 def add_mime_types(line):
261         if not line.lower().startswith("\\format"):
262                 return no_match
263         entries = get_format(line)
264         converted = line
265         i = len(entries)
266         while i < 7:
267                 converted = converted + '       ""'
268                 i = i + 1
269         formats = {'tgif':'application/x-tgif', \
270                 'fig':'application/x-xfig', \
271                 'dia':'application/x-dia-diagram', \
272                 'odg':'application/vnd.oasis.opendocument.graphics', \
273                 'svg':'image/svg+xml', \
274                 'bmp':'image/x-bmp', \
275                 'gif':'image/gif', \
276                 'jpg':'image/jpeg', \
277                 'pbm':'image/x-portable-bitmap', \
278                 'pgm':'image/x-portable-graymap', \
279                 'png':'image/x-png', \
280                 'ppm':'image/x-portable-pixmap', \
281                 'tiff':'image/tiff', \
282                 'xbm':'image/x-xbitmap', \
283                 'xpm':'image/x-xpixmap', \
284                 'docbook-xml':'application/docbook+xml', \
285                 'dot':'text/vnd.graphviz', \
286                 'ly':'text/x-lilypond', \
287                 'latex':'text/x-tex', \
288                 'text':'text/plain', \
289                 'gnumeric':'application/x-gnumeric', \
290                 'excel':'application/vnd.ms-excel', \
291                 'oocalc':'application/vnd.oasis.opendocument.spreadsheet', \
292                 'xhtml':'application/xhtml+xml', \
293                 'bib':'text/x-bibtex', \
294                 'eps':'image/x-eps', \
295                 'ps':'application/postscript', \
296                 'pdf':'application/pdf', \
297                 'dvi':'application/x-dvi', \
298                 'html':'text/html', \
299                 'odt':'application/vnd.oasis.opendocument.text', \
300                 'sxw':'application/vnd.sun.xml.writer', \
301                 'rtf':'application/rtf', \
302                 'doc':'application/msword', \
303                 'csv':'text/csv', \
304                 'lyx':'application/x-lyx', \
305                 'wmf':'image/x-wmf', \
306                 'emf':'image/x-emf'}
307         if entries[1] in formats.keys():
308                 converted = converted + '       "' + formats[entries[1]] + '"'
309         else:
310                 converted = converted + '       ""'
311         return (True, converted)
312
313 re_converter = re.compile(r'^\\converter\s+', re.IGNORECASE)
314
315 def split_pdf_format(line):
316         # strictly speaking, a new format would not require to bump the
317         # version number, but the old pdf format was hardcoded at several
318         # places in the C++ code, so an update seemed like a good idea.
319         if line.lower().startswith("\\format"):
320                 entries = get_format(line)
321                 if entries[1] == 'pdf':
322                         if len(entries) < 6:
323                                 viewer = ''
324                         else:
325                                 viewer = entries[5]
326                         converted = line.replace('application/pdf', '') + '''
327 \Format pdf6       pdf    "PDF (graphics)"        "" "''' + viewer + '" ""      "vector"        "application/pdf"'
328                         return (True, converted)
329         elif line.lower().startswith("\\viewer_alternatives") or \
330              line.lower().startswith("\\editor_alternatives"):
331                 entries = get_format(line)
332                 if entries[1] == 'pdf':
333                         converted = line + "\n" + entries[0] + ' pdf6 "' + entries[2] + '"'
334                         return (True, converted)
335         elif re_converter.match(line):
336                 entries = get_format(line)
337                 # The only converter from pdf that is touched is pdf->eps:
338                 # All other converters are likely meant for further processing on export.
339                 # The only converter to pdf that stays untouched is dvi->pdf:
340                 # All other converters are likely meant for graphics.
341                 if len(entries) > 2 and \
342                    ((entries[1] == 'pdf' and entries[2] == 'eps') or \
343                    (entries[1] != 'ps'  and entries[2] == 'pdf')):
344                         if entries[1] == 'pdf':
345                                 converted = entries[0] + ' pdf6 ' + entries[2]
346                         else:
347                                 converted = entries[0] + ' ' + entries[1] + ' pdf6'
348                         i = 3
349                         while i < len(entries):
350                                 converted = converted + ' "' + entries[i] + '"'
351                                 i = i + 1
352                         return (True, converted)
353         return no_match
354
355 def remove_default_language(line):
356         if not line.lower().startswith("\\default_language"):
357                 return no_match
358         return (True, "")
359
360 def mac_cursor_movement(line):
361         return simple_renaming(line, "\\mac_like_word_movement", "\\mac_like_cursor_movement")
362
363 # End conversions for LyX 2.0 to 2.1
364 ####################################
365
366
367 #################################
368 # Conversions from LyX 2.1 to 2.2
369
370 def remove_force_paint_single_char(line):
371         if not line.lower().startswith("\\force_paint_single_char"):
372                 return no_match
373         return (True, "")
374
375 def remove_rtl(line):
376         if not line.lower().startswith("\\rtl "):
377                 return no_match
378         return (True, "")
379
380 def remove_print_support(line):
381         tags = ("\\printer", "\\print_adapt_output", "\\print_command",
382                 "\\print_evenpage_flag", "\\print_oddpage_flag", "\\print_pagerange_flag",
383                 "\\print_copies_flag", "\\print_collcopies_flag", "\\print_reverse_flag",
384                 "\\print_to_printer", "\\print_to_file", "\\print_file_extension")
385         line = line.lower().lstrip()
386         for tag in tags:
387                 if line.lower().startswith(tag):
388                         return (True, "")
389         return no_match
390
391 # End conversions for LyX 2.1 to 2.2
392 ####################################
393
394
395 #################################
396 # Conversions from LyX 2.2 to 2.3
397
398 def rename_collapsible(line):
399         return simple_renaming(line, "\\set_color \"collapsable", "\\set_color \"collapsible")
400
401 # End conversions for LyX 2.2 to 2.3
402 ####################################
403
404
405 #################################
406 # Conversions from LyX 2.3 to 2.4
407
408 def remove_use_qimage(line):
409         if not line.lower().startswith("\\use_qimage "):
410                 return no_match
411         return (True, "")
412
413 def remove_font_encoding(line):
414         if not line.lower().startswith("\\font_encoding "):
415                 return no_match
416         return (True, "")
417
418 def remove_date_insert_format(line):
419         if not line.lower().startswith("\\date_insert_format "):
420                 return no_match
421         return (True, "")
422
423 def remove_use_pixmap_cache(line):
424         if not line.lower().startswith("\\use_pixmap_cache "):
425                 return no_match
426         return (True, "")
427
428 # End conversions for LyX 2.3 to 2.4
429 ####################################
430
431
432
433 ############################################################
434 # Format-conversion map. Also add empty format changes here.
435
436 conversions = [
437         [  1, [ # there were several conversions for format 1
438                 export_menu,
439                 latex_flavor,
440                 remove_obsolete,
441                 language_use_babel,
442                 language_package
443         ]],
444         [ 2, []],
445         [ 3, [ zipped_native ]],
446         [ 4, [ remove_default_papersize ]],
447         [ 5, []],
448         [ 6, []],
449         [ 7, [add_mime_types]],
450         [ 8, []],
451         [ 9, [ remove_default_language ]],
452         [ 10, []],
453         [ 11, [split_pdf_format]],
454         [ 12, []],
455         [ 13, [mac_cursor_movement]],
456         [ 14, []],
457         [ 15, []],
458         [ 16, [remove_force_paint_single_char]],
459         [ 17, [remove_rtl]],
460         [ 18, []],
461         [ 19, [remove_print_support]],
462         [ 20, []],
463         [ 21, []],
464         [ 22, []],
465         [ 23, []],
466         [ 24, [rename_collapsible]],
467         [ 25, [remove_use_qimage]],
468         [ 26, [remove_font_encoding]],
469         [ 27, []],
470         [ 28, [remove_date_insert_format]],
471         [ 29, [remove_use_pixmap_cache]],
472         [ 30, []]
473 ]