src/Tools/jEdit/src/jedit_sessions.scala
author wenzelm
Tue Nov 07 15:45:33 2017 +0100 (17 months ago)
changeset 67023 e27e05d6f2a7
parent 66989 25665e7775b7
child 67024 72d37a2e9cca
permissions -rw-r--r--
tuned signature;
     1 /*  Title:      Tools/jEdit/src/jedit_sessions.scala
     2     Author:     Makarius
     3 
     4 Isabelle/jEdit session information, based on implicit process environment
     5 and explicit options.
     6 */
     7 
     8 package isabelle.jedit
     9 
    10 
    11 import isabelle._
    12 
    13 import scala.swing.ComboBox
    14 import scala.swing.event.SelectionChanged
    15 
    16 
    17 object JEdit_Sessions
    18 {
    19   /* session options */
    20 
    21   def session_options(options: Options): Options =
    22     Isabelle_System.getenv("JEDIT_ML_PROCESS_POLICY") match {
    23       case "" => options
    24       case s => options.string.update("ML_process_policy", s)
    25     }
    26 
    27   def session_dirs(): List[Path] =
    28     Path.split(Isabelle_System.getenv("JEDIT_SESSION_DIRS"))
    29 
    30   def session_build_mode(): String = Isabelle_System.getenv("JEDIT_BUILD_MODE")
    31 
    32 
    33   /* raw logic info */
    34 
    35   private val jedit_logic_option = "jedit_logic"
    36 
    37   def logic_name(options: Options): String =
    38     Isabelle_System.default_logic(
    39       Isabelle_System.getenv("JEDIT_LOGIC"),
    40       options.string(jedit_logic_option))
    41 
    42   def logic_ancestor: Option[String] = proper_string(Isabelle_System.getenv("JEDIT_LOGIC_ANCESTOR"))
    43   def logic_focus: Boolean = Isabelle_System.getenv("JEDIT_LOGIC_FOCUS") == "true"
    44   def logic_base: Boolean = Isabelle_System.getenv("JEDIT_LOGIC_BASE") == "true"
    45   def logic_parent: Boolean = Isabelle_System.getenv("JEDIT_LOGIC_PARENT") == "true"
    46 
    47   def logic_info(options: Options): Option[Sessions.Info] =
    48     try { Sessions.load(session_options(options), dirs = session_dirs()).get(logic_name(options)) }
    49     catch { case ERROR(_) => None }
    50 
    51   def logic_root(options: Options): Position.T =
    52     if (Isabelle_System.getenv("JEDIT_LOGIC_ROOT") == "true") {
    53       logic_info(options).map(_.pos) getOrElse Position.none
    54     }
    55     else Position.none
    56 
    57 
    58   /* logic selector */
    59 
    60   private class Logic_Entry(val name: String, val description: String)
    61   {
    62     override def toString: String = description
    63   }
    64 
    65   def logic_selector(options: Options_Variable, autosave: Boolean): Option_Component =
    66   {
    67     GUI_Thread.require {}
    68 
    69     val session_list =
    70     {
    71       val sessions_structure = Sessions.load(options.value, dirs = session_dirs())
    72       val (main_sessions, other_sessions) =
    73         sessions_structure.imports_topological_order.
    74           partition(name => sessions_structure(name).groups.contains("main"))
    75       main_sessions.sorted ::: other_sessions.sorted
    76     }
    77 
    78     val entries =
    79       new Logic_Entry("", "default (" + logic_name(options.value) + ")") ::
    80         session_list.map(name => new Logic_Entry(name, name))
    81 
    82     val component = new ComboBox(entries) with Option_Component {
    83       name = jedit_logic_option
    84       val title = "Logic"
    85       def load: Unit =
    86       {
    87         val logic = options.string(jedit_logic_option)
    88         entries.find(_.name == logic) match {
    89           case Some(entry) => selection.item = entry
    90           case None =>
    91         }
    92       }
    93       def save: Unit = options.string(jedit_logic_option) = selection.item.name
    94     }
    95 
    96     component.load()
    97     if (autosave) {
    98       component.listenTo(component.selection)
    99       component.reactions += { case SelectionChanged(_) => component.save() }
   100     }
   101     component.tooltip = "Logic session name (change requires restart)"
   102     component
   103   }
   104 
   105 
   106   /* session build process */
   107 
   108   def session_base_info(options: Options): Sessions.Base_Info =
   109     Sessions.base_info(options,
   110       dirs = JEdit_Sessions.session_dirs(),
   111       session =
   112         if (logic_parent) logic_info(options).flatMap(_.parent) getOrElse logic_name(options)
   113         else logic_name(options),
   114       ancestor_session = logic_ancestor,
   115       all_known = !logic_focus,
   116       focus_session = logic_focus,
   117       required_session = logic_base)
   118 
   119   def session_build(
   120     options: Options, progress: Progress = No_Progress, no_build: Boolean = false): Int =
   121   {
   122     val mode = session_build_mode()
   123 
   124     Build.build(options = session_options(options), progress = progress,
   125       build_heap = true, no_build = no_build, system_mode = mode == "" || mode == "system",
   126       dirs = session_dirs(), infos = PIDE.resources.session_base_info.infos,
   127       sessions = List(PIDE.resources.session_name)).rc
   128   }
   129 
   130   def session_start(options: Options)
   131   {
   132     Isabelle_Process.start(PIDE.session, session_options(options),
   133       sessions = Some(PIDE.resources.session_base_info.sessions),
   134       logic = PIDE.resources.session_name,
   135       store = Sessions.store(session_build_mode() == "system"),
   136       modes =
   137         (space_explode(',', options.string("jedit_print_mode")) :::
   138          space_explode(',', Isabelle_System.getenv("JEDIT_PRINT_MODE"))).reverse,
   139       phase_changed = PIDE.plugin.session_phase_changed)
   140   }
   141 }