src/Pure/System/options.scala
author wenzelm
Thu, 15 May 2025 22:55:29 +0200
changeset 82625 0fa6759948bc
parent 81085 fe69241e8786
permissions -rw-r--r--
explicit support for dark GUI themes in Isabelle/jEdit;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
5b1ac9843f02 tuned comments;
wenzelm
parents: 51620
diff changeset
     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
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74827
diff changeset
    10
object Options {
77624
809ad223f406 clarified signature: more explicit types;
wenzelm
parents: 77623
diff changeset
    11
  val empty: Options = new Options()
48421
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    12
77626
af8ac22d97f0 clarified signature: more explicit type Options.Spec, which incorporates all variants of Options.+;
wenzelm
parents: 77625
diff changeset
    13
  object Spec {
78911
81dab48582c6 clarified modules;
wenzelm
parents: 78909
diff changeset
    14
    val syntax: Outer_Syntax = Outer_Syntax.empty + "=" + ","
81dab48582c6 clarified modules;
wenzelm
parents: 78909
diff changeset
    15
81dab48582c6 clarified modules;
wenzelm
parents: 78909
diff changeset
    16
    def parse(content: String): List[Spec] = {
81dab48582c6 clarified modules;
wenzelm
parents: 78909
diff changeset
    17
      val parser = Parsers.repsep(Parsers.option_spec, Parsers.$$$(","))
81dab48582c6 clarified modules;
wenzelm
parents: 78909
diff changeset
    18
      val reader = Token.reader(Token.explode(syntax.keywords, content), Token.Pos.none)
81dab48582c6 clarified modules;
wenzelm
parents: 78909
diff changeset
    19
      Parsers.parse_all(parser, reader) match {
81dab48582c6 clarified modules;
wenzelm
parents: 78909
diff changeset
    20
        case Parsers.Success(result, _) => result
81dab48582c6 clarified modules;
wenzelm
parents: 78909
diff changeset
    21
        case bad => error(bad.toString)
81dab48582c6 clarified modules;
wenzelm
parents: 78909
diff changeset
    22
      }
81dab48582c6 clarified modules;
wenzelm
parents: 78909
diff changeset
    23
    }
81dab48582c6 clarified modules;
wenzelm
parents: 78909
diff changeset
    24
78916
e97fa2edf4b2 clarified signature;
wenzelm
parents: 78915
diff changeset
    25
    def eq(a: String, b: String, permissive: Boolean = false): Spec =
e97fa2edf4b2 clarified signature;
wenzelm
parents: 78915
diff changeset
    26
      Spec(a, value = Some(b), permissive = permissive)
e97fa2edf4b2 clarified signature;
wenzelm
parents: 78915
diff changeset
    27
77626
af8ac22d97f0 clarified signature: more explicit type Options.Spec, which incorporates all variants of Options.+;
wenzelm
parents: 77625
diff changeset
    28
    def make(s: String): Spec =
af8ac22d97f0 clarified signature: more explicit type Options.Spec, which incorporates all variants of Options.+;
wenzelm
parents: 77625
diff changeset
    29
      s match {
78916
e97fa2edf4b2 clarified signature;
wenzelm
parents: 78915
diff changeset
    30
        case Properties.Eq(a, b) => eq(a, b)
77626
af8ac22d97f0 clarified signature: more explicit type Options.Spec, which incorporates all variants of Options.+;
wenzelm
parents: 77625
diff changeset
    31
        case _ => Spec(s)
af8ac22d97f0 clarified signature: more explicit type Options.Spec, which incorporates all variants of Options.+;
wenzelm
parents: 77625
diff changeset
    32
      }
77628
a538dab533ef clarified signature: prefer explicit types;
wenzelm
parents: 77626
diff changeset
    33
a538dab533ef clarified signature: prefer explicit types;
wenzelm
parents: 77626
diff changeset
    34
    def ISABELLE_BUILD_OPTIONS: List[Spec] =
a538dab533ef clarified signature: prefer explicit types;
wenzelm
parents: 77626
diff changeset
    35
      Word.explode(Isabelle_System.getenv("ISABELLE_BUILD_OPTIONS")).map(make)
78560
39f6f180008d clarified signature;
wenzelm
parents: 78559
diff changeset
    36
39f6f180008d clarified signature;
wenzelm
parents: 78559
diff changeset
    37
    def print_value(s: String): String =
39f6f180008d clarified signature;
wenzelm
parents: 78559
diff changeset
    38
      s match {
39f6f180008d clarified signature;
wenzelm
parents: 78559
diff changeset
    39
        case Value.Boolean(_) => s
39f6f180008d clarified signature;
wenzelm
parents: 78559
diff changeset
    40
        case Value.Long(_) => s
39f6f180008d clarified signature;
wenzelm
parents: 78559
diff changeset
    41
        case Value.Double(_) => s
78911
81dab48582c6 clarified modules;
wenzelm
parents: 78909
diff changeset
    42
        case _ => Token.quote_name(syntax.keywords, s)
78560
39f6f180008d clarified signature;
wenzelm
parents: 78559
diff changeset
    43
      }
39f6f180008d clarified signature;
wenzelm
parents: 78559
diff changeset
    44
39f6f180008d clarified signature;
wenzelm
parents: 78559
diff changeset
    45
    def print(name: String, value: String): String = Properties.Eq(name, print_value(value))
78908
f38153e58f21 proper Option.Spec.toString for bash script: avoid Token.quote_name of Options.Spec.print_value (amending 3d1746a716fa, see also 39f6f180008d);
wenzelm
parents: 78614
diff changeset
    46
78915
90756ad4d8d7 more accurate treatment of surrounding whitespace;
wenzelm
parents: 78911
diff changeset
    47
    def bash_strings(opts: Iterable[Spec], bg: Boolean = false, en: Boolean = false): String = {
90756ad4d8d7 more accurate treatment of surrounding whitespace;
wenzelm
parents: 78911
diff changeset
    48
      val it = opts.iterator
90756ad4d8d7 more accurate treatment of surrounding whitespace;
wenzelm
parents: 78911
diff changeset
    49
      if (it.isEmpty) ""
90756ad4d8d7 more accurate treatment of surrounding whitespace;
wenzelm
parents: 78911
diff changeset
    50
      else {
90756ad4d8d7 more accurate treatment of surrounding whitespace;
wenzelm
parents: 78911
diff changeset
    51
        it.map(opt => "-o " + Bash.string(opt.toString))
90756ad4d8d7 more accurate treatment of surrounding whitespace;
wenzelm
parents: 78911
diff changeset
    52
          .mkString(if (bg) " " else "", " ", if (en) " " else "")
90756ad4d8d7 more accurate treatment of surrounding whitespace;
wenzelm
parents: 78911
diff changeset
    53
      }
90756ad4d8d7 more accurate treatment of surrounding whitespace;
wenzelm
parents: 78911
diff changeset
    54
    }
77626
af8ac22d97f0 clarified signature: more explicit type Options.Spec, which incorporates all variants of Options.+;
wenzelm
parents: 77625
diff changeset
    55
  }
af8ac22d97f0 clarified signature: more explicit type Options.Spec, which incorporates all variants of Options.+;
wenzelm
parents: 77625
diff changeset
    56
af8ac22d97f0 clarified signature: more explicit type Options.Spec, which incorporates all variants of Options.+;
wenzelm
parents: 77625
diff changeset
    57
  sealed case class Spec(name: String, value: Option[String] = None, permissive: Boolean = false) {
77625
25b7914f488c tuned output;
wenzelm
parents: 77624
diff changeset
    58
    override def toString: String = name + if_proper(value, "=" + value.get)
78909
wenzelm
parents: 78908
diff changeset
    59
    def print: String =
wenzelm
parents: 78908
diff changeset
    60
      value match {
wenzelm
parents: 78908
diff changeset
    61
        case None => name
wenzelm
parents: 78908
diff changeset
    62
        case Some(v) => Spec.print(name, v)
wenzelm
parents: 78908
diff changeset
    63
      }
77625
25b7914f488c tuned output;
wenzelm
parents: 77624
diff changeset
    64
  }
48421
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    65
77623
157ad1f976d2 clarified signature: prefer static types;
wenzelm
parents: 77622
diff changeset
    66
  sealed case class Change(name: String, value: String, unknown: Boolean) {
78916
e97fa2edf4b2 clarified signature;
wenzelm
parents: 78915
diff changeset
    67
    def spec: Spec = Spec.eq(name, value)
77626
af8ac22d97f0 clarified signature: more explicit type Options.Spec, which incorporates all variants of Options.+;
wenzelm
parents: 77625
diff changeset
    68
77623
157ad1f976d2 clarified signature: prefer static types;
wenzelm
parents: 77622
diff changeset
    69
    def print_prefs: String =
157ad1f976d2 clarified signature: prefer static types;
wenzelm
parents: 77622
diff changeset
    70
      name + " = " + Outer_Syntax.quote_string(value) +
157ad1f976d2 clarified signature: prefer static types;
wenzelm
parents: 77622
diff changeset
    71
        if_proper(unknown, "  (* unknown *)") + "\n"
157ad1f976d2 clarified signature: prefer static types;
wenzelm
parents: 77622
diff changeset
    72
  }
157ad1f976d2 clarified signature: prefer static types;
wenzelm
parents: 77622
diff changeset
    73
48421
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    74
75847
93436389db1c clarified signature: more explicit types;
wenzelm
parents: 75846
diff changeset
    75
  /* typed access */
75842
a8c401312f9d clarified signature;
wenzelm
parents: 75405
diff changeset
    76
75846
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
    77
  abstract class Access[A](val options: Options) {
75842
a8c401312f9d clarified signature;
wenzelm
parents: 75405
diff changeset
    78
    def apply(name: String): A
a8c401312f9d clarified signature;
wenzelm
parents: 75405
diff changeset
    79
    def update(name: String, x: A): Options
75847
93436389db1c clarified signature: more explicit types;
wenzelm
parents: 75846
diff changeset
    80
    def change(name: String, f: A => A): Options = update(name, f(apply(name)))
75842
a8c401312f9d clarified signature;
wenzelm
parents: 75405
diff changeset
    81
  }
a8c401312f9d clarified signature;
wenzelm
parents: 75405
diff changeset
    82
75847
93436389db1c clarified signature: more explicit types;
wenzelm
parents: 75846
diff changeset
    83
  class Access_Variable[A](
93436389db1c clarified signature: more explicit types;
wenzelm
parents: 75846
diff changeset
    84
    val options: Options_Variable,
93436389db1c clarified signature: more explicit types;
wenzelm
parents: 75846
diff changeset
    85
    val pure_access: Options => Access[A]
93436389db1c clarified signature: more explicit types;
wenzelm
parents: 75846
diff changeset
    86
  ) {
93436389db1c clarified signature: more explicit types;
wenzelm
parents: 75846
diff changeset
    87
    def apply(name: String): A = pure_access(options.value)(name)
93436389db1c clarified signature: more explicit types;
wenzelm
parents: 75846
diff changeset
    88
    def update(name: String, x: A): Unit =
93436389db1c clarified signature: more explicit types;
wenzelm
parents: 75846
diff changeset
    89
      options.change(options => pure_access(options).update(name, x))
93436389db1c clarified signature: more explicit types;
wenzelm
parents: 75846
diff changeset
    90
    def change(name: String, f: A => A): Unit = update(name, f(apply(name)))
75842
a8c401312f9d clarified signature;
wenzelm
parents: 75405
diff changeset
    91
  }
a8c401312f9d clarified signature;
wenzelm
parents: 75405
diff changeset
    92
a8c401312f9d clarified signature;
wenzelm
parents: 75405
diff changeset
    93
48421
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    94
  /* representation */
c4d337782de4 propagate defined options;
wenzelm
parents: 48411
diff changeset
    95
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74827
diff changeset
    96
  sealed abstract class Type {
56599
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents: 56559
diff changeset
    97
    def print: String = Word.lowercase(toString)
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
    98
  }
49246
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
    99
  case object Bool extends Type
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
   100
  case object Int extends Type
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
   101
  case object Real extends Type
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
   102
  case object String extends Type
248e66e8321f more systematic JEdit_Options.make_component;
wenzelm
parents: 49245
diff changeset
   103
  case object Unknown extends Type
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   104
77611
606ac3fae270 clarified tags;
wenzelm
parents: 77610
diff changeset
   105
  val TAG_CONTENT = "content"    // formal theory content
606ac3fae270 clarified tags;
wenzelm
parents: 77610
diff changeset
   106
  val TAG_DOCUMENT = "document"  // document preparation
79526
6e5397fcc41b add build_sync tag to sync certain options (e.g., build_engine) across build processes;
Fabian Huch <huch@in.tum.de>
parents: 78916
diff changeset
   107
  val TAG_BUILD = "build"        // relevant for "isabelle build"
6e5397fcc41b add build_sync tag to sync certain options (e.g., build_engine) across build processes;
Fabian Huch <huch@in.tum.de>
parents: 78916
diff changeset
   108
  val TAG_BUILD_SYNC = "build_sync" // relevant for distributed "isabelle build"
77611
606ac3fae270 clarified tags;
wenzelm
parents: 77610
diff changeset
   109
  val TAG_UPDATE = "update"      // relevant for "isabelle update"
606ac3fae270 clarified tags;
wenzelm
parents: 77610
diff changeset
   110
  val TAG_CONNECTION = "connection"  // private information about connections (password etc.)
77612
3e235fab64db avoid hard-wired stuff (see also 78f2475aa126);
wenzelm
parents: 77611
diff changeset
   111
  val TAG_COLOR_DIALOG = "color_dialog"  // special color selection dialog
81049
45ef41e823f7 added vscode options tag;
Thomas Lindae <thomas.lindae@in.tum.de>
parents: 79656
diff changeset
   112
  val TAG_VSCODE = "vscode"      // relevant for "isabelle vscode" and "isabelle vscode_server"
77603
236e43c8bb5b support option tags;
wenzelm
parents: 77504
diff changeset
   113
82625
0fa6759948bc explicit support for dark GUI themes in Isabelle/jEdit;
wenzelm
parents: 81085
diff changeset
   114
  val SUFFIX_DARK = "_dark"
0fa6759948bc explicit support for dark GUI themes in Isabelle/jEdit;
wenzelm
parents: 81085
diff changeset
   115
  def theme_suffix(): String = if (GUI.is_dark_laf()) SUFFIX_DARK else ""
0fa6759948bc explicit support for dark GUI themes in Isabelle/jEdit;
wenzelm
parents: 81085
diff changeset
   116
77605
bc1248c5d159 clarified signature;
wenzelm
parents: 77603
diff changeset
   117
  case class Entry(
56465
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   118
    public: Boolean,
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   119
    pos: Position.T,
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   120
    name: String,
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   121
    typ: Type,
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   122
    value: String,
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   123
    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
   124
    standard_value: Option[String],
77603
236e43c8bb5b support option tags;
wenzelm
parents: 77504
diff changeset
   125
    tags: List[String],
56465
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   126
    description: String,
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74827
diff changeset
   127
    section: String
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74827
diff changeset
   128
  ) {
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
    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
   130
    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
   131
      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
   132
        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
   133
        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
   134
        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
   135
      }
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74827
diff changeset
   136
    private def print(default: Boolean): String = {
49289
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
   137
      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
   138
      "option " + name + " : " + typ.print + " = " + print_value(x) + print_standard +
77504
wenzelm
parents: 77374
diff changeset
   139
        if_proper(description, "\n  -- " + quote(description))
49289
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
   140
    }
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
   141
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
   142
    def print: String = print(false)
60424f123621 more informative tooltip: default value;
wenzelm
parents: 49270
diff changeset
   143
    def print_default: String = print(true)
49247
ffd9ad9dc35b more detailed option tooltip;
wenzelm
parents: 49246
diff changeset
   144
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74827
diff changeset
   145
    def title(strip: String = ""): String = {
56600
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
   146
      val words = Word.explode('_', name)
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   147
      val words1 =
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   148
        words match {
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   149
          case word :: rest if word == strip => rest
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   150
          case _ => words
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   151
        }
78614
4da5cdaa4dcd clarified signature;
wenzelm
parents: 78560
diff changeset
   152
      Word.implode(words1.map(Word.perhaps_capitalized))
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   153
    }
76579
c79b43c1c7ab tuned signature;
wenzelm
parents: 75847
diff changeset
   154
    def title_jedit: String = title("jedit")
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   155
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   156
    def unknown: Boolean = typ == Unknown
77603
236e43c8bb5b support option tags;
wenzelm
parents: 77504
diff changeset
   157
77617
58b7f3fb73cb tuned signature;
wenzelm
parents: 77616
diff changeset
   158
    def for_tag(tag: String): Boolean = tags.contains(tag)
58b7f3fb73cb tuned signature;
wenzelm
parents: 77616
diff changeset
   159
    def for_content: Boolean = for_tag(TAG_CONTENT)
58b7f3fb73cb tuned signature;
wenzelm
parents: 77616
diff changeset
   160
    def for_document: Boolean = for_tag(TAG_DOCUMENT)
58b7f3fb73cb tuned signature;
wenzelm
parents: 77616
diff changeset
   161
    def for_color_dialog: Boolean = for_tag(TAG_COLOR_DIALOG)
79526
6e5397fcc41b add build_sync tag to sync certain options (e.g., build_engine) across build processes;
Fabian Huch <huch@in.tum.de>
parents: 78916
diff changeset
   162
    def for_build_sync: Boolean = for_tag(TAG_BUILD_SYNC)
81085
fe69241e8786 clarified: add operation;
Fabian Huch <huch@in.tum.de>
parents: 81049
diff changeset
   163
    def for_vscode: Boolean = for_tag(TAG_VSCODE)
77617
58b7f3fb73cb tuned signature;
wenzelm
parents: 77616
diff changeset
   164
82625
0fa6759948bc explicit support for dark GUI themes in Isabelle/jEdit;
wenzelm
parents: 81085
diff changeset
   165
    def is_dark: Boolean = name.endsWith(SUFFIX_DARK)
0fa6759948bc explicit support for dark GUI themes in Isabelle/jEdit;
wenzelm
parents: 81085
diff changeset
   166
77617
58b7f3fb73cb tuned signature;
wenzelm
parents: 77616
diff changeset
   167
    def session_content: Boolean = for_content || for_document
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   168
  }
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   169
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
  /* parsing */
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   172
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   173
  private val SECTION = "section"
