src/Pure/System/options.scala
author wenzelm
Mon Sep 10 15:20:50 2012 +0200 (2012-09-10)
changeset 49245 cb70157293c0
parent 48992 0518bf89c777
child 49246 248e66e8321f
permissions -rw-r--r--
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm@48365
     1
/*  Title:      Pure/System/options.scala
wenzelm@48365
     2
    Author:     Makarius
wenzelm@48365
     3
wenzelm@48365
     4
Stand-alone options with external string representation.
wenzelm@48365
     5
*/
wenzelm@48365
     6
wenzelm@48365
     7
package isabelle
wenzelm@48365
     8
wenzelm@48365
     9
wenzelm@49245
    10
import java.util.Locale
wenzelm@48807
    11
import java.util.Calendar
wenzelm@48807
    12
wenzelm@48807
    13
wenzelm@48365
    14
object Options
wenzelm@48365
    15
{
wenzelm@48421
    16
  type Spec = (String, Option[String])
wenzelm@48421
    17
wenzelm@48421
    18
  val empty: Options = new Options()
wenzelm@48421
    19
wenzelm@48421
    20
wenzelm@48421
    21
  /* representation */
wenzelm@48421
    22
wenzelm@48421
    23
  sealed abstract class Type
wenzelm@48365
    24
  {
wenzelm@49245
    25
    def print: String = toString.toLowerCase(Locale.ENGLISH)
wenzelm@48365
    26
  }
wenzelm@48421
    27
  private case object Bool extends Type
wenzelm@48421
    28
  private case object Int extends Type
wenzelm@48421
    29
  private case object Real extends Type
wenzelm@48421
    30
  private case object String extends Type
wenzelm@48860
    31
  private case object Unknown extends Type
wenzelm@48365
    32
wenzelm@48370
    33
  case class Opt(typ: Type, value: String, description: String)
wenzelm@48860
    34
  {
wenzelm@48860
    35
    def unknown: Boolean = typ == Unknown
wenzelm@48860
    36
  }
wenzelm@48365
    37
wenzelm@48365
    38
wenzelm@48365
    39
  /* parsing */
wenzelm@48365
    40
wenzelm@48795
    41
  private val OPTION = "option"
wenzelm@48807
    42
  private val OPTIONS = Path.explode("etc/options")
wenzelm@48807
    43
  private val PREFS = Path.explode("$ISABELLE_HOME_USER/etc/preferences")
wenzelm@48807
    44
  private val PREFS_BACKUP = Path.explode("$ISABELLE_HOME_USER/etc/preferences~")
wenzelm@48713
    45
wenzelm@48807
    46
  lazy val options_syntax = Outer_Syntax.init() + ":" + "=" + "--" + (OPTION, Keyword.THY_DECL)
wenzelm@48807
    47
  lazy val prefs_syntax = Outer_Syntax.init() + "="
wenzelm@48713
    48
wenzelm@48807
    49
  object Parser extends Parse.Parser
wenzelm@48365
    50
  {
wenzelm@48807
    51
    val option_name = atom("option name", _.is_xname)
wenzelm@48807
    52
    val option_type = atom("option type", _.is_ident)
wenzelm@48807
    53
    val option_value =
wenzelm@48807
    54
      opt(token("-", tok => tok.is_sym_ident && tok.content == "-")) ~ atom("nat", _.is_nat) ^^
wenzelm@48807
    55
        { case s ~ n => if (s.isDefined) "-" + n else n } |
wenzelm@48807
    56
      atom("option value", tok => tok.is_name || tok.is_float)
wenzelm@48807
    57
wenzelm@48807
    58
    val option_entry: Parser[Options => Options] =
wenzelm@48365
    59
    {
wenzelm@48795
    60
      command(OPTION) ~! (option_name ~ keyword(":") ~ option_type ~
wenzelm@48580
    61
      keyword("=") ~ option_value ~ (keyword("--") ~! text ^^ { case _ ~ x => x } | success(""))) ^^
wenzelm@48795
    62
        { case _ ~ (a ~ _ ~ b ~ _ ~ c ~ d) => (options: Options) => options.declare(a, b, c, d) }
wenzelm@48365
    63
    }
wenzelm@48365
    64
wenzelm@48807
    65
    val prefs_entry: Parser[Options => Options] =
wenzelm@48807
    66
    {
wenzelm@48807
    67
      option_name ~ (keyword("=") ~! option_value) ^^
wenzelm@48860
    68
      { case a ~ (_ ~ b) => (options: Options) => options.add_permissive(a, b) }
wenzelm@48807
    69
    }
wenzelm@48807
    70
wenzelm@48807
    71
    def parse_file(syntax: Outer_Syntax, parser: Parser[Options => Options],
wenzelm@48807
    72
      options: Options, file: Path): Options =
wenzelm@48365
    73
    {
wenzelm@48807
    74
      val toks = syntax.scan(File.read(file))
wenzelm@48807
    75
      val ops =
wenzelm@48807
    76
        parse_all(rep(parser), Token.reader(toks, file.implode)) match {
wenzelm@48807
    77
          case Success(result, _) => result
wenzelm@48807
    78
          case bad => error(bad.toString)
wenzelm@48807
    79
        }
wenzelm@48807
    80
      try { (options /: ops) { case (opts, op) => op(opts) } }
wenzelm@48992
    81
      catch { case ERROR(msg) => error(msg + Position.here(file.position)) }
wenzelm@48365
    82
    }
wenzelm@48365
    83
  }
wenzelm@48365
    84
wenzelm@48807
    85
  def init_defaults(): Options =
wenzelm@48365
    86
  {
wenzelm@48365
    87
    var options = empty
wenzelm@48365
    88
    for {
wenzelm@48365
    89
      dir <- Isabelle_System.components()
wenzelm@48548
    90
      file = dir + OPTIONS if file.is_file
wenzelm@48807
    91
    } { options = Parser.parse_file(options_syntax, Parser.option_entry, options, file) }
wenzelm@48365
    92
    options
wenzelm@48365
    93
  }
wenzelm@48457
    94
wenzelm@48807
    95
  def init(): Options = init_defaults().load_prefs()
wenzelm@48807
    96
wenzelm@48457
    97
wenzelm@48457
    98
  /* encode */
wenzelm@48457
    99
wenzelm@48457
   100
  val encode: XML.Encode.T[Options] = (options => options.encode)
wenzelm@48693
   101
wenzelm@48693
   102
wenzelm@48693
   103
  /* command line entry point */
wenzelm@48693
   104
wenzelm@48693
   105
  def main(args: Array[String])
wenzelm@48693
   106
  {
wenzelm@48693
   107
    Command_Line.tool {
wenzelm@48693
   108
      args.toList match {
wenzelm@48693
   109
        case export_file :: more_options =>
wenzelm@48807
   110
          val options = (Options.init() /: more_options)(_ + _)
wenzelm@48693
   111
wenzelm@48693
   112
          if (export_file == "") java.lang.System.out.println(options.print)
wenzelm@48693
   113
          else File.write(Path.explode(export_file), YXML.string_of_body(options.encode))
wenzelm@48693
   114
wenzelm@48693
   115
          0
wenzelm@48693
   116
        case _ => error("Bad arguments:\n" + cat_lines(args))
wenzelm@48693
   117
      }
wenzelm@48693
   118
    }
wenzelm@48693
   119
  }
wenzelm@48365
   120
}
wenzelm@48365
   121
