src/Pure/System/options.scala
author wenzelm
Wed, 09 Jan 2013 21:21:41 +0100
changeset 50793 b93484db43ed
parent 50299 f70b3712040f
child 50893 d55eb82ae77b
permissions -rw-r--r--
create required PREFS_DIR;
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
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
     4
Stand-alone options with external string representation.
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
  {
50299
f70b3712040f renamed dockable "Prover Session" to "Theories";
wenzelm
parents: 50207
diff changeset
    24
    def print: String = Library.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
49289
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
    32
  case class Opt(name: String, typ: Type, value: String, default_value: String,
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
    33
    description: String, section: String)
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
    34
  {
49289
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
    35
    private def print(default: Boolean): String =
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
    36
    {
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
    37
      val x = if (default) default_value else value
49247
ffd9ad9dc35b more detailed option tooltip;
wenzelm
parents: 49246
diff changeset
    38
      "option " + name + " : " + typ.print + " = " +
49289
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
    39
        (if (typ == Options.String) quote(x) else x) +
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
    40
        (if (description == "") "" else "\n  -- " + quote(description))
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
    41
    }
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
    42
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
    43
    def print: String = print(false)
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
    44
    def print_default: String = print(true)
49247
ffd9ad9dc35b more detailed option tooltip;
wenzelm
parents: 49246
diff changeset
    45
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    46
    def title(strip: String): String =
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    47
    {
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    48
      val words = space_explode('_', name)
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    49
      val words1 =
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    50
        words match {
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    51
          case word :: rest if word == strip => rest
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    52
          case _ => words
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    53
        }
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    54
      words1.map(Library.capitalize).mkString(" ")
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    55
    }
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    56
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
    57
    def unknown: Boolean = typ == Unknown
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
    58
  }
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    59
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    60
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    61
  /* parsing */
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    62
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    63
  private val SECTION = "section"
48795
bece259ee055 clarified format of etc/options: only declarations, not re-definitions;
wenzelm
parents: 48718
diff changeset
    64
  private val OPTION = "option"
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    65
  private val OPTIONS = Path.explode("etc/options")
50793
b93484db43ed create required PREFS_DIR;
wenzelm
parents: 50299
diff changeset
    66
  private val PREFS_DIR = Path.explode("$ISABELLE_HOME_USER/etc")
b93484db43ed create required PREFS_DIR;
wenzelm
parents: 50299
diff changeset
    67
  private val PREFS = PREFS_DIR + Path.basic("preferences")
b93484db43ed create required PREFS_DIR;
wenzelm
parents: 50299
diff changeset
    68
  private val PREFS_BACKUP = PREFS_DIR + Path.basic("preferences~")
48713
de26cf3191a3 more token markers, based on actual outer syntax;
wenzelm
parents: 48693
diff changeset
    69
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    70
  lazy val options_syntax =
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    71
    Outer_Syntax.init() + ":" + "=" + "--" +
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    72
      (SECTION, Keyword.THY_HEADING2) + (OPTION, Keyword.THY_DECL)
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    73
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    74
  lazy val prefs_syntax = Outer_Syntax.init() + "="
48713
de26cf3191a3 more token markers, based on actual outer syntax;
wenzelm
parents: 48693
diff changeset
    75
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    76
  object Parser extends Parse.Parser
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    77
  {
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    78
    val option_name = atom("option name", _.is_xname)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    79
    val option_type = atom("option type", _.is_ident)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    80
    val option_value =
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    81
      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
    82
        { case s ~ n => if (s.isDefined) "-" + n else n } |
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    83
      atom("option value", tok => tok.is_name || tok.is_float)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    84
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    85
    val option_entry: Parser[Options => Options] =
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    86
    {
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
    87
      command(SECTION) ~! text ^^
49295
2750756db9c5 more precise sections;
wenzelm
parents: 49289
diff changeset
    88
        { case _ ~ a => (options: Options) => options.set_section(a) } |
48795
bece259ee055 clarified format of etc/options: only declarations, not re-definitions;
wenzelm
parents: 48718
diff changeset
    89
      command(OPTION) ~! (option_name ~ keyword(":") ~ option_type ~
48580
9df76dd45900 some description of main build options;
wenzelm
parents: 48548
diff changeset
    90
      keyword("=") ~ option_value ~ (keyword("--") ~! text ^^ { case _ ~ x => x } | success(""))) ^^
48795
bece259ee055 clarified format of etc/options: only declarations, not re-definitions;
wenzelm
parents: 48718
diff changeset
    91
        { case _ ~ (a ~ _ ~ b ~ _ ~ c ~ d) => (options: Options) => options.declare(a, b, c, d) }
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    92
    }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    93
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    94
    val prefs_entry: Parser[Options => Options] =
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    95
    {
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    96
      option_name ~ (keyword("=") ~! option_value) ^^
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
    97
      { case a ~ (_ ~ b) => (options: Options) => options.add_permissive(a, b) }
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    98
    }
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
    99
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   100
    def parse_file(syntax: Outer_Syntax, parser: Parser[Options => Options],
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   101
      options: Options, file: Path): Options =
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   102
    {
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   103
      val toks = syntax.scan(File.read(file))
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   104
      val ops =
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   105
        parse_all(rep(parser), Token.reader(toks, file.implode)) match {
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   106
          case Success(result, _) => result
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   107
          case bad => error(bad.toString)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   108
        }
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   109
      try { (options.set_section("") /: ops) { case (opts, op) => op(opts) } }
48992
0518bf89c777 renamed Position.str_of to Position.here;
wenzelm
parents: 48860
diff changeset
   110
      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
   111
    }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   112
  }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   113
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   114
  def init_defaults(): Options =
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   115
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   116
    var options = empty
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   117
    for {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   118
      dir <- Isabelle_System.components()
48548
49afe0e92163 simplified Path vs. JVM File operations;
wenzelm
parents: 48457
diff changeset
   119
      file = dir + OPTIONS if file.is_file
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   120
    } { 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
   121
    options
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   122
  }
48457
fd9e28d5a143 pass build options to ML;
wenzelm
parents: 48456
diff changeset
   123
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   124
  def init(): Options = init_defaults().load_prefs()
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   125
48457
fd9e28d5a143 pass build options to ML;
wenzelm
parents: 48456
diff changeset
   126
fd9e28d5a143 pass build options to ML;
wenzelm
parents: 48456
diff changeset
   127
  /* encode */
fd9e28d5a143 pass build options to ML;
wenzelm
parents: 48456
diff changeset
   128
fd9e28d5a143 pass build options to ML;
wenzelm
parents: 48456
diff changeset
   129
  val encode: XML.Encode.T[Options] = (options => options.encode)
48693
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   130
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   131
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   132
  /* command line entry point */
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   133
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   134
  def main(args: Array[String])
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   135
  {
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   136
    Command_Line.tool {
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   137
      args.toList match {
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   138
        case export_file :: more_options =>
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   139
          val options = (Options.init() /: more_options)(_ + _)
48693
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   140
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   141
          if (export_file == "") java.lang.System.out.println(options.print)
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   142
          else File.write(Path.explode(export_file), YXML.string_of_body(options.encode))
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   143
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   144
          0
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   145
        case _ => error("Bad arguments:\n" + cat_lines(args))
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   146
      }
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   147
    }
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   148
  }
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   149
}
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   150
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   151
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   152
final class Options private(
49296
313369027391 some GUI support for color options;
wenzelm
parents: 49295
diff changeset
   153
  val options: Map[String, Options.Opt] = Map.empty,
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   154
  val section: String = "")
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   155
{
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   156
  override def toString: String = options.iterator.mkString("Options (", ",", ")")
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   157
49247
ffd9ad9dc35b more detailed option tooltip;
wenzelm
parents: 49246
diff changeset
   158
  def print: String = cat_lines(options.toList.sortBy(_._1).map(p => p._2.print))
48693
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   159
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   160
  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
   161
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   162
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   163
  /* check */
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   164
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
   165
  def check_name(name: String): Options.Opt =
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   166
    options.get(name) match {
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   167
      case Some(opt) if !opt.unknown => opt
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   168
      case _ => error("Unknown option " + quote(name))
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   169
    }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   170
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   171
  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
   172
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   173
    val opt = check_name(name)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   174
    if (opt.typ == typ) opt
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   175
    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
   176
  }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   177
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   178
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   179
  /* basic operations */
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   180
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   181
  private def put[A](name: String, typ: Options.Type, value: String): Options =
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   182
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   183
    val opt = check_type(name, typ)
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   184
    new Options(options + (name -> opt.copy(value = value)), section)
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   185
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   186
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   187
  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
   188
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   189
    val opt = check_type(name, typ)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   190
    parse(opt.value) match {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   191
      case Some(x) => x
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   192
      case None =>
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   193
        error("Malformed value for option " + quote(name) +
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   194
          " : " + typ.print + " =\n" + quote(opt.value))
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   195
    }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   196
  }
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
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   199
  /* internal lookup and update */
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   200
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
   201
  class Bool_Access
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   202
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   203
    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
   204
    def update(name: String, x: Boolean): Options =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   205
      put(name, Options.Bool, Properties.Value.Boolean(x))
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   206
  }
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
   207
  val bool = new Bool_Access
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   208
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
   209
  class Int_Access
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   210
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   211
    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
   212
    def update(name: String, x: Int): Options =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   213
      put(name, Options.Int, Properties.Value.Int(x))
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   214
  }
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
   215
  val int = new Int_Access
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   216
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
   217
  class Real_Access
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   218
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   219
    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
   220
    def update(name: String, x: Double): Options =
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   221
      put(name, Options.Real, Properties.Value.Double(x))
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   222
  }
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
   223
  val real = new Real_Access
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   224
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
   225
  class String_Access
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   226
  {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   227
    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
   228
    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
   229
  }
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
   230
  val string = new String_Access
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   231
50207
54be125d8cdc tuned signature;
wenzelm
parents: 49954
diff changeset
   232
  class Seconds_Access
