src/Pure/System/options.scala
author wenzelm
Tue Aug 14 20:50:50 2012 +0200 (2012-08-14)
changeset 48807 fde8c3d84ff5
parent 48795 bece259ee055
child 48860 56ec76f769c0
permissions -rw-r--r--
some support for persistent user 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@48807
    10
import java.util.Calendar
wenzelm@48807
    11
wenzelm@48807
    12
wenzelm@48365
    13
object Options
wenzelm@48365
    14
{
wenzelm@48421
    15
  type Spec = (String, Option[String])
wenzelm@48421
    16
wenzelm@48421
    17
  val empty: Options = new Options()
wenzelm@48421
    18
wenzelm@48421
    19
wenzelm@48421
    20
  /* representation */
wenzelm@48421
    21
wenzelm@48421
    22
  sealed abstract class Type
wenzelm@48365
    23
  {
wenzelm@48365
    24
    def print: String = toString.toLowerCase
wenzelm@48365
    25
  }
wenzelm@48421
    26
  private case object Bool extends Type
wenzelm@48421
    27
  private case object Int extends Type
wenzelm@48421
    28
  private case object Real extends Type
wenzelm@48421
    29
  private case object String extends Type
wenzelm@48365
    30
wenzelm@48370
    31
  case class Opt(typ: Type, value: String, description: String)
wenzelm@48365
    32
wenzelm@48365
    33
wenzelm@48365
    34
  /* parsing */
wenzelm@48365
    35
wenzelm@48795
    36
  private val OPTION = "option"
wenzelm@48807
    37
  private val OPTIONS = Path.explode("etc/options")
wenzelm@48807
    38
  private val PREFS = Path.explode("$ISABELLE_HOME_USER/etc/preferences")
wenzelm@48807
    39
  private val PREFS_BACKUP = Path.explode("$ISABELLE_HOME_USER/etc/preferences~")
wenzelm@48713
    40
wenzelm@48807
    41
  lazy val options_syntax = Outer_Syntax.init() + ":" + "=" + "--" + (OPTION, Keyword.THY_DECL)
wenzelm@48807
    42
  lazy val prefs_syntax = Outer_Syntax.init() + "="
wenzelm@48713
    43
wenzelm@48807
    44
  object Parser extends Parse.Parser
wenzelm@48365
    45
  {
wenzelm@48807
    46
    val option_name = atom("option name", _.is_xname)
wenzelm@48807
    47
    val option_type = atom("option type", _.is_ident)
wenzelm@48807
    48
    val option_value =
wenzelm@48807
    49
      opt(token("-", tok => tok.is_sym_ident && tok.content == "-")) ~ atom("nat", _.is_nat) ^^
wenzelm@48807
    50
        { case s ~ n => if (s.isDefined) "-" + n else n } |
wenzelm@48807
    51
      atom("option value", tok => tok.is_name || tok.is_float)
wenzelm@48807
    52
wenzelm@48807
    53
    val option_entry: Parser[Options => Options] =
wenzelm@48365
    54
    {
wenzelm@48795
    55
      command(OPTION) ~! (option_name ~ keyword(":") ~ option_type ~
wenzelm@48580
    56
      keyword("=") ~ option_value ~ (keyword("--") ~! text ^^ { case _ ~ x => x } | success(""))) ^^
wenzelm@48795
    57
        { case _ ~ (a ~ _ ~ b ~ _ ~ c ~ d) => (options: Options) => options.declare(a, b, c, d) }
wenzelm@48365
    58
    }
wenzelm@48365
    59
wenzelm@48807
    60
    val prefs_entry: Parser[Options => Options] =
wenzelm@48807
    61
    {
wenzelm@48807
    62
      option_name ~ (keyword("=") ~! option_value) ^^
wenzelm@48807
    63
      { case a ~ (_ ~ b) => (options: Options) => options + (a, b) }
wenzelm@48807
    64
    }
wenzelm@48807
    65
wenzelm@48807
    66
    def parse_file(syntax: Outer_Syntax, parser: Parser[Options => Options],
wenzelm@48807
    67
      options: Options, file: Path): Options =
wenzelm@48365
    68
    {
wenzelm@48807
    69
      val toks = syntax.scan(File.read(file))
wenzelm@48807
    70
      val ops =
wenzelm@48807
    71
        parse_all(rep(parser), Token.reader(toks, file.implode)) match {
wenzelm@48807
    72
          case Success(result, _) => result
wenzelm@48807
    73
          case bad => error(bad.toString)
wenzelm@48807
    74
        }
wenzelm@48807
    75
      try { (options /: ops) { case (opts, op) => op(opts) } }
wenzelm@48807
    76
      catch { case ERROR(msg) => error(msg + Position.str_of(file.position)) }
wenzelm@48365
    77
    }
wenzelm@48365
    78
  }
wenzelm@48365
    79
wenzelm@48807
    80
  def init_defaults(): Options =
wenzelm@48365
    81
  {
wenzelm@48365
    82
    var options = empty
wenzelm@48365
    83
    for {
wenzelm@48365
    84
      dir <- Isabelle_System.components()
wenzelm@48548
    85
      file = dir + OPTIONS if file.is_file
wenzelm@48807
    86
    } { options = Parser.parse_file(options_syntax, Parser.option_entry, options, file) }
wenzelm@48365
    87
    options
wenzelm@48365
    88
  }
wenzelm@48457
    89
wenzelm@48807
    90
  def init(): Options = init_defaults().load_prefs()
wenzelm@48807
    91
wenzelm@48457
    92
wenzelm@48457
    93
  /* encode */
wenzelm@48457
    94
wenzelm@48457
    95
  val encode: XML.Encode.T[Options] = (options => options.encode)
wenzelm@48693
    96
wenzelm@48693
    97
wenzelm@48693
    98
  /* command line entry point */
wenzelm@48693
    99
wenzelm@48693
   100
  def main(args: Array[String])
wenzelm@48693
   101
  {
wenzelm@48693
   102
    Command_Line.tool {
wenzelm@48693
   103
      args.toList match {
wenzelm@48693
   104
        case export_file :: more_options =>
wenzelm@48807
   105
          val options = (Options.init() /: more_options)(_ + _)
wenzelm@48693
   106
wenzelm@48693
   107
          if (export_file == "") java.lang.System.out.println(options.print)
wenzelm@48693
   108
          else File.write(Path.explode(export_file), YXML.string_of_body(options.encode))
wenzelm@48693
   109
wenzelm@48693
   110
          0
wenzelm@48693
   111
        case _ => error("Bad arguments:\n" + cat_lines(args))
wenzelm@48693
   112
      }
wenzelm@48693
   113
    }
wenzelm@48693
   114
  }
wenzelm@48365
   115
}
wenzelm@48365
   116