52065
78f2475aa126 explicit notion of public options, which are shown in the editor options dialog;
wenzelm
parents: 51945
diff changeset
   174
  private val PUBLIC = "public"
48795
bece259ee055 clarified format of etc/options: only declarations, not re-definitions;
wenzelm
parents: 48718
diff changeset
   175
  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
   176
  private val STANDARD = "standard"
77603
236e43c8bb5b support option tags;
wenzelm
parents: 77504
diff changeset
   177
  private val FOR = "for"
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   178
  private val OPTIONS = Path.explode("etc/options")
67845
46fa8c2c142a more flexible preferences: avoid hardwired file;
wenzelm
parents: 67178
diff changeset
   179
  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
   180
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 69366
diff changeset
   181
  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
   182
    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
   183
      Symbol.comment + Symbol.comment_decoded +
63441
4c3fa4dba79f explicit kind "before_command";
wenzelm
parents: 62968
diff changeset
   184
      (SECTION, Keyword.DOCUMENT_HEADING) +
4c3fa4dba79f explicit kind "before_command";
wenzelm
parents: 62968
diff changeset
   185
      (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
   186
      (OPTION, Keyword.THY_DECL) +
77603
236e43c8bb5b support option tags;
wenzelm
parents: 77504
diff changeset
   187
      STANDARD + FOR
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   188
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 69366
diff changeset
   189
  val prefs_syntax: Outer_Syntax = Outer_Syntax.empty + "="
48713
de26cf3191a3 more token markers, based on actual outer syntax;
wenzelm
parents: 48693
diff changeset
   190
75405
b13ab7d11b90 clarified signature: avoid ambiguity in scala3;
wenzelm
parents: 75394
diff changeset
   191
  trait Parsers extends Parse.Parsers {
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 69366
diff changeset
   192
    val option_name: Parser[String] = atom("option name", _.is_name)
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 69366
diff changeset
   193
    val option_type: Parser[String] = atom("option type", _.is_name)
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 69366
diff changeset
   194
    val option_value: Parser[String] =
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   195
      opt(token("-", tok => tok.is_sym_ident && tok.content == "-")) ~ atom("nat", _.is_nat) ^^
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   196
        { case s ~ n => if (s.isDefined) "-" + n else n } |
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   197
      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
   198
    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
   199
      $$$("(") ~! $$$(STANDARD) ~ opt(option_value) ~ $$$(")") ^^ { case _ ~ _ ~ a ~ _ => a }
77603
236e43c8bb5b support option tags;
wenzelm
parents: 77504
diff changeset
   200
    val option_tag: Parser[String] = atom("option tag", _.is_name)
236e43c8bb5b support option tags;
wenzelm
parents: 77504
diff changeset
   201
    val option_tags: Parser[List[String]] =
236e43c8bb5b support option tags;
wenzelm
parents: 77504
diff changeset
   202
      $$$(FOR) ~! rep(option_tag) ^^ { case _ ~ x => x } | success(Nil)
78407
b262ecc98319 more operations for independent "inline" options;
wenzelm
parents: 77668
diff changeset
   203
    val option_spec: Parser[Spec] =
b262ecc98319 more operations for independent "inline" options;
wenzelm
parents: 77668
diff changeset
   204
      option_name ~ opt($$$("=") ~! option_value ^^ { case _ ~ x => x }) ^^
78916
e97fa2edf4b2 clarified signature;
wenzelm
parents: 78915
diff changeset
   205
        { case x ~ y => Options.Spec(x, value = y) }
62968
4e4738698db4 clarified syntax;
wenzelm
parents: 62832
diff changeset
   206
  }
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   207
75405
b13ab7d11b90 clarified signature: avoid ambiguity in scala3;
wenzelm
parents: 75394
diff changeset
   208
  private object Parsers extends Parsers {
61579
634cd44bb1d3 symbolic syntax "\<comment> text";
wenzelm
parents: 60215
diff changeset
   209
    def comment_marker: Parser[String] =
634cd44bb1d3 symbolic syntax "\<comment> text";
wenzelm
parents: 60215
diff changeset
   210
      $$$("--") | $$$(Symbol.comment) | $$$(Symbol.comment_decoded)
634cd44bb1d3 symbolic syntax "\<comment> text";
wenzelm
parents: 60215
diff changeset
   211
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74827
diff changeset
   212
    val option_entry: Parser[Options => Options] = {
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   213
      command(SECTION) ~! text ^^
49295
2750756db9c5 more precise sections;
wenzelm
parents: 49289
diff changeset
   214
        { case _ ~ a => (options: Options) => options.set_section(a) } |
60133
a90982bbe8b4 clarified keywords for quasi-command spans and Sidekick structure;
wenzelm
parents: 59811
diff changeset
   215
      opt($$$(PUBLIC)) ~ command(OPTION) ~! (position(option_name) ~ $$$(":") ~ option_type ~
77603
236e43c8bb5b support option tags;
wenzelm
parents: 77504
diff changeset
   216
      $$$("=") ~ 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
   217
        (comment_marker ~! text ^^ { case _ ~ x => x } | success(""))) ^^
77603
236e43c8bb5b support option tags;
wenzelm
parents: 77504
diff changeset
   218
        { case a ~ _ ~ ((b, pos) ~ _ ~ c ~ _ ~ d ~ e ~ f ~ g) =>
236e43c8bb5b support option tags;
wenzelm
parents: 77504
diff changeset
   219
            (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
   220
    }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   221
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74827
diff changeset
   222
    val prefs_entry: Parser[Options => Options] = {
58908
58bedbc18915 tuned signature;
wenzelm
parents: 58868
diff changeset
   223
      option_name ~ ($$$("=") ~! option_value) ^^
77626
af8ac22d97f0 clarified signature: more explicit type Options.Spec, which incorporates all variants of Options.+;
wenzelm
parents: 77625
diff changeset
   224
      { case a ~ (_ ~ b) => (options: Options) =>
78916
e97fa2edf4b2 clarified signature;
wenzelm
parents: 78915
diff changeset
   225
          options + Options.Spec.eq(a, b, permissive = true) }
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   226
    }
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   227
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74827
diff changeset
   228
    def parse_file(
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74827
diff changeset
   229
      options: Options,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74827
diff changeset
   230
      file_name: String,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74827
diff changeset
   231
      content: String,
67845
46fa8c2c142a more flexible preferences: avoid hardwired file;
wenzelm
parents: 67178
diff changeset
   232
      syntax: Outer_Syntax = options_syntax,
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74827
diff changeset
   233
      parser: Parser[Options => Options] = option_entry
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74827
diff changeset
   234
    ): Options = {
67845
46fa8c2c142a more flexible preferences: avoid hardwired file;
wenzelm
parents: 67178
diff changeset
   235
      val toks = Token.explode(syntax.keywords, content)
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   236
      val ops =
67845
46fa8c2c142a more flexible preferences: avoid hardwired file;
wenzelm
parents: 67178
diff changeset
   237
        parse_all(rep(parser), Token.reader(toks, Token.Pos.file(file_name))) match {
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   238
          case Success(result, _) => result
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   239
          case bad => error(bad.toString)
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   240
        }
73359
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   241
      try { ops.foldLeft(options.set_section("")) { case (opts, op) => op(opts) } }
73166
78dd1abfbbe1 proper message;
wenzelm
parents: 72763
diff changeset
   242
      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
   243
    }
67845
46fa8c2c142a more flexible preferences: avoid hardwired file;
wenzelm
parents: 67178
diff changeset
   244
46fa8c2c142a more flexible preferences: avoid hardwired file;
wenzelm
parents: 67178
diff changeset
   245
    def parse_prefs(options: Options, content: String): Options =
69366
b6dacf6eabe3 clarified signature;
wenzelm
parents: 69074
diff changeset
   246
      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
   247
  }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   248
67845
46fa8c2c142a more flexible preferences: avoid hardwired file;
wenzelm
parents: 67178
diff changeset
   249
  def read_prefs(file: Path = PREFS): String =
46fa8c2c142a more flexible preferences: avoid hardwired file;
wenzelm
parents: 67178
diff changeset
   250
    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
   251
78407
b262ecc98319 more operations for independent "inline" options;
wenzelm
parents: 77668
diff changeset
   252
  def inline(content: String): Options = Parsers.parse_file(empty, "inline", content)
b262ecc98319 more operations for independent "inline" options;
wenzelm
parents: 77668
diff changeset
   253
77628
a538dab533ef clarified signature: prefer explicit types;
wenzelm
parents: 77626
diff changeset
   254
  def init(prefs: String = read_prefs(file = PREFS), specs: List[Spec] = Nil): Options = {
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   255
    var options = empty
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   256
    for {
73815
43882e34c038 clarified modules;
wenzelm
parents: 73715
diff changeset
   257
      dir <- Components.directories()
48548
49afe0e92163 simplified Path vs. JVM File operations;
wenzelm
parents: 48457
diff changeset
   258
      file = dir + OPTIONS if file.is_file
75405
b13ab7d11b90 clarified signature: avoid ambiguity in scala3;
wenzelm
parents: 75394
diff changeset
   259
    } { options = Parsers.parse_file(options, file.implode, File.read(file)) }
77628
a538dab533ef clarified signature: prefer explicit types;
wenzelm
parents: 77626
diff changeset
   260
    Parsers.parse_prefs(options, prefs) ++ specs
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   261
  }
48457
fd9e28d5a143 pass build options to ML;
wenzelm
parents: 48456
diff changeset
   262
77609
a45cce93529c tuned signature;
wenzelm
parents: 77608
diff changeset
   263
  def init0(): Options = init(prefs = "")
a45cce93529c tuned signature;
wenzelm
parents: 77608
diff changeset
   264
48457
fd9e28d5a143 pass build options to ML;
wenzelm
parents: 48456
diff changeset
   265
62832
c1410bcf6e87 prefer internal tool;
wenzelm
parents: 62454
diff changeset
   266
  /* Isabelle tool wrapper */
48693
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   267
72763
3cc73d00553c added document antiquotation @{tool};
wenzelm
parents: 72375
diff changeset
   268
  val isabelle_tool = Isabelle_Tool("options", "print Isabelle system options",
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   269
    Scala_Project.here,
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   270
    { args =>
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   271
      var build_options = false
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   272
      var get_option = ""
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   273
      var list_options = false
77616
29effd67d8a8 support "isabelle options -l -t TAGS";
wenzelm
parents: 77614
diff changeset
   274
      var list_tags = List.empty[String]
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   275
      var export_file = ""
62437
bccad0374407 moved getopts to Scala;
wenzelm
parents: 62227
diff changeset
   276
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   277
      val getopts = Getopts("""
62437
bccad0374407 moved getopts to Scala;
wenzelm
parents: 62227
diff changeset
   278
Usage: isabelle options [OPTIONS] [MORE_OPTIONS ...]
48693
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   279
62437
bccad0374407 moved getopts to Scala;
wenzelm
parents: 62227
diff changeset
   280
  Options are:
bccad0374407 moved getopts to Scala;
wenzelm
parents: 62227
diff changeset
   281
    -b           include $ISABELLE_BUILD_OPTIONS
bccad0374407 moved getopts to Scala;
wenzelm
parents: 62227
diff changeset
   282
    -g OPTION    get value of OPTION
bccad0374407 moved getopts to Scala;
wenzelm
parents: 62227
diff changeset
   283
    -l           list options
77616
29effd67d8a8 support "isabelle options -l -t TAGS";
wenzelm
parents: 77614
diff changeset
   284
    -t TAGS      restrict list to given tags (comma-separated)
62437
bccad0374407 moved getopts to Scala;
wenzelm
parents: 62227
diff changeset
   285
    -x FILE      export options to FILE in YXML format
bccad0374407 moved getopts to Scala;
wenzelm
parents: 62227
diff changeset
   286
bccad0374407 moved getopts to Scala;
wenzelm
parents: 62227
diff changeset
   287
  Report Isabelle system options, augmented by MORE_OPTIONS given as
bccad0374407 moved getopts to Scala;
wenzelm
parents: 62227
diff changeset
   288
  arguments NAME=VAL or NAME.
bccad0374407 moved getopts to Scala;
wenzelm
parents: 62227
diff changeset
   289
""",
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   290
        "b" -> (_ => build_options = true),
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   291
        "g:" -> (arg => get_option = arg),
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   292
        "l" -> (_ => list_options = true),
77616
29effd67d8a8 support "isabelle options -l -t TAGS";
wenzelm
parents: 77614
diff changeset
   293
        "t:" -> (arg => list_tags = space_explode(',', arg)),
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   294
        "x:" -> (arg => export_file = arg))
52737
7b396ef36af6 clarified meaning of options for "isabelle options";
wenzelm
parents: 52735
diff changeset
   295
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   296
      val more_options = getopts(args)
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   297
      if (get_option == "" && !list_options && export_file == "") getopts.usage()
52737
7b396ef36af6 clarified meaning of options for "isabelle options";
wenzelm
parents: 52735
diff changeset
   298
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   299
      val options = {
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   300
        val options0 = Options.init()
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   301
        val options1 =
77628
a538dab533ef clarified signature: prefer explicit types;
wenzelm
parents: 77626
diff changeset
   302
          if (build_options) options0 ++ Options.Spec.ISABELLE_BUILD_OPTIONS else options0
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   303
        more_options.foldLeft(options1)(_ + _)
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   304
      }
48693
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   305
77613
wenzelm
parents: 77612
diff changeset
   306
      if (get_option != "") {
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   307
        Output.writeln(options.check_name(get_option).value, stdout = true)
77613
wenzelm
parents: 77612
diff changeset
   308
      }
62437
bccad0374407 moved getopts to Scala;
wenzelm
parents: 62227
diff changeset
   309
77613
wenzelm
parents: 77612
diff changeset
   310
      if (export_file != "") {
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   311
        File.write(Path.explode(export_file), YXML.string_of_body(options.encode))
77613
wenzelm
parents: 77612
diff changeset
   312
      }
62437
bccad0374407 moved getopts to Scala;
wenzelm
parents: 62227
diff changeset
   313
77613
wenzelm
parents: 77612
diff changeset
   314
      if (get_option == "" && export_file == "") {
77616
29effd67d8a8 support "isabelle options -l -t TAGS";
wenzelm
parents: 77614
diff changeset
   315
        val filter: Options.Entry => Boolean =
29effd67d8a8 support "isabelle options -l -t TAGS";
wenzelm
parents: 77614
diff changeset
   316
          if (list_tags.isEmpty) (_ => true)
77617
58b7f3fb73cb tuned signature;
wenzelm
parents: 77616
diff changeset
   317
          else opt => list_tags.exists(opt.for_tag)
77616
29effd67d8a8 support "isabelle options -l -t TAGS";
wenzelm
parents: 77614
diff changeset
   318
        Output.writeln(options.print(filter = filter), stdout = true)
77613
wenzelm
parents: 77612
diff changeset
   319
      }
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   320
    })
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   321
}
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   322
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   323
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   324
final class Options private(
77605
bc1248c5d159 clarified signature;
wenzelm
parents: 77603
diff changeset
   325
  options: Map[String, Options.Entry] = Map.empty,
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74827
diff changeset
   326
  val section: String = ""
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74827
diff changeset
   327
) {
78407
b262ecc98319 more operations for independent "inline" options;
wenzelm
parents: 77668
diff changeset
   328
  def defined(name: String): Boolean = options.isDefinedAt(name)
b262ecc98319 more operations for independent "inline" options;
wenzelm
parents: 77668
diff changeset
   329
77605
bc1248c5d159 clarified signature;
wenzelm
parents: 77603
diff changeset
   330
  def iterator: Iterator[Options.Entry] = options.valuesIterator
75844
7d27944d7141 clarified signature: avoid public representation;
wenzelm
parents: 75843
diff changeset
   331
77605
bc1248c5d159 clarified signature;
wenzelm
parents: 77603
diff changeset
   332
  override def toString: String = iterator.mkString("Options(", ",", ")")
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   333
77605
bc1248c5d159 clarified signature;
wenzelm
parents: 77603
diff changeset
   334
  private def print_entry(opt: Options.Entry): String =
77614
b619d80f61fa clarified signature;
wenzelm
parents: 77613
diff changeset
   335
    if_proper(opt.public, "public ") + opt.print
54347
d5589530f3ba clarified isabelle options -l;
wenzelm
parents: 53336
diff changeset
   336
77616
29effd67d8a8 support "isabelle options -l -t TAGS";
wenzelm
parents: 77614
diff changeset
   337
  def print(filter: Options.Entry => Boolean = _ => true): String =
29effd67d8a8 support "isabelle options -l -t TAGS";
wenzelm
parents: 77614
diff changeset
   338
    cat_lines(iterator.filter(filter).toList.sortBy(_.name).map(print_entry))
48693
ceeea46bdeba "isabelle options" prints Isabelle system options;
wenzelm
parents: 48605
diff changeset
   339
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   340
  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
   341
78559
020fecb4da0c tuned signature;
wenzelm
parents: 78556
diff changeset
   342
  def spec(name: String): Options.Spec =
78916
e97fa2edf4b2 clarified signature;
wenzelm
parents: 78915
diff changeset
   343
    Options.Spec.eq(name, check_name(name).value)
78559
020fecb4da0c tuned signature;
wenzelm
parents: 78556
diff changeset
   344
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   345
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   346
  /* check */
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   347
77605
bc1248c5d159 clarified signature;
wenzelm
parents: 77603
diff changeset
   348
  def get(name: String): Option[Options.Entry] = options.get(name)
75844
7d27944d7141 clarified signature: avoid public representation;
wenzelm
parents: 75843
diff changeset
   349
77605
bc1248c5d159 clarified signature;
wenzelm
parents: 77603
diff changeset
   350
  def check_name(name: String): Options.Entry =
75844
7d27944d7141 clarified signature: avoid public representation;
wenzelm
parents: 75843
diff changeset
   351
    get(name) match {
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   352
      case Some(opt) if !opt.unknown => opt
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   353
      case _ => error("Unknown option " + quote(name))
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   354
    }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   355
77605
bc1248c5d159 clarified signature;
wenzelm
parents: 77603
diff changeset
   356
  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
   357
    val opt = check_name(name)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   358
    if (opt.typ == typ) opt
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   359
    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
   360
  }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   361
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   362
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   363
  /* basic operations */
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   364
75843
d750ead045a1 tuned signature;
wenzelm
parents: 75842
diff changeset
   365
  private def put(name: String, typ: Options.Type, value: String): Options = {
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   366
    val opt = check_type(name, typ)
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   367
    new Options(options + (name -> opt.copy(value = value)), section)
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   368
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   369
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74827
diff changeset
   370
  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
   371
    val opt = check_type(name, typ)
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   372
    parse(opt.value) match {
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   373
      case Some(x) => x
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   374
      case None =>
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   375
        error("Malformed value for option " + quote(name) +
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   376
          " : " + typ.print + " =\n" + quote(opt.value))
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   377
    }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   378
  }
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   379
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   380
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   381
  /* internal lookup and update */
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   382
75846
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   383
  val bool: Options.Access[Boolean] =
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   384
    new Options.Access[Boolean](this) {
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   385
      def apply(name: String): Boolean = get(name, Options.Bool, Value.Boolean.unapply)
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   386
      def update(name: String, x: Boolean): Options = put(name, Options.Bool, Value.Boolean(x))
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   387
    }
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   388
75846
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   389
  val int: Options.Access[Int] =
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   390
    new Options.Access[Int](this) {
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   391
      def apply(name: String): Int = get(name, Options.Int, Value.Int.unapply)
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   392
      def update(name: String, x: Int): Options = put(name, Options.Int, Value.Int(x))
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   393
    }
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   394
75846
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   395
  val real: Options.Access[Double] =
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   396
    new Options.Access[Double](this) {
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   397
      def apply(name: String): Double = get(name, Options.Real, Value.Double.unapply)
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   398
      def update(name: String, x: Double): Options = put(name, Options.Real, Value.Double(x))
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   399
    }
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   400
75846
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   401
  val string: Options.Access[String] =
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   402
    new Options.Access[String](this) {
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   403
      def apply(name: String): String = get(name, Options.String, Some(_))
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   404
      def update(name: String, x: String): Options = put(name, Options.String, x)
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   405
    }
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   406
69073
d05defa39e3d tuned signature;
wenzelm
parents: 68841
diff changeset
   407
  def seconds(name: String): Time = Time.seconds(real(name))
50207
54be125d8cdc tuned signature;
wenzelm
parents: 49954
diff changeset
   408
79655
422a6e04cf0f clarified signature: more explicit types/scopes;
wenzelm
parents: 79526
diff changeset
   409
  def threads(default: => Int = Multithreading.num_processors()): Int =
422a6e04cf0f clarified signature: more explicit types/scopes;
wenzelm
parents: 79526
diff changeset
   410
    Multithreading.max_threads(value = int("threads"), default = default)
422a6e04cf0f clarified signature: more explicit types/scopes;
wenzelm
parents: 79526
diff changeset
   411
79656
10e560f2f580 more robust default: Scala imposes explicit "threads" value on ML, both the Poly/ML RTS and Isabelle/ML;
wenzelm
parents: 79655
diff changeset
   412
  def standard_ml(): Options = int.update("threads", threads())
10e560f2f580 more robust default: Scala imposes explicit "threads" value on ML, both the Poly/ML RTS and Isabelle/ML;
wenzelm
parents: 79655
diff changeset
   413
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   414
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   415
  /* external updates */
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   416
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74827
diff changeset
   417
  private def check_value(name: String): Options = {
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   418
    val opt = check_name(name)
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   419
    opt.typ match {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   420
      case Options.Bool => bool(name); this
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   421
      case Options.Int => int(name); this
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   422
      case Options.Real => real(name); this
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   423
      case Options.String => string(name); this
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   424
      case Options.Unknown => this
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   425
    }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   426
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   427
56465
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   428
  def declare(
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   429
    public: Boolean,
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   430
    pos: Position.T,
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   431
    name: String,
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   432
    typ_name: String,
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   433
    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
   434
    standard: Option[Option[String]],
77603
236e43c8bb5b support option tags;
wenzelm
parents: 77504
diff changeset
   435
    tags: List[String],
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74827
diff changeset
   436
    description: String
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74827
diff changeset
   437
  ): Options = {
75844
7d27944d7141 clarified signature: avoid public representation;
wenzelm
parents: 75843
diff changeset
   438
    get(name) match {
56465
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   439
      case Some(other) =>
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   440
        error("Duplicate declaration of option " + quote(name) + Position.here(pos) +
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   441
          Position.here(other.pos))
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   442
      case None =>
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   443
        val typ =
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   444
          typ_name match {
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   445
            case "bool" => Options.Bool
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   446
            case "int" => Options.Int
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   447
            case "real" => Options.Real
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   448
            case "string" => Options.String
56465
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   449
            case _ =>
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   450
              error("Unknown type for option " + quote(name) + " : " + quote(typ_name) +
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   451
                Position.here(pos))
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   452
          }
74827
c1b5d6e6ff74 clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents: 74144
diff changeset
   453
        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
   454
          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
   455
            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
   456
            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
   457
              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
   458
                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
   459
            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
   460
          }
c1b5d6e6ff74 clarified system option standard values: avoid oddities like "isabelle build -o document_output" producing directories named "true";
wenzelm
parents: 74144
diff changeset
   461
        val opt =
77605
bc1248c5d159 clarified signature;
wenzelm
parents: 77603
diff changeset
   462
          Options.Entry(
77603
236e43c8bb5b support option tags;
wenzelm
parents: 77504
diff changeset
   463
            public, pos, name, typ, value, value, standard_value, tags, description, section)
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   464
        (new Options(options + (name -> opt), section)).check_value(name)
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   465
    }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   466
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   467
77626
af8ac22d97f0 clarified signature: more explicit type Options.Spec, which incorporates all variants of Options.+;
wenzelm
parents: 77625
diff changeset
   468
  def + (spec: Options.Spec): Options = {
af8ac22d97f0 clarified signature: more explicit type Options.Spec, which incorporates all variants of Options.+;
wenzelm
parents: 77625
diff changeset
   469
    val name = spec.name
78407
b262ecc98319 more operations for independent "inline" options;
wenzelm
parents: 77668
diff changeset
   470
    if (spec.permissive && !defined(name)) {
77626
af8ac22d97f0 clarified signature: more explicit type Options.Spec, which incorporates all variants of Options.+;
wenzelm
parents: 77625
diff changeset
   471
      val value = spec.value.getOrElse("")
77603
236e43c8bb5b support option tags;
wenzelm
parents: 77504
diff changeset
   472
      val opt =
77605
bc1248c5d159 clarified signature;
wenzelm
parents: 77603
diff changeset
   473
        Options.Entry(false, Position.none, name, Options.Unknown, value, value, None, Nil, "", "")
56465
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   474
      new Options(options + (name -> opt), section)
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   475
    }
77626
af8ac22d97f0 clarified signature: more explicit type Options.Spec, which incorporates all variants of Options.+;
wenzelm
parents: 77625
diff changeset
   476
    else {
af8ac22d97f0 clarified signature: more explicit type Options.Spec, which incorporates all variants of Options.+;
wenzelm
parents: 77625
diff changeset
   477
      val opt = check_name(name)
af8ac22d97f0 clarified signature: more explicit type Options.Spec, which incorporates all variants of Options.+;
wenzelm
parents: 77625
diff changeset
   478
      def put(value: String): Options =
af8ac22d97f0 clarified signature: more explicit type Options.Spec, which incorporates all variants of Options.+;
wenzelm
parents: 77625
diff changeset
   479
        (new Options(options + (name -> opt.copy(value = value)), section)).check_value(name)
af8ac22d97f0 clarified signature: more explicit type Options.Spec, which incorporates all variants of Options.+;
wenzelm
parents: 77625
diff changeset
   480
      spec.value orElse opt.standard_value match {
af8ac22d97f0 clarified signature: more explicit type Options.Spec, which incorporates all variants of Options.+;
wenzelm
parents: 77625
diff changeset
   481
        case Some(value) => put(value)
af8ac22d97f0 clarified signature: more explicit type Options.Spec, which incorporates all variants of Options.+;
wenzelm
parents: 77625
diff changeset
   482
        case None if opt.typ == Options.Bool => put("true")
af8ac22d97f0 clarified signature: more explicit type Options.Spec, which incorporates all variants of Options.+;
wenzelm
parents: 77625
diff changeset
   483
        case None => error("Missing value for option " + quote(name) + " : " + opt.typ.print)
af8ac22d97f0 clarified signature: more explicit type Options.Spec, which incorporates all variants of Options.+;
wenzelm
parents: 77625
diff changeset
   484
      }
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   485
    }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   486
  }
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 48369
diff changeset
   487
77626
af8ac22d97f0 clarified signature: more explicit type Options.Spec, which incorporates all variants of Options.+;
wenzelm
parents: 77625
diff changeset
   488
  def + (s: String): Options = this + Options.Spec.make(s)
48456
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   489
77626
af8ac22d97f0 clarified signature: more explicit type Options.Spec, which incorporates all variants of Options.+;
wenzelm
parents: 77625
diff changeset
   490
  def ++ (specs: List[Options.Spec]): Options = specs.foldLeft(this)(_ + _)
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   491
48456
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   492
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   493
  /* sections */
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   494
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   495
  def set_section(new_section: String): Options =
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   496
    new Options(options, new_section)
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   497
77605
bc1248c5d159 clarified signature;
wenzelm
parents: 77603
diff changeset
   498
  def sections: List[(String, List[Options.Entry])] =
49270
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   499
    options.groupBy(_._2.section).toList.map({ case (a, opts) => (a, opts.toList.map(_._2)) })
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   500
e5d162d15867 some support to organize options in sections;
wenzelm
parents: 49247
diff changeset
   501
48456
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   502
  /* encode */
d8ff14f44a40 added ML version of stand-alone options, with XML.encode/decode operations (unidirectional from Scala to ML);
wenzelm
parents: 48421
diff changeset
   503
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74827
diff changeset
   504
  def encode: XML.Body = {
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   505
    val opts =
56465
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   506
      for ((_, opt) <- options.toList; if !opt.unknown)
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   507
        yield (opt.pos, (opt.name, (opt.typ.print, opt.value)))
48860
56ec76f769c0 retain unknown options within preferences;
wenzelm
parents: 48807
diff changeset
   508
56465
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   509
    import XML.Encode.{string => string_, _}
6ad693903e22 more positions and markup;
wenzelm
parents: 55618
diff changeset
   510
    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
   511
  }
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   512
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   513
77622
f458547b4f0f clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents: 77621
diff changeset
   514
  /* changed options */
f458547b4f0f clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents: 77621
diff changeset
   515
f458547b4f0f clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents: 77621
diff changeset
   516
  def changed(
f458547b4f0f clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents: 77621
diff changeset
   517
    defaults: Options = Options.init0(),
f458547b4f0f clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents: 77621
diff changeset
   518
    filter: Options.Entry => Boolean = _ => true
77623
157ad1f976d2 clarified signature: prefer static types;
wenzelm
parents: 77622
diff changeset
   519
  ): List[Options.Change] = {
77622
f458547b4f0f clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents: 77621
diff changeset
   520
    List.from(
f458547b4f0f clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents: 77621
diff changeset
   521
      for {
f458547b4f0f clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents: 77621
diff changeset
   522
        (name, opt2) <- options.iterator
f458547b4f0f clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents: 77621
diff changeset
   523
        opt1 = defaults.get(name)
f458547b4f0f clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents: 77621
diff changeset
   524
        if (opt1.isEmpty || opt1.get.value != opt2.value) && filter(opt2)
77668
5cb7fd36223b proper sorting of result (amending f458547b4f0f);
wenzelm
parents: 77628
diff changeset
   525
      } yield Options.Change(name, opt2.value, opt1.isEmpty)).sortBy(_.name)
77622
f458547b4f0f clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents: 77621
diff changeset
   526
  }
f458547b4f0f clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents: 77621
diff changeset
   527
f458547b4f0f clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents: 77621
diff changeset
   528
77608
eaa6b47fab2c tuned comments;
wenzelm
parents: 77607
diff changeset
   529
  /* preferences */
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   530
77603
236e43c8bb5b support option tags;
wenzelm
parents: 77504
diff changeset
   531
  def make_prefs(
77609
a45cce93529c tuned signature;
wenzelm
parents: 77608
diff changeset
   532
    defaults: Options = Options.init0(),
77605
bc1248c5d159 clarified signature;
wenzelm
parents: 77603
diff changeset
   533
    filter: Options.Entry => Boolean = _ => true
77622
f458547b4f0f clarified signature (again, see also 8c64e51d9dde and 268bf61631ec);
wenzelm
parents: 77621
diff changeset
   534
  ): String = changed(defaults = defaults, filter = filter).map(_.print_prefs).mkString
77374
268bf61631ec more robust options in "prefs" format: avoid odd control character;
wenzelm
parents: 77366
diff changeset
   535
77609
a45cce93529c tuned signature;
wenzelm
parents: 77608
diff changeset
   536
  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
   537
    val prefs = make_prefs(defaults = defaults)
72375
e48d93811ed7 clarified signature;
wenzelm
parents: 71601
diff changeset
   538
    Isabelle_System.make_directory(file.dir)
67845
46fa8c2c142a more flexible preferences: avoid hardwired file;
wenzelm
parents: 67178
diff changeset
   539
    File.write_backup(file, "(* generated by Isabelle " + Date.now() + " *)\n\n" + prefs)
48807
fde8c3d84ff5 some support for persistent user preferences;
wenzelm
parents: 48795
diff changeset
   540
  }
48365
d88aefda01c4 basic support for stand-alone options with external string representation;
wenzelm
parents:
diff changeset
   541
}
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   542
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   543
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74827
diff changeset
   544
class Options_Variable(init_options: Options) {
75846
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   545
  private var _options = init_options
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   546
75846
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   547
  def value: Options = synchronized { _options }
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   548
  def change(f: Options => Options): Unit = synchronized { _options = f(_options) }
78916
e97fa2edf4b2 clarified signature;
wenzelm
parents: 78915
diff changeset
   549
  def += (name: String, x: String): Unit = change(options => options + Options.Spec.eq(name, x))
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 48992
diff changeset
   550
75846
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   551
  val bool: Options.Access_Variable[Boolean] =
75847
93436389db1c clarified signature: more explicit types;
wenzelm
parents: 75846
diff changeset
   552
    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
   553
75846
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   554
  val int: Options.Access_Variable[Int] =
75847
93436389db1c clarified signature: more explicit types;
wenzelm
parents: 75846
diff changeset
   555
    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
   556
75846
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   557
  val real: Options.Access_Variable[Double] =
75847
93436389db1c clarified signature: more explicit types;
wenzelm
parents: 75846
diff changeset
   558
    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
   559
75846
d9926523855e clarified signature --- avoid dependent types;
wenzelm
parents: 75845
diff changeset
   560
  val string: Options.Access_Variable[String] =
75847
93436389db1c clarified signature: more explicit types;
wenzelm
parents: 75846
diff changeset
   561
    new Options.Access_Variable[String](this, _.string)
50207
54be125d8cdc tuned signature;
wenzelm
parents: 49954
diff changeset
   562
69073
d05defa39e3d tuned signature;
wenzelm
parents: 68841
diff changeset
   563
  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
   564
}