| author | haftmann | 
| Mon, 23 Mar 2015 19:05:14 +0100 | |
| changeset 59816 | 034b13f4efae | 
| parent 59811 | 6b0d9e8ac227 | 
| child 60133 | a90982bbe8b4 | 
| permissions | -rw-r--r-- | 
| 48365 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 1 | /* Title: Pure/System/options.scala | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 2 | Author: Makarius | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 3 | |
| 51945 | 4 | System options with external string representation. | 
| 48365 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 5 | */ | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 6 | |
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 7 | package isabelle | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 8 | |
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 9 | |
| 48807 | 10 | import java.util.Calendar | 
| 11 | ||
| 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 | 15 | type Spec = (String, Option[String]) | 
| 16 | ||
| 17 | val empty: Options = new Options() | |
| 18 | ||
| 19 | ||
| 20 | /* representation */ | |
| 21 | ||
| 22 | sealed abstract class Type | |
| 48365 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 23 |   {
 | 
| 56599 | 24 | def print: String = Word.lowercase(toString) | 
| 48365 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 25 | } | 
| 49246 | 26 | case object Bool extends Type | 
| 27 | case object Int extends Type | |
| 28 | case object Real extends Type | |
| 29 | case object String extends Type | |
| 30 | case object Unknown extends Type | |
| 48365 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 31 | |
| 56465 | 32 | case class Opt( | 
| 33 | public: Boolean, | |
| 34 | pos: Position.T, | |
| 35 | name: String, | |
| 36 | typ: Type, | |
| 37 | value: String, | |
| 38 | default_value: String, | |
| 39 | description: String, | |
| 40 | section: String) | |
| 48860 | 41 |   {
 | 
| 49289 | 42 | private def print(default: Boolean): String = | 
| 43 |     {
 | |
| 44 | val x = if (default) default_value else value | |
| 49247 | 45 | "option " + name + " : " + typ.print + " = " + | 
| 49289 | 46 | (if (typ == Options.String) quote(x) else x) + | 
| 47 | (if (description == "") "" else "\n -- " + quote(description)) | |
| 48 | } | |
| 49 | ||
| 50 | def print: String = print(false) | |
| 51 | def print_default: String = print(true) | |
| 49247 | 52 | |
| 56559 | 53 | def title(strip: String = ""): String = | 
| 49270 | 54 |     {
 | 
| 56600 | 55 |       val words = Word.explode('_', name)
 | 
| 49270 | 56 | val words1 = | 
| 57 |         words match {
 | |
| 58 | case word :: rest if word == strip => rest | |
| 59 | case _ => words | |
| 60 | } | |
| 56609 
5ac67041ccf8
capitalize more carefully, e.g. relevant for option "ML_exception_trace";
 wenzelm parents: 
56600diff
changeset | 61 | Word.implode(words1.map(Word.perhaps_capitalize(_))) | 
| 49270 | 62 | } | 
| 63 | ||
| 48860 | 64 | def unknown: Boolean = typ == Unknown | 
| 65 | } | |
| 48365 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 66 | |
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 67 | |
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 68 | /* parsing */ | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 69 | |
| 49270 | 70 | private val SECTION = "section" | 
| 52065 
78f2475aa126
explicit notion of public options, which are shown in the editor options dialog;
 wenzelm parents: 
51945diff
changeset | 71 | private val PUBLIC = "public" | 
| 48795 
bece259ee055
clarified format of etc/options: only declarations, not re-definitions;
 wenzelm parents: 
48718diff
changeset | 72 | private val OPTION = "option" | 
| 48807 | 73 |   private val OPTIONS = Path.explode("etc/options")
 | 
| 50793 | 74 |   private val PREFS_DIR = Path.explode("$ISABELLE_HOME_USER/etc")
 | 
| 75 |   private val PREFS = PREFS_DIR + Path.basic("preferences")
 | |
| 48713 
de26cf3191a3
more token markers, based on actual outer syntax;
 wenzelm parents: 
