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