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