src/Pure/System/options.scala
author wenzelm
Sun, 03 Apr 2016 22:36:11 +0200
changeset 62832 c1410bcf6e87
parent 62454 38c89353b349
child 62968 4e4738698db4
permissions -rw-r--r--
prefer internal tool;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/System/options.scala
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     3
51945
5b1ac9843f02 tuned comments;
wenzelm
parents: 51620
diff changeset
     4
System options with external string representation.
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     5
*/
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     6
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     7
package isabelle
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     8
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     9
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    10
import java.util.Calendar
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    11
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    12
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    13
object Options
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    14
{
48421
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    15
  type Spec = (String, Option[String])
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    16
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    17
  val empty: Options = new Options()
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    18
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    19
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    20
  /* representation */
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    21
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    22
  sealed abstract class Type
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    23
  {
56599
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents: 56559
diff changeset
    24
    def print: String = Word.lowercase(toString)
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    25
  }
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    26
  case object Bool extends Type
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    27
  case object Int extends Type
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    28
  case object Real extends Type
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    29
  case object String extends Type
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    30
  case object Unknown extends Type
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    31
56465
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
    32
  case class Opt(
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
    33
    public: Boolean,
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
    34
    pos: Position.T,
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
    35
    name: String,
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
    36
    typ: Type,
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
    37
    value: String,
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
    38
    default_value: String,
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
    39
    description: String,
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
    40
    section: String)
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
    41
  {
49289
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
    42
    private def print(default: Boolean): String =
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
    43
    {
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
    44
      val x = if (default) default_value else value
49247
ffd9ad9dc35b more detailed option tooltip;
wenzelm
parents: 49246
diff changeset
    45
      "option " + name + " : " + typ.print + " = " +
49289
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
    46
        (if (typ == Options.String) quote(x) else x) +
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
    47
        (if (description == "") "" else "\n  -- " + quote(description))
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
    48
    }
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
    49
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
    50
    def print: String = print(false)
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
    51
    def print_default: String = print(true)
49247
ffd9ad9dc35b more detailed option tooltip;
wenzelm
parents: 49246
diff changeset
    52
56559
eece73c31e38 added dictionaries_selector GUI;
wenzelm
parents: 56552
diff changeset
    53
    def title(strip: String = ""): String =
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    54
    {
56600
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    55
      val words = Word.explode('_', name)
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    56
      val words1 =
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    57
        words match {
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    58
          case word :: rest if word == strip => rest
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    59
          case _ => words
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    60
        }
56609
5ac67041ccf8 capitalize more carefully, e.g. relevant for option "ML_exception_trace";
wenzelm
parents: 56600
diff changeset
    61
      Word.implode(words1.map(Word.perhaps_capitalize(_)))
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    62
    }
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    63
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
    64
    def unknown: Boolean = typ == Unknown
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
    65
  }
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    66
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    67
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    68
  /* parsing */
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    69
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    70
  private val SECTION = "section"
52065
78f2475aa126 explicit notion of public options, which are shown in the editor options dialog;
wenzelm
parents: 51945
diff changeset
    71
  private val PUBLIC = "public"
48795
bece259ee055 clarified format of etc/options: only declarations, not re-definitions;
wenzelm
parents: 48718
diff changeset
    72
  private val OPTION = "option"
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    73
  private val OPTIONS = Path.explode("etc/options")
50793
b93484db43ed create required PREFS_DIR;
wenzelm
parents: 50299
diff changeset
    74
  private val PREFS_DIR = Path.explode("$ISABELLE_HOME_USER/etc")
b93484db43ed create required PREFS_DIR;
wenzelm
parents: 50299
diff changeset
    75
  private val PREFS = PREFS_DIR + Path.basic("preferences")
48713
de26cf3191a3 more token markers, based on actual outer syntax;
wenzelm
parents: 48693
diff changeset
    76
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    77
  lazy val options_syntax =
61579
634cd44bb1d3 symbolic syntax "\<comment> text";
wenzelm
parents: 60215
diff changeset
    78
    Outer_Syntax.init() + ":" + "=" + "--" + Symbol.comment + Symbol.comment_decoded +
60133
a90982bbe8b4 clarified keywords for quasi-command spans and Sidekick structure;
wenzelm
parents: 59811
diff changeset
    79
      (SECTION, Keyword.DOCUMENT_HEADING) + PUBLIC + (OPTION, Keyword.THY_DECL)
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    80
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    81
  lazy val prefs_syntax = Outer_Syntax.init() + "="
48713
de26cf3191a3 more token markers, based on actual outer syntax;
wenzelm
parents: 48693
diff changeset
    82
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    83
  object Parser extends Parse.Parser
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    84
  {
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    85
    val option_name = atom("option name", _.is_xname)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    86
    val option_type = atom("option type", _.is_ident)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    87
    val option_value =
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    88
      opt(token("-", tok => tok.is_sym_ident && tok.content == "-")) ~ atom("nat", _.is_nat) ^^
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    89
        { case s ~ n => if (s.isDefined) "-" + n else n } |
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    90
      atom("option value", tok => tok.is_name || tok.is_float)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    91
61579
634cd44bb1d3 symbolic syntax "\<comment> text";
wenzelm
parents: 60215
diff changeset
    92
    def comment_marker: Parser[String] =
634cd44bb1d3 symbolic syntax "\<comment> text";
wenzelm
parents: 60215
diff changeset
    93
      $$$("--") | $$$(Symbol.comment) | $$$(Symbol.comment_decoded)
634cd44bb1d3 symbolic syntax "\<comment> text";
wenzelm
parents: 60215
diff changeset
    94
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    95
    val option_entry: Parser[Options => Options] =
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    96
    {
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    97
      command(SECTION) ~! text ^^
49295
2750756db9c5 more precise sections;
wenzelm
parents: 49289
diff changeset
    98
        { case _ ~ a => (options: Options) => options.set_section(a) } |
60133
a90982bbe8b4 clarified keywords for quasi-command spans and Sidekick structure;
wenzelm
parents: 59811
diff changeset
    99
      opt($$$(PUBLIC)) ~ command(OPTION) ~! (position(option_name) ~ $$$(":") ~ option_type ~
61579
634cd44bb1d3 symbolic syntax "\<comment> text";
wenzelm
parents: 60215
diff changeset
   100
      $$$("=") ~ option_value ~ (comment_marker ~! text ^^ { case _ ~ x => x } | success(""))) ^^
59811
6b0d9e8ac227 clarified position;
wenzelm
parents: 59705
diff changeset
   101
        { case a ~ _ ~ ((b, pos) ~ _ ~ c ~ _ ~ d ~ e) =>
56465
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   102
            (options: Options) => options.declare(a.isDefined, pos, b, c, d, e) }
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   103
    }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   104
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   105
    val prefs_entry: Parser[Options => Options] =
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   106
    {
58908
58bedbc18915 tuned signature;
wenzelm
parents: 58868
diff changeset
   107
      option_name ~ ($$$("=") ~! option_value) ^^
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   108
      { case a ~ (_ ~ b) => (options: Options) => options.add_permissive(a, b) }
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   109
    }
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   110
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   111
    def parse_file(syntax: Outer_Syntax, parser: Parser[Options => Options],
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   112
      options: Options, file: Path): Options =
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   113
    {
59083
88b0b1f28adc tuned signature;
wenzelm
parents: 58999
diff changeset
   114
      val toks = Token.explode(syntax.keywords, File.read(file))
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   115
      val ops =
59705
740a0ca7e09b clarified span position;
wenzelm
parents: 59692
diff changeset
   116
        parse_all(rep(parser), Token.reader(toks, Token.Pos.file(file.implode))) match {
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   117
          case Success(result, _) => result
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   118
          case bad => error(bad.toString)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   119
        }
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   120
      try { (options.set_section("") /: ops) { case (opts, op) => op(opts) } }
48992
0518bf89c777 renamed Position.str_of to Position.here;
wenzelm
parents: 48860
diff changeset
   121
      catch { case ERROR(msg) => error(msg + Position.here(file.position)) }
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   122
    }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   123
  }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   124
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   125
  def init_defaults(): Options =
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   126
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   127
    var options = empty
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   128
    for {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   129
      dir <- Isabelle_System.components()
48548
49afe0e92163 simplified Path vs. JVM File operations;
wenzelm
parents: 48457
diff changeset
   130
      file = dir + OPTIONS if file.is_file
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   131
    } { options = Parser.parse_file(options_syntax, Parser.option_entry, options, file) }
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   132
    options
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   133
  }
