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