54be125d8cdc tuned signature;
wenzelm
parents: 49954
diff changeset
   233
  {
54be125d8cdc tuned signature;
wenzelm
parents: 49954
diff changeset
   234
    def apply(name: String): Time = Time.seconds(real(name))
54be125d8cdc tuned signature;
wenzelm
parents: 49954
diff changeset
   235
  }
54be125d8cdc tuned signature;
wenzelm
parents: 49954
diff changeset
   236
  val seconds = new Seconds_Access
54be125d8cdc tuned signature;
wenzelm
parents: 49954
diff changeset
   237
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   238
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   239
  /* external updates */
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   240
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   241
  private def check_value(name: String): Options =
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   242
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   243
    val opt = check_name(name)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   244
    opt.typ match {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   245
      case Options.Bool => bool(name); this
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   246
      case Options.Int => int(name); this
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   247
      case Options.Real => real(name); this
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   248
      case Options.String => string(name); this
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   249
      case Options.Unknown => this
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   250
    }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   251
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   252
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   253
  def declare(name: String, typ_name: String, value: String, description: String = ""): Options =
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   254
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   255
    options.get(name) match {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   256
      case Some(_) => error("Duplicate declaration of option " + quote(name))
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   257
      case None =>
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   258
        val typ =
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   259
          typ_name match {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   260
            case "bool" => Options.Bool
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   261
            case "int" => Options.Int
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   262
            case "real" => Options.Real
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   263
            case "string" => Options.String
48456
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   264
            case _ => error("Unknown type for option " + quote(name) + " : " + quote(typ_name))
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   265
          }
49289
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
   266
        val opt = Options.Opt(name, typ, value, value, description, section)
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   267
        (new Options(options + (name -> opt), section)).check_value(name)
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   268
    }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   269
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   270
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   271
  def add_permissive(name: String, value: String): Options =
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   272
  {
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   273
    if (options.isDefinedAt(name)) this + (name, value)
49289
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
   274
    else
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
   275
      new Options(
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
   276
        options + (name -> Options.Opt(name, Options.Unknown, value, value, "", "")), section)
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   277
  }
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   278
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   279
  def + (name: String, value: String): Options =
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   280
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   281
    val opt = check_name(name)
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   282
    (new Options(options + (name -> opt.copy(value = value)), section)).check_value(name)
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   283
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   284
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   285
  def + (name: String, opt_value: Option[String]): Options =
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   286
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   287
    val opt = check_name(name)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   288
    opt_value match {
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   289
      case Some(value) => this + (name, value)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   290
      case None if opt.typ == Options.Bool => this + (name, "true")
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   291
      case None => error("Missing value for option " + quote(name) + " : " + opt.typ.print)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   292
    }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   293
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   294
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   295
  def + (str: String): Options =
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   296
  {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   297
    str.indexOf('=') match {
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   298
      case -1 => this + (str, None)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   299
      case i => this + (str.substring(0, i), str.substring(i + 1))
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   300
    }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   301
  }