48693diff
changeset | 76 | |
| 49270 | 77 | lazy val options_syntax = | 
| 78 | Outer_Syntax.init() + ":" + "=" + "--" + | |
| 58999 
ed09ae4ea2d8
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
 wenzelm parents: 
58908diff
changeset | 79 | (SECTION, Keyword.DOCUMENT_HEADING) + (PUBLIC, Keyword.THY_DECL) + (OPTION, Keyword.THY_DECL) | 
| 49270 | 80 | |
| 48807 | 81 | lazy val prefs_syntax = Outer_Syntax.init() + "=" | 
| 48713 
de26cf3191a3
more token markers, based on actual outer syntax;
 wenzelm parents: 
48693diff
changeset | 82 | |
| 48807 | 83 | object Parser extends Parse.Parser | 
| 48365 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 84 |   {
 | 
| 48807 | 85 |     val option_name = atom("option name", _.is_xname)
 | 
| 86 |     val option_type = atom("option type", _.is_ident)
 | |
| 87 | val option_value = | |
| 88 |       opt(token("-", tok => tok.is_sym_ident && tok.content == "-")) ~ atom("nat", _.is_nat) ^^
 | |
| 89 |         { case s ~ n => if (s.isDefined) "-" + n else n } |
 | |
| 90 |       atom("option value", tok => tok.is_name || tok.is_float)
 | |
| 91 | ||
| 92 | val option_entry: Parser[Options => Options] = | |
| 48365 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 93 |     {
 | 
| 49270 | 94 | command(SECTION) ~! text ^^ | 
| 49295 | 95 |         { case _ ~ a => (options: Options) => options.set_section(a) } |
 | 
| 59811 | 96 |       opt(command(PUBLIC)) ~ command(OPTION) ~! (position(option_name) ~ $$$(":") ~ option_type ~
 | 
| 58908 | 97 |       $$$("=") ~ option_value ~ ($$$("--") ~! text ^^ { case _ ~ x => x } | success(""))) ^^
 | 
| 59811 | 98 |         { case a ~ _ ~ ((b, pos) ~ _ ~ c ~ _ ~ d ~ e) =>
 | 
| 56465 | 99 | (options: Options) => options.declare(a.isDefined, pos, b, c, d, e) } | 
| 48365 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 100 | } | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 101 | |
| 48807 | 102 | val prefs_entry: Parser[Options => Options] = | 
| 103 |     {
 | |
| 58908 | 104 |       option_name ~ ($$$("=") ~! option_value) ^^
 | 
| 48860 | 105 |       { case a ~ (_ ~ b) => (options: Options) => options.add_permissive(a, b) }
 | 
| 48807 | 106 | } | 
| 107 | ||
| 108 | def parse_file(syntax: Outer_Syntax, parser: Parser[Options => Options], | |
| 109 | options: Options, file: Path): Options = | |
| 48365 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 110 |     {
 | 
| 59083 | 111 | val toks = Token.explode(syntax.keywords, File.read(file)) | 
| 48807 | 112 | val ops = | 
| 59705 | 113 |         parse_all(rep(parser), Token.reader(toks, Token.Pos.file(file.implode))) match {
 | 
| 48807 | 114 | case Success(result, _) => result | 
| 115 | case bad => error(bad.toString) | |
| 116 | } | |
| 49270 | 117 |       try { (options.set_section("") /: ops) { case (opts, op) => op(opts) } }
 | 
| 48992 | 118 |       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 | 119 | } | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 120 | } | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 121 | |
| 48807 | 122 | def init_defaults(): Options = | 
| 48365 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 123 |   {
 | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 124 | var options = empty | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 125 |     for {
 | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 126 | dir <- Isabelle_System.components() | 
| 48548 | 127 | file = dir + OPTIONS if file.is_file | 
| 48807 | 128 |     } { 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 | 129 | options | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 130 | } | 
| 48457 | 131 | |
| 48807 | 132 | def init(): Options = init_defaults().load_prefs() | 
| 133 | ||
| 48457 | 134 | |
| 135 | /* encode */ | |
| 136 | ||
| 137 | val encode: XML.Encode.T[Options] = (options => options.encode) | |
| 48693 
ceeea46bdeba
"isabelle options" prints Isabelle system options;
 wenzelm parents: 
48605diff
changeset | 138 | |
| 
ceeea46bdeba
"isabelle options" prints Isabelle system options;
 wenzelm parents: 
48605diff
changeset | 139 | |
| 
ceeea46bdeba
"isabelle options" prints Isabelle system options;
 wenzelm parents: 
48605diff
changeset | 140 | /* command line entry point */ | 
| 
ceeea46bdeba
"isabelle options" prints Isabelle system options;
 wenzelm parents: 
48605diff
changeset | 141 | |
| 
ceeea46bdeba
"isabelle options" prints Isabelle system options;
 wenzelm parents: 
48605diff
changeset | 142 | def main(args: Array[String]) | 
| 
ceeea46bdeba
"isabelle options" prints Isabelle system options;
 wenzelm parents: 
48605diff
changeset | 143 |   {
 | 
| 56631 | 144 |     Command_Line.tool0 {
 | 
| 48693 
ceeea46bdeba
"isabelle options" prints Isabelle system options;
 wenzelm parents: 
48605diff
changeset | 145 |       args.toList match {
 | 
| 52735 | 146 | case get_option :: export_file :: more_options => | 
| 48807 | 147 | val options = (Options.init() /: more_options)(_ + _) | 
| 48693 
ceeea46bdeba
"isabelle options" prints Isabelle system options;
 wenzelm parents: 
48605diff
changeset | 148 | |
| 52735 | 149 | if (get_option != "") | 
| 56831 
e3ccf0809d51
prefer scala.Console with its support for thread-local redirection;
 wenzelm parents: 
56631diff
changeset | 150 | Console.println(options.check_name(get_option).value) | 
| 52737 
7b396ef36af6
clarified meaning of options for "isabelle options";
 wenzelm parents: 
52735diff
changeset | 151 | |
| 
7b396ef36af6
clarified meaning of options for "isabelle options";
 wenzelm parents: 
52735diff
changeset | 152 | if (export_file != "") | 
| 52735 | 153 | File.write(Path.explode(export_file), YXML.string_of_body(options.encode)) | 
| 52737 
7b396ef36af6
clarified meaning of options for "isabelle options";
 wenzelm parents: 
52735diff
changeset | 154 | |
| 
7b396ef36af6
clarified meaning of options for "isabelle options";
 wenzelm parents: 
52735diff
changeset | 155 | if (get_option == "" && export_file == "") | 
| 56831 
e3ccf0809d51
prefer scala.Console with its support for thread-local redirection;
 wenzelm parents: 
56631diff
changeset | 156 | Console.println(options.print) | 
| 48693 
ceeea46bdeba
"isabelle options" prints Isabelle system options;
 wenzelm parents: 
48605diff
changeset | 157 | |
| 
ceeea46bdeba
"isabelle options" prints Isabelle system options;
 wenzelm parents: 
48605diff
changeset | 158 |         case _ => error("Bad arguments:\n" + cat_lines(args))
 | 
| 
ceeea46bdeba
"isabelle options" prints Isabelle system options;
 wenzelm parents: 
48605diff
changeset | 159 | } | 
| 
ceeea46bdeba
"isabelle options" prints Isabelle system options;
 wenzelm parents: 
48605diff
changeset | 160 | } | 
| 
ceeea46bdeba
"isabelle options" prints Isabelle system options;
 wenzelm parents: 
48605diff
changeset | 161 | } | 
| 48365 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 162 | } | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 163 | |
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 164 | |
| 49270 | 165 | final class Options private( | 
| 49296 | 166 | val options: Map[String, Options.Opt] = Map.empty, | 
| 49270 | 167 | val section: String = "") | 
| 48365 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 168 | {
 | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 169 |   override def toString: String = options.iterator.mkString("Options (", ",", ")")
 | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 170 | |
| 54347 | 171 | private def print_opt(opt: Options.Opt): String = | 
| 172 | if (opt.public) "public " + opt.print else opt.print | |
| 173 | ||
| 174 | def print: String = cat_lines(options.toList.sortBy(_._1).map(p => print_opt(p._2))) | |
| 48693 
ceeea46bdeba
"isabelle options" prints Isabelle system options;
 wenzelm parents: 
48605diff
changeset | 175 | |
| 49245 
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
 wenzelm parents: 
48992diff
changeset | 176 | def description(name: String): String = check_name(name).description | 
| 
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
 wenzelm parents: 
48992diff
changeset | 177 | |
| 48365 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 178 | |
| 48370 | 179 | /* check */ | 
| 48365 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 180 | |
| 49246 | 181 | def check_name(name: String): Options.Opt = | 
| 48365 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 182 |     options.get(name) match {
 | 
| 48860 | 183 | case Some(opt) if !opt.unknown => opt | 
| 184 |       case _ => error("Unknown option " + quote(name))
 | |
| 48365 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 185 | } | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 186 | |
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 187 | 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 | 188 |   {
 | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 189 | val opt = check_name(name) | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 190 | if (opt.typ == typ) opt | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 191 |     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 | 192 | } | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 193 | |
| 48370 | 194 | |
| 195 | /* basic operations */ | |
| 196 | ||
| 197 | private def put[A](name: String, typ: Options.Type, value: String): Options = | |
| 198 |   {
 | |
| 199 | val opt = check_type(name, typ) | |
| 49270 | 200 | new Options(options + (name -> opt.copy(value = value)), section) | 
| 48370 | 201 | } | 
| 202 | ||
| 48365 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 203 | 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 | 204 |   {
 | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 205 | val opt = check_type(name, typ) | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 206 |     parse(opt.value) match {
 | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 207 | case Some(x) => x | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 208 | case None => | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 209 |         error("Malformed value for option " + quote(name) +
 | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 210 | " : " + typ.print + " =\n" + quote(opt.value)) | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 211 | } | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 212 | } | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 213 | |
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 214 | |
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 215 | /* internal lookup and update */ | 
| 
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: 
49296diff
changeset | 217 | class Bool_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): Boolean = get(name, Options.Bool, Properties.Value.Boolean.unapply) | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 220 | def update(name: String, x: Boolean): Options = | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 221 | put(name, Options.Bool, Properties.Value.Boolean(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: 
49296diff
changeset | 223 | val bool = new Bool_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: 
49296diff
changeset | 225 | class Int_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): Int = get(name, Options.Int, Properties.Value.Int.unapply) | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 228 | def update(name: String, x: Int): Options = | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 229 | put(name, Options.Int, Properties.Value.Int(x)) | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 230 | } | 
| 49954 
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
 wenzelm parents: 
