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