48457
fd9e28d5a143 pass build options to ML;
wenzelm
parents: 48456
diff changeset
   134
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   135
  def init(): Options = init_defaults().load_prefs()
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   136
48457
fd9e28d5a143 pass build options to ML;
wenzelm
parents: 48456
diff changeset
   137
fd9e28d5a143 pass build options to ML;
wenzelm
parents: 48456
diff changeset
   138
  /* encode */
fd9e28d5a143 pass build options to ML;
wenzelm
parents: 48456
diff changeset
   139
fd9e28d5a143 pass build options to ML;
wenzelm
parents: 48456
diff changeset
   140
  val encode: XML.Encode.T[Options] = (options => options.encode)
48693
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   141
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   142
62832
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   143
  /* Isabelle tool wrapper */
48693
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   144
62832
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   145
  val isabelle_tool = Isabelle_Tool("option", "print Isabelle system options", args =>
48693
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   146
  {
62832
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   147
    var build_options = false
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   148
    var get_option = ""
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   149
    var list_options = false
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   150
    var export_file = ""
62437
bccad0374407 moved getopts to Scala;
wenzelm
parents: 62227
diff changeset
   151
62832
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   152
    val getopts = Getopts("""
62437
bccad0374407 moved getopts to Scala;
wenzelm
parents: 62227
diff changeset
   153
Usage: isabelle options [OPTIONS] [MORE_OPTIONS ...]
48693
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   154
62437
bccad0374407 moved getopts to Scala;
wenzelm
parents: 62227
diff changeset
   155
  Options are:
bccad0374407 moved getopts to Scala;
wenzelm
parents: 62227
diff changeset
   156
    -b           include $ISABELLE_BUILD_OPTIONS
bccad0374407 moved getopts to Scala;
wenzelm
parents: 62227
diff changeset
   157
    -g OPTION    get value of OPTION
bccad0374407 moved getopts to Scala;
wenzelm
parents: 62227
diff changeset
   158
    -l           list options
bccad0374407 moved getopts to Scala;
wenzelm
parents: 62227
diff changeset
   159
    -x FILE      export options to FILE in YXML format
bccad0374407 moved getopts to Scala;
wenzelm
parents: 62227
diff changeset
   160
bccad0374407 moved getopts to Scala;
wenzelm
parents: 62227
diff changeset
   161
  Report Isabelle system options, augmented by MORE_OPTIONS given as
bccad0374407 moved getopts to Scala;
wenzelm
parents: 62227
diff changeset
   162
  arguments NAME=VAL or NAME.
bccad0374407 moved getopts to Scala;
wenzelm
parents: 62227
diff changeset
   163
""",
62832
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   164
      "b" -> (_ => build_options = true),
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   165
      "g:" -> (arg => get_option = arg),
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   166
      "l" -> (_ => list_options = true),
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   167
      "x:" -> (arg => export_file = arg))
52737
7b396ef36af6 clarified meaning of options for "isabelle options";
wenzelm
parents: 52735
diff changeset
   168
62832
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   169
    val more_options = getopts(args)
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   170
    if (get_option == "" && !list_options && export_file == "") getopts.usage()
52737
7b396ef36af6 clarified meaning of options for "isabelle options";
wenzelm
parents: 52735
diff changeset
   171
62832
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   172
    val options =
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   173
    {
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   174
      val options0 = Options.init()
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   175
      val options1 =
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   176
        if (build_options)
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   177
          (options0 /: Word.explode(Isabelle_System.getenv("ISABELLE_BUILD_OPTIONS")))(_ + _)
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   178
        else options0
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   179
      (options1 /: more_options)(_ + _)
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   180
    }
48693
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   181
62832
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   182
    if (get_option != "")
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   183
      Console.println(options.check_name(get_option).value)
62437
bccad0374407 moved getopts to Scala;
wenzelm
parents: 62227
diff changeset
   184
62832
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   185
    if (export_file != "")
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   186
      File.write(Path.explode(export_file), YXML.string_of_body(options.encode))
62437
bccad0374407 moved getopts to Scala;
wenzelm
parents: 62227
diff changeset
   187
62832
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   188
    if (get_option == "" && export_file == "")
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   189
      Console.println(options.print)
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   190
  })
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   191
}
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   192
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   193
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   194
final class Options private(
49296
313369027391 some GUI support for color options;
wenzelm
parents: 49295
diff changeset
   195
  val options: Map[String, Options.Opt] = Map.empty,
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   196
  val section: String = "")
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   197
{
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   198
  override def toString: String = options.iterator.mkString("Options (", ",", ")")
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   199
54347
d5589530f3ba clarified isabelle options -l;
wenzelm
parents: 53336
diff changeset
   200
  private def print_opt(opt: Options.Opt): String =
d5589530f3ba clarified isabelle options -l;
wenzelm
parents: 53336
diff changeset
   201
    if (opt.public) "public " + opt.print else opt.print
d5589530f3ba clarified isabelle options -l;
wenzelm
parents: 53336
diff changeset
   202
d5589530f3ba clarified isabelle options -l;
wenzelm
parents: 53336
diff changeset
   203
  def print: String = cat_lines(options.toList.sortBy(_._1).map(p => print_opt(p._2)))
48693
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   204
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   205
  def description(name: String): String = check_name(name).description
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   206
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   207
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   208
  /* check */
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   209
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
   210
  def check_name(name: String): Options.Opt =
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   211
    options.get(name) match {
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   212
      case Some(opt) if !opt.unknown => opt
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   213
      case _ => error("Unknown option " + quote(name))
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   214
    }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   215
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   216
  private def check_type(name: String, typ: Options.Type): Options.Opt =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   217
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   218
    val opt = check_name(name)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   219
    if (opt.typ == typ) opt
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   220
    else error("Ill-typed option " + quote(name) + " : " + opt.typ.print + " vs. " + typ.print)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   221
  }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   222
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   223
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   224
  /* basic operations */
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   225
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   226
  private def put[A](name: String, typ: Options.Type, value: String): Options =
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   227
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   228
    val opt = check_type(name, typ)
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   229
    new Options(options + (name -> opt.copy(value = value)), section)
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   230
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   231
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   232
  private def get[A](name: String, typ: Options.Type, parse: String => Option[A]): A =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   233
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   234
    val opt = check_type(name, typ)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   235
    parse(opt.value) match {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   236
      case Some(x) => x
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   237
      case None =>
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   238
        error("Malformed value for option " + quote(name) +
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   239
          " : " + typ.print + " =\n" + quote(opt.value))
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   240
    }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   241
  }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   242
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   243
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   244
  /* internal lookup and update */
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   245
49954
44658062d822 more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents: 49296
diff changeset
   246
  class Bool_Access
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   247
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   248
    def apply(name: String): Boolean = get(name, Options.Bool, Properties.Value.Boolean.unapply)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   249
    def update(name: String, x: Boolean): Options =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   250
      put(name, Options.Bool, Properties.Value.Boolean(x))
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   251
  }