49296diff
changeset | 231 | val int = new Int_Access | 
| 48365 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 232 | |
| 49954 
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
 wenzelm parents: 
49296diff
changeset | 233 | class Real_Access | 
| 48365 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 234 |   {
 | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 235 | 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 | 236 | def update(name: String, x: Double): Options = | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 237 | put(name, Options.Real, Properties.Value.Double(x)) | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 238 | } | 
| 49954 
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
 wenzelm parents: 
49296diff
changeset | 239 | val real = new Real_Access | 
| 48365 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 240 | |
| 49954 
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
 wenzelm parents: 
49296diff
changeset | 241 | class String_Access | 
| 48365 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 242 |   {
 | 
| 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 243 | 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 | 244 | 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 | 245 | } | 
| 49954 
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
 wenzelm parents: 
49296diff
changeset | 246 | val string = new String_Access | 
| 48370 | 247 | |
| 50207 | 248 | class Seconds_Access | 
| 249 |   {
 | |
| 250 | def apply(name: String): Time = Time.seconds(real(name)) | |
| 251 | } | |
| 252 | val seconds = new Seconds_Access | |
| 253 | ||
| 48370 | 254 | |
| 48807 | 255 | /* external updates */ | 
| 48370 | 256 | |
| 257 | private def check_value(name: String): Options = | |
| 258 |   {
 | |
| 259 | val opt = check_name(name) | |
| 260 |     opt.typ match {
 | |
| 261 | case Options.Bool => bool(name); this | |
| 262 | case Options.Int => int(name); this | |
| 263 | case Options.Real => real(name); this | |
| 264 | case Options.String => string(name); this | |
| 48860 | 265 | case Options.Unknown => this | 
| 48370 | 266 | } | 
| 267 | } | |
| 268 | ||
| 56465 | 269 | def declare( | 
| 270 | public: Boolean, | |
| 271 | pos: Position.T, | |
| 272 | name: String, | |
| 273 | typ_name: String, | |
| 274 | value: String, | |
| 275 | description: String): Options = | |
| 48370 | 276 |   {
 | 
| 277 |     options.get(name) match {
 | |
| 56465 | 278 | case Some(other) => | 
| 279 |         error("Duplicate declaration of option " + quote(name) + Position.here(pos) +
 | |
| 280 | Position.here(other.pos)) | |
| 48370 | 281 | case None => | 
| 282 | val typ = | |
| 283 |           typ_name match {
 | |
| 284 | case "bool" => Options.Bool | |
| 285 | case "int" => Options.Int | |
| 286 | case "real" => Options.Real | |
| 287 | case "string" => Options.String | |
| 56465 | 288 | case _ => | 
| 289 |               error("Unknown type for option " + quote(name) + " : " + quote(typ_name) +
 | |
| 290 | Position.here(pos)) | |
| 48370 | 291 | } | 
| 56465 | 292 | val opt = Options.Opt(public, pos, name, typ, value, value, description, section) | 
| 49270 | 293 | (new Options(options + (name -> opt), section)).check_value(name) | 
| 48370 | 294 | } | 
| 295 | } | |
| 296 | ||
| 48860 | 297 | def add_permissive(name: String, value: String): Options = | 
| 298 |   {
 | |
| 299 | if (options.isDefinedAt(name)) this + (name, value) | |
| 56465 | 300 |     else {
 | 
| 301 | val opt = Options.Opt(false, Position.none, name, Options.Unknown, value, value, "", "") | |
| 302 | new Options(options + (name -> opt), section) | |
| 303 | } | |
| 48860 | 304 | } | 
| 305 | ||
| 48807 | 306 | def + (name: String, value: String): Options = | 
| 48370 | 307 |   {
 | 
| 308 | val opt = check_name(name) | |
| 49270 | 309 | (new Options(options + (name -> opt.copy(value = value)), section)).check_value(name) | 
| 48370 | 310 | } | 
| 311 | ||
| 48807 | 312 | def + (name: String, opt_value: Option[String]): Options = | 
| 48370 | 313 |   {
 | 
| 314 | val opt = check_name(name) | |
| 315 |     opt_value match {
 | |
| 48807 | 316 | case Some(value) => this + (name, value) | 
| 317 | case None if opt.typ == Options.Bool => this + (name, "true") | |
| 48370 | 318 |       case None => error("Missing value for option " + quote(name) + " : " + opt.typ.print)
 | 
| 319 | } | |
| 320 | } | |
| 321 | ||
| 48807 | 322 | def + (str: String): Options = | 
| 48370 | 323 |   {
 | 
| 324 |     str.indexOf('=') match {
 | |
| 48807 | 325 | case -1 => this + (str, None) | 
| 326 | case i => this + (str.substring(0, i), str.substring(i + 1)) | |
| 48370 | 327 | } | 
| 328 | } | |
| 48456 
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
 wenzelm parents: 
48421diff
changeset | 329 | |
| 48807 | 330 | def ++ (specs: List[Options.Spec]): Options = | 
| 331 |     (this /: specs)({ case (x, (y, z)) => x + (y, z) })
 | |
| 332 | ||
| 48456 
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
 wenzelm parents: 
48421diff
changeset | 333 | |
| 49270 | 334 | /* sections */ | 
| 335 | ||
| 336 | def set_section(new_section: String): Options = | |
| 337 | new Options(options, new_section) | |
| 338 | ||
| 339 | def sections: List[(String, List[Options.Opt])] = | |
| 340 |     options.groupBy(_._2.section).toList.map({ case (a, opts) => (a, opts.toList.map(_._2)) })
 | |
| 341 | ||
| 342 | ||
| 48456 
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
 wenzelm parents: 
48421diff
changeset | 343 | /* encode */ | 
| 
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
 wenzelm parents: 
48421diff
changeset | 344 | |
| 
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
 wenzelm parents: 
48421diff
changeset | 345 | def encode: XML.Body = | 
| 
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
 wenzelm parents: 
48421diff
changeset | 346 |   {
 | 
| 48860 | 347 | val opts = | 
| 56465 | 348 | for ((_, opt) <- options.toList; if !opt.unknown) | 
| 349 | yield (opt.pos, (opt.name, (opt.typ.print, opt.value))) | |
| 48860 | 350 | |
| 56465 | 351 |     import XML.Encode.{string => string_, _}
 | 
| 352 | list(pair(properties, pair(string_, pair(string_, string_))))(opts) | |
| 48456 
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
 wenzelm parents: 
48421diff
changeset | 353 | } | 
| 48807 | 354 | |
| 355 | ||
| 356 | /* user preferences */ | |
| 357 | ||
| 358 | def load_prefs(): Options = | |
| 359 | if (Options.PREFS.is_file) | |
| 360 | Options.Parser.parse_file( | |
| 361 | Options.prefs_syntax, Options.Parser.prefs_entry, this, Options.PREFS) | |
| 362 | else this | |
| 363 | ||
| 364 | def save_prefs() | |
| 365 |   {
 | |
| 48860 | 366 | val defaults = Options.init_defaults() | 
| 48807 | 367 | val changed = | 
| 368 |       (for {
 | |
| 48860 | 369 | (name, opt2) <- options.iterator | 
| 370 | opt1 = defaults.options.get(name) | |
| 371 | if (opt1.isEmpty || opt1.get.value != opt2.value) | |
| 372 | } yield (name, opt2.value, if (opt1.isEmpty) " (* unknown *)" else "")).toList | |
| 373 | ||
| 48807 | 374 | val prefs = | 
| 375 | changed.sortBy(_._1) | |
| 48860 | 376 |         .map({ case (x, y, z) => x + " = " + Outer_Syntax.quote_string(y) + z + "\n" }).mkString
 | 
| 48807 | 377 | |
| 50893 
d55eb82ae77b
Isabelle_System.mkdirs with explicit error checking (in accordance to ML version), e.g. relevant with read-only DMG file-system on Mac OS X;
 wenzelm parents: 
50793diff
changeset | 378 | Isabelle_System.mkdirs(Options.PREFS_DIR) | 
| 53336 | 379 | File.write_backup(Options.PREFS, | 
| 48807 | 380 | "(* generated by Isabelle " + Calendar.getInstance.getTime + " *)\n\n" + prefs) | 
| 381 | } | |
| 48365 
d88aefda01c4
basic support for stand-alone options with external string representation;
 wenzelm parents: diff