48456
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   302
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   303
  def ++ (specs: List[Options.Spec]): Options =
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   304
    (this /: specs)({ case (x, (y, z)) => x + (y, z) })
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   305
48456
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   306
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   307
  /* sections */
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   308
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   309
  def set_section(new_section: String): Options =
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   310
    new Options(options, new_section)
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   311
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   312
  def sections: List[(String, List[Options.Opt])] =
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   313
    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
   314
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   315
48456
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   316
  /* encode */
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   317
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   318
  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
   319
  {
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   320
    val opts =
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   321
      for ((name, opt) <- options.toList; if !opt.unknown)
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   322
        yield (name, opt.typ.print, opt.value)
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   323
48456
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   324
    import XML.Encode.{string => str, _}
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   325
    list(triple(str, str, str))(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
   326
  }
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   327
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   328
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   329
  /* user preferences */
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   330
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   331
  def load_prefs(): Options =
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   332
    if (Options.PREFS.is_file)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   333
      Options.Parser.parse_file(
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   334
        Options.prefs_syntax, Options.Parser.prefs_entry, this, Options.PREFS)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   335
    else this
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   336
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   337
  def save_prefs()
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   338
  {
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   339
    val defaults = Options.init_defaults()
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   340
    val changed =
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   341
      (for {
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   342
        (name, opt2) <- options.iterator
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   343
        opt1 = defaults.options.get(name)
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   344
        if (opt1.isEmpty || opt1.get.value != opt2.value)
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   345
      } yield (name, opt2.value, if (opt1.isEmpty) "  (* unknown *)" else "")).toList
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   346
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   347
    val prefs =
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   348
      changed.sortBy(_._1)
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   349
        .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
   350
50793
b93484db43ed create required PREFS_DIR;
wenzelm
parents: 50299
diff changeset
   351
    Options.PREFS_DIR.file.mkdirs()
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   352
    Options.PREFS.file renameTo Options.PREFS_BACKUP.file
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   353
    File.write(Options.PREFS,
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   354
      "(* generated by Isabelle " + Calendar.getInstance.getTime + " *)\n\n" + prefs)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   355
  }
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   356
}
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   357
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   358
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   359
class Options_Variable
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   360
{
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   361
  // owned by Swing thread
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   362
  @volatile private var options = Options.empty
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   363
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   364
  def value: Options = options
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   365
  def update(new_options: Options)
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   366
  {
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   367
    Swing_Thread.require()
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   368
    options = new_options
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   369
  }
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   370
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   371
  def + (name: String, x: String)
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   372
  {
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   373
    Swing_Thread.require()
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   374
    options = options + (name, x)
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   375
  }
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   376
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
   377
  class Bool_Access
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   378
  {
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   379
    def apply(name: String): Boolean = options.bool(name)
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   380
    def update(name: String, x: Boolean)
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   381
    {
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   382
      Swing_Thread.require()
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   383
      options = options.bool.update(name, x)
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   384
    }
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   385
  }
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
   386
  val bool = new Bool_Access
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   387
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
   388
  class Int_Access
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   389
  {
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   390
    def apply(name: String): Int = options.int(name)
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   391
    def update(name: String, x: Int)
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   392
    {
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   393
      Swing_Thread.require()
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   394
      options = options.int.update(name, x)
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   395
    }
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   396
  }
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
   397
  val int = new Int_Access
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   398
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
   399
  class Real_Access
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   400
  {
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   401
    def apply(name: String): Double = options.real(name)
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   402
    def update(name: String, x: Double)
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   403
    {
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   404
      Swing_Thread.require()
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   405
      options = options.real.update(name, x)
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   406
    }
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   407
  }
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
   408
  val real = new Real_Access
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   409
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
   410
  class String_Access
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   411
  {
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   412
    def apply(name: String): String = options.string(name)
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   413
    def update(name: String, x: String)
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   414
    {
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   415
      Swing_Thread.require()
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   416
      options = options.string.update(name, x)
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   417
    }
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   418
  }
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
   419
  val string = new String_Access
50207
54be125d8cdc tuned signature;
wenzelm
parents: 49954
diff changeset
   420
54be125d8cdc tuned signature;
wenzelm
parents: 49954
diff changeset
   421
  class Seconds_Access
54be125d8cdc tuned signature;
wenzelm
parents: 49954
diff changeset
   422
  {
54be125d8cdc tuned signature;
wenzelm
parents: 49954
diff changeset
   423
    def apply(name: String): Time = options.seconds(name)
54be125d8cdc tuned signature;
wenzelm
parents: 49954
diff changeset
   424
  }
54be125d8cdc tuned signature;
wenzelm
parents: 49954
diff changeset
   425
  val seconds = new Seconds_Access
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   426
}
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   427