author | wenzelm |
Mon, 13 Mar 2023 13:46:36 +0100 | |
changeset 77625 | 25b7914f488c |
parent 77624 | 809ad223f406 |
child 77626 | af8ac22d97f0 |
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 |
|
75393 | 10 |
object Options { |
77624 | 11 |
val empty: Options = new Options() |
48421 | 12 |
|
77625 | 13 |
sealed case class Spec(name: String, value: Option[String] = None) { |
14 |
override def toString: String = name + if_proper(value, "=" + value.get) |
|
15 |
} |
|
48421 | 16 |
|
77623 | 17 |
sealed case class Change(name: String, value: String, unknown: Boolean) { |
18 |
def print_props: String = Properties.Eq(name, value) |
|
19 |
def print_prefs: String = |
|
20 |
name + " = " + Outer_Syntax.quote_string(value) + |
|
21 |
if_proper(unknown, " (* unknown *)") + "\n" |
|
22 |
} |
|
23 |
||
48421 | 24 |
|
75847 | 25 |
/* typed access */ |
75842 | 26 |
|
75846 | 27 |
abstract class Access[A](val options: Options) { |
75842 | 28 |
def apply(name: String): A |
29 |
def update(name: String, x: A): Options |
|
75847 | 30 |
def change(name: String, f: A => A): Options = update(name, f(apply(name))) |
75842 | 31 |
} |
32 |
||
75847 | 33 |
class Access_Variable[A]( |
34 |
val options: Options_Variable, |
|
35 |
val pure_access: Options => Access[A] |
|
36 |
) { |
|
37 |
def apply(name: String): A = pure_access(options.value)(name) |
|
38 |
def update(name: String, x: A): Unit = |
|
39 |
options.change(options => pure_access(options).update(name, x)) |
|
40 |
def change(name: String, f: A => A): Unit = update(name, f(apply(name))) |
|
75842 | 41 |
} |
42 |
||
43 |
||
48421 | 44 |
/* representation */ |
45 |
||
75393 | 46 |
sealed abstract class Type { |
56599 | 47 |
def print: String = Word.lowercase(toString) |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
48 |
} |
49246 | 49 |
case object Bool extends Type |
50 |
case object Int extends Type |
|
51 |
case object Real extends Type |
|
52 |
case object String extends Type |
|
53 |
case object Unknown extends Type |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
54 |
|
77611 | 55 |
val TAG_CONTENT = "content" // formal theory content |
56 |
val TAG_DOCUMENT = "document" // document preparation |
|
57 |
val TAG_BUILD = "build" // relavant for "isabelle build" |
|
58 |
val TAG_UPDATE = "update" // relevant for "isabelle update" |
|
59 |
val TAG_CONNECTION = "connection" // private information about connections (password etc.) |
|
77612 | 60 |
val TAG_COLOR_DIALOG = "color_dialog" // special color selection dialog |
77603 | 61 |
|
77605 | 62 |
case class Entry( |
56465 | 63 |
public: Boolean, |
64 |
pos: Position.T, |
|
65 |
name: String, |
|
66 |
typ: Type, |
|
67 |
value: String, |
|
68 |
default_value: String, |
|
74827
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
69 |
standard_value: Option[String], |
77603 | 70 |
tags: List[String], |
56465 | 71 |
description: String, |
75393 | 72 |
section: String |
73 |
) { |
|
74827
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
74 |
private def print_value(x: String): String = if (typ == Options.String) quote(x) else x |
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
75 |
private def print_standard: String = |
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
76 |
standard_value match { |
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
77 |
case None => "" |
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
78 |
case Some(s) if s == default_value => " (standard)" |
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
79 |
case Some(s) => " (standard " + print_value(s) + ")" |
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
80 |
} |
75393 | 81 |
private def print(default: Boolean): String = { |
49289 | 82 |
val x = if (default) default_value else value |
74827
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
83 |
"option " + name + " : " + typ.print + " = " + print_value(x) + print_standard + |
77504 | 84 |
if_proper(description, "\n -- " + quote(description)) |
49289 | 85 |
} |
86 |
||
87 |
def print: String = print(false) |
|
88 |
def print_default: String = print(true) |
|
49247 | 89 |
|
75393 | 90 |
def title(strip: String = ""): String = { |
56600 | 91 |
val words = Word.explode('_', name) |
49270 | 92 |
val words1 = |
93 |
words match { |
|
94 |
case word :: rest if word == strip => rest |
|
95 |
case _ => words |
|
96 |
} |
|
71601 | 97 |
Word.implode(words1.map(Word.perhaps_capitalize)) |
49270 | 98 |
} |
76579 | 99 |
def title_jedit: String = title("jedit") |
49270 | 100 |
|
48860 | 101 |
def unknown: Boolean = typ == Unknown |
77603 | 102 |
|
77617 | 103 |
def for_tag(tag: String): Boolean = tags.contains(tag) |
104 |
def for_content: Boolean = for_tag(TAG_CONTENT) |
|
105 |
def for_document: Boolean = for_tag(TAG_DOCUMENT) |
|
106 |
def for_color_dialog: Boolean = for_tag(TAG_COLOR_DIALOG) |
|
107 |
||
108 |
def session_content: Boolean = for_content || for_document |
|
48860 | 109 |
} |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
110 |
|
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 |
/* parsing */ |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
113 |
|
49270 | 114 |
private val SECTION = "section" |
52065
78f2475aa126
explicit notion of public options, which are shown in the editor options dialog;
wenzelm
parents:
51945
diff
changeset
|
115 |
private val PUBLIC = "public" |
48795
bece259ee055
clarified format of etc/options: only declarations, not re-definitions;
wenzelm
parents:
48718
diff
changeset
|
116 |
private val OPTION = "option" |
74827
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
117 |
private val STANDARD = "standard" |
77603 | 118 |
private val FOR = "for" |
48807 | 119 |
private val OPTIONS = Path.explode("etc/options") |
67845 | 120 |
private val PREFS = Path.explode("$ISABELLE_HOME_USER/etc/preferences") |
48713
de26cf3191a3
more token markers, based on actual outer syntax;
wenzelm
parents:
48693
diff
changeset
|
121 |
|
71601 | 122 |
val options_syntax: Outer_Syntax = |
74827
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
123 |
Outer_Syntax.empty + ":" + "=" + "--" + "(" + ")" + |
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
124 |
Symbol.comment + Symbol.comment_decoded + |
63441 | 125 |
(SECTION, Keyword.DOCUMENT_HEADING) + |
126 |
(PUBLIC, Keyword.BEFORE_COMMAND) + |
|
74827
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
127 |
(OPTION, Keyword.THY_DECL) + |
77603 | 128 |
STANDARD + FOR |
49270 | 129 |
|
71601 | 130 |
val prefs_syntax: Outer_Syntax = Outer_Syntax.empty + "=" |
48713
de26cf3191a3
more token markers, based on actual outer syntax;
wenzelm
parents:
48693
diff
changeset
|
131 |
|
75405 | 132 |
trait Parsers extends Parse.Parsers { |
71601 | 133 |
val option_name: Parser[String] = atom("option name", _.is_name) |
134 |
val option_type: Parser[String] = atom("option type", _.is_name) |
|
135 |
val option_value: Parser[String] = |
|
48807 | 136 |
opt(token("-", tok => tok.is_sym_ident && tok.content == "-")) ~ atom("nat", _.is_nat) ^^ |
137 |
{ case s ~ n => if (s.isDefined) "-" + n else n } | |
|
138 |
atom("option value", tok => tok.is_name || tok.is_float) |
|
74827
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
139 |
val option_standard: Parser[Option[String]] = |
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
140 |
$$$("(") ~! $$$(STANDARD) ~ opt(option_value) ~ $$$(")") ^^ { case _ ~ _ ~ a ~ _ => a } |
77603 | 141 |
val option_tag: Parser[String] = atom("option tag", _.is_name) |
142 |
val option_tags: Parser[List[String]] = |
|
143 |
$$$(FOR) ~! rep(option_tag) ^^ { case _ ~ x => x } | success(Nil) |
|
62968 | 144 |
} |
48807 | 145 |
|
75405 | 146 |
private object Parsers extends Parsers { |
61579 | 147 |
def comment_marker: Parser[String] = |
148 |
$$$("--") | $$$(Symbol.comment) | $$$(Symbol.comment_decoded) |
|
149 |
||
75393 | 150 |
val option_entry: Parser[Options => Options] = { |
49270 | 151 |
command(SECTION) ~! text ^^ |
49295 | 152 |
{ case _ ~ a => (options: Options) => options.set_section(a) } | |
60133
a90982bbe8b4
clarified keywords for quasi-command spans and Sidekick structure;
wenzelm
parents:
59811
diff
changeset
|
153 |
opt($$$(PUBLIC)) ~ command(OPTION) ~! (position(option_name) ~ $$$(":") ~ option_type ~ |
77603 | 154 |
$$$("=") ~ option_value ~ opt(option_standard) ~ option_tags ~ |
74827
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
155 |
(comment_marker ~! text ^^ { case _ ~ x => x } | success(""))) ^^ |
77603 | 156 |
{ case a ~ _ ~ ((b, pos) ~ _ ~ c ~ _ ~ d ~ e ~ f ~ g) => |
157 |
(options: Options) => options.declare(a.isDefined, pos, b, c, d, e, f, g) } |
|
48365
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 |
|
75393 | 160 |
val prefs_entry: Parser[Options => Options] = { |
58908 | 161 |
option_name ~ ($$$("=") ~! option_value) ^^ |
48860 | 162 |
{ case a ~ (_ ~ b) => (options: Options) => options.add_permissive(a, b) } |
48807 | 163 |
} |
164 |
||
75393 | 165 |
def parse_file( |
166 |
options: Options, |
|
167 |
file_name: String, |
|
168 |
content: String, |
|
67845 | 169 |
syntax: Outer_Syntax = options_syntax, |
75393 | 170 |
parser: Parser[Options => Options] = option_entry |
171 |
): Options = { |
|
67845 | 172 |
val toks = Token.explode(syntax.keywords, content) |
48807 | 173 |
val ops = |
67845 | 174 |
parse_all(rep(parser), Token.reader(toks, Token.Pos.file(file_name))) match { |
48807 | 175 |
case Success(result, _) => result |
176 |
case bad => error(bad.toString) |
|
177 |
} |
|
73359 | 178 |
try { ops.foldLeft(options.set_section("")) { case (opts, op) => op(opts) } } |
73166 | 179 |
catch { case ERROR(msg) => error(msg + Position.here(Position.File(file_name))) } |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
180 |
} |
67845 | 181 |
|
182 |
def parse_prefs(options: Options, content: String): Options = |
|
69366 | 183 |
parse_file(options, PREFS.file_name, content, syntax = prefs_syntax, parser = prefs_entry) |
48365
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 |
|
67845 | 186 |
def read_prefs(file: Path = PREFS): String = |
187 |
if (file.is_file) File.read(file) else "" |
|
64186
49816908ae42
support for separate sub-system options, independent of main Isabelle options;
wenzelm
parents:
64151
diff
changeset
|
188 |
|
77622
f458547b4f0f
clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents:
77621
diff
changeset
|
189 |
def init(prefs: String = read_prefs(file = PREFS), opts: List[String] = Nil): Options = { |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
190 |
var options = empty |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
191 |
for { |
73815 | 192 |
dir <- Components.directories() |
48548 | 193 |
file = dir + OPTIONS if file.is_file |
75405 | 194 |
} { options = Parsers.parse_file(options, file.implode, File.read(file)) } |
195 |
opts.foldLeft(Parsers.parse_prefs(options, prefs))(_ + _) |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
196 |
} |
48457 | 197 |
|
77609 | 198 |
def init0(): Options = init(prefs = "") |
199 |
||
48457 | 200 |
|
62832 | 201 |
/* Isabelle tool wrapper */ |
48693
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
202 |
|
72763 | 203 |
val isabelle_tool = Isabelle_Tool("options", "print Isabelle system options", |
75394 | 204 |
Scala_Project.here, |
205 |
{ args => |
|
206 |
var build_options = false |
|
207 |
var get_option = "" |
|
208 |
var list_options = false |
|
77616 | 209 |
var list_tags = List.empty[String] |
75394 | 210 |
var export_file = "" |
62437 | 211 |
|
75394 | 212 |
val getopts = Getopts(""" |
62437 | 213 |
Usage: isabelle options [OPTIONS] [MORE_OPTIONS ...] |
48693
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
214 |
|
62437 | 215 |
Options are: |
216 |
-b include $ISABELLE_BUILD_OPTIONS |
|
217 |
-g OPTION get value of OPTION |
|
218 |
-l list options |
|
77616 | 219 |
-t TAGS restrict list to given tags (comma-separated) |
62437 | 220 |
-x FILE export options to FILE in YXML format |
221 |
||
222 |
Report Isabelle system options, augmented by MORE_OPTIONS given as |
|
223 |
arguments NAME=VAL or NAME. |
|
224 |
""", |
|
75394 | 225 |
"b" -> (_ => build_options = true), |
226 |
"g:" -> (arg => get_option = arg), |
|
227 |
"l" -> (_ => list_options = true), |
|
77616 | 228 |
"t:" -> (arg => list_tags = space_explode(',', arg)), |
75394 | 229 |
"x:" -> (arg => export_file = arg)) |
52737
7b396ef36af6
clarified meaning of options for "isabelle options";
wenzelm
parents:
52735
diff
changeset
|
230 |
|
75394 | 231 |
val more_options = getopts(args) |
232 |
if (get_option == "" && !list_options && export_file == "") getopts.usage() |
|
52737
7b396ef36af6
clarified meaning of options for "isabelle options";
wenzelm
parents:
52735
diff
changeset
|
233 |
|
75394 | 234 |
val options = { |
235 |
val options0 = Options.init() |
|
236 |
val options1 = |
|
77613 | 237 |
if (build_options) { |
75394 | 238 |
Word.explode(Isabelle_System.getenv("ISABELLE_BUILD_OPTIONS")).foldLeft(options0)(_ + _) |
77613 | 239 |
} |
75394 | 240 |
else options0 |
241 |
more_options.foldLeft(options1)(_ + _) |
|
242 |
} |
|
48693
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
243 |
|
77613 | 244 |
if (get_option != "") { |
75394 | 245 |
Output.writeln(options.check_name(get_option).value, stdout = true) |
77613 | 246 |
} |
62437 | 247 |
|
77613 | 248 |
if (export_file != "") { |
75394 | 249 |
File.write(Path.explode(export_file), YXML.string_of_body(options.encode)) |
77613 | 250 |
} |
62437 | 251 |
|
77613 | 252 |
if (get_option == "" && export_file == "") { |
77616 | 253 |
val filter: Options.Entry => Boolean = |
254 |
if (list_tags.isEmpty) (_ => true) |
|
77617 | 255 |
else opt => list_tags.exists(opt.for_tag) |
77616 | 256 |
Output.writeln(options.print(filter = filter), stdout = true) |
77613 | 257 |
} |
75394 | 258 |
}) |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
259 |
} |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
260 |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
261 |
|
49270 | 262 |
final class Options private( |
77605 | 263 |
options: Map[String, Options.Entry] = Map.empty, |
75393 | 264 |
val section: String = "" |
265 |
) { |
|
77605 | 266 |
def iterator: Iterator[Options.Entry] = options.valuesIterator |
75844
7d27944d7141
clarified signature: avoid public representation;
wenzelm
parents:
75843
diff
changeset
|
267 |
|
77605 | 268 |
override def toString: String = iterator.mkString("Options(", ",", ")") |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
269 |
|
77605 | 270 |
private def print_entry(opt: Options.Entry): String = |
77614 | 271 |
if_proper(opt.public, "public ") + opt.print |
54347 | 272 |
|
77616 | 273 |
def print(filter: Options.Entry => Boolean = _ => true): String = |
274 |
cat_lines(iterator.filter(filter).toList.sortBy(_.name).map(print_entry)) |
|
48693
ceeea46bdeba
"isabelle options" prints Isabelle system options;
wenzelm
parents:
48605
diff
changeset
|
275 |
|
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
276 |
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
|
277 |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
278 |
|
48370 | 279 |
/* check */ |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
280 |
|
77605 | 281 |
def get(name: String): Option[Options.Entry] = options.get(name) |
75844
7d27944d7141
clarified signature: avoid public representation;
wenzelm
parents:
75843
diff
changeset
|
282 |
|
77605 | 283 |
def check_name(name: String): Options.Entry = |
75844
7d27944d7141
clarified signature: avoid public representation;
wenzelm
parents:
75843
diff
changeset
|
284 |
get(name) match { |
48860 | 285 |
case Some(opt) if !opt.unknown => opt |
286 |
case _ => error("Unknown option " + quote(name)) |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
287 |
} |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
288 |
|
77605 | 289 |
private def check_type(name: String, typ: Options.Type): Options.Entry = { |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
290 |
val opt = check_name(name) |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
291 |
if (opt.typ == typ) opt |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
292 |
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
|
293 |
} |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
294 |
|
48370 | 295 |
|
296 |
/* basic operations */ |
|
297 |
||
75843 | 298 |
private def put(name: String, typ: Options.Type, value: String): Options = { |
48370 | 299 |
val opt = check_type(name, typ) |
49270 | 300 |
new Options(options + (name -> opt.copy(value = value)), section) |
48370 | 301 |
} |
302 |
||
75393 | 303 |
private def get[A](name: String, typ: Options.Type, parse: String => Option[A]): A = { |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
304 |
val opt = check_type(name, typ) |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
305 |
parse(opt.value) match { |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
306 |
case Some(x) => x |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
307 |
case None => |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
308 |
error("Malformed value for option " + quote(name) + |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
309 |
" : " + typ.print + " =\n" + quote(opt.value)) |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
310 |
} |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
311 |
} |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
312 |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
313 |
|
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
314 |
/* internal lookup and update */ |
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
315 |
|
75846 | 316 |
val bool: Options.Access[Boolean] = |
317 |
new Options.Access[Boolean](this) { |
|
318 |
def apply(name: String): Boolean = get(name, Options.Bool, Value.Boolean.unapply) |
|
319 |
def update(name: String, x: Boolean): Options = put(name, Options.Bool, Value.Boolean(x)) |
|
320 |
} |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
321 |
|
75846 | 322 |
val int: Options.Access[Int] = |
323 |
new Options.Access[Int](this) { |
|
324 |
def apply(name: String): Int = get(name, Options.Int, Value.Int.unapply) |
|
325 |
def update(name: String, x: Int): Options = put(name, Options.Int, Value.Int(x)) |
|
326 |
} |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
327 |
|
75846 | 328 |
val real: Options.Access[Double] = |
329 |
new Options.Access[Double](this) { |
|
330 |
def apply(name: String): Double = get(name, Options.Real, Value.Double.unapply) |
|
331 |
def update(name: String, x: Double): Options = put(name, Options.Real, Value.Double(x)) |
|
332 |
} |
|
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
333 |
|
75846 | 334 |
val string: Options.Access[String] = |
335 |
new Options.Access[String](this) { |
|
336 |
def apply(name: String): String = get(name, Options.String, Some(_)) |
|
337 |
def update(name: String, x: String): Options = put(name, Options.String, x) |
|
338 |
} |
|
48370 | 339 |
|
69073 | 340 |
def seconds(name: String): Time = Time.seconds(real(name)) |
50207 | 341 |
|
48370 | 342 |
|
48807 | 343 |
/* external updates */ |
48370 | 344 |
|
75393 | 345 |
private def check_value(name: String): Options = { |
48370 | 346 |
val opt = check_name(name) |
347 |
opt.typ match { |
|
348 |
case Options.Bool => bool(name); this |
|
349 |
case Options.Int => int(name); this |
|
350 |
case Options.Real => real(name); this |
|
351 |
case Options.String => string(name); this |
|
48860 | 352 |
case Options.Unknown => this |
48370 | 353 |
} |
354 |
} |
|
355 |
||
56465 | 356 |
def declare( |
357 |
public: Boolean, |
|
358 |
pos: Position.T, |
|
359 |
name: String, |
|
360 |
typ_name: String, |
|
361 |
value: String, |
|
74827
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
362 |
standard: Option[Option[String]], |
77603 | 363 |
tags: List[String], |
75393 | 364 |
description: String |
365 |
): Options = { |
|
75844
7d27944d7141
clarified signature: avoid public representation;
wenzelm
parents:
75843
diff
changeset
|
366 |
get(name) match { |
56465 | 367 |
case Some(other) => |
368 |
error("Duplicate declaration of option " + quote(name) + Position.here(pos) + |
|
369 |
Position.here(other.pos)) |
|
48370 | 370 |
case None => |
371 |
val typ = |
|
372 |
typ_name match { |
|
373 |
case "bool" => Options.Bool |
|
374 |
case "int" => Options.Int |
|
375 |
case "real" => Options.Real |
|
376 |
case "string" => Options.String |
|
56465 | 377 |
case _ => |
378 |
error("Unknown type for option " + quote(name) + " : " + quote(typ_name) + |
|
379 |
Position.here(pos)) |
|
48370 | 380 |
} |
74827
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
381 |
val standard_value = |
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
382 |
standard match { |
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
383 |
case None => None |
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
384 |
case Some(_) if typ == Options.Bool => |
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
385 |
error("Illegal standard value for option " + quote(name) + " : " + typ_name + |
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
386 |
Position.here) |
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
387 |
case Some(s) => Some(s.getOrElse(value)) |
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
388 |
} |
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
389 |
val opt = |
77605 | 390 |
Options.Entry( |
77603 | 391 |
public, pos, name, typ, value, value, standard_value, tags, description, section) |
49270 | 392 |
(new Options(options + (name -> opt), section)).check_value(name) |
48370 | 393 |
} |
394 |
} |
|
395 |
||
75393 | 396 |
def add_permissive(name: String, value: String): Options = { |
48860 | 397 |
if (options.isDefinedAt(name)) this + (name, value) |
56465 | 398 |
else { |
77603 | 399 |
val opt = |
77605 | 400 |
Options.Entry(false, Position.none, name, Options.Unknown, value, value, None, Nil, "", "") |
56465 | 401 |
new Options(options + (name -> opt), section) |
402 |
} |
|
48860 | 403 |
} |
404 |
||
75393 | 405 |
def + (name: String, value: String): Options = { |
48370 | 406 |
val opt = check_name(name) |
49270 | 407 |
(new Options(options + (name -> opt.copy(value = value)), section)).check_value(name) |
48370 | 408 |
} |
409 |
||
75393 | 410 |
def + (name: String, opt_value: Option[String]): Options = { |
48370 | 411 |
val opt = check_name(name) |
74827
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
412 |
opt_value orElse opt.standard_value match { |
48807 | 413 |
case Some(value) => this + (name, value) |
74827
c1b5d6e6ff74
clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents:
74144
diff
changeset
|
414 |
case None if opt.typ == Options.Bool => this + (name, "true") |
48370 | 415 |
case None => error("Missing value for option " + quote(name) + " : " + opt.typ.print) |
416 |
} |
|
417 |
} |
|
418 |
||
48807 | 419 |
def + (str: String): Options = |
73712 | 420 |
str match { |
73715
bf51c23f3f99
clarified signature -- avoid odd warning about scala/bug#6675;
wenzelm
parents:
73712
diff
changeset
|
421 |
case Properties.Eq(a, b) => this + (a, b) |
73712 | 422 |
case _ => this + (str, None) |
48370 | 423 |
} |
48456
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
424 |
|
48807 | 425 |
def ++ (specs: List[Options.Spec]): Options = |
77624 | 426 |
specs.foldLeft(this) { case (x, Options.Spec(y, z)) => x + (y, z) } |
48807 | 427 |
|
48456
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
428 |
|
49270 | 429 |
/* sections */ |
430 |
||
431 |
def set_section(new_section: String): Options = |
|
432 |
new Options(options, new_section) |
|
433 |
||
77605 | 434 |
def sections: List[(String, List[Options.Entry])] = |
49270 | 435 |
options.groupBy(_._2.section).toList.map({ case (a, opts) => (a, opts.toList.map(_._2)) }) |
436 |
||
437 |
||
48456
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
438 |
/* encode */ |
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
439 |
|
75393 | 440 |
def encode: XML.Body = { |
48860 | 441 |
val opts = |
56465 | 442 |
for ((_, opt) <- options.toList; if !opt.unknown) |
443 |
yield (opt.pos, (opt.name, (opt.typ.print, opt.value))) |
|
48860 | 444 |
|
56465 | 445 |
import XML.Encode.{string => string_, _} |
446 |
list(pair(properties, pair(string_, pair(string_, string_))))(opts) |
|
48456
d8ff14f44a40
added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents:
48421
diff
changeset
|
447 |
} |
48807 | 448 |
|
449 |
||
77622
f458547b4f0f
clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents:
77621
diff
changeset
|
450 |
/* changed options */ |
f458547b4f0f
clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents:
77621
diff
changeset
|
451 |
|
f458547b4f0f
clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents:
77621
diff
changeset
|
452 |
def changed( |
f458547b4f0f
clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents:
77621
diff
changeset
|
453 |
defaults: Options = Options.init0(), |
f458547b4f0f
clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents:
77621
diff
changeset
|
454 |
filter: Options.Entry => Boolean = _ => true |
77623 | 455 |
): List[Options.Change] = { |
77622
f458547b4f0f
clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents:
77621
diff
changeset
|
456 |
List.from( |
f458547b4f0f
clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents:
77621
diff
changeset
|
457 |
for { |
f458547b4f0f
clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents:
77621
diff
changeset
|
458 |
(name, opt2) <- options.iterator |
f458547b4f0f
clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents:
77621
diff
changeset
|
459 |
opt1 = defaults.get(name) |
f458547b4f0f
clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents:
77621
diff
changeset
|
460 |
if (opt1.isEmpty || opt1.get.value != opt2.value) && filter(opt2) |
77623 | 461 |
} yield Options.Change(name, opt2.value, opt1.isEmpty)) |
77622
f458547b4f0f
clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents:
77621
diff
changeset
|
462 |
} |
f458547b4f0f
clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents:
77621
diff
changeset
|
463 |
|
f458547b4f0f
clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents:
77621
diff
changeset
|
464 |
|
77608 | 465 |
/* preferences */ |
48807 | 466 |
|
77603 | 467 |
def make_prefs( |
77609 | 468 |
defaults: Options = Options.init0(), |
77605 | 469 |
filter: Options.Entry => Boolean = _ => true |
77622
f458547b4f0f
clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents:
77621
diff
changeset
|
470 |
): String = changed(defaults = defaults, filter = filter).map(_.print_prefs).mkString |
77374
268bf61631ec
more robust options in "prefs" format: avoid odd control character;
wenzelm
parents:
77366
diff
changeset
|
471 |
|
77609 | 472 |
def save_prefs(file: Path = Options.PREFS, defaults: Options = Options.init0()): Unit = { |
77374
268bf61631ec
more robust options in "prefs" format: avoid odd control character;
wenzelm
parents:
77366
diff
changeset
|
473 |
val prefs = make_prefs(defaults = defaults) |
72375 | 474 |
Isabelle_System.make_directory(file.dir) |
67845 | 475 |
File.write_backup(file, "(* generated by Isabelle " + Date.now() + " *)\n\n" + prefs) |
48807 | 476 |
} |
48365
d88aefda01c4
basic support for stand-alone options with external string representation;
wenzelm
parents:
diff
changeset
|
477 |
} |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
478 |
|
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
479 |
|
75393 | 480 |
class Options_Variable(init_options: Options) { |
75846 | 481 |
private var _options = init_options |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
482 |
|
75846 | 483 |
def value: Options = synchronized { _options } |
484 |
def change(f: Options => Options): Unit = synchronized { _options = f(_options) } |
|
485 |
def += (name: String, x: String): Unit = change(options => options + (name, x)) |
|
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
486 |
|
75846 | 487 |
val bool: Options.Access_Variable[Boolean] = |
75847 | 488 |
new Options.Access_Variable[Boolean](this, _.bool) |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
489 |
|
75846 | 490 |
val int: Options.Access_Variable[Int] = |
75847 | 491 |
new Options.Access_Variable[Int](this, _.int) |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
492 |
|
75846 | 493 |
val real: Options.Access_Variable[Double] = |
75847 | 494 |
new Options.Access_Variable[Double](this, _.real) |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
495 |
|
75846 | 496 |
val string: Options.Access_Variable[String] = |
75847 | 497 |
new Options.Access_Variable[String](this, _.string) |
50207 | 498 |
|
69073 | 499 |
def seconds(name: String): Time = value.seconds(name) |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48992
diff
changeset
|
500 |
} |