wenzelm@48365
   122
wenzelm@48807
   123
final class Options private(protected val options: Map[String, Options.Opt] = Map.empty)
wenzelm@48365
   124
{
wenzelm@48365
   125
  override def toString: String = options.iterator.mkString("Options (", ",", ")")
wenzelm@48365
   126
wenzelm@48693
   127
  def print: String =
wenzelm@48693
   128
    cat_lines(options.toList.sortBy(_._1).map({ case (name, opt) =>
wenzelm@48795
   129
      "option " + name + " : " + opt.typ.print + " = " +
wenzelm@48693
   130
        (if (opt.typ == Options.String) quote(opt.value) else opt.value) +
wenzelm@48860
   131
      (if (opt.description == "") "" else "\n  -- " + quote(opt.description)) }))
wenzelm@48693
   132
wenzelm@49245
   133
  def title(strip: String, name: String): String =
wenzelm@49245
   134
  {
wenzelm@49245
   135
    check_name(name)
wenzelm@49245
   136
    val words = space_explode('_', name)
wenzelm@49245
   137
    val words1 =
wenzelm@49245
   138
      words match {
wenzelm@49245
   139
        case word :: rest if word == strip => rest
wenzelm@49245
   140
        case _ => words
wenzelm@49245
   141
      }
wenzelm@49245
   142
    words1.map(Library.capitalize).mkString(" ")
wenzelm@49245
   143
  }
wenzelm@49245
   144
wenzelm@49245
   145
  def description(name: String): String = check_name(name).description
wenzelm@49245
   146
wenzelm@48365
   147
wenzelm@48370
   148
  /* check */
wenzelm@48365
   149
wenzelm@48365
   150
  private def check_name(name: String): Options.Opt =
wenzelm@48365
   151
    options.get(name) match {
wenzelm@48860
   152
      case Some(opt) if !opt.unknown => opt
wenzelm@48860
   153
      case _ => error("Unknown option " + quote(name))
wenzelm@48365
   154
    }
wenzelm@48365
   155
wenzelm@48365
   156
  private def check_type(name: String, typ: Options.Type): Options.Opt =
wenzelm@48365
   157
  {
wenzelm@48365
   158
    val opt = check_name(name)
wenzelm@48365
   159
    if (opt.typ == typ) opt
wenzelm@48365
   160
    else error("Ill-typed option " + quote(name) + " : " + opt.typ.print + " vs. " + typ.print)
wenzelm@48365
   161
  }
wenzelm@48365
   162
wenzelm@48370
   163
wenzelm@48370
   164
  /* basic operations */
wenzelm@48370
   165
wenzelm@48370
   166
  private def put[A](name: String, typ: Options.Type, value: String): Options =
wenzelm@48370
   167
  {
wenzelm@48370
   168
    val opt = check_type(name, typ)
wenzelm@48370
   169
    new Options(options + (name -> opt.copy(value = value)))
wenzelm@48370
   170
  }
wenzelm@48370
   171
wenzelm@48365
   172
  private def get[A](name: String, typ: Options.Type, parse: String => Option[A]): A =
wenzelm@48365
   173
  {
wenzelm@48365
   174
    val opt = check_type(name, typ)
wenzelm@48365
   175
    parse(opt.value) match {
wenzelm@48365
   176
      case Some(x) => x
wenzelm@48365
   177
      case None =>
wenzelm@48365
   178
        error("Malformed value for option " + quote(name) +
wenzelm@48365
   179
          " : " + typ.print + " =\n" + quote(opt.value))
wenzelm@48365
   180
    }
wenzelm@48365
   181
  }
wenzelm@48365
   182
wenzelm@48365
   183
wenzelm@48365
   184
  /* internal lookup and update */
wenzelm@48365
   185
wenzelm@48365
   186
  val bool = new Object
wenzelm@48365
   187
  {
wenzelm@48365
   188
    def apply(name: String): Boolean = get(name, Options.Bool, Properties.Value.Boolean.unapply)
wenzelm@48365
   189
    def update(name: String, x: Boolean): Options =
wenzelm@48365
   190
      put(name, Options.Bool, Properties.Value.Boolean(x))
wenzelm@48365
   191
  }
wenzelm@48365
   192
wenzelm@48365
   193
  val int = new Object
wenzelm@48365
   194
  {
wenzelm@48365
   195
    def apply(name: String): Int = get(name, Options.Int, Properties.Value.Int.unapply)
wenzelm@48365
   196
    def update(name: String, x: Int): Options =
wenzelm@48365
   197
      put(name, Options.Int, Properties.Value.Int(x))
wenzelm@48365
   198
  }
wenzelm@48365
   199
wenzelm@48365
   200
  val real = new Object
wenzelm@48365
   201
  {
wenzelm@48365
   202
    def apply(name: String): Double = get(name, Options.Real, Properties.Value.Double.unapply)
wenzelm@48365
   203
    def update(name: String, x: Double): Options =
wenzelm@48365
   204
      put(name, Options.Real, Properties.Value.Double(x))
wenzelm@48365
   205
  }
wenzelm@48365
   206
wenzelm@48365
   207
  val string = new Object
wenzelm@48365
   208
  {
wenzelm@48365
   209
    def apply(name: String): String = get(name, Options.String, s => Some(s))
wenzelm@48365
   210
    def update(name: String, x: String): Options = put(name, Options.String, x)
wenzelm@48365
   211
  }
wenzelm@48370
   212
wenzelm@48370
   213
wenzelm@48807
   214
  /* external updates */
wenzelm@48370
   215
wenzelm@48370
   216
  private def check_value(name: String): Options =
wenzelm@48370
   217
  {
wenzelm@48370
   218
    val opt = check_name(name)
wenzelm@48370
   219
    opt.typ match {
wenzelm@48370
   220
      case Options.Bool => bool(name); this
wenzelm@48370
   221
      case Options.Int => int(name); this
wenzelm@48370
   222
      case Options.Real => real(name); this
wenzelm@48370
   223
      case Options.String => string(name); this
wenzelm@48860
   224
      case Options.Unknown => this
wenzelm@48370
   225
    }
wenzelm@48370
   226
  }
wenzelm@48370
   227
wenzelm@48370
   228
  def declare(name: String, typ_name: String, value: String, description: String = ""): Options =
wenzelm@48370
   229
  {
wenzelm@48370
   230
    options.get(name) match {
wenzelm@48370
   231
      case Some(_) => error("Duplicate declaration of option " + quote(name))
wenzelm@48370
   232
      case None =>
wenzelm@48370
   233
        val typ =
wenzelm@48370
   234
          typ_name match {
wenzelm@48370
   235
            case "bool" => Options.Bool
wenzelm@48370
   236
            case "int" => Options.Int
wenzelm@48370
   237
            case "real" => Options.Real
wenzelm@48370
   238
            case "string" => Options.String
wenzelm@48456
   239
            case _ => error("Unknown type for option " + quote(name) + " : " + quote(typ_name))
wenzelm@48370
   240
          }
wenzelm@48807
   241
        val opt = Options.Opt(typ, value, description)
wenzelm@48807
   242
        (new Options(options + (name -> opt))).check_value(name)
wenzelm@48370
   243
    }
wenzelm@48370
   244
  }
wenzelm@48370
   245
wenzelm@48860
   246
  def add_permissive(name: String, value: String): Options =
wenzelm@48860
   247
  {
wenzelm@48860
   248
    if (options.isDefinedAt(name)) this + (name, value)
wenzelm@48860
   249
    else new Options(options + (name -> Options.Opt(Options.Unknown, value, "")))
wenzelm@48860
   250
  }
wenzelm@48860
   251
wenzelm@48807
   252
  def + (name: String, value: String): Options =
wenzelm@48370
   253
  {
wenzelm@48370
   254
    val opt = check_name(name)
wenzelm@48370
   255
    (new Options(options + (name -> opt.copy(value = value)))).check_value(name)
wenzelm@48370
   256
  }
wenzelm@48370
   257
wenzelm@48807
   258
  def + (name: String, opt_value: Option[String]): Options =
wenzelm@48370
   259
  {
wenzelm@48370
   260
    val opt = check_name(name)
wenzelm@48370
   261
    opt_value match {
wenzelm@48807
   262
      case Some(value) => this + (name, value)
wenzelm@48807
   263
      case None if opt.typ == Options.Bool => this + (name, "true")
wenzelm@48370
   264
      case None => error("Missing value for option " + quote(name) + " : " + opt.typ.print)
wenzelm@48370
   265
    }
wenzelm@48370
   266
  }
wenzelm@48370
   267
wenzelm@48807
   268
  def + (str: String): Options =
wenzelm@48370
   269
  {
wenzelm@48370
   270
    str.indexOf('=') match {
wenzelm@48807
   271
      case -1 => this + (str, None)
wenzelm@48807
   272
      case i => this + (str.substring(0, i), str.substring(i + 1))
wenzelm@48370
   273
    }
wenzelm@48370
   274
  }
wenzelm@48456
   275
wenzelm@48807
   276
  def ++ (specs: List[Options.Spec]): Options =
wenzelm@48807
   277
    (this /: specs)({ case (x, (y, z)) => x + (y, z) })
wenzelm@48807
   278
wenzelm@48456
   279
wenzelm@48456
   280
  /* encode */
wenzelm@48456
   281
wenzelm@48456
   282
  def encode: XML.Body =
wenzelm@48456
   283
  {
wenzelm@48860
   284
    val opts =
wenzelm@48860
   285
      for ((name, opt) <- options.toList; if !opt.unknown)
wenzelm@48860
   286
        yield (name, opt.typ.print, opt.value)
wenzelm@48860
   287
wenzelm@48456
   288
    import XML.Encode.{string => str, _}
wenzelm@48860
   289
    list(triple(str, str, str))(opts)
wenzelm@48456
   290
  }
wenzelm@48807
   291
wenzelm@48807
   292
wenzelm@48807
   293
  /* user preferences */
wenzelm@48807
   294
wenzelm@48807
   295
  def load_prefs(): Options =
wenzelm@48807
   296
    if (Options.PREFS.is_file)
wenzelm@48807
   297
      Options.Parser.parse_file(
wenzelm@48807
   298
        Options.prefs_syntax, Options.Parser.prefs_entry, this, Options.PREFS)
wenzelm@48807
   299
    else this
wenzelm@48807
   300
wenzelm@48807
   301
  def save_prefs()
wenzelm@48807
   302
  {
wenzelm@48860
   303
    val defaults = Options.init_defaults()
wenzelm@48807
   304
    val changed =
wenzelm@48807
   305
      (for {
wenzelm@48860
   306
        (name, opt2) <- options.iterator
wenzelm@48860
   307
        opt1 = defaults.options.get(name)
wenzelm@48860
   308
        if (opt1.isEmpty || opt1.get.value != opt2.value)
wenzelm@48860
   309
      } yield (name, opt2.value, if (opt1.isEmpty) "  (* unknown *)" else "")).toList
wenzelm@48860
   310
wenzelm@48807
   311
    val prefs =
wenzelm@48807
   312
      changed.sortBy(_._1)
wenzelm@48860
   313
        .map({ case (x, y, z) => x + " = " + Outer_Syntax.quote_string(y) + z + "\n" }).mkString
wenzelm@48807
   314
wenzelm@48807
   315
    Options.PREFS.file renameTo Options.PREFS_BACKUP.file
wenzelm@48807
   316
    File.write(Options.PREFS,
wenzelm@48807
   317
      "(* generated by Isabelle " + Calendar.getInstance.getTime + " *)\n\n" + prefs)
wenzelm@48807
   318
  }
wenzelm@48365
   319
}
wenzelm@49245
   320
