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