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