]> git.lyx.org Git - lyx.git/blob - lib/scripts/prefs2prefs_prefs.py
Create Chapter 6 Bullets in Additional.lyx and move the bullet section into it; this...
[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 # NOTE: The format should also be updated in LYXRC.cpp and
172 # in configure.py (search for lyxrc_fileformat).
173
174 import re
175
176 ###########################################################
177 #
178 # Conversion chain
179
180 def get_format(line):
181         entries = []
182         i = 0
183         while i < len(line):
184                 if line[i] == '"':
185                         beg = i + 1
186                         i = i + 1
187                         while i < len(line) and line[i] != '"':
188                                 if line[i] == '\\' and i < len(line) - 1 and line[i+1] == '"':
189                                         # convert \" to "
190                                         i = i + 1
191                                 i = i + 1
192                         end = i
193                         entries.append(line[beg:end].replace('\\"', '"'))
194                 elif line[i] == '#':
195                         return entries
196                 elif not line[i].isspace():
197                         beg = i
198                         while i < len(line) and not line[i].isspace():
199                                 i = i + 1
200                         end = i
201                         entries.append(line[beg:end])
202                 i = i + 1
203         return entries
204
205
206 def simple_renaming(line, old, new):
207         i = line.lower().find(old.lower())
208         if i == -1:
209                 return no_match
210         line = line[:i] + new + line[i+len(old):]
211         return (True, line)
212
213 no_match = (False, [])
214
215 ######################################
216 ### Format 1 conversions (for LyX 2.0)
217
218 def remove_obsolete(line):
219         tags = ("\\use_tempdir", "\\spell_command", "\\personal_dictionary",
220                 "\\plaintext_roff_command", "\\use_alt_language",
221                 "\\use_escape_chars", "\\use_input_encoding",
222                 "\\use_personal_dictionary", "\\use_pspell",
223                 "\\use_spell_lib")
224         line = line.lower().lstrip()
225         for tag in tags:
226                 if line.lower().startswith(tag):
227                         return (True, "")
228         return no_match
229
230
231 def language_use_babel(line):
232         if not line.lower().startswith(r"\language_use_babel"):
233                 return no_match
234         re_lub = re.compile(r'^\\language_use_babel\s+"?(true|false)', re.IGNORECASE)
235         m = re_lub.match(line)
236         val = m.group(1)
237         newval = '0'
238         if val == 'false':
239                 newval = '3'
240         newline = "\\language_package_selection " + newval
241         return (True, newline)
242
243
244 def language_package(line):
245         return simple_renaming(line, "\\language_package", "\\language_custom_package")
246
247
248 lfre = re.compile(r'^\\converter\s+"?(\w+)"?\s+"?(\w+)"?\s+"([^"]*?)"\s+"latex"', re.IGNORECASE)
249 def latex_flavor(line):
250         if not line.lower().startswith("\\converter"):
251                 return no_match
252         m = lfre.match(line)
253         if not m:
254                 return no_match
255         conv = m.group(1)
256         fmat = m.group(2)
257         args = m.group(3)
258         conv2fl = {
259                    "luatex":   "lualatex",
260                    "pplatex":  "latex",
261                    "xetex":    "xelatex",
262                   }
263         if conv in conv2fl.keys():
264                 flavor = conv2fl[conv]
265         else:
266                 flavor = conv
267         if flavor == "latex":
268                 return no_match
269         return (True,
270                 "\\converter \"%s\" \"%s\" \"%s\" \"latex=%s\"" % (conv, fmat, args, flavor))
271
272
273 emre = re.compile(r'^\\format\s+(.*)\s+"(document[^"]*?)"', re.IGNORECASE)
274 def export_menu(line):
275         if not line.lower().startswith("\\format"):
276                 return no_match
277         m = emre.match(line)
278         if not m:
279                 return no_match
280         fmat = m.group(1)
281         opts = m.group(2)
282         return (True,
283                 "\\Format %s \"%s,menu=export\"" % (fmat, opts))
284
285 # End format 1 conversions (for LyX 2.0)
286 ########################################
287
288 #################################
289 # Conversions from LyX 2.0 to 2.1
290 zipre = re.compile(r'^\\format\s+("?dia"?\s+.*)\s+"([^"]*?)"', re.IGNORECASE)
291 def zipped_native(line):
292         if not line.lower().startswith("\\format"):
293                 return no_match
294         m = zipre.match(line)
295         if not m:
296                 return no_match
297         fmat = m.group(1)
298         opts = m.group(2)
299         return (True,
300                 "\\Format %s \"%s,zipped=native\"" % (fmat, opts))
301
302 def remove_default_papersize(line):
303         if not line.lower().startswith("\\default_papersize"):
304                 return no_match
305         return (True, "")
306
307 def add_mime_types(line):
308         if not line.lower().startswith("\\format"):
309                 return no_match
310         entries = get_format(line)
311         converted = line
312         i = len(entries)
313         while i < 7:
314                 converted = converted + '       ""'
315                 i = i + 1
316         formats = {'tgif':'application/x-tgif', \
317                 'fig':'application/x-xfig', \
318                 'dia':'application/x-dia-diagram', \
319                 'odg':'application/vnd.oasis.opendocument.graphics', \
320                 'svg':'image/svg+xml', \
321                 'bmp':'image/x-bmp', \
322                 'gif':'image/gif', \
323                 'jpg':'image/jpeg', \
324                 'pbm':'image/x-portable-bitmap', \
325                 'pgm':'image/x-portable-graymap', \
326                 'png':'image/x-png', \
327                 'ppm':'image/x-portable-pixmap', \
328                 'tiff':'image/tiff', \
329                 'xbm':'image/x-xbitmap', \
330                 'xpm':'image/x-xpixmap', \
331                 'docbook-xml':'application/docbook+xml', \
332                 'dot':'text/vnd.graphviz', \
333                 'ly':'text/x-lilypond', \
334                 'latex':'text/x-tex', \
335                 'text':'text/plain', \
336                 'gnumeric':'application/x-gnumeric', \
337                 'excel':'application/vnd.ms-excel', \
338                 'oocalc':'application/vnd.oasis.opendocument.spreadsheet', \
339                 'xhtml':'application/xhtml+xml', \
340                 'bib':'text/x-bibtex', \
341                 'eps':'image/x-eps', \
342                 'ps':'application/postscript', \
343                 'pdf':'application/pdf', \
344                 'dvi':'application/x-dvi', \
345                 'html':'text/html', \
346                 'odt':'application/vnd.oasis.opendocument.text', \
347                 'sxw':'application/vnd.sun.xml.writer', \
348                 'rtf':'application/rtf', \
349                 'doc':'application/msword', \
350                 'csv':'text/csv', \
351                 'lyx':'application/x-lyx', \
352                 'wmf':'image/x-wmf', \
353                 'emf':'image/x-emf'}
354         if entries[1] in formats.keys():
355                 converted = converted + '       "' + formats[entries[1]] + '"'
356         else:
357                 converted = converted + '       ""'
358         return (True, converted)
359
360 re_converter = re.compile(r'^\\converter\s+', re.IGNORECASE)
361
362 def split_pdf_format(line):
363         # strictly speaking, a new format would not require to bump the
364         # version number, but the old pdf format was hardcoded at several
365         # places in the C++ code, so an update seemed like a good idea.
366         if line.lower().startswith("\\format"):
367                 entries = get_format(line)
368                 if entries[1] == 'pdf':
369                         if len(entries) < 6:
370                                 viewer = ''
371                         else:
372                                 viewer = entries[5]
373                         converted = line.replace('application/pdf', '') + r'''
374 \Format pdf6       pdf    "PDF (graphics)"        "" "''' + viewer + '" ""      "vector"        "application/pdf"'
375                         return (True, converted)
376         elif line.lower().startswith("\\viewer_alternatives") or \
377              line.lower().startswith("\\editor_alternatives"):
378                 entries = get_format(line)
379                 if entries[1] == 'pdf':
380                         converted = line + "\n" + entries[0] + ' pdf6 "' + entries[2] + '"'
381                         return (True, converted)
382         elif re_converter.match(line):
383                 entries = get_format(line)
384                 # The only converter from pdf that is touched is pdf->eps:
385                 # All other converters are likely meant for further processing on export.
386                 # The only converter to pdf that stays untouched is dvi->pdf:
387                 # All other converters are likely meant for graphics.
388                 if len(entries) > 2 and \
389                    ((entries[1] == 'pdf' and entries[2] == 'eps') or \
390                    (entries[1] != 'ps'  and entries[2] == 'pdf')):
391                         if entries[1] == 'pdf':
392                                 converted = entries[0] + ' pdf6 ' + entries[2]
393                         else:
394                                 converted = entries[0] + ' ' + entries[1] + ' pdf6'
395                         i = 3
396                         while i < len(entries):
397                                 converted = converted + ' "' + entries[i] + '"'
398                                 i = i + 1
399                         return (True, converted)
400         return no_match
401
402 def remove_default_language(line):
403         if not line.lower().startswith("\\default_language"):
404                 return no_match
405         return (True, "")
406
407 def mac_cursor_movement(line):
408         return simple_renaming(line, "\\mac_like_word_movement", "\\mac_like_cursor_movement")
409
410 # End conversions for LyX 2.0 to 2.1
411 ####################################
412
413
414 #################################
415 # Conversions from LyX 2.1 to 2.2
416
417 def remove_force_paint_single_char(line):
418         if not line.lower().startswith("\\force_paint_single_char"):
419                 return no_match
420         return (True, "")
421
422 def remove_rtl(line):
423         if not line.lower().startswith("\\rtl "):
424                 return no_match
425         return (True, "")
426
427 def remove_print_support(line):
428         tags = ("\\printer", "\\print_adapt_output", "\\print_command",
429                 "\\print_evenpage_flag", "\\print_oddpage_flag", "\\print_pagerange_flag",
430                 "\\print_copies_flag", "\\print_collcopies_flag", "\\print_reverse_flag",
431                 "\\print_to_printer", "\\print_to_file", "\\print_file_extension")
432         line = line.lower().lstrip()
433         for tag in tags:
434                 if line.lower().startswith(tag):
435                         return (True, "")
436         return no_match
437
438 # End conversions for LyX 2.1 to 2.2
439 ####################################
440
441
442 #################################
443 # Conversions from LyX 2.2 to 2.3
444
445 def rename_collapsible(line):
446         return simple_renaming(line, "\\set_color \"collapsable", "\\set_color \"collapsible")
447
448 # End conversions for LyX 2.2 to 2.3
449 ####################################
450
451
452 #################################
453 # Conversions from LyX 2.3 to 2.4
454
455 def remove_use_qimage(line):
456         if not line.lower().startswith("\\use_qimage "):
457                 return no_match
458         return (True, "")
459
460 def remove_font_encoding(line):
461         if not line.lower().startswith("\\font_encoding "):
462                 return no_match
463         return (True, "")
464
465 def remove_date_insert_format(line):
466         if not line.lower().startswith("\\date_insert_format "):
467                 return no_match
468         return (True, "")
469
470 def remove_use_pixmap_cache(line):
471         if not line.lower().startswith("\\use_pixmap_cache "):
472                 return no_match
473         return (True, "")
474
475 def rename_cyrillic_kmap_files(line):
476         line = line.lower()
477         if not (line.startswith("\\kbmap_primary ")
478                         or line.startswith("\\kbmap_secondary ")):
479                 return no_match
480         line = line.replace('"bg-bds-1251"', '"bulgarian"')
481         line = line.replace('"koi8-r"', '"russian"')
482         line = line.replace('"koi8-u"', '"ukrainian"')
483         return (True, line)
484
485 def add_dark_color(line):
486         if not line.lower().startswith("\\set_color "):
487                 return no_match
488         colre = re.compile(r'^\\set_color\s+("[^"]+")\s+("[^"]+")\s*$', re.IGNORECASE)
489         m = colre.match(line)
490         if not m:
491                 return no_match
492         line += " " + m.group(2)
493         return (True, line)
494
495 def add_spellcheck_default(lines):
496         for l in lines:
497                 if l.startswith("\\spellcheck_continuously"):
498                         return
499         lines.append("\\spellcheck_continuously false")
500
501 def remove_fullscreen_widthlimit(line):
502         lower = line.lower()
503         if lower.startswith("\\fullscreen_width") or lower.startswith("\\fullscreen_limit"):
504                 return (True, "")
505         return no_match
506
507 # End conversions for LyX 2.3 to 2.4
508 ####################################
509
510
511
512 ############################################################
513 # Format-conversion map. Also add empty format changes here.
514
515 conversions = [
516         [  1, [ # there were several conversions for format 1
517                 export_menu,
518                 latex_flavor,
519                 remove_obsolete,
520                 language_use_babel,
521                 language_package
522         ]],
523         [ 2, []],
524         [ 3, [ zipped_native ]],
525         [ 4, [ remove_default_papersize ]],
526         [ 5, []],
527         [ 6, []],
528         [ 7, [add_mime_types]],
529         [ 8, []],
530         [ 9, [ remove_default_language ]],
531         [ 10, []],
532         [ 11, [split_pdf_format]],
533         [ 12, []],
534         [ 13, [mac_cursor_movement]],
535         [ 14, []],
536         [ 15, []],
537         [ 16, [remove_force_paint_single_char]],
538         [ 17, [remove_rtl]],
539         [ 18, []],
540         [ 19, [remove_print_support]],
541         [ 20, []],
542         [ 21, []],
543         [ 22, []],
544         [ 23, []],
545         [ 24, [rename_collapsible]],
546         [ 25, [remove_use_qimage]],
547         [ 26, [remove_font_encoding]],
548         [ 27, []],
549         [ 28, [remove_date_insert_format]],
550         [ 29, [remove_use_pixmap_cache]],
551         [ 30, []],
552         [ 31, []],
553         [ 32, []],
554         [ 33, []],
555         [ 34, [rename_cyrillic_kmap_files]],
556         [ 35, [add_dark_color]],
557         [ 36, [add_spellcheck_default]],
558         [ 37, [remove_fullscreen_widthlimit]]
559 ]