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