49954
44658062d822 more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents: 49296
diff changeset
   252
  val bool = new Bool_Access
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   253
49954
44658062d822 more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents: 49296
diff changeset
   254
  class Int_Access
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   255
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   256
    def apply(name: String): Int = get(name, Options.Int, Properties.Value.Int.unapply)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   257
    def update(name: String, x: Int): Options =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   258
      put(name, Options.Int, Properties.Value.Int(x))
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   259
  }
49954
44658062d822 more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents: 49296
diff changeset
   260
  val int = new Int_Access
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   261
49954
44658062d822 more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents: 49296
diff changeset
   262
  class Real_Access
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   263
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   264
    def apply(name: String): Double = get(name, Options.Real, Properties.Value.Double.unapply)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   265
    def update(name: String, x: Double): Options =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   266
      put(name, Options.Real, Properties.Value.Double(x))
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   267
  }
49954
44658062d822 more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents: 49296
diff changeset
   268
  val real = new Real_Access
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   269
49954
44658062d822 more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents: 49296
diff changeset
   270
  class String_Access
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   271
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   272
    def apply(name: String): String = get(name, Options.String, s => Some(s))
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   273
    def update(name: String, x: String): Options = put(name, Options.String, x)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   274
  }
49954
44658062d822 more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents: 49296
diff changeset
   275
  val string = new String_Access
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   276
50207
54be125d8cdc tuned signature;
wenzelm
parents: 49954
diff changeset
   277
  class Seconds_Access