wenzelm@48365
   117
wenzelm@48807
   118
final class Options private(protected val options: Map[String, Options.Opt] = Map.empty)
wenzelm@48365
   119
{
wenzelm@48365
   120
  override def toString: String = options.iterator.mkString("Options (", ",", ")")
wenzelm@48365
   121
wenzelm@48693
   122
  def print: String =
wenzelm@48693
   123
    cat_lines(options.toList.sortBy(_._1).map({ case (name, opt) =>
wenzelm@48795
   124
      "option " + name + " : " + opt.typ.print + " = " +
wenzelm@48693
   125
        (if (opt.typ == Options.String) quote(opt.value) else opt.value) +
wenzelm@48693
   126
      "\n  -- " + quote(opt.description) }))
wenzelm@48693
   127
wenzelm@48365
   128
wenzelm@48370
   129
  /* check */
wenzelm@48365
   130
wenzelm@48365
   131
  private def check_name(name: String): Options.Opt =
wenzelm@48365
   132
    options.get(name) match {
wenzelm@48365
   133
      case Some(opt) => opt
wenzelm@48368
   134
      case None => error("Unknown option " + quote(name))
wenzelm@48365
   135
    }
wenzelm@48365
   136
wenzelm@48365
   137
  private def check_type(name: String, typ: Options.Type): Options.Opt =
wenzelm@48365
   138
  {
wenzelm@48365
   139
    val opt = check_name(name)
wenzelm@48365
   140
    if (opt.typ == typ) opt
wenzelm@48365
   141
    else error("Ill-typed option " + quote(name) + " : " + opt.typ.print + " vs. " + typ.print)
wenzelm@48365
   142
  }
wenzelm@48365
   143
wenzelm@48370
   144
wenzelm@48370
   145
  /* basic operations */
wenzelm@48370
   146
wenzelm@48370
   147
  private def put[A](name: String, typ: Options.Type, value: String): Options =
wenzelm@48370
   148
  {
wenzelm@48370
   149
    val opt = check_type(name, typ)
wenzelm@48370
   150
    new Options(options + (name -> opt.copy(value = value)))
wenzelm@48370
   151
  }
wenzelm@48370
   152
wenzelm@48365
   153
  private def get[A](name: String, typ: Options.Type, parse: String => Option[A]): A =
wenzelm@48365
   154
  {
wenzelm@48365
   155
    val opt = check_type(name, typ)
wenzelm@48365
   156
    parse(opt.value) match {
wenzelm@48365
   157
      case Some(x) => x
wenzelm@48365
   158
      case None =>
wenzelm@48365
   159
        error("Malformed value for option " + quote(name) +
wenzelm@48365
   160
          " : " + typ.print + " =\n" + quote(opt.value))
wenzelm@48365
   161
    }
wenzelm@48365
   162
  }
wenzelm@48365
   163
wenzelm@48365
   164
wenzelm@48365
   165
  /* internal lookup and update */
wenzelm@48365
   166
wenzelm@48365
   167
  val bool = new Object
wenzelm@48365
   168
  {
wenzelm@48365
   169
    def apply(name: String): Boolean = get(name, Options.Bool, Properties.Value.Boolean.unapply)
wenzelm@48365
   170
    def update(name: String, x: Boolean): Options =
wenzelm@48365
   171
      put(name, Options.Bool, Properties.Value.Boolean(x))
wenzelm@48365
   172
  }
wenzelm@48365
   173
wenzelm@48365
   174
  val int = new Object
wenzelm@48365
   175
  {
wenzelm@48365
   176
    def apply(name: String): Int = get(name, Options.Int, Properties.Value.Int.unapply)
wenzelm@48365
   177
    def update(name: String, x: Int): Options =
wenzelm@48365
   178
      put(name, Options.Int, Properties.Value.Int(x))
wenzelm@48365
   179
  }
wenzelm@48365
   180
wenzelm@48365
   181
  val real = new Object
wenzelm@48365
   182
  {
wenzelm@48365
   183
    def apply(name: String): Double = get(name, Options.Real, Properties.Value.Double.unapply)
wenzelm@48365
   184
    def update(name: String, x: Double): Options =
wenzelm@48365
   185
      put(name, Options.Real, Properties.Value.Double(x))
wenzelm@48365
   186
  }
wenzelm@48365
   187
wenzelm@48365
   188
  val string = new Object
wenzelm@48365
   189
  {
wenzelm@48365
   190
    def apply(name: String): String = get(name, Options.String, s => Some(s))
wenzelm@48365
   191
    def update(name: String, x: String): Options = put(name, Options.String, x)
wenzelm@48365
   192
  }
wenzelm@48370
   193
wenzelm@48370
   194
wenzelm@48807
   195
  /* external updates */
wenzelm@48370
   196
wenzelm@48370
   197
  private def check_value(name: String): Options =
wenzelm@48370
   198
  {
wenzelm@48370
   199
    val opt = check_name(name)
wenzelm@48370
   200
    opt.typ match {
wenzelm@48370
   201
      case Options.Bool => bool(name); this
wenzelm@48370
   202
      case Options.Int => int(name); this
wenzelm@48370
   203
      case Options.Real => real(name); this
wenzelm@48370
   204
      case Options.String => string(name); this
wenzelm@48370
   205
    }
wenzelm@48370
   206
  }
wenzelm@48370
   207
wenzelm@48370
   208
  def declare(name: String, typ_name: String, value: String, description: String = ""): Options =
wenzelm@48370
   209
  {
wenzelm@48370
   210
    options.get(name) match {
wenzelm@48370
   211
      case Some(_) => error("Duplicate declaration of option " + quote(name))
wenzelm@48370
   212
      case None =>
wenzelm@48370
   213
        val typ =
wenzelm@48370
   214
          typ_name match {
wenzelm@48370
   215
            case "bool" => Options.Bool
wenzelm@48370
   216
            case "int" => Options.Int
wenzelm@48370
   217
            case "real" => Options.Real
wenzelm@48370
   218
            case "string" => Options.String
wenzelm@48456
   219
            case _ => error("Unknown type for option " + quote(name) + " : " + quote(typ_name))
wenzelm@48370
   220
          }
wenzelm@48807
   221
        val opt = Options.Opt(typ, value, description)
wenzelm@48807
   222
        (new Options(options + (name -> opt))).check_value(name)
wenzelm@48370
   223
    }
wenzelm@48370
   224
  }
wenzelm@48370
   225
wenzelm@48807
   226
  def + (name: String, value: String): Options =
wenzelm@48370
   227
  {
wenzelm@48370
   228
    val opt = check_name(name)
wenzelm@48370
   229
    (new Options(options + (name -> opt.copy(value = value)))).check_value(name)
wenzelm@48370
   230
  }
wenzelm@48370
   231
wenzelm@48807
   232
  def + (name: String, opt_value: Option[String]): Options =
wenzelm@48370
   233
  {
wenzelm@48370
   234
    val opt = check_name(name)
wenzelm@48370
   235
    opt_value match {
wenzelm@48807
   236
      case Some(value) => this + (name, value)
wenzelm@48807
   237
      case None if opt.typ == Options.Bool => this + (name, "true")
wenzelm@48370
   238
      case None => error("Missing value for option " + quote(name) + " : " + opt.typ.print)
wenzelm@48370
   239
    }
wenzelm@48370
   240
  }
wenzelm@48370
   241
wenzelm@48807
   242
  def + (str: String): Options =
wenzelm@48370
   243
  {
wenzelm@48370
   244
    str.indexOf('=') match {
wenzelm@48807
   245
      case -1 => this + (str, None)
wenzelm@48807
   246
      case i => this + (str.substring(0, i), str.substring(i + 1))
wenzelm@48370
   247
    }
wenzelm@48370
   248
  }
wenzelm@48456
   249
wenzelm@48807
   250
  def ++ (specs: List[Options.Spec]): Options =
wenzelm@48807
   251
    (this /: specs)({ case (x, (y, z)) => x + (y, z) })
wenzelm@48807
   252
wenzelm@48456
   253
wenzelm@48456
   254
  /* encode */
wenzelm@48456
   255
wenzelm@48456
   256
  def encode: XML.Body =
wenzelm@48456
   257
  {
wenzelm@48456
   258
    import XML.Encode.{string => str, _}
wenzelm@48456
   259
    list(triple(str, str, str))(
wenzelm@48456
   260
      options.toList.map({ case (name, opt) => (name, opt.typ.print, opt.value) }))
wenzelm@48456
   261
  }
wenzelm@48807
   262
wenzelm@48807
   263
wenzelm@48807
   264
  /* user preferences */
wenzelm@48807
   265
wenzelm@48807
   266
  def load_prefs(): Options =
wenzelm@48807
   267
    if (Options.PREFS.is_file)
wenzelm@48807
   268
      Options.Parser.parse_file(
wenzelm@48807
   269
        Options.prefs_syntax, Options.Parser.prefs_entry, this, Options.PREFS)
wenzelm@48807
   270
    else this
wenzelm@48807
   271
wenzelm@48807
   272
  def save_prefs()
wenzelm@48807
   273
  {
wenzelm@48807
   274
    val current_defaults = Options.init_defaults()
wenzelm@48807
   275
    val changed =
wenzelm@48807
   276
      (for {
wenzelm@48807
   277
        (name, opt1) <- current_defaults.options.iterator
wenzelm@48807
   278
        opt2 <- options.get(name)
wenzelm@48807
   279
        if (opt1.value != opt2.value)
wenzelm@48807
   280
      } yield (name, opt2.value)).toList
wenzelm@48807
   281
    val prefs =
wenzelm@48807
   282
      changed.sortBy(_._1)
wenzelm@48807
   283
        .map({ case (x, y) => x + " = " + Outer_Syntax.quote_string(y) + "\n" }).mkString
wenzelm@48807
   284
wenzelm@48807
   285
    Options.PREFS.file renameTo Options.PREFS_BACKUP.file
wenzelm@48807
   286
    File.write(Options.PREFS,
wenzelm@48807
   287
      "(* generated by Isabelle " + Calendar.getInstance.getTime + " *)\n\n" + prefs)
wenzelm@48807
   288
  }
wenzelm@48365
   289
}