wenzelm@49245
   321
wenzelm@49245
   322
class Options_Variable
wenzelm@49245
   323
{
wenzelm@49245
   324
  // owned by Swing thread
wenzelm@49245
   325
  @volatile private var options = Options.empty
wenzelm@49245
   326
wenzelm@49245
   327
  def value: Options = options
wenzelm@49245
   328
  def update(new_options: Options)
wenzelm@49245
   329
  {
wenzelm@49245
   330
    Swing_Thread.require()
wenzelm@49245
   331
    options = new_options
wenzelm@49245
   332
  }
wenzelm@49245
   333
wenzelm@49245
   334
  def + (name: String, x: String)
wenzelm@49245
   335
  {
wenzelm@49245
   336
    Swing_Thread.require()
wenzelm@49245
   337
    options = options + (name, x)
wenzelm@49245
   338
  }
wenzelm@49245
   339
wenzelm@49245
   340
  val bool = new Object
wenzelm@49245
   341
  {
wenzelm@49245
   342
    def apply(name: String): Boolean = options.bool(name)
wenzelm@49245
   343
    def update(name: String, x: Boolean)
wenzelm@49245
   344
    {
wenzelm@49245
   345
      Swing_Thread.require()
wenzelm@49245
   346
      options = options.bool.update(name, x)
wenzelm@49245
   347
    }
wenzelm@49245
   348
  }
wenzelm@49245
   349
wenzelm@49245
   350
  val int = new Object
wenzelm@49245
   351
  {
wenzelm@49245
   352
    def apply(name: String): Int = options.int(name)
wenzelm@49245
   353
    def update(name: String, x: Int)
wenzelm@49245
   354
    {
wenzelm@49245
   355
      Swing_Thread.require()
wenzelm@49245
   356
      options = options.int.update(name, x)
wenzelm@49245
   357
    }
wenzelm@49245
   358
  }
wenzelm@49245
   359
wenzelm@49245
   360
  val real = new Object
wenzelm@49245
   361
  {
wenzelm@49245
   362
    def apply(name: String): Double = options.real(name)
wenzelm@49245
   363
    def update(name: String, x: Double)
wenzelm@49245
   364
    {
wenzelm@49245
   365
      Swing_Thread.require()
wenzelm@49245
   366
      options = options.real.update(name, x)
wenzelm@49245
   367
    }
wenzelm@49245
   368
  }
wenzelm@49245
   369
wenzelm@49245
   370
  val string = new Object
wenzelm@49245
   371
  {
wenzelm@49245
   372
    def apply(name: String): String = options.string(name)
wenzelm@49245
   373
    def update(name: String, x: String)
wenzelm@49245
   374
    {
wenzelm@49245
   375
      Swing_Thread.require()
wenzelm@49245
   376
      options = options.string.update(name, x)
wenzelm@49245
   377
    }
wenzelm@49245
   378
  }
wenzelm@49245
   379
}
wenzelm@49245
   380