54be125d8cdc tuned signature;
wenzelm
parents: 49954
diff changeset
   278
  {
54be125d8cdc tuned signature;
wenzelm
parents: 49954
diff changeset
   279
    def apply(name: String): Time = Time.seconds(real(name))
54be125d8cdc tuned signature;
wenzelm
parents: 49954
diff changeset
   280
  }
54be125d8cdc tuned signature;
wenzelm
parents: 49954
diff changeset
   281
  val seconds = new Seconds_Access
54be125d8cdc tuned signature;
wenzelm
parents: 49954
diff changeset
   282
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   283
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   284
  /* external updates */
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   285
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   286
  private def check_value(name: String): Options =
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   287
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   288
    val opt = check_name(name)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   289
    opt.typ match {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   290
      case Options.Bool => bool(name); this
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   291
      case Options.Int => int(name); this
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   292
      case Options.Real => real(name); this
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   293
      case Options.String => string(name); this
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   294
      case Options.Unknown => this
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   295
    }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   296
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   297
56465
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   298
  def declare(
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   299
    public: Boolean,
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   300
    pos: Position.T,
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   301
    name: String,
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   302
    typ_name: String,
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   303
    value: String,
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   304
    description: String): Options =
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   305
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   306
    options.get(name) match {
56465
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   307
      case Some(other) =>
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   308
        error("Duplicate declaration of option " + quote(name) + Position.here(pos) +
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   309
          Position.here(other.pos))
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   310
      case None =>
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   311
        val typ =
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   312
          typ_name match {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   313
            case "bool" => Options.Bool
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   314
            case "int" => Options.Int
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   315
            case "real" => Options.Real
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   316
            case "string" => Options.String
56465
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   317
            case _ =>
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   318
              error("Unknown type for option " + quote(name) + " : " + quote(typ_name) +
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   319
                Position.here(pos))
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   320
          }
56465
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   321
        val opt = Options.Opt(public, pos, name, typ, value, value, description, section)
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   322
        (new Options(options + (name -> opt), section)).check_value(name)
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   323
    }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   324
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   325
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   326
  def add_permissive(name: String, value: String): Options =
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   327
  {
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   328
    if (options.isDefinedAt(name)) this + (name, value)
56465
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   329
    else {
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   330
      val opt = Options.Opt(false, Position.none, name, Options.Unknown, value, value, "", "")
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   331
      new Options(options + (name -> opt), section)
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   332
    }
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   333
  }
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   334
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   335
  def + (name: String, value: String): Options =
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   336
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   337
    val opt = check_name(name)
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   338
    (new Options(options + (name -> opt.copy(value = value)), section)).check_value(name)
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   339
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   340
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   341
  def + (name: String, opt_value: Option[String]): Options =
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   342
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   343
    val opt = check_name(name)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   344
    opt_value match {
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   345
      case Some(value) => this + (name, value)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   346
      case None if opt.typ == Options.Bool => this + (name, "true")
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   347
      case None => error("Missing value for option " + quote(name) + " : " + opt.typ.print)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   348
    }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   349
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   350
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   351
  def + (str: String): Options =
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   352
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   353
    str.indexOf('=') match {
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   354
      case -1 => this + (str, None)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   355
      case i => this + (str.substring(0, i), str.substring(i + 1))
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   356
    }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   357
  }
