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