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