]> git.lyx.org Git - lyx.git/blob - lib/scripts/prefs2prefs_prefs.py
Remove profiling.py
[lyx.git] / lib / scripts / prefs2prefs_prefs.py
1 # file prefs2prefs-prefs.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 preferences file.
10
11 # There are two kinds of converter functions.
12
13 # Most of them 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 # This can be used to erase lines (return (True, "")) or to modify 
18 # existing preference lines.
19
20 # It is also possible for conversion routines to accept the whole
21 # list of lines and process that. This is useful (as in the change
22 # to format 35) when you need to add a preference if it's not already
23 # there.
24
25
26 ######################################################################
27 #
28 # FORMAT CHANGES
29 #
30 ######################################################################
31
32 # Incremented to format 2, r39670 by jrioux
33 #   Support for multiple file extensions per format.
34 #   No conversion necessary.
35
36 # Incremented to format 3, r39705 by tommaso
37 #   Support for file formats that are natively (g)zipped.
38 #   We must add the flag zipped=native to formats that
39 #   were previously hardcoded in the C++ source: dia.
40
41 # Incremented to format 4, r40028 by vfr
42 #   Remove support for default paper size.
43
44 # Incremented to format 5, r40030 by vfr
45 #   Add a default length unit.
46 #   No conversion necessary.
47
48 # Incremented to format 6, r40515 by younes
49 #   Add use_qimage option.
50 #   No conversion necessary.
51
52 # Incremented to format 7, r40789 by gb
53 #   Add mime type to file format
54
55 # Incremented to format 8, 288c1e0f by rgh
56 #   Add "nice" flag for converters
57 #   No conversion necessary.
58
59 # Incremented to format 9, a18af620 by spitz
60 #  Remove default_language rc.
61
62 # Incremented to format 10, 4985015 by tommaso
63 #  Add close_buffer_with_last_view in preferences.
64 #  No conversion necessary.
65
66 # Incremented to format 11, by gb
67 #   Split pdf format into pdf and pdf6
68
69 # Incremented to format 12, by vfr
70 #   Add option to use the system's theme icons
71 #   No conversion necessary.
72
73 # Incremented to format 13, by bh
74 #   Rename mac_like_word_movement to mac_like_cursor_movement
75
76 # Incremented to format 14, by spitz
77 #   New RC default_otf_view_format
78 #   No conversion necessary.
79
80 # Incremented to format 15, by prannoy
81 #   Add fullscreen_statusbar
82 #   No conversion necessary.
83
84 # Incremented to format 16, by lasgouttes
85 #  Remove force_paint_single_char rc.
86
87 # Incremented to format 17, by lasgouttes
88 #  Remove rtl_support rc.
89
90 # Incremented to format 18, by ef
91 #   Add option to allow saving the document directory
92 #   No conversion necessary.
93
94 # Incremented to format 19, by rgh
95 #   remove print support
96
97 # Incremented to format 20, by tommaso
98 #   Add options to forbid/ignore 'needauth' option
99 #   No conversion necessary.
100
101 # Incremented to format 21, by spitz
102 #   Add jbibtex_alternatives, allow "automatic" value
103 #   of bibtex_command and jbibtex_command (actually the
104 #   default now)
105 #   No conversion necessary.
106
107 # Incremented to format 22, by ef
108 #   Add pygmentize_command for the python pygments syntax highlighter
109 #   No conversion necessary.
110
111 # Incremented to format 23, by spitz
112 #   Add default_platex_view_format, a default output format for
113 #   Japanese documents via pLaTeX.
114 #   No conversion necessary.
115
116 # Incremented to format 24, by spitz
117 #   Rename collapsable to collapsible
118
119 # Incremented to format 25, by lasgouttes
120 #   Remove use_qimage preference
121
122 # Incremented to format 26, by spitz
123 #   Rename font_encoding preference
124
125 # Incremented to format 27, by spitz
126 #   Add optional flavor value to needaux flag
127
128 # Incremented to format 28, by spitz
129 #   Remove date_insert_format
130
131 # Incremented to format 29, by lasgouttes
132 #   Remove use_pixmap_cache
133
134 # Incremented to format 30, by lasgouttes
135 #   Add respect_os_kbd_language.
136 #   No convergence necessary.
137
138 # Incremented to format 31, by spitz
139 #   Add ct_additions_underlined.
140 #   No convergence necessary.
141
142 # Incremented to format 32, by spitz
143 #   Add ct_markup_copied.
144 #   No convergence necessary.
145
146 # Incremented to format 33, by sanda
147 #   Add \citation_search, \citation_search_pattern
148 #   and \citation_search_view.
149 #   No conversion necessary.
150
151 # Incremented to format 34, by yuriy
152 #   Rename *.kmap files for Cyrillic languages
153
154 # Incremented to format 35, by spitz
155 #   \set_color now takes three arguments
156 #   \set_color lyxname x11hexname x11darkhexname
157
158 # Incremented to format 36, by rkh
159 #   Set spellcheck_continuously to FALSE if it is not otherwise set
160 #   (the new default is true, so this keeps behavior the same for 
161 #   existing users)
162
163 # Incremented to format 37, by chillenb
164 #  Remove \fullscreen_width
165 #  Remove \fullscreen_limit
166 #  Add \screen_width
167 #  Add \screen_limit
168
169 # Incremented to format 38, by ec
170 #   Add option to configure ui style
171 #   No conversion necessary.
172
173 # NOTE: The format should also be updated in LYXRC.cpp and
174 # in configure.py (search for lyxrc_fileformat).
175
176 import re
177
178 ###########################################################
179 #
180 # Conversion chain
181
182 def get_format(line):
183         entries = []
184         i = 0
185         while i < len(line):
186                 if line[i] == '"':
187                         beg = i + 1
188                         i = i + 1
189                         while i < len(line) and line[i] != '"':
190                                 if line[i] == '\\' and i < len(line) - 1 and line[i+1] == '"':
191                                         # convert \" to "
192                                         i = i + 1
193                                 i = i + 1
194                         end = i
195                         entries.append(line[beg:end].replace('\\"', '"'))
196                 elif line[i] == '#':
197                         return entries
198                 elif not line[i].isspace():
199                         beg = i
200                         while i < len(line) and not line[i].isspace():
201                                 i = i + 1
202                         end = i
203                         entries.append(line[beg:end])
204                 i = i + 1
205         return entries
206
207
208 def simple_renaming(line, old, new):
209         i = line.lower().find(old.lower())
210         if i == -1:
211                 return no_match
212         line = line[:i] + new + line[i+len(old):]
213         return (True, line)
214
215 no_match = (False, [])
216
217 ######################################
218 ### Format 1 conversions (for LyX 2.0)
219
220 def remove_obsolete(line):
221         tags = ("\\use_tempdir", "\\spell_command", "\\personal_dictionary",
222                 "\\plaintext_roff_command", "\\use_alt_language",
223                 "\\use_escape_chars", "\\use_input_encoding",
224                 "\\use_personal_dictionary", "\\use_pspell",
225                 "\\use_spell_lib")
226         line = line.lower().lstrip()
227         for tag in tags:
228                 if line.lower().startswith(tag):
229                         return (True, "")
230         return no_match
231
232
233 def language_use_babel(line):
234         if not line.lower().startswith(r"\language_use_babel"):
235                 return no_match
236         re_lub = re.compile(r'^\\language_use_babel\s+"?(true|false)', re.IGNORECASE)
237         m = re_lub.match(line)
238         val = m.group(1)
239         newval = '0'
240         if val == 'false':
241                 newval = '3'
242         newline = "\\language_package_selection " + newval
243         return (True, newline)
244
245
246 def language_package(line):
247         return simple_renaming(line, "\\language_package", "\\language_custom_package")
248
249
250 lfre = re.compile(r'^\\converter\s+"?(\w+)"?\s+"?(\w+)"?\s+"([^"]*?)"\s+"latex"', re.IGNORECASE)
251 def latex_flavor(line):
252         if not line.lower().startswith("\\converter"):
253                 return no_match
254         m = lfre.match(line)
255         if not m:
256                 return no_match
257         conv = m.group(1)
258         fmat = m.group(2)
259         args = m.group(3)
260         conv2fl = {
261                    "luatex":   "lualatex",
262                    "pplatex":  "latex",
263                    "xetex":    "xelatex",
264                   }
265         if conv in conv2fl.keys():
266                 flavor = conv2fl[conv]
267         else:
268                 flavor = conv
269         if flavor == "latex":
270                 return no_match
271         return (True,
272                 f"\\converter \"{conv}\" \"{fmat}\" \"{args}\" \"latex={flavor}\"")
273
274
275 emre = re.compile(r'^\\format\s+(.*)\s+"(document[^"]*?)"', re.IGNORECASE)
276 def export_menu(line):
277         if not line.lower().startswith("\\format"):
278                 return no_match
279         m = emre.match(line)
280         if not m:
281                 return no_match
282         fmat = m.group(1)
283         opts = m.group(2)
284         return (True,
285                 f"\\Format {fmat} \"{opts},menu=export\"")
286
287 # End format 1 conversions (for LyX 2.0)
288 ########################################
289
290 #################################
291 # Conversions from LyX 2.0 to 2.1
292 zipre = re.compile(r'^\\format\s+("?dia"?\s+.*)\s+"([^"]*?)"', re.IGNORECASE)
293 def zipped_native(line):
294         if not line.lower().startswith("\\format"):
295                 return no_match
296         m = zipre.match(line)
297         if not m:
298                 return no_match
299         fmat = m.group(1)
300         opts = m.group(2)
301         return (True,
302                 f"\\Format {fmat} \"{opts},zipped=native\"")
303
304 def remove_default_papersize(line):
305         if not line.lower().startswith("\\default_papersize"):
306                 return no_match
307         return (True, "")
308
309 def add_mime_types(line):
310         if not line.lower().startswith("\\format"):
311                 return no_match
312         entries = get_format(line)
313         converted = line
314         i = len(entries)
315         while i < 7:
316                 converted = converted + '       ""'
317                 i = i + 1
318         formats = {'tgif':'application/x-tgif', \
319                 'fig':'application/x-xfig', \
320                 'dia':'application/x-dia-diagram', \
321                 'odg':'application/vnd.oasis.opendocument.graphics', \
322                 'svg':'image/svg+xml', \
323                 'bmp':'image/x-bmp', \
324                 'gif':'image/gif', \
325                 'jpg':'image/jpeg', \
326                 'pbm':'image/x-portable-bitmap', \
327                 'pgm':'image/x-portable-graymap', \
328                 'png':'image/x-png', \
329                 'ppm':'image/x-portable-pixmap', \
330                 'tiff':'image/tiff', \
331                 'xbm':'image/x-xbitmap', \
332                 'xpm':'image/x-xpixmap', \
333                 'docbook-xml':'application/docbook+xml', \
334                 'dot':'text/vnd.graphviz', \
335                 'ly':'text/x-lilypond', \
336                 'latex':'text/x-tex', \
337                 'text':'text/plain', \
338                 'gnumeric':'application/x-gnumeric', \
339                 'excel':'application/vnd.ms-excel', \
340                 'oocalc':'application/vnd.oasis.opendocument.spreadsheet', \
341                 'xhtml':'application/xhtml+xml', \
342                 'bib':'text/x-bibtex', \
343                 'eps':'image/x-eps', \
344                 'ps':'application/postscript', \
345                 'pdf':'application/pdf', \
346                 'dvi':'application/x-dvi', \
347                 'html':'text/html', \
348                 'odt':'application/vnd.oasis.opendocument.text', \
349                 'sxw':'application/vnd.sun.xml.writer', \
350                 'rtf':'application/rtf', \
351                 'doc':'application/msword', \
352                 'csv':'text/csv', \
353                 'lyx':'application/x-lyx', \
354                 'wmf':'image/x-wmf', \
355                 'emf':'image/x-emf'}
356         if entries[1] in formats.keys():
357                 converted = converted + '       "' + formats[entries[1]] + '"'
358         else:
359                 converted = converted + '       ""'
360         return (True, converted)
361
362 re_converter = re.compile(r'^\\converter\s+', re.IGNORECASE)
363
364 def split_pdf_format(line):
365         # strictly speaking, a new format would not require to bump the
366         # version number, but the old pdf format was hardcoded at several
367         # places in the C++ code, so an update seemed like a good idea.
368         if line.lower().startswith("\\format"):
369                 entries = get_format(line)
370                 if entries[1] == 'pdf':
371                         if len(entries) < 6:
372                                 viewer = ''
373                         else:
374                                 viewer = entries[5]
375                         converted = line.replace('application/pdf', '') + r'''
376 \Format pdf6       pdf    "PDF (graphics)"        "" "''' + viewer + '" ""      "vector"        "application/pdf"'
377                         return (True, converted)
378         elif line.lower().startswith("\\viewer_alternatives") or \
379              line.lower().startswith("\\editor_alternatives"):
380                 entries = get_format(line)
381                 if entries[1] == 'pdf':
382                         converted = line + "\n" + entries[0] + ' pdf6 "' + entries[2] + '"'
383                         return (True, converted)
384         elif re_converter.match(line):
385                 entries = get_format(line)
386                 # The only converter from pdf that is touched is pdf->eps:
387                 # All other converters are likely meant for further processing on export.
388                 # The only converter to pdf that stays untouched is dvi->pdf:
389                 # All other converters are likely meant for graphics.
390                 if len(entries) > 2 and \
391                    ((entries[1] == 'pdf' and entries[2] == 'eps') or \
392                    (entries[1] != 'ps'  and entries[2] == 'pdf')):
393                         if entries[1] == 'pdf':
394                                 converted = entries[0] + ' pdf6 ' + entries[2]
395                         else:
396                                 converted = entries[0] + ' ' + entries[1] + ' pdf6'
397                         i = 3
398                         while i < len(entries):
399                                 converted = converted + ' "' + entries[i] + '"'
400                                 i = i + 1
401                         return (True, converted)
402         return no_match
403
404 def remove_default_language(line):
405         if not line.lower().startswith("\\default_language"):
406                 return no_match
407         return (True, "")
408
409 def mac_cursor_movement(line):
410         return simple_renaming(line, "\\mac_like_word_movement", "\\mac_like_cursor_movement")
411
412 # End conversions for LyX 2.0 to 2.1
413 ####################################
414
415
416 #################################
417 # Conversions from LyX 2.1 to 2.2
418
419 def remove_force_paint_single_char(line):
420         if not line.lower().startswith("\\force_paint_single_char"):
421                 return no_match
422         return (True, "")
423
424 def remove_rtl(line):
425         if not line.lower().startswith("\\rtl "):
426                 return no_match
427         return (True, "")
428
429 def remove_print_support(line):
430         tags = ("\\printer", "\\print_adapt_output", "\\print_command",
431                 "\\print_evenpage_flag", "\\print_oddpage_flag", "\\print_pagerange_flag",
432                 "\\print_copies_flag", "\\print_collcopies_flag", "\\print_reverse_flag",
433                 "\\print_to_printer", "\\print_to_file", "\\print_file_extension")
434         line = line.lower().lstrip()
435         for tag in tags:
436                 if line.lower().startswith(tag):
437                         return (True, "")
438         return no_match
439
440 # End conversions for LyX 2.1 to 2.2
441 ####################################
442
443
444 #################################
445 # Conversions from LyX 2.2 to 2.3
446
447 def rename_collapsible(line):
448         return simple_renaming(line, "\\set_color \"collapsable", "\\set_color \"collapsible")
449
450 # End conversions for LyX 2.2 to 2.3
451 ####################################
452
453
454 #################################
455 # Conversions from LyX 2.3 to 2.4
456
457 def remove_use_qimage(line):
458         if not line.lower().startswith("\\use_qimage "):
459                 return no_match
460         return (True, "")
461
462 def remove_font_encoding(line):
463         if not line.lower().startswith("\\font_encoding "):
464                 return no_match
465         return (True, "")
466
467 def remove_date_insert_format(line):
468         if not line.lower().startswith("\\date_insert_format "):
469                 return no_match
470         return (True, "")
471
472 def remove_use_pixmap_cache(line):
473         if not line.lower().startswith("\\use_pixmap_cache "):
474                 return no_match
475         return (True, "")
476
477 def rename_cyrillic_kmap_files(line):
478         line = line.lower()
479         if not (line.startswith("\\kbmap_primary ")
480                         or line.startswith("\\kbmap_secondary ")):
481                 return no_match
482         line = line.replace('"bg-bds-1251"', '"bulgarian"')
483         line = line.replace('"koi8-r"', '"russian"')
484         line = line.replace('"koi8-u"', '"ukrainian"')
485         return (True, line)
486
487 def add_dark_color(line):
488         if not line.lower().startswith("\\set_color "):
489                 return no_match
490         colre = re.compile(r'^\\set_color\s+("[^"]+")\s+("[^"]+")\s*$', re.IGNORECASE)
491         m = colre.match(line)
492         if not m:
493                 return no_match
494         line += " " + m.group(2)
495         return (True, line)
496
497 def add_spellcheck_default(lines):
498         for l in lines:
499                 if l.startswith("\\spellcheck_continuously"):
500                         return
501         lines.append("\\spellcheck_continuously false")
502
503 def remove_fullscreen_widthlimit(line):
504         lower = line.lower()
505         if lower.startswith("\\fullscreen_width") or lower.startswith("\\fullscreen_limit"):
506                 return (True, "")
507         return no_match
508
509 # End conversions for LyX 2.3 to 2.4
510 ####################################
511
512
513
514 ############################################################
515 # Format-conversion map. Also add empty format changes here.
516
517 conversions = [
518         [  1, [ # there were several conversions for format 1
519                 export_menu,
520                 latex_flavor,
521                 remove_obsolete,
522                 language_use_babel,
523                 language_package
524         ]],
525         [ 2, []],
526         [ 3, [ zipped_native ]],
527         [ 4, [ remove_default_papersize ]],
528         [ 5, []],
529         [ 6, []],
530         [ 7, [add_mime_types]],
531         [ 8, []],
532         [ 9, [ remove_default_language ]],
533         [ 10, []],
534         [ 11, [split_pdf_format]],
535         [ 12, []],
536         [ 13, [mac_cursor_movement]],
537         [ 14, []],
538         [ 15, []],
539         [ 16, [remove_force_paint_single_char]],
540         [ 17, [remove_rtl]],
541         [ 18, []],
542         [ 19, [remove_print_support]],
543         [ 20, []],
544         [ 21, []],
545         [ 22, []],
546         [ 23, []],
547         [ 24, [rename_collapsible]],
548         [ 25, [remove_use_qimage]],
549         [ 26, [remove_font_encoding]],
550         [ 27, []],
551         [ 28, [remove_date_insert_format]],
552         [ 29, [remove_use_pixmap_cache]],
553         [ 30, []],
554         [ 31, []],
555         [ 32, []],
556         [ 33, []],
557         [ 34, [rename_cyrillic_kmap_files]],
558         [ 35, [add_dark_color]],
559         [ 36, [add_spellcheck_default]],
560         [ 37, [remove_fullscreen_widthlimit]],
561         [ 38, []]
562 ]