48456
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   358
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   359
  def ++ (specs: List[Options.Spec]): Options =
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   360
    (this /: specs)({ case (x, (y, z)) => x + (y, z) })
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   361
48456
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   362
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   363
  /* sections */
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   364
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   365
  def set_section(new_section: String): Options =
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   366
    new Options(options, new_section)
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   367
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   368
  def sections: List[(String, List[Options.Opt])] =
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   369
    options.groupBy(_._2.section).toList.map({ case (a, opts) => (a, opts.toList.map(_._2)) })
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   370
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   371
48456
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   372
  /* encode */
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   373
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   374
  def encode: XML.Body =
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   375
  {
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   376
    val opts =
56465
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   377
      for ((_, opt) <- options.toList; if !opt.unknown)
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   378
        yield (opt.pos, (opt.name, (opt.typ.print, opt.value)))
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   379
56465
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   380
    import XML.Encode.{string => string_, _}
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   381
    list(pair(properties, pair(string_, pair(string_, string_))))(opts)
48456
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   382
  }
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   383
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   384
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   385
  /* user preferences */
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   386
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   387
  def load_prefs(): Options =
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   388
    if (Options.PREFS.is_file)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   389
      Options.Parser.parse_file(
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   390
        Options.prefs_syntax, Options.Parser.prefs_entry, this, Options.PREFS)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   391
    else this
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   392
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   393
  def save_prefs()
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   394
  {
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   395
    val defaults = Options.init_defaults()
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   396
    val changed =
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   397
      (for {
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   398
        (name, opt2) <- options.iterator
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   399
        opt1 = defaults.options.get(name)
60215
5fb4990dfc73 misc tuning, based on warnings by IntelliJ IDEA;
wenzelm
parents: 60133
diff changeset
   400
        if opt1.isEmpty || opt1.get.value != opt2.value
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   401
      } yield (name, opt2.value, if (opt1.isEmpty) "  (* unknown *)" else "")).toList
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   402
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   403
    val prefs =
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   404
      changed.sortBy(_._1)
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   405
        .map({ case (x, y, z) => x + " = " + Outer_Syntax.quote_string(y) + z + "\n" }).mkString
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   406
50893
d55eb82ae77b Isabelle_System.mkdirs with explicit error checking (in accordance to ML version), e.g. relevant with read-only DMG file-system on Mac OS X;
wenzelm
parents: 50793
diff changeset
   407
    Isabelle_System.mkdirs(Options.PREFS_DIR)
53336
b3bf6d72fea5 more general backup files;
wenzelm
parents: 52737
diff changeset
   408
    File.write_backup(Options.PREFS,
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   409
      "(* generated by Isabelle " + Calendar.getInstance.getTime + " *)\n\n" + prefs)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   410
  }
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   411
}
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   412
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   413
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   414
class Options_Variable
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   415
{
62227
6eeaaefcea56 clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents: 61579
diff changeset
   416
  private var options: Option[Options] = None
6eeaaefcea56 clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents: 61579
diff changeset
   417
6eeaaefcea56 clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents: 61579
diff changeset
   418
  def store(new_options: Options): Unit = synchronized { options = Some(new_options) }
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   419
62227
6eeaaefcea56 clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents: 61579
diff changeset
   420
  def value: Options = synchronized {
6eeaaefcea56 clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents: 61579
diff changeset
   421
    options match {
6eeaaefcea56 clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents: 61579
diff changeset
   422
      case Some(opts) => opts
6eeaaefcea56 clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents: 61579
diff changeset
   423
      case None => error("Uninitialized Isabelle system options")
6eeaaefcea56 clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents: 61579
diff changeset
   424
    }
6eeaaefcea56 clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents: 61579
diff changeset
   425
  }
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   426
62227
6eeaaefcea56 clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents: 61579
diff changeset
   427
  private def upd(f: Options => Options): Unit = synchronized { options = Some(f(value)) }
6eeaaefcea56 clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents: 61579
diff changeset
   428
6eeaaefcea56 clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents: 61579
diff changeset
   429
  def + (name: String, x: String): Unit = upd(opts => opts + (name, x))
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   430
49954
44658062d822 more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents: 49296
diff changeset
   431
  class Bool_Access
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   432
  {
62227
6eeaaefcea56 clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents: 61579
diff changeset
   433
    def apply(name: String): Boolean = value.bool(name)
6eeaaefcea56 clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents: 61579
diff changeset
   434
    def update(name: String, x: Boolean): Unit = upd(opts => opts.bool.update(name, x))
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   435
  }
49954
44658062d822 more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents: 49296
diff changeset
   436
  val bool = new Bool_Access
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   437
49954
44658062d822 more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents: 49296
diff changeset
   438
  class Int_Access
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   439
  {
62227
6eeaaefcea56 clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents: 61579
diff changeset
   440
    def apply(name: String): Int = value.int(name)
6eeaaefcea56 clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents: 61579
diff changeset
   441
    def update(name: String, x: Int): Unit = upd(opts => opts.int.update(name, x))
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   442
  }
49954
44658062d822 more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents: 49296
diff changeset
   443
  val int = new Int_Access
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   444
49954
44658062d822 more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents: 49296
diff changeset
   445
  class Real_Access
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   446
  {
62227
6eeaaefcea56 clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents: 61579
diff changeset
   447
    def apply(name: String): Double = value.real(name)
6eeaaefcea56 clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents: 61579
diff changeset
   448
    def update(name: String, x: Double): Unit = upd(opts => opts.real.update(name, x))
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   449
  }
49954
44658062d822 more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents: 49296
diff changeset
   450
  val real = new Real_Access
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   451
49954
44658062d822 more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents: 49296
diff changeset
   452
  class String_Access
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   453
  {
62227
6eeaaefcea56 clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents: 61579
diff changeset
   454
    def apply(name: String): String = value.string(name)
6eeaaefcea56 clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents: 61579
diff changeset
   455
    def update(name: String, x: String): Unit = upd(opts => opts.string.update(name, x))
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   456
  }
49954
44658062d822 more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents: 49296
diff changeset
   457
  val string = new String_Access
50207
54be125d8cdc tuned signature;
wenzelm
parents: 49954
diff changeset
   458
54be125d8cdc tuned signature;
wenzelm
parents: 49954
diff changeset
   459
  class Seconds_Access
54be125d8cdc tuned signature;
wenzelm
parents: 49954
diff changeset
   460
  {
62227
6eeaaefcea56 clarified errors: more explicit treatment of uninitialized state;
wenzelm
parents: 61579
diff changeset
   461
    def apply(name: String): Time = value.seconds(name)
50207
54be125d8cdc tuned signature;
wenzelm
parents: 49954
diff changeset
   462
  }
54be125d8cdc tuned signature;
wenzelm
parents: 49954
diff changeset
   463
  val seconds = new Seconds_Access
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   464
}