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