| author | wenzelm |
| Thu, 08 Nov 2012 20:25:48 +0100 | |
| changeset 50078 | 02aa7f6e530d |
| parent 49954 | 44658062d822 |
| child 50207 | 54be125d8cdc |
| 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 |
|
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
4 |
Stand-alone options with external string representation. |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
5 |
*/ |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
6 |
|
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
7 |
package isabelle |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
8 |
|
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
9 |
|
|
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
10 |
import java.util.Locale |
| 48807 | 11 |
import java.util.Calendar |
12 |
||
13 |
||
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
14 |
object Options |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
15 |
{
|
| 48421 | 16 |
type Spec = (String, Option[String]) |
17 |
||
18 |
val empty: Options = new Options() |
|
19 |
||
20 |
||
21 |
/* representation */ |
|
22 |
||
23 |
sealed abstract class Type |
|
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
24 |
{
|
|
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
25 |
def print: String = toString.toLowerCase(Locale.ENGLISH) |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
26 |
} |
| 49246 | 27 |
case object Bool extends Type |
28 |
case object Int extends Type |
|
29 |
case object Real extends Type |
|
30 |
case object String extends Type |
|
31 |
case object Unknown extends Type |
|
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
32 |
|
| 49289 | 33 |
case class Opt(name: String, typ: Type, value: String, default_value: String, |
34 |
description: String, section: String) |
|
| 48860 | 35 |
{
|
| 49289 | 36 |
private def print(default: Boolean): String = |
37 |
{
|
|
38 |
val x = if (default) default_value else value |
|
| 49247 | 39 |
"option " + name + " : " + typ.print + " = " + |
| 49289 | 40 |
(if (typ == Options.String) quote(x) else x) + |
41 |
(if (description == "") "" else "\n -- " + quote(description)) |
|
42 |
} |
|
43 |
||
44 |
def print: String = print(false) |
|
45 |
def print_default: String = print(true) |
|
| 49247 | 46 |
|
| 49270 | 47 |
def title(strip: String): String = |
48 |
{
|
|
49 |
val words = space_explode('_', name)
|
|
50 |
val words1 = |
|
51 |
words match {
|
|
52 |
case word :: rest if word == strip => rest |
|
53 |
case _ => words |
|
54 |
} |
|
55 |
words1.map(Library.capitalize).mkString(" ")
|
|
56 |
} |
|
57 |
||
| 48860 | 58 |
def unknown: Boolean = typ == Unknown |
59 |
} |
|
|
48365
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 |
|
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
62 |
/* parsing */ |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
63 |
|
| 49270 | 64 |
private val SECTION = "section" |
|
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")
|
67 |
private val PREFS = Path.explode("$ISABELLE_HOME_USER/etc/preferences")
|
|
68 |
private val PREFS_BACKUP = Path.explode("$ISABELLE_HOME_USER/etc/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() + ":" + "=" + "--" + |
|
72 |
(SECTION, Keyword.THY_HEADING2) + (OPTION, Keyword.THY_DECL) |
|
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) } |
|
|
48795
bece259ee055
clarified format of etc/options: only declarations, not re-definitions;
wenzelm
parents:
48718
diff
changeset
|
89 |
command(OPTION) ~! (option_name ~ keyword(":") ~ option_type ~
|
| 48580 | 90 |
keyword("=") ~ option_value ~ (keyword("--") ~! text ^^ { case _ ~ x => x } | success(""))) ^^
|
|
48795
bece259ee055
clarified format of etc/options: only declarations, not re-definitions;
wenzelm
parents:
48718
diff
changeset
|
91 |
{ case _ ~ (a ~ _ ~ b ~ _ ~ c ~ d) => (options: Options) => options.declare(a, b, c, d) }
|
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
92 |
} |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
93 |
|
| 48807 | 94 |
val prefs_entry: Parser[Options => Options] = |
95 |
{
|
|
96 |
option_name ~ (keyword("=") ~! option_value) ^^
|
|
| 48860 | 97 |
{ case a ~ (_ ~ b) => (options: Options) => options.add_permissive(a, b) }
|
| 48807 | 98 |
} |
99 |
||
100 |
def parse_file(syntax: Outer_Syntax, parser: Parser[Options => Options], |
|
101 |
options: Options, file: Path): Options = |
|
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
102 |
{
|
| 48807 | 103 |
val toks = syntax.scan(File.read(file)) |
104 |
val ops = |
|
105 |
parse_all(rep(parser), Token.reader(toks, file.implode)) match {
|
|
106 |
case Success(result, _) => result |
|
107 |
case bad => error(bad.toString) |
|
108 |
} |
|
| 49270 | 109 |
try { (options.set_section("") /: ops) { case (opts, op) => op(opts) } }
|
| 48992 | 110 |
catch { case ERROR(msg) => error(msg + Position.here(file.position)) }
|
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
111 |
} |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
112 |
} |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
113 |
|
| 48807 | 114 |
def init_defaults(): Options = |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
115 |
{
|
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
116 |
var options = empty |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
117 |
for {
|
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
118 |
dir <- Isabelle_System.components() |
| 48548 | 119 |
file = dir + OPTIONS if file.is_file |
| 48807 | 120 |
} { options = Parser.parse_file(options_syntax, Parser.option_entry, options, file) }
|
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
121 |
options |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
122 |
} |
| 48457 | 123 |
|
| 48807 | 124 |
def init(): Options = init_defaults().load_prefs() |
125 |
||
| 48457 | 126 |
|
127 |
/* encode */ |
|
128 |
||
129 |
val encode: XML.Encode.T[Options] = (options => options.encode) |
|
|
48693
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
130 |
|
|
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
131 |
|
|
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
132 |
/* command line entry point */ |
|
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
133 |
|
|
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
134 |
def main(args: Array[String]) |
|
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
135 |
{
|
|
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
136 |
Command_Line.tool {
|
|
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
137 |
args.toList match {
|
|
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
138 |
case export_file :: more_options => |
| 48807 | 139 |
val options = (Options.init() /: more_options)(_ + _) |
|
48693
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
140 |
|
|
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
141 |
if (export_file == "") java.lang.System.out.println(options.print) |
|
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
142 |
else File.write(Path.explode(export_file), YXML.string_of_body(options.encode)) |
|
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
143 |
|
|
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
144 |
0 |
|
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
145 |
case _ => error("Bad arguments:\n" + cat_lines(args))
|
|
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
146 |
} |
|
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
147 |
} |
|
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
148 |
} |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
149 |
} |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
150 |
|
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
151 |
|
| 49270 | 152 |
final class Options private( |
| 49296 | 153 |
val options: Map[String, Options.Opt] = Map.empty, |
| 49270 | 154 |
val section: String = "") |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
155 |
{
|
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
156 |
override def toString: String = options.iterator.mkString("Options (", ",", ")")
|
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
157 |
|
| 49247 | 158 |
def print: String = cat_lines(options.toList.sortBy(_._1).map(p => p._2.print)) |
|
48693
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
159 |
|
|
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
160 |
def description(name: String): String = check_name(name).description |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
161 |
|
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
162 |
|
| 48370 | 163 |
/* check */ |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
164 |
|
| 49246 | 165 |
def check_name(name: String): Options.Opt = |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
166 |
options.get(name) match {
|
| 48860 | 167 |
case Some(opt) if !opt.unknown => opt |
168 |
case _ => error("Unknown option " + quote(name))
|
|
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
169 |
} |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
170 |
|
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
171 |
private def check_type(name: String, typ: Options.Type): Options.Opt = |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
172 |
{
|
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
173 |
val opt = check_name(name) |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
174 |
if (opt.typ == typ) opt |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
175 |
else error("Ill-typed option " + quote(name) + " : " + opt.typ.print + " vs. " + typ.print)
|
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
176 |
} |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
177 |
|
| 48370 | 178 |
|
179 |
/* basic operations */ |
|
180 |
||
181 |
private def put[A](name: String, typ: Options.Type, value: String): Options = |
|
182 |
{
|
|
183 |
val opt = check_type(name, typ) |
|
| 49270 | 184 |
new Options(options + (name -> opt.copy(value = value)), section) |
| 48370 | 185 |
} |
186 |
||
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
187 |
private def get[A](name: String, typ: Options.Type, parse: String => Option[A]): A = |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
188 |
{
|
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
189 |
val opt = check_type(name, typ) |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
190 |
parse(opt.value) match {
|
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
191 |
case Some(x) => x |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
192 |
case None => |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
193 |
error("Malformed value for option " + quote(name) +
|
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
194 |
" : " + typ.print + " =\n" + quote(opt.value)) |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
195 |
} |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
196 |
} |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
197 |
|
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
198 |
|
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
199 |
/* internal lookup and update */ |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
200 |
|
|
49954
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents:
49296
diff
changeset
|
201 |
class Bool_Access |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
202 |
{
|
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
203 |
def apply(name: String): Boolean = get(name, Options.Bool, Properties.Value.Boolean.unapply) |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
204 |
def update(name: String, x: Boolean): Options = |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
205 |
put(name, Options.Bool, Properties.Value.Boolean(x)) |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
206 |
} |
|
49954
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents:
49296
diff
changeset
|
207 |
val bool = new Bool_Access |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
208 |
|
|
49954
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents:
49296
diff
changeset
|
209 |
class Int_Access |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
210 |
{
|
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
211 |
def apply(name: String): Int = get(name, Options.Int, Properties.Value.Int.unapply) |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
212 |
def update(name: String, x: Int): Options = |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
213 |
put(name, Options.Int, Properties.Value.Int(x)) |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
214 |
} |
|
49954
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents:
49296
diff
changeset
|
215 |
val int = new Int_Access |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
216 |
|
|
49954
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents:
49296
diff
changeset
|
217 |
class Real_Access |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
218 |
{
|
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
219 |
def apply(name: String): Double = get(name, Options.Real, Properties.Value.Double.unapply) |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
220 |
def update(name: String, x: Double): Options = |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
221 |
put(name, Options.Real, Properties.Value.Double(x)) |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
222 |
} |
|
49954
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents:
49296
diff
changeset
|
223 |
val real = new Real_Access |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
224 |
|
|
49954
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents:
49296
diff
changeset
|
225 |
class String_Access |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
226 |
{
|
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
227 |
def apply(name: String): String = get(name, Options.String, s => Some(s)) |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
228 |
def update(name: String, x: String): Options = put(name, Options.String, x) |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
229 |
} |
|
49954
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents:
49296
diff
changeset
|
230 |
val string = new String_Access |
| 48370 | 231 |
|
232 |
||
| 48807 | 233 |
/* external updates */ |
| 48370 | 234 |
|
235 |
private def check_value(name: String): Options = |
|
236 |
{
|
|
237 |
val opt = check_name(name) |
|
238 |
opt.typ match {
|
|
239 |
case Options.Bool => bool(name); this |
|
240 |
case Options.Int => int(name); this |
|
241 |
case Options.Real => real(name); this |
|
242 |
case Options.String => string(name); this |
|
| 48860 | 243 |
case Options.Unknown => this |
| 48370 | 244 |
} |
245 |
} |
|
246 |
||
247 |
def declare(name: String, typ_name: String, value: String, description: String = ""): Options = |
|
248 |
{
|
|
249 |
options.get(name) match {
|
|
250 |
case Some(_) => error("Duplicate declaration of option " + quote(name))
|
|
251 |
case None => |
|
252 |
val typ = |
|
253 |
typ_name match {
|
|
254 |
case "bool" => Options.Bool |
|
255 |
case "int" => Options.Int |
|
256 |
case "real" => Options.Real |
|
257 |
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
|
258 |
case _ => error("Unknown type for option " + quote(name) + " : " + quote(typ_name))
|
| 48370 | 259 |
} |
| 49289 | 260 |
val opt = Options.Opt(name, typ, value, value, description, section) |
| 49270 | 261 |
(new Options(options + (name -> opt), section)).check_value(name) |
| 48370 | 262 |
} |
263 |
} |
|
264 |
||
| 48860 | 265 |
def add_permissive(name: String, value: String): Options = |
266 |
{
|
|
267 |
if (options.isDefinedAt(name)) this + (name, value) |
|
| 49289 | 268 |
else |
269 |
new Options( |
|
270 |
options + (name -> Options.Opt(name, Options.Unknown, value, value, "", "")), section) |
|
| 48860 | 271 |
} |
272 |
||
| 48807 | 273 |
def + (name: String, value: String): Options = |
| 48370 | 274 |
{
|
275 |
val opt = check_name(name) |
|
| 49270 | 276 |
(new Options(options + (name -> opt.copy(value = value)), section)).check_value(name) |
| 48370 | 277 |
} |
278 |
||
| 48807 | 279 |
def + (name: String, opt_value: Option[String]): Options = |
| 48370 | 280 |
{
|
281 |
val opt = check_name(name) |
|
282 |
opt_value match {
|
|
| 48807 | 283 |
case Some(value) => this + (name, value) |
284 |
case None if opt.typ == Options.Bool => this + (name, "true") |
|
| 48370 | 285 |
case None => error("Missing value for option " + quote(name) + " : " + opt.typ.print)
|
286 |
} |
|
287 |
} |
|
288 |
||
| 48807 | 289 |
def + (str: String): Options = |
| 48370 | 290 |
{
|
291 |
str.indexOf('=') match {
|
|
| 48807 | 292 |
case -1 => this + (str, None) |
293 |
case i => this + (str.substring(0, i), str.substring(i + 1)) |
|
| 48370 | 294 |
} |
295 |
} |
|
|
48456
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
296 |
|
| 48807 | 297 |
def ++ (specs: List[Options.Spec]): Options = |
298 |
(this /: specs)({ case (x, (y, z)) => x + (y, z) })
|
|
299 |
||
|
48456
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
300 |
|
| 49270 | 301 |
/* sections */ |
302 |
||
303 |
def set_section(new_section: String): Options = |
|
304 |
new Options(options, new_section) |
|
305 |
||
306 |
def sections: List[(String, List[Options.Opt])] = |
|
307 |
options.groupBy(_._2.section).toList.map({ case (a, opts) => (a, opts.toList.map(_._2)) })
|
|
308 |
||
309 |
||
|
48456
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
310 |
/* encode */ |
|
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
311 |
|
|
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
312 |
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
|
313 |
{
|
| 48860 | 314 |
val opts = |
315 |
for ((name, opt) <- options.toList; if !opt.unknown) |
|
316 |
yield (name, opt.typ.print, opt.value) |
|
317 |
||
|
48456
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
318 |
import XML.Encode.{string => str, _}
|
| 48860 | 319 |
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
|
320 |
} |
| 48807 | 321 |
|
322 |
||
323 |
/* user preferences */ |
|
324 |
||
325 |
def load_prefs(): Options = |
|
326 |
if (Options.PREFS.is_file) |
|
327 |
Options.Parser.parse_file( |
|
328 |
Options.prefs_syntax, Options.Parser.prefs_entry, this, Options.PREFS) |
|
329 |
else this |
|
330 |
||
331 |
def save_prefs() |
|
332 |
{
|
|
| 48860 | 333 |
val defaults = Options.init_defaults() |
| 48807 | 334 |
val changed = |
335 |
(for {
|
|
| 48860 | 336 |
(name, opt2) <- options.iterator |
337 |
opt1 = defaults.options.get(name) |
|
338 |
if (opt1.isEmpty || opt1.get.value != opt2.value) |
|
339 |
} yield (name, opt2.value, if (opt1.isEmpty) " (* unknown *)" else "")).toList |
|
340 |
||
| 48807 | 341 |
val prefs = |
342 |
changed.sortBy(_._1) |
|
| 48860 | 343 |
.map({ case (x, y, z) => x + " = " + Outer_Syntax.quote_string(y) + z + "\n" }).mkString
|
| 48807 | 344 |
|
345 |
Options.PREFS.file renameTo Options.PREFS_BACKUP.file |
|
346 |
File.write(Options.PREFS, |
|
347 |
"(* generated by Isabelle " + Calendar.getInstance.getTime + " *)\n\n" + prefs) |
|
348 |
} |
|
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
349 |
} |
|
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
350 |
|
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
351 |
|
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
352 |
class Options_Variable |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
353 |
{
|
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
354 |
// owned by Swing thread |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
355 |
@volatile private var options = Options.empty |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
356 |
|
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
357 |
def value: Options = options |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
358 |
def update(new_options: Options) |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
359 |
{
|
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
360 |
Swing_Thread.require() |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
361 |
options = new_options |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
362 |
} |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
363 |
|
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
364 |
def + (name: String, x: String) |
|
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 |
Swing_Thread.require() |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
367 |
options = options + (name, x) |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
368 |
} |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
369 |
|
|
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
|
370 |
class Bool_Access |
|
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
371 |
{
|
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
372 |
def apply(name: String): Boolean = options.bool(name) |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
373 |
def update(name: String, x: Boolean) |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
374 |
{
|
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
375 |
Swing_Thread.require() |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
376 |
options = options.bool.update(name, x) |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
377 |
} |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
378 |
} |
|
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
|
379 |
val bool = new Bool_Access |
|
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
380 |
|
|
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
|
381 |
class Int_Access |
|
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
382 |
{
|
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
383 |
def apply(name: String): Int = options.int(name) |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
384 |
def update(name: String, x: Int) |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
385 |
{
|
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
386 |
Swing_Thread.require() |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
387 |
options = options.int.update(name, x) |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
388 |
} |
|
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 |
{
|
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
394 |
def apply(name: String): Double = options.real(name) |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
395 |
def update(name: String, x: Double) |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
396 |
{
|
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
397 |
Swing_Thread.require() |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
398 |
options = options.real.update(name, x) |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
399 |
} |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
400 |
} |
|
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
|
401 |
val real = new Real_Access |
|
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
402 |
|
|
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
|
403 |
class String_Access |
|
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
404 |
{
|
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
405 |
def apply(name: String): String = options.string(name) |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
406 |
def update(name: String, x: String) |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
407 |
{
|
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
408 |
Swing_Thread.require() |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
409 |
options = options.string.update(name, x) |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
410 |
} |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
411 |
} |
|
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
|
412 |
val string = new String_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 |