changeset | 382 | } | 
| 49245 
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
 wenzelm parents: 
48992diff
changeset | 383 | |
| 
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
 wenzelm parents: 
48992diff
changeset | 384 | |
| 
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
 wenzelm parents: 
48992diff
changeset | 385 | class Options_Variable | 
| 
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
 wenzelm parents: 
48992diff
changeset | 386 | {
 | 
| 51620 
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
 wenzelm parents: 
50893diff
changeset | 387 | private var options = Options.empty | 
| 49245 
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
 wenzelm parents: 
48992diff
changeset | 388 | |
| 51620 
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
 wenzelm parents: 
50893diff
changeset | 389 |   def value: Options = synchronized { options }
 | 
| 
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
 wenzelm parents: 
50893diff
changeset | 390 |   def update(new_options: Options): Unit = synchronized { options = new_options }
 | 
| 49245 
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
 wenzelm parents: 
48992diff
changeset | 391 | |
| 51620 
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
 wenzelm parents: 
50893diff
changeset | 392 |   def + (name: String, x: String): Unit = synchronized { options = options + (name, x) }
 | 
| 49245 
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
 wenzelm parents: 
48992diff
changeset | 393 | |
| 49954 
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
 wenzelm parents: 
49296diff
changeset | 394 | class Bool_Access | 
| 49245 
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
 wenzelm parents: 
48992diff
changeset | 395 |   {
 | 
| 51620 
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
 wenzelm parents: 
50893diff
changeset | 396 |     def apply(name: String): Boolean = synchronized { options.bool(name) }
 | 
| 
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
 wenzelm parents: 
50893diff
changeset | 397 | def update(name: String, x: Boolean): Unit = | 
| 
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
 wenzelm parents: 
50893diff
changeset | 398 |       synchronized { options = options.bool.update(name, x) }
 | 
| 49245 
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
 wenzelm parents: 
48992diff
changeset | 399 | } | 
| 49954 
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
 wenzelm parents: 
49296diff
changeset | 400 | val bool = new Bool_Access | 
| 49245 
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
 wenzelm parents: 
48992diff
changeset | 401 | |
| 49954 
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
 wenzelm parents: 
49296diff
changeset | 402 | class Int_Access | 
| 49245 
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
 wenzelm parents: 
48992diff
changeset | 403 |   {
 | 
| 51620 
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
 wenzelm parents: 
50893diff
changeset | 404 |     def apply(name: String): Int = synchronized { options.int(name) }
 | 
| 
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
 wenzelm parents: 
50893diff
changeset | 405 | def update(name: String, x: Int): Unit = | 
| 
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
 wenzelm parents: 
50893diff
changeset | 406 |       synchronized { options = options.int.update(name, x) }
 | 
| 49245 
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
 wenzelm parents: 
48992diff
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: 
49296diff
changeset | 408 | val int = new Int_Access | 
| 49245 
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
 wenzelm parents: 
48992diff
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: 
49296diff
changeset | 410 | class Real_Access | 
| 49245 
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
 wenzelm parents: 
48992diff
changeset | 411 |   {
 | 
| 51620 
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
 wenzelm parents: 
50893diff
changeset | 412 |     def apply(name: String): Double = synchronized { options.real(name) }
 | 
| 
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
 wenzelm parents: 
50893diff
changeset | 413 | def update(name: String, x: Double): Unit = | 
| 
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
 wenzelm parents: 
50893diff
changeset | 414 |       synchronized { options = options.real.update(name, x) }
 | 
| 49245 
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
 wenzelm parents: 
48992diff
changeset | 415 | } | 
| 49954 
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
 wenzelm parents: 
49296diff
changeset | 416 | val real = new Real_Access | 
| 49245 
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
 wenzelm parents: 
48992diff
changeset | 417 | |
| 49954 
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
 wenzelm parents: 
49296diff
changeset | 418 | class String_Access | 
| 49245 
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
 wenzelm parents: 
48992diff
changeset | 419 |   {
 | 
| 51620 
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
 wenzelm parents: 
50893diff
changeset | 420 |     def apply(name: String): String = synchronized { options.string(name) }
 | 
| 
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
 wenzelm parents: 
50893diff
changeset | 421 | def update(name: String, x: String): Unit = | 
| 
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
 wenzelm parents: 
50893diff
changeset | 422 |       synchronized { options = options.string.update(name, x) }
 | 
| 49245 
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
 wenzelm parents: 
48992diff
changeset | 423 | } | 
| 49954 
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
 wenzelm parents: 
49296diff
changeset | 424 | val string = new String_Access | 
| 50207 | 425 | |
| 426 | class Seconds_Access | |
| 427 |   {
 | |
| 51620 
7c39677f9ea0
more conventional synchronized access to Options_Variable -- avoid Swing_Thread getting in the way, which might be absent in some environments (e.g. SWT);
 wenzelm parents: 
50893diff
changeset | 428 |     def apply(name: String): Time = synchronized { options.seconds(name) }
 | 
| 50207 | 429 | } | 
| 430 | val seconds = new Seconds_Access | |
| 49245 
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
 wenzelm parents: 
48992diff
changeset | 431 | } | 
| 
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
 wenzelm parents: 
48992diff
